Skip to content

Commit

Permalink
test: add note script compilation test;
Browse files Browse the repository at this point in the history
Remove ImportMetadata (function digest) since we get digests from the
linked libraries.
  • Loading branch information
greenhat committed Oct 22, 2024
1 parent 6f3d32a commit 228bfa1
Show file tree
Hide file tree
Showing 31 changed files with 3,159 additions and 829 deletions.
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/[email protected]"),
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/[email protected]")]
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!()
}
6 changes: 6 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,12 @@ use miden_stdlib_sys::{Felt, Word};
#[derive(Copy, Clone)]
pub struct AccountId(Felt);

impl AccountId {
pub fn as_felt(&self) -> Felt {
self.0
}
}

impl From<AccountId> for Felt {
fn from(account_id: AccountId) -> Felt {
account_id.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(component
;; Component Imports
(lower (("miden:add-package/[email protected]" #add) (digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi wasm) (param u32) (param u32) (result u32)))) (#miden:add-package/[email protected] #add)
(lower (("miden:add-package/[email protected]" #add) (type (func (abi wasm) (param u32) (param u32) (result u32)))) (#miden:add-package/[email protected] #add)

;; Modules
(module #inc_wasm_component
Expand Down
20 changes: 10 additions & 10 deletions tests/integration/expected/rust_sdk/basic_wallet.hir
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
;; Modules
(module #basic_wallet
;; Data Segments
(data (mut) (offset 1048576) 0x0100000001000000010000000100000001000000010000000100000002000000)
(data (mut) (offset 1048576) 0x010000000100000001000000010000000100000001000000010000000100000002000000)

;; Constants
(const (id 0) 0x00100000)
Expand Down Expand Up @@ -37,7 +37,7 @@

(func (export #__rust_alloc) (param i32) (param i32) (result i32)
(block 0 (param v0 i32) (param v1 i32)
(let (v3 i32) (const.i32 1048616))
(let (v3 i32) (const.i32 1048620))
(let (v4 i32) (call #<miden_sdk_alloc::BumpAlloc as core::alloc::global::GlobalAlloc>::alloc v3 v1 v0))
(br (block 1 v4)))

Expand All @@ -56,7 +56,7 @@
(func (export #__rust_realloc)
(param i32) (param i32) (param i32) (param i32) (result i32)
(block 0 (param v0 i32) (param v1 i32) (param v2 i32) (param v3 i32)
(let (v5 i32) (const.i32 1048616))
(let (v5 i32) (const.i32 1048620))
(let (v6 i32) (call #<miden_sdk_alloc::BumpAlloc as core::alloc::global::GlobalAlloc>::alloc v5 v2 v3))
(let (v7 i1) (eq v6 0))
(let (v8 i32) (zext v7))
Expand Down Expand Up @@ -87,7 +87,7 @@

(func (export #__rust_alloc_zeroed) (param i32) (param i32) (result i32)
(block 0 (param v0 i32) (param v1 i32)
(let (v3 i32) (const.i32 1048616))
(let (v3 i32) (const.i32 1048620))
(let (v4 i32) (call #<miden_sdk_alloc::BumpAlloc as core::alloc::global::GlobalAlloc>::alloc v3 v1 v0))
(let (v5 i1) (eq v4 0))
(let (v6 i32) (zext v5))
Expand Down Expand Up @@ -477,14 +477,14 @@
(let (v166 (ptr i64)) (inttoptr v164))
(let (v167 i64) (load v166))
(let (v168 u32) (bitcast v162))
(let (v169 u32) (add.checked v168 1048608))
(let (v169 u32) (add.checked v168 1048612))
(let (v170 u32) (mod.unchecked v169 4))
(assertz 250 v170)
(let (v171 (ptr i64)) (inttoptr v169))
(store v171 v167)
(let (v172 (ptr i32)) (global.symbol #__stack_pointer))
(store v172 v4)
(let (v173 i32) (const.i32 1048608))
(let (v173 i32) (const.i32 1048612))
(ret v173))
)

Expand Down Expand Up @@ -536,7 +536,7 @@
(block 6
(let (v9 i32) (const.i32 0))
(let (v10 u32) (bitcast v9))
(let (v11 u32) (add.checked v10 1048620))
(let (v11 u32) (add.checked v10 1048624))
(let (v12 (ptr u8)) (inttoptr v11))
(let (v13 u8) (load v12))
(let (v14 i32) (zext v13))
Expand All @@ -551,7 +551,7 @@
(block 0
(let (v0 i32) (const.i32 0))
(let (v1 u32) (bitcast v0))
(let (v2 u32) (add.checked v1 1048621))
(let (v2 u32) (add.checked v1 1048625))
(let (v3 (ptr u8)) (inttoptr v2))
(let (v4 u8) (load v3))
(let (v5 i32) (zext v4))
Expand All @@ -571,7 +571,7 @@
(let (v9 u32) (bitcast v8))
(let (v10 u8) (trunc v9))
(let (v11 u32) (bitcast v7))
(let (v12 u32) (add.checked v11 1048621))
(let (v12 u32) (add.checked v11 1048625))
(let (v13 (ptr u8)) (inttoptr v12))
(store v13 v10)
(br (block 2)))
Expand Down Expand Up @@ -1163,7 +1163,7 @@
(block 5
(let (v8 i32) (const.i32 0))
(let (v9 u32) (bitcast v8))
(let (v10 u32) (add.checked v9 1048620))
(let (v10 u32) (add.checked v9 1048624))
(let (v11 (ptr u8)) (inttoptr v10))
(let (v12 u8) (load v11))
(let (v13 i32) (zext v12))
Expand Down
Loading

0 comments on commit 228bfa1

Please sign in to comment.