Skip to content

Commit

Permalink
WASM GC Support (#218)
Browse files Browse the repository at this point in the history
* updated to wasm-tools 219.1

* added encoding of structs and arrays

* added array and struct types

* got gc types parsing

* added gc encoding

* added gc instructions for injection

* got gc subtyping working

* roundtrip gc working

* one test failing

* added default behaviour for add func type

* added some comments

* added some more documentation
  • Loading branch information
thesuhas authored Nov 7, 2024
1 parent 5e1d02f commit 32f5c7b
Show file tree
Hide file tree
Showing 32 changed files with 4,765 additions and 190 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ include = [
]

[dependencies]
wasm-encoder = { version = "0.217.0", features = ["wasmparser"]}
wasmparser = "0.217.0"
wasm-encoder = { version = "0.219.1", features = ["wasmparser"]}
wasmparser = "0.219.1"
tempfile = "3.10.1"
serde_json = "1.0.121"
log = "0.4.22"
gimli = "0.31.0"

[dev-dependencies]
wasmprinter = "0.217.0"
wat = "1.214.0"
wasmprinter = "0.219.1"
wat = "1.219.1"
26 changes: 20 additions & 6 deletions src/ir/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,11 @@ impl<'a> Component<'a> {
let mut type_section = wasm_encoder::CoreTypeSection::new();
for cty_idx in last_processed_core_ty..last_processed_core_ty + num {
match &self.core_types[cty_idx as usize] {
CoreType::Sub(subtype) => {
let enc = type_section.ty();
encode_core_type_subtype(enc, subtype, &mut reencode);
CoreType::Rec(recgroup) => {
for subtype in recgroup.types() {
let enc = type_section.ty().core();
encode_core_type_subtype(enc, subtype, &mut reencode);
}
}
CoreType::Module(module) => {
let enc = type_section.ty();
Expand Down Expand Up @@ -612,9 +614,15 @@ impl<'a> Component<'a> {
for c in comp.iter() {
match c {
ComponentTypeDeclaration::CoreType(core) => match core {
CoreType::Sub(sub) => {
let enc = new_comp.core_type();
encode_core_type_subtype(enc, sub, &mut reencode);
CoreType::Rec(recgroup) => {
for sub in recgroup.types() {
let enc = new_comp.core_type().core();
encode_core_type_subtype(
enc,
sub,
&mut reencode,
);
}
}
CoreType::Module(module) => {
let enc = new_comp.core_type();
Expand Down Expand Up @@ -804,6 +812,12 @@ impl<'a> Component<'a> {
CanonicalFunction::ResourceRep { resource } => {
canon_sec.resource_rep(*resource);
}
CanonicalFunction::ThreadSpawn { func_ty_index } => {
canon_sec.thread_spawn(*func_ty_index);
}
CanonicalFunction::ThreadHwConcurrency => {
canon_sec.thread_hw_concurrency();
}
}
last_processed_canon += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> FunctionBuilder<'a> {
let imp = module.imports.get(import_id);
if let TypeRef::Func(imp_ty_id) = imp.ty {
if let Some(ty) = module.types.get(TypeID(imp_ty_id)) {
if *ty.params == self.params && *ty.results == self.results {
if *ty.params() == self.params && *ty.results() == self.results {
let mut local_func = LocalFunction::new(
TypeID(imp_ty_id),
FunctionID(*import_id),
Expand Down
15 changes: 11 additions & 4 deletions src/ir/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ pub fn print_subtype(ty: &SubType) {
CompositeInnerType::Array(_) => eprintln!("SubType Array"),
CompositeInnerType::Func(_) => eprintln!("SubType Func"),
CompositeInnerType::Struct(_) => eprintln!("SubType Struct"),
CompositeInnerType::Cont(_) => eprintln!("SubType Cont"),
}
}

pub fn print_module_ty_declaration(ty: &ModuleTypeDeclaration) {
eprint!("Module: ");
match ty {
ModuleTypeDeclaration::Type(subtype) => {
eprint!("SubType: ");
print_subtype(subtype)
ModuleTypeDeclaration::Type(recgroup) => {
for subtype in recgroup.types() {
eprint!("SubType: ");
print_subtype(subtype)
}
}
ModuleTypeDeclaration::Export {
name: _name,
Expand All @@ -55,7 +58,11 @@ pub fn print_module_ty_declaration(ty: &ModuleTypeDeclaration) {
pub fn print_core_type(ty: &CoreType) {
eprint!("CoreType: ");
match ty {
CoreType::Sub(subtype) => print_subtype(subtype),
CoreType::Rec(recgroup) => {
for subtype in recgroup.types() {
print_subtype(subtype);
}
}
CoreType::Module(module) => {
for m in module.iter() {
print_module_ty_declaration(m);
Expand Down
28 changes: 28 additions & 0 deletions src/ir/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,31 @@ impl std::ops::DerefMut for MemoryID {
&mut self.0
}
}

/// Field ID in a Struct or Array
pub struct FieldID(pub u32);
impl std::ops::Deref for FieldID {
type Target = u32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for FieldID {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// ID of an element in the Elements Section
pub struct ElementID(pub u32);
impl std::ops::Deref for ElementID {
type Target = u32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for ElementID {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
Loading

0 comments on commit 32f5c7b

Please sign in to comment.