Skip to content

Commit 0cd2755

Browse files
authored
Switch GC binary enconding (bytecodealliance#1204)
* Update GC types encoding * Update GC unprefixed instructions * Update GC prefixed instructions
1 parent 4975360 commit 0cd2755

7 files changed

Lines changed: 89 additions & 89 deletions

File tree

crates/wasm-encoder/src/core/code.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ impl Encode for Instruction<'_> {
917917
l.encode(sink);
918918
}
919919
Instruction::BrOnNull(l) => {
920-
sink.push(0xD4);
920+
sink.push(0xD5);
921921
l.encode(sink);
922922
}
923923
Instruction::BrOnNonNull(l) => {
@@ -1318,20 +1318,20 @@ impl Encode for Instruction<'_> {
13181318
sink.push(0xd2);
13191319
f.encode(sink);
13201320
}
1321-
Instruction::RefAsNonNull => sink.push(0xD3),
1321+
Instruction::RefAsNonNull => sink.push(0xd4),
13221322

13231323
// GC instructions.
13241324
Instruction::RefI31 => {
13251325
sink.push(0xfb);
1326-
sink.push(0x20)
1326+
sink.push(0x1c)
13271327
}
13281328
Instruction::I31GetS => {
13291329
sink.push(0xfb);
1330-
sink.push(0x21)
1330+
sink.push(0x1d)
13311331
}
13321332
Instruction::I31GetU => {
13331333
sink.push(0xfb);
1334-
sink.push(0x22)
1334+
sink.push(0x1e)
13351335
}
13361336

13371337
// Bulk memory instructions.

