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