2080
2080

Reputation: 1417

WebAssembly: Is it possible to use 2x i32.store instead of one i64.store?

If there is an i64 on the stack, is it possible to use two i32.store to subsequent memory indices to write the i64 to memory? Or is this not a valid module anymore?

==i32==|==i32==

======i64======

Upvotes: 0

Views: 594

Answers (2)

Andreas Rossberg
Andreas Rossberg

Reputation: 36118

No, you cannot assume that stack operands lie in contiguous memory -- or in memory at all, since in reality they will typically live in separate CPU registers. So implicitly "merging" two i32 operands from the operand stack as if they were a single i64 is not possible. You need to store them separately. Similarly, the other way round.

Upvotes: 1

ColinE
ColinE

Reputation: 70162

Two subsequent i32.store operations are valid, as long as you have two values on the stack. But the net result will not be the same as an i64.store. You will instead need to perform a shift operation to store the lower then the upper bytes.

;; the value to store 
i64.const 0x11223344556677
;; storage offset
i32.const 0
;; store
i32.store

;; load the value again 
i64.const 0x11223344556677
;; the number of bits to shift by
i32.const 32
;; shift
i32.shr_u
;; storage offset
i32.const 4
;; store
i32.store

You’d probably use a local to avoid loading the same number twice.

Upvotes: 1

Related Questions