Revert "Remove FunctionBuilderContext from API, and change FunctionBuilder API"

This reverts commit 39e638af99dbe6537bc935bfb1a74669b62877b3.
This commit is contained in:
Erin Power
2019-09-14 17:08:10 +02:00
committed by Benjamin Bouvier
parent 8d62d5f724
commit 5426e42a27
11 changed files with 172 additions and 162 deletions

View File

@@ -12,7 +12,7 @@ use crate::wasm_unsupported;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::{self, Ebb, InstBuilder, ValueLabel};
use cranelift_codegen::timing;
use cranelift_frontend::{FunctionBuilder, Variable};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
use log::info;
use wasmparser::{self, BinaryReader};
@@ -22,7 +22,7 @@ use wasmparser::{self, BinaryReader};
/// by a `FuncEnvironment` object. A single translator instance can be reused to translate multiple
/// functions which will reduce heap allocation traffic.
pub struct FuncTranslator {
builder: FunctionBuilder,
func_ctx: FunctionBuilderContext,
state: TranslationState,
}
@@ -30,7 +30,7 @@ impl FuncTranslator {
/// Create a new translator.
pub fn new() -> Self {
Self {
builder: FunctionBuilder::new(ir::Function::new()),
func_ctx: FunctionBuilderContext::new(),
state: TranslationState::new(),
}
}
@@ -57,9 +57,9 @@ impl FuncTranslator {
&mut self,
code: &[u8],
code_offset: usize,
func: ir::Function,
func: &mut ir::Function,
environ: &mut FE,
) -> WasmResult<ir::Function> {
) -> WasmResult<()> {
self.translate_from_reader(
BinaryReader::new_with_offset(code, code_offset),
func,
@@ -71,9 +71,9 @@ impl FuncTranslator {
pub fn translate_from_reader<FE: FuncEnvironment + ?Sized>(
&mut self,
mut reader: BinaryReader,
func: ir::Function,
func: &mut ir::Function,
environ: &mut FE,
) -> WasmResult<ir::Function> {
) -> WasmResult<()> {
let _tt = timing::wasm_translate_function();
info!(
"translate({} bytes, {}{})",
@@ -84,32 +84,31 @@ impl FuncTranslator {
debug_assert_eq!(func.dfg.num_ebbs(), 0, "Function must be empty");
debug_assert_eq!(func.dfg.num_insts(), 0, "Function must be empty");
self.builder.func = func;
self.builder.set_srcloc(cur_srcloc(&reader));
let entry_block = self.builder.create_ebb();
self.builder
.append_ebb_params_for_function_params(entry_block);
self.builder.switch_to_block(entry_block); // This also creates values for the arguments.
self.builder.seal_block(entry_block); // Declare all predecessors known.
// This clears the `FunctionBuilderContext`.
let mut builder = FunctionBuilder::new(func, &mut self.func_ctx);
builder.set_srcloc(cur_srcloc(&reader));
let entry_block = builder.create_ebb();
builder.append_ebb_params_for_function_params(entry_block);
builder.switch_to_block(entry_block); // This also creates values for the arguments.
builder.seal_block(entry_block); // Declare all predecessors known.
// Make sure the entry block is inserted in the layout before we make any callbacks to
// `environ`. The callback functions may need to insert things in the entry block.
self.builder.ensure_inserted_ebb();
builder.ensure_inserted_ebb();
let num_params = declare_wasm_parameters(&mut self.builder, entry_block);
let num_params = declare_wasm_parameters(&mut builder, entry_block);
// Set up the translation state with a single pushed control block representing the whole
// function and its return values.
let exit_block = self.builder.create_ebb();
self.builder
.append_ebb_params_for_function_returns(exit_block);
self.state
.initialize(&self.builder.func.signature, exit_block);
let exit_block = builder.create_ebb();
builder.append_ebb_params_for_function_returns(exit_block);
self.state.initialize(&builder.func.signature, exit_block);
parse_local_decls(&mut reader, &mut self.builder, num_params, environ)?;
parse_function_body(reader, &mut self.builder, &mut self.state, environ)?;
parse_local_decls(&mut reader, &mut builder, num_params, environ)?;
parse_function_body(reader, &mut builder, &mut self.state, environ)?;
Ok(self.builder.finalize())
builder.finalize();
Ok(())
}
}
@@ -289,8 +288,8 @@ mod tests {
ctx.func.signature.params.push(ir::AbiParam::new(I32));
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
@@ -328,8 +327,8 @@ mod tests {
ctx.func.signature.params.push(ir::AbiParam::new(I32));
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
@@ -375,8 +374,8 @@ mod tests {
ctx.func.name = ir::ExternalName::testcase("infloop");
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();