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