-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.cpp
More file actions
114 lines (104 loc) · 3.88 KB
/
Copy pathUnit.cpp
File metadata and controls
114 lines (104 loc) · 3.88 KB
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
#include "Unit.h"
#include <algorithm>
#include <stack>
#include <string>
#include "Ast.h"
#include "SymbolTable.h"
#include "Type.h"
extern FILE* yyout;
void Unit::insertFunc(Function* f) {
func_list.push_back(f);
}
void Unit::removeFunc(Function* func) {
func_list.erase(std::find(func_list.begin(), func_list.end(), func));
}
void Unit::insertGlobal(SymbolEntry* se) {
global_decl_list.push_back(se);
}
void Unit::insertDeclare(SymbolEntry* se) {
auto it = std::find(declare_list.begin(), declare_list.end(), se);
if (it == declare_list.end()) {
declare_list.push_back(se);
}
}
void Unit::output() const {
for (auto se : global_decl_list) {
if (se->getType()->isInt())
fprintf(yyout, "%s = global %s %d, align 4\n", se->toStr().c_str(),
se->getType()->toStr().c_str(),
((IdentifierSymbolEntry*)se)->getValue());
else if (se->getType()->isArray()) {
ArrayType* type = (ArrayType*)(se->getType());
// int size = type->getSize() / TypeSystem::intType->getSize();
int* val = ((IdentifierSymbolEntry*)se)->getArrayValue();
int i = 0;
fprintf(yyout, "%s = global ", se->toStr().c_str());
if (((IdentifierSymbolEntry*)se)->isAllZero()) {
fprintf(yyout, "%s zeroinitializer", type->toStr().c_str());
} else {
std::stack<ArrayType*> stk;
std::stack<int> stk1;
stk.push(type);
stk1.push(0);
ArrayType* temp;
while (!stk.empty()) {
temp = stk.top();
if (temp->getElementType()->isInt()) {
fprintf(yyout, "%s [", temp->toStr().c_str());
for (int j = 0; j < temp->getCurDimWidth(); j++) {
if (j != 0)
fprintf(yyout, ", ");
fprintf(yyout, "i32 %d", val[i++]);
}
fprintf(yyout, "]");
stk1.pop();
stk.pop();
if (stk.empty())
break;
stk1.top()++;
continue;
}
if (stk1.top() != temp->getCurDimWidth()) {
stk.push((ArrayType*)(temp->getElementType()));
if (stk1.top() == 0)
fprintf(yyout, "%s [", temp->toStr().c_str());
if (stk1.top() != 0)
fprintf(yyout, ", ");
stk1.push(0);
} else {
fprintf(yyout, "]");
stk.pop();
stk1.pop();
if (stk.empty())
break;
stk1.top()++;
continue;
}
}
}
fprintf(yyout, ", align 4\n");
}
}
for (auto& func : func_list)
func->output();
for (auto se : declare_list) {
FunctionType* type = (FunctionType*)(se->getType());
std::string str = type->toStr();
std::string name = str.substr(0, str.find('('));
std::string param = str.substr(str.find('('));
fprintf(yyout, "declare %s %s%s\n", type->getRetType()->toStr().c_str(),
se->toStr().c_str(), param.c_str());
}
}
void Unit::genMachineCode(MachineUnit* munit)
{
AsmBuilder* builder = new AsmBuilder();
builder->setUnit(munit);
for (auto &func : func_list)
func->genMachineCode(builder);
}
Unit::~Unit()
{
for(auto &func:func_list)
delete func;
}