Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Feat/cli label indices #274

Merged
merged 6 commits into from
Apr 25, 2023
Merged
Changes from 5 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
62 changes: 60 additions & 2 deletions huff_cli/src/huffc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use huff_tests::{
use huff_utils::{
file_provider::FileSystemFileProvider,
prelude::{
export_interfaces, gen_sol_interfaces, str_to_bytes32, unpack_files, AstSpan, CodegenError,
CodegenErrorKind, CompilerError, FileSource, Literal, OutputLocation, Span,
export_interfaces, gen_sol_interfaces, str_to_bytes32, unpack_files, AstSpan, BytecodeRes,
CodegenError, CodegenErrorKind, CompilerError, FileSource, Literal, OutputLocation, Span,
},
};
use isatty::stdout_isatty;
Expand Down Expand Up @@ -82,6 +82,10 @@ struct Huff {
#[clap(short = 'v', long = "verbose")]
verbose: bool,

/// Prints out the jump label PC indices for the specified contract.
#[clap(short = 'x', long = "label-indices")]
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
label_indices: bool,

/// Override / set constants for the compilation environment.
#[clap(short = 'c', long = "constants", multiple_values = true)]
constants: Option<Vec<String>>,
Expand Down Expand Up @@ -204,6 +208,60 @@ fn main() {
file_provider: Arc::new(FileSystemFileProvider {}),
};

if cli.label_indices {
match compiler.grab_contracts() {
Ok(contracts) => {
if contracts.len() > 1 {
eprintln!(
"{}",
Paint::red("Multiple contracts found. Please specify a single contract and try again.")
);
std::process::exit(1);
}

if let Some(contract) = contracts.first() {
let m_macro = contract.find_macro_by_name("MAIN").unwrap();
merklefruit marked this conversation as resolved.
Show resolved Hide resolved

// Recurse through the macro and generate bytecode
let bytecode_res: BytecodeRes = Codegen::macro_to_bytecode(
m_macro.clone(),
contract,
&mut vec![m_macro],
0,
&mut Vec::default(),
false,
None,
)
.unwrap();

// Format the label indices nicely
let label_indices = bytecode_res
.label_indices
.iter()
.map(|(label, index)| format!("{}: {:#04x}", label, index))
.collect::<Vec<_>>()
.join("\n ");

println!("\n------ JUMP LABEL INDICES ------");
println!(" {}", label_indices);
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
println!("--------------------------------\n")
} else {
eprintln!(
"{}",
Paint::red("No contract found. Please specify a contract and try again.")
);
std::process::exit(1);
}
}
Err(e) => {
tracing::error!(target: "cli", "PARSER ERRORED!");
eprintln!("{}", Paint::red(e));
std::process::exit(1);
}
}
return
}

if let Some(TestCommands::Test { format, match_ }) = cli.test {
match compiler.grab_contracts() {
Ok(contracts) => {
Expand Down