Path: blob/main/cranelift/reader/src/sourcemap.rs
2450 views
//! Source map associating entities with their source locations.1//!2//! When the parser reads in a source file, it records the locations of the3//! definitions of entities like instructions, blocks, and values.4//!5//! The `SourceMap` struct defined in this module makes this mapping available6//! to parser clients.78use crate::error::{Location, ParseResult};9use crate::lexer::split_entity_name;10use cranelift_codegen::ir::entities::{AnyEntity, DynamicType};11use cranelift_codegen::ir::{12Block, Constant, DynamicStackSlot, FuncRef, GlobalValue, JumpTable, MemoryType, SigRef,13StackSlot, Value,14};15use std::collections::HashMap;1617/// Mapping from entity names to source locations.18#[derive(Debug, Default)]19pub struct SourceMap {20// Store locations for entities, including instructions.21locations: HashMap<AnyEntity, Location>,22}2324/// Read-only interface which is exposed outside the parser crate.25impl SourceMap {26/// Look up a value entity.27pub fn contains_value(&self, v: Value) -> bool {28self.locations.contains_key(&v.into())29}3031/// Look up a block entity.32pub fn contains_block(&self, block: Block) -> bool {33self.locations.contains_key(&block.into())34}3536/// Look up a stack slot entity.37pub fn contains_ss(&self, ss: StackSlot) -> bool {38self.locations.contains_key(&ss.into())39}4041/// Look up a dynamic stack slot entity.42pub fn contains_dss(&self, dss: DynamicStackSlot) -> bool {43self.locations.contains_key(&dss.into())44}4546/// Look up a global value entity.47pub fn contains_gv(&self, gv: GlobalValue) -> bool {48self.locations.contains_key(&gv.into())49}5051/// Look up a signature entity.52pub fn contains_sig(&self, sig: SigRef) -> bool {53self.locations.contains_key(&sig.into())54}5556/// Look up a function entity.57pub fn contains_fn(&self, fn_: FuncRef) -> bool {58self.locations.contains_key(&fn_.into())59}6061/// Look up a jump table entity.62pub fn contains_jt(&self, jt: JumpTable) -> bool {63self.locations.contains_key(&jt.into())64}6566/// Look up a constant entity.67pub fn contains_constant(&self, c: Constant) -> bool {68self.locations.contains_key(&c.into())69}7071/// Look up an entity by source name.72/// Returns the entity reference corresponding to `name`, if it exists.73pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> {74split_entity_name(name).and_then(|(ent, num)| match ent {75"v" => Value::with_number(num).and_then(|v| {76if !self.contains_value(v) {77None78} else {79Some(v.into())80}81}),82"block" => Block::with_number(num).and_then(|block| {83if !self.contains_block(block) {84None85} else {86Some(block.into())87}88}),89"ss" => StackSlot::with_number(num).and_then(|ss| {90if !self.contains_ss(ss) {91None92} else {93Some(ss.into())94}95}),96"gv" => GlobalValue::with_number(num).and_then(|gv| {97if !self.contains_gv(gv) {98None99} else {100Some(gv.into())101}102}),103"sig" => SigRef::with_number(num).and_then(|sig| {104if !self.contains_sig(sig) {105None106} else {107Some(sig.into())108}109}),110"fn" => FuncRef::with_number(num).and_then(|fn_| {111if !self.contains_fn(fn_) {112None113} else {114Some(fn_.into())115}116}),117"jt" => JumpTable::with_number(num).and_then(|jt| {118if !self.contains_jt(jt) {119None120} else {121Some(jt.into())122}123}),124_ => None,125})126}127128/// Get the source location where an entity was defined.129pub fn location(&self, entity: AnyEntity) -> Option<Location> {130self.locations.get(&entity).cloned()131}132}133134impl SourceMap {135/// Create a new empty `SourceMap`.136pub fn new() -> Self {137Self {138locations: HashMap::new(),139}140}141142/// Define the value `entity`.143pub fn def_value(&mut self, entity: Value, loc: Location) -> ParseResult<()> {144self.def_entity(entity.into(), loc)145}146147/// Define the block `entity`.148pub fn def_block(&mut self, entity: Block, loc: Location) -> ParseResult<()> {149self.def_entity(entity.into(), loc)150}151152/// Define the stack slot `entity`.153pub fn def_ss(&mut self, entity: StackSlot, loc: Location) -> ParseResult<()> {154self.def_entity(entity.into(), loc)155}156157/// Define the dynamic stack slot `entity`.158pub fn def_dss(&mut self, entity: DynamicStackSlot, loc: Location) -> ParseResult<()> {159self.def_entity(entity.into(), loc)160}161162/// Define the dynamic type `entity`.163pub fn def_dt(&mut self, entity: DynamicType, loc: Location) -> ParseResult<()> {164self.def_entity(entity.into(), loc)165}166167/// Define the global value `entity`.168pub fn def_gv(&mut self, entity: GlobalValue, loc: Location) -> ParseResult<()> {169self.def_entity(entity.into(), loc)170}171172/// Define the memory type `entity`.173pub fn def_mt(&mut self, entity: MemoryType, loc: Location) -> ParseResult<()> {174self.def_entity(entity.into(), loc)175}176177/// Define the signature `entity`.178pub fn def_sig(&mut self, entity: SigRef, loc: Location) -> ParseResult<()> {179self.def_entity(entity.into(), loc)180}181182/// Define the external function `entity`.183pub fn def_fn(&mut self, entity: FuncRef, loc: Location) -> ParseResult<()> {184self.def_entity(entity.into(), loc)185}186187/// Define the jump table `entity`.188pub fn def_jt(&mut self, entity: JumpTable, loc: Location) -> ParseResult<()> {189self.def_entity(entity.into(), loc)190}191192/// Define the jump table `entity`.193pub fn def_constant(&mut self, entity: Constant, loc: Location) -> ParseResult<()> {194self.def_entity(entity.into(), loc)195}196197/// Define an entity. This can be used for instructions whose numbers never198/// appear in source, or implicitly defined signatures.199pub fn def_entity(&mut self, entity: AnyEntity, loc: Location) -> ParseResult<()> {200if self.locations.insert(entity, loc).is_some() {201err!(loc, "duplicate entity: {}", entity)202} else {203Ok(())204}205}206}207208#[cfg(test)]209mod tests {210use crate::{ParseOptions, parse_test};211212#[test]213fn details() {214let tf = parse_test(215"function %detail() {216ss10 = explicit_slot 13217block0(v4: i32, v7: i32):218v10 = iadd v4, v7219}",220ParseOptions::default(),221)222.unwrap();223let map = &tf.functions[0].1.map;224225assert_eq!(map.lookup_str("v0"), None);226assert_eq!(map.lookup_str("ss1"), None);227assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss10");228assert_eq!(map.lookup_str("block0").unwrap().to_string(), "block0");229assert_eq!(map.lookup_str("v4").unwrap().to_string(), "v4");230assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v7");231assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v10");232}233}234235236