Skip to content

Commit c331f4d

Browse files
committed
Simplify stratum API.
Stratum and LineMapping are now simple immutable data classes. All the SMAP formatting logic has been moved to the Smap class. Builder patters have been added to construct them. Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent b0a077d commit c331f4d

14 files changed

Lines changed: 616 additions & 737 deletions

File tree

build-time-compiler/src/main/java/com/dylibso/chicory/build/time/compiler/Generator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ public void generateMetaWasm(Set<Integer> interpretedFunctions) throws IOExcepti
130130

131131
writeVarUInt32(out, count);
132132

133-
Stratum inputStratum = Stratum.create("WASM");
133+
var inputStratum = Stratum.builder("WASM").build();
134134
if (config.debugParser() != null) {
135135
inputStratum = config.debugParser().apply(module);
136136
}
137-
final Stratum outputStratum = Stratum.create("WASM");
137+
final var outputStratum = Stratum.builder("WASM");
138138

139139
var actual = readVarUInt32(source);
140140
assert count == actual;
@@ -205,7 +205,7 @@ private static void remapFunctionStratum(
205205
WasmModule module,
206206
int funcId,
207207
Stratum inputStratum,
208-
Stratum outputStratum,
208+
Stratum.Builder outputStratum,
209209
int newFunctionAddress,
210210
int functionSize) {
211211
String debugFunctionName = null;
@@ -223,7 +223,7 @@ private static void remapFunctionStratum(
223223

224224
if (debugFunctionName == null) {
225225
debugFunctionName = inputStratum.getFunctionMapping(relativeAddress);
226-
outputStratum.addFunctionMapping(
226+
outputStratum.withFunctionMapping(
227227
debugFunctionName, newFunctionAddress, newFunctionAddress + functionSize);
228228
}
229229

@@ -233,7 +233,7 @@ private static void remapFunctionStratum(
233233
if (lastLine != null && !line.equals(lastLine)) {
234234
var newAddress = lastLineAddress - originalFunctionAddress + newFunctionAddress;
235235
var instructionSize = relativeAddress - lastLineAddress;
236-
outputStratum.addLineData(
236+
outputStratum.withLineMapping(
237237
lastLine.fileName(),
238238
lastLine.filePath(),
239239
lastLine.line(),
@@ -249,7 +249,7 @@ private static void remapFunctionStratum(
249249
if (lastLine != null) {
250250
var newAddress = lastLineAddress - originalFunctionAddress + newFunctionAddress;
251251
var instructionSize = 1;
252-
outputStratum.addLineData(
252+
outputStratum.withLineMapping(
253253
lastLine.fileName(),
254254
lastLine.filePath(),
255255
lastLine.line(),

compiler/src/main/java/com/dylibso/chicory/compiler/internal/Compiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public final class Compiler {
135135
// Debug information
136136
static final class DebugContext {
137137
final Stratum inputStratum;
138-
final Stratum outputStratum = Stratum.create("WASM");
138+
final Stratum.Builder outputStratum = Stratum.builder("WASM");
139139
int nextOutputLineNo = 1;
140140

141141
DebugContext(Stratum stratum) {
@@ -160,7 +160,7 @@ private Compiler(
160160
this.analyzer = new WasmAnalyzer(module);
161161
this.functionImports = module.importSection().count(ExternalType.FUNCTION);
162162
this.debugContext =
163-
new DebugContext(requireNonNullElseGet(stratum, () -> Stratum.create("")));
163+
new DebugContext(requireNonNullElseGet(stratum, () -> Stratum.builder("").build()));
164164

165165
if (interpretedFunctions == null || interpretedFunctions.isEmpty()) {
166166
this.interpretedFunctions = new HashSet<>();

compiler/src/main/java/com/dylibso/chicory/compiler/internal/WasmAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public List<CompilerInstruction> analyze(int funcId, Compiler.DebugContext debug
167167

168168
String file = lineMapping.fileName();
169169
String path = lineMapping.filePath();
170-
debugContext.outputStratum.addLineData(
170+
debugContext.outputStratum.withLineMapping(
171171
file, path, lineMapping.line(), lineMapping.count(), outputLineNo, 1);
172172

173173
labels.add(idx);
@@ -405,7 +405,7 @@ public List<CompilerInstruction> analyze(int funcId, Compiler.DebugContext debug
405405

406406
if (debugFunctionName != null && startLineNo >= 0) {
407407
var endLineNo = debugContext.nextOutputLineNo++;
408-
debugContext.outputStratum.addFunctionMapping(
408+
debugContext.outputStratum.withFunctionMapping(
409409
debugFunctionName, startLineNo, endLineNo);
410410

411411
var idx = body.instructions().size();

dwarf-rust-experimental/src/main/java/com/dylibso/chicory/dwarf/rust/DebugParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static Stratum parse(byte[] wasm) throws ParserException {
107107

108108
private static Stratum parse(InputStream is) throws ParserException {
109109
SourceResult result = getSourceResult(is);
110-
var stratum = Stratum.create("WASM");
110+
var stratum = Stratum.builder("WASM");
111111
HashMap<Integer, FileInfo> sourceFilesByID = new HashMap<>();
112112
for (var unit : result.units) {
113113
for (var file : unit.files) {
@@ -144,7 +144,7 @@ private static Stratum parse(InputStream is) throws ParserException {
144144
int line = (int) location[2];
145145

146146
var file = sourceFilesByID.get(fileID);
147-
stratum.addLineData(file.file, file.path, line, 1, address, outputLineCount);
147+
stratum.withLineMapping(file.file, file.path, line, 1, address, outputLineCount);
148148
}
149149

150150
if (result.functions != null) {
@@ -156,11 +156,11 @@ private static Stratum parse(InputStream is) throws ParserException {
156156
}
157157
long startAddress = locations[0];
158158
long endAddress = locations[1];
159-
stratum.addFunctionMapping(functionName, startAddress, endAddress);
159+
stratum.withFunctionMapping(functionName, startAddress, endAddress);
160160
}
161161
}
162162

163-
return stratum.optimizeForLookups();
163+
return stratum.build();
164164
}
165165

166166
static SourceResult getSourceResult(InputStream is) {

dwarf-rust-experimental/src/test/java/com/dylibso/chicory/dwarf/rust/MachinesTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public void shouldEmitUnderstandableStackTracesCompiledAndInterpreted() throws E
139139
var countVowels = instance.export("count_vowels");
140140
var exception = assertThrows(TrapException.class, () -> countVowels.apply(0, -1));
141141
var exceptionTxt = readStackTrace(exception);
142-
System.out.println(exceptionTxt);
143142

144143
assertTrue(
145144
exceptionTxt.contains(

dwarf-rust-experimental/src/test/java/com/dylibso/chicory/dwarf/rust/RustParserTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.dylibso.chicory.dwarf.rust;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
43
import static org.junit.jupiter.api.Assertions.assertFalse;
54
import static org.junit.jupiter.api.Assertions.assertNotNull;
65
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
77

8-
import com.dylibso.chicory.runtime.internal.smap.SmapStratum;
98
import com.dylibso.chicory.wasm.Parser;
109
import java.io.File;
1110
import java.io.StringWriter;
@@ -19,17 +18,18 @@ public void shouldParseCountVowels() throws Exception {
1918
var module =
2019
Parser.parse(
2120
RustParserTest.class.getResourceAsStream("/compiled/count_vowels.rs.wasm"));
22-
var result = (SmapStratum) DebugParser.parse(module);
21+
var result = DebugParser.parse(module);
2322
assertNotNull(result);
2423

2524
// Just want peek at an entry and make sure it values look like
2625
// what you would be normal for a source line...
27-
var entry = result.lineData().get(200);
26+
var entry = result.getInputLine(300);
27+
assertNotNull(entry);
2828

2929
var writer = new StringWriter();
30-
writer.append("input file=").append(result.getPath(entry.lineFileID())).append("\n");
31-
writer.append("input line=").append(String.valueOf(entry.inputStartLine())).append("\n");
32-
writer.append("output line=").append(String.valueOf(entry.outputStartLine())).append("\n");
30+
writer.append("input file=").append(entry.fileName()).append("\n");
31+
writer.append("input path=").append(entry.filePath()).append("\n");
32+
writer.append("input line=").append(String.valueOf(entry.line())).append("\n");
3333

3434
Approvals.verify(writer.toString());
3535
}
@@ -51,8 +51,8 @@ public void shouldParseWasmSourceInfo() throws Exception {
5151

5252
// this file does not contain debug info
5353
var module = Parser.parse(new File("./rust/target/wasm32-wasip1/release/dwarf-rust.wasm"));
54-
var result = (SmapStratum) DebugParser.parse(module);
54+
var result = DebugParser.parse(module);
5555
assertNotNull(result);
56-
assertEquals(0, result.lineData().size());
56+
assertTrue(result.isEmpty());
5757
}
5858
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
input file=/rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library/core/src/fmt/num.rs
2-
input line=186
3-
output line=5914
1+
input file=error.rs
2+
input path=/rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library/core/src/str/error.rs
3+
input line=45

runtime/src/main/java/com/dylibso/chicory/runtime/InterpreterMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public InterpreterMachine(Instance instance) {
5151
} else if (instance.debugParser() != null) {
5252
stratum = instance.debugParser().apply(instance.module());
5353
} else {
54-
stratum = Stratum.create("WASM");
54+
stratum = Stratum.builder("WASM").build();
5555
}
5656
}
5757

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.dylibso.chicory.runtime;
2+
3+
public class LineMapping {
4+
5+
private final int lineFileID;
6+
private final long inputStartLine;
7+
private final long outputStartLine;
8+
private final int inputLineCount;
9+
private final int outputLineCount;
10+
11+
public LineMapping(
12+
int lineFileID,
13+
long inputStartLine,
14+
long outputStartLine,
15+
int inputLineCount,
16+
int outputLineCount) {
17+
this.inputLineCount = inputLineCount;
18+
this.outputLineCount = outputLineCount;
19+
this.lineFileID = lineFileID;
20+
this.inputStartLine = inputStartLine;
21+
this.outputStartLine = outputStartLine;
22+
}
23+
24+
public static Builder builder() {
25+
return new Builder();
26+
}
27+
28+
public Builder toBuilder() {
29+
Builder builder = new Builder();
30+
builder.lineFileID = lineFileID;
31+
builder.inputStartLine = inputStartLine;
32+
builder.outputStartLine = outputStartLine;
33+
builder.inputLineCount = inputLineCount;
34+
builder.outputLineCount = outputLineCount;
35+
return builder;
36+
}
37+
38+
public static final class Builder {
39+
int lineFileID;
40+
long inputStartLine = -1;
41+
long outputStartLine = -1;
42+
int inputLineCount = 1;
43+
int outputLineCount = 1;
44+
45+
private Builder() {}
46+
47+
public Builder withInputStartLine(long inputStartLine) {
48+
if (inputStartLine < 0) {
49+
throw new IllegalArgumentException("" + inputStartLine);
50+
}
51+
this.inputStartLine = inputStartLine;
52+
return this;
53+
}
54+
55+
public Builder withOutputStartLine(long outputStartLine) {
56+
if (outputStartLine < 0) {
57+
throw new IllegalArgumentException("" + outputStartLine);
58+
}
59+
this.outputStartLine = outputStartLine;
60+
return this;
61+
}
62+
63+
public Builder withLineFileID(int lineFileID) {
64+
if (lineFileID < 0) {
65+
throw new IllegalArgumentException("" + lineFileID);
66+
}
67+
this.lineFileID = lineFileID;
68+
return this;
69+
}
70+
71+
public Builder withInputLineCount(int value) {
72+
if (value < 0) {
73+
throw new IllegalArgumentException("" + value);
74+
}
75+
this.inputLineCount = (int) value;
76+
return this;
77+
}
78+
79+
public Builder withOutputLineCount(int value) {
80+
if (value < 0) {
81+
throw new IllegalArgumentException("" + value);
82+
}
83+
this.outputLineCount = (int) value;
84+
return this;
85+
}
86+
87+
public LineMapping build() {
88+
return new LineMapping(
89+
lineFileID, inputStartLine, outputStartLine, inputLineCount, outputLineCount);
90+
}
91+
92+
public long inputStartLine() {
93+
return inputStartLine;
94+
}
95+
96+
public long outputStartLine() {
97+
return outputStartLine;
98+
}
99+
100+
public int lineFileID() {
101+
return lineFileID;
102+
}
103+
104+
public int inputLineCount() {
105+
return inputLineCount;
106+
}
107+
108+
public int outputLineCount() {
109+
return outputLineCount;
110+
}
111+
}
112+
113+
public long inputStartLine() {
114+
return inputStartLine;
115+
}
116+
117+
public long outputStartLine() {
118+
return outputStartLine;
119+
}
120+
121+
public int lineFileID() {
122+
return lineFileID;
123+
}
124+
125+
public int inputLineCount() {
126+
return inputLineCount;
127+
}
128+
129+
public int outputLineCount() {
130+
return outputLineCount;
131+
}
132+
}

0 commit comments

Comments
 (0)