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