Skip to content

Commit

Permalink
feature: draft LiftImportsCrossCtxStage scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Nov 29, 2024
1 parent c33c696 commit 5b56840
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
10 changes: 10 additions & 0 deletions hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ impl<'a> ComponentBuilder<'a> {
self
}

/// Replace the imports of the [Component] being built.
pub fn with_imports(mut self, imports: BTreeMap<FunctionIdent, ComponentImport>) -> Self {
self.imports = imports;
self
}

/// Add `module` to the set of modules to link into the final [Component]
///
/// Returns `Err` if a module with the same name already exists
Expand Down Expand Up @@ -445,6 +451,10 @@ impl<'a> ComponentBuilder<'a> {
self.exports.insert(name, export);
}

pub fn imports(&self) -> &BTreeMap<FunctionIdent, ComponentImport> {
&self.imports
}

pub fn exports(&self) -> &BTreeMap<InterfaceFunctionIdent, ComponentExport> {
&self.exports
}
Expand Down
60 changes: 53 additions & 7 deletions midenc-compile/src/stages/lift_cross_ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
//! Lifting the imports into the Miden ABI for the cross-context calls
use midenc_hir::pass::AnalysisManager;
use midenc_session::Session;
use std::collections::BTreeMap;

use midenc_hir::{pass::AnalysisManager, ComponentBuilder, ComponentImport, FunctionIdent};
use midenc_session::{DiagnosticsHandler, Session};

use super::LinkerInput;
use crate::{stage::Stage, CompilerResult};

/// Generates lifting for imports for the cross-context calls according to the Miden ABI.
///
/// For every imported function call put the arguments on the stack or the advice provider and load
/// the result from the stack/advice provider into the memory according to the pointer after the
/// call.
/// For each component import ensure a module for each interface that has a lifting function i.e. a
/// function that takes the arguments according to the Wasm CABI, lowers them to the cross-context
/// Miden ABI, calls the imported function with the lowered arguments, takes the result
/// (cross-context Miden ABI), and lifts it to the Wasm CABI.
/// The calls to the component import is switched to the generated lifting function.
pub struct LiftImportsCrossCtxStage;

impl Stage for LiftImportsCrossCtxStage {
Expand All @@ -21,8 +25,50 @@ impl Stage for LiftImportsCrossCtxStage {
&mut self,
input: Self::Input,
_analyses: &mut AnalysisManager,
_session: &Session,
session: &Session,
) -> CompilerResult<Self::Output> {
Ok(input)
let component = if let LinkerInput::Hir(component) = input {
component
} else {
return Ok(input);
};

let mut component_builder = ComponentBuilder::load(*component, &session.diagnostics);

let mut lifted_imports: BTreeMap<FunctionIdent, ComponentImport> = BTreeMap::new();
let imports = component_builder.imports().clone();
for (id, import) in imports.into_iter() {
if let ComponentImport::MidenAbiImport(_) = import {
// skip imports that are already lifted
lifted_imports.insert(id, import);
continue;
}
let new_import = generate_lifting_function(
&mut component_builder,
id,
import,
&session.diagnostics,
)?;
lifted_imports.insert(id, new_import);

// TODO: find all the calls to the component import and replace them with the generated lifting function
}

let component_builder = component_builder.with_imports(lifted_imports);

let component = component_builder.build();
// dbg!(&component.imports());
// dbg!(&component.modules().len());
Ok(LinkerInput::Hir(component.into()))
}
}

fn generate_lifting_function(
_component_builder: &mut ComponentBuilder<'_>,
_id: FunctionIdent,
import: ComponentImport,
_diagnostics: &DiagnosticsHandler,
) -> CompilerResult<ComponentImport> {
// TODO: implement the lifting function generation
Ok(import)
}

0 comments on commit 5b56840

Please sign in to comment.