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