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