462
|
1 /* Micro-C Partial Evaluator Part */
|
|
2
|
607
|
3 /************************************************************************
|
|
4 ** Copyright (C) 2006 Shinji Kono
|
|
5 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
7 **
|
|
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
11 ** 営利利用も上記に反しない範囲で許可します。
|
|
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
13 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
14 **
|
|
15 ** Everyone is permitted to do anything on this program
|
|
16 ** including copying, modifying, improving,
|
|
17 ** as long as you don't try to pretend that you wrote it.
|
|
18 ** i.e., the above copyright notice has to appear in all copies.
|
|
19 ** Binary distribution requires original version messages.
|
|
20 ** You don't have to ask before copying, redistribution or publishing.
|
|
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
22 ***********************************************************************/
|
625
|
23
|
|
24 /*
|
|
25
|
|
26 Inline handler
|
|
27
|
|
28 inline code is stored as parse tree
|
|
29 expr already has parse tree
|
|
30 statement part is handled here
|
|
31
|
|
32 st_hoge() code generator (called from gexpr())
|
|
33 p_hoge() partial evaluator called in gen_inline()
|
|
34 after p_hoge(), it contains no ST_* node.
|
|
35
|
|
36 PVAR has an offset for pvartable, it can be
|
|
37 constant, local variable or global variable
|
|
38 other complex expression is evaluated before the code expansion
|
|
39
|
|
40 We always perform inline expansion.
|
|
41 Non static inline function can be referenced from other. Real
|
|
42 function body is generated by pfdecl() in mc-parse.c.
|
|
43
|
|
44 */
|
|
45
|
462
|
46 #include <stdio.h>
|
|
47 #include "mc.h"
|
|
48 #include "mc-parse.h"
|
|
49 #include "mc-codegen.h"
|
|
50 #include "mc-switch.h"
|
464
|
51 #include "mc-code.h"
|
|
52 #include "mc-inline.h"
|
462
|
53
|
463
|
54 static int pvartable;
|
|
55 static int pdisp;
|
462
|
56
|
514
|
57 static int ret_register,ret_reg_mode;
|
|
58
|
527
|
59 static int inline_lvars;
|
|
60
|
462
|
61 /*
|
|
62 Basic code generator from parse tree
|
|
63 */
|
|
64
|
|
65 extern void
|
|
66 st_decl(int e1){
|
|
67 // NMTBL *n = (NMTBL *)caddr(e1);
|
|
68 // int stmode = cadddr(e1);
|
|
69 }
|
|
70
|
|
71 extern void
|
|
72 st_if(int e1){
|
|
73 int l1,l2,slfree;
|
|
74 int e2=caddr(e1),e3;
|
|
75 // conv->if_();
|
|
76 slfree=lfree;
|
|
77 checkret();
|
|
78 l1 = bexpr(car(e2),0,fwdlabel());
|
|
79 // conv->if_then_();
|
|
80 g_expr_u(cadr(e2));
|
|
81 checkret();
|
|
82 if ((e3=caddr(e2))) { // else
|
|
83 // conv->if_else_();
|
|
84 if ((l2 = control))
|
|
85 gen_jmp(l2=fwdlabel());
|
|
86 fwddef(l1);
|
|
87 g_expr_u(e3);
|
|
88 checkret();
|
|
89 if (l2) fwddef(l2);
|
|
90 } else {
|
|
91 fwddef(l1);
|
|
92 }
|
|
93 // conv->if_endif_();
|
|
94 }
|
|
95
|
|
96
|
|
97 extern void
|
|
98 st_do(int e1){
|
|
99 int sbreak,scontinue,l;
|
|
100
|
|
101 sbreak=blabel;
|
|
102 scontinue=clabel;
|
|
103 blabel=fwdlabel();
|
|
104 clabel=fwdlabel();
|
|
105 control=1;
|
|
106 checkret();
|
|
107 l=backdef();
|
|
108 // conv->dowhile_();
|
|
109 g_expr_u(cadddr(e1));
|
|
110 checkret();
|
|
111 // conv->dowhile_cond_();
|
|
112 bexpr(caddr(e1),1,l);
|
|
113 // conv->dowhile_end_();
|
|
114 fwddef(blabel);
|
|
115 clabel=scontinue;
|
|
116 blabel=sbreak;
|
|
117 }
|
|
118
|
|
119
|
|
120 extern void
|
|
121 st_while(int e1){
|
|
122 int sbreak,scontinue,e;
|
|
123
|
|
124 sbreak=blabel;
|
|
125 scontinue=clabel;
|
|
126 blabel=fwdlabel();
|
|
127 control=1;
|
|
128 checkret();
|
|
129 clabel=backdef();
|
|
130 // conv->while_();
|
|
131 // conv->while_body_();
|
|
132 if(!(e=cadddr(e1))) {
|
|
133 bexpr(caddr(e1),1,clabel);
|
|
134 // conv->sm_();
|
|
135 } else {
|
|
136 bexpr(caddr(e1),0,blabel);
|
|
137 g_expr_u(e);
|
|
138 checkret();
|
|
139 if(control)
|
|
140 gen_jmp(clabel);
|
|
141 }
|
|
142 // conv->while_end_();
|
|
143 fwddef(blabel);
|
|
144 clabel=scontinue;
|
|
145 blabel=sbreak;
|
|
146 }
|
|
147
|
|
148
|
|
149 extern void
|
|
150 st_for(int e1){
|
|
151 int p0,p1,p2,body;
|
|
152 int l,e;
|
|
153 int sbreak=blabel;
|
|
154 int scontinue=clabel;
|
|
155
|
|
156 e = caddr(e1);
|
|
157 p0 = car(e); p1 = cadr(e); p2 = caddr(e); body = cadddr(e);
|
|
158
|
|
159 blabel=fwdlabel();
|
|
160 // conv->for_();
|
|
161 if (p0) {
|
|
162 checkret();
|
|
163 g_expr_u(p0);
|
|
164 }
|
|
165 // conv->for1_();
|
|
166 control=1;
|
|
167 checkret();
|
|
168 l=backdef();
|
|
169 if (p1) {
|
|
170 bexpr(p1,0,blabel);
|
|
171 }
|
|
172 // conv->for2_();
|
|
173 // conv->for_body_();
|
|
174 if (!p2) {
|
|
175 clabel=l;
|
|
176 g_expr_u(body);
|
|
177 checkret();
|
|
178 } else {
|
|
179 clabel=fwdlabel();
|
|
180 g_expr_u(body);
|
|
181 checkret();
|
|
182 fwddef(clabel);
|
|
183 g_expr_u(p2);
|
|
184 }
|
|
185 // conv->for_end_();
|
|
186 gen_jmp(l);
|
|
187 fwddef(blabel);
|
|
188 clabel=scontinue;
|
|
189 blabel=sbreak;
|
|
190 }
|
|
191
|
|
192
|
|
193 extern void
|
|
194 st_switch(int e1){
|
|
195 int sbreak,scase,sdefault,slfree,svalue,slist;
|
558
|
196 int cst,e;
|
462
|
197
|
|
198 checkret();
|
|
199 slist = cslist;
|
|
200 cslist = 0;
|
|
201 sbreak=blabel; /* save parents break label */
|
|
202 blabel=fwdlabel();
|
|
203 sdefault=dlabel; /* save parents default label */
|
|
204 dlabel=0;
|
|
205 scase=cslabel; /* save parents next case label */
|
|
206 // conv->switch_();
|
|
207 slfree=lfree;
|
|
208 svalue=csvalue1; /* save parents switch value */
|
558
|
209 e =caddr(e1); /* switch value */
|
|
210 if (car(e)==CONST) {
|
|
211 cst = 1;
|
|
212 csvalue1=glist2(CONST,cadr(e));
|
|
213 gen_jmp( cslabel=fwdlabel());
|
|
214 } else {
|
|
215 cst = 0;
|
|
216 gexpr(e,1); /* switch value */
|
|
217 csvalue1=csvalue() ;
|
|
218 cslabel = control = 0;
|
|
219 }
|
462
|
220 // conv->switch_body_();
|
|
221 g_expr_u(cadddr(e1));
|
|
222 // conv->switch_end_();
|
|
223 checkret();
|
|
224 #if CASE_CODE
|
558
|
225 if (!cst) {
|
|
226 if (control) gen_jmp(blabel);
|
|
227 genswitch(cslist,cslabel);
|
|
228 } else if (!cslist) {
|
|
229 if(dlabel) def_label(cslabel,dlabel);
|
|
230 else fwddef(cslabel);
|
|
231 }
|
462
|
232 #else
|
558
|
233 if (!(cst && cslist)) {
|
|
234 if(dlabel) def_label(cslabel,dlabel);
|
|
235 else fwddef(cslabel);
|
|
236 }
|
462
|
237 #endif
|
558
|
238 free_glist2(csvalue1);
|
|
239 if (cst && !cslist) {
|
|
240 if(pending_jmp!=cslabel)
|
|
241 fwddef(cslabel);
|
|
242 else pending_jmp = 0; // cslabel is here
|
|
243 }
|
|
244 fwddef(blabel);
|
462
|
245 csvalue1=svalue;
|
|
246 cslabel=scase;
|
|
247 dlabel=sdefault;
|
|
248 blabel=sbreak;
|
|
249 cslist = slist;
|
|
250 }
|
|
251
|
|
252
|
|
253 extern void
|
|
254 st_comp(int e1){
|
|
255 g_expr_u(caddr(e1));
|
|
256 }
|
|
257
|
|
258
|
|
259 extern void
|
|
260 st_break(int e1){
|
|
261 checkret();
|
|
262 // conv->break_();
|
|
263 if (control)
|
|
264 gen_jmp(blabel);
|
|
265 }
|
|
266
|
|
267
|
|
268 extern void
|
|
269 st_continue(int e1){
|
|
270 checkret();
|
|
271 // conv->continue_();
|
|
272 if (control) gen_jmp(clabel);
|
|
273 }
|
|
274
|
|
275
|
|
276 extern void
|
|
277 st_case(int e1){
|
|
278 #if CASE_CODE
|
569
|
279 int l=0,clist=caddr(e1),c;
|
558
|
280 int cst = (car(csvalue1)==CONST);
|
|
281 if (cst) {
|
|
282 c=cadr(csvalue1);
|
|
283 for(;clist;clist=cadr(clist)) {
|
|
284 if (car(clist)==c) break;
|
|
285 }
|
|
286 if (!clist) return; // no match
|
|
287 } else
|
|
288 l = fwdlabel();
|
462
|
289 if (retpending) {
|
|
290 ret(); retpending=0;
|
|
291 }
|
558
|
292 if (cst) {
|
|
293 if (cslist) { // may duplicat csvalue
|
|
294 return;
|
|
295 }
|
|
296 cslist=1;
|
|
297 fwddef(cslabel);
|
|
298 return;
|
|
299 }
|
462
|
300 if (!cslabel) {
|
|
301 if (!control) {
|
|
302 cmpdimm(car(clist),csvalue1,cslabel=fwdlabel(),1);
|
|
303 caddr(clist)=0;
|
|
304 } else {
|
|
305 error(-1);
|
|
306 }
|
|
307 }
|
|
308 while(clist) {
|
|
309 caddr(clist) = l;
|
|
310 clist = cadr(c=clist); cadr(c) = 0; // insert destroy cadr of clist
|
|
311 cslist=insert_ascend(cslist,c,docase_eq);
|
|
312 }
|
|
313 fwddef(l);
|
|
314 control=1;
|
|
315 #else
|
558
|
316 int c,clist,l;
|
|
317 int cst = (car(csvalue1)==CONST);
|
|
318 clist = caddr(e1);
|
|
319 if (cst) {
|
|
320 c=cadr(csvalue1);
|
|
321 for(;clist;clist=cadr(clist)) {
|
|
322 if (car(clist)==c) break; // no match
|
|
323 }
|
|
324 if (!clist) return;
|
|
325 }
|
|
326 if (cst) {
|
|
327 if (!cslist) {
|
|
328 if (retpending) {
|
|
329 ret(); retpending=0;
|
|
330 }
|
|
331 fwddef(cslabel);
|
|
332 cslist = 1;
|
|
333 } // else error(CSERR);
|
|
334 return;
|
|
335 }
|
462
|
336 if (retpending) {
|
|
337 ret(); retpending=0;
|
|
338 }
|
|
339 l=fwdlabel();
|
|
340 if (control) {
|
|
341 control=0;
|
|
342 gen_jmp(l);
|
|
343 }
|
|
344 if (cslabel) fwddef(cslabel);
|
558
|
345 while(cadr(clist)) {
|
|
346 cmpdimm(car(clist),csvalue1,l,0);
|
|
347 clist=cadr(clist);
|
462
|
348 }
|
558
|
349 cmpdimm(car(clist),csvalue1,cslabel=fwdlabel(),1);
|
462
|
350 if (l) fwddef(l);
|
|
351 #endif
|
|
352 }
|
|
353
|
|
354
|
|
355 extern void
|
|
356 st_default(int e1){
|
558
|
357 // int cst = (car(csvalue1)==CONST);
|
462
|
358 control=1;
|
|
359 checkret();
|
|
360 if (dlabel) error(STERR); // double default:
|
|
361 dlabel = backdef();
|
|
362 // conv->case_(0,1);
|
|
363 }
|
|
364
|
|
365
|
|
366 extern void
|
|
367 st_return(int e1){
|
514
|
368 int e,t;
|
462
|
369
|
|
370 if (!cslabel) gen_jmp(cslabel = fwdlabel());
|
|
371 if(!(e=caddr(e1))) {
|
513
|
372 // no return value
|
462
|
373 retpending = 1;
|
|
374 return;
|
|
375 }
|
551
|
376 t = type_value(cadr(fnptr->ty));
|
|
377 if (t>0 && (car(t)==STRUCT || car(t)==UNION)) {
|
|
378 // copy is included in e, pass the pointer
|
|
379 t = list2(POINTER,type_value(cadr(fnptr->ty)));
|
|
380 }
|
|
381 g_expr(e);
|
|
382 if (ret_reg_mode==0) {
|
|
383 // return value register is not fixed
|
|
384 ret_reg_mode=1;
|
|
385 ret_register = code_get_fixed_creg(USE_CREG,t);
|
462
|
386 } else {
|
551
|
387 code_set_fixed_creg(ret_register,1,t);
|
462
|
388 }
|
|
389 // conv->return_end_();
|
|
390 retpending = 1;
|
|
391 }
|
|
392
|
|
393 extern void
|
|
394 st_goto(int e){
|
551
|
395 NMTBL *nptr0;
|
|
396 int e1,e2,env;
|
462
|
397
|
|
398 checkret();
|
|
399 e1 = caddr(e);
|
|
400 if (car(e1)==RINDIRECT) {
|
|
401 gen_indirect_goto(cadr(e1));
|
505
|
402 return ;
|
552
|
403 } else if (car(e1)==RLVAR) {
|
551
|
404 gen_indirect_goto(e1);
|
|
405 return ;
|
552
|
406 } else if (car(e1)==LVAR||car(e1)==FLABEL) {
|
551
|
407 gen_jmp(cadr(e1));
|
462
|
408 control=0;
|
505
|
409 return ;
|
551
|
410 } else if (car(e1)==CODE) {
|
462
|
411 /* CbC continuation */
|
|
412 // conv->jump_(env);
|
551
|
413 // should be separate function
|
462
|
414 e2 = cadr(e1);
|
|
415 env = caddr(e1);
|
|
416 if (car(e2) == FNAME) {
|
|
417 nptr0=(NMTBL *)cadr(e2);
|
|
418 if (nptr0->sc==EMPTY)
|
|
419 nptr0->sc = EXTRN1;
|
|
420 else if(nptr0->sc==FUNCTION)
|
|
421 nptr0->sc = CODE;
|
|
422 if (nptr0->ty>0&&car(nptr0->ty)==FUNCTION)
|
|
423 car(nptr0->ty)=CODE;
|
|
424 }
|
609
|
425 gexpr(list3(JUMP,e1,env),0);
|
462
|
426 control=0;
|
|
427 // conv->sm_();
|
505
|
428 return ;
|
551
|
429 } else {
|
|
430 error(-1);
|
462
|
431 }
|
|
432 }
|
|
433
|
|
434
|
|
435 #if ASM_CODE
|
|
436 extern void
|
|
437 st_asm(int e1){
|
|
438 checkret();
|
|
439 g_expr_u(list3(ASM,caddr(e1),cadddr(e1)));
|
|
440 }
|
|
441 #endif
|
|
442
|
|
443
|
|
444 extern void
|
|
445 st_label(int e1){
|
551
|
446 int lb = caddr(e1);
|
462
|
447 control=1;
|
|
448 checkret();
|
551
|
449 if (car(lb)==LVAR) { // label variable case
|
|
450 } else if (car(lb)!=FLABEL) error(-1);
|
|
451 fwddef(cadr(lb));
|
462
|
452 }
|
|
453
|
|
454 extern void
|
|
455 st_comment(int e1){
|
574
|
456 glineno++;
|
585
|
457 printf("## %d ",glineno);
|
462
|
458 gen_comment((char *)caddr(e1));
|
|
459 }
|
|
460
|
|
461 /*
|
|
462 partial evaluator
|
|
463 */
|
|
464
|
|
465 static int
|
557
|
466 p_vartable(int adisp,int ldisp)
|
462
|
467 {
|
464
|
468 int i;
|
505
|
469 int pvartable = getfree(adisp-ldisp); // have to be local heap
|
509
|
470 pdisp = pvartable-ldisp;
|
|
471 for(i=ldisp;i<0;i++) {
|
|
472 heap[pdisp+i] = 0;
|
463
|
473 }
|
505
|
474 return pvartable;
|
462
|
475 }
|
|
476
|
|
477 static int
|
|
478 p_lvar(int e1)
|
|
479 {
|
463
|
480 int sz = is_memory(e1);
|
|
481 int d = cadr(e1);
|
527
|
482 int d1,e;
|
502
|
483 if ((d1=(heap[pdisp+d]))) return d1;
|
574
|
484 fprintf(stderr,"## undeclared ivar %d\n",d);
|
|
485 // error(-1);
|
527
|
486 e = heap[pdisp+d]=list3(LVAR,new_lvar(sz),0);
|
|
487 inline_lvars = glist2(e,inline_lvars);
|
|
488 return e;
|
462
|
489 }
|
|
490
|
|
491 static int
|
|
492 pfunction(int e)
|
|
493 {
|
501
|
494 // list4(INLINE,e1,arglist,ftype);
|
|
495 // include code segement case
|
|
496 int e1 = pexpr(cadr(e));
|
|
497 int arglist = caddr(e);
|
|
498 int newargs = 0;
|
|
499 int ftype = cadddr(e);
|
|
500 int e3;
|
|
501 for (e3 = arglist; e3; e3 = cadr(e3)) {
|
514
|
502 newargs = list3( pexpr(car(e3)), newargs, caddr(e3));
|
501
|
503 }
|
|
504 newargs = reverse0(newargs);
|
513
|
505 return list4(car(e),e1,newargs,ftype);
|
462
|
506 }
|
|
507
|
|
508 static int
|
|
509 prindirect(int e)
|
|
510 {
|
525
|
511 int lvar;
|
533
|
512 int offset = caddr(e);
|
530
|
513 if (car(lvar=cadr(e))==IVAR) {
|
525
|
514 lvar=p_lvar(cadr(e)); // can be anything....
|
530
|
515 switch(car(lvar)) {
|
|
516 case LVAR:
|
533
|
517 if(offset) {
|
|
518 return list3(car(e),lvar,offset);
|
|
519 }
|
530
|
520 return rvalue_t(lvar,cadddr(e));
|
|
521 case REGISTER: case DREGISTER:
|
|
522 case FREGISTER: case LREGISTER:
|
|
523 case CONST: case FCONST: case DCONST: case LCONST:
|
|
524 // should do type check
|
533
|
525 if (offset) error(-1);
|
530
|
526 return lvar;
|
|
527 }
|
512
|
528 }
|
533
|
529 return list3(car(e),pexpr(cadr(e)),offset);
|
462
|
530 }
|
|
531
|
|
532 static int
|
527
|
533 pindirect(int e)
|
|
534 {
|
553
|
535 //int lvar;
|
|
536 //if (car(lvar=cadr(e))==IVAR)
|
|
537 // lvar=p_lvar(cadr(e)); // can be anything....
|
527
|
538 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
|
539 }
|
|
540
|
|
541 static int
|
462
|
542 paddress(int e)
|
|
543 {
|
601
|
544 // if (car(cadr(e))==INDIRECT) return pexpr(cadr(cadr(e)));
|
501
|
545 return list2(car(e),pexpr(cadr(e)));
|
462
|
546 }
|
|
547
|
|
548 static int
|
464
|
549 p_conv(int e1,int e2)
|
462
|
550 {
|
557
|
551 int t;
|
|
552 if (is_const(e2) && (t=type_of_conv(e1))) {
|
|
553 return correct_type(e2,t);
|
|
554 }
|
527
|
555 return list3(CONV,pexpr(e2),e1);
|
462
|
556 }
|
|
557
|
|
558 static int
|
464
|
559 pbinop(int op,int e1,int e2)
|
462
|
560 {
|
557
|
561 e1 = pexpr(e1);
|
|
562 e2 = pexpr(e2);
|
|
563 if (is_const(e1)&&is_const(e2)) {
|
|
564 int t;
|
|
565 if((t= type_of_bop(op)))
|
599
|
566 return binop(OP(op),e1,e2,t,t);
|
557
|
567 }
|
|
568 return list3(op,e1,e2);
|
502
|
569 }
|
|
570
|
|
571 static int
|
526
|
572 plor(int op,int e1,int e2)
|
|
573 {
|
555
|
574 int e = pexpr(e1);
|
|
575 return list3(op,e,pexpr(e2));
|
526
|
576 }
|
|
577
|
|
578 static int
|
|
579 pland(int op,int e1,int e2)
|
|
580 {
|
555
|
581 int e = pexpr(e1);
|
|
582 return list3(op,e,pexpr(e2));
|
526
|
583 }
|
|
584
|
|
585 static int
|
462
|
586 psassign(int e)
|
|
587 {
|
555
|
588 int e1 = pexpr(cadr(e));
|
|
589 int e2 = pexpr(caddr(e));
|
|
590 return list4(car(e),e1,e2,cadddr(e));
|
462
|
591 }
|
|
592
|
|
593 static int
|
|
594 passign(int e)
|
|
595 {
|
555
|
596 int e1 = pexpr(cadr(e));
|
|
597 int e2 = pexpr(caddr(e));
|
|
598 return list3(car(e),e1,e2);
|
462
|
599 }
|
|
600
|
|
601 static int
|
|
602 passop(int e)
|
|
603 {
|
555
|
604 int e1 = pexpr(cadr(e));
|
|
605 int e2 = pexpr(caddr(e));
|
|
606 return list4(car(e),e1,e2,cadddr(e));
|
462
|
607 }
|
|
608
|
|
609 static int
|
|
610 pdassign(int e)
|
|
611 {
|
555
|
612 int e1 = pexpr(cadr(e));
|
|
613 int e2 = pexpr(caddr(e));
|
|
614 return list3(car(e),e1,e2);
|
462
|
615 }
|
|
616
|
|
617 static int
|
|
618 pdassop(int e)
|
|
619 {
|
555
|
620 int e1 = pexpr(cadr(e));
|
|
621 int e2 = pexpr(caddr(e));
|
|
622 return list4(car(e),e1,e2,cadddr(e));
|
462
|
623 }
|
|
624
|
|
625 static int
|
|
626 plassign(int e)
|
|
627 {
|
555
|
628 int e1 = pexpr(cadr(e));
|
|
629 int e2 = pexpr(caddr(e));
|
|
630 return list3(car(e),e1,e2);
|
462
|
631 }
|
|
632
|
|
633 static int
|
|
634 plassop(int e)
|
|
635 {
|
555
|
636 int e1 = pexpr(cadr(e));
|
|
637 int e2 = pexpr(caddr(e));
|
|
638 return list4(car(e),e1,e2,cadddr(e));
|
462
|
639 }
|
|
640
|
|
641 static int
|
|
642 palloc(int e)
|
|
643 {
|
501
|
644 return list2(car(e),pexpr(cadr(e)));
|
462
|
645 }
|
|
646
|
|
647 static int
|
464
|
648 pcomma(int e1,int e2)
|
462
|
649 {
|
556
|
650 int e = pexpr(e1);
|
555
|
651 return list3(COMMA,e,pexpr(e2));
|
462
|
652 }
|
|
653
|
|
654 static int
|
|
655 prbit_field(int e)
|
|
656 {
|
501
|
657 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
658 }
|
|
659
|
|
660 static int
|
|
661 pbassign(int e)
|
|
662 {
|
553
|
663 // list4(BASS,e1,e2,list2(BASS,t)));
|
|
664 int e1=pexpr(caddr(e));
|
|
665 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
666 }
|
|
667
|
|
668 static int
|
|
669 pbassop(int e)
|
|
670 {
|
553
|
671 int e1=pexpr(caddr(e));
|
|
672 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
673 }
|
|
674
|
561
|
675 // handle local variable declaration
|
|
676 // initialization is accumrated in parse tree
|
|
677 // should consider int k=some_compile_time_constant;
|
462
|
678 static int
|
|
679 p_decl(int e)
|
|
680 {
|
552
|
681 // list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode));
|
554
|
682 int ctmode=cadddr(e);
|
463
|
683 NMTBL *n=(NMTBL*)caddr(e);
|
|
684 int dsp = n->dsp;
|
569
|
685 int v=0;
|
552
|
686 int sstmode = stmode;
|
|
687 int smode = mode;
|
500
|
688 // in real partial evaluation, we have to check whether this variable
|
|
689 // is used or not.
|
552
|
690 if (ctmode) {
|
|
691 mode = car(ctmode); stmode = cadr(ctmode); ctmode = caddr(ctmode);
|
574
|
692 } else {
|
|
693 mode = LDECL; stmode = 0; ctmode = 0;
|
552
|
694 }
|
463
|
695 switch(stmode) {
|
528
|
696 case EXTRN: case EXTRN1: case STATIC:
|
552
|
697 // def(n,ctmode); we don't need this. already done.
|
|
698 // stmode = sstmode;
|
|
699 stmode = sstmode;
|
|
700 mode = smode;
|
|
701 return pexpr(cadr(e));
|
|
702 // case LLDECL: LLDECL is mode, not stmode (bad design)
|
|
703 // v = list2(FLABEL,fwdlabel()); break;
|
529
|
704 #if 1
|
463
|
705 case REGISTER:
|
|
706 switch(n->ty) {
|
|
707 case ULONGLONG: case LONGLONG:
|
|
708 v = get_lregister_var(n); break;
|
|
709 case FLOAT:
|
|
710 v = get_dregister_var(n,0); break;
|
|
711 case DOUBLE:
|
|
712 v = get_dregister_var(n,1); break;
|
|
713 default:
|
|
714 if (scalar(n->ty))
|
524
|
715 v = get_register_var(n);
|
463
|
716 else
|
|
717 error(TYERR);
|
|
718 }
|
529
|
719 break;
|
528
|
720 #endif
|
463
|
721 default:
|
552
|
722 if (n->sc==FLABEL)
|
|
723 v = list3(LVAR,fwdlabel(),(int)n);
|
|
724 else
|
|
725 v = list3(LVAR,new_lvar(size(n->ty)),(int)n);
|
463
|
726 }
|
552
|
727 if (n->sc!=FLABEL)
|
|
728 inline_lvars = glist2(v,inline_lvars);
|
574
|
729 if (heap[pdisp+dsp]) {
|
|
730 fprintf(stderr,"## double p_decl %s %s\n",((NMTBL*)(caddr(heap[pdisp+dsp])))->nm,n->nm);
|
|
731 error(-1);
|
|
732 }
|
463
|
733 heap[pdisp+dsp]=v;
|
552
|
734 stmode = sstmode;
|
|
735 mode = smode;
|
463
|
736 return pexpr(cadr(e));
|
462
|
737 }
|
|
738
|
|
739 static int
|
500
|
740 p_if(int e1)
|
462
|
741 {
|
500
|
742 int cond,l1,l2;
|
|
743 int e2=caddr(e1),e3;
|
|
744 cond = pexpr(car(e2));
|
|
745 // conv->if_then_();
|
|
746 l1 = pexpr(cadr(e2));
|
|
747 if ((e3=caddr(e2))) { // else
|
|
748 l2 = pexpr(e3);
|
|
749 } else {
|
|
750 l2 = 0;
|
|
751 }
|
|
752 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
753 }
|
|
754
|
|
755 static int
|
|
756 p_do(int e)
|
|
757 {
|
524
|
758 int e2 = pexpr(caddr(e));
|
|
759 int e3 = pexpr(cadddr(e));
|
|
760 return list4(ST_DO,pexpr(cadr(e)),e2,e3);
|
462
|
761 }
|
|
762
|
|
763 static int
|
|
764 p_while(int e)
|
|
765 {
|
524
|
766 int e2 = pexpr(caddr(e));
|
|
767 int e3 = pexpr(cadddr(e));
|
|
768 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3);
|
462
|
769 }
|
|
770
|
|
771 static int
|
|
772 p_for(int e)
|
|
773 {
|
501
|
774 int e1=caddr(e);
|
524
|
775 int p0=pexpr(car(e1));
|
|
776 int p1=pexpr(cadr(e1));
|
|
777 int p2=pexpr(caddr(e1));
|
|
778 int p3=pexpr(cadddr(e1));
|
625
|
779 // unfolding for constant case?
|
524
|
780 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
|
462
|
781 }
|
|
782
|
|
783 static int
|
|
784 p_switch(int e)
|
|
785 {
|
524
|
786 int e2 = pexpr(caddr(e));
|
|
787 int e3 = pexpr(cadddr(e));
|
625
|
788 // if cadr(e) is a constant, we have to prune case statement.
|
|
789 // here?
|
524
|
790 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
|
462
|
791 }
|
|
792
|
|
793 static int
|
|
794 p_comp(int e)
|
|
795 {
|
524
|
796 int e1=pexpr(caddr(e));
|
|
797 return list3(ST_COMP,pexpr(cadr(e)),e1);
|
462
|
798 }
|
|
799
|
|
800 static int
|
|
801 p_break(int e)
|
|
802 {
|
502
|
803 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
804 }
|
|
805
|
|
806 static int
|
|
807 p_continue(int e)
|
|
808 {
|
502
|
809 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
810 }
|
|
811
|
|
812 static int
|
|
813 p_case(int e)
|
|
814 {
|
506
|
815 int new=0,clist = caddr(e);
|
|
816 // insert destory clist, we have to copy it now
|
|
817 for(;clist;clist=cadr(clist))
|
|
818 new=glist3(car(clist),new,0);
|
|
819 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new));
|
462
|
820 }
|
|
821
|
|
822 static int
|
|
823 p_default(int e)
|
|
824 {
|
625
|
825 // should be removed if case value is a constant
|
502
|
826 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
827 }
|
|
828
|
|
829 static int
|
|
830 p_return(int e)
|
|
831 {
|
524
|
832 int e1=pexpr(caddr(e));
|
|
833 return list3(ST_RETURN,pexpr(cadr(e)),e1);
|
462
|
834 }
|
|
835
|
|
836 static int
|
|
837 p_goto(int e)
|
|
838 {
|
555
|
839 int e1,lb,e2;
|
501
|
840 if ((e1=caddr(e))) {
|
|
841 switch(car(e1)) {
|
|
842 case RINDIRECT: e1=pexpr(e1); break;
|
555
|
843 case CODE: e2=pexpr(cadr(e1));
|
609
|
844 e1=list3(JUMP,e2,pexpr(caddr(e1))); break;
|
551
|
845 case FLABEL: /* error(-1); */ break;
|
|
846 case IVAR:
|
|
847 lb = cadr(e1);
|
|
848 if (!(e1=heap[pdisp+lb])) {
|
|
849 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
850 }
|
501
|
851 }
|
|
852 }
|
502
|
853 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
854 }
|
|
855
|
|
856 static int
|
|
857 p_list_expr(int e)
|
|
858 {
|
|
859 int e3,new = 0;
|
|
860 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
861 new= list2( pexpr(car(e3)), new);
|
|
862 }
|
|
863 return reverse0(new);
|
462
|
864 }
|
|
865
|
|
866 static int
|
|
867 p_asm(int e)
|
|
868 {
|
501
|
869 int param=caddr(e);
|
|
870 int e1 = p_list_expr(cadddr(e));
|
|
871 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
872 }
|
|
873
|
|
874 static int
|
|
875 p_label(int e)
|
|
876 {
|
551
|
877 int e1,lb;
|
|
878 if ((e1=caddr(e))) {
|
|
879 switch(car(e1)) {
|
|
880 case FLABEL: /* error(-1); */ break;
|
|
881 case IVAR:
|
|
882 lb = cadr(e1);
|
|
883 if (!(e1=heap[pdisp+lb])) {
|
|
884 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
885 }
|
|
886 }
|
|
887 }
|
|
888 return list3(ST_LABEL,pexpr(cadr(e)),e1);
|
|
889 }
|
|
890
|
|
891 static int
|
|
892 p_label_var(int e)
|
|
893 {
|
|
894 int d,e1;
|
|
895 switch(car(e1=e)) {
|
|
896 case LVAR: /* error(-1) */ break;
|
|
897 case IVAR:
|
|
898 // should this done in p_decl? (in __label__?)
|
|
899 d = cadr(e);
|
|
900 if (!(e1=(heap[pdisp+d]))) {
|
552
|
901 // error(-1);
|
551
|
902 e1 = heap[pdisp+d]=list3(LVAR,fwdlabel(),caddr(e));
|
|
903 }
|
|
904 }
|
|
905 return list2(LABEL,e1);
|
462
|
906 }
|
|
907
|
|
908 static int
|
|
909 p_bool(int e)
|
|
910 {
|
501
|
911 error(-1);
|
464
|
912 return e;
|
|
913 }
|
462
|
914
|
464
|
915 static int
|
|
916 p_comment(int e)
|
|
917 {
|
574
|
918 glineno++;
|
501
|
919 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
920 }
|
|
921
|
508
|
922 static int
|
512
|
923 p_inline(int e)
|
508
|
924 {
|
|
925 int e3;
|
|
926 int narg;
|
|
927
|
|
928 /* inline function arguments */
|
|
929 narg = 0;
|
|
930 for (e3 = caddr(e); e3; e3 = cadr(e3)) {
|
|
931 narg=list3(pexpr(car(e3)),narg,caddr(e3));
|
|
932 }
|
|
933 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e));
|
|
934 }
|
|
935
|
463
|
936 extern int
|
|
937 pexpr(int e1)
|
462
|
938 {
|
|
939 int e2,e3;
|
|
940
|
463
|
941 // if (inmode) error(-1);
|
502
|
942 if (e1==0) return 0;
|
462
|
943 e2 = cadr(e1);
|
|
944 switch (car(e1)){
|
|
945 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
946 case SURGVAR: case REGISTER:
|
|
947 case DREGISTER: case FREGISTER:
|
500
|
948 case FRGVAR: case DRGVAR:
|
462
|
949 case LREGISTER:
|
500
|
950 case LRGVAR: case LURGVAR:
|
551
|
951 case CONST:
|
462
|
952 case DCONST: case FCONST:
|
|
953 case LCONST:
|
|
954 case STRING:
|
|
955 case FNAME:
|
503
|
956 case FLABEL:
|
616
|
957 case BUILTIN_INF:
|
|
958 case BUILTIN_INFF:
|
|
959 case BUILTIN_INFL:
|
462
|
960 return e1;
|
551
|
961 case RSTRUCT:
|
|
962 // list3(RSTRUCT,e,size)
|
|
963 return pexpr(e2);
|
|
964 case LABEL:
|
|
965 return p_label_var(e2);
|
462
|
966 case LVAR:
|
|
967 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
500
|
968 case FRLVAR: case DRLVAR:
|
|
969 case LRLVAR: case LURLVAR:
|
|
970 return e1;
|
|
971 case IVAR:
|
462
|
972 return p_lvar(e1);
|
|
973 case FUNCTION:
|
|
974 return pfunction(e1);
|
609
|
975 case JUMP:
|
601
|
976 e2 = pexpr(e2);
|
574
|
977 return list3(car(e1),e2,pexpr(cadddr(e1)));
|
601
|
978 case ARRAY:
|
|
979 e2 = pexpr(e2);
|
|
980 e1 = binop(ADD,e2,pexpr(caddr(e1)),cadddr(e1),caddddr(e1));
|
|
981 return indop(e1);
|
|
982 case PERIOD:
|
|
983 e2 = pexpr(e2);
|
|
984 nptr = (NMTBL*)caddr(e1);
|
|
985 type = cadddr(e1);
|
|
986 return strop(e2,0);
|
|
987 case ARROW:
|
|
988 e2 = pexpr(e2);
|
|
989 nptr = (NMTBL*)caddr(e1);
|
|
990 type = cadddr(e1);
|
|
991 return strop(e2,1);
|
462
|
992 case INLINE:
|
512
|
993 return p_inline(e1);
|
462
|
994 case INDIRECT:
|
527
|
995 return pindirect(e1);
|
462
|
996 case RINDIRECT: case URINDIRECT:
|
|
997 case CRINDIRECT: case CURINDIRECT:
|
|
998 case SRINDIRECT: case SURINDIRECT:
|
|
999 case FRINDIRECT: case DRINDIRECT:
|
|
1000 case LRINDIRECT: case LURINDIRECT:
|
|
1001 return prindirect(e1);
|
|
1002 case ADDRESS:
|
502
|
1003 return paddress(e1);
|
566
|
1004 case ST_OP:
|
|
1005 e3=caddr(e1);
|
|
1006 e1=pexpr(car(e3));
|
|
1007 return binop(e2,e1,pexpr(cadr(e3)),caddr(e3),cadddr(e3));
|
462
|
1008 case MINUS:
|
463
|
1009 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
1010 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1011 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
1012 return list2(car(e1),e3);
|
|
1013 #if LONGLONG_CODE
|
|
1014 case LMINUS:
|
463
|
1015 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1016 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
1017 return list2(car(e1),e3);
|
|
1018 #endif
|
|
1019 #if FLOAT_CODE
|
|
1020 case DMINUS:
|
463
|
1021 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1022 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
1023 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
1024 return list2(car(e1),e3);
|
|
1025 case FMINUS:
|
463
|
1026 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1027 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
1028 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
1029 return list2(car(e1),e3);
|
|
1030 #endif
|
|
1031 case CONV:
|
501
|
1032 return p_conv(caddr(e1),e2);
|
462
|
1033 case BNOT: /* ~ */
|
463
|
1034 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1035 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
1036 return list2(BNOT,e3);
|
|
1037 case LNOT: /* ! */
|
463
|
1038 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1039 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
1040 return list2(LNOT,e3);
|
|
1041 case PREINC:
|
|
1042 case UPREINC:
|
463
|
1043 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
1044 if (car(e3)==CONST) return list2(CONST,cadr(e3)+caddr(e1));
|
503
|
1045 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1046 case POSTINC:
|
|
1047 case UPOSTINC:
|
463
|
1048 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1049 if (car(e3)==CONST) return e3;
|
503
|
1050 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1051 #if FLOAT_CODE
|
|
1052 case DPREINC: /* ++d */
|
463
|
1053 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1054 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1055 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1056 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1057 case DPOSTINC: /* d++ */
|
463
|
1058 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1059 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1060 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1061 case FPREINC: /* ++f */
|
463
|
1062 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1063 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1064 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1065 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1066 case FPOSTINC: /* f++ */
|
463
|
1067 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1068 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1069 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1070 #endif
|
|
1071 #if LONGLONG_CODE
|
|
1072 case LPREINC: /* ++d */
|
|
1073 case LUPREINC: /* ++d */
|
463
|
1074 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1075 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
1076 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1077 case LPOSTINC: /* d++ */
|
|
1078 case LUPOSTINC: /* d++ */
|
463
|
1079 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1080 if (car(e3)==LCONST) return e3;
|
503
|
1081 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1082 #endif
|
|
1083 case MUL: case UMUL:
|
|
1084 case DIV: case UDIV:
|
|
1085 case MOD: case UMOD:
|
|
1086 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
1087 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
1088 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
1089 #if FLOAT_CODE
|
|
1090 case DMUL: case DDIV:
|
|
1091 case DADD: case DSUB:
|
|
1092 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
1093 case FMUL: case FDIV:
|
|
1094 case FADD: case FSUB:
|
|
1095 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
1096 #endif
|
|
1097 #if LONGLONG_CODE
|
|
1098 case LMUL: case LUMUL:
|
|
1099 case LDIV: case LUDIV:
|
|
1100 case LMOD: case LUMOD:
|
|
1101 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
1102 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
1103 #endif
|
501
|
1104 return pbinop(car(e1),e2,caddr(e1));
|
557
|
1105 // relational operator
|
|
1106 case GT: case UGT: case GE: case UGE: case LT:
|
|
1107 case ULT: case LE: case ULE:
|
|
1108 case LOP+GT: case LOP+UGT: case LOP+GE: case LOP+UGE: case LOP+LT:
|
|
1109 case LOP+ULT: case LOP+LE: case LOP+ULE:
|
|
1110 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE:
|
|
1111 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE:
|
|
1112 case FOP+EQ: case FOP+NEQ:
|
|
1113 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ:
|
|
1114 case LOP+EQ: case LOP+NEQ:
|
|
1115 return pbinop(car(e1),e2,caddr(e1));
|
|
1116 case LAND:
|
|
1117 return pland(car(e1),cadr(e1),caddr(e1));
|
|
1118 case LOR:
|
|
1119 return plor(car(e1),cadr(e1),caddr(e1));
|
528
|
1120 case LCOND: case DCOND: case FCOND: case COND: case UCOND: case LUCOND:
|
555
|
1121 e2 = pexpr(e2);
|
|
1122 if (car(e2)==CONST) return
|
|
1123 caddr(e1)? pexpr(cadr(e2)?caddr(e1):cadddr(e1)) :
|
|
1124 pexpr(cadr(e2)?e2:cadddr(e1)); // GNU extension h?:g
|
|
1125 e3=pexpr(caddr(e1));
|
|
1126 return list4(car(e1),e2,e3,pexpr(cadddr(e1)));
|
462
|
1127 case STASS:
|
|
1128 return psassign(e1);
|
|
1129 case ASS: case CASS: case SASS:
|
|
1130 return passign(e1);
|
|
1131 case SASSOP: case SUASSOP:
|
|
1132 case ASSOP: case CASSOP: case CUASSOP:
|
|
1133 return passop(e1);
|
|
1134 #if FLOAT_CODE
|
|
1135 case FASS: case DASS:
|
|
1136 return pdassign(e1);
|
|
1137 case DASSOP: case FASSOP:
|
|
1138 return pdassop(e1);
|
616
|
1139
|
|
1140 case BUILTIN_FABS:
|
|
1141 case BUILTIN_FABSF:
|
|
1142 case BUILTIN_FABSL:
|
|
1143 e2 = pexpr(e2);
|
|
1144 if (is_const(e2)) {
|
|
1145 if (dcadr(e2) >= 0.0 ) return e2;
|
|
1146 return pexpr(list2(car(e1)==BUILTIN_FABSF?
|
|
1147 FMINUS:DMINUS,e2));
|
|
1148 }
|
|
1149 return list2(car(e1),e2);
|
462
|
1150 #endif
|
|
1151 #if LONGLONG_CODE
|
|
1152 case LASS:
|
|
1153 return plassign(e1);
|
|
1154 case LASSOP: case LUASSOP:
|
|
1155 return plassop(e1);
|
|
1156 #endif
|
|
1157 case ALLOCA:
|
501
|
1158 return palloc(e2);
|
462
|
1159 case BUILTINP:
|
463
|
1160 return list2(CONST,is_const(pexpr(e2)));
|
462
|
1161 case COMMA:
|
555
|
1162 return pcomma(e2,caddr(e1));
|
462
|
1163 case RETURN:
|
|
1164 case ENVIRONMENT:
|
|
1165 case LCALL:
|
|
1166 return e1;
|
|
1167 #if BIT_FIELD_CODE
|
|
1168 case RBIT_FIELD:
|
|
1169 return prbit_field(e1);
|
553
|
1170 case BIT_FIELD:
|
|
1171 return list3(BIT_FIELD,pexpr(e2),caddr(e1));
|
462
|
1172 case BASS:
|
|
1173 return pbassign(e1);
|
|
1174 case BPREINC:
|
|
1175 case BPOSTINC:
|
|
1176 case BASSOP:
|
|
1177 return pbassop(e1);
|
|
1178 #endif
|
|
1179 #if ASM_CODE
|
|
1180 case ASM:
|
|
1181 return list3(ASM,list4(
|
501
|
1182 car(e2),cadr(e2),caddr(e2),cadddr(e2)),
|
|
1183 caddr(e1));
|
462
|
1184 #endif
|
|
1185 case ST_DECL: return p_decl(e1);
|
|
1186 case ST_IF: return p_if(e1);
|
|
1187 case ST_DO: return p_do(e1);
|
|
1188 case ST_WHILE: return p_while(e1);
|
|
1189 case ST_FOR: return p_for(e1);
|
|
1190 case ST_SWITCH: return p_switch(e1);
|
|
1191 case ST_COMP: return p_comp(e1);
|
|
1192 case ST_BREAK: return p_break(e1);
|
|
1193 case ST_CONTINUE: return p_continue(e1);
|
|
1194 case ST_CASE: return p_case(e1);
|
|
1195 case ST_DEFAULT: return p_default(e1);
|
|
1196 case ST_RETURN: return p_return(e1);
|
|
1197 case ST_GOTO: return p_goto(e1);
|
|
1198 case ST_ASM: return p_asm(e1);
|
|
1199 case ST_LABEL: return p_label(e1);
|
|
1200 case ST_COMMENT: return p_comment(e1);
|
|
1201 default:
|
551
|
1202 error(-1);
|
462
|
1203 return p_bool(e1);
|
|
1204 }
|
|
1205 return VOID;
|
|
1206 }
|
|
1207
|
625
|
1208 /*
|
|
1209 Prepare pvariable table
|
|
1210 */
|
|
1211
|
557
|
1212 static int
|
|
1213 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals)
|
|
1214 {
|
|
1215 int arg;
|
|
1216 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) {
|
625
|
1217 // replacable const variable
|
557
|
1218 if (is_memory(e4)) {
|
|
1219 heap[pdisp+narg]=reference(e4);
|
|
1220 return evals;
|
|
1221 } else if (is_const(e4)) {
|
|
1222 heap[pdisp+narg]=e4;
|
|
1223 return evals;
|
|
1224 }
|
|
1225 }
|
625
|
1226 // we need real local variable for this inline
|
557
|
1227 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),0);
|
|
1228 inline_lvars = glist2(arg,inline_lvars);
|
|
1229 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals);
|
|
1230 return evals;
|
|
1231 }
|
|
1232
|
|
1233 /*
|
|
1234 setup parameter replacement of inline call
|
|
1235 */
|
|
1236
|
|
1237 static void
|
|
1238 enter_inline(NMTBL *n, int e)
|
|
1239 {
|
|
1240 int e1 = attr_value(n,INLINE);
|
|
1241 int arg_disp = cadr(e1); // number of arguments
|
|
1242 int narg,e3,e4, e5, fargtype;
|
|
1243 int evals = 0;
|
|
1244 int t;
|
|
1245 NMTBL *anptr;
|
|
1246
|
|
1247 fnptr = n; // st_return see this
|
|
1248 pvartable = p_vartable(arg_disp, /* number of arg */
|
|
1249 caddr(e1) /* number of local parameter */);
|
|
1250
|
|
1251 /* function arguments type */
|
|
1252 fargtype = n->dsp; // we cannot do destruct reverse here
|
463
|
1253
|
557
|
1254 /* inline function arguments */
|
|
1255 narg = 0;
|
|
1256 if (!fargtype) {
|
|
1257 goto no_args; // wrong number of arguments
|
|
1258 }
|
|
1259 for (e3 = e5 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
|
1260 anptr = (NMTBL*)caddr(fargtype);
|
|
1261 if (!anptr) break; // should not happen?
|
|
1262 t=caddr(e3); // type
|
|
1263 e4 = car(e3);
|
|
1264 evals = replace_inline_parameter(anptr,t,e4,narg,evals);
|
|
1265 narg ++;
|
|
1266 fargtype = cadr(fargtype);
|
|
1267 }
|
|
1268 caddr(e) = reverse0(e5); // make it normal
|
|
1269 if (eval_order==NORMAL) evals = reverse0(evals);
|
|
1270 for(;evals;evals=cadr(evals)) {
|
|
1271 g_expr_u(car(evals));
|
|
1272 }
|
|
1273 no_args:
|
|
1274 return;
|
|
1275 }
|
|
1276
|
|
1277 /*
|
|
1278 pass local static variable list to our parents
|
|
1279 clean up and free used inline parameters
|
|
1280 */
|
|
1281
|
|
1282 static void
|
|
1283 leave_inline(int e1)
|
|
1284 {
|
|
1285 NMTBL *n;
|
|
1286 NMTBL *local_statics = (NMTBL*)cadddr(e1); // local static list
|
|
1287
|
|
1288 if (retcont) error(STERR); // inline can't handle return/environment
|
|
1289
|
|
1290 if (local_statics && local_statics != &null_nptr) {
|
|
1291 // append our local static variables to the parent list
|
|
1292 n = local_statics;
|
|
1293 while(n->next != &null_nptr) n=n->next;
|
|
1294 n->next = local_static_list; local_static_list = local_statics;
|
|
1295 cadddr(e1) = 0; // prevent duplicate initialize
|
|
1296 }
|
625
|
1297
|
|
1298 // free used local variables or registers
|
557
|
1299 while(inline_lvars) {
|
|
1300 int e;
|
|
1301 int l = car(inline_lvars);
|
|
1302 switch(car(l)) {
|
|
1303 case LVAR: free_lvar(cadr(l)); break;
|
|
1304 case REGISTER: case DREGISTER:
|
|
1305 case FREGISTER: case LREGISTER:
|
|
1306 free_register(cadr(l));
|
|
1307 }
|
|
1308 e = cadr(inline_lvars);
|
|
1309 free_glist2(inline_lvars);
|
|
1310 inline_lvars = e;
|
|
1311 }
|
|
1312 }
|
530
|
1313
|
463
|
1314 extern int
|
|
1315 gen_inline(int e)
|
462
|
1316 {
|
514
|
1317 // these saved value should be some struct
|
|
1318 int sretlabel;
|
|
1319 NMTBL *sfnptr;
|
552
|
1320 int svartable;
|
|
1321 int sretcont;
|
513
|
1322 int scslabel = cslabel;
|
463
|
1323 int sdisp = pdisp;
|
552
|
1324 int sret_register;
|
|
1325 int sret_reg_mode;
|
|
1326 int sinline_lvars;
|
|
1327 int slfree;
|
531
|
1328 // int slreg_count=lreg_count;
|
513
|
1329
|
463
|
1330 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
1331 int e1 = attr_value(n,INLINE);
|
500
|
1332 int parse = car(e1); // inline parse tree
|
557
|
1333 int dots;
|
508
|
1334 int ret_type = function_type(cadddr(e),&dots);
|
463
|
1335
|
552
|
1336 checkret();
|
|
1337 checkjmp(-1);
|
|
1338
|
|
1339 svartable = pvartable;
|
|
1340 sretcont = retcont;
|
|
1341 scslabel = cslabel;
|
|
1342 sdisp = pdisp;
|
|
1343 sret_register = ret_register;
|
|
1344 sret_reg_mode = ret_reg_mode;
|
|
1345 sinline_lvars = inline_lvars;
|
|
1346 slfree=lfree;
|
|
1347 // int slreg_count=lreg_count;
|
514
|
1348
|
|
1349 sretlabel = retlabel;
|
|
1350 sfnptr = fnptr;
|
513
|
1351 retcont = 0;
|
|
1352 cslabel = -1;
|
|
1353 retpending = 0;
|
514
|
1354 ret_reg_mode = 0;
|
|
1355 ret_register = 5555;
|
527
|
1356 inline_lvars = 0;
|
514
|
1357 retlabel = fwdlabel();
|
513
|
1358
|
557
|
1359 enter_inline(n,e);
|
526
|
1360
|
557
|
1361 // partial evaluation of parse tree
|
|
1362 // constant propergation, etc.
|
|
1363 parse = pexpr(parse);
|
463
|
1364 pdisp = sdisp;
|
464
|
1365 pvartable = svartable;
|
513
|
1366
|
557
|
1367 // generate code if necessary
|
513
|
1368 if (ret_type!=VOID)
|
557
|
1369 g_expr0(parse);
|
513
|
1370 else
|
557
|
1371 g_expr_u(parse);
|
|
1372
|
524
|
1373 checkret();
|
513
|
1374 fwddef(retlabel);
|
514
|
1375 control=1;
|
513
|
1376
|
557
|
1377 leave_inline(e1);
|
510
|
1378
|
513
|
1379 fnptr = sfnptr;
|
|
1380 retlabel = sretlabel;
|
|
1381 retcont = sretcont;
|
|
1382 cslabel = scslabel;
|
514
|
1383 ret_register = sret_register;
|
|
1384 ret_reg_mode = sret_reg_mode;
|
527
|
1385 inline_lvars = sinline_lvars;
|
528
|
1386 lfree=slfree;
|
513
|
1387
|
508
|
1388 return ret_type;
|
462
|
1389 }
|
|
1390
|
|
1391 /* end */
|