Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to build the runnable representation. #6687

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions crates/cairo-lang-runnable/src/runnable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use cairo_lang_casm::assembler::AssembledCairoProgram;
use cairo_lang_casm::casm;
use cairo_vm::types::builtin_name::BuiltinName;
use itertools::chain;
use serde::{Deserialize, Serialize};

use crate::compile::CompiledFunction;

/// Structure to hold the runnable represenstation of a program.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Runnable {
Expand All @@ -11,6 +15,35 @@ pub struct Runnable {
pub entrypoints: Vec<RunnableEntryPoint>,
}

impl Runnable {
/// Create a new runnable program from a compiled function.
pub fn new(compiled: CompiledFunction) -> Self {
let non_returning_header = casm! {
ap += (compiled.wrapper.builtins.len());
call rel 4;
jmp rel 0;
};
Self {
program: compiled.program.assemble_ex(
chain!(&non_returning_header.instructions, &compiled.wrapper.header),
&compiled.wrapper.footer,
),
entrypoints: vec![
RunnableEntryPoint {
builtins: compiled.wrapper.builtins.clone(),
offset: 0,
kind: EntryPointKind::NonReturning,
},
RunnableEntryPoint {
builtins: compiled.wrapper.builtins,
offset: non_returning_header.current_code_offset,
kind: EntryPointKind::Function,
},
],
}
}
}

/// Information about a runnable entrypoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunnableEntryPoint {
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-sierra-to-casm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ impl CairoProgram {

/// Creates an assembled representation of the program preceded by `header` and followed by
/// `footer`.
pub fn assemble_ex(
&self,
header: &[Instruction],
pub fn assemble_ex<'a>(
&'a self,
header: impl IntoIterator<Item = &'a Instruction>,
footer: &[Instruction],
) -> AssembledCairoProgram {
let mut bytecode = vec![];
Expand Down