Search in sources :

Example 1 with DataSym

use of org.graalvm.wasm.Linker.ResolutionDag.DataSym in project graal by oracle.

the class Linker method resolveDataSegment.

void resolveDataSegment(WasmContext context, WasmInstance instance, int dataSegmentId, int offsetAddress, int offsetGlobalIndex, int byteLength, byte[] data) {
    assertTrue(instance.symbolTable().memoryExists(), String.format("No memory declared or imported in the module '%s'", instance.name()), Failure.UNSPECIFIED_MALFORMED);
    final Runnable resolveAction = () -> {
        WasmMemory memory = instance.memory();
        Assert.assertNotNull(memory, String.format("No memory declared or imported in the module '%s'", instance.name()), Failure.UNSPECIFIED_MALFORMED);
        int baseAddress;
        if (offsetGlobalIndex != -1) {
            final int offsetGlobalAddress = instance.globalAddress(offsetGlobalIndex);
            assertTrue(offsetGlobalAddress != -1, "The global variable '" + offsetGlobalIndex + "' for the offset of the data segment " + dataSegmentId + " in module '" + instance.name() + "' was not initialized.", Failure.UNSPECIFIED_MALFORMED);
            baseAddress = context.globals().loadAsInt(offsetGlobalAddress);
        } else {
            baseAddress = offsetAddress;
        }
        Assert.assertUnsignedIntLessOrEqual(baseAddress, WasmMath.toUnsignedIntExact(memory.byteSize()), Failure.DATA_SEGMENT_DOES_NOT_FIT);
        Assert.assertUnsignedIntLessOrEqual(baseAddress + byteLength, WasmMath.toUnsignedIntExact(memory.byteSize()), Failure.DATA_SEGMENT_DOES_NOT_FIT);
        for (int writeOffset = 0; writeOffset != byteLength; ++writeOffset) {
            byte b = data[writeOffset];
            memory.store_i32_8(null, baseAddress + writeOffset, b);
        }
    };
    final ArrayList<Sym> dependencies = new ArrayList<>();
    if (instance.symbolTable().importedMemory() != null) {
        dependencies.add(new ImportMemorySym(instance.name(), instance.symbolTable().importedMemory()));
    }
    if (dataSegmentId > 0) {
        dependencies.add(new DataSym(instance.name(), dataSegmentId - 1));
    }
    if (offsetGlobalIndex != -1) {
        dependencies.add(new InitializeGlobalSym(instance.name(), offsetGlobalIndex));
    }
    resolutionDag.resolveLater(new DataSym(instance.name(), dataSegmentId), dependencies.toArray(new Sym[dependencies.size()]), resolveAction);
}
Also used : DataSym(org.graalvm.wasm.Linker.ResolutionDag.DataSym) ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ElemSym(org.graalvm.wasm.Linker.ResolutionDag.ElemSym) ExportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym) ExportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ExportTableSym) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym) ExportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ExportFunctionSym) CodeEntrySym(org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym) CallsiteSym(org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym) ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ExportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym) ImportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym) Sym(org.graalvm.wasm.Linker.ResolutionDag.Sym) ImportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym) ImportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym) DataSym(org.graalvm.wasm.Linker.ResolutionDag.DataSym) ArrayList(java.util.ArrayList) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym) WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Aggregations

ArrayList (java.util.ArrayList)1 CallsiteSym (org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym)1 CodeEntrySym (org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym)1 DataSym (org.graalvm.wasm.Linker.ResolutionDag.DataSym)1 ElemSym (org.graalvm.wasm.Linker.ResolutionDag.ElemSym)1 ExportFunctionSym (org.graalvm.wasm.Linker.ResolutionDag.ExportFunctionSym)1 ExportGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym)1 ExportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)1 ExportTableSym (org.graalvm.wasm.Linker.ResolutionDag.ExportTableSym)1 ImportFunctionSym (org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym)1 ImportGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym)1 ImportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym)1 ImportTableSym (org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym)1 InitializeGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym)1 Sym (org.graalvm.wasm.Linker.ResolutionDag.Sym)1 WasmMemory (org.graalvm.wasm.memory.WasmMemory)1