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