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