Repository navigation
Add const expression utility to binaryen-c/.js - #1288
Conversation
kripken
left a comment
There was a problem hiding this comment.
Nice!
A few minor comments below. Also needs a test.
| int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr); | ||
| // Gets the 64-bit integer value of the specified known-to-be-constant expression. | ||
| int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr); | ||
| // Gets the low 32-bits of a 64-bit integer value of the specified known-to-be-constant expression. |
There was a problem hiding this comment.
i guess that low/high variants are for calling from JS? worth mentioning that i think.
| BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr); | ||
| // Print an expression to stdout. Useful for debugging. | ||
| void BinaryenExpressionPrint(BinaryenExpressionRef expr); | ||
| // Gets the 32-bit integer value of the specified known-to-be-constant expression. |
There was a problem hiding this comment.
"known-to-be-constant" seems to use "constant" as a general property - we should say that the expression passed in must be a captital-C Const node.
|
Still thinking how to implement a |
|
Why not just run the precompute pass on the whole module? Is there a reason to do it on individual expressions? |
|
Yeah, the reason is that the MVP does not allow more complex (for example binary) expressions as initializers of, let's say, globals and I am trying to avoid reimplementing precompute in my compiler. Hence, if I have a const global with an initializer like Being able to precompute just the initializer would solve this, while not affecting the entire module and not having to reinvent the wheel. |
|
I see. One way could be, in JS, to create a module (empty except you would want the global imports), add just that expression in a function, and run the pass on that, then get that function body. We should make doing stuff like that easy anyhow from JS, I think, so this could be a good way to verify that. |
|
Sure, that would be one option. This will, however, not cover things like |
|
I don't see why not, can you elaborate? (Or do you mean you'd need to loop, continuing to work while something is reduced? Yes, that would be necessary.) |
|
Well, because |
Given an i32/i64/f32/f64.const expression reference, these functions return the expression's literal value. One use case is precomputing an expression (i.e. through running the precompute pass) and then checking the precomputed value.