Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 863
Arafangion Avatar asked Oct 20 '22 08:10

Arafangion


1 Answers

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.

like image 87
getupandgo Avatar answered Dec 30 '22 17:12

getupandgo