-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic runnable to textual casm compilation. (#6595)
- Loading branch information
Showing
11 changed files
with
319 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::path::Path; | ||
use std::sync::Arc; | ||
|
||
use anyhow::{Context, Result}; | ||
use cairo_lang_compiler::db::RootDatabase; | ||
use cairo_lang_compiler::diagnostics::DiagnosticsReporter; | ||
use cairo_lang_compiler::project::setup_project; | ||
use cairo_lang_filesystem::ids::CrateId; | ||
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; | ||
use cairo_lang_runnable_utils::builder::RunnableBuilder; | ||
use cairo_lang_semantic::plugin::PluginSuite; | ||
use cairo_lang_sierra_generator::db::SierraGenGroup; | ||
use cairo_lang_sierra_generator::executables::find_executable_function_ids; | ||
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug; | ||
use cairo_lang_utils::Upcast; | ||
use itertools::Itertools; | ||
|
||
use crate::plugin::{RUNNABLE_ATTR, RunnablePlugin}; | ||
|
||
/// Compile the function given by path. | ||
/// Errors if there is ambiguity. | ||
pub fn compile_runnable( | ||
path: &Path, | ||
runnable_path: Option<&str>, | ||
diagnostics_reporter: DiagnosticsReporter<'_>, | ||
) -> Result<String> { | ||
let mut db = RootDatabase::builder() | ||
.detect_corelib() | ||
.with_plugin_suite(std::mem::take(PluginSuite::default().add_plugin::<RunnablePlugin>())) | ||
.build()?; | ||
|
||
let main_crate_ids = setup_project(&mut db, Path::new(&path))?; | ||
|
||
compile_runnable_in_prepared_db(&db, runnable_path, main_crate_ids, diagnostics_reporter) | ||
} | ||
|
||
/// Runs compiler on the specified runnable function. | ||
/// If no runnable was specified, verify that there is only one. | ||
/// Otherwise, return an error. | ||
pub fn compile_runnable_in_prepared_db( | ||
db: &RootDatabase, | ||
runnable_path: Option<&str>, | ||
main_crate_ids: Vec<CrateId>, | ||
mut diagnostics_reporter: DiagnosticsReporter<'_>, | ||
) -> Result<String> { | ||
let mut runnables: Vec<_> = find_executable_function_ids(db, main_crate_ids) | ||
.into_iter() | ||
.filter_map(|(id, labels)| labels.into_iter().any(|l| l == RUNNABLE_ATTR).then_some(id)) | ||
.collect(); | ||
|
||
// TODO(ilya): Add contract names. | ||
if let Some(runnable_path) = runnable_path { | ||
runnables.retain(|runnable| { | ||
runnable.base_semantic_function(db).full_path(db.upcast()) == runnable_path | ||
}); | ||
}; | ||
let runnable = match runnables.len() { | ||
0 => { | ||
// Report diagnostics as they might reveal the reason why no runnable was found. | ||
diagnostics_reporter.ensure(db)?; | ||
anyhow::bail!("Requested `#[runnable]` not found."); | ||
} | ||
1 => runnables[0], | ||
_ => { | ||
let runnable_names = runnables | ||
.iter() | ||
.map(|runnable| runnable.base_semantic_function(db).full_path(db.upcast())) | ||
.join("\n "); | ||
anyhow::bail!( | ||
"More than one runnable found in the main crate: \n {}\nUse --runnable to \ | ||
specify which to compile.", | ||
runnable_names | ||
); | ||
} | ||
}; | ||
|
||
compile_runnable_function_in_prepared_db(db, runnable, diagnostics_reporter) | ||
} | ||
|
||
/// Runs compiler for a runnable function. | ||
/// | ||
/// # Arguments | ||
/// * `db` - Preloaded compilation database. | ||
/// * `runnable` - [`ConcreteFunctionWithBodyId`]s to compile. | ||
/// * `compiler_config` - The compiler configuration. | ||
/// # Returns | ||
/// * `Ok(Vec<String>)` - The result artifact of the compilation. | ||
/// * `Err(anyhow::Error)` - Compilation failed. | ||
pub fn compile_runnable_function_in_prepared_db( | ||
db: &RootDatabase, | ||
runnable: ConcreteFunctionWithBodyId, | ||
mut diagnostics_reporter: DiagnosticsReporter<'_>, | ||
) -> Result<String> { | ||
diagnostics_reporter.ensure(db)?; | ||
let SierraProgramWithDebug { program: sierra_program, debug_info: _ } = Arc::unwrap_or_clone( | ||
db.get_sierra_program_for_functions(vec![runnable]) | ||
.ok() | ||
.with_context(|| "Compilation failed without any diagnostics.")?, | ||
); | ||
let runnable_func = sierra_program.funcs[0].clone(); | ||
let builder = RunnableBuilder::new(sierra_program, None)?; | ||
Ok(builder.casm_function_program(&runnable_func)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//! > Basic runnable. | ||
|
||
//! > test_runner_name | ||
CompileRunnableTestRunner(expect_diagnostics: false) | ||
|
||
//! > cairo_code | ||
#[runnable] | ||
fn main() {} | ||
|
||
//! > generated_casm_code | ||
# builtins: | ||
# header # | ||
ap += 0; | ||
call rel 3; | ||
ret; | ||
# sierra based code # | ||
ret; | ||
# footer # | ||
ret; | ||
|
||
//! > expected_diagnostics | ||
|
||
//! > ========================================================================== | ||
|
||
//! > Test runnable with arguments. | ||
|
||
//! > test_runner_name | ||
CompileRunnableTestRunner(expect_diagnostics: false) | ||
|
||
//! > cairo_code | ||
#[runnable] | ||
fn main(a: felt252, b: felt252) -> felt252 { | ||
a + b | ||
} | ||
|
||
//! > generated_casm_code | ||
# builtins: | ||
# header # | ||
ap += 0; | ||
%{ raise NotImplementedError %} | ||
%{ raise NotImplementedError %} | ||
ap += 2; | ||
call rel 3; | ||
ret; | ||
# sierra based code # | ||
[ap + 0] = [fp + -4] + [fp + -3], ap++; | ||
ret; | ||
# footer # | ||
ret; | ||
|
||
//! > expected_diagnostics | ||
|
||
//! > ========================================================================== | ||
|
||
//! > Test runnable with recursion. | ||
|
||
//! > test_runner_name | ||
CompileRunnableTestRunner(expect_diagnostics: false) | ||
|
||
//! > cairo_code | ||
#[runnable] | ||
fn fib(a: u128, b: u128, n: u128) -> u128 { | ||
if n == 0 { | ||
a | ||
} else { | ||
fib(b, a + b, n - 1) | ||
} | ||
} | ||
|
||
//! > generated_casm_code | ||
# builtins: range_check | ||
# header # | ||
ap += 0; | ||
[ap + 0] = [fp + -3], ap++; | ||
%{ raise NotImplementedError %} | ||
%{ raise NotImplementedError %} | ||
%{ raise NotImplementedError %} | ||
ap += 3; | ||
call rel 3; | ||
ret; | ||
# sierra based code # | ||
jmp rel 9 if [fp + -3] != 0; | ||
[ap + 0] = [fp + -6], ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = [fp + -5], ap++; | ||
ret; | ||
[ap + 1] = [fp + -5] + [fp + -4], ap++; | ||
%{ memory[ap + -1] = memory[ap + 0] < 340282366920938463463374607431768211456 %} | ||
jmp rel 7 if [ap + -1] != 0, ap++; | ||
[ap + -1] = [ap + 0] + 340282366920938463463374607431768211456, ap++; | ||
[ap + -1] = [[fp + -6] + 0]; | ||
jmp rel 35; | ||
[ap + -1] = [[fp + -6] + 0]; | ||
[ap + 0] = 1, ap++; | ||
[fp + -3] = [ap + 1] + [ap + -1], ap++; | ||
%{ memory[ap + -1] = memory[ap + 0] < 340282366920938463463374607431768211456 %} | ||
jmp rel 7 if [ap + -1] != 0, ap++; | ||
[ap + 0] = [ap + -1] + 340282366920938463463374607431768211456, ap++; | ||
[ap + -1] = [[fp + -6] + 1]; | ||
jmp rel 11; | ||
[ap + -1] = [[fp + -6] + 1]; | ||
[ap + 0] = [fp + -6] + 2, ap++; | ||
[ap + 0] = [fp + -4], ap++; | ||
[ap + 0] = [ap + -6], ap++; | ||
[ap + 0] = [ap + -4], ap++; | ||
call rel -34; | ||
ret; | ||
%{ memory[ap + 0] = segments.add() %} | ||
ap += 1; | ||
[ap + 0] = 39878429859763533771555484554338820190071, ap++; | ||
[ap + -1] = [[ap + -2] + 0]; | ||
[ap + 0] = [fp + -6] + 2, ap++; | ||
[ap + 0] = 1, ap++; | ||
[ap + 0] = [ap + -4], ap++; | ||
[ap + 0] = [ap + -5] + 1, ap++; | ||
ret; | ||
%{ memory[ap + 0] = segments.add() %} | ||
ap += 1; | ||
[ap + 0] = 39878429859757942499084499860145094553463, ap++; | ||
[ap + -1] = [[ap + -2] + 0]; | ||
[ap + 0] = [fp + -6] + 1, ap++; | ||
[ap + 0] = 1, ap++; | ||
[ap + 0] = [ap + -4], ap++; | ||
[ap + 0] = [ap + -5] + 1, ap++; | ||
ret; | ||
# footer # | ||
ret; | ||
|
||
//! > expected_diagnostics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
pub mod compile; | ||
pub mod plugin; | ||
|
||
#[cfg(test)] | ||
mod test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.