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;
|
690
|
216 g_expr(e); /* switch value */
|
558
|
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) {
|
648
|
229 if(dlabel) df_label(cslabel,dlabel);
|
558
|
230 else fwddef(cslabel);
|
|
231 }
|
462
|
232 #else
|
558
|
233 if (!(cst && cslist)) {
|
648
|
234 if(dlabel) df_label(cslabel,dlabel);
|
558
|
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 }
|
705
|
381 if (e) {
|
|
382 g_expr(e);
|
|
383 }
|
|
384 if (e && t!=VOID) {
|
|
385 if (ret_reg_mode==0) {
|
|
386 // return value register is not fixed
|
|
387 ret_reg_mode=1;
|
|
388 ret_register = code_get_fixed_creg(USE_CREG,t);
|
|
389 } else {
|
|
390 code_set_fixed_creg(ret_register,1,t);
|
|
391 }
|
462
|
392 }
|
|
393 // conv->return_end_();
|
|
394 retpending = 1;
|
|
395 }
|
|
396
|
|
397 extern void
|
|
398 st_goto(int e){
|
696
|
399 int e1;
|
462
|
400
|
|
401 checkret();
|
|
402 e1 = caddr(e);
|
|
403 if (car(e1)==RINDIRECT) {
|
|
404 gen_indirect_goto(cadr(e1));
|
505
|
405 return ;
|
552
|
406 } else if (car(e1)==RLVAR) {
|
551
|
407 gen_indirect_goto(e1);
|
|
408 return ;
|
552
|
409 } else if (car(e1)==LVAR||car(e1)==FLABEL) {
|
551
|
410 gen_jmp(cadr(e1));
|
462
|
411 control=0;
|
505
|
412 return ;
|
691
|
413 } else if (car(e1)==JUMP) {
|
462
|
414 /* CbC continuation */
|
|
415 // conv->jump_(env);
|
551
|
416 // should be separate function
|
696
|
417 jump(cadr(e1),caddr(e1));
|
462
|
418 control=0;
|
|
419 // conv->sm_();
|
505
|
420 return ;
|
551
|
421 } else {
|
|
422 error(-1);
|
462
|
423 }
|
|
424 }
|
|
425
|
|
426
|
|
427 #if ASM_CODE
|
|
428 extern void
|
|
429 st_asm(int e1){
|
|
430 checkret();
|
|
431 g_expr_u(list3(ASM,caddr(e1),cadddr(e1)));
|
|
432 }
|
|
433 #endif
|
|
434
|
|
435
|
|
436 extern void
|
|
437 st_label(int e1){
|
551
|
438 int lb = caddr(e1);
|
462
|
439 control=1;
|
|
440 checkret();
|
551
|
441 if (car(lb)==LVAR) { // label variable case
|
|
442 } else if (car(lb)!=FLABEL) error(-1);
|
|
443 fwddef(cadr(lb));
|
462
|
444 }
|
|
445
|
690
|
446 static
|
|
447 char *plinebuf; // last comment in parse tree (for compiler debug)
|
|
448
|
462
|
449 extern void
|
|
450 st_comment(int e1){
|
574
|
451 glineno++;
|
585
|
452 printf("## %d ",glineno);
|
690
|
453 gen_comment(plinebuf=(char *)caddr(e1));
|
462
|
454 }
|
|
455
|
|
456 /*
|
|
457 partial evaluator
|
|
458 */
|
|
459
|
|
460 static int
|
557
|
461 p_vartable(int adisp,int ldisp)
|
462
|
462 {
|
464
|
463 int i;
|
505
|
464 int pvartable = getfree(adisp-ldisp); // have to be local heap
|
509
|
465 pdisp = pvartable-ldisp;
|
|
466 for(i=ldisp;i<0;i++) {
|
|
467 heap[pdisp+i] = 0;
|
463
|
468 }
|
505
|
469 return pvartable;
|
462
|
470 }
|
|
471
|
|
472 static int
|
|
473 p_lvar(int e1)
|
|
474 {
|
463
|
475 int sz = is_memory(e1);
|
|
476 int d = cadr(e1);
|
527
|
477 int d1,e;
|
502
|
478 if ((d1=(heap[pdisp+d]))) return d1;
|
574
|
479 fprintf(stderr,"## undeclared ivar %d\n",d);
|
|
480 // error(-1);
|
527
|
481 e = heap[pdisp+d]=list3(LVAR,new_lvar(sz),0);
|
|
482 inline_lvars = glist2(e,inline_lvars);
|
|
483 return e;
|
462
|
484 }
|
|
485
|
|
486 static int
|
|
487 pfunction(int e)
|
|
488 {
|
501
|
489 // list4(INLINE,e1,arglist,ftype);
|
|
490 // include code segement case
|
|
491 int e1 = pexpr(cadr(e));
|
|
492 int arglist = caddr(e);
|
|
493 int newargs = 0;
|
|
494 int ftype = cadddr(e);
|
|
495 int e3;
|
|
496 for (e3 = arglist; e3; e3 = cadr(e3)) {
|
514
|
497 newargs = list3( pexpr(car(e3)), newargs, caddr(e3));
|
501
|
498 }
|
|
499 newargs = reverse0(newargs);
|
513
|
500 return list4(car(e),e1,newargs,ftype);
|
462
|
501 }
|
|
502
|
|
503 static int
|
|
504 prindirect(int e)
|
|
505 {
|
525
|
506 int lvar;
|
533
|
507 int offset = caddr(e);
|
690
|
508 int type0;
|
|
509 type = cadddr(e);
|
|
510 type0 = type_value(type);
|
|
511 if (type0>0 && car(type0)==POINTER)
|
|
512 type=set_type_with_attr(cadr(type),type);
|
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 {
|
690
|
551 int t,stype = type;
|
557
|
552 if (is_const(e2) && (t=type_of_conv(e1))) {
|
690
|
553 type = INT;
|
|
554 e1 = correct_type(e2,t);
|
|
555 type = stype;
|
|
556 return e1;
|
557
|
557 }
|
527
|
558 return list3(CONV,pexpr(e2),e1);
|
462
|
559 }
|
|
560
|
|
561 static int
|
464
|
562 pbinop(int op,int e1,int e2)
|
462
|
563 {
|
557
|
564 e1 = pexpr(e1);
|
|
565 e2 = pexpr(e2);
|
|
566 if (is_const(e1)&&is_const(e2)) {
|
|
567 int t;
|
|
568 if((t= type_of_bop(op)))
|
599
|
569 return binop(OP(op),e1,e2,t,t);
|
557
|
570 }
|
|
571 return list3(op,e1,e2);
|
502
|
572 }
|
|
573
|
|
574 static int
|
526
|
575 plor(int op,int e1,int e2)
|
|
576 {
|
555
|
577 int e = pexpr(e1);
|
|
578 return list3(op,e,pexpr(e2));
|
526
|
579 }
|
|
580
|
|
581 static int
|
|
582 pland(int op,int e1,int e2)
|
|
583 {
|
555
|
584 int e = pexpr(e1);
|
|
585 return list3(op,e,pexpr(e2));
|
526
|
586 }
|
|
587
|
|
588 static int
|
462
|
589 psassign(int e)
|
|
590 {
|
555
|
591 int e1 = pexpr(cadr(e));
|
|
592 int e2 = pexpr(caddr(e));
|
|
593 return list4(car(e),e1,e2,cadddr(e));
|
462
|
594 }
|
|
595
|
|
596 static int
|
|
597 passign(int e)
|
|
598 {
|
555
|
599 int e1 = pexpr(cadr(e));
|
|
600 int e2 = pexpr(caddr(e));
|
|
601 return list3(car(e),e1,e2);
|
462
|
602 }
|
|
603
|
|
604 static int
|
|
605 passop(int e)
|
|
606 {
|
555
|
607 int e1 = pexpr(cadr(e));
|
|
608 int e2 = pexpr(caddr(e));
|
|
609 return list4(car(e),e1,e2,cadddr(e));
|
462
|
610 }
|
|
611
|
|
612 static int
|
|
613 pdassign(int e)
|
|
614 {
|
555
|
615 int e1 = pexpr(cadr(e));
|
|
616 int e2 = pexpr(caddr(e));
|
|
617 return list3(car(e),e1,e2);
|
462
|
618 }
|
|
619
|
|
620 static int
|
|
621 pdassop(int e)
|
|
622 {
|
555
|
623 int e1 = pexpr(cadr(e));
|
|
624 int e2 = pexpr(caddr(e));
|
|
625 return list4(car(e),e1,e2,cadddr(e));
|
462
|
626 }
|
|
627
|
|
628 static int
|
|
629 plassign(int e)
|
|
630 {
|
555
|
631 int e1 = pexpr(cadr(e));
|
|
632 int e2 = pexpr(caddr(e));
|
|
633 return list3(car(e),e1,e2);
|
462
|
634 }
|
|
635
|
|
636 static int
|
|
637 plassop(int e)
|
|
638 {
|
555
|
639 int e1 = pexpr(cadr(e));
|
|
640 int e2 = pexpr(caddr(e));
|
|
641 return list4(car(e),e1,e2,cadddr(e));
|
462
|
642 }
|
|
643
|
|
644 static int
|
|
645 palloc(int e)
|
|
646 {
|
695
|
647 int e1 = pexpr(e);
|
|
648 if (car(e1)==CONST)
|
|
649 return list2(ADDRESS,list3(LVAR,new_lvar_align(cadr(e1),16),0));
|
|
650 return list2(ALLOCA,e1);
|
462
|
651 }
|
|
652
|
|
653 static int
|
464
|
654 pcomma(int e1,int e2)
|
462
|
655 {
|
556
|
656 int e = pexpr(e1);
|
555
|
657 return list3(COMMA,e,pexpr(e2));
|
462
|
658 }
|
|
659
|
|
660 static int
|
|
661 prbit_field(int e)
|
|
662 {
|
501
|
663 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
664 }
|
|
665
|
|
666 static int
|
|
667 pbassign(int e)
|
|
668 {
|
553
|
669 // list4(BASS,e1,e2,list2(BASS,t)));
|
|
670 int e1=pexpr(caddr(e));
|
|
671 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
672 }
|
|
673
|
|
674 static int
|
|
675 pbassop(int e)
|
|
676 {
|
695
|
677 int e1 = caddr(e);
|
|
678 if (car(e)==BASSOP) e1=pexpr(e1);
|
553
|
679 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
680 }
|
|
681
|
681
|
682 /*
|
|
683 variable initialization with offset
|
|
684 */
|
|
685
|
|
686 static int
|
|
687 passign_data(int var,int target_type, int e,int t,int offset)
|
|
688 {
|
|
689 int ass,sz,bfd;
|
|
690
|
|
691 #if STRUCT_ALIGN
|
|
692 if (t!=-99) {
|
|
693 int strtype=0;
|
|
694 if (t>0 && (car(t)==STRUCT||car(t)==UNION))
|
|
695 strtype=1;
|
|
696 sz = size(t);
|
|
697 if (sz%size_of_int==0||strtype) {
|
|
698 offset = ((offset+(size_of_int-1))&~(size_of_int-1));
|
|
699 }
|
|
700 }
|
|
701 #endif
|
|
702 if (car(e)==ADDRESS||car(e)==GVAR) {
|
702
|
703 if (scalar(t)) {
|
681
|
704 t = list2(POINTER,VOID); // fake
|
702
|
705 } else if (t>0 && car(t)==ARRAY) {
|
681
|
706 } else {
|
|
707 error(TYERR);
|
|
708 }
|
|
709 }
|
|
710 if (t==EMPTY) {
|
|
711 /* empty space in partial initialization */
|
|
712 return offset+cadr(e);
|
|
713 }
|
|
714 type = t;
|
702
|
715 // e = rvalue_t(e,t);
|
681
|
716 if (!scalar(type) && (type>0 && (car(type)!=ADDRESS && car(type)!=ARRAY)))
|
|
717 e = correct_type(e,target_type);
|
|
718 /* If this is a local declared constant, we don't have to assign.
|
|
719 But some one may take it's address. We have to generate assign.
|
|
720 */
|
|
721 ass = assign_expr0(
|
|
722 offset?
|
|
723 list3(ADD,var,list2(CONST,offset)):
|
|
724 var,
|
|
725 e,target_type,t); // already correct_typed
|
|
726 init_vars = list2(ass,init_vars);
|
|
727 if (t>0&&car(t)==BIT_FIELD) {
|
|
728 sz = 0;
|
|
729 bfd = cadr(caddr(t)); /* bit_field_disp */
|
|
730 #if BIT_FIELD_CODE
|
|
731 code_bit_field_disp(t,&offset,&bfd,&sz);
|
|
732 #endif
|
|
733 return offset+sz;
|
|
734 }
|
|
735 return offset+((t==EMPTY)?cadr(e):size(t));
|
|
736 }
|
|
737
|
|
738 static int pdecl_data(int var, int target_type, int init,int offset);
|
|
739
|
|
740 static void
|
|
741 pflush_decl_data(int var,int sz)
|
|
742 {
|
|
743 int offset;
|
|
744 int offset0=0;
|
|
745 int e;
|
|
746 int t,offset1=0;
|
|
747 int smode=mode;
|
|
748 int init = decl_str_init;
|
|
749
|
|
750 decl_str_init = 0;
|
|
751 mode = STADECL;
|
|
752 /*
|
|
753 decl_str_init
|
|
754 output delayed decl data
|
|
755 list4(offset,next,expression,list2(type0,type1));
|
|
756 */
|
|
757 while (init) {
|
|
758 offset= car(init);
|
|
759 e=caddr(init);
|
|
760 t=car(cadddr(init)); // type of source
|
|
761 type=cadr(cadddr(init)); // destination type
|
|
762 if (offset!=offset0) {
|
|
763 // make space
|
|
764 passign_data(var,EMPTY,list2(CONST,offset-offset0),EMPTY,offset0);
|
|
765 }
|
|
766 e = pexpr(e);
|
|
767 // offset0 = passign_data(var,type,e,t,offset);
|
|
768 offset0 = pdecl_data(var,type,e,offset);
|
|
769 init = cadr(init);
|
|
770 }
|
|
771 offset = offset0;
|
|
772 if ((sz=(offset1+sz-offset))>0)
|
|
773 passign_data(var,EMPTY,list2(CONST,sz),EMPTY,offset0);
|
|
774 local_nptr = 0;
|
|
775 mode=smode;
|
|
776 }
|
|
777
|
|
778 static int
|
|
779 str_init_eq()
|
|
780 {
|
|
781 // error(-1); // duplicate struct field value
|
|
782 return 2; // allow override keep unique
|
|
783 }
|
|
784
|
|
785
|
|
786 static int
|
|
787 pdecl_data_array(int var,int init,int target_type,int offset)
|
|
788 {
|
|
789 int type0 = cadr(target_type); /* array item type */
|
|
790 int e;
|
|
791
|
|
792 for(; init; init = cadr(init)) {
|
|
793 // unordered data with tag or array offset
|
|
794 if (car(init)!=DECL_DATA_ARRAY) {
|
|
795 error(-1);
|
|
796 }
|
|
797 e = pexpr(caddr(init));
|
|
798 offset = pdecl_data(var,type0,e,offset);
|
|
799 }
|
|
800 return offset;
|
|
801 }
|
|
802
|
|
803 static int
|
|
804 pdecl_data_field(int var,int init,int target_type,int offset)
|
|
805 {
|
|
806 int type0 = target_type; /* list of fields */
|
|
807 int e,t,type1,foffset;
|
|
808 NMTBL *n;
|
|
809
|
|
810 for(; init; init = cadr(init)) {
|
|
811 // unordered data with tag or array offset
|
|
812 if (car(init)!=DECL_DATA_FIELD) {
|
|
813 error(-1);
|
|
814 }
|
|
815 n = (NMTBL*)cadddr(init);
|
|
816 type1 = search_struct_type(type0,n->nm,&foffset);
|
|
817 e = caddr(init);
|
|
818 if (car(e)!=DECL_DATA) error(-1);
|
|
819 t = caddr(e);
|
|
820 decl_str_init=insert_ascend(decl_str_init,
|
|
821 glist4(offset+foffset,0,e,glist2(type1,t)),str_init_eq);
|
|
822 }
|
|
823 return offset;
|
|
824 }
|
|
825
|
|
826 static int
|
|
827 pdecl_data_list(int var,int init,int target_type,int offset)
|
|
828 {
|
|
829 int type0 = caddr(target_type); /* list of fields */
|
|
830 int e;
|
|
831
|
703
|
832 for(; init; init = cadr(init),type0 = cadr(type0)) {
|
|
833 if (car(init)==DECL_DATA) {
|
692
|
834 // casted initilizer ?
|
703
|
835 //error(-1);
|
|
836 e = cadr(init); // value
|
|
837 if (!e) continue; // {...,} case
|
|
838 e = pexpr(e);
|
|
839 offset = pdecl_data(var,caddr(init),e,offset);
|
692
|
840 continue;
|
|
841 }
|
681
|
842 // ordered data
|
703
|
843 if (car(init)!=DECL_DATA_LIST) {
|
681
|
844 error(-1);
|
|
845 }
|
703
|
846 e = caddr(init);
|
|
847 if (!e) continue; // {...,} case
|
|
848 e = pexpr(e);
|
681
|
849 offset = pdecl_data(var,car(type0),e,offset);
|
|
850 }
|
|
851 return offset;
|
|
852 }
|
|
853
|
|
854 static int
|
|
855 pdecl_data(int var, int target_type, int init,int offset)
|
|
856 {
|
|
857 int t,e;
|
|
858 int save_decl_str_init = decl_str_init;
|
|
859 decl_str_init = 0;
|
|
860 target_type = type_value(target_type);
|
|
861
|
692
|
862 if (init==0) {
|
|
863 // empty declaration
|
|
864 // it can happen like a = (struct hoge){};
|
|
865 return offset;
|
|
866 }
|
682
|
867 e = cadr(init);
|
681
|
868 if (car(init)==DECL_DATA) {
|
682
|
869 switch(car(e)) {
|
681
|
870 case DECL_DATA_LIST:
|
|
871 offset = pdecl_data_list(var,e,target_type,offset);
|
|
872 break;
|
|
873 case DECL_DATA_FIELD:
|
|
874 offset = pdecl_data_field(var,e,target_type,offset);
|
|
875 break;
|
|
876 case DECL_DATA_ARRAY:
|
|
877 offset = pdecl_data_array(var,e,target_type,offset);
|
|
878 break;
|
|
879 default:
|
|
880 e = pexpr(e);
|
|
881 t = caddr(init); // type of source
|
|
882 offset = passign_data(var,target_type,e,t,offset);
|
|
883 }
|
|
884 } else {
|
|
885 error(-1);
|
|
886 }
|
|
887 if (decl_str_init) {
|
|
888 int sz = size(target_type);
|
|
889 pflush_decl_data(var,sz);
|
|
890 }
|
|
891 decl_str_init = save_decl_str_init;
|
|
892 return offset;
|
|
893 }
|
|
894
|
561
|
895 // handle local variable declaration
|
681
|
896 // initialization is accumrated in init argument
|
561
|
897 // should consider int k=some_compile_time_constant;
|
462
|
898 static int
|
|
899 p_decl(int e)
|
|
900 {
|
681
|
901 // list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode),init);
|
554
|
902 int ctmode=cadddr(e);
|
463
|
903 NMTBL *n=(NMTBL*)caddr(e);
|
|
904 int dsp = n->dsp;
|
569
|
905 int v=0;
|
552
|
906 int sstmode = stmode;
|
|
907 int smode = mode;
|
681
|
908 int save_init_vars = init_vars;
|
|
909 int init = caddddr(e); // variable initialization
|
|
910
|
|
911 init_vars = 0;
|
500
|
912 // in real partial evaluation, we have to check whether this variable
|
|
913 // is used or not.
|
552
|
914 if (ctmode) {
|
|
915 mode = car(ctmode); stmode = cadr(ctmode); ctmode = caddr(ctmode);
|
574
|
916 } else {
|
|
917 mode = LDECL; stmode = 0; ctmode = 0;
|
552
|
918 }
|
463
|
919 switch(stmode) {
|
528
|
920 case EXTRN: case EXTRN1: case STATIC:
|
552
|
921 // def(n,ctmode); we don't need this. already done.
|
|
922 // stmode = sstmode;
|
681
|
923 if (init) error(-1);
|
552
|
924 stmode = sstmode;
|
|
925 mode = smode;
|
681
|
926 init_vars = save_init_vars;
|
552
|
927 return pexpr(cadr(e));
|
529
|
928 #if 1
|
463
|
929 case REGISTER:
|
|
930 switch(n->ty) {
|
|
931 case ULONGLONG: case LONGLONG:
|
|
932 v = get_lregister_var(n); break;
|
|
933 case FLOAT:
|
|
934 v = get_dregister_var(n,0); break;
|
|
935 case DOUBLE:
|
|
936 v = get_dregister_var(n,1); break;
|
|
937 default:
|
|
938 if (scalar(n->ty))
|
524
|
939 v = get_register_var(n);
|
463
|
940 else
|
|
941 error(TYERR);
|
|
942 }
|
529
|
943 break;
|
528
|
944 #endif
|
701
|
945 // case LLDECL: LLDECL is mode, not stmode (bad design)
|
|
946 // v = list2(FLABEL,fwdlabel()); break;
|
463
|
947 default:
|
701
|
948 if (mode==STADECL) {
|
|
949 if (init) {
|
|
950 v = list3(GVAR,0,(int)n);
|
|
951 gen_decl_data(init,v);
|
|
952 }
|
|
953 stmode = sstmode;
|
|
954 mode = smode;
|
|
955 init_vars = save_init_vars;
|
|
956 return pexpr(cadr(e));
|
|
957 }
|
552
|
958 if (n->sc==FLABEL)
|
|
959 v = list3(LVAR,fwdlabel(),(int)n);
|
|
960 else
|
|
961 v = list3(LVAR,new_lvar(size(n->ty)),(int)n);
|
463
|
962 }
|
552
|
963 if (n->sc!=FLABEL)
|
|
964 inline_lvars = glist2(v,inline_lvars);
|
574
|
965 if (heap[pdisp+dsp]) {
|
|
966 fprintf(stderr,"## double p_decl %s %s\n",((NMTBL*)(caddr(heap[pdisp+dsp])))->nm,n->nm);
|
|
967 error(-1);
|
|
968 }
|
463
|
969 heap[pdisp+dsp]=v;
|
552
|
970 stmode = sstmode;
|
|
971 mode = smode;
|
681
|
972 if (init) {
|
|
973 pdecl_data(v,n->ty,init,0);
|
|
974 if (init_vars) {
|
|
975 int e1 = pexpr(cadr(e));
|
|
976 init_vars = reverse0(init_vars);
|
|
977 while (init_vars) {
|
|
978 e1 = list3(ST_COMP,e1,car(init_vars));
|
|
979 init_vars = cadr(init_vars);
|
|
980 }
|
|
981 init_vars = save_init_vars;
|
|
982 return e1;
|
|
983 }
|
|
984 }
|
|
985 init_vars = save_init_vars;
|
463
|
986 return pexpr(cadr(e));
|
462
|
987 }
|
|
988
|
|
989 static int
|
500
|
990 p_if(int e1)
|
462
|
991 {
|
500
|
992 int cond,l1,l2;
|
|
993 int e2=caddr(e1),e3;
|
|
994 cond = pexpr(car(e2));
|
|
995 // conv->if_then_();
|
|
996 l1 = pexpr(cadr(e2));
|
|
997 if ((e3=caddr(e2))) { // else
|
|
998 l2 = pexpr(e3);
|
|
999 } else {
|
|
1000 l2 = 0;
|
|
1001 }
|
|
1002 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
1003 }
|
|
1004
|
|
1005 static int
|
|
1006 p_do(int e)
|
|
1007 {
|
524
|
1008 int e2 = pexpr(caddr(e));
|
|
1009 int e3 = pexpr(cadddr(e));
|
|
1010 return list4(ST_DO,pexpr(cadr(e)),e2,e3);
|
462
|
1011 }
|
|
1012
|
|
1013 static int
|
|
1014 p_while(int e)
|
|
1015 {
|
524
|
1016 int e2 = pexpr(caddr(e));
|
|
1017 int e3 = pexpr(cadddr(e));
|
|
1018 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3);
|
462
|
1019 }
|
|
1020
|
|
1021 static int
|
|
1022 p_for(int e)
|
|
1023 {
|
501
|
1024 int e1=caddr(e);
|
524
|
1025 int p0=pexpr(car(e1));
|
|
1026 int p1=pexpr(cadr(e1));
|
|
1027 int p2=pexpr(caddr(e1));
|
|
1028 int p3=pexpr(cadddr(e1));
|
625
|
1029 // unfolding for constant case?
|
524
|
1030 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
|
462
|
1031 }
|
|
1032
|
|
1033 static int
|
|
1034 p_switch(int e)
|
|
1035 {
|
690
|
1036 int e2 = pexpr(caddr(e)); // switch variable
|
|
1037 int e3 = pexpr(cadddr(e)); // a statement in switch
|
625
|
1038 // if cadr(e) is a constant, we have to prune case statement.
|
690
|
1039 // No we cannot. A statement in case may contain labels or
|
|
1040 // falling down entry.
|
|
1041 // case constants are need to be pexpred in p_case.
|
|
1042
|
524
|
1043 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
|
462
|
1044 }
|
|
1045
|
|
1046 static int
|
|
1047 p_comp(int e)
|
|
1048 {
|
690
|
1049 int e1;
|
|
1050 e1=pexpr(caddr(e));
|
524
|
1051 return list3(ST_COMP,pexpr(cadr(e)),e1);
|
462
|
1052 }
|
|
1053
|
|
1054 static int
|
|
1055 p_break(int e)
|
|
1056 {
|
502
|
1057 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
1058 }
|
|
1059
|
|
1060 static int
|
|
1061 p_continue(int e)
|
|
1062 {
|
502
|
1063 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
1064 }
|
|
1065
|
|
1066 static int
|
|
1067 p_case(int e)
|
|
1068 {
|
506
|
1069 int new=0,clist = caddr(e);
|
|
1070 // insert destory clist, we have to copy it now
|
690
|
1071 // car(clist) have to be expred, it may contain operators.
|
506
|
1072 for(;clist;clist=cadr(clist))
|
690
|
1073 new=glist3(cexpr(pexpr(car(clist))),new,0);
|
506
|
1074 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new));
|
462
|
1075 }
|
|
1076
|
|
1077 static int
|
|
1078 p_default(int e)
|
|
1079 {
|
625
|
1080 // should be removed if case value is a constant
|
502
|
1081 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
1082 }
|
|
1083
|
|
1084 static int
|
|
1085 p_return(int e)
|
|
1086 {
|
524
|
1087 int e1=pexpr(caddr(e));
|
|
1088 return list3(ST_RETURN,pexpr(cadr(e)),e1);
|
462
|
1089 }
|
|
1090
|
|
1091 static int
|
|
1092 p_goto(int e)
|
|
1093 {
|
555
|
1094 int e1,lb,e2;
|
501
|
1095 if ((e1=caddr(e))) {
|
|
1096 switch(car(e1)) {
|
|
1097 case RINDIRECT: e1=pexpr(e1); break;
|
555
|
1098 case CODE: e2=pexpr(cadr(e1));
|
609
|
1099 e1=list3(JUMP,e2,pexpr(caddr(e1))); break;
|
551
|
1100 case FLABEL: /* error(-1); */ break;
|
|
1101 case IVAR:
|
|
1102 lb = cadr(e1);
|
|
1103 if (!(e1=heap[pdisp+lb])) {
|
|
1104 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
1105 }
|
691
|
1106 break;
|
|
1107 case JUMP:
|
|
1108 e1 = list3(JUMP,pexpr(cadr(e1)),pexpr(caddr(e1)));
|
|
1109 break;
|
|
1110 default:
|
|
1111 error(-1);
|
501
|
1112 }
|
|
1113 }
|
502
|
1114 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
1115 }
|
|
1116
|
|
1117 static int
|
|
1118 p_list_expr(int e)
|
|
1119 {
|
|
1120 int e3,new = 0;
|
|
1121 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
1122 new= list2( pexpr(car(e3)), new);
|
|
1123 }
|
|
1124 return reverse0(new);
|
462
|
1125 }
|
|
1126
|
|
1127 static int
|
|
1128 p_asm(int e)
|
|
1129 {
|
501
|
1130 int param=caddr(e);
|
|
1131 int e1 = p_list_expr(cadddr(e));
|
|
1132 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
1133 }
|
|
1134
|
|
1135 static int
|
|
1136 p_label(int e)
|
|
1137 {
|
551
|
1138 int e1,lb;
|
|
1139 if ((e1=caddr(e))) {
|
|
1140 switch(car(e1)) {
|
|
1141 case FLABEL: /* error(-1); */ break;
|
|
1142 case IVAR:
|
|
1143 lb = cadr(e1);
|
|
1144 if (!(e1=heap[pdisp+lb])) {
|
|
1145 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
1146 }
|
|
1147 }
|
|
1148 }
|
|
1149 return list3(ST_LABEL,pexpr(cadr(e)),e1);
|
|
1150 }
|
|
1151
|
|
1152 static int
|
|
1153 p_label_var(int e)
|
|
1154 {
|
|
1155 int d,e1;
|
|
1156 switch(car(e1=e)) {
|
|
1157 case LVAR: /* error(-1) */ break;
|
|
1158 case IVAR:
|
|
1159 // should this done in p_decl? (in __label__?)
|
|
1160 d = cadr(e);
|
|
1161 if (!(e1=(heap[pdisp+d]))) {
|
552
|
1162 // error(-1);
|
551
|
1163 e1 = heap[pdisp+d]=list3(LVAR,fwdlabel(),caddr(e));
|
|
1164 }
|
|
1165 }
|
|
1166 return list2(LABEL,e1);
|
462
|
1167 }
|
|
1168
|
|
1169 static int
|
|
1170 p_bool(int e)
|
|
1171 {
|
501
|
1172 error(-1);
|
464
|
1173 return e;
|
|
1174 }
|
462
|
1175
|
464
|
1176 static int
|
|
1177 p_comment(int e)
|
|
1178 {
|
574
|
1179 glineno++;
|
501
|
1180 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
1181 }
|
|
1182
|
508
|
1183 static int
|
512
|
1184 p_inline(int e)
|
508
|
1185 {
|
|
1186 int e3;
|
|
1187 int narg;
|
|
1188
|
|
1189 /* inline function arguments */
|
|
1190 narg = 0;
|
|
1191 for (e3 = caddr(e); e3; e3 = cadr(e3)) {
|
|
1192 narg=list3(pexpr(car(e3)),narg,caddr(e3));
|
|
1193 }
|
|
1194 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e));
|
|
1195 }
|
|
1196
|
463
|
1197 extern int
|
|
1198 pexpr(int e1)
|
462
|
1199 {
|
|
1200 int e2,e3;
|
|
1201
|
463
|
1202 // if (inmode) error(-1);
|
502
|
1203 if (e1==0) return 0;
|
462
|
1204 e2 = cadr(e1);
|
|
1205 switch (car(e1)){
|
|
1206 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
1207 case SURGVAR: case REGISTER:
|
|
1208 case DREGISTER: case FREGISTER:
|
500
|
1209 case FRGVAR: case DRGVAR:
|
462
|
1210 case LREGISTER:
|
500
|
1211 case LRGVAR: case LURGVAR:
|
551
|
1212 case CONST:
|
462
|
1213 case DCONST: case FCONST:
|
|
1214 case LCONST:
|
|
1215 case STRING:
|
|
1216 case FNAME:
|
503
|
1217 case FLABEL:
|
616
|
1218 case BUILTIN_INF:
|
|
1219 case BUILTIN_INFF:
|
|
1220 case BUILTIN_INFL:
|
462
|
1221 return e1;
|
551
|
1222 case RSTRUCT:
|
|
1223 // list3(RSTRUCT,e,size)
|
|
1224 return pexpr(e2);
|
|
1225 case LABEL:
|
|
1226 return p_label_var(e2);
|
462
|
1227 case LVAR:
|
|
1228 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
500
|
1229 case FRLVAR: case DRLVAR:
|
|
1230 case LRLVAR: case LURLVAR:
|
|
1231 return e1;
|
|
1232 case IVAR:
|
462
|
1233 return p_lvar(e1);
|
696
|
1234 case CODE:
|
462
|
1235 case FUNCTION:
|
|
1236 return pfunction(e1);
|
609
|
1237 case JUMP:
|
601
|
1238 e2 = pexpr(e2);
|
574
|
1239 return list3(car(e1),e2,pexpr(cadddr(e1)));
|
601
|
1240 case ARRAY:
|
|
1241 e2 = pexpr(e2);
|
|
1242 e1 = binop(ADD,e2,pexpr(caddr(e1)),cadddr(e1),caddddr(e1));
|
|
1243 return indop(e1);
|
|
1244 case PERIOD:
|
|
1245 e2 = pexpr(e2);
|
|
1246 nptr = (NMTBL*)caddr(e1);
|
|
1247 type = cadddr(e1);
|
|
1248 return strop(e2,0);
|
|
1249 case ARROW:
|
|
1250 e2 = pexpr(e2);
|
|
1251 nptr = (NMTBL*)caddr(e1);
|
|
1252 type = cadddr(e1);
|
|
1253 return strop(e2,1);
|
462
|
1254 case INLINE:
|
512
|
1255 return p_inline(e1);
|
462
|
1256 case INDIRECT:
|
527
|
1257 return pindirect(e1);
|
462
|
1258 case RINDIRECT: case URINDIRECT:
|
|
1259 case CRINDIRECT: case CURINDIRECT:
|
|
1260 case SRINDIRECT: case SURINDIRECT:
|
|
1261 case FRINDIRECT: case DRINDIRECT:
|
|
1262 case LRINDIRECT: case LURINDIRECT:
|
|
1263 return prindirect(e1);
|
|
1264 case ADDRESS:
|
502
|
1265 return paddress(e1);
|
566
|
1266 case ST_OP:
|
|
1267 e3=caddr(e1);
|
|
1268 e1=pexpr(car(e3));
|
705
|
1269 return binop0(e2,e1,pexpr(cadr(e3)),caddr(e3),cadddr(e3));
|
462
|
1270 case MINUS:
|
463
|
1271 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1272 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
1273 return list2(car(e1),e3);
|
|
1274 #if LONGLONG_CODE
|
|
1275 case LMINUS:
|
463
|
1276 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1277 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
1278 return list2(car(e1),e3);
|
|
1279 #endif
|
|
1280 #if FLOAT_CODE
|
|
1281 case DMINUS:
|
463
|
1282 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1283 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
1284 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
1285 return list2(car(e1),e3);
|
|
1286 case FMINUS:
|
463
|
1287 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1288 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
1289 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
1290 return list2(car(e1),e3);
|
|
1291 #endif
|
|
1292 case CONV:
|
501
|
1293 return p_conv(caddr(e1),e2);
|
462
|
1294 case BNOT: /* ~ */
|
463
|
1295 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1296 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
1297 return list2(BNOT,e3);
|
|
1298 case LNOT: /* ! */
|
463
|
1299 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1300 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
1301 return list2(LNOT,e3);
|
|
1302 case PREINC:
|
|
1303 case UPREINC:
|
463
|
1304 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
1305 if (car(e3)==CONST) return list2(CONST,cadr(e3)+caddr(e1));
|
503
|
1306 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1307 case POSTINC:
|
|
1308 case UPOSTINC:
|
463
|
1309 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1310 if (car(e3)==CONST) return e3;
|
503
|
1311 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1312 #if FLOAT_CODE
|
|
1313 case DPREINC: /* ++d */
|
463
|
1314 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1315 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1316 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1317 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1318 case DPOSTINC: /* d++ */
|
463
|
1319 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1320 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1321 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1322 case FPREINC: /* ++f */
|
463
|
1323 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1324 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1325 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1326 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1327 case FPOSTINC: /* f++ */
|
463
|
1328 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1329 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1330 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1331 #endif
|
|
1332 #if LONGLONG_CODE
|
|
1333 case LPREINC: /* ++d */
|
|
1334 case LUPREINC: /* ++d */
|
463
|
1335 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1336 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
1337 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1338 case LPOSTINC: /* d++ */
|
|
1339 case LUPOSTINC: /* d++ */
|
463
|
1340 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1341 if (car(e3)==LCONST) return e3;
|
503
|
1342 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1343 #endif
|
|
1344 case MUL: case UMUL:
|
|
1345 case DIV: case UDIV:
|
|
1346 case MOD: case UMOD:
|
|
1347 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
1348 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
1349 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
1350 #if FLOAT_CODE
|
|
1351 case DMUL: case DDIV:
|
|
1352 case DADD: case DSUB:
|
|
1353 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
1354 case FMUL: case FDIV:
|
|
1355 case FADD: case FSUB:
|
|
1356 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
1357 #endif
|
|
1358 #if LONGLONG_CODE
|
|
1359 case LMUL: case LUMUL:
|
|
1360 case LDIV: case LUDIV:
|
|
1361 case LMOD: case LUMOD:
|
|
1362 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
1363 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
1364 #endif
|
501
|
1365 return pbinop(car(e1),e2,caddr(e1));
|
557
|
1366 // relational operator
|
|
1367 case GT: case UGT: case GE: case UGE: case LT:
|
|
1368 case ULT: case LE: case ULE:
|
|
1369 case LOP+GT: case LOP+UGT: case LOP+GE: case LOP+UGE: case LOP+LT:
|
|
1370 case LOP+ULT: case LOP+LE: case LOP+ULE:
|
|
1371 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE:
|
|
1372 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE:
|
|
1373 case FOP+EQ: case FOP+NEQ:
|
|
1374 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ:
|
|
1375 case LOP+EQ: case LOP+NEQ:
|
|
1376 return pbinop(car(e1),e2,caddr(e1));
|
|
1377 case LAND:
|
|
1378 return pland(car(e1),cadr(e1),caddr(e1));
|
|
1379 case LOR:
|
|
1380 return plor(car(e1),cadr(e1),caddr(e1));
|
528
|
1381 case LCOND: case DCOND: case FCOND: case COND: case UCOND: case LUCOND:
|
555
|
1382 e2 = pexpr(e2);
|
|
1383 if (car(e2)==CONST) return
|
|
1384 caddr(e1)? pexpr(cadr(e2)?caddr(e1):cadddr(e1)) :
|
|
1385 pexpr(cadr(e2)?e2:cadddr(e1)); // GNU extension h?:g
|
|
1386 e3=pexpr(caddr(e1));
|
|
1387 return list4(car(e1),e2,e3,pexpr(cadddr(e1)));
|
462
|
1388 case STASS:
|
|
1389 return psassign(e1);
|
|
1390 case ASS: case CASS: case SASS:
|
|
1391 return passign(e1);
|
|
1392 case SASSOP: case SUASSOP:
|
|
1393 case ASSOP: case CASSOP: case CUASSOP:
|
|
1394 return passop(e1);
|
|
1395 #if FLOAT_CODE
|
|
1396 case FASS: case DASS:
|
|
1397 return pdassign(e1);
|
|
1398 case DASSOP: case FASSOP:
|
|
1399 return pdassop(e1);
|
616
|
1400
|
|
1401 case BUILTIN_FABS:
|
|
1402 case BUILTIN_FABSF:
|
|
1403 case BUILTIN_FABSL:
|
|
1404 e2 = pexpr(e2);
|
|
1405 if (is_const(e2)) {
|
|
1406 if (dcadr(e2) >= 0.0 ) return e2;
|
|
1407 return pexpr(list2(car(e1)==BUILTIN_FABSF?
|
|
1408 FMINUS:DMINUS,e2));
|
|
1409 }
|
|
1410 return list2(car(e1),e2);
|
462
|
1411 #endif
|
|
1412 #if LONGLONG_CODE
|
|
1413 case LASS:
|
|
1414 return plassign(e1);
|
|
1415 case LASSOP: case LUASSOP:
|
|
1416 return plassop(e1);
|
|
1417 #endif
|
|
1418 case ALLOCA:
|
501
|
1419 return palloc(e2);
|
462
|
1420 case BUILTINP:
|
463
|
1421 return list2(CONST,is_const(pexpr(e2)));
|
462
|
1422 case COMMA:
|
555
|
1423 return pcomma(e2,caddr(e1));
|
462
|
1424 case RETURN:
|
|
1425 case ENVIRONMENT:
|
|
1426 case LCALL:
|
|
1427 return e1;
|
|
1428 #if BIT_FIELD_CODE
|
|
1429 case RBIT_FIELD:
|
|
1430 return prbit_field(e1);
|
553
|
1431 case BIT_FIELD:
|
|
1432 return list3(BIT_FIELD,pexpr(e2),caddr(e1));
|
462
|
1433 case BASS:
|
|
1434 return pbassign(e1);
|
|
1435 case BPREINC:
|
|
1436 case BPOSTINC:
|
|
1437 case BASSOP:
|
|
1438 return pbassop(e1);
|
|
1439 #endif
|
|
1440 #if ASM_CODE
|
|
1441 case ASM:
|
|
1442 return list3(ASM,list4(
|
501
|
1443 car(e2),cadr(e2),caddr(e2),cadddr(e2)),
|
|
1444 caddr(e1));
|
462
|
1445 #endif
|
681
|
1446 case CAST:
|
692
|
1447 if (e2==0) {
|
|
1448 // casted empty structure (struct hoge){}
|
|
1449 // I think we can skip it. It means nothing in declaration
|
|
1450 return 0;
|
|
1451 } else if (car(e2)==DECL_DATA) {
|
681
|
1452 // casted initialized structure (struct hoge){...}
|
|
1453 return list3(DECL_DATA,pexpr(cadr(e2)),caddr(e2));
|
|
1454 }
|
|
1455 if (type_compatible(caddr(e1),cadddr(e1))) {
|
690
|
1456 return pexpr(e2); // rvalue ?
|
687
|
1457 } else {
|
690
|
1458 e2 = pexpr(e2); // will override type
|
687
|
1459 type = cadddr(e1);
|
690
|
1460 return correct_type(e2,caddr(e1));
|
687
|
1461 // return list4(CAST,pexpr(e2),caddr(e1),cadddr(e1));
|
|
1462 }
|
681
|
1463 case DECL_DATA:
|
|
1464 return list3(DECL_DATA,pexpr(e2),caddr(e1));
|
|
1465 case DECL_DATA_LIST:
|
697
|
1466 case DECL_DATA_ARRAY:
|
681
|
1467 case DECL_DATA_FIELD:
|
703
|
1468 return list4(car(e1),pexpr(e2),pexpr(caddr(e1)),cadddr(e1));
|
462
|
1469 case ST_DECL: return p_decl(e1);
|
|
1470 case ST_IF: return p_if(e1);
|
|
1471 case ST_DO: return p_do(e1);
|
|
1472 case ST_WHILE: return p_while(e1);
|
|
1473 case ST_FOR: return p_for(e1);
|
|
1474 case ST_SWITCH: return p_switch(e1);
|
|
1475 case ST_COMP: return p_comp(e1);
|
|
1476 case ST_BREAK: return p_break(e1);
|
|
1477 case ST_CONTINUE: return p_continue(e1);
|
|
1478 case ST_CASE: return p_case(e1);
|
|
1479 case ST_DEFAULT: return p_default(e1);
|
|
1480 case ST_RETURN: return p_return(e1);
|
|
1481 case ST_GOTO: return p_goto(e1);
|
|
1482 case ST_ASM: return p_asm(e1);
|
|
1483 case ST_LABEL: return p_label(e1);
|
|
1484 case ST_COMMENT: return p_comment(e1);
|
|
1485 default:
|
551
|
1486 error(-1);
|
462
|
1487 return p_bool(e1);
|
|
1488 }
|
|
1489 return VOID;
|
|
1490 }
|
|
1491
|
625
|
1492 /*
|
|
1493 Prepare pvariable table
|
|
1494 */
|
|
1495
|
557
|
1496 static int
|
|
1497 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals)
|
|
1498 {
|
|
1499 int arg;
|
|
1500 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) {
|
625
|
1501 // replacable const variable
|
557
|
1502 if (is_memory(e4)) {
|
|
1503 heap[pdisp+narg]=reference(e4);
|
|
1504 return evals;
|
|
1505 } else if (is_const(e4)) {
|
|
1506 heap[pdisp+narg]=e4;
|
|
1507 return evals;
|
|
1508 }
|
|
1509 }
|
625
|
1510 // we need real local variable for this inline
|
709
|
1511 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),(int)anptr);
|
557
|
1512 inline_lvars = glist2(arg,inline_lvars);
|
|
1513 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals);
|
|
1514 return evals;
|
|
1515 }
|
|
1516
|
|
1517 /*
|
|
1518 setup parameter replacement of inline call
|
|
1519 */
|
|
1520
|
|
1521 static void
|
696
|
1522 enter_inline(NMTBL *n, int e,int toplevel)
|
557
|
1523 {
|
|
1524 int e1 = attr_value(n,INLINE);
|
|
1525 int arg_disp = cadr(e1); // number of arguments
|
|
1526 int narg,e3,e4, e5, fargtype;
|
|
1527 int evals = 0;
|
696
|
1528 int t, replace;
|
557
|
1529 NMTBL *anptr;
|
|
1530
|
|
1531 fnptr = n; // st_return see this
|
|
1532 pvartable = p_vartable(arg_disp, /* number of arg */
|
|
1533 caddr(e1) /* number of local parameter */);
|
|
1534
|
704
|
1535 replace = (is_code(fnptr)||toplevel);
|
696
|
1536
|
557
|
1537 /* function arguments type */
|
|
1538 fargtype = n->dsp; // we cannot do destruct reverse here
|
463
|
1539
|
557
|
1540 /* inline function arguments */
|
|
1541 narg = 0;
|
|
1542 if (!fargtype) {
|
|
1543 goto no_args; // wrong number of arguments
|
|
1544 }
|
699
|
1545
|
557
|
1546 for (e3 = e5 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
|
1547 anptr = (NMTBL*)caddr(fargtype);
|
|
1548 if (!anptr) break; // should not happen?
|
|
1549 t=caddr(e3); // type
|
|
1550 e4 = car(e3);
|
696
|
1551 if (replace) {
|
|
1552 heap[pdisp+narg]=reference(e4);
|
|
1553 } else {
|
|
1554 evals = replace_inline_parameter(anptr,t,e4,narg,evals);
|
|
1555 }
|
557
|
1556 narg ++;
|
|
1557 fargtype = cadr(fargtype);
|
|
1558 }
|
|
1559 caddr(e) = reverse0(e5); // make it normal
|
|
1560 if (eval_order==NORMAL) evals = reverse0(evals);
|
|
1561 for(;evals;evals=cadr(evals)) {
|
|
1562 g_expr_u(car(evals));
|
|
1563 }
|
|
1564 no_args:
|
|
1565 return;
|
|
1566 }
|
|
1567
|
|
1568 /*
|
|
1569 pass local static variable list to our parents
|
|
1570 clean up and free used inline parameters
|
|
1571 */
|
|
1572
|
|
1573 static void
|
696
|
1574 leave_inline(int e1,int toplevel)
|
557
|
1575 {
|
|
1576 NMTBL *n;
|
|
1577 NMTBL *local_statics = (NMTBL*)cadddr(e1); // local static list
|
|
1578
|
696
|
1579 if (retcont && !toplevel) error(STERR);
|
|
1580 // inline can't handle return/environment except top level
|
557
|
1581
|
|
1582 if (local_statics && local_statics != &null_nptr) {
|
|
1583 // append our local static variables to the parent list
|
|
1584 n = local_statics;
|
|
1585 while(n->next != &null_nptr) n=n->next;
|
|
1586 n->next = local_static_list; local_static_list = local_statics;
|
|
1587 cadddr(e1) = 0; // prevent duplicate initialize
|
|
1588 }
|
625
|
1589
|
|
1590 // free used local variables or registers
|
557
|
1591 while(inline_lvars) {
|
|
1592 int e;
|
|
1593 int l = car(inline_lvars);
|
|
1594 switch(car(l)) {
|
|
1595 case LVAR: free_lvar(cadr(l)); break;
|
|
1596 case REGISTER: case DREGISTER:
|
|
1597 case FREGISTER: case LREGISTER:
|
|
1598 free_register(cadr(l));
|
|
1599 }
|
|
1600 e = cadr(inline_lvars);
|
|
1601 free_glist2(inline_lvars);
|
|
1602 inline_lvars = e;
|
|
1603 }
|
|
1604 }
|
530
|
1605
|
463
|
1606 extern int
|
696
|
1607 gen_inline(int e, int toplevel)
|
462
|
1608 {
|
514
|
1609 // these saved value should be some struct
|
|
1610 int sretlabel;
|
|
1611 NMTBL *sfnptr;
|
552
|
1612 int svartable;
|
696
|
1613
|
513
|
1614 int scslabel = cslabel;
|
463
|
1615 int sdisp = pdisp;
|
552
|
1616 int sret_register;
|
|
1617 int sret_reg_mode;
|
|
1618 int sinline_lvars;
|
|
1619 int slfree;
|
531
|
1620 // int slreg_count=lreg_count;
|
513
|
1621
|
463
|
1622 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
1623 int e1 = attr_value(n,INLINE);
|
500
|
1624 int parse = car(e1); // inline parse tree
|
557
|
1625 int dots;
|
508
|
1626 int ret_type = function_type(cadddr(e),&dots);
|
463
|
1627
|
552
|
1628 checkret();
|
|
1629 checkjmp(-1);
|
|
1630
|
|
1631 svartable = pvartable;
|
696
|
1632
|
552
|
1633 scslabel = cslabel;
|
|
1634 sdisp = pdisp;
|
|
1635 sret_register = ret_register;
|
|
1636 sret_reg_mode = ret_reg_mode;
|
|
1637 sinline_lvars = inline_lvars;
|
|
1638 slfree=lfree;
|
|
1639 // int slreg_count=lreg_count;
|
514
|
1640
|
|
1641 sretlabel = retlabel;
|
|
1642 sfnptr = fnptr;
|
696
|
1643
|
513
|
1644 cslabel = -1;
|
|
1645 retpending = 0;
|
527
|
1646 inline_lvars = 0;
|
705
|
1647 if (!toplevel) {
|
|
1648 retlabel = fwdlabel();
|
|
1649 }
|
513
|
1650
|
696
|
1651 enter_inline(n,e,toplevel);
|
526
|
1652
|
704
|
1653 if (toplevel) {
|
705
|
1654 ret_register = code_set_return_register(0); // fnptr required
|
704
|
1655 ret_reg_mode = 1;
|
|
1656 } else {
|
705
|
1657 ret_register = 555;
|
704
|
1658 ret_reg_mode = 0;
|
|
1659 }
|
|
1660
|
557
|
1661 // partial evaluation of parse tree
|
|
1662 // constant propergation, etc.
|
|
1663 parse = pexpr(parse);
|
463
|
1664 pdisp = sdisp;
|
464
|
1665 pvartable = svartable;
|
513
|
1666
|
557
|
1667 // generate code if necessary
|
513
|
1668 if (ret_type!=VOID)
|
557
|
1669 g_expr0(parse);
|
513
|
1670 else
|
557
|
1671 g_expr_u(parse);
|
|
1672
|
705
|
1673 if (!toplevel) {
|
|
1674 checkret();
|
|
1675 fwddef(retlabel);
|
|
1676 control=1;
|
|
1677 }
|
513
|
1678
|
696
|
1679 leave_inline(e1,toplevel);
|
510
|
1680
|
513
|
1681 fnptr = sfnptr;
|
|
1682 retlabel = sretlabel;
|
696
|
1683
|
513
|
1684 cslabel = scslabel;
|
514
|
1685 ret_register = sret_register;
|
|
1686 ret_reg_mode = sret_reg_mode;
|
527
|
1687 inline_lvars = sinline_lvars;
|
528
|
1688 lfree=slfree;
|
513
|
1689
|
508
|
1690 return ret_type;
|
462
|
1691 }
|
|
1692
|
|
1693 /* end */
|