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