Declare constants in the function preamble

This allows us to give names to constants in the constant pool and then use these names in the function body. The original behavior, specifiying the constant value as an instruction immediate, is still supported as a shortcut but some filetests had to change since the canonical way of printing the CLIF constants is now in the preamble.
This commit is contained in:
Andrew Brown
2020-03-20 14:08:03 -07:00
parent 7d88384c0f
commit 0672d1dc0f
14 changed files with 255 additions and 74 deletions

View File

@@ -10,7 +10,7 @@ use crate::error::{Location, ParseResult};
use crate::lexer::split_entity_name;
use cranelift_codegen::ir::entities::AnyEntity;
use cranelift_codegen::ir::{
Block, FuncRef, GlobalValue, Heap, JumpTable, SigRef, StackSlot, Table, Value,
Block, Constant, FuncRef, GlobalValue, Heap, JumpTable, SigRef, StackSlot, Table, Value,
};
use std::collections::HashMap;
@@ -68,6 +68,11 @@ impl SourceMap {
self.locations.contains_key(&jt.into())
}
/// Look up a constant entity.
pub fn contains_constant(&self, c: Constant) -> bool {
self.locations.contains_key(&c.into())
}
/// Look up an entity by source name.
/// Returns the entity reference corresponding to `name`, if it exists.
pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> {
@@ -198,6 +203,11 @@ impl SourceMap {
self.def_entity(entity.into(), loc)
}
/// Define the jump table `entity`.
pub fn def_constant(&mut self, entity: Constant, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
/// 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) -> ParseResult<()> {