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