Skip to content

Commit

Permalink
Merge 9e7c56b into db8ba0b
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Jul 3, 2024
2 parents db8ba0b + 9e7c56b commit 69ca4b3
Show file tree
Hide file tree
Showing 25 changed files with 744 additions and 155 deletions.
5 changes: 5 additions & 0 deletions forc-pkg/src/manifest/build_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct BuildProfile {
#[serde(default)]
pub print_bytecode: bool,
#[serde(default)]
pub print_bytecode_spans: bool,
#[serde(default)]
pub terse: bool,
#[serde(default)]
pub time_phases: bool,
Expand Down Expand Up @@ -57,6 +59,7 @@ impl BuildProfile {
print_ir: PrintIr::default(),
print_asm: PrintAsm::default(),
print_bytecode: false,
print_bytecode_spans: false,
terse: false,
time_phases: false,
metrics_outfile: None,
Expand All @@ -80,6 +83,7 @@ impl BuildProfile {
print_ir: PrintIr::default(),
print_asm: PrintAsm::default(),
print_bytecode: false,
print_bytecode_spans: false,
terse: false,
time_phases: false,
metrics_outfile: None,
Expand Down Expand Up @@ -152,6 +156,7 @@ mod tests {
print_ir: PrintIr::r#final(),
print_asm: PrintAsm::all(),
print_bytecode: true,
print_bytecode_spans: true,
terse: true,
time_phases: true,
metrics_outfile: Some("metrics_outfile".into()),
Expand Down
8 changes: 7 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ pub struct PrintOpts {
pub asm: PrintAsm,
/// Print the bytecode. This is the final output of the compiler.
pub bytecode: bool,
/// Print the original source code together with bytecode.
pub bytecode_spans: bool,
/// Print the generated Sway IR (Intermediate Representation).
pub ir: PrintIr,
/// Output build errors and warnings in reverse order.
Expand Down Expand Up @@ -1549,7 +1551,10 @@ pub fn sway_build_config(
.with_print_dca_graph(build_profile.print_dca_graph.clone())
.with_print_dca_graph_url_format(build_profile.print_dca_graph_url_format.clone())
.with_print_asm(build_profile.print_asm)
.with_print_bytecode(build_profile.print_bytecode)
.with_print_bytecode(
build_profile.print_bytecode,
build_profile.print_bytecode_spans,
)
.with_print_ir(build_profile.print_ir.clone())
.with_include_tests(build_profile.include_tests)
.with_time_phases(build_profile.time_phases)
Expand Down Expand Up @@ -2079,6 +2084,7 @@ fn build_profile_from_opts(
profile.print_ir |= print.ir.clone();
profile.print_asm |= print.asm;
profile.print_bytecode |= print.bytecode;
profile.print_bytecode_spans |= print.bytecode_spans;
profile.terse |= pkg.terse;
profile.time_phases |= time_phases;
if profile.metrics_outfile.is_none() {
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy) -> pkg::BuildOpts {
dca_graph_url_format: cmd.print.dca_graph_url_format.clone(),
asm: cmd.print.asm(),
bytecode: cmd.print.bytecode,
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
},
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
dca_graph_url_format: cmd.print.dca_graph_url_format.clone(),
asm: cmd.print.asm(),
bytecode: cmd.print.bytecode,
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
},
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ fn opts_from_cmd(cmd: Command) -> forc_test::TestOpts {
dca_graph_url_format: cmd.build.print.dca_graph_url_format.clone(),
asm: cmd.build.print.asm(),
bytecode: cmd.build.print.bytecode,
bytecode_spans: false,
ir: cmd.build.print.ir(),
reverse_order: cmd.build.print.reverse_order,
},
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
dca_graph_url_format: cmd.build.print.dca_graph_url_format.clone(),
asm: cmd.build.print.asm(),
bytecode: cmd.build.print.bytecode,
bytecode_spans: false,
ir: cmd.build.print.ir(),
reverse_order: cmd.build.print.reverse_order,
},
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_contract_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn build_opts_from_cmd(cmd: &ContractIdCommand) -> pkg::BuildOpts {
dca_graph_url_format: cmd.print.dca_graph_url_format.clone(),
asm: cmd.print.asm(),
bytecode: cmd.print.bytecode,
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
},
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_predicate_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn build_opts_from_cmd(cmd: PredicateRootCommand) -> pkg::BuildOpts {
dca_graph_url_format: cmd.print.dca_graph_url_format.clone(),
asm: cmd.print.asm(),
bytecode: cmd.print.bytecode,
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
},
Expand Down
48 changes: 46 additions & 2 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ fn to_bytecode_mut(
println!(";; --- START OF TARGET BYTECODE ---\n");
}

let mut last_span = None;
let mut indentation = if build_config.print_bytecode_spans {
4
} else {
0
};

let mut half_word_ix = 0;
let mut offset_from_instr_start = 0;
for op in ops.iter() {
Expand All @@ -165,7 +172,7 @@ fn to_bytecode_mut(
match fuel_op {
Either::Right(data) => {
if build_config.print_bytecode {
print!("{:#010x} ", bytecode.len());
print!("{}{:#010x} ", " ".repeat(indentation), bytecode.len());
println!(
" ;; {:?}",
data
Expand All @@ -181,8 +188,45 @@ fn to_bytecode_mut(
}
Either::Left(instructions) => {
for instruction in instructions {
// Print original source span only once
if build_config.print_bytecode_spans {
last_span = match (last_span, &span) {
(None, Some(span)) => {
indentation = 4;
let line_col = span.start_pos().line_col();
println!(
"{} @ {}:{}:{}",
span.as_str(),
span.source_id()
.map(|source_id| source_engine.get_path(source_id))
.map(|x| x.display().to_string())
.unwrap_or("<autogenerated>".to_string()),
line_col.line,
line_col.col
);
Some(span.clone())
}
(Some(last), Some(span)) if last != *span => {
indentation = 4;
let line_col = span.start_pos().line_col();
println!(
"{} @ {}:{}:{}",
span.as_str(),
span.source_id()
.map(|source_id| source_engine.get_path(source_id))
.map(|x| x.display().to_string())
.unwrap_or("<autogenerated>".to_string()),
line_col.line,
line_col.col
);
Some(span.clone())
}
(last, _) => last,
};
}

if build_config.print_bytecode {
print!("{:#010x} ", bytecode.len());
print!("{}{:#010x} ", " ".repeat(indentation), bytecode.len());
print_instruction(&instruction);
}

Expand Down
7 changes: 5 additions & 2 deletions sway-core/src/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub struct BuildConfig {
pub(crate) print_dca_graph_url_format: Option<String>,
pub(crate) print_asm: PrintAsm,
pub(crate) print_bytecode: bool,
pub(crate) print_bytecode_spans: bool,
pub(crate) print_ir: PrintIr,
pub(crate) include_tests: bool,
pub(crate) optimization_level: OptLevel,
Expand Down Expand Up @@ -234,6 +235,7 @@ impl BuildConfig {
print_dca_graph_url_format: None,
print_asm: PrintAsm::default(),
print_bytecode: false,
print_bytecode_spans: false,
print_ir: PrintIr::default(),
include_tests: false,
time_phases: false,
Expand Down Expand Up @@ -264,9 +266,10 @@ impl BuildConfig {
Self { print_asm, ..self }
}

pub fn with_print_bytecode(self, a: bool) -> Self {
pub fn with_print_bytecode(self, bytecode: bool, bytecode_spans: bool) -> Self {
Self {
print_bytecode: a,
print_bytecode: bytecode,
print_bytecode_spans: bytecode_spans,
..self
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ use super::{
///
/// # Details
///
/// This algorithm checks is a match expression is exhaustive and if its match
/// This algorithm checks if a match expression is exhaustive and if its match
/// arms are reachable by applying the above definitions of usefulness and
/// witnesses. This algorithm sequentially creates a [WitnessReport] for every
/// match arm by calling *U(P, q)*, where *P* is the [Matrix] of patterns seen
Expand Down
Loading

0 comments on commit 69ca4b3

Please sign in to comment.