Mercurial > hg > CbC > old > device
annotate mc-code-ia32.c @ 139:742224f4da02
*** empty log message ***
author | kono |
---|---|
date | Tue, 06 May 2003 02:36:55 +0900 |
parents | e6e77af048a1 |
children | 56211702f298 |
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)); |
1135 } | |
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)); | |
1146 } | |
1147 | |
1148 | |
1149 void | |
1150 code_assop(int op,int byte) { | |
1151 char *xrn; | |
1152 int xreg; | |
1153 int edx = edx_setup(); | |
1154 xrn = register_name(xreg = emit_pop(0),0); /* pop e3 value */ | |
1155 regv[xreg]=regs[xreg]=1; | |
1156 printf("\tmovl %s,%s # assop \n",register_name(creg,0),register_name(edx,0)); | |
1157 regv[edx]=1; | |
1158 ld_indexx(byte,0,edx); | |
1159 tosop(op,xreg); | |
1160 printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",register_name(creg,byte),register_name(edx,0)); | |
1161 edx_cleanup(); | |
1162 emit_pop_free(xreg); | |
1163 } | |
1164 | |
1165 | |
1166 void | |
1167 tosop(int op,int oreg) | |
1168 { | |
1169 int dx; | |
1170 char *orn,*crn; | |
1171 | |
1172 switch(op) { | |
1173 case LSHIFT: | |
1174 case ULSHIFT: | |
1175 shift("sall",oreg); | |
1176 return; | |
1177 case RSHIFT: | |
1178 shift("sarl",oreg); | |
1179 return; | |
1180 case URSHIFT: | |
1181 shift("shrl",oreg); | |
1182 return; | |
1183 } | |
1184 if(oreg==-1) { | |
1185 printf("\tpopl %s\n",register_name(dreg,0)); | |
1186 oreg = dreg; | |
1187 regv[dreg]=1; | |
94 | 1188 } else if (oreg<= -REG_LVAR_OFFSET) { |
119 | 1189 code_rlvar(oreg+REG_LVAR_OFFSET,dreg); |
117 | 1190 free_lvar(oreg+REG_LVAR_OFFSET); |
94 | 1191 oreg = dreg; |
1192 regv[dreg]=1; | |
61 | 1193 } |
1194 regv[oreg]=1; regs[oreg]=1; | |
1195 orn = register_name(oreg,0); | |
1196 crn = register_name(creg,0); | |
1197 switch(op) { | |
1198 case ADD: | |
1199 printf("\taddl %s,%s\n",orn,crn); | |
1200 break; | |
89 | 1201 case SUB: case CMP: |
61 | 1202 printf("\tsubl %s,%s\n",orn,crn); |
1203 break; | |
1204 case BAND: | |
1205 printf("\tandl %s,%s\n",orn,crn); | |
1206 break; | |
1207 case EOR: | |
1208 printf("\txorl %s,%s\n",orn,crn); | |
1209 break; | |
1210 case BOR: | |
1211 printf("\torl %s,%s\n",orn,crn); | |
1212 break; | |
1213 case MUL: | |
1214 case UMUL: | |
1215 printf("\t%s %s,%s\n","imull",orn,crn); | |
1216 break; | |
1217 case DIV: | |
1218 case UDIV: | |
1219 use_register(creg,REG_EAX,1); | |
1220 edx_setup(); | |
1221 orn = register_name(oreg,0); | |
1222 if (op==DIV) | |
127 | 1223 printf("\tcltd\n\tidivl %s\n",orn); |
61 | 1224 else |
127 | 1225 printf("\txor %%edx,%%edx\n\tdivl %s\n",orn); |
61 | 1226 edx_cleanup(); |
1227 break; | |
1228 case MOD: | |
1229 case UMOD: | |
1230 use_register(creg,REG_EAX,1); | |
1231 edx_setup(); | |
1232 orn = register_name(oreg,0); | |
1233 if (op==DIV) | |
127 | 1234 printf("\tcltd\n\tidivl %s\n",orn); |
61 | 1235 else |
127 | 1236 printf("\txor %%edx,%%edx\n\tdivl %s\n",orn); |
61 | 1237 dx = virtual(REG_EDX); |
1238 if (dx!=creg) { | |
1239 rname[dx]=rname[creg]; | |
1240 rname[creg]=REG_EDX; | |
1241 } | |
1242 edx_cleanup(); | |
1243 break; | |
1244 } | |
1245 if (oreg!=dreg&&oreg>=0) | |
1246 free_register(oreg); | |
94 | 1247 else if (oreg==dreg) regv[dreg]=0; |
61 | 1248 } |
1249 | |
1250 static int edx_stack=0; | |
1251 | |
1252 int | |
1253 edx_setup() | |
1254 { | |
1255 int edx_save; | |
1256 /* make real EDX register empty */ | |
137 | 1257 if (free_register_count(0)<1) { |
61 | 1258 for(edx_save = 0;edx_save==dreg||edx_save==creg;edx_save++); |
1259 printf("\tpushl %s\n",register_name(edx_save,0)); | |
1260 edx_stack = list3(edx_save,edx_stack,0); | |
1261 } else { | |
1262 edx_save = get_register(); | |
1263 edx_stack = list3(edx_save,edx_stack,1); | |
1264 } | |
1265 regv[edx_save]=0; | |
1266 use_register(edx_save,REG_EDX,0); | |
1267 return edx_save; | |
1268 } | |
1269 | |
1270 | |
1271 void | |
1272 edx_cleanup() | |
1273 { | |
1274 if (caddr(edx_stack)==0) { | |
1275 printf("\tpopl %s\n",register_name(car(edx_stack),0)); | |
1276 } else | |
1277 free_register(car(edx_stack)); | |
1278 edx_stack = cadr(edx_stack); | |
1279 } | |
1280 | |
1281 void | |
1282 shift(char *op, int reg) | |
1283 { | |
1284 if (reg>=0) { | |
1285 use_register(reg,REG_ECX,1); | |
103 | 1286 } else if (reg<= -REG_LVAR_OFFSET) { |
1287 use_register(dreg,REG_ECX,0); | |
119 | 1288 code_rlvar(reg+REG_LVAR_OFFSET,dreg); |
103 | 1289 reg = dreg; |
1290 regv[dreg]=0; | |
61 | 1291 } else { |
1292 use_register(dreg,REG_ECX,0); | |
1293 printf("\tpopl %%ecx\n"); | |
103 | 1294 regv[dreg]=0; |
61 | 1295 } |
1296 printf("\t%s %%cl,%s\n",op,register_name(creg,0)); | |
1297 } | |
1298 | |
1299 void | |
1300 ld_indexx(int byte, int n, int xreg) | |
1301 { | |
1302 char *op; | |
1303 | |
1304 op = byte ? "movsbl" : "movl"; | |
1305 if (n) | |
95 | 1306 printf("\t%s %d(%s),%s\n",op,n, |
1307 register_name(xreg,0),register_name(creg,byte)); | |
61 | 1308 else |
95 | 1309 printf("\t%s (%s),%s\n",op, |
1310 register_name(xreg,0),register_name(creg,byte)); | |
1311 } | |
1312 | |
1313 int | |
1314 code_csvalue() | |
1315 { | |
123 | 1316 return rname[creg]; /* for switch value */ |
61 | 1317 } |
1318 | |
1319 void | |
95 | 1320 code_cmpdimm(int e, int csreg) |
61 | 1321 { |
1322 /* used in dosiwtch() */ | |
66 | 1323 if(chk) return; |
123 | 1324 use_register(creg,csreg,0); |
61 | 1325 printf("\tcmpl $%d,%s\n",e,register_name(creg,0)); |
1326 } | |
1327 | |
1328 void | |
66 | 1329 code_opening(char *filename) |
61 | 1330 { |
1331 printf("\t.file \"%s\"\n",filename); | |
1332 printf("\t.version\t\"01.01\"\n"); | |
66 | 1333 /* printf("gcc2_compiled.:\n"); */ |
61 | 1334 printf(".text\n"); |
1335 } | |
1336 | |
1337 void | |
66 | 1338 code_closing() |
61 | 1339 { |
78 | 1340 global_table(); |
61 | 1341 printf("\t.ident \"Micro-C compiled\"\n"); |
1342 } | |
1343 | |
1344 void | |
89 | 1345 rexpr(int e1, int l1, char *s,int t) |
61 | 1346 { |
89 | 1347 g_expr(list3(CMP,cadr(e1),caddr(e1))); |
61 | 1348 printf("\tj%s\t_%d\n",s,l1); |
1349 } | |
1350 | |
82 | 1351 |
1352 void | |
61 | 1353 jcond(int l, char cond) |
1354 { | |
66 | 1355 if (chk) return; |
61 | 1356 printf("\tj%s\t_%d\n",cond?"ne":"e",l); |
1357 } | |
1358 | |
1359 void | |
1360 jmp(int l) | |
1361 { | |
1362 control=0; | |
66 | 1363 if (chk) return; |
61 | 1364 printf("\tjmp\t_%d\n",l); |
1365 /* align? */ | |
1366 /* | |
1367 this is not allowed because of ? operator | |
1368 regv[creg]=regv[dreg]=0; | |
1369 use_register(creg,REG_EAX,0); | |
1370 use_register(dreg,REG_EBX,0); | |
1371 */ | |
1372 } | |
1373 | |
1374 void | |
1375 gen_comment(char *s) | |
1376 { | |
66 | 1377 if (chk) return; |
1378 printf("## %s",s); | |
61 | 1379 } |
1380 | |
1381 | |
1382 void | |
1383 code_enter(char *name) | |
1384 { | |
1385 printf("\t.align 4\n"); | |
1386 if (stmode!=STATIC) | |
1387 printf(".globl %s\n",name); | |
1388 printf("\t.type\t%s,@function\n",name); | |
1389 printf("%s:\n",name); | |
1390 } | |
1391 | |
77 | 1392 |
61 | 1393 void |
1394 code_enter1(int args) | |
1395 { | |
1396 code_disp_label=fwdlabel(); | |
1397 printf("\tlea _%d(%%ebp),%%esp\n",code_disp_label); | |
1398 | |
1399 printf("## args %d disp %d code_arg_offset=%d code_disp_offset=%d\n",args,disp,code_arg_offset,code_disp_offset); | |
1400 } | |
1401 | |
1402 void | |
1403 code_leave(char *name) | |
1404 { | |
63 | 1405 disp&= -size_of_int; |
61 | 1406 printf("\t.set _%d,%d\n",code_disp_label,disp+code_disp_offset); |
1407 printf("_%d:\n",labelno); | |
1408 printf("\t.size\t%s,_%d-%s\n",name,labelno,name); | |
1409 local_table(); | |
1410 labelno++; | |
1411 free_all_register(); | |
1412 } | |
1413 | |
1414 void | |
1415 enter(char *name) | |
1416 { | |
1417 printf("\t.align 2\n"); | |
1418 if (stmode!=STATIC) | |
1419 printf(".globl %s\n",name); | |
1420 printf("%s:\n",name); | |
1421 printf("\t.type\t%s,@function\n",name); | |
1422 printf("\tpushl %%ebp\n"); | |
1423 printf("\tmovl %%esp,%%ebp\n"); | |
1424 printf("\tpushl %%ebx\n"); | |
1425 printf("\tpushl %%esi\n"); | |
1426 printf("\tpushl %%edi\n"); | |
1427 } | |
1428 | |
1429 void | |
1430 enter1() | |
1431 { | |
1432 func_disp_label=fwdlabel(); | |
1433 printf("\tlea _%d(%%ebp),%%esp\n",func_disp_label); | |
1434 /* if(disp) printf("\tsubl $%d,%%esp\n",-disp); */ | |
1435 } | |
1436 | |
1437 void | |
1438 leave(int control, char *name) | |
1439 { | |
128 | 1440 int sz; |
1441 | |
1442 disp &= -size_of_int; | |
61 | 1443 if (control) |
108 | 1444 code_set_return_register(1); |
61 | 1445 if (retcont) { |
1446 if (control) | |
1447 jmp(retlabel); | |
1448 fwddef(retcont); | |
128 | 1449 if (cadr(fnptr->ty)==FLOAT||cadr(fnptr->ty)==DOUBLE) { |
1450 printf("\tfldl %d(%%ebp)\n",-size_of_double); | |
1451 } else if (cadr(fnptr->ty)>0&&( | |
1452 car(cadr(fnptr->ty))==STRUCT || | |
1453 car(cadr(fnptr->ty))==UNION)) { | |
1454 sz = size(cadr(fnptr->ty)); | |
1455 printf("\tlea %d(%%ebp),%s\n",-sz,register_name(dreg,0)); | |
1456 printf("\tmovl %d(%%ebp),%s\n",disp-size_of_int, | |
1457 register_name(creg,0)); | |
1458 emit_copy(dreg,creg,sz,0,1,1); | |
1459 } else if (cadr(fnptr->ty)!=VOID) { | |
1460 use_register(creg,REG_EAX,0); | |
1461 printf("\tmovl %s,%s\n",reg_name[REG_ESI],register_name(creg,0)); | |
1462 } | |
61 | 1463 } |
1464 fwddef(retlabel); | |
63 | 1465 |
61 | 1466 printf("\tlea %d(%%ebp),%%esp\n",disp_offset); |
1467 printf("\tpopl %%edi\n"); | |
1468 printf("\tpopl %%esi\n"); | |
1469 printf("\tpopl %%ebx\n"); | |
1470 printf("\tleave\n"); | |
1471 printf("\tret\n"); | |
1472 printf("\t.set _%d,%d\n",func_disp_label,disp+disp_offset); | |
1473 printf("_%d:\n",labelno); | |
1474 printf("\t.size\t%s,_%d-%s\n",name,labelno,name); | |
1475 local_table(); | |
1476 labelno++; | |
1477 free_all_register(); | |
1478 } | |
1479 | |
1480 | |
1481 void | |
108 | 1482 code_set_fixed_creg(int mode,int type) { |
1483 if (type==FLOAT||type==DOUBLE) { | |
1484 } else | |
1485 use_register(creg,REG_EAX,mode); | |
61 | 1486 } |
1487 | |
1488 void | |
107 | 1489 code_set_return_register(int mode) { |
1490 if (fnptr->ty==DOUBLE||fnptr->ty==FLOAT) { | |
1491 } else { | |
1492 use_register(creg,REG_EAX,mode); | |
1493 } | |
1494 } | |
1495 | |
1496 void | |
61 | 1497 gen_gdecl(char *n, int gpc) |
1498 { | |
1499 /* | |
1500 if (stmode!=STATIC) | |
1501 printf(".globl %s\n",n); | |
1502 */ | |
1503 } | |
1504 | |
1505 void | |
1506 align(int t) | |
1507 { | |
1508 if (t!=CHAR) { | |
1509 if (data_alignment & 1) | |
1510 printf("\t.align 2\n"); | |
1511 data_alignment = 0; | |
1512 } | |
1513 } | |
1514 | |
1515 void | |
1516 emit_data(int e, int t, NMTBL *n) | |
1517 { | |
1518 int l; | |
82 | 1519 double d; |
1520 float f; | |
61 | 1521 char *name; |
1522 name = n->nm; | |
1523 if(mode!=GDECL) { | |
1524 error(-1); return; | |
1525 } | |
66 | 1526 if (chk) return; |
61 | 1527 if (n->dsp != -1) { |
1528 n->dsp = -1; /* initiallized flag */ | |
1529 printf(".globl\t%s\n",name); | |
1530 data_mode(name); | |
1531 align(t); | |
1532 printf("%s:\n",name); | |
1533 } else { | |
1534 data_mode(0); | |
1535 } | |
1536 if(car(e)==CONST) { | |
1537 if (t==CHAR) { | |
1538 printf("\t.byte %d\n",cadr(e)); | |
1539 if (data_alignment>0) | |
1540 data_alignment++; | |
1541 gpc += 1; | |
82 | 1542 } else if (t==SHORT) { |
1543 printf("\t.word %d\n",cadr(e)); | |
1544 if (data_alignment>0) data_alignment++; | |
1545 gpc += 2; | |
61 | 1546 } else { |
1547 printf("\t.long %d\n",cadr(e)); | |
1548 gpc += size_of_int; | |
1549 } | |
82 | 1550 } else if(t==DOUBLE) { |
1551 d = dcadr(e); | |
1552 printf("\t.long\t0x%x,0x%x\n",code_d1(d),code_d2(d)); | |
1553 } else if(t==FLOAT) { | |
1554 f = dcadr(e); | |
1555 printf("\t.long\t0x%x\n",*(int *)&f); | |
61 | 1556 } else if(t!=CHAR) { |
1557 gpc += size_of_int; | |
1558 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { | |
109 | 1559 printf("\t.long %s\n",((NMTBL *)cadr(cadr(e)))->nm); |
1560 } else if(car(e)==GVAR) { | |
1561 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); | |
61 | 1562 } else if(car(e)==FNAME) { |
1563 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); | |
1564 } else if(car(e)==STRING) { | |
1565 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { | |
1566 l = fwdlabel(); | |
1567 printf("\t.long _%d\n",l); | |
1568 printf(".section\t.rodata\n"); | |
1569 printf("_%d:\n",l); | |
1570 output_mode = RODATA_EMIT_MODE; | |
1571 } | |
1572 ascii((char *)cadr(e)); | |
1573 } else error(TYERR); | |
1574 } | |
1575 } | |
1576 | |
1577 void | |
1578 emit_data_closing(NMTBL *n) | |
1579 { | |
1580 int lb; | |
66 | 1581 if (chk) return; |
61 | 1582 if (mode==GDECL) { |
1583 data_mode(0); | |
1584 lb=fwdlabel(); | |
1585 printf("_%d:\n",lb); | |
1586 printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm); | |
1587 } | |
1588 } | |
1589 | |
1590 void | |
1591 global_table(void) | |
1592 { | |
1593 NMTBL *n; | |
1594 int init; | |
1595 init=0; | |
1596 for(n=ntable;n < &ntable[GSYMS];n++) { | |
117 | 1597 if ((n->sc == GVAR||n->sc == STATIC) && n->dsp != -1) { |
61 | 1598 /* n->dsp = -1 means initialized global */ |
1599 if (init==0) { | |
1600 data_mode(0); | |
1601 init=1; | |
1602 } | |
1603 printf(".comm %s,%d\n",n->nm,size(n->ty)); | |
1604 } | |
1605 } | |
1606 } | |
1607 | |
1608 void | |
1609 local_table(void) | |
1610 { | |
1611 NMTBL *n; | |
1612 int init; | |
1613 init=0; | |
1614 /* static local variables */ | |
1615 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { | |
1616 if (n->sc == GVAR) { | |
1617 if (init==0) { | |
1618 data_mode(0); | |
1619 init=1; | |
1620 } | |
1621 printf(".lcomm %s,%d\n",n->nm,size(n->ty)); | |
1622 } | |
1623 } | |
1624 } | |
1625 | |
1626 void | |
1627 text_mode(void) | |
1628 { | |
1629 if (output_mode!=TEXT_EMIT_MODE) { | |
1630 printf(".text\n"); | |
1631 printf("\t.align 2\n"); | |
1632 output_mode = TEXT_EMIT_MODE; | |
1633 } | |
1634 } | |
1635 | |
1636 void | |
1637 data_mode(char *name) | |
1638 { | |
1639 if (output_mode!=DATA_EMIT_MODE) { | |
1640 printf(".data\n"); | |
1641 output_mode = DATA_EMIT_MODE; | |
1642 } | |
1643 if (name) | |
1644 printf("\t.type\t%s,@object\n",name); | |
1645 } | |
1646 | |
81 | 1647 /* floating point */ |
1648 | |
1649 | |
82 | 1650 char * |
1651 fstore(int d) | |
1652 { | |
83 | 1653 return use? |
1654 (d?"fstl":"fsts"): | |
1655 (d?"fstpl":"fstps") | |
1656 ; | |
1657 } | |
1658 | |
1659 char * | |
1660 fstore_u(int d) | |
1661 { | |
82 | 1662 return d?"fstpl":"fstps"; |
1663 } | |
81 | 1664 |
82 | 1665 char * |
1666 fload(int d) | |
1667 { | |
1668 return d?"fldl":"flds"; | |
1669 } | |
81 | 1670 |
1671 | |
103 | 1672 void code_dassign_gvar(int e2,int freg,int d) |
82 | 1673 { |
109 | 1674 printf("\t%s %s\n",fstore(d),((NMTBL*)cadr(e2))->nm); |
82 | 1675 } |
1676 | |
103 | 1677 void code_dassign_lvar(int e2,int freg,int d) |
82 | 1678 { |
119 | 1679 printf("\t%s %d(%%ebp)\n",fstore(d),lvar(e2)); |
82 | 1680 } |
1681 | |
138 | 1682 void code_dassign_dregister(int e,int d,int freg) |
111 | 1683 { |
1684 error(-1); | |
1685 } | |
1686 | |
103 | 1687 void code_dassign(int e2,int freg,int d) |
82 | 1688 { |
1689 printf("\t%s (%s)\n",fstore(d),register_name(e2,0)); | |
1690 } | |
1691 | |
1692 static double d0 = 1.0; | |
81 | 1693 |
82 | 1694 int |
1695 code_d1(double d) | |
1696 { | |
1697 int *i = (int *)&d0; int *j = (int *)&d; | |
1698 return (i[1] == 0x3ff00000)?j[0]:j[1]; | |
1699 } | |
1700 | |
1701 int | |
1702 code_d2(double d) | |
1703 { | |
1704 int *i = (int *)&d0; int *j = (int *)&d; | |
1705 return (i[1] == 0x3ff00000)?j[1]:j[0]; | |
1706 } | |
1707 | |
138 | 1708 void code_dconst(int e2,int freg,int d) |
82 | 1709 { |
1710 int lb; | |
138 | 1711 double value = dcadr(e2); |
81 | 1712 |
138 | 1713 if (value==0.0) { |
82 | 1714 printf("\tfldz\n"); return; |
1715 } | |
138 | 1716 if (value==1.0) { |
82 | 1717 printf("\tfld1\n"); return; |
1718 } | |
1719 printf(" \t.section\t.rodata\n\t.align 8\n"); | |
1720 lb=fwdlabel(); | |
1721 printf("_%d:\n",lb); | |
138 | 1722 printf("\t.long\t0x%x,0x%x\n",code_d1(value),code_d2(value)); |
82 | 1723 if (output_mode==TEXT_EMIT_MODE) { |
1724 printf(".text\n"); | |
1725 } else { | |
1726 text_mode(); | |
1727 } | |
1728 printf("\tfldl _%d\n",lb); | |
1729 } | |
1730 | |
138 | 1731 void code_dneg(int freg,int d) |
82 | 1732 { |
1733 printf("\tfchs\n"); | |
1734 } | |
81 | 1735 |
107 | 1736 void code_d2i(int freg,int creg) |
82 | 1737 { |
1738 printf("\tlea -%d(%%esp),%%esp\n",size_of_int*2); | |
1739 printf("\tfnstcw (%%esp)\n"); | |
1740 printf("\tmovl (%%esp), %s\n",register_name(creg,0)); | |
1741 printf("\tmovb $12, 1(%%esp)\n"); | |
1742 printf("\tfldcw (%%esp)\n"); | |
85 | 1743 printf("\tfistpl %d(%%esp)\n",size_of_int); |
127 | 1744 printf("\tmovl %s, (%%esp)\n",register_name(creg,0)); |
85 | 1745 printf("\tfldcw (%%esp)\n"); |
82 | 1746 printf("\tpopl %s\n",register_name(creg,0)); |
1747 printf("\tpopl %s\n",register_name(creg,0)); | |
1748 } | |
81 | 1749 |
107 | 1750 void code_i2d(int creg,int freg) |
82 | 1751 { |
1752 printf("\tpushl %s\n",register_name(creg,0)); | |
1753 printf("\tfildl (%%esp)\n"); | |
1754 printf("\tlea %d(%%esp),%%esp\n",size_of_int); | |
1755 } | |
1756 | |
107 | 1757 void code_d2u(int freg,int creg) |
85 | 1758 { |
86 | 1759 printf("\tlea -%d(%%esp),%%esp\n",size_of_int*3); |
85 | 1760 printf("\tfnstcw (%%esp)\n"); |
1761 printf("\tmovl (%%esp), %s\n",register_name(creg,0)); | |
1762 printf("\tmovb $12, 1(%%esp)\n"); | |
1763 printf("\tfldcw (%%esp)\n"); | |
127 | 1764 printf("\tmovl %s, (%%esp)\n",register_name(creg,0)); |
86 | 1765 printf("\tfistpll %d(%%esp)\n",size_of_int); |
85 | 1766 printf("\tfldcw (%%esp)\n"); |
86 | 1767 printf("\tmovl %d(%%esp),%s\n",size_of_int,register_name(creg,0)); |
1768 printf("\tlea %d(%%esp),%%esp\n",size_of_int*3); | |
85 | 1769 } |
1770 | |
107 | 1771 void code_u2d(int creg,int freg) |
85 | 1772 { |
86 | 1773 printf("\tpushl %s\n",register_name(creg,0)); |
1774 printf("\tpushl %s\n",register_name(creg,0)); | |
1775 printf("\tmovl $0, %d(%%esp)\n",size_of_int); | |
1776 printf("\tfildll (%%esp)\n"); | |
1777 printf("\tlea %d(%%esp),%%esp\n",size_of_int*2); | |
85 | 1778 } |
1779 | |
133 | 1780 void code_d2f(int freg) { } |
1781 void code_f2d(int freg) { } | |
1782 void code_f2i(int freg,int creg) { code_d2i(freg,creg); } | |
1783 void code_f2u(int freg,int creg) { code_d2u(freg,creg); } | |
1784 void code_i2f(int creg,int freg) { code_i2d(creg,freg); } | |
1785 void code_u2f(int creg,int freg) { code_u2d(creg,freg); } | |
1786 | |
107 | 1787 void code_drgvar(int e2,int d,int freg) |
82 | 1788 { |
109 | 1789 printf("\t%s %s\n",fload(d),((NMTBL*)cadr(e2))->nm); |
82 | 1790 } |
81 | 1791 |
1792 | |
107 | 1793 void code_drlvar(int e2,int d,int freg) |
82 | 1794 { |
119 | 1795 printf("\t%s %d(%%ebp)\n",fload(d),lvar(e2)); |
82 | 1796 } |
1797 | |
138 | 1798 void code_cmp_drgvar(int e2,int d) |
82 | 1799 { |
109 | 1800 printf("\tfcomp %s\n",((NMTBL*)cadr(e2))->nm); |
82 | 1801 } |
1802 | |
138 | 1803 void code_cmp_drlvar(int e2,int d) |
82 | 1804 { |
119 | 1805 printf("\tfcomp %d(%%ebp)\n",lvar(e2)); |
82 | 1806 } |
1807 | |
1808 void dtosop(int op,int e1) | |
94 | 1809 { |
82 | 1810 switch(op) { |
133 | 1811 case FADD: |
82 | 1812 case DADD: printf("\tfaddp %%st,%%st(1)\n"); break; |
133 | 1813 case FSUB: |
85 | 1814 case DSUB: printf("\tfsubp %%st,%%st(1)\n"); break; |
133 | 1815 case FDIV: |
85 | 1816 case DDIV: printf("\tfdivp %%st,%%st(1)\n"); break; |
133 | 1817 case FMUL: |
82 | 1818 case DMUL: printf("\tfmulp %%st,%%st(1)\n"); break; |
133 | 1819 case FCMP: |
89 | 1820 case DCMP: |
82 | 1821 printf("\tfucompp\n"); |
1822 printf("\tfnstsw\t%%ax\n"); | |
1823 break; | |
1824 } | |
1825 } | |
81 | 1826 |
83 | 1827 void |
1828 code_dassop(int op,int d) { | |
1829 /* we have lvalue in creg, applied floating value is in %st(0) */ | |
102 | 1830 emit_dpop(d); /* do nothing for 387 */ |
83 | 1831 printf("\t%s (%s)\n",fload(d),register_name(creg,0)); |
1832 dtosop(op,0); | |
1833 printf("\t%s (%s)\n",fstore(d),register_name(creg,0)); | |
1834 } | |
1835 | |
1836 void | |
107 | 1837 code_dpreinc(int e1,int e2,int d,int freg) { |
83 | 1838 g_expr(e2); |
1839 printf("\t%s (%s)\n",fload(d),register_name(creg,0)); | |
1840 printf("\tfld1\n"); | |
87 | 1841 if (caddr(e1)>0) |
83 | 1842 printf("\tfaddp %%st,%%st(1)\n"); |
1843 else | |
1844 printf("\tfsubrp %%st,%%st(1)\n"); | |
1845 printf("\t%s (%s)\n",fstore(d),register_name(creg,0)); | |
1846 } | |
1847 | |
1848 void | |
107 | 1849 code_dpostinc(int e1,int e2,int d,int freg) { |
83 | 1850 g_expr(e2); |
1851 printf("\t%s (%s)\n",fload(d),register_name(creg,0)); | |
1852 if (use) | |
86 | 1853 printf("\t%s (%s)\n",fload(d),register_name(creg,0)); |
83 | 1854 printf("\tfld1\n"); |
87 | 1855 if (caddr(e1)>0) |
83 | 1856 printf("\tfaddp %%st,%%st(1)\n"); |
1857 else | |
1858 printf("\tfsubrp %%st,%%st(1)\n"); | |
87 | 1859 printf("\t%s (%s)\n",(use?fstore_u(d):fstore(d)),register_name(creg,0)); |
83 | 1860 } |
1861 | |
84 | 1862 void |
1863 drexpr(int e1, int e2,int l1, int op) | |
1864 { | |
89 | 1865 g_expr(list3(DCMP,e1,e2)); |
84 | 1866 switch(op) { |
1867 case DOP+GE: | |
133 | 1868 case FOP+GE: |
84 | 1869 printf("\ttestb\t$5,%%ah\n"); |
1870 printf("\tjne\t_%d\n",l1); | |
1871 break; | |
1872 case DOP+GT: | |
133 | 1873 case FOP+GT: |
84 | 1874 printf("\ttestb\t$69,%%ah\n"); |
1875 printf("\tjne\t_%d\n",l1); | |
1876 break; | |
1877 case DOP+EQ: | |
133 | 1878 case FOP+EQ: |
84 | 1879 printf("\tandb\t$69,%%ah\n"); |
1880 printf("\txorb\t$64,%%ah\n"); | |
1881 printf("\tjne\t_%d\n",l1); | |
1882 break; | |
1883 case DOP+NEQ: | |
133 | 1884 case FOP+NEQ: |
84 | 1885 printf("\tandb\t$69,%%ah\n"); |
1886 printf("\txorb\t$64,%%ah\n"); | |
1887 printf("\tje\t_%d\n",l1); | |
1888 break; | |
1889 } | |
1890 } | |
1891 | |
94 | 1892 void |
138 | 1893 code_dregister(int e2,int freg,int d) |
94 | 1894 { |
1895 error(-1); | |
1896 } | |
1897 | |
138 | 1898 void |
1899 code_cmp_dregister(int e2,int d) | |
94 | 1900 { |
1901 error(-1); | |
82 | 1902 } |
81 | 1903 |
97 | 1904 int pop_fregister() |
82 | 1905 { |
94 | 1906 if (freg_sp<0) { error(-1); return -1;} |
1907 printf("# fpop: %d\n",freg_sp-1); | |
1908 return freg_stack[--freg_sp]; | |
82 | 1909 } |
81 | 1910 |
94 | 1911 int |
138 | 1912 emit_dpop(int d) |
94 | 1913 { |
1914 int xreg; | |
97 | 1915 if ((xreg=pop_fregister())==-1) { |
94 | 1916 } else if (xreg<= -REG_LVAR_OFFSET) { |
119 | 1917 code_drlvar(REG_LVAR_OFFSET+xreg,1,freg); |
117 | 1918 free_lvar(xreg+REG_LVAR_OFFSET); |
94 | 1919 /* pushed order is reversed. We don't need this for comutable |
1920 operator, but it is ok to do this. */ | |
1921 printf("\tfxch\t%%st(1)\n"); | |
1922 } | |
1923 return xreg; | |
1924 } | |
1925 | |
1926 | |
138 | 1927 void emit_dpop_free(int e1,int d) |
82 | 1928 { |
1929 } | |
81 | 1930 |
133 | 1931 void emit_dpush(int type) |
82 | 1932 { |
119 | 1933 if (freg_sp>=MAX_FPU_STACK) code_save_fstacks(); |
94 | 1934 if (freg_sp>MAX_MAX) error(-1); |
107 | 1935 freg_stack[freg_sp++]=-1; |
94 | 1936 printf("# fpush:%d\n",freg_sp); |
1937 } | |
1938 | |
1939 void | |
1940 code_save_stacks() | |
1941 { | |
1942 /* temporal registers or stacks in fpu are saved in local variable */ | |
1943 int xreg,sp,screg; | |
1944 sp=reg_sp; | |
1945 while(sp-->0) { | |
1946 if ((xreg=reg_stack[sp])>=0) { | |
1947 screg=creg; | |
1948 if(creg!=xreg) { | |
1949 if (xreg!=dreg) free_register(xreg); | |
1950 creg = xreg; | |
1951 } | |
1952 code_assign_lvar( | |
119 | 1953 (reg_stack[sp]=new_lvar(size_of_int)),creg,0); |
94 | 1954 reg_stack[sp]= reg_stack[sp]-REG_LVAR_OFFSET; |
1955 regv[xreg]=0; | |
1956 creg=screg; | |
1957 } | |
1958 } | |
107 | 1959 code_save_fstacks(); |
1960 } | |
1961 | |
1962 void | |
1963 code_save_fstacks() | |
1964 { | |
127 | 1965 int xreg,sp,uses; |
1966 uses = use; use = 0; | |
94 | 1967 sp=freg_sp; |
1968 while(sp-->0) { | |
1969 if ((xreg=freg_stack[sp])==-1) { | |
1970 code_dassign_lvar( | |
119 | 1971 (freg_stack[sp]=new_lvar(size_of_double)),freg,1); |
94 | 1972 freg_stack[sp]= freg_stack[sp]-REG_LVAR_OFFSET; |
1973 } | |
1974 } | |
127 | 1975 use = uses; |
82 | 1976 } |
81 | 1977 |
61 | 1978 /* end */ |