Thomas Körner
Thomas Körner

Reputation: 86

Bad results with Emscripten compiling C++ to JavaScript & Asm.js

For a course at the university I have to demonstrate asm.js. I found a tutorial (http://www.sitepoint.com/understanding-asm-js/) which was exactly what I was looking for. So I created the given C++-File and compiled it with Emscripten. The result was a nearly 10000-lines long file. Nowhere to find the "use asm"-statement. And by comparison to the handwritten JavaScript-File it is much slower.

I'm using a portable Emscripten-SDK-package and updated it before using it.

How can I get Emscripten to generate good asm-Code?


UPDATE: I found a different solution for my demonstration without Emscipten: https://gist.github.com/dherman/3d0b4733303eaf4bae5e. Maybe someone need this to.

Upvotes: 1

Views: 639

Answers (2)

Scott Stensland
Scott Stensland

Reputation: 28285

10k lines of javascript is quite modest considering it must include the functional equivalent to system libraries (libc, etc...) which live as separate files when you execute c++ compiled source - when browsers execute javascript its sandboxed and cannot access such system libs on the target computer (due to security, OS neutrality ...) for instance just do a ldd command on some dynamically linked c/c++ executable to gain an appreciation of what the bulk of those 10k lines of javascript are replacing:

ldd /bin/ls 


linux-vdso.so.1 =>  (0x00007fff8c865000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f7b82854000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f7b8264b000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7b82285000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f7b82018000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7b81e14000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7b82aba000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f7b81c0e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7b819f0000)

this might give you more tips on using asm.js and emscripten: https://hacks.mozilla.org/2014/11/porting-to-emscripten/

Upvotes: 1

Michal Charemza
Michal Charemza

Reputation: 26982

From my testing Emscripten seems to only use asm.js, and put "use asm"; into the generated Javascript at optimization levels -O1 and above. So when compiling you need to pass -O1 (or a higher level than 1) to the compiler:

eemcc source.cpp -O1 -o target.js`

If you don't specifiy an optimization level, or pass -O0:

eemcc source.cpp -O0 -o target.js`

then "use asm"; doesn't get put into the generated Javascript.

Upvotes: 1

Related Questions