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:
@@ -11,8 +11,8 @@ isa intel haswell
|
||||
|
||||
; Tests from binary32.cton affected by allones_funcaddrs.
|
||||
function %I32() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
ebb0:
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ isa intel haswell
|
||||
|
||||
; Tests from binary64.cton affected by allones_funcaddrs.
|
||||
function %I64() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
ebb0:
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ isa intel haswell
|
||||
;
|
||||
|
||||
function %I32() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
gv0 = globalsym %some_gv
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ isa intel haswell
|
||||
|
||||
; Tests for i64 instructions.
|
||||
function %I64() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
gv0 = globalsym %some_gv
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ isa intel haswell
|
||||
|
||||
; Tests for i64 instructions.
|
||||
function %I64() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
gv0 = globalsym %some_gv
|
||||
|
||||
@@ -641,8 +641,8 @@ ebb1:
|
||||
; encodings which are chosen by default. Switching to non-REX encodings should
|
||||
; be done by an instruction shrinking pass.
|
||||
function %I32() {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
ss0 = incoming_arg 8, offset 0
|
||||
ss1 = incoming_arg 1024, offset -1024
|
||||
|
||||
@@ -76,9 +76,9 @@ ebb0(v0: i32, v999: i64):
|
||||
; check: $ebb0(
|
||||
; nextln: trap heap_oob
|
||||
; check: ebb1:
|
||||
; nextln: v2 = iconst.i64 0
|
||||
; nextln: v3 = load.f32 v2+16
|
||||
; nextln: return v3
|
||||
; nextln: v1 = iconst.i64 0
|
||||
; nextln: v2 = load.f32 v1+16
|
||||
; nextln: return v2
|
||||
; nextln: }
|
||||
v1 = heap_addr.i64 heap0, v0, 0x1000_0001
|
||||
v2 = load.f32 v1+16
|
||||
|
||||
@@ -3,8 +3,8 @@ test binemit
|
||||
isa riscv
|
||||
|
||||
function %RV32I(i32 link [%x1]) -> i32 link [%x1] {
|
||||
fn0 = function %foo()
|
||||
sig0 = ()
|
||||
fn0 = function %foo()
|
||||
|
||||
ebb0(v9999: i32):
|
||||
[-,%x10] v1 = iconst.i32 1
|
||||
|
||||
@@ -27,7 +27,7 @@ function %parse_encoding(i32 [%x5]) -> i32 [%x10] {
|
||||
; check: sig5 = () -> f32 [0] native
|
||||
|
||||
; 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
|
||||
; nextln: fn0 = sig6 %bar
|
||||
|
||||
|
||||
@@ -25,22 +25,22 @@ ebb3(v30: i32):
|
||||
}
|
||||
; sameln:function %multiple_blocks(i32) -> i32 {
|
||||
; nextln: ebb0(v0: i32):
|
||||
; nextln: v2 = iconst.i32 1
|
||||
; nextln: v3 = iconst.i32 2
|
||||
; nextln: v4 = iadd v2, v3
|
||||
; nextln: v9 = iadd v2, v4
|
||||
; nextln: v11 = iconst.i32 1
|
||||
; nextln: v12 = iconst.i32 2
|
||||
; nextln: v13 = iadd v11, v12
|
||||
; nextln: v31 = iadd v11, v13
|
||||
; nextln: jump ebb1(v0)
|
||||
; nextln:
|
||||
; nextln: ebb1(v1: i32):
|
||||
; nextln: brz v1, ebb2(v1)
|
||||
; nextln: v5 = isub v1, v2
|
||||
; nextln: brz v5, ebb3(v5)
|
||||
; nextln: v6 = isub v1, v2
|
||||
; nextln: jump ebb1(v6)
|
||||
; nextln: ebb1(v10: i32):
|
||||
; nextln: brz v10, ebb2(v10)
|
||||
; nextln: v15 = isub v10, v11
|
||||
; nextln: brz v15, ebb3(v15)
|
||||
; nextln: v14 = isub v10, v11
|
||||
; nextln: jump ebb1(v14)
|
||||
; nextln:
|
||||
; nextln: ebb2(v7: i32):
|
||||
; nextln: return v7
|
||||
; nextln: ebb2(v20: i32):
|
||||
; nextln: return v20
|
||||
; nextln:
|
||||
; nextln: ebb3(v8: i32):
|
||||
; nextln: jump ebb1(v8)
|
||||
; nextln: ebb3(v30: i32):
|
||||
; nextln: jump ebb1(v30)
|
||||
; nextln: }
|
||||
|
||||
@@ -33,23 +33,23 @@ ebb4(v30: i32):
|
||||
; nextln: v2 = iconst.i32 1
|
||||
; nextln: v3 = iconst.i32 2
|
||||
; nextln: v4 = iadd v2, v3
|
||||
; nextln: v8 = iconst.i32 1
|
||||
; nextln: v12 = iconst.i32 1
|
||||
; nextln: jump ebb1(v0)
|
||||
; nextln:
|
||||
; nextln: ebb1(v1: i32):
|
||||
; nextln: v5 = isub v1, v2
|
||||
; nextln: v9 = iadd.i32 v8, v5
|
||||
; nextln: v15 = iadd.i32 v12, v5
|
||||
; nextln: jump ebb2(v5, v5)
|
||||
; nextln:
|
||||
; nextln: ebb2(v6: i32, v7: i32):
|
||||
; nextln: brz v7, ebb3(v6)
|
||||
; nextln: v10 = isub v7, v8
|
||||
; nextln: jump ebb2(v6, v10)
|
||||
; nextln: ebb2(v10: i32, v11: i32):
|
||||
; nextln: brz v11, ebb3(v10)
|
||||
; nextln: v13 = isub v11, v12
|
||||
; nextln: jump ebb2(v10, v13)
|
||||
; nextln:
|
||||
; nextln: ebb3(v11: i32):
|
||||
; nextln: brz v11, ebb4(v11)
|
||||
; nextln: jump ebb1(v11)
|
||||
; nextln: ebb3(v20: i32):
|
||||
; nextln: brz v20, ebb4(v20)
|
||||
; nextln: jump ebb1(v20)
|
||||
; nextln:
|
||||
; nextln: ebb4(v12: i32):
|
||||
; nextln: return v12
|
||||
; nextln: ebb4(v30: i32):
|
||||
; nextln: return v30
|
||||
; nextln: }
|
||||
|
||||
@@ -95,19 +95,18 @@ ebb40:
|
||||
trap user4
|
||||
}
|
||||
; sameln: function %jumptable(i32) native {
|
||||
; nextln: jt0 = jump_table 0
|
||||
; nextln: jt1 = jump_table 0, 0, ebb0, ebb3, ebb1, ebb2
|
||||
; nextln:
|
||||
; nextln: ebb0($v3: i32):
|
||||
; nextln: br_table $v3, jt1
|
||||
; check: jt2 = jump_table 0, 0, ebb10, ebb40, ebb20, ebb30
|
||||
; check: jt200 = jump_table 0
|
||||
; check: ebb10($v3: i32):
|
||||
; nextln: br_table $v3, jt2
|
||||
; nextln: trap user1
|
||||
; nextln:
|
||||
; nextln: ebb1:
|
||||
; nextln: ebb20:
|
||||
; nextln: trap user2
|
||||
; nextln:
|
||||
; nextln: ebb2:
|
||||
; nextln: ebb30:
|
||||
; nextln: trap user3
|
||||
; nextln:
|
||||
; nextln: ebb3:
|
||||
; nextln: ebb40:
|
||||
; nextln: trap user4
|
||||
; nextln: }
|
||||
|
||||
@@ -6,7 +6,7 @@ ebb1:
|
||||
return
|
||||
}
|
||||
; sameln: function %mini() native {
|
||||
; nextln: ebb0:
|
||||
; nextln: ebb1:
|
||||
; nextln: return
|
||||
; nextln: }
|
||||
|
||||
@@ -17,7 +17,7 @@ ebb1:
|
||||
return v1, v2
|
||||
}
|
||||
; sameln: function %r1() -> i32, f32 spiderwasm {
|
||||
; nextln: ebb0:
|
||||
; nextln: ebb1:
|
||||
; nextln: $v1 = iconst.i32 3
|
||||
; nextln: $v2 = f32const 0.0
|
||||
; nextln: return $v1, $v2
|
||||
@@ -30,12 +30,12 @@ function %signatures() {
|
||||
fn8 = function %bar(i32) -> b1
|
||||
}
|
||||
; sameln: function %signatures() native {
|
||||
; nextln: $sig10 = () native
|
||||
; nextln: $sig11 = (i32, f64) -> i32, b1 spiderwasm
|
||||
; nextln: sig2 = (i32) -> b1 native
|
||||
; nextln: $fn5 = $sig11 %foo
|
||||
; nextln: $fn8 = sig2 %bar
|
||||
; nextln: }
|
||||
; check: $sig10 = () native
|
||||
; check: $sig11 = (i32, f64) -> i32, b1 spiderwasm
|
||||
; check: sig12 = (i32) -> b1 native
|
||||
; check: $fn5 = $sig11 %foo
|
||||
; check: $fn8 = sig12 %bar
|
||||
; check: }
|
||||
|
||||
function %direct() {
|
||||
fn0 = function %none()
|
||||
|
||||
@@ -30,8 +30,8 @@ ebb100(v20: i32):
|
||||
jump ebb100(v1000)
|
||||
}
|
||||
; sameln: function %use_value() native {
|
||||
; nextln: ebb0($v20: i32):
|
||||
; nextln: ebb100($v20: i32):
|
||||
; nextln: $v1000 = iadd_imm $v20, 5
|
||||
; nextln: $v200 = iadd $v20, $v1000
|
||||
; nextln: jump ebb0($v1000)
|
||||
; nextln: jump ebb100($v1000)
|
||||
; nextln: }
|
||||
|
||||
@@ -58,8 +58,8 @@ ebb0(v95: i32, v96: i32, v97: b1):
|
||||
v98 = selectif.i32 eq v97, v95, v96
|
||||
}
|
||||
; sameln: function %selectif() native {
|
||||
; nextln: ebb0(v0: i32, v1: i32, v2: b1):
|
||||
; nextln: v3 = selectif.i32 eq v2, v0, v1
|
||||
; nextln: ebb0(v95: i32, v96: i32, v97: b1):
|
||||
; nextln: v98 = selectif.i32 eq v97, v95, v96
|
||||
; nextln: }
|
||||
|
||||
; Lane indexes.
|
||||
@@ -136,11 +136,11 @@ ebb0:
|
||||
stack_store v2, ss2
|
||||
}
|
||||
; sameln: function %stack() native {
|
||||
; nextln: $ss10 = spill_slot 8
|
||||
; nextln: $ss2 = local 4
|
||||
; nextln: $ss3 = incoming_arg 4, offset 8
|
||||
; nextln: $ss4 = outgoing_arg 4
|
||||
; nextln: $ss5 = emergency_slot 4
|
||||
; check: $ss2 = local 4
|
||||
; check: $ss3 = incoming_arg 4, offset 8
|
||||
; check: $ss4 = outgoing_arg 4
|
||||
; check: $ss5 = emergency_slot 4
|
||||
; check: $ss10 = spill_slot 8
|
||||
|
||||
; check: ebb0:
|
||||
; nextln: $v1 = stack_load.i32 $ss10
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
test verifier
|
||||
|
||||
function %deref_cycle() {
|
||||
gv1 = deref(gv2)-32 ; error: deref cycle: [gv0, gv1]
|
||||
gv1 = deref(gv2)-32 ; error: deref cycle: [gv1, gv2]
|
||||
gv2 = deref(gv1)
|
||||
|
||||
ebb1:
|
||||
|
||||
@@ -61,7 +61,7 @@ impl SubTest for TestDomtree {
|
||||
for src_ebb in tail.split_whitespace() {
|
||||
let ebb = match context.details.map.lookup_str(src_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`.
|
||||
|
||||
@@ -8,6 +8,7 @@ use ir::extfunc::ExtFuncData;
|
||||
use ir::instructions::{InstructionData, CallInfo, BranchInfo};
|
||||
use ir::types;
|
||||
use ir::{Ebb, Inst, Value, Type, SigRef, Signature, FuncRef, ValueList, ValueListPool};
|
||||
use packed_option::ReservedValue;
|
||||
use write::write_operands;
|
||||
use std::fmt;
|
||||
use std::iter;
|
||||
@@ -287,16 +288,6 @@ impl DataFlowGraph {
|
||||
|
||||
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?
|
||||
@@ -370,14 +361,6 @@ impl DataFlowGraph {
|
||||
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`.
|
||||
pub fn display_inst<'a, I: Into<Option<&'a TargetIsa>>>(
|
||||
&'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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -885,9 +953,7 @@ mod tests {
|
||||
opcode: Opcode::Iconst,
|
||||
imm: 0.into(),
|
||||
};
|
||||
let next = dfg.next_inst();
|
||||
let inst = dfg.make_inst(idata);
|
||||
assert_eq!(next, inst);
|
||||
|
||||
dfg.make_inst_results(inst, types::I32);
|
||||
assert_eq!(inst.to_string(), "inst0");
|
||||
|
||||
@@ -65,6 +65,19 @@ entity_impl!(Inst, "inst");
|
||||
pub struct StackSlot(u32);
|
||||
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.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct GlobalVar(u32);
|
||||
@@ -88,21 +101,61 @@ impl GlobalVar {
|
||||
pub struct JumpTable(u32);
|
||||
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.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct FuncRef(u32);
|
||||
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.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SigRef(u32);
|
||||
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.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Heap(u32);
|
||||
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.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum AnyEntity {
|
||||
|
||||
@@ -8,7 +8,7 @@ use ir::{Type, StackSlot};
|
||||
use packed_option::PackedOption;
|
||||
use std::cmp;
|
||||
use std::fmt;
|
||||
use std::ops::Index;
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::str::FromStr;
|
||||
|
||||
/// 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.
|
||||
impl StackSlots {
|
||||
/// Create a new spill slot for spilling values of type `ty`.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
//! entity numbers. The parser maintains a mapping from the entity names in the source to the final
|
||||
//! entity references.
|
||||
//! When the parser reads in a source file, it records the locations of the
|
||||
//! definitions of entities like instructions, EBBs, and values.
|
||||
//!
|
||||
//! The `SourceMap` struct defined in this module makes the same mapping available to parser
|
||||
//! clients.
|
||||
//! The `SourceMap` struct defined in this module makes this mapping available
|
||||
//! to parser clients.
|
||||
|
||||
use cretonne::entity::EntityRef;
|
||||
use cretonne::ir::entities::AnyEntity;
|
||||
use cretonne::ir::{StackSlot, GlobalVar, Heap, JumpTable, Ebb, Value, SigRef, FuncRef};
|
||||
use error::{Result, Location};
|
||||
use lexer::split_entity_name;
|
||||
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)]
|
||||
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.
|
||||
locations: HashMap<AnyEntity, Location>,
|
||||
}
|
||||
@@ -33,43 +22,43 @@ pub struct SourceMap {
|
||||
/// Read-only interface which is exposed outside the parser crate.
|
||||
impl SourceMap {
|
||||
/// Look up a value entity by its source number.
|
||||
pub fn get_value(&self, src: Value) -> Option<Value> {
|
||||
self.values.get(&src).cloned()
|
||||
pub fn contains_value(&self, v: Value) -> bool {
|
||||
self.locations.contains_key(&v.into())
|
||||
}
|
||||
|
||||
/// Look up a EBB entity by its source number.
|
||||
pub fn get_ebb(&self, src: Ebb) -> Option<Ebb> {
|
||||
self.ebbs.get(&src).cloned()
|
||||
pub fn contains_ebb(&self, ebb: Ebb) -> bool {
|
||||
self.locations.contains_key(&ebb.into())
|
||||
}
|
||||
|
||||
/// Look up a stack slot entity by its source number.
|
||||
pub fn get_ss(&self, src_num: u32) -> Option<StackSlot> {
|
||||
self.stack_slots.get(&src_num).cloned()
|
||||
pub fn contains_ss(&self, ss: StackSlot) -> bool {
|
||||
self.locations.contains_key(&ss.into())
|
||||
}
|
||||
|
||||
/// Look up a global variable entity by its source number.
|
||||
pub fn get_gv(&self, src_num: u32) -> Option<GlobalVar> {
|
||||
self.global_vars.get(&src_num).cloned()
|
||||
pub fn contains_gv(&self, gv: GlobalVar) -> bool {
|
||||
self.locations.contains_key(&gv.into())
|
||||
}
|
||||
|
||||
/// Look up a heap entity by its source number.
|
||||
pub fn get_heap(&self, src_num: u32) -> Option<Heap> {
|
||||
self.heaps.get(&src_num).cloned()
|
||||
pub fn contains_heap(&self, heap: Heap) -> bool {
|
||||
self.locations.contains_key(&heap.into())
|
||||
}
|
||||
|
||||
/// Look up a signature entity by its source number.
|
||||
pub fn get_sig(&self, src_num: u32) -> Option<SigRef> {
|
||||
self.signatures.get(&src_num).cloned()
|
||||
pub fn contains_sig(&self, sig: SigRef) -> bool {
|
||||
self.locations.contains_key(&sig.into())
|
||||
}
|
||||
|
||||
/// Look up a function entity by its source number.
|
||||
pub fn get_fn(&self, src_num: u32) -> Option<FuncRef> {
|
||||
self.functions.get(&src_num).cloned()
|
||||
pub fn contains_fn(&self, fn_: FuncRef) -> bool {
|
||||
self.locations.contains_key(&fn_.into())
|
||||
}
|
||||
|
||||
/// Look up a jump table entity by its source number.
|
||||
pub fn get_jt(&self, src_num: u32) -> Option<JumpTable> {
|
||||
self.jump_tables.get(&src_num).cloned()
|
||||
pub fn contains_jt(&self, jt: JumpTable) -> bool {
|
||||
self.locations.contains_key(&jt.into())
|
||||
}
|
||||
|
||||
/// Look up an entity by source name.
|
||||
@@ -77,193 +66,120 @@ impl SourceMap {
|
||||
pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> {
|
||||
split_entity_name(name).and_then(|(ent, num)| match ent {
|
||||
"v" => {
|
||||
Value::with_number(num)
|
||||
.and_then(|v| self.get_value(v))
|
||||
.map(AnyEntity::Value)
|
||||
Value::with_number(num).and_then(|v| if !self.contains_value(v) {
|
||||
None
|
||||
} else {
|
||||
Some(v.into())
|
||||
})
|
||||
}
|
||||
"ebb" => {
|
||||
Ebb::with_number(num).and_then(|e| self.get_ebb(e)).map(
|
||||
AnyEntity::Ebb,
|
||||
)
|
||||
Ebb::with_number(num).and_then(|ebb| if !self.contains_ebb(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,
|
||||
})
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
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.
|
||||
pub fn rewrite_value(&self, val: &mut Value, loc: AnyEntity) -> Result<()> {
|
||||
match self.get_value(*val) {
|
||||
Some(new) => {
|
||||
*val = new;
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
err!(
|
||||
self.location(loc).unwrap_or_default(),
|
||||
"undefined reference: {}",
|
||||
val
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrite a slice of value references.
|
||||
pub fn rewrite_values(&self, vals: &mut [Value], loc: AnyEntity) -> 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(),
|
||||
}
|
||||
impl SourceMap {
|
||||
/// Create a new empty `SourceMap`.
|
||||
pub fn new() -> Self {
|
||||
Self { 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)
|
||||
}
|
||||
/// Define the value `entity`.
|
||||
pub fn def_value(&mut self, entity: Value, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_ebb(&mut self, src: Ebb, entity: Ebb, loc: &Location) -> Result<()> {
|
||||
if self.ebbs.insert(src, entity).is_some() {
|
||||
err!(loc, "duplicate EBB: {}", src)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the ebb `entity`.
|
||||
pub fn def_ebb(&mut self, entity: Ebb, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_ss(&mut self, src_num: u32, entity: StackSlot, loc: &Location) -> Result<()> {
|
||||
if self.stack_slots.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate stack slot: ss{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the stack slot `entity`.
|
||||
pub fn def_ss(&mut self, entity: StackSlot, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_gv(&mut self, src_num: u32, entity: GlobalVar, loc: &Location) -> Result<()> {
|
||||
if self.global_vars.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate global variable: gv{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the global variable `entity`.
|
||||
pub fn def_gv(&mut self, entity: GlobalVar, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_heap(&mut self, src_num: u32, entity: Heap, loc: &Location) -> Result<()> {
|
||||
if self.heaps.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate heap: heap{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the heap `entity`.
|
||||
pub fn def_heap(&mut self, entity: Heap, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_sig(&mut self, src_num: u32, entity: SigRef, loc: &Location) -> Result<()> {
|
||||
if self.signatures.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate signature: sig{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the signature `entity`.
|
||||
pub fn def_sig(&mut self, entity: SigRef, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_fn(&mut self, src_num: u32, entity: FuncRef, loc: &Location) -> Result<()> {
|
||||
if self.functions.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate function: fn{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the external function `entity`.
|
||||
pub fn def_fn(&mut self, entity: FuncRef, loc: &Location) -> Result<()> {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
|
||||
fn def_jt(&mut self, src_num: u32, entity: JumpTable, loc: &Location) -> Result<()> {
|
||||
if self.jump_tables.insert(src_num, entity).is_some() {
|
||||
err!(loc, "duplicate jump table: jt{}", src_num)
|
||||
} else {
|
||||
self.def_entity(entity.into(), loc)
|
||||
}
|
||||
/// Define the jump table `entity`.
|
||||
pub fn def_jt(&mut self, entity: JumpTable, loc: &Location) -> Result<()> {
|
||||
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() {
|
||||
err!(loc, "duplicate entity: {}", entity)
|
||||
} else {
|
||||
@@ -290,11 +206,11 @@ mod tests {
|
||||
|
||||
assert_eq!(map.lookup_str("v0"), None);
|
||||
assert_eq!(map.lookup_str("ss1"), None);
|
||||
assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss0");
|
||||
assert_eq!(map.lookup_str("jt10").unwrap().to_string(), "jt0");
|
||||
assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss10");
|
||||
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("v4").unwrap().to_string(), "v0");
|
||||
assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v1");
|
||||
assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v2");
|
||||
assert_eq!(map.lookup_str("v4").unwrap().to_string(), "v4");
|
||||
assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v7");
|
||||
assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v10");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,7 @@ pub struct Details<'a> {
|
||||
pub location: Location,
|
||||
/// Annotation comments that appeared inside or after the function.
|
||||
pub comments: Vec<Comment<'a>>,
|
||||
/// Mapping of source entity numbers to parsed entity numbers.
|
||||
/// Source locations of parsed entities.
|
||||
/// Mapping of entity numbers to source locations.
|
||||
pub map: SourceMap,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user