Skip to content

Commit

Permalink
Merge pull request bytecodealliance#2449 from bytecodealliance/cfalli…
Browse files Browse the repository at this point in the history
…n/add-pre-host-hooks

Add FuncEnvironment hooks to generate prologue and epilogue code.
  • Loading branch information
fitzgen authored Nov 25, 2020
2 parents b06ed39 + 4300c2c commit 93c1993
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/aarch64/inst/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl UnwindInfoGenerator<Inst> for AArch64UnwindInfo {

// TODO epilogues

let prologue_size = if context.prologue.is_empty() {
let prologue_size = if context.prologue.len() == 0 {
0
} else {
context.insts_layout[context.prologue.end as usize - 1]
Expand Down
20 changes: 20 additions & 0 deletions cranelift/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,26 @@ pub trait FuncEnvironment: TargetEnvironment {
) -> WasmResult<()> {
Ok(())
}

/// Optional callback for the `FunctionEnvironment` performing this translation to perform work
/// before the function body is translated.
fn before_translate_function(
&mut self,
_builder: &mut FunctionBuilder,
_state: &FuncTranslationState,
) -> WasmResult<()> {
Ok(())
}

/// Optional callback for the `FunctionEnvironment` performing this translation to perform work
/// after the function body is translated.
fn after_translate_function(
&mut self,
_builder: &mut FunctionBuilder,
_state: &FuncTranslationState,
) -> WasmResult<()> {
Ok(())
}
}

/// An object satisfying the `ModuleEnvironment` trait can be passed as argument to the
Expand Down
3 changes: 2 additions & 1 deletion cranelift/wasm/src/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
// The control stack is initialized with a single block representing the whole function.
debug_assert_eq!(state.control_stack.len(), 1, "State not initialized");

environ.before_translate_function(builder, state)?;
while !reader.eof() {
let pos = reader.original_position();
builder.set_srcloc(cur_srcloc(&reader));
Expand All @@ -234,6 +235,7 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
translate_operator(validator, &op, builder, state, environ)?;
environ.after_translate_operator(&op, builder, state)?;
}
environ.after_translate_function(builder, state)?;
let pos = reader.original_position();
validator.finish(pos)?;

Expand All @@ -243,7 +245,6 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
// If the exit block is unreachable, it may not have the correct arguments, so we would
// generate a return instruction that doesn't match the signature.
if state.reachable {
debug_assert!(builder.is_pristine());
if !builder.is_unreachable() {
match environ.return_mode() {
ReturnMode::NormalReturns => {
Expand Down

0 comments on commit 93c1993

Please sign in to comment.