462
|
1 /* Micro-C Partial Evaluator Part */
|
|
2 /* $Id$ */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include "mc.h"
|
|
6 #include "mc-parse.h"
|
|
7 #include "mc-codegen.h"
|
|
8 #include "mc-switch.h"
|
464
|
9 #include "mc-code.h"
|
|
10 #include "mc-inline.h"
|
462
|
11
|
463
|
12 static int pvartable;
|
|
13 static int pdisp;
|
462
|
14
|
514
|
15 static int ret_register,ret_reg_mode;
|
|
16
|
527
|
17 static int inline_lvars;
|
|
18
|
462
|
19 /*
|
|
20 Basic code generator from parse tree
|
|
21 */
|
|
22
|
|
23 extern void
|
|
24 st_decl(int e1){
|
|
25 // NMTBL *n = (NMTBL *)caddr(e1);
|
|
26 // int stmode = cadddr(e1);
|
|
27 }
|
|
28
|
|
29 extern void
|
|
30 st_if(int e1){
|
|
31 int l1,l2,slfree;
|
|
32 int e2=caddr(e1),e3;
|
|
33 // conv->if_();
|
|
34 slfree=lfree;
|
|
35 checkret();
|
|
36 l1 = bexpr(car(e2),0,fwdlabel());
|
|
37 // conv->if_then_();
|
|
38 g_expr_u(cadr(e2));
|
|
39 checkret();
|
|
40 if ((e3=caddr(e2))) { // else
|
|
41 // conv->if_else_();
|
|
42 if ((l2 = control))
|
|
43 gen_jmp(l2=fwdlabel());
|
|
44 fwddef(l1);
|
|
45 g_expr_u(e3);
|
|
46 checkret();
|
|
47 if (l2) fwddef(l2);
|
|
48 } else {
|
|
49 fwddef(l1);
|
|
50 }
|
|
51 // conv->if_endif_();
|
|
52 }
|
|
53
|
|
54
|
|
55 extern void
|
|
56 st_do(int e1){
|
|
57 int sbreak,scontinue,l;
|
|
58
|
|
59 sbreak=blabel;
|
|
60 scontinue=clabel;
|
|
61 blabel=fwdlabel();
|
|
62 clabel=fwdlabel();
|
|
63 control=1;
|
|
64 checkret();
|
|
65 l=backdef();
|
|
66 // conv->dowhile_();
|
|
67 g_expr_u(cadddr(e1));
|
|
68 checkret();
|
|
69 // conv->dowhile_cond_();
|
|
70 bexpr(caddr(e1),1,l);
|
|
71 // conv->dowhile_end_();
|
|
72 fwddef(blabel);
|
|
73 clabel=scontinue;
|
|
74 blabel=sbreak;
|
|
75 }
|
|
76
|
|
77
|
|
78 extern void
|
|
79 st_while(int e1){
|
|
80 int sbreak,scontinue,e;
|
|
81
|
|
82 sbreak=blabel;
|
|
83 scontinue=clabel;
|
|
84 blabel=fwdlabel();
|
|
85 control=1;
|
|
86 checkret();
|
|
87 clabel=backdef();
|
|
88 // conv->while_();
|
|
89 // conv->while_body_();
|
|
90 if(!(e=cadddr(e1))) {
|
|
91 bexpr(caddr(e1),1,clabel);
|
|
92 // conv->sm_();
|
|
93 } else {
|
|
94 bexpr(caddr(e1),0,blabel);
|
|
95 g_expr_u(e);
|
|
96 checkret();
|
|
97 if(control)
|
|
98 gen_jmp(clabel);
|
|
99 }
|
|
100 // conv->while_end_();
|
|
101 fwddef(blabel);
|
|
102 clabel=scontinue;
|
|
103 blabel=sbreak;
|
|
104 }
|
|
105
|
|
106
|
|
107 extern void
|
|
108 st_for(int e1){
|
|
109 int p0,p1,p2,body;
|
|
110 int l,e;
|
|
111 int sbreak=blabel;
|
|
112 int scontinue=clabel;
|
|
113
|
|
114 e = caddr(e1);
|
|
115 p0 = car(e); p1 = cadr(e); p2 = caddr(e); body = cadddr(e);
|
|
116
|
|
117 blabel=fwdlabel();
|
|
118 // conv->for_();
|
|
119 if (p0) {
|
|
120 checkret();
|
|
121 g_expr_u(p0);
|
|
122 }
|
|
123 // conv->for1_();
|
|
124 control=1;
|
|
125 checkret();
|
|
126 l=backdef();
|
|
127 if (p1) {
|
|
128 bexpr(p1,0,blabel);
|
|
129 }
|
|
130 // conv->for2_();
|
|
131 // conv->for_body_();
|
|
132 if (!p2) {
|
|
133 clabel=l;
|
|
134 g_expr_u(body);
|
|
135 checkret();
|
|
136 } else {
|
|
137 clabel=fwdlabel();
|
|
138 g_expr_u(body);
|
|
139 checkret();
|
|
140 fwddef(clabel);
|
|
141 g_expr_u(p2);
|
|
142 }
|
|
143 // conv->for_end_();
|
|
144 gen_jmp(l);
|
|
145 fwddef(blabel);
|
|
146 clabel=scontinue;
|
|
147 blabel=sbreak;
|
|
148 }
|
|
149
|
|
150
|
|
151 extern void
|
|
152 st_switch(int e1){
|
|
153 int sbreak,scase,sdefault,slfree,svalue,slist;
|
558
|
154 int cst,e;
|
462
|
155
|
|
156 checkret();
|
|
157 slist = cslist;
|
|
158 cslist = 0;
|
|
159 sbreak=blabel; /* save parents break label */
|
|
160 blabel=fwdlabel();
|
|
161 sdefault=dlabel; /* save parents default label */
|
|
162 dlabel=0;
|
|
163 scase=cslabel; /* save parents next case label */
|
|
164 // conv->switch_();
|
|
165 slfree=lfree;
|
|
166 svalue=csvalue1; /* save parents switch value */
|
558
|
167 e =caddr(e1); /* switch value */
|
|
168 if (car(e)==CONST) {
|
|
169 cst = 1;
|
|
170 csvalue1=glist2(CONST,cadr(e));
|
|
171 gen_jmp( cslabel=fwdlabel());
|
|
172 } else {
|
|
173 cst = 0;
|
|
174 gexpr(e,1); /* switch value */
|
|
175 csvalue1=csvalue() ;
|
|
176 cslabel = control = 0;
|
|
177 }
|
462
|
178 // conv->switch_body_();
|
|
179 g_expr_u(cadddr(e1));
|
|
180 // conv->switch_end_();
|
|
181 checkret();
|
|
182 #if CASE_CODE
|
558
|
183 if (!cst) {
|
|
184 if (control) gen_jmp(blabel);
|
|
185 genswitch(cslist,cslabel);
|
|
186 } else if (!cslist) {
|
|
187 if(dlabel) def_label(cslabel,dlabel);
|
|
188 else fwddef(cslabel);
|
|
189 }
|
462
|
190 #else
|
558
|
191 if (!(cst && cslist)) {
|
|
192 if(dlabel) def_label(cslabel,dlabel);
|
|
193 else fwddef(cslabel);
|
|
194 }
|
462
|
195 #endif
|
558
|
196 free_glist2(csvalue1);
|
|
197 if (cst && !cslist) {
|
|
198 if(pending_jmp!=cslabel)
|
|
199 fwddef(cslabel);
|
|
200 else pending_jmp = 0; // cslabel is here
|
|
201 }
|
|
202 fwddef(blabel);
|
462
|
203 csvalue1=svalue;
|
|
204 cslabel=scase;
|
|
205 dlabel=sdefault;
|
|
206 blabel=sbreak;
|
|
207 cslist = slist;
|
|
208 }
|
|
209
|
|
210
|
|
211 extern void
|
|
212 st_comp(int e1){
|
|
213 g_expr_u(caddr(e1));
|
|
214 }
|
|
215
|
|
216
|
|
217 extern void
|
|
218 st_break(int e1){
|
|
219 checkret();
|
|
220 // conv->break_();
|
|
221 if (control)
|
|
222 gen_jmp(blabel);
|
|
223 }
|
|
224
|
|
225
|
|
226 extern void
|
|
227 st_continue(int e1){
|
|
228 checkret();
|
|
229 // conv->continue_();
|
|
230 if (control) gen_jmp(clabel);
|
|
231 }
|
|
232
|
|
233
|
|
234 extern void
|
|
235 st_case(int e1){
|
|
236 #if CASE_CODE
|
569
|
237 int l=0,clist=caddr(e1),c;
|
558
|
238 int cst = (car(csvalue1)==CONST);
|
|
239 if (cst) {
|
|
240 c=cadr(csvalue1);
|
|
241 for(;clist;clist=cadr(clist)) {
|
|
242 if (car(clist)==c) break;
|
|
243 }
|
|
244 if (!clist) return; // no match
|
|
245 } else
|
|
246 l = fwdlabel();
|
462
|
247 if (retpending) {
|
|
248 ret(); retpending=0;
|
|
249 }
|
558
|
250 if (cst) {
|
|
251 if (cslist) { // may duplicat csvalue
|
|
252 return;
|
|
253 }
|
|
254 cslist=1;
|
|
255 fwddef(cslabel);
|
|
256 return;
|
|
257 }
|
462
|
258 if (!cslabel) {
|
|
259 if (!control) {
|
|
260 cmpdimm(car(clist),csvalue1,cslabel=fwdlabel(),1);
|
|
261 caddr(clist)=0;
|
|
262 } else {
|
|
263 error(-1);
|
|
264 }
|
|
265 }
|
|
266 while(clist) {
|
|
267 caddr(clist) = l;
|
|
268 clist = cadr(c=clist); cadr(c) = 0; // insert destroy cadr of clist
|
|
269 cslist=insert_ascend(cslist,c,docase_eq);
|
|
270 }
|
|
271 fwddef(l);
|
|
272 control=1;
|
|
273 #else
|
558
|
274 int c,clist,l;
|
|
275 int cst = (car(csvalue1)==CONST);
|
|
276 clist = caddr(e1);
|
|
277 if (cst) {
|
|
278 c=cadr(csvalue1);
|
|
279 for(;clist;clist=cadr(clist)) {
|
|
280 if (car(clist)==c) break; // no match
|
|
281 }
|
|
282 if (!clist) return;
|
|
283 }
|
|
284 if (cst) {
|
|
285 if (!cslist) {
|
|
286 if (retpending) {
|
|
287 ret(); retpending=0;
|
|
288 }
|
|
289 fwddef(cslabel);
|
|
290 cslist = 1;
|
|
291 } // else error(CSERR);
|
|
292 return;
|
|
293 }
|
462
|
294 if (retpending) {
|
|
295 ret(); retpending=0;
|
|
296 }
|
|
297 l=fwdlabel();
|
|
298 if (control) {
|
|
299 control=0;
|
|
300 gen_jmp(l);
|
|
301 }
|
|
302 if (cslabel) fwddef(cslabel);
|
558
|
303 while(cadr(clist)) {
|
|
304 cmpdimm(car(clist),csvalue1,l,0);
|
|
305 clist=cadr(clist);
|
462
|
306 }
|
558
|
307 cmpdimm(car(clist),csvalue1,cslabel=fwdlabel(),1);
|
462
|
308 if (l) fwddef(l);
|
|
309 #endif
|
|
310 }
|
|
311
|
|
312
|
|
313 extern void
|
|
314 st_default(int e1){
|
558
|
315 // int cst = (car(csvalue1)==CONST);
|
462
|
316 control=1;
|
|
317 checkret();
|
|
318 if (dlabel) error(STERR); // double default:
|
|
319 dlabel = backdef();
|
|
320 // conv->case_(0,1);
|
|
321 }
|
|
322
|
|
323
|
|
324 extern void
|
|
325 st_return(int e1){
|
514
|
326 int e,t;
|
462
|
327
|
|
328 if (!cslabel) gen_jmp(cslabel = fwdlabel());
|
|
329 if(!(e=caddr(e1))) {
|
513
|
330 // no return value
|
462
|
331 retpending = 1;
|
|
332 return;
|
|
333 }
|
551
|
334 t = type_value(cadr(fnptr->ty));
|
|
335 if (t>0 && (car(t)==STRUCT || car(t)==UNION)) {
|
|
336 // copy is included in e, pass the pointer
|
|
337 t = list2(POINTER,type_value(cadr(fnptr->ty)));
|
|
338 }
|
|
339 g_expr(e);
|
|
340 if (ret_reg_mode==0) {
|
|
341 // return value register is not fixed
|
|
342 ret_reg_mode=1;
|
|
343 ret_register = code_get_fixed_creg(USE_CREG,t);
|
462
|
344 } else {
|
551
|
345 code_set_fixed_creg(ret_register,1,t);
|
462
|
346 }
|
|
347 // conv->return_end_();
|
|
348 retpending = 1;
|
|
349 }
|
|
350
|
|
351 extern void
|
|
352 st_goto(int e){
|
551
|
353 NMTBL *nptr0;
|
|
354 int e1,e2,env;
|
462
|
355
|
|
356 checkret();
|
|
357 e1 = caddr(e);
|
|
358 if (car(e1)==RINDIRECT) {
|
|
359 gen_indirect_goto(cadr(e1));
|
505
|
360 return ;
|
552
|
361 } else if (car(e1)==RLVAR) {
|
551
|
362 gen_indirect_goto(e1);
|
|
363 return ;
|
552
|
364 } else if (car(e1)==LVAR||car(e1)==FLABEL) {
|
551
|
365 gen_jmp(cadr(e1));
|
462
|
366 control=0;
|
505
|
367 return ;
|
551
|
368 } else if (car(e1)==CODE) {
|
462
|
369 /* CbC continuation */
|
|
370 // conv->jump_(env);
|
551
|
371 // should be separate function
|
462
|
372 e2 = cadr(e1);
|
|
373 env = caddr(e1);
|
|
374 if (car(e2) == FNAME) {
|
|
375 nptr0=(NMTBL *)cadr(e2);
|
|
376 if (nptr0->sc==EMPTY)
|
|
377 nptr0->sc = EXTRN1;
|
|
378 else if(nptr0->sc==FUNCTION)
|
|
379 nptr0->sc = CODE;
|
|
380 if (nptr0->ty>0&&car(nptr0->ty)==FUNCTION)
|
|
381 car(nptr0->ty)=CODE;
|
|
382 }
|
|
383 gexpr(list3(CODE,e1,env),0);
|
|
384 control=0;
|
|
385 // conv->sm_();
|
505
|
386 return ;
|
551
|
387 } else {
|
|
388 error(-1);
|
462
|
389 }
|
|
390 }
|
|
391
|
|
392
|
|
393 #if ASM_CODE
|
|
394 extern void
|
|
395 st_asm(int e1){
|
|
396 checkret();
|
|
397 g_expr_u(list3(ASM,caddr(e1),cadddr(e1)));
|
|
398 }
|
|
399 #endif
|
|
400
|
|
401
|
|
402 extern void
|
|
403 st_label(int e1){
|
551
|
404 int lb = caddr(e1);
|
462
|
405 control=1;
|
|
406 checkret();
|
551
|
407 if (car(lb)==LVAR) { // label variable case
|
|
408 } else if (car(lb)!=FLABEL) error(-1);
|
|
409 fwddef(cadr(lb));
|
462
|
410 }
|
|
411
|
|
412 extern void
|
|
413 st_comment(int e1){
|
574
|
414 glineno++;
|
585
|
415 printf("## %d ",glineno);
|
462
|
416 gen_comment((char *)caddr(e1));
|
|
417 }
|
|
418
|
|
419 /*
|
|
420 partial evaluator
|
|
421 */
|
|
422
|
|
423 static int
|
557
|
424 p_vartable(int adisp,int ldisp)
|
462
|
425 {
|
464
|
426 int i;
|
505
|
427 int pvartable = getfree(adisp-ldisp); // have to be local heap
|
509
|
428 pdisp = pvartable-ldisp;
|
|
429 for(i=ldisp;i<0;i++) {
|
|
430 heap[pdisp+i] = 0;
|
463
|
431 }
|
505
|
432 return pvartable;
|
462
|
433 }
|
|
434
|
|
435 static int
|
|
436 p_lvar(int e1)
|
|
437 {
|
463
|
438 int sz = is_memory(e1);
|
|
439 int d = cadr(e1);
|
527
|
440 int d1,e;
|
502
|
441 if ((d1=(heap[pdisp+d]))) return d1;
|
574
|
442 fprintf(stderr,"## undeclared ivar %d\n",d);
|
|
443 // error(-1);
|
527
|
444 e = heap[pdisp+d]=list3(LVAR,new_lvar(sz),0);
|
|
445 inline_lvars = glist2(e,inline_lvars);
|
|
446 return e;
|
462
|
447 }
|
|
448
|
|
449 static int
|
|
450 pfunction(int e)
|
|
451 {
|
501
|
452 // list4(INLINE,e1,arglist,ftype);
|
|
453 // include code segement case
|
|
454 int e1 = pexpr(cadr(e));
|
|
455 int arglist = caddr(e);
|
|
456 int newargs = 0;
|
|
457 int ftype = cadddr(e);
|
|
458 int e3;
|
|
459 for (e3 = arglist; e3; e3 = cadr(e3)) {
|
514
|
460 newargs = list3( pexpr(car(e3)), newargs, caddr(e3));
|
501
|
461 }
|
|
462 newargs = reverse0(newargs);
|
513
|
463 return list4(car(e),e1,newargs,ftype);
|
462
|
464 }
|
|
465
|
|
466 static int
|
|
467 prindirect(int e)
|
|
468 {
|
525
|
469 int lvar;
|
533
|
470 int offset = caddr(e);
|
530
|
471 if (car(lvar=cadr(e))==IVAR) {
|
525
|
472 lvar=p_lvar(cadr(e)); // can be anything....
|
530
|
473 switch(car(lvar)) {
|
|
474 case LVAR:
|
533
|
475 if(offset) {
|
|
476 return list3(car(e),lvar,offset);
|
|
477 }
|
530
|
478 return rvalue_t(lvar,cadddr(e));
|
|
479 case REGISTER: case DREGISTER:
|
|
480 case FREGISTER: case LREGISTER:
|
|
481 case CONST: case FCONST: case DCONST: case LCONST:
|
|
482 // should do type check
|
533
|
483 if (offset) error(-1);
|
530
|
484 return lvar;
|
|
485 }
|
512
|
486 }
|
533
|
487 return list3(car(e),pexpr(cadr(e)),offset);
|
462
|
488 }
|
|
489
|
|
490 static int
|
527
|
491 pindirect(int e)
|
|
492 {
|
553
|
493 //int lvar;
|
|
494 //if (car(lvar=cadr(e))==IVAR)
|
|
495 // lvar=p_lvar(cadr(e)); // can be anything....
|
527
|
496 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
|
497 }
|
|
498
|
|
499 static int
|
462
|
500 paddress(int e)
|
|
501 {
|
601
|
502 // if (car(cadr(e))==INDIRECT) return pexpr(cadr(cadr(e)));
|
501
|
503 return list2(car(e),pexpr(cadr(e)));
|
462
|
504 }
|
|
505
|
|
506 static int
|
464
|
507 p_conv(int e1,int e2)
|
462
|
508 {
|
557
|
509 int t;
|
|
510 if (is_const(e2) && (t=type_of_conv(e1))) {
|
|
511 return correct_type(e2,t);
|
|
512 }
|
527
|
513 return list3(CONV,pexpr(e2),e1);
|
462
|
514 }
|
|
515
|
|
516 static int
|
464
|
517 pbinop(int op,int e1,int e2)
|
462
|
518 {
|
557
|
519 e1 = pexpr(e1);
|
|
520 e2 = pexpr(e2);
|
|
521 if (is_const(e1)&&is_const(e2)) {
|
|
522 int t;
|
|
523 if((t= type_of_bop(op)))
|
599
|
524 return binop(OP(op),e1,e2,t,t);
|
557
|
525 }
|
|
526 return list3(op,e1,e2);
|
502
|
527 }
|
|
528
|
|
529 static int
|
526
|
530 plor(int op,int e1,int e2)
|
|
531 {
|
555
|
532 int e = pexpr(e1);
|
|
533 return list3(op,e,pexpr(e2));
|
526
|
534 }
|
|
535
|
|
536 static int
|
|
537 pland(int op,int e1,int e2)
|
|
538 {
|
555
|
539 int e = pexpr(e1);
|
|
540 return list3(op,e,pexpr(e2));
|
526
|
541 }
|
|
542
|
|
543 static int
|
462
|
544 psassign(int e)
|
|
545 {
|
555
|
546 int e1 = pexpr(cadr(e));
|
|
547 int e2 = pexpr(caddr(e));
|
|
548 return list4(car(e),e1,e2,cadddr(e));
|
462
|
549 }
|
|
550
|
|
551 static int
|
|
552 passign(int e)
|
|
553 {
|
555
|
554 int e1 = pexpr(cadr(e));
|
|
555 int e2 = pexpr(caddr(e));
|
|
556 return list3(car(e),e1,e2);
|
462
|
557 }
|
|
558
|
|
559 static int
|
|
560 passop(int e)
|
|
561 {
|
555
|
562 int e1 = pexpr(cadr(e));
|
|
563 int e2 = pexpr(caddr(e));
|
|
564 return list4(car(e),e1,e2,cadddr(e));
|
462
|
565 }
|
|
566
|
|
567 static int
|
|
568 pdassign(int e)
|
|
569 {
|
555
|
570 int e1 = pexpr(cadr(e));
|
|
571 int e2 = pexpr(caddr(e));
|
|
572 return list3(car(e),e1,e2);
|
462
|
573 }
|
|
574
|
|
575 static int
|
|
576 pdassop(int e)
|
|
577 {
|
555
|
578 int e1 = pexpr(cadr(e));
|
|
579 int e2 = pexpr(caddr(e));
|
|
580 return list4(car(e),e1,e2,cadddr(e));
|
462
|
581 }
|
|
582
|
|
583 static int
|
|
584 plassign(int e)
|
|
585 {
|
555
|
586 int e1 = pexpr(cadr(e));
|
|
587 int e2 = pexpr(caddr(e));
|
|
588 return list3(car(e),e1,e2);
|
462
|
589 }
|
|
590
|
|
591 static int
|
|
592 plassop(int e)
|
|
593 {
|
555
|
594 int e1 = pexpr(cadr(e));
|
|
595 int e2 = pexpr(caddr(e));
|
|
596 return list4(car(e),e1,e2,cadddr(e));
|
462
|
597 }
|
|
598
|
|
599 static int
|
|
600 palloc(int e)
|
|
601 {
|
501
|
602 return list2(car(e),pexpr(cadr(e)));
|
462
|
603 }
|
|
604
|
|
605 static int
|
464
|
606 pcomma(int e1,int e2)
|
462
|
607 {
|
556
|
608 int e = pexpr(e1);
|
555
|
609 return list3(COMMA,e,pexpr(e2));
|
462
|
610 }
|
|
611
|
|
612 static int
|
|
613 prbit_field(int e)
|
|
614 {
|
501
|
615 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
616 }
|
|
617
|
|
618 static int
|
|
619 pbassign(int e)
|
|
620 {
|
553
|
621 // list4(BASS,e1,e2,list2(BASS,t)));
|
|
622 int e1=pexpr(caddr(e));
|
|
623 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
624 }
|
|
625
|
|
626 static int
|
|
627 pbassop(int e)
|
|
628 {
|
553
|
629 int e1=pexpr(caddr(e));
|
|
630 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
631 }
|
|
632
|
561
|
633 // handle local variable declaration
|
|
634 // initialization is accumrated in parse tree
|
|
635 // should consider int k=some_compile_time_constant;
|
462
|
636 static int
|
|
637 p_decl(int e)
|
|
638 {
|
552
|
639 // list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode));
|
554
|
640 int ctmode=cadddr(e);
|
463
|
641 NMTBL *n=(NMTBL*)caddr(e);
|
|
642 int dsp = n->dsp;
|
569
|
643 int v=0;
|
552
|
644 int sstmode = stmode;
|
|
645 int smode = mode;
|
500
|
646 // in real partial evaluation, we have to check whether this variable
|
|
647 // is used or not.
|
552
|
648 if (ctmode) {
|
|
649 mode = car(ctmode); stmode = cadr(ctmode); ctmode = caddr(ctmode);
|
574
|
650 } else {
|
|
651 mode = LDECL; stmode = 0; ctmode = 0;
|
552
|
652 }
|
463
|
653 switch(stmode) {
|
528
|
654 case EXTRN: case EXTRN1: case STATIC:
|
552
|
655 // def(n,ctmode); we don't need this. already done.
|
|
656 // stmode = sstmode;
|
|
657 stmode = sstmode;
|
|
658 mode = smode;
|
|
659 return pexpr(cadr(e));
|
|
660 // case LLDECL: LLDECL is mode, not stmode (bad design)
|
|
661 // v = list2(FLABEL,fwdlabel()); break;
|
529
|
662 #if 1
|
463
|
663 case REGISTER:
|
|
664 switch(n->ty) {
|
|
665 case ULONGLONG: case LONGLONG:
|
|
666 v = get_lregister_var(n); break;
|
|
667 case FLOAT:
|
|
668 v = get_dregister_var(n,0); break;
|
|
669 case DOUBLE:
|
|
670 v = get_dregister_var(n,1); break;
|
|
671 default:
|
|
672 if (scalar(n->ty))
|
524
|
673 v = get_register_var(n);
|
463
|
674 else
|
|
675 error(TYERR);
|
|
676 }
|
529
|
677 break;
|
528
|
678 #endif
|
463
|
679 default:
|
552
|
680 if (n->sc==FLABEL)
|
|
681 v = list3(LVAR,fwdlabel(),(int)n);
|
|
682 else
|
|
683 v = list3(LVAR,new_lvar(size(n->ty)),(int)n);
|
463
|
684 }
|
552
|
685 if (n->sc!=FLABEL)
|
|
686 inline_lvars = glist2(v,inline_lvars);
|
574
|
687 if (heap[pdisp+dsp]) {
|
|
688 fprintf(stderr,"## double p_decl %s %s\n",((NMTBL*)(caddr(heap[pdisp+dsp])))->nm,n->nm);
|
|
689 error(-1);
|
|
690 }
|
463
|
691 heap[pdisp+dsp]=v;
|
552
|
692 stmode = sstmode;
|
|
693 mode = smode;
|
463
|
694 return pexpr(cadr(e));
|
462
|
695 }
|
|
696
|
|
697 static int
|
500
|
698 p_if(int e1)
|
462
|
699 {
|
500
|
700 int cond,l1,l2;
|
|
701 int e2=caddr(e1),e3;
|
|
702 cond = pexpr(car(e2));
|
|
703 // conv->if_then_();
|
|
704 l1 = pexpr(cadr(e2));
|
|
705 if ((e3=caddr(e2))) { // else
|
|
706 l2 = pexpr(e3);
|
|
707 } else {
|
|
708 l2 = 0;
|
|
709 }
|
|
710 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
711 }
|
|
712
|
|
713 static int
|
|
714 p_do(int e)
|
|
715 {
|
524
|
716 int e2 = pexpr(caddr(e));
|
|
717 int e3 = pexpr(cadddr(e));
|
|
718 return list4(ST_DO,pexpr(cadr(e)),e2,e3);
|
462
|
719 }
|
|
720
|
|
721 static int
|
|
722 p_while(int e)
|
|
723 {
|
524
|
724 int e2 = pexpr(caddr(e));
|
|
725 int e3 = pexpr(cadddr(e));
|
|
726 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3);
|
462
|
727 }
|
|
728
|
|
729 static int
|
|
730 p_for(int e)
|
|
731 {
|
501
|
732 int e1=caddr(e);
|
524
|
733 int p0=pexpr(car(e1));
|
|
734 int p1=pexpr(cadr(e1));
|
|
735 int p2=pexpr(caddr(e1));
|
|
736 int p3=pexpr(cadddr(e1));
|
|
737 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
|
462
|
738 }
|
|
739
|
|
740 static int
|
|
741 p_switch(int e)
|
|
742 {
|
524
|
743 int e2 = pexpr(caddr(e));
|
|
744 int e3 = pexpr(cadddr(e));
|
|
745 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
|
462
|
746 }
|
|
747
|
|
748 static int
|
|
749 p_comp(int e)
|
|
750 {
|
524
|
751 int e1=pexpr(caddr(e));
|
|
752 return list3(ST_COMP,pexpr(cadr(e)),e1);
|
462
|
753 }
|
|
754
|
|
755 static int
|
|
756 p_break(int e)
|
|
757 {
|
502
|
758 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
759 }
|
|
760
|
|
761 static int
|
|
762 p_continue(int e)
|
|
763 {
|
502
|
764 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
765 }
|
|
766
|
|
767 static int
|
|
768 p_case(int e)
|
|
769 {
|
506
|
770 int new=0,clist = caddr(e);
|
|
771 // insert destory clist, we have to copy it now
|
|
772 for(;clist;clist=cadr(clist))
|
|
773 new=glist3(car(clist),new,0);
|
|
774 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new));
|
462
|
775 }
|
|
776
|
|
777 static int
|
|
778 p_default(int e)
|
|
779 {
|
558
|
780 // should be removed if constant case value
|
502
|
781 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
782 }
|
|
783
|
|
784 static int
|
|
785 p_return(int e)
|
|
786 {
|
524
|
787 int e1=pexpr(caddr(e));
|
|
788 return list3(ST_RETURN,pexpr(cadr(e)),e1);
|
462
|
789 }
|
|
790
|
|
791 static int
|
|
792 p_goto(int e)
|
|
793 {
|
555
|
794 int e1,lb,e2;
|
501
|
795 if ((e1=caddr(e))) {
|
|
796 switch(car(e1)) {
|
|
797 case RINDIRECT: e1=pexpr(e1); break;
|
555
|
798 case CODE: e2=pexpr(cadr(e1));
|
|
799 e1=list3(CODE,e2,pexpr(caddr(e1))); break;
|
551
|
800 case FLABEL: /* error(-1); */ break;
|
|
801 case IVAR:
|
|
802 lb = cadr(e1);
|
|
803 if (!(e1=heap[pdisp+lb])) {
|
|
804 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
805 }
|
501
|
806 }
|
|
807 }
|
502
|
808 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
809 }
|
|
810
|
|
811 static int
|
|
812 p_list_expr(int e)
|
|
813 {
|
|
814 int e3,new = 0;
|
|
815 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
816 new= list2( pexpr(car(e3)), new);
|
|
817 }
|
|
818 return reverse0(new);
|
462
|
819 }
|
|
820
|
|
821 static int
|
|
822 p_asm(int e)
|
|
823 {
|
501
|
824 int param=caddr(e);
|
|
825 int e1 = p_list_expr(cadddr(e));
|
|
826 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
827 }
|
|
828
|
|
829 static int
|
|
830 p_label(int e)
|
|
831 {
|
551
|
832 int e1,lb;
|
|
833 if ((e1=caddr(e))) {
|
|
834 switch(car(e1)) {
|
|
835 case FLABEL: /* error(-1); */ break;
|
|
836 case IVAR:
|
|
837 lb = cadr(e1);
|
|
838 if (!(e1=heap[pdisp+lb])) {
|
|
839 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
840 }
|
|
841 }
|
|
842 }
|
|
843 return list3(ST_LABEL,pexpr(cadr(e)),e1);
|
|
844 }
|
|
845
|
|
846 static int
|
|
847 p_label_var(int e)
|
|
848 {
|
|
849 int d,e1;
|
|
850 switch(car(e1=e)) {
|
|
851 case LVAR: /* error(-1) */ break;
|
|
852 case IVAR:
|
|
853 // should this done in p_decl? (in __label__?)
|
|
854 d = cadr(e);
|
|
855 if (!(e1=(heap[pdisp+d]))) {
|
552
|
856 // error(-1);
|
551
|
857 e1 = heap[pdisp+d]=list3(LVAR,fwdlabel(),caddr(e));
|
|
858 }
|
|
859 }
|
|
860 return list2(LABEL,e1);
|
462
|
861 }
|
|
862
|
|
863 static int
|
|
864 p_bool(int e)
|
|
865 {
|
501
|
866 error(-1);
|
464
|
867 return e;
|
|
868 }
|
462
|
869
|
464
|
870 static int
|
|
871 p_comment(int e)
|
|
872 {
|
574
|
873 glineno++;
|
501
|
874 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
875 }
|
|
876
|
508
|
877 static int
|
512
|
878 p_inline(int e)
|
508
|
879 {
|
|
880 int e3;
|
|
881 int narg;
|
|
882
|
|
883 /* inline function arguments */
|
|
884 narg = 0;
|
|
885 for (e3 = caddr(e); e3; e3 = cadr(e3)) {
|
|
886 narg=list3(pexpr(car(e3)),narg,caddr(e3));
|
|
887 }
|
|
888 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e));
|
|
889 }
|
|
890
|
463
|
891 extern int
|
|
892 pexpr(int e1)
|
462
|
893 {
|
|
894 int e2,e3;
|
|
895
|
463
|
896 // if (inmode) error(-1);
|
502
|
897 if (e1==0) return 0;
|
462
|
898 e2 = cadr(e1);
|
|
899 switch (car(e1)){
|
|
900 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
901 case SURGVAR: case REGISTER:
|
|
902 case DREGISTER: case FREGISTER:
|
500
|
903 case FRGVAR: case DRGVAR:
|
462
|
904 case LREGISTER:
|
500
|
905 case LRGVAR: case LURGVAR:
|
551
|
906 case CONST:
|
462
|
907 case DCONST: case FCONST:
|
|
908 case LCONST:
|
|
909 case STRING:
|
|
910 case FNAME:
|
503
|
911 case FLABEL:
|
462
|
912 return e1;
|
551
|
913 case RSTRUCT:
|
|
914 // list3(RSTRUCT,e,size)
|
|
915 return pexpr(e2);
|
|
916 case LABEL:
|
|
917 return p_label_var(e2);
|
462
|
918 case LVAR:
|
|
919 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
500
|
920 case FRLVAR: case DRLVAR:
|
|
921 case LRLVAR: case LURLVAR:
|
|
922 return e1;
|
|
923 case IVAR:
|
462
|
924 return p_lvar(e1);
|
|
925 case FUNCTION:
|
|
926 return pfunction(e1);
|
|
927 case CODE:
|
601
|
928 e2 = pexpr(e2);
|
574
|
929 return list3(car(e1),e2,pexpr(cadddr(e1)));
|
601
|
930 case ARRAY:
|
|
931 e2 = pexpr(e2);
|
|
932 e1 = binop(ADD,e2,pexpr(caddr(e1)),cadddr(e1),caddddr(e1));
|
|
933 return indop(e1);
|
|
934 case PERIOD:
|
|
935 e2 = pexpr(e2);
|
|
936 nptr = (NMTBL*)caddr(e1);
|
|
937 type = cadddr(e1);
|
|
938 return strop(e2,0);
|
|
939 case ARROW:
|
|
940 e2 = pexpr(e2);
|
|
941 nptr = (NMTBL*)caddr(e1);
|
|
942 type = cadddr(e1);
|
|
943 return strop(e2,1);
|
462
|
944 case INLINE:
|
512
|
945 return p_inline(e1);
|
462
|
946 case INDIRECT:
|
527
|
947 return pindirect(e1);
|
462
|
948 case RINDIRECT: case URINDIRECT:
|
|
949 case CRINDIRECT: case CURINDIRECT:
|
|
950 case SRINDIRECT: case SURINDIRECT:
|
|
951 case FRINDIRECT: case DRINDIRECT:
|
|
952 case LRINDIRECT: case LURINDIRECT:
|
|
953 return prindirect(e1);
|
|
954 case ADDRESS:
|
502
|
955 return paddress(e1);
|
566
|
956 case ST_OP:
|
|
957 e3=caddr(e1);
|
|
958 e1=pexpr(car(e3));
|
|
959 return binop(e2,e1,pexpr(cadr(e3)),caddr(e3),cadddr(e3));
|
462
|
960 case MINUS:
|
463
|
961 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
962 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
963 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
964 return list2(car(e1),e3);
|
|
965 #if LONGLONG_CODE
|
|
966 case LMINUS:
|
463
|
967 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
968 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
969 return list2(car(e1),e3);
|
|
970 #endif
|
|
971 #if FLOAT_CODE
|
|
972 case DMINUS:
|
463
|
973 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
974 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
975 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
976 return list2(car(e1),e3);
|
|
977 case FMINUS:
|
463
|
978 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
979 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
980 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
981 return list2(car(e1),e3);
|
|
982 #endif
|
|
983 case CONV:
|
501
|
984 return p_conv(caddr(e1),e2);
|
462
|
985 case BNOT: /* ~ */
|
463
|
986 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
987 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
988 return list2(BNOT,e3);
|
|
989 case LNOT: /* ! */
|
463
|
990 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
991 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
992 return list2(LNOT,e3);
|
|
993 case PREINC:
|
|
994 case UPREINC:
|
463
|
995 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
996 if (car(e3)==CONST) return list2(CONST,cadr(e3)+caddr(e1));
|
503
|
997 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
998 case POSTINC:
|
|
999 case UPOSTINC:
|
463
|
1000 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1001 if (car(e3)==CONST) return e3;
|
503
|
1002 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1003 #if FLOAT_CODE
|
|
1004 case DPREINC: /* ++d */
|
463
|
1005 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1006 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1007 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1008 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1009 case DPOSTINC: /* d++ */
|
463
|
1010 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1011 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1012 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1013 case FPREINC: /* ++f */
|
463
|
1014 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1015 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1016 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1017 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1018 case FPOSTINC: /* f++ */
|
463
|
1019 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1020 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1021 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1022 #endif
|
|
1023 #if LONGLONG_CODE
|
|
1024 case LPREINC: /* ++d */
|
|
1025 case LUPREINC: /* ++d */
|
463
|
1026 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1027 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
1028 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1029 case LPOSTINC: /* d++ */
|
|
1030 case LUPOSTINC: /* d++ */
|
463
|
1031 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1032 if (car(e3)==LCONST) return e3;
|
503
|
1033 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1034 #endif
|
|
1035 case MUL: case UMUL:
|
|
1036 case DIV: case UDIV:
|
|
1037 case MOD: case UMOD:
|
|
1038 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
1039 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
1040 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
1041 #if FLOAT_CODE
|
|
1042 case DMUL: case DDIV:
|
|
1043 case DADD: case DSUB:
|
|
1044 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
1045 case FMUL: case FDIV:
|
|
1046 case FADD: case FSUB:
|
|
1047 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
1048 #endif
|
|
1049 #if LONGLONG_CODE
|
|
1050 case LMUL: case LUMUL:
|
|
1051 case LDIV: case LUDIV:
|
|
1052 case LMOD: case LUMOD:
|
|
1053 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
1054 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
1055 #endif
|
501
|
1056 return pbinop(car(e1),e2,caddr(e1));
|
557
|
1057 // relational operator
|
|
1058 case GT: case UGT: case GE: case UGE: case LT:
|
|
1059 case ULT: case LE: case ULE:
|
|
1060 case LOP+GT: case LOP+UGT: case LOP+GE: case LOP+UGE: case LOP+LT:
|
|
1061 case LOP+ULT: case LOP+LE: case LOP+ULE:
|
|
1062 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE:
|
|
1063 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE:
|
|
1064 case FOP+EQ: case FOP+NEQ:
|
|
1065 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ:
|
|
1066 case LOP+EQ: case LOP+NEQ:
|
|
1067 return pbinop(car(e1),e2,caddr(e1));
|
|
1068 case LAND:
|
|
1069 return pland(car(e1),cadr(e1),caddr(e1));
|
|
1070 case LOR:
|
|
1071 return plor(car(e1),cadr(e1),caddr(e1));
|
528
|
1072 case LCOND: case DCOND: case FCOND: case COND: case UCOND: case LUCOND:
|
555
|
1073 e2 = pexpr(e2);
|
|
1074 if (car(e2)==CONST) return
|
|
1075 caddr(e1)? pexpr(cadr(e2)?caddr(e1):cadddr(e1)) :
|
|
1076 pexpr(cadr(e2)?e2:cadddr(e1)); // GNU extension h?:g
|
|
1077 e3=pexpr(caddr(e1));
|
|
1078 return list4(car(e1),e2,e3,pexpr(cadddr(e1)));
|
462
|
1079 case STASS:
|
|
1080 return psassign(e1);
|
|
1081 case ASS: case CASS: case SASS:
|
|
1082 return passign(e1);
|
|
1083 case SASSOP: case SUASSOP:
|
|
1084 case ASSOP: case CASSOP: case CUASSOP:
|
|
1085 return passop(e1);
|
|
1086 #if FLOAT_CODE
|
|
1087 case FASS: case DASS:
|
|
1088 return pdassign(e1);
|
|
1089 case DASSOP: case FASSOP:
|
|
1090 return pdassop(e1);
|
|
1091 #endif
|
|
1092 #if LONGLONG_CODE
|
|
1093 case LASS:
|
|
1094 return plassign(e1);
|
|
1095 case LASSOP: case LUASSOP:
|
|
1096 return plassop(e1);
|
|
1097 #endif
|
|
1098 case ALLOCA:
|
501
|
1099 return palloc(e2);
|
462
|
1100 case BUILTINP:
|
463
|
1101 return list2(CONST,is_const(pexpr(e2)));
|
462
|
1102 case COMMA:
|
555
|
1103 return pcomma(e2,caddr(e1));
|
462
|
1104 case RETURN:
|
|
1105 case ENVIRONMENT:
|
|
1106 case LCALL:
|
|
1107 return e1;
|
|
1108 #if BIT_FIELD_CODE
|
|
1109 case RBIT_FIELD:
|
|
1110 return prbit_field(e1);
|
553
|
1111 case BIT_FIELD:
|
|
1112 return list3(BIT_FIELD,pexpr(e2),caddr(e1));
|
462
|
1113 case BASS:
|
|
1114 return pbassign(e1);
|
|
1115 case BPREINC:
|
|
1116 case BPOSTINC:
|
|
1117 case BASSOP:
|
|
1118 return pbassop(e1);
|
|
1119 #endif
|
|
1120 #if ASM_CODE
|
|
1121 case ASM:
|
|
1122 return list3(ASM,list4(
|
501
|
1123 car(e2),cadr(e2),caddr(e2),cadddr(e2)),
|
|
1124 caddr(e1));
|
462
|
1125 #endif
|
|
1126 case ST_DECL: return p_decl(e1);
|
|
1127 case ST_IF: return p_if(e1);
|
|
1128 case ST_DO: return p_do(e1);
|
|
1129 case ST_WHILE: return p_while(e1);
|
|
1130 case ST_FOR: return p_for(e1);
|
|
1131 case ST_SWITCH: return p_switch(e1);
|
|
1132 case ST_COMP: return p_comp(e1);
|
|
1133 case ST_BREAK: return p_break(e1);
|
|
1134 case ST_CONTINUE: return p_continue(e1);
|
|
1135 case ST_CASE: return p_case(e1);
|
|
1136 case ST_DEFAULT: return p_default(e1);
|
|
1137 case ST_RETURN: return p_return(e1);
|
|
1138 case ST_GOTO: return p_goto(e1);
|
|
1139 case ST_ASM: return p_asm(e1);
|
|
1140 case ST_LABEL: return p_label(e1);
|
|
1141 case ST_COMMENT: return p_comment(e1);
|
|
1142 default:
|
551
|
1143 error(-1);
|
462
|
1144 return p_bool(e1);
|
|
1145 }
|
|
1146 return VOID;
|
|
1147 }
|
|
1148
|
557
|
1149 static int
|
|
1150 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals)
|
|
1151 {
|
|
1152 int arg;
|
|
1153 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) {
|
|
1154 if (is_memory(e4)) {
|
|
1155 heap[pdisp+narg]=reference(e4);
|
|
1156 return evals;
|
|
1157 } else if (is_const(e4)) {
|
|
1158 heap[pdisp+narg]=e4;
|
|
1159 return evals;
|
|
1160 }
|
|
1161 }
|
|
1162 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),0);
|
|
1163 inline_lvars = glist2(arg,inline_lvars);
|
|
1164 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals);
|
|
1165 return evals;
|
|
1166 }
|
|
1167
|
|
1168 /*
|
|
1169 setup parameter replacement of inline call
|
|
1170 */
|
|
1171
|
|
1172 static void
|
|
1173 enter_inline(NMTBL *n, int e)
|
|
1174 {
|
|
1175 int e1 = attr_value(n,INLINE);
|
|
1176 int arg_disp = cadr(e1); // number of arguments
|
|
1177 int narg,e3,e4, e5, fargtype;
|
|
1178 int evals = 0;
|
|
1179 int t;
|
|
1180 NMTBL *anptr;
|
|
1181
|
|
1182 fnptr = n; // st_return see this
|
|
1183 pvartable = p_vartable(arg_disp, /* number of arg */
|
|
1184 caddr(e1) /* number of local parameter */);
|
|
1185
|
|
1186 /* function arguments type */
|
|
1187 fargtype = n->dsp; // we cannot do destruct reverse here
|
463
|
1188
|
557
|
1189 /* inline function arguments */
|
|
1190 narg = 0;
|
|
1191 if (!fargtype) {
|
|
1192 goto no_args; // wrong number of arguments
|
|
1193 }
|
|
1194 for (e3 = e5 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
|
1195 anptr = (NMTBL*)caddr(fargtype);
|
|
1196 if (!anptr) break; // should not happen?
|
|
1197 t=caddr(e3); // type
|
|
1198 e4 = car(e3);
|
|
1199 evals = replace_inline_parameter(anptr,t,e4,narg,evals);
|
|
1200 narg ++;
|
|
1201 fargtype = cadr(fargtype);
|
|
1202 }
|
|
1203 caddr(e) = reverse0(e5); // make it normal
|
|
1204 if (eval_order==NORMAL) evals = reverse0(evals);
|
|
1205 for(;evals;evals=cadr(evals)) {
|
|
1206 g_expr_u(car(evals));
|
|
1207 }
|
|
1208 no_args:
|
|
1209 return;
|
|
1210 }
|
|
1211
|
|
1212 /*
|
|
1213 pass local static variable list to our parents
|
|
1214 clean up and free used inline parameters
|
|
1215 */
|
|
1216
|
|
1217 static void
|
|
1218 leave_inline(int e1)
|
|
1219 {
|
|
1220 NMTBL *n;
|
|
1221 NMTBL *local_statics = (NMTBL*)cadddr(e1); // local static list
|
|
1222
|
|
1223 if (retcont) error(STERR); // inline can't handle return/environment
|
|
1224
|
|
1225 if (local_statics && local_statics != &null_nptr) {
|
|
1226 // append our local static variables to the parent list
|
|
1227 n = local_statics;
|
|
1228 while(n->next != &null_nptr) n=n->next;
|
|
1229 n->next = local_static_list; local_static_list = local_statics;
|
|
1230 cadddr(e1) = 0; // prevent duplicate initialize
|
|
1231 }
|
|
1232 while(inline_lvars) {
|
|
1233 int e;
|
|
1234 int l = car(inline_lvars);
|
|
1235 switch(car(l)) {
|
|
1236 case LVAR: free_lvar(cadr(l)); break;
|
|
1237 case REGISTER: case DREGISTER:
|
|
1238 case FREGISTER: case LREGISTER:
|
|
1239 free_register(cadr(l));
|
|
1240 }
|
|
1241 e = cadr(inline_lvars);
|
|
1242 free_glist2(inline_lvars);
|
|
1243 inline_lvars = e;
|
|
1244 }
|
|
1245 }
|
530
|
1246
|
463
|
1247 extern int
|
|
1248 gen_inline(int e)
|
462
|
1249 {
|
514
|
1250 // these saved value should be some struct
|
|
1251 int sretlabel;
|
|
1252 NMTBL *sfnptr;
|
552
|
1253 int svartable;
|
|
1254 int sretcont;
|
513
|
1255 int scslabel = cslabel;
|
463
|
1256 int sdisp = pdisp;
|
552
|
1257 int sret_register;
|
|
1258 int sret_reg_mode;
|
|
1259 int sinline_lvars;
|
|
1260 int slfree;
|
531
|
1261 // int slreg_count=lreg_count;
|
513
|
1262
|
463
|
1263 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
1264 int e1 = attr_value(n,INLINE);
|
500
|
1265 int parse = car(e1); // inline parse tree
|
557
|
1266 int dots;
|
508
|
1267 int ret_type = function_type(cadddr(e),&dots);
|
463
|
1268
|
552
|
1269 checkret();
|
|
1270 checkjmp(-1);
|
|
1271
|
|
1272 svartable = pvartable;
|
|
1273 sretcont = retcont;
|
|
1274 scslabel = cslabel;
|
|
1275 sdisp = pdisp;
|
|
1276 sret_register = ret_register;
|
|
1277 sret_reg_mode = ret_reg_mode;
|
|
1278 sinline_lvars = inline_lvars;
|
|
1279 slfree=lfree;
|
|
1280 // int slreg_count=lreg_count;
|
514
|
1281
|
|
1282 sretlabel = retlabel;
|
|
1283 sfnptr = fnptr;
|
513
|
1284 retcont = 0;
|
|
1285 cslabel = -1;
|
|
1286 retpending = 0;
|
514
|
1287 ret_reg_mode = 0;
|
|
1288 ret_register = 5555;
|
527
|
1289 inline_lvars = 0;
|
514
|
1290 retlabel = fwdlabel();
|
513
|
1291
|
557
|
1292 enter_inline(n,e);
|
526
|
1293
|
557
|
1294 // partial evaluation of parse tree
|
|
1295 // constant propergation, etc.
|
|
1296 parse = pexpr(parse);
|
463
|
1297 pdisp = sdisp;
|
464
|
1298 pvartable = svartable;
|
513
|
1299
|
557
|
1300 // generate code if necessary
|
513
|
1301 if (ret_type!=VOID)
|
557
|
1302 g_expr0(parse);
|
513
|
1303 else
|
557
|
1304 g_expr_u(parse);
|
|
1305
|
524
|
1306 checkret();
|
513
|
1307 fwddef(retlabel);
|
514
|
1308 control=1;
|
513
|
1309
|
557
|
1310 leave_inline(e1);
|
510
|
1311
|
513
|
1312 fnptr = sfnptr;
|
|
1313 retlabel = sretlabel;
|
|
1314 retcont = sretcont;
|
|
1315 cslabel = scslabel;
|
514
|
1316 ret_register = sret_register;
|
|
1317 ret_reg_mode = sret_reg_mode;
|
527
|
1318 inline_lvars = sinline_lvars;
|
528
|
1319 lfree=slfree;
|
513
|
1320
|
508
|
1321 return ret_type;
|
462
|
1322 }
|
|
1323
|
|
1324 /* end */
|