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