diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-21 16:45:59 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-21 16:45:59 +0000 |
| commit | b51059a7cbec54cc89811dd8e78d1bd3e55ca081 (patch) | |
| tree | cb63d4dba613c4a8da692d206541ce750527c34b /src/main/java | |
| parent | 5e38469d9a6e71a41fd3b489777af27172a85f9c (diff) | |
Extract explicit notion of WasmRelocationSection
Diffstat (limited to 'src/main/java')
3 files changed, 38 insertions, 15 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java index 255cc52e..34b98eca 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java @@ -34,10 +34,9 @@ public class WasmBinaryFormatLinking { SymbolTable symbolTable, WasmBinaryWriter writer ) throws IOException { - for (var relocationEntriesForSection : writer.relocationEntries().entrySet()) { + for (var relocationSection : writer.relocationSections()) { writeRelocationSection( - relocationEntriesForSection.getKey(), - relocationEntriesForSection.getValue(), + relocationSection, symbolTable, writer ); @@ -45,16 +44,14 @@ public class WasmBinaryFormatLinking { } private static void writeRelocationSection( - int sectionIndex, - List<WasmRelocationEntry> relocationEntries, + WasmRelocationSection relocationSection, SymbolTable symbolTable, WasmBinaryWriter writer ) throws IOException { - // TODO: don't assume CODE - writeCustomSection("reloc.CODE", writer, () -> { - writer.writeUnsignedInt32(sectionIndex); + writeCustomSection("reloc." + relocationSection.relocatedSectionName(), writer, () -> { + writer.writeUnsignedInt32(relocationSection.relocatedSectionIndex()); writer.writeVec( - relocationEntries, + relocationSection.entries(), relocationEntry -> writeRelocationEntry(relocationEntry, symbolTable, writer) ); }); 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 f13bd2f1..c8fc458b 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 @@ -14,7 +14,7 @@ import java.util.*; public class WasmBinaryWriter { private final BinaryWriter writer = new BinaryWriter(); - private final Map<Integer, List<WasmRelocationEntry>> relocationEntries = new HashMap<>(); + private final Map<Integer, WasmRelocationSection> relocationSections = new HashMap<>(); private final WasmIndexTable indexTable; private Optional<WasmSectionId> currentSection = Optional.empty(); private int sectionIndex = -1; @@ -131,10 +131,11 @@ public class WasmBinaryWriter { writePaddedUnsignedInt32(value); } - if (!relocationEntries.containsKey(sectionIndex)) { - relocationEntries.put(sectionIndex, new ArrayList<>()); + if (!relocationSections.containsKey(sectionIndex)) { + // TODO: don't assume CODE + relocationSections.put(sectionIndex, WasmRelocationSection.empty(sectionIndex, "CODE")); } - relocationEntries.get(sectionIndex).add(new WasmRelocationEntry( + relocationSections.get(sectionIndex).addEntry(new WasmRelocationEntry( relocationType, sectionContentsOffset, index @@ -192,8 +193,8 @@ public class WasmBinaryWriter { WasmBinaryFormatSize.writeSize(writer, writeContents); } - public Map<Integer, List<WasmRelocationEntry>> relocationEntries() { - return relocationEntries; + public Collection<WasmRelocationSection> relocationSections() { + return relocationSections.values(); } public byte[] toBytes() { diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationSection.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationSection.java new file mode 100644 index 00000000..120b9fab --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationSection.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import java.util.ArrayList; +import java.util.List; + +public record WasmRelocationSection( + int relocatedSectionIndex, + String relocatedSectionName, + List<WasmRelocationEntry> entries +) { + public static WasmRelocationSection empty( + int relocatedSectionIndex, + String relocatedSectionName + ) { + return new WasmRelocationSection( + relocatedSectionIndex, + relocatedSectionName, + new ArrayList<>() + ); + } + + public void addEntry(WasmRelocationEntry entry) { + this.entries.add(entry); + } +} |