crates/wasm-encoder/src/core/types.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ impl ValType {
124124
impl Encode for StorageType {
125125
fn encode(&self, sink: &mut Vec<u8>) {
126126
match self {
127-
StorageType::I8 => sink.push(0x7A),
128-
StorageType::I16 => sink.push(0x79),
127+
StorageType::I8 => sink.push(0x78),
128+
StorageType::I16 => sink.push(0x77),
129129
StorageType::Val(vt) => vt.encode(sink),
130130
}
131131
}
@@ -183,9 +183,9 @@ impl Encode for RefType {
183183
}
184184

185185
if self.nullable {
186-
sink.push(0x6C);
186+
sink.push(0x63);
187187
} else {
188-
sink.push(0x6B);
188+
sink.push(0x64);
189189
}
190190
self.heap_type.encode(sink);
191191
}
@@ -231,13 +231,13 @@ impl Encode for HeapType {
231231
HeapType::Func => sink.push(0x70),
232232
HeapType::Extern => sink.push(0x6F),
233233
HeapType::Any => sink.push(0x6E),
234-
HeapType::None => sink.push(0x65),
235-
HeapType::NoExtern => sink.push(0x69),
236-
HeapType::NoFunc => sink.push(0x68),
234+
HeapType::None => sink.push(0x71),
235+
HeapType::NoExtern => sink.push(0x72),
236+
HeapType::NoFunc => sink.push(0x73),
237237
HeapType::Eq => sink.push(0x6D),
238-
HeapType::Struct => sink.push(0x67),
239-
HeapType::Array => sink.push(0x66),
240-
HeapType::I31 => sink.push(0x6A),
238+
HeapType::Struct => sink.push(0x6B),
239+
HeapType::Array => sink.push(0x6A),
240+
HeapType::I31 => sink.push(0x6C),
241241
// Note that this is encoded as a signed type rather than unsigned
242242
// as it's decoded as an s33
243243
HeapType::Indexed(i) => i64::from(*i).encode(sink),

crates/wasmparser/src/binary_reader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,8 @@ impl<'a> BinaryReader<'a> {
978978
0xd0 => visitor.visit_ref_null(self.read()?),
979979
0xd1 => visitor.visit_ref_is_null(),
980980
0xd2 => visitor.visit_ref_func(self.read_var_u32()?),
981-
0xd3 => visitor.visit_ref_as_non_null(),
982-
0xd4 => visitor.visit_br_on_null(self.read_var_u32()?),
981+
0xd4 => visitor.visit_ref_as_non_null(),
982+
0xd5 => visitor.visit_br_on_null(self.read_var_u32()?),
983983
0xd6 => visitor.visit_br_on_non_null(self.read_var_u32()?),
984984

985985
0xfb => self.visit_0xfb_operator(pos, visitor)?,
@@ -1001,9 +1001,9 @@ impl<'a> BinaryReader<'a> {
10011001
{
10021002
let code = self.read_var_u32()?;
10031003
Ok(match code {
1004-
0x20 => visitor.visit_ref_i31(),
1005-
0x21 => visitor.visit_i31_get_s(),
1006-
0x22 => visitor.visit_i31_get_u(),
1004+
0x1c => visitor.visit_ref_i31(),
1005+
0x1d => visitor.visit_i31_get_s(),
1006+
0x1e => visitor.visit_i31_get_u(),
10071007

10081008
_ => bail!(pos, "unknown 0xfb subopcode: 0x{code:x}"),
10091009
})

crates/wasmparser/src/readers/core/types.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ impl ValType {
9999

100100
pub(crate) fn is_valtype_byte(byte: u8) -> bool {
101101
match byte {
102-
0x7F | 0x7E | 0x7D | 0x7C | 0x7B | 0x70 | 0x6F | 0x6B | 0x6C | 0x6E | 0x65 | 0x69
103-
| 0x68 | 0x6D | 0x67 | 0x66 | 0x6A => true,
102+
0x7F | 0x7E | 0x7D | 0x7C | 0x7B | 0x70 | 0x6F | 0x64 | 0x63 | 0x6E | 0x71 | 0x72
103+
| 0x73 | 0x6D | 0x6B | 0x6A | 0x6C => true,
104104
_ => false,
105105
}
106106
}
@@ -132,11 +132,11 @@ impl Inherits for ValType {
132132
impl<'a> FromReader<'a> for StorageType {
133133
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
134134
match reader.peek()? {
135-
0x7A => {
135+
0x78 => {
136136
reader.position += 1;
137137
Ok(StorageType::I8)
138138
}
139-
0x79 => {
139+
0x77 => {
140140
reader.position += 1;
141141
Ok(StorageType::I16)
142142
}
@@ -168,7 +168,7 @@ impl<'a> FromReader<'a> for ValType {
168168
reader.position += 1;
169169
Ok(ValType::V128)
170170
}
171-
0x70 | 0x6F | 0x6B | 0x6C | 0x6E | 0x65 | 0x69 | 0x68 | 0x6D | 0x67 | 0x66 | 0x6A => {
171+
0x70 | 0x6F | 0x64 | 0x63 | 0x6E | 0x71 | 0x72 | 0x73 | 0x6D | 0x6B | 0x6A | 0x6C => {
172172
Ok(ValType::Ref(reader.read()?))
173173
}
174174
_ => bail!(reader.original_position(), "invalid value type"),
@@ -578,15 +578,15 @@ impl<'a> FromReader<'a> for RefType {
578578
0x70 => Ok(RefType::FUNC.nullable()),
579579
0x6F => Ok(RefType::EXTERN.nullable()),
580580
0x6E => Ok(RefType::ANY.nullable()),
581-
0x65 => Ok(RefType::NONE.nullable()),
582-
0x69 => Ok(RefType::NOEXTERN.nullable()),
583-
0x68 => Ok(RefType::NOFUNC.nullable()),
581+
0x71 => Ok(RefType::NONE.nullable()),
582+
0x72 => Ok(RefType::NOEXTERN.nullable()),
583+
0x73 => Ok(RefType::NOFUNC.nullable()),
584584
0x6D => Ok(RefType::EQ.nullable()),
585-
0x67 => Ok(RefType::STRUCT.nullable()),
586-
0x66 => Ok(RefType::ARRAY.nullable()),
587-
0x6A => Ok(RefType::I31.nullable()),
588-
byte @ (0x6B | 0x6C) => {
589-
let nullable = byte == 0x6C;
585+
0x6B => Ok(RefType::STRUCT.nullable()),
586+
0x6A => Ok(RefType::ARRAY.nullable()),
587+
0x6C => Ok(RefType::I31.nullable()),
588+
byte @ (0x63 | 0x64) => {
589+
let nullable = byte == 0x63;
590590
let pos = reader.original_position();
591591
RefType::new(nullable, reader.read()?)
592592
.ok_or_else(|| crate::BinaryReaderError::new("type index too large", pos))
@@ -754,31 +754,31 @@ impl<'a> FromReader<'a> for HeapType {
754754
reader.position += 1;
755755
Ok(HeapType::Any)
756756
}
757-
0x65 => {
757+
0x71 => {
758758
reader.position += 1;
759759
Ok(HeapType::None)
760760
}
761-
0x69 => {
761+
0x72 => {
762762
reader.position += 1;
763763
Ok(HeapType::NoExtern)
764764
}
765-
0x68 => {
765+
0x73 => {
766766
reader.position += 1;
767767
Ok(HeapType::NoFunc)
768768
}
769769
0x6D => {
770770
reader.position += 1;
771771
Ok(HeapType::Eq)
772772
}
773-
0x67 => {
773+
0x6B => {
774774
reader.position += 1;
775775
Ok(HeapType::Struct)
776776
}
777-
0x66 => {
777+
0x6A => {
778778
reader.position += 1;
779779
Ok(HeapType::Array)
780780
}
781-
0x6A => {
781+
0x6C => {
782782
reader.position += 1;
783783
Ok(HeapType::I31)
784784
}

crates/wast/src/core/binary.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ impl<'a> Encode for HeapType<'a> {
248248
HeapType::Extern => e.push(0x6f),
249249
HeapType::Any => e.push(0x6e),
250250
HeapType::Eq => e.push(0x6d),
251-
HeapType::Struct => e.push(0x67),
252-
HeapType::Array => e.push(0x66),
253-
HeapType::I31 => e.push(0x6a),
254-
HeapType::NoFunc => e.push(0x68),
255-
HeapType::NoExtern => e.push(0x69),
256-
HeapType::None => e.push(0x65),
251+
HeapType::Struct => e.push(0x6b),
252+
HeapType::Array => e.push(0x6a),
253+
HeapType::I31 => e.push(0x6c),
254+
HeapType::NoFunc => e.push(0x73),
255+
HeapType::NoExtern => e.push(0x72),
256+
HeapType::None => e.push(0x71),
257257
// Note that this is encoded as a signed leb128 so be sure to cast
258258
// to an i64 first
259259
HeapType::Index(Index::Num(n, _)) => i64::from(*n).encode(e),
@@ -286,42 +286,42 @@ impl<'a> Encode for RefType<'a> {
286286
RefType {
287287
nullable: true,
288288
heap: HeapType::Struct,
289-
} => e.push(0x67),
289+
} => e.push(0x6b),
290290
// The 'i31ref' binary abbreviation
291291
RefType {
292292
nullable: true,
293293
heap: HeapType::I31,
294-
} => e.push(0x6a),
294+
} => e.push(0x6c),
295295
// The 'nullfuncref' binary abbreviation
296296
RefType {
297297
nullable: true,
298298
heap: HeapType::NoFunc,
299-
} => e.push(0x68),
299+
} => e.push(0x73),
300300
// The 'nullexternref' binary abbreviation
301301
RefType {
302302
nullable: true,
303303
heap: HeapType::NoExtern,
304-
} => e.push(0x69),
304+
} => e.push(0x72),
305305
// The 'nullref' binary abbreviation
306306
RefType {
307307
nullable: true,
308308
heap: HeapType::None,
309-
} => e.push(0x65),
309+
} => e.push(0x71),
310310

311311
// Generic 'ref null <heaptype>' encoding
312312
RefType {
313313
nullable: true,
314314
heap,
315315
} => {
316-
e.push(0x6c);
316+
e.push(0x63);
317317
heap.encode(e);
318318
}
319319
// Generic 'ref <heaptype>' encoding
320320
RefType {
321321
nullable: false,
322322
heap,
323323
} => {
324-
e.push(0x6b);
324+
e.push(0x64);
325325
heap.encode(e);
326326
}
327327
}
@@ -331,8 +331,8 @@ impl<'a> Encode for RefType<'a> {
331331
impl<'a> Encode for StorageType<'a> {
332332
fn encode(&self, e: &mut Vec<u8>) {
333333
match self {
334-
StorageType::I8 => e.push(0x7a),
335-
StorageType::I16 => e.push(0x79),
334+
StorageType::I8 => e.push(0x78),
335+
StorageType::I16 => e.push(0x77),
336336
StorageType::Val(ty) => {
337337
ty.encode(e);
338338
}
@@ -1181,9 +1181,9 @@ impl Encode for RefTest<'_> {
11811181
fn encode(&self, e: &mut Vec<u8>) {
11821182
e.push(0xfb);
11831183
if self.r#type.nullable {
1184-
e.push(0x48);
1184+
e.push(0x15);
11851185
} else {
1186-
e.push(0x40);
1186+
e.push(0x14);
11871187
}
11881188
self.r#type.heap.encode(e);
11891189
}
@@ -1193,9 +1193,9 @@ impl Encode for RefCast<'_> {
11931193
fn encode(&self, e: &mut Vec<u8>) {
11941194
e.push(0xfb);
11951195
if self.r#type.nullable {
1196-
e.push(0x49);
1196+
e.push(0x17);
11971197
} else {
1198-
e.push(0x41);
1198+
e.push(0x16);
11991199
}
12001200
self.r#type.heap.encode(e);
12011201
}
@@ -1215,7 +1215,7 @@ fn br_on_cast_flags(from_nullable: bool, to_nullable: bool) -> u8 {
12151215
impl Encode for BrOnCast<'_> {
12161216
fn encode(&self, e: &mut Vec<u8>) {
12171217
e.push(0xfb);
1218-
e.push(0x4e);
1218+
e.push(0x18);
12191219
e.push(br_on_cast_flags(
12201220
self.from_type.nullable,
12211221
self.to_type.nullable,
@@ -1229,7 +1229,7 @@ impl Encode for BrOnCast<'_> {
12291229
impl Encode for BrOnCastFail<'_> {
12301230
fn encode(&self, e: &mut Vec<u8>) {
12311231
e.push(0xfb);
1232-
e.push(0x4f);
1232+
e.push(0x19);
12331233
e.push(br_on_cast_flags(
12341234
self.from_type.nullable,
12351235
self.to_type.nullable,

crates/wast/src/core/expr.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -582,41 +582,41 @@ instructions! {
582582
RefFunc(Index<'a>) : [0xd2] : "ref.func",
583583

584584
// function-references proposal
585-
RefAsNonNull : [0xd3] : "ref.as_non_null",
586-
BrOnNull(Index<'a>) : [0xd4] : "br_on_null",
585+
RefAsNonNull : [0xd4] : "ref.as_non_null",
586+
BrOnNull(Index<'a>) : [0xd5] : "br_on_null",
587587
BrOnNonNull(Index<'a>) : [0xd6] : "br_on_non_null",
588588

589589
// gc proposal: eqref
590-
RefEq : [0xd5] : "ref.eq",
590+
RefEq : [0xd3] : "ref.eq",
591591

592592
// gc proposal: struct
593-
StructNew(Index<'a>) : [0xfb, 0x07] : "struct.new",
594-
StructNewDefault(Index<'a>) : [0xfb, 0x08] : "struct.new_default",
595-
StructGet(StructAccess<'a>) : [0xfb, 0x03] : "struct.get",
596-
StructGetS(StructAccess<'a>) : [0xfb, 0x04] : "struct.get_s",
597-
StructGetU(StructAccess<'a>) : [0xfb, 0x05] : "struct.get_u",
598-
StructSet(StructAccess<'a>) : [0xfb, 0x06] : "struct.set",
593+
StructNew(Index<'a>) : [0xfb, 0x00] : "struct.new",
594+
StructNewDefault(Index<'a>) : [0xfb, 0x01] : "struct.new_default",
595+
StructGet(StructAccess<'a>) : [0xfb, 0x02] : "struct.get",
596+
StructGetS(StructAccess<'a>) : [0xfb, 0x03] : "struct.get_s",
597+
StructGetU(StructAccess<'a>) : [0xfb, 0x04] : "struct.get_u",
598+
StructSet(StructAccess<'a>) : [0xfb, 0x05] : "struct.set",
599599

600600
// gc proposal: array
601-
ArrayNew(Index<'a>) : [0xfb, 0x1b] : "array.new",
602-
ArrayNewDefault(Index<'a>) : [0xfb, 0x1c] : "array.new_default",
603-
ArrayNewFixed(ArrayNewFixed<'a>) : [0xfb, 0x1a] : "array.new_fixed",
604-
ArrayNewData(ArrayNewData<'a>) : [0xfb, 0x1d] : "array.new_data",
605-
ArrayNewElem(ArrayNewElem<'a>) : [0xfb, 0x1f] : "array.new_elem",
606-
ArrayGet(Index<'a>) : [0xfb, 0x13] : "array.get",
607-
ArrayGetS(Index<'a>) : [0xfb, 0x14] : "array.get_s",
608-
ArrayGetU(Index<'a>) : [0xfb, 0x15] : "array.get_u",
609-
ArraySet(Index<'a>) : [0xfb, 0x16] : "array.set",
610-
ArrayLen : [0xfb, 0x19] : "array.len",
611-
ArrayFill(ArrayFill<'a>) : [0xfb, 0x0f] : "array.fill",
612-
ArrayCopy(ArrayCopy<'a>) : [0xfb, 0x18] : "array.copy",
613-
ArrayInitData(ArrayInit<'a>) : [0xfb, 0x54] : "array.init_data",
614-
ArrayInitElem(ArrayInit<'a>) : [0xfb, 0x55] : "array.init_elem",
601+
ArrayNew(Index<'a>) : [0xfb, 0x06] : "array.new",
602+
ArrayNewDefault(Index<'a>) : [0xfb, 0x07] : "array.new_default",
603+
ArrayNewFixed(ArrayNewFixed<'a>) : [0xfb, 0x08] : "array.new_fixed",
604+
ArrayNewData(ArrayNewData<'a>) : [0xfb, 0x09] : "array.new_data",
605+
ArrayNewElem(ArrayNewElem<'a>) : [0xfb, 0x0a] : "array.new_elem",
606+
ArrayGet(Index<'a>) : [0xfb, 0x0b] : "array.get",
607+
ArrayGetS(Index<'a>) : [0xfb, 0x0c] : "array.get_s",
608+
ArrayGetU(Index<'a>) : [0xfb, 0x0d] : "array.get_u",
609+
ArraySet(Index<'a>) : [0xfb, 0x0e] : "array.set",
610+
ArrayLen : [0xfb, 0x0f] : "array.len",
611+
ArrayFill(ArrayFill<'a>) : [0xfb, 0x10] : "array.fill",
612+
ArrayCopy(ArrayCopy<'a>) : [0xfb, 0x11] : "array.copy",
613+
ArrayInitData(ArrayInit<'a>) : [0xfb, 0x12] : "array.init_data",
614+
ArrayInitElem(ArrayInit<'a>) : [0xfb, 0x13] : "array.init_elem",
615615

616616
// gc proposal, i31
617-
RefI31 : [0xfb, 0x20] : "ref.i31",
618-
I31GetS : [0xfb, 0x21] : "i31.get_s",
619-
I31GetU : [0xfb, 0x22] : "i31.get_u",
617+
RefI31 : [0xfb, 0x1c] : "ref.i31",
618+
I31GetS : [0xfb, 0x1d] : "i31.get_s",
619+
I31GetU : [0xfb, 0x1e] : "i31.get_u",
620620

621621
// gc proposal, concrete casting
622622
RefTest(RefTest<'a>) : [] : "ref.test",
@@ -625,8 +625,8 @@ instructions! {
625625
BrOnCastFail(Box<BrOnCastFail<'a>>) : [] : "br_on_cast_fail",
626626

627627
// gc proposal extern/any coercion operations
628-
ExternInternalize : [0xfb, 0x70] : "extern.internalize",
629-
ExternExternalize : [0xfb, 0x71] : "extern.externalize",
628+
ExternInternalize : [0xfb, 0x1a] : "extern.internalize",
629+
ExternExternalize : [0xfb, 0x1b] : "extern.externalize",
630630

631631
I32Const(i32) : [0x41] : "i32.const",
632632
I64Const(i64) : [0x42] : "i64.const",

0 commit comments

Comments
 (0)