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