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