Skip to content

Binary encoding + decoding + text printing support for i32.atomic.load ordering immediate - #7

Open
stevenfontanella wants to merge 5 commits into
mainfrom
binary
Open

Binary encoding + decoding + text printing support for i32.atomic.load ordering immediate#7
stevenfontanella wants to merge 5 commits into
mainfrom
binary

Conversation

@stevenfontanella

@stevenfontanella stevenfontanella commented Jul 31, 2026

Copy link
Copy Markdown
Member
  • Binary encoding + decoding for atomic ordering immediate for i32.atomic.load
    • The decoding is tested by the spec test. Encoding was checked manually by encoding the same spec test, as well as side-by-side with Binaryen's implementation, and by round-tripping the spec test (see below).
  • Text printing for ordering immediate for i32.atomic.load
    • Tested by round-tripping the spec test (see below).

Binary format output:

xxd out.wasm
00000000: 0061 736d 0100 0000 0184 8080 8000 0160  .asm...........`
00000010: 0000 0382 8080 8000 0100 0584 8080 8000  ................
00000020: 0101 0101 0a9e 8080 8000 0198 8080 8000  ................
00000030: 0041 00fe 1002 001a 4100 fe10 1201 001a  .A......A.......
00000040: 4100 fe10 0200 1a0b                      A.......

Explainer for the binary format output:

    • 0xfe10 - i32.atomic.load
    • 0x02 - alignment of 2 (2^2 = 4 bytes)
    • 0x00 - offset of 0
    • 0xf10 - i32.atomic.load
    • 12 - alignment of 2 | bit 4 set indicating that an ordering immediate follows
    • 01 - acqrel ordering
    • 0x00 - offset of 0

The third case is encoded the same as the first, since SeqCst ordering is the default and we choose not to emit it in the binary format.

Round-tripped WAST shows that the text + binary are round-tripped correctly (seqcst immediate is dropped in both cases since it's the default):

./interpreter/wasm test/core/relaxed-atomics/relaxed-atomics.wast -o out.wast
(module
  (type $0 (func))
  (memory $0 1 1)
  (func $0
    (type 0)
    (i32.const 0)
    (i32.atomic.load)
    (drop)
    (i32.const 0)
    (i32.atomic.load acqrel)
    (drop)
    (i32.const 0)
    (i32.atomic.load)
    (drop)
  )
)
(assert_malformed
  (module quote
    "(memory 1 1 shared) (func $i32load (drop (i32.load acqrel (i32.const 51))))"
  )
  "unexpected token"
)
(module
  (type $0 (func))
  (memory $0 1 1)
  (func $0
    (type 0)
    (i32.const 0)
    (i32.atomic.load)
    (drop)
    (i32.const 0)
    (i32.atomic.load acqrel)
    (drop)
    (i32.const 0)
    (i32.atomic.load)
    (drop)
  )
)

@stevenfontanella
stevenfontanella marked this pull request as ready for review July 31, 2026 17:51
Comment thread interpreter/binary/encode.ml Outdated
Comment thread interpreter/binary/encode.ml Outdated
Comment thread interpreter/binary/encode.ml
Comment thread interpreter/binary/encode.ml
@tlively

tlively commented Aug 11, 2026

Copy link
Copy Markdown
Member

@conrad-watt, PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants