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