-
Notifications
You must be signed in to change notification settings - Fork 9
/
asmcode.cpp
463 lines (449 loc) · 13.1 KB
/
asmcode.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "comm.h"
struct asm_gen_t
{
FILE *fp;
int label_count;
asm_gen_t()
{
fp=0;
label_count=-1;
}
void write_asm( const char * format, ... )
{
va_list args;
va_start (args, format);
vfprintf (fp,format, args);
va_end (args);
}
string cut(string s)
{
if(s.length()==0) return "";
std::size_t pos=s.find('/');
if(s=="/") return "/";
if(pos==string::npos) return s;
return s.substr(0,pos);
}
string mod(string s) //generate operand
{
if(s=="[ebx]")
return s;
if(s.length()>2)
{
if(s[0]=='?'&&s[1]=='q')//it's a variable related to pointer
{
string r;
write_asm("mov ebx,[$%s]\n",s.c_str()); //indirect addressing
r="?";
return "[ebx]"; //another indirect addressing
}
}
if(s.length()>=1)
{
if(s[0]=='\''||s[0]>='0'&&s[0]<='9') //it's integer or char
{
return s;
}
else//it's a normal variable
{
string r="[";
r+="$";
r+=s;
r+="]";
return r; //only one indirect addressing
}
}
return s;
}
string creat_lb()
{
label_count++;
//addr0++;
char s[100];
string r = "?LB"; // LB for label generated by asm_gen, lb for quat_gen
var_type_t typetmp;
typetmp.s = "temp_label";
//typetmp.addr=addr0;
sprintf(s, "%d", label_count);
r += s;
return r;
}
int run(map<string,string> asmmp,map<string,var_type_t> vb,vector<quat_t> quat,asm_format_t asm_format,string output_name)
{
vector<quat_t>::iterator it;
map<string,var_type_t> ::iterator it2;
//char output_name[]="output.asm";
fp=fopen(output_name.c_str(),"w");
if(asm_format==format_bin)
{
write_asm(";;format: bin\n\n");
write_asm("section .text\n");
write_asm("\norg 100h\n");
}
else if(asm_format==format_elf32)
{
write_asm(";;format: elf32\n\n");
write_asm("section .text\n");
write_asm("global _start\n");
write_asm("_start:\n");
}
else if(asm_format==format_win32)
{
write_asm(";;format: win32\n\n");
write_asm("section .text\n");
write_asm("global _main\n"); //gnu ld (native/cygwin/mingw)
write_asm("global start\n"); //golink
write_asm("_main:\n");
write_asm("start:\n");
}
write_asm("call _?db_init\n");
write_asm("call main\n");
if(asm_format==format_bin)
{
//write_asm("mov ah,01h\n");
//write_asm("int 21h\n");
write_asm("mov ah,0x4c\n");
write_asm("int 21h\n");
}
else if(asm_format==format_elf32)
{
write_asm("xor ebx, ebx\n");
write_asm("mov eax, 0x1\n");
write_asm("int 0x80\n");
}
else if(asm_format==format_win32)
{
write_asm("ret\n");
}
vector <string> arr;
for(it2=vb.begin();it2!=vb.end();it2++)
{
if(it2->second.s=="int_arr"||it2->second.s=="string_constant"||it2->second.s=="array_constant") arr.push_back(it2->first);
}
write_asm("\n");
write_asm("_?db_init:\n");
for(int i=0;i<arr.size();i++)
{
string r="arr?";r+=arr[i];
write_asm("mov eax,%s\n",r.c_str());
write_asm("mov [%s],eax\n",arr[i].c_str());
}
write_asm("ret\n\n");
write_asm("_?batch_push:\n");
write_asm("pop ecx\n");
write_asm("_?batch_push_l1:\n");
write_asm("push dword [eax]\n");
write_asm("add eax,4\n");
write_asm("dec ebx\n");
write_asm("jnz _?batch_push_l1\n");
write_asm("push ecx\n");
write_asm("ret\n\n");
write_asm("_?batch_pop:\n");
write_asm("pop ecx\n");
write_asm("add eax,ebx\n");
write_asm("add eax,ebx\n");
write_asm("add eax,ebx\n");
write_asm("add eax,ebx\n");
write_asm("_?batch_pop_l1:\n");
write_asm("sub eax,4\n");
write_asm("pop dword [eax]\n");
write_asm("dec ebx\n");
write_asm("jnz _?batch_pop_l1\n");
write_asm("push ecx\n");
write_asm("ret\n\n");
for(it=quat.begin();it!=quat.end();it++)
{
if(cut(it->s1)=="call") write_asm("call %s\n",it->s4.c_str());
else if(cut(it->s1)=="ret") write_asm("ret\n");
else if(cut(it->s1)=="rts") write_asm("rts\n");
else if(cut(it->s1)=="func") write_asm("%s:\n",it->s4.c_str());
else if(cut(it->s1)=="lb") write_asm("%s:\n",it->s4.c_str());
else if(cut(it->s1)=="asm")write_asm("%s\n",asmmp.find(it->s4)->second.c_str()); //write_asm("pusha\n%s\npopa\n",asmmp.find(it->s4)->second.c_str());
else if(cut(it->s1)=="pushnow")
{
map<string,var_type_t>::iterator it3;
for(it3=vb.begin();it3!=vb.end();it3++)
{
if(it3->second.func==it->s4)
{
write_asm("push dword [$%s]\n",it3->first.c_str());//push,pop的总是变量的内容
//write_asm("push eax\n");
if(it3->second.s=="int_arr"&&it3->second.n!=0)
{
write_asm("mov eax,$arr?%s\n",it3->first.c_str());
write_asm("mov ebx,%d\n",it3->second.n);
write_asm("call _?batch_push\n");
}
}
}
}
else if(cut(it->s1)=="popnow")
{
map<string,var_type_t>::iterator it3;
for(it3=vb.end();;)
{
it3--;
if(it3->second.func==it->s4)
{
if(it3->second.s=="int_arr"&&it3->second.n!=0)
{
write_asm("mov eax,$arr?%s\n",it3->first.c_str());
write_asm("mov ebx,%d\n",it3->second.n);
write_asm("call _?batch_pop\n");
}
//write_asm("pop eax\n");
write_asm("pop dword [$%s]\n",it3->first.c_str());
}
if(it3==vb.begin()) break;
}
}
else if(cut(it->s1)=="+")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("add eax,%s\n",mod(it->s3).c_str());
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="-")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("sub eax,%s\n",mod(it->s3).c_str());
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="*")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("mov ecx,%s\n",mod(it->s3).c_str());
write_asm("mov edx,0\n");
write_asm("imul ecx\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="/")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("mov ecx,%s\n",mod(it->s3).c_str());
write_asm("mov edx,0\n");
write_asm("idiv ecx\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="%")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("mov ecx,%s\n",mod(it->s3).c_str());
write_asm("mov edx,0\n");
write_asm("idiv ecx\n");
write_asm("mov [%s],edx\n",it->s4.c_str());
}
else if(cut(it->s1)=="=")//特殊的运算符
{
write_asm("mov eax,%s\n",mod(it->s3).c_str());
write_asm("mov %s,eax\n",mod(it->s2).c_str());
}
else if(cut(it->s1)=="!=")
{
write_asm("mov eax,%s\n",mod(it->s3).c_str());
write_asm("cmp eax,%s\n",mod(it->s2).c_str());
write_asm("LAHF\n");
write_asm("SHR EAX,14\n");
write_asm("not EAX\n");
write_asm("and EAX,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="==")
{
write_asm("mov eax,%s\n",mod(it->s3).c_str());
write_asm("cmp eax,%s\n",mod(it->s2).c_str());
write_asm("LAHF\n");
write_asm("SHR EAX,14\n");
write_asm("and EAX,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)==">")
{
write_asm("xor eax,eax\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,%s\n",mod(it->s3).c_str());
string lb1=creat_lb();
write_asm("jle %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
}
else if(cut(it->s1)=="<")
{
write_asm("xor eax,eax\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,%s\n",mod(it->s3).c_str());
string lb1=creat_lb();
write_asm("jge %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
}
else if(cut(it->s1)==">=")
{
write_asm("xor eax,eax\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,%s\n",mod(it->s3).c_str());
string lb1=creat_lb();
write_asm("jl %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
}
else if(cut(it->s1)=="<=")
{
write_asm("xor eax,eax\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,%s\n",mod(it->s3).c_str());
string lb1=creat_lb();
write_asm("jg %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
}
else if(cut(it->s1)=="&&")
{
string lb1=creat_lb(),lb2=creat_lb();
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,0\n");
write_asm("jne %s\n",lb1.c_str());
write_asm("mov eax,0\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
write_asm("mov eax,%s\n",mod(it->s3).c_str());
write_asm("cmp eax,0\n");
write_asm("jne %s\n",lb2.c_str());
write_asm("mov eax,0\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb2.c_str());
}
else if(cut(it->s1)=="||")
{
string lb1=creat_lb(),lb2=creat_lb();
write_asm("mov eax,0\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,0\n");
write_asm("je %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
write_asm("mov eax,%s\n",mod(it->s3).c_str());
write_asm("cmp eax,0\n");
write_asm("je %s\n",lb2.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb2.c_str());
}
else if(cut(it->s1)=="!")
{
write_asm("xor eax,eax\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("mov eax,0\n");
write_asm("cmp eax,%s\n",mod(it->s2).c_str());
string lb1=creat_lb();
write_asm("jne %s\n",lb1.c_str());
write_asm("mov eax,1\n");
write_asm("mov [%s],eax\n",it->s4.c_str());
write_asm("%s:\n",lb1.c_str());
}
else if(cut(it->s1)=="&")
{
if(it->s2.length()>2&&it->s2[0]=='?'&&it->s2[1]=='q')//对引用取地址特殊处理
{
write_asm("mov eax,[%s]\n",it->s2.c_str());
}
else write_asm("mov eax,%s\n",it->s2.c_str());//只能对变量取地址
write_asm("mov [%s],eax\n",it->s4.c_str());
}
else if(cut(it->s1)=="**")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("mov [%s],eax\n",it->s4.c_str());//引用
}
else if(cut(it->s1)=="[]")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
//write_asm("mov ecx,eax\n");
write_asm("add eax,%s\n",mod(it->s3).c_str());
write_asm("add eax,%s\n",mod(it->s3).c_str());
write_asm("add eax,%s\n",mod(it->s3).c_str());
write_asm("add eax,%s\n",mod(it->s3).c_str());
write_asm("mov [%s],eax\n",it->s4.c_str());
//write_asm("mov [%s],eax\n",it->s4.c_str());//引用
}
else if(cut(it->s1)=="jpz")
{
write_asm("mov eax,%s\n",mod(it->s2).c_str());
write_asm("cmp eax,0\n");
write_asm("je %s\n",it->s4.c_str());
}
else if(cut(it->s1)=="jmp")
{
write_asm("jmp %s\n",it->s4.c_str());
}
else {printf("<unknow symbol %s>\n",it->s1.c_str());exit(-1);}
}
//////////////////////////////////////////////////////////数据区在后面
write_asm("\n");
write_asm("section .data\n");
for(it2=vb.begin();it2!=vb.end();it2++)
{
if(it2->second.s=="int"||it2->second.s=="tmp_int"||it2->second.s=="quo_int") write_asm("%s dd 0\n",it2->first.c_str());
else if(it2->second.s=="int_arr")
{
write_asm("arr?%s times %d dd 0\n",it2->first.c_str(),it2->second.n);
write_asm("%s dd 0\n",it2->first.c_str());
arr.push_back(it2->first);
}
else if(it2->second.s=="string_constant")
{
write_asm("arr?%s dd ",it2->first.c_str());
string t=it2->second.para[0];
for(int i=1;i<t.length()-1;i++)
{
write_asm("\'%c\',",t[i]);
}
write_asm("0\n");
write_asm("%s dd 0\n",it2->first.c_str());
arr.push_back(it2->first);
}
else if(it2->second.s=="array_constant")
{
write_asm("arr?%s dd ",it2->first.c_str());
for(int i=0;i<it2->second.para.size();i++)
{
write_asm("%s,",it2->second.para[i].c_str());
}
write_asm("0\n");
write_asm("%s dd 0\n",it2->first.c_str());
arr.push_back(it2->first);
}
}
/////////////////////////////////////////////////////////////////////
fclose(fp);
//execlp("nasm", "nasm", "-f", "bin","temp.txt","-o","temp.exe" ,(char *)NULL);
//WinExec(".\\nasm\\nasm.exe temp.txt -o temp.exe",SW_SHOW);
//WinExec("d:\\windows\\notepad.exe c:\\wycc.txt",SW_SHOW);
//getchar();
/*
for(;;)
{
getchar();
}*/
printf("done\n");
printf("format:%s\n",format_name_table[asm_format]);
printf("wrote to \"%s\"\n",output_name.c_str());
return 0;
}
};
int asm_code_gen(const map<string,string> &asmmp,const map<string,var_type_t> &vb,const vector<quat_t> &quat,asm_format_t asm_format,string output_name)
{
asm_gen_t asm_gen;
return asm_gen.run(asmmp,vb,quat,asm_format,output_name);
}