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