From ad363d7e6be281782311f30e88d5c591f1830672 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 12 Mar 2018 10:28:25 -0700 Subject: [PATCH] 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. --- lib/frontend/src/variable.rs | 8 ++++++++ lib/wasm/src/code_translator.rs | 22 ++++++++++++---------- lib/wasm/src/func_translator.rs | 17 ++++++++--------- lib/wasm/src/translation_utils.rs | 19 ------------------- 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/lib/frontend/src/variable.rs b/lib/frontend/src/variable.rs index 6b322e184f..5b35a3426b 100644 --- a/lib/frontend/src/variable.rs +++ b/lib/frontend/src/variable.rs @@ -12,6 +12,14 @@ use std::u32; #[derive(Copy, Clone, PartialEq, Eq, Debug)] 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 { fn new(index: usize) -> Self { assert!(index < (u32::MAX as usize)); diff --git a/lib/wasm/src/code_translator.rs b/lib/wasm/src/code_translator.rs index 100dfbab41..60b432a582 100644 --- a/lib/wasm/src/code_translator.rs +++ b/lib/wasm/src/code_translator.rs @@ -26,9 +26,9 @@ use cretonne::ir::{self, InstBuilder, MemFlags, JumpTableData}; use cretonne::ir::types::*; use cretonne::ir::condcodes::{IntCC, FloatCC}; use cretonne::packed_option::ReservedValue; -use cton_frontend::FunctionBuilder; +use cton_frontend::{FunctionBuilder, Variable}; 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 state::{TranslationState, ControlStackFrame}; use std::collections::{HashMap, hash_map}; @@ -39,7 +39,7 @@ use std::{i32, u32}; /// a return. pub fn translate_operator( op: Operator, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, state: &mut TranslationState, environ: &mut FE, ) { @@ -53,14 +53,16 @@ pub fn translate_operator( * `get_local` and `set_local` are treated as non-SSA variables and will completely * 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 } => { let val = state.pop1(); - builder.def_var(Local(local_index), val); + builder.def_var(Variable::with_u32(local_index), val); } Operator::TeeLocal { local_index } => { let val = state.peek1(); - builder.def_var(Local(local_index), val); + builder.def_var(Variable::with_u32(local_index), val); } /********************************** Globals **************************************** * `get_global` and `set_global` are handled by the environment. @@ -934,7 +936,7 @@ pub fn translate_operator( /// portion so the translation state muts be updated accordingly. fn translate_unreachable_operator( op: Operator, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, state: &mut TranslationState, ) { match op { @@ -1019,7 +1021,7 @@ fn get_heap_addr( addr32: ir::Value, offset: u32, addr_ty: ir::Type, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, ) -> (ir::Value, i32) { use std::cmp::min; @@ -1055,7 +1057,7 @@ fn translate_load( offset: u32, opcode: ir::Opcode, result_ty: ir::Type, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, state: &mut TranslationState, environ: &mut FE, ) { @@ -1078,7 +1080,7 @@ fn translate_load( fn translate_store( offset: u32, opcode: ir::Opcode, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, state: &mut TranslationState, environ: &mut FE, ) { diff --git a/lib/wasm/src/func_translator.rs b/lib/wasm/src/func_translator.rs index d5737bd446..0632630095 100644 --- a/lib/wasm/src/func_translator.rs +++ b/lib/wasm/src/func_translator.rs @@ -9,10 +9,9 @@ use cretonne::entity::EntityRef; use cretonne::ir::{self, InstBuilder, Ebb}; use cretonne::result::{CtonResult, CtonError}; use cretonne::timing; -use cton_frontend::{ILBuilder, FunctionBuilder}; +use cton_frontend::{ILBuilder, FunctionBuilder, Variable}; use environ::FuncEnvironment; use state::TranslationState; -use translation_utils::Local; use wasmparser::{self, BinaryReader}; /// 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 /// functions which will reduce heap allocation traffic. pub struct FuncTranslator { - il_builder: ILBuilder, + il_builder: ILBuilder, state: TranslationState, } @@ -107,7 +106,7 @@ impl FuncTranslator { /// Declare local variables for the signature parameters that correspond to WebAssembly locals. /// /// Return the number of local variables declared. -fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> usize { +fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> usize { let sig_len = builder.func.signature.params.len(); let mut next_local = 0; for i in 0..sig_len { @@ -116,7 +115,7 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Eb // signature parameters. For example, a `vmctx` pointer. if param_type.purpose == ir::ArgumentPurpose::Normal { // 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); next_local += 1; @@ -133,7 +132,7 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Eb /// Declare local variables, starting from `num_params`. fn parse_local_decls( reader: &mut BinaryReader, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, num_params: usize, ) -> CtonResult { 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. fn declare_locals( - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, count: u32, wasm_type: wasmparser::Type, next_local: &mut usize, @@ -174,7 +173,7 @@ fn declare_locals( let ty = builder.func.dfg.value_type(zeroval); for _ in 0..count { - let local = Local::new(*next_local); + let local = Variable::new(*next_local); builder.declare_var(local, ty); builder.def_var(local, zeroval); *next_local += 1; @@ -187,7 +186,7 @@ fn declare_locals( /// arguments and locals are declared in the builder. fn parse_function_body( mut reader: BinaryReader, - builder: &mut FunctionBuilder, + builder: &mut FunctionBuilder, state: &mut TranslationState, environ: &mut FE, ) -> CtonResult { diff --git a/lib/wasm/src/translation_utils.rs b/lib/wasm/src/translation_utils.rs index 4c70d708ed..44c03fb6dd 100644 --- a/lib/wasm/src/translation_utils.rs +++ b/lib/wasm/src/translation_utils.rs @@ -71,25 +71,6 @@ pub struct Memory { 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. pub fn type_to_type(ty: &wasmparser::Type) -> Result { match *ty {