Mercurial > hg > CbC > old > device
annotate mc-inline.c @ 805:362b0258b4db
fix func_disp_offset in PS3
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 23 Nov 2010 23:08:56 +0900 |
parents | 6b93d95a1564 |
children | c59753132812 |
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); |
801 | 996 else { |
997 NMTBL tn; | |
998 tn = *n; | |
999 code_lvar_alignment(disp,&tn,n->ty,size(n->ty)); | |
1000 v = list3n(LVAR,tn.dsp,n); | |
1001 } | |
463 | 1002 } |
552 | 1003 if (n->sc!=FLABEL) |
1004 inline_lvars = glist2(v,inline_lvars); | |
574 | 1005 if (heap[pdisp+dsp]) { |
712 | 1006 fprintf(stderr,"## double p_decl %s %s\n",(ncaddr(heap[pdisp+dsp]))->nm,n->nm); |
574 | 1007 error(-1); |
1008 } | |
463 | 1009 heap[pdisp+dsp]=v; |
552 | 1010 stmode = sstmode; |
1011 mode = smode; | |
681 | 1012 if (init) { |
1013 pdecl_data(v,n->ty,init,0); | |
1014 if (init_vars) { | |
1015 int e1 = pexpr(cadr(e)); | |
1016 init_vars = reverse0(init_vars); | |
1017 while (init_vars) { | |
1018 e1 = list3(ST_COMP,e1,car(init_vars)); | |
1019 init_vars = cadr(init_vars); | |
1020 } | |
1021 init_vars = save_init_vars; | |
1022 return e1; | |
1023 } | |
1024 } | |
1025 init_vars = save_init_vars; | |
463 | 1026 return pexpr(cadr(e)); |
462 | 1027 } |
1028 | |
1029 static int | |
500 | 1030 p_if(int e1) |
462 | 1031 { |
500 | 1032 int cond,l1,l2; |
1033 int e2=caddr(e1),e3; | |
1034 cond = pexpr(car(e2)); | |
1035 // conv->if_then_(); | |
1036 l1 = pexpr(cadr(e2)); | |
1037 if ((e3=caddr(e2))) { // else | |
1038 l2 = pexpr(e3); | |
1039 } else { | |
1040 l2 = 0; | |
1041 } | |
1042 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2)); | |
462 | 1043 } |
1044 | |
1045 static int | |
1046 p_do(int e) | |
1047 { | |
524 | 1048 int e2 = pexpr(caddr(e)); |
1049 int e3 = pexpr(cadddr(e)); | |
1050 return list4(ST_DO,pexpr(cadr(e)),e2,e3); | |
462 | 1051 } |
1052 | |
1053 static int | |
1054 p_while(int e) | |
1055 { | |
524 | 1056 int e2 = pexpr(caddr(e)); |
1057 int e3 = pexpr(cadddr(e)); | |
1058 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3); | |
462 | 1059 } |
1060 | |
1061 static int | |
1062 p_for(int e) | |
1063 { | |
501 | 1064 int e1=caddr(e); |
524 | 1065 int p0=pexpr(car(e1)); |
1066 int p1=pexpr(cadr(e1)); | |
1067 int p2=pexpr(caddr(e1)); | |
1068 int p3=pexpr(cadddr(e1)); | |
625 | 1069 // unfolding for constant case? |
524 | 1070 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3)); |
462 | 1071 } |
1072 | |
1073 static int | |
1074 p_switch(int e) | |
1075 { | |
690 | 1076 int e2 = pexpr(caddr(e)); // switch variable |
1077 int e3 = pexpr(cadddr(e)); // a statement in switch | |
625 | 1078 // if cadr(e) is a constant, we have to prune case statement. |
690 | 1079 // No we cannot. A statement in case may contain labels or |
1080 // falling down entry. | |
1081 // case constants are need to be pexpred in p_case. | |
1082 | |
524 | 1083 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3); |
462 | 1084 } |
1085 | |
1086 static int | |
1087 p_comp(int e) | |
1088 { | |
690 | 1089 int e1; |
1090 e1=pexpr(caddr(e)); | |
524 | 1091 return list3(ST_COMP,pexpr(cadr(e)),e1); |
462 | 1092 } |
1093 | |
1094 static int | |
1095 p_break(int e) | |
1096 { | |
502 | 1097 return list2(ST_BREAK,pexpr(cadr(e))); |
462 | 1098 } |
1099 | |
1100 static int | |
1101 p_continue(int e) | |
1102 { | |
502 | 1103 return list2(ST_CONTINUE,pexpr(cadr(e))); |
462 | 1104 } |
1105 | |
1106 static int | |
1107 p_case(int e) | |
1108 { | |
506 | 1109 int new=0,clist = caddr(e); |
1110 // insert destory clist, we have to copy it now | |
690 | 1111 // car(clist) have to be expred, it may contain operators. |
506 | 1112 for(;clist;clist=cadr(clist)) |
713 | 1113 // new=glist3(cexpr(pexpr(car(clist))),new,0); |
1114 new=glist3(car(clist),new,0); | |
506 | 1115 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new)); |
462 | 1116 } |
1117 | |
1118 static int | |
1119 p_default(int e) | |
1120 { | |
625 | 1121 // should be removed if case value is a constant |
502 | 1122 return list2(ST_DEFAULT,pexpr(cadr(e))); |
462 | 1123 } |
1124 | |
1125 static int | |
1126 p_return(int e) | |
1127 { | |
524 | 1128 int e1=pexpr(caddr(e)); |
1129 return list3(ST_RETURN,pexpr(cadr(e)),e1); | |
462 | 1130 } |
1131 | |
1132 static int | |
1133 p_goto(int e) | |
1134 { | |
555 | 1135 int e1,lb,e2; |
501 | 1136 if ((e1=caddr(e))) { |
1137 switch(car(e1)) { | |
1138 case RINDIRECT: e1=pexpr(e1); break; | |
555 | 1139 case CODE: e2=pexpr(cadr(e1)); |
609 | 1140 e1=list3(JUMP,e2,pexpr(caddr(e1))); break; |
551 | 1141 case FLABEL: /* error(-1); */ break; |
1142 case IVAR: | |
1143 lb = cadr(e1); | |
1144 if (!(e1=heap[pdisp+lb])) { | |
1145 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel()); | |
1146 } | |
691 | 1147 break; |
1148 case JUMP: | |
1149 e1 = list3(JUMP,pexpr(cadr(e1)),pexpr(caddr(e1))); | |
1150 break; | |
1151 default: | |
1152 error(-1); | |
501 | 1153 } |
1154 } | |
502 | 1155 return list3(ST_GOTO,pexpr(cadr(e)),e1); |
501 | 1156 } |
1157 | |
1158 static int | |
1159 p_list_expr(int e) | |
1160 { | |
1161 int e3,new = 0; | |
1162 for (e3 = e; e3; e3 = cadr(e3)) { | |
1163 new= list2( pexpr(car(e3)), new); | |
1164 } | |
1165 return reverse0(new); | |
462 | 1166 } |
1167 | |
1168 static int | |
1169 p_asm(int e) | |
1170 { | |
501 | 1171 int param=caddr(e); |
1172 int e1 = p_list_expr(cadddr(e)); | |
1173 return list4(ST_ASM,pexpr(cadr(e)),param,e1); | |
462 | 1174 } |
1175 | |
1176 static int | |
1177 p_label(int e) | |
1178 { | |
551 | 1179 int e1,lb; |
1180 if ((e1=caddr(e))) { | |
1181 switch(car(e1)) { | |
1182 case FLABEL: /* error(-1); */ break; | |
1183 case IVAR: | |
1184 lb = cadr(e1); | |
1185 if (!(e1=heap[pdisp+lb])) { | |
1186 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel()); | |
1187 } | |
1188 } | |
1189 } | |
1190 return list3(ST_LABEL,pexpr(cadr(e)),e1); | |
1191 } | |
1192 | |
1193 static int | |
1194 p_label_var(int e) | |
1195 { | |
1196 int d,e1; | |
1197 switch(car(e1=e)) { | |
1198 case LVAR: /* error(-1) */ break; | |
1199 case IVAR: | |
1200 // should this done in p_decl? (in __label__?) | |
1201 d = cadr(e); | |
1202 if (!(e1=(heap[pdisp+d]))) { | |
552 | 1203 // error(-1); |
551 | 1204 e1 = heap[pdisp+d]=list3(LVAR,fwdlabel(),caddr(e)); |
1205 } | |
1206 } | |
1207 return list2(LABEL,e1); | |
462 | 1208 } |
1209 | |
1210 static int | |
1211 p_bool(int e) | |
1212 { | |
501 | 1213 error(-1); |
464 | 1214 return e; |
1215 } | |
462 | 1216 |
464 | 1217 static int |
1218 p_comment(int e) | |
1219 { | |
574 | 1220 glineno++; |
713 | 1221 plineno = caddr(e); |
1222 return list4n(ST_COMMENT,pexpr(cadr(e)),caddr(e),ncadddr(e)); | |
462 | 1223 } |
1224 | |
508 | 1225 static int |
512 | 1226 p_inline(int e) |
508 | 1227 { |
1228 int e3; | |
1229 int narg; | |
1230 | |
1231 /* inline function arguments */ | |
1232 narg = 0; | |
1233 for (e3 = caddr(e); e3; e3 = cadr(e3)) { | |
1234 narg=list3(pexpr(car(e3)),narg,caddr(e3)); | |
1235 } | |
1236 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e)); | |
1237 } | |
1238 | |
463 | 1239 extern int |
1240 pexpr(int e1) | |
462 | 1241 { |
1242 int e2,e3; | |
1243 | |
463 | 1244 // if (inmode) error(-1); |
502 | 1245 if (e1==0) return 0; |
462 | 1246 e2 = cadr(e1); |
1247 switch (car(e1)){ | |
1248 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR: | |
788 | 1249 case URGVAR: |
462 | 1250 case SURGVAR: case REGISTER: |
1251 case DREGISTER: case FREGISTER: | |
500 | 1252 case FRGVAR: case DRGVAR: |
462 | 1253 case LREGISTER: |
500 | 1254 case LRGVAR: case LURGVAR: |
551 | 1255 case CONST: |
462 | 1256 case DCONST: case FCONST: |
1257 case LCONST: | |
1258 case STRING: | |
778
a177c65f3e37
large string (STRINGS)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
770
diff
changeset
|
1259 case STRINGS: |
462 | 1260 case FNAME: |
503 | 1261 case FLABEL: |
616 | 1262 case BUILTIN_INF: |
1263 case BUILTIN_INFF: | |
1264 case BUILTIN_INFL: | |
462 | 1265 return e1; |
551 | 1266 case RSTRUCT: |
1267 // list3(RSTRUCT,e,size) | |
1268 return pexpr(e2); | |
1269 case LABEL: | |
1270 return p_label_var(e2); | |
462 | 1271 case LVAR: |
1272 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR: | |
500 | 1273 case FRLVAR: case DRLVAR: |
1274 case LRLVAR: case LURLVAR: | |
1275 return e1; | |
1276 case IVAR: | |
462 | 1277 return p_lvar(e1); |
696 | 1278 case CODE: |
462 | 1279 case FUNCTION: |
1280 return pfunction(e1); | |
609 | 1281 case JUMP: |
601 | 1282 e2 = pexpr(e2); |
574 | 1283 return list3(car(e1),e2,pexpr(cadddr(e1))); |
601 | 1284 case ARRAY: |
1285 e2 = pexpr(e2); | |
1286 e1 = binop(ADD,e2,pexpr(caddr(e1)),cadddr(e1),caddddr(e1)); | |
1287 return indop(e1); | |
1288 case PERIOD: | |
1289 e2 = pexpr(e2); | |
711 | 1290 nptr = ncadddr(e1); |
1291 type = caddr(e1); | |
601 | 1292 return strop(e2,0); |
1293 case ARROW: | |
1294 e2 = pexpr(e2); | |
711 | 1295 nptr = ncadddr(e1); |
1296 type = caddr(e1); | |
601 | 1297 return strop(e2,1); |
462 | 1298 case INLINE: |
512 | 1299 return p_inline(e1); |
462 | 1300 case INDIRECT: |
527 | 1301 return pindirect(e1); |
462 | 1302 case RINDIRECT: case URINDIRECT: |
1303 case CRINDIRECT: case CURINDIRECT: | |
1304 case SRINDIRECT: case SURINDIRECT: | |
1305 case FRINDIRECT: case DRINDIRECT: | |
1306 case LRINDIRECT: case LURINDIRECT: | |
1307 return prindirect(e1); | |
1308 case ADDRESS: | |
502 | 1309 return paddress(e1); |
566 | 1310 case ST_OP: |
1311 e3=caddr(e1); | |
1312 e1=pexpr(car(e3)); | |
705 | 1313 return binop0(e2,e1,pexpr(cadr(e3)),caddr(e3),cadddr(e3)); |
462 | 1314 case MINUS: |
463 | 1315 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1316 if (car(e3)==CONST) return list2(CONST,-cadr(e3)); |
1317 return list2(car(e1),e3); | |
1318 #if LONGLONG_CODE | |
1319 case LMINUS: | |
463 | 1320 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1321 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3)); |
1322 return list2(car(e1),e3); | |
1323 #endif | |
1324 #if FLOAT_CODE | |
1325 case DMINUS: | |
463 | 1326 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1327 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3)); |
1328 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3)); | |
1329 return list2(car(e1),e3); | |
1330 case FMINUS: | |
463 | 1331 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1332 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3)); |
1333 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3)); | |
1334 return list2(car(e1),e3); | |
1335 #endif | |
1336 case CONV: | |
501 | 1337 return p_conv(caddr(e1),e2); |
462 | 1338 case BNOT: /* ~ */ |
463 | 1339 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1340 if (car(e3)==CONST) return list2(CONST,~cadr(e3)); |
1341 return list2(BNOT,e3); | |
1342 case LNOT: /* ! */ | |
463 | 1343 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1344 if (car(e3)==CONST) return list2(CONST,!cadr(e3)); |
1345 return list2(LNOT,e3); | |
1346 case PREINC: | |
1347 case UPREINC: | |
463 | 1348 if ((e3 = pexpr(e2))==e2) return e1; |
557 | 1349 if (car(e3)==CONST) return list2(CONST,cadr(e3)+caddr(e1)); |
503 | 1350 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1351 case POSTINC: |
1352 case UPOSTINC: | |
463 | 1353 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1354 if (car(e3)==CONST) return e3; |
503 | 1355 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1356 #if FLOAT_CODE |
1357 case DPREINC: /* ++d */ | |
463 | 1358 if ((e3 = pexpr(e2))==e2) return e1; |
1359 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2)); | |
1360 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2)); | |
503 | 1361 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1362 case DPOSTINC: /* d++ */ |
463 | 1363 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1364 if (car(e3)==FCONST||car(e3)==DCONST) return e3; |
503 | 1365 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1366 case FPREINC: /* ++f */ |
463 | 1367 if ((e3 = pexpr(e2))==e2) return e1; |
1368 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2)); | |
1369 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2)); | |
503 | 1370 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1371 case FPOSTINC: /* f++ */ |
463 | 1372 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1373 if (car(e3)==FCONST||car(e3)==DCONST) return e3; |
503 | 1374 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1375 #endif |
1376 #if LONGLONG_CODE | |
1377 case LPREINC: /* ++d */ | |
1378 case LUPREINC: /* ++d */ | |
463 | 1379 if ((e3 = pexpr(e2))==e2) return e1; |
1380 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2)); | |
503 | 1381 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1382 case LPOSTINC: /* d++ */ |
1383 case LUPOSTINC: /* d++ */ | |
463 | 1384 if ((e3 = pexpr(e2))==e2) return e1; |
462 | 1385 if (car(e3)==LCONST) return e3; |
503 | 1386 return list4(car(e1),e3,caddr(e1),cadddr(e1)); |
462 | 1387 #endif |
1388 case MUL: case UMUL: | |
1389 case DIV: case UDIV: | |
1390 case MOD: case UMOD: | |
1391 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT: | |
1392 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE: | |
1393 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE: | |
1394 #if FLOAT_CODE | |
1395 case DMUL: case DDIV: | |
1396 case DADD: case DSUB: | |
1397 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ: | |
1398 case FMUL: case FDIV: | |
1399 case FADD: case FSUB: | |
1400 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ: | |
1401 #endif | |
1402 #if LONGLONG_CODE | |
1403 case LMUL: case LUMUL: | |
1404 case LDIV: case LUDIV: | |
1405 case LMOD: case LUMOD: | |
1406 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT: | |
1407 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP: | |
1408 #endif | |
501 | 1409 return pbinop(car(e1),e2,caddr(e1)); |
557 | 1410 // relational operator |
1411 case GT: case UGT: case GE: case UGE: case LT: | |
1412 case ULT: case LE: case ULE: | |
1413 case LOP+GT: case LOP+UGT: case LOP+GE: case LOP+UGE: case LOP+LT: | |
1414 case LOP+ULT: case LOP+LE: case LOP+ULE: | |
1415 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE: | |
1416 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE: | |
1417 case FOP+EQ: case FOP+NEQ: | |
1418 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ: | |
1419 case LOP+EQ: case LOP+NEQ: | |
1420 return pbinop(car(e1),e2,caddr(e1)); | |
1421 case LAND: | |
1422 return pland(car(e1),cadr(e1),caddr(e1)); | |
1423 case LOR: | |
1424 return plor(car(e1),cadr(e1),caddr(e1)); | |
528 | 1425 case LCOND: case DCOND: case FCOND: case COND: case UCOND: case LUCOND: |
555 | 1426 e2 = pexpr(e2); |
1427 if (car(e2)==CONST) return | |
1428 caddr(e1)? pexpr(cadr(e2)?caddr(e1):cadddr(e1)) : | |
1429 pexpr(cadr(e2)?e2:cadddr(e1)); // GNU extension h?:g | |
1430 e3=pexpr(caddr(e1)); | |
1431 return list4(car(e1),e2,e3,pexpr(cadddr(e1))); | |
462 | 1432 case STASS: |
1433 return psassign(e1); | |
1434 case ASS: case CASS: case SASS: | |
1435 return passign(e1); | |
1436 case SASSOP: case SUASSOP: | |
1437 case ASSOP: case CASSOP: case CUASSOP: | |
1438 return passop(e1); | |
1439 #if FLOAT_CODE | |
1440 case FASS: case DASS: | |
1441 return pdassign(e1); | |
1442 case DASSOP: case FASSOP: | |
1443 return pdassop(e1); | |
616 | 1444 |
1445 case BUILTIN_FABS: | |
1446 case BUILTIN_FABSF: | |
1447 case BUILTIN_FABSL: | |
1448 e2 = pexpr(e2); | |
1449 if (is_const(e2)) { | |
1450 if (dcadr(e2) >= 0.0 ) return e2; | |
1451 return pexpr(list2(car(e1)==BUILTIN_FABSF? | |
1452 FMINUS:DMINUS,e2)); | |
1453 } | |
1454 return list2(car(e1),e2); | |
462 | 1455 #endif |
1456 #if LONGLONG_CODE | |
1457 case LASS: | |
1458 return plassign(e1); | |
1459 case LASSOP: case LUASSOP: | |
1460 return plassop(e1); | |
1461 #endif | |
1462 case ALLOCA: | |
501 | 1463 return palloc(e2); |
462 | 1464 case BUILTINP: |
463 | 1465 return list2(CONST,is_const(pexpr(e2))); |
462 | 1466 case COMMA: |
555 | 1467 return pcomma(e2,caddr(e1)); |
462 | 1468 case RETURN: |
1469 case ENVIRONMENT: | |
1470 case LCALL: | |
1471 return e1; | |
1472 #if BIT_FIELD_CODE | |
1473 case RBIT_FIELD: | |
1474 return prbit_field(e1); | |
553 | 1475 case BIT_FIELD: |
1476 return list3(BIT_FIELD,pexpr(e2),caddr(e1)); | |
462 | 1477 case BASS: |
1478 return pbassign(e1); | |
1479 case BPREINC: | |
1480 case BPOSTINC: | |
1481 case BASSOP: | |
1482 return pbassop(e1); | |
1483 #endif | |
1484 #if ASM_CODE | |
1485 case ASM: | |
1486 return list3(ASM,list4( | |
501 | 1487 car(e2),cadr(e2),caddr(e2),cadddr(e2)), |
1488 caddr(e1)); | |
462 | 1489 #endif |
681 | 1490 case CAST: |
692 | 1491 if (e2==0) { |
1492 // casted empty structure (struct hoge){} | |
1493 // I think we can skip it. It means nothing in declaration | |
1494 return 0; | |
1495 } else if (car(e2)==DECL_DATA) { | |
681 | 1496 // casted initialized structure (struct hoge){...} |
1497 return list3(DECL_DATA,pexpr(cadr(e2)),caddr(e2)); | |
1498 } | |
1499 if (type_compatible(caddr(e1),cadddr(e1))) { | |
690 | 1500 return pexpr(e2); // rvalue ? |
687 | 1501 } else { |
690 | 1502 e2 = pexpr(e2); // will override type |
687 | 1503 type = cadddr(e1); |
690 | 1504 return correct_type(e2,caddr(e1)); |
687 | 1505 // return list4(CAST,pexpr(e2),caddr(e1),cadddr(e1)); |
1506 } | |
681 | 1507 case DECL_DATA: |
1508 return list3(DECL_DATA,pexpr(e2),caddr(e1)); | |
1509 case DECL_DATA_LIST: | |
697 | 1510 case DECL_DATA_ARRAY: |
711 | 1511 return list4(car(e1),pexpr(e2),pexpr(caddr(e1)),cadddr(e1)); |
681 | 1512 case DECL_DATA_FIELD: |
711 | 1513 return list4n(car(e1),pexpr(e2),pexpr(caddr(e1)),ncadddr(e1)); |
462 | 1514 case ST_DECL: return p_decl(e1); |
1515 case ST_IF: return p_if(e1); | |
1516 case ST_DO: return p_do(e1); | |
1517 case ST_WHILE: return p_while(e1); | |
1518 case ST_FOR: return p_for(e1); | |
1519 case ST_SWITCH: return p_switch(e1); | |
1520 case ST_COMP: return p_comp(e1); | |
1521 case ST_BREAK: return p_break(e1); | |
1522 case ST_CONTINUE: return p_continue(e1); | |
1523 case ST_CASE: return p_case(e1); | |
1524 case ST_DEFAULT: return p_default(e1); | |
1525 case ST_RETURN: return p_return(e1); | |
1526 case ST_GOTO: return p_goto(e1); | |
1527 case ST_ASM: return p_asm(e1); | |
1528 case ST_LABEL: return p_label(e1); | |
1529 case ST_COMMENT: return p_comment(e1); | |
1530 default: | |
551 | 1531 error(-1); |
462 | 1532 return p_bool(e1); |
1533 } | |
1534 return VOID; | |
1535 } | |
1536 | |
625 | 1537 /* |
1538 Prepare pvariable table | |
1539 */ | |
1540 | |
557 | 1541 static int |
1542 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals) | |
1543 { | |
1544 int arg; | |
1545 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) { | |
625 | 1546 // replacable const variable |
557 | 1547 if (is_memory(e4)) { |
1548 heap[pdisp+narg]=reference(e4); | |
1549 return evals; | |
1550 } else if (is_const(e4)) { | |
1551 heap[pdisp+narg]=e4; | |
1552 return evals; | |
1553 } | |
1554 } | |
625 | 1555 // we need real local variable for this inline |
711 | 1556 arg = heap[pdisp+narg]=list3n(LVAR,new_lvar(size(t)),anptr); |
557 | 1557 inline_lvars = glist2(arg,inline_lvars); |
1558 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals); | |
1559 return evals; | |
1560 } | |
1561 | |
1562 /* | |
1563 setup parameter replacement of inline call | |
1564 */ | |
1565 | |
1566 static void | |
696 | 1567 enter_inline(NMTBL *n, int e,int toplevel) |
557 | 1568 { |
1569 int e1 = attr_value(n,INLINE); | |
1570 int arg_disp = cadr(e1); // number of arguments | |
1571 int narg,e3,e4, e5, fargtype; | |
1572 int evals = 0; | |
696 | 1573 int t, replace; |
557 | 1574 NMTBL *anptr; |
1575 | |
1576 fnptr = n; // st_return see this | |
1577 pvartable = p_vartable(arg_disp, /* number of arg */ | |
1578 caddr(e1) /* number of local parameter */); | |
1579 | |
704 | 1580 replace = (is_code(fnptr)||toplevel); |
696 | 1581 |
557 | 1582 /* function arguments type */ |
1583 fargtype = n->dsp; // we cannot do destruct reverse here | |
463 | 1584 |
557 | 1585 /* inline function arguments */ |
1586 narg = 0; | |
1587 if (!fargtype) { | |
1588 goto no_args; // wrong number of arguments | |
1589 } | |
699 | 1590 |
557 | 1591 for (e3 = e5 = reverse0(caddr(e)); e3; e3 = cadr(e3)) { |
712 | 1592 anptr = ncadddr(fargtype); |
557 | 1593 if (!anptr) break; // should not happen? |
1594 t=caddr(e3); // type | |
1595 e4 = car(e3); | |
696 | 1596 if (replace) { |
1597 heap[pdisp+narg]=reference(e4); | |
1598 } else { | |
1599 evals = replace_inline_parameter(anptr,t,e4,narg,evals); | |
1600 } | |
557 | 1601 narg ++; |
1602 fargtype = cadr(fargtype); | |
1603 } | |
1604 caddr(e) = reverse0(e5); // make it normal | |
1605 if (eval_order==NORMAL) evals = reverse0(evals); | |
1606 for(;evals;evals=cadr(evals)) { | |
1607 g_expr_u(car(evals)); | |
1608 } | |
1609 no_args: | |
1610 return; | |
1611 } | |
1612 | |
1613 /* | |
1614 pass local static variable list to our parents | |
1615 clean up and free used inline parameters | |
1616 */ | |
1617 | |
1618 static void | |
696 | 1619 leave_inline(int e1,int toplevel) |
557 | 1620 { |
1621 NMTBL *n; | |
712 | 1622 NMTBL *local_statics = ncadddr(e1); // local static list |
557 | 1623 |
696 | 1624 if (retcont && !toplevel) error(STERR); |
1625 // inline can't handle return/environment except top level | |
557 | 1626 |
1627 if (local_statics && local_statics != &null_nptr) { | |
1628 // append our local static variables to the parent list | |
1629 n = local_statics; | |
1630 while(n->next != &null_nptr) n=n->next; | |
1631 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
|
1632 ncadddr(e1) = 0; // prevent duplicate initialize |
557 | 1633 } |
625 | 1634 |
1635 // free used local variables or registers | |
557 | 1636 while(inline_lvars) { |
1637 int e; | |
1638 int l = car(inline_lvars); | |
1639 switch(car(l)) { | |
1640 case LVAR: free_lvar(cadr(l)); break; | |
1641 case REGISTER: case DREGISTER: | |
1642 case FREGISTER: case LREGISTER: | |
1643 free_register(cadr(l)); | |
1644 } | |
1645 e = cadr(inline_lvars); | |
1646 free_glist2(inline_lvars); | |
1647 inline_lvars = e; | |
1648 } | |
1649 } | |
530 | 1650 |
463 | 1651 extern int |
696 | 1652 gen_inline(int e, int toplevel) |
462 | 1653 { |
514 | 1654 // these saved value should be some struct |
1655 int sretlabel; | |
1656 NMTBL *sfnptr; | |
552 | 1657 int svartable; |
696 | 1658 |
513 | 1659 int scslabel = cslabel; |
463 | 1660 int sdisp = pdisp; |
552 | 1661 int sret_register; |
1662 int sret_reg_mode; | |
1663 int sinline_lvars; | |
1664 int slfree; | |
531 | 1665 // int slreg_count=lreg_count; |
513 | 1666 |
711 | 1667 NMTBL *n = (NMTBL*)ncaddr(cadr(e)); |
463 | 1668 int e1 = attr_value(n,INLINE); |
500 | 1669 int parse = car(e1); // inline parse tree |
557 | 1670 int dots; |
508 | 1671 int ret_type = function_type(cadddr(e),&dots); |
463 | 1672 |
552 | 1673 checkret(); |
1674 checkjmp(-1); | |
1675 | |
1676 svartable = pvartable; | |
696 | 1677 |
552 | 1678 scslabel = cslabel; |
1679 sdisp = pdisp; | |
1680 sret_register = ret_register; | |
1681 sret_reg_mode = ret_reg_mode; | |
1682 sinline_lvars = inline_lvars; | |
1683 slfree=lfree; | |
1684 // int slreg_count=lreg_count; | |
514 | 1685 |
1686 sretlabel = retlabel; | |
1687 sfnptr = fnptr; | |
696 | 1688 |
513 | 1689 cslabel = -1; |
1690 retpending = 0; | |
527 | 1691 inline_lvars = 0; |
705 | 1692 if (!toplevel) { |
1693 retlabel = fwdlabel(); | |
1694 } | |
513 | 1695 |
696 | 1696 enter_inline(n,e,toplevel); |
526 | 1697 |
704 | 1698 if (toplevel) { |
705 | 1699 ret_register = code_set_return_register(0); // fnptr required |
704 | 1700 ret_reg_mode = 1; |
1701 } else { | |
705 | 1702 ret_register = 555; |
704 | 1703 ret_reg_mode = 0; |
1704 } | |
1705 | |
557 | 1706 // partial evaluation of parse tree |
1707 // constant propergation, etc. | |
1708 parse = pexpr(parse); | |
463 | 1709 pdisp = sdisp; |
464 | 1710 pvartable = svartable; |
513 | 1711 |
557 | 1712 // generate code if necessary |
513 | 1713 if (ret_type!=VOID) |
557 | 1714 g_expr0(parse); |
513 | 1715 else |
557 | 1716 g_expr_u(parse); |
1717 | |
705 | 1718 if (!toplevel) { |
1719 checkret(); | |
1720 fwddef(retlabel); | |
1721 control=1; | |
1722 } | |
513 | 1723 |
696 | 1724 leave_inline(e1,toplevel); |
510 | 1725 |
513 | 1726 fnptr = sfnptr; |
1727 retlabel = sretlabel; | |
696 | 1728 |
513 | 1729 cslabel = scslabel; |
514 | 1730 ret_register = sret_register; |
1731 ret_reg_mode = sret_reg_mode; | |
527 | 1732 inline_lvars = sinline_lvars; |
528 | 1733 lfree=slfree; |
513 | 1734 |
508 | 1735 return ret_type; |
462 | 1736 } |
1737 | |
1738 /* end */ |