Introduce TranslationContext
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use backend::*;
|
||||
use module::TranslationContext;
|
||||
use error::Error;
|
||||
use wasmparser::{FuncType, FunctionBody, Operator, Type};
|
||||
|
||||
@@ -88,6 +89,7 @@ impl ControlFrame {
|
||||
|
||||
pub fn translate(
|
||||
session: &mut CodeGenSession,
|
||||
translation_ctx: &TranslationContext,
|
||||
func_type: &FuncType,
|
||||
body: &FunctionBody,
|
||||
) -> Result<(), Error> {
|
||||
|
||||
@@ -2,7 +2,7 @@ use backend::TranslatedCodeSection;
|
||||
use error::Error;
|
||||
use std::mem;
|
||||
use translate_sections;
|
||||
use wasmparser::{ModuleReader, SectionCode};
|
||||
use wasmparser::{FuncType, ModuleReader, SectionCode};
|
||||
|
||||
#[derive(Default)]
|
||||
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.
|
||||
pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
|
||||
let mut reader = ModuleReader::new(data)?;
|
||||
@@ -37,12 +51,12 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
|
||||
return Ok(output);
|
||||
}
|
||||
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 {
|
||||
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()?;
|
||||
if reader.eof() {
|
||||
@@ -64,7 +78,7 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
|
||||
|
||||
if let SectionCode::Function = section.code {
|
||||
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()?;
|
||||
if reader.eof() {
|
||||
@@ -142,7 +156,7 @@ pub fn translate(data: &[u8]) -> Result<TranslatedModule, Error> {
|
||||
if let SectionCode::Code = section.code {
|
||||
let code = section.get_code_section_reader()?;
|
||||
output.translated_code_section =
|
||||
Some(translate_sections::code(code, &types, &func_ty_indicies)?);
|
||||
Some(translate_sections::code(code, &ctx)?);
|
||||
|
||||
reader.skip_custom_sections()?;
|
||||
if reader.eof() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use backend::{CodeGenSession, TranslatedCodeSection};
|
||||
use error::Error;
|
||||
use function_body;
|
||||
use module::TranslationContext;
|
||||
#[allow(unused_imports)] // for now
|
||||
use wasmparser::{
|
||||
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.
|
||||
pub fn code(
|
||||
code: CodeSectionReader,
|
||||
types: &[FuncType],
|
||||
func_ty_indicies: &[u32],
|
||||
translation_ctx: &TranslationContext
|
||||
) -> Result<TranslatedCodeSection, Error> {
|
||||
let mut session = CodeGenSession::new();
|
||||
for (idx, body) in code.into_iter().enumerate() {
|
||||
let func_ty_idx = func_ty_indicies[idx];
|
||||
let func_ty = &types[func_ty_idx as usize];
|
||||
|
||||
function_body::translate(&mut session, &func_ty, &body?)?;
|
||||
let func_ty = translation_ctx.func_type(idx as u32);
|
||||
function_body::translate(&mut session, translation_ctx, &func_ty, &body?)?;
|
||||
}
|
||||
Ok(session.into_translated_code_section()?)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user