Skip to content

Instantly share code, notes, and snippets.

@tshort
Last active December 30, 2019 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshort/b6598cece87d90ced5c14c7d569413cb to your computer and use it in GitHub Desktop.
Save tshort/b6598cece87d90ced5c14c7d569413cb to your computer and use it in GitHub Desktop.

I'd like to turn the following

i32_from_name(id) = @extern("i32_from_name", Int32, (Any,), string(id))

into:

i32_from_name(id) = ccall("extern i32_from_name", llvmcall, Int32, (Any,), string(id))

I just want to simplify the ccall with a macro or generated function, so I don't have to include the "extern " and llvmcall every time. I haven't had luck with either macros or generated functions.

As background, I want to compile Julia to WebAssembly. To do that, I want to compile Julia code statically: https://github.com/tshort/StaticCompiler.jl. This ccall incantation provides a way to call external functions. Here's an example of the LLVM code generated. From the WebAssembly point of view, this is how I'd call JavaScript functions from Julia. For other embedded or static use cases, you could call to functions in other libraries.

julia> i32_from_name(id) = ccall("extern i32_from_name", llvmcall, Int32, (Any,), string(id))
 i32_from_name (generic function with 1 method)
 
 julia> @code_llvm i32_from_name("a")
 
 ;  @ REPL[2]:1 within `i32_from_name'
 define i32 @julia_i32_from_name_17496(%jl_value_t addrspace(10)* nonnull) {
 top:
   %1 = call i32 @i32_from_name(%jl_value_t addrspace(10)* nonnull %0)
   ret i32 %1
 }
@tshort
Copy link
Author

tshort commented Dec 30, 2019

Got it figured out. See: tshort/StaticCompiler.jl#11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment