Search in sources :

Example 1 with WasmFunction

use of org.graalvm.wasm.WasmFunction in project graal by oracle.

the class WasmBlockNode method resolveCallNode.

@TruffleBoundary
public void resolveCallNode(int childOffset) {
    final WasmFunction function = ((WasmCallStubNode) children[childOffset]).function();
    final CallTarget target = instance().target(function.index());
    children[childOffset] = DirectCallNode.create(target);
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) WasmFunction(org.graalvm.wasm.WasmFunction) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 2 with WasmFunction

use of org.graalvm.wasm.WasmFunction in project graal by oracle.

the class WebAssembly method readModuleImports.

private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
    CompilerAsserts.neverPartOfCompilation();
    final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
    if (imports.getArraySize() != 0 && importObject == null) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
    }
    HashMap<String, ImportModule> importModules = new HashMap<>();
    final InteropLibrary lib = InteropLibrary.getUncached();
    try {
        int i = 0;
        while (i < imports.getArraySize()) {
            final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
            final Object importedModule = getMember(importObject, d.module());
            final Object member = getMember(importedModule, d.name());
            switch(d.kind()) {
                case function:
                    if (!lib.isExecutable(member)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
                    }
                    WasmFunction f = module.importedFunction(d.name());
                    ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
                    break;
                case memory:
                    if (!(member instanceof WasmMemory)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
                    }
                    ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
                    break;
                case table:
                    if (!(member instanceof WasmTable)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
                    }
                    ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
                    break;
                case global:
                    if (!(member instanceof WasmGlobal)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
                    }
                    ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
                    break;
                default:
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
            }
            i += 1;
        }
    } catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
        throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
    }
    return importModules;
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) HashMap(java.util.HashMap) WasmTable(org.graalvm.wasm.WasmTable) WasmFunction(org.graalvm.wasm.WasmFunction) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 3 with WasmFunction

use of org.graalvm.wasm.WasmFunction in project graal by oracle.

the class WebAssembly method instanceExport.

public static Object instanceExport(WasmInstance instance, String name) {
    CompilerAsserts.neverPartOfCompilation();
    WasmFunction function = instance.module().exportedFunctions().get(name);
    Integer globalIndex = instance.module().exportedGlobals().get(name);
    if (function != null) {
        return instance.functionInstance(function);
    } else if (globalIndex != null) {
        final int index = globalIndex;
        final int address = instance.globalAddress(index);
        if (address < 0) {
            return instance.context().globals().externalGlobal(address);
        } else {
            final ValueType valueType = ValueType.fromByteValue(instance.symbolTable().globalValueType(index));
            final boolean mutable = instance.symbolTable().isGlobalMutable(index);
            return new ExportedWasmGlobal(valueType, mutable, instance.context().globals(), address);
        }
    } else if (instance.module().exportedMemoryNames().contains(name)) {
        return instance.memory();
    } else if (instance.module().exportedTableNames().contains(name)) {
        return instance.table();
    } else {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, name + " is not a exported name of the given instance");
    }
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmFunction(org.graalvm.wasm.WasmFunction) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal)

Example 4 with WasmFunction

use of org.graalvm.wasm.WasmFunction in project graal by oracle.

the class BuiltinModule method defineExportedFunction.

protected void defineExportedFunction(WasmInstance instance, String name, byte[] paramType, byte[] retTypes, WasmFunctionInstance functionInstance) {
    final int typeIdx = instance.symbolTable().allocateFunctionType(paramType, retTypes);
    final WasmFunction function = instance.symbolTable().declareExportedFunction(typeIdx, name);
    instance.setFunctionInstance(function.index(), functionInstance);
    instance.setTarget(function.index(), functionInstance.target());
}
Also used : WasmFunction(org.graalvm.wasm.WasmFunction)

Example 5 with WasmFunction

use of org.graalvm.wasm.WasmFunction in project graal by oracle.

the class BuiltinModule method defineFunction.

protected void defineFunction(WasmInstance instance, String name, byte[] paramTypes, byte[] retTypes, RootNode rootNode) {
    // We could check if the same function type had already been allocated,
    // but this is just an optimization, and probably not very important,
    // since predefined modules have a relatively small size.
    final int typeIdx = instance.symbolTable().allocateFunctionType(paramTypes, retTypes);
    final WasmFunction function = instance.symbolTable().declareExportedFunction(typeIdx, name);
    instance.setTarget(function.index(), rootNode.getCallTarget());
}
Also used : WasmFunction(org.graalvm.wasm.WasmFunction)

Aggregations

WasmFunction (org.graalvm.wasm.WasmFunction)10 WasmTable (org.graalvm.wasm.WasmTable)3 WasmMemory (org.graalvm.wasm.memory.WasmMemory)3 CallTarget (com.oracle.truffle.api.CallTarget)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SymbolTable (org.graalvm.wasm.SymbolTable)2 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)2 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)2 ExportedWasmGlobal (org.graalvm.wasm.globals.ExportedWasmGlobal)2 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 BytecodeInterpreterSwitch (com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitch)1 BytecodeInterpreterSwitchBoundary (com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitchBoundary)1 TruffleContext (com.oracle.truffle.api.TruffleContext)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 ExplodeLoop (com.oracle.truffle.api.nodes.ExplodeLoop)1