61
|
1 /* Micro-C Generic Code Generatation Part */
|
|
2 /* $Id$ */
|
|
3
|
|
4 #define EXTERN extern
|
|
5 #include "mc.h"
|
|
6 #include "mc-codegen.h"
|
|
7 #include "mc-code.h"
|
|
8
|
|
9 int creg; /* current register */
|
|
10 int dreg; /* temporary register */
|
|
11 int reg_sp; /* REGister Stack-Pointer */
|
|
12
|
81
|
13 int freg; /* current floating point register */
|
|
14
|
61
|
15 /*
|
|
16 creg currrent virtual register
|
|
17 dreg spare virtual register
|
|
18
|
|
19 rname[creg] currrent real register
|
|
20 rname[dreg] spare real register
|
|
21
|
|
22 regs[] virtual register usage
|
|
23 regv[] value in virtual register flag
|
|
24
|
|
25 reg_name[rname[creg]]
|
|
26 */
|
|
27
|
|
28 void remove0(int *parent,int e) ;
|
|
29 void remove0_all(int *parent,int e) ;
|
|
30 int is_same_type(int e1,int e2);
|
|
31 void jump(int e1, int env);
|
|
32 void machinop(int e1);
|
|
33 void sassign(int e1);
|
|
34 void assign(int e1);
|
|
35 void assop(int e1);
|
|
36
|
|
37 int
|
|
38 get_register(void)
|
|
39 { /* 使われていないレジスタを調べる */
|
|
40 int i;
|
|
41 for(i=0;i<MAX_REGISTER;i++) {
|
|
42 if (! regs[i]) { /* 使われていないなら */
|
|
43 regs[i]=1; /* そのレジスタを使うことを宣言し */
|
|
44 return i; /* その場所を表す番号を返す */
|
|
45 }
|
|
46 }
|
|
47 return -1; /* 空いている場所がないなら、それを表す -1 を返す */
|
|
48 }
|
|
49
|
|
50 void
|
|
51 free_register(int i) { /* いらなくなったレジスタを開放 */
|
|
52 regv[i]=regs[i]=0;
|
|
53 }
|
|
54
|
|
55 int
|
|
56 register_full(void)
|
|
57 {
|
|
58 int i;
|
|
59 for(i=0;i<MAX_REGISTER;i++) {
|
|
60 if (! regs[i]) {
|
|
61 return 0;
|
|
62 }
|
|
63 }
|
|
64 return 1;
|
|
65 }
|
|
66
|
|
67 int
|
|
68 free_register_count(void)
|
|
69 {
|
|
70 int i,count;
|
|
71 count = 0;
|
|
72 for(i=0;i<MAX_REGISTER;i++) {
|
|
73 if (! regs[i] && ! regv[i]) count++;
|
|
74 }
|
|
75 return count;
|
|
76 }
|
|
77
|
|
78 void
|
|
79 free_all_register(void)
|
|
80 {
|
|
81 int i;
|
|
82 for(i=0;i<MAX_REGISTER;i++) {
|
|
83 regs[i]=regv[i]=0;
|
|
84 }
|
|
85 creg = get_register();
|
|
86 dreg = get_register();
|
|
87 return;
|
|
88 }
|
|
89
|
|
90 void
|
|
91 use_register_var(int i) {
|
|
92 regv[i]=1;
|
|
93 }
|
|
94
|
77
|
95 void
|
|
96 set_register_var() {
|
|
97 }
|
|
98
|
|
99 void
|
79
|
100 arg_register0(int args)
|
77
|
101 {
|
|
102 NMTBL *n;
|
|
103 if (args) {
|
|
104 /* process in reverse order */
|
79
|
105 n = (NMTBL*)caddr(args);
|
77
|
106 if(n->sc==REGISTER) {
|
|
107 if ((n->dsp = get_register_var()) <0) {
|
|
108 error(-1); return;
|
|
109 }
|
|
110 use_register_var(n->dsp); /* it has now value in it */
|
|
111 }
|
79
|
112 arg_register0(cadr(args));
|
77
|
113 }
|
|
114 }
|
|
115
|
79
|
116 void
|
|
117 arg_register(NMTBL *fnptr)
|
|
118 {
|
|
119 arg_register0(fnptr->dsp);
|
|
120 }
|
|
121
|
|
122
|
61
|
123 int creg_regvar = -1;
|
|
124 static int creg_regvar_back;
|
|
125 static int creg_back;
|
|
126
|
|
127 void
|
|
128 creg_destroy() {
|
|
129 creg_back = creg; creg_regvar_back = creg_regvar;
|
|
130 if (creg_regvar>=0)
|
|
131 creg = creg_regvar;
|
|
132 creg_regvar=-1;
|
|
133 }
|
|
134
|
|
135 void
|
|
136 creg_un_destroy() {
|
|
137 creg = creg_back; creg_regvar = creg_regvar_back;
|
|
138 }
|
|
139
|
|
140 void
|
|
141 register_usage(char *s)
|
|
142 {
|
|
143 int i;
|
66
|
144 if (chk) return;
|
61
|
145 printf("# %d: %s:",lineno,s);
|
|
146 printf(" creg=%s dreg=%s ",register_name(creg,0),register_name(dreg,0));
|
|
147 for(i=0;i<MAX_REGISTER;i++) {
|
|
148 printf("%d",regs[i]);
|
|
149 }
|
|
150 printf(":");
|
|
151 for(i=0;i<MAX_REGISTER;i++) {
|
|
152 printf("%d",regv[i]);
|
|
153 }
|
|
154 #if 0
|
|
155 printf(" regs_stack",register_name(creg,0),register_name(dreg,0));
|
|
156 for(i=reg_sp;i>=0;i--) {
|
|
157 if(reg_stack[i]>=0)
|
|
158 printf(" %s",register_name(reg_stack[i],0));
|
|
159 }
|
|
160 #endif
|
|
161 printf("\n");
|
|
162 }
|
|
163
|
|
164 void
|
|
165 gexpr_init(void)
|
|
166 {
|
|
167 while(reg_sp > 0) {
|
|
168 free_register(reg_stack[--reg_sp]);
|
|
169 }
|
|
170 text_mode();
|
|
171 gexpr_code_init();
|
|
172 register_usage("gexpr_init");
|
|
173 }
|
|
174
|
|
175
|
|
176 void
|
|
177 emit_init(void)
|
|
178 {
|
|
179 int i;
|
|
180 for(i=0;i<MAX_REGISTER;i++) { regs[i]=0; regv[i]=0;rname[i]=i;}
|
|
181 free_all_register();
|
|
182 reg_sp = 0;
|
|
183 text_mode();
|
|
184 }
|
|
185
|
|
186 int
|
|
187 virtual(int real)
|
|
188 {
|
|
189 int real_v,i;
|
|
190 real_v = -1;
|
|
191 for(i=0;i<MAX_REGISTER;i++) {
|
|
192 if (rname[i]==real) {
|
|
193 real_v=i;
|
|
194 break;
|
|
195 }
|
|
196 }
|
|
197 return real_v;
|
|
198 }
|
|
199
|
|
200 int
|
|
201 pop_register(void)
|
|
202 { /* レジスタから値を取り出す */
|
|
203 return reg_stack[--reg_sp];
|
|
204 }
|
|
205
|
|
206 int
|
|
207 stack_used(void) {
|
|
208 return reg_stack[--reg_sp]<0;
|
|
209 }
|
|
210
|
|
211 void
|
|
212 emit_pop_free(int xreg)
|
|
213 {
|
|
214 if (xreg==dreg) {
|
|
215 regv[dreg]=0;
|
|
216 } else if (xreg!=-1) {
|
|
217 free_register(xreg);
|
|
218 }
|
|
219 }
|
|
220
|
|
221 void
|
|
222 gexpr(int e1)
|
|
223 {
|
66
|
224 if (chk) return;
|
61
|
225 gexpr_init();
|
|
226 #if 0
|
|
227 if(lineno==2862) {
|
|
228 g_expr(e1); /*break here*/
|
|
229 return;
|
|
230 }
|
|
231 #endif
|
|
232 g_expr(e1);
|
|
233 }
|
|
234
|
|
235 int
|
|
236 csvalue()
|
|
237 {
|
|
238 return rname[creg]; /* for switch value */
|
|
239 }
|
|
240
|
|
241 void
|
|
242 g_expr(int e1)
|
|
243 {
|
|
244 int e2,e3/*,e4*/;
|
|
245 NMTBL *n;
|
|
246
|
|
247 e2 = cadr(e1);
|
|
248 switch (car(e1)){
|
|
249 case GVAR:
|
|
250 code_gvar(e1);
|
|
251 regv[creg]=1;
|
|
252 return;
|
|
253 case RGVAR:
|
|
254 code_rgvar(e1);
|
|
255 regv[creg]=1;
|
|
256 return;
|
|
257 case CRGVAR:
|
|
258 code_crgvar(e1);
|
|
259 regv[creg]=1;
|
|
260 return;
|
|
261 case LVAR:
|
|
262 code_lvar(lvar(e2));
|
|
263 regv[creg]=1;
|
|
264 return;
|
|
265 case REGISTER:
|
|
266 /* this is of course redundant... */
|
|
267 /* we can use rname for this? */
|
|
268 /* or why not creg=e2? */
|
|
269 code_register(e2);
|
|
270 regv[creg]=1;
|
|
271 return;
|
|
272 case RLVAR:
|
|
273 code_rlvar(lvar(e2));
|
|
274 regv[creg]=1;
|
|
275 return;
|
|
276 case CRLVAR:
|
|
277 code_crlvar(lvar(e2));
|
|
278 regv[creg]=1;
|
|
279 return;
|
81
|
280 case FRLVAR:
|
82
|
281 code_drlvar(lvar(e2),0);
|
|
282 regv[freg]=1;
|
81
|
283 return;
|
|
284 case FRGVAR:
|
82
|
285 code_drgvar(e1,0);
|
|
286 regv[freg]=1;
|
81
|
287 return;
|
|
288 case DRLVAR:
|
82
|
289 code_drlvar(lvar(e2),1);
|
|
290 regv[freg]=1;
|
81
|
291 return;
|
|
292 case DRGVAR:
|
82
|
293 code_drgvar(e1,1);
|
|
294 regv[freg]=1;
|
81
|
295 return;
|
61
|
296 case FNAME:
|
|
297 code_fname(((NMTBL *)(e2))->nm);
|
|
298 regv[creg]=1;
|
|
299 return;
|
|
300 case CONST: /* 代入する値が0でも特別な処理はしない */
|
|
301 code_const(e2);
|
|
302 regv[creg]=1;
|
|
303 return;
|
81
|
304 case DCONST:
|
82
|
305 code_dconst(e1);
|
|
306 regv[freg]=1;
|
81
|
307 return;
|
61
|
308 case STRING:
|
|
309 string(e1);
|
|
310 regv[creg]=1;
|
|
311 return;
|
|
312 case FUNCTION:
|
|
313 function(e1);
|
|
314 regv[creg]=1;
|
|
315 return;
|
|
316 case CODE:
|
|
317 jump(e2,caddr(e1));
|
|
318 return;
|
|
319 case INDIRECT:
|
|
320 g_expr(e2);
|
|
321 return;
|
81
|
322 case RINDIRECT: case CRINDIRECT:
|
|
323 case DRINDIRECT: case FRINDIRECT:
|
61
|
324 rindirect(e1);
|
|
325 return;
|
|
326 case ADDRESS:
|
|
327 g_expr(e2);
|
|
328 return;
|
|
329 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */
|
|
330 g_expr(e2);
|
|
331 code_neg();
|
|
332 return;
|
81
|
333 case DMINUS:
|
|
334 g_expr(e2);
|
|
335 code_dneg();
|
|
336 return;
|
82
|
337 case I2D:
|
|
338 g_expr(e2);
|
|
339 code_i2d();
|
|
340 return;
|
|
341 case D2I:
|
|
342 g_expr(e2);
|
|
343 code_d2i();
|
|
344 return;
|
61
|
345 case BNOT: /* ~ */
|
|
346 g_expr(e2);
|
|
347 code_not();
|
|
348 return;
|
|
349 case LNOT: /* ! */
|
|
350 g_expr(e2);
|
|
351 code_lnot();
|
|
352 return;
|
|
353 case PREINC:
|
|
354 code_preinc(e1,e2);
|
|
355 return;
|
|
356 case POSTINC:
|
|
357 code_postinc(e1,e2);
|
|
358 return;
|
|
359 case CPOSTINC:
|
|
360 /* char *p; *p++ */
|
|
361 code_cpostinc(e1,e2);
|
|
362 return;
|
|
363 case CPREINC:
|
|
364 code_cpreinc(e1,e2);
|
|
365 return;
|
|
366 case CPOSTDEC:
|
|
367 code_cpostdec(e1,e2);
|
|
368 return;
|
|
369 case CPREDEC:
|
|
370 code_cpredec(e1,e2);
|
|
371 return;
|
|
372 case MUL: case UMUL:
|
|
373 case DIV: case UDIV:
|
|
374 case MOD: case UMOD:
|
|
375 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
376 case ADD: case SUB: case BAND: case EOR: case BOR:
|
|
377 machinop(e1);
|
|
378 return;
|
81
|
379 case DMUL: case DDIV:
|
|
380 case DADD: case DSUB:
|
82
|
381 case DCOMP:
|
81
|
382 dmachinop(e1);
|
|
383 return;
|
61
|
384 case COND:
|
|
385 e2=fwdlabel();
|
|
386 b_expr(cadr(e1),0,e2,0);
|
|
387 code_set_fixed_creg(0);
|
|
388 g_expr(caddr(e1));
|
|
389 /* e4 = rname[creg]; this is a bad idea */
|
|
390 code_set_fixed_creg(1);
|
|
391 jmp(e3=fwdlabel());
|
|
392 fwddef(e2);
|
|
393 code_set_fixed_creg(0);
|
|
394 g_expr(cadddr(e1));
|
|
395 code_set_fixed_creg(1);
|
|
396 fwddef(e3);
|
|
397 return;
|
|
398 case SASS:
|
|
399 sassign(e1);
|
|
400 return;
|
82
|
401 case ASS: case CASS:
|
61
|
402 assign(e1);
|
|
403 return;
|
82
|
404 case FASS: case DASS: case LASS:
|
81
|
405 dassign(e1);
|
|
406 return;
|
61
|
407 case ASSOP: case CASSOP:
|
|
408 assop(e1);
|
|
409 return;
|
81
|
410 case DASSOP: case FASSOP:
|
|
411 dassop(e1);
|
|
412 return;
|
61
|
413 case RSTRUCT:
|
|
414 g_expr(e2);
|
|
415 return;
|
|
416 case COMMA:
|
|
417 g_expr(e2);
|
|
418 g_expr(caddr(e1));
|
|
419 return;
|
|
420 case RETURN:
|
|
421 n = (NMTBL *)e2;
|
|
422 if (retcont==0)
|
|
423 retcont=fwdlabel();
|
|
424 code_return(creg);
|
|
425 regv[creg]=1;
|
|
426 return;
|
|
427 case ENVIRONMENT:
|
|
428 code_environment(creg);
|
|
429 regv[creg]=1;
|
|
430 return;
|
|
431 default:
|
|
432 code_bool(e1);
|
|
433 regv[creg]=1;
|
|
434 }
|
|
435 }
|
|
436
|
|
437 void
|
|
438 bexpr(int e1, char cond, int l1)
|
|
439 {
|
66
|
440 if (chk) return;
|
61
|
441 gexpr_init();
|
|
442 b_expr(e1,cond,l1,0);
|
|
443 }
|
|
444
|
|
445 void
|
|
446 b_expr(int e1, char cond, int l1,int err)
|
|
447 {
|
|
448 int e2,l2;
|
|
449 e2=cadr(e1);
|
|
450 switch(car(e1)) {
|
|
451 case LNOT:
|
|
452 b_expr(e2,!cond,l1,0);
|
|
453 return;
|
|
454 case GT:
|
|
455 rexpr(e1,l1,code_gt(cond));
|
|
456 return;
|
|
457 case UGT:
|
|
458 rexpr(e1,l1,code_ugt(cond));
|
|
459 return;
|
|
460 case GE:
|
|
461 rexpr(e1,l1,code_ge(cond));
|
|
462 return;
|
|
463 case UGE:
|
|
464 rexpr(e1,l1,code_uge(cond));
|
|
465 return;
|
|
466 case LT:
|
|
467 rexpr(e1,l1,code_ge(!cond));
|
|
468 return;
|
|
469 case ULT:
|
|
470 rexpr(e1,l1,code_uge(!cond));
|
|
471 return;
|
|
472 case LE:
|
|
473 rexpr(e1,l1,code_gt(!cond));
|
|
474 return;
|
|
475 case ULE:
|
|
476 rexpr(e1,l1,code_ugt(!cond));
|
|
477 return;
|
|
478 case EQ:
|
|
479 rexpr(e1,l1,code_eq(cond));
|
|
480 return;
|
|
481 case NEQ:
|
|
482 rexpr(e1,l1,code_eq(!cond));
|
|
483 return;
|
82
|
484
|
|
485 case DOP+GT:
|
|
486 drexpr(cadr(e1),caddr(e1),l1,DOP+GT);
|
|
487 return;
|
|
488 case DOP+GE:
|
|
489 drexpr(cadr(e1),caddr(e1),l1,DOP+GE);
|
|
490 return;
|
|
491 case DOP+LT:
|
|
492 drexpr(caddr(e1),cadr(e1),l1,DOP+GE);
|
|
493 return;
|
|
494 case DOP+LE:
|
|
495 drexpr(caddr(e1),cadr(e1),l1,DOP+GT);
|
|
496 return;
|
|
497 case DOP+EQ:
|
|
498 drexpr(cadr(e1),caddr(e1),l1,DOP+EQ);
|
|
499 return;
|
|
500 case DOP+NEQ:
|
|
501 drexpr(cadr(e1),caddr(e1),l1,DOP+NEQ);
|
|
502 return;
|
|
503
|
61
|
504 case LAND:
|
|
505 b_expr(e2,0,cond?(l2=fwdlabel()):l1,0);
|
|
506 b_expr(caddr(e1),cond,l1,0);
|
|
507 if(cond) fwddef(l2);
|
|
508 return;
|
|
509 case LOR:
|
|
510 b_expr(e2,1,cond?l1:(l2=fwdlabel()),0);
|
|
511 b_expr(caddr(e1),cond,l1,0);
|
|
512 if(!cond) fwddef(l2);
|
|
513 return;
|
|
514 case CRGVAR:
|
|
515 code_cmp_crgvar(e1);
|
|
516 jcond(l1,cond);
|
|
517 return;
|
|
518 case CRLVAR:
|
|
519 code_cmp_crlvar(lvar(e2));
|
|
520 jcond(l1,cond);
|
|
521 return;
|
|
522 case RGVAR:
|
|
523 code_cmp_rgvar(e1);
|
|
524 jcond(l1,cond);
|
|
525 return;
|
|
526 case RLVAR:
|
|
527 code_cmp_rlvar(lvar(e2));
|
|
528 jcond(l1,cond);
|
|
529 return;
|
81
|
530 case DRLVAR:
|
|
531 code_cmp_drlvar(lvar(e2));
|
|
532 jcond(l1,cond);
|
|
533 return;
|
|
534 case DRGVAR:
|
|
535 code_cmp_drgvar(lvar(e2));
|
|
536 jcond(l1,cond);
|
|
537 return;
|
61
|
538 case REGISTER:
|
|
539 code_cmp_register(e2);
|
|
540 jcond(l1,cond);
|
|
541 return;
|
|
542 case CONST:
|
|
543 if((cond&&e2)||(!cond&&!e2)) jmp(l1);
|
|
544 return;
|
81
|
545 case DCONST:
|
|
546 if((dcadr(e2)!=0.0)^cond) jmp(l1);
|
|
547 return;
|
61
|
548 default:
|
81
|
549 /* type ? */
|
61
|
550 if(err) {
|
81
|
551 error(-1); return; /* recursive g_expr/b_expr */
|
61
|
552 }
|
|
553 g_expr(e1);
|
|
554 code_cmp_register(creg);
|
|
555 jcond(l1,cond);
|
|
556 return;
|
|
557 }
|
|
558 }
|
|
559
|
|
560
|
|
561 /* goto arguments list */
|
|
562 /* target list4(list2(tag,disp),cdr,ty,source_expr) */
|
|
563 /* source expr=listn(tag,...) */
|
|
564 /* source (after) list2(tag,disp) */
|
|
565 /* source list list3(e,cdr,sz) */
|
|
566
|
|
567 #define DEBUG_PARALLEL_ASSIGN 1
|
|
568
|
|
569 int
|
|
570 overrap(int t,int sz,int source)
|
|
571 {
|
|
572 int s,s0,s1;
|
|
573 int t0=cadr(t);
|
|
574 int t1=t0+sz;
|
|
575 for(;source;source=cadr(source)) {
|
|
576 s=car(source); s0=cadr(s);
|
|
577 if(car(s)==REGISTER && car(t)==REGISTER) {
|
|
578 if(s0==t0) return s;
|
|
579 } else if (is_same_type(s,t)) {
|
|
580 s1=s0+caddr(source);
|
|
581 #if DEBUG_PARALLEL_ASSIGN>1
|
|
582 printf("# ovedrrap source %d t0 %d t1 %d\n",car(car(t)),t0,t1);
|
|
583 printf("# ovedrrap target %d s0 %d s1 %d\n",car(car(source)),s0,s1);
|
|
584 printf("# ovedrrap equal = %d\n",((t0<=s0&&s0<t1)||(t0<s1&&s1<=t1)));
|
|
585 #endif
|
|
586 if((t0<=s0&&s0<t1)||(t0<s1&&s1<=t1)) return s;
|
|
587 }
|
|
588 }
|
|
589 return 0;
|
|
590 }
|
|
591
|
|
592 void
|
|
593 remove_target(int *target,int t,int *use)
|
|
594 {
|
|
595 int use0=*use;
|
|
596 while(use0) {
|
|
597 if (car(use0)==t) {
|
|
598 free_register(caddr(use0));
|
|
599 break;
|
|
600 }
|
|
601 use0 = cadr(use0);
|
|
602 }
|
|
603 remove0(target,t);
|
|
604 }
|
|
605
|
|
606 void
|
|
607 save_target(int t,int s,int *target,int *use,int sz,int ty)
|
|
608 {
|
|
609 int e1;
|
|
610 /*新しいレジスタ(or スタック)を取得する*/
|
|
611 if (sz==size_of_int && (e1=get_register())!=-1) {
|
|
612 *use=list3(t,*use,e1);
|
|
613 e1=list2(REGISTER,e1);
|
|
614 g_expr(assign_expr0(e1,s,ty,ty));
|
|
615 *target = append4(*target,t,ty,e1);
|
|
616 } else {
|
|
617 disp-=sz;
|
|
618 g_expr(assign_expr0((e1=list2(LVAR,disp)),s,ty,ty));
|
|
619 *target = append4(*target,t,ty,e1);
|
|
620 }
|
|
621 }
|
|
622
|
|
623 int
|
|
624 circular_dependency(int t,int s,int *target,int *source)
|
|
625 {
|
|
626 int target0=*target;
|
|
627 int t1,sz,ty,s1;
|
|
628 while(target0) {
|
|
629 if (cadddr(target0)==s) {
|
|
630 t1=car(target0);
|
|
631 s=cadddr(target0);
|
|
632 sz=size(ty=caddr(target0));
|
|
633 if(t==t1) {
|
|
634 #if DEBUG_PARALLEL_ASSIGN
|
|
635 printf("# circular dependency %d ty %d+%d sz %d\n",car(t1),ty,cadr(t1),sz);
|
|
636 #endif
|
|
637 return 1;
|
|
638 }
|
|
639 if ((s1=overrap(t1,sz,*source))) {
|
|
640 /* another overrap start over */
|
|
641 return circular_dependency(t,s1,target,source);
|
|
642 }
|
|
643 }
|
|
644 target0=cadr(target0);
|
|
645 }
|
|
646 return 0;
|
|
647 }
|
|
648
|
|
649 void
|
|
650 parallel_assign(int *target,int *source,int *processing,int *use)
|
|
651 {
|
|
652 int t,s,sz,ty,target0,s1;
|
|
653 while(*target) {
|
|
654 target0=*target;
|
|
655 while(target0) {
|
|
656 t=car(target0); s=cadddr(target0);
|
|
657 sz=size(ty=caddr(target0));
|
|
658 if(car(t)==car(s) && cadr(t)==cadr(s)) {
|
|
659 /*書き込み先が自分自身*/
|
|
660 #if DEBUG_PARALLEL_ASSIGN
|
|
661 printf("# remove same %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
662 #endif
|
|
663 remove_target(target,t,use);
|
|
664 /* 破壊されては困るので、source listからは除かない */
|
|
665 } else if (!(s1=overrap(t,sz,*source))) {
|
|
666 /* 重なってないので安心して書き込める */
|
|
667 #if DEBUG_PARALLEL_ASSIGN
|
|
668 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
669 #endif
|
|
670 g_expr(assign_expr0(t,s,ty,ty));
|
|
671 remove_target(target,t,use); remove0(source,s);
|
|
672 } else {
|
|
673 if(circular_dependency(t,s1,target,source)) {
|
|
674 #if DEBUG_PARALLEL_ASSIGN
|
|
675 printf("# saving %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
676 #endif
|
|
677 remove_target(target,t,use); remove0(source,s);
|
|
678 save_target(t,s,target,use,sz,ty);
|
|
679 }
|
|
680 }
|
|
681 target0=cadr(target0);
|
|
682 }
|
|
683 }
|
|
684 }
|
|
685
|
|
686 void
|
|
687 remove0(int *parent,int e)
|
|
688 {
|
|
689 int list;
|
|
690 while ((list=*parent)) {
|
|
691 if (car(list)==e) {
|
|
692 *parent= cadr(list); return;
|
|
693 } else {
|
|
694 parent=&cadr(list);
|
|
695 }
|
|
696 }
|
|
697 }
|
|
698
|
|
699 void
|
|
700 remove0_all(int *parent,int e)
|
|
701 {
|
|
702 int list;
|
|
703 while ((list=*parent)) {
|
|
704 if (car(list)==e) {
|
|
705 *parent= cadr(list);
|
|
706 } else {
|
|
707 parent=&cadr(list);
|
|
708 }
|
|
709 }
|
|
710 }
|
|
711
|
|
712 int
|
|
713 is_simple(int e1)
|
|
714 {
|
|
715 return (
|
|
716 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER ||
|
81
|
717 e1==GVAR || e1==RGVAR || e1==RLVAR || e1==CRLVAR || e1==CRGVAR ||
|
|
718 e1==DRLVAR || e1==FRLVAR
|
61
|
719 );
|
|
720 }
|
|
721
|
|
722 int
|
|
723 is_same_type(int e1,int e2)
|
|
724 {
|
|
725 int ce1=car(e1);
|
|
726 int ce2=car(e2);
|
|
727 return (
|
81
|
728 (ce1==LVAR && (ce2==RLVAR||ce2==CRLVAR||ce2==FRLVAR||ce2==DRLVAR))
|
|
729 || (ce2==LVAR && (ce1==RLVAR||ce1==CRLVAR||ce1==FRLVAR||ce1==DRLVAR))
|
|
730 || (ce1==GVAR && (ce2==RGVAR||ce2==CRGVAR||ce2==FRGVAR||ce2==DRGVAR))
|
|
731 || (ce2==GVAR && (ce1==RGVAR||ce1==CRGVAR||ce1==FRGVAR||ce1==DRGVAR))
|
61
|
732 );
|
|
733 }
|
|
734
|
|
735 int
|
|
736 is_memory(int e1)
|
|
737 {
|
|
738 int ce1=car(e1);
|
|
739 return (
|
81
|
740 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || ce1==DRLVAR ||
|
|
741 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || ce1==DRGVAR ||
|
61
|
742 ce1==REGISTER
|
|
743 );
|
|
744 }
|
|
745
|
|
746 void
|
|
747 jump(int e1, int env)
|
|
748 {
|
|
749 int e2,e3,e4,sz,arg_size,ty,max_regs,regs;
|
|
750 int t0,s0;
|
|
751 NMTBL *code0;
|
|
752 int target = 0;
|
|
753 int source = 0;
|
|
754 int processing = 0;
|
|
755 int use = 0;
|
|
756
|
|
757 /* まず、サイズを計算しながら、決まった形に落す。 */
|
|
758
|
|
759 arg_size = 0; regs = 0; max_regs = MAX_REGISTER_VAR-1;
|
|
760 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) {
|
|
761 e2 = car(e3); sz = size(ty=caddr(e3));
|
80
|
762 if (regs <= max_regs&&sz==size_of_int) {
|
61
|
763 target=list4(list2(REGISTER,register_var(regs++)), target,ty,e2);
|
|
764 } else {
|
|
765 target=list4(list2(LVAR,0), target,ty,e2);
|
|
766 arg_size += sz;
|
|
767 }
|
|
768 #if DEBUG_PARALLEL_ASSIGN
|
|
769 printf("# target %d ty %d+%d sz %d\n",car(car(target)),ty,cadr(car(target)),sz);
|
|
770 #endif
|
|
771 }
|
|
772
|
|
773 /* disp を飛び先似合わせて修正 */
|
|
774 if (fnptr->sc==CODE) {
|
|
775 if (-arg_size<disp) disp = -arg_size;
|
|
776 } else {
|
|
777 if (disp_offset-arg_size<disp) disp = disp_offset-arg_size;
|
|
778 }
|
|
779
|
|
780 /* 複雑な式を前もって計算しておく */
|
|
781 /* 必要なら局所変数を用いる。 */
|
|
782 /* 局所変数へのオフセットを覚えておく */
|
|
783
|
|
784 for (e2 = target; e2; e2 = cadr(e2)) {
|
|
785 t0=car(e2); s0=cadddr(e2);
|
|
786 sz=size(ty=caddr(e2));
|
|
787 if(car(t0)==LVAR) {
|
|
788 /* ここで、書込先アドレスを決める */
|
|
789 cadr(t0)=-arg_size;
|
|
790 arg_size-=sz;
|
|
791 }
|
|
792 if (!is_simple(car(s0))) {
|
|
793 disp-=sz;
|
|
794 g_expr(assign_expr0((e4=list2(LVAR,disp)),s0,ty,ty));
|
|
795 cadddr(e2)=e4;
|
|
796 s0=e4;
|
|
797 } else if (is_same_type(t0,s0)) {
|
|
798 if(cadr(t0)==cadr(s0)) {
|
|
799 #if DEBUG_PARALLEL_ASSIGN
|
|
800 printf("# remove same memory %d ty %d+%d sz %d\n",car(t0),ty,cadr(t0),sz);
|
|
801 #endif
|
|
802 /* we should check size also (but currently useless */
|
|
803 remove0(&target,t0);
|
|
804 /* still we have source to avoid overwrite */
|
|
805 }
|
|
806 }
|
|
807 if(is_memory(s0)) {
|
|
808 source=list3(s0,source,sz);
|
|
809 #if DEBUG_PARALLEL_ASSIGN
|
|
810 printf("# source %d ty %d+%d sz %d\n",car(car(source)),ty,cadr(car(source)),sz);
|
|
811 #endif
|
|
812 }
|
|
813 }
|
|
814
|
|
815 /* compute jump address */
|
|
816 e2 = cadr(e1);
|
|
817 if (car(e2) == FNAME) {
|
|
818 code0=(NMTBL *)cadr(e2);
|
|
819 if (code0->sc!=CODE) {
|
|
820 error(TYERR); return;
|
|
821 }
|
|
822 } else { /* indirect */
|
|
823 g_expr(e2);
|
|
824 emit_push();
|
|
825 }
|
|
826 if (env) {
|
|
827 g_expr(env);
|
|
828 emit_push();
|
|
829 }
|
|
830
|
|
831 /* 並列代入を実行 */
|
|
832
|
|
833 parallel_assign(&target,&source,&processing,&use);
|
|
834 while (use) {
|
|
835 free_register(caddr(use)); use=cadr(use);
|
|
836 }
|
|
837 if(target) error(-1);
|
|
838
|
|
839 if (env) {
|
|
840 /* change the frame pointer */
|
|
841 e3 = emit_pop(0);
|
|
842 code_frame_pointer(e3);
|
|
843 emit_pop_free(e3);
|
|
844 } else if (fnptr->sc==FUNCTION) {
|
|
845 code_fix_frame_pointer(disp_offset);
|
|
846 }
|
|
847
|
|
848 if (car(e2) == FNAME) {
|
|
849 code_jmp(code0->nm);
|
|
850 } else {
|
|
851 e2 = emit_pop(0);
|
|
852 code_indirect_jmp(e2);
|
|
853 emit_pop_free(e2);
|
|
854 }
|
|
855 }
|
|
856
|
|
857 void
|
|
858 machinop(int e1)
|
|
859 {
|
|
860 int e2,e3,op;
|
|
861
|
|
862 e2 = cadr(e1);
|
|
863 op = car(e1);
|
|
864 e3 = caddr(e1);
|
|
865 g_expr(e3);
|
|
866 emit_push();
|
|
867 g_expr(e2);
|
|
868 tosop(car(e1),(e2=pop_register()));
|
|
869 emit_pop_free(e2);
|
|
870 regv[creg]=1;
|
|
871 return;
|
|
872 }
|
|
873
|
81
|
874 void
|
|
875 dmachinop(int e1)
|
|
876 {
|
|
877 int e2,e3,op;
|
|
878
|
|
879 e2 = cadr(e1);
|
|
880 op = car(e1);
|
|
881 e3 = caddr(e1);
|
|
882 g_expr(e3);
|
|
883 emit_dpush();
|
|
884 g_expr(e2);
|
|
885 dtosop(car(e1),(e2=dpop_register()));
|
|
886 emit_dpop_free(e2);
|
|
887 regv[freg]=1;
|
|
888 return;
|
|
889 }
|
|
890
|
61
|
891
|
|
892 void
|
|
893 sassign(int e1)
|
|
894 {
|
|
895 int e2,e3,e4,sz,xreg,det;
|
|
896
|
|
897 /* structure assignment */
|
|
898 e2 = cadr(e1); /* pointer variable to the struct */
|
|
899 e3 = cadr(e2); /* offset of the variable (distination) */
|
|
900 e4 = caddr(e1); /* right value (source) */
|
|
901 sz = cadddr(e1); /* size of struct or union */
|
|
902 g_expr(e4);
|
|
903 emit_push();
|
|
904 g_expr(e2);
|
|
905 xreg = emit_pop(0);
|
|
906 /* 一般的にはコピーのオーバラップの状況は実行時にしかわからない */
|
|
907 /* しかし、わかる場合もある */
|
|
908 if (car(e4)==RSTRUCT) e4=cadr(e4);
|
|
909 if (is_same_type(e2,e4)) {
|
|
910 if(cadr(e2)<cadr(e4)) sz=-sz;
|
|
911 det=1;
|
|
912 } else {
|
|
913 det = 0;
|
|
914 }
|
|
915 emit_copy(xreg,creg,sz,0,1,det);
|
|
916 emit_pop_free(xreg);
|
|
917 return;
|
|
918 }
|
|
919
|
|
920 void
|
|
921 assign(int e1)
|
|
922 {
|
|
923 int e2,e3,e4,byte;
|
|
924
|
|
925 byte=(car(e1) == CASS);
|
81
|
926 if(car(e1) == FASS) {
|
|
927 /* move float to creg */
|
|
928 }
|
61
|
929 /* e2=e4 */
|
|
930 e2 = cadr(e1);
|
|
931 e3 = cadr(e2);
|
|
932 e4 = caddr(e1);
|
|
933 switch(car(e2)) {
|
|
934 case GVAR: /* i=3 */
|
|
935 g_expr(e4);
|
|
936 code_assign_gvar(e2,byte);
|
|
937 return;
|
|
938 case LVAR:
|
|
939 g_expr(e4);
|
|
940 code_assign_lvar(lvar(cadr(e2)),byte);
|
|
941 return;
|
|
942 case REGISTER:
|
|
943 g_expr(e4);
|
|
944 if (creg!=cadr(e2))
|
|
945 code_assign_register(cadr(e2),byte);
|
|
946 return;
|
|
947 }
|
|
948 g_expr(e2);
|
|
949 emit_push();
|
|
950 use_data_reg(creg,0);
|
|
951 g_expr(e4);
|
|
952 if (byte) use_data_reg(creg,1);
|
|
953 e2 = emit_pop(0);
|
|
954 code_assign(e2,byte);
|
|
955 emit_pop_free(e2);
|
|
956 regv[creg]=1;
|
|
957 return;
|
|
958 }
|
|
959
|
|
960 void
|
81
|
961 dassign(int e1)
|
|
962 {
|
82
|
963 int e2,e3,e4,d;
|
81
|
964
|
|
965 /* e2=e4 */
|
|
966 e2 = cadr(e1);
|
|
967 e3 = cadr(e2);
|
|
968 e4 = caddr(e1);
|
82
|
969 d = (car(e1)==LASS)?2:(car(e1)==DASS)?1:0;
|
81
|
970 switch(car(e2)) {
|
82
|
971 case GVAR:
|
81
|
972 g_expr(e4);
|
82
|
973 code_dassign_gvar(e2,d);
|
81
|
974 return;
|
|
975 case LVAR:
|
|
976 g_expr(e4);
|
82
|
977 code_dassign_lvar(lvar(cadr(e2)),d);
|
81
|
978 return;
|
|
979 }
|
|
980 g_expr(e2);
|
82
|
981 emit_push();
|
81
|
982 g_expr(e4);
|
82
|
983 e2 = emit_pop(0);
|
|
984 code_dassign(e2,d);
|
|
985 emit_pop_free(e2);
|
81
|
986 return;
|
|
987 }
|
|
988
|
|
989 void
|
61
|
990 assop(int e1)
|
|
991 {
|
|
992 int e2,e3,byte,op;
|
|
993
|
|
994 /* e2 op= e3 */
|
|
995 byte = (car(e1) == CASSOP);
|
|
996 e2 = cadr(e1);
|
|
997 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
998 e3 = caddr(e1);
|
|
999 op = cadddr(e1);
|
|
1000
|
|
1001 g_expr(e3);
|
|
1002 if (car(e2)==REGISTER) {
|
|
1003 code_register_assop(cadr(e2),op,byte);
|
|
1004 regv[creg]=1;
|
|
1005 return;
|
|
1006 }
|
|
1007 emit_push();
|
|
1008 g_expr(e2);
|
|
1009 code_assop(op,byte);
|
|
1010 regv[creg]=1;
|
|
1011 return;
|
|
1012 }
|
|
1013
|
81
|
1014 void
|
|
1015 dassop(int e1)
|
|
1016 {
|
|
1017 }
|
|
1018
|
|
1019 void
|
|
1020 fassop(int e1)
|
|
1021 {
|
|
1022 }
|
61
|
1023
|
|
1024 int
|
|
1025 fwdlabel(void)
|
|
1026 {
|
|
1027 return labelno++;
|
|
1028 }
|
|
1029
|
|
1030 void
|
|
1031 fwddef(int l)
|
|
1032 {
|
|
1033 control=1;
|
66
|
1034 if (!chk)
|
|
1035 printf("_%d:\n",l);
|
61
|
1036 }
|
|
1037
|
|
1038 int
|
|
1039 backdef(void)
|
|
1040 {
|
|
1041 control=1;
|
66
|
1042 if (!chk)
|
|
1043 printf("_%d:\n",labelno);
|
61
|
1044 return labelno++;
|
|
1045 }
|
|
1046
|
|
1047 void
|
|
1048 def_label(int cslabel, int dlabel)
|
|
1049 {
|
|
1050 int fl;
|
|
1051
|
|
1052 fl = 0;
|
|
1053 if (control) {
|
|
1054 jmp(fl=fwdlabel());
|
|
1055 }
|
|
1056 fwddef(cslabel);
|
|
1057 if (dlabel)
|
|
1058 jmp(dlabel);
|
|
1059 if (fl) {
|
|
1060 fwddef(fl);
|
|
1061 }
|
|
1062 }
|
|
1063
|
|
1064 void
|
|
1065 gen_source(char *s)
|
|
1066 {
|
|
1067 printf("%s",s);
|
|
1068 }
|
|
1069
|
|
1070 void
|
|
1071 ret(void)
|
|
1072 {
|
|
1073 code_set_fixed_creg(1);
|
|
1074 jmp(retlabel);
|
|
1075 }
|
|
1076
|
66
|
1077 void
|
|
1078 opening(char *filename)
|
|
1079 {
|
|
1080 emit_init();
|
|
1081 if (!chk)
|
|
1082 code_opening(filename);
|
|
1083 }
|
|
1084
|
|
1085 void
|
|
1086 closing()
|
|
1087 {
|
|
1088 if (!chk)
|
|
1089 code_closing();
|
|
1090 }
|
|
1091
|
61
|
1092 /* end */
|