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