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