Skip to content

Commit

Permalink
Merge pull request #348 from 0xPolygonMiden/greenhat/i314-note-script…
Browse files Browse the repository at this point in the history
…-compilation

[3/x] Add note script compilation test
  • Loading branch information
greenhat authored Oct 30, 2024
2 parents ab3947b + 3d980af commit 9e1322f
Show file tree
Hide file tree
Showing 33 changed files with 4,458 additions and 2,123 deletions.
8 changes: 6 additions & 2 deletions codegen/masm/src/masm/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ impl midenc_hir::formatter::PrettyPrint for Module {
}
}

for (i, import) in self.imports.iter().enumerate() {
let mut imports_sorted = self.imports.iter().collect::<Vec<_>>();
imports_sorted.sort();
for (i, import) in imports_sorted.iter().enumerate() {
if i > 0 {
doc += nl();
}
Expand Down Expand Up @@ -266,7 +268,9 @@ impl midenc_hir::formatter::PrettyPrint for Module {
doc += nl() + nl();
}

for (i, func) in self.functions.iter().enumerate() {
let mut functions_sorted = self.functions.iter().collect::<Vec<_>>();
functions_sorted.sort_by_key(|f| f.name);
for (i, func) in functions_sorted.iter().enumerate() {
if i > 0 {
doc += nl();
}
Expand Down
16 changes: 2 additions & 14 deletions frontend-wasm/src/component/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ fn inline(

#[cfg(test)]
mod tests {
use miden_core::crypto::hash::RpoDigest;
use midenc_hir::{FunctionType, Ident, InterfaceFunctionIdent, InterfaceIdent, Symbol};
use midenc_hir_type::Type;

use super::*;
use crate::{component::StaticModuleIndex, config::ImportMetadata, test_utils::test_context};
use crate::{component::StaticModuleIndex, test_utils::test_context};

#[test]
fn translate_simple() {
Expand Down Expand Up @@ -182,19 +181,8 @@ mod tests {
interface: InterfaceIdent::from_full_ident("miden:add/add@1.0.0"),
function: Symbol::intern("add"),
};
let import_metadata = [(
interface_function_ident,
ImportMetadata {
digest: RpoDigest::default(),
},
)]
.into_iter()
.collect();

let config = WasmTranslationConfig {
import_metadata,
..Default::default()
};
let config = WasmTranslationConfig::default();
let (mut component_types_builder, parsed_component) =
parse(&config, &wasm, &context.session).unwrap();
let component_translation =
Expand Down
13 changes: 0 additions & 13 deletions frontend-wasm/src/component/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,11 @@ impl<'a, 'data> ComponentTranslator<'a, 'data> {
interface: InterfaceIdent::from_full_ident(&full_interface_name),
function: Symbol::intern(import_func_name),
};
let Some(import_metadata) = self.config.import_metadata.get(&interface_function) else {
return Err(self
.session
.diagnostics
.diagnostic(Severity::Error)
.with_message(format!(
"wasm error: import metadata for interface function \
{interface_function:?} not found"
))
.into_report());
};
let lifted_func_ty = convert_lifted_func_ty(&signature, &self.component_types);

let component_import =
midenc_hir::ComponentImport::CanonAbiImport(CanonAbiImport::new(
interface_function,
lifted_func_ty,
import_metadata.digest,
self.translate_canonical_options(options)?,
));
Ok(Some(component_import))
Expand Down
25 changes: 1 addition & 24 deletions frontend-wasm/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
use alloc::{borrow::Cow, collections::BTreeMap, fmt};

use miden_core::crypto::hash::RpoDigest;
use midenc_hir::InterfaceFunctionIdent;

/// Represents Miden VM codegen metadata for a function import.
/// This struct will have more fields in the future e.g. where the function
/// for this MAST hash is located (to be loaded by the VM)
#[derive(Clone)]
pub struct ImportMetadata {
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
}
impl fmt::Debug for ImportMetadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entry(&"digest", &self.digest.to_hex()).finish()
}
}
use alloc::borrow::Cow;

/// Configuration for the WASM translation.
#[derive(Debug)]
Expand All @@ -33,11 +16,6 @@ pub struct WasmTranslationConfig {

/// Whether or not to retain DWARF sections in compiled modules.
pub parse_wasm_debuginfo: bool,

/// Import metadata for MAST hashes, calling convention, of
/// each imported function. Having it here might be a temporary solution,
/// later we might want to move it to Wasm custom section.
pub import_metadata: BTreeMap<InterfaceFunctionIdent, ImportMetadata>,
}

impl Default for WasmTranslationConfig {
Expand All @@ -47,7 +25,6 @@ impl Default for WasmTranslationConfig {
override_name: None,
generate_native_debuginfo: false,
parse_wasm_debuginfo: true,
import_metadata: Default::default(),
}
}
}
10 changes: 0 additions & 10 deletions hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::collections::BTreeMap;
use core::ops::{Deref, DerefMut};

use indexmap::IndexMap;
use miden_core::crypto::hash::RpoDigest;
use midenc_hir_type::Abi;

use self::formatter::PrettyPrint;
Expand Down Expand Up @@ -32,8 +31,6 @@ pub struct CanonAbiImport {
pub interface_function: InterfaceFunctionIdent,
/// The component(lifted) type of the imported function
function_ty: FunctionType,
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
/// Any options associated with this import
pub options: CanonicalOptions,
}
Expand All @@ -42,14 +39,12 @@ impl CanonAbiImport {
pub fn new(
interface_function: InterfaceFunctionIdent,
function_ty: FunctionType,
digest: RpoDigest,
options: CanonicalOptions,
) -> Self {
assert_eq!(function_ty.abi, Abi::Wasm, "expected Abi::Wasm function type ABI");
Self {
interface_function,
function_ty,
digest,
options,
}
}
Expand Down Expand Up @@ -111,13 +106,8 @@ impl formatter::PrettyPrint for ComponentImport {
ComponentImport::MidenAbiImport(_import) => "".to_string(),
};

let digest = match self {
ComponentImport::CanonAbiImport(import) => format!("(digest {})", import.digest),
ComponentImport::MidenAbiImport(_) => "".to_string(),
};
const_text("(")
+ text(name)
+ text(digest)
+ const_text(" ")
+ const_text("(")
+ const_text("type")
Expand Down
15 changes: 15 additions & 0 deletions midenc-session/src/libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ pub enum LibraryKind {
Mast,
/// A source-form MASM library, using the standard project layout
Masm,
// A Miden package (MASP)
Masp,
}
impl fmt::Display for LibraryKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mast => f.write_str("mast"),
Self::Masm => f.write_str("masm"),
Self::Masp => f.write_str("masp"),
}
}
}
Expand All @@ -47,6 +50,7 @@ impl FromStr for LibraryKind {
match s {
"mast" | "masl" => Ok(Self::Mast),
"masm" => Ok(Self::Masm),
"masp" => Ok(Self::Masp),
_ => Err(()),
}
}
Expand Down Expand Up @@ -107,6 +111,9 @@ impl LinkLibrary {
path.display()
))
}),
LibraryKind::Masp => {
todo!("Should be implemented as part of the https://github.com/0xPolygonMiden/compiler/issues/346")
}
}
}

