0xPolygonMiden / compiler

Compiler from MidenIR to Miden Assembly

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty `if.true else op end` causing parsing error in the assembler

greenhat opened this issue · comments

Wasm:

  (func $__rust_alloc_zeroed (;3;) (type 2) (param i32 i32) (result i32)
    block ;; label = @1
      i32.const 1048576
      local.get 1
      local.get 0
      call $<wee_alloc::WeeAlloc as core::alloc::global::GlobalAlloc>::alloc
      local.tee 1
      i32.eqz
      br_if 0 (;@1;)
      local.get 1
      i32.const 0
      local.get 0
      memory.fill
    end
    local.get 1
  )

translated into the following IR:

(func (export #__rust_alloc_zeroed) (param i32) (param i32) (result i32)
    (block 0 (param v0 i32) (param v1 i32)
        (let (v3 i32) (const.i32 1048576))
        (let (v4 i32) (call #<wee_alloc__WeeAlloc as core__alloc__global__GlobalAlloc>::alloc v3 v1 v0))
        (let (v5 i1) (eq v4 0))
        (let (v6 i32) (cast v5))
        (let (v7 i1) (neq v6 0))
        (condbr v7 (block 2 v4) (block 3)))

    (block 1 (param v2 i32)
        (ret v2))

    (block 2 (param v9 i32)
        (br (block 1 v9)))

    (block 3
        (let (v8 i32) (const.i32 0))
        (assert.eq 0 v8)
        (br (block 2 v4)))
)

which then compiled into the following MASM:

export."__rust_alloc_zeroed"
    push.1048576
    movup.2
    swap.1
    exec."<wee_alloc__WeeAlloc as core__alloc__global__GlobalAlloc>::alloc"
    dup.0
    eq.0
    neq.0
    if.true

    else
        push.0 eq.0 assert
    end
end

Which fails to parse by the VM assembler with an error:

 25 | 
 26 |     else
    :     ^^|^
    :       `-- found a else here
 27 |         push.0 eq.0 assert
    `----
  help: expected primitive opcode (e.g. "add"), or control flow opcode (e.g.
        "if.true")

After 0xPolygonMiden/miden-vm#1360 is merged, revert the #212 changes and relay on the Assembler to accept empty if blocks.

Thanks @bobbinth!

@greenhat I'll close this and #207 for now, if you want to update #212 to simply update the miden-assembly dependency, rather than implement the workaround mentioned in #207, that will unblock merging the remaining open PRs behind #212