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