I am writing a tiny compiler that outputs Wasm directly. In my source language all values are f64s and they can be written into (and read out of) a fixed size buffer by index.
I am attempting to use a (the?) Wasm memory to store that buffer.
When storing consecutive f64 values in memory, should I be deriving the address at runtime by multiplying the index by the byte size of an f64, or can I somehow achieve this using align/offset?
In my initial approach (which is stupid in retrospect) assumed I could just use consecutive indices and somehow the fact that I was using f64.store and f64.load it would "just work".
It looked something like this .wat file:
(module
      (memory 1)
      (func (result f64)
          ;; Write 0 to index 99
          i32.const 99
          f64.const 0
          f64.store
          ;; Write 1 to index 100
          i32.const 100
          f64.const 1
          f64.store
          ;; Read from 99
          i32.const 99
          f64.load
      )
      (export "run" (func 0))
  )
I expected this to output 0 but it output -3.105036184601418e+231 which I assume is because some of the bytes I'm reading when I start reading at index 99 were just written when I wrote to index 100. 
What would a "correct" version of the above .wat file -- one that that stores two consecutive f64 values into memory and then reads one -- look like?
The official f64.load/f64.store spec can be found here.
When storing consecutive f64 values in memory, should I be deriving the address at runtime by multiplying the index by the byte size of an f64, or can I somehow achieve this using align/offset?
No, you can't obtain this with align/offset.
You have to multiply all indexes by 8 (since f64 is 64 bit / 8 bit = 8).
You can do that directly in every memory access or another way would be to define two utility functions:
These function will then do the load/store at index*8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With