<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jan 7, 2011, at 1:31 PMPST, Lockal wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>I have a simple structure in my program:<br><br>struct pair { double value; int32_t type; };<br><br>Also I have 2 functions, which are called from LLVM code:<br><br>extern "C" void dump(pair s) {<br>    unsigned char *p = reinterpret_cast<unsigned char *>(&s);<br>    printf("#dump, %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",<br>      p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]);<br>}<br><br>extern "C" pair set() {<br>    pair ret = { 0, 3 };<br><br>    unsigned char *p = reinterpret_cast<unsigned char *>(&ret);<br>    printf("#set, %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",<br>      p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]);<br><br>    return ret;<br>}<br><br>LLVM code contains the same structure definition and prototypes for functions:<br><br>%0 = type { double, i32 }<br>declare %0 @set()<br>declare void @dump(%0)<br><br><br>So the question is: why the structure data is smashed after this code?<br><br>%a = call %0 @set()<br>call void @dump(%0 %a)<br><br>Output:<br>#set, 00000000 00000000 03000000<br>#dump, 00000000 0000f8ff 88b93709<br><br>Is there any way to return structure from C to LLVM by value?<br></div></blockquote><br></div><div>Yes, but what you're doing won't usually work.  You need to use IR that reflects the calling convention on your target.  sret is often the right thing to use; some targets are more complicated.  The best way to find out what you need is run llvm-gcc -S -emit-llvm on a testcase and look at the output file.</div><div><br></div></body></html>