comparison mc-codegen.c @ 681:e16b34f2b386 decl-data-tree

DECL_DATA has parse tree now.
author kono
date Mon, 01 Oct 2007 01:01:47 +0900
parents 5e71527f9fd6
children 639db8597a58
comparison
equal deleted inserted replaced
680:f536897fa3cb 681:e16b34f2b386
58 static void jump(int e1, int env); 58 static void jump(int e1, int env);
59 static void machinop(int e1); 59 static void machinop(int e1);
60 static int register_to_lvar(int e); 60 static int register_to_lvar(int e);
61 static void remove0(int *parent,int e) ; 61 static void remove0(int *parent,int e) ;
62 static void sassign(int e1); 62 static void sassign(int e1);
63 static int gen_decl_data(int e);
63 64
64 #if FLOAT_CODE 65 #if FLOAT_CODE
65 66
66 /* floating point */ 67 /* floating point */
67 68
290 jump(e2,caddr(e1)); 291 jump(e2,caddr(e1));
291 return VOID; 292 return VOID;
292 case ARRAY: 293 case ARRAY:
293 if (chk==2) { 294 if (chk==2) {
294 g_expr0(e2); 295 g_expr0(e2);
295 t = g_expr0(caddr(e1)); 296 g_expr0(caddr(e1));
296 code_gexpr(e1); 297 code_gexpr(e1);
297 return t; 298 return t;
298 } 299 }
299 e1=binop(ADD,e2,caddr(e1),cadddr(e1),caddddr(e1)); 300 e1=binop(ADD,e2,caddr(e1),cadddr(e1),caddddr(e1));
300 e1 = indop(e1); t = type; 301 e1 = indop(e1); t = type;
573 return VOID; 574 return VOID;
574 case ENVIRONMENT: 575 case ENVIRONMENT:
575 code_environment(USE_CREG); 576 code_environment(USE_CREG);
576 return ADDRESS; 577 return ADDRESS;
577 case LCALL: 578 case LCALL:
578 // we cannot trust register variable either...
579 code_save_stacks(); 579 code_save_stacks();
580 gen_jmp(e2); 580 gen_jmp(e2);
581 fwddef(caddr(e1)); 581 fwddef(caddr(e1));
582 return VOID; 582 return VOID;
583 #if BIT_FIELD_CODE 583 #if BIT_FIELD_CODE
599 case ASM: 599 case ASM:
600 gen_asm(car(e2),cadr(e2),caddr(e2),cadddr(e2),caddr(e1)); 600 gen_asm(car(e2),cadr(e2),caddr(e2),cadddr(e2),caddr(e1));
601 /* asm in (str) out (str) opt(str) expr */ 601 /* asm in (str) out (str) opt(str) expr */
602 return VOID; 602 return VOID;
603 #endif 603 #endif
604 case CAST:
605 type = cadddr(e1);
606 e2 = correct_type(e2,caddr(e1));
607 continue;
608 case DECL_DATA:
609 e1 = gen_decl_data(e1);
610 return e1;
604 case ST_DECL: st_decl(e1); break; 611 case ST_DECL: st_decl(e1); break;
605 case ST_IF: st_if(e1); break; 612 case ST_IF: st_if(e1); break;
606 case ST_DO: st_do(e1); break; 613 case ST_DO: st_do(e1); break;
607 case ST_WHILE: st_while(e1); break; 614 case ST_WHILE: st_while(e1); break;
608 case ST_FOR: st_for(e1); break; 615 case ST_FOR: st_for(e1); break;
2821 2828
2822 extern int 2829 extern int
2823 contains_p(int e,int (*p)(int)) 2830 contains_p(int e,int (*p)(int))
2824 { 2831 {
2825 while(e) { 2832 while(e) {
2826 if (!car(e)) return 0; 2833 int e1 = car(e);
2827 if (p(car(e))) return 1; 2834 if (!e1) return 0;
2828 if (LIST_ARGS(car(e))){ 2835 if (p(e1)) return 1;
2836 if (LIST_ARGS(e1)){
2829 /* list arguments */ 2837 /* list arguments */
2830 return contains_in_list_p(caddr(e),p); 2838 return contains_in_list_p(caddr(e),p);
2831 } else if (UNARY_ARGS(car(e))) { 2839 } else if (UNARY_ARGS(e1)) {
2832 /* unary operators */ 2840 /* unary operators */
2833 e = cadr(e); 2841 e = cadr(e);
2834 continue; 2842 continue;
2835 } else if (BINARY_ARGS(car(e))) { 2843 } else if (BINARY_ARGS(e1)) {
2836 /* biary operators */ 2844 /* biary operators */
2837 if (contains_p(cadr(e),p)) return 1; 2845 if (contains_p(cadr(e),p)) return 1;
2838 e = caddr(e); 2846 e = caddr(e);
2839 continue; 2847 continue;
2840 } else if (TERNARY_ARGS(car(e))) { 2848 } else if (TERNARY_ARGS(e1)) {
2841 /* tarary operators */ 2849 /* tarary operators */
2842 if (contains_p(cadr(e), p)) return 1; 2850 if (contains_p(cadr(e), p)) return 1;
2843 if (contains_p(caddr(e),p)) return 1; 2851 if (contains_p(caddr(e),p)) return 1;
2844 e = cadddr(e); 2852 e = cadddr(e);
2845 continue; 2853 continue;
2846 } else if (NULLARY_ARGS(car(e))) { 2854 } else if (NULLARY_ARGS(e1)) {
2847 /* nullary operators */ 2855 /* nullary operators */
2848 return 0; 2856 return 0;
2849 } else if (IS_STATEMENT(car(e))) { 2857 } else if (IS_STATEMENT(e1)) {
2850 return 1; // may contain anything 2858 return 1; // may contain anything
2851 } else { 2859 } else {
2852 // if (lsrc)fprintf(stderr,"Unknown Tree ID %d\n",car(e)); 2860 // if (lsrc)fprintf(stderr,"Unknown Tree ID %d\n",car(e));
2853 error(-1); 2861 error(-1);
2854 return 0; 2862 return 0;
2871 2879
2872 extern int 2880 extern int
2873 contains_p1(int arg,int e,int (*p)(int,int)) 2881 contains_p1(int arg,int e,int (*p)(int,int))
2874 { 2882 {
2875 while(e) { 2883 while(e) {
2876 if (!car(e)) return arg; 2884 int e1 = car(e);
2877 if (LIST_ARGS(car(e))){ 2885 if (!e1) return arg;
2886 if (LIST_ARGS(e1)){
2878 /* list arguments */ 2887 /* list arguments */
2879 return contains_in_list_p1(arg,caddr(e),p); 2888 return contains_in_list_p1(arg,caddr(e),p);
2880 } else if (UNARY_ARGS(car(e))) { 2889 } else if (UNARY_ARGS(e1)) {
2881 /* unary operators */ 2890 /* unary operators */
2882 e = cadr(e); 2891 e = cadr(e);
2883 continue; 2892 continue;
2884 } else if (BINARY_ARGS(car(e))) { 2893 } else if (BINARY_ARGS(e1)) {
2885 /* biary operators */ 2894 /* biary operators */
2886 arg=contains_p1(arg,cadr(e),p); 2895 arg=contains_p1(arg,cadr(e),p);
2887 e = caddr(e); 2896 e = caddr(e);
2888 continue; 2897 continue;
2889 } else if (TERNARY_ARGS(car(e))) { 2898 } else if (TERNARY_ARGS(e1)) {
2890 /* tarary operators */ 2899 /* tarary operators */
2891 arg=contains_p1(arg,cadr(e), p); 2900 arg=contains_p1(arg,cadr(e), p);
2892 arg=contains_p1(arg,caddr(e),p); 2901 arg=contains_p1(arg,caddr(e),p);
2893 e = cadddr(e); 2902 e = cadddr(e);
2894 continue; 2903 continue;
2895 } else if (NULLARY_ARGS(car(e))) { 2904 } else if (NULLARY_ARGS(e1)) {
2896 /* nullary operators */ 2905 /* nullary operators */
2897 arg=p(arg,e); 2906 arg=p(arg,e);
2898 return arg; 2907 return arg;
2899 } else if (IS_STATEMENT(car(e))) { 2908 } else if (IS_STATEMENT(e1)) {
2900 return arg; 2909 return arg;
2901 } else { 2910 } else {
2902 // if (lsrc)fprintf(stderr,"Unknown Tree ID %d\n",car(e)); 2911 // if (lsrc)fprintf(stderr,"Unknown Tree ID %d\n",car(e));
2903 // error(-1); 2912 // error(-1);
2904 return arg; 2913 return arg;
3431 e = cadr(e); 3440 e = cadr(e);
3432 } 3441 }
3433 return; 3442 return;
3434 } 3443 }
3435 while(e) { 3444 while(e) {
3436 gexpr(car(e),0); 3445 g_expr_u(car(e));
3437 e = cadr(e); 3446 e = cadr(e);
3438 } 3447 }
3439 } 3448 }
3440 3449
3441 static int 3450 static int
3521 // if (lsrc)fprintf(stderr,"## type= %d\n",t); 3530 // if (lsrc)fprintf(stderr,"## type= %d\n",t);
3522 } 3531 }
3523 error(INERR); 3532 error(INERR);
3524 } 3533 }
3525 3534
3535 static int gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset);
3536
3537 extern int
3538 gen_delayed_decl_data(NMTBL *n,int offset)
3539 {
3540 int offset0=0;
3541 int e;
3542 int t,sz,offset1=0;
3543 int init = decl_str_init;
3544
3545 decl_str_init = 0;
3546 sz = size(n->ty);
3547 /*
3548 decl_str_init
3549 output delayed decl data
3550 list4(offset,next,expression,list2(type0,type1));
3551 */
3552 while (init) {
3553 offset= car(init);
3554 e=caddr(init);
3555 t=car(cadddr(init));
3556 if (offset!=offset0) {
3557 // make space
3558 assign_data(list2(CONST,offset-offset0),EMPTY,n,offset0);
3559 }
3560 type=cadr(cadddr(init));
3561 offset0 = gen_decl_data0(n,t,e,offset);
3562 init = cadr(init);
3563 }
3564 offset = offset0;
3565 if ((sz=(offset1+sz-offset))>0)
3566 assign_data(list2(CONST,sz),EMPTY,n,offset0);
3567 decl_str_init = 0;
3568 local_nptr = 0;
3569 return offset;
3570 }
3571
3572 static int
3573 gen_decl_data_array(NMTBL *nptr0,int init,int target_type,int offset)
3574 {
3575 int type0 = cadr(target_type); /* array item type */
3576 int e;
3577
3578 for(; init; init = cadr(init)) {
3579 // unordered data with tag or array offset
3580 if (car(init)!=DECL_DATA_ARRAY) {
3581 error(-1);
3582 }
3583 e = pexpr(caddr(init));
3584 offset = gen_decl_data0(nptr0,type0,e,offset);
3585 }
3586 return offset;
3587 }
3588
3589 static int
3590 gen_decl_data_field(NMTBL *nptr0,int init,int target_type,int offset)
3591 {
3592 int type0 = target_type; /* list of fields */
3593 int e,t,type1,foffset;
3594 NMTBL *n;
3595
3596 for(; init; init = cadr(init)) {
3597 // unordered data with tag or array offset
3598 if (car(init)!=DECL_DATA_FIELD) {
3599 error(-1);
3600 }
3601 n = (NMTBL*)cadddr(init);
3602 type1 = search_struct_type(type0,n->nm,&foffset);
3603 e = caddr(init);
3604 if (car(e)!=DECL_DATA) error(-1);
3605 t = caddr(e);
3606 decl_str_init=insert_ascend(decl_str_init,
3607 glist4(offset+foffset,0,e,glist2(type1,t)),str_init_eq);
3608 }
3609 return offset;
3610 }
3611
3612 static int
3613 gen_decl_data_list(NMTBL *nptr0,int init,int target_type,int offset)
3614 {
3615 int type0 = caddr(target_type); /* list of fields */
3616 int e;
3617
3618 for(; init; init = cadr(init)) {
3619 // ordered data
3620 if (car(init)!=DECL_DATA_LIST) {
3621 error(-1);
3622 }
3623 e = caddr(init);
3624 offset = gen_decl_data0(nptr0,car(type0),e,offset);
3625 type0 = cadr(type0);
3626 }
3627 return offset;
3628 }
3629
3630
3631 static int
3632 gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset)
3633 {
3634 int e,t;
3635 if (car(init)==DECL_DATA) {
3636 switch( car(e=cadr(init))) {
3637 case DECL_DATA_LIST:
3638 offset = gen_decl_data_list(nptr0,e,target_type,offset);
3639 break;
3640 case DECL_DATA_FIELD:
3641 offset = gen_decl_data_field(nptr0,e,target_type,offset);
3642 break;
3643 case DECL_DATA_ARRAY:
3644 offset = gen_decl_data_array(nptr0,e,target_type,offset);
3645 break;
3646 default:
3647 t = caddr(init); // type of source
3648 e = rvalue_t(e,t);
3649 offset=assign_data(e,t,nptr0,offset);
3650 }
3651 } else {
3652 error(-1);
3653 }
3654 if (decl_str_init) {
3655 offset = gen_delayed_decl_data(nptr0,offset);
3656 }
3657 return offset;
3658 }
3659
3660 static int
3661 gen_decl_data(int e)
3662 {
3663 NMTBL *nptr0;
3664 int t = caddr(e);
3665 int e1,sz;
3666 int offset = 0;
3667 int sinit_vars = init_vars;
3668 init_vars = 0;
3669
3670 nptr0 = get_nptr();
3671 nptr0->nm = "";
3672 nptr0->sc = LVAR;
3673 nptr0->attr = 0;
3674 type = nptr0->ty = t;
3675 sz = size(type);
3676 nptr0->dsp = new_lvar_align(sz,16);
3677
3678 e1 = list3(RSTRUCT,list3(
3679 nptr0->sc,nptr0->dsp,(int)nptr0),sz);
3680
3681 gen_decl_data0(nptr0,t,e,offset);
3682
3683 if (init_vars) emit_init_vars();
3684 g_expr0(e1);
3685 init_vars = sinit_vars;
3686 return type;
3687 }
3688
3526 // 3689 //
3527 // local variable initialization 3690 // local variable initialization
3528 // 3691 //
3529 3692
3530 extern int 3693 extern int
3531 assign_data(int e, int t, NMTBL *n,int offset) 3694 assign_data(int e, int t, NMTBL *n,int offset)
3532 { 3695 {
3533 int ass,sz,bfd; 3696 int ass,sz,bfd;
3534 3697
3698 if (inmode) error(-1);
3535 #if STRUCT_ALIGN 3699 #if STRUCT_ALIGN
3536 if (t!=-99) { 3700 if (t!=-99) {
3537 int strtype=0; 3701 int strtype=0;
3538 if (t>0 && (car(t)==STRUCT||car(t)==UNION)) 3702 if (t>0 && (car(t)==STRUCT||car(t)==UNION))
3539 strtype=1; 3703 strtype=1;
3548 t = list2(POINTER,VOID); // fake 3712 t = list2(POINTER,VOID); // fake
3549 } else { 3713 } else {
3550 error(TYERR); 3714 error(TYERR);
3551 } 3715 }
3552 } 3716 }
3553 if(mode==GDECL) { 3717 switch (mode) {
3718 case GDECL:
3554 if (!is_const(e)) error(INERR); 3719 if (!is_const(e)) error(INERR);
3555 emit_data(e,t,n); 3720 emit_data(e,t,n);
3556 } else if(mode==LDECL || (mode==STADECL&&local_nptr&&(n=local_nptr))) { 3721 break;
3722 case STADECL:
3723 if (!local_nptr) {
3724 if (!is_const(e)) error(INERR);
3725 else emit_data(e,t,n);
3726 break;
3727 }
3728 n = local_nptr;
3729 case LDECL:
3730 case STAT: // inline case
3557 if (t==EMPTY) { 3731 if (t==EMPTY) {
3558 /* empty space in partial initialization */ 3732 /* empty space in partial initialization */
3559 return offset+cadr(e); 3733 return offset+cadr(e);
3560 } 3734 }
3561 /* If this is a local declared constant, we don't have to assign. 3735 /* If this is a local declared constant, we don't have to assign.
3567 list3(ADD,list3(n->sc,n->dsp,(int)n),list2(CONST,offset)): 3741 list3(ADD,list3(n->sc,n->dsp,(int)n),list2(CONST,offset)):
3568 list3(n->sc,n->dsp,(int)n)): 3742 list3(n->sc,n->dsp,(int)n)):
3569 (n->sc==REGISTER||n->sc==DREGISTER||n->sc==FREGISTER||n->sc==LREGISTER)? 3743 (n->sc==REGISTER||n->sc==DREGISTER||n->sc==FREGISTER||n->sc==LREGISTER)?
3570 list3(n->sc,n->dsp,(int)n): 3744 list3(n->sc,n->dsp,(int)n):
3571 list3(n->sc,n->dsp+offset,(int)n), 3745 list3(n->sc,n->dsp+offset,(int)n),
3572 e,t,type); 3746 e,t,type);
3573 init_vars = list2(ass,init_vars); 3747 init_vars = list2(ass,init_vars);
3574 } else if(mode==STADECL) { 3748 break;
3575 if (!is_const(e)) error(INERR); 3749 case SFDINIT:
3576 else emit_data(e,t,n);
3577 } else if(mode==SFDINIT) {
3578 // if (lsrc)printf("## %d sfdinit c0(e)=%d type=%d t=%d offset=%d\n",lineno,car(e),type,t,offset); 3750 // if (lsrc)printf("## %d sfdinit c0(e)=%d type=%d t=%d offset=%d\n",lineno,car(e),type,t,offset);
3579 decl_str_init=insert_ascend(decl_str_init, 3751 decl_str_init=insert_ascend(decl_str_init,
3580 glist4(offset,0,e,glist2(t,type)),str_init_eq); 3752 glist4(offset,0,e,glist2(t,type)),str_init_eq);
3581 } else { 3753 break;
3754 default:
3582 error(DCERR); 3755 error(DCERR);
3583 return offset; 3756 return offset;
3584 } 3757 }
3585 if (t>0&&car(t)==BIT_FIELD) { 3758 if (t>0&&car(t)==BIT_FIELD) {
3586 sz = 0; 3759 sz = 0;
3603 { 3776 {
3604 int offset; 3777 int offset;
3605 int offset0=0; 3778 int offset0=0;
3606 int e; 3779 int e;
3607 int t,sz,offset1=0; 3780 int t,sz,offset1=0;
3608 int smode=mode;
3609 mode = STADECL;
3610 sz = size(n->ty); 3781 sz = size(n->ty);
3611 /* 3782 /*
3612 decl_str_init 3783 decl_str_init
3613 output delayed decl data 3784 output delayed decl data
3614 list4(offset,next,expression,list2(type0,type1)); 3785 list4(offset,next,expression,list2(type0,type1));
3629 offset = offset0; 3800 offset = offset0;
3630 if ((sz=(offset1+sz-offset))>0) 3801 if ((sz=(offset1+sz-offset))>0)
3631 assign_data(list2(CONST,sz),EMPTY,n,offset0); 3802 assign_data(list2(CONST,sz),EMPTY,n,offset0);
3632 decl_str_init = 0; 3803 decl_str_init = 0;
3633 local_nptr = 0; 3804 local_nptr = 0;
3634 mode=smode;
3635 } 3805 }
3636 3806
3637 extern void 3807 extern void
3638 data_closing(NMTBL *n) 3808 data_closing(NMTBL *n)
3639 { 3809 {
3640 if (!chk) { 3810 if (!chk) {
3811 int smode = mode; mode = STADECL;
3641 if (decl_str_init) flush_delayed_decl_data(n); 3812 if (decl_str_init) flush_delayed_decl_data(n);
3813 mode = smode;
3642 emit_data_closing(n); 3814 emit_data_closing(n);
3643 } 3815 }
3644 } 3816 }
3645 3817
3646 #define ARG_REORDER_DEBUG 0 3818 #define ARG_REORDER_DEBUG 0
3777 } else { 3949 } else {
3778 error(DCERR); 3950 error(DCERR);
3779 } 3951 }
3780 } 3952 }
3781 3953
3782 static void 3954 extern int
3783 compatible(int t1, int t2) 3955 type_compatible(int t1, int t2)
3784 { 3956 {
3785 t1 = type_value(t1); 3957 t1 = type_value(t1);
3786 t2 = type_value(t2); 3958 t2 = type_value(t2);
3787 if(integral(t1)) { 3959 if(integral(t1)) {
3788 if(t1!=t2) error(TYERR); 3960 if(t1!=t2) return 0;
3789 } 3961 }
3790 else if(t1<0 || t2<0) { 3962 else if(t1<0 || t2<0) {
3791 if(t1!=t2) error(TYERR); 3963 if(t1!=t2) return 0;
3792 } else if(car(t1)!=car(t2)) 3964 } else if(car(t1)!=car(t2))
3793 error(TYERR); 3965 return 0;
3794 else if((car(t1)==STRUCT || car(t1)==UNION) && cadr(t1)!=cadr(t2)) 3966 else if((car(t1)==STRUCT || car(t1)==UNION) && cadr(t1)!=cadr(t2))
3795 error(TYERR); 3967 return 0;
3796 else if(car(t1)==POINTER || car(t1)==ARRAY ||car(t1)==FUNCTION) 3968 else if(car(t1)==POINTER || car(t1)==ARRAY ||car(t1)==FUNCTION)
3797 compatible(cadr(t1),cadr(t2)); 3969 return type_compatible(cadr(t1),cadr(t2));
3970 return 1;
3971 }
3972
3973 static void
3974 compatible(int t1, int t2)
3975 {
3976 if (!type_compatible(t1,t2)) error(TYERR);
3798 } 3977 }
3799 3978
3800 extern int 3979 extern int
3801 scalar(int t) 3980 scalar(int t)
3802 { 3981 {
4068 extern int 4247 extern int
4069 strop(int e,int ind) 4248 strop(int e,int ind)
4070 { 4249 {
4071 int dsp = 0; 4250 int dsp = 0;
4072 int type0; 4251 int type0;
4073 int e1 = 0; 4252 int e1;
4074 4253
4075 if (inmode || chk) { 4254 if (inmode || chk) {
4076 e1 = list4(ind?ARROW:PERIOD,e,(int)nptr,type); 4255 e1 = list4(ind?ARROW:PERIOD,e,(int)nptr,type);
4077 } 4256 }
4078 if (ind) e = indop(rvalue(e)); 4257 if (ind) e = indop(rvalue(e));
4084 error(TYERR); type=INT; return e; 4263 error(TYERR); type=INT; return e;
4085 } 4264 }
4086 /* type = list4(s,disp,fields,tag_nptr); */ 4265 /* type = list4(s,disp,fields,tag_nptr); */
4087 /* print_fields(caddr(type),"strop"); */ 4266 /* print_fields(caddr(type),"strop"); */
4088 type = search_struct_type(type,nptr->nm,&dsp); 4267 type = search_struct_type(type,nptr->nm,&dsp);
4089 if (!type) { error(TYERR); type=INT; return e; } 4268 if (!type) { error(UFLDERR); type=INT; return e; }
4090 if (inmode || chk) { 4269 if (inmode || chk) {
4091 // bitfield will be checked after parse 4270 // bitfield will be checked after parse
4092 return e1; 4271 return e1;
4093 } else if(dsp) { 4272 } else if(dsp) {
4094 switch(car(e)) { 4273 switch(car(e)) {