Skip to content

Commit

Permalink
Pass Encoding to compute_size() for runtime Encoding inspection. byte…
Browse files Browse the repository at this point in the history
…codealliance#1156

In some cases, compute_size() is used to choose between various different Encodings
before one has been assigned to an instruction. For x86, the REX.W bit is stored
in the Encoding. To share recipes between REX/non-REX, that bit must be inspected
by compute_size().
  • Loading branch information
sstangl authored and abrown committed Nov 8, 2019
1 parent 143cb01 commit a06f2c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 13 additions & 7 deletions cranelift/codegen/src/isa/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,29 @@ impl fmt::Display for DisplayEncoding {
}
}

type SizeCalculatorFn = fn(&RecipeSizing, Inst, &RegDiversions, &Function) -> u8;
type SizeCalculatorFn = fn(&RecipeSizing, Encoding, Inst, &RegDiversions, &Function) -> u8;

/// Returns the base size of the Recipe, assuming it's fixed. This is the default for most
/// encodings; others can be variable and longer than this base size, depending on the registers
/// they're using and use a different function, specific per platform.
pub fn base_size(sizing: &RecipeSizing, _: Inst, _: &RegDiversions, _: &Function) -> u8 {
pub fn base_size(
sizing: &RecipeSizing,
_: Encoding,
_: Inst,
_: &RegDiversions,
_: &Function,
) -> u8 {
sizing.base_size
}

/// Code size information for an encoding recipe.
///
/// All encoding recipes correspond to an exact instruction size.
/// Encoding recipes may have runtime-determined instruction size.
pub struct RecipeSizing {
/// Size in bytes of instructions encoded with this recipe.
/// Minimum size in bytes of instructions encoded with this recipe.
pub base_size: u8,

/// Method computing the real instruction's size, given inputs and outputs.
/// Method computing the instruction's real size, given inputs and outputs.
pub compute_size: SizeCalculatorFn,

/// Allowed branch range in this recipe, if any.
Expand Down Expand Up @@ -132,7 +138,7 @@ impl EncInfo {
}
}

/// Get the precise size in bytes of instructions encoded with `enc`.
/// Get the size in bytes of `inst`, if it were encoded with `enc`.
///
/// Returns 0 for illegal encodings.
pub fn byte_size(
Expand All @@ -144,7 +150,7 @@ impl EncInfo {
) -> CodeOffset {
self.sizing.get(enc.recipe()).map_or(0, |s| {
let compute_size = s.compute_size;
CodeOffset::from(compute_size(&s, inst, divert, func))
CodeOffset::from(compute_size(&s, enc, inst, divert, func))
})
}

Expand Down
8 changes: 7 additions & 1 deletion cranelift/codegen/src/isa/x86/enc_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ir::{self, Function, Inst, InstBuilder};
use crate::isa::constraints::*;
use crate::isa::enc_tables::*;
use crate::isa::encoding::base_size;
use crate::isa::encoding::RecipeSizing;
use crate::isa::encoding::{Encoding, RecipeSizing};
use crate::isa::RegUnit;
use crate::isa::{self, TargetIsa};
use crate::predicates;
Expand Down Expand Up @@ -46,6 +46,7 @@ fn additional_size_if(

fn size_plus_maybe_offset_for_in_reg_0(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand All @@ -54,6 +55,7 @@ fn size_plus_maybe_offset_for_in_reg_0(
}
fn size_plus_maybe_offset_for_in_reg_1(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand All @@ -62,6 +64,7 @@ fn size_plus_maybe_offset_for_in_reg_1(
}
fn size_plus_maybe_sib_for_in_reg_0(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand All @@ -70,6 +73,7 @@ fn size_plus_maybe_sib_for_in_reg_0(
}
fn size_plus_maybe_sib_for_in_reg_1(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand All @@ -78,6 +82,7 @@ fn size_plus_maybe_sib_for_in_reg_1(
}
fn size_plus_maybe_sib_or_offset_for_in_reg_0(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand All @@ -86,6 +91,7 @@ fn size_plus_maybe_sib_or_offset_for_in_reg_0(
}
fn size_plus_maybe_sib_or_offset_for_in_reg_1(
sizing: &RecipeSizing,
_enc: Encoding,
inst: Inst,
divert: &RegDiversions,
func: &Function,
Expand Down

0 comments on commit a06f2c8

Please sign in to comment.