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 */
|
83
|
10
|
|
11 int use; /* generated value will be used */
|
81
|
12
|
61
|
13 /*
|
|
14 creg currrent virtual register
|
|
15 */
|
|
16
|
105
|
17 static void remove0(int *parent,int e) ;
|
|
18 /* static void remove0_all(int *parent,int e) ; */
|
|
19 static int is_same_type(int e1,int e2);
|
|
20 static void jump(int e1, int env);
|
|
21 static void machinop(int e1);
|
|
22 static void sassign(int e1);
|
|
23 static void assign(int e1);
|
|
24 static void assop(int e1);
|
|
25 static int g_expr0(int e1);
|
|
26 static int register_to_lvar(int e);
|
61
|
27
|
102
|
28 void
|
|
29 codegen_init()
|
|
30 {
|
|
31 code_init();
|
|
32 }
|
|
33
|
137
|
34 void
|
|
35 arg_register(NMTBL *fnptr)
|
|
36 {
|
|
37 code_arg_register(fnptr);
|
|
38 }
|
|
39
|
94
|
40 int
|
83
|
41 gexpr(int e1,int use0)
|
61
|
42 {
|
94
|
43 if (chk) return INT;
|
61
|
44 gexpr_init();
|
83
|
45 use = use0;
|
61
|
46 #if 0
|
|
47 if(lineno==2862) {
|
94
|
48 return g_expr0(e1); /*break here*/
|
61
|
49 }
|
|
50 #endif
|
94
|
51 return g_expr0(e1);
|
61
|
52 }
|
|
53
|
|
54 int
|
83
|
55 g_expr_u(int e1)
|
|
56 {
|
94
|
57 int t;
|
83
|
58 int suse = use; use=0;
|
94
|
59 t=g_expr0(e1);
|
147
|
60 code_gexpr(e1);
|
|
61
|
83
|
62 use=suse;
|
94
|
63 return t;
|
83
|
64 }
|
|
65
|
94
|
66 int
|
61
|
67 g_expr(int e1)
|
|
68 {
|
94
|
69 int t;
|
83
|
70 int suse = use; use=1;
|
94
|
71 t=g_expr0(e1);
|
147
|
72 code_gexpr(e1);
|
|
73
|
83
|
74 use=suse;
|
94
|
75 return t;
|
83
|
76 }
|
|
77
|
94
|
78 int
|
83
|
79 g_expr0(int e1)
|
|
80 {
|
133
|
81 int e2,e3,t,d;
|
61
|
82 NMTBL *n;
|
|
83
|
147
|
84 code_gexpr(e1);
|
|
85
|
61
|
86 e2 = cadr(e1);
|
|
87 switch (car(e1)){
|
|
88 case GVAR:
|
147
|
89 creg=use_int(creg);
|
107
|
90 code_gvar(e1,creg);
|
94
|
91 return ADDRESS;
|
61
|
92 case RGVAR:
|
147
|
93 creg=use_int(creg);
|
107
|
94 code_rgvar(e1,creg);
|
94
|
95 return INT;
|
61
|
96 case CRGVAR:
|
147
|
97 creg=use_int(creg);
|
107
|
98 code_crgvar(e1,creg);
|
94
|
99 return CHAR;
|
61
|
100 case LVAR:
|
147
|
101 creg=use_int(creg);
|
119
|
102 code_lvar(e2,creg);
|
94
|
103 return ADDRESS;
|
61
|
104 case REGISTER:
|
147
|
105 creg=use_int(creg);
|
107
|
106 code_register(e2,creg);
|
94
|
107 return INT;
|
|
108 case DREGISTER:
|
147
|
109 creg=use_double(creg);
|
|
110 code_dregister(e2,creg,1);
|
94
|
111 return DOUBLE;
|
137
|
112 case FREGISTER:
|
147
|
113 creg=use_float(creg);
|
|
114 code_dregister(e2,creg,0);
|
137
|
115 return FLOAT;
|
61
|
116 case RLVAR:
|
147
|
117 creg=use_int(creg);
|
119
|
118 code_rlvar(e2,creg);
|
94
|
119 return INT;
|
61
|
120 case CRLVAR:
|
147
|
121 creg=use_int(creg);
|
119
|
122 code_crlvar(e2,creg);
|
94
|
123 return CHAR;
|
81
|
124 case FRLVAR:
|
147
|
125 creg=use_float(creg);
|
|
126 code_drlvar(e2,0,creg);
|
94
|
127 return FLOAT;
|
81
|
128 case FRGVAR:
|
147
|
129 creg=use_float(creg);
|
|
130 code_drgvar(e1,0,creg);
|
94
|
131 return FLOAT;
|
81
|
132 case DRLVAR:
|
147
|
133 creg=use_double(creg);
|
|
134 code_drlvar(e2,1,creg);
|
94
|
135 return DOUBLE;
|
81
|
136 case DRGVAR:
|
147
|
137 creg=use_double(creg);
|
|
138 code_drgvar(e1,1,creg);
|
94
|
139 return DOUBLE;
|
61
|
140 case FNAME:
|
147
|
141 creg=use_int(creg);
|
109
|
142 code_fname((NMTBL *)(e2),creg);
|
94
|
143 return ADDRESS;
|
61
|
144 case CONST: /* ÂåÆþ¤¹¤ëÃͤ¬0¤Ç¤âÆÃÊ̤ʽèÍý¤Ï¤·¤Ê¤¤ */
|
147
|
145 creg=use_int(creg);
|
107
|
146 code_const(e2,creg);
|
94
|
147 return INT;
|
81
|
148 case DCONST:
|
147
|
149 creg=use_double(creg);
|
|
150 code_dconst(e1,creg,1);
|
94
|
151 return DOUBLE;
|
133
|
152 case FCONST:
|
147
|
153 creg=use_float(creg);
|
|
154 code_dconst(e1,creg,0);
|
133
|
155 return FLOAT;
|
61
|
156 case STRING:
|
147
|
157 creg=use_int(creg);
|
107
|
158 code_string(e1,creg);
|
94
|
159 return ADDRESS;
|
61
|
160 case FUNCTION:
|
94
|
161 t = function(e1);
|
|
162 return t;
|
61
|
163 case CODE:
|
|
164 jump(e2,caddr(e1));
|
94
|
165 return VOID;
|
61
|
166 case INDIRECT:
|
94
|
167 return g_expr0(e2);
|
81
|
168 case RINDIRECT: case CRINDIRECT:
|
|
169 case DRINDIRECT: case FRINDIRECT:
|
94
|
170 return rindirect(e1);
|
61
|
171 case ADDRESS:
|
138
|
172 if (car(e2)==REGISTER||car(e2)==DREGISTER||car(e2)==FREGISTER)
|
|
173 return register_to_lvar(e2); /* too late? */
|
105
|
174 else
|
|
175 return g_expr0(e2);
|
61
|
176 case MINUS: /* ¥ì¥¸¥¹¥¿¤ËÂФ·¡¢negl¤ò¼Â¹Ô¤¹¤ì¤Ð¼Â¸½²Äǽ */
|
107
|
177 g_expr0(e2); code_neg(creg);
|
94
|
178 return INT;
|
81
|
179 case DMINUS:
|
147
|
180 g_expr0(e2); code_dneg(creg,1);
|
94
|
181 return DOUBLE;
|
133
|
182 case FMINUS:
|
147
|
183 g_expr0(e2); code_dneg(creg,0);
|
133
|
184 return FLOAT;
|
108
|
185 case CONV:
|
|
186 g_expr0(e2);
|
|
187 switch(caddr(e1)) {
|
147
|
188 case I2D: code_i2d(creg); return DOUBLE;
|
|
189 case D2I: code_d2i(creg); return INT;
|
|
190 case U2D: code_u2d(creg); return DOUBLE;
|
|
191 case F2U: code_f2u(creg); return UNSIGNED;
|
|
192 case I2F: code_i2f(creg); return FLOAT;
|
|
193 case F2I: code_f2i(creg); return INT;
|
|
194 case U2F: code_u2f(creg); return FLOAT;
|
|
195 case D2U: code_d2u(creg); return UNSIGNED;
|
|
196 case D2F: code_d2f(creg); return FLOAT;
|
|
197 case F2D: code_f2d(creg); return DOUBLE;
|
108
|
198 default:
|
|
199 error(-1); return INT;
|
|
200 }
|
61
|
201 case BNOT: /* ~ */
|
107
|
202 g_expr0(e2); code_not(creg);
|
94
|
203 return INT;
|
61
|
204 case LNOT: /* ! */
|
107
|
205 g_expr0(e2); code_lnot(creg);
|
94
|
206 return INT;
|
61
|
207 case PREINC:
|
147
|
208 creg=use_int(creg);
|
107
|
209 code_preinc(e1,e2,creg);
|
94
|
210 return INT;
|
61
|
211 case POSTINC:
|
147
|
212 creg=use_int(creg);
|
107
|
213 code_postinc(e1,e2,creg);
|
94
|
214 return INT;
|
147
|
215 case DPREINC: /* ++d */
|
|
216 creg=use_double(creg);
|
|
217 code_dpreinc(e1,e2,1,creg);
|
94
|
218 return DOUBLE;
|
147
|
219 case DPOSTINC: /* d++ */
|
|
220 creg=use_double(creg);
|
|
221 code_dpostinc(e1,e2,1,creg);
|
94
|
222 return DOUBLE;
|
147
|
223 case FPREINC: /* ++f */
|
|
224 creg=use_float(creg);
|
|
225 code_dpreinc(e1,e2,0,creg);
|
94
|
226 return FLOAT;
|
147
|
227 case FPOSTINC: /* f++ */
|
|
228 creg=use_float(creg);
|
|
229 code_dpostinc(e1,e2,0,creg);
|
94
|
230 return FLOAT;
|
61
|
231 case CPOSTINC:
|
|
232 /* char *p; *p++ */
|
147
|
233 creg=use_int(creg);
|
107
|
234 code_cpostinc(e1,e2,creg);
|
94
|
235 return CHAR;
|
61
|
236 case CPREINC:
|
147
|
237 creg=use_int(creg);
|
107
|
238 code_cpreinc(e1,e2,creg);
|
94
|
239 return CHAR;
|
61
|
240 case CPOSTDEC:
|
147
|
241 creg=use_int(creg);
|
107
|
242 code_cpostdec(e1,e2,creg);
|
94
|
243 return CHAR;
|
61
|
244 case CPREDEC:
|
147
|
245 creg=use_int(creg);
|
107
|
246 code_cpredec(e1,e2,creg);
|
94
|
247 return CHAR;
|
61
|
248 case MUL: case UMUL:
|
|
249 case DIV: case UDIV:
|
|
250 case MOD: case UMOD:
|
|
251 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
94
|
252 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP:
|
147
|
253 creg=use_int(creg);
|
61
|
254 machinop(e1);
|
94
|
255 return INT;
|
81
|
256 case DMUL: case DDIV:
|
|
257 case DADD: case DSUB:
|
91
|
258 case DCMP: case DCMPGE:
|
147
|
259 creg=use_double(creg);
|
133
|
260 dmachinop(e1,1);
|
94
|
261 return DOUBLE;
|
133
|
262 case FMUL: case FDIV:
|
|
263 case FADD: case FSUB:
|
|
264 case FCMP: case FCMPGE:
|
147
|
265 creg=use_float(creg);
|
133
|
266 dmachinop(e1,0);
|
|
267 return FLOAT;
|
61
|
268 case COND:
|
|
269 e2=fwdlabel();
|
|
270 b_expr(cadr(e1),0,e2,0);
|
108
|
271 code_set_fixed_creg(0,INT);
|
|
272 t = g_expr0(caddr(e1));
|
|
273 code_set_fixed_creg(1,t);
|
61
|
274 jmp(e3=fwdlabel());
|
|
275 fwddef(e2);
|
108
|
276 code_set_fixed_creg(0,INT);
|
94
|
277 t = g_expr0(cadddr(e1));
|
108
|
278 code_set_fixed_creg(1,t);
|
|
279 fwddef(e3);
|
|
280 return t;
|
|
281 case DCOND:
|
133
|
282 case FCOND:
|
138
|
283 d = (car(e1)==DCOND?DOUBLE:FLOAT);
|
108
|
284 e2=fwdlabel();
|
|
285 b_expr(cadr(e1),0,e2,0);
|
133
|
286 code_set_fixed_creg(0,d);
|
108
|
287 t = g_expr0(caddr(e1));
|
|
288 code_set_fixed_creg(1,t);
|
|
289 jmp(e3=fwdlabel());
|
|
290 fwddef(e2);
|
133
|
291 code_set_fixed_creg(0,d);
|
108
|
292 t = g_expr0(cadddr(e1));
|
|
293 code_set_fixed_creg(1,t);
|
61
|
294 fwddef(e3);
|
94
|
295 return t;
|
61
|
296 case SASS:
|
|
297 sassign(e1);
|
94
|
298 return RSTRUCT;
|
82
|
299 case ASS: case CASS:
|
61
|
300 assign(e1);
|
94
|
301 return INT;
|
82
|
302 case FASS: case DASS: case LASS:
|
81
|
303 dassign(e1);
|
94
|
304 return DOUBLE;
|
61
|
305 case ASSOP: case CASSOP:
|
|
306 assop(e1);
|
94
|
307 return INT;
|
81
|
308 case DASSOP: case FASSOP:
|
|
309 dassop(e1);
|
94
|
310 return DOUBLE;
|
61
|
311 case RSTRUCT:
|
83
|
312 g_expr0(e2);
|
94
|
313 return RSTRUCT;
|
61
|
314 case COMMA:
|
83
|
315 g_expr_u(e2);
|
94
|
316 return g_expr0(caddr(e1));
|
61
|
317 case RETURN:
|
147
|
318 creg = use_int(creg);
|
61
|
319 n = (NMTBL *)e2;
|
|
320 if (retcont==0)
|
|
321 retcont=fwdlabel();
|
|
322 code_return(creg);
|
94
|
323 return VOID;
|
61
|
324 case ENVIRONMENT:
|
147
|
325 creg = use_int(creg);
|
61
|
326 code_environment(creg);
|
94
|
327 return ADDRESS;
|
61
|
328 default:
|
116
|
329 code_bool(e1); /* type? */
|
94
|
330 return INT;
|
61
|
331 }
|
|
332 }
|
|
333
|
94
|
334 #define dual_ops(op) \
|
|
335 (op==GT|| op==UGT|| op==GE|| op==UGE|| op==LT|| \
|
|
336 op==ULT|| op==LE|| op==ULE|| \
|
|
337 op==DOP+GT|| op==DOP+GE|| op==DOP+LT|| op==DOP+LE || \
|
135
|
338 op==FOP+GT|| op==FOP+GE|| op==FOP+LT|| op==FOP+LE || \
|
|
339 op==FOP+EQ|| op==FOP+NEQ || \
|
|
340 op==EQ|| op==NEQ|| op==DOP+EQ|| op==DOP+NEQ)
|
94
|
341
|
|
342 int
|
|
343 rop_dual(op)
|
|
344 {
|
|
345 if (op==GT) return LT;
|
|
346 if (op==UGT) return ULT;
|
|
347 if (op==GE) return LE;
|
|
348 if (op==UGE) return ULE;
|
|
349 if (op==LT) return GT;
|
|
350 if (op==ULT) return UGT;
|
|
351 if (op==LE) return GE;
|
|
352 if (op==ULE) return UGE;
|
|
353 if (op==DOP+GT) return DOP+LT;
|
|
354 if (op==DOP+GE) return DOP+LE;
|
|
355 if (op==DOP+LT) return DOP+GT;
|
|
356 if (op==DOP+LE) return DOP+GE;
|
135
|
357 if (op==FOP+GT) return FOP+LT;
|
|
358 if (op==FOP+GE) return FOP+LE;
|
|
359 if (op==FOP+LT) return FOP+GT;
|
|
360 if (op==FOP+LE) return FOP+GE;
|
94
|
361 return op;
|
|
362 }
|
|
363
|
61
|
364 void
|
|
365 bexpr(int e1, char cond, int l1)
|
|
366 {
|
94
|
367 int op = car(e1);
|
66
|
368 if (chk) return;
|
61
|
369 gexpr_init();
|
94
|
370 if (dual_ops(op) && (car(caddr(e1))==CONST||(car(caddr(e1))==DCONST)))
|
|
371 b_expr(list3(rop_dual(op),caddr(e1),cadr(e1)),cond,l1,0);
|
|
372 else
|
|
373 b_expr(e1,cond,l1,0);
|
61
|
374 }
|
|
375
|
|
376 void
|
|
377 b_expr(int e1, char cond, int l1,int err)
|
|
378 {
|
94
|
379 int e2,l2,t;
|
132
|
380 if (!control) return;
|
127
|
381 l2 = 0;
|
61
|
382 e2=cadr(e1);
|
|
383 switch(car(e1)) {
|
|
384 case LNOT:
|
|
385 b_expr(e2,!cond,l1,0);
|
|
386 return;
|
|
387 case GT:
|
89
|
388 rexpr(e1,l1,code_gt(cond),INT);
|
61
|
389 return;
|
|
390 case UGT:
|
89
|
391 rexpr(e1,l1,code_ugt(cond),UNSIGNED);
|
61
|
392 return;
|
|
393 case GE:
|
89
|
394 rexpr(e1,l1,code_ge(cond),INT);
|
61
|
395 return;
|
|
396 case UGE:
|
89
|
397 rexpr(e1,l1,code_uge(cond),UNSIGNED);
|
61
|
398 return;
|
|
399 case LT:
|
89
|
400 rexpr(e1,l1,code_ge(!cond),INT);
|
61
|
401 return;
|
|
402 case ULT:
|
89
|
403 rexpr(e1,l1,code_uge(!cond),UNSIGNED);
|
61
|
404 return;
|
|
405 case LE:
|
89
|
406 rexpr(e1,l1,code_gt(!cond),INT);
|
61
|
407 return;
|
|
408 case ULE:
|
89
|
409 rexpr(e1,l1,code_ugt(!cond),UNSIGNED);
|
61
|
410 return;
|
|
411 case EQ:
|
89
|
412 rexpr(e1,l1,code_eq(cond),INT);
|
61
|
413 return;
|
|
414 case NEQ:
|
89
|
415 rexpr(e1,l1,code_eq(!cond),INT);
|
61
|
416 return;
|
82
|
417
|
|
418 case DOP+GT:
|
|
419 drexpr(cadr(e1),caddr(e1),l1,DOP+GT);
|
|
420 return;
|
|
421 case DOP+GE:
|
|
422 drexpr(cadr(e1),caddr(e1),l1,DOP+GE);
|
|
423 return;
|
|
424 case DOP+LT:
|
84
|
425 drexpr(caddr(e1),cadr(e1),l1,DOP+GT);
|
82
|
426 return;
|
|
427 case DOP+LE:
|
84
|
428 drexpr(caddr(e1),cadr(e1),l1,DOP+GE);
|
82
|
429 return;
|
|
430 case DOP+EQ:
|
|
431 drexpr(cadr(e1),caddr(e1),l1,DOP+EQ);
|
|
432 return;
|
|
433 case DOP+NEQ:
|
|
434 drexpr(cadr(e1),caddr(e1),l1,DOP+NEQ);
|
|
435 return;
|
|
436
|
135
|
437 case FOP+GT:
|
|
438 drexpr(cadr(e1),caddr(e1),l1,FOP+GT);
|
|
439 return;
|
|
440 case FOP+GE:
|
|
441 drexpr(cadr(e1),caddr(e1),l1,FOP+GE);
|
|
442 return;
|
|
443 case FOP+LT:
|
|
444 drexpr(caddr(e1),cadr(e1),l1,FOP+GT);
|
|
445 return;
|
|
446 case FOP+LE:
|
|
447 drexpr(caddr(e1),cadr(e1),l1,FOP+GE);
|
|
448 return;
|
|
449 case FOP+EQ:
|
|
450 drexpr(cadr(e1),caddr(e1),l1,FOP+EQ);
|
|
451 return;
|
|
452 case FOP+NEQ:
|
|
453 drexpr(cadr(e1),caddr(e1),l1,FOP+NEQ);
|
|
454 return;
|
|
455
|
|
456
|
61
|
457 case LAND:
|
|
458 b_expr(e2,0,cond?(l2=fwdlabel()):l1,0);
|
|
459 b_expr(caddr(e1),cond,l1,0);
|
|
460 if(cond) fwddef(l2);
|
|
461 return;
|
|
462 case LOR:
|
|
463 b_expr(e2,1,cond?l1:(l2=fwdlabel()),0);
|
|
464 b_expr(caddr(e1),cond,l1,0);
|
|
465 if(!cond) fwddef(l2);
|
|
466 return;
|
|
467 case CRGVAR:
|
147
|
468 creg=use_int(creg);
|
61
|
469 code_cmp_crgvar(e1);
|
|
470 jcond(l1,cond);
|
|
471 return;
|
|
472 case CRLVAR:
|
147
|
473 creg=use_int(creg);
|
119
|
474 code_cmp_crlvar(e2);
|
61
|
475 jcond(l1,cond);
|
|
476 return;
|
|
477 case RGVAR:
|
147
|
478 creg=use_int(creg);
|
61
|
479 code_cmp_rgvar(e1);
|
|
480 jcond(l1,cond);
|
|
481 return;
|
|
482 case RLVAR:
|
147
|
483 creg=use_int(creg);
|
119
|
484 code_cmp_rlvar(e2);
|
61
|
485 jcond(l1,cond);
|
|
486 return;
|
81
|
487 case DRLVAR:
|
147
|
488 creg=use_double(creg);
|
138
|
489 code_cmp_drlvar(e2,1);
|
|
490 jcond(l1,cond);
|
|
491 return;
|
|
492 case FRLVAR:
|
147
|
493 creg=use_float(creg);
|
138
|
494 code_cmp_drlvar(e2,0);
|
81
|
495 jcond(l1,cond);
|
|
496 return;
|
|
497 case DRGVAR:
|
147
|
498 creg=use_double(creg);
|
138
|
499 code_cmp_drgvar(e2,1);
|
|
500 jcond(l1,cond);
|
|
501 return;
|
|
502 case FRGVAR:
|
147
|
503 creg=use_float(creg);
|
138
|
504 code_cmp_drgvar(e2,0);
|
81
|
505 jcond(l1,cond);
|
|
506 return;
|
61
|
507 case REGISTER:
|
147
|
508 creg=use_int(creg);
|
61
|
509 code_cmp_register(e2);
|
|
510 jcond(l1,cond);
|
|
511 return;
|
138
|
512 case FREGISTER:
|
147
|
513 creg=use_float(creg);
|
138
|
514 code_cmp_dregister(e2,0);
|
|
515 jcond(l1,cond);
|
|
516 return;
|
94
|
517 case DREGISTER:
|
147
|
518 creg=use_double(creg);
|
138
|
519 code_cmp_dregister(e2,1);
|
94
|
520 jcond(l1,cond);
|
|
521 return;
|
61
|
522 case CONST:
|
132
|
523 if(control&&((cond&&e2)||(!cond&&!e2))) jmp(l1);
|
61
|
524 return;
|
81
|
525 case DCONST:
|
138
|
526 case FCONST:
|
132
|
527 if(control&&((dcadr(e2)!=0.0)^cond)) jmp(l1);
|
81
|
528 return;
|
61
|
529 default:
|
|
530 if(err) {
|
81
|
531 error(-1); return; /* recursive g_expr/b_expr */
|
94
|
532 }
|
|
533 t=g_expr(e1);
|
138
|
534 if(t==FLOAT)
|
147
|
535 code_cmp_dregister(creg,0);
|
138
|
536 else if(t==DOUBLE)
|
147
|
537 code_cmp_dregister(creg,1);
|
94
|
538 else
|
|
539 code_cmp_register(creg);
|
61
|
540 jcond(l1,cond);
|
|
541 return;
|
|
542 }
|
|
543 }
|
|
544
|
126
|
545 int
|
|
546 is_code(NMTBL *fnptr)
|
|
547 {
|
|
548 int type = fnptr->ty;
|
|
549 return type==CODE|| (type>0 && car(type)==CODE);
|
|
550 }
|
|
551
|
|
552 int
|
|
553 is_function(NMTBL *fnptr)
|
|
554 {
|
|
555 int type = fnptr->ty;
|
|
556 return type==FUNCTION || (type>0 && car(type)==FUNCTION);
|
|
557 }
|
|
558
|
61
|
559
|
105
|
560 static int
|
|
561 register_to_lvar(int e)
|
|
562 {
|
|
563 error(REG_ERR);
|
|
564 return 0;
|
|
565 #if 0
|
|
566 ÅÓÃæ¤Ç¥ì¥¸¥¹¥¿¤«¤éLVAR¤ËÊѹ¹¤·¤Æ¤â¡¢´Ö¤Ë¹ç¤ï¤Ê¤¤¡£
|
|
567
|
|
568 NMTBL *n = (NMTBL*)caddr(e);
|
|
569 int reg = cadr(e);
|
|
570 int tag = car(e);
|
|
571 int lvar;
|
|
572 int t;
|
|
573 if (!n||n==&null_nptr) error(REG_ERR);
|
|
574 if (tag==REGISTER) {
|
|
575 n->dsp = new_lvar(size_of_int);
|
|
576 t = INT;
|
|
577 } else if (tag==DREGISTER) {
|
|
578 n->dsp = new_lvar(size_of_double);
|
|
579 t = DOUBLE;
|
138
|
580 } else if (tag==FREGISTER) {
|
|
581 n->dsp = new_lvar(size_of_float);
|
|
582 t = DOUBLE;
|
105
|
583 } else error(-1);
|
|
584 n->sc = LVAR;
|
|
585 lvar = list2(LVAR,n->dsp);
|
|
586 g_expr_u(assign_expr0(list2(LVAR,n->dsp),list3(tag,reg,(int)n),t,t));
|
147
|
587 if (tag==REGISTER||tag==DREGISTER||tag==FREGISTER) {
|
105
|
588 free_register(reg);
|
|
589 return g_expr0(lvar);
|
|
590 #endif
|
|
591 }
|
|
592
|
61
|
593 /* goto arguments list */
|
|
594 /* target list4(list2(tag,disp),cdr,ty,source_expr) */
|
|
595 /* source expr=listn(tag,...) */
|
|
596 /* source (after) list2(tag,disp) */
|
|
597 /* source list list3(e,cdr,sz) */
|
|
598
|
|
599 #define DEBUG_PARALLEL_ASSIGN 1
|
|
600
|
|
601 int
|
|
602 overrap(int t,int sz,int source)
|
|
603 {
|
|
604 int s,s0,s1;
|
|
605 int t0=cadr(t);
|
|
606 int t1=t0+sz;
|
|
607 for(;source;source=cadr(source)) {
|
|
608 s=car(source); s0=cadr(s);
|
|
609 if(car(s)==REGISTER && car(t)==REGISTER) {
|
|
610 if(s0==t0) return s;
|
|
611 } else if (is_same_type(s,t)) {
|
|
612 s1=s0+caddr(source);
|
|
613 #if DEBUG_PARALLEL_ASSIGN>1
|
|
614 printf("# ovedrrap source %d t0 %d t1 %d\n",car(car(t)),t0,t1);
|
|
615 printf("# ovedrrap target %d s0 %d s1 %d\n",car(car(source)),s0,s1);
|
|
616 printf("# ovedrrap equal = %d\n",((t0<=s0&&s0<t1)||(t0<s1&&s1<=t1)));
|
|
617 #endif
|
|
618 if((t0<=s0&&s0<t1)||(t0<s1&&s1<=t1)) return s;
|
|
619 }
|
|
620 }
|
|
621 return 0;
|
|
622 }
|
|
623
|
|
624 void
|
|
625 remove_target(int *target,int t,int *use)
|
|
626 {
|
|
627 int use0=*use;
|
147
|
628 int reg;
|
61
|
629 while(use0) {
|
|
630 if (car(use0)==t) {
|
147
|
631 reg = car(caddr(use0));
|
|
632 if (reg==REGISTER||reg==FREGISTER||reg==DREGISTER)
|
99
|
633 free_register(cadr(caddr(use0)));
|
61
|
634 break;
|
|
635 }
|
|
636 use0 = cadr(use0);
|
|
637 }
|
|
638 remove0(target,t);
|
|
639 }
|
|
640
|
|
641 void
|
|
642 save_target(int t,int s,int *target,int *use,int sz,int ty)
|
|
643 {
|
|
644 int e1;
|
|
645 /*¿·¤·¤¤¥ì¥¸¥¹¥¿(or ¥¹¥¿¥Ã¥¯)¤ò¼èÆÀ¤¹¤ë*/
|
|
646 if (sz==size_of_int && (e1=get_register())!=-1) {
|
105
|
647 e1=list3(REGISTER,e1,0);
|
61
|
648 *use=list3(t,*use,e1);
|
99
|
649 g_expr_u(assign_expr0(e1,s,ty,ty));
|
|
650 *target = append4(*target,t,ty,e1);
|
138
|
651 } else if (sz==size_of_double && (e1=get_dregister(1))!=-1) {
|
105
|
652 e1=list3(DREGISTER,e1,0);
|
99
|
653 *use=list3(t,*use,e1);
|
83
|
654 g_expr_u(assign_expr0(e1,s,ty,ty));
|
61
|
655 *target = append4(*target,t,ty,e1);
|
138
|
656 } else if (sz==size_of_float && (e1=get_dregister(0))!=-1) {
|
|
657 e1=list3(FREGISTER,e1,0);
|
|
658 *use=list3(t,*use,e1);
|
|
659 g_expr_u(assign_expr0(e1,s,ty,ty));
|
|
660 *target = append4(*target,t,ty,e1);
|
61
|
661 } else {
|
94
|
662 g_expr_u(assign_expr0((e1=list2(LVAR,new_lvar(sz))),s,ty,ty));
|
61
|
663 *target = append4(*target,t,ty,e1);
|
117
|
664 *use=list3(t,*use,e1);
|
61
|
665 }
|
|
666 }
|
|
667
|
|
668 int
|
|
669 circular_dependency(int t,int s,int *target,int *source)
|
|
670 {
|
|
671 int target0=*target;
|
|
672 int t1,sz,ty,s1;
|
|
673 while(target0) {
|
|
674 if (cadddr(target0)==s) {
|
|
675 t1=car(target0);
|
|
676 s=cadddr(target0);
|
|
677 sz=size(ty=caddr(target0));
|
|
678 if(t==t1) {
|
|
679 #if DEBUG_PARALLEL_ASSIGN
|
|
680 printf("# circular dependency %d ty %d+%d sz %d\n",car(t1),ty,cadr(t1),sz);
|
|
681 #endif
|
|
682 return 1;
|
|
683 }
|
|
684 if ((s1=overrap(t1,sz,*source))) {
|
|
685 /* another overrap start over */
|
|
686 return circular_dependency(t,s1,target,source);
|
|
687 }
|
|
688 }
|
|
689 target0=cadr(target0);
|
|
690 }
|
|
691 return 0;
|
|
692 }
|
|
693
|
|
694 void
|
|
695 parallel_assign(int *target,int *source,int *processing,int *use)
|
|
696 {
|
|
697 int t,s,sz,ty,target0,s1;
|
|
698 while(*target) {
|
|
699 target0=*target;
|
|
700 while(target0) {
|
|
701 t=car(target0); s=cadddr(target0);
|
|
702 sz=size(ty=caddr(target0));
|
|
703 if(car(t)==car(s) && cadr(t)==cadr(s)) {
|
|
704 /*½ñ¤¹þ¤ßÀ褬¼«Ê¬¼«¿È*/
|
|
705 #if DEBUG_PARALLEL_ASSIGN
|
|
706 printf("# remove same %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
707 #endif
|
|
708 remove_target(target,t,use);
|
|
709 /* Ç˲õ¤µ¤ì¤Æ¤Ïº¤¤ë¤Î¤Ç¡¢source list¤«¤é¤Ï½ü¤«¤Ê¤¤ */
|
|
710 } else if (!(s1=overrap(t,sz,*source))) {
|
|
711 /* ½Å¤Ê¤Ã¤Æ¤Ê¤¤¤Î¤Ç°Â¿´¤·¤Æ½ñ¤¹þ¤á¤ë */
|
|
712 #if DEBUG_PARALLEL_ASSIGN
|
|
713 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
714 #endif
|
83
|
715 g_expr_u(assign_expr0(t,s,ty,ty));
|
61
|
716 remove_target(target,t,use); remove0(source,s);
|
|
717 } else {
|
|
718 if(circular_dependency(t,s1,target,source)) {
|
|
719 #if DEBUG_PARALLEL_ASSIGN
|
|
720 printf("# saving %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz);
|
|
721 #endif
|
|
722 remove_target(target,t,use); remove0(source,s);
|
|
723 save_target(t,s,target,use,sz,ty);
|
|
724 }
|
|
725 }
|
|
726 target0=cadr(target0);
|
|
727 }
|
|
728 }
|
|
729 }
|
|
730
|
|
731 void
|
|
732 remove0(int *parent,int e)
|
|
733 {
|
|
734 int list;
|
|
735 while ((list=*parent)) {
|
|
736 if (car(list)==e) {
|
|
737 *parent= cadr(list); return;
|
|
738 } else {
|
|
739 parent=&cadr(list);
|
|
740 }
|
|
741 }
|
|
742 }
|
|
743
|
105
|
744 /*
|
61
|
745 void
|
|
746 remove0_all(int *parent,int e)
|
|
747 {
|
|
748 int list;
|
|
749 while ((list=*parent)) {
|
|
750 if (car(list)==e) {
|
|
751 *parent= cadr(list);
|
|
752 } else {
|
|
753 parent=&cadr(list);
|
|
754 }
|
|
755 }
|
|
756 }
|
105
|
757 */
|
61
|
758
|
|
759 int
|
|
760 is_simple(int e1)
|
|
761 {
|
|
762 return (
|
99
|
763 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER ||e1==DREGISTER ||
|
138
|
764 e1==FREGISTER ||
|
81
|
765 e1==GVAR || e1==RGVAR || e1==RLVAR || e1==CRLVAR || e1==CRGVAR ||
|
|
766 e1==DRLVAR || e1==FRLVAR
|
61
|
767 );
|
|
768 }
|
|
769
|
|
770 int
|
|
771 is_same_type(int e1,int e2)
|
|
772 {
|
|
773 int ce1=car(e1);
|
|
774 int ce2=car(e2);
|
|
775 return (
|
81
|
776 (ce1==LVAR && (ce2==RLVAR||ce2==CRLVAR||ce2==FRLVAR||ce2==DRLVAR))
|
|
777 || (ce2==LVAR && (ce1==RLVAR||ce1==CRLVAR||ce1==FRLVAR||ce1==DRLVAR))
|
|
778 || (ce1==GVAR && (ce2==RGVAR||ce2==CRGVAR||ce2==FRGVAR||ce2==DRGVAR))
|
|
779 || (ce2==GVAR && (ce1==RGVAR||ce1==CRGVAR||ce1==FRGVAR||ce1==DRGVAR))
|
61
|
780 );
|
|
781 }
|
|
782
|
|
783 int
|
|
784 is_memory(int e1)
|
|
785 {
|
|
786 int ce1=car(e1);
|
|
787 return (
|
81
|
788 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || ce1==DRLVAR ||
|
|
789 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || ce1==DRGVAR ||
|
138
|
790 ce1==REGISTER|| ce1==DREGISTER || ce1==FREGISTER
|
61
|
791 );
|
|
792 }
|
|
793
|
98
|
794
|
61
|
795 void
|
|
796 jump(int e1, int env)
|
|
797 {
|
99
|
798 int e2,e3,e4,sz,arg_size,ty,regs,fregs;
|
147
|
799 int t0,s0,r,reg;
|
127
|
800 NMTBL *code0 = 0;
|
61
|
801 int target = 0;
|
|
802 int source = 0;
|
|
803 int processing = 0;
|
|
804 int use = 0;
|
|
805
|
|
806 /* ¤Þ¤º¡¢¥µ¥¤¥º¤ò·×»»¤·¤Ê¤¬¤é¡¢·è¤Þ¤Ã¤¿·Á¤ËÍ¡£ */
|
|
807
|
99
|
808 arg_size = 0; regs = 0;
|
|
809 fregs = 0;
|
61
|
810 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) {
|
|
811 e2 = car(e3); sz = size(ty=caddr(e3));
|
138
|
812 if (scalar(ty) && (r = get_input_register_var(regs,0,1))) {
|
|
813 target=list4(r,target,ty,e2); regs++;
|
|
814 } else if (ty==FLOAT && (r = get_input_dregister_var(fregs,0,1,0))) {
|
|
815 target=list4(r, target,ty,e2); fregs++;
|
|
816 } else if (ty==DOUBLE && (r = get_input_dregister_var(fregs,0,1,1))) {
|
|
817 target=list4(r, target,ty,e2); fregs++;
|
61
|
818 } else {
|
|
819 target=list4(list2(LVAR,0), target,ty,e2);
|
|
820 }
|
128
|
821 /* keep arg space for register variables */
|
|
822 arg_size += sz;
|
61
|
823 #if DEBUG_PARALLEL_ASSIGN
|
|
824 printf("# target %d ty %d+%d sz %d\n",car(car(target)),ty,cadr(car(target)),sz);
|
|
825 #endif
|
|
826 }
|
|
827
|
|
828 /* disp ¤òÈô¤ÓÀè»÷¹ç¤ï¤»¤Æ½¤Àµ */
|
98
|
829 if (is_code(fnptr)) {
|
61
|
830 if (-arg_size<disp) disp = -arg_size;
|
|
831 } else {
|
|
832 if (disp_offset-arg_size<disp) disp = disp_offset-arg_size;
|
|
833 }
|
|
834
|
|
835 /* Ê£»¨¤Ê¼°¤òÁ°¤â¤Ã¤Æ·×»»¤·¤Æ¤ª¤¯ */
|
|
836 /* ɬÍפʤé¶É½êÊÑ¿ô¤òÍѤ¤¤ë¡£ */
|
|
837 /* ¶É½êÊÑ¿ô¤Ø¤Î¥ª¥Õ¥»¥Ã¥È¤ò³Ð¤¨¤Æ¤ª¤¯ */
|
|
838
|
|
839 for (e2 = target; e2; e2 = cadr(e2)) {
|
|
840 t0=car(e2); s0=cadddr(e2);
|
|
841 sz=size(ty=caddr(e2));
|
|
842 if(car(t0)==LVAR) {
|
|
843 /* ¤³¤³¤Ç¡¢½ñ¹þÀ襢¥É¥ì¥¹¤ò·è¤á¤ë */
|
|
844 cadr(t0)=-arg_size;
|
|
845 }
|
128
|
846 arg_size-=sz;
|
61
|
847 if (!is_simple(car(s0))) {
|
94
|
848 g_expr_u(assign_expr0((e4=list2(LVAR,new_lvar(sz))),s0,ty,ty));
|
117
|
849 use=list3(ty,use,e1);
|
61
|
850 cadddr(e2)=e4;
|
|
851 s0=e4;
|
|
852 } else if (is_same_type(t0,s0)) {
|
|
853 if(cadr(t0)==cadr(s0)) {
|
|
854 #if DEBUG_PARALLEL_ASSIGN
|
|
855 printf("# remove same memory %d ty %d+%d sz %d\n",car(t0),ty,cadr(t0),sz);
|
|
856 #endif
|
|
857 /* we should check size also (but currently useless */
|
|
858 remove0(&target,t0);
|
|
859 /* still we have source to avoid overwrite */
|
|
860 }
|
|
861 }
|
|
862 if(is_memory(s0)) {
|
|
863 source=list3(s0,source,sz);
|
|
864 #if DEBUG_PARALLEL_ASSIGN
|
|
865 printf("# source %d ty %d+%d sz %d\n",car(car(source)),ty,cadr(car(source)),sz);
|
|
866 #endif
|
|
867 }
|
|
868 }
|
|
869
|
|
870 /* compute jump address */
|
|
871 e2 = cadr(e1);
|
|
872 if (car(e2) == FNAME) {
|
|
873 code0=(NMTBL *)cadr(e2);
|
98
|
874 if (!is_code(code0)) {
|
61
|
875 error(TYERR); return;
|
|
876 }
|
|
877 } else { /* indirect */
|
|
878 g_expr(e2);
|
|
879 emit_push();
|
|
880 }
|
|
881 if (env) {
|
|
882 g_expr(env);
|
|
883 emit_push();
|
|
884 }
|
|
885
|
|
886 /* ÊÂÎóÂåÆþ¤ò¼Â¹Ô */
|
|
887 parallel_assign(&target,&source,&processing,&use);
|
|
888 while (use) {
|
147
|
889 reg = car(caddr(use));
|
|
890 if (reg==REGISTER||reg==FREGISTER||reg==DREGISTER)
|
99
|
891 free_register(cadr(caddr(use)));
|
117
|
892 else if (car(caddr(use))==LVAR)
|
|
893 free_lvar(cadr(caddr(use)));
|
99
|
894 use=cadr(use);
|
61
|
895 }
|
|
896 if(target) error(-1);
|
|
897
|
|
898 if (env) {
|
|
899 /* change the frame pointer */
|
|
900 e3 = emit_pop(0);
|
|
901 code_frame_pointer(e3);
|
|
902 emit_pop_free(e3);
|
98
|
903 } else if (is_function(fnptr)) {
|
128
|
904 if (car(e2) != FNAME) {
|
|
905 e2 = emit_pop(0);
|
|
906 code_fix_frame_pointer(disp_offset);
|
|
907 code_indirect_jmp(e2);
|
|
908 emit_pop_free(e2);
|
|
909 return;
|
|
910 }
|
61
|
911 code_fix_frame_pointer(disp_offset);
|
|
912 }
|
|
913
|
|
914 if (car(e2) == FNAME) {
|
|
915 code_jmp(code0->nm);
|
|
916 } else {
|
|
917 e2 = emit_pop(0);
|
|
918 code_indirect_jmp(e2);
|
|
919 emit_pop_free(e2);
|
|
920 }
|
|
921 }
|
|
922
|
|
923 void
|
|
924 machinop(int e1)
|
|
925 {
|
|
926 int e2,e3,op;
|
|
927
|
|
928 e2 = cadr(e1);
|
|
929 op = car(e1);
|
|
930 e3 = caddr(e1);
|
|
931 g_expr(e3);
|
|
932 emit_push();
|
|
933 g_expr(e2);
|
|
934 tosop(car(e1),(e2=pop_register()));
|
|
935 emit_pop_free(e2);
|
|
936 return;
|
|
937 }
|
|
938
|
81
|
939 void
|
133
|
940 dmachinop(int e1,int d)
|
81
|
941 {
|
|
942 int e2,e3,op;
|
|
943
|
|
944 e2 = cadr(e1);
|
|
945 op = car(e1);
|
|
946 e3 = caddr(e1);
|
|
947 g_expr(e3);
|
133
|
948 emit_dpush(d);
|
81
|
949 g_expr(e2);
|
133
|
950 dtosop(car(e1),(e2=emit_dpop(d)));
|
138
|
951 emit_dpop_free(e2,d);
|
81
|
952 return;
|
|
953 }
|
|
954
|
61
|
955 void
|
|
956 sassign(int e1)
|
|
957 {
|
|
958 int e2,e3,e4,sz,xreg,det;
|
|
959
|
|
960 /* structure assignment */
|
|
961 e2 = cadr(e1); /* pointer variable to the struct */
|
|
962 e3 = cadr(e2); /* offset of the variable (distination) */
|
|
963 e4 = caddr(e1); /* right value (source) */
|
|
964 sz = cadddr(e1); /* size of struct or union */
|
|
965 g_expr(e4);
|
|
966 emit_push();
|
|
967 g_expr(e2);
|
|
968 xreg = emit_pop(0);
|
|
969 /* °ìÈÌŪ¤Ë¤Ï¥³¥Ô¡¼¤Î¥ª¡¼¥Ð¥é¥Ã¥×¤Î¾õ¶·¤Ï¼Â¹Ô»þ¤Ë¤·¤«¤ï¤«¤é¤Ê¤¤ */
|
|
970 /* ¤·¤«¤·¡¢¤ï¤«¤ë¾ì¹ç¤â¤¢¤ë */
|
|
971 if (car(e4)==RSTRUCT) e4=cadr(e4);
|
|
972 if (is_same_type(e2,e4)) {
|
|
973 if(cadr(e2)<cadr(e4)) sz=-sz;
|
|
974 det=1;
|
|
975 } else {
|
|
976 det = 0;
|
|
977 }
|
|
978 emit_copy(xreg,creg,sz,0,1,det);
|
|
979 emit_pop_free(xreg);
|
|
980 return;
|
|
981 }
|
|
982
|
|
983 void
|
118
|
984 assign_opt(int e5,int e2,int e4,int byte)
|
|
985 {
|
|
986 int reg;
|
|
987 /* e2=e4 */
|
|
988 if (e5==REGISTER) {
|
|
989 reg = cadr(e4);
|
|
990 switch(car(e2)) {
|
|
991 case GVAR: code_assign_gvar(e2,reg,byte); return;
|
119
|
992 case LVAR: code_assign_lvar(cadr(e2),reg,byte); return;
|
118
|
993 case REGISTER: code_assign_register(cadr(e2),byte,reg); return;
|
|
994 }
|
|
995 g_expr(e2);
|
|
996 code_assign(e2,byte,reg);
|
|
997 return;
|
|
998 }
|
|
999 /* e2 is register now */
|
|
1000 if (car(e2)!=REGISTER) error(-1);
|
|
1001 reg = cadr(e2);
|
|
1002 switch(e5) {
|
|
1003 case CRGVAR: code_crgvar(e4,reg); return;
|
|
1004 case RGVAR: code_rgvar(e4,reg); return;
|
119
|
1005 case CRLVAR: code_crlvar(cadr(e4),reg); return;
|
|
1006 case RLVAR: code_rlvar(cadr(e4),reg); return;
|
118
|
1007 case GVAR: code_gvar(e4,reg); return;
|
119
|
1008 case LVAR: code_lvar(cadr(e4),reg); return;
|
118
|
1009 case CONST: code_const(cadr(e4),reg); return;
|
|
1010 case ADDRESS:
|
|
1011 if (car(cadr(e4))==STRING) code_string(cadr(e4),reg);
|
|
1012 else code_gvar(cadr(e4),reg);
|
|
1013 return;
|
|
1014 case FNAME: code_fname((NMTBL*)cadr(e4),reg); return;
|
|
1015 case STRING: code_string(e4,reg); return;
|
135
|
1016 default: error(-1);
|
118
|
1017 }
|
|
1018 }
|
|
1019
|
|
1020 void
|
61
|
1021 assign(int e1)
|
|
1022 {
|
118
|
1023 int e2,e4,byte,e5;
|
61
|
1024
|
|
1025 byte=(car(e1) == CASS);
|
|
1026 /* e2=e4 */
|
|
1027 e2 = cadr(e1);
|
118
|
1028 e4 = caddr(e1);e5=car(e4);
|
|
1029 if (!use && (
|
|
1030 (e5==REGISTER) ||
|
|
1031 (car(e2)==REGISTER&&(
|
|
1032 e5== CRGVAR || e5== CRLVAR || e5== RGVAR || e5== RLVAR ||
|
|
1033 e5== GVAR || e5== LVAR ||
|
|
1034 e5== CONST || e5== FNAME || e5== STRING ||
|
|
1035 (e5==ADDRESS&&car(cadr(e4))==STRING) ||
|
|
1036 (e5==ADDRESS&&car(cadr(e4))==GVAR) )))) {
|
147
|
1037 creg = use_int(creg);
|
118
|
1038 assign_opt(e5,e2,e4,byte);
|
|
1039 return;
|
|
1040 }
|
61
|
1041 switch(car(e2)) {
|
|
1042 case GVAR: /* i=3 */
|
|
1043 g_expr(e4);
|
103
|
1044 code_assign_gvar(e2,creg,byte);
|
61
|
1045 return;
|
|
1046 case LVAR:
|
|
1047 g_expr(e4);
|
119
|
1048 code_assign_lvar(cadr(e2),creg,byte);
|
61
|
1049 return;
|
|
1050 case REGISTER:
|
|
1051 g_expr(e4);
|
|
1052 if (creg!=cadr(e2))
|
111
|
1053 code_assign_register(cadr(e2),byte,creg);
|
61
|
1054 return;
|
|
1055 }
|
|
1056 g_expr(e2);
|
|
1057 emit_push();
|
|
1058 g_expr(e4);
|
|
1059 e2 = emit_pop(0);
|
118
|
1060 code_assign(e2,byte,creg);
|
61
|
1061 emit_pop_free(e2);
|
|
1062 return;
|
|
1063 }
|
|
1064
|
|
1065 void
|
118
|
1066 dassign_opt(int e5,int e2,int e4,int d)
|
|
1067 {
|
|
1068 int reg;
|
|
1069 /* e2=e4 */
|
138
|
1070 if (e5==DREGISTER||e5==FREGISTER) {
|
118
|
1071 reg = cadr(e4);
|
|
1072 switch(car(e2)) {
|
|
1073 case GVAR: /* i=3 */
|
|
1074 code_dassign_gvar(e2,reg,d);
|
|
1075 return;
|
|
1076 case LVAR:
|
119
|
1077 code_dassign_lvar(cadr(e2),reg,d);
|
118
|
1078 return;
|
|
1079 case DREGISTER:
|
138
|
1080 case FREGISTER:
|
118
|
1081 if (reg!=cadr(e2))
|
138
|
1082 code_dassign_dregister(cadr(e2),d,reg);
|
118
|
1083 return;
|
135
|
1084 default:
|
|
1085 error(-1);
|
118
|
1086 }
|
|
1087 }
|
|
1088 /* e2 is register now */
|
138
|
1089 if (car(e2)!=DREGISTER && car(e2)!=FREGISTER) error(-1);
|
118
|
1090 reg = cadr(e2);
|
|
1091 switch(e5) {
|
135
|
1092 case FRGVAR:
|
118
|
1093 case DRGVAR: code_drgvar(e4,d,reg); return;
|
135
|
1094 case FRLVAR:
|
119
|
1095 case DRLVAR: code_drlvar(cadr(e4),d,reg); return;
|
135
|
1096 case FCONST:
|
138
|
1097 case DCONST: code_dconst(e4,reg,1); return;
|
135
|
1098 default:
|
118
|
1099 error(-1);
|
|
1100 }
|
|
1101 }
|
|
1102
|
|
1103 void
|
81
|
1104 dassign(int e1)
|
|
1105 {
|
118
|
1106 int e2,e3,e4,d,e5;
|
81
|
1107
|
|
1108 /* e2=e4 */
|
|
1109 e2 = cadr(e1);
|
|
1110 e3 = cadr(e2);
|
118
|
1111 e4 = caddr(e1); e5=car(e4);
|
82
|
1112 d = (car(e1)==LASS)?2:(car(e1)==DASS)?1:0;
|
118
|
1113 if (!use && (
|
138
|
1114 (e5==DREGISTER) || (e5==FREGISTER) ||
|
133
|
1115 (car(e2)==DREGISTER&&(e5==DRGVAR||e5==DRLVAR||e5==DCONST))||
|
|
1116 (car(e2)==DREGISTER&&(e5==FRGVAR||e5==FRLVAR||e5==FCONST))
|
118
|
1117 )) {
|
147
|
1118 creg = d?use_double(creg):use_float(creg);
|
118
|
1119 dassign_opt(e5,e2,e4,d);
|
|
1120 return;
|
|
1121 }
|
81
|
1122 switch(car(e2)) {
|
82
|
1123 case GVAR:
|
81
|
1124 g_expr(e4);
|
151
|
1125 creg = d?use_double(creg):use_float(creg);
|
147
|
1126 code_dassign_gvar(e2,creg,d);
|
81
|
1127 return;
|
|
1128 case LVAR:
|
|
1129 g_expr(e4);
|
151
|
1130 creg = d?use_double(creg):use_float(creg);
|
147
|
1131 code_dassign_lvar(cadr(e2),creg,d);
|
81
|
1132 return;
|
111
|
1133 case DREGISTER:
|
138
|
1134 case FREGISTER:
|
111
|
1135 g_expr(e4);
|
147
|
1136 if (creg!=cadr(e2)) {
|
|
1137 if (d) creg = use_double(creg); else creg = use_float(creg);
|
|
1138 code_dassign_dregister(cadr(e2),d,creg);
|
|
1139 }
|
111
|
1140 return;
|
81
|
1141 }
|
|
1142 g_expr(e2);
|
82
|
1143 emit_push();
|
81
|
1144 g_expr(e4);
|
82
|
1145 e2 = emit_pop(0);
|
147
|
1146 code_dassign(e2,creg,d);
|
82
|
1147 emit_pop_free(e2);
|
81
|
1148 return;
|
|
1149 }
|
|
1150
|
|
1151 void
|
61
|
1152 assop(int e1)
|
|
1153 {
|
|
1154 int e2,e3,byte,op;
|
|
1155
|
|
1156 /* e2 op= e3 */
|
|
1157 byte = (car(e1) == CASSOP);
|
|
1158 e2 = cadr(e1);
|
|
1159 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
1160 e3 = caddr(e1);
|
|
1161 op = cadddr(e1);
|
|
1162
|
147
|
1163 creg = use_int(creg);
|
61
|
1164 g_expr(e3);
|
|
1165 if (car(e2)==REGISTER) {
|
|
1166 code_register_assop(cadr(e2),op,byte);
|
|
1167 return;
|
|
1168 }
|
|
1169 emit_push();
|
|
1170 g_expr(e2);
|
|
1171 code_assop(op,byte);
|
|
1172 return;
|
|
1173 }
|
|
1174
|
81
|
1175 void
|
|
1176 dassop(int e1)
|
|
1177 {
|
83
|
1178 int e2,e3,op,d;
|
81
|
1179
|
83
|
1180 /* e2 op= e3 */
|
|
1181 d = (car(e1) == DASSOP);
|
|
1182 e2 = cadr(e1);
|
|
1183 if (car(e2)==INDIRECT) e2=cadr(e2);
|
|
1184 e3 = caddr(e1);
|
|
1185 op = cadddr(e1);
|
|
1186
|
147
|
1187 creg = d?use_double(creg):use_float(creg);
|
83
|
1188 g_expr(e3);
|
133
|
1189 emit_dpush(d);
|
83
|
1190 g_expr(e2);
|
144
|
1191 if (car(e2)==DREGISTER||car(e2)==FREGISTER) {
|
|
1192 /* code_register_dassop(cadr(e2),op,d); */
|
|
1193 error(-1); /* unsupported now */
|
|
1194 return;
|
|
1195 }
|
83
|
1196 code_dassop(op,d);
|
|
1197 return;
|
81
|
1198 }
|
61
|
1199
|
95
|
1200 void
|
|
1201 cmpdimm(int e, int csreg)
|
|
1202 {
|
|
1203 code_cmpdimm(e, csreg);
|
|
1204 }
|
|
1205
|
|
1206 int
|
|
1207 csvalue()
|
|
1208 {
|
|
1209 return code_csvalue();
|
|
1210 }
|
|
1211
|
|
1212
|
61
|
1213 int
|
|
1214 fwdlabel(void)
|
|
1215 {
|
|
1216 return labelno++;
|
|
1217 }
|
|
1218
|
|
1219 void
|
|
1220 fwddef(int l)
|
|
1221 {
|
|
1222 control=1;
|
66
|
1223 if (!chk)
|
92
|
1224 code_label(l);
|
61
|
1225 }
|
|
1226
|
|
1227 int
|
|
1228 backdef(void)
|
|
1229 {
|
|
1230 control=1;
|
66
|
1231 if (!chk)
|
92
|
1232 code_label(labelno);
|
61
|
1233 return labelno++;
|
|
1234 }
|
|
1235
|
|
1236 void
|
|
1237 def_label(int cslabel, int dlabel)
|
|
1238 {
|
|
1239 int fl;
|
|
1240
|
|
1241 fl = 0;
|
|
1242 if (control) {
|
|
1243 jmp(fl=fwdlabel());
|
|
1244 }
|
|
1245 fwddef(cslabel);
|
|
1246 if (dlabel)
|
|
1247 jmp(dlabel);
|
|
1248 if (fl) {
|
|
1249 fwddef(fl);
|
|
1250 }
|
|
1251 }
|
|
1252
|
|
1253 void
|
|
1254 gen_source(char *s)
|
|
1255 {
|
|
1256 printf("%s",s);
|
|
1257 }
|
|
1258
|
|
1259 void
|
|
1260 ret(void)
|
|
1261 {
|
107
|
1262 code_set_return_register(1);
|
61
|
1263 jmp(retlabel);
|
|
1264 }
|
|
1265
|
66
|
1266 void
|
|
1267 opening(char *filename)
|
|
1268 {
|
|
1269 emit_init();
|
|
1270 if (!chk)
|
|
1271 code_opening(filename);
|
|
1272 }
|
|
1273
|
|
1274 void
|
|
1275 closing()
|
|
1276 {
|
|
1277 if (!chk)
|
|
1278 code_closing();
|
|
1279 }
|
|
1280
|
99
|
1281 int
|
|
1282 contains_in_list(int e,int type)
|
|
1283 {
|
|
1284 while(e) {
|
|
1285 if(contains(car(e),type)) return 1;
|
|
1286 e = cadr(e);
|
|
1287 }
|
|
1288 return 0;
|
|
1289 }
|
|
1290
|
|
1291 int
|
|
1292 contains(int e,int type)
|
|
1293 {
|
|
1294 while(e) {
|
|
1295 if (car(e)==type) return 1;
|
|
1296 switch (car(e)){
|
|
1297 /* list arguments */
|
|
1298 case FUNCTION: case CODE:
|
|
1299 return contains_in_list(caddr(e),type);
|
|
1300 /* unary operators */
|
|
1301 case INDIRECT: case RINDIRECT: case CRINDIRECT:
|
|
1302 case DRINDIRECT: case FRINDIRECT: case ADDRESS: case MINUS: case DMINUS:
|
135
|
1303 case BNOT: case LNOT:
|
99
|
1304 case PREINC: case POSTINC: case DPREINC: case DPOSTINC:
|
|
1305 case FPREINC: case FPOSTINC: case CPOSTINC:
|
|
1306 case CPREINC: case CPOSTDEC: case CPREDEC:
|
|
1307 case RSTRUCT:
|
135
|
1308 /* case I2D: case D2I: case U2D: case D2U: */
|
|
1309 case CONV:
|
99
|
1310 e = cadr(e);
|
|
1311 continue;
|
|
1312 /* biary operators */
|
|
1313 case MUL: case UMUL: case DIV: case UDIV: case MOD: case UMOD:
|
|
1314 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
1315 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP:
|
|
1316 case DMUL: case DDIV: case DADD: case DSUB: case DCMP: case DCMPGE:
|
133
|
1317 case FMUL: case FDIV: case FADD: case FSUB: case FCMP: case FCMPGE:
|
99
|
1318 case SASS: case ASS: case CASS: case FASS: case DASS: case LASS:
|
|
1319 case ASSOP: case CASSOP: case DASSOP: case FASSOP: case COMMA:
|
135
|
1320 case EQ: case NEQ:
|
151
|
1321 case LAND: case LOR:
|
135
|
1322 case DOP+EQ: case DOP+NEQ:
|
|
1323 case FOP+EQ: case FOP+NEQ:
|
|
1324 case LT: case ULT: case LE: case ULE:
|
|
1325 case GT: case UGT: case GE: case UGE:
|
|
1326 case DOP+LT: case DOP+LE: case DOP+GT: case DOP+GE:
|
|
1327 case FOP+LT: case FOP+LE: case FOP+GT: case FOP+GE:
|
99
|
1328 if (contains(cadr(e),type)) return 1;
|
|
1329 e = caddr(e);
|
|
1330 continue;
|
|
1331 /* tarary operators */
|
133
|
1332 case COND: case DCOND: case FCOND:
|
99
|
1333 if (contains(cadr(e), type)) return 1;
|
|
1334 if (contains(caddr(e),type)) return 1;
|
|
1335 e = cadddr(e);
|
|
1336 continue;
|
135
|
1337 /* nullary operators */
|
99
|
1338 case GVAR: case RGVAR: case CRGVAR: case LVAR:
|
138
|
1339 case REGISTER: case DREGISTER: case FREGISTER:
|
99
|
1340 case RLVAR: case CRLVAR: case FRLVAR: case FRGVAR:
|
|
1341 case DRLVAR: case DRGVAR:
|
135
|
1342 case FNAME: case CONST: case DCONST: case FCONST: case STRING:
|
|
1343 case RETURN: case ENVIRONMENT:
|
|
1344 return 0;
|
|
1345 default:
|
|
1346 fprintf(stderr,"Unknown Tree ID %d\n",car(e));
|
|
1347 error(-1);
|
99
|
1348 return 0;
|
|
1349 }
|
|
1350 }
|
|
1351 return 0;
|
|
1352 }
|
|
1353
|
61
|
1354 /* end */
|