[LLVMdev] Problem in llvm gcc back-end

quho armbox at gmail.com
Sun May 27 21:39:22 PDT 2007


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.



More information about the llvm-dev mailing list