Revert "Remove FunctionBuilderContext from API, and change FunctionBuilder API"
This reverts commit 39e638af99dbe6537bc935bfb1a74669b62877b3.
This commit is contained in:
committed by
Benjamin Bouvier
parent
8d62d5f724
commit
5426e42a27
@@ -81,7 +81,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
* `get_global` and `set_global` are handled by the environment.
|
||||
***********************************************************************************/
|
||||
Operator::GetGlobal { global_index } => {
|
||||
let val = match state.get_global(&mut builder.func, *global_index, environ)? {
|
||||
let val = match state.get_global(builder.func, *global_index, environ)? {
|
||||
GlobalVariable::Const(val) => val,
|
||||
GlobalVariable::Memory { gv, offset, ty } => {
|
||||
let addr = builder.ins().global_value(environ.pointer_type(), gv);
|
||||
@@ -92,7 +92,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
state.push1(val);
|
||||
}
|
||||
Operator::SetGlobal { global_index } => {
|
||||
match state.get_global(&mut builder.func, *global_index, environ)? {
|
||||
match state.get_global(builder.func, *global_index, environ)? {
|
||||
GlobalVariable::Const(_) => panic!("global #{} is a constant", *global_index),
|
||||
GlobalVariable::Memory { gv, offset, ty } => {
|
||||
let addr = builder.ins().global_value(environ.pointer_type(), gv);
|
||||
@@ -367,8 +367,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
* argument referring to an index in the external functions table of the module.
|
||||
************************************************************************************/
|
||||
Operator::Call { function_index } => {
|
||||
let (fref, num_args) =
|
||||
state.get_direct_func(&mut builder.func, *function_index, environ)?;
|
||||
let (fref, num_args) = state.get_direct_func(builder.func, *function_index, environ)?;
|
||||
let call = environ.translate_call(
|
||||
builder.cursor(),
|
||||
FuncIndex::from_u32(*function_index),
|
||||
@@ -389,8 +388,8 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
Operator::CallIndirect { index, table_index } => {
|
||||
// `index` is the index of the function's signature and `table_index` is the index of
|
||||
// the table to search the function in.
|
||||
let (sigref, num_args) = state.get_indirect_sig(&mut builder.func, *index, environ)?;
|
||||
let table = state.get_table(&mut builder.func, *table_index, environ)?;
|
||||
let (sigref, num_args) = state.get_indirect_sig(builder.func, *index, environ)?;
|
||||
let table = state.get_table(builder.func, *table_index, environ)?;
|
||||
let callee = state.pop1();
|
||||
let call = environ.translate_call_indirect(
|
||||
builder.cursor(),
|
||||
@@ -418,13 +417,13 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
// The WebAssembly MVP only supports one linear memory, but we expect the reserved
|
||||
// argument to be a memory index.
|
||||
let heap_index = MemoryIndex::from_u32(*reserved);
|
||||
let heap = state.get_heap(&mut builder.func, *reserved, environ)?;
|
||||
let heap = state.get_heap(builder.func, *reserved, environ)?;
|
||||
let val = state.pop1();
|
||||
state.push1(environ.translate_memory_grow(builder.cursor(), heap_index, heap, val)?)
|
||||
}
|
||||
Operator::MemorySize { reserved } => {
|
||||
let heap_index = MemoryIndex::from_u32(*reserved);
|
||||
let heap = state.get_heap(&mut builder.func, *reserved, environ)?;
|
||||
let heap = state.get_heap(builder.func, *reserved, environ)?;
|
||||
state.push1(environ.translate_memory_size(builder.cursor(), heap_index, heap)?);
|
||||
}
|
||||
/******************************* Load instructions ***********************************
|
||||
@@ -1238,7 +1237,7 @@ fn translate_load<FE: FuncEnvironment + ?Sized>(
|
||||
) -> WasmResult<()> {
|
||||
let addr32 = state.pop1();
|
||||
// We don't yet support multiple linear memories.
|
||||
let heap = state.get_heap(&mut builder.func, 0, environ)?;
|
||||
let heap = state.get_heap(builder.func, 0, environ)?;
|
||||
let (base, offset) = get_heap_addr(heap, addr32, offset, environ.pointer_type(), builder);
|
||||
// Note that we don't set `is_aligned` here, even if the load instruction's
|
||||
// alignment immediate says it's aligned, because WebAssembly's immediate
|
||||
@@ -1263,7 +1262,7 @@ fn translate_store<FE: FuncEnvironment + ?Sized>(
|
||||
let val_ty = builder.func.dfg.value_type(val);
|
||||
|
||||
// We don't yet support multiple linear memories.
|
||||
let heap = state.get_heap(&mut builder.func, 0, environ)?;
|
||||
let heap = state.get_heap(builder.func, 0, environ)?;
|
||||
let (base, offset) = get_heap_addr(heap, addr32, offset, environ.pointer_type(), builder);
|
||||
// See the comments in `translate_load` about the flags.
|
||||
let flags = MemFlags::new();
|
||||
|
||||
@@ -543,7 +543,8 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
func.collect_debug_info();
|
||||
}
|
||||
self.trans
|
||||
.translate(body_bytes, body_offset, func, &mut func_environ)?
|
||||
.translate(body_bytes, body_offset, &mut func, &mut func_environ)?;
|
||||
func
|
||||
};
|
||||
self.func_bytecode_sizes.push(body_bytes.len());
|
||||
self.info.function_bodies.push(func);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user