Mercurial > hg > CbC > old > device
changeset 331:f25aa4f03198
bit-field continue...
author | kono |
---|---|
date | Wed, 23 Jun 2004 22:37:32 +0900 |
parents | fa4c7b15d7ed |
children | ce85d6106119 |
files | .gdbinit Changes mc-code-powerpc.c mc-code.h mc-codegen.c mc-codegen.h mc-parse.c mc.h |
diffstat | 8 files changed, 185 insertions(+), 171 deletions(-) [+] |
line wrap: on
line diff
--- a/.gdbinit Wed Jun 23 17:12:33 2004 +0900 +++ b/.gdbinit Wed Jun 23 22:37:32 2004 +0900 @@ -1,7 +1,8 @@ tb main # run -s test/arg.c # run -s -ob00.s mc-parse.c -run -s l.c +# run -s l.c +run -s test/bitfield.c # run -s test/code-gen-all.c define regs printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4
--- a/Changes Wed Jun 23 17:12:33 2004 +0900 +++ b/Changes Wed Jun 23 22:37:32 2004 +0900 @@ -5134,3 +5134,6 @@ bit_field の大域変数の初期化ができない。いったん、どっかに 貯めないとだめ。assign_data level? + +なんか、assign_expr の 引数 type が大域変数を上書きしてました。 +それが恥ずかしいバグの原因だったのか。
--- a/mc-code-powerpc.c Wed Jun 23 17:12:33 2004 +0900 +++ b/mc-code-powerpc.c Wed Jun 23 22:37:32 2004 +0900 @@ -1858,6 +1858,9 @@ increment_function_arg(int e3,int *pnargs,int *preg_arg,int *pfreg_arg) { int nargs=0,reg_arg=0,freg_arg=0; int t=caddr(e3); + if (t>=0&&(car(t)==BIT_FIELD)) { + t = cadr(t); + } if(scalar(t)) { nargs ++ ; reg_arg++; } else if (t==LONGLONG||t==ULONGLONG) { @@ -1886,6 +1889,9 @@ static int get_input_arg(int t,int mode,int nargs,int reg_arg,int freg_arg) { + if (t>=0&&(car(t)==BIT_FIELD)) { + t = cadr(t); + } if(scalar(t)) { if (mode==AS_SAVE) { return get_register_var(0); @@ -5080,6 +5086,7 @@ set_bitsz(int type,int *psign,int *pbitsz,int *palign,int *pl) { int sign=0,bitsz; + int align,l=0; switch(cadr(type)) { case INT: sign=1; bitsz=32; align=4;break; case UNSIGNED: bitsz=32; align=4;break; @@ -5105,13 +5112,13 @@ extern int code_bit_field_disp(int type,int *poffset,int *bfd,int *sz) { - int sign,bitsz; + int sign,bitsz,align; int i; int bitpos = *bfd; int offset = *poffset; int l; int bitsize = cadddr(type); - set_bitsz(type,&sign,&bitsz,&l); + set_bitsz(type,&sign,&bitsz,&align,&l); if (bitsize>bitsz) { error(BTERR); bitsize = i; } @@ -5150,29 +5157,29 @@ /* reg contains container value, result should be in reg */ extern void -code_bit_field(int type,int bit_offset,int bit_size,int reg) +code_bit_field(int type,int bitpos,int reg) { - int sign,bitsz,l; + int sign,bitsz,l,align; int bitsize = cadddr(type); int i; - set_bitsz(type,&sign,&bitsz,&l); + set_bitsz(type,&sign,&bitsz,&align,&l); /* this implementation returns -1 for int i:1; */ if (l==1) { use_longlong(reg); /* shift left */ - if (bit_offset) - loprtc(LLSHIFT,reg,bit_offset); + if (bitpos) + loprtc(LLSHIFT,reg,bitpos); /* shift right */ if ((i=bitsz-bitsize)) - loprtc(sign?LRSHIFT:LRUSHIFT,reg,i); + loprtc(sign?LRSHIFT:LURSHIFT,reg,i); } else { use_int(reg); /* shift left */ - if (bit_offset) - oprtc(LSHIFT,reg,bit_offset); + if (bitpos) + oprtc(LSHIFT,reg,bitpos); /* shift right */ if ((i=bitsz-bitsize)) - oprtc(sign?RSHIFT:RUSHIFT,reg,i); + oprtc(sign?RSHIFT:URSHIFT,reg,i); } } @@ -5185,15 +5192,16 @@ int bit = 1; int i; if (from<0||from>32) error(-1); - for (i=31;from<=i;i--,bit<<1) { + for (i=31;from<=i;i--,bit<<=1) { if (i<=to) { mask |= bit; } } + return mask; } static void -make_mask_and_or(int mask,int tmp,char *trn,*crn,*lrn) +make_mask_and_or(int mask,int tmp,char *trn,char *crn,char *lrn) { code_const(~mask,tmp); printf("\tor %s,%s,%s\n",trn,crn,trn); @@ -5207,21 +5215,22 @@ } extern void -code_bit_replace(int value,int lvalue,int type,int bit_offset) +code_bit_replace(int value,int lvalue,int type,int bitpos) { - int sign,bitsz,l; + int sign,bitsz,l,align; int bitsize = cadddr(type); int mask = 0; int tmp = -1; + int i; char *crn,*lrn,*trn; - set_bitsz(type,&sign,&bitsz,&l); + set_bitsz(type,&sign,&bitsz,&align,&l); if (l) { use_longlong(value); crn = lregister_name_low(value); lrn = lregister_name_low(lvalue); /* shift left */ if ((i=bitsz-bitsize-bitpos)) - loprtc(LLSHIFT,reg,i); + loprtc(LLSHIFT,value,i); trn = register_name(tmp = get_register()); if (bitpos+bitsize>32) { /* make and-mask lower */ @@ -5241,7 +5250,7 @@ lrn = register_name(lvalue); /* shift left */ if ((i=bitsz-bitsize-bitpos)) - oprtc(LSHIFT,reg,i); + oprtc(LSHIFT,value,i); trn = register_name(tmp = get_register()); /* make and-mask */ mask = make_mask(bitpos+(32-bitsz),bitpos+bitsize);
--- a/mc-code.h Wed Jun 23 17:12:33 2004 +0900 +++ b/mc-code.h Wed Jun 23 22:37:32 2004 +0900 @@ -250,7 +250,7 @@ /* bit field alignment calcuration */ -extern int code_bit_field_disp(int type,int *offset,int *bfd,int *sz,int mode); +extern int code_bit_field_disp(int type,int *offset,int *bfd,int *sz); /* bit field value */
--- a/mc-codegen.c Wed Jun 23 17:12:33 2004 +0900 +++ b/mc-codegen.c Wed Jun 23 22:37:32 2004 +0900 @@ -47,9 +47,10 @@ #endif #if BIT_FIELD_CODE +static int bit_field_repl(int e1,int e2,int t); static int bit_field(int e1,int t); static int bassign(int e1,int e2,int t); -static int bassop(int e1,int e2,int op,int t); +static int bassop(int e1,int e2,int op,int t,int post); #endif @@ -468,9 +469,15 @@ return bit_field(e2,caddr(e1) /* type */); case BASS: return bassign(e2,caddr(e1),cadr(cadddr(e1))/* type */); + case BPREINC: + return bassop(e2,list2(CONST,caddr(e1)),ADD, + cadr(cadddr(e1))/* type */,0); + case BPOSTINC: + return bassop(e2,list2(CONST,caddr(e1)),ADD, + cadr(cadddr(e1))/* type */,1); case BASSOP: return bassop(e2,caddr(e1),car(cadddr(e1)),/* op */ - cadr(cadddr(e1))/* type */); + cadr(cadddr(e1))/* type */,0); case BFD_REPL: return bit_field_repl(e2,caddr(e1),cadddr(e1) /* type */); #endif @@ -1655,7 +1662,7 @@ stype=type; type = type0; e2 = rvalue(e2); - e1=assign_expr(e1,e2,t,type); + e1=assign_expr(e1,e2,t); type=stype; return e1; } @@ -1665,17 +1672,17 @@ /* with conversion (will destroy type global variable) */ extern int -assign_expr(int e1,int e2,int t,int type) { +assign_expr(int e1,int e2,int t) { if(t==VOID) error(TYERR); if(t==CHAR||t==UCHAR) { e2=(t==UCHAR)?unsigned_value(e2,type):int_value(e2,type); if (!integral(type)) error(TYERR); - type= INT;return(list3(CASS,e1,e2)); + type= t==CHAR?INT:UNSIGNED;return(list3(CASS,e1,e2)); } else if(t==SHORT||t==USHORT) { e2=(t==USHORT)?unsigned_value(e2,type):int_value(e2,type); if (!integral(type)) error(TYERR); - type= t;return(list3(SASS,e1,e2)); + type= t==SHORT?INT:UNSIGNED; return(list3(SASS,e1,e2)); #if FLOAT_CODE } else if(t==DOUBLE) { e2=double_value(e2,type); @@ -1953,7 +1960,8 @@ /* *new = *new op e3 */ n = list2(LVAR,new_lvar(size_of_int)); g_expr_u(assign_expr0(n,list2(ADDRESS,e2),INT,INT)); - g_expr(assign_expr0(list2(INDIRECT,n),list3(op,n,e3),t,t)); + g_expr(assign_expr0(list2(INDIRECT,n), + list3(op,list2(LRINDIRECT,n),e3),t,t)); free_lvar(cadr(n)); return; } @@ -2237,7 +2245,7 @@ bit_field_disp=sbit_f; // default is 0, recover only here. // n->ty = list4(BIT_FIELD,type,bit_offset,bit_width); caddr(n->ty) = code_bit_field_disp( - cadr(n->ty),cadddr(n->ty),&disp,&bit_field_disp,&sz); + n->ty,&disp,&bit_field_disp,&sz); /* bit_field_disp is next bit posision */ } else { sz = size(type); @@ -2402,7 +2410,7 @@ if (t>0&&car(t)==BIT_FIELD) { sz = 0; bfd = caddr(t); /* bit_field_disp */ - code_bit_field_disp(cadr(t),cadddr(t),&offset,&bfd,&sz); + code_bit_field_disp(t,&offset,&bfd,&sz); return offset+sz; } return offset+((t==EMPTY)?cadr(e):size(t)); @@ -2598,6 +2606,7 @@ { int op; + if (e==0) error(-1); op = 0; switch(type) { case INT: break; @@ -2612,6 +2621,7 @@ case FLOAT: op=FOP; break; case DOUBLE: op=DOP; break; case CODE: return e; + case 0: error(-1); return e; default: if (integral(type)) break; switch(car(type)) { @@ -2630,9 +2640,9 @@ case POINTER: break; case BIT_FIELD: - return list3(RBIT_FIELD,rvalue(cadddr(e)),type); + return list3(RBIT_FIELD,rvalue_t(cadr(e),cadr(type)),type); /* byte rvalue, type */ - type = cadr(e); + type = cadr(type); break; default: error(TYERR); @@ -3164,9 +3174,7 @@ bit_field(int e1,int t) { g_expr(e1); - code_bit_field(cadr(t) /* type */, - caddr(t) /* bit offset */, - USE_CREG); + code_bit_field(t, caddr(t) /* bit offset */, USE_CREG); return cadr(t); } @@ -3180,13 +3188,14 @@ g_expr(e1); code_bit_replace(USE_CREG,(e2=lo?emit_lpop():pop_register()), t /* type */,caddr(t) /* bit offset */); - if (lo) emit_lpop_free(e2) else emit_pop_free(e2); + if (lo) emit_lpop_free(e2); else emit_pop_free(e2); return cadr(t); } static int -bassop(int e2,int e3,int op,int t) +bassop(int e2,int e3,int op,int t,int post) { + int n; int type = cadr(t); /* if op==NULL e2 = e3 */ /* e2 = e2 op e3; */ @@ -3199,9 +3208,9 @@ /* new = &e2 */ /* *new = *new op e3 */ n = list2(LVAR,new_lvar(size_of_int)); - g_expr_u(assign_expr0(n,list2(ADDRESS,e2),INT,INT)); + g_expr_u(assign_expr0(n,list2(ADDRESS,cadr(e2)),INT,INT)); g_expr(assign_expr0(list2(INDIRECT,n), - list4(BFD_REPL,n,op?list3(op,n,e3):e3,t), + list4(BFD_REPL,n,op?list3(op,rvalue_t(list2(INDIRECT,n),t),e3):e3,t), type,type)); free_lvar(cadr(n)); return type; @@ -3210,7 +3219,7 @@ static int bassign(int e2,int e3,int t) { - return bassop(e2,e3,0,t); + return bassop(e2,e3,0,t,0); } #endif
--- a/mc-codegen.h Wed Jun 23 17:12:33 2004 +0900 +++ b/mc-codegen.h Wed Jun 23 22:37:32 2004 +0900 @@ -18,78 +18,57 @@ /* used by mc-parse */ + +extern NMTBL * def(NMTBL *n); +extern int arg_reorder(int arg,int new_arg); +extern int assign_data(int e, int t, NMTBL *n,int offset); +extern int assign_expr(int e1,int e2,int t); +extern int assign_expr0(int e1,int e2,int t,int type0); +extern int assop(int e1,int e2,int op,int t,int no_float); +extern int backdef(void); +extern int binop(int op, int e1, int e2, int t1, int t2); +extern int cexpr(int e); +extern int cond(int t,int e1,int e2,int e3); +extern int correct_type(int e,int t); +extern int csvalue(); +extern int function_type(int e1,int *dots); +extern int gexpr(int e1,int use0); +extern int indop(int e); +extern int integral(int t); +extern int new_lvar(int size); +extern int new_lvar0(int sz); extern int rvalue(int e); extern int rvalue_t(int e,int t); -extern int integral(int t); - - +extern int search_struct_type(int type,char *name,int *dsp); +extern int strop(int e,int ind); extern void arg_register(NMTBL *fnptr); -extern int arg_reorder(int arg,int new_arg); -extern int assign_data(int e, int t, NMTBL *n,int offset); -extern int assop(int e1,int e2,int op,int t,int no_float); -extern int backdef(void); -extern int cexpr(int e); +extern void bexpr(int e1, char cond, int l1); +extern void bexpr_u(int e1, char cond, int l1); extern void checkret(void); extern void closing(); extern void cmpdimm(int e, int csreg,int label,int cond); +extern void codegen_decl_init(); /* called before each declaration */ extern void codegen_init(); /* called only once */ extern void codegen_reinit(); /* called for each file */ -extern void codegen_decl_init(); /* called before each declaration */ -extern int cond(int t,int e1,int e2,int e3); -extern int correct_type(int e,int t); -extern int csvalue(); extern void data_closing(NMTBL *n); extern void def_label(int cslabel, int dlabel); extern void emit_init_vars(void); +extern void fcheck(NMTBL *n); extern void fdecl_struct(int fntype); -extern int function_type(int e1,int *dots); -extern void lcheck(int e); -extern void opening(char *filename); -extern void parallel_rassign(int assigns); -extern int strop(int e,int ind); - -extern void def_label(int cslabel, int dlabel); -extern NMTBL * def(NMTBL *n); -extern int search_struct_type(int type,char *name,int *dsp); -extern int gexpr(int e1,int use0); -extern void bexpr_u(int e1, char cond, int l1); -extern void bexpr(int e1, char cond, int l1); -extern void ret(void); -extern void replace_return_struct(int func,int left); -extern int assign_expr0(int e1,int e2,int t,int type0); -extern int assign_expr(int e1,int e2,int t,int type); -extern int binop(int op, int e1, int e2, int t1, int t2); -extern int indop(int e); -extern void fcheck(NMTBL *n); - -extern void init_free_lvar_list(); extern void free_lvar(int disp); -extern int new_lvar(int size); -extern int new_lvar0(int sz); - extern void gen_code_enter(char *name); extern void gen_code_enter1(int args); extern void gen_code_leave(char *name); extern void gen_enter(char *name); extern void gen_enter1(); +extern void gen_jmp(int l); extern void gen_leave(int control, char *name); -extern void gen_jmp(int l); - - -extern int assign_expr0(int e1,int e2,int t,int type0) ; -extern int assign_expr(int e1,int e2,int t,int type) ; -extern void bexpr_u(int e1, char cond, int l1); -extern int binop(int op, int e1, int e2, int t1, int t2); -extern void fcheck(NMTBL *n); -extern void replace_return_struct(int func,int left) ; -extern int gexpr(int e1,int use0); -extern void bexpr_u(int e1, char cond, int l1); -extern void bexpr(int e1, char cond, int l1); +extern void init_free_lvar_list(); +extern void lcheck(int e); +extern void opening(char *filename); +extern void parallel_rassign(int assigns); +extern void replace_return_struct(int func,int left); extern void ret(void); -extern void def_label(int cslabel, int dlabel); -extern NMTBL * def(NMTBL *n); -extern int indop(int e); -extern int search_struct_type(int type,char *name,int *dsp); /* used by mc-cod-* */
--- a/mc-parse.c Wed Jun 23 17:12:33 2004 +0900 +++ b/mc-parse.c Wed Jun 23 22:37:32 2004 +0900 @@ -560,10 +560,12 @@ n=decl0(); reverse(t); if (n == &null_nptr) { - error(DCERR); + /* only bit field allows null identifier */ + if (!(type>0&&car(type)==BIT_FIELD)) + error(DCERR); return; } - if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS )) { + if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS)) { if (mode!=GDECL) error(DCERR); stypedecl=sd; if (car(type)==CODE) { @@ -574,7 +576,7 @@ } conv->return_type_(type,n,sd); def(n); - if (sym==ASS) { decl_data(type,n,0,0); data_closing(n); } + if (sym==ASS && n!=&null_nptr) { decl_data(type,n,0,0); data_closing(n); } while(sym==COMMA) { conv->comma_(); @@ -582,10 +584,13 @@ type=t; n=decl0(); reverse(t); - if(n == &null_nptr) error(DCERR); + if(n == &null_nptr) { + if (!(type>0&&car(type)==BIT_FIELD)) + error(DCERR); + } conv->return_type_(type,n,1); def(n); - if (sym==ASS) { decl_data(type,n,0,0); data_closing(n); } + if (sym==ASS && n!=&null_nptr) {decl_data(type,n,0,0);data_closing(n);} } if(sym!=SM) error(DCERR); conv->sm_(); @@ -796,14 +801,16 @@ with arg type list. See def/ADECL */ if (mode!=GDECL) n->dsp=arg; - } else if(sym==COLON) { /* brain dead bit-field */ + } else if(sym==COLON) { /* bit-field */ if (mode==GSDECL||mode==GUDECL||mode==LSDECL||mode==LUDECL) { if (scalar(type) || type==LONGLONG || type==ULONGLONG) { getsym(0); - n->ty = list4(BIT_FIELD,type,0 /* bit offset */,symval); + type = n->ty = + list4(BIT_FIELD,type,0 /* bit offset */,symval); + getsym(0); } - } - error(DCERR); + } else + error(DCERR); return n; } else return n; @@ -2001,8 +2008,7 @@ t=type; getsym(0); e2=rvalue(expr1()); - e1 = assign_expr(e1,e2,t,type); - type = t; + e1 = assign_expr(e1,e2,t); return e1; case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: case EOR+AS: case BOR+AS: case MOD+AS: @@ -2256,6 +2262,8 @@ if(type==DOUBLE) return(list3(DPREINC,e,dir)); #endif + if(type>0 && car(type)==BIT_FIELD) + return(list3(BPREINC,e,dir)); if(car(type)!=POINTER) error(TYERR); return(list4(UPREINC,e, @@ -2401,6 +2409,8 @@ if(type==ULONGLONG) return(list3(LUPOSTINC,e,dir)); #endif + if(type>0 && car(type)==BIT_FIELD) + return(list3(BPOSTINC,e,dir)); if(car(type)!=POINTER) error(TYERR); return(list4(UPOSTINC,e,
--- a/mc.h Wed Jun 23 17:12:33 2004 +0900 +++ b/mc.h Wed Jun 23 22:37:32 2004 +0900 @@ -200,52 +200,54 @@ #define ALLOCA 28 #define BIT_FIELD 29 #define RBIT_FIELD 30 -#define CONV 31 +#define BPREINC 31 +#define BPOSTINC 32 +#define CONV 33 #define UNARY_ARGS(i) (ADDRESS<=(i%SOP)&&(i%SOP)<=CONV) /* binary argments */ -#define MUL 32 -#define UMUL 33 -#define DIV 34 -#define UDIV 35 -#define MOD 36 -#define UMOD 37 -#define ADD 38 -#define SUB 39 -#define CMP 40 -#define RSHIFT 41 -#define URSHIFT 42 -#define LSHIFT 43 -#define ULSHIFT 44 -#define GT 45 -#define UGT 46 -#define GE 47 -#define UGE 48 -#define LT 49 -#define ULT 50 -#define LE 51 -#define ULE 52 -#define EQ 53 -#define NEQ 54 -#define BAND 55 -#define EOR 56 -#define BOR 57 -#define LAND 58 -#define LOR 59 -#define ASS 60 -#define UCMP 61 -#define UCMPGE 62 -#define CMPGE 63 -#define CMPEQ 64 -#define CMPNEQ 65 -#define ASSOP 66 -#define COMMA 67 +#define MUL 34 +#define UMUL 35 +#define DIV 36 +#define UDIV 37 +#define MOD 38 +#define UMOD 39 +#define ADD 40 +#define SUB 41 +#define CMP 42 +#define RSHIFT 43 +#define URSHIFT 44 +#define LSHIFT 45 +#define ULSHIFT 46 +#define GT 47 +#define UGT 48 +#define GE 49 +#define UGE 50 +#define LT 51 +#define ULT 52 +#define LE 53 +#define ULE 54 +#define EQ 55 +#define NEQ 56 +#define BAND 57 +#define EOR 58 +#define BOR 59 +#define LAND 60 +#define LOR 61 +#define ASS 62 +#define UCMP 63 +#define UCMPGE 64 +#define CMPGE 65 +#define CMPEQ 66 +#define CMPNEQ 67 +#define ASSOP 68 +#define COMMA 69 -#define CASS 68 -#define CASSOP 69 -#define CUASSOP 70 +#define CASS 70 +#define CASSOP 71 +#define CUASSOP 72 #define SASS (SOP+CASS) #define SASSOP (SOP+CASSOP) @@ -301,18 +303,18 @@ #define LEOR (LOP+EOR) #define LBOR (LOP+BOR) -#define BASS 71 -#define BASSOP 72 -#define BFD_REPL 73 +#define BASS 73 +#define BASSOP 74 +#define BFD_REPL 75 -#define STASS 74 +#define STASS 76 #define BINARY_ARGS(i) (MUL<=(i%SOP)&&(i%SOP)<=STASS) /* tarnary argments */ -#define COND 75 +#define COND 77 #define SCOND (SOP+COND) #define DCOND (DOP+COND) #define FCOND (FOP+COND) @@ -322,19 +324,19 @@ /* not appeared as tags */ -#define I2I 76 -#define I2U 77 -#define I2D 78 -#define I2F 79 -#define I2LL 80 -#define I2ULL 81 +#define I2I 78 +#define I2U 79 +#define I2D 80 +#define I2F 81 +#define I2LL 82 +#define I2ULL 83 -#define U2I 82 -#define U2U 83 -#define U2D 84 -#define U2F 85 -#define U2LL 86 -#define U2ULL 87 +#define U2I 84 +#define U2U 85 +#define U2D 86 +#define U2F 87 +#define U2LL 88 +#define U2ULL 89 #define D2I (DOP+I2I) #define D2U (DOP+I2U) @@ -364,17 +366,17 @@ #define ULL2LL (LOP+U2LL) #define ULL2ULL (LOP+U2ULL) -#define LPAR 88 -#define RPAR 89 -#define LBRA 90 -#define RBRA 91 -#define LC 92 -#define RC 93 -#define COLON 94 -#define SM 95 -#define PERIOD 96 -#define ARROW 97 -#define CNAME 98 +#define LPAR 90 +#define RPAR 91 +#define LBRA 92 +#define RBRA 93 +#define LC 94 +#define RC 95 +#define COLON 96 +#define SM 97 +#define PERIOD 98 +#define ARROW 99 +#define CNAME 100 /* tree node tags end */ @@ -402,7 +404,8 @@ #define REG_ERR 20 #define CODE_ERR 21 #define MSERR 22 -#define INERR 23 +#define BTERR 23 +#define INERR 24 /* error number end */