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

@@ -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");

View File

@@ -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 {

View File

@@ -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

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
//! 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");
}
}

View File

@@ -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,
}