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