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