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