Introduce TranslationContext

This commit is contained in:
Sergey Pepyakin
2018-11-29 22:28:10 +01:00
parent 52c0443368
commit 5eb43f027a
3 changed files with 26 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
use backend::*; use backend::*;
use module::TranslationContext;
use error::Error; use error::Error;
use wasmparser::{FuncType, FunctionBody, Operator, Type}; use wasmparser::{FuncType, FunctionBody, Operator, Type};
@@ -88,6 +89,7 @@ impl ControlFrame {
pub fn translate( pub fn translate(
session: &mut CodeGenSession, session: &mut CodeGenSession,
translation_ctx: &TranslationContext,
func_type: &FuncType, func_type: &FuncType,
body: &FunctionBody, body: &FunctionBody,
) -> Result<(), Error> { ) -> Result<(), Error> {

View File

@@ -2,7 +2,7 @@ use backend::TranslatedCodeSection;
use error::Error; use error::Error;
use std::mem; use std::mem;
use translate_sections; use translate_sections;
use wasmparser::{ModuleReader, SectionCode}; use wasmparser::{FuncType, ModuleReader, SectionCode};
#[derive(Default)] #[derive(Default)]
pub struct TranslatedModule { pub struct TranslatedModule {
@@ -27,6 +27,20 @@ impl TranslatedModule {
} }
} }
#[derive(Default)]
pub struct TranslationContext {
types: Vec<FuncType>,
func_ty_indicies: Vec<u32>,
}
impl TranslationContext {
pub fn func_type(&self, func_idx: u32) -> &FuncType {
// TODO: This assumes that there is no imported functions.
let func_ty_idx = self.func_ty_indicies[func_idx as usize];
&self.types[func_ty_idx as usize]
}
}
/// Translate from a slice of bytes holding a wasm module. /// Translate from a slice of bytes holding a wasm module.
pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> { pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
let mut reader = ModuleReader::new(data)?; let mut reader = ModuleReader::new(data)?;
@@ -37,12 +51,12 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
return Ok(output); return Ok(output);
} }
let mut section = reader.read()?; let mut section = reader.read()?;
let mut types = vec![];
let mut func_ty_indicies = vec![]; let mut ctx = TranslationContext::default();
if let SectionCode::Type = section.code { if let SectionCode::Type = section.code {
let types_reader = section.get_type_section_reader()?; let types_reader = section.get_type_section_reader()?;
types = translate_sections::type_(types_reader)?; ctx.types = translate_sections::type_(types_reader)?;
reader.skip_custom_sections()?; reader.skip_custom_sections()?;
if reader.eof() { if reader.eof() {
@@ -64,7 +78,7 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
if let SectionCode::Function = section.code { if let SectionCode::Function = section.code {
let functions = section.get_function_section_reader()?; let functions = section.get_function_section_reader()?;
func_ty_indicies = translate_sections::function(functions)?; ctx.func_ty_indicies = translate_sections::function(functions)?;
reader.skip_custom_sections()?; reader.skip_custom_sections()?;
if reader.eof() { if reader.eof() {
@@ -142,7 +156,7 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
if let SectionCode::Code = section.code { if let SectionCode::Code = section.code {
let code = section.get_code_section_reader()?; let code = section.get_code_section_reader()?;
output.translated_code_section = output.translated_code_section =
Some(translate_sections::code(code, &types, &func_ty_indicies)?); Some(translate_sections::code(code, &ctx)?);
reader.skip_custom_sections()?; reader.skip_custom_sections()?;
if reader.eof() { if reader.eof() {

View File

@@ -1,6 +1,7 @@
use backend::{CodeGenSession, TranslatedCodeSection}; use backend::{CodeGenSession, TranslatedCodeSection};
use error::Error; use error::Error;
use function_body; use function_body;
use module::TranslationContext;
#[allow(unused_imports)] // for now #[allow(unused_imports)] // for now
use wasmparser::{ use wasmparser::{
CodeSectionReader, Data, DataSectionReader, Element, ElementSectionReader, Export, CodeSectionReader, Data, DataSectionReader, Element, ElementSectionReader, Export,
@@ -84,15 +85,12 @@ pub fn element(elements: ElementSectionReader) -> Result<(), Error> {
/// Parses the Code section of the wasm module. /// Parses the Code section of the wasm module.
pub fn code( pub fn code(
code: CodeSectionReader, code: CodeSectionReader,
types: &[FuncType], translation_ctx: &TranslationContext
func_ty_indicies: &[u32],
) -> Result<TranslatedCodeSection, Error> { ) -> Result<TranslatedCodeSection, Error> {
let mut session = CodeGenSession::new(); let mut session = CodeGenSession::new();
for (idx, body) in code.into_iter().enumerate() { for (idx, body) in code.into_iter().enumerate() {
let func_ty_idx = func_ty_indicies[idx]; let func_ty = translation_ctx.func_type(idx as u32);
let func_ty = &types[func_ty_idx as usize]; function_body::translate(&mut session, translation_ctx, &func_ty, &body?)?;
function_body::translate(&mut session, &func_ty, &body?)?;
} }
Ok(session.into_translated_code_section()?) Ok(session.into_translated_code_section()?)
} }