1
2
3
4
5
6 #ifndef CMAJOR_CMSXI_CONTEXT_INCLUDED
7 #define CMAJOR_CMSXI_CONTEXT_INCLUDED
8 #include <cmajor/cmsxi/Data.hpp>
9 #include <cmajor/cmsxi/Metadata.hpp>
10
11 namespace cmsxi {
12
13 class Function;
14 class BasicBlock;
15 class Instruction;
16
17 class Context
18 {
19 public:
20 Context();
21 TypeRepository& GetTypeRepository() { return typeRepository; }
22 DataRepository& GetDataRepository() { return dataRepository; }
23 Metadata& GetMetadata() { return metadata; }
24 Type* GetVoidType() { return typeRepository.GetVoidType(); }
25 Type* GetBoolType() { return typeRepository.GetBoolType(); }
26 Type* GetSByteType() { return typeRepository.GetSByteType(); }
27 Type* GetByteType() { return typeRepository.GetByteType(); }
28 Type* GetShortType() { return typeRepository.GetShortType(); }
29 Type* GetUShortType() { return typeRepository.GetUShortType(); }
30 Type* GetIntType() { return typeRepository.GetIntType(); }
31 Type* GetUIntType() { return typeRepository.GetUIntType(); }
32 Type* GetLongType() { return typeRepository.GetLongType(); }
33 Type* GetULongType() { return typeRepository.GetULongType(); }
34 Type* GetFloatType() { return typeRepository.GetFloatType(); }
35 Type* GetDoubleType() { return typeRepository.GetDoubleType(); }
36 Type* GetPtrType(Type* baseType) { return typeRepository.GetPtrType(baseType); }
37 Type* GetStructureType(const std::std::vector<Type*>&memberTypes){returntypeRepository.GetStructureType(memberTypes);}
38 Type* CreateStructureType() { return typeRepository.CreateStructureType(); }
39 Type* GetArrayType(Type* elementType, uint64_t size) { return typeRepository.GetArrayType(elementType, size); }
40 Type* GetFunctionType(Type* returnType, const std::std::vector<Type*>¶mTypes){returntypeRepository.GetFunctionType(returnType, paramTypes);}
41 ConstantValue* GetDefaultBoolValue() { return GetBoolType()->DefaultValue(); }
42 ConstantValue* GetDefaultSByteValue() { return GetSByteType()->DefaultValue(); }
43 ConstantValue* GetDefaultByteValue() { return GetByteType()->DefaultValue(); }
44 ConstantValue* GetDefaultShortValue() { return GetShortType()->DefaultValue(); }
45 ConstantValue* GetDefaultUShortValue() { return GetUShortType()->DefaultValue(); }
46 ConstantValue* GetDefaultIntValue() { return GetIntType()->DefaultValue(); }
47 ConstantValue* GetDefaultUIntValue() { return GetUIntType()->DefaultValue(); }
48 ConstantValue* GetDefaultLongValue() { return GetLongType()->DefaultValue(); }
49 ConstantValue* GetDefaultULongValue() { return GetULongType()->DefaultValue(); }
50 ConstantValue* GetDefaultFloatValue() { return GetFloatType()->DefaultValue(); }
51 ConstantValue* GetDefaultDoubleValue() { return GetDoubleType()->DefaultValue(); }
52 ConstantValue* GetNullValue(PtrType* ptrType) { return ptrType->DefaultValue(); }
53 ConstantValue* GetBoolValue(bool value);
54 ConstantValue* GetSByteValue(int8_t value);
55 ConstantValue* GetByteValue(uint8_t value);
56 ConstantValue* GetShortValue(int16_t value);
57 ConstantValue* GetUShortValue(uint16_t value);
58 ConstantValue* GetIntValue(int32_t value);
59 ConstantValue* GetUIntValue(uint32_t value);
60 ConstantValue* GetLongValue(int64_t value);
61 ConstantValue* GetULongValue(uint64_t value);
62 ConstantValue* GetFloatValue(float value);
63 ConstantValue* GetDoubleValue(double value);
64 ArrayValue* GetArrayValue(Type* arrayType, const std::std::vector<ConstantValue*>&elements, conststd::string&prefix);
65 StructureValue* GetStructureValue(Type* structureType, const std::std::vector<ConstantValue*>&members);
66 StringValue* GetStringValue(Type* stringType, const std::string& value);
67 ConversionValue* GetConversionValue(Type* type, ConstantValue* from);
68 ClsIdValue* GetClsIdValue(const std::string& typeId);
69 void AddValue(Value* value);
70 void SetCurrentBasicBlock(BasicBlock* bb) { currentBasicBlock = bb; }
71 BasicBlock* GetCurrentBasicBlock() const { return currentBasicBlock; }
72 Instruction* CreateNot(Value* arg);
73 Instruction* CreateNeg(Value* arg);
74 Instruction* CreateAdd(Value* left, Value* right);
75 Instruction* CreateSub(Value* left, Value* right);
76 Instruction* CreateMul(Value* left, Value* right);
77 Instruction* CreateDiv(Value* left, Value* right);
78 Instruction* CreateMod(Value* left, Value* right);
79 Instruction* CreateAnd(Value* left, Value* right);
80 Instruction* CreateOr(Value* left, Value* right);
81 Instruction* CreateXor(Value* left, Value* right);
82 Instruction* CreateShl(Value* left, Value* right);
83 Instruction* CreateShr(Value* left, Value* right);
84 Instruction* CreateEqual(Value* left, Value* right);
85 Instruction* CreateLess(Value* left, Value* right);
86 Instruction* CreateSignExtend(Value* arg, Type* destType);
87 Instruction* CreateZeroExtend(Value* arg, Type* destType);
88 Instruction* CreateTruncate(Value* arg, Type* destType);
89 Instruction* CreateBitCast(Value* arg, Type* destType);
90 Instruction* CreateIntToFloat(Value* arg, Type* destType);
91 Instruction* CreateFloatToInt(Value* arg, Type* destType);
92 Instruction* CreateIntToPtr(Value* arg, Type* destType);
93 Instruction* CreatePtrToInt(Value* arg, Type* destType);
94 Instruction* CreateParam(Type* type);
95 Instruction* CreateLocal(Type* type);
96 Instruction* CreateLoad(Value* ptr);
97 Instruction* CreateStore(Value* value, Value* ptr);
98 Instruction* CreateArg(Value* arg);
99 Instruction* CreateElemAddr(Value* ptr, Value* index);
100 Instruction* CreatePtrOffset(Value* ptr, Value* offset);
101 Instruction* CreatePtrDiff(Value* leftPtr, Value* rightPtr);
102 Instruction* CreateCall(Value* function);
103 Instruction* CreateRet(Value* value);
104 Instruction* CreateJump(BasicBlock* dest);
105 Instruction* CreateBranch(Value* cond, BasicBlock* trueDest, BasicBlock* falseDest);
106 Instruction* CreateSwitch(Value* cond, BasicBlock* defaultDest);
107 Instruction* CreateTrap(const std::std::vector<Value*>&args);
108 Instruction* CreateNop();
109 Instruction* CreateSave();
110 GlobalVariable* GetOrInsertGlobal(const std::string& name, Type* type);
111 GlobalVariable* CreateGlobalStringPtr(const std::string& stringValue);
112 void SetCompileUnitId(const std::string& compileUnitId_);
113 MDBool* CreateMDBool(bool value) { return metadata.CreateMDBool(value); }
114 MDLong* CreateMDLong(int64_t value) { return metadata.CreateMDLong(value); }
115 MDString* CreateMDString(const std::string& value) { return metadata.CreateMDString(value); }
116 MDStructRef* CreateMDStructRef(int id) { return metadata.CreateMDStructRef(id); }
117 MDStruct* CreateMDStruct() { return metadata.CreateMDStruct(); }
118 MDBasicBlockRef* CreateMDBasicBlockRef(void* bb) { return metadata.CreateMDBasicBlockRef(bb); }
119 void AddMDStructItem(MDStruct* mdStruct, const std::string& fieldName, MDItem* item);
120 void SetCurrentLineNumber(int lineNumber);
121 void AddLineInfo(Instruction* inst);
122 MDStructRef* GetMDStructRefForSourceFile(const std::string& sourceFileName);
123 void SetMetadataRef(Instruction* inst, MDStructRef* metadataRef);
124 private:
125 TypeRepository typeRepository;
126 DataRepository dataRepository;
127 Metadata metadata;
128 std::vector<std::std::unique_ptr<Value>>values;
129 Function* currentFunction;
130 BasicBlock* currentBasicBlock;
131 int currentLineNumber;
132 std::unordered_map<int, MDStructRef*> lineNumberInfoMap;
133 std::unordered_map<std::string, MDStructRef*> sourceFileMap;
134 };
135
136 }
137
138 #endif // CMAJOR_CMSXI_CONTEXT_INCLUDED