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