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