Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

64-bit: Returning a multi-field structure by value does not work #65

Closed
benob opened this issue Aug 6, 2011 · 5 comments
Closed

64-bit: Returning a multi-field structure by value does not work #65

benob opened this issue Aug 6, 2011 · 5 comments

Comments

@benob
Copy link

benob commented Aug 6, 2011

When returning a structure from a function, only the first element is returned.

#include <stdio.h>

typedef struct {
    int x; int y;
} data;

data f(int x, int y) {
    data tmp;
    tmp.x = x;
    tmp.y = y;
    return tmp;
}

int main() {
    data z = f(5,6);
    printf("%d %d\n", z.x, z.y);
}

This program prints "5 0" instead of "5 6". If adding a third field to the structure, d8 dies with the following error:

ReferenceError: $ is not defined
    HEAP[$14]=$;; HEAP[$14+4]=1;; HEAP[$14+8]=5;

(llvm v2.9, v8 3.2.10.2, emscripten cca5278)

-- edit:
This only affects 64-bit compilation. Please compile with -m32 until 64-bit is properly supported.

@kripken
Copy link
Member

kripken commented Aug 6, 2011

That source code works for me with both llvm-gcc and clang with various emscripten settings.

Can you paste the generated .ll and .js files?

@benob
Copy link
Author

benob commented Aug 6, 2011

Here is the .ll file: http://pastebin.com/gtfAshzZ
and the .js file: http://pastebin.com/KrU6QYve

@kripken
Copy link
Member

kripken commented Aug 6, 2011

Looks like that is compiled as 64-bit? (target triple = "x86_64-unknown-linux-gnu" and many appearances of i64)

Emscripten doesn't support 64-bit code well, because it is a poor fit for JavaScript (JS integers are 32-bit, and while floats are 64-bit, the actual integers they can contain are only up to 53 bits or so).

Compiling the source with -m32 should make it work properly (you may need to install some 32-bit headers on your system).

Long-term, the solution for emscripten is to bundle our own headers and build only against them. We have an issue open for that.

@benob
Copy link
Author

benob commented Aug 6, 2011

Thanks for the tip, it does work fine.

I changed the issue to reflect that it only affects 64-bit compliation.

@kripken
Copy link
Member

kripken commented Aug 6, 2011

I added a FAQ entry for this. Marking as closed since I don't think there is a way to properly fix this. 32-bit compilation is the way to go for now.

@kripken kripken closed this as completed Aug 6, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants