From 5d987fd53baf2dd6632cc6db8d7acea7633f6780 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Jun 2023 14:48:50 +0100 Subject: Don't write empty symbol table --- .../structure/binary/WasmBinaryFormatLinking.java | 55 +++++++++++++--------- 1 file changed, 32 insertions(+), 23 deletions(-) (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 1023206e..3cb75953 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 @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmInitFunc; import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmSymbolIndex; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex; @@ -38,12 +39,8 @@ public class WasmBinaryFormatLinking { var symbolTable = generateSymbolTable(module); writeSegmentInfoSubsection(module, writer); - writeSymbolTableSubsection(symbolTable, writer); - - if (module.initFuncs().size() > 0) { - writeInitFuncsSubsection(module, symbolTable, writer); - } + writeInitFuncsSubsection(module, symbolTable, writer); }); } @@ -93,30 +90,42 @@ public class WasmBinaryFormatLinking { SymbolTable symbolTable, BinaryWriter writer ) throws IOException { - writeSubsection(SubsectionType.WASM_INIT_FUNCS, writer, () -> { - writeVec( - module.initFuncs(), - initFunc -> { - Leb128Encoding.encodeUnsignedInt32(initFunc.priority(), writer); - var symbolIndex = symbolTable.funcIndexToSymbolIndex(initFunc.funcIndex()); - writeSymbolIndex(symbolIndex, writer); - }, - writer - ); - }); + if (module.initFuncs().size() > 0) { + writeSubsection(SubsectionType.WASM_INIT_FUNCS, writer, () -> { + writeVec( + module.initFuncs(), + initFunc -> { + writeInitFunc(initFunc, symbolTable, writer); + }, + writer + ); + }); + } + } + + private static void writeInitFunc( + WasmInitFunc initFunc, + SymbolTable symbolTable, + BinaryWriter writer + ) { + Leb128Encoding.encodeUnsignedInt32(initFunc.priority(), writer); + var symbolIndex = symbolTable.funcIndexToSymbolIndex(initFunc.funcIndex()); + writeSymbolIndex(symbolIndex, writer); } private static void writeSymbolTableSubsection( SymbolTable symbolTable, BinaryWriter writer ) throws IOException { - writeSubsection(SubsectionType.WASM_SYMBOL_TABLE, writer, () -> { - writeVec( - symbolTable.symbolInfos, - symbolInfo -> writeSymbolInfo(symbolInfo, writer), - writer - ); - }); + if (symbolTable.symbolInfos.size() > 0) { + writeSubsection(SubsectionType.WASM_SYMBOL_TABLE, writer, () -> { + writeVec( + symbolTable.symbolInfos, + symbolInfo -> writeSymbolInfo(symbolInfo, writer), + writer + ); + }); + } } private static void writeSymbolInfo(SymbolInfo symbolInfo, BinaryWriter writer) { -- cgit v1.2.3