Skip to content

Commit

Permalink
winch(x64): Add support for loop, br and br_if (bytecodeallianc…
Browse files Browse the repository at this point in the history
…e#6603)

This change adds support for the `loop`, `br` and `br_if` instructions
as well as unreachable code handling. Whenever an instruction that
affects reachability is emitted (`br` in the case of this PR), the
compiler will enter into an unreachable code state, essentially ignoring
most of the subsequent instructions. When handling the unreachable code
state some instructions are still observed, in order to determine if
reachability should be restored.

This change, particulary the handling of unreachable code, adds all the
necessary building blocks to the compiler to emit other instructions
that affect reachability (e.g `unreachable`, `return`).

Address review feedback

* Rename `branch_target` to `is_branch_target`
* Use the visitor pattern to handle unreachable code

Avoid string comparison and split unreachable handling functions
  • Loading branch information
saulecabrera authored Jun 21, 2023
1 parent 3dfbfb6 commit 1bc4ff3
Show file tree
Hide file tree
Showing 59 changed files with 1,869 additions and 49 deletions.
5 changes: 4 additions & 1 deletion fuzz/fuzz_targets/differential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ fn winch_supports_module(module: &[u8]) -> bool {
| End { .. }
| If { .. }
| Else { .. }
| Block { .. } => {}
| Block { .. }
| Loop { .. }
| Br { .. }
| BrIf { .. } => {}
_ => {
supported = false;
break 'main;
Expand Down
2 changes: 1 addition & 1 deletion winch/codegen/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl ABIArg {
}

/// ABI-specific representation of the function result.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub(crate) enum ABIResult {
Reg {
/// Type of the result.
Expand Down
15 changes: 15 additions & 0 deletions winch/codegen/src/codegen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub(crate) struct CodeGenContext<'a> {
pub stack: Stack,
/// The current function's frame.
pub frame: &'a Frame,
/// Reachability state.
pub reachable: bool,
}

impl<'a> CodeGenContext<'a> {
Expand All @@ -39,6 +41,7 @@ impl<'a> CodeGenContext<'a> {
regalloc,
stack,
frame,
reachable: true,
}
}

Expand Down Expand Up @@ -243,6 +246,18 @@ impl<'a> CodeGenContext<'a> {
self.stack.inner_mut().truncate(truncate);
}

/// Reset value and stack pointer to the given length
/// and stack pointer offset respectively.
pub fn reset_stack<M: MacroAssembler>(
&mut self,
masm: &mut M,
stack_len: usize,
sp_offset: u32,
) {
masm.reset_stack_pointer(sp_offset);
self.drop_last(self.stack.len() - stack_len);
}

/// Convenience wrapper around [`Self::spill_callback`].
///
/// This function exists for cases in which triggering an unconditional
Expand Down
Loading

0 comments on commit 1bc4ff3

Please sign in to comment.