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