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;
|
|
454 if (car(lvar=cadr(e))==IVAR)
|
|
455 lvar=p_lvar(cadr(e)); // can be anything....
|
|
456 switch(car(lvar)) {
|
|
457 case LVAR:
|
|
458 return rvalue_t(lvar,cadddr(e));
|
|
459 default:
|
512
|
460 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
|
461 }
|
462
|
462 }
|
|
463
|
|
464 static int
|
527
|
465 pindirect(int e)
|
|
466 {
|
|
467 int lvar;
|
|
468 if (car(lvar=cadr(e))==IVAR)
|
|
469 lvar=p_lvar(cadr(e)); // can be anything....
|
|
470 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
|
471 }
|
|
472
|
|
473 static int
|
462
|
474 paddress(int e)
|
|
475 {
|
501
|
476 return list2(car(e),pexpr(cadr(e)));
|
462
|
477 }
|
|
478
|
|
479 static int
|
464
|
480 p_conv(int e1,int e2)
|
462
|
481 {
|
527
|
482 return list3(CONV,pexpr(e2),e1);
|
462
|
483 }
|
|
484
|
|
485 static int
|
464
|
486 pbinop(int op,int e1,int e2)
|
462
|
487 {
|
501
|
488 return list3(op,pexpr(e1),pexpr(e2));
|
462
|
489 }
|
|
490
|
|
491 static int
|
502
|
492 prexpr(int op,int e1,int e2)
|
|
493 {
|
|
494 return list3(op,pexpr(e1),pexpr(e2));
|
|
495 }
|
|
496
|
|
497 static int
|
526
|
498 plor(int op,int e1,int e2)
|
|
499 {
|
|
500 return list3(op,pexpr(e1),pexpr(e2));
|
|
501 }
|
|
502
|
|
503 static int
|
|
504 pland(int op,int e1,int e2)
|
|
505 {
|
|
506 return list3(op,pexpr(e1),pexpr(e2));
|
|
507 }
|
|
508
|
|
509 static int
|
462
|
510 psassign(int e)
|
|
511 {
|
501
|
512 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
513 }
|
|
514
|
|
515 static int
|
|
516 passign(int e)
|
|
517 {
|
501
|
518 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
519 }
|
|
520
|
|
521 static int
|
|
522 passop(int e)
|
|
523 {
|
501
|
524 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
525 }
|
|
526
|
|
527 static int
|
|
528 pdassign(int e)
|
|
529 {
|
501
|
530 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
531 }
|
|
532
|
|
533 static int
|
|
534 pdassop(int e)
|
|
535 {
|
501
|
536 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
537 }
|
|
538
|
|
539 static int
|
|
540 plassign(int e)
|
|
541 {
|
501
|
542 return list3(car(e),pexpr(cadr(e)),pexpr(caddr(e)));
|
462
|
543 }
|
|
544
|
|
545 static int
|
|
546 plassop(int e)
|
|
547 {
|
501
|
548 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
549 }
|
|
550
|
|
551 static int
|
|
552 palloc(int e)
|
|
553 {
|
501
|
554 return list2(car(e),pexpr(cadr(e)));
|
462
|
555 }
|
|
556
|
|
557 static int
|
464
|
558 pcomma(int e1,int e2)
|
462
|
559 {
|
501
|
560 return list3(COMMA,pexpr(e1),pexpr(e2));
|
462
|
561 }
|
|
562
|
|
563 static int
|
|
564 prbit_field(int e)
|
|
565 {
|
501
|
566 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
567 }
|
|
568
|
|
569 static int
|
|
570 pbassign(int e)
|
|
571 {
|
501
|
572 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
573 }
|
|
574
|
|
575 static int
|
|
576 pbassop(int e)
|
|
577 {
|
501
|
578 return list4(car(e),pexpr(cadr(e)),pexpr(caddr(e)),cadddr(e));
|
462
|
579 }
|
|
580
|
|
581 static int
|
|
582 p_decl(int e)
|
|
583 {
|
463
|
584 // list4(ST_DECL,parse,(int)n,stmode);
|
|
585 int stmode=cadddr(e);
|
|
586 NMTBL *n=(NMTBL*)caddr(e);
|
|
587 int dsp = n->dsp;
|
|
588 int v;
|
500
|
589 // in real partial evaluation, we have to check whether this variable
|
|
590 // is used or not.
|
463
|
591 switch(stmode) {
|
|
592 case REGISTER:
|
|
593 switch(n->ty) {
|
|
594 case ULONGLONG: case LONGLONG:
|
|
595 v = get_lregister_var(n); break;
|
|
596 case FLOAT:
|
|
597 v = get_dregister_var(n,0); break;
|
|
598 case DOUBLE:
|
|
599 v = get_dregister_var(n,1); break;
|
|
600 default:
|
|
601 if (scalar(n->ty))
|
524
|
602 v = get_register_var(n);
|
463
|
603 else
|
|
604 error(TYERR);
|
|
605 }
|
|
606 case EXTRN: case EXTRN1: case STATIC:
|
|
607 return pexpr(cadr(e));
|
|
608 default:
|
519
|
609 v = list3(LVAR,new_lvar(size(n->ty)),(int)n);
|
463
|
610 }
|
527
|
611 inline_lvars = glist2(v,inline_lvars);
|
463
|
612 if (heap[pdisp+dsp]) error(-1);
|
|
613 heap[pdisp+dsp]=v;
|
|
614 return pexpr(cadr(e));
|
462
|
615 }
|
|
616
|
|
617 static int
|
500
|
618 p_if(int e1)
|
462
|
619 {
|
500
|
620 int cond,l1,l2;
|
|
621 int e2=caddr(e1),e3;
|
|
622 cond = pexpr(car(e2));
|
|
623 // conv->if_then_();
|
|
624 l1 = pexpr(cadr(e2));
|
|
625 if ((e3=caddr(e2))) { // else
|
|
626 l2 = pexpr(e3);
|
|
627 } else {
|
|
628 l2 = 0;
|
|
629 }
|
|
630 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
631 }
|
|
632
|
|
633 static int
|
|
634 p_do(int e)
|
|
635 {
|
524
|
636 int e2 = pexpr(caddr(e));
|
|
637 int e3 = pexpr(cadddr(e));
|
|
638 return list4(ST_DO,pexpr(cadr(e)),e2,e3);
|
462
|
639 }
|
|
640
|
|
641 static int
|
|
642 p_while(int e)
|
|
643 {
|
524
|
644 int e2 = pexpr(caddr(e));
|
|
645 int e3 = pexpr(cadddr(e));
|
|
646 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3);
|
462
|
647 }
|
|
648
|
|
649 static int
|
|
650 p_for(int e)
|
|
651 {
|
501
|
652 int e1=caddr(e);
|
524
|
653 int p0=pexpr(car(e1));
|
|
654 int p1=pexpr(cadr(e1));
|
|
655 int p2=pexpr(caddr(e1));
|
|
656 int p3=pexpr(cadddr(e1));
|
|
657 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
|
462
|
658 }
|
|
659
|
|
660 static int
|
|
661 p_switch(int e)
|
|
662 {
|
524
|
663 int e2 = pexpr(caddr(e));
|
|
664 int e3 = pexpr(cadddr(e));
|
|
665 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
|
462
|
666 }
|
|
667
|
|
668 static int
|
|
669 p_comp(int e)
|
|
670 {
|
524
|
671 int e1=pexpr(caddr(e));
|
|
672 return list3(ST_COMP,pexpr(cadr(e)),e1);
|
462
|
673 }
|
|
674
|
|
675 static int
|
|
676 p_break(int e)
|
|
677 {
|
502
|
678 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
679 }
|
|
680
|
|
681 static int
|
|
682 p_continue(int e)
|
|
683 {
|
502
|
684 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
685 }
|
|
686
|
|
687 static int
|
|
688 p_case(int e)
|
|
689 {
|
506
|
690 int new=0,clist = caddr(e);
|
|
691 // insert destory clist, we have to copy it now
|
|
692 for(;clist;clist=cadr(clist))
|
|
693 new=glist3(car(clist),new,0);
|
|
694 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new));
|
462
|
695 }
|
|
696
|
|
697 static int
|
|
698 p_default(int e)
|
|
699 {
|
502
|
700 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
701 }
|
|
702
|
|
703 static int
|
|
704 p_return(int e)
|
|
705 {
|
524
|
706 int e1=pexpr(caddr(e));
|
|
707 return list3(ST_RETURN,pexpr(cadr(e)),e1);
|
462
|
708 }
|
|
709
|
|
710 static int
|
|
711 p_goto(int e)
|
|
712 {
|
501
|
713 int e1;
|
|
714 if ((e1=caddr(e))) {
|
|
715 switch(car(e1)) {
|
|
716 case RINDIRECT: e1=pexpr(e1); break;
|
|
717 case CODE: e1=list3(CODE,pexpr(cadr(e1)),pexpr(caddr(e1))); break;
|
|
718 case FLABEL: break;
|
|
719 }
|
|
720 }
|
502
|
721 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
722 }
|
|
723
|
|
724 static int
|
|
725 p_list_expr(int e)
|
|
726 {
|
|
727 int e3,new = 0;
|
|
728 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
729 new= list2( pexpr(car(e3)), new);
|
|
730 }
|
|
731 return reverse0(new);
|
462
|
732 }
|
|
733
|
|
734 static int
|
|
735 p_asm(int e)
|
|
736 {
|
501
|
737 int param=caddr(e);
|
|
738 int e1 = p_list_expr(cadddr(e));
|
|
739 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
740 }
|
|
741
|
|
742 static int
|
|
743 p_label(int e)
|
|
744 {
|
503
|
745 return list3(ST_LABEL,pexpr(cadr(e)),caddr(e));
|
462
|
746 }
|
|
747
|
|
748 static int
|
|
749 p_bool(int e)
|
|
750 {
|
501
|
751 error(-1);
|
464
|
752 return e;
|
|
753 }
|
462
|
754
|
464
|
755 static int
|
|
756 p_comment(int e)
|
|
757 {
|
501
|
758 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
759 }
|
|
760
|
508
|
761 static int
|
512
|
762 p_inline(int e)
|
508
|
763 {
|
|
764 int e3;
|
|
765 int narg;
|
|
766
|
|
767 /* inline function arguments */
|
|
768 narg = 0;
|
|
769 for (e3 = caddr(e); e3; e3 = cadr(e3)) {
|
|
770 narg=list3(pexpr(car(e3)),narg,caddr(e3));
|
|
771 }
|
|
772 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e));
|
|
773 }
|
|
774
|
463
|
775 extern int
|
|
776 pexpr(int e1)
|
462
|
777 {
|
|
778 int e2,e3;
|
|
779
|
463
|
780 // if (inmode) error(-1);
|
502
|
781 if (e1==0) return 0;
|
462
|
782 e2 = cadr(e1);
|
|
783 switch (car(e1)){
|
|
784 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
785 case SURGVAR: case REGISTER:
|
|
786 #if FLOAT_CODE
|
|
787 case DREGISTER: case FREGISTER:
|
500
|
788 case FRGVAR: case DRGVAR:
|
462
|
789 #endif
|
|
790 #if LONGLONG_CODE
|
|
791 case LREGISTER:
|
500
|
792 case LRGVAR: case LURGVAR:
|
462
|
793 #endif
|
|
794 case LABEL: case CONST:
|
|
795 #if FLOAT_CODE
|
|
796 case DCONST: case FCONST:
|
|
797 #endif
|
|
798 #if LONGLONG_CODE
|
|
799 case LCONST:
|
|
800 #endif
|
|
801 case STRING:
|
|
802 case FNAME:
|
503
|
803 case FLABEL:
|
462
|
804 case RSTRUCT:
|
|
805 return e1;
|
|
806 case LVAR:
|
|
807 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
|
808 #if FLOAT_CODE
|
500
|
809 case FRLVAR: case DRLVAR:
|
462
|
810 #endif
|
|
811 #if LONGLONG_CODE
|
500
|
812 case LRLVAR: case LURLVAR:
|
|
813 #endif
|
|
814 return e1;
|
|
815 case IVAR:
|
462
|
816 return p_lvar(e1);
|
|
817 case FUNCTION:
|
|
818 return pfunction(e1);
|
|
819 case CODE:
|
463
|
820 return list2(car(e1),pexpr(e2));
|
462
|
821 case INLINE:
|
512
|
822 return p_inline(e1);
|
462
|
823 case INDIRECT:
|
527
|
824 return pindirect(e1);
|
462
|
825 case RINDIRECT: case URINDIRECT:
|
|
826 case CRINDIRECT: case CURINDIRECT:
|
|
827 case SRINDIRECT: case SURINDIRECT:
|
|
828 #if FLOAT_CODE
|
|
829 case FRINDIRECT: case DRINDIRECT:
|
|
830 #endif
|
|
831 #if LONGLONG_CODE
|
|
832 case LRINDIRECT: case LURINDIRECT:
|
|
833 #endif
|
|
834 return prindirect(e1);
|
|
835 case ADDRESS:
|
502
|
836 return paddress(e1);
|
462
|
837 case MINUS:
|
463
|
838 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
839 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
840 return list2(car(e1),e3);
|
|
841 #if LONGLONG_CODE
|
|
842 case LMINUS:
|
463
|
843 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
844 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
845 return list2(car(e1),e3);
|
|
846 #endif
|
|
847 #if FLOAT_CODE
|
|
848 case DMINUS:
|
463
|
849 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
850 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
851 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
852 return list2(car(e1),e3);
|
|
853 case FMINUS:
|
463
|
854 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
855 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
856 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
857 return list2(car(e1),e3);
|
|
858 #endif
|
|
859 case CONV:
|
501
|
860 return p_conv(caddr(e1),e2);
|
462
|
861 case BNOT: /* ~ */
|
463
|
862 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
863 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
864 return list2(BNOT,e3);
|
|
865 case LNOT: /* ! */
|
463
|
866 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
867 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
868 return list2(LNOT,e3);
|
|
869 case PREINC:
|
|
870 case UPREINC:
|
463
|
871 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
872 if (car(e3)==CONST) return list2(CONST,cadr(e3)+1);
|
503
|
873 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
874 case POSTINC:
|
|
875 case UPOSTINC:
|
463
|
876 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
877 if (car(e3)==CONST) return e3;
|
503
|
878 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
879 #if FLOAT_CODE
|
|
880 case DPREINC: /* ++d */
|
463
|
881 if ((e3 = pexpr(e2))==e2) return e1;
|
|
882 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
883 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
884 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
885 case DPOSTINC: /* d++ */
|
463
|
886 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
887 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
888 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
889 case FPREINC: /* ++f */
|
463
|
890 if ((e3 = pexpr(e2))==e2) return e1;
|
|
891 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
892 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
893 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
894 case FPOSTINC: /* f++ */
|
463
|
895 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
896 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
897 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
898 #endif
|
|
899 #if LONGLONG_CODE
|
|
900 case LPREINC: /* ++d */
|
|
901 case LUPREINC: /* ++d */
|
463
|
902 if ((e3 = pexpr(e2))==e2) return e1;
|
|
903 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
904 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
905 case LPOSTINC: /* d++ */
|
|
906 case LUPOSTINC: /* d++ */
|
463
|
907 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
908 if (car(e3)==LCONST) return e3;
|
503
|
909 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
910 #endif
|
|
911 case MUL: case UMUL:
|
|
912 case DIV: case UDIV:
|
|
913 case MOD: case UMOD:
|
|
914 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
915 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
916 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
917 #if FLOAT_CODE
|
|
918 case DMUL: case DDIV:
|
|
919 case DADD: case DSUB:
|
|
920 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
921 case FMUL: case FDIV:
|
|
922 case FADD: case FSUB:
|
|
923 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
924 #endif
|
|
925 #if LONGLONG_CODE
|
|
926 case LMUL: case LUMUL:
|
|
927 case LDIV: case LUDIV:
|
|
928 case LMOD: case LUMOD:
|
|
929 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
930 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
931 #endif
|
501
|
932 return pbinop(car(e1),e2,caddr(e1));
|
462
|
933 case LCOND: case DCOND: case FCOND: case COND:
|
463
|
934 e3 = pexpr(e2);
|
|
935 if (car(e3)==CONST) return pexpr(cadr(e3)?caddr(e1):cadddr(e1));
|
|
936 return list4(car(e1),e3,pexpr(cadr(e1)),pexpr(cadr(e2)));
|
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
|
|
1021 extern int
|
|
1022 gen_inline(int e)
|
462
|
1023 {
|
514
|
1024 // these saved value should be some struct
|
|
1025 int sretlabel;
|
|
1026 NMTBL *sfnptr;
|
463
|
1027 int svartable = pvartable;
|
513
|
1028 int sretcont = retcont;
|
|
1029 int scslabel = cslabel;
|
463
|
1030 int sdisp = pdisp;
|
514
|
1031 int sret_register = ret_register;
|
|
1032 int sret_reg_mode = ret_reg_mode;
|
527
|
1033 int sinline_lvars = inline_lvars;
|
513
|
1034
|
463
|
1035 int narg,arg;
|
|
1036 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
1037 int e1 = attr_value(n,INLINE);
|
500
|
1038 int parse = car(e1); // inline parse tree
|
519
|
1039 int arg_disp = cadr(e1); // number of arguments
|
511
|
1040 int e2,e3,t,e4,dots;
|
508
|
1041 int ret_type = function_type(cadddr(e),&dots);
|
526
|
1042 int fargtype;
|
|
1043 NMTBL *anptr;
|
463
|
1044
|
514
|
1045 // checkret();
|
|
1046
|
|
1047 sretlabel = retlabel;
|
|
1048 sfnptr = fnptr;
|
513
|
1049 retcont = 0;
|
|
1050 cslabel = -1;
|
|
1051 retpending = 0;
|
514
|
1052 ret_reg_mode = 0;
|
|
1053 ret_register = 5555;
|
527
|
1054 inline_lvars = 0;
|
514
|
1055 retlabel = fwdlabel();
|
513
|
1056
|
511
|
1057 enter_scope(); // to make label scope happy
|
|
1058
|
510
|
1059 fnptr = n; // st_return see this
|
503
|
1060 pvartable = p_vartable(e,arg_disp,caddr(e1));
|
526
|
1061
|
|
1062 /* function arguments type */
|
|
1063 fargtype = n->dsp; // we cannot do destruct reverse here
|
|
1064
|
463
|
1065 /* inline function arguments */
|
|
1066 narg = 0;
|
|
1067 for (e3 = e1 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
526
|
1068 anptr = (NMTBL*)caddr(fargtype);
|
500
|
1069 t=caddr(e3); // type
|
|
1070 e4 = car(e3);
|
524
|
1071 if (0 && is_const(e4) /* ||(is_memory(e3)&&is_readonly(e3)) */ ) {
|
500
|
1072 heap[pdisp+narg]=e4;
|
463
|
1073 } else {
|
509
|
1074 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),0);
|
527
|
1075 inline_lvars = glist2(arg,inline_lvars);
|
509
|
1076 // should contain correct argument variable name
|
526
|
1077 if (anptr)
|
|
1078 printf("## var %s\n",anptr->nm);
|
|
1079 else
|
|
1080 printf("## var 0\n");
|
500
|
1081 g_expr_u(assign_expr0(arg,e4,t,t));
|
463
|
1082 }
|
500
|
1083 narg ++;
|
526
|
1084 fargtype = cadr(fargtype);
|
463
|
1085 }
|
508
|
1086 caddr(e) = reverse0(e1); // make it normal
|
511
|
1087 e2 = pexpr(parse);
|
463
|
1088 pdisp = sdisp;
|
464
|
1089 pvartable = svartable;
|
513
|
1090
|
|
1091 if (ret_type!=VOID)
|
|
1092 g_expr0(e2);
|
|
1093 else
|
|
1094 g_expr_u(e2);
|
524
|
1095 checkret();
|
513
|
1096 fwddef(retlabel);
|
514
|
1097 control=1;
|
513
|
1098
|
|
1099 if (retcont) error(STERR); // inline can't handle return/environment
|
510
|
1100
|
511
|
1101 leave_scope();
|
527
|
1102 while(inline_lvars) {
|
|
1103 int e;
|
|
1104 int l = car(inline_lvars);
|
|
1105 switch(car(l)) {
|
|
1106 case LVAR: free_lvar(cadr(l)); break;
|
|
1107 case REGISTER: case DREGISTER:
|
|
1108 case FREGISTER: case LREGISTER:
|
|
1109 free_register(cadr(l));
|
|
1110 }
|
|
1111 e = cadr(inline_lvars);
|
|
1112 free_glist2(inline_lvars);
|
|
1113 inline_lvars = e;
|
|
1114 }
|
513
|
1115 fnptr = sfnptr;
|
|
1116 retlabel = sretlabel;
|
|
1117 retcont = sretcont;
|
|
1118 cslabel = scslabel;
|
514
|
1119 ret_register = sret_register;
|
|
1120 ret_reg_mode = sret_reg_mode;
|
527
|
1121 inline_lvars = sinline_lvars;
|
513
|
1122
|
508
|
1123 return ret_type;
|
462
|
1124 }
|
|
1125
|
|
1126 /* end */
|