Use the WasmRuntime's type list rather than keeping a separate list.

This commit is contained in:
Dan Gohman
2017-10-06 16:16:39 -07:00
parent 90ed698e83
commit 3841552b7c
4 changed files with 14 additions and 11 deletions

View File

@@ -5,7 +5,7 @@ use sections_translator::{SectionParsingError, parse_function_signatures, parse_
parse_function_section, parse_export_section, parse_memory_section, parse_function_section, parse_export_section, parse_memory_section,
parse_global_section, parse_table_section, parse_elements_section, parse_global_section, parse_table_section, parse_elements_section,
parse_data_section}; parse_data_section};
use translation_utils::{Import, SignatureIndex, FunctionIndex}; use translation_utils::{Import, FunctionIndex};
use cretonne::ir::{Function, FunctionName}; use cretonne::ir::{Function, FunctionName};
use func_translator::FuncTranslator; use func_translator::FuncTranslator;
use std::collections::HashMap; use std::collections::HashMap;
@@ -39,7 +39,6 @@ pub fn translate_module(
} }
ref s => panic!("modules should begin properly: {:?}", s), ref s => panic!("modules should begin properly: {:?}", s),
} }
let mut functions: Vec<SignatureIndex> = Vec::new();
let mut globals = Vec::new(); let mut globals = Vec::new();
let mut exports: HashMap<FunctionIndex, String> = HashMap::new(); let mut exports: HashMap<FunctionIndex, String> = HashMap::new();
let mut next_input = ParserInput::Default; let mut next_input = ParserInput::Default;
@@ -62,7 +61,6 @@ pub fn translate_module(
for import in imps { for import in imps {
match import { match import {
Import::Function { sig_index } => { Import::Function { sig_index } => {
functions.push(sig_index as SignatureIndex);
function_index += 1; function_index += 1;
} }
Import::Memory(mem) => { Import::Memory(mem) => {
@@ -86,9 +84,7 @@ pub fn translate_module(
} }
ParserState::BeginSection { code: SectionCode::Function, .. } => { ParserState::BeginSection { code: SectionCode::Function, .. } => {
match parse_function_section(&mut parser, runtime) { match parse_function_section(&mut parser, runtime) {
Ok(funcs) => { Ok(()) => {}
functions.extend(funcs);
}
Err(SectionParsingError::WrongSectionContent(s)) => { Err(SectionParsingError::WrongSectionContent(s)) => {
return Err(format!("wrong content in the function section: {}", s)) return Err(format!("wrong content in the function section: {}", s))
} }
@@ -193,7 +189,9 @@ pub fn translate_module(
runtime.next_function(); runtime.next_function();
// First we build the Function object with its name and signature // First we build the Function object with its name and signature
let mut func = Function::new(); let mut func = Function::new();
func.signature = runtime.get_signature(functions[function_index]).clone(); func.signature = runtime
.get_signature(runtime.get_func_type(function_index))
.clone();
if let Some(name) = exports.get(&function_index) { if let Some(name) = exports.get(&function_index) {
func.name = FunctionName::new(name.clone()); func.name = FunctionName::new(name.clone());
} }

View File

@@ -146,6 +146,10 @@ impl WasmRuntime for DummyRuntime {
self.func_types.push(sig_index); self.func_types.push(sig_index);
} }
fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex {
self.func_types[func_index]
}
fn declare_global(&mut self, global: Global) { fn declare_global(&mut self, global: Global) {
self.globals.push(global); self.globals.push(global);
} }

View File

@@ -162,6 +162,9 @@ pub trait WasmRuntime: FuncEnvironment {
/// Declares the type (signature) of a local function in the module. /// Declares the type (signature) of a local function in the module.
fn declare_func_type(&mut self, sig_index: SignatureIndex); fn declare_func_type(&mut self, sig_index: SignatureIndex);
/// Return the signature index for the given function index.
fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex;
/// Declares a global to the runtime. /// Declares a global to the runtime.
fn declare_global(&mut self, global: Global); fn declare_global(&mut self, global: Global);
/// Declares a table to the runtime. /// Declares a table to the runtime.

View File

@@ -113,19 +113,17 @@ pub fn parse_import_section(
pub fn parse_function_section( pub fn parse_function_section(
parser: &mut Parser, parser: &mut Parser,
runtime: &mut WasmRuntime, runtime: &mut WasmRuntime,
) -> Result<Vec<SignatureIndex>, SectionParsingError> { ) -> Result<(), SectionParsingError> {
let mut funcs = Vec::new();
loop { loop {
match *parser.read() { match *parser.read() {
ParserState::FunctionSectionEntry(sigindex) => { ParserState::FunctionSectionEntry(sigindex) => {
runtime.declare_func_type(sigindex as SignatureIndex); runtime.declare_func_type(sigindex as SignatureIndex);
funcs.push(sigindex as SignatureIndex);
} }
ParserState::EndSection => break, ParserState::EndSection => break,
ref s => return Err(SectionParsingError::WrongSectionContent(format!("{:?}", s))), ref s => return Err(SectionParsingError::WrongSectionContent(format!("{:?}", s))),
}; };
} }
Ok(funcs) Ok(())
} }
/// Retrieves the names of the functions from the export section /// Retrieves the names of the functions from the export section