Skip to content

Commit

Permalink
refactor!: move Package to miden-package in the VM repo
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Oct 25, 2024
1 parent 54d3ccf commit 7e3be95
Show file tree
Hide file tree
Showing 29 changed files with 319 additions and 638 deletions.
38 changes: 28 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ thiserror = { package = "miden-thiserror", version = "1.0" }
toml = { version = "0.8", features = ["preserve_order"] }
derive_more = "0.99"
indexmap = "2.2"
miden-assembly = { version = "0.10.3" }
miden-core = { version = "0.10.3" }
# miden-assembly = { version = "0.10.3" }
# miden-core = { version = "0.10.3" }
miden-assembly = { version = "0.10.5", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "e84368bb5b0629e367e615973fa7dd4823a872b7" }
miden-core = { version = "0.10.5", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "e84368bb5b0629e367e615973fa7dd4823a872b7" }
miden-parsing = "0.1"
miden-processor = { version = "0.10.3" }
miden-stdlib = { version = "0.10.3", features = ["with-debug-info"] }
# miden-processor = { version = "0.10.3" }
# miden-stdlib = { version = "0.10.3", features = ["with-debug-info"] }
miden-processor = { version = "0.10.5", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "e84368bb5b0629e367e615973fa7dd4823a872b7" }
miden-stdlib = { version = "0.10.5", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "e84368bb5b0629e367e615973fa7dd4823a872b7", features = ["with-debug-info"] }
miden-package = { version = "0.10.5", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "e84368bb5b0629e367e615973fa7dd4823a872b7" }
#miden-assembly = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-core = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-processor = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
Expand Down
1 change: 1 addition & 0 deletions codegen/masm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ midenc-hir = { workspace = true, features = ["serde"] }
midenc-hir-analysis.workspace = true
midenc-hir-transform.workspace = true
midenc-session = { workspace = true, features = ["serde"] }
miden-package.workspace = true
paste.workspace = true
petgraph.workspace = true
rustc-hash.workspace = true
Expand Down
9 changes: 9 additions & 0 deletions codegen/masm/src/compiler/mast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ impl Emit for MastArtifact {
}
}
}

impl From<MastArtifact> for miden_package::MastArtifact {
fn from(mast: MastArtifact) -> Self {
match mast {
MastArtifact::Executable(prog) => miden_package::MastArtifact::Executable(prog),
MastArtifact::Library(lib) => miden_package::MastArtifact::Library(lib),
}
}
}
2 changes: 0 additions & 2 deletions codegen/masm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod compiler;
mod convert;
mod emulator;
mod masm;
mod packaging;
#[cfg(test)]
mod tests;

Expand All @@ -24,5 +23,4 @@ pub use self::{
Emulator, EmulatorEvent, InstructionPointer, WatchMode, Watchpoint, WatchpointId,
},
masm::*,
packaging::*,
};
23 changes: 23 additions & 0 deletions codegen/masm/src/masm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod intrinsics;
mod module;
mod program;
mod region;
mod rodata;

pub use midenc_hir::{
Local, LocalId, MasmBlock as Block, MasmBlockId as BlockId, MasmImport as Import, MasmOp as Op,
Expand All @@ -15,6 +16,7 @@ pub use self::{
module::{FrozenModuleTree, Module, ModuleTree},
program::{Library, Program},
region::Region,
rodata::Rodata,
};

/// This represents a descriptor for a pointer translated from the IR into a form suitable for
Expand Down Expand Up @@ -108,3 +110,24 @@ impl NativePtr {
(self.waddr * 16) + (self.index as u32 * 4) + self.offset as u32
}
}

impl From<miden_package::PtrDesc> for NativePtr {
fn from(value: miden_package::PtrDesc) -> Self {
NativePtr {
waddr: value.waddr,
index: value.index,
offset: value.offset,
addrspace: midenc_hir::AddressSpace::Unknown,
}
}
}

impl From<NativePtr> for miden_package::PtrDesc {
fn from(value: NativePtr) -> Self {
miden_package::PtrDesc {
waddr: value.waddr,
index: value.index,
offset: value.offset,
}
}
}
1 change: 0 additions & 1 deletion codegen/masm/src/masm/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use midenc_hir_analysis::GlobalVariableAnalysis;
use midenc_session::{Emit, Session};

use super::{module::Modules, *};
use crate::packaging::Rodata;

inventory::submit! {
midenc_session::CompileFlag::new("test_harness")
Expand Down
90 changes: 90 additions & 0 deletions codegen/masm/src/masm/rodata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use alloc::{fmt, sync::Arc};

use miden_processor::Digest;
use midenc_hir::{formatter::DisplayHex, ConstantData};

use crate::*;

/// Represents a read-only data segment, combined with its content digest
#[derive(Clone, PartialEq, Eq)]
pub struct Rodata {
/// The content digest computed for `data`
pub digest: Digest,
/// The address at which the data for this segment begins
pub start: NativePtr,
/// The raw binary data for this segment
pub data: Arc<ConstantData>,
}
impl fmt::Debug for Rodata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Rodata")
.field("digest", &format_args!("{}", DisplayHex::new(&self.digest.as_bytes())))
.field("start", &self.start)
.field_with("data", |f| {
f.debug_struct("ConstantData")
.field("len", &self.data.len())
.finish_non_exhaustive()
})
.finish()
}
}
impl Rodata {
pub fn size_in_bytes(&self) -> usize {
self.data.len()
}

pub fn size_in_felts(&self) -> usize {
self.data.len().next_multiple_of(4) / 4
}

pub fn size_in_words(&self) -> usize {
self.size_in_felts().next_multiple_of(4) / 4
}

/// Attempt to convert this rodata object to its equivalent representation in felts
///
/// The resulting felts will be in padded out to the nearest number of words, i.e. if the data
/// only takes up 3 felts worth of bytes, then the resulting `Vec` will contain 4 felts, so that
/// the total size is a valid number of words.
pub fn to_elements(&self) -> Result<Vec<miden_processor::Felt>, String> {
use miden_core::FieldElement;
use miden_processor::Felt;

let data = self.data.as_slice();
let mut felts = Vec::with_capacity(data.len() / 4);
let mut iter = data.iter().copied().array_chunks::<4>();
felts.extend(iter.by_ref().map(|bytes| Felt::new(u32::from_le_bytes(bytes) as u64)));
if let Some(remainder) = iter.into_remainder() {
let mut chunk = [0u8; 4];
for (i, byte) in remainder.into_iter().enumerate() {
chunk[i] = byte;
}
felts.push(Felt::new(u32::from_le_bytes(chunk) as u64));
}

let padding = (self.size_in_words() * 4).abs_diff(felts.len());
felts.resize(felts.len() + padding, Felt::ZERO);

Ok(felts)
}
}

impl From<miden_package::Rodata> for Rodata {
fn from(pkg_rodata: miden_package::Rodata) -> Self {
Rodata {
digest: pkg_rodata.digest,
start: pkg_rodata.start.into(),
data: Arc::new(pkg_rodata.data.into()),
}
}
}

impl From<Rodata> for miden_package::Rodata {
fn from(rodata: Rodata) -> Self {
miden_package::Rodata {
digest: rodata.digest,
start: rodata.start.into(),
data: <midenc_hir::ConstantData as Clone>::clone(&rodata.data).into(),
}
}
}
56 changes: 0 additions & 56 deletions codegen/masm/src/packaging/de.rs

This file was deleted.

7 changes: 0 additions & 7 deletions codegen/masm/src/packaging/mod.rs

This file was deleted.

Loading

0 comments on commit 7e3be95

Please sign in to comment.