asm-js / validator

A reference validator for asm.js.
Apache License 2.0
1.78k stars 148 forks source link

booleans #34

Closed chadaustin closed 11 years ago

chadaustin commented 11 years ago

Hi Dave,

I am excited about this project. From Alon Zakai's brief description, I consider asm.js to be a formalization of the Emscripten ABI. And on that note, I'd like to point out a bit of weirdness.

If you've got a C++ function that returns a C++ bool, depending on the generated Emscripten code, it will either return a JavaScript boolean or an integer (probably 0 or 1). In JavaScript, booleans and integers have different types, so presumably JITs will best handle code that only uses one or the other. I would look to asm.js to specify the representation of booleans.

Please forgive me if you've already considered this.

dherman commented 11 years ago

Hi Chad, thanks for the issue! I will talk with Alon about this. Presumably, Emscripten could simply return an integer instead, but alternatively, we could allow functions to return the type bit instead, which would be made manifest with the double-negation pattern !!. I'll see what Alon and Luke think.

kripken commented 11 years ago

Hmm, the LLVM optimizer could anyhow turn an i1 into an i32, and other compilers might do the same since most cpu architectures do not have bool registers. So I think it would be simplest to keep the types in asm.js code only i32 or double, basically asm.js (at least as I see it) can be seen as closer to the metal than C/C++ (which can differentiate bool from i32).

dherman commented 11 years ago

@chadaustin Just to be clear, asm.js isn't about defining the ABI or FFI between regular JS and the output of Emscripten; it's about formalizing the sub-language that Emscripten generates. The interface that asm.js provides between low-level "asm" functions and ordinary JS is extremely spare. If you want to generate more ergonomic, high-level FFI functions, you have to create wrapper functions. So I think we're going to leave things like booleans as simple integers, and if you want to provide a more idiomatic interface you have to put it in a wrapper function.

Dave

chadaustin commented 11 years ago

Great! Specifying that booleans are always integers is what I would expect, actually. I was just surprised when an Emscripten-generated function that returned booleans usually returned integers but sometimes returned true or false. I thought "I wonder if the JIT would have a problem with this."

The implication from this thread is that Emscripten should generate code like function() { return (x>y)|0; } instead of function() { return x > y; }

Thanks!

kripken commented 11 years ago

Oh interesting, that you saw emscripten generate functions that return booleans sometimes, I knew it was possible in theory but didn't see it in practice. Yes, this can interfere with JITs. In asm.js we'll avoid that of course.