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