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