From b51059a7cbec54cc89811dd8e78d1bd3e55ca081 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 21 Dec 2023 16:45:59 +0000 Subject: Extract explicit notion of WasmRelocationSection --- .../structure/binary/WasmBinaryFormatLinking.java | 15 ++++++------- .../wasm/structure/binary/WasmBinaryWriter.java | 13 +++++------ .../structure/binary/WasmRelocationSection.java | 25 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmRelocationSection.java (limited to 'src/main/java') 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 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> relocationEntries = new HashMap<>(); + private final Map relocationSections = new HashMap<>(); private final WasmIndexTable indexTable; private Optional 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> relocationEntries() { - return relocationEntries; + public Collection 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 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); + } +} -- cgit v1.2.3