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