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