Replace cretonne-wasm's Local with cretonne-frontend's Variable.

Previously, cretonne-wasm used its own Local struct for identifying
local variables. However, now that cretonne-frontend provides a
Variable struct, just use that instead.
This commit is contained in:
Dan Gohman
2018-03-12 10:28:25 -07:00
parent b8a106adf0
commit ad363d7e6b
4 changed files with 28 additions and 38 deletions

View File

@@ -12,6 +12,14 @@ use std::u32;
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Variable(u32); pub struct Variable(u32);
impl Variable {
/// Create a new Variable with the given index.
pub fn with_u32(index: u32) -> Self {
assert!(index < u32::MAX);
Variable(index)
}
}
impl EntityRef for Variable { impl EntityRef for Variable {
fn new(index: usize) -> Self { fn new(index: usize) -> Self {
assert!(index < (u32::MAX as usize)); assert!(index < (u32::MAX as usize));

View File

@@ -26,9 +26,9 @@ use cretonne::ir::{self, InstBuilder, MemFlags, JumpTableData};
use cretonne::ir::types::*; use cretonne::ir::types::*;
use cretonne::ir::condcodes::{IntCC, FloatCC}; use cretonne::ir::condcodes::{IntCC, FloatCC};
use cretonne::packed_option::ReservedValue; use cretonne::packed_option::ReservedValue;
use cton_frontend::FunctionBuilder; use cton_frontend::{FunctionBuilder, Variable};
use wasmparser::{Operator, MemoryImmediate}; use wasmparser::{Operator, MemoryImmediate};
use translation_utils::{f32_translation, f64_translation, type_to_type, num_return_values, Local}; use translation_utils::{f32_translation, f64_translation, type_to_type, num_return_values};
use translation_utils::{TableIndex, SignatureIndex, FunctionIndex, MemoryIndex}; use translation_utils::{TableIndex, SignatureIndex, FunctionIndex, MemoryIndex};
use state::{TranslationState, ControlStackFrame}; use state::{TranslationState, ControlStackFrame};
use std::collections::{HashMap, hash_map}; use std::collections::{HashMap, hash_map};
@@ -39,7 +39,7 @@ use std::{i32, u32};
/// a return. /// a return.
pub fn translate_operator<FE: FuncEnvironment + ?Sized>( pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
op: Operator, op: Operator,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
state: &mut TranslationState, state: &mut TranslationState,
environ: &mut FE, environ: &mut FE,
) { ) {
@@ -53,14 +53,16 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
* `get_local` and `set_local` are treated as non-SSA variables and will completely * `get_local` and `set_local` are treated as non-SSA variables and will completely
* disappear in the Cretonne Code * disappear in the Cretonne Code
***********************************************************************************/ ***********************************************************************************/
Operator::GetLocal { local_index } => state.push1(builder.use_var(Local(local_index))), Operator::GetLocal { local_index } => {
state.push1(builder.use_var(Variable::with_u32(local_index)))
}
Operator::SetLocal { local_index } => { Operator::SetLocal { local_index } => {
let val = state.pop1(); let val = state.pop1();
builder.def_var(Local(local_index), val); builder.def_var(Variable::with_u32(local_index), val);
} }
Operator::TeeLocal { local_index } => { Operator::TeeLocal { local_index } => {
let val = state.peek1(); let val = state.peek1();
builder.def_var(Local(local_index), val); builder.def_var(Variable::with_u32(local_index), val);
} }
/********************************** Globals **************************************** /********************************** Globals ****************************************
* `get_global` and `set_global` are handled by the environment. * `get_global` and `set_global` are handled by the environment.
@@ -934,7 +936,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
/// portion so the translation state muts be updated accordingly. /// portion so the translation state muts be updated accordingly.
fn translate_unreachable_operator( fn translate_unreachable_operator(
op: Operator, op: Operator,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
state: &mut TranslationState, state: &mut TranslationState,
) { ) {
match op { match op {
@@ -1019,7 +1021,7 @@ fn get_heap_addr(
addr32: ir::Value, addr32: ir::Value,
offset: u32, offset: u32,
addr_ty: ir::Type, addr_ty: ir::Type,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
) -> (ir::Value, i32) { ) -> (ir::Value, i32) {
use std::cmp::min; use std::cmp::min;
@@ -1055,7 +1057,7 @@ fn translate_load<FE: FuncEnvironment + ?Sized>(
offset: u32, offset: u32,
opcode: ir::Opcode, opcode: ir::Opcode,
result_ty: ir::Type, result_ty: ir::Type,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
state: &mut TranslationState, state: &mut TranslationState,
environ: &mut FE, environ: &mut FE,
) { ) {
@@ -1078,7 +1080,7 @@ fn translate_load<FE: FuncEnvironment + ?Sized>(
fn translate_store<FE: FuncEnvironment + ?Sized>( fn translate_store<FE: FuncEnvironment + ?Sized>(
offset: u32, offset: u32,
opcode: ir::Opcode, opcode: ir::Opcode,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
state: &mut TranslationState, state: &mut TranslationState,
environ: &mut FE, environ: &mut FE,
) { ) {

View File

@@ -9,10 +9,9 @@ use cretonne::entity::EntityRef;
use cretonne::ir::{self, InstBuilder, Ebb}; use cretonne::ir::{self, InstBuilder, Ebb};
use cretonne::result::{CtonResult, CtonError}; use cretonne::result::{CtonResult, CtonError};
use cretonne::timing; use cretonne::timing;
use cton_frontend::{ILBuilder, FunctionBuilder}; use cton_frontend::{ILBuilder, FunctionBuilder, Variable};
use environ::FuncEnvironment; use environ::FuncEnvironment;
use state::TranslationState; use state::TranslationState;
use translation_utils::Local;
use wasmparser::{self, BinaryReader}; use wasmparser::{self, BinaryReader};
/// WebAssembly to Cretonne IL function translator. /// WebAssembly to Cretonne IL function translator.
@@ -21,7 +20,7 @@ use wasmparser::{self, BinaryReader};
/// by a `FuncEnvironment` object. A single translator instance can be reused to translate multiple /// by a `FuncEnvironment` object. A single translator instance can be reused to translate multiple
/// functions which will reduce heap allocation traffic. /// functions which will reduce heap allocation traffic.
pub struct FuncTranslator { pub struct FuncTranslator {
il_builder: ILBuilder<Local>, il_builder: ILBuilder<Variable>,
state: TranslationState, state: TranslationState,
} }
@@ -107,7 +106,7 @@ impl FuncTranslator {
/// Declare local variables for the signature parameters that correspond to WebAssembly locals. /// Declare local variables for the signature parameters that correspond to WebAssembly locals.
/// ///
/// Return the number of local variables declared. /// Return the number of local variables declared.
fn declare_wasm_parameters(builder: &mut FunctionBuilder<Local>, entry_block: Ebb) -> usize { fn declare_wasm_parameters(builder: &mut FunctionBuilder<Variable>, entry_block: Ebb) -> usize {
let sig_len = builder.func.signature.params.len(); let sig_len = builder.func.signature.params.len();
let mut next_local = 0; let mut next_local = 0;
for i in 0..sig_len { for i in 0..sig_len {
@@ -116,7 +115,7 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder<Local>, entry_block: Eb
// signature parameters. For example, a `vmctx` pointer. // signature parameters. For example, a `vmctx` pointer.
if param_type.purpose == ir::ArgumentPurpose::Normal { if param_type.purpose == ir::ArgumentPurpose::Normal {
// This is a normal WebAssembly signature parameter, so create a local for it. // This is a normal WebAssembly signature parameter, so create a local for it.
let local = Local::new(next_local); let local = Variable::new(next_local);
builder.declare_var(local, param_type.value_type); builder.declare_var(local, param_type.value_type);
next_local += 1; next_local += 1;
@@ -133,7 +132,7 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder<Local>, entry_block: Eb
/// Declare local variables, starting from `num_params`. /// Declare local variables, starting from `num_params`.
fn parse_local_decls( fn parse_local_decls(
reader: &mut BinaryReader, reader: &mut BinaryReader,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
num_params: usize, num_params: usize,
) -> CtonResult { ) -> CtonResult {
let mut next_local = num_params; let mut next_local = num_params;
@@ -157,7 +156,7 @@ fn parse_local_decls(
/// ///
/// Fail of too many locals are declared in the function, or if the type is not valid for a local. /// Fail of too many locals are declared in the function, or if the type is not valid for a local.
fn declare_locals( fn declare_locals(
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
count: u32, count: u32,
wasm_type: wasmparser::Type, wasm_type: wasmparser::Type,
next_local: &mut usize, next_local: &mut usize,
@@ -174,7 +173,7 @@ fn declare_locals(
let ty = builder.func.dfg.value_type(zeroval); let ty = builder.func.dfg.value_type(zeroval);
for _ in 0..count { for _ in 0..count {
let local = Local::new(*next_local); let local = Variable::new(*next_local);
builder.declare_var(local, ty); builder.declare_var(local, ty);
builder.def_var(local, zeroval); builder.def_var(local, zeroval);
*next_local += 1; *next_local += 1;
@@ -187,7 +186,7 @@ fn declare_locals(
/// arguments and locals are declared in the builder. /// arguments and locals are declared in the builder.
fn parse_function_body<FE: FuncEnvironment + ?Sized>( fn parse_function_body<FE: FuncEnvironment + ?Sized>(
mut reader: BinaryReader, mut reader: BinaryReader,
builder: &mut FunctionBuilder<Local>, builder: &mut FunctionBuilder<Variable>,
state: &mut TranslationState, state: &mut TranslationState,
environ: &mut FE, environ: &mut FE,
) -> CtonResult { ) -> CtonResult {

View File

@@ -71,25 +71,6 @@ pub struct Memory {
pub shared: bool, pub shared: bool,
} }
/// Wrapper to a `get_local` and `set_local` index. They are WebAssembly's non-SSA variables.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Local(pub u32);
impl cretonne::entity::EntityRef for Local {
fn new(index: usize) -> Self {
debug_assert!(index < (u32::MAX as usize));
Local(index as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
impl Default for Local {
fn default() -> Self {
Local(u32::MAX)
}
}
/// Helper function translating wasmparser types to Cretonne types when possible. /// Helper function translating wasmparser types to Cretonne types when possible.
pub fn type_to_type(ty: &wasmparser::Type) -> Result<cretonne::ir::Type, ()> { pub fn type_to_type(ty: &wasmparser::Type) -> Result<cretonne::ir::Type, ()> {
match *ty { match *ty {