assemblyscript
assemblyscript copied to clipboard
Compound assignment with post increment in lhs miscompiles
Bug description
Compound assignment where the left hand side is an array with a post increment index generates incorrect code.
let a = new Uint32Array(1);
let v = 0;
a[v++] |= 1234;
actually is generating something like
let a = new Uint32Array(1);
let v = 0;
let v$0 = v;
v += 1;
a[v$0] = a[v] | 1234;
Steps to reproduce
Should print 1234
, instead crashes
AssemblyScript version
0.27.7
Appears that this generates something like
(func $start:module
(local $0 i32)
(local $1 i32)
(local $2 i32)
global.get $module/arr
global.get $module/val ;; = 0
local.tee $2 ;; $2 = $val (= 0)
i32.const 1
i32.add
global.set $module/val ;; $val = $2 + 1 (= 1)
local.get $2 ;; (-> 0)
global.get $module/arr
global.get $module/val
local.tee $1 ;; $1 = $val (= 1)
i32.const 1
i32.add
global.set $module/val ;; $val = $1 + 1 (= 2)
local.get $1 ;; (-> 1)
call $~lib/staticarray/StaticArray<u32>#__get;;($arr, 1) -> $temp
i32.const 1234
i32.or
call $~lib/staticarray/StaticArray<u32>#__set;;($arr, 2, $temp | 1234)
)
)
where the increment is erroneously performed two times, likely due to compiling the additional get
ter before set
ting. The fix seems to be to use one additional temporary, which perhaps is the designated $0
already that actually remains unused.