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 {
|
501
|
502 return list2(car(e),pexpr(cadr(e)));
|
462
|
503 }
|
|
504
|
|
505 static int
|
464
|
506 p_conv(int e1,int e2)
|
462
|
507 {
|
557
|
508 int t;
|
|
509 if (is_const(e2) && (t=type_of_conv(e1))) {
|
|
510 return correct_type(e2,t);
|
|
511 }
|
527
|
512 return list3(CONV,pexpr(e2),e1);
|
462
|
513 }
|
|
514
|
|
515 static int
|
464
|
516 pbinop(int op,int e1,int e2)
|
462
|
517 {
|
557
|
518 e1 = pexpr(e1);
|
|
519 e2 = pexpr(e2);
|
|
520 if (is_const(e1)&&is_const(e2)) {
|
|
521 int t;
|
|
522 if((t= type_of_bop(op)))
|
599
|
523 return binop(OP(op),e1,e2,t,t);
|
557
|
524 }
|
|
525 return list3(op,e1,e2);
|
502
|
526 }
|
|
527
|
|
528 static int
|
526
|
529 plor(int op,int e1,int e2)
|
|
530 {
|
555
|
531 int e = pexpr(e1);
|
|
532 return list3(op,e,pexpr(e2));
|
526
|
533 }
|
|
534
|
|
535 static int
|
|
536 pland(int op,int e1,int e2)
|
|
537 {
|
555
|
538 int e = pexpr(e1);
|
|
539 return list3(op,e,pexpr(e2));
|
526
|
540 }
|
|
541
|
|
542 static int
|
462
|
543 psassign(int e)
|
|
544 {
|
555
|
545 int e1 = pexpr(cadr(e));
|
|
546 int e2 = pexpr(caddr(e));
|
|
547 return list4(car(e),e1,e2,cadddr(e));
|
462
|
548 }
|
|
549
|
|
550 static int
|
|
551 passign(int e)
|
|
552 {
|
555
|
553 int e1 = pexpr(cadr(e));
|
|
554 int e2 = pexpr(caddr(e));
|
|
555 return list3(car(e),e1,e2);
|
462
|
556 }
|
|
557
|
|
558 static int
|
|
559 passop(int e)
|
|
560 {
|
555
|
561 int e1 = pexpr(cadr(e));
|
|
562 int e2 = pexpr(caddr(e));
|
|
563 return list4(car(e),e1,e2,cadddr(e));
|
462
|
564 }
|
|
565
|
|
566 static int
|
|
567 pdassign(int e)
|
|
568 {
|
555
|
569 int e1 = pexpr(cadr(e));
|
|
570 int e2 = pexpr(caddr(e));
|
|
571 return list3(car(e),e1,e2);
|
462
|
572 }
|
|
573
|
|
574 static int
|
|
575 pdassop(int e)
|
|
576 {
|
555
|
577 int e1 = pexpr(cadr(e));
|
|
578 int e2 = pexpr(caddr(e));
|
|
579 return list4(car(e),e1,e2,cadddr(e));
|
462
|
580 }
|
|
581
|
|
582 static int
|
|
583 plassign(int e)
|
|
584 {
|
555
|
585 int e1 = pexpr(cadr(e));
|
|
586 int e2 = pexpr(caddr(e));
|
|
587 return list3(car(e),e1,e2);
|
462
|
588 }
|
|
589
|
|
590 static int
|
|
591 plassop(int e)
|
|
592 {
|
555
|
593 int e1 = pexpr(cadr(e));
|
|
594 int e2 = pexpr(caddr(e));
|
|
595 return list4(car(e),e1,e2,cadddr(e));
|
462
|
596 }
|
|
597
|
|
598 static int
|
|
599 palloc(int e)
|
|
600 {
|
501
|
601 return list2(car(e),pexpr(cadr(e)));
|
462
|
602 }
|
|
603
|
|
604 static int
|
464
|
605 pcomma(int e1,int e2)
|
462
|
606 {
|
556
|
607 int e = pexpr(e1);
|
555
|
608 return list3(COMMA,e,pexpr(e2));
|
462
|
609 }
|
|
610
|
|
611 static int
|
|
612 prbit_field(int e)
|
|
613 {
|
501
|
614 return list3(car(e),pexpr(cadr(e)),caddr(e));
|
462
|
615 }
|
|
616
|
|
617 static int
|
|
618 pbassign(int e)
|
|
619 {
|
553
|
620 // list4(BASS,e1,e2,list2(BASS,t)));
|
|
621 int e1=pexpr(caddr(e));
|
|
622 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
623 }
|
|
624
|
|
625 static int
|
|
626 pbassop(int e)
|
|
627 {
|
553
|
628 int e1=pexpr(caddr(e));
|
|
629 return list4(car(e),pexpr(cadr(e)),e1,cadddr(e));
|
462
|
630 }
|
|
631
|
561
|
632 // handle local variable declaration
|
|
633 // initialization is accumrated in parse tree
|
|
634 // should consider int k=some_compile_time_constant;
|
462
|
635 static int
|
|
636 p_decl(int e)
|
|
637 {
|
552
|
638 // list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode));
|
554
|
639 int ctmode=cadddr(e);
|
463
|
640 NMTBL *n=(NMTBL*)caddr(e);
|
|
641 int dsp = n->dsp;
|
569
|
642 int v=0;
|
552
|
643 int sstmode = stmode;
|
|
644 int smode = mode;
|
500
|
645 // in real partial evaluation, we have to check whether this variable
|
|
646 // is used or not.
|
552
|
647 if (ctmode) {
|
|
648 mode = car(ctmode); stmode = cadr(ctmode); ctmode = caddr(ctmode);
|
574
|
649 } else {
|
|
650 mode = LDECL; stmode = 0; ctmode = 0;
|
552
|
651 }
|
463
|
652 switch(stmode) {
|
528
|
653 case EXTRN: case EXTRN1: case STATIC:
|
552
|
654 // def(n,ctmode); we don't need this. already done.
|
|
655 // stmode = sstmode;
|
|
656 stmode = sstmode;
|
|
657 mode = smode;
|
|
658 return pexpr(cadr(e));
|
|
659 // case LLDECL: LLDECL is mode, not stmode (bad design)
|
|
660 // v = list2(FLABEL,fwdlabel()); break;
|
529
|
661 #if 1
|
463
|
662 case REGISTER:
|
|
663 switch(n->ty) {
|
|
664 case ULONGLONG: case LONGLONG:
|
|
665 v = get_lregister_var(n); break;
|
|
666 case FLOAT:
|
|
667 v = get_dregister_var(n,0); break;
|
|
668 case DOUBLE:
|
|
669 v = get_dregister_var(n,1); break;
|
|
670 default:
|
|
671 if (scalar(n->ty))
|
524
|
672 v = get_register_var(n);
|
463
|
673 else
|
|
674 error(TYERR);
|
|
675 }
|
529
|
676 break;
|
528
|
677 #endif
|
463
|
678 default:
|
552
|
679 if (n->sc==FLABEL)
|
|
680 v = list3(LVAR,fwdlabel(),(int)n);
|
|
681 else
|
|
682 v = list3(LVAR,new_lvar(size(n->ty)),(int)n);
|
463
|
683 }
|
552
|
684 if (n->sc!=FLABEL)
|
|
685 inline_lvars = glist2(v,inline_lvars);
|
574
|
686 if (heap[pdisp+dsp]) {
|
|
687 fprintf(stderr,"## double p_decl %s %s\n",((NMTBL*)(caddr(heap[pdisp+dsp])))->nm,n->nm);
|
|
688 error(-1);
|
|
689 }
|
463
|
690 heap[pdisp+dsp]=v;
|
552
|
691 stmode = sstmode;
|
|
692 mode = smode;
|
463
|
693 return pexpr(cadr(e));
|
462
|
694 }
|
|
695
|
|
696 static int
|
500
|
697 p_if(int e1)
|
462
|
698 {
|
500
|
699 int cond,l1,l2;
|
|
700 int e2=caddr(e1),e3;
|
|
701 cond = pexpr(car(e2));
|
|
702 // conv->if_then_();
|
|
703 l1 = pexpr(cadr(e2));
|
|
704 if ((e3=caddr(e2))) { // else
|
|
705 l2 = pexpr(e3);
|
|
706 } else {
|
|
707 l2 = 0;
|
|
708 }
|
|
709 return list3(ST_IF,pexpr(cadr(e1)),list3(cond,l1,l2));
|
462
|
710 }
|
|
711
|
|
712 static int
|
|
713 p_do(int e)
|
|
714 {
|
524
|
715 int e2 = pexpr(caddr(e));
|
|
716 int e3 = pexpr(cadddr(e));
|
|
717 return list4(ST_DO,pexpr(cadr(e)),e2,e3);
|
462
|
718 }
|
|
719
|
|
720 static int
|
|
721 p_while(int e)
|
|
722 {
|
524
|
723 int e2 = pexpr(caddr(e));
|
|
724 int e3 = pexpr(cadddr(e));
|
|
725 return list4(ST_WHILE,pexpr(cadr(e)),e2,e3);
|
462
|
726 }
|
|
727
|
|
728 static int
|
|
729 p_for(int e)
|
|
730 {
|
501
|
731 int e1=caddr(e);
|
524
|
732 int p0=pexpr(car(e1));
|
|
733 int p1=pexpr(cadr(e1));
|
|
734 int p2=pexpr(caddr(e1));
|
|
735 int p3=pexpr(cadddr(e1));
|
|
736 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
|
462
|
737 }
|
|
738
|
|
739 static int
|
|
740 p_switch(int e)
|
|
741 {
|
524
|
742 int e2 = pexpr(caddr(e));
|
|
743 int e3 = pexpr(cadddr(e));
|
|
744 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
|
462
|
745 }
|
|
746
|
|
747 static int
|
|
748 p_comp(int e)
|
|
749 {
|
524
|
750 int e1=pexpr(caddr(e));
|
|
751 return list3(ST_COMP,pexpr(cadr(e)),e1);
|
462
|
752 }
|
|
753
|
|
754 static int
|
|
755 p_break(int e)
|
|
756 {
|
502
|
757 return list2(ST_BREAK,pexpr(cadr(e)));
|
462
|
758 }
|
|
759
|
|
760 static int
|
|
761 p_continue(int e)
|
|
762 {
|
502
|
763 return list2(ST_CONTINUE,pexpr(cadr(e)));
|
462
|
764 }
|
|
765
|
|
766 static int
|
|
767 p_case(int e)
|
|
768 {
|
506
|
769 int new=0,clist = caddr(e);
|
|
770 // insert destory clist, we have to copy it now
|
|
771 for(;clist;clist=cadr(clist))
|
|
772 new=glist3(car(clist),new,0);
|
|
773 return list3(ST_CASE,pexpr(cadr(e)),reverse0(new));
|
462
|
774 }
|
|
775
|
|
776 static int
|
|
777 p_default(int e)
|
|
778 {
|
558
|
779 // should be removed if constant case value
|
502
|
780 return list2(ST_DEFAULT,pexpr(cadr(e)));
|
462
|
781 }
|
|
782
|
|
783 static int
|
|
784 p_return(int e)
|
|
785 {
|
524
|
786 int e1=pexpr(caddr(e));
|
|
787 return list3(ST_RETURN,pexpr(cadr(e)),e1);
|
462
|
788 }
|
|
789
|
|
790 static int
|
|
791 p_goto(int e)
|
|
792 {
|
555
|
793 int e1,lb,e2;
|
501
|
794 if ((e1=caddr(e))) {
|
|
795 switch(car(e1)) {
|
|
796 case RINDIRECT: e1=pexpr(e1); break;
|
555
|
797 case CODE: e2=pexpr(cadr(e1));
|
|
798 e1=list3(CODE,e2,pexpr(caddr(e1))); break;
|
551
|
799 case FLABEL: /* error(-1); */ break;
|
|
800 case IVAR:
|
|
801 lb = cadr(e1);
|
|
802 if (!(e1=heap[pdisp+lb])) {
|
|
803 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
804 }
|
501
|
805 }
|
|
806 }
|
502
|
807 return list3(ST_GOTO,pexpr(cadr(e)),e1);
|
501
|
808 }
|
|
809
|
|
810 static int
|
|
811 p_list_expr(int e)
|
|
812 {
|
|
813 int e3,new = 0;
|
|
814 for (e3 = e; e3; e3 = cadr(e3)) {
|
|
815 new= list2( pexpr(car(e3)), new);
|
|
816 }
|
|
817 return reverse0(new);
|
462
|
818 }
|
|
819
|
|
820 static int
|
|
821 p_asm(int e)
|
|
822 {
|
501
|
823 int param=caddr(e);
|
|
824 int e1 = p_list_expr(cadddr(e));
|
|
825 return list4(ST_ASM,pexpr(cadr(e)),param,e1);
|
462
|
826 }
|
|
827
|
|
828 static int
|
|
829 p_label(int e)
|
|
830 {
|
551
|
831 int e1,lb;
|
|
832 if ((e1=caddr(e))) {
|
|
833 switch(car(e1)) {
|
|
834 case FLABEL: /* error(-1); */ break;
|
|
835 case IVAR:
|
|
836 lb = cadr(e1);
|
|
837 if (!(e1=heap[pdisp+lb])) {
|
|
838 e1 = heap[pdisp+lb]=list2(FLABEL,fwdlabel());
|
|
839 }
|
|
840 }
|
|
841 }
|
|
842 return list3(ST_LABEL,pexpr(cadr(e)),e1);
|
|
843 }
|
|
844
|
|
845 static int
|
|
846 p_label_var(int e)
|
|
847 {
|
|
848 int d,e1;
|
|
849 switch(car(e1=e)) {
|
|
850 case LVAR: /* error(-1) */ break;
|
|
851 case IVAR:
|
|
852 // should this done in p_decl? (in __label__?)
|
|
853 d = cadr(e);
|
|
854 if (!(e1=(heap[pdisp+d]))) {
|
552
|
855 // error(-1);
|
551
|
856 e1 = heap[pdisp+d]=list3(LVAR,fwdlabel(),caddr(e));
|
|
857 }
|
|
858 }
|
|
859 return list2(LABEL,e1);
|
462
|
860 }
|
|
861
|
|
862 static int
|
|
863 p_bool(int e)
|
|
864 {
|
501
|
865 error(-1);
|
464
|
866 return e;
|
|
867 }
|
462
|
868
|
464
|
869 static int
|
|
870 p_comment(int e)
|
|
871 {
|
574
|
872 glineno++;
|
501
|
873 return list3(ST_COMMENT,pexpr(cadr(e)),caddr(e));
|
462
|
874 }
|
|
875
|
508
|
876 static int
|
512
|
877 p_inline(int e)
|
508
|
878 {
|
|
879 int e3;
|
|
880 int narg;
|
|
881
|
|
882 /* inline function arguments */
|
|
883 narg = 0;
|
|
884 for (e3 = caddr(e); e3; e3 = cadr(e3)) {
|
|
885 narg=list3(pexpr(car(e3)),narg,caddr(e3));
|
|
886 }
|
|
887 return list4(INLINE,cadr(e),reverse0(narg),cadddr(e));
|
|
888 }
|
|
889
|
463
|
890 extern int
|
|
891 pexpr(int e1)
|
462
|
892 {
|
|
893 int e2,e3;
|
|
894
|
463
|
895 // if (inmode) error(-1);
|
502
|
896 if (e1==0) return 0;
|
462
|
897 e2 = cadr(e1);
|
|
898 switch (car(e1)){
|
|
899 case GVAR: case RGVAR: case CRGVAR: case CURGVAR: case SRGVAR:
|
|
900 case SURGVAR: case REGISTER:
|
|
901 case DREGISTER: case FREGISTER:
|
500
|
902 case FRGVAR: case DRGVAR:
|
462
|
903 case LREGISTER:
|
500
|
904 case LRGVAR: case LURGVAR:
|
551
|
905 case CONST:
|
462
|
906 case DCONST: case FCONST:
|
|
907 case LCONST:
|
|
908 case STRING:
|
|
909 case FNAME:
|
503
|
910 case FLABEL:
|
462
|
911 return e1;
|
551
|
912 case RSTRUCT:
|
|
913 // list3(RSTRUCT,e,size)
|
|
914 return pexpr(e2);
|
|
915 case LABEL:
|
|
916 return p_label_var(e2);
|
462
|
917 case LVAR:
|
|
918 case RLVAR: case CRLVAR: case CURLVAR: case SRLVAR: case SURLVAR:
|
500
|
919 case FRLVAR: case DRLVAR:
|
|
920 case LRLVAR: case LURLVAR:
|
|
921 return e1;
|
|
922 case IVAR:
|
462
|
923 return p_lvar(e1);
|
|
924 case FUNCTION:
|
|
925 return pfunction(e1);
|
|
926 case CODE:
|
574
|
927 e2=pexpr(e2);
|
|
928 return list3(car(e1),e2,pexpr(cadddr(e1)));
|
462
|
929 case INLINE:
|
512
|
930 return p_inline(e1);
|
462
|
931 case INDIRECT:
|
527
|
932 return pindirect(e1);
|
462
|
933 case RINDIRECT: case URINDIRECT:
|
|
934 case CRINDIRECT: case CURINDIRECT:
|
|
935 case SRINDIRECT: case SURINDIRECT:
|
|
936 case FRINDIRECT: case DRINDIRECT:
|
|
937 case LRINDIRECT: case LURINDIRECT:
|
|
938 return prindirect(e1);
|
|
939 case ADDRESS:
|
502
|
940 return paddress(e1);
|
566
|
941 case ST_OP:
|
|
942 e3=caddr(e1);
|
|
943 e1=pexpr(car(e3));
|
|
944 return binop(e2,e1,pexpr(cadr(e3)),caddr(e3),cadddr(e3));
|
462
|
945 case MINUS:
|
463
|
946 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
947 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
948 if (car(e3)==CONST) return list2(CONST,-cadr(e3));
|
|
949 return list2(car(e1),e3);
|
|
950 #if LONGLONG_CODE
|
|
951 case LMINUS:
|
463
|
952 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
953 if (car(e3)==LCONST) return llist2(LCONST,-lcadr(e3));
|
|
954 return list2(car(e1),e3);
|
|
955 #endif
|
|
956 #if FLOAT_CODE
|
|
957 case DMINUS:
|
463
|
958 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
959 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
960 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
961 return list2(car(e1),e3);
|
|
962 case FMINUS:
|
463
|
963 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
964 if (car(e3)==DCONST) return dlist2(DCONST,-dcadr(e3));
|
|
965 if (car(e3)==FCONST) return dlist2(FCONST,-dcadr(e3));
|
|
966 return list2(car(e1),e3);
|
|
967 #endif
|
|
968 case CONV:
|
501
|
969 return p_conv(caddr(e1),e2);
|
462
|
970 case BNOT: /* ~ */
|
463
|
971 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
972 if (car(e3)==CONST) return list2(CONST,~cadr(e3));
|
|
973 return list2(BNOT,e3);
|
|
974 case LNOT: /* ! */
|
463
|
975 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
976 if (car(e3)==CONST) return list2(CONST,!cadr(e3));
|
|
977 return list2(LNOT,e3);
|
|
978 case PREINC:
|
|
979 case UPREINC:
|
463
|
980 if ((e3 = pexpr(e2))==e2) return e1;
|
557
|
981 if (car(e3)==CONST) return list2(CONST,cadr(e3)+caddr(e1));
|
503
|
982 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
983 case POSTINC:
|
|
984 case UPOSTINC:
|
463
|
985 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
986 if (car(e3)==CONST) return e3;
|
503
|
987 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
988 #if FLOAT_CODE
|
|
989 case DPREINC: /* ++d */
|
463
|
990 if ((e3 = pexpr(e2))==e2) return e1;
|
|
991 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
992 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
993 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
994 case DPOSTINC: /* d++ */
|
463
|
995 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
996 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
997 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
998 case FPREINC: /* ++f */
|
463
|
999 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1000 if (car(e3)==FCONST) return dlist2(FCONST,dcadr(e3)+cadr(e2));
|
|
1001 if (car(e3)==DCONST) return dlist2(DCONST,dcadr(e3)+cadr(e2));
|
503
|
1002 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1003 case FPOSTINC: /* f++ */
|
463
|
1004 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1005 if (car(e3)==FCONST||car(e3)==DCONST) return e3;
|
503
|
1006 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1007 #endif
|
|
1008 #if LONGLONG_CODE
|
|
1009 case LPREINC: /* ++d */
|
|
1010 case LUPREINC: /* ++d */
|
463
|
1011 if ((e3 = pexpr(e2))==e2) return e1;
|
|
1012 if (car(e3)==LCONST) return llist2(LCONST,lcadr(e3)+cadr(e2));
|
503
|
1013 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1014 case LPOSTINC: /* d++ */
|
|
1015 case LUPOSTINC: /* d++ */
|
463
|
1016 if ((e3 = pexpr(e2))==e2) return e1;
|
462
|
1017 if (car(e3)==LCONST) return e3;
|
503
|
1018 return list4(car(e1),e3,caddr(e1),cadddr(e1));
|
462
|
1019 #endif
|
|
1020 case MUL: case UMUL:
|
|
1021 case DIV: case UDIV:
|
|
1022 case MOD: case UMOD:
|
|
1023 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
|
|
1024 case ADD: case SUB: case BAND: case EOR: case BOR: case CMP: case CMPGE:
|
|
1025 case UCMP: case CMPEQ: case CMPNEQ: case UCMPGE:
|
|
1026 #if FLOAT_CODE
|
|
1027 case DMUL: case DDIV:
|
|
1028 case DADD: case DSUB:
|
|
1029 case DCMP: case DCMPGE: case DCMPEQ: case DCMPNEQ:
|
|
1030 case FMUL: case FDIV:
|
|
1031 case FADD: case FSUB:
|
|
1032 case FCMP: case FCMPGE: case FCMPEQ: case FCMPNEQ:
|
|
1033 #endif
|
|
1034 #if LONGLONG_CODE
|
|
1035 case LMUL: case LUMUL:
|
|
1036 case LDIV: case LUDIV:
|
|
1037 case LMOD: case LUMOD:
|
|
1038 case LLSHIFT: case LULSHIFT: case LRSHIFT: case LURSHIFT:
|
|
1039 case LADD: case LSUB: case LBAND: case LEOR: case LBOR: case LCMP:
|
|
1040 #endif
|
501
|
1041 return pbinop(car(e1),e2,caddr(e1));
|
557
|
1042 // relational operator
|
|
1043 case GT: case UGT: case GE: case UGE: case LT:
|
|
1044 case ULT: case LE: case ULE:
|
|
1045 case LOP+GT: case LOP+UGT: case LOP+GE: case LOP+UGE: case LOP+LT:
|
|
1046 case LOP+ULT: case LOP+LE: case LOP+ULE:
|
|
1047 case DOP+GT: case DOP+GE: case DOP+LT: case DOP+LE:
|
|
1048 case FOP+GT: case FOP+GE: case FOP+LT: case FOP+LE:
|
|
1049 case FOP+EQ: case FOP+NEQ:
|
|
1050 case EQ: case NEQ: case DOP+EQ: case DOP+NEQ:
|
|
1051 case LOP+EQ: case LOP+NEQ:
|
|
1052 return pbinop(car(e1),e2,caddr(e1));
|
|
1053 case LAND:
|
|
1054 return pland(car(e1),cadr(e1),caddr(e1));
|
|
1055 case LOR:
|
|
1056 return plor(car(e1),cadr(e1),caddr(e1));
|
528
|
1057 case LCOND: case DCOND: case FCOND: case COND: case UCOND: case LUCOND:
|
555
|
1058 e2 = pexpr(e2);
|
|
1059 if (car(e2)==CONST) return
|
|
1060 caddr(e1)? pexpr(cadr(e2)?caddr(e1):cadddr(e1)) :
|
|
1061 pexpr(cadr(e2)?e2:cadddr(e1)); // GNU extension h?:g
|
|
1062 e3=pexpr(caddr(e1));
|
|
1063 return list4(car(e1),e2,e3,pexpr(cadddr(e1)));
|
462
|
1064 case STASS:
|
|
1065 return psassign(e1);
|
|
1066 case ASS: case CASS: case SASS:
|
|
1067 return passign(e1);
|
|
1068 case SASSOP: case SUASSOP:
|
|
1069 case ASSOP: case CASSOP: case CUASSOP:
|
|
1070 return passop(e1);
|
|
1071 #if FLOAT_CODE
|
|
1072 case FASS: case DASS:
|
|
1073 return pdassign(e1);
|
|
1074 case DASSOP: case FASSOP:
|
|
1075 return pdassop(e1);
|
|
1076 #endif
|
|
1077 #if LONGLONG_CODE
|
|
1078 case LASS:
|
|
1079 return plassign(e1);
|
|
1080 case LASSOP: case LUASSOP:
|
|
1081 return plassop(e1);
|
|
1082 #endif
|
|
1083 case ALLOCA:
|
501
|
1084 return palloc(e2);
|
462
|
1085 case BUILTINP:
|
463
|
1086 return list2(CONST,is_const(pexpr(e2)));
|
462
|
1087 case COMMA:
|
555
|
1088 return pcomma(e2,caddr(e1));
|
462
|
1089 case RETURN:
|
|
1090 case ENVIRONMENT:
|
|
1091 case LCALL:
|
|
1092 return e1;
|
|
1093 #if BIT_FIELD_CODE
|
|
1094 case RBIT_FIELD:
|
|
1095 return prbit_field(e1);
|
553
|
1096 case BIT_FIELD:
|
|
1097 return list3(BIT_FIELD,pexpr(e2),caddr(e1));
|
462
|
1098 case BASS:
|
|
1099 return pbassign(e1);
|
|
1100 case BPREINC:
|
|
1101 case BPOSTINC:
|
|
1102 case BASSOP:
|
|
1103 return pbassop(e1);
|
|
1104 #endif
|
|
1105 #if ASM_CODE
|
|
1106 case ASM:
|
|
1107 return list3(ASM,list4(
|
501
|
1108 car(e2),cadr(e2),caddr(e2),cadddr(e2)),
|
|
1109 caddr(e1));
|
462
|
1110 #endif
|
|
1111 case ST_DECL: return p_decl(e1);
|
|
1112 case ST_IF: return p_if(e1);
|
|
1113 case ST_DO: return p_do(e1);
|
|
1114 case ST_WHILE: return p_while(e1);
|
|
1115 case ST_FOR: return p_for(e1);
|
|
1116 case ST_SWITCH: return p_switch(e1);
|
|
1117 case ST_COMP: return p_comp(e1);
|
|
1118 case ST_BREAK: return p_break(e1);
|
|
1119 case ST_CONTINUE: return p_continue(e1);
|
|
1120 case ST_CASE: return p_case(e1);
|
|
1121 case ST_DEFAULT: return p_default(e1);
|
|
1122 case ST_RETURN: return p_return(e1);
|
|
1123 case ST_GOTO: return p_goto(e1);
|
|
1124 case ST_ASM: return p_asm(e1);
|
|
1125 case ST_LABEL: return p_label(e1);
|
|
1126 case ST_COMMENT: return p_comment(e1);
|
|
1127 default:
|
551
|
1128 error(-1);
|
462
|
1129 return p_bool(e1);
|
|
1130 }
|
|
1131 return VOID;
|
|
1132 }
|
|
1133
|
557
|
1134 static int
|
|
1135 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals)
|
|
1136 {
|
|
1137 int arg;
|
|
1138 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) {
|
|
1139 if (is_memory(e4)) {
|
|
1140 heap[pdisp+narg]=reference(e4);
|
|
1141 return evals;
|
|
1142 } else if (is_const(e4)) {
|
|
1143 heap[pdisp+narg]=e4;
|
|
1144 return evals;
|
|
1145 }
|
|
1146 }
|
|
1147 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),0);
|
|
1148 inline_lvars = glist2(arg,inline_lvars);
|
|
1149 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals);
|
|
1150 return evals;
|
|
1151 }
|
|
1152
|
|
1153 /*
|
|
1154 setup parameter replacement of inline call
|
|
1155 */
|
|
1156
|
|
1157 static void
|
|
1158 enter_inline(NMTBL *n, int e)
|
|
1159 {
|
|
1160 int e1 = attr_value(n,INLINE);
|
|
1161 int arg_disp = cadr(e1); // number of arguments
|
|
1162 int narg,e3,e4, e5, fargtype;
|
|
1163 int evals = 0;
|
|
1164 int t;
|
|
1165 NMTBL *anptr;
|
|
1166
|
|
1167 fnptr = n; // st_return see this
|
|
1168 pvartable = p_vartable(arg_disp, /* number of arg */
|
|
1169 caddr(e1) /* number of local parameter */);
|
|
1170
|
|
1171 /* function arguments type */
|
|
1172 fargtype = n->dsp; // we cannot do destruct reverse here
|
463
|
1173
|
557
|
1174 /* inline function arguments */
|
|
1175 narg = 0;
|
|
1176 if (!fargtype) {
|
|
1177 goto no_args; // wrong number of arguments
|
|
1178 }
|
|
1179 for (e3 = e5 = reverse0(caddr(e)); e3; e3 = cadr(e3)) {
|
|
1180 anptr = (NMTBL*)caddr(fargtype);
|
|
1181 if (!anptr) break; // should not happen?
|
|
1182 t=caddr(e3); // type
|
|
1183 e4 = car(e3);
|
|
1184 evals = replace_inline_parameter(anptr,t,e4,narg,evals);
|
|
1185 narg ++;
|
|
1186 fargtype = cadr(fargtype);
|
|
1187 }
|
|
1188 caddr(e) = reverse0(e5); // make it normal
|
|
1189 if (eval_order==NORMAL) evals = reverse0(evals);
|
|
1190 for(;evals;evals=cadr(evals)) {
|
|
1191 g_expr_u(car(evals));
|
|
1192 }
|
|
1193 no_args:
|
|
1194 return;
|
|
1195 }
|
|
1196
|
|
1197 /*
|
|
1198 pass local static variable list to our parents
|
|
1199 clean up and free used inline parameters
|
|
1200 */
|
|
1201
|
|
1202 static void
|
|
1203 leave_inline(int e1)
|
|
1204 {
|
|
1205 NMTBL *n;
|
|
1206 NMTBL *local_statics = (NMTBL*)cadddr(e1); // local static list
|
|
1207
|
|
1208 if (retcont) error(STERR); // inline can't handle return/environment
|
|
1209
|
|
1210 if (local_statics && local_statics != &null_nptr) {
|
|
1211 // append our local static variables to the parent list
|
|
1212 n = local_statics;
|
|
1213 while(n->next != &null_nptr) n=n->next;
|
|
1214 n->next = local_static_list; local_static_list = local_statics;
|
|
1215 cadddr(e1) = 0; // prevent duplicate initialize
|
|
1216 }
|
|
1217 while(inline_lvars) {
|
|
1218 int e;
|
|
1219 int l = car(inline_lvars);
|
|
1220 switch(car(l)) {
|
|
1221 case LVAR: free_lvar(cadr(l)); break;
|
|
1222 case REGISTER: case DREGISTER:
|
|
1223 case FREGISTER: case LREGISTER:
|
|
1224 free_register(cadr(l));
|
|
1225 }
|
|
1226 e = cadr(inline_lvars);
|
|
1227 free_glist2(inline_lvars);
|
|
1228 inline_lvars = e;
|
|
1229 }
|
|
1230 }
|
530
|
1231
|
463
|
1232 extern int
|
|
1233 gen_inline(int e)
|
462
|
1234 {
|
514
|
1235 // these saved value should be some struct
|
|
1236 int sretlabel;
|
|
1237 NMTBL *sfnptr;
|
552
|
1238 int svartable;
|
|
1239 int sretcont;
|
513
|
1240 int scslabel = cslabel;
|
463
|
1241 int sdisp = pdisp;
|
552
|
1242 int sret_register;
|
|
1243 int sret_reg_mode;
|
|
1244 int sinline_lvars;
|
|
1245 int slfree;
|
531
|
1246 // int slreg_count=lreg_count;
|
513
|
1247
|
463
|
1248 NMTBL *n = (NMTBL*)cadr(cadr(e));
|
|
1249 int e1 = attr_value(n,INLINE);
|
500
|
1250 int parse = car(e1); // inline parse tree
|
557
|
1251 int dots;
|
508
|
1252 int ret_type = function_type(cadddr(e),&dots);
|
463
|
1253
|
552
|
1254 checkret();
|
|
1255 checkjmp(-1);
|
|
1256
|
|
1257 svartable = pvartable;
|
|
1258 sretcont = retcont;
|
|
1259 scslabel = cslabel;
|
|
1260 sdisp = pdisp;
|
|
1261 sret_register = ret_register;
|
|
1262 sret_reg_mode = ret_reg_mode;
|
|
1263 sinline_lvars = inline_lvars;
|
|
1264 slfree=lfree;
|
|
1265 // int slreg_count=lreg_count;
|
514
|
1266
|
|
1267 sretlabel = retlabel;
|
|
1268 sfnptr = fnptr;
|
513
|
1269 retcont = 0;
|
|
1270 cslabel = -1;
|
|
1271 retpending = 0;
|
514
|
1272 ret_reg_mode = 0;
|
|
1273 ret_register = 5555;
|
527
|
1274 inline_lvars = 0;
|
514
|
1275 retlabel = fwdlabel();
|
513
|
1276
|
557
|
1277 enter_inline(n,e);
|
526
|
1278
|
557
|
1279 // partial evaluation of parse tree
|
|
1280 // constant propergation, etc.
|
|
1281 parse = pexpr(parse);
|
463
|
1282 pdisp = sdisp;
|
464
|
1283 pvartable = svartable;
|
513
|
1284
|
557
|
1285 // generate code if necessary
|
513
|
1286 if (ret_type!=VOID)
|
557
|
1287 g_expr0(parse);
|
513
|
1288 else
|
557
|
1289 g_expr_u(parse);
|
|
1290
|
524
|
1291 checkret();
|
513
|
1292 fwddef(retlabel);
|
514
|
1293 control=1;
|
513
|
1294
|
557
|
1295 leave_inline(e1);
|
510
|
1296
|
513
|
1297 fnptr = sfnptr;
|
|
1298 retlabel = sretlabel;
|
|
1299 retcont = sretcont;
|
|
1300 cslabel = scslabel;
|
514
|
1301 ret_register = sret_register;
|
|
1302 ret_reg_mode = sret_reg_mode;
|
527
|
1303 inline_lvars = sinline_lvars;
|
528
|
1304 lfree=slfree;
|
513
|
1305
|
508
|
1306 return ret_type;
|
462
|
1307 }
|
|
1308
|
|
1309 /* end */
|