Expand Down Expand Up @@ -149,6 +156,14 @@ impl LinkLibrary {
)));
}
}
LibraryKind::Masp => {
if !path.is_file() {
return Err(Report::msg(format!(
"unable to load Miden Assembly package from '{}': not a file",
path.display()
)));
}
}
}
return Ok(path);
}
Expand Down
6 changes: 6 additions & 0 deletions sdk/base-sys/src/bindings/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use alloc::vec::Vec;

use miden_stdlib_sys::Felt;

use super::CoreAsset;

#[link(wasm_import_module = "miden:core-import/note@1.0.0")]
extern "C" {
#[link_name = "get-inputs"]
Expand Down Expand Up @@ -39,3 +41,7 @@ pub fn get_inputs() -> Vec<Felt> {
}
inputs
}

pub fn get_assets() -> Vec<CoreAsset> {
todo!()
}
7 changes: 7 additions & 0 deletions sdk/base-sys/src/bindings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use miden_stdlib_sys::{Felt, Word};
#[derive(Copy, Clone)]
pub struct AccountId(Felt);

impl AccountId {
#[inline(always)]
pub const fn as_felt(&self) -> Felt {
self.0
}
}

impl From<AccountId> for Felt {
fn from(account_id: AccountId) -> Felt {
account_id.0
Expand Down
Loading

0 comments on commit 9e1322f

Please sign in to comment.