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