Comments (3)
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.
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.
Couple of useful links:
- https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
- https://bugs.llvm.org/show_bug.cgi?id=35385
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)
- Linking stage randomly stops HOT 5
- The `primitive` haskell lib does not build HOT 4
- Unregisterised code size is two orders of magnitude too large HOT 3
- fprintCCS_stderr signature mismatch
- C FFI Compilation bug HOT 3
- Panic while linking/ creating static lib for integer-logarithms HOT 1
- Add stage=1 in mk/build.mk HOT 2
- GHC cannot create output dir tree
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ghc.