-
Notifications
You must be signed in to change notification settings - Fork 58
/
CodeGen.h
152 lines (125 loc) · 4.37 KB
/
CodeGen.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef __CODEGEN_H__
#define __CODEGEN_H__
#include <llvm/IR/Value.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <json/json.h>
#include <stack>
#include <vector>
#include <memory>
#include <string>
#include <map>
#include "ASTNodes.h"
#include "grammar.hpp"
#include "TypeSystem.h"
using namespace llvm;
using std::unique_ptr;
using std::string;
using SymTable = std::map<string, Value*>;
class CodeGenBlock{
public:
BasicBlock * block;
Value * returnValue;
std::map<string, Value*> locals;
std::map<string, shared_ptr<NIdentifier>> types; // type name string of vars
std::map<string, bool> isFuncArg;
std::map<string, std::vector<uint64_t>> arraySizes;
};
class CodeGenContext{
private:
std::vector<CodeGenBlock*> blockStack;
public:
LLVMContext llvmContext;
IRBuilder<> builder;
unique_ptr<Module> theModule;
SymTable globalVars;
TypeSystem typeSystem;
CodeGenContext(): builder(llvmContext), typeSystem(llvmContext){
theModule = unique_ptr<Module>(new Module("main", this->llvmContext));
}
Value* getSymbolValue(string name) const{
for(auto it=blockStack.rbegin(); it!=blockStack.rend(); it++){
// cout << "(*it)->locals[" << name << "] = " << (*it)->locals[name] << endl;
if( (*it)->locals.find(name) != (*it)->locals.end() ){
return (*it)->locals[name];
}
}
return nullptr;
}
shared_ptr<NIdentifier> getSymbolType(string name) const{
for(auto it=blockStack.rbegin(); it!=blockStack.rend(); it++){
// cout << "(*it)->locals[" << name << "] = " << (*it)->locals[name] << endl;
if( (*it)->types.find(name) != (*it)->types.end() ){
return (*it)->types[name];
}
}
return nullptr;
}
bool isFuncArg(string name) const{
for(auto it=blockStack.rbegin(); it!=blockStack.rend(); it++){
if( (*it)->isFuncArg.find(name) != (*it)->isFuncArg.end() ){
return (*it)->isFuncArg[name];
}
}
return false;
}
void setSymbolValue(string name, Value* value){
blockStack.back()->locals[name] = value;
}
void setSymbolType(string name, shared_ptr<NIdentifier> value){
blockStack.back()->types[name] = value;
}
void setFuncArg(string name, bool value){
cout << "Set " << name << " as func arg" << endl;
blockStack.back()->isFuncArg[name] = value;
}
BasicBlock* currentBlock() const{
return blockStack.back()->block;
}
void pushBlock(BasicBlock * block){
CodeGenBlock * codeGenBlock = new CodeGenBlock();
codeGenBlock->block = block;
codeGenBlock->returnValue = nullptr;
blockStack.push_back(codeGenBlock);
}
void popBlock(){
CodeGenBlock * codeGenBlock = blockStack.back();
blockStack.pop_back();
delete codeGenBlock;
}
void setCurrentReturnValue(Value* value){
blockStack.back()->returnValue = value;
}
Value* getCurrentReturnValue(){
return blockStack.back()->returnValue;
}
void setArraySize(string name, std::vector<uint64_t> value){
cout << "setArraySize: " << name << ": " << value.size() << endl;
blockStack.back()->arraySizes[name] = value;
// cout << "blockStack.back()->arraySizes.size()" << blockStack.back()->arraySizes.size() << endl;
}
std::vector<uint64_t> getArraySize(string name){
for(auto it=blockStack.rbegin(); it!=blockStack.rend(); it++){
if( (*it)->arraySizes.find(name) != (*it)->arraySizes.end() ){
return (*it)->arraySizes[name];
}
}
return blockStack.back()->arraySizes[name];
}
void PrintSymTable() const{
cout << "======= Print Symbol Table ========" << endl;
string prefix = "";
for(auto it=blockStack.begin(); it!=blockStack.end(); it++){
for(auto it2=(*it)->locals.begin(); it2!=(*it)->locals.end(); it2++){
cout << prefix << it2->first << " = " << it2->second << ": " << this->getSymbolType(it2->first) << endl;
}
prefix += "\t";
}
cout << "===================================" << endl;
}
void generateCode(NBlock& );
};
Value* LogErrorV(const char* str);
Value* LogErrorV(string str);
#endif