source: icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h @ 5281

Last change on this file since 5281 was 5281, checked in by cameron, 2 years ago

Stdin kernel: initial check-in

File size: 4.1 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#ifndef CBUILDER_H
6#define CBUILDER_H
7
8#include <string>
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Constants.h>
11namespace llvm { class Function; }
12namespace llvm { class IntegerType; }
13namespace llvm { class Module; }
14namespace llvm { class PointerType; }
15namespace llvm { class Type; }
16namespace llvm { class Value; }
17
18class CBuilder : public llvm::IRBuilder<> {
19   
20public:
21   
22    CBuilder(llvm::Module * m, unsigned GeneralRegisterWidthInBits, unsigned CacheLineAlignmentInBytes = 64);
23   
24    virtual ~CBuilder() {}
25
26    llvm::Module * getModule() const {
27        return mMod;
28    }
29   
30    void setModule(llvm::Module * m)  {
31        mMod = m;
32    }
33   
34    llvm::Value * CreateMalloc(llvm::Type * type, llvm::Value * size);
35   
36    llvm::Value * CreateAlignedMalloc(llvm::Type * type, llvm::Value * size, const unsigned alignment);
37   
38    void CreateFree(llvm::Value * const ptr);
39   
40    void CreateAlignedFree(llvm::Value * const ptr, const bool ptrMayBeNull = false);
41   
42    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
43   
44    void CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1);
45
46    inline llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
47        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
48        instr->setAlignment(getCacheAlignment());
49        return instr;
50    }
51   
52    // stdio.h functions
53    //
54    //  Create a call to:  FILE *fopen(const char *filename, const char *mode);
55    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
56    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
57    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
58    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
59    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
60    //  Create a call to:  int fclose ( FILE * stream );
61    llvm::Value * CreateFCloseCall(llvm::Value * stream);
62   
63    llvm::Function * GetPrintf();
64
65    //  Create calls to unistd.h functions.
66    //
67    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
68    llvm::Value * CreateWriteCall(llvm::Value * fildes, llvm::Value * buf, llvm::Value * nbyte);
69    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
70    llvm::Value * CreateReadCall(llvm::Value * fildes, llvm::Value * buf, llvm::Value * nbyte);
71
72   
73    //  Posix thread (pthread.h) functions.
74    //
75    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
76    //                    void *(*start_routine)(void*), void *arg);
77    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
78   
79    //  Create a call to:  void pthread_exit(void *value_ptr);
80    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
81   
82    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
83    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
84   
85    void CallPrintInt(const std::string & name, llvm::Value * const value);
86   
87    inline llvm::IntegerType * getSizeTy() const {
88        return mSizeType;
89    }
90   
91    inline llvm::ConstantInt * getSize(const size_t value) {
92        return llvm::ConstantInt::get(getSizeTy(), value);
93    }
94   
95    llvm::PointerType * getVoidPtrTy() const;
96    llvm::PointerType * getFILEptrTy();
97   
98    inline unsigned getCacheAlignment() const {
99        return mCacheLineAlignment;
100    }
101   
102    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
103
104    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
105   
106protected:
107    llvm::Module *      mMod;
108    unsigned            mCacheLineAlignment;
109    llvm::IntegerType * mSizeType;
110    llvm::StructType *  mFILEtype;
111};
112
113#endif
Note: See TracBrowser for help on using the repository browser.