462
|
1 /* Micro-C Partial Evaluator Part */
|
|
2 /* $Id$ */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include "mc.h"
|
|
6 #include "mc-parse.h"
|
|
7 #include "mc-codegen.h"
|
|
8 #include "mc-switch.h"
|
464
|
9 #include "mc-code.h"
|
|
10 #include "mc-inline.h"
|
462
|
11
|
463
|
12 static int pvartable;
|
|
13 static int pdisp;
|
462
|
14
|
|
15 /*
|
|
16 Basic code generator from parse tree
|
|
17 */
|
|
18
|
|
19 extern void
|
|
20 st_decl(int e1){
|
|
21 // NMTBL *n = (NMTBL *)caddr(e1);
|
|
22 // int stmode = cadddr(e1);
|
|
23 }
|
|
24
|
|
25 extern void
|
|
26 st_if(int e1){
|
|
27 int l1,l2,slfree;
|
|
28 int e2=caddr(e1),e3;
|
|
29 // conv->if_();
|
|
30 slfree=lfree;
|
|
31 checkret();
|
|
32 l1 = bexpr(car(e2),0,fwdlabel());
|
|
33 // conv->if_then_();
|
|
34 g_expr_u(cadr(e2));
|
|
35 checkret();
|
|
36 if ((e3=caddr(e2))) { // else
|
|
37 // conv->if_else_();
|
|
38 if ((l2 = control))
|
|
39 gen_jmp(l2=fwdlabel());
|
|
40 fwddef(l1);
|
|
41 g_expr_u(e3);
|
|
42 checkret();
|
|
43 if (l2) fwddef(l2);
|
|
44 } else {
|
|
45 fwddef(l1);
|
|
46 }
|
|
47 // conv->if_endif_();
|
|
48 }
|
|
49
|
|
50
|
|
51 extern void
|
|
52 st_do(int e1){
|
|
53 int sbreak,scontinue,l;
|
|
54
|
|
55 sbreak=blabel;
|
|
56 scontinue=clabel;
|
|
57 blabel=fwdlabel();
|
|
58 clabel=fwdlabel();
|
|
59 control=1;
|
|
60 checkret();
|
|
61 l=backdef();
|
|
62 // conv->dowhile_();
|
|
63 g_expr_u(cadddr(e1));
|
|
64 checkret();
|
|
65 // conv->dowhile_cond_();
|
|
66 bexpr(caddr(e1),1,l);
|
|
67 // conv->dowhile_end_();
|
|
68 fwddef(blabel);
|
|
69 clabel=scontinue;
|
|
70 blabel=sbreak;
|
|
71 }
|
|
72
|
|
73
|
|
74 extern void
|
|
75 st_while(int e1){
|
|
76 int sbreak,scontinue,e;
|
|
77
|
|
78 sbreak=blabel;
|
|
79 scontinue=clabel;
|
|
80 blabel=fwdlabel();
|
|
81 control=1;
|
|
82 checkret();
|
|
83 clabel=backdef();
|
|
84 // conv->while_();
|
|
85 // conv->while_body_();
|
|
86 if(!(e=cadddr(e1))) {
|
|
87 bexpr(caddr(e1),1,clabel);
|
|
88 // conv->sm_();
|
|
89 } else {
|
|
90 bexpr(caddr(e1),0,blabel);
|
|
91 g_expr_u(e);
|
|
92 checkret();
|
|
93 if(control)
|
|
94 gen_jmp(clabel);
|
|
95 }
|
|
96 // conv->while_end_();
|
|
97 fwddef(blabel);
|
|
98 clabel=scontinue;
|
|
99 blabel=sbreak;
|
|
100 }
|
|
101
|
|
102
|
|
103 extern void
|
|
104 st_for(int e1){
|
|
105 int p0,p1,p2,body;
|
|
106 int l,e;
|
|
107 int sbreak=blabel;
|
|
108 int scontinue=clabel;
|
|
109
|
|
110 e = caddr(e1);
|
|
111 p0 = car(e); p1 = cadr(e); p2 = caddr(e); body = cadddr(e);
|
|
112
|
|
113 blabel=fwdlabel();
|
|
114 // conv->for_();
|
|
115 if (p0) {
|
|
116 checkret();
|
|
117 g_expr_u(p0);
|
|
118 }
|
|
119 // conv->for1_();
|
|
120 control=1;
|
|
121 checkret();
|
|
122 l=backdef();
|
|
123 if (p1) {
|
|
124 bexpr(p1,0,blabel);
|
|
125 }
|
|
126 // conv->for2_();
|
|
127 // conv->for_body_();
|
|
128 if (!p2) {
|
|
129 clabel=l;
|
|
130 g_expr_u(body);
|
|
131 checkret();
|
|
132 } else {
|
|
133 clabel=fwdlabel();
|
|
134 g_expr_u(body);
|
|
135 checkret();
|
|
136 fwddef(clabel);
|
|
137 g_expr_u(p2);
|
|
138 }
|
|
139 // conv->for_end_();
|
|
140 gen_jmp(l);
|
|
141 fwddef(blabel);
|
|
142 clabel=scontinue;
|
|
143 blabel=sbreak;
|
|
144 }
|
|
145
|
|
146
|
|
147 extern void
|
|
148 st_switch(int e1){
|
|
149 int sbreak,scase,sdefault,slfree,svalue,slist;
|
|
150
|
|
151 checkret();
|
|
152 slist = cslist;
|
|
153 cslist = 0;
|
|
154 sbreak=blabel; /* save parents break label */
|
|
155 blabel=fwdlabel();
|
|
156 sdefault=dlabel; /* save parents default label */
|
|
157 dlabel=0;
|
|
158 scase=cslabel; /* save parents next case label */
|
|
159 // conv->switch_();
|
|
160 slfree=lfree;
|
|
161 svalue=csvalue1; /* save parents switch value */
|
|
162 gexpr(caddr(e1),1);
|
|
163 csvalue1=csvalue() ;
|
|
164 // conv->switch_body_();
|
|
165 cslabel = control = 0;
|
|
166 g_expr_u(cadddr(e1));
|
|
167 // conv->switch_end_();
|
|
168 checkret();
|
|
169 #if CASE_CODE
|
|
170 if (control) gen_jmp(blabel);
|
|
171 genswitch(cslist,cslabel);
|
|
172 #else
|
|
173 if(dlabel) def_label(cslabel,dlabel);
|
|
174 else fwddef(cslabel);
|
|
175 #endif
|
|
176 csvalue1=svalue;
|
|
177 cslabel=scase;
|
|
178 dlabel=sdefault;
|
|
179 fwddef(blabel);
|
|
180 blabel=sbreak;
|
|
181 cslist = slist;
|
|
182 }
|
|
183
|
|
184
|
|
185 extern void
|
|
186 st_comp(int e1){
|
|
187 g_expr_u(caddr(e1));
|
|
188 }
|
|
189
|
|
190
|
|
191 extern void
|
|
192 st_break(int e1){
|
|
193 checkret();
|
|
194 // conv->break_();
|
|
195 if (control)
|
|
196 gen_jmp(blabel);
|
|
197 }
|
|
198
|
|
199
|
|
200 extern void
|
|
201 st_continue(int e1){
|
|
202 checkret();
|
|
203 // conv->continue_();
|
|
204 if (control) gen_jmp(clabel);
|
|
205 }
|
|
206
|
|
207
|
|
208 extern void
|
|
209 st_case(int e1){
|
|
210 #if CASE_CODE
|
|
211 int l,clist=caddr(e1),c;
|
|
212 l = fwdlabel();
|
|
213 // conv->case_begin_(0,0);
|
|
214 // conv->case_(0,0);
|
|
215 if (retpending) {
|
|
216 ret(); retpending=0;
|
|
217 }
|
|
218 if (!cslabel) {
|
|
219 if (!control) {
|
|
220 cmpdimm(car(clist),csvalue1,cslabel=fwdlabel(),1);
|
|
221 caddr(clist)=0;
|
|
222 } else {
|
|
223 error(-1);
|
|
224 }
|
|
225 }
|
|
226 while(clist) {
|
|
227 caddr(clist) = l;
|
|
228 clist = cadr(c=clist); cadr(c) = 0; // insert destroy cadr of clist
|
|
229 cslist=insert_ascend(cslist,c,docase_eq);
|
|
230 }
|
|
231 fwddef(l);
|
|
232 control=1;
|
|
233 #else
|
|
234 /* casading branch implementation */
|
|
235 int c,l;
|
|
236 c = caddr(e1);
|
|
237 if (retpending) {
|
|
238 ret(); retpending=0;
|
|
239 }
|
|
240 // conv->case_begin_(0,0);
|
|
241 // conv->case_(0,0);
|
|
242 l=fwdlabel();
|
|
243 if (control) {
|
|
244 control=0;
|
|
245 gen_jmp(l);
|
|
246 }
|
|
247 if (cslabel) fwddef(cslabel);
|
|
248 while(cadr(c)) {
|
|
249 cmpdimm(car(c),csvalue1,l,0);
|
|
250 c=cadr(c);
|
|
251 }
|
|
252 cmpdimm(car(c),csvalue1,cslabel=fwdlabel(),1);
|
|
253 if (l) fwddef(l);
|
|
254 #endif
|
|
255 }
|
|
256
|
|
257
|
|
258 extern void
|
|
259 st_default(int e1){
|
|
260 control=1;
|
|
261 checkret();
|
|
262 if (dlabel) error(STERR); // double default:
|
|
263 dlabel = backdef();
|
|
264 // conv->case_(0,1);
|
|
265 }
|
|
266
|
|
267
|
|
268 extern void
|
|
269 st_return(int e1){
|
|
270 int e;
|
|
271
|
|
272 if (!cslabel) gen_jmp(cslabel = fwdlabel());
|
|
273 if(!(e=caddr(e1))) {
|
|
274 // conv->return_();
|
|
275 // conv->return_end_();
|
|
276 retpending = 1;
|
504
|
277 g_expr_u(cadr(e1));
|
462
|
278 return;
|
|
279 }
|
|
280 // conv->return_();
|
|
281 if (struct_return) {
|
|
282 if ((car(type)==STRUCT || car(type)==UNION)&&
|
|
283 size(type)==cadr(struct_return)) {
|
|
284 if(car(e)==RSTRUCT && car(cadr(e))==FUNCTION) {
|
|
285 /* pass the return pointer to the called function */
|
|
286 replace_return_struct(cadr(e),
|
|
287 rvalue_t(car(struct_return),caddr(struct_return)));
|
|
288 replace_return_struct(cadr(e),
|
|
289 rvalue_t(car(struct_return),caddr(struct_return)));
|
|
290 gexpr(cadr(e),0);
|
|
291 } else {
|
|
292 type = caddr(struct_return);
|
|
293 // e1 = rvalue_t(cadr(struct_return),INT); /* size */
|
|
294 e1 = cadr(struct_return); /* size */
|
|
295 gexpr(list4(STASS,rvalue(car(struct_return)),e,e1),0);
|
|
296 }
|
|
297 } else {
|
|
298 error(TYERR); /* should check compatible */
|
|
299 }
|
|
300 } else {
|
|
301 gexpr(correct_type(e,cadr(fnptr->ty)),1);
|
|
302 }
|
|
303 // conv->return_end_();
|
|
304 retpending = 1;
|
|
305 }
|
|
306
|
|
307
|
|
308 extern void
|
|
309 st_goto(int e){
|
|
310 NMTBL *nlist,*nptr0;
|
|
311 int t,e1,e2,env;
|
|
312
|
|
313 checkret();
|
|
314 // conv->goto_();
|
|
315 e1 = caddr(e);
|
|
316 if (car(e1)==RINDIRECT) {
|
|
317 gen_indirect_goto(cadr(e1));
|
505
|
318 return ;
|
462
|
319 } else if (car(e1)==FLABEL) {
|
|
320 nlist = (NMTBL *)cadr(e1);
|
|
321 nptr0 = name_space_search(nlist,0);
|
|
322 t = nptr0->sc;
|
|
323 if (t==EMPTY||t==EXTRN1||t==EXTRN) {
|
|
324 nptr0 = make_local_scope(nlist,nptr0,0);
|
|
325 nptr0->sc = FLABEL;
|
|
326 nptr0->dsp = fwdlabel();
|
|
327 } else if (!(t==FLABEL||t==BLABEL)) {
|
|
328 error(STERR);
|
|
329 }
|
|
330 gen_jmp(nptr0->dsp);
|
|
331 control=0;
|
|
332 // conv->sm_();
|
|
333 // conv->goto_label_(nptr0);
|
505
|
334 return ;
|
462
|
335 } else {
|
|
336 /* CbC continuation */
|
|
337 // conv->jump_(env);
|
|
338 e2 = cadr(e1);
|
|
339 env = caddr(e1);
|
|
340 if (car(e2) == FNAME) {
|
|
341 nptr0=(NMTBL *)cadr(e2);
|
|
342 if (nptr0->sc==EMPTY)
|
|
343 nptr0->sc = EXTRN1;
|
|
344 else if(nptr0->sc==FUNCTION)
|
|
345 nptr0->sc = CODE;
|
|
346 if (nptr0->ty>0&&car(nptr0->ty)==FUNCTION)
|
|
347 car(nptr0->ty)=CODE;
|
|
348 }
|
|
349 gexpr(list3(CODE,e1,env),0);
|
|
350 control=0;
|
|
351 // conv->sm_();
|
505
|
352 return ;
|
462
|
353 }
|
|
354 }
|
|
355
|
|
356
|
|
357 #if ASM_CODE
|
|
358 extern void
|
|
359 st_asm(int e1){
|
|
360 checkret();
|
|
361 g_expr_u(list3(ASM,caddr(e1),cadddr(e1)));
|
|
362 }
|
|
363 #endif
|
|
364
|
|
365
|
|
366 extern void
|
|
367 st_label(int e1){
|
|
368 NMTBL *nptr,*nlist;
|
|
369 nlist = (NMTBL *)caddr(e1);
|
|
370 nptr = name_space_search(nlist,0);
|
|
371 control=1;
|
|
372 checkret();
|
|
373 if(nptr->sc == FLABEL) {
|
|
374 fwddef(nptr->dsp);
|
|
375 } else if(nptr->sc != EMPTY && nptr->sc != EXTRN1 && nptr->sc !=BLABEL) {
|
|
376 error(TYERR);
|
|
377 } else {
|
|
378 nptr->sc=EMPTY;
|
|
379 nptr = make_local_scope(nlist,nptr,0);
|
|
380 nptr->sc = BLABEL;
|
|
381 nptr->dsp = backdef();
|
|
382 }
|
|
383 // conv->label_();
|
|
384 }
|
|
385
|
|
386 extern void
|
|
387 st_comment(int e1){
|
|
388 gen_comment((char *)caddr(e1));
|
|
389 }
|
|
390
|
|
391 /*
|
|
392 partial evaluator
|
|
393 */
|
|
394
|
|
395 static int
|
463
|
396 p_vartable(int e,int adisp,int ldisp)
|
462
|
397 {
|
464
|
398 int i;
|
505
|
399 int pvartable = getfree(adisp-ldisp); // have to be local heap
|
463
|
400 pdisp = pvartable+adisp;
|
503
|
401 for(i=adisp-ldisp;i>=0;i--) {
|
|
402 heap[pvartable+i] = 0;
|
463
|
403 }
|
505
|
404 return pvartable;
|
462
|
405 }
|
|
406
|
|
407 static int
|
|
408 p_lvar(int e1)
|
|
409 {
|
463
|
410 int sz = is_memory(e1);
|
|
411 int d = cadr(e1);
|
|
412 int d1;
|
502
|
413 if ((d1=(heap[pdisp+d]))) return d1;
|
|
414 return (heap[pdisp+d]=list3(LVAR,new_lvar(sz),0));
|
462
|
415 }
|
|
416
|
|
417 static int
|
|
418 pfunction(int e)
|
|
419 {
|
501
|
420 // list4(INLINE,e1,arglist,ftype);
|
|
421 // include code segement case
|
|
422 int e1 = pexpr(cadr(e));
|
|
423 int arglist = caddr(e);
|
|
424 int newargs = 0;
|
|
425 int ftype = cadddr(e);
|
|
426 int e3;
|
|
427 for (e3 = arglist; e3; e3 = cadr(e3)) {
|
|
428 newargs = list3( pexpr(car(e3)), cadr(e3), caddr(e3));
|
|
429 }
|
|
430 newargs = reverse0(newargs);
|
|
431 return list4(INLINE,e1,newargs,ftype);
|
462
|
432 }
|
|
433
|
|
434 static int
|
|
435 prindirect(int e)
|
|
436 {
|
501
|
437 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
438 }
|
|
439
|
|
440 static int
|
|
441 paddress(int e)
|
|
442 {
|
501
|
443 return list2(car(e),pexpr(cadr(e)));
|
462
|
444 }
|
|
445
|
|
446 static int
|
464
|
447 p_conv(int e1,int e2)
|
462
|
448 {
|
501
|
449 return list3(CONV,pexpr(e1),e2);
|
462
|
450 }
|
|
451
|
|
452 static int
|
464
|
453 pbinop(int op,int e1,int e2)
|
462
|
454 {
|
501
|
455 return list3(op,pexpr(e1),pexpr(e2));
|
462
|
456 }
|
|
457
|
|
458 static int
|
502
|
459 prexpr(int op,int e1,int e2)
|
|
460 {
|
|
461 return list3(op,pexpr(e1),pexpr(e2));
|
|
462 }
|
|
463
|
|
464 static int
|
462
|
465 psassign(int e)
|
|
466 {
|
501
|
467 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
468 }
|
|
469
|
|
470 static int
|
|
471 passign(int e)
|
|
472 {
|
501
|
473 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
474 }
|
|
475
|
|
476 static int
|
|
477 passop(int e)
|
|
478 {
|
501
|
479 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
480 }
|
|
481
|
|
482 static int
|
|
483 pdassign(int e)
|
|
484 {
|
501
|
485 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
486 }
|
|
487
|
|
488 static int
|
|
489 pdassop(int e)
|
|
490 {
|
501
|
491 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
492 }
|
|
493
|
|
494 static int
|
|
495 plassign(int e)
|
|
496 {
|
501
|
497 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
498 }
|
|
499
|
|
500 static int
|
|
501 plassop(int e)
|
|
502 {
|
501
|
503 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
504 }
|
|
505
|
|
506 static int
|
|
507 palloc(int e)
|
|
508 {
|
501
|
509 return list2(car(e),pexpr(cadr(e)));
|
462
|
510 }
|
|
511
|
|
512 static int
|
464
|
513 pcomma(int e1,int e2)
|
462
|
514 {
|
501
|
515 return list3(COMMA,pexpr(e1),pexpr(e2));
|
462
|
516 }
|
|
517
|
|
518 static int
|
|
519 prbit_field(int e)
|
|
520 {
|
501
|
521 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
522 }
|
|
523
|
|
524 static int
|
|
525 pbassign(int e)
|
|
526 {
|
501
|
527 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
528 }
|
|
529
|
|
530 static int
|
|
531 pbassop(int e)
|
|
532 {
|
501
|
533 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
534 }
|
|
535
|
|
536 static int
|
|
537 p_decl(int e)
|
|
538 {
|
463
|
539 // list4(ST_DECL,parse,(int)n,stmode);
|
|
540 int stmode=cadddr(e);
|
|
541 NMTBL *n=(NMTBL*)caddr(e);
|
|
542 int dsp = n->dsp;
|
|
543 int v;
|
500
|
544 // in real partial evaluation, we have to check whether this variable
|
|
545 // is used or not.
|
463
|
546 switch(stmode) {
|
|
547 case REGISTER:
|
|
548 switch(n->ty) {
|
|
549 case ULONGLONG: case LONGLONG:
|
|
550 v = get_lregister_var(n); break;
|
|
551 case FLOAT:
|
|
552 v = get_dregister_var(n,0); break;
|
|
553 case DOUBLE:
|
|
554 v = get_dregister_var(n,1); break;
|
|
555 default:
|
|
556 if (scalar(n->ty))
|
|
557 v = get_dregister_var(n,1);
|
|
558 else
|
|
559 error(TYERR);
|
|
560 }
|
|
561 case EXTRN: case EXTRN1: case STATIC:
|
|
562 return pexpr(cadr(e));
|
|
563 default:
|
503
|
564 v = list3(LVAR,new_lvar(size(n->dsp)),(int)n);
|
463
|
565 }
|
|
566 if (heap[pdisp+dsp]) error(-1);
|
|
567 heap[pdisp+dsp]=v;
|
|
568 return pexpr(cadr(e));
|
462
|
569 }
|
|
570
|
|
571 static int
|
500
|
572 p_if(int e1)
|
462
|
573 {
|
500
|
574 int cond,l1,l2;
|
|
575 int e2=caddr(e1),e3;
|
|
576 cond = pexpr(car(e2));
|
|
577 // conv->if_then_();
|
|
578 l1 = pexpr(cadr(e2));
|
|
579 if ((e3=caddr(e2))) { // else
|
|
580 l2 = pexpr(e3);
|
|
581 } else {
|
|
582 l2 = 0;
|
|
583 }
|
|
584 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
585 }
|
|
586
|
|
587 static int
|
|
588 p_do(int e)
|
|
589 {
|
501
|
590 return list4(ST_DO,pexpr(cadr(e)),pexpr(caddr(e)),pexpr(cadddr(e)));
|
462
|
591 }
|
|
592
|
|
593 static int
|
|
594 p_while(int e)
|
|
595 {
|
501
|
596 return list4(ST_WHILE,pexpr(cadr(e)),pexpr(caddr(e)),pexpr(cadddr(e)));
|
462
|
597 }
|
|
598
|
|
599 static int
|
|
600 p_for(int e)
|
|
601 {
|
501
|
602 int e1=caddr(e);
|
|
603 int p0=car(e1);
|
|
604 int p1=cadr(e1);
|
|
605 int p2=caddr(e1);
|
|
606 int p3=cadddr(e1);
|
|
607 return list3(ST_FOR,pexpr(cadr(e)),
|
|
608 list4(pexpr(p0),pexpr(p1),pexpr(p2),pexpr(p3)));
|
462
|
609 }
|
|
610
|
|
611 static int
|
|
612 p_switch(int e)
|
|
613 {
|
503
|
614 return list4(ST_SWITCH,pexpr(cadr(e)),pexpr(caddr(e)),pexpr(cadddr(e)));
|
462
|
615 }
|
|
616
|
|
617 static int
|
|
618 p_comp(int e)
|
|
619 {
|
501
|
620 return list3(ST_COMP,pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
621 }
|
|
622
|
|
623 static int
|
|
624 p_break(int e)
|
|
625 {
|
502
|
626 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
627 }
|
|
628
|
|
629 static int
|
|
630 p_continue(int e)
|
|
631 {
|
502
|
632 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
633 }
|
|
634
|
|
635 static int
|
|
636 p_case(int e)
|
|
637 {
|
502
|
638 return list3(ST_CASE,pexpr(cadr(e)),caddr(e));
|
462
|
639 }
|
|
640
|
|
641 static int
|
|
642 p_default(int e)
|
|
643 {
|
502
|
644 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
645 }
|
|
646
|
|
647 static int
|
|
648 p_return(int e)
|
|
649 {
|
502
|
650 return list3(ST_RETURN,pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
651 }
|
|
652
|
|
653 static int
|
|
654 p_goto(int e)
|
|
655 {
|
501
|
656 int e1;
|
|
657 if ((e1=caddr(e))) {
|
|
658 switch(car(e1)) {
|
|
659 case RINDIRECT: e1=pexpr(e1); break;
|
|
660 case CODE: e1=list3(CODE,pexpr(cadr(e1)),pexpr(caddr(e1))); break;
|
|
661 case FLABEL: break;
|
|
662 }
|
|
663 }
|
502
|
664 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
665 }
|
|
666
|
|
667 static int
|
|
668 p_list_expr(int e)
|
|
669 {
|
|
670 int e3,new = 0;
|
|
671 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
672 new= list2( pexpr(car(e3)), new);
|
|
673 }
|
|
674 return reverse0(new);
|
462
|
675 }
|
|
676
|
|
677 static int
|
|
678 p_asm(int e)
|
|
679 {
|
501
|
680 int param=caddr(e);
|
|
681 int e1 = p_list_expr(cadddr(e));
|
|
682 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
683 }
|
|
684
|
|
685 static int
|
|
686 p_label(int e)
|
|
687 {
|
503
|
688 return list3(ST_LABEL,pexpr(cadr(e)),caddr(e));
|
462
|
689 }
|
|
690
|
|
691 static int
|
|
692 p_bool(int e)
|
|
693 {
|
501
|
694 error(-1);
|
464
|
695 return e;
|
|
696 }
|
462
|
697
|
464
|
698 static int
|
|
699 p_comment(int e)
|
|
700 {
|
501
|
701 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
702 }
|
|
703
|
463
|
704 extern int
|
|
705 pexpr(int e1)
|
462
|
706 {
|
|
707 int e2,e3;
|
|
708
|
463
|
709 // if (inmode) error(-1);
|
502
|
710 if (e1==0) return 0;
|
462
|
711 e2 = cadr(e1);
|
|
712 switch (car(e1)){
|
|
713 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
714 case SURGVAR: case REGISTER:
|
|
715 #if FLOAT_CODE
|
|
716 case DREGISTER: case FREGISTER:
|
500
|
717 case FRGVAR: case DRGVAR:
|
462
|
718 #endif
|
|
719 #if LONGLONG_CODE
|
|
720 case LREGISTER:
|
500
|
721 case LRGVAR: case LURGVAR:
|
462
|
722 #endif
|
|
723 case LABEL: case CONST:
|
|
724 #if FLOAT_CODE
|
|
725 case DCONST: case FCONST:
|
|
726 #endif
|
|
727 #if LONGLONG_CODE
|
|
728 case LCONST:
|
|
729 #endif
|
|
730 case STRING:
|
|
731 case FNAME:
|
503
|
732 case FLABEL:
|
462
|
733 case RSTRUCT:
|
|
734 return e1;
|
|
735 case LVAR:
|
|
736 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
|
737 #if FLOAT_CODE
|
500
|
738 case FRLVAR: case DRLVAR:
|
462
|
739 #endif
|
|
740 #if LONGLONG_CODE
|
500
|
741 case LRLVAR: case LURLVAR:
|
|
742 #endif
|
|
743 return e1;
|
|
744 case IVAR:
|
462
|
745 return p_lvar(e1);
|
|
746 case FUNCTION:
|
|
747 return pfunction(e1);
|
|
748 case CODE:
|
463
|
749 return list2(car(e1),pexpr(e2));
|
462
|
750 case INLINE:
|
463
|
751 return gen_inline(e1);
|
462
|
752 case INDIRECT:
|
|
753 return prindirect(e1);
|
|
754 case RINDIRECT: case URINDIRECT:
|
|
755 case CRINDIRECT: case CURINDIRECT:
|
|
756 case SRINDIRECT: case SURINDIRECT:
|
|
757 #if FLOAT_CODE
|
|
758 case FRINDIRECT: case DRINDIRECT:
|
|
759 #endif
|
|
760 #if LONGLONG_CODE
|
|
761 case LRINDIRECT: case LURINDIRECT:
|
|
762 #endif
|
|
763 return prindirect(e1);
|
|
764 case ADDRESS:
|
502
|
765 return paddress(e1);
|
462
|
766 case MINUS:
|
463
|
767 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
768 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
769 return list2(car(e1),e3);
|
|
770 #if LONGLONG_CODE
|
|
771 case LMINUS:
|
463
|
772 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
773 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
774 return list2(car(e1),e3);
|
|
775 #endif
|
|
776 #if FLOAT_CODE
|
|
777 case DMINUS:
|
463
|
778 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
779 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
780 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
781 return list2(car(e1),e3);
|
|
782 case FMINUS:
|
463
|
783 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
784 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
785 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
786 return list2(car(e1),e3);
|
|
787 #endif
|
|
788 case CONV:
|
501
|
789 return p_conv(caddr(e1),e2);
|
462
|
790 case BNOT: /* ~ */
|
463
|
791 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
792 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
793 return list2(BNOT,e3);
|
|
794 case LNOT: /* ! */
|
463
|
795 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
796 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
797 return list2(LNOT,e3);
|
|
798 case PREINC:
|
|
799 case UPREINC:
|
463
|
800 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
801 if (car(e3)==CONST) return list2(CONST,cadr(e3)+1);
|
503
|
802 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
803 case POSTINC:
|
|
804 case UPOSTINC:
|
463
|
805 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
806 if (car(e3)==CONST) return e3;
|
503
|
807 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
808 #if FLOAT_CODE
|
|
809 case DPREINC: /* ++d */
|
463
|
810 if ((e3 = pexpr(e2))==e2) return e1;
|
|
811 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
812 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
813 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
814 case DPOSTINC: /* d++ */
|
463
|
815 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
816 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
817 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
818 case FPREINC: /* ++f */
|
463
|
819 if ((e3 = pexpr(e2))==e2) return e1;
|
|
820 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
821 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
822 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
823 case FPOSTINC: /* f++ */
|
463
|
824 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
825 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
826 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
827 #endif
|
|
828 #if LONGLONG_CODE
|
|
829 case LPREINC: /* ++d */
|
|
830 case LUPREINC: /* ++d */
|
463
|
831 if ((e3 = pexpr(e2))==e2) return e1;
|
|
832 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
833 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
834 case LPOSTINC: /* d++ */
|
|
835 case LUPOSTINC: /* d++ */
|
463
|
836 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
837 if (car(e3)==LCONST) return e3;
|
503
|
838 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
839 #endif
|
|
840 case MUL: case UMUL:
|
|
841 case DIV: case UDIV:
|
|
842 case MOD: case UMOD:
|
|
843 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
844 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
845 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
846 #if FLOAT_CODE
|
|
847 case DMUL: case DDIV:
|
|
848 case DADD: case DSUB:
|
|
849 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
850 case FMUL: case FDIV:
|
|
851 case FADD: case FSUB:
|
|
852 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
853 #endif
|
|
854 #if LONGLONG_CODE
|
|
855 case LMUL: case LUMUL:
|
|
856 case LDIV: case LUDIV:
|
|
857 case LMOD: case LUMOD:
|
|
858 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
859 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
860 #endif
|
501
|
861 return pbinop(car(e1),e2,caddr(e1));
|
462
|
862 case LCOND: case DCOND: case FCOND: case COND:
|
463
|
863 e3 = pexpr(e2);
|
|
864 if (car(e3)==CONST) return pexpr(cadr(e3)?caddr(e1):cadddr(e1));
|
|
865 return list4(car(e1),e3,pexpr(cadr(e1)),pexpr(cadr(e2)));
|
462
|
866 case STASS:
|
|
867 return psassign(e1);
|
|
868 case ASS: case CASS: case SASS:
|
|
869 return passign(e1);
|
|
870 case SASSOP: case SUASSOP:
|
|
871 case ASSOP: case CASSOP: case CUASSOP:
|
|
872 return passop(e1);
|
|
873 #if FLOAT_CODE
|
|
874 case FASS: case DASS:
|
|
875 return pdassign(e1);
|
|
876 case DASSOP: case FASSOP:
|
|
877 return pdassop(e1);
|
|
878 #endif
|
|
879 #if LONGLONG_CODE
|
|
880 case LASS:
|
|
881 return plassign(e1);
|
|
882 case LASSOP: case LUASSOP:
|
|
883 return plassop(e1);
|
|
884 #endif
|
|
885 case ALLOCA:
|
501
|
886 return palloc(e2);
|
462
|
887 case BUILTINP:
|
463
|
888 return list2(CONST,is_const(pexpr(e2)));
|
462
|
889 case COMMA:
|
463
|
890 return pcomma(pexpr(e2),pexpr(caddr(e1)));
|
462
|
891 case RETURN:
|
|
892 case ENVIRONMENT:
|
|
893 case LCALL:
|
|
894 return e1;
|
502
|
895 // relational operator
|
|
896 case GT: case UGT: case GE: case UGE: case LT:
|
|
897 case ULT: case LE: case ULE:
|
|
898 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE:
|
|
899 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE:
|
|
900 case FOP+EQ: case FOP+NEQ:
|
|
901 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ:
|
|
902 return prexpr(car(e1),cadr(e1),caddr(e1));
|
462
|
903 #if BIT_FIELD_CODE
|
|
904 case RBIT_FIELD:
|
|
905 return prbit_field(e1);
|
|
906 case BASS:
|
|
907 return pbassign(e1);
|
|
908 case BPREINC:
|
|
909 case BPOSTINC:
|
|
910 case BASSOP:
|
|
911 return pbassop(e1);
|
|
912 #endif
|
|
913 #if ASM_CODE
|
|
914 case ASM:
|
|
915 return list3(ASM,list4(
|
501
|
916 car(e2),cadr(e2),caddr(e2),cadddr(e2)),
|
|
917 caddr(e1));
|
462
|
918 #endif
|
|
919 case ST_DECL: return p_decl(e1);
|
|
920 case ST_IF: return p_if(e1);
|
|
921 case ST_DO: return p_do(e1);
|
|
922 case ST_WHILE: return p_while(e1);
|
|
923 case ST_FOR: return p_for(e1);
|
|
924 case ST_SWITCH: return p_switch(e1);
|
|
925 case ST_COMP: return p_comp(e1);
|
|
926 case ST_BREAK: return p_break(e1);
|
|
927 case ST_CONTINUE: return p_continue(e1);
|
|
928 case ST_CASE: return p_case(e1);
|
|
929 case ST_DEFAULT: return p_default(e1);
|
|
930 case ST_RETURN: return p_return(e1);
|
|
931 case ST_GOTO: return p_goto(e1);
|
|
932 case ST_ASM: return p_asm(e1);
|
|
933 case ST_LABEL: return p_label(e1);
|
|
934 case ST_COMMENT: return p_comment(e1);
|
|
935 default:
|
|
936 return p_bool(e1);
|
|
937 }
|
|
938 return VOID;
|
|
939 }
|
|
940
|
463
|
941 #define round4(i) ((i+(SIZE_OF_INT-1))&~(SIZE_OF_INT-1))
|
|
942
|
|
943 extern int
|
|
944 gen_inline(int e)
|
462
|
945 {
|
463
|
946 int svartable = pvartable;
|
|
947 int sdisp = pdisp;
|
|
948 int narg,arg;
|
|
949 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
950 int e1 = attr_value(n,INLINE);
|
500
|
951 int parse = car(e1); // inline parse tree
|
|
952 int arg_disp = cadr(e1); // size of local variable
|
|
953 int e3,t,e4;
|
463
|
954
|
503
|
955 pvartable = p_vartable(e,arg_disp,caddr(e1));
|
463
|
956 /* inline function arguments */
|
|
957 narg = 0;
|
|
958 for (e3 = e1 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
500
|
959 t=caddr(e3); // type
|
|
960 e4 = car(e3);
|
|
961 if (is_const(e4) /* ||(is_memory(e3)&&is_readonly(e3)) */ ) {
|
|
962 heap[pdisp+narg]=e4;
|
463
|
963 } else {
|
502
|
964 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),cadddr(e3));
|
500
|
965 g_expr_u(assign_expr0(arg,e4,t,t));
|
463
|
966 }
|
500
|
967 narg ++;
|
463
|
968 }
|
500
|
969 caddr(e) = reverse0(caddr(e)); // make it normal
|
463
|
970 e = pexpr(parse);
|
|
971 pdisp = sdisp;
|
464
|
972 pvartable = svartable;
|
462
|
973 return e;
|
|
974 }
|
|
975
|
|
976 /* end */
|