turbolent / w2c2

Translates WebAssembly modules to portable C
MIT License
712 stars 37 forks source link

Converting floats to ints doesn't work #60

Closed Heath123 closed 1 year ago

Heath123 commented 1 year ago

If you compile this AssemblyScript code with optimizations off:

export function returnFive(n: i32): i32 {
  return i32(5.0)
}

you get this:

;; INFO asc module.ts --textFile module.wat --outFile module.wasm --bindings raw --runtime stub
(module
 (type $i32_=>_i32 (func (param i32) (result i32)))
 (memory $0 0)
 (table $0 1 1 funcref)
 (elem $0 (i32.const 1))
 (export "returnFive" (func $module/returnFive))
 (export "memory" (memory $0))
 (func $module/returnFive (param $0 i32) (result i32)
  f64.const 5
  i32.trunc_sat_f64_s
  return
 )
)

and the following C code:

U32 as_f0(moduleInstance *i) {
  U32 si0;
  F64 sd0;
  sd0 = 5;
  goto L0;
L0:;
  return si0;
}

This looks... obviously wrong, it's returning an uninitialised value

turbolent commented 1 year ago

Non-trapping float-to-int conversions are not yet implemented: https://github.com/turbolent/w2c2/blob/f4ee21a5aa12c7d650c49fb9087295477d4d26f7/w2c2/c.c#L2648-L2682

Compiling should have reported an error message.

I opened #62 to implement those instructions.

Heath123 commented 1 year ago

Thank you!

Heath123 commented 1 year ago

I didn't realise this wasn't in the core spec, sorry, I probably could have disabled it from the compiler