GithubHelp home page GithubHelp logo

Comments (3)

ElvishJerricco avatar ElvishJerricco commented on September 3, 2024

So both party's are at fault here. LLVM is failing to support the parameter lists, and GHC is failing to declare a proper return type. C allows you to declare C extern functions with rettype name(); // empty param list. You can then call this function with parameters, as long as the types of parameters you pass match the types expected by the actual implementation. The only requirement is that you specified the return type correctly. GHC is just doing void f(); for all the C externs it needs (EDIT: by way of EFF_ f;), and casting them to the expected function type. This is fine on most platforms, but technically not supported by the C standard (due to the inaccurate return type), so it fails on wasm. GHC is allowed to use the implicit param types, but it is not allowed to just use void as the return type and expect that to be ok because it gets cast. So the return type problem is GHC's fault.

But LLVM is also failing to produce the proper parameter types; it's just always using a single i32 param, because LLVM represents this stuff as varargs functions. WebAssembly's implementation of varargs is to take a single stack pointer argument, and find the variable number of arguments on the linear memory stack instead of as wasm parameters. On other platforms, representing int f(); as int f(...); works because the C ABI happens to work the same way for both. But on wasm, this isn't true, so the fact that LLVM uses this representation for parameters is incorrect.

And really, since you can cast function pointers and move them into variables, inferring the correct type is probably undecidable in the large. So unless the C standard explicitly doesn't support rettype f(); being cast to a function pointer, this may actually be a problem in the wasm spec.

All in all, I'm saying we should fix this in GHC :P

from ghc.

ElvishJerricco avatar ElvishJerricco commented on September 3, 2024

Ideally, the generated C would not use EFF_ f;, and instead use a local extern.

void bar() {
  extern int foo(int, int);
  int x = foo(1, 2);
}

The extern should be local because I've been told that sometimes the same symbol will end up being declared multiple times in the same file, with different types (somehow). EDIT: If this is only true because of the potential use of varargs, we probably don't have to worry about it, since the C standard says you are not allowed to declare a function with non-variadic type if its implementation uses variadic arguments (i.e. the fact that this works on other platforms is just luck with the C ABI).

from ghc.

ElvishJerricco avatar ElvishJerricco commented on September 3, 2024

Couple of useful links:

I that SO answer, they point out that casting a function to void * is undefined behavior, and that's definitely something GHC is doing.

from ghc.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.