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