Remove FunctionBuilderContext from API, and change FunctionBuilder API

This commit is contained in:
Aaron Power
2019-03-21 10:32:42 +01:00
committed by Dan Gohman
parent 0273eb84e0
commit 8fd1128990
11 changed files with 162 additions and 172 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, FunctionBuilderContext, Variable};
use cranelift_frontend::{FunctionBuilder, 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 {
func_ctx: FunctionBuilderContext,
builder: FunctionBuilder,
state: TranslationState,
}
@@ -30,7 +30,7 @@ impl FuncTranslator {
/// Create a new translator.
pub fn new() -> Self {
Self {
func_ctx: FunctionBuilderContext::new(),
builder: FunctionBuilder::new(ir::Function::new()),
state: TranslationState::new(),
}
}
@@ -57,9 +57,9 @@ impl FuncTranslator {
&mut self,
code: &[u8],
code_offset: usize,
func: &mut ir::Function,
func: ir::Function,
environ: &mut FE,
) -> WasmResult<()> {
) -> WasmResult<ir::Function> {
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: &mut ir::Function,
func: ir::Function,
environ: &mut FE,
) -> WasmResult<()> {
) -> WasmResult<ir::Function> {
let _tt = timing::wasm_translate_function();
info!(
"translate({} bytes, {}{})",
@@ -84,31 +84,32 @@ 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");
// 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.
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.
// 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.
builder.ensure_inserted_ebb();
self.builder.ensure_inserted_ebb();
let num_params = declare_wasm_parameters(&mut builder, entry_block);
let num_params = declare_wasm_parameters(&mut self.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 = builder.create_ebb();
builder.append_ebb_params_for_function_returns(exit_block);
self.state.initialize(&builder.func.signature, exit_block);
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);
parse_local_decls(&mut reader, &mut builder, num_params, environ)?;
parse_function_body(reader, &mut builder, &mut self.state, environ)?;
parse_local_decls(&mut reader, &mut self.builder, num_params, environ)?;
parse_function_body(reader, &mut self.builder, &mut self.state, environ)?;
builder.finalize();
Ok(())
Ok(self.builder.finalize())
}
}
@@ -288,8 +289,8 @@ mod tests {
ctx.func.signature.params.push(ir::AbiParam::new(I32));
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
@@ -327,8 +328,8 @@ mod tests {
ctx.func.signature.params.push(ir::AbiParam::new(I32));
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
@@ -374,8 +375,8 @@ mod tests {
ctx.func.name = ir::ExternalName::testcase("infloop");
ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
ctx.func = trans
.translate(&BODY, 0, ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();