Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compound assignment with post increment in lhs miscompiles #2730

Open
abextm opened this issue Aug 1, 2023 · 1 comment
Open

Compound assignment with post increment in lhs miscompiles #2730

abextm opened this issue Aug 1, 2023 · 1 comment
Labels

Comments

@abextm
Copy link

abextm commented Aug 1, 2023

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 abextm added the bug label Aug 1, 2023
@dcodeIO
Copy link
Member

dcodeIO commented Aug 1, 2023

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.

Changqing-JING added a commit to Changqing-JING/assemblyscript that referenced this issue Oct 17, 2023
Changqing-JING added a commit to Changqing-JING/assemblyscript that referenced this issue Oct 17, 2023
Changqing-JING added a commit to Changqing-JING/assemblyscript that referenced this issue Oct 17, 2023
Changqing-JING added a commit to Changqing-JING/assemblyscript that referenced this issue Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants