This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.h
125 lines (101 loc) · 3.06 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
//
// Created by 徐维亚 on 05/06/2018.
//
#ifndef SPLC_CODEGEN_H
#define SPLC_CODEGEN_H
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <utility>
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/IRBuilder.h>
#include "AST.h"
#include "ConstTable.h"
static llvm::LLVMContext MyContext;
class Node;
class FunctionHead;
class FuncVars {
public:
std::vector<llvm::Value *> storePlace;
std::vector<int> position;
};
class CodeGenBlock {
public:
llvm::BasicBlock *block;
CodeGenBlock *preBlock;
llvm::Function *function;
std::map<std::string, llvm::Value *> locals;
std::map<std::string, TypeDecl *> varTypes;
std::map<std::string, TypeDecl *> types;
std::set<std::string> references;
std::string outputFilename;
explicit CodeGenBlock(llvm::BasicBlock *block, CodeGenBlock *preBlock) : block(block), preBlock(preBlock) {}
};
class CodeGenContext {
public:
std::stack<CodeGenBlock *> blocks;
llvm::Module *module;
std::map<std::string, FuncVars> funcVars;
ConstTable constTable;
bool isGlobal;
CodeGenContext() : module(new llvm::Module("main", MyContext)), print(nullptr), isGlobal(true) {}
~CodeGenContext() {
delete module;
}
void pushBlock(llvm::BasicBlock *block) {
CodeGenBlock *top = nullptr;
if (!blocks.empty()) {
top = blocks.top();
}
blocks.push(new CodeGenBlock(block, top));
}
void popBlock() {
CodeGenBlock *top = blocks.top();
blocks.pop();
delete top;
}
CodeGenBlock * isReferece(const std::string & var) {
CodeGenBlock * p = blocks.top();
while (p) {
if(p->references.find(var)!= p->references.end())
return p;
p = p->preBlock;
}
return nullptr;
}
CodeGenBlock * isType(const std::string & t) {
CodeGenBlock * p = blocks.top();
while (p) {
if(p->types.find(t)!= p->types.end())
return p;
p = p->preBlock;
}
return nullptr;
}
CodeGenBlock * isVariable(const std::string & v) {
CodeGenBlock * p = blocks.top();
while (p) {
if(p->locals.find(v)!= p->locals.end())
return p;
p = p->preBlock;
}
return nullptr;
}
std::map<std::string, llvm::Value *> &local() { return blocks.top()->locals; };
std::map<std::string, TypeDecl *> &varType() { return blocks.top()->varTypes; };
std::set<std::string> &reference() { return blocks.top()->references; };
std::map<std::string, TypeDecl *> &type() { return blocks.top()->types; };
llvm::BasicBlock *currentBlock() { return blocks.top()->block; }
void generateCode(Node *root, const std::string &outputFilename);
void outputCode(const char *filename, bool mips);
void CreateRead();
void CreatePrint();
llvm::Function *print;
llvm::Function *read;
};
#endif //SPLC_CODEGEN_H