Use EntityRef::from_u32 to reduce casting.

This commit is contained in:
Dan Gohman
2018-12-11 12:25:06 -08:00
parent a55c933f19
commit bc18085ad1
6 changed files with 27 additions and 34 deletions

View File

@@ -26,7 +26,6 @@ use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
use cranelift_codegen::ir::types::*;
use cranelift_codegen::ir::{self, InstBuilder, JumpTableData, MemFlags};
use cranelift_codegen::packed_option::ReservedValue;
use cranelift_entity::EntityRef;
use cranelift_frontend::{FunctionBuilder, Variable};
use environ::{FuncEnvironment, GlobalVariable, ReturnMode, WasmError, WasmResult};
use state::{ControlStackFrame, TranslationState};
@@ -355,7 +354,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let (fref, num_args) = state.get_direct_func(builder.func, function_index, environ);
let call = environ.translate_call(
builder.cursor(),
FuncIndex::new(function_index as usize),
FuncIndex::from_u32(function_index),
fref,
state.peekn(num_args),
)?;
@@ -378,9 +377,9 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let callee = state.pop1();
let call = environ.translate_call_indirect(
builder.cursor(),
TableIndex::new(table_index as usize),
TableIndex::from_u32(table_index),
table,
SignatureIndex::new(index as usize),
SignatureIndex::from_u32(index),
sigref,
callee,
state.peekn(num_args),
@@ -401,13 +400,13 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
Operator::MemoryGrow { reserved } => {
// The WebAssembly MVP only supports one linear memory, but we expect the reserved
// argument to be a memory index.
let heap_index = MemoryIndex::new(reserved as usize);
let heap_index = MemoryIndex::from_u32(reserved);
let heap = state.get_heap(builder.func, reserved, environ);
let val = state.pop1();
state.push1(environ.translate_memory_grow(builder.cursor(), heap_index, heap, val)?)
}
Operator::MemorySize { reserved } => {
let heap_index = MemoryIndex::new(reserved as usize);
let heap_index = MemoryIndex::from_u32(reserved);
let heap = state.get_heap(builder.func, reserved, environ);
state.push1(environ.translate_memory_size(builder.cursor(), heap_index, heap)?);
}

View File

@@ -70,11 +70,7 @@ pub fn parse_import_section<'data>(
match import.ty {
ImportSectionEntryType::Function(sig) => {
environ.declare_func_import(
SignatureIndex::new(sig as usize),
module_name,
field_name,
);
environ.declare_func_import(SignatureIndex::from_u32(sig), module_name, field_name);
}
ImportSectionEntryType::Memory(MemoryType {
limits: ref memlimits,
@@ -127,7 +123,7 @@ pub fn parse_function_section(
) -> WasmResult<()> {
for entry in functions {
let sigindex = entry?;
environ.declare_func_type(SignatureIndex::new(sigindex as usize));
environ.declare_func_type(SignatureIndex::from_u32(sigindex));
}
Ok(())
}
@@ -187,7 +183,7 @@ pub fn parse_global_section(
Operator::F32Const { value } => GlobalInit::F32Const(value.bits()),
Operator::F64Const { value } => GlobalInit::F64Const(value.bits()),
Operator::GetGlobal { global_index } => {
GlobalInit::GetGlobal(GlobalIndex::new(global_index as usize))
GlobalInit::GetGlobal(GlobalIndex::from_u32(global_index))
}
ref s => panic!("unsupported init expr in global section: {:?}", s),
};
@@ -230,7 +226,7 @@ pub fn parse_export_section<'data>(
/// Parses the Start section of the wasm module.
pub fn parse_start_section(index: u32, environ: &mut ModuleEnvironment) -> WasmResult<()> {
environ.declare_start_func(FuncIndex::new(index as usize));
environ.declare_start_func(FuncIndex::from_u32(index));
Ok(())
}
@@ -249,11 +245,11 @@ pub fn parse_element_section<'data>(
let (base, offset) = match init_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u32 as usize),
Operator::GetGlobal { global_index } => match environ
.get_global(GlobalIndex::new(global_index as usize))
.get_global(GlobalIndex::from_u32(global_index))
.initializer
{
GlobalInit::I32Const(value) => (None, value as u32 as usize),
GlobalInit::Import => (Some(GlobalIndex::new(global_index as usize)), 0),
GlobalInit::Import => (Some(GlobalIndex::from_u32(global_index)), 0),
_ => panic!("should not happen"),
},
ref s => panic!("unsupported init expr in element section: {:?}", s),
@@ -262,9 +258,9 @@ pub fn parse_element_section<'data>(
let mut elems = Vec::new();
for item in items_reader {
let x = item?;
elems.push(FuncIndex::new(x as usize));
elems.push(FuncIndex::from_u32(x));
}
environ.declare_table_elements(TableIndex::new(table_index as usize), base, offset, elems)
environ.declare_table_elements(TableIndex::from_u32(table_index), base, offset, elems)
}
Ok(())
}
@@ -297,17 +293,17 @@ pub fn parse_data_section<'data>(
let (base, offset) = match init_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u32 as usize),
Operator::GetGlobal { global_index } => match environ
.get_global(GlobalIndex::new(global_index as usize))
.get_global(GlobalIndex::from_u32(global_index))
.initializer
{
GlobalInit::I32Const(value) => (None, value as u32 as usize),
GlobalInit::Import => (Some(GlobalIndex::new(global_index as usize)), 0),
GlobalInit::Import => (Some(GlobalIndex::from_u32(global_index)), 0),
_ => panic!("should not happen"),
},
ref s => panic!("unsupported init expr in data section: {:?}", s),
};
environ.declare_data_initialization(
MemoryIndex::new(memory_index as usize),
MemoryIndex::from_u32(memory_index),
base,
offset,
data,

View File

@@ -4,7 +4,6 @@
//! value and control stacks during the translation of a single function.
use cranelift_codegen::ir::{self, Ebb, Inst, Value};
use cranelift_entity::EntityRef;
use environ::{FuncEnvironment, GlobalVariable};
use std::collections::HashMap;
use std::vec::Vec;
@@ -287,7 +286,7 @@ impl TranslationState {
index: u32,
environ: &mut FE,
) -> GlobalVariable {
let index = GlobalIndex::new(index as usize);
let index = GlobalIndex::from_u32(index);
*self
.globals
.entry(index)
@@ -302,7 +301,7 @@ impl TranslationState {
index: u32,
environ: &mut FE,
) -> ir::Heap {
let index = MemoryIndex::new(index as usize);
let index = MemoryIndex::from_u32(index);
*self
.heaps
.entry(index)
@@ -317,7 +316,7 @@ impl TranslationState {
index: u32,
environ: &mut FE,
) -> ir::Table {
let index = TableIndex::new(index as usize);
let index = TableIndex::from_u32(index);
*self
.tables
.entry(index)
@@ -334,7 +333,7 @@ impl TranslationState {
index: u32,
environ: &mut FE,
) -> (ir::SigRef, usize) {
let index = SignatureIndex::new(index as usize);
let index = SignatureIndex::from_u32(index);
*self.signatures.entry(index).or_insert_with(|| {
let sig = environ.make_indirect_sig(func, index);
(sig, normal_args(&func.dfg.signatures[sig]))
@@ -351,7 +350,7 @@ impl TranslationState {
index: u32,
environ: &mut FE,
) -> (ir::FuncRef, usize) {
let index = FuncIndex::new(index as usize);
let index = FuncIndex::from_u32(index);
*self.functions.entry(index).or_insert_with(|| {
let fref = environ.make_direct_func(func, index);
let sig = func.dfg.ext_funcs[fref].signature;