Mercurial > hg > CbC > old > device
comparison mc-codegen.c @ 83:f3f75911d62c
*** empty log message ***
author | kono |
---|---|
date | Wed, 05 Mar 2003 03:41:08 +0900 |
parents | 25654dc29ecc |
children | 1a723130a2c7 |
comparison
equal
deleted
inserted
replaced
82:25654dc29ecc | 83:f3f75911d62c |
---|---|
8 | 8 |
9 int creg; /* current register */ | 9 int creg; /* current register */ |
10 int dreg; /* temporary register */ | 10 int dreg; /* temporary register */ |
11 int reg_sp; /* REGister Stack-Pointer */ | 11 int reg_sp; /* REGister Stack-Pointer */ |
12 | 12 |
13 int rname[MAX_MAX]; | |
14 int regs[MAX_MAX]; /* 使われているレジスタを示すフラグ */ | |
15 int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ | |
16 int regv[MAX_MAX]; /* 値が入っているかどうか */ | |
17 | |
18 /* floating point registers */ | |
19 | |
13 int freg; /* current floating point register */ | 20 int freg; /* current floating point register */ |
21 int greg; /* current floating point register */ | |
22 int freg_sp; /* floating point REGister Stack-Pointer */ | |
23 | |
24 int frname[MAX_MAX]; | |
25 int fregs[MAX_MAX]; /* 使われているレジスタを示すフラグ */ | |
26 int freg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ | |
27 int fregv[MAX_MAX]; /* 値が入っているかどうか */ | |
28 | |
29 int use; /* generated value will be used */ | |
14 | 30 |
15 /* | 31 /* |
16 creg currrent virtual register | 32 creg currrent virtual register |
17 dreg spare virtual register | 33 dreg spare virtual register |
18 | 34 |
21 | 37 |
22 regs[] virtual register usage | 38 regs[] virtual register usage |
23 regv[] value in virtual register flag | 39 regv[] value in virtual register flag |
24 | 40 |
25 reg_name[rname[creg]] | 41 reg_name[rname[creg]] |
42 | |
43 freg current floating point register | |
44 fregv calue in floating point register | |
26 */ | 45 */ |
27 | 46 |
28 void remove0(int *parent,int e) ; | 47 void remove0(int *parent,int e) ; |
29 void remove0_all(int *parent,int e) ; | 48 void remove0_all(int *parent,int e) ; |
30 int is_same_type(int e1,int e2); | 49 int is_same_type(int e1,int e2); |
31 void jump(int e1, int env); | 50 void jump(int e1, int env); |
32 void machinop(int e1); | 51 void machinop(int e1); |
33 void sassign(int e1); | 52 void sassign(int e1); |
34 void assign(int e1); | 53 void assign(int e1); |
35 void assop(int e1); | 54 void assop(int e1); |
55 void g_expr0(int e1); | |
36 | 56 |
37 int | 57 int |
38 get_register(void) | 58 get_register(void) |
39 { /* 使われていないレジスタを調べる */ | 59 { /* 使われていないレジスタを調べる */ |
40 int i; | 60 int i; |
117 arg_register(NMTBL *fnptr) | 137 arg_register(NMTBL *fnptr) |
118 { | 138 { |
119 arg_register0(fnptr->dsp); | 139 arg_register0(fnptr->dsp); |
120 } | 140 } |
121 | 141 |
122 | |
123 int creg_regvar = -1; | |
124 static int creg_regvar_back; | |
125 static int creg_back; | |
126 | |
127 void | |
128 creg_destroy() { | |
129 creg_back = creg; creg_regvar_back = creg_regvar; | |
130 if (creg_regvar>=0) | |
131 creg = creg_regvar; | |
132 creg_regvar=-1; | |
133 } | |
134 | |
135 void | |
136 creg_un_destroy() { | |
137 creg = creg_back; creg_regvar = creg_regvar_back; | |
138 } | |
139 | |
140 void | 142 void |
141 register_usage(char *s) | 143 register_usage(char *s) |
142 { | 144 { |
143 int i; | 145 int i; |
144 if (chk) return; | 146 if (chk) return; |
217 free_register(xreg); | 219 free_register(xreg); |
218 } | 220 } |
219 } | 221 } |
220 | 222 |
221 void | 223 void |
222 gexpr(int e1) | 224 gexpr(int e1,int use0) |
223 { | 225 { |
224 if (chk) return; | 226 if (chk) return; |
225 gexpr_init(); | 227 gexpr_init(); |
228 use = use0; | |
226 #if 0 | 229 #if 0 |
227 if(lineno==2862) { | 230 if(lineno==2862) { |
228 g_expr(e1); /*break here*/ | 231 g_expr0(e1); /*break here*/ |
229 return; | 232 return; |
230 } | 233 } |
231 #endif | 234 #endif |
232 g_expr(e1); | 235 g_expr0(e1); |
233 } | 236 } |
234 | 237 |
235 int | 238 int |
236 csvalue() | 239 csvalue() |
237 { | 240 { |
238 return rname[creg]; /* for switch value */ | 241 return rname[creg]; /* for switch value */ |
239 } | 242 } |
240 | 243 |
241 void | 244 void |
245 g_expr_u(int e1) | |
246 { | |
247 int suse = use; use=0; | |
248 g_expr0(e1); | |
249 use=suse; | |
250 } | |
251 | |
252 void | |
242 g_expr(int e1) | 253 g_expr(int e1) |
243 { | 254 { |
244 int e2,e3/*,e4*/; | 255 int suse = use; use=1; |
256 g_expr0(e1); | |
257 use=suse; | |
258 } | |
259 | |
260 void | |
261 g_expr0(int e1) | |
262 { | |
263 int e2,e3; | |
245 NMTBL *n; | 264 NMTBL *n; |
246 | 265 |
247 e2 = cadr(e1); | 266 e2 = cadr(e1); |
248 switch (car(e1)){ | 267 switch (car(e1)){ |
249 case GVAR: | 268 case GVAR: |
277 code_crlvar(lvar(e2)); | 296 code_crlvar(lvar(e2)); |
278 regv[creg]=1; | 297 regv[creg]=1; |
279 return; | 298 return; |
280 case FRLVAR: | 299 case FRLVAR: |
281 code_drlvar(lvar(e2),0); | 300 code_drlvar(lvar(e2),0); |
282 regv[freg]=1; | 301 fregv[freg]=1; |
283 return; | 302 return; |
284 case FRGVAR: | 303 case FRGVAR: |
285 code_drgvar(e1,0); | 304 code_drgvar(e1,0); |
286 regv[freg]=1; | 305 fregv[freg]=1; |
287 return; | 306 return; |
288 case DRLVAR: | 307 case DRLVAR: |
289 code_drlvar(lvar(e2),1); | 308 code_drlvar(lvar(e2),1); |
290 regv[freg]=1; | 309 fregv[freg]=1; |
291 return; | 310 return; |
292 case DRGVAR: | 311 case DRGVAR: |
293 code_drgvar(e1,1); | 312 code_drgvar(e1,1); |
294 regv[freg]=1; | 313 fregv[freg]=1; |
295 return; | 314 return; |
296 case FNAME: | 315 case FNAME: |
297 code_fname(((NMTBL *)(e2))->nm); | 316 code_fname(((NMTBL *)(e2))->nm); |
298 regv[creg]=1; | 317 regv[creg]=1; |
299 return; | 318 return; |
301 code_const(e2); | 320 code_const(e2); |
302 regv[creg]=1; | 321 regv[creg]=1; |
303 return; | 322 return; |
304 case DCONST: | 323 case DCONST: |
305 code_dconst(e1); | 324 code_dconst(e1); |
306 regv[freg]=1; | 325 fregv[freg]=1; |
307 return; | 326 return; |
308 case STRING: | 327 case STRING: |
309 string(e1); | 328 string(e1); |
310 regv[creg]=1; | 329 regv[creg]=1; |
311 return; | 330 return; |
315 return; | 334 return; |
316 case CODE: | 335 case CODE: |
317 jump(e2,caddr(e1)); | 336 jump(e2,caddr(e1)); |
318 return; | 337 return; |
319 case INDIRECT: | 338 case INDIRECT: |
320 g_expr(e2); | 339 g_expr0(e2); |
321 return; | 340 return; |
322 case RINDIRECT: case CRINDIRECT: | 341 case RINDIRECT: case CRINDIRECT: |
323 case DRINDIRECT: case FRINDIRECT: | 342 case DRINDIRECT: case FRINDIRECT: |
324 rindirect(e1); | 343 rindirect(e1); |
325 return; | 344 return; |
326 case ADDRESS: | 345 case ADDRESS: |
327 g_expr(e2); | 346 g_expr0(e2); |
328 return; | 347 return; |
329 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */ | 348 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */ |
330 g_expr(e2); | 349 g_expr0(e2); |
331 code_neg(); | 350 code_neg(); |
332 return; | 351 return; |
333 case DMINUS: | 352 case DMINUS: |
334 g_expr(e2); | 353 g_expr0(e2); |
335 code_dneg(); | 354 code_dneg(); |
336 return; | 355 return; |
337 case I2D: | 356 case I2D: |
338 g_expr(e2); | 357 g_expr0(e2); |
339 code_i2d(); | 358 code_i2d(); |
340 return; | 359 return; |
341 case D2I: | 360 case D2I: |
342 g_expr(e2); | 361 g_expr0(e2); |
343 code_d2i(); | 362 code_d2i(); |
344 return; | 363 return; |
345 case BNOT: /* ~ */ | 364 case BNOT: /* ~ */ |
346 g_expr(e2); | 365 g_expr0(e2); |
347 code_not(); | 366 code_not(); |
348 return; | 367 return; |
349 case LNOT: /* ! */ | 368 case LNOT: /* ! */ |
350 g_expr(e2); | 369 g_expr0(e2); |
351 code_lnot(); | 370 code_lnot(); |
352 return; | 371 return; |
353 case PREINC: | 372 case PREINC: |
354 code_preinc(e1,e2); | 373 code_preinc(e1,e2); |
355 return; | 374 return; |
356 case POSTINC: | 375 case POSTINC: |
357 code_postinc(e1,e2); | 376 code_postinc(e1,e2); |
377 return; | |
378 case DPREINC: | |
379 code_dpreinc(e1,e2,1); | |
380 return; | |
381 case DPOSTINC: | |
382 code_dpostinc(e1,e2,1); | |
383 return; | |
384 case FPREINC: | |
385 code_dpreinc(e1,e2,0); | |
386 return; | |
387 case FPOSTINC: | |
388 code_dpostinc(e1,e2,0); | |
358 return; | 389 return; |
359 case CPOSTINC: | 390 case CPOSTINC: |
360 /* char *p; *p++ */ | 391 /* char *p; *p++ */ |
361 code_cpostinc(e1,e2); | 392 code_cpostinc(e1,e2); |
362 return; | 393 return; |
383 return; | 414 return; |
384 case COND: | 415 case COND: |
385 e2=fwdlabel(); | 416 e2=fwdlabel(); |
386 b_expr(cadr(e1),0,e2,0); | 417 b_expr(cadr(e1),0,e2,0); |
387 code_set_fixed_creg(0); | 418 code_set_fixed_creg(0); |
388 g_expr(caddr(e1)); | 419 g_expr0(caddr(e1)); |
389 /* e4 = rname[creg]; this is a bad idea */ | 420 /* e4 = rname[creg]; this is a bad idea */ |
390 code_set_fixed_creg(1); | 421 code_set_fixed_creg(1); |
391 jmp(e3=fwdlabel()); | 422 jmp(e3=fwdlabel()); |
392 fwddef(e2); | 423 fwddef(e2); |
393 code_set_fixed_creg(0); | 424 code_set_fixed_creg(0); |
394 g_expr(cadddr(e1)); | 425 g_expr0(cadddr(e1)); |
395 code_set_fixed_creg(1); | 426 code_set_fixed_creg(1); |
396 fwddef(e3); | 427 fwddef(e3); |
397 return; | 428 return; |
398 case SASS: | 429 case SASS: |
399 sassign(e1); | 430 sassign(e1); |
409 return; | 440 return; |
410 case DASSOP: case FASSOP: | 441 case DASSOP: case FASSOP: |
411 dassop(e1); | 442 dassop(e1); |
412 return; | 443 return; |
413 case RSTRUCT: | 444 case RSTRUCT: |
414 g_expr(e2); | 445 g_expr0(e2); |
415 return; | 446 return; |
416 case COMMA: | 447 case COMMA: |
417 g_expr(e2); | 448 g_expr_u(e2); |
418 g_expr(caddr(e1)); | 449 g_expr0(caddr(e1)); |
419 return; | 450 return; |
420 case RETURN: | 451 case RETURN: |
421 n = (NMTBL *)e2; | 452 n = (NMTBL *)e2; |
422 if (retcont==0) | 453 if (retcont==0) |
423 retcont=fwdlabel(); | 454 retcont=fwdlabel(); |
544 return; | 575 return; |
545 case DCONST: | 576 case DCONST: |
546 if((dcadr(e2)!=0.0)^cond) jmp(l1); | 577 if((dcadr(e2)!=0.0)^cond) jmp(l1); |
547 return; | 578 return; |
548 default: | 579 default: |
549 /* type ? */ | |
550 if(err) { | 580 if(err) { |
551 error(-1); return; /* recursive g_expr/b_expr */ | 581 error(-1); return; /* recursive g_expr/b_expr */ |
552 } | 582 } /* type ? */ |
553 g_expr(e1); | 583 g_expr(e1); |
554 code_cmp_register(creg); | 584 code_cmp_register(creg); |
555 jcond(l1,cond); | 585 jcond(l1,cond); |
556 return; | 586 return; |
557 } | 587 } |
609 int e1; | 639 int e1; |
610 /*新しいレジスタ(or スタック)を取得する*/ | 640 /*新しいレジスタ(or スタック)を取得する*/ |
611 if (sz==size_of_int && (e1=get_register())!=-1) { | 641 if (sz==size_of_int && (e1=get_register())!=-1) { |
612 *use=list3(t,*use,e1); | 642 *use=list3(t,*use,e1); |
613 e1=list2(REGISTER,e1); | 643 e1=list2(REGISTER,e1); |
614 g_expr(assign_expr0(e1,s,ty,ty)); | 644 g_expr_u(assign_expr0(e1,s,ty,ty)); |
615 *target = append4(*target,t,ty,e1); | 645 *target = append4(*target,t,ty,e1); |
616 } else { | 646 } else { |
617 disp-=sz; | 647 disp-=sz; |
618 g_expr(assign_expr0((e1=list2(LVAR,disp)),s,ty,ty)); | 648 g_expr_u(assign_expr0((e1=list2(LVAR,disp)),s,ty,ty)); |
619 *target = append4(*target,t,ty,e1); | 649 *target = append4(*target,t,ty,e1); |
620 } | 650 } |
621 } | 651 } |
622 | 652 |
623 int | 653 int |
665 } else if (!(s1=overrap(t,sz,*source))) { | 695 } else if (!(s1=overrap(t,sz,*source))) { |
666 /* 重なってないので安心して書き込める */ | 696 /* 重なってないので安心して書き込める */ |
667 #if DEBUG_PARALLEL_ASSIGN | 697 #if DEBUG_PARALLEL_ASSIGN |
668 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); | 698 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); |
669 #endif | 699 #endif |
670 g_expr(assign_expr0(t,s,ty,ty)); | 700 g_expr_u(assign_expr0(t,s,ty,ty)); |
671 remove_target(target,t,use); remove0(source,s); | 701 remove_target(target,t,use); remove0(source,s); |
672 } else { | 702 } else { |
673 if(circular_dependency(t,s1,target,source)) { | 703 if(circular_dependency(t,s1,target,source)) { |
674 #if DEBUG_PARALLEL_ASSIGN | 704 #if DEBUG_PARALLEL_ASSIGN |
675 printf("# saving %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); | 705 printf("# saving %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); |
789 cadr(t0)=-arg_size; | 819 cadr(t0)=-arg_size; |
790 arg_size-=sz; | 820 arg_size-=sz; |
791 } | 821 } |
792 if (!is_simple(car(s0))) { | 822 if (!is_simple(car(s0))) { |
793 disp-=sz; | 823 disp-=sz; |
794 g_expr(assign_expr0((e4=list2(LVAR,disp)),s0,ty,ty)); | 824 g_expr_u(assign_expr0((e4=list2(LVAR,disp)),s0,ty,ty)); |
795 cadddr(e2)=e4; | 825 cadddr(e2)=e4; |
796 s0=e4; | 826 s0=e4; |
797 } else if (is_same_type(t0,s0)) { | 827 } else if (is_same_type(t0,s0)) { |
798 if(cadr(t0)==cadr(s0)) { | 828 if(cadr(t0)==cadr(s0)) { |
799 #if DEBUG_PARALLEL_ASSIGN | 829 #if DEBUG_PARALLEL_ASSIGN |
882 g_expr(e3); | 912 g_expr(e3); |
883 emit_dpush(); | 913 emit_dpush(); |
884 g_expr(e2); | 914 g_expr(e2); |
885 dtosop(car(e1),(e2=dpop_register())); | 915 dtosop(car(e1),(e2=dpop_register())); |
886 emit_dpop_free(e2); | 916 emit_dpop_free(e2); |
887 regv[freg]=1; | 917 fregv[freg]=1; |
888 return; | 918 return; |
889 } | 919 } |
890 | 920 |
891 | 921 |
892 void | 922 void |
1012 } | 1042 } |
1013 | 1043 |
1014 void | 1044 void |
1015 dassop(int e1) | 1045 dassop(int e1) |
1016 { | 1046 { |
1017 } | 1047 int e2,e3,op,d; |
1018 | 1048 |
1019 void | 1049 /* e2 op= e3 */ |
1020 fassop(int e1) | 1050 d = (car(e1) == DASSOP); |
1021 { | 1051 e2 = cadr(e1); |
1052 if (car(e2)==INDIRECT) e2=cadr(e2); | |
1053 e3 = caddr(e1); | |
1054 op = cadddr(e1); | |
1055 | |
1056 g_expr(e3); | |
1057 emit_dpush(); | |
1058 g_expr(e2); | |
1059 code_dassop(op,d); | |
1060 regv[creg]=1; | |
1061 return; | |
1022 } | 1062 } |
1023 | 1063 |
1024 int | 1064 int |
1025 fwdlabel(void) | 1065 fwdlabel(void) |
1026 { | 1066 { |