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