thesuhas / orca

WebAssembly Transformation Library for the Component Model
https://crates.io/crates/orca-wasm
Apache License 2.0
14 stars 2 forks source link

Add `u32_const` macro opcode #133

Closed ejrgilbert closed 2 months ago

ejrgilbert commented 2 months ago

Add in a notion of macro opcodes that emulate some desired behavior, making the API usage more expressive.

The first example of this is a u32_const to help users ensure their u32's are emitted correctly when using the i32.const opcode. This protects users from doing func.i32_const(u32_val as i32), which will emit signed-ness incorrectly due to sign extension rules for i32s vs u32s. We will need to use the cast_signed function for u32 primitives to do this.

The code will look like the following:

pub fn u32_const(&self, val: u32) -> self {
    self.i32_const(val.cast_signed())
}

NOTE: When considering IDs represented as u32 value (global IDs, function IDs, etc.), we don't have to worry about the u32 fitting into an i32 since the number of IDs allowed in a module is capped. It will never surpass I32::MAX anyway since the cap is somewhere in the millions.

ejrgilbert commented 2 months ago

Actually, I found the source for that cast_signed function and it just does a u32 as i32 cast (seen in the macro invocation).

Given the test in the docs they use to demonstrate the functionality, we should be okay to just cast using the as keyword.