-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_asm.c
526 lines (513 loc) · 16.5 KB
/
to_asm.c
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "types.h"
#include "sume_tokens.h"
#include "from_ir.h"
#include "data_s.h"
#include "populate_fns.h"
#include "instrs.h"
asm_fn_info fn_infos[64];
asm_blk_info blk_infos[256];
uint fn_info_n;
int blk_info_n;
uint goodret_addr = 0;
uint trapret_addr = 0;
uint start_addr = 0;
uint cur_blk_stk[64];
int
prelude(
char* asm_loc,
uint asm_i_start,
uint base_addr)
{
uint asm_i = asm_i_start;
int nil;
asm_i += push_r32(EBP, &nil, asm_loc+asm_i);
asm_i += mov_r32_r32(EBP, ESP, asm_loc+asm_i);
asm_i += push_r32(EBX, &nil, asm_loc+asm_i);
asm_i += push_r32(ESI, &nil, asm_loc+asm_i);
asm_i += push_r32(EDI, &nil, asm_loc+asm_i);
asm_i += mov_r32_dis_ebp(EDI, 8 + 0, asm_loc+asm_i);
asm_i += mov_r32_dis_ebp(ESI, 8 + 4, asm_loc+asm_i);
asm_i += mov_r32_dis_ebp(EAX, 8 + 8, asm_loc+asm_i);
asm_i += sub_r32_imm(EAX, 4, asm_loc+asm_i);
asm_i += mov_r32_r32(EBP, EAX, asm_loc+asm_i);
asm_i += mov_ar32_r32(EAX, ESP, asm_loc+asm_i);
asm_i += mov_r32_r32(ESP, EAX, asm_loc+asm_i);
asm_i += mov_r32_r32(EBP, ESP, asm_loc+asm_i);
asm_i += sub_r32_imm(ESP, 4*fn_infos[0].local_n, asm_loc+asm_i);
asm_i += push_r32(EBP, &nil, asm_loc+asm_i);
asm_i += mov_r32_r32(EBP, ESP, asm_loc+asm_i);
asm_i += sub_r32_imm(EBP, 4, asm_loc+asm_i);
asm_i += call_rel(base_addr+asm_i, start_addr, asm_loc+asm_i);
return asm_i - asm_i_start;
}
int
goodret(
char* asm_loc,
uint asm_i_start)
{
int nil;
uint asm_i = asm_i_start;
asm_i += pop_r32(ESP, &nil, asm_loc+asm_i);
asm_i += pop_r32(ESP, &nil, asm_loc+asm_i);
asm_i += pop_r32(EDI, &nil, asm_loc+asm_i);
asm_i += pop_r32(ESI, &nil, asm_loc+asm_i);
asm_i += pop_r32(EBX, &nil, asm_loc+asm_i);
asm_i += pop_r32(EBP, &nil, asm_loc+asm_i);
asm_i += xor_r32_r32(EAX, EAX, asm_loc+asm_i);
asm_i += ret_fn(asm_loc+asm_i);
return asm_i - asm_i_start;
}
int
trapret(
char* asm_loc,
uint asm_i_start)
{
int nil;
uint asm_i = asm_i_start;
asm_i += mov_r32_r32(ESP, EBP, asm_loc+asm_i);
asm_i += mov_r32_imm(EBX, goodret_addr, asm_loc+asm_i);
// label unwind
asm_i += pop_r32(EAX, &nil, asm_loc+asm_i);
asm_i += pop_r32(ESP, &nil, asm_loc+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, asm_loc+asm_i);
asm_i += jne_rel(0, -4, asm_loc+asm_i); // 1 + 1 + 2
asm_i += pop_r32(ESP, &nil, asm_loc+asm_i);
asm_i += pop_r32(EDI, &nil, asm_loc+asm_i);
asm_i += pop_r32(ESI, &nil, asm_loc+asm_i);
asm_i += pop_r32(EBX, &nil, asm_loc+asm_i);
asm_i += pop_r32(EBP, &nil, asm_loc+asm_i);
asm_i += mov_r32_imm(EAX, -1, asm_loc+asm_i);
asm_i += ret_fn(asm_loc+asm_i);
return asm_i - asm_i_start;
}
uint
populate_infos(
char* prog,
uint prog_len,
char* buf,
uint base_addr,
int gen_info)
{
uint place = 0;
uint asm_i = 0;
int cur_fn = -1;
int cur_blk = -1;
int cur_blk_h = -1;
int stkh = 0;
asm_i += prelude(buf, asm_i, base_addr);
goodret_addr = asm_i;
asm_i += goodret(buf, asm_i);
trapret_addr = asm_i;
asm_i += trapret(buf, asm_i);
start_addr = asm_i;
while (place < prog_len) {
int iv;
uint uiv;
int fnrt;
int fnaddr;
int fnlocal_n;
int fnparam_n;
int blkstk_h;
int blkaddr;
int blk_n;
int nil;
char empty_b[64];
instr_token token = prog[place];
place += 1;
switch (token) {
case i_const:
get_int(prog, prog_len, &place, &iv);
asm_i += mov_r32_imm(EAX, iv, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_clz:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += bsf_r32_r32(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_ctz:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += bsr_r32_r32(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_popcnt:
break;
case i_add:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += add_r32_r32(EAX, EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_and:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += and_r32_r32(EAX, EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_div_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(EDX, EDX, buf+asm_i);
asm_i += idiv_r32(EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_div_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(EDX, EDX, buf+asm_i);
asm_i += div_r32(EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_mul:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mul_r32(EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_or:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += or_r32_r32(EAX, EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_rem_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(EDX, EDX, buf+asm_i);
asm_i += idiv_r32(EBX, buf+asm_i);
asm_i += push_r32(EDX, &stkh, buf+asm_i);
break;
case i_rem_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(EDX, EDX, buf+asm_i);
asm_i += div_r32(EBX, buf+asm_i);
asm_i += push_r32(EDX, &stkh, buf+asm_i);
break;
case i_rotl:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_r32_r32(ECX, EBX, buf+asm_i);
asm_i += rol_r32(EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_rotr:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_r32_r32(ECX, EBX, buf+asm_i);
asm_i += ror_r32(EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_shl:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_r32_r32(ECX, EBX, buf+asm_i);
asm_i += shl_r32(EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_shr_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_r32_r32(ECX, EBX, buf+asm_i);
asm_i += sar_r32(EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_shr_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_r32_r32(ECX, EBX, buf+asm_i);
asm_i += shr_r32(EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_sub:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += sub_r32_r32(EAX, EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_xor:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(EAX, EBX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_eqz:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += test_r32_r32(EAX, EAX, buf+asm_i);
asm_i += setz_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_eq:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setz_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_ge_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setge_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_ge_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setae_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_gt_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setg_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_gt_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += seta_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_le_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setle_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_le_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setbe_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_lt_s:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setl_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_lt_u:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setb_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_ne:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += xor_r32_r32(ECX, ECX, buf+asm_i);
asm_i += cmp_r32_r32(EAX, EBX, buf+asm_i);
asm_i += setne_r8(CL, buf+asm_i);
asm_i += push_r32(ECX, &stkh, buf+asm_i);
break;
case i_ld32:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += shl_r32_imm(EAX, 2, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += mov_r32_ar32(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_ld16_s:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += shl_r32_imm(EAX, 1, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += movsx_r32_ar16(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_ld16_u:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += shl_r32_imm(EAX, 1, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += movzx_r32_ar16(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_ld8_s:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += movsx_r32_ar8(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_ld8_u:
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += movzx_r32_ar8(EAX, EAX, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case i_memsz:
asm_i += push_r32(ESI, &stkh, buf+asm_i);
break;
case str32:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += shl_r32_imm(EAX, 2, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += mov_ar32_r32(EAX, EBX, buf+asm_i);
break;
case str16:
break;// TEMPORARY
case str8:
asm_i += pop_r32(EBX, &stkh, buf+asm_i);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += cmp_r32_r32(EAX, ESI, buf+asm_i);
asm_i += jae_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
asm_i += add_r32_r32(EAX, EDI, buf+asm_i);
asm_i += mov_ar32_r8(EAX, BL, buf+asm_i);
break;
case get_l:
get_int(prog, prog_len, &place, &iv);
fnparam_n = fn_infos[cur_fn].param_n;
fnlocal_n = fn_infos[cur_fn].local_n;
iv = (fnparam_n + fnlocal_n - iv + 1);
asm_i += mov_r32_dis_ebp(EAX, 4*iv, buf+asm_i);
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case set_l:
get_int(prog, prog_len, &place, &iv);
fnparam_n = fn_infos[cur_fn].param_n;
fnlocal_n = fn_infos[cur_fn].local_n;
iv = (fnparam_n + fnlocal_n - iv + 1);
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += mov_dis_ebp_r32(4*iv, EAX, buf+asm_i);
break;
case trap:
asm_i += jmp_rel(base_addr+asm_i, trapret_addr, buf+asm_i);
break;
case block:
cur_blk += 1;
cur_blk_h += 1;
cur_blk_stk[cur_blk_h] = cur_blk;
if (gen_info) {
blk_infos[cur_blk].br_addr = 0;
blk_infos[cur_blk].stk_height = stkh;
}
break;
case loop:
cur_blk += 1;
cur_blk_h += 1;
cur_blk_stk[cur_blk_h] = cur_blk;
if (gen_info) {
blk_infos[cur_blk].br_addr = base_addr+asm_i;
blk_infos[cur_blk].stk_height = stkh;
}
break;
case br:
get_int(prog, prog_len, &place, &iv);
blk_n = cur_blk_stk[cur_blk_h-iv];
blkaddr = blk_infos[blk_n].br_addr;
asm_i += add_r32_imm(ESP, 4*(stkh), buf+asm_i);
asm_i += jmp_rel(base_addr+asm_i, blkaddr, buf+asm_i);
break;
case br_if:
get_int(prog, prog_len, &place, &iv);
blk_n = cur_blk_stk[cur_blk_h-iv];
blkaddr = blk_infos[blk_n].br_addr;
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
asm_i += test_r32_r32(EAX, EAX, buf+asm_i);
asm_i += jz_rel(0, 17, buf+asm_i);
asm_i += add_r32_imm(ESP, 4*(stkh), buf+asm_i);
asm_i += jmp_rel(base_addr+asm_i, blkaddr, buf+asm_i);
//next = 6 + 6 + 5
break;
case ret:
if (fn_infos[cur_fn].rt != 0) {
asm_i += pop_r32(EAX, &stkh, buf+asm_i);
}
asm_i += mov_r32_r32(ESP, EBP, buf+asm_i);
asm_i += ret_fn(buf+asm_i);
break;
case end:
if (cur_blk_h > 0) {
blk_n = cur_blk_stk[cur_blk_h];
blkstk_h = blk_infos[blk_n].stk_height;
blk_n = cur_blk_stk[cur_blk_h-1];
stkh = blk_infos[blk_n].stk_height;
blkstk_h -= stkh;
asm_i += add_r32_imm(ESP, 4*blkstk_h, buf+asm_i);
blk_n = cur_blk_stk[cur_blk_h];
if (gen_info && blk_infos[blk_n].br_addr == 0) {
blk_infos[blk_n].br_addr = base_addr+asm_i;
}
}
cur_blk_h -= 1;
break;
case call:
get_int(prog, prog_len, &place, &iv);
fnrt = fn_infos[iv].rt;
fnaddr = fn_infos[iv].fn_addr;
fnparam_n = fn_infos[iv].param_n;
fnlocal_n = fn_infos[iv].local_n;
asm_i += sub_r32_imm(ESP, fnlocal_n, buf+asm_i);
asm_i += push_r32(EBP, &nil, buf+asm_i);
asm_i += mov_r32_r32(EBP, ESP, buf+asm_i);
asm_i += sub_r32_imm(EBP, 4, buf+asm_i);
asm_i += call_rel(base_addr+asm_i, fnaddr, buf+asm_i);
asm_i += pop_r32(EBP, &nil, buf+asm_i);
asm_i += add_r32_imm(ESP, 4*(fnparam_n+fnlocal_n), buf+asm_i);
stkh -= fnparam_n;
if (fnrt != 0)
asm_i += push_r32(EAX, &stkh, buf+asm_i);
break;
case v_declfn:
case i_declfn:
cur_fn += 1;
cur_blk += 1;
cur_blk_h += 1;
cur_blk_stk[cur_blk_h] = cur_blk;
get_type_list(prog, prog_len, &place, empty_b, &uiv);
get_type_list(prog, prog_len, &place, empty_b, &uiv);
if (gen_info) {
fn_infos[cur_fn].fn_addr = base_addr+asm_i;
blk_infos[cur_blk].br_addr = -1;
blk_infos[cur_blk].stk_height = 0;
}
break;
default:
break;
}
}
return asm_i;
}
void
to_asm(
char* prog,
uint prog_len,
char* asm_loc,
uint* asm_sz,
uint base_addr)
{
asm_populate_fns(prog, prog_len, fn_infos, &fn_info_n);
populate_infos(prog, prog_len, asm_loc, base_addr, 1);
*asm_sz = populate_infos(prog, prog_len, asm_loc, base_addr, 0);
}