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