Skip to content

Commit 27d321e

Browse files
askeksaCommit Queue
authored andcommitted
[wasm_builder] Fix --watch option to work with instructions
The `--watch` debug option to print the stack trace leading to a particular byte in the output Wasm file didn't work for instructions after the instruction encoder was changed to produce intermediate instruction objects. This is fixed by keeping a map from instruction objects to stack traces in the instructions builder and registering those stack traces when serializing the instructions. Change-Id: I90d665753813452d07783c7f47e1e6bf63a3a18e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324681 Reviewed-by: Jess Lally <[email protected]> Commit-Queue: Aske Simon Christensen <[email protected]>
1 parent 5a4b252 commit 27d321e

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

pkg/wasm_builder/lib/src/builder/instructions.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ class InstructionsBuilder with Builder<ir.Instructions> {
144144
/// List of instructions.
145145
final List<ir.Instruction> _instructions = [];
146146

147+
/// Stored stack traces leading to the instructions for watch points.
148+
final Map<ir.Instruction, StackTrace>? _stackTraces;
149+
147150
/// Create a new instruction sequence.
148151
InstructionsBuilder(this.module, List<ir.ValueType> outputs,
149-
{this.isGlobalInitializer = false}) {
152+
{this.isGlobalInitializer = false})
153+
: _stackTraces = module.watchPoints != null ? {} : null {
150154
_labelStack.add(Expression(const [], outputs));
151155
}
152156

@@ -161,9 +165,14 @@ class InstructionsBuilder with Builder<ir.Instructions> {
161165

162166
@override
163167
ir.Instructions forceBuild() =>
164-
ir.Instructions(locals, _instructions, _traceLines);
168+
ir.Instructions(locals, _instructions, _stackTraces, _traceLines);
165169

166-
void _add(ir.Instruction i) => _instructions.add(i);
170+
void _add(ir.Instruction i) {
171+
_instructions.add(i);
172+
if (module.watchPoints != null) {
173+
_stackTraces![i] = StackTrace.current;
174+
}
175+
}
167176

168177
ir.Local addLocal(ir.ValueType type, {required bool isParameter}) {
169178
final local = ir.Local(locals.length, type);

pkg/wasm_builder/lib/src/ir/instructions.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ class Instructions implements Serializable {
1414
/// A sequence of Wasm instructions.
1515
final List<Instruction> instructions;
1616

17+
final Map<Instruction, StackTrace>? _stackTraces;
18+
1719
final List<String> _traceLines;
1820

1921
/// A string trace.
2022
late final trace = _traceLines.join();
2123

2224
/// Create a new instruction sequence.
23-
Instructions(this.locals, this.instructions, this._traceLines);
25+
Instructions(
26+
this.locals, this.instructions, this._stackTraces, this._traceLines);
2427

2528
@override
2629
void serialize(Serializer s) {
2730
for (final i in instructions) {
31+
if (_stackTraces != null) s.debugTrace(_stackTraces![i]!);
2832
i.serialize(s);
2933
}
3034
}

pkg/wasm_builder/lib/src/serialize/serializer.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ class Serializer {
3434
}
3535
}
3636

37-
void _debugTrace(Object data) {
37+
void debugTrace(Object data) {
3838
_traces[_index] ??= data;
3939
}
4040

4141
void writeByte(int byte) {
42-
if (traceEnabled) _debugTrace(StackTrace.current);
42+
if (traceEnabled) debugTrace(StackTrace.current);
4343
assert(byte == byte & 0xFF);
4444
_ensure(1);
4545
_data[_index++] = byte;
4646
}
4747

4848
void writeBytes(List<int> bytes) {
49-
if (traceEnabled) _debugTrace(StackTrace.current);
49+
if (traceEnabled) debugTrace(StackTrace.current);
5050
_ensure(bytes.length);
5151
_data.setRange(_index, _index += bytes.length, bytes);
5252
}
@@ -102,7 +102,7 @@ class Serializer {
102102
}
103103

104104
void writeData(Serializer chunk, [List<int>? watchPoints]) {
105-
if (traceEnabled) _debugTrace(chunk);
105+
if (traceEnabled) debugTrace(chunk);
106106
if (watchPoints != null) {
107107
for (int watchPoint in watchPoints) {
108108
if (_index <= watchPoint && watchPoint < _index + chunk.data.length) {

0 commit comments

Comments
 (0)