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