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