Skip to content

Commit 3f7fd58

Browse files
Jeremy Braunfacebook-github-bot
authored andcommitted
improve bytecode printing w/ padding and AST locations
Summary: In developing D73038348, it was helpful to have the annotated source locations displayed in the bytecode dumps. This also computes the maximum length used to display an `IP` value, and padds all such values so that all of the `:` for a particular loop depth are aligned. Reviewed By: cjhopman Differential Revision: D73038341 fbshipit-source-id: 914e044a014fb19859f5c1817d94a1a57b2dca38
1 parent 7ae7b47 commit 3f7fd58

File tree

68 files changed

+192
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+192
-98
lines changed

starlark/src/eval/bc/instrs.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ impl BcInstrs {
218218

219219
pub(crate) fn fmt_impl(&self, f: &mut dyn Write, newline: bool) -> fmt::Result {
220220
let end_arg = self.end_arg();
221+
let ip_pad = if newline {
222+
let max_ip = self.iter().map(|(_, ip)| ip).max().unwrap_or(BcAddr(0));
223+
format!("{}", (max_ip).0).len()
224+
} else {
225+
0
226+
};
221227

222228
let mut loop_ends = Vec::new();
223229
let mut jump_targets = HashSet::new();
@@ -227,13 +233,21 @@ impl BcInstrs {
227233
});
228234
}
229235
for (ptr, ip) in self.iter() {
230-
if ptr != self.start_ptr() && !newline {
231-
write!(f, "; ")?;
232-
}
233-
234236
if loop_ends.last() == Some(&ip) {
235237
loop_ends.pop().unwrap();
236238
}
239+
let loop_pad = loop_ends.len() * 2;
240+
241+
if newline {
242+
if let Some(loc) = self.stmt_locs.stmt_at(ip) {
243+
writeln!(f, "{:loop_pad$} {:ip_pad$} # {}", "", "", loc.span.span)?;
244+
}
245+
} else {
246+
if ptr != self.start_ptr() {
247+
write!(f, "; ")?;
248+
}
249+
}
250+
237251
let opcode = ptr.get_opcode();
238252
if !jump_targets.is_empty() {
239253
if jump_targets.contains(&ip) {
@@ -243,11 +257,9 @@ impl BcInstrs {
243257
}
244258
}
245259
if newline {
246-
for _ in &loop_ends {
247-
write!(f, " ")?;
248-
}
260+
write!(f, "{:loop_pad$}", "")?;
249261
}
250-
write!(f, "{}: {:?}", ip.0, opcode)?;
262+
write!(f, "{:ip_pad$}: {:?}", ip.0, opcode)?;
251263
if opcode != BcOpcode::End {
252264
// `End` args are too verbose and not really instruction args.
253265
opcode.fmt_append_arg(ptr, ip, end_arg, f)?;
@@ -402,7 +414,7 @@ mod tests {
402414
bc.to_string()
403415
);
404416
assert_eq!(
405-
"0: Const True ->&abc\n24: Return &abc\n32: End\n",
417+
" 0: Const True ->&abc\n24: Return &abc\n32: End\n",
406418
bc.dump_debug()
407419
);
408420
} else if mem::size_of::<usize>() == 4 {

starlark/src/tests/bc/golden/and_or_false_and_x.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ def test(x): return False and x
1010

1111
Max stack size: 0
1212
Instructions:
13-
0: ReturnConst False
13+
# instrs.star.bzl:1:14-32
14+
0: ReturnConst False
1415
16: End

starlark/src/tests/bc/golden/and_or_false_or_x.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ def test(x): return False or x
1010

1111
Max stack size: 0
1212
Instructions:
13+
# instrs.star.bzl:1:14-31
1314
0: Return &x
1415
8: End

starlark/src/tests/bc/golden/and_or_true_and_x.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ def test(x): return True and x
1010

1111
Max stack size: 0
1212
Instructions:
13+
# instrs.star.bzl:1:14-31
1314
0: Return &x
1415
8: End

starlark/src/tests/bc/golden/and_or_true_or_x.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ def test(x): return True or x
1010

1111
Max stack size: 0
1212
Instructions:
13-
0: ReturnConst True
13+
# instrs.star.bzl:1:14-30
14+
0: ReturnConst True
1415
16: End

starlark/src/tests/bc/golden/and_or_x_and_false.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test(x): return x and False
1010

1111
Max stack size: 1
1212
Instructions:
13-
0: IfNotBr &x 48
13+
# instrs.star.bzl:1:14-32
14+
0: IfNotBr &x 48
1415
16: Const False ->&1
1516
40: Br 64
1617
>48: Mov &x ->&1

starlark/src/tests/bc/golden/and_or_x_and_true.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test(x): return x and True
1010

1111
Max stack size: 1
1212
Instructions:
13-
0: IfNotBr &x 48
13+
# instrs.star.bzl:1:14-31
14+
0: IfNotBr &x 48
1415
16: Const True ->&1
1516
40: Br 64
1617
>48: Mov &x ->&1

starlark/src/tests/bc/golden/and_or_x_or_false.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test(x): return x or False
1010

1111
Max stack size: 1
1212
Instructions:
13-
0: IfNotBr &x 40
13+
# instrs.star.bzl:1:14-31
14+
0: IfNotBr &x 40
1415
16: Mov &x ->&1
1516
32: Br 64
1617
>40: Const False ->&1

starlark/src/tests/bc/golden/and_or_x_or_true.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test(x): return x or True
1010

1111
Max stack size: 1
1212
Instructions:
13-
0: IfNotBr &x 40
13+
# instrs.star.bzl:1:14-30
14+
0: IfNotBr &x 40
1415
16: Mov &x ->&1
1516
32: Br 64
1617
>40: Const True ->&1

starlark/src/tests/bc/golden/call.golden

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ def test(a, k):
1919

2020
Max stack size: 6
2121
Instructions:
22-
0: Const 10 ->&3
23-
24: Const 20 ->&4
24-
48: Const 30 ->&5
25-
72: Const 40 ->&6
26-
96: Const 50 ->&7
22+
# instrs.star.bzl:2:5-10:6
23+
0: Const 10 ->&3
24+
24: Const 20 ->&4
25+
48: Const 30 ->&5
26+
72: Const 40 ->&6
27+
96: Const 50 ->&7
2728
120: CallFrozenNative noop {&3..&8 2 p q r *&0 **&1} instrs.star.bzl:2:5-10:6 ->&2
2829
208: ReturnConst None
2930
224: End

0 commit comments

Comments
 (0)