0
|
1 /* Micro-C Code Generatation Part for intel386 */
|
|
2
|
|
3 #define EXTERN extern
|
|
4 #include "mc.h"
|
|
5
|
|
6 #define REGISTERS6 0
|
|
7
|
|
8 void code_init(void);
|
|
9 void free_register(int i);
|
|
10 void emit_init(void);
|
|
11 void emit_push(void);
|
|
12 void gexpr(int e1);
|
|
13 void g_expr(int e1);
|
|
14 void bexpr(int e1, char cond, int l1);
|
|
15 void b_expr(int e1, char cond, int l1);
|
|
16 void string(int e1);
|
|
17 void function(int e1);
|
|
18 void machinop(int e1);
|
|
19 void rindirect(int e1);
|
|
20 void assign(int e1);
|
|
21 void sassign(int e1);
|
|
22 void assop(int e1);
|
|
23 void tosop(int op);
|
|
24 char *div_setup(int oreg) ;
|
|
25 void shift(char *op, int reg) ;
|
|
26 void ld_indexx(int byte, int n, char *xrn);
|
|
27 void st_indexx(int byte, int n, char *xrn);
|
|
28 void indexy(char *op, char *gvar);
|
|
29 void cmpdimm(int e, int csreg);
|
|
30 void global_table(void) ;
|
|
31 void local_table(void) ;
|
|
32 void opening(char *filename);
|
|
33 void closing(void);
|
|
34 void gen_gdecl(char *n, int gpc) ;
|
|
35 void jmp_label(int l);
|
|
36 void jmp_eq_label(int l);
|
|
37 void rexpr(int e1, int l1, char *s);
|
|
38 void jcond(int l, char cond);
|
|
39 void jmp(int l);
|
|
40 void def_label(int cslabel, int dlabel) ;
|
|
41 void gen_comment(char *s);
|
|
42 void gen_source(char *s);
|
|
43 void code_enter(char *name) ;
|
|
44 void code_leave(char *name) ;
|
|
45 void enter(char *name) ;
|
|
46 void enter1(int disp) ;
|
|
47 void ret(void);
|
|
48 void emit_data(int e, int t, NMTBL *n);
|
|
49 void text_mode(void);
|
|
50 void data_mode(char *name);
|
|
51 int get_register_var(void);
|
|
52 void jump(int e1, int env);
|
|
53 int lvar(int l);
|
|
54
|
|
55 int cadr(int e);
|
|
56 int car(int e);
|
|
57 int caddr(int e);
|
|
58 int fwdlabel(void);
|
|
59 void fwddef(int l);
|
|
60 int cadddr(int e);
|
|
61 int backdef(void);
|
|
62 int error(int n);
|
|
63 int size(int t);
|
|
64 int list3(int e1, int e2, int e3);
|
|
65
|
|
66 #define TEXT_EMIT_MODE 0
|
|
67 #define DATA_EMIT_MODE 1
|
|
68 #define RODATA_EMIT_MODE 2
|
|
69
|
|
70 static output_mode = TEXT_EMIT_MODE;
|
|
71 static data_alignment = 0;
|
|
72
|
|
73 /*
|
|
74 local1 <----24 local variable
|
|
75 %esi -20 <- disp_offset
|
|
76 %edi -16
|
|
77 %edx -12
|
|
78 %ecx -8
|
|
79 %ebx -4
|
|
80 %ebp = %esp 0
|
|
81 %eip 4 <- arg_offset
|
|
82 arg1 8
|
|
83 see enter/enter1/leave
|
|
84 */
|
|
85 int arg_offset = 8;
|
|
86 int disp_offset = -20;
|
|
87 int func_disp_offset = -20;
|
|
88 int code_disp_offset = 0;
|
|
89 int int_size = 4;
|
|
90 int endian = 0;
|
|
91 int MAX_REGISTER=6; /* intel386のレジスタを4つまで使う*/
|
|
92 int REAL_MAX_REGISTER=8; /* intel386のレジスタが8つということ*/
|
|
93 int MAX_DATA_REG=4;
|
|
94 int MAX_POINTER=3;
|
|
95 int MAX_REGISTGER_VAR=2;
|
|
96
|
|
97 static int creg; /* current register */
|
|
98 static char *crn; /* current register name */
|
|
99 static int lreg; /* operand register */
|
|
100 static char *lrn; /* operand register name */
|
|
101 static int dreg; /* temporary register */
|
|
102 static char *drn; /* temporary register name */
|
|
103 static int xreg; /* pointer register */
|
|
104 static int reg_sp; /* REGister Stack-Pointer */
|
|
105
|
|
106
|
|
107 #define REG_EAX 0
|
|
108 #define REG_EBX 1
|
|
109 #define REG_ECX 2
|
|
110 #define REG_EDX 3
|
|
111 #define REG_ESI 4
|
|
112 #define REG_EDI 5
|
|
113 #define REG_EBP 6
|
|
114 #define REG_ESP 7
|
|
115
|
|
116
|
|
117 #define DATA_REG 0
|
|
118 #define POINTER_REG 3
|
|
119 static char *reg_name[8];
|
|
120 static char *reg_name_l[4];
|
|
121
|
|
122 /*
|
|
123 creg currrent virtual register
|
|
124 lreg operand virtual register
|
|
125 dreg spare virtual register
|
|
126
|
|
127 rname[creg] currrent real register
|
|
128 rname[lreg] operand real register
|
|
129 rname[dreg] spare real register
|
|
130
|
|
131 regs[] virtual register usage
|
|
132
|
|
133 reg_name[rname[creg]]
|
|
134 */
|
|
135
|
|
136 #define MAX_MAX 10
|
|
137 static int rname[MAX_MAX];
|
|
138 static int regs[MAX_MAX]; /* 使われているレジスタを示すフラグ */
|
|
139 static int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */
|
|
140
|
|
141 void
|
|
142 code_init(void)
|
|
143 {
|
|
144 if (REGISTERS6) {
|
|
145 arg_offset = 8;
|
|
146 func_disp_offset = -20;
|
|
147 int_size = 4;
|
|
148 endian = 0;
|
|
149 MAX_REGISTER=6;
|
|
150 MAX_DATA_REG=4;
|
|
151 MAX_POINTER=3;
|
|
152 } else {
|
|
153 arg_offset = 8;
|
|
154 func_disp_offset = -12;
|
|
155 int_size = 4;
|
|
156 endian = 0;
|
|
157 MAX_REGISTER=4;
|
|
158 MAX_DATA_REG=4;
|
|
159 MAX_POINTER=1;
|
|
160 }
|
|
161 MAX_REGISTER_VAR=2;
|
|
162
|
|
163 reg_name[REG_EAX] = "%eax";
|
|
164 reg_name[REG_EBX] = "%ebx";
|
|
165 reg_name[REG_ECX] = "%ecx";
|
|
166 reg_name[REG_EDX] = "%edx";
|
|
167 reg_name[REG_ESI] = "%esi";
|
|
168 reg_name[REG_EDI] = "%edi";
|
|
169 reg_name[REG_EBP] = "%ebp";
|
|
170 reg_name[REG_ESP] = "%esp";
|
|
171 reg_name_l[REG_EAX] = "%al";
|
|
172 reg_name_l[REG_EBX] = "%bl";
|
|
173 reg_name_l[REG_ECX] = "%cl";
|
|
174 reg_name_l[REG_EDX] = "%dl";
|
|
175
|
|
176 }
|
|
177
|
|
178 char *
|
|
179 register_name(int i)
|
|
180 {
|
|
181 return reg_name[rname[i]];
|
|
182 }
|
|
183
|
|
184 int
|
|
185 get_register(void)
|
|
186 { /* 使われていないレジスタを調べる */
|
|
187 int i;
|
|
188 for(i=0;i<MAX_REGISTER;i++) {
|
|
189 if (! regs[i]) { /* 使われていないなら */
|
|
190 regs[i]=1; /* そのレジスタを使うことを宣言し */
|
|
191 return i; /* その場所を表す番号を返す */
|
|
192 }
|
|
193 }
|
|
194 return -1; /* 空いている場所がないなら、それを表す -1 を返す */
|
|
195 }
|
|
196
|
|
197 void
|
|
198 free_register(int i) { /* いらなくなったレジスタを開放 */
|
|
199 regs[i]=0;
|
|
200 }
|
|
201
|
|
202 int
|
|
203 register_full(void)
|
|
204 {
|
|
205 int i;
|
|
206 for(i=0;i<MAX_REGISTER;i++) {
|
|
207 if (! regs[i]) {
|
|
208 return 0;
|
|
209 }
|
|
210 }
|
|
211 return 1;
|
|
212 }
|
|
213
|
|
214 void
|
|
215 gexpr_init(void)
|
|
216 {
|
|
217 int i;
|
|
218 while(reg_sp > 0) {
|
|
219 free_register(reg_stack[--reg_sp]);
|
|
220 }
|
|
221 text_mode();
|
|
222 }
|
|
223
|
|
224 void
|
|
225 emit_init(void)
|
|
226 {
|
|
227 int i;
|
|
228 for(i=0;i<REAL_MAX_REGISTER;i++) regs[i]=0;
|
|
229 for(i=0;i<REAL_MAX_REGISTER;i++) rname[i]=i;
|
|
230 creg = get_register();
|
|
231 crn = reg_name[rname[creg]];
|
|
232 dreg = get_register();
|
|
233 drn = reg_name[rname[dreg]];
|
|
234 reg_sp = 0;
|
|
235 text_mode();
|
|
236 }
|
|
237
|
|
238 void
|
|
239 set_crn(int i)
|
|
240 {
|
|
241 creg = i;
|
|
242 crn = reg_name[rname[creg]];
|
|
243 }
|
|
244
|
|
245 void
|
|
246 set_drn(int i)
|
|
247 {
|
|
248 dreg = i;
|
|
249 drn = reg_name[rname[dreg]];
|
|
250 }
|
|
251
|
|
252 int
|
|
253 virtual(int real)
|
|
254 {
|
|
255 int real_v,i;
|
|
256 real_v = -1;
|
|
257 for(i=0;i<MAX_REGISTER;i++) {
|
|
258 if (rname[i]==real) {
|
|
259 real_v=i;
|
|
260 break;
|
|
261 }
|
|
262 }
|
|
263 if (real_v == -1)
|
|
264 error(-1);
|
|
265 return real_v;
|
|
266 }
|
|
267
|
|
268 void
|
|
269 use_register(int virt, int real, int move)
|
|
270 {
|
|
271 int real_v;
|
|
272 char *move_op;
|
|
273 if (rname[virt]==real)
|
|
274 return;
|
|
275 real_v = virtual(real);
|
|
276 move_op = regs[real_v]?"\txchg %s,%s\n":"\tmovl %s,%s\n";
|
|
277 if (move || regs[real_v]) {
|
|
278 printf(move_op,reg_name[rname[virt]],reg_name[real]);
|
|
279 }
|
|
280 rname[real_v] = rname[virt];
|
|
281 rname[virt] = real;
|
|
282 crn = reg_name[rname[creg]];
|
|
283 drn = reg_name[rname[dreg]];
|
|
284 lrn = reg_name[rname[lreg]];
|
|
285 }
|
|
286
|
|
287 void
|
|
288 use_pointer(int virt, int move)
|
|
289 {
|
|
290 int i;
|
|
291 if (rname[virt]>=POINTER_REG)
|
|
292 return;
|
|
293 for(i=POINTER_REG;i<MAX_REGISTER;i++) {
|
|
294 if (!regs[virtual(i)]) {
|
|
295 use_register(virt,i,move);
|
|
296 return;
|
|
297 }
|
|
298 }
|
|
299 /* we prefer EBX */
|
|
300 use_register(virt,REG_EBX,move);
|
|
301 }
|
|
302
|
|
303 void
|
|
304 use_data_reg(int virt, int move)
|
|
305 {
|
|
306 int i;
|
|
307 if (rname[virt]<MAX_DATA_REG)
|
|
308 return;
|
|
309 for(i=0;i<MAX_DATA_REG;i++) {
|
|
310 if (!regs[virtual(i)]) {
|
|
311 use_register(virt,i,move);
|
|
312 return;
|
|
313 }
|
|
314 }
|
|
315 /* we prefer EBX */
|
|
316 use_register(virt,REG_EBX,move);
|
|
317 }
|
|
318
|
|
319 int
|
|
320 pop_register(void)
|
|
321 { /* レジスタから値を取り出す */
|
|
322 int i,j;
|
|
323
|
|
324 j = creg;
|
|
325 i = reg_stack[--reg_sp];
|
|
326
|
|
327 if(i<0) {
|
|
328 return i;
|
|
329 } else {
|
|
330 free_register(i);
|
|
331 lreg = i;
|
|
332 lrn = reg_name[rname[lreg]];
|
|
333 regs[i]=0;
|
|
334 return lreg;
|
|
335 }
|
|
336 }
|
|
337
|
|
338 int
|
|
339 stack_used(void) {
|
|
340 return reg_stack[--reg_sp]<0;
|
|
341 }
|
|
342
|
|
343 void
|
|
344 emit_push(void)
|
|
345 {
|
|
346 int new_reg;
|
|
347 new_reg = get_register();
|
|
348 if(new_reg<0) { /* もうレジスタがない */
|
|
349 reg_stack[reg_sp++] = -1;
|
|
350 printf("\tpushl %s\n",crn);
|
|
351 } else {
|
|
352 reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */
|
|
353 set_crn(new_reg);
|
|
354 }
|
|
355 }
|
|
356
|
|
357 char *
|
|
358 emit_pop(int type)
|
|
359 {
|
|
360 char *xrn;
|
|
361 if (pop_register()==-1) {
|
|
362 if (type==POINTER_REG)
|
|
363 use_pointer(dreg,0);
|
|
364 else if (type==DATA_REG)
|
|
365 use_data_reg(dreg,0);
|
|
366 printf("\tpopl %s\n",drn);
|
|
367 xrn = drn;
|
|
368 xreg = dreg;
|
|
369 } else {
|
|
370 xrn = lrn;
|
|
371 xreg = lreg;
|
|
372 }
|
|
373 return xrn;
|
|
374 }
|
|
375
|
|
376 int
|
|
377 get_register_var(void)
|
|
378 {
|
|
379 int i;
|
|
380 for(i=REG_ESI;i<REG_ESP;i++) {
|
|
381 if (! regs[i]) { /* 使われていないなら */
|
|
382 regs[i]=1; /* そのレジスタを使うことを宣言し */
|
|
383 return i; /* その場所を表す番号を返す */
|
|
384 }
|
|
385 }
|
|
386 return -1;
|
|
387 }
|
|
388
|
|
389 void
|
|
390 gexpr(int e1)
|
|
391 {
|
|
392 gexpr_init();
|
|
393 g_expr(e1);
|
|
394 csvalue = rname[creg]; /* for siwtch value */
|
|
395 }
|
|
396
|
|
397 void
|
|
398 g_expr(int e1)
|
|
399 {
|
|
400 int e2,e3,e4;
|
|
401 char *xrn;
|
|
402 NMTBL *n;
|
|
403
|
|
404 if (chk) return;
|
|
405 e2 = cadr(e1);
|
|
406 switch (car(e1)){
|
|
407 case GVAR:
|
|
408 /* use_pointer(creg,0); */
|
|
409 printf("\tmovl $%s,%s\n",caddr(e1),crn);
|
|
410 return;
|
|
411 case RGVAR:
|
|
412 /* use_pointer(creg,0); */
|
|
413 printf("\tmovl %s,%s\n",caddr(e1),crn);
|
|
414 return;
|
|
415 case CRGVAR:
|
|
416 printf("\tmovsbl %s,%s\n",caddr(e1),crn);
|
|
417 return;
|
|
418 case LVAR:
|
|
419 /* use_pointer(creg,0); */
|
|
420 printf("\tlea %d(%%ebp),%s\n",lvar(e2),crn);
|
|
421 return;
|
|
422 case REGISTER:
|
|
423 /* this is of course redundant... */
|
|
424 printf("\tmovl %s,%s\n",register_name(e2),crn);
|
|
425 return;
|
|
426 case RLVAR:
|
|
427 printf("\tmovl %d(%%ebp),%s\n",lvar(e2),crn);
|
|
428 return;
|
|
429 case CRLVAR:
|
|
430 printf("\tmovsbl %d(%%ebp),%s\n",lvar(e2),crn);
|
|
431 return;
|
|
432 case FNAME:
|
|
433 printf("\tmovl $%s,%s\n",((NMTBL *)e2)->nm,crn);
|
|
434 return;
|
|
435 case CONST: /* 代入する値が0でも特別な処理はしない */
|
|
436 printf("\tmovl $%d,%s\n",e2,crn);
|
|
437 return;
|
|
438 case STRING:
|
|
439 string(e1);
|
|
440 return;
|
|
441 case FUNCTION:
|
|
442 function(e1);
|
|
443 return;
|
|
444 case CODE:
|
|
445 jump(e2,caddr(e1));
|
|
446 return;
|
|
447 case INDIRECT:
|
|
448 g_expr(e2);
|
|
449 return;
|
|
450 case RINDIRECT: case CRINDIRECT:
|
|
451 rindirect(e1);
|
|
452 return;
|
|
453 case ADDRESS:
|
|
454 g_expr(e2);
|
|
455 return;
|
|
456 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */
|
|
457 g_expr(e2);
|
|
458 /* use_data_reg(creg,1); */
|
|
459 printf("\tnegl %s\n", crn);
|
|
460 return;
|
|
461 case BNOT: /* ~ */
|
|
462 g_expr(e2);
|
|
463 /* use_data_reg(creg,1); */
|
|
464 printf("\tnotl %s\n", crn);
|
|
465 return;
|
|
466 case LNOT: /* ! */
|
|
467 g_expr(e2);
|
|
468 use_data_reg(creg,1);
|
|
469 printf("\tcmpl $0,%s\n", crn);
|
|
470 printf("\tsete %s\n", reg_name_l[rname[creg]]);
|
|
471 printf("\tmovzbl %s,%s\n", reg_name_l[rname[creg]],crn);
|
|
472 return;
|
|
473 case PREINC:
|
|
474 if (car(e2)==REGISTER) {
|
|
475 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2)));
|
|
476 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
477 return;
|
|
478 }
|
|
479 g_expr(e2);
|
|
480 printf("\taddl $%d,(%s)\n",caddr(e1),crn);
|
|
481 printf("\tmovl (%s),%s\n",crn,crn);
|
|
482 return;
|
|
483 case POSTINC:
|
|
484 if (car(e2)==REGISTER) {
|
|
485 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
486 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2)));
|
|
487 return;
|
|
488 }
|
|
489 g_expr(e2);
|
|
490 emit_push();
|
|
491 /* in case of register full we should copy crn to drn */
|
|
492 xrn = emit_pop(0);
|
|
493 printf("\tmovl (%s),%s\n",xrn,crn);
|
|
494 printf("\taddl $%d,(%s)\n",caddr(e1),xrn);
|
|
495 return;
|
|
496 case CPOSTINC:
|
|
497 /* char *p; *p++ */
|
|
498 if (car(e2)==REGISTER) {
|
|
499 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
500 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2)));
|
|
501 return;
|
|
502 }
|
|
503 g_expr(e2);
|
|
504 emit_push(); /* in case of register full we should copy crn to drn */
|
|
505 xrn = emit_pop(0);
|
|
506 printf("\tmovsbl (%s),%s\n",xrn,crn);
|
|
507 printf("\tincl (%s)\n",xrn);
|
|
508 return;
|
|
509 case CPREINC:
|
|
510 if (car(e2)==REGISTER) {
|
|
511 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2)));
|
|
512 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
513 return;
|
|
514 }
|
|
515 g_expr(e2);
|
|
516 printf("\tincl (%s)\n",crn);
|
|
517 printf("\tmovsbl (%s),%s\n",crn,crn);
|
|
518 return;
|
|
519 case CPOSTDEC:
|
|
520 if (car(e2)==REGISTER) {
|
|
521 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
522 printf("\tdecl %s\n",register_name(cadr(e2)));
|
|
523 return;
|
|
524 }
|
|
525 g_expr(e2);
|
|
526 printf("\tmovsbl (%s),%s\n",crn,crn);
|
|
527 printf("\tdecl (%s)\n",crn);
|
|
528 return;
|
|
529 case CPREDEC:
|
|
530 if (car(e2)==REGISTER) {
|
|
531 printf("\tdecl %s\n",register_name(cadr(e2)));
|
|
532 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
533 return;
|
|
534 }
|
|
535 g_expr(e2);
|
|
536 emit_push();
|
|
537 xrn = emit_pop(0);
|
|
538 printf("\tdecl (%s)\n",xrn);
|
|
539 printf("\tmovsbl (%s),%s\n",xrn,crn);
|
|
540 return;
|
|
541 case MUL: case UMUL:
|
|
542 case DIV: case UDIV:
|
|
543 case MOD: case UMOD:
|
|
544 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
545 case ADD: case SUB: case BAND: case EOR: case BOR:
|
|
546 machinop(e1);
|
|
547 return;
|
|
548 case COND:
|
|
549 e2=fwdlabel();
|
|
550 b_expr(cadr(e1),0,e2);
|
|
551 g_expr(caddr(e1));
|
|
552 e4 = rname[creg];
|
|
553 jmp(e3=fwdlabel());
|
|
554 fwddef(e2);
|
|
555 g_expr(cadddr(e1));
|
|
556 use_register(creg,e4,1);
|
|
557 fwddef(e3);
|
|
558 return;
|
|
559 case SASS:
|
|
560 sassign(e1);
|
|
561 return;
|
|
562 case ASS: case CASS:
|
|
563 assign(e1);
|
|
564 return;
|
|
565 case ASSOP: case CASSOP:
|
|
566 assop(e1);
|
|
567 return;
|
|
568 case COMMA:
|
|
569 g_expr(e2);
|
|
570 g_expr(caddr(e1));
|
|
571 return;
|
|
572 case RETURN:
|
|
573 n = (NMTBL *)e2;
|
|
574 if (retcont==0)
|
|
575 retcont=fwdlabel();
|
|
576 printf("\tleal _%d,%s\n",retcont,crn);
|
|
577 return;
|
|
578 case ENVIRONMENT:
|
|
579 printf("\tmovl %%ebp,%s\n",crn);
|
|
580 return;
|
|
581 default:
|
|
582 b_expr(e1,1,e2=fwdlabel()); /* including > < ... */
|
|
583 printf("\txorl %s,%s\n",crn,crn);
|
|
584 jmp(e3=fwdlabel());
|
|
585 fwddef(e2);
|
|
586 printf("\tmovl $1,%s\n",crn);
|
|
587 fwddef(e3);
|
|
588 }
|
|
589 }
|
|
590
|
|
591 void
|
|
592 bexpr(int e1, char cond, int l1)
|
|
593 {
|
|
594 gexpr_init();
|
|
595 b_expr(e1,cond,l1);
|
|
596 }
|
|
597
|
|
598 void
|
|
599 b_expr(int e1, char cond, int l1)
|
|
600 {
|
|
601 int e2,l2;
|
|
602 if (chk) return;
|
|
603 e2=cadr(e1);
|
|
604 switch(car(e1)) {
|
|
605 case LNOT:
|
|
606 b_expr(e2,!cond,l1);
|
|
607 return;
|
|
608 case GT:
|
|
609 rexpr(e1,l1,cond?"g":"le");
|
|
610 return;
|
|
611 case UGT:
|
|
612 rexpr(e1,l1,cond?"a":"be");
|
|
613 return;
|
|
614 case GE:
|
|
615 rexpr(e1,l1,cond?"ge":"l");
|
|
616 return;
|
|
617 case UGE:
|
|
618 rexpr(e1,l1,cond?"ae":"b");
|
|
619 return;
|
|
620 case LT:
|
|
621 rexpr(e1,l1,cond?"l":"ge");
|
|
622 return;
|
|
623 case ULT:
|
|
624 rexpr(e1,l1,cond?"b":"ae");
|
|
625 return;
|
|
626 case LE:
|
|
627 rexpr(e1,l1,cond?"le":"g");
|
|
628 return;
|
|
629 case ULE:
|
|
630 rexpr(e1,l1,cond?"be":"a");
|
|
631 return;
|
|
632 case EQ:
|
|
633 rexpr(e1,l1,cond?"e":"ne");
|
|
634 return;
|
|
635 case NEQ:
|
|
636 rexpr(e1,l1,cond?"ne":"e");
|
|
637 return;
|
|
638 case LAND:
|
|
639 b_expr(e2,0,cond?(l2=fwdlabel()):l1);
|
|
640 b_expr(caddr(e1),cond,l1);
|
|
641 if(cond) fwddef(l2);
|
|
642 return;
|
|
643 case LOR:
|
|
644 b_expr(e2,1,cond?l1:(l2=fwdlabel()));
|
|
645 b_expr(caddr(e1),cond,l1);
|
|
646 if(!cond) fwddef(l2);
|
|
647 return;
|
|
648 case CRGVAR:
|
|
649 printf("\tcmpb $0,%s\n",caddr(e1));
|
|
650 jcond(l1,cond);
|
|
651 return;
|
|
652 case CRLVAR:
|
|
653 printf("\tcmpb $0,%d(%%ebp)\n",lvar(e2));
|
|
654 jcond(l1,cond);
|
|
655 return;
|
|
656 case RGVAR:
|
|
657 printf("\tcmpl $0,%s\n",caddr(e1));
|
|
658 jcond(l1,cond);
|
|
659 return;
|
|
660 case RLVAR:
|
|
661 printf("\tcmpl $0,%d(%%ebp)\n",lvar(e2));
|
|
662 jcond(l1,cond);
|
|
663 return;
|
|
664 case REGISTER:
|
|
665 printf("\tcmpl $0,%s\n",register_name(e2));
|
|
666 jcond(l1,cond);
|
|
667 return;
|
|
668 case CONST:
|
|
669 if((cond&&e2)||(!cond&&!e2)) jmp(l1);
|
|
670 return;
|
|
671 default:
|
|
672 g_expr(e1);
|
|
673 printf("\tcmpl $0,%s\n",crn);
|
|
674 jcond(l1,cond);
|
|
675 return;
|
|
676 }
|
|
677 }
|
|
678
|
|
679 void
|
|
680 ascii(char *s)
|
|
681 {
|
|
682 printf("\t.string \"");
|
|
683 while(*s) {
|
|
684 if (*s=='\n')
|
|
685 printf("%cn",92);
|
|
686 else if (*s<' ')
|
|
687 printf("%c%03o",92,*s);
|
|
688 else if (*s==34)
|
|
689 printf("%c%c",92,34);
|
|
690 else
|
|
691 printf("%c",*s);
|
|
692 s++;
|
|
693 }
|
|
694 printf("%c\n",34);
|
|
695 }
|
|
696
|
|
697 void
|
|
698 string(int e1)
|
|
699 {
|
|
700 char *s;
|
|
701 int i,lb;
|
|
702
|
|
703 if (0) {
|
|
704 s=(char *)cadr(e1);
|
|
705 lb=fwdlabel();
|
|
706 printf("\tjmp _%d\n",lb);
|
|
707 i=backdef();
|
|
708 ascii(s);
|
|
709 printf("\t.align 2\n");
|
|
710 fwddef(lb);
|
|
711 printf("\tlea _%d,%s\n",i,crn);
|
|
712 } else {
|
|
713 s=(char *)cadr(e1);
|
|
714 printf(".section\t.rodata\n");
|
|
715 lb=fwdlabel();
|
|
716 printf("_%d:\n",lb);
|
|
717 ascii(s);
|
|
718 if (output_mode==TEXT_EMIT_MODE) {
|
|
719 printf(".text\n");
|
|
720 } else {
|
|
721 text_mode();
|
|
722 }
|
|
723 printf("\tlea _%d,%s\n",lb,crn);
|
|
724 }
|
|
725 }
|
|
726
|
|
727 void
|
|
728 function(int e1)
|
|
729 {
|
|
730 int e2,e3,e4,e5,nargs;
|
|
731 NMTBL *n;
|
|
732
|
|
733 e2 = cadr(e1);
|
|
734 nargs = 0;
|
|
735 for (e3 = caddr(e1); e3; e3 = cadr(e3)) {
|
|
736 n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
|
|
737 switch(car(e4)) {
|
|
738 case FNAME:
|
|
739 printf("\tlea %s,%s\n",n->nm,crn);
|
|
740 printf("\tpushl %s\n",crn);
|
|
741 break;
|
|
742 case ADDRESS:
|
|
743 g_expr(e5);
|
|
744 printf("\tpushl %s\n",crn);
|
|
745 break;
|
|
746 default:g_expr(e4);
|
|
747 printf("\tpushl %s\n",crn);
|
|
748 }
|
|
749 ++nargs;
|
|
750 }
|
|
751 if (car(e2) == FNAME) {
|
|
752 n=(NMTBL *)cadr(e2);
|
|
753 use_register(creg,REG_EAX,0);
|
|
754 printf("\tcall\t%s\n",n->nm);
|
|
755 } else {
|
|
756 g_expr(e2);
|
|
757 use_register(creg,REG_EAX,0);
|
|
758 printf("\tcall\t*%s\n",crn);
|
|
759 }
|
|
760 if (nargs) printf("\taddl $%d,%%esp\n",4*nargs);
|
|
761 }
|
|
762
|
|
763 void
|
|
764 jump(int e1, int env)
|
|
765 {
|
|
766 int i,e2,e3,e4,e5,nargs,regs;
|
|
767 NMTBL *n,*code0;
|
|
768 int new_disp;
|
|
769 char *xrn;
|
|
770
|
|
771 /* count number of args */
|
|
772 nargs = 0;
|
|
773 for (e3 = caddr(e1); e3;e3 =cadr(e3)) nargs++;
|
|
774 new_disp = -(nargs-MAX_REGISTER_VAR)*int_size;
|
|
775 if (new_disp > 0)
|
|
776 new_disp=0;
|
|
777 /* printf("# new_disp %d,disp %d\n",new_disp,disp); */
|
|
778 if (new_disp < disp) {
|
|
779 printf("\tlea %d(%%ebp),%%esp\n",new_disp+disp_offset);
|
|
780 }
|
|
781 /* compute jump address */
|
|
782 e2 = cadr(e1);
|
|
783 if (car(e2) == FNAME) {
|
|
784 code0=(NMTBL *)cadr(e2);
|
|
785 if (code0->sc!=CODE) {
|
|
786 error(STERR); return;
|
|
787 }
|
|
788 } else { /* indirect */
|
|
789 g_expr(e2);
|
|
790 emit_push();
|
|
791 }
|
|
792 /* compute arguments */
|
|
793 regs = nargs-1;
|
|
794 i=0;
|
|
795 for (e3 = caddr(e1); e3;e3 =cadr(e3),i++) {
|
|
796 n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
|
|
797 switch(car(e4)) {
|
|
798 case FNAME:
|
|
799 printf("\tlea %s,%s\n",n->nm,crn);
|
|
800 emit_push();
|
|
801 break;
|
|
802 case ADDRESS:
|
|
803 g_expr(e5);
|
|
804 emit_push();
|
|
805 break;
|
|
806 /*
|
|
807 printf("# cadr(e4) %d ==disp_offset+(-1-i)*int_size %d\n", cadr(e4), disp_offset+(-2-i)*int_size);
|
|
808 */
|
|
809 case RLVAR:
|
|
810 case CRLVAR:
|
|
811 if (env==0 && cadr(e4)==disp_offset+(-1-i)*int_size) {
|
|
812 /* Same positioned local variable. No need to copy */
|
|
813 reg_stack[reg_sp++] = -2;
|
|
814 --regs;
|
|
815 continue;
|
|
816 }
|
|
817 case REGISTER:
|
|
818 if (rname[cadr(e4)]==REG_ESI+regs) {
|
|
819 /* Same register variable. No need to copy */
|
|
820 reg_stack[reg_sp++] = cadr(e4);
|
|
821 --regs;
|
|
822 continue;
|
|
823 }
|
|
824 default:
|
|
825 g_expr(e4);
|
|
826 emit_push();
|
|
827 }
|
|
828 --regs;
|
|
829 }
|
|
830 /* save frame pointer before jump in fuction */
|
|
831 if (fnptr->sc==FUNCTION) {
|
|
832 printf("\tmovl %%ebp,%d(%%ebp)\n",disp_offset);
|
|
833 }
|
|
834 if (env) {
|
|
835 /* change frame pointer */
|
|
836 g_expr(env);
|
|
837 printf("\tmovl %s,%%ebp\n",crn);
|
|
838 }
|
|
839 /* copy arguments to destination environment if necessary */
|
|
840 for(i=0;i<nargs;i++) {
|
|
841 if (i>=MAX_REGISTGER_VAR) {
|
|
842 if (reg_stack[reg_sp-1]== -2) {
|
|
843 /* same positioned variable */
|
|
844 reg_sp--;
|
|
845 } else {
|
|
846 xrn=emit_pop(0);
|
|
847 printf("\tmovl %s,%d(%%ebp)\n",xrn,
|
|
848 disp_offset+(-(nargs-i))*int_size);
|
|
849 }
|
|
850 } else if (reg_stack[--reg_sp]>=REG_ESI) {
|
|
851 /* the same registger */
|
|
852 ;
|
|
853 } else {
|
|
854 if(reg_stack[reg_sp]<0) {
|
|
855 printf("\tpopl %s\n",reg_name[rname[REG_ESI+i]]);
|
|
856 } else {
|
|
857 printf("\tmovl %s,%s\n",
|
|
858 reg_name[rname[reg_stack[reg_sp]]],
|
|
859 reg_name[rname[REG_ESI+i]]);
|
|
860 free_register(reg_stack[reg_sp]);
|
|
861 }
|
|
862 }
|
|
863 }
|
|
864 if (car(e2) != FNAME) {
|
|
865 xrn=emit_pop(0);
|
|
866 }
|
|
867 /* printf("# new_disp %d,disp %d\n",new_disp,disp); */
|
|
868 if (new_disp>disp) {
|
|
869 /* shrink stack if necessary */
|
|
870 printf("\tlea %d(%%ebp),%%esp\n",new_disp+disp_offset);
|
|
871 }
|
|
872 if (fnptr->sc==FUNCTION) {
|
|
873 /* link operation for leave, %ebp must point saved old %ebp */
|
|
874 printf("\tlea %d(%%ebp),%%ebp\n",disp_offset);
|
|
875 }
|
|
876 if (car(e2) == FNAME) {
|
|
877 printf("\tjmp %s\n",code0->nm);
|
|
878 } else {
|
|
879 printf("\tjmp *%s\n",xrn);
|
|
880 }
|
|
881 }
|
|
882
|
|
883
|
|
884 void
|
|
885 machinop(int e1)
|
|
886 {
|
|
887 int e2,e3,op;
|
|
888
|
|
889 e2 = cadr(e1);
|
|
890 op = car(e1);
|
|
891 e3 = caddr(e1);
|
|
892 g_expr(e3);
|
|
893 emit_push();
|
|
894 g_expr(e2);
|
|
895 tosop(car(e1));
|
|
896 return;
|
|
897 }
|
|
898
|
|
899
|
|
900 void
|
|
901 rindirect(int e1) /* *(p +5 ) */
|
|
902
|
|
903 {
|
|
904 char *op;
|
|
905 int e2,e3,byte;
|
|
906
|
|
907 op = ((byte = (car(e1) == CRINDIRECT)) ? "movsbl" : "movl");
|
|
908 e3 = cadr(e2 = cadr(e1));
|
|
909 g_expr(e2);
|
|
910 printf("\t%s (%s),%s\n",op,crn,crn);
|
|
911 }
|
|
912
|
|
913 void
|
|
914 sassign(int e1)
|
|
915 {
|
|
916 int e2,e3,e4,sz;
|
|
917 char *xrn;
|
|
918
|
|
919 e2 = cadr(e1);
|
|
920 e3 = cadr(e2);
|
|
921 e4 = caddr(e1);
|
|
922 sz = size(cadr(e2)); /* cld rep movsb */
|
|
923 g_expr(e4);
|
|
924 emit_push();
|
|
925 g_expr(e2);
|
|
926 xrn = emit_pop(0);
|
|
927 return;
|
|
928 }
|
|
929
|
|
930 void
|
|
931 assign(int e1)
|
|
932 {
|
|
933 char *op;
|
|
934 int e2,e3,e4,byte;
|
|
935 char *xrn;
|
|
936
|
|
937 op = ((byte=(car(e1) == CASS))? "movb" : "movl");
|
|
938 /* e2=e4 */
|
|
939 e2 = cadr(e1);
|
|
940 e3 = cadr(e2);
|
|
941 e4 = caddr(e1);
|
|
942 switch(car(e2)) {
|
|
943 case GVAR: /* i=3 */
|
|
944 g_expr(e4);
|
|
945 if (byte) use_data_reg(creg,1);
|
|
946 printf("\t%s %s,%s\n",op,crn,caddr(e2));
|
|
947 return;
|
|
948 case LVAR:
|
|
949 g_expr(e4);
|
|
950 if (byte) use_data_reg(creg,1);
|
|
951 printf("\t%s %s,%d(%%ebp)\n",op,crn,lvar(cadr(e2)));
|
|
952 return;
|
|
953 case REGISTER:
|
|
954 g_expr(e4);
|
|
955 if (creg!=cadr(e2))
|
|
956 printf("\t%s %s,%s\n",op,crn,register_name(cadr(e2)));
|
|
957 return;
|
|
958 }
|
|
959 g_expr(e4);
|
|
960 emit_push();
|
|
961 g_expr(e2);
|
|
962 xrn = emit_pop(0);
|
|
963 if (byte) use_data_reg(creg,1);
|
|
964 printf("\t%s %s,(%s)\n",op,xrn,crn);
|
|
965 printf("\tmovl %s,%s\n",xrn,crn);
|
|
966 return;
|
|
967 }
|
|
968
|
|
969 void
|
|
970 assop(int e1)
|
|
971 {
|
|
972 int e2,e3,byte,op,new_reg;
|
|
973 char *xrn;
|
|
974
|
|
975 /* e2 op= e3 */
|
|
976 byte = (car(e1) == CASSOP);
|
|
977 e2 = cadr(e1);
|
|
978 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
979 e3 = caddr(e1);
|
|
980 op = cadddr(e1);
|
|
981
|
|
982 g_expr(e3);
|
|
983 emit_push();
|
|
984 if (car(e2)==REGISTER) {
|
|
985 new_reg=creg;
|
|
986 set_crn(cadr(e2));
|
|
987 tosop(op);
|
|
988 set_crn(new_reg);
|
|
989 printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn);
|
|
990 return;
|
|
991 }
|
|
992 g_expr(e2);
|
|
993 xrn = emit_pop(0); /* pop e3 value */
|
|
994 printf("\tpushl %s # assop \n",crn); /* push e2 address */
|
|
995 ld_indexx(byte,0,crn);
|
|
996 new_reg = get_register();
|
|
997 /* push e3 value */
|
|
998 if(new_reg<0) { /* もうレジスタがない */
|
|
999 reg_stack[reg_sp++] = -1;
|
|
1000 printf("\tpushl %s\n",xrn);
|
|
1001 } else {
|
|
1002 reg_stack[reg_sp++] = xreg; /* push するかわりにレジスタを使う */
|
|
1003 }
|
|
1004 tosop(op);
|
|
1005 if(new_reg>=0) free_register(new_reg);
|
|
1006 printf("\tpopl %s # assop \n",drn);
|
|
1007 printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",crn,drn);
|
|
1008 return;
|
|
1009
|
|
1010 }
|
|
1011
|
|
1012
|
|
1013 void
|
|
1014 tosop(int op)
|
|
1015 {
|
|
1016 int oreg;
|
|
1017 char *orn;
|
|
1018
|
|
1019 oreg = pop_register();
|
|
1020 switch(op) {
|
|
1021 case LSHIFT:
|
|
1022 case ULSHIFT:
|
|
1023 shift("sall",oreg);
|
|
1024 return;
|
|
1025 case RSHIFT:
|
|
1026 shift("sarl",oreg);
|
|
1027 return;
|
|
1028 case URSHIFT:
|
|
1029 shift("shrl",oreg);
|
|
1030 return;
|
|
1031 }
|
|
1032 if(oreg==-1) {
|
|
1033 printf("\tpopl %s\n",drn);
|
|
1034 oreg = dreg;
|
|
1035 }
|
|
1036 orn = reg_name[rname[oreg]];
|
|
1037 switch(op) {
|
|
1038 case ADD:
|
|
1039 printf("\taddl %s,%s\n",orn,crn);
|
|
1040 return;
|
|
1041 case SUB:
|
|
1042 printf("\tsubl %s,%s\n",orn,crn);
|
|
1043 return;
|
|
1044 case BAND:
|
|
1045 printf("\tandl %s,%s\n",orn,crn);
|
|
1046 return;
|
|
1047 case EOR:
|
|
1048 printf("\txorl %s,%s\n",orn,crn);
|
|
1049 return;
|
|
1050 case BOR:
|
|
1051 printf("\torl %s,%s\n",orn,crn);
|
|
1052 return;
|
|
1053 case MUL:
|
|
1054 case UMUL:
|
|
1055 /* use_register(dreg,REG_EDX,0); */
|
|
1056 printf("\t%s %s,%s\n","imull",orn,crn);
|
|
1057 return;
|
|
1058 case DIV:
|
|
1059 case UDIV:
|
|
1060 orn = div_setup(oreg);
|
|
1061 if (op==DIV)
|
|
1062 printf("\tcltd\n\tdivl %s\n",orn);
|
|
1063 else
|
|
1064 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn);
|
|
1065 div_cleanup(orn);
|
|
1066 return;
|
|
1067 case MOD:
|
|
1068 case UMOD:
|
|
1069 orn = div_setup(oreg);
|
|
1070 if (op==DIV)
|
|
1071 printf("\tcltd\n\tdivl %s\n",orn);
|
|
1072 else
|
|
1073 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn);
|
|
1074 rname[creg]=REG_EDX;
|
|
1075 crn=reg_name[REG_EDX];
|
|
1076 rname[dreg]=REG_EAX;
|
|
1077 drn=reg_name[REG_EAX];
|
|
1078 div_cleanup(orn);
|
|
1079 return;
|
|
1080 }
|
|
1081 }
|
|
1082
|
|
1083 static char *dvi_push = "(%esp)";
|
|
1084
|
|
1085 char *
|
|
1086 div_setup(int oreg)
|
|
1087 {
|
|
1088 if (oreg<0) {
|
|
1089 use_register(creg,REG_EAX,1);
|
|
1090 use_register(dreg,REG_EDX,0);
|
|
1091 return 0;
|
|
1092 }
|
|
1093 if (register_full()) {
|
|
1094 printf("\tpushl %s\n",reg_name[rname[oreg]]);
|
|
1095 use_register(creg,REG_EAX,1);
|
|
1096 use_register(oreg,REG_EDX,0);
|
|
1097 return dvi_push;
|
|
1098 }
|
|
1099 regs[oreg]=1;
|
|
1100 use_register(creg,REG_EAX,1);
|
|
1101 use_register(dreg,REG_EDX,0);
|
|
1102 regs[oreg]=0;
|
|
1103 return reg_name[rname[oreg]];
|
|
1104 }
|
|
1105
|
|
1106 div_cleanup(char *orn)
|
|
1107 {
|
|
1108 if (orn==dvi_push)
|
|
1109 printf("\taddl $4,%%esp\n");
|
|
1110 }
|
|
1111
|
|
1112 void
|
|
1113 shift(char *op, int reg)
|
|
1114 {
|
|
1115 if (reg>=0) {
|
|
1116 use_register(lreg,REG_ECX,1);
|
|
1117 } else {
|
|
1118 use_register(dreg,REG_ECX,0);
|
|
1119 printf("\tpopl %%ecx\n");
|
|
1120 }
|
|
1121 printf("\t%s %%cl,%s\n",op,crn);
|
|
1122 }
|
|
1123
|
|
1124 void
|
|
1125 ld_indexx(int byte, int n, char *xrn)
|
|
1126 {
|
|
1127 char *op;
|
|
1128
|
|
1129 op = byte ? "movsbl" : "movl";
|
|
1130 if (n)
|
|
1131 printf("\t%s %d(%s),%s\n",op,n,xrn,crn);
|
|
1132 else
|
|
1133 printf("\t%s (%s),%s\n",op,xrn,crn);
|
|
1134 }
|
|
1135
|
|
1136 void
|
|
1137 st_indexx(int byte, int n, char *xrn)
|
|
1138 {
|
|
1139 char *op;
|
|
1140
|
|
1141 op = byte ? "movb" : "movl";
|
|
1142 if (n)
|
|
1143 printf("\t%s %s,%d(%s)\n",op,crn,n,xrn);
|
|
1144 else
|
|
1145 printf("\t%s %s,(%s)\n",op,crn,xrn);
|
|
1146 }
|
|
1147
|
|
1148 void
|
|
1149 indexy(char *op, char *gvar)
|
|
1150 {
|
|
1151 printf("\t%s %s,%s\n",op,crn,gvar);
|
|
1152 }
|
|
1153
|
|
1154 void
|
|
1155 cmpdimm(int e, int csreg)
|
|
1156 {
|
|
1157 /* used in dosiwtch() */
|
|
1158 use_register(creg,csreg,0);
|
|
1159 printf("\tcmpl $%d,%s\n",e,crn);
|
|
1160 }
|
|
1161
|
|
1162 void
|
|
1163 opening(char *filename)
|
|
1164 {
|
|
1165 printf("\t.file \"%s\"\n",filename);
|
|
1166 printf("\t.version\t\"01.01\"\n");
|
|
1167 printf("gcc2_compiled.:\n");
|
|
1168 printf(".text\n");
|
|
1169 emit_init();
|
|
1170 }
|
|
1171
|
|
1172 void
|
|
1173 closing(void)
|
|
1174 {
|
|
1175 global_table();
|
|
1176 printf("\t.ident \"Micro-C compiled\"\n");
|
|
1177 }
|
|
1178
|
|
1179 void
|
|
1180 jmp_label(int l)
|
|
1181 {
|
|
1182 printf("\tjmp\t_%d\n",l);
|
|
1183 }
|
|
1184
|
|
1185 void
|
|
1186 jmp_eq_label(int l)
|
|
1187 {
|
|
1188 printf("\tje\t_%d\n",l);
|
|
1189 }
|
|
1190
|
|
1191 void
|
|
1192 rexpr(int e1, int l1, char *s)
|
|
1193 {
|
|
1194 g_expr(list3(SUB,cadr(e1),caddr(e1)));
|
|
1195 printf("\tj%s\t_%d\n",s,l1);
|
|
1196 }
|
|
1197
|
|
1198 void
|
|
1199 jcond(int l, char cond)
|
|
1200 {
|
|
1201 printf("\tj%s\t_%d\n",cond?"ne":"e",l);
|
|
1202 }
|
|
1203
|
|
1204 void
|
|
1205 jmp(int l)
|
|
1206 {
|
|
1207 control=0;
|
|
1208 printf("\tjmp\t_%d\n",l);
|
|
1209 /* align? */
|
|
1210 }
|
|
1211
|
|
1212 int
|
|
1213 fwdlabel(void)
|
|
1214 {
|
|
1215 return labelno++;
|
|
1216 }
|
|
1217
|
|
1218 void
|
|
1219 fwddef(int l)
|
|
1220 {
|
|
1221 control=1;
|
|
1222 printf("_%d:\n",l);
|
|
1223 }
|
|
1224
|
|
1225 int
|
|
1226 backdef(void)
|
|
1227 {
|
|
1228 control=1;
|
|
1229 printf("_%d:\n",labelno);
|
|
1230 return labelno++;
|
|
1231 }
|
|
1232
|
|
1233 void
|
|
1234 def_label(int cslabel, int dlabel)
|
|
1235 {
|
|
1236 int fl;
|
|
1237
|
|
1238 fl = 0;
|
|
1239 if (control) {
|
|
1240 jmp(fl=fwdlabel());
|
|
1241 }
|
|
1242 fwddef(cslabel);
|
|
1243 if (dlabel)
|
|
1244 jmp(dlabel);
|
|
1245 if (fl) {
|
|
1246 fwddef(fl);
|
|
1247 }
|
|
1248 }
|
|
1249
|
|
1250 void
|
|
1251 gen_comment(char *s)
|
|
1252 {
|
|
1253 printf("# %s",s);
|
|
1254 }
|
|
1255
|
|
1256 void
|
|
1257 gen_source(char *s)
|
|
1258 {
|
|
1259 printf("%s",s);
|
|
1260 }
|
|
1261
|
|
1262 void
|
|
1263 code_enter(char *name)
|
|
1264 {
|
|
1265 disp_offset = code_disp_offset;
|
|
1266 emit_init();
|
|
1267 printf("\t.align 4\n");
|
|
1268 if (stmode!=STATIC)
|
|
1269 printf(".globl %s\n",name);
|
|
1270 printf("\t.type\t%s,@function\n",name);
|
|
1271 printf("%s:\n",name);
|
|
1272 }
|
|
1273
|
|
1274 void
|
|
1275 code_enter1(int disp)
|
|
1276 {
|
|
1277 if(disp) {
|
|
1278 printf("\tsubl $%d,%%esp\n",-disp);
|
|
1279 }
|
|
1280 }
|
|
1281
|
|
1282 void
|
|
1283 code_leave(char *name)
|
|
1284 {
|
|
1285 local_table();
|
|
1286 printf("_%d:\n",labelno);
|
|
1287 printf("\t.size\t%s,_%d-%s\n",name,labelno,name);
|
|
1288 labelno++;
|
|
1289 }
|
|
1290
|
|
1291 void
|
|
1292 enter(char *name)
|
|
1293 {
|
|
1294 disp_offset = func_disp_offset;
|
|
1295 emit_init();
|
|
1296 printf("\t.align 2\n");
|
|
1297 if (stmode!=STATIC)
|
|
1298 printf(".globl %s\n",name);
|
|
1299 printf("%s:\n",name);
|
|
1300 printf("\t.type\t%s,@function\n",name);
|
|
1301 printf("\tpushl %%ebp\n");
|
|
1302 printf("\tmovl %%esp,%%ebp\n");
|
|
1303 printf("\tpushl %%ebx\n");
|
|
1304 printf("\tpushl %%ecx\n");
|
|
1305 printf("\tpushl %%edx\n");
|
|
1306 if (REGISTERS6) {
|
|
1307 printf("\tpushl %%edi\n");
|
|
1308 printf("\tpushl %%esi\n");
|
|
1309 }
|
|
1310 }
|
|
1311
|
|
1312 void
|
|
1313 enter1(int disp)
|
|
1314 {
|
|
1315 if(disp) printf("\tsubl $%d,%%esp\n",-disp);
|
|
1316 }
|
|
1317
|
|
1318 void
|
|
1319 leave(int control, char *name)
|
|
1320 {
|
|
1321 if (control)
|
|
1322 use_register(creg,REG_EAX,1);
|
|
1323 if (retcont) {
|
|
1324 if (control)
|
|
1325 jmp(retlabel);
|
|
1326 fwddef(retcont);
|
|
1327 use_register(creg,REG_EAX,0);
|
|
1328 printf("\tmovl %s,%s\n",reg_name[REG_ESI],crn);
|
|
1329 /* printf("\tleave\n"); */
|
|
1330 }
|
|
1331 fwddef(retlabel);
|
|
1332 use_register(creg,REG_EAX,0);
|
|
1333 if (REGISTERS6) {
|
|
1334 printf("\tpopl %%esi\n");
|
|
1335 printf("\tpopl %%edi\n");
|
|
1336 }
|
|
1337 /* if(disp) printf("\taddl $%d,%%esp\n",-disp); */
|
|
1338 printf("\tlea %d(%%ebp),%%esp\n",disp_offset);
|
|
1339 printf("\tpopl %%edx\n");
|
|
1340 printf("\tpopl %%ecx\n");
|
|
1341 printf("\tpopl %%ebx\n");
|
|
1342 printf("\tleave\n");
|
|
1343 printf("\tret\n");
|
|
1344 local_table();
|
|
1345 printf("_%d:\n",labelno);
|
|
1346 printf("\t.size\t%s,_%d-%s\n",name,labelno,name);
|
|
1347 labelno++;
|
|
1348 }
|
|
1349
|
|
1350
|
|
1351 void
|
|
1352 ret(void)
|
|
1353 {
|
|
1354 use_register(creg,REG_EAX,1);
|
|
1355 jmp(retlabel);
|
|
1356 }
|
|
1357
|
|
1358 void
|
|
1359 gen_gdecl(char *n, int gpc)
|
|
1360 {
|
|
1361 /*
|
|
1362 if (stmode!=STATIC)
|
|
1363 printf(".globl %s\n",n);
|
|
1364 */
|
|
1365 }
|
|
1366
|
|
1367 void
|
|
1368 align(int t)
|
|
1369 {
|
|
1370 if (t!=CHAR) {
|
|
1371 if (data_alignment & 1)
|
|
1372 printf("\t.align 2\n");
|
|
1373 data_alignment = 0;
|
|
1374 }
|
|
1375 }
|
|
1376
|
|
1377 void
|
|
1378 emit_data(int e, int t, NMTBL *n)
|
|
1379 {
|
|
1380 int l;
|
|
1381 char *name;
|
|
1382 name = n->nm;
|
|
1383 if(mode==GDECL) { /* global */
|
|
1384 if (n->dsp != -1) {
|
|
1385 n->dsp = -1; /* initiallized flag */
|
|
1386 printf(".globl\t%s\n",name);
|
|
1387 data_mode(name);
|
|
1388 align(t);
|
|
1389 printf("%s:\n",name);
|
|
1390 } else {
|
|
1391 data_mode(0);
|
|
1392 }
|
|
1393 if(car(e)==CONST) {
|
|
1394 if (t==CHAR) {
|
|
1395 printf("\t.byte %d\n",cadr(e));
|
|
1396 if (data_alignment>0)
|
|
1397 data_alignment++;
|
|
1398 gpc += 1;
|
|
1399 } else {
|
|
1400 printf("\t.long %d\n",cadr(e));
|
|
1401 gpc += int_size;
|
|
1402 }
|
|
1403 } else if(t!=CHAR) {
|
|
1404 gpc += int_size;
|
|
1405 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) {
|
|
1406 printf("\t.long %s\n",caddr(cadr(e)));
|
|
1407 } else if(car(e)==FNAME) {
|
|
1408 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm);
|
|
1409 } else if(car(e)==STRING) {
|
|
1410 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
|
|
1411 l = fwdlabel();
|
|
1412 printf("\t.long _%d\n",l);
|
|
1413 printf(".section\t.rodata\n");
|
|
1414 printf("_%d:\n",l);
|
|
1415 output_mode = RODATA_EMIT_MODE;
|
|
1416 }
|
|
1417 ascii((char *)cadr(e));
|
|
1418 } else error(TYERR);
|
|
1419 }
|
|
1420 } else {
|
|
1421 /* if (n->sc!=LVAR) { error(TYERR); return; } */
|
|
1422 if(car(e)==CONST) {
|
|
1423 printf("\tmovl $%d,%s\n",cadr(e),crn);
|
|
1424 printf("\t%s %s,%d(%%ebp)\n",t==CHAR?"movsbl":"movl",crn,n->dsp);
|
|
1425 } else if(t!=CHAR) {
|
|
1426 if(car(e)==ADDRESS&&car(cadr(e))==GVAR)
|
|
1427 printf("\tlea %s,%s\n",caddr(e),crn);
|
|
1428 else if(car(e)==FNAME)
|
|
1429 printf("\tlea %s,%s\n",((NMTBL *)cadr(e))->nm,crn);
|
|
1430 else if(car(e)==STRING) {
|
|
1431 string(e);
|
|
1432 } else error(TYERR);
|
|
1433 printf("\tmovl %s,%d(%%ebp)\n",crn,n->dsp);
|
|
1434 }
|
|
1435 }
|
|
1436 }
|
|
1437
|
|
1438 void
|
|
1439 emit_data_closing(NMTBL *n)
|
|
1440 {
|
|
1441 int lb;
|
|
1442 if (mode=GDECL) {
|
|
1443 data_mode(0);
|
|
1444 lb=fwdlabel();
|
|
1445 printf("_%d:\n",lb);
|
|
1446 printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm);
|
|
1447 }
|
|
1448 }
|
|
1449
|
|
1450 void
|
|
1451 global_table(void)
|
|
1452 {
|
|
1453 NMTBL *n;
|
|
1454 for(n=ntable;n < &ntable[GSYMS];n++) {
|
|
1455 if (n->sc == GVAR && n->dsp != -1) {
|
|
1456 /* n->dsp = -1 means initialized global */
|
|
1457 printf(".comm %s,%d\n",n->nm,size(n->ty));
|
|
1458 }
|
|
1459 }
|
|
1460 }
|
|
1461
|
|
1462 void
|
|
1463 local_table(void)
|
|
1464 {
|
|
1465 NMTBL *n;
|
|
1466 /* static local variables */
|
|
1467 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) {
|
|
1468 if (n->sc == GVAR) {
|
|
1469 printf(".lcomm %s,%d\n",n->nm,size(n->ty));
|
|
1470 }
|
|
1471 }
|
|
1472 }
|
|
1473
|
|
1474 void
|
|
1475 text_mode(void)
|
|
1476 {
|
|
1477 if (output_mode!=TEXT_EMIT_MODE) {
|
|
1478 printf(".text\n");
|
|
1479 printf("\t.align 2\n");
|
|
1480 output_mode = TEXT_EMIT_MODE;
|
|
1481 }
|
|
1482 }
|
|
1483
|
|
1484 void
|
|
1485 data_mode(char *name)
|
|
1486 {
|
|
1487 if (output_mode!=DATA_EMIT_MODE) {
|
|
1488 printf(".data\n");
|
|
1489 output_mode = DATA_EMIT_MODE;
|
|
1490 }
|
|
1491 if (name)
|
|
1492 printf("\t.type\t%s,@object\n",name);
|
|
1493 }
|
|
1494
|
|
1495 int
|
|
1496 lvar(int l)
|
|
1497 {
|
|
1498 if (l<0) {
|
|
1499 if (fnptr->sc==CODE)
|
|
1500 return l+code_disp_offset;
|
|
1501 else
|
|
1502 return l+disp_offset;
|
|
1503 } else {
|
|
1504 return l+arg_offset;
|
|
1505 }
|
|
1506 }
|
|
1507
|
|
1508 /* end */
|