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