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? */
|
|
106 code_dregister(e2);
|
|
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:
|
|
416 code_cmp_dregister(e2);
|
|
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)
|
|
431 code_cmp_dregister(freg);
|
|
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) {
|
|
483 free_register(caddr(use0));
|
|
484 break;
|
|
485 }
|
|
486 use0 = cadr(use0);
|
|
487 }
|
|
488 remove0(target,t);
|
|
489 }
|
|
490
|
|
491 void
|
|
492 save_target(int t,int s,int *target,int *use,int sz,int ty)
|
|
493 {
|
|
494 int e1;
|
|
495 /*新しいレジスタ(or スタック)を取得する*/
|
|
496 if (sz==size_of_int && (e1=get_register())!=-1) {
|
|
497 *use=list3(t,*use,e1);
|
|
498 e1=list2(REGISTER,e1);
|
83
|
499 g_expr_u(assign_expr0(e1,s,ty,ty));
|
61
|
500 *target = append4(*target,t,ty,e1);
|
|
501 } else {
|
94
|
502 g_expr_u(assign_expr0((e1=list2(LVAR,new_lvar(sz))),s,ty,ty));
|
61
|
503 *target = append4(*target,t,ty,e1);
|
|
504 }
|
|
505 }
|
|
506
|
|
507 int
|
|
508 circular_dependency(int t,int s,int *target,int *source)
|
|
509 {
|
|
510 int target0=*target;
|
|
511 int t1,sz,ty,s1;
|
|
512 while(target0) {
|
|
513 if (cadddr(target0)==s) {
|
|
514 t1=car(target0);
|
|
515 s=cadddr(target0);
|
|
516 sz=size(ty=caddr(target0));
|
|
517 if(t==t1) {
|
|
518 #if DEBUG_PARALLEL_ASSIGN
|
|
519 printf("# circular dependency %d ty %d+%d sz %d\n",car(t1),ty,cadr(t1),sz);
|
|
520 #endif
|
|
521 return 1;
|
|
522 }
|
|
523 if ((s1=overrap(t1,sz,*source))) {
|
|
524 /* another overrap start over */
|
|
525 return circular_dependency(t,s1,target,source);
|
|
526 }
|
|
527 }
|
|
528 target0=cadr(target0);
|
|
529 }
|
|
530 return 0;
|
|
531 }
|
|
532
|
|
533 void
|
|
534 parallel_assign(int *target,int *source,int *processing,int *use)
|
|
535 {
|
|
536 int t,s,sz,ty,target0,s1;
|
|
537 while(*target) {
|
|
538 target0=*target;
|
|
539 while(target0) {
|
|
540 t=car(target0); s=cadddr(target0);
|
|
541 sz=size(ty=caddr(target0));
|
|
542 if(car(t)==car(s) && cadr(t)==cadr(s)) {
|
|
543 /*書き込み先が自分自身*/
|
|
544 #if DEBUG_PARALLEL_ASSIGN
|
|
545 printf("# remove same %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
546 #endif
|
|
547 remove_target(target,t,use);
|
|
548 /* 破壊されては困るので、source listからは除かない */
|
|
549 } else if (!(s1=overrap(t,sz,*source))) {
|
|
550 /* 重なってないので安心して書き込める */
|
|
551 #if DEBUG_PARALLEL_ASSIGN
|
|
552 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
553 #endif
|
83
|
554 g_expr_u(assign_expr0(t,s,ty,ty));
|
61
|
555 remove_target(target,t,use); remove0(source,s);
|
|
556 } else {
|
|
557 if(circular_dependency(t,s1,target,source)) {
|
|
558 #if DEBUG_PARALLEL_ASSIGN
|
|
559 printf("# saving %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
560 #endif
|
|
561 remove_target(target,t,use); remove0(source,s);
|
|
562 save_target(t,s,target,use,sz,ty);
|
|
563 }
|
|
564 }
|
|
565 target0=cadr(target0);
|
|
566 }
|
|
567 }
|
|
568 }
|
|
569
|
|
570 void
|
|
571 remove0(int *parent,int e)
|
|
572 {
|
|
573 int list;
|
|
574 while ((list=*parent)) {
|
|
575 if (car(list)==e) {
|
|
576 *parent= cadr(list); return;
|
|
577 } else {
|
|
578 parent=&cadr(list);
|
|
579 }
|
|
580 }
|
|
581 }
|
|
582
|
|
583 void
|
|
584 remove0_all(int *parent,int e)
|
|
585 {
|
|
586 int list;
|
|
587 while ((list=*parent)) {
|
|
588 if (car(list)==e) {
|
|
589 *parent= cadr(list);
|
|
590 } else {
|
|
591 parent=&cadr(list);
|
|
592 }
|
|
593 }
|
|
594 }
|
|
595
|
|
596 int
|
|
597 is_simple(int e1)
|
|
598 {
|
|
599 return (
|
|
600 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER ||
|
81
|
601 e1==GVAR || e1==RGVAR || e1==RLVAR || e1==CRLVAR || e1==CRGVAR ||
|
|
602 e1==DRLVAR || e1==FRLVAR
|
61
|
603 );
|
|
604 }
|
|
605
|
|
606 int
|
|
607 is_same_type(int e1,int e2)
|
|
608 {
|
|
609 int ce1=car(e1);
|
|
610 int ce2=car(e2);
|
|
611 return (
|
81
|
612 (ce1==LVAR && (ce2==RLVAR||ce2==CRLVAR||ce2==FRLVAR||ce2==DRLVAR))
|
|
613 || (ce2==LVAR && (ce1==RLVAR||ce1==CRLVAR||ce1==FRLVAR||ce1==DRLVAR))
|
|
614 || (ce1==GVAR && (ce2==RGVAR||ce2==CRGVAR||ce2==FRGVAR||ce2==DRGVAR))
|
|
615 || (ce2==GVAR && (ce1==RGVAR||ce1==CRGVAR||ce1==FRGVAR||ce1==DRGVAR))
|
61
|
616 );
|
|
617 }
|
|
618
|
|
619 int
|
|
620 is_memory(int e1)
|
|
621 {
|
|
622 int ce1=car(e1);
|
|
623 return (
|
81
|
624 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || ce1==DRLVAR ||
|
|
625 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || ce1==DRGVAR ||
|
61
|
626 ce1==REGISTER
|
|
627 );
|
|
628 }
|
|
629
|
|
630 void
|
|
631 jump(int e1, int env)
|
|
632 {
|
|
633 int e2,e3,e4,sz,arg_size,ty,max_regs,regs;
|
|
634 int t0,s0;
|
|
635 NMTBL *code0;
|
|
636 int target = 0;
|
|
637 int source = 0;
|
|
638 int processing = 0;
|
|
639 int use = 0;
|
|
640
|
|
641 /* まず、サイズを計算しながら、決まった形に落す。 */
|
|
642
|
|
643 arg_size = 0; regs = 0; max_regs = MAX_REGISTER_VAR-1;
|
|
644 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) {
|
|
645 e2 = car(e3); sz = size(ty=caddr(e3));
|
80
|
646 if (regs <= max_regs&&sz==size_of_int) {
|
61
|
647 target=list4(list2(REGISTER,register_var(regs++)), target,ty,e2);
|
|
648 } else {
|
|
649 target=list4(list2(LVAR,0), target,ty,e2);
|
|
650 arg_size += sz;
|
|
651 }
|
|
652 #if DEBUG_PARALLEL_ASSIGN
|
|
653 printf("# target %d ty %d+%d sz %d\n",car(car(target)),ty,cadr(car(target)),sz);
|
|
654 #endif
|
|
655 }
|
|
656
|
|
657 /* disp を飛び先似合わせて修正 */
|
|
658 if (fnptr->sc==CODE) {
|
|
659 if (-arg_size<disp) disp = -arg_size;
|
|
660 } else {
|
|
661 if (disp_offset-arg_size<disp) disp = disp_offset-arg_size;
|
|
662 }
|
|
663
|
|
664 /* 複雑な式を前もって計算しておく */
|
|
665 /* 必要なら局所変数を用いる。 */
|
|
666 /* 局所変数へのオフセットを覚えておく */
|
|
667
|
|
668 for (e2 = target; e2; e2 = cadr(e2)) {
|
|
669 t0=car(e2); s0=cadddr(e2);
|
|
670 sz=size(ty=caddr(e2));
|
|
671 if(car(t0)==LVAR) {
|
|
672 /* ここで、書込先アドレスを決める */
|
|
673 cadr(t0)=-arg_size;
|
|
674 arg_size-=sz;
|
|
675 }
|
|
676 if (!is_simple(car(s0))) {
|
94
|
677 g_expr_u(assign_expr0((e4=list2(LVAR,new_lvar(sz))),s0,ty,ty));
|
61
|
678 cadddr(e2)=e4;
|
|
679 s0=e4;
|
|
680 } else if (is_same_type(t0,s0)) {
|
|
681 if(cadr(t0)==cadr(s0)) {
|
|
682 #if DEBUG_PARALLEL_ASSIGN
|
|
683 printf("# remove same memory %d ty %d+%d sz %d\n",car(t0),ty,cadr(t0),sz);
|
|
684 #endif
|
|
685 /* we should check size also (but currently useless */
|
|
686 remove0(&target,t0);
|
|
687 /* still we have source to avoid overwrite */
|
|
688 }
|
|
689 }
|
|
690 if(is_memory(s0)) {
|
|
691 source=list3(s0,source,sz);
|
|
692 #if DEBUG_PARALLEL_ASSIGN
|
|
693 printf("# source %d ty %d+%d sz %d\n",car(car(source)),ty,cadr(car(source)),sz);
|
|
694 #endif
|
|
695 }
|
|
696 }
|
|
697
|
|
698 /* compute jump address */
|
|
699 e2 = cadr(e1);
|
|
700 if (car(e2) == FNAME) {
|
|
701 code0=(NMTBL *)cadr(e2);
|
|
702 if (code0->sc!=CODE) {
|
|
703 error(TYERR); return;
|
|
704 }
|
|
705 } else { /* indirect */
|
|
706 g_expr(e2);
|
|
707 emit_push();
|
|
708 }
|
|
709 if (env) {
|
|
710 g_expr(env);
|
|
711 emit_push();
|
|
712 }
|
|
713
|
|
714 /* 並列代入を実行 */
|
|
715
|
|
716 parallel_assign(&target,&source,&processing,&use);
|
|
717 while (use) {
|
|
718 free_register(caddr(use)); use=cadr(use);
|
|
719 }
|
|
720 if(target) error(-1);
|
|
721
|
|
722 if (env) {
|
|
723 /* change the frame pointer */
|
|
724 e3 = emit_pop(0);
|
|
725 code_frame_pointer(e3);
|
|
726 emit_pop_free(e3);
|
|
727 } else if (fnptr->sc==FUNCTION) {
|
|
728 code_fix_frame_pointer(disp_offset);
|
|
729 }
|
|
730
|
|
731 if (car(e2) == FNAME) {
|
|
732 code_jmp(code0->nm);
|
|
733 } else {
|
|
734 e2 = emit_pop(0);
|
|
735 code_indirect_jmp(e2);
|
|
736 emit_pop_free(e2);
|
|
737 }
|
|
738 }
|
|
739
|
|
740 void
|
|
741 machinop(int e1)
|
|
742 {
|
|
743 int e2,e3,op;
|
|
744
|
|
745 e2 = cadr(e1);
|
|
746 op = car(e1);
|
|
747 e3 = caddr(e1);
|
|
748 g_expr(e3);
|
|
749 emit_push();
|
|
750 g_expr(e2);
|
|
751 tosop(car(e1),(e2=pop_register()));
|
|
752 emit_pop_free(e2);
|
|
753 regv[creg]=1;
|
|
754 return;
|
|
755 }
|
|
756
|
81
|
757 void
|
|
758 dmachinop(int e1)
|
|
759 {
|
|
760 int e2,e3,op;
|
|
761
|
|
762 e2 = cadr(e1);
|
|
763 op = car(e1);
|
|
764 e3 = caddr(e1);
|
|
765 g_expr(e3);
|
|
766 emit_dpush();
|
|
767 g_expr(e2);
|
94
|
768 dtosop(car(e1),(e2=emit_dpop(1)));
|
81
|
769 emit_dpop_free(e2);
|
83
|
770 fregv[freg]=1;
|
81
|
771 return;
|
|
772 }
|
|
773
|
61
|
774
|
|
775 void
|
|
776 sassign(int e1)
|
|
777 {
|
|
778 int e2,e3,e4,sz,xreg,det;
|
|
779
|
|
780 /* structure assignment */
|
|
781 e2 = cadr(e1); /* pointer variable to the struct */
|
|
782 e3 = cadr(e2); /* offset of the variable (distination) */
|
|
783 e4 = caddr(e1); /* right value (source) */
|
|
784 sz = cadddr(e1); /* size of struct or union */
|
|
785 g_expr(e4);
|
|
786 emit_push();
|
|
787 g_expr(e2);
|
|
788 xreg = emit_pop(0);
|
|
789 /* 一般的にはコピーのオーバラップの状況は実行時にしかわからない */
|
|
790 /* しかし、わかる場合もある */
|
|
791 if (car(e4)==RSTRUCT) e4=cadr(e4);
|
|
792 if (is_same_type(e2,e4)) {
|
|
793 if(cadr(e2)<cadr(e4)) sz=-sz;
|
|
794 det=1;
|
|
795 } else {
|
|
796 det = 0;
|
|
797 }
|
|
798 emit_copy(xreg,creg,sz,0,1,det);
|
|
799 emit_pop_free(xreg);
|
|
800 return;
|
|
801 }
|
|
802
|
|
803 void
|
|
804 assign(int e1)
|
|
805 {
|
|
806 int e2,e3,e4,byte;
|
|
807
|
|
808 byte=(car(e1) == CASS);
|
|
809 /* e2=e4 */
|
|
810 e2 = cadr(e1);
|
|
811 e3 = cadr(e2);
|
|
812 e4 = caddr(e1);
|
|
813 switch(car(e2)) {
|
|
814 case GVAR: /* i=3 */
|
|
815 g_expr(e4);
|
|
816 code_assign_gvar(e2,byte);
|
|
817 return;
|
|
818 case LVAR:
|
|
819 g_expr(e4);
|
|
820 code_assign_lvar(lvar(cadr(e2)),byte);
|
|
821 return;
|
|
822 case REGISTER:
|
|
823 g_expr(e4);
|
|
824 if (creg!=cadr(e2))
|
|
825 code_assign_register(cadr(e2),byte);
|
|
826 return;
|
|
827 }
|
|
828 g_expr(e2);
|
|
829 emit_push();
|
|
830 g_expr(e4);
|
|
831 e2 = emit_pop(0);
|
|
832 code_assign(e2,byte);
|
|
833 emit_pop_free(e2);
|
|
834 regv[creg]=1;
|
|
835 return;
|
|
836 }
|
|
837
|
|
838 void
|
81
|
839 dassign(int e1)
|
|
840 {
|
82
|
841 int e2,e3,e4,d;
|
81
|
842
|
|
843 /* e2=e4 */
|
|
844 e2 = cadr(e1);
|
|
845 e3 = cadr(e2);
|
|
846 e4 = caddr(e1);
|
82
|
847 d = (car(e1)==LASS)?2:(car(e1)==DASS)?1:0;
|
81
|
848 switch(car(e2)) {
|
82
|
849 case GVAR:
|
81
|
850 g_expr(e4);
|
82
|
851 code_dassign_gvar(e2,d);
|
81
|
852 return;
|
|
853 case LVAR:
|
|
854 g_expr(e4);
|
82
|
855 code_dassign_lvar(lvar(cadr(e2)),d);
|
81
|
856 return;
|
|
857 }
|
|
858 g_expr(e2);
|
82
|
859 emit_push();
|
81
|
860 g_expr(e4);
|
82
|
861 e2 = emit_pop(0);
|
|
862 code_dassign(e2,d);
|
|
863 emit_pop_free(e2);
|
81
|
864 return;
|
|
865 }
|
|
866
|
|
867 void
|
61
|
868 assop(int e1)
|
|
869 {
|
|
870 int e2,e3,byte,op;
|
|
871
|
|
872 /* e2 op= e3 */
|
|
873 byte = (car(e1) == CASSOP);
|
|
874 e2 = cadr(e1);
|
|
875 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
876 e3 = caddr(e1);
|
|
877 op = cadddr(e1);
|
|
878
|
|
879 g_expr(e3);
|
|
880 if (car(e2)==REGISTER) {
|
|
881 code_register_assop(cadr(e2),op,byte);
|
|
882 regv[creg]=1;
|
|
883 return;
|
|
884 }
|
|
885 emit_push();
|
|
886 g_expr(e2);
|
|
887 code_assop(op,byte);
|
|
888 regv[creg]=1;
|
|
889 return;
|
|
890 }
|
|
891
|
81
|
892 void
|
|
893 dassop(int e1)
|
|
894 {
|
83
|
895 int e2,e3,op,d;
|
81
|
896
|
83
|
897 /* e2 op= e3 */
|
|
898 d = (car(e1) == DASSOP);
|
|
899 e2 = cadr(e1);
|
|
900 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
901 e3 = caddr(e1);
|
|
902 op = cadddr(e1);
|
|
903
|
|
904 g_expr(e3);
|
|
905 emit_dpush();
|
|
906 g_expr(e2);
|
|
907 code_dassop(op,d);
|
|
908 regv[creg]=1;
|
|
909 return;
|
81
|
910 }
|
61
|
911
|
95
|
912 void
|
|
913 cmpdimm(int e, int csreg)
|
|
914 {
|
|
915 code_cmpdimm(e, csreg);
|
|
916 }
|
|
917
|
|
918 int
|
|
919 csvalue()
|
|
920 {
|
|
921 return code_csvalue();
|
|
922 }
|
|
923
|
|
924
|
61
|
925 int
|
|
926 fwdlabel(void)
|
|
927 {
|
|
928 return labelno++;
|
|
929 }
|
|
930
|
|
931 void
|
|
932 fwddef(int l)
|
|
933 {
|
|
934 control=1;
|
66
|
935 if (!chk)
|
92
|
936 code_label(l);
|
61
|
937 }
|
|
938
|
|
939 int
|
|
940 backdef(void)
|
|
941 {
|
|
942 control=1;
|
66
|
943 if (!chk)
|
92
|
944 code_label(labelno);
|
61
|
945 return labelno++;
|
|
946 }
|
|
947
|
|
948 void
|
|
949 def_label(int cslabel, int dlabel)
|
|
950 {
|
|
951 int fl;
|
|
952
|
|
953 fl = 0;
|
|
954 if (control) {
|
|
955 jmp(fl=fwdlabel());
|
|
956 }
|
|
957 fwddef(cslabel);
|
|
958 if (dlabel)
|
|
959 jmp(dlabel);
|
|
960 if (fl) {
|
|
961 fwddef(fl);
|
|
962 }
|
|
963 }
|
|
964
|
|
965 void
|
|
966 gen_source(char *s)
|
|
967 {
|
|
968 printf("%s",s);
|
|
969 }
|
|
970
|
|
971 void
|
|
972 ret(void)
|
|
973 {
|
|
974 code_set_fixed_creg(1);
|
|
975 jmp(retlabel);
|
|
976 }
|
|
977
|
66
|
978 void
|
|
979 opening(char *filename)
|
|
980 {
|
|
981 emit_init();
|
|
982 if (!chk)
|
|
983 code_opening(filename);
|
|
984 }
|
|
985
|
|
986 void
|
|
987 closing()
|
|
988 {
|
|
989 if (!chk)
|
|
990 code_closing();
|
|
991 }
|
|
992
|
61
|
993 /* end */
|