-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cc
152 lines (126 loc) · 3.73 KB
/
driver.cc
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
#include "driver.hh"
#include "parser.hh"
driver::driver () {
label_counter = 0;
label_prefix = "Label_";
curr_scope = -1;
global_offset = 0;
function_offset = 0;
push_scope();
in_loop = 0;
in_switch = 0;
}
std::string driver::get_label () {
std::string label = label_prefix;
label_counter++;
label = label + std::to_string(label_counter);
return label;
}
void driver::push_scope () {
curr_scope++;
variables.push_back(std::map<std::string, Var>());
}
void driver::pop_scope () {
curr_scope--;
if (curr_scope == 0) {
function_offset = 0;
}
variables.pop_back();
}
Var driver::make_variable(std::string id, yy::location & loc, int type, int size, int initial_value) {
if (has_variable_in_scope(id, curr_scope)) {
throw yy::parser::syntax_error(loc, "multiple definitions for '" + id + "'");
}
// std::cerr << id << curr_scope << std::endl;
Var var;
var.size = size;
var.id = id;
if (curr_scope == 0) {
var.offset = global_offset;
global_offset += size;
} else {
var.offset = function_offset;
function_offset += size;
}
var.scope = curr_scope;
var.type = type == 1 ? Var::Type::INT : Var::Type::CHAR; // int = 1, char = 2
var.initial_value = initial_value;
variables.back()[id] = var;
// ! test
// std::cerr << variables.size() << std::endl;
// for (auto p : variables.back()) {
// std::cerr << p.first << ": " << p.second.scope << " " << p.second.offset << std::endl;
// }
return var;
}
Var driver::get_variable(std::string id, yy::location & loc) {
for (int scope = curr_scope; scope >= 0; scope--) {
auto it = variables[scope].find(id);
if (it != variables[scope].end()) {
return it->second;
}
}
throw yy::parser::syntax_error(loc, "undefined identifier " + id);
}
Function driver::get_function(std::string id, yy::location & loc) {
auto it = Functions.find(id);
if (it != Functions.end()) {
return it->second;
}
throw yy::parser::syntax_error(loc, "undefined identifier " + id);
}
void driver::make_func(std::string id, int type) {
curr_function = id;
Functions[curr_function] = Function();
Functions[curr_function].id = id;
Functions[curr_function].type = (Function::Type)(type - 1);
}
void driver::add_args_to_func(std::vector<Var> &vars, yy::location & loc) {
if (vars.size() > 4) {
throw yy::parser::syntax_error(loc, "Too many arguments");
}
Functions[curr_function].number_of_arguments = vars.size();
}
bool driver::has_variable_in_scope(std::string id, int scope) {
// auto it = variables[scope].find(id);
// if (it == variables[scope].end()) {
// return false;
// }
// return true;
return variables[scope].find(id) != variables[scope].end();
}
int driver::parse (const std::string &f)
{
file = f;
location.initialize (&file);
scan_begin ();
yy::parser parse (*this);
// parse.set_debug_level (false);
// std::cerr << "here" << std::endl;
int res = parse();
// std::cerr << "here2" << std::endl;
scan_end ();
// std::cerr << "here3" << std::endl;
return res;
}
void driver::pop_loop() {
loop_labels_stack.pop_back();
}
void driver::push_loop() {
loop_labels_stack.push_back(std::map<std::string, std::string>());
loop_labels_stack.back()["loop"] = get_label();
loop_labels_stack.back()["end"] = get_label();
}
void driver::make_output(Node & node) {
std::ofstream out(file + ".out");
out << ".text" << std::endl;
out << "\t\tMOVE\t$s0,\t$sp" << std::endl;
// out << "\t\taddiu\t$sp,\t$sp,\t" + std::to_string(-4 * global_offset) << std::endl;
for (auto v : variables[0]) {
auto var = v.second;
out << "\t\tli\t\t$t0,\t" + std::to_string(var.initial_value) + "\n"
<< "\t\tsw\t\t$t0,\t$sp\n";
}
out << "\t\tB\t\tmain" << std::endl;
out << node.code.text;
}