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