Skip to content

Commit

Permalink
Merge 332b0af into b7918e5
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Jul 4, 2024
2 parents b7918e5 + 332b0af commit 3cd9fe7
Show file tree
Hide file tree
Showing 26 changed files with 805 additions and 73 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: false,
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 @@ -263,6 +263,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 @@ -1557,7 +1559,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 @@ -2087,6 +2092,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 @@ -242,6 +242,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
107 changes: 104 additions & 3 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
fuel::{checks, data_section::DataSection},
ProgramABI, ProgramKind,
};
use crate::asm_generation::fuel::data_section::{DataId, Datum, Entry};
use crate::asm_lang::allocated_ops::{AllocatedOp, AllocatedOpcode};
use crate::decl_engine::DeclRefFunction;
use crate::source_map::SourceMap;
Expand Down Expand Up @@ -151,6 +152,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 +173,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 +189,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 All @@ -202,8 +247,64 @@ fn to_bytecode_mut(
}
}
}

if build_config.print_bytecode {
println!("{}", data_section);
println!(".data_section:");

let offset = bytecode.len();

fn print_entry(indentation: usize, offset: usize, pair: &Entry) {
print!("{}{:#010x} ", " ".repeat(indentation), offset);

match &pair.value {
Datum::Byte(w) => println!(".byte i{w}, as hex {w:02X}"),
Datum::Word(w) => {
println!(".word i{w}, as hex be bytes ({:02X?})", w.to_be_bytes())
}
Datum::ByteArray(bs) => {
print!(".bytes as hex ({bs:02X?}), len i{}, as ascii \"", bs.len());

for b in bs {
print!(
"{}",
if *b == b' ' || b.is_ascii_graphic() {
*b as char
} else {
'.'
}
);
}
println!("\"");
}
Datum::Slice(bs) => {
print!(".slice as hex ({bs:02X?}), len i{}, as ascii \"", bs.len());

for b in bs {
print!(
"{}",
if *b == b' ' || b.is_ascii_graphic() {
*b as char
} else {
'.'
}
);
}
println!("\"");
}
Datum::Collection(els) => {
println!(".collection");
for e in els {
print_entry(indentation + 1, offset, e);
}
}
};
}

for (i, entry) in data_section.value_pairs.iter().enumerate() {
let entry_offset = data_section.data_id_to_offset(&DataId(i as u32));
print_entry(indentation, offset + entry_offset, entry);
}

println!(";; --- END OF TARGET BYTECODE ---\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ impl AllocatedAbstractInstructionSet {
new_ops.push(AllocatedAbstractOp {
opcode: Either::Left(AllocatedOpcode::PSHL(mask_l)),
comment: "Save registers 16..40".into(),
owning_span: None,
owning_span: op.owning_span.clone(),
});
}
if mask_h.value != 0 {
new_ops.push(AllocatedAbstractOp {
opcode: Either::Left(AllocatedOpcode::PSHH(mask_h)),
comment: "Save registers 40..64".into(),
owning_span: None,
owning_span: op.owning_span.clone(),
});
}
}
Expand All @@ -147,14 +147,14 @@ impl AllocatedAbstractInstructionSet {
new_ops.push(AllocatedAbstractOp {
opcode: Either::Left(AllocatedOpcode::POPH(mask_h)),
comment: "Restore registers 40..64".into(),
owning_span: None,
owning_span: op.owning_span.clone(),
});
}
if mask_l.value != 0 {
new_ops.push(AllocatedAbstractOp {
opcode: Either::Left(AllocatedOpcode::POPL(mask_l)),
comment: "Restore registers 16..40".into(),
owning_span: None,
owning_span: op.owning_span.clone(),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/asm_generation/fuel/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
function.get_name(self.context)
);

self.cur_bytecode.push(match span {
Some(span) => Op::jump_label_comment(start_label, span, comment),
self.cur_bytecode.push(match &span {
Some(span) => Op::jump_label_comment(start_label, span.clone(), comment),
None => Op::unowned_jump_label_comment(start_label, comment),
});

Expand All @@ -285,7 +285,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
self.cur_bytecode.push(Op {
opcode: Either::Right(OrganizationalOp::PushAll(start_label)),
comment: "save all regs".to_owned(),
owning_span: None,
owning_span: span.clone(),
});
}

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 3cd9fe7

Please sign in to comment.