aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-21 16:37:44 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-21 16:37:44 +0000
commit2242b12c09562b0227c8ee875750930f42b06b50 (patch)
tree5e5724af6214ec75b10edefd234d69b353e5ae70 /src/main/java
parent4ff129c58d805a6c5887c1d6a1063a238e37d70b (diff)
Check that we're not using placeholder unrelocatable values
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java
index bb931c65..0c35db7e 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java
@@ -60,7 +60,7 @@ public class WasmBinaryWriter {
var funcIndex = indexTable.funcIndex(funcName);
writeRelocatableIndex(
WasmRelocationType.FUNCTION_INDEX_LEB,
- funcIndex.value(),
+ OptionalInt.of(funcIndex.value()),
new WasmRelocatableIndex.FuncIndex(funcName)
);
}
@@ -69,7 +69,7 @@ public class WasmBinaryWriter {
var globalIndex = indexTable.globalIndex(globalName);
writeRelocatableIndex(
WasmRelocationType.GLOBAL_INDEX_LEB,
- globalIndex.value(),
+ OptionalInt.of(globalIndex.value()),
new WasmRelocatableIndex.GlobalIndex(globalName)
);
}
@@ -77,8 +77,7 @@ public class WasmBinaryWriter {
public void writeMemoryAddressConst(WasmMemoryAddress index) throws IOException {
writeRelocatableIndex(
WasmRelocationType.MEMORY_ADDR_SLEB,
- // TODO: use actual value? Or throw if not in relocatable section?
- 0,
+ OptionalInt.empty(),
new WasmRelocatableIndex.MemoryAddress(index)
);
}
@@ -90,8 +89,7 @@ public class WasmBinaryWriter {
public void writeTableElementIndexConst(WasmFuncName funcName) throws IOException {
writeRelocatableIndex(
WasmRelocationType.TABLE_INDEX_SLEB,
- // TODO: use actual value? Or throw if not in relocatable section?
- 0,
+ OptionalInt.empty(),
new WasmRelocatableIndex.TableElementIndex(funcName)
);
}
@@ -100,7 +98,7 @@ public class WasmBinaryWriter {
var tagIndex = indexTable.tagIndex(tag);
writeRelocatableIndex(
WasmRelocationType.TAG_INDEX_LEB,
- tagIndex.value(),
+ OptionalInt.of(tagIndex.value()),
new WasmRelocatableIndex.TagIndex(tag)
);
}
@@ -109,14 +107,14 @@ public class WasmBinaryWriter {
var typeIndex = indexTable.typeIndex(funcType);
writeRelocatableIndex(
WasmRelocationType.TYPE_INDEX_LEB,
- typeIndex.value(),
+ OptionalInt.of(typeIndex.value()),
new WasmRelocatableIndex.TypeIndex(funcType)
);
}
private void writeRelocatableIndex(
WasmRelocationType relocationType,
- int unrelocatedIndex,
+ OptionalInt unrelocatedIndex,
WasmRelocatableIndex index
) throws IOException {
// TODO: handle custom sections?
@@ -125,11 +123,12 @@ public class WasmBinaryWriter {
.orElse(false);
if (isRelocatable) {
+ var value = unrelocatedIndex.orElse(0);
var sectionContentsOffset = writer.position() - sectionContentsStart;
if (relocationType.isSigned()) {
- writePaddedSignedInt32(unrelocatedIndex);
+ writePaddedSignedInt32(value);
} else {
- writePaddedUnsignedInt32(unrelocatedIndex);
+ writePaddedUnsignedInt32(value);
}
if (!relocationEntries.containsKey(sectionIndex)) {
@@ -141,11 +140,13 @@ public class WasmBinaryWriter {
sectionContentsOffset,
index
));
+ } else if (unrelocatedIndex.isEmpty()) {
+ throw new IllegalStateException("cannot write memory address outside of relocatable section");
} else {
if (relocationType.isSigned()) {
- writeSignedInt32(unrelocatedIndex);
+ writeSignedInt32(unrelocatedIndex.getAsInt());
} else {
- writeUnsignedInt32(unrelocatedIndex);
+ writeUnsignedInt32(unrelocatedIndex.getAsInt());
}
}
}