Skip to content

Commit

Permalink
feat: test version without id in NamedProcedure
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Oct 6, 2023
1 parent 9b0fcfd commit b24ec84
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
20 changes: 3 additions & 17 deletions assembly/src/assembler/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,21 +433,7 @@ impl ModuleContext {
pub fn complete_proc(&mut self, code: CodeBlock) {
let proc_context = self.proc_stack.pop().expect("no procedures");

// build an ID for the procedure as follows:
// - for module without library path (#anon): None
// - for module with library path:
// --- for exported procedures: Some(hash("module_path::proc_name"))
// --- for internal procedures: Some(hash("module_path::proc_index"))
let proc_id = if self.path == LibraryPath::anon_path() {
None
} else if proc_context.is_export {
Some(ProcedureId::from_name(&proc_context.name, &self.path))
} else {
let proc_idx = self.compiled_procs.len() as u16;
Some(ProcedureId::from_index(proc_idx, &self.path))
};

let proc = proc_context.into_procedure(proc_id, code);
let proc = proc_context.into_procedure(code);
self.callset.append(proc.callset());
self.compiled_procs.push(proc);
}
Expand Down Expand Up @@ -564,14 +550,14 @@ impl ProcedureContext {
&self.name
}

pub fn into_procedure(self, id: Option<ProcedureId>, code_root: CodeBlock) -> NamedProcedure {
pub fn into_procedure(self, code_root: CodeBlock) -> NamedProcedure {
let Self {
name,
is_export,
num_locals,
callset,
} = self;

NamedProcedure::new(id, name, is_export, num_locals as u32, code_root, callset)
NamedProcedure::new(name, is_export, num_locals as u32, code_root, callset)
}
}
26 changes: 24 additions & 2 deletions assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,20 @@ impl Assembler {
// - a procedure is exported from the module, or
// - a procedure is present in the combined callset - i.e., it is an internal procedure
// which has been invoked via a local call instruction.
for proc in module_procs.into_iter() {
for (proc_index, proc) in module_procs.into_iter().enumerate() {
if proc.is_export() {
proc_roots.push(proc.mast_root());
}

if proc.is_export() || module_callset.contains(&proc.mast_root()) {
// build the procedure ID if this module has the library path
let proc_id = build_procedure_id(path, &proc, proc_index);

// this is safe because we fail if the cache is borrowed.
self.proc_cache
.try_borrow_mut()
.map_err(|_| AssemblyError::InvalidCacheLock)?
.insert(proc)?;
.insert(proc, proc_id)?;
}
}

Expand Down Expand Up @@ -490,3 +493,22 @@ fn combine_spans(spans: &mut Vec<CodeBlock>) -> CodeBlock {
});
CodeBlock::new_span_with_decorators(ops, decorators)
}

/// Builds a procedure ID based on the provided parameters.
///
/// Returns [ProcedureId] if `path` is provided, [None] otherwise.
fn build_procedure_id(
path: Option<&LibraryPath>,
proc: &NamedProcedure,
proc_index: usize,
) -> Option<ProcedureId> {
let mut proc_id = None;
if let Some(path) = path {
if proc.is_export() {
proc_id = Some(ProcedureId::from_name(proc.name(), path));
} else {
proc_id = Some(ProcedureId::from_index(proc_index as u16, path))
}
}
proc_id
}
18 changes: 11 additions & 7 deletions assembly/src/assembler/procedure_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ impl ProcedureCache {
/// - A procedure with the same ID is already in the cache.
/// - A procedure with the same MAST root but conflicting procedure metadata exists in the
/// cache.
pub fn insert(&mut self, proc: NamedProcedure) -> Result<(), AssemblyError> {
pub fn insert(
&mut self,
proc: NamedProcedure,
id: Option<ProcedureId>,
) -> Result<(), AssemblyError> {
// if a procedure with the same id is already in the cache, return an error
if proc.id().is_some_and(|id| self.contains_id(&id)) {
return Err(AssemblyError::duplicate_proc_id(&proc.id().unwrap()));
if id.is_some_and(|id| self.contains_id(&id)) {
return Err(AssemblyError::duplicate_proc_id(&id.unwrap()));
}

// If the entry is `Vacant` then insert the Procedure. If the procedure with the same MAST
Expand All @@ -77,15 +81,15 @@ impl ProcedureCache {
if proc.num_locals() != cached_proc.num_locals() {
Err(AssemblyError::conflicting_num_locals(proc.name()))
} else {
if let Some(id) = proc.id() {
self.proc_id_map.insert(*id, proc.mast_root());
if let Some(id) = id {
self.proc_id_map.insert(id, proc.mast_root());
}
Ok(())
}
}
Entry::Vacant(entry) => {
if let Some(id) = proc.id() {
self.proc_id_map.insert(*id, proc.mast_root());
if let Some(id) = id {
self.proc_id_map.insert(id, proc.mast_root());
}
entry.insert(proc.into_inner());
Ok(())
Expand Down
8 changes: 0 additions & 8 deletions assembly/src/procedures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ impl Procedure {
/// - A set of MAST roots of procedures which are invoked from this procedure.
#[derive(Clone, Debug)]
pub struct NamedProcedure {
id: Option<ProcedureId>,
name: ProcedureName,
is_export: bool,
procedure: Procedure,
Expand All @@ -71,15 +70,13 @@ impl NamedProcedure {
// --------------------------------------------------------------------------------------------
/// Returns a new [Procedure] instantiated with the specified properties.
pub fn new(
id: Option<ProcedureId>,
name: ProcedureName,
is_export: bool,
num_locals: u32,
code: CodeBlock,
callset: CallSet,
) -> Self {
NamedProcedure {
id,
name,
is_export,
procedure: Procedure {
Expand All @@ -93,11 +90,6 @@ impl NamedProcedure {
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------

/// Returns ID of this procedure.
pub fn id(&self) -> &Option<ProcedureId> {
&self.id
}

/// Returns a label of this procedure.
pub fn name(&self) -> &ProcedureName {
&self.name
Expand Down

0 comments on commit b24ec84

Please sign in to comment.