HI, While I testing some code, I found some problem on Union handling. I've wrte following test code, and it has union assignment. The code's output is from pointerToUnion: chars mystring, length 64 from original: chars mystring, length 8000 It's caused by second char member(charlength) of LongestMember. For union assignment, llvm-backend seems generates assigning each member of largest union member. (I've checked it on llvm assembly level..) But sometimes there is padding between members and it's different for members. So in my opinion you should handle union assignment as pseudo array of word(or byte). (it's same for argument passing..) ====================================================#include <stdio.h> struct MyString { char *chars; int length; }; struct LongestMember { char *chars; char charlength; int empty1; int empty2; }; typedef union { MyString string; LongestMember mt; } UnionType; int main() { UnionType original; UnionType *pointerToUnion; UnionType buffer; pointerToUnion = &buffer; original.string.chars = "mystring"; original.string.length = 8000; *pointerToUnion = original; printf("from pointerToUnion: chars %s, length %d\n", pointerToUnion->string.chars, pointerToUnion->string.length); printf("from original: chars %s, length %d\n", original.string.chars, original.string.length); } ==================================================== Thanks, Quho.
Hi, I'm sorry, I found it on bugzilla database.. http://llvm.org/bugs/show_bug.cgi?id=1421 And there was instruction for fix it. Thanks Quho 2007/5/28, quho <armbox at gmail.com>:> HI, > > While I testing some code, I found some problem on Union handling. > > I've wrte following test code, and it has union assignment. > > The code's output is > > from pointerToUnion: chars mystring, length 64 > from original: chars mystring, length 8000 > > It's caused by second char member(charlength) of LongestMember. > > For union assignment, llvm-backend seems generates assigning each > member of largest union member. (I've checked it on llvm assembly > level..) > But sometimes there is padding between members and it's different for members. > > So in my opinion you should handle union assignment as pseudo array of > word(or byte). > (it's same for argument passing..) > > > ====================================================> #include <stdio.h> > > struct MyString { > char *chars; > int length; > }; > > struct LongestMember { > char *chars; > char charlength; > int empty1; > int empty2; > }; > > typedef union { > MyString string; > LongestMember mt; > } UnionType; > > > int main() { > UnionType original; > UnionType *pointerToUnion; > UnionType buffer; > > pointerToUnion = &buffer; > > original.string.chars = "mystring"; > original.string.length = 8000; > > *pointerToUnion = original; > > printf("from pointerToUnion: chars %s, length %d\n", > pointerToUnion->string.chars, pointerToUnion->string.length); > printf("from original: chars %s, length %d\n", > original.string.chars, original.string.length); > } > ====================================================> > > Thanks, > Quho. >-- Armbox.
On Mon, 28 May 2007, quho wrote:> While I testing some code, I found some problem on Union handling. > I've wrte following test code, and it has union assignment.BTW, this is fixed in mainline now. Thanks for the report. -Chris> The code's output is > > from pointerToUnion: chars mystring, length 64 > from original: chars mystring, length 8000 > > It's caused by second char member(charlength) of LongestMember. > > For union assignment, llvm-backend seems generates assigning each > member of largest union member. (I've checked it on llvm assembly > level..) > But sometimes there is padding between members and it's different for members. > > So in my opinion you should handle union assignment as pseudo array of > word(or byte). > (it's same for argument passing..) > > > ====================================================> #include <stdio.h> > > struct MyString { > char *chars; > int length; > }; > > struct LongestMember { > char *chars; > char charlength; > int empty1; > int empty2; > }; > > typedef union { > MyString string; > LongestMember mt; > } UnionType; > > > int main() { > UnionType original; > UnionType *pointerToUnion; > UnionType buffer; > > pointerToUnion = &buffer; > > original.string.chars = "mystring"; > original.string.length = 8000; > > *pointerToUnion = original; > > printf("from pointerToUnion: chars %s, length %d\n", > pointerToUnion->string.chars, pointerToUnion->string.length); > printf("from original: chars %s, length %d\n", > original.string.chars, original.string.length); > } > ====================================================> > > Thanks, > Quho. > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-Chris -- http://nondot.org/sabre/ http://llvm.org/