-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.cpp
More file actions
executable file
·397 lines (363 loc) · 12.1 KB
/
generator.cpp
File metadata and controls
executable file
·397 lines (363 loc) · 12.1 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "generator.h"
std::string commands[] =
{
"push", "pop", "add", "sub", "mul", "div", "mov", "idiv",
"ret", "faddp", "fsubp", "fdivp", "fmulp", "fxch", "fabs", "fchs",
"fstp", "fld", "jz", "jnz", "jne", "jg", "jge", "jl",
"jle", "je", "test", "fstsw", "lea", "call", "jmp", "cmp",
"invoke", "movsd", "or", "xor", "and", "imul", "neg", "inc",
"dec", "fcompp", "sahf", "setg", "setl", "sete", "setne", "setle",
"setge", "cdq", "fild", "sal", "sar", "seta", "setb", "setae",
"setbe", "setz",
};
void Instruction::write_command(std::ofstream &output) const
{
std::string str_cmd;
switch(cmd_)
{
case cmd_wrlab:
str_cmd = first_op_->get_name();
if (first_op_->get_operand_type() == op_label)
str_cmd += ":\n";
break;
case cmd_const_decl:
str_cmd = "\t" + first_op_->get_name() + second_op_->get_name();
break;
default:
output << "\t";
if (second_op_->get_operand_type() == op_null)
str_cmd = commands[cmd_] + "\t" + first_op_->get_name() + "\n";
else
str_cmd = commands[cmd_] + "\t" + first_op_->get_name() + ", " + second_op_->get_name() + "\n";
break;
}
output << str_cmd;
}
Instruction::Instruction(AsmCommands c, AsmOperands t1, const std::string &o1, AsmOperands t2, const std::string &o2): cmd_(c)
{
first_op_ = std::make_shared<Operand>(t1, o1);
second_op_ = std::make_shared<Operand>(t2, o2);
}
Instruction::Instruction(AsmCommands c, std::shared_ptr<Operand> op1, std::shared_ptr<Operand> op2): cmd_(c)
{
first_op_ = op1;
second_op_ = op2 ? op2 : std::make_shared<Operand>(op_null, "");
}
Instruction::Instruction(AsmCommands c, AsmOperands t1, const std::string &o1, AsmOperands t2, size_t o2): cmd_(c)
{
first_op_ = std::make_shared<Operand>(t1, o1);
second_op_ = std::make_shared<Operand>(t2, boost::lexical_cast<std::string>(o2));
}
void Generator::write_to_file(std::ofstream &output, bool opt)
{
if (opt)
optimize();
for each(const auto& it in commands_)
it.write_command(output);
}
void Generator::optimize()
{
bool flag = true;
std::list<std::pair<std::string, std::string>> label_list;
std::list<std::pair<std::string, std::string>> var_list;
do
{
auto first_instr = commands_.begin();
auto second_instr = ++commands_.begin();
auto decl_it = first_instr;
flag = false;
do
{
if (*first_instr == cmd_wrlab && first_instr->get_first() == op_null && first_instr->get_first()->get_name() == "\ninclude source\\end.inc\n")
{
decl_it = first_instr;
++first_instr;
++second_instr;
}
// push a
// pop b
if(*first_instr == cmd_push && *second_instr == cmd_pop)
{
// a != b -> mov b, a
if (first_instr->get_first() != second_instr->get_first() && second_instr->get_first() == op_register)
{
commands_.insert(first_instr, Instruction(cmd_mov, second_instr->get_first(), first_instr->get_first()));
delete_instr(first_instr, second_instr);
flag = true;
}
if(first_instr->get_first() == second_instr->get_second())
{
delete_instr(first_instr, second_instr);
flag = true;
}
}
// pop r
// push r
// -> delete pop && push
if(*first_instr == cmd_pop && *second_instr == cmd_push && first_instr->get_first() == second_instr->get_first())
{
delete_instr(first_instr, second_instr);
flag = true;
}
// ...
// label1:
// label2:
// ...
// -> delete label1
if (*first_instr == cmd_wrlab && *second_instr == cmd_wrlab && first_instr->get_first() == op_label && second_instr->get_first() == op_label)
{
label_list.push_back(std::make_pair(first_instr->get_first()->get_name(), second_instr->get_first()->get_name()));
delete_instr(first_instr);
flag = true;
}
//change deleted labels in jumps
if (is_jump(first_instr->get_cmd()))
{
for each(auto it in label_list)
if (it.first == first_instr->get_first()->get_name())
{
first_instr->get_first()->set_name(it.second);
flag = true;
break;
}
}
// jmp label
// label:
// -> delete jmp
if (*first_instr == cmd_jmp && *second_instr == cmd_wrlab && first_instr->get_first()->get_name() == second_instr->get_first()->get_name())
{
delete_instr(first_instr);
flag = true;
}
// jmp l1
// jmp l2
// -> delete l2
if (*first_instr == cmd_jmp && *second_instr == cmd_jmp)
{
second_instr = commands_.erase(second_instr);
flag = true;
}
//mov r, 0 -> xor r, r
if (*first_instr == cmd_mov && first_instr->get_first() == op_register && first_instr->get_second()->get_name() == "0")
{
commands_.insert(first_instr, Instruction(cmd_xor, first_instr->get_first(), first_instr->get_first()));
delete_instr(first_instr);
flag = true;
}
// mov r, r
// -> delete
if (*first_instr == cmd_mov && first_instr->get_first() == first_instr->get_second())
{
delete_instr(first_instr);
flag = true;
}
// add r, 0 || sub r, 0
// -> delete
if ((*first_instr == cmd_add || *first_instr == cmd_sub) && first_instr->get_second() == "0")
{
delete_instr(first_instr);
flag = true;
}
// add r, 1
// -> inc r
if (*first_instr == cmd_add && first_instr->get_second()->get_name() == "1")
{
commands_.insert(first_instr, Instruction(cmd_inc, first_instr->get_first()));
delete_instr(first_instr);
flag = true;
}
// sub r, 1
// -> dec r
if (*first_instr == cmd_sub && first_instr->get_second()->get_name() == "1")
{
commands_.insert(first_instr, Instruction(cmd_dec, first_instr->get_first()));
delete_instr(first_instr);
flag = true;
}
// mov r, 1
// dec r
// -> xor r, r
if (*first_instr == cmd_mov && *second_instr == cmd_dec && first_instr->get_first() == second_instr->get_first() && first_instr->get_second()->get_name() == "1")
{
commands_.insert(first_instr, Instruction(cmd_xor, first_instr->get_first(), first_instr->get_first()));
delete_instr(first_instr, second_instr);
flag = true;
}
// xor r, r
// imul r, i || add a, r || sub a, r || div r, i
// -> delete imul/add/sub
if (*first_instr == cmd_xor && (((*second_instr == cmd_imul || *second_instr == cmd_idiv) && first_instr->get_first() == second_instr->get_first()) ||
((*second_instr == cmd_add || *second_instr == cmd_sub) && first_instr->get_first() == second_instr->get_second())))
{
second_instr = commands_.erase(second_instr);
flag = true;
}
// val1 dq 1.2
// val2 dq 1.2
// -> delete val2, and use val1 instead val2
if (*first_instr == cmd_const_decl)
{
for (auto it = decl_it; it != commands_.end(); ++it)
if (*it == cmd_const_decl && first_instr->get_first()->get_name() != (*it).get_first()->get_name() &&
first_instr->get_second()->get_name() == (*it).get_second()->get_name())
{
var_list.push_back(std::make_pair(it->get_first()->get_name(), first_instr->get_first()->get_name()));
if (second_instr == it)
++second_instr;
it = commands_.erase(it);
flag = true;
}
}
if ((*first_instr == cmd_push && first_instr->get_first() == op_memory) ||
(*first_instr == cmd_mov && first_instr->get_first() == op_register && first_instr->get_second() == op_memory))
for each(auto it in var_list)
{
if ("qword ptr " + it.first == first_instr->get_first()->get_name())
{
first_instr->get_first()->set_name("qword ptr " + it.second);
flag = true;
break;
}
else if ("offset " + it.first == first_instr->get_second()->get_name())
{
first_instr->get_second()->set_name("offset " + it.second);
flag = true;
break;
}
}
++first_instr;
++second_instr;
} while (second_instr != commands_.end());
} while(flag);
}
void Generator::delete_instr(std::list<Instruction>::iterator &it1, std::list<Instruction>::iterator &it2)
{
it2 = commands_.erase(it2);
delete_instr(it1);
}
void Generator::delete_instr(std::list<Instruction>::iterator &it1)
{
it1 = commands_.erase(it1);
--it1;
}
bool Generator::is_jump(AsmCommands c)
{
switch(c)
{
case cmd_jz:
case cmd_jnz:
case cmd_jne:
case cmd_jg:
case cmd_jmp:
case cmd_jge:
case cmd_jl:
case cmd_jle:
case cmd_je:
return true;
break;
default:
return false;
break;
}
}
int Generator::generate_int_arithmetic(LexemeType t, bool use_bitwise_op, int i)
{
switch(t)
{
case plus_op:
push(Instruction(cmd_add, op_register, "eax", op_register, "ecx"));
break;
case minus_op:
push(Instruction(cmd_sub, op_register, "eax", op_register, "ecx"));
break;
case mul_op:
if (use_bitwise_op)
push(Instruction(cmd_sal, op_register, "eax", op_immediate, i));
else
push(Instruction(cmd_mul, op_register, "ecx"));
break;
case or_op:
push(Instruction(cmd_or, op_register, "eax", op_register, "ecx"));
break;
case xor_op:
push(Instruction(cmd_xor, op_register, "eax", op_register, "ecx"));
break;
case and_op:
push(Instruction(cmd_and, op_register, "eax", op_register, "ecx"));
break;
case mod_op:
push(Instruction(cmd_cdq));
push(Instruction(cmd_idiv, op_register, "ecx"));
return 0;
break;
case div_op:
if (use_bitwise_op)
push(Instruction(cmd_sar, op_register, "eax", op_immediate, i));
else
{
push(Instruction(cmd_cdq));
push(Instruction(cmd_idiv, op_register, "ecx"));
}
break;
}
return 1;
}
void Generator::generate_double_arithmetic(LexemeType t)
{
switch (t)
{
case plus_op:
push(Instruction(cmd_faddp));
break;
case minus_op:
push(Instruction(cmd_fsubp));
break;
case mul_op:
push(Instruction(cmd_fmulp));
break;
case div_op:
push(Instruction(cmd_fdivp));
break;
}
}
void Generator::generate_setcc(LexemeType t, bool is_unsigned_cmp)
{
switch(t)
{
case lesser_equal:
if (is_unsigned_cmp)
push(Instruction(cmd_setbe, op_register, "al"));
else
push(Instruction(cmd_setle, op_register, "al"));
break;
case greater_equal:
if (is_unsigned_cmp)
push(Instruction(cmd_setae, op_register, "al"));
else
push(Instruction(cmd_setge, op_register, "al"));
break;
case equal:
push(Instruction(cmd_sete, op_register, "al"));
break;
case not_equal:
push(Instruction(cmd_setne, op_register, "al"));
break;
case greater:
if (is_unsigned_cmp)
push(Instruction(cmd_seta, op_register, "al"));
else
push(Instruction(cmd_setg, op_register, "al"));
break;
case lesser:
if (is_unsigned_cmp)
push(Instruction(cmd_setb, op_register, "al"));
else
push(Instruction(cmd_setl, op_register, "al"));
break;
}
}
void Generator::generate_pop_test()
{
push(Instruction(cmd_pop, op_register, "eax"));
push(Instruction(cmd_test, op_register, "al", op_register, "al"));
}