unc_vm_compiler/translator/
module.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md
3
4//! Translation skeleton that traverses the whole WebAssembly module and call helper functions
5//! to deal with each part of it.
6use super::environ::ModuleEnvironment;
7use super::sections::{
8    parse_data_section, parse_element_section, parse_export_section, parse_function_section,
9    parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
10    parse_start_section, parse_table_section, parse_type_section,
11};
12use super::state::ModuleTranslationState;
13use crate::WasmResult;
14use wasmparser::{NameSectionReader, Parser, Payload};
15
16/// Translate a sequence of bytes forming a valid Wasm binary into a
17/// parsed ModuleInfo `ModuleTranslationState`.
18#[tracing::instrument(target = "unc_vm", level = "trace", skip_all)]
19pub fn translate_module<'data>(
20    data: &'data [u8],
21    environ: &mut ModuleEnvironment<'data>,
22) -> WasmResult<ModuleTranslationState> {
23    let mut module_translation_state = ModuleTranslationState::new();
24
25    for payload in Parser::new(0).parse_all(data) {
26        match payload? {
27            Payload::Version { .. } | Payload::End(_) => {}
28
29            Payload::TypeSection(types) => {
30                parse_type_section(types, &mut module_translation_state, environ)?;
31            }
32
33            Payload::ImportSection(imports) => {
34                parse_import_section(imports, environ)?;
35            }
36
37            Payload::FunctionSection(functions) => {
38                parse_function_section(functions, environ)?;
39            }
40
41            Payload::TableSection(tables) => {
42                parse_table_section(tables, environ)?;
43            }
44
45            Payload::MemorySection(memories) => {
46                parse_memory_section(memories, environ)?;
47            }
48
49            Payload::GlobalSection(globals) => {
50                parse_global_section(globals, environ)?;
51            }
52
53            Payload::ExportSection(exports) => {
54                parse_export_section(exports, environ)?;
55            }
56
57            Payload::StartSection { func, .. } => {
58                parse_start_section(func, environ)?;
59            }
60
61            Payload::ElementSection(elements) => {
62                parse_element_section(elements, environ)?;
63            }
64
65            Payload::CodeSectionStart { .. } => {}
66            Payload::CodeSectionEntry(code) => {
67                let mut code = code.get_binary_reader();
68                let size = code.bytes_remaining();
69                let offset = code.original_position();
70                environ.define_function_body(
71                    &module_translation_state,
72                    code.read_bytes(size)?,
73                    offset,
74                )?;
75            }
76
77            Payload::DataSection(data) => {
78                parse_data_section(data, environ)?;
79            }
80
81            Payload::DataCountSection { count, .. } => {
82                environ.reserve_passive_data(count)?;
83            }
84
85            Payload::InstanceSection(_) => {
86                unimplemented!("module linking not implemented yet")
87            }
88
89            Payload::TagSection(_) => {
90                unimplemented!("exception handling proposal is not implemented yet")
91            }
92
93            Payload::CustomSection(reader) => {
94                if reader.name() == "name" {
95                    parse_name_section(
96                        NameSectionReader::new(reader.data(), reader.data_offset()),
97                        environ,
98                    )?;
99                } else {
100                    environ.custom_section(reader.name(), reader.data())?;
101                }
102            }
103
104            Payload::ModuleSection { .. } => unimplemented!("module sections not supported yet"), // which proposal is this coming from?
105            Payload::CoreTypeSection { .. } => {
106                unimplemented!("component proposal is not implemented yet")
107            }
108            Payload::ComponentSection { .. } => {
109                unimplemented!("component proposal is not implemented yet")
110            }
111            Payload::ComponentInstanceSection { .. } => {
112                unimplemented!("component proposal is not implemented yet")
113            }
114            Payload::ComponentAliasSection { .. } => {
115                unimplemented!("component proposal is not implemented yet")
116            }
117            Payload::ComponentTypeSection { .. } => {
118                unimplemented!("component proposal is not implemented yet")
119            }
120            Payload::ComponentCanonicalSection { .. } => {
121                unimplemented!("component proposal is not implemented yet")
122            }
123            Payload::ComponentStartSection { .. } => {
124                unimplemented!("component proposal is not implemented yet")
125            }
126            Payload::ComponentImportSection { .. } => {
127                unimplemented!("component proposal is not implemented yet")
128            }
129            Payload::ComponentExportSection { .. } => {
130                unimplemented!("component proposal is not implemented yet")
131            }
132
133            Payload::UnknownSection { .. } => unreachable!(),
134        }
135    }
136
137    module_translation_state.build_import_map(&environ.module);
138
139    Ok(module_translation_state)
140}