Don't renumber entities in the parser.

This makes it easier to debug testcases:
 - the entity numbers in a .cton file match the entity numbers used
   within Cretonne.
 - serializing and deserializing doesn't cause indices to change.

One disadvantage is that if a .cton file uses sparse entity numbers,
deserializing to the in-memory form doesn't compact it. However, the
text format is not intended to be performance-critical, so this isn't
expected to be a big burden.
This commit is contained in:
Dan Gohman
2018-02-15 21:13:25 -08:00
parent c846ec1626
commit a5b00b173e
22 changed files with 677 additions and 650 deletions

View File

@@ -11,8 +11,8 @@ isa intel haswell
; Tests from binary32.cton affected by allones_funcaddrs. ; Tests from binary32.cton affected by allones_funcaddrs.
function %I32() { function %I32() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
ebb0: ebb0:

View File

@@ -12,8 +12,8 @@ isa intel haswell
; Tests from binary64.cton affected by allones_funcaddrs. ; Tests from binary64.cton affected by allones_funcaddrs.
function %I64() { function %I64() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
ebb0: ebb0:

View File

@@ -9,8 +9,8 @@ isa intel haswell
; ;
function %I32() { function %I32() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
gv0 = globalsym %some_gv gv0 = globalsym %some_gv

View File

@@ -12,8 +12,8 @@ isa intel haswell
; Tests for i64 instructions. ; Tests for i64 instructions.
function %I64() { function %I64() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
gv0 = globalsym %some_gv gv0 = globalsym %some_gv

View File

@@ -11,8 +11,8 @@ isa intel haswell
; Tests for i64 instructions. ; Tests for i64 instructions.
function %I64() { function %I64() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
gv0 = globalsym %some_gv gv0 = globalsym %some_gv
@@ -641,8 +641,8 @@ ebb1:
; encodings which are chosen by default. Switching to non-REX encodings should ; encodings which are chosen by default. Switching to non-REX encodings should
; be done by an instruction shrinking pass. ; be done by an instruction shrinking pass.
function %I32() { function %I32() {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
ss0 = incoming_arg 8, offset 0 ss0 = incoming_arg 8, offset 0
ss1 = incoming_arg 1024, offset -1024 ss1 = incoming_arg 1024, offset -1024

View File

@@ -76,9 +76,9 @@ ebb0(v0: i32, v999: i64):
; check: $ebb0( ; check: $ebb0(
; nextln: trap heap_oob ; nextln: trap heap_oob
; check: ebb1: ; check: ebb1:
; nextln: v2 = iconst.i64 0 ; nextln: v1 = iconst.i64 0
; nextln: v3 = load.f32 v2+16 ; nextln: v2 = load.f32 v1+16
; nextln: return v3 ; nextln: return v2
; nextln: } ; nextln: }
v1 = heap_addr.i64 heap0, v0, 0x1000_0001 v1 = heap_addr.i64 heap0, v0, 0x1000_0001
v2 = load.f32 v1+16 v2 = load.f32 v1+16

View File

@@ -3,8 +3,8 @@ test binemit
isa riscv isa riscv
function %RV32I(i32 link [%x1]) -> i32 link [%x1] { function %RV32I(i32 link [%x1]) -> i32 link [%x1] {
fn0 = function %foo()
sig0 = () sig0 = ()
fn0 = function %foo()
ebb0(v9999: i32): ebb0(v9999: i32):
[-,%x10] v1 = iconst.i32 1 [-,%x10] v1 = iconst.i32 1

View File

@@ -27,7 +27,7 @@ function %parse_encoding(i32 [%x5]) -> i32 [%x10] {
; check: sig5 = () -> f32 [0] native ; check: sig5 = () -> f32 [0] native
; function + signature ; function + signature
fn15 = function %bar(i32 [%x10]) -> b1 [%x10] native fn0 = function %bar(i32 [%x10]) -> b1 [%x10] native
; check: sig6 = (i32 [%x10]) -> b1 [%x10] native ; check: sig6 = (i32 [%x10]) -> b1 [%x10] native
; nextln: fn0 = sig6 %bar ; nextln: fn0 = sig6 %bar

View File

@@ -25,22 +25,22 @@ ebb3(v30: i32):
} }
; sameln:function %multiple_blocks(i32) -> i32 { ; sameln:function %multiple_blocks(i32) -> i32 {
; nextln: ebb0(v0: i32): ; nextln: ebb0(v0: i32):
; nextln: v2 = iconst.i32 1 ; nextln: v11 = iconst.i32 1
; nextln: v3 = iconst.i32 2 ; nextln: v12 = iconst.i32 2
; nextln: v4 = iadd v2, v3 ; nextln: v13 = iadd v11, v12
; nextln: v9 = iadd v2, v4 ; nextln: v31 = iadd v11, v13
; nextln: jump ebb1(v0) ; nextln: jump ebb1(v0)
; nextln: ; nextln:
; nextln: ebb1(v1: i32): ; nextln: ebb1(v10: i32):
; nextln: brz v1, ebb2(v1) ; nextln: brz v10, ebb2(v10)
; nextln: v5 = isub v1, v2 ; nextln: v15 = isub v10, v11
; nextln: brz v5, ebb3(v5) ; nextln: brz v15, ebb3(v15)
; nextln: v6 = isub v1, v2 ; nextln: v14 = isub v10, v11
; nextln: jump ebb1(v6) ; nextln: jump ebb1(v14)
; nextln: ; nextln:
; nextln: ebb2(v7: i32): ; nextln: ebb2(v20: i32):
; nextln: return v7 ; nextln: return v20
; nextln: ; nextln:
; nextln: ebb3(v8: i32): ; nextln: ebb3(v30: i32):
; nextln: jump ebb1(v8) ; nextln: jump ebb1(v30)
; nextln: } ; nextln: }

View File

@@ -33,23 +33,23 @@ ebb4(v30: i32):
; nextln: v2 = iconst.i32 1 ; nextln: v2 = iconst.i32 1
; nextln: v3 = iconst.i32 2 ; nextln: v3 = iconst.i32 2
; nextln: v4 = iadd v2, v3 ; nextln: v4 = iadd v2, v3
; nextln: v8 = iconst.i32 1 ; nextln: v12 = iconst.i32 1
; nextln: jump ebb1(v0) ; nextln: jump ebb1(v0)
; nextln: ; nextln:
; nextln: ebb1(v1: i32): ; nextln: ebb1(v1: i32):
; nextln: v5 = isub v1, v2 ; nextln: v5 = isub v1, v2
; nextln: v9 = iadd.i32 v8, v5 ; nextln: v15 = iadd.i32 v12, v5
; nextln: jump ebb2(v5, v5) ; nextln: jump ebb2(v5, v5)
; nextln: ; nextln:
; nextln: ebb2(v6: i32, v7: i32): ; nextln: ebb2(v10: i32, v11: i32):
; nextln: brz v7, ebb3(v6) ; nextln: brz v11, ebb3(v10)
; nextln: v10 = isub v7, v8 ; nextln: v13 = isub v11, v12
; nextln: jump ebb2(v6, v10) ; nextln: jump ebb2(v10, v13)
; nextln: ; nextln:
; nextln: ebb3(v11: i32): ; nextln: ebb3(v20: i32):
; nextln: brz v11, ebb4(v11) ; nextln: brz v20, ebb4(v20)
; nextln: jump ebb1(v11) ; nextln: jump ebb1(v20)
; nextln: ; nextln:
; nextln: ebb4(v12: i32): ; nextln: ebb4(v30: i32):
; nextln: return v12 ; nextln: return v30
; nextln: } ; nextln: }

View File

@@ -95,19 +95,18 @@ ebb40:
trap user4 trap user4
} }
; sameln: function %jumptable(i32) native { ; sameln: function %jumptable(i32) native {
; nextln: jt0 = jump_table 0 ; check: jt2 = jump_table 0, 0, ebb10, ebb40, ebb20, ebb30
; nextln: jt1 = jump_table 0, 0, ebb0, ebb3, ebb1, ebb2 ; check: jt200 = jump_table 0
; nextln: ; check: ebb10($v3: i32):
; nextln: ebb0($v3: i32): ; nextln: br_table $v3, jt2
; nextln: br_table $v3, jt1
; nextln: trap user1 ; nextln: trap user1
; nextln: ; nextln:
; nextln: ebb1: ; nextln: ebb20:
; nextln: trap user2 ; nextln: trap user2
; nextln: ; nextln:
; nextln: ebb2: ; nextln: ebb30:
; nextln: trap user3 ; nextln: trap user3
; nextln: ; nextln:
; nextln: ebb3: ; nextln: ebb40:
; nextln: trap user4 ; nextln: trap user4
; nextln: } ; nextln: }

View File

@@ -6,7 +6,7 @@ ebb1:
return return
} }
; sameln: function %mini() native { ; sameln: function %mini() native {
; nextln: ebb0: ; nextln: ebb1:
; nextln: return ; nextln: return
; nextln: } ; nextln: }
@@ -17,7 +17,7 @@ ebb1:
return v1, v2 return v1, v2
} }
; sameln: function %r1() -> i32, f32 spiderwasm { ; sameln: function %r1() -> i32, f32 spiderwasm {
; nextln: ebb0: ; nextln: ebb1:
; nextln: $v1 = iconst.i32 3 ; nextln: $v1 = iconst.i32 3
; nextln: $v2 = f32const 0.0 ; nextln: $v2 = f32const 0.0
; nextln: return $v1, $v2 ; nextln: return $v1, $v2
@@ -30,12 +30,12 @@ function %signatures() {
fn8 = function %bar(i32) -> b1 fn8 = function %bar(i32) -> b1
} }
; sameln: function %signatures() native { ; sameln: function %signatures() native {
; nextln: $sig10 = () native ; check: $sig10 = () native
; nextln: $sig11 = (i32, f64) -> i32, b1 spiderwasm ; check: $sig11 = (i32, f64) -> i32, b1 spiderwasm
; nextln: sig2 = (i32) -> b1 native ; check: sig12 = (i32) -> b1 native
; nextln: $fn5 = $sig11 %foo ; check: $fn5 = $sig11 %foo
; nextln: $fn8 = sig2 %bar ; check: $fn8 = sig12 %bar
; nextln: } ; check: }
function %direct() { function %direct() {
fn0 = function %none() fn0 = function %none()

View File

@@ -30,8 +30,8 @@ ebb100(v20: i32):
jump ebb100(v1000) jump ebb100(v1000)
} }
; sameln: function %use_value() native { ; sameln: function %use_value() native {
; nextln: ebb0($v20: i32): ; nextln: ebb100($v20: i32):
; nextln: $v1000 = iadd_imm $v20, 5 ; nextln: $v1000 = iadd_imm $v20, 5
; nextln: $v200 = iadd $v20, $v1000 ; nextln: $v200 = iadd $v20, $v1000
; nextln: jump ebb0($v1000) ; nextln: jump ebb100($v1000)
; nextln: } ; nextln: }

View File

@@ -58,8 +58,8 @@ ebb0(v95: i32, v96: i32, v97: b1):
v98 = selectif.i32 eq v97, v95, v96 v98 = selectif.i32 eq v97, v95, v96
} }
; sameln: function %selectif() native { ; sameln: function %selectif() native {
; nextln: ebb0(v0: i32, v1: i32, v2: b1): ; nextln: ebb0(v95: i32, v96: i32, v97: b1):
; nextln: v3 = selectif.i32 eq v2, v0, v1 ; nextln: v98 = selectif.i32 eq v97, v95, v96
; nextln: } ; nextln: }
; Lane indexes. ; Lane indexes.
@@ -136,11 +136,11 @@ ebb0:
stack_store v2, ss2 stack_store v2, ss2
} }
; sameln: function %stack() native { ; sameln: function %stack() native {
; nextln: $ss10 = spill_slot 8 ; check: $ss2 = local 4
; nextln: $ss2 = local 4 ; check: $ss3 = incoming_arg 4, offset 8
; nextln: $ss3 = incoming_arg 4, offset 8 ; check: $ss4 = outgoing_arg 4
; nextln: $ss4 = outgoing_arg 4 ; check: $ss5 = emergency_slot 4
; nextln: $ss5 = emergency_slot 4 ; check: $ss10 = spill_slot 8
; check: ebb0: ; check: ebb0:
; nextln: $v1 = stack_load.i32 $ss10 ; nextln: $v1 = stack_load.i32 $ss10

View File

@@ -1,7 +1,7 @@
test verifier test verifier
function %deref_cycle() { function %deref_cycle() {
gv1 = deref(gv2)-32 ; error: deref cycle: [gv0, gv1] gv1 = deref(gv2)-32 ; error: deref cycle: [gv1, gv2]
gv2 = deref(gv1) gv2 = deref(gv1)
ebb1: ebb1:

View File

@@ -61,7 +61,7 @@ impl SubTest for TestDomtree {
for src_ebb in tail.split_whitespace() { for src_ebb in tail.split_whitespace() {
let ebb = match context.details.map.lookup_str(src_ebb) { let ebb = match context.details.map.lookup_str(src_ebb) {
Some(AnyEntity::Ebb(ebb)) => ebb, Some(AnyEntity::Ebb(ebb)) => ebb,
_ => return Err(format!("expected EBB: {}", src_ebb)), _ => return Err(format!("expected defined EBB, got {}", src_ebb)),
}; };
// Annotations say that `inst` is the idom of `ebb`. // Annotations say that `inst` is the idom of `ebb`.

View File

@@ -8,6 +8,7 @@ use ir::extfunc::ExtFuncData;
use ir::instructions::{InstructionData, CallInfo, BranchInfo}; use ir::instructions::{InstructionData, CallInfo, BranchInfo};
use ir::types; use ir::types;
use ir::{Ebb, Inst, Value, Type, SigRef, Signature, FuncRef, ValueList, ValueListPool}; use ir::{Ebb, Inst, Value, Type, SigRef, Signature, FuncRef, ValueList, ValueListPool};
use packed_option::ReservedValue;
use write::write_operands; use write::write_operands;
use std::fmt; use std::fmt;
use std::iter; use std::iter;
@@ -287,16 +288,6 @@ impl DataFlowGraph {
self.clear_results(dest_inst); self.clear_results(dest_inst);
} }
/// Create a new value alias.
///
/// Note that this function should only be called by the parser.
pub fn make_value_alias(&mut self, src: Value) -> Value {
let ty = self.value_type(src);
let data = ValueData::Alias { ty, original: src };
self.make_value(data)
}
} }
/// Where did a value come from? /// Where did a value come from?
@@ -370,14 +361,6 @@ impl DataFlowGraph {
self.insts.push(data) self.insts.push(data)
} }
/// Get the instruction reference that will be assigned to the next instruction created by
/// `make_inst`.
///
/// This is only really useful to the parser.
pub fn next_inst(&self) -> Inst {
self.insts.next_key()
}
/// Returns an object that displays `inst`. /// Returns an object that displays `inst`.
pub fn display_inst<'a, I: Into<Option<&'a TargetIsa>>>( pub fn display_inst<'a, I: Into<Option<&'a TargetIsa>>>(
&'a self, &'a self,
@@ -870,6 +853,91 @@ impl<'a> fmt::Display for DisplayInst<'a> {
} }
} }
/// Parser routines. These routines should not be used outside the parser.
impl DataFlowGraph {
/// Set the type of a value. This is only for use in the parser, which needs
/// to create invalid values for index padding which may be reassigned later.
#[cold]
fn set_value_type_for_parser(&mut self, v: Value, t: Type) {
debug_assert!(
self.value_type(v) == types::VOID,
"this function is only for assigning types to previously invalid values"
);
match self.values[v] {
ValueData::Inst { ref mut ty, .. } |
ValueData::Param { ref mut ty, .. } |
ValueData::Alias { ref mut ty, .. } => *ty = t,
}
}
/// Create result values for `inst`, reusing the provided detached values.
/// This is similar to `make_inst_results_reusing` except it's only for use
/// in the parser, which needs to reuse previously invalid values.
#[cold]
pub fn make_inst_results_for_parser(
&mut self,
inst: Inst,
ctrl_typevar: Type,
reuse: &[Value],
) -> usize {
// Get the call signature if this is a function call.
if let Some(sig) = self.call_signature(inst) {
debug_assert_eq!(self.insts[inst].opcode().constraints().fixed_results(), 0);
for res_idx in 0..self.signatures[sig].returns.len() {
let ty = self.signatures[sig].returns[res_idx].value_type;
if let Some(v) = reuse.get(res_idx) {
self.set_value_type_for_parser(*v, ty);
}
}
} else {
let constraints = self.insts[inst].opcode().constraints();
for res_idx in 0..constraints.fixed_results() {
let ty = constraints.result_type(res_idx, ctrl_typevar);
if let Some(v) = reuse.get(res_idx) {
self.set_value_type_for_parser(*v, ty);
}
}
}
self.make_inst_results_reusing(inst, ctrl_typevar, reuse.iter().map(|x| Some(*x)))
}
/// Similar to `append_ebb_param`, append a parameter with type `ty` to
/// `ebb`, but using value `val`. This is only for use by the parser to
/// create parameters with specific values.
#[cold]
pub fn append_ebb_param_for_parser(&mut self, ebb: Ebb, ty: Type, val: Value) {
let num = self.ebbs[ebb].params.push(val, &mut self.value_lists);
assert!(num <= u16::MAX as usize, "Too many parameters on EBB");
self.values[val] = ValueData::Param {
ty,
num: num as u16,
ebb,
};
}
/// Create a new value alias. This is only for use by the parser to create
/// aliases with specific values.
#[cold]
pub fn make_value_alias_for_parser(&mut self, src: Value, dest: Value) {
let ty = self.value_type(src);
let data = ValueData::Alias { ty, original: src };
self.values[dest] = data;
}
/// Create an invalid value, to pad the index space. This is only for use by
/// the parser to pad out the value index space.
#[cold]
pub fn make_invalid_value_for_parser(&mut self) {
let data = ValueData::Alias {
ty: types::VOID,
original: Value::reserved_value(),
};
self.make_value(data);
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -885,9 +953,7 @@ mod tests {
opcode: Opcode::Iconst, opcode: Opcode::Iconst,
imm: 0.into(), imm: 0.into(),
}; };
let next = dfg.next_inst();
let inst = dfg.make_inst(idata); let inst = dfg.make_inst(idata);
assert_eq!(next, inst);
dfg.make_inst_results(inst, types::I32); dfg.make_inst_results(inst, types::I32);
assert_eq!(inst.to_string(), "inst0"); assert_eq!(inst.to_string(), "inst0");

View File

@@ -65,6 +65,19 @@ entity_impl!(Inst, "inst");
pub struct StackSlot(u32); pub struct StackSlot(u32);
entity_impl!(StackSlot, "ss"); entity_impl!(StackSlot, "ss");
impl StackSlot {
/// Create a new stack slot reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<StackSlot> {
if n < u32::MAX {
Some(StackSlot(n))
} else {
None
}
}
}
/// An opaque reference to a global variable. /// An opaque reference to a global variable.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct GlobalVar(u32); pub struct GlobalVar(u32);
@@ -88,21 +101,61 @@ impl GlobalVar {
pub struct JumpTable(u32); pub struct JumpTable(u32);
entity_impl!(JumpTable, "jt"); entity_impl!(JumpTable, "jt");
impl JumpTable {
/// Create a new jump table reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<JumpTable> {
if n < u32::MAX {
Some(JumpTable(n))
} else {
None
}
}
}
/// A reference to an external function. /// A reference to an external function.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FuncRef(u32); pub struct FuncRef(u32);
entity_impl!(FuncRef, "fn"); entity_impl!(FuncRef, "fn");
impl FuncRef {
/// Create a new external function reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<FuncRef> {
if n < u32::MAX { Some(FuncRef(n)) } else { None }
}
}
/// A reference to a function signature. /// A reference to a function signature.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct SigRef(u32); pub struct SigRef(u32);
entity_impl!(SigRef, "sig"); entity_impl!(SigRef, "sig");
impl SigRef {
/// Create a new function signature reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<SigRef> {
if n < u32::MAX { Some(SigRef(n)) } else { None }
}
}
/// A reference to a heap. /// A reference to a heap.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Heap(u32); pub struct Heap(u32);
entity_impl!(Heap, "heap"); entity_impl!(Heap, "heap");
impl Heap {
/// Create a new heap reference from its number.
///
/// This method is for use by the parser.
pub fn with_number(n: u32) -> Option<Heap> {
if n < u32::MAX { Some(Heap(n)) } else { None }
}
}
/// A reference to any of the entities defined in this module. /// A reference to any of the entities defined in this module.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum AnyEntity { pub enum AnyEntity {

