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