Mercurial > hg > CbC > old > device
changeset 336:d488b72254fb
bit-field done.
author | kono |
---|---|
date | Fri, 25 Jun 2004 03:56:48 +0900 |
parents | 4f98dc4b5fd8 |
children | 694cdf15c5bd |
files | Changes Makefile mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-codegen.c mc-codegen.h mc-parse.c test/bitfield.c test/bitfield1.c |
diffstat | 10 files changed, 1157 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/Changes Fri Jun 25 01:01:41 2004 +0900 +++ b/Changes Fri Jun 25 03:56:48 2004 +0900 @@ -5141,3 +5141,6 @@ Fri Jun 25 00:00:46 JST 2004 なんか、Union のテストコードを書いてない気がする。 +Union は、間違ってました。 + +bit field はできました。
--- a/Makefile Fri Jun 25 01:01:41 2004 +0900 +++ b/Makefile Fri Jun 25 03:56:48 2004 +0900 @@ -66,9 +66,9 @@ make check TARGET=test/tmp10 make check TARGET=test/tmp11 make check TARGET=test/tmp12 -# make check TARGET=test/tmp5 + make check TARGET=test/tmp5 make check TARGET=test/tmp7 -# make check TARGET=test/tmp8 + make check TARGET=test/tmp8 make check TARGET=test/tmp9 make check TARGET=test/enum make check TARGET=test/obsf @@ -77,6 +77,8 @@ make check TARGET=test/switch make check TARGET=test/strinit make check TARGET=test/code-gen-all + make check TARGET=test/bitfield.c + make check TARGET=test/bitfield1.c #MK =-make MK= check-all-code:
--- a/mc-code-ia32.c Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-code-ia32.c Fri Jun 25 03:56:48 2004 +0900 @@ -3211,5 +3211,171 @@ #endif +#if BIT_FIELD_CODE + +/* bit field alignment calcuration */ + +static void +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; + case CHAR: sign=1; bitsz= 8; align=1;break; + case UCHAR: bitsz= 8; align=1;break; + case SHORT: sign=1; bitsz=16; align=2;break; + case USHORT: sign=1; bitsz=16; align=2;break; + case LONGLONG: sign=1; bitsz=64; align=4;l=1; break; + case ULONGLONG: bitsz=64; align=4;l=1; break; + default: error(-1); + } + *psign = sign; + *pbitsz = bitsz; + *palign = align; + *pl = l; +} + +/* + bit field alignment calcuration + this is architecture depenedent + */ + +extern int +code_bit_field_disp(int type,int *poffset,int *bfd,int *sz) +{ + int sign,bitsz,align; + int i; + int bitpos = *bfd; + int offset = *poffset; + int l; + int bitsize = cadddr(type); + set_bitsz(type,&sign,&bitsz,&align,&l); + + if (bitsize>bitsz) { error(BTERR); bitsize = i; } + + /* bfd means previous bit field bit offset */ + if (bitpos) { + /* previous field is bit field and spaces may remain */ + /* calc previsous offset */ + + i= offset-(bitpos+7)/8; + + for(l = bitpos;l>0;l -= 8,i++) { + if ((i & (align-1))==0 && l+bitsize <= bitsz) { + /* alignment is correct and space remains */ + *poffset=offset=i; + i = l+bitsize; + *bfd = i; + *sz = (i+7)/8; +printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",l,bitsize,bitsz,*poffset); + return l; + } + } + } + + /* first bit-field */ + + if ((i=(offset & (align-1)))) { + *poffset = (offset += (align-i)); + } + bitpos = 0; + *bfd = bitsize; + *sz = (bitsize+7)/8; + +printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",bitpos,bitsize,bitsz,*poffset); + return bitpos; +} + +/* bit field value */ + +/* reg contains container value, result should be in reg */ +extern void +code_bit_field(int type,int bitpos,int reg) +{ + int sign,bitsz,l,align; + int bitsize = cadddr(type); + int i; + set_bitsz(type,&sign,&bitsz,&align,&l); +printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); + /* this implementation returns -1 for int i:1; */ + if (l==1) { + use_longlong(reg); + /* shift left */ + if (bitpos) + loprtc(LLSHIFT,reg,list2(CONST,bitpos)); + /* shift right */ + if ((i=bitsz-bitsize)) + loprtc(sign?LRSHIFT:LURSHIFT,reg,list2(CONST,i)); + } else { + use_int(reg); + /* shift left */ + if ((i=bitpos+(32-bitsz))) + oprtc(LSHIFT,reg,list2(CONST,i)); + /* shift right */ + if ((i=bitsz-bitsize+(32-bitsz))) + oprtc(sign?RSHIFT:URSHIFT,reg,list2(CONST,i)); + } +} + +/* bit field replacement */ + +static void +make_mask_and_or(int mask,int lreg) +{ +printf("# mask 0x%08x ~0x%08x\n",mask,~mask); + /* make and-mask */ + printf("\tmovl %s,%s\n",register_name(creg,0),register_name(dreg,0)); + oprtc(BOR,creg,~mask); + /* do conjunction */ + printf("\tandl %s,%s\n",register_name(lreg,0),register_name(creg,0)); + + /* make or-mask */ + oprtc(BAND,dreg,mask); + /* do disjunction */ + printf("\tor %s,%s\n",register_name(lreg,0),register_name(dreg,0)); +} + +extern void +code_bit_replace(int value,int lvalue,int type,int bitpos) +{ + int sign,bitsz,l,align; + int bitsize = cadddr(type); + int mask = 0; + int tmp = -1; + int i; + set_bitsz(type,&sign,&bitsz,&align,&l); +// printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); + if (l) { + use_longlong(value); + /* shift left */ + if ((i=bitsz-bitsize-bitpos)) + loprtc(LLSHIFT,value,list2(CONST,i)); + tmp = get_register(); + if (bitpos+bitsize>=32) { + /* make and-mask lower */ + mask = make_mask(bitpos>=32?bitpos-32:0,bitpos+bitsize-32-1); + make_mask_and_or(mask,tmp); + } + if (bitpos<32) { + /* make and-mask upper */ + mask = make_mask(bitpos,bitpos+bitsize>=32?31:bitpos+bitsize-1); + make_mask_and_or(mask,tmp); + } + } else { + use_int(value); + /* shift left */ + if ((i=bitsz-bitsize-bitpos)) + oprtc(LSHIFT,value,list2(CONST,i)); + tmp = get_register(); + /* make and-mask */ + mask = make_mask(bitpos+(32-bitsz),bitpos+bitsize-1+(32-bitsz)); + make_mask_and_or(mask,tmp); + } + if (tmp!=-1) free_register(tmp); +} + +#endif + /* end */ -
--- a/mc-code-mips.c Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-code-mips.c Fri Jun 25 03:56:48 2004 +0900 @@ -5415,5 +5415,178 @@ #endif + +#if BIT_FIELD_CODE + +/* bit field alignment calcuration */ + +static void +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; + case CHAR: sign=1; bitsz= 8; align=1;break; + case UCHAR: bitsz= 8; align=1;break; + case SHORT: sign=1; bitsz=16; align=2;break; + case USHORT: sign=1; bitsz=16; align=2;break; + case LONGLONG: sign=1; bitsz=64; align=4;l=1; break; + case ULONGLONG: bitsz=64; align=4;l=1; break; + default: error(-1); + } + *psign = sign; + *pbitsz = bitsz; + *palign = align; + *pl = l; +} + +/* + bit field alignment calcuration + this is architecture depenedent + */ + +extern int +code_bit_field_disp(int type,int *poffset,int *bfd,int *sz) +{ + int sign,bitsz,align; + int i; + int bitpos = *bfd; + int offset = *poffset; + int l; + int bitsize = cadddr(type); + set_bitsz(type,&sign,&bitsz,&align,&l); + + if (bitsize>bitsz) { error(BTERR); bitsize = i; } + + /* bfd means previous bit field bit offset */ + if (bitpos) { + /* previous field is bit field and spaces may remain */ + /* calc previsous offset */ + + i= offset-(bitpos+7)/8; + + for(l = bitpos;l>0;l -= 8,i++) { + if ((i & (align-1))==0 && l+bitsize <= bitsz) { + /* alignment is correct and space remains */ + *poffset=offset=i; + i = l+bitsize; + *bfd = i; + *sz = (i+7)/8; +// printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",l,bitsize,bitsz,*poffset); + return l; + } + } + } + + /* first bit-field */ + + if ((i=(offset & (align-1)))) { + *poffset = (offset += (align-i)); + } + bitpos = 0; + *bfd = bitsize; + *sz = (bitsize+7)/8; + +// printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",bitpos,bitsize,bitsz,*poffset); + return bitpos; +} + +/* bit field value */ + +/* reg contains container value, result should be in reg */ +extern void +code_bit_field(int type,int bitpos,int reg) +{ + int sign,bitsz,l,align; + int bitsize = cadddr(type); + int i; + set_bitsz(type,&sign,&bitsz,&align,&l); +// printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); + /* this implementation returns -1 for int i:1; */ + if (l==1) { + use_longlong(reg); + /* shift left */ + if (bitpos) + loprtc(LLSHIFT,reg,list2(CONST,bitpos)); + /* shift right */ + if ((i=bitsz-bitsize)) + loprtc(sign?LRSHIFT:LURSHIFT,reg,list2(CONST,i)); + } else { + use_int(reg); + /* shift left */ + if ((i=bitpos+(32-bitsz))) + oprtc(LSHIFT,reg,list2(CONST,i)); + /* shift right */ + if ((i=bitsz-bitsize+(32-bitsz))) + oprtc(sign?RSHIFT:URSHIFT,reg,list2(CONST,i)); + } +} + +/* bit field replacement */ + +static void +make_mask_and_or(int mask,int tmp,char *trn,char *crn,char *lrn) +{ +// printf("# mask 0x%08x ~0x%08x\n",mask,~mask); + code_const(~mask,tmp); + printf("\tor %s,%s,%s\n",trn,crn,trn); + /* do conjunction */ + printf("\tand %s,%s,%s\n",lrn,trn,lrn); + /* make or-mask */ + code_const(mask,tmp); + printf("\tand %s,%s,%s\n",trn,crn,trn); + /* do disjunction */ + printf("\tor %s,%s,%s\n",crn,trn,lrn); +} + +extern void +code_bit_replace(int value,int lvalue,int type,int bitpos) +{ + 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,&align,&l); +// printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); + if (l) { + use_longlong(value); + crn = lregister_name_low(value); + lrn = lregister_name_low(lvalue); + /* shift left */ + if ((i=bitsz-bitsize-bitpos)) + loprtc(LLSHIFT,value,list2(CONST,i)); + trn = register_name(tmp = get_register()); + if (bitpos+bitsize>=32) { + /* make and-mask lower */ + mask = make_mask(bitpos>=32?bitpos-32:0,bitpos+bitsize-32-1); + make_mask_and_or(mask,tmp,trn,crn,lrn); + } + crn = lregister_name_high(value); + lrn = lregister_name_high(lvalue); + if (bitpos<32) { + /* make and-mask upper */ + mask = make_mask(bitpos,bitpos+bitsize>=32?31:bitpos+bitsize-1); + make_mask_and_or(mask,tmp,trn,crn,lrn); + } + } else { + use_int(value); + crn = register_name(value); + lrn = register_name(lvalue); + /* shift left */ + if ((i=bitsz-bitsize-bitpos)) + oprtc(LSHIFT,value,list2(CONST,i)); + trn = register_name(tmp = get_register()); + /* make and-mask */ + mask = make_mask(bitpos+(32-bitsz),bitpos+bitsize-1+(32-bitsz)); + make_mask_and_or(mask,tmp,trn,crn,lrn); + } + if (tmp!=-1) free_register(tmp); +} + +#endif + /* end */ -
--- a/mc-code-powerpc.c Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-code-powerpc.c Fri Jun 25 03:56:48 2004 +0900 @@ -5133,12 +5133,13 @@ i= offset-(bitpos+7)/8; for(l = bitpos;l>0;l -= 8,i++) { - if ((i & (align-1))==0 && l+bitsize < bitsz) { + if ((i & (align-1))==0 && l+bitsize <= bitsz) { /* alignment is correct and space remains */ *poffset=offset=i; i = l+bitsize; *bfd = i; *sz = (i+7)/8; +// printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",l,bitsize,bitsz,*poffset); return l; } } @@ -5153,6 +5154,7 @@ *bfd = bitsize; *sz = (bitsize+7)/8; +// printf("# bitpos=%d bitsize=%d bitsz=%d offset=%d\n",bitpos,bitsize,bitsz,*poffset); return bitpos; } @@ -5166,6 +5168,7 @@ int bitsize = cadddr(type); int i; set_bitsz(type,&sign,&bitsz,&align,&l); +// printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); /* this implementation returns -1 for int i:1; */ if (l==1) { use_longlong(reg); @@ -5178,34 +5181,20 @@ } else { use_int(reg); /* shift left */ - if (bitpos) - oprtc(LSHIFT,reg,list2(CONST,bitpos)); + if ((i=bitpos+(32-bitsz))) + oprtc(LSHIFT,reg,list2(CONST,i)); /* shift right */ - if ((i=bitsz-bitsize)) + if ((i=bitsz-bitsize+(32-bitsz))) oprtc(sign?RSHIFT:URSHIFT,reg,list2(CONST,i)); } } /* bit field replacement */ -static int -make_mask(int from,int to) -{ - int mask = 0; - int bit = 1; - int i; - if (from<0||from>32) error(-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,char *crn,char *lrn) { +// printf("# mask 0x%08x ~0x%08x\n",mask,~mask); code_const(~mask,tmp); printf("\tor %s,%s,%s\n",trn,crn,trn); /* do conjunction */ @@ -5214,7 +5203,7 @@ code_const(mask,tmp); printf("\tand %s,%s,%s\n",trn,crn,trn); /* do disjunction */ - printf("\tor %s,%s,%s\n",lrn,trn,lrn); + printf("\tor %s,%s,%s\n",crn,trn,lrn); } extern void @@ -5227,6 +5216,7 @@ int i; char *crn,*lrn,*trn; set_bitsz(type,&sign,&bitsz,&align,&l); +// printf("# %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz); if (l) { use_longlong(value); crn = lregister_name_low(value); @@ -5235,16 +5225,16 @@ if ((i=bitsz-bitsize-bitpos)) loprtc(LLSHIFT,value,list2(CONST,i)); trn = register_name(tmp = get_register()); - if (bitpos+bitsize>32) { + if (bitpos+bitsize>=32) { /* make and-mask lower */ - mask = make_mask(bitpos>32?bitpos-32:0,bitpos+bitsize-32); + mask = make_mask(bitpos>=32?bitpos-32:0,bitpos+bitsize-32-1); make_mask_and_or(mask,tmp,trn,crn,lrn); } crn = lregister_name_high(value); lrn = lregister_name_high(lvalue); if (bitpos<32) { /* make and-mask upper */ - mask = make_mask(bitpos,bitpos+bitsize>32?31:bitpos+bitsize); + mask = make_mask(bitpos,bitpos+bitsize>=32?31:bitpos+bitsize-1); make_mask_and_or(mask,tmp,trn,crn,lrn); } } else { @@ -5256,7 +5246,7 @@ oprtc(LSHIFT,value,list2(CONST,i)); trn = register_name(tmp = get_register()); /* make and-mask */ - mask = make_mask(bitpos+(32-bitsz),bitpos+bitsize); + mask = make_mask(bitpos+(32-bitsz),bitpos+bitsize-1+(32-bitsz)); make_mask_and_or(mask,tmp,trn,crn,lrn); } if (tmp!=-1) free_register(tmp);
--- a/mc-codegen.c Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-codegen.c Fri Jun 25 03:56:48 2004 +0900 @@ -2271,7 +2271,8 @@ } else { sz = size(type); } - fields = list4(type,fields,(int)(n->nm),disp); + if (n!=&null_nptr) + fields = list4(type,fields,(int)(n->nm),disp); } else if (mode==GUDECL||mode==LUDECL) { if (car(type)==BIT_FIELD) { caddr(type) = 0; sz = size(cadr(type)); @@ -3213,7 +3214,7 @@ static int bit_field_repl(int e1,int e2,int t) { - /* e1 = e2 */ + /* e2 = e1 */ int lo = is_long_type(cadr(t)); g_expr(e1); if (lo) emit_lpush(); else emit_push(); @@ -3365,4 +3366,19 @@ jmp(l); } +extern int +make_mask(int from,int to) +{ + int mask = 0; + int bit = 1; + int i; + if (from<0||from>32) error(-1); + for (i=31;from<=i;i--,bit<<=1) { + if (i<=to) { + mask |= bit; + } + } + return mask; +} + /* end */
--- a/mc-codegen.h Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-codegen.h Fri Jun 25 03:56:48 2004 +0900 @@ -83,5 +83,7 @@ extern int is_code(NMTBL *fnptr); extern int is_function(NMTBL *fnptr); extern int scalar(int t); +extern int make_mask(int from,int to); + /* end */
--- a/mc-parse.c Fri Jun 25 01:01:41 2004 +0900 +++ b/mc-parse.c Fri Jun 25 03:56:48 2004 +0900 @@ -561,9 +561,9 @@ reverse(t); if (n == &null_nptr) { /* only bit field allows null identifier */ - if (!(type>0&&car(type)==BIT_FIELD)) - error(DCERR); - return; + if (!(type>0&&car(type)==BIT_FIELD)) { + error(DCERR); return; + } } if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS)) { if (mode!=GDECL) error(DCERR);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bitfield.c Fri Jun 25 03:56:48 2004 +0900 @@ -0,0 +1,539 @@ + + +typedef union { + struct _PTE { + unsigned long v:1; + unsigned long vsid:24; + unsigned long h:1; + unsigned long api:6; + unsigned long rpn:20; + + unsigned long :3; + unsigned long r:1; + unsigned long c:1; + unsigned long w:1; + unsigned long i:1; + unsigned long m:1; + unsigned long g:1; + unsigned long :1; + unsigned long pp:2; + } pte; + struct { + unsigned char v:1; + signed long vsid:24; + unsigned long h:1; + signed char api:6; + signed long rpn:20; + + signed long :3; + unsigned long r:1; + unsigned long c:1; + unsigned long w:1; + unsigned long i:1; + unsigned long m:1; + unsigned long g:1; + unsigned long :1; + signed long pp:2; + } sg; + unsigned long u[2]; +} PTE; + +PTE g; + +main() +{ + PTE a,*p; + int i = 0; + int j = 0; + + a.pte.rpn = 55; + j = a.pte.rpn; +printf("% 3d: %d\n",i++,j); + a.pte.c = 1; + j = a.pte.c; +printf("% 3d: %d\n",i++,j); + a.pte.pp = -1; + j = a.pte.pp; +printf("% 3d: %d\n",i++,j); + a.sg.pp = -1; + j = a.sg.pp; +printf("% 3d: %d\n",i++,j); + + p = &g; + + g.u[0]=0; + g.u[1]=0; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. v = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. vsid = -1; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. h = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. api = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. rpn = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.pte. r = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. c = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. w = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. i = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. m = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. g = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. pp = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. v = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. vsid = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. h = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. api = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. rpn = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.pte. r = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. c = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. w = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. i = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. m = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. g = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. pp = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. v = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. vsid = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. h = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. api = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. rpn = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.pte. r = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. c = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. w = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. i = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. m = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. g = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. pp = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +for(i=0;i<0x200;i++) { +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. v = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. vsid = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. h = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. api = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. rpn = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.pte. r = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. c = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. w = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. i = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. m = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. g = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.pte. pp = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); +} + + g = a; + + printf("%d %d %d\n",a.pte.pp,g.pte.pp,p->pte.pp); + main2(); + main5(); + return 0; +} + +main2() +{ + int i = 0; + + g.u[0]=0; + g.u[1]=0; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. v = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. vsid = -1; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. h = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. api = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. rpn = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.sg. r = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. c = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. w = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. i = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. m = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. g = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. pp = 3; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. v = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. vsid = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. h = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. api = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. rpn = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.sg. r = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. c = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. w = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. i = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. m = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. g = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. pp = 0; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. v = 0x55; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. vsid = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. h = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. api = 0x55; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. rpn = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.sg. r = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. c = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. w = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. i = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. m = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. g = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. pp = 0x5555555; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.u[0]=-1; + g.u[1]=-1; + +for(i=-0x100;i<0x100;i++) { +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. v = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. vsid = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. h = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. api = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. rpn = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + + g.sg. r = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. c = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. w = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. i = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. m = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. g = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); + g.sg. pp = i; +printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]); +} + +// printf("size of %d\n",sizeof(g.sg.pp)); no size of for bit-field + +} + +// int :12 bit-field cannot be a return value; +// main3( int a:8 ) bit-field cannot be an argument; +main3( int a ) +{ + // int b:9; // nor local variable + int b; + + + g.pte.rpn--; + g.pte.rpn++; + --g.pte.rpn; + ++g.pte.rpn; + g.pte.rpn += 3; + g.pte.rpn -= 3; + g.pte.rpn *= 3; + g.pte.rpn /= 3; + g.pte.rpn %= 3; + g.pte.rpn |= 3; + g.pte.rpn &= 3; + g.pte.rpn ^= 3; + + return --b; +} + + + + +union ll1 { + int a[8]; + struct { + char a:4; + long long v:33; + long long w:33; + long long x:33; + } b; +} ll1; + +union ll0 { + int a[8]; + struct { + char a:4; + long long v:48; + long long w:48; + long long x:48; + } b; +} ll0; + + +union ll { + int a[8]; + struct { + char a:4; + long long v:56; + long long w:56; + long long x:56; + } b; +} ll; + +union cc { + int a; + struct { + char a:1; + char b:4; + char c:7; + char d:4; + char e:4; + char f:4; + } b; +} cc; + +union ii { + int a; + struct { + int a:1; + int b:4; + int c:4; + int d:4; + int e:4; + int f:4; + } b; +} ii; + +int m1 = -1; +int p1 = 1; +int zero = 0; + +main5() +{ + ii.b.a = -1; + printf("00:%d\n",ii.b.a); + + ll1.b.v = m1; + printf("01:%llx\n",ll1.b.v); + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + ll1.b.v = zero; + ll1.b.w = m1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + ll1.b.w = zero; + ll1.b.x = m1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + + ll0.b.v = m1; + printf("01:%llx\n",ll0.b.v); + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + ll0.b.v = zero; + ll0.b.w = m1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + ll0.b.w = zero; + ll0.b.x = m1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + + ll.b.v = m1; + printf("1:%llx\n",ll.b.v); + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + ll.b.v = zero; + ll.b.w = m1; + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + ll.b.w = zero; + ll.b.x = m1; + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + + printf("char a:1; char b:4; char c:7; char d:4; char e:4; char f:4;\n"); + cc.b.a = m1; + printf("a:%08x\n",cc.a); + cc.b.b = m1; + printf("b:%08x\n",cc.a); + cc.b.c = m1; + printf("c:%08x\n",cc.a); + cc.b.d = m1; + printf("d:%08x\n",cc.a); + cc.b.e = m1; + printf("e:%08x\n",cc.a); + cc.b.f = m1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = m1; + printf("f:%08x\n",cc.a); + cc.b.a = zero; + printf("a:%08x\n",cc.a); + cc.b.b = zero; + printf("b:%08x\n",cc.a); + cc.b.c = zero; + printf("c:%08x\n",cc.a); + cc.b.d = zero; + printf("d:%08x\n",cc.a); + cc.b.e = zero; + printf("e:%08x\n",cc.a); + cc.b.f = zero; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = zero; + printf("f:%08x\n",cc.a); + cc.b.a = p1; + printf("a:%08x\n",cc.a); + cc.b.b = p1; + printf("b:%08x\n",cc.a); + cc.b.c = p1; + printf("c:%08x\n",cc.a); + cc.b.d = p1; + printf("d:%08x\n",cc.a); + cc.b.e = p1; + printf("e:%08x\n",cc.a); + cc.b.f = p1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = m1; + printf("f:%08x\n",cc.a); + cc.b.a = p1; + printf("a:%08x\n",cc.a); + cc.b.b = p1; + printf("b:%08x\n",cc.a); + cc.b.c = p1; + printf("c:%08x\n",cc.a); + cc.b.d = p1; + printf("d:%08x\n",cc.a); + cc.b.e = p1; + printf("e:%08x\n",cc.a); + cc.b.f = p1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + + ii.b.a = m1; + printf("6:%08x\n",ii.a); + ii.b.d = m1; + printf("6:%08x\n",ii.a); + printf("5:%d %d\n",ii.b.a,ii.b.d); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bitfield1.c Fri Jun 25 03:56:48 2004 +0900 @@ -0,0 +1,233 @@ + + struct aho { + int i:1,j:1; + char a:7; + char :7,k:1,:3; + long long v:33; + long long w:33; + long long x:33; + } a /* = {-1,3,3,3,3} */ ; + + union aho1 { + int i:1,j:1; + char a:7; + char :7,k:1,:3; + long long v:33; + long long w:33; + long long x:33; + } a11 ; + +union ll1 { + int a[8]; + struct { + char a:4; + long long v:33; + long long w:33; + long long x:33; + } b; +} ll1; + +union ll0 { + int a[8]; + struct { + char a:4; + long long v:48; + long long w:48; + long long x:48; + } b; +} ll0; + + +union ll { + int a[8]; + struct { + char a:4; + long long v:56; + long long w:56; + long long x:56; + } b; +} ll; + +union cc { + int a; + struct { + char a:1; + char b:4; + char c:7; + char d:4; + char e:4; + char f:4; + } b; +} cc; + +union ii { + int a; + struct { + int a:1; + int b:4; + int c:4; + int d:4; + int e:4; + int f:4; + } b; +} ii; + +main() +{ + printf("%d\n",a.i); + + ll1.b.v = -1; + printf("01:%llx\n",ll1.b.v); + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + ll1.b.v = 0; + ll1.b.w = -1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + ll1.b.w = 0; + ll1.b.x = -1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3], + ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7] + ); + + ll0.b.v = -1; + printf("01:%llx\n",ll0.b.v); + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + ll0.b.v = 0; + ll0.b.w = -1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + ll0.b.w = 0; + ll0.b.x = -1; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3], + ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7] + ); + + ll.b.v = -1; + printf("1:%llx\n",ll.b.v); + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + ll.b.v = 0; + ll.b.w = -1; + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + ll.b.w = 0; + ll.b.x = -1; + printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + + printf("char a:1; char b:4; char c:7; char d:4; char e:4; char f:4;\n"); + cc.b.a = -1; + printf("a:%08x\n",cc.a); + cc.b.b = -1; + printf("b:%08x\n",cc.a); + cc.b.c = -1; + printf("c:%08x\n",cc.a); + cc.b.d = -1; + printf("d:%08x\n",cc.a); + cc.b.e = -1; + printf("e:%08x\n",cc.a); + cc.b.f = -1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = -1; + printf("f:%08x\n",cc.a); + cc.b.a = 0; + printf("a:%08x\n",cc.a); + cc.b.b = 0; + printf("b:%08x\n",cc.a); + cc.b.c = 0; + printf("c:%08x\n",cc.a); + cc.b.d = 0; + printf("d:%08x\n",cc.a); + cc.b.e = 0; + printf("e:%08x\n",cc.a); + cc.b.f = 0; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = 0; + printf("f:%08x\n",cc.a); + cc.b.a = 1; + printf("a:%08x\n",cc.a); + cc.b.b = 1; + printf("b:%08x\n",cc.a); + cc.b.c = 1; + printf("c:%08x\n",cc.a); + cc.b.d = 1; + printf("d:%08x\n",cc.a); + cc.b.e = 1; + printf("e:%08x\n",cc.a); + cc.b.f = 1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + cc.a = -1; + printf("f:%08x\n",cc.a); + cc.b.a = 1; + printf("a:%08x\n",cc.a); + cc.b.b = 1; + printf("b:%08x\n",cc.a); + cc.b.c = 1; + printf("c:%08x\n",cc.a); + cc.b.d = 1; + printf("d:%08x\n",cc.a); + cc.b.e = 1; + printf("e:%08x\n",cc.a); + cc.b.f = 1; + printf("f:%08x\n",cc.a); + printf("3:%d %d\n",cc.b.c,cc.b.d); + + ii.b.a = -1; + printf("6:%08x\n",ii.a); + ii.b.d = -1; + printf("6:%08x\n",ii.a); + printf("5:%d %d\n",ii.b.a,ii.b.d); + + main5(); + return 0; +} + + +main5() +{ + int i,j,bit=1; + for(i=0;i<32;i++,bit<<=1) { + ii.a = bit; + printf("%08x:ii.b.a=%d ii.b.b=%d ii.b.c=%d ii.b.d=%d ii.b.e=%d ii.b.f=%d\n",bit, + ii.b.a,ii.b.b,ii.b.c,ii.b.d,ii.b.e,ii.b.f); + } + bit=1; + for(i=0;i<32;i++,bit<<=1) { + cc.a = bit; + printf("%08x:cc.b.a=%d cc.b.b=%d cc.b.c=%d cc.b.d=%d cc.b.e=%d cc.b.f=%d\n",bit, + cc.b.a,cc.b.b,cc.b.c,cc.b.d,cc.b.e,cc.b.f); + } + for(i=0;i<8;i++) { + bit=1; + for(j=0;j<32;j++,bit<<=1) { + ll.a[i] = bit; + printf("02:%08x %08x %08x %08x %08x %08x %08x %08x ", + ll.a[0],ll.a[1],ll.a[2],ll.a[3], + ll.a[4],ll.a[5],ll.a[6],ll.a[7] + ); + printf("ll.b.v=%d ll.b.w=%d ll.b.x=%d\n",ll.b.v,ll.b.w,ll.b.x); + } + } +} +