-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm_generator.cpp
243 lines (189 loc) · 6.07 KB
/
asm_generator.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "asm_generator.h"
#include "parser.h"
#include <string>
using namespace std;
static const string asm_op_str[] = {
"push", "pop", "imul", "div", "add", "sub",
"neg", "not", "or", "and", "xor", "shl", "shr",
"call", "mov", "ret", "test", "cmp",
"jz", "jnz", "jmp", "jg", "jge", "jl", "jle", "je", "jne",
"fld", "fild", "fstp", "fadd", "fsub", "fdiv", "fmul",
"fiadd", "fisub", "fidiv", "fimul",
"ja", "jb", "jae", "jbe",
"fcom", "fcomi", "fcomip", "inc", "dec"
};
static const string asm_reg_str[] = { "eax", "ebx", "ecx", "edx", "ebp", "esp" };
static const string asm_type_str[] = { "db", "dw", "dd", "dq" };
asm_code::asm_code(parser &prs){
head = ".686\n.model flat, stdcall\n\ninclude \\masm32\\include\\msvcrt.inc\nincludelib \\masm32\\lib\\msvcrt.lib\n\n\
STR_LITERAL macro value : req\n\
LOCAL lbl\n\
.data\n\
lbl BYTE value, 0\n\
.code\n\
EXITM <lbl>\n\
endm\n\n";
for (auto it = prs.table->symbols.begin(); it != prs.table->symbols.end(); it++)
(*it).second->generate(this);
for (auto it = prs.table->functions.begin(); it != prs.table->functions.end(); it++)
(*it)->generate(this);
}
void asm_code::print(ostream &os){
os << head;
os << ".data" << endl;
for (auto it = data.begin(); it != data.end(); it++)
(*it)->print(os);
os << ".code" << endl;
for (auto it = code.begin(); it != code.end(); it++){
(*it)->print(os);
}
}
void asm_code::add(asm_function *new_func){
code.push_back(new_func);
}
void asm_code::add_data(asm_t *new_var){
data.push_back(new_var);
}
/*-------------------------------class asm_function------------------------------*/
asm_main_function::asm_main_function(asm_cmd_list *fcmds){
cmds = fcmds;
}
void asm_main_function::print(ostream &os){
os << "start:" << endl;
cmds->print(os);
os << "end start" << endl;
}
asm_function::asm_function(string func_name, asm_cmd_list *fcmds){
name = func_name;
cmds = fcmds;
}
void asm_function::print(ostream &os){
os << name << "_ proc" << endl;
cmds->print(os);
os << name << "_ endp" << endl;
}
/*-------------------------------class asm_global_var---------------------------------*/
asm_global_var::asm_global_var(string &name, asm_type_t type){
gv_name = name;
gv_type = type;
}
void asm_global_var::print(ostream &os){
os << '\t' << gv_name << '\t' << asm_type_str[gv_type] << '\t' << 0 << endl;
}
/*-------------------------------class asm_global_array-------------------------------*/
asm_global_array::asm_global_array(string &name, asm_type_t type, size_t length){
ga_name = name;
ga_type = type;
size = length;
}
void asm_global_array::print(ostream &os){
os << '\t' << ga_name << '\t' << asm_type_str[ga_type] << '\t' << size << "\tdup(0)" << endl;
}
/*-------------------------------class asm_cmd_list---------------------------------*/
void asm_cmd_list::push_back(asm_t *cmd){
cmds.push_back(cmd);
}
void asm_cmd_list::print(ostream &os){
for (auto it = cmds.begin(); it != cmds.end(); it++){
os << '\t';
(*it)->print(os);
}
}
void asm_cmd_list::add(asm_op_t op){
cmds.push_back(new asm_t(op));
}
void asm_cmd_list::add(asm_op_t op, string val){
cmds.push_back(new asm_unar_op_t(op, new asm_operand_const(val)));
}
void asm_cmd_list::add(asm_op_t op, asm_reg_t reg){
cmds.push_back(new asm_unar_op_t(op, new asm_operand_reg_t(reg)));
}
void asm_cmd_list::add(asm_op_t op, asm_reg_t reg1, asm_reg_t reg2){
cmds.push_back(new asm_bin_op_t(op, new asm_operand_reg_t(reg1), new asm_operand_reg_t(reg2)));
}
void asm_cmd_list::add(asm_op_t op, asm_reg_t reg, string val){
cmds.push_back(new asm_bin_op_t(op, new asm_operand_reg_t(reg), new asm_operand_const(val)));
}
void asm_cmd_list::add_offset(asm_op_t op, string value){
cmds.push_back(new asm_unar_op_t(op, new asm_operand_offset(value)));
}
void asm_cmd_list::add_assign(asm_op_t op, asm_reg_t addr, asm_reg_t val){
cmds.push_back(new asm_bin_op_t(op, new asm_operand_deref(addr), new asm_operand_reg_t(val)));
}
void asm_cmd_list::add_deref(asm_op_t op, asm_reg_t reg){
cmds.push_back(new asm_unar_op_t(op, new asm_operand_deref(reg)));
}
void asm_cmd_list::add_deref(asm_op_t op, asm_reg_t reg, int offset){
cmds.push_back(new asm_unar_op_t(op, new asm_operand_deref(reg, offset)));
}
string asm_cmd_list::get_label_name(){
return "L_" + to_string(label_cnt++);
}
void asm_cmd_list::add_label(string lname){
cmds.push_back(new asm_label_t(lname));
}
/*---------------------------------------class asm_t-----------------------------------------*/
asm_t::asm_t(asm_op_t asm_op){
op = asm_op;
}
void asm_t::print(ostream &os){
os << asm_op_str[op] << endl;
}
asm_label_t::asm_label_t(string label_name){
name = label_name;
}
void asm_label_t::print(ostream &os){
os << name << ':' << endl;
}
/*-----------------------------------class asm_operand----------------------------------------*/
asm_operand_reg_t::asm_operand_reg_t(asm_reg_t asm_reg){
reg = asm_reg;
}
void asm_operand_reg_t::print(ostream &os){
os << asm_reg_str[reg];
}
asm_operand_const::asm_operand_const(string value){
val = value;
}
void asm_operand_const::print(ostream &os){
os << val;
}
asm_operand_offset::asm_operand_offset(string variable){
var = variable;
}
void asm_operand_offset::print(ostream &os){
os << "offset " << var;
}
asm_operand_deref::asm_operand_deref(asm_reg_t asm_reg){
reg = asm_reg;
offset = 0;
}
asm_operand_deref::asm_operand_deref(asm_reg_t asm_reg, int asm_offset){
reg = asm_reg;
offset = asm_offset;
}
void asm_operand_deref::print(ostream &os){
os << "[" << asm_reg_str[reg] << " + " << offset << "]";
}
/*------------------------------------class asm_unar_op_t----------------------------------*/
asm_unar_op_t::asm_unar_op_t(asm_op_t asm_op, asm_operand_t *asm_operand){
op = asm_op;
operand = asm_operand;
}
void asm_unar_op_t::print(ostream &os){
os << asm_op_str[op] << ' ';
operand->print(os);
os << endl;
}
asm_bin_op_t::asm_bin_op_t(asm_op_t asm_op, asm_operand_t *asm_operand1, asm_operand_t *asm_operand2){
op = asm_op;
operand1 = asm_operand1;
operand2 = asm_operand2;
}
void asm_bin_op_t::print(ostream &os){
os << asm_op_str[op] << ' ';
operand1->print(os);
os << ',';
operand2->print(os);
os << endl;
}