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