View File

@@ -8,7 +8,7 @@ use ir::{Type, StackSlot};
use packed_option::PackedOption; use packed_option::PackedOption;
use std::cmp; use std::cmp;
use std::fmt; use std::fmt;
use std::ops::Index; use std::ops::{Index, IndexMut};
use std::str::FromStr; use std::str::FromStr;
/// The size of an object on the stack, or the size of a stack frame. /// The size of an object on the stack, or the size of a stack frame.
@@ -228,6 +228,12 @@ impl Index<StackSlot> for StackSlots {
} }
} }
impl IndexMut<StackSlot> for StackSlots {
fn index_mut(&mut self, ss: StackSlot) -> &mut StackSlotData {
&mut self.slots[ss]
}
}
/// Higher-level stack frame manipulation functions. /// Higher-level stack frame manipulation functions.
impl StackSlots { impl StackSlots {
/// Create a new spill slot for spilling values of type `ty`. /// Create a new spill slot for spilling values of type `ty`.

File diff suppressed because it is too large Load Diff

View File

@@ -1,31 +1,20 @@
//! Source map for translating source entity names to parsed entities. //! Source map associating entities with their source locations.
//! //!
//! When the parser reads in a source file, entities like instructions, EBBs, and values get new //! When the parser reads in a source file, it records the locations of the
//! entity numbers. The parser maintains a mapping from the entity names in the source to the final //! definitions of entities like instructions, EBBs, and values.
//! entity references.
//! //!
//! The `SourceMap` struct defined in this module makes the same mapping available to parser //! The `SourceMap` struct defined in this module makes this mapping available
//! clients. //! to parser clients.
use cretonne::entity::EntityRef;
use cretonne::ir::entities::AnyEntity; use cretonne::ir::entities::AnyEntity;
use cretonne::ir::{StackSlot, GlobalVar, Heap, JumpTable, Ebb, Value, SigRef, FuncRef}; use cretonne::ir::{StackSlot, GlobalVar, Heap, JumpTable, Ebb, Value, SigRef, FuncRef};
use error::{Result, Location}; use error::{Result, Location};
use lexer::split_entity_name; use lexer::split_entity_name;
use std::collections::HashMap; use std::collections::HashMap;
/// Mapping from source entity names to entity references that are valid in the parsed function. /// Mapping from entity names to source locations.
#[derive(Debug)] #[derive(Debug)]
pub struct SourceMap { pub struct SourceMap {
values: HashMap<Value, Value>, // vNN
ebbs: HashMap<Ebb, Ebb>, // ebbNN
stack_slots: HashMap<u32, StackSlot>, // ssNN
global_vars: HashMap<u32, GlobalVar>, // gvNN
heaps: HashMap<u32, Heap>, // heapNN
signatures: HashMap<u32, SigRef>, // sigNN
functions: HashMap<u32, FuncRef>, // fnNN
jump_tables: HashMap<u32, JumpTable>, // jtNN
// Store locations for entities, including instructions. // Store locations for entities, including instructions.
locations: HashMap<AnyEntity, Location>, locations: HashMap<AnyEntity, Location>,
} }
@@ -33,43 +22,43 @@ pub struct SourceMap {
/// Read-only interface which is exposed outside the parser crate. /// Read-only interface which is exposed outside the parser crate.
impl SourceMap { impl SourceMap {
/// Look up a value entity by its source number. /// Look up a value entity by its source number.
pub fn get_value(&self, src: Value) -> Option<Value> { pub fn contains_value(&self, v: Value) -> bool {
self.values.get(&src).cloned() self.locations.contains_key(&v.into())
} }
/// Look up a EBB entity by its source number. /// Look up a EBB entity by its source number.
pub fn get_ebb(&self, src: Ebb) -> Option<Ebb> { pub fn contains_ebb(&self, ebb: Ebb) -> bool {
self.ebbs.get(&src).cloned() self.locations.contains_key(&ebb.into())
} }
/// Look up a stack slot entity by its source number. /// Look up a stack slot entity by its source number.
pub fn get_ss(&self, src_num: u32) -> Option<StackSlot> { pub fn contains_ss(&self, ss: StackSlot) -> bool {
self.stack_slots.get(&src_num).cloned() self.locations.contains_key(&ss.into())
} }
/// Look up a global variable entity by its source number. /// Look up a global variable entity by its source number.
pub fn get_gv(&self, src_num: u32) -> Option<GlobalVar> { pub fn contains_gv(&self, gv: GlobalVar) -> bool {
self.global_vars.get(&src_num).cloned() self.locations.contains_key(&gv.into())
} }
/// Look up a heap entity by its source number. /// Look up a heap entity by its source number.
pub fn get_heap(&self, src_num: u32) -> Option<Heap> { pub fn contains_heap(&self, heap: Heap) -> bool {
self.heaps.get(&src_num).cloned() self.locations.contains_key(&heap.into())
} }
/// Look up a signature entity by its source number. /// Look up a signature entity by its source number.
pub fn get_sig(&self, src_num: u32) -> Option<SigRef> { pub fn contains_sig(&self, sig: SigRef) -> bool {
self.signatures.get(&src_num).cloned() self.locations.contains_key(&sig.into())
} }
/// Look up a function entity by its source number. /// Look up a function entity by its source number.
pub fn get_fn(&self, src_num: u32) -> Option<FuncRef> { pub fn contains_fn(&self, fn_: FuncRef) -> bool {
self.functions.get(&src_num).cloned() self.locations.contains_key(&fn_.into())
} }
/// Look up a jump table entity by its source number. /// Look up a jump table entity by its source number.
pub fn get_jt(&self, src_num: u32) -> Option<JumpTable> { pub fn contains_jt(&self, jt: JumpTable) -> bool {
self.jump_tables.get(&src_num).cloned() self.locations.contains_key(&jt.into())
} }
/// Look up an entity by source name. /// Look up an entity by source name.
@@ -77,193 +66,120 @@ impl SourceMap {
pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> { pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> {
split_entity_name(name).and_then(|(ent, num)| match ent { split_entity_name(name).and_then(|(ent, num)| match ent {
"v" => { "v" => {
Value::with_number(num) Value::with_number(num).and_then(|v| if !self.contains_value(v) {
.and_then(|v| self.get_value(v)) None
.map(AnyEntity::Value) } else {
Some(v.into())
})
} }
"ebb" => { "ebb" => {
Ebb::with_number(num).and_then(|e| self.get_ebb(e)).map( Ebb::with_number(num).and_then(|ebb| if !self.contains_ebb(ebb) {
AnyEntity::Ebb, None
) } else {
Some(ebb.into())
})
}
"ss" => {
StackSlot::with_number(num).and_then(|ss| if !self.contains_ss(ss) {
None
} else {
Some(ss.into())
})
}
"gv" => {
GlobalVar::with_number(num).and_then(|gv| if !self.contains_gv(gv) {
None
} else {
Some(gv.into())
})
}
"heap" => {
Heap::with_number(num).and_then(|heap| if !self.contains_heap(heap) {
None
} else {
Some(heap.into())
})
}
"sig" => {
SigRef::with_number(num).and_then(|sig| if !self.contains_sig(sig) {
None
} else {
Some(sig.into())
})
}
"fn" => {
FuncRef::with_number(num).and_then(|fn_| if !self.contains_fn(fn_) {
None
} else {
Some(fn_.into())
})
}
"jt" => {
JumpTable::with_number(num).and_then(|jt| if !self.contains_jt(jt) {
None
} else {
Some(jt.into())
})
} }
"ss" => self.get_ss(num).map(AnyEntity::StackSlot),
"gv" => self.get_gv(num).map(AnyEntity::GlobalVar),
"heap" => self.get_heap(num).map(AnyEntity::Heap),
"sig" => self.get_sig(num).map(AnyEntity::SigRef),
"fn" => self.get_fn(num).map(AnyEntity::FuncRef),
"jt" => self.get_jt(num).map(AnyEntity::JumpTable),
_ => None, _ => None,
}) })
} }
/// Get the source location where an entity was defined. /// Get the source location where an entity was defined.
/// This looks up entities in the parsed function, not the source entity numbers.
pub fn location(&self, entity: AnyEntity) -> Option<Location> { pub fn location(&self, entity: AnyEntity) -> Option<Location> {
self.locations.get(&entity).cloned() self.locations.get(&entity).cloned()
} }
/// Rewrite an Ebb reference.
pub fn rewrite_ebb(&self, ebb: &mut Ebb, loc: AnyEntity) -> Result<()> {
match self.get_ebb(*ebb) {
Some(new) => {
*ebb = new;
Ok(())
}
None => {
err!(
self.location(loc).unwrap_or_default(),
"undefined reference: {}",
ebb
)
}
}
} }
/// Rewrite a value reference. impl SourceMap {
pub fn rewrite_value(&self, val: &mut Value, loc: AnyEntity) -> Result<()> { /// Create a new empty `SourceMap`.
match self.get_value(*val) { pub fn new() -> Self {
Some(new) => { Self { locations: HashMap::new() }
*val = new;
Ok(())
}
None => {
err!(
self.location(loc).unwrap_or_default(),
"undefined reference: {}",
val
)
}
}
} }
/// Rewrite a slice of value references. /// Define the value `entity`.
pub fn rewrite_values(&self, vals: &mut [Value], loc: AnyEntity) -> Result<()> { pub fn def_value(&mut self, entity: Value, loc: &Location) -> Result<()> {
for val in vals {
self.rewrite_value(val, loc)?;
}
Ok(())
}
/// Rewrite a `GlobalVar` reference.
pub fn rewrite_gv(&self, gv: &mut GlobalVar, loc: AnyEntity) -> Result<()> {
match self.get_gv(gv.index() as u32) {
Some(new) => {
*gv = new;
Ok(())
}
None => {
err!(
self.location(loc).unwrap_or_default(),
"undefined reference: {}",
gv
)
}
}
}
}
/// Interface for mutating a source map.
///
/// This interface is provided for the parser itself, it is not made available outside the crate.
pub trait MutableSourceMap {
fn new() -> Self;
/// Define a value mapping from the source name `src` to the final `entity`.
fn def_value(&mut self, src: Value, entity: Value, loc: &Location) -> Result<()>;
fn def_ebb(&mut self, src: Ebb, entity: Ebb, loc: &Location) -> Result<()>;
fn def_ss(&mut self, src_num: u32, entity: StackSlot, loc: &Location) -> Result<()>;
fn def_gv(&mut self, src_num: u32, entity: GlobalVar, loc: &Location) -> Result<()>;
fn def_heap(&mut self, src_num: u32, entity: Heap, loc: &Location) -> Result<()>;
fn def_sig(&mut self, src_num: u32, entity: SigRef, loc: &Location) -> Result<()>;
fn def_fn(&mut self, src_num: u32, entity: FuncRef, loc: &Location) -> Result<()>;
fn def_jt(&mut self, src_num: u32, entity: JumpTable, loc: &Location) -> Result<()>;
/// Define an entity without an associated source number. This can be used for instructions
/// whose numbers never appear in source, or implicitly defined signatures.
fn def_entity(&mut self, entity: AnyEntity, loc: &Location) -> Result<()>;
}
impl MutableSourceMap for SourceMap {
fn new() -> Self {
Self {
values: HashMap::new(),
ebbs: HashMap::new(),
stack_slots: HashMap::new(),
global_vars: HashMap::new(),
heaps: HashMap::new(),
signatures: HashMap::new(),
functions: HashMap::new(),
jump_tables: HashMap::new(),
locations: HashMap::new(),
}
}
fn def_value(&mut self, src: Value, entity: Value, loc: &Location) -> Result<()> {
if self.values.insert(src, entity).is_some() {
err!(loc, "duplicate value: {}", src)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_ebb(&mut self, src: Ebb, entity: Ebb, loc: &Location) -> Result<()> { /// Define the ebb `entity`.
if self.ebbs.insert(src, entity).is_some() { pub fn def_ebb(&mut self, entity: Ebb, loc: &Location) -> Result<()> {
err!(loc, "duplicate EBB: {}", src)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_ss(&mut self, src_num: u32, entity: StackSlot, loc: &Location) -> Result<()> { /// Define the stack slot `entity`.
if self.stack_slots.insert(src_num, entity).is_some() { pub fn def_ss(&mut self, entity: StackSlot, loc: &Location) -> Result<()> {
err!(loc, "duplicate stack slot: ss{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_gv(&mut self, src_num: u32, entity: GlobalVar, loc: &Location) -> Result<()> { /// Define the global variable `entity`.
if self.global_vars.insert(src_num, entity).is_some() { pub fn def_gv(&mut self, entity: GlobalVar, loc: &Location) -> Result<()> {
err!(loc, "duplicate global variable: gv{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_heap(&mut self, src_num: u32, entity: Heap, loc: &Location) -> Result<()> { /// Define the heap `entity`.
if self.heaps.insert(src_num, entity).is_some() { pub fn def_heap(&mut self, entity: Heap, loc: &Location) -> Result<()> {
err!(loc, "duplicate heap: heap{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_sig(&mut self, src_num: u32, entity: SigRef, loc: &Location) -> Result<()> { /// Define the signature `entity`.
if self.signatures.insert(src_num, entity).is_some() { pub fn def_sig(&mut self, entity: SigRef, loc: &Location) -> Result<()> {
err!(loc, "duplicate signature: sig{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_fn(&mut self, src_num: u32, entity: FuncRef, loc: &Location) -> Result<()> { /// Define the external function `entity`.
if self.functions.insert(src_num, entity).is_some() { pub fn def_fn(&mut self, entity: FuncRef, loc: &Location) -> Result<()> {
err!(loc, "duplicate function: fn{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_jt(&mut self, src_num: u32, entity: JumpTable, loc: &Location) -> Result<()> { /// Define the jump table `entity`.
if self.jump_tables.insert(src_num, entity).is_some() { pub fn def_jt(&mut self, entity: JumpTable, loc: &Location) -> Result<()> {
err!(loc, "duplicate jump table: jt{}", src_num)
} else {
self.def_entity(entity.into(), loc) self.def_entity(entity.into(), loc)
} }
}
fn def_entity(&mut self, entity: AnyEntity, loc: &Location) -> Result<()> { /// Define an entity. This can be used for instructions whose numbers never
/// appear in source, or implicitly defined signatures.
pub fn def_entity(&mut self, entity: AnyEntity, loc: &Location) -> Result<()> {
if self.locations.insert(entity, *loc).is_some() { if self.locations.insert(entity, *loc).is_some() {
err!(loc, "duplicate entity: {}", entity) err!(loc, "duplicate entity: {}", entity)
} else { } else {
@@ -290,11 +206,11 @@ mod tests {
assert_eq!(map.lookup_str("v0"), None); assert_eq!(map.lookup_str("v0"), None);
assert_eq!(map.lookup_str("ss1"), None); assert_eq!(map.lookup_str("ss1"), None);
assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss0"); assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss10");
assert_eq!(map.lookup_str("jt10").unwrap().to_string(), "jt0"); assert_eq!(map.lookup_str("jt10").unwrap().to_string(), "jt10");
assert_eq!(map.lookup_str("ebb0").unwrap().to_string(), "ebb0"); assert_eq!(map.lookup_str("ebb0").unwrap().to_string(), "ebb0");
assert_eq!(map.lookup_str("v4").unwrap().to_string(), "v0"); assert_eq!(map.lookup_str("v4").unwrap().to_string(), "v4");
assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v1"); assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v7");
assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v2"); assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v10");
} }
} }

View File

@@ -36,8 +36,7 @@ pub struct Details<'a> {
pub location: Location, pub location: Location,
/// Annotation comments that appeared inside or after the function. /// Annotation comments that appeared inside or after the function.
pub comments: Vec<Comment<'a>>, pub comments: Vec<Comment<'a>>,
/// Mapping of source entity numbers to parsed entity numbers. /// Mapping of entity numbers to source locations.
/// Source locations of parsed entities.
pub map: SourceMap, pub map: SourceMap,
} }