Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cranelift/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["webassembly", "wasm"]
edition = "2018"

[dependencies]
wasmparser = { version = "0.58.0", default-features = false }
wasmparser = { version = "0.59.0", default-features = false }
cranelift-codegen = { path = "../codegen", version = "0.65.0", default-features = false }
cranelift-entity = { path = "../entity", version = "0.65.0" }
cranelift-frontend = { path = "../frontend", version = "0.65.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion cranelift/wasm/src/code_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
Operator::RefNull { ty } => {
state.push1(environ.translate_ref_null(builder.cursor(), (*ty).try_into()?)?)
}
Operator::RefIsNull { ty: _ } => {
Operator::RefIsNull => {
let value = state.pop1();
state.push1(environ.translate_ref_is_null(builder.cursor(), value)?);
}
Expand Down
89 changes: 47 additions & 42 deletions cranelift/wasm/src/module_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
//! to deal with each part of it.
use crate::environ::{ModuleEnvironment, WasmResult};
use crate::sections_translator::{
parse_code_section, parse_data_section, parse_element_section, parse_export_section,
parse_function_section, parse_global_section, parse_import_section, parse_memory_section,
parse_name_section, parse_start_section, parse_table_section, parse_type_section,
parse_data_section, parse_element_section, parse_export_section, parse_function_section,
parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
parse_start_section, parse_table_section, parse_type_section,
};
use crate::state::ModuleTranslationState;
use cranelift_codegen::timing;
use wasmparser::{CustomSectionContent, ModuleReader, SectionContent};
use wasmparser::{NameSectionReader, Parser, Payload};

/// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cranelift IR
/// [`Function`](cranelift_codegen::ir::Function).
Expand All @@ -17,80 +17,85 @@ pub fn translate_module<'data>(
environ: &mut dyn ModuleEnvironment<'data>,
) -> WasmResult<ModuleTranslationState> {
let _tt = timing::wasm_translate_module();
let mut reader = ModuleReader::new(data)?;
let mut module_translation_state = ModuleTranslationState::new();

while !reader.eof() {
let section = reader.read()?;
match section.content()? {
SectionContent::Type(types) => {
for payload in Parser::new(0).parse_all(data) {
match payload? {
Payload::Version { .. } | Payload::End => {}

Payload::TypeSection(types) => {
parse_type_section(types, &mut module_translation_state, environ)?;
}

SectionContent::Import(imports) => {
Payload::ImportSection(imports) => {
parse_import_section(imports, environ)?;
}

SectionContent::Function(functions) => {
Payload::FunctionSection(functions) => {
parse_function_section(functions, environ)?;
}

SectionContent::Table(tables) => {
Payload::TableSection(tables) => {
parse_table_section(tables, environ)?;
}

SectionContent::Memory(memories) => {
Payload::MemorySection(memories) => {
parse_memory_section(memories, environ)?;
}

SectionContent::Global(globals) => {
Payload::GlobalSection(globals) => {
parse_global_section(globals, environ)?;
}

SectionContent::Export(exports) => {
Payload::ExportSection(exports) => {
parse_export_section(exports, environ)?;
}

SectionContent::Start(start) => {
parse_start_section(start, environ)?;
Payload::StartSection { func, .. } => {
parse_start_section(func, environ)?;
}

SectionContent::Element(elements) => {
Payload::ElementSection(elements) => {
parse_element_section(elements, environ)?;
}

SectionContent::Code(code) => {
parse_code_section(code, &module_translation_state, environ)?;
Payload::CodeSectionStart { .. } => {}
Payload::CodeSectionEntry(code) => {
let mut code = code.get_binary_reader();
let size = code.bytes_remaining();
let offset = code.original_position();
environ.define_function_body(
&module_translation_state,
code.read_bytes(size)?,
offset,
)?;
}

SectionContent::Data(data) => {
Payload::DataSection(data) => {
parse_data_section(data, environ)?;
}

SectionContent::DataCount(count) => {
Payload::DataCountSection { count, .. } => {
environ.reserve_passive_data(count)?;
}

SectionContent::Module(_)
| SectionContent::ModuleCode(_)
| SectionContent::Instance(_)
| SectionContent::Alias(_) => unimplemented!("module linking not implemented yet"),

SectionContent::Custom {
name,
binary,
content,
} => match content {
Some(CustomSectionContent::Name(names)) => {
parse_name_section(names, environ)?;
}
_ => {
let mut reader = binary.clone();
let len = reader.bytes_remaining();
let payload = reader.read_bytes(len)?;
environ.custom_section(name, payload)?;
}
},
Payload::ModuleSection(_)
| Payload::InstanceSection(_)
| Payload::AliasSection(_)
| Payload::ModuleCodeSectionStart { .. }
| Payload::ModuleCodeSectionEntry { .. } => {
unimplemented!("module linking not implemented yet")
}

Payload::CustomSection {
name: "name",
data,
data_offset,
} => parse_name_section(NameSectionReader::new(data, data_offset)?, environ)?,

Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,

Payload::UnknownSection { .. } => unreachable!(),
}
}

Expand Down
25 changes: 5 additions & 20 deletions cranelift/wasm/src/sections_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use cranelift_entity::EntityRef;
use std::boxed::Box;
use std::vec::Vec;
use wasmparser::{
self, CodeSectionReader, Data, DataKind, DataSectionReader, Element, ElementItem, ElementItems,
ElementKind, ElementSectionReader, Export, ExportSectionReader, ExternalKind,
FunctionSectionReader, GlobalSectionReader, GlobalType, ImportSectionEntryType,
ImportSectionReader, MemorySectionReader, MemoryType, NameSectionReader, Naming, NamingReader,
Operator, TableSectionReader, Type, TypeDef, TypeSectionReader,
self, Data, DataKind, DataSectionReader, Element, ElementItem, ElementItems, ElementKind,
ElementSectionReader, Export, ExportSectionReader, ExternalKind, FunctionSectionReader,
GlobalSectionReader, GlobalType, ImportSectionEntryType, ImportSectionReader,
MemorySectionReader, MemoryType, NameSectionReader, Naming, NamingReader, Operator,
TableSectionReader, Type, TypeDef, TypeSectionReader,
};

/// Parses the Type section of the wasm module.
Expand Down Expand Up @@ -358,21 +358,6 @@ pub fn parse_element_section<'data>(
Ok(())
}

/// Parses the Code section of the wasm module.
pub fn parse_code_section<'data>(
code: CodeSectionReader<'data>,
module_translation_state: &ModuleTranslationState,
environ: &mut dyn ModuleEnvironment<'data>,
) -> WasmResult<()> {
for body in code {
let mut reader = body?.get_binary_reader();
let size = reader.bytes_remaining();
let offset = reader.original_position();
environ.define_function_body(module_translation_state, reader.read_bytes(size)?, offset)?;
}
Ok(())
}

/// Parses the Data section of the wasm module.
pub fn parse_data_section<'data>(
data: DataSectionReader<'data>,
Expand Down
2 changes: 1 addition & 1 deletion crates/debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"

[dependencies]
gimli = "0.21.0"
wasmparser = "0.58.0"
wasmparser = "0.59.0"
object = { version = "0.20", default-features = false, features = ["read", "write"] }
wasmtime-environ = { path = "../environ", version = "0.18.0" }
target-lexicon = { version = "0.10.0", default-features = false }
Expand Down
Loading