Skip to content
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
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[alias]
lint = "clippy --workspace --all-targets --all-features -- --no-deps"
lint = "all-features clippy --workspace --all-targets -- --no-deps"
docs = "doc --workspace --all-features --no-deps"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --check
- run: cargo install cargo-all-features
- run: cargo lint
env:
# Make sure CI fails on all warnings, including Clippy lints.
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
.idea/
.spr.yml
.DS_Store
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ serde_json = "1"
tracing = "0.1"
anyhow = "1.0"
starknet-types-core = "0.2.4"

[features]
dev = []
19 changes: 19 additions & 0 deletions src/debugger/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use cairo_lang_sierra::program::{Program, ProgramArtifact, Statement, StatementI
use cairo_lang_sierra::program_registry::ProgramRegistry;
use scarb_metadata::MetadataCommand;

#[cfg(feature = "dev")]
mod readable_sierra_ids;

/// Struct that holds all the initial data needed for the debugger during execution.
pub struct Context {
pub root_path: PathBuf,
Expand All @@ -25,6 +28,8 @@ pub struct Context {
files_data: HashMap<PathBuf, FileCodeLocationsData>,
program: Program,
sierra_program_registry: ProgramRegistry<CoreType, CoreLibfunc>,
#[cfg(feature = "dev")]
labels: HashMap<usize, String>,
}

pub struct CasmDebugInfo {
Expand Down Expand Up @@ -74,6 +79,9 @@ impl Context {
let files_data = build_file_locations_map(&casm_debug_info, &code_locations);

Ok(Self {
#[cfg(feature = "dev")]
labels: readable_sierra_ids::extract_labels(&program),

root_path,
code_locations,
function_names,
Expand Down Expand Up @@ -149,6 +157,17 @@ impl Context {
fn statement_idx_to_statement(&self, statement_idx: StatementIdx) -> &Statement {
&self.program.statements[statement_idx.0]
}

#[cfg(feature = "dev")]
#[allow(unused)]
pub fn print_statement(&self, statement_idx: StatementIdx) {
let statement = self.statement_idx_to_statement(statement_idx);
let with_labels = readable_sierra_ids::replace_statement_id(statement.clone(), |idx| {
self.labels[&idx.0].clone()
});

eprintln!("{with_labels}")
}
}

fn build_file_locations_map(
Expand Down
64 changes: 64 additions & 0 deletions src/debugger/context/readable_sierra_ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::collections::{HashMap, HashSet};

use cairo_lang_sierra::program::{
GenBranchInfo, GenBranchTarget, GenInvocation, GenStatement, Program,
};

// https://github.com/starkware-libs/cairo/blob/64b88f06c6261ac67c6b478434c844d4af81e5a3/crates/cairo-lang-sierra/src/fmt.rs#L29
pub fn extract_labels(program: &Program) -> HashMap<usize, String> {
let funcs_labels = HashMap::<usize, String>::from_iter(
program.funcs.iter().enumerate().map(|(i, f)| (f.entry_point.0, format!("F{i}"))),
);
// The offsets of branch targets.
let mut block_offsets = HashSet::<usize>::default();
for s in &program.statements {
replace_statement_id(s.clone(), |idx| {
block_offsets.insert(idx.0);
});
}
// All labels including inner function labels.
let mut labels = funcs_labels.clone();
// Starting as `NONE` for support of invalid Sierra code.
let mut function_label = "NONE".to_string();
// Assuming function code is contiguous - this is the index for same function labels.
let mut inner_idx = 0;
for i in 0..program.statements.len() {
if let Some(label) = funcs_labels.get(&i) {
function_label = label.clone();
inner_idx = 0;
} else if block_offsets.contains(&i) {
labels.insert(i, format!("{function_label}_B{inner_idx}"));
inner_idx += 1;
}
}

labels
}

// https://github.com/starkware-libs/cairo/blob/c539d077479654eee6323d9c0c6eafad82d4851a/crates/cairo-lang-sierra/src/labeled_statement.rs#L25
pub fn replace_statement_id<StatementIdIn, StatementIdOut>(
statement: GenStatement<StatementIdIn>,
mut map_stmt_id: impl FnMut(StatementIdIn) -> StatementIdOut,
) -> GenStatement<StatementIdOut> {
match statement {
GenStatement::Invocation(GenInvocation { libfunc_id, args, branches }) => {
GenStatement::Invocation(GenInvocation {
libfunc_id,
args,
branches: branches
.into_iter()
.map(|GenBranchInfo { results, target }| GenBranchInfo {
results,
target: match target {
GenBranchTarget::Fallthrough => GenBranchTarget::Fallthrough,
GenBranchTarget::Statement(statement_id) => {
GenBranchTarget::Statement(map_stmt_id(statement_id))
}
},
})
.collect(),
})
}
GenStatement::Return(vars) => GenStatement::Return(vars),
}
}