-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathTypeSystem.h
116 lines (90 loc) · 2.55 KB
/
TypeSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Created by cs on 2017/5/31.
//
#ifndef TINYCOMPILER_TYPESYSTEM_H
#define TINYCOMPILER_TYPESYSTEM_H
#include <llvm/IR/Type.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <string>
#include <map>
#include <vector>
#include "ASTNodes.h"
using std::string;
using namespace llvm;
//
//struct VarType {
// string name;
//
// VarType(string name): name(name){}
//
// virtual Value* getDefaultValue(LLVMContext & context) const;
//
// virtual bool isArray() const{
// return false;
// }
//
// virtual bool isStruct() const{
// return false;
// }
//
//};
//
//struct VarArrayType: VarType{
//
// NExpression & size;
//
// VarArrayType(string name, NExpression& size)
// :VarType(name), size(size){
//
// }
//
// Value *getDefaultValue(LLVMContext &context) const override;
//
//
// bool isArray() const override{
// return true;
// }
//
//};
//
//struct VarStructType: VarType{
// VarStructType(string name): VarType(name){}
//
// Value *getDefaultValue(LLVMContext &context) const override ;
//
// bool isStruct() const override{
// return true;
// }
//};
using TypeNamePair = std::pair<std::string, std::string>;
class TypeSystem{
private:
LLVMContext& llvmContext;
std::map<string, std::vector<TypeNamePair>> _structMembers;
std::map<string, llvm::StructType*> _structTypes;
std::map<Type*, std::map<Type*, CastInst::CastOps>> _castTable;
void addCast(Type* from, Type* to, CastInst::CastOps op);
public:
Type* floatTy = Type::getFloatTy(llvmContext);
Type* intTy = Type::getInt32Ty(llvmContext);
Type* charTy = Type::getInt8Ty(llvmContext);
Type* doubleTy = Type::getDoubleTy(llvmContext);
Type* stringTy = Type::getInt8PtrTy(llvmContext);
Type* voidTy = Type::getVoidTy(llvmContext);
Type* boolTy = Type::getInt1Ty(llvmContext);
TypeSystem(LLVMContext& context);
void addStructType(string structName, llvm::StructType*);
void addStructMember(string structName, string memType, string memName);
long getStructMemberIndex(string structName, string memberName);
Type* getVarType(const NIdentifier& type) ;
Type* getVarType(string typeStr) ;
Value* getDefaultValue(string typeStr, LLVMContext &context) ;
Value* cast(Value* value, Type* type, BasicBlock* block) ;
bool isStruct(string typeStr) const;
static string llvmTypeToStr(Value* value) ;
static string llvmTypeToStr(Type* type) ;
};
#endif //TINYCOMPILER_TYPESYSTEM_H