assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Compound assignment with post increment in lhs miscompiles

Open abextm opened this issue 1 year ago • 1 comments

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

Editor

Should print 1234, instead crashes

AssemblyScript version

0.27.7

abextm avatar Aug 01 '23 10:08 abextm

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 getter before setting. The fix seems to be to use one additional temporary, which perhaps is the designated $0 already that actually remains unused.

dcodeIO avatar Aug 01 '23 10:08 dcodeIO