Arafangion
Arafangion

Reputation: 11908

How do I generate LLVM bitcode for use by emscripten?

I am investigating emscripten for a personal project, and I would like to use a language other than C or C++ to do so.

However, while I am investigating emscripten, I figured I should use a trivial 'hello world' example written in C.

I know that I should compile this using emcc:

$ python `which emcc` tmp.c

And this will generate a working a.out.js file for me. So far it's good.

However, I want to use a different language, which means I can't use emcc or emcc++, so I want to generate the llvm bitcode directly.

I have tried using clang 3.3, which is the current version on my mac os x 10.9.2 system, however the following does not work:

$ clang -S -emit-llvm tmp.c -o tmp.ll
$ python `which emcc` tmp.ll      
warning: incorrect target triple 'x86_64-apple-macosx10.9.0' (did you use emcc/em++ on all source files and not clang directly?)

The warning is correct; I am indeed using clang directly, how do I do so regardless, so that I can then attempt to do the same thing in another language that also uses llvm?

Upvotes: 5

Views: 2675

Answers (2)

oreshetnik
oreshetnik

Reputation: 105

According to this issue, Emscripten supports the wasm32-unknown-unknown-elf target, common for both CLang & Emscripten.

So, for compiling code in your language to Emscripten-compatible LLVM-bitcode via plain Clang you can use:

clang -emit-llvm --target=wasm32-unknown-unknown-elf -S test.c

And for compiling resulting bitcode to WASM:

emcc -s WASM=1 test.ll

Tested this approach on Emscripten's test file linpack.c - 1157 lines of code, works as expected.

Upvotes: 4

Alon Zakai
Alon Zakai

Reputation: 1048

You need to use emscripten's LLVM and clang (as it is not upstream yet), so that you can emit code using the emscripten-asmjs target.

If you have another language using llvm, and you can use a build of that llvm with it, then it should work. You just need to tell that language to emit LLVM IR targeting the emscripten-asmjs target.

Upvotes: 0

Related Questions