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