Changeset 224020 in webkit


Ignore:
Timestamp:
Oct 26, 2017 8:33:55 AM (6 years ago)
Author:
jfbastien@apple.com
Message:

WebAssembly: no VM / JS version of our implementation
https://bugs.webkit.org/show_bug.cgi?id=177472

Reviewed by Michael Saboff.

This patch removes all appearances of "JS" and "VM" in the wasm
directory. These now only appear in the wasm/js directory, which
is only used in a JS embedding of wasm. It should therefore now be
possible to create non-JS embeddings of wasm through JSC, though
it'll still require:

  • Mild codegen for wasm<->embedder calls;
  • A strategy for trap handling (no need for full unwind! Could kill).
  • Creation of the Wasm::* objects.
  • Calling convention handling to call the embedder.
  • Handling of multiple embedders (see #177475, this is optional).

Most of the patch consists in renaming JSWebAssemblyInstance to
Instance, and removing temporary copies which I'd added to make
this specific patch very simple.

  • interpreter/CallFrame.cpp:

(JSC::CallFrame::wasmAwareLexicalGlobalObject): this one place
which needs to know about who "owns" the Wasm::Instance. In a JS
embedding it's the JSWebAssemblyInstance.

  • wasm/WasmB3IRGenerator.cpp:

(JSC::Wasm::B3IRGenerator::B3IRGenerator):
(JSC::Wasm::B3IRGenerator::restoreWebAssemblyGlobalState):
(JSC::Wasm::B3IRGenerator::addGrowMemory):
(JSC::Wasm::B3IRGenerator::addCurrentMemory):
(JSC::Wasm::B3IRGenerator::getGlobal):
(JSC::Wasm::B3IRGenerator::setGlobal):
(JSC::Wasm::B3IRGenerator::addCall):
(JSC::Wasm::B3IRGenerator::addCallIndirect):

  • wasm/WasmBinding.cpp:

(JSC::Wasm::wasmToWasm):

  • wasm/WasmContext.cpp:

(JSC::Wasm::Context::load const):
(JSC::Wasm::Context::store):

  • wasm/WasmContext.h:
  • wasm/WasmEmbedder.h:
  • wasm/WasmInstance.cpp:

(JSC::Wasm::Instance::Instance):
(JSC::Wasm::Instance::create):
(JSC::Wasm::Instance::extraMemoryAllocated const):

  • wasm/WasmInstance.h: add an "owner", the Wasm::Context, move the

"tail" import information from JSWebAssemblyInstance over to here.
(JSC::Wasm::Instance::finalizeCreation):
(JSC::Wasm::Instance::owner const):
(JSC::Wasm::Instance::offsetOfOwner):
(JSC::Wasm::Instance::context const):
(JSC::Wasm::Instance::setMemory):
(JSC::Wasm::Instance::setTable):
(JSC::Wasm::Instance::offsetOfMemory):
(JSC::Wasm::Instance::offsetOfGlobals):
(JSC::Wasm::Instance::offsetOfTable):
(JSC::Wasm::Instance::offsetOfTail):
(JSC::Wasm::Instance::numImportFunctions const):
(JSC::Wasm::Instance::importFunctionInfo):
(JSC::Wasm::Instance::offsetOfTargetInstance):
(JSC::Wasm::Instance::offsetOfWasmEntrypoint):
(JSC::Wasm::Instance::offsetOfWasmToEmbedderStubExecutableAddress):
(JSC::Wasm::Instance::offsetOfImportFunction):
(JSC::Wasm::Instance::importFunction):
(JSC::Wasm::Instance::allocationSize):
(JSC::Wasm::Instance::create): Deleted.

  • wasm/WasmOMGPlan.cpp:

(JSC::Wasm::OMGPlan::runForIndex):

  • wasm/WasmOMGPlan.h:
  • wasm/WasmTable.cpp:

(JSC::Wasm::Table::Table):
(JSC::Wasm::Table::setFunction):

  • wasm/WasmTable.h:
  • wasm/WasmThunks.cpp:

(JSC::Wasm::throwExceptionFromWasmThunkGenerator):
(JSC::Wasm::triggerOMGTierUpThunkGenerator):

  • wasm/js/JSToWasm.cpp:

(JSC::Wasm::createJSToWasmWrapper):

  • wasm/js/JSWebAssemblyInstance.cpp: delete code that is now on Wasm::Instance

(JSC::JSWebAssemblyInstance::JSWebAssemblyInstance): The embedder
decides what the import function is. Here we must properly
placement-new it to what we've elected (and initialize it later).
(JSC::JSWebAssemblyInstance::visitChildren):
(JSC::JSWebAssemblyInstance::finalizeCreation):
(JSC::JSWebAssemblyInstance::create):

  • wasm/js/JSWebAssemblyInstance.h: delete code that is now on Wasm::Instance

(JSC::JSWebAssemblyInstance::instance):
(JSC::JSWebAssemblyInstance::moduleNamespaceObject):
(JSC::JSWebAssemblyInstance::setMemory):
(JSC::JSWebAssemblyInstance::table):
(JSC::JSWebAssemblyInstance::setTable):
(JSC::JSWebAssemblyInstance::offsetOfInstance):
(JSC::JSWebAssemblyInstance::offsetOfCallee):
(JSC::JSWebAssemblyInstance::context const): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfTail): Deleted.
(): Deleted.
(JSC::JSWebAssemblyInstance::importFunctionInfo): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfTargetInstance): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfWasmEntrypoint): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfWasmToEmbedderStubExecutableAddress): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfImportFunction): Deleted.
(JSC::JSWebAssemblyInstance::importFunction): Deleted.
(JSC::JSWebAssemblyInstance::internalMemory): Deleted.
(JSC::JSWebAssemblyInstance::wasmCodeBlock const): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfWasmTable): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfGlobals): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfCodeBlock): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfWasmCodeBlock): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfCachedStackLimit): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfWasmMemory): Deleted.
(JSC::JSWebAssemblyInstance::offsetOfTopEntryFramePointer): Deleted.
(JSC::JSWebAssemblyInstance::cachedStackLimit const): Deleted.
(JSC::JSWebAssemblyInstance::setCachedStackLimit): Deleted.
(JSC::JSWebAssemblyInstance::wasmMemory): Deleted.
(JSC::JSWebAssemblyInstance::wasmModule): Deleted.
(JSC::JSWebAssemblyInstance::allocationSize): Deleted.

  • wasm/js/JSWebAssemblyTable.cpp:

(JSC::JSWebAssemblyTable::setFunction):

  • wasm/js/WasmToJS.cpp: One extra indirection to find the JSWebAssemblyInstance.

(JSC::Wasm::materializeImportJSCell):
(JSC::Wasm::handleBadI64Use):
(JSC::Wasm::wasmToJS):
(JSC::Wasm::wasmToJSException):

  • wasm/js/WasmToJS.h:
  • wasm/js/WebAssemblyFunction.cpp:

(JSC::callWebAssemblyFunction):

  • wasm/js/WebAssemblyInstanceConstructor.cpp:

(JSC::constructJSWebAssemblyInstance):

  • wasm/js/WebAssemblyModuleRecord.cpp:

(JSC::WebAssemblyModuleRecord::link):
(JSC::WebAssemblyModuleRecord::evaluate):

  • wasm/js/WebAssemblyPrototype.cpp:

(JSC::instantiate):

  • wasm/js/WebAssemblyWrapperFunction.cpp:

(JSC::WebAssemblyWrapperFunction::create):

Location:
trunk/Source/JavaScriptCore
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r224002 r224020  
     12017-10-26  JF Bastien  <jfbastien@apple.com>
     2
     3        WebAssembly: no VM / JS version of our implementation
     4        https://bugs.webkit.org/show_bug.cgi?id=177472
     5
     6        Reviewed by Michael Saboff.
     7
     8        This patch removes all appearances of "JS" and "VM" in the wasm
     9        directory. These now only appear in the wasm/js directory, which
     10        is only used in a JS embedding of wasm. It should therefore now be
     11        possible to create non-JS embeddings of wasm through JSC, though
     12        it'll still require:
     13
     14          - Mild codegen for wasm<->embedder calls;
     15          - A strategy for trap handling (no need for full unwind! Could kill).
     16          - Creation of the Wasm::* objects.
     17          - Calling convention handling to call the embedder.
     18          - Handling of multiple embedders (see #177475, this is optional).
     19
     20        Most of the patch consists in renaming JSWebAssemblyInstance to
     21        Instance, and removing temporary copies which I'd added to make
     22        this specific patch very simple.
     23
     24        * interpreter/CallFrame.cpp:
     25        (JSC::CallFrame::wasmAwareLexicalGlobalObject): this one place
     26        which needs to know about who "owns" the Wasm::Instance. In a JS
     27        embedding it's the JSWebAssemblyInstance.
     28        * wasm/WasmB3IRGenerator.cpp:
     29        (JSC::Wasm::B3IRGenerator::B3IRGenerator):
     30        (JSC::Wasm::B3IRGenerator::restoreWebAssemblyGlobalState):
     31        (JSC::Wasm::B3IRGenerator::addGrowMemory):
     32        (JSC::Wasm::B3IRGenerator::addCurrentMemory):
     33        (JSC::Wasm::B3IRGenerator::getGlobal):
     34        (JSC::Wasm::B3IRGenerator::setGlobal):
     35        (JSC::Wasm::B3IRGenerator::addCall):
     36        (JSC::Wasm::B3IRGenerator::addCallIndirect):
     37        * wasm/WasmBinding.cpp:
     38        (JSC::Wasm::wasmToWasm):
     39        * wasm/WasmContext.cpp:
     40        (JSC::Wasm::Context::load const):
     41        (JSC::Wasm::Context::store):
     42        * wasm/WasmContext.h:
     43        * wasm/WasmEmbedder.h:
     44        * wasm/WasmInstance.cpp:
     45        (JSC::Wasm::Instance::Instance):
     46        (JSC::Wasm::Instance::create):
     47        (JSC::Wasm::Instance::extraMemoryAllocated const):
     48        * wasm/WasmInstance.h: add an "owner", the Wasm::Context, move the
     49        "tail" import information from JSWebAssemblyInstance over to here.
     50        (JSC::Wasm::Instance::finalizeCreation):
     51        (JSC::Wasm::Instance::owner const):
     52        (JSC::Wasm::Instance::offsetOfOwner):
     53        (JSC::Wasm::Instance::context const):
     54        (JSC::Wasm::Instance::setMemory):
     55        (JSC::Wasm::Instance::setTable):
     56        (JSC::Wasm::Instance::offsetOfMemory):
     57        (JSC::Wasm::Instance::offsetOfGlobals):
     58        (JSC::Wasm::Instance::offsetOfTable):
     59        (JSC::Wasm::Instance::offsetOfTail):
     60        (JSC::Wasm::Instance::numImportFunctions const):
     61        (JSC::Wasm::Instance::importFunctionInfo):
     62        (JSC::Wasm::Instance::offsetOfTargetInstance):
     63        (JSC::Wasm::Instance::offsetOfWasmEntrypoint):
     64        (JSC::Wasm::Instance::offsetOfWasmToEmbedderStubExecutableAddress):
     65        (JSC::Wasm::Instance::offsetOfImportFunction):
     66        (JSC::Wasm::Instance::importFunction):
     67        (JSC::Wasm::Instance::allocationSize):
     68        (JSC::Wasm::Instance::create): Deleted.
     69        * wasm/WasmOMGPlan.cpp:
     70        (JSC::Wasm::OMGPlan::runForIndex):
     71        * wasm/WasmOMGPlan.h:
     72        * wasm/WasmTable.cpp:
     73        (JSC::Wasm::Table::Table):
     74        (JSC::Wasm::Table::setFunction):
     75        * wasm/WasmTable.h:
     76        * wasm/WasmThunks.cpp:
     77        (JSC::Wasm::throwExceptionFromWasmThunkGenerator):
     78        (JSC::Wasm::triggerOMGTierUpThunkGenerator):
     79        * wasm/js/JSToWasm.cpp:
     80        (JSC::Wasm::createJSToWasmWrapper):
     81        * wasm/js/JSWebAssemblyInstance.cpp: delete code that is now on Wasm::Instance
     82        (JSC::JSWebAssemblyInstance::JSWebAssemblyInstance): The embedder
     83        decides what the import function is. Here we must properly
     84        placement-new it to what we've elected (and initialize it later).
     85        (JSC::JSWebAssemblyInstance::visitChildren):
     86        (JSC::JSWebAssemblyInstance::finalizeCreation):
     87        (JSC::JSWebAssemblyInstance::create):
     88        * wasm/js/JSWebAssemblyInstance.h: delete code that is now on Wasm::Instance
     89        (JSC::JSWebAssemblyInstance::instance):
     90        (JSC::JSWebAssemblyInstance::moduleNamespaceObject):
     91        (JSC::JSWebAssemblyInstance::setMemory):
     92        (JSC::JSWebAssemblyInstance::table):
     93        (JSC::JSWebAssemblyInstance::setTable):
     94        (JSC::JSWebAssemblyInstance::offsetOfInstance):
     95        (JSC::JSWebAssemblyInstance::offsetOfCallee):
     96        (JSC::JSWebAssemblyInstance::context const): Deleted.
     97        (JSC::JSWebAssemblyInstance::offsetOfTail): Deleted.
     98        (): Deleted.
     99        (JSC::JSWebAssemblyInstance::importFunctionInfo): Deleted.
     100        (JSC::JSWebAssemblyInstance::offsetOfTargetInstance): Deleted.
     101        (JSC::JSWebAssemblyInstance::offsetOfWasmEntrypoint): Deleted.
     102        (JSC::JSWebAssemblyInstance::offsetOfWasmToEmbedderStubExecutableAddress): Deleted.
     103        (JSC::JSWebAssemblyInstance::offsetOfImportFunction): Deleted.
     104        (JSC::JSWebAssemblyInstance::importFunction): Deleted.
     105        (JSC::JSWebAssemblyInstance::internalMemory): Deleted.
     106        (JSC::JSWebAssemblyInstance::wasmCodeBlock const): Deleted.
     107        (JSC::JSWebAssemblyInstance::offsetOfWasmTable): Deleted.
     108        (JSC::JSWebAssemblyInstance::offsetOfGlobals): Deleted.
     109        (JSC::JSWebAssemblyInstance::offsetOfCodeBlock): Deleted.
     110        (JSC::JSWebAssemblyInstance::offsetOfWasmCodeBlock): Deleted.
     111        (JSC::JSWebAssemblyInstance::offsetOfCachedStackLimit): Deleted.
     112        (JSC::JSWebAssemblyInstance::offsetOfWasmMemory): Deleted.
     113        (JSC::JSWebAssemblyInstance::offsetOfTopEntryFramePointer): Deleted.
     114        (JSC::JSWebAssemblyInstance::cachedStackLimit const): Deleted.
     115        (JSC::JSWebAssemblyInstance::setCachedStackLimit): Deleted.
     116        (JSC::JSWebAssemblyInstance::wasmMemory): Deleted.
     117        (JSC::JSWebAssemblyInstance::wasmModule): Deleted.
     118        (JSC::JSWebAssemblyInstance::allocationSize): Deleted.
     119        * wasm/js/JSWebAssemblyTable.cpp:
     120        (JSC::JSWebAssemblyTable::setFunction):
     121        * wasm/js/WasmToJS.cpp: One extra indirection to find the JSWebAssemblyInstance.
     122        (JSC::Wasm::materializeImportJSCell):
     123        (JSC::Wasm::handleBadI64Use):
     124        (JSC::Wasm::wasmToJS):
     125        (JSC::Wasm::wasmToJSException):
     126        * wasm/js/WasmToJS.h:
     127        * wasm/js/WebAssemblyFunction.cpp:
     128        (JSC::callWebAssemblyFunction):
     129        * wasm/js/WebAssemblyInstanceConstructor.cpp:
     130        (JSC::constructJSWebAssemblyInstance):
     131        * wasm/js/WebAssemblyModuleRecord.cpp:
     132        (JSC::WebAssemblyModuleRecord::link):
     133        (JSC::WebAssemblyModuleRecord::evaluate):
     134        * wasm/js/WebAssemblyPrototype.cpp:
     135        (JSC::instantiate):
     136        * wasm/js/WebAssemblyWrapperFunction.cpp:
     137        (JSC::WebAssemblyWrapperFunction::create):
     138
    11392017-10-25  Devin Rousso  <webkit@devinrousso.com>
    2140
  • trunk/Source/JavaScriptCore/interpreter/CallFrame.cpp

    r223738 r224020  
    3434#include "VMEntryScope.h"
    3535#include "WasmContext.h"
     36#include "WasmInstance.h"
    3637#include <wtf/StringPrintStream.h>
    3738
     
    219220    if (!callee().isWasm())
    220221        return lexicalGlobalObject();
    221     return vm.wasmContext.load()->globalObject();
     222    return vm.wasmContext.load()->owner<JSWebAssemblyInstance>()->globalObject();
    222223#else
    223224    UNUSED_PARAM(vm);
  • trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp

    r223907 r224020  
    4949#include "B3WasmBoundsCheckValue.h"
    5050#include "JSCInlines.h"
    51 #include "JSWebAssemblyInstance.h"
    5251#include "ScratchRegisterAllocator.h"
    5352#include "VirtualRegister.h"
     
    5655#include "WasmExceptionType.h"
    5756#include "WasmFunctionParser.h"
     57#include "WasmInstance.h"
    5858#include "WasmMemory.h"
    5959#include "WasmOMGPlan.h"
     
    429429                    jit.loadWasmContextInstance(contextInstance);
    430430
    431                 jit.loadPtr(CCallHelpers::Address(contextInstance, JSWebAssemblyInstance::offsetOfCachedStackLimit()), scratch2);
     431                jit.loadPtr(CCallHelpers::Address(contextInstance, Instance::offsetOfCachedStackLimit()), scratch2);
    432432                jit.addPtr(CCallHelpers::TrustedImm32(-checkSize), fp, scratch1);
    433433                MacroAssembler::JumpList overflow;
     
    473473        patchpoint->setGenerator([pinnedRegs] (CCallHelpers& jit, const B3::StackmapGenerationParams& params) {
    474474            GPRReg baseMemory = pinnedRegs->baseMemoryPointer;
    475             jit.loadPtr(CCallHelpers::Address(params[0].gpr(), JSWebAssemblyInstance::offsetOfWasmMemory()), baseMemory);
     475            jit.loadPtr(CCallHelpers::Address(params[0].gpr(), Instance::offsetOfMemory()), baseMemory);
    476476            const auto& sizeRegs = pinnedRegs->sizeRegisters;
    477477            ASSERT(sizeRegs.size() >= 1);
     
    556556auto B3IRGenerator::addGrowMemory(ExpressionType delta, ExpressionType& result) -> PartialResult
    557557{
    558     int32_t (*growMemory)(JSWebAssemblyInstance*, int32_t) = [] (JSWebAssemblyInstance* instance, int32_t delta) -> int32_t {
     558    int32_t (*growMemory)(Instance*, int32_t) = [] (Instance* instance, int32_t delta) -> int32_t {
    559559        if (delta < 0)
    560560            return -1;
    561561
    562         auto grown = instance->internalMemory().grow(PageCount(delta));
     562        auto grown = instance->memory()->grow(PageCount(delta));
    563563        if (!grown) {
    564564            switch (grown.error()) {
     
    585585auto B3IRGenerator::addCurrentMemory(ExpressionType& result) -> PartialResult
    586586{
    587     Value* memoryObject = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfWasmMemory()));
     587    Value* memoryObject = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(Instance::offsetOfMemory()));
    588588
    589589    static_assert(sizeof(decltype(static_cast<Memory*>(nullptr)->size())) == sizeof(uint64_t), "codegen relies on this size");
     
    609609auto B3IRGenerator::getGlobal(uint32_t index, ExpressionType& result) -> PartialResult
    610610{
    611     Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfGlobals()));
     611    Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(Instance::offsetOfGlobals()));
    612612    result = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, toB3Type(m_info.globals[index].type), origin(), globalsArray, safeCast<int32_t>(index * sizeof(Register)));
    613613    return { };
     
    617617{
    618618    ASSERT(toB3Type(m_info.globals[index].type) == value->type());
    619     Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfGlobals()));
     619    Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(Instance::offsetOfGlobals()));
    620620    m_currentBlock->appendNew<MemoryValue>(m_proc, Store, origin(), value, globalsArray, safeCast<int32_t>(index * sizeof(Register)));
    621621    return { };
     
    10821082
    10831083        // FIXME imports can be linked here, instead of generating a patchpoint, because all import stubs are generated before B3 compilation starts. https://bugs.webkit.org/show_bug.cgi?id=166462
    1084         Value* targetInstance = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfTargetInstance(functionIndex)));
     1084        Value* targetInstance = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(Instance::offsetOfTargetInstance(functionIndex)));
     1085        // The target instance is 0 unless the call is wasm->wasm.
    10851086        Value* isWasmCall = m_currentBlock->appendNew<Value>(m_proc, NotEqual, origin(), targetInstance, m_currentBlock->appendNew<Const64Value>(m_proc, origin(), 0));
    10861087
     
    11141115        // https://bugs.webkit.org/show_bug.cgi?id=170375
    11151116        Value* jumpDestination = isEmbedderBlock->appendNew<MemoryValue>(m_proc,
    1116             Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfWasmToEmbedderStubExecutableAddress(functionIndex)));
     1117            Load, pointerType(), origin(), instanceValue(), safeCast<int32_t>(Instance::offsetOfWasmToEmbedderStubExecutableAddress(functionIndex)));
    11171118        Value* embedderCallResult = wasmCallingConvention().setupCall(m_proc, isEmbedderBlock, origin(), args, toB3Type(returnType),
    11181119            [&] (PatchpointValue* patchpoint) {
     
    11791180    {
    11801181        ExpressionType table = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(),
    1181             instanceValue(), safeCast<int32_t>(JSWebAssemblyInstance::offsetOfWasmTable()));
     1182            instanceValue(), safeCast<int32_t>(Instance::offsetOfTable()));
    11821183        callableFunctionBuffer = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(),
    11831184            table, safeCast<int32_t>(Table::offsetOfFunctions()));
     
    12621263            GPRReg baseMemory = pinnedRegs.baseMemoryPointer;
    12631264            ASSERT(newContextInstance != baseMemory);
    1264             jit.loadPtr(CCallHelpers::Address(oldContextInstance, JSWebAssemblyInstance::offsetOfCachedStackLimit()), baseMemory);
    1265             jit.storePtr(baseMemory, CCallHelpers::Address(newContextInstance, JSWebAssemblyInstance::offsetOfCachedStackLimit()));
     1265            jit.loadPtr(CCallHelpers::Address(oldContextInstance, Instance::offsetOfCachedStackLimit()), baseMemory);
     1266            jit.storePtr(baseMemory, CCallHelpers::Address(newContextInstance, Instance::offsetOfCachedStackLimit()));
    12661267            jit.storeWasmContextInstance(newContextInstance);
    1267             jit.loadPtr(CCallHelpers::Address(newContextInstance, JSWebAssemblyInstance::offsetOfWasmMemory()), baseMemory); // Memory*.
     1268            jit.loadPtr(CCallHelpers::Address(newContextInstance, Instance::offsetOfMemory()), baseMemory); // Memory*.
    12681269            ASSERT(sizeRegs.size() == 1);
    12691270            ASSERT(sizeRegs[0].sizeRegister != baseMemory);
  • trunk/Source/JavaScriptCore/wasm/WasmBinding.cpp

    r223738 r224020  
    3131#include "CCallHelpers.h"
    3232#include "JSCInlines.h"
    33 #include "JSWebAssemblyInstance.h"
    3433#include "LinkBuffer.h"
     34#include "WasmInstance.h"
    3535
    3636namespace JSC { namespace Wasm {
     
    5454    // B3's call codegen ensures that the JSCell is a WebAssemblyFunction.
    5555    jit.loadWasmContextInstance(sizeRegAsScratch); // Old Instance*
    56     // Get the callee's WebAssembly.Instance and set it as WasmContext's instance. The caller will take care of restoring its own Instance.
    57     jit.loadPtr(JIT::Address(sizeRegAsScratch, JSWebAssemblyInstance::offsetOfTargetInstance(importIndex)), baseMemory); // JSWebAssemblyInstance*.
     56    // Get the callee's Wasm::Instance and set it as WasmContext's instance. The caller will take care of restoring its own Instance.
     57    jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfTargetInstance(importIndex)), baseMemory); // Instance*.
    5858    // While we're accessing that cacheline, also get the wasm entrypoint so we can tail call to it below.
    59     jit.loadPtr(JIT::Address(sizeRegAsScratch, JSWebAssemblyInstance::offsetOfWasmEntrypoint(importIndex)), scratch); // Wasm::WasmEntrypointLoadLocation.
     59    jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfWasmEntrypoint(importIndex)), scratch); // Wasm::WasmEntrypointLoadLocation.
    6060    jit.storeWasmContextInstance(baseMemory);
    6161
    62     jit.loadPtr(JIT::Address(sizeRegAsScratch, JSWebAssemblyInstance::offsetOfCachedStackLimit()), sizeRegAsScratch);
    63     jit.storePtr(sizeRegAsScratch, JIT::Address(baseMemory, JSWebAssemblyInstance::offsetOfCachedStackLimit()));
     62    jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfCachedStackLimit()), sizeRegAsScratch);
     63    jit.storePtr(sizeRegAsScratch, JIT::Address(baseMemory, Instance::offsetOfCachedStackLimit()));
    6464
    65     // FIXME the following code assumes that all WebAssembly.Instance have the same pinned registers. https://bugs.webkit.org/show_bug.cgi?id=162952
     65    // FIXME the following code assumes that all Wasm::Instance have the same pinned registers. https://bugs.webkit.org/show_bug.cgi?id=162952
    6666    // Set up the callee's baseMemory register as well as the memory size registers.
    67     jit.loadPtr(JIT::Address(baseMemory, JSWebAssemblyInstance::offsetOfWasmMemory()), baseMemory); // Wasm::Memory*.
     67    jit.loadPtr(JIT::Address(baseMemory, Instance::offsetOfMemory()), baseMemory); // Wasm::Memory*.
    6868    ASSERT(!sizeRegs[0].sizeOffset); // The following code assumes we start at 0, and calculates subsequent size registers relative to 0.
    6969    jit.loadPtr(JIT::Address(baseMemory, Wasm::Memory::offsetOfSize()), sizeRegs[0].sizeRegister); // Memory size.
  • trunk/Source/JavaScriptCore/wasm/WasmContext.cpp

    r223738 r224020  
    2929#if ENABLE(WEBASSEMBLY)
    3030
    31 #include "JSWebAssemblyInstance.h"
    3231#include "Options.h"
     32#include "WasmInstance.h"
    3333
    3434#include <mutex>
     
    4646}
    4747
    48 JSWebAssemblyInstance* Context::load() const
     48Instance* Context::load() const
    4949{
    5050#if ENABLE(FAST_TLS_JIT)
    5151    if (useFastTLS())
    52         return bitwise_cast<JSWebAssemblyInstance*>(_pthread_getspecific_direct(WTF_WASM_CONTEXT_KEY));
     52        return bitwise_cast<Instance*>(_pthread_getspecific_direct(WTF_WASM_CONTEXT_KEY));
    5353#endif
    5454    return instance;
    5555}
    5656
    57 void Context::store(JSWebAssemblyInstance* inst, void* softStackLimit)
     57void Context::store(Instance* inst, void* softStackLimit)
    5858{
    5959#if ENABLE(FAST_TLS_JIT)
  • trunk/Source/JavaScriptCore/wasm/WasmContext.h

    r223738 r224020  
    2828#if ENABLE(WEBASSEMBLY)
    2929
    30 namespace JSC {
     30namespace JSC { namespace Wasm {
    3131
    32 class JSWebAssemblyInstance;
    33 
    34 namespace Wasm {
     32class Instance;
    3533
    3634struct Context {
    37     JSWebAssemblyInstance* instance { nullptr };
     35    Instance* instance { nullptr };
    3836
    39     JSWebAssemblyInstance* load() const;
    40     void store(JSWebAssemblyInstance*, void* softStackLimit);
     37    Instance* load() const;
     38    void store(Instance*, void* softStackLimit);
    4139
    4240    static bool useFastTLS();
  • trunk/Source/JavaScriptCore/wasm/WasmEmbedder.h

    r223738 r224020  
    3838
    3939class ExecState;
    40 class JSWebAssemblyInstance; // FIXME this should be Wasm::Instance, so should use below. https://webkit.org/b/177472
    4140
    4241namespace Wasm {
    4342
    4443struct CompilationContext;
     44class Instance;
    4545struct InternalFunction;
    4646struct ModuleInformation;
     
    5252
    5353// Called as soon as an exception is detected. The return value is the PC to continue at.
    54 using ThrowWasmException = void* (*)(ExecState*, Wasm::ExceptionType, JSWebAssemblyInstance*);
     54using ThrowWasmException = void* (*)(ExecState*, Wasm::ExceptionType, Instance*);
    5555
    5656} } // namespace JSC::Wasm
  • trunk/Source/JavaScriptCore/wasm/WasmInstance.cpp

    r223875 r224020  
    4141}
    4242
    43 Instance::Instance(Ref<Module>&& module, EntryFrame** topEntryFramePointer)
    44     : m_module(WTFMove(module))
     43Instance::Instance(Context* context, Ref<Module>&& module, EntryFrame** topEntryFramePointer)
     44    : m_context(context)
     45    , m_module(WTFMove(module))
    4546    , m_globals(MallocPtr<uint64_t>::malloc(globalMemoryByteSize(m_module.get())))
    4647    , m_topEntryFramePointer(topEntryFramePointer)
     48    , m_numImportFunctions(m_module->moduleInformation().importFunctionCount())
    4749{
     50    for (unsigned i = 0; i < m_numImportFunctions; ++i)
     51        new (importFunctionInfo(i)) ImportFunctionInfo();
     52}
     53
     54Ref<Instance> Instance::create(Context* context, Ref<Module>&& module, EntryFrame** topEntryFramePointer)
     55{
     56    return adoptRef(*new (NotNull, fastMalloc(allocationSize(module->moduleInformation().importFunctionCount()))) Instance(context, WTFMove(module), topEntryFramePointer));
    4857}
    4958
     
    5261size_t Instance::extraMemoryAllocated() const
    5362{
    54     return globalMemoryByteSize(m_module.get());
     63    return globalMemoryByteSize(m_module.get()) + allocationSize(m_numImportFunctions);
    5564}
    5665
  • trunk/Source/JavaScriptCore/wasm/WasmInstance.h

    r223875 r224020  
    3939namespace JSC { namespace Wasm {
    4040
     41struct Context;
     42
    4143class Instance : public ThreadSafeRefCounted<Instance> {
    4244public:
    43     static Ref<Instance> create(Ref<Module>&& module, EntryFrame** topEntryFramePointer)
     45    static Ref<Instance> create(Context* context, Ref<Module>&& module, EntryFrame** topEntryFramePointer);
     46
     47    void finalizeCreation(void* owner, Ref<CodeBlock>&& codeBlock)
    4448    {
    45         return adoptRef(*new Instance(WTFMove(module), topEntryFramePointer));
    46     }
    47 
    48     void finalizeCreation(Ref<CodeBlock>&& codeBlock)
    49     {
     49        m_owner = owner;
    5050        m_codeBlock = WTFMove(codeBlock);
    5151    }
     
    5353    JS_EXPORT_PRIVATE ~Instance();
    5454
     55    template<typename T> T* owner() const { return reinterpret_cast<T*>(m_owner); }
     56    static ptrdiff_t offsetOfOwner() { return OBJECT_OFFSETOF(Instance, m_owner); }
     57
    5558    size_t extraMemoryAllocated() const;
     59
     60    Wasm::Context* context() const { return m_context; }
    5661
    5762    Module& module() { return m_module.get(); }
     
    5964    Memory* memory() { return m_memory.get(); }
    6065    Table* table() { return m_table.get(); }
     66   
     67    void setMemory(Ref<Memory>&& memory) { m_memory = WTFMove(memory); }
     68    void setTable(Ref<Table>&& table) { m_table = WTFMove(table); }
    6169
    6270    int32_t loadI32Global(unsigned i) const { return m_globals.get()[i]; }
     
    6674    void setGlobal(unsigned i, int64_t bits) { m_globals.get()[i] = bits; }
    6775
     76    static ptrdiff_t offsetOfMemory() { return OBJECT_OFFSETOF(Instance, m_memory); }
     77    static ptrdiff_t offsetOfGlobals() { return OBJECT_OFFSETOF(Instance, m_globals); }
     78    static ptrdiff_t offsetOfTable() { return OBJECT_OFFSETOF(Instance, m_table); }
    6879    static ptrdiff_t offsetOfTopEntryFramePointer() { return OBJECT_OFFSETOF(Instance, m_topEntryFramePointer); }
    6980
     
    7283    void setCachedStackLimit(void* limit) { m_cachedStackLimit = limit; }
    7384
    74     friend class JSC::JSWebAssemblyInstance; // FIXME remove this once refactored https://webkit.org/b/177472.
     85    // Tail accessors.
     86    static size_t offsetOfTail() { return WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(Instance)); }
     87    struct ImportFunctionInfo {
     88        // Target instance and entrypoint are only set for wasm->wasm calls, and are otherwise nullptr. The embedder-specific logic occurs through import function.
     89        Instance* targetInstance { nullptr };
     90        Wasm::WasmEntrypointLoadLocation wasmEntrypoint { nullptr };
     91        void* wasmToEmbedderStubExecutableAddress { nullptr };
     92        void* importFunction { nullptr }; // In a JS embedding, this is a WriteBarrier<JSObject>.
     93    };
     94    unsigned numImportFunctions() const { return m_numImportFunctions; }
     95    ImportFunctionInfo* importFunctionInfo(size_t importFunctionNum)
     96    {
     97        RELEASE_ASSERT(importFunctionNum < m_numImportFunctions);
     98        return &bitwise_cast<ImportFunctionInfo*>(bitwise_cast<char*>(this) + offsetOfTail())[importFunctionNum];
     99    }
     100    static size_t offsetOfTargetInstance(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, targetInstance); }
     101    static size_t offsetOfWasmEntrypoint(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmEntrypoint); }
     102    static size_t offsetOfWasmToEmbedderStubExecutableAddress(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmToEmbedderStubExecutableAddress); }
     103    static size_t offsetOfImportFunction(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, importFunction); }
     104    template<typename T> T* importFunction(unsigned importFunctionNum) { return reinterpret_cast<T*>(&importFunctionInfo(importFunctionNum)->importFunction); }
    75105
    76106private:
    77     Instance(Ref<Module>&&, EntryFrame**);
     107    Instance(Context* context, Ref<Module>&&, EntryFrame**);
     108   
     109    static size_t allocationSize(Checked<size_t> numImportFunctions)
     110    {
     111        return (offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions).unsafeGet();
     112    }
    78113
     114    void* m_owner { nullptr }; // In a JS embedding, this is a JSWebAssemblyInstance*.
     115    Context* m_context { nullptr };
    79116    Ref<Module> m_module;
    80117    RefPtr<CodeBlock> m_codeBlock;
     
    84121    EntryFrame** m_topEntryFramePointer { nullptr };
    85122    void* m_cachedStackLimit { bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) };
     123   
     124    unsigned m_numImportFunctions { 0 };
    86125};
    87126
  • trunk/Source/JavaScriptCore/wasm/WasmOMGPlan.cpp

    r223738 r224020  
    3232#include "B3OpaqueByproducts.h"
    3333#include "JSCInlines.h"
    34 #include "JSWebAssemblyInstance.h"
    3534#include "LinkBuffer.h"
    3635#include "WasmB3IRGenerator.h"
    3736#include "WasmCallee.h"
    3837#include "WasmContext.h"
     38#include "WasmInstance.h"
    3939#include "WasmMachineThreads.h"
    4040#include "WasmMemory.h"
     
    164164}
    165165
    166 void OMGPlan::runForIndex(JSWebAssemblyInstance* instance, uint32_t functionIndex)
     166void OMGPlan::runForIndex(Instance* instance, uint32_t functionIndex)
    167167{
    168     Wasm::CodeBlock& codeBlock = instance->wasmCodeBlock();
    169     ASSERT(instance->wasmMemory()->mode() == codeBlock.mode());
     168    Wasm::CodeBlock& codeBlock = *instance->codeBlock();
     169    ASSERT(instance->memory()->mode() == codeBlock.mode());
    170170
    171171    if (codeBlock.tierUpCount(functionIndex).shouldStartTierUp()) {
    172         Ref<Plan> plan = adoptRef(*new OMGPlan(instance->context(), Ref<Wasm::Module>(instance->wasmModule()), functionIndex, codeBlock.mode(), Plan::dontFinalize()));
     172        Ref<Plan> plan = adoptRef(*new OMGPlan(instance->context(), Ref<Wasm::Module>(instance->module()), functionIndex, codeBlock.mode(), Plan::dontFinalize()));
    173173        ensureWorklist().enqueue(plan.copyRef());
    174174        if (UNLIKELY(!Options::useConcurrentJIT()))
  • trunk/Source/JavaScriptCore/wasm/WasmOMGPlan.h

    r223738 r224020  
    4646    bool multiThreaded() const override { return false; }
    4747
    48     static void runForIndex(JSWebAssemblyInstance*, uint32_t functionIndex);
     48    static void runForIndex(Instance*, uint32_t functionIndex);
    4949
    5050private:
  • trunk/Source/JavaScriptCore/wasm/WasmTable.cpp

    r223738 r224020  
    5656    // But for now, we're not doing that.
    5757    m_functions = MallocPtr<Wasm::CallableFunction>::malloc(sizeof(Wasm::CallableFunction) * static_cast<size_t>(size()));
    58     m_instances = MallocPtr<JSWebAssemblyInstance*>::malloc(sizeof(JSWebAssemblyInstance*) * static_cast<size_t>(size()));
     58    m_instances = MallocPtr<Instance*>::malloc(sizeof(Instance*) * static_cast<size_t>(size()));
    5959    for (uint32_t i = 0; i < size(); ++i) {
    6060        new (&m_functions.get()[i]) CallableFunction();
     
    111111}
    112112
    113 void Table::setFunction(uint32_t index, CallableFunction function, JSWebAssemblyInstance* instance)
     113void Table::setFunction(uint32_t index, CallableFunction function, Instance* instance)
    114114{
    115115    RELEASE_ASSERT(index < size());
  • trunk/Source/JavaScriptCore/wasm/WasmTable.h

    r223738 r224020  
    3535#include <wtf/ThreadSafeRefCounted.h>
    3636
    37 namespace JSC {
     37namespace JSC { namespace Wasm {
    3838
    39 class JSWebAssemblyInstance; // FIXME this should be Wasm::Instance https://webkit.org/b/177472
    40 
    41 namespace Wasm {
     39class Instance;
    4240
    4341class Table : public ThreadSafeRefCounted<Table> {
     
    5149    std::optional<uint32_t> grow(uint32_t delta) WARN_UNUSED_RETURN;
    5250    void clearFunction(uint32_t);
    53     void setFunction(uint32_t, CallableFunction, JSWebAssemblyInstance*); // FIXME make this Wasm::Instance. https://webkit.org/b/177472
     51    void setFunction(uint32_t, CallableFunction, Instance*);
    5452
    5553    static ptrdiff_t offsetOfSize() { return OBJECT_OFFSETOF(Table, m_size); }
     
    6664    MallocPtr<CallableFunction> m_functions;
    6765    // call_indirect needs to do an Instance check to potentially context switch when calling a function to another instance. We can hold raw pointers to Instance here because the embedder ensures that Table keeps all the instances alive. We couldn't hold a Ref here because it would cause cycles.
    68     MallocPtr<JSWebAssemblyInstance*> m_instances; // FIXME make this a Wasm::Instance. https://webkit.org/b/177472
     66    MallocPtr<Instance*> m_instances;
    6967};
    7068
  • trunk/Source/JavaScriptCore/wasm/WasmThunks.cpp

    r223875 r224020  
    3232#include "HeapCellInlines.h"
    3333#include "JITExceptions.h"
    34 #include "JSWebAssemblyInstance.h"
    3534#include "LinkBuffer.h"
    3635#include "ScratchRegisterAllocator.h"
    3736#include "WasmContext.h"
    3837#include "WasmExceptionType.h"
     38#include "WasmInstance.h"
    3939#include "WasmOMGPlan.h"
    4040
     
    4848    // We're allowed to use temp registers here. We are not allowed to use callee saves.
    4949    jit.loadWasmContextInstance(GPRInfo::argumentGPR2);
    50     jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR2, JSWebAssemblyInstance::offsetOfTopEntryFramePointer()), GPRInfo::argumentGPR0);
     50    jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR2, Instance::offsetOfTopEntryFramePointer()), GPRInfo::argumentGPR0);
    5151    jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0), GPRInfo::argumentGPR0);
    5252    jit.copyCalleeSavesToEntryFrameCalleeSavesBuffer(GPRInfo::argumentGPR0);
     
    9292
    9393    jit.loadWasmContextInstance(GPRInfo::argumentGPR0);
    94     typedef void (*Run)(JSWebAssemblyInstance*, uint32_t);
     94    typedef void (*Run)(Instance*, uint32_t);
    9595    Run run = OMGPlan::runForIndex;
    9696    jit.move(MacroAssembler::TrustedImmPtr(reinterpret_cast<void*>(run)), GPRInfo::argumentGPR2);
  • trunk/Source/JavaScriptCore/wasm/js/JSToWasm.cpp

    r223907 r224020  
    113113        ptrdiff_t jsOffset = CallFrameSlot::thisArgument * sizeof(EncodedJSValue);
    114114
    115         // vmEntryToWasm passes Wasm::Context*'s instance as the first JS argument when we're
    116         // not using fast TLS to hold the Wasm::Context*'s instance.
     115        // vmEntryToWasm passes the JSWebAssemblyInstance corresponding to Wasm::Context*'s
     116        // instance as the first JS argument when we're not using fast TLS to hold the
     117        // Wasm::Context*'s instance.
    117118        if (!Context::useFastTLS()) {
    118119            jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmContextInstanceGPR);
     120            jit.loadPtr(CCallHelpers::Address(wasmContextInstanceGPR, JSWebAssemblyInstance::offsetOfInstance()), wasmContextInstanceGPR);
    119121            jsOffset += sizeof(EncodedJSValue);
    120122        }
     
    173175
    174176        if (!Context::useFastTLS())
    175             jit.loadPtr(CCallHelpers::Address(wasmContextInstanceGPR, JSWebAssemblyInstance::offsetOfWasmMemory()), baseMemory);
     177            jit.loadPtr(CCallHelpers::Address(wasmContextInstanceGPR, Instance::offsetOfMemory()), baseMemory);
    176178        else {
    177179            jit.loadWasmContextInstance(baseMemory);
    178             jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyInstance::offsetOfWasmMemory()), baseMemory);
     180            jit.loadPtr(CCallHelpers::Address(baseMemory, Instance::offsetOfMemory()), baseMemory);
    179181        }
    180182
  • trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp

    r223875 r224020  
    5050}
    5151
    52 JSWebAssemblyInstance::JSWebAssemblyInstance(VM& vm, Structure* structure, unsigned numImportFunctions, Ref<Wasm::Instance>&& instance)
     52JSWebAssemblyInstance::JSWebAssemblyInstance(VM& vm, Structure* structure, Ref<Wasm::Instance>&& instance)
    5353    : Base(vm, structure)
    5454    , m_instance(WTFMove(instance))
    55     , m_vm(&vm)
    56     , m_wasmModule(m_instance->module())
    57     , m_wasmTable(m_instance->m_table.get())
    58     , m_globals(m_instance->m_globals.get())
    59     , m_topEntryFramePointer(m_instance->m_topEntryFramePointer)
    60     , m_numImportFunctions(numImportFunctions)
    61 {
    62     for (unsigned i = 0; i < m_numImportFunctions; ++i)
    63         new (importFunctionInfo(i)) ImportFunctionInfo();
     55{
     56    for (unsigned i = 0; i < this->instance().numImportFunctions(); ++i)
     57        new (this->instance().importFunction<WriteBarrier<JSObject>>(i)) WriteBarrier<JSObject>();
    6458}
    6559
     
    9488    visitor.append(thisObject->m_callee);
    9589    visitor.reportExtraMemoryVisited(thisObject->m_instance->extraMemoryAllocated());
    96     for (unsigned i = 0; i < thisObject->m_numImportFunctions; ++i)
    97         visitor.append(thisObject->importFunctionInfo(i)->importFunction); // This also keeps the functions' JSWebAssemblyInstance alive.
     90    for (unsigned i = 0; i < thisObject->instance().numImportFunctions(); ++i)
     91        visitor.append(*thisObject->instance().importFunction<WriteBarrier<JSObject>>(i)); // This also keeps the functions' JSWebAssemblyInstance alive.
    9892}
    9993
    10094void JSWebAssemblyInstance::finalizeCreation(VM& vm, ExecState* exec, Ref<Wasm::CodeBlock>&& wasmCodeBlock)
    10195{
    102     m_instance->finalizeCreation(wasmCodeBlock.copyRef());
    103     m_wasmCodeBlock = wasmCodeBlock.ptr();
     96    m_instance->finalizeCreation(this, wasmCodeBlock.copyRef());
    10497
    10598    auto scope = DECLARE_THROW_SCOPE(vm);
     
    120113        m_codeBlock.set(vm, this, jsCodeBlock);
    121114    } else {
    122         jsCodeBlock = JSWebAssemblyCodeBlock::create(vm, WTFMove(wasmCodeBlock), wasmModule().moduleInformation());
     115        jsCodeBlock = JSWebAssemblyCodeBlock::create(vm, WTFMove(wasmCodeBlock), module()->module().moduleInformation());
    123116        if (UNLIKELY(!jsCodeBlock->runnable())) {
    124117            throwException(exec, scope, JSWebAssemblyLinkError::create(exec, vm, globalObject()->WebAssemblyLinkErrorStructure(), jsCodeBlock->errorMessage()));
     
    129122    }
    130123
    131     for (size_t importFunctionNum = 0; importFunctionNum < m_numImportFunctions; ++importFunctionNum) {
    132         ImportFunctionInfo* info = importFunctionInfo(importFunctionNum);
     124    for (unsigned importFunctionNum = 0; importFunctionNum < instance().numImportFunctions(); ++importFunctionNum) {
     125        auto* info = instance().importFunctionInfo(importFunctionNum);
    133126        info->wasmToEmbedderStubExecutableAddress = m_codeBlock->wasmToEmbedderStubExecutableAddress(importFunctionNum);
    134127    }
     
    172165    JSModuleNamespaceObject* moduleNamespace = moduleRecord->getModuleNamespace(exec);
    173166    // FIXME: These objects could be pretty big we should try to throw OOM here.
    174     auto* jsInstance = new (NotNull, allocateCell<JSWebAssemblyInstance>(vm.heap, allocationSize(moduleInformation.importFunctionCount()))) JSWebAssemblyInstance(vm, instanceStructure, moduleInformation.importFunctionCount(), WTFMove(instance));
     167    auto* jsInstance = new (NotNull, allocateCell<JSWebAssemblyInstance>(vm.heap)) JSWebAssemblyInstance(vm, instanceStructure, WTFMove(instance));
    175168    jsInstance->finishCreation(vm, jsModule, moduleNamespace);
    176169    RETURN_IF_EXCEPTION(throwScope, nullptr);
     
    204197                return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "import function", "must be callable")));
    205198
    206             JSWebAssemblyInstance* calleeInstance = nullptr;
     199            Wasm::Instance* calleeInstance = nullptr;
    207200            Wasm::WasmEntrypointLoadLocation wasmEntrypoint = nullptr;
    208201            JSObject* function = jsCast<JSObject*>(value);
     
    216209                if (wasmFunction) {
    217210                    importedSignatureIndex = wasmFunction->signatureIndex();
    218                     calleeInstance = wasmFunction->instance();
     211                    calleeInstance = &wasmFunction->instance()->instance();
    219212                    wasmEntrypoint = wasmFunction->wasmEntrypointLoadLocation();
    220213                }
     
    235228
    236229            ASSERT(numImportFunctions == import.kindIndex);
    237             ImportFunctionInfo* info = jsInstance->importFunctionInfo(numImportFunctions++);
     230            auto* info = jsInstance->instance().importFunctionInfo(numImportFunctions);
    238231            info->targetInstance = calleeInstance;
    239232            info->wasmEntrypoint = wasmEntrypoint;
    240             info->importFunction.set(vm, jsInstance, function);
     233            jsInstance->instance().importFunction<WriteBarrier<JSObject>>(numImportFunctions)->set(vm, jsInstance, function);
     234            ++numImportFunctions;
    241235            // v. Append closure to imports.
    242236            break;
     
    266260            // ii. Append v to tables.
    267261            // iii. Append v.[[Table]] to imports.
    268             jsInstance->m_table.set(vm, jsInstance, table);
    269             jsInstance->m_wasmTable = table->table();
     262            jsInstance->setTable(vm, table);
     263            RETURN_IF_EXCEPTION(throwScope, nullptr);
    270264            break;
    271265        }
     
    297291            // ii. Append v to memories.
    298292            // iii. Append v.[[Memory]] to imports.
    299             ASSERT(!jsInstance->m_memory);
    300293            jsInstance->setMemory(vm, memory);
    301294            RETURN_IF_EXCEPTION(throwScope, nullptr);
     
    375368            throwScope.assertNoException();
    376369            ASSERT(table);
    377             jsInstance->m_table.set(vm, jsInstance, table);
    378             jsInstance->m_wasmTable = table->table();
     370            jsInstance->setTable(vm, table);
     371            RETURN_IF_EXCEPTION(throwScope, nullptr);
    379372        }
    380373    }
  • trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h

    r223875 r224020  
    3333#include "JSWebAssemblyMemory.h"
    3434#include "JSWebAssemblyTable.h"
    35 #include "WasmContext.h"
    3635#include "WasmInstance.h"
    3736
     
    4443namespace Wasm {
    4544class CodeBlock;
    46 class Table; // FIXME remove this after refactoring. https://webkit.org/b/177472
    4745}
    4846
     
    5957   
    6058    Wasm::Instance& instance() { return m_instance.get(); }
    61     Wasm::Context* context() const { return &m_vm->wasmContext; }
    6259    JSModuleNamespaceObject* moduleNamespaceObject() { return m_moduleNamespaceObject.get(); }
    63     JSWebAssemblyTable* table() { return m_table.get(); }
    6460    WebAssemblyToJSCallee* webAssemblyToJSCallee() { return m_callee.get(); }
    6561
    6662    JSWebAssemblyMemory* memory() { return m_memory.get(); }
    67     void setMemory(VM& vm, JSWebAssemblyMemory* value) { ASSERT(!memory()); m_memory.set(vm, this, value); m_wasmMemory = &memory()->memory(); }
     63    void setMemory(VM& vm, JSWebAssemblyMemory* value) {
     64        ASSERT(!memory());
     65        m_memory.set(vm, this, value);
     66        instance().setMemory(makeRef(memory()->memory()));
     67    }
    6868    Wasm::MemoryMode memoryMode() { return memory()->memory().mode(); }
    6969
    70     // Tail accessors.
    71     static size_t offsetOfTail() { return WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(JSWebAssemblyInstance)); }
    72     struct ImportFunctionInfo {
    73         // Target instance and entrypoint are only set for wasm->wasm calls, and are otherwise nullptr. The embedder-specific logic occurs through import function.
    74         JSWebAssemblyInstance* targetInstance { nullptr };
    75         Wasm::WasmEntrypointLoadLocation wasmEntrypoint { nullptr };
    76         void* wasmToEmbedderStubExecutableAddress { nullptr };
    77         WriteBarrier<JSObject> importFunction { WriteBarrier<JSObject>() };
    78     };
    79     ImportFunctionInfo* importFunctionInfo(size_t importFunctionNum)
    80     {
    81         RELEASE_ASSERT(importFunctionNum < m_numImportFunctions);
    82         return &bitwise_cast<ImportFunctionInfo*>(bitwise_cast<char*>(this) + offsetOfTail())[importFunctionNum];
     70    JSWebAssemblyTable* table() { return m_table.get(); }
     71    void setTable(VM& vm, JSWebAssemblyTable* value) {
     72        ASSERT(!table());
     73        m_table.set(vm, this, value);
     74        instance().setTable(makeRef(*table()->table()));
    8375    }
    84     static size_t offsetOfTargetInstance(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, targetInstance); }
    85     static size_t offsetOfWasmEntrypoint(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmEntrypoint); }
    86     static size_t offsetOfWasmToEmbedderStubExecutableAddress(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmToEmbedderStubExecutableAddress); }
    87     static size_t offsetOfImportFunction(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, importFunction); }
    88     JSObject* importFunction(unsigned importFunctionNum) { return importFunctionInfo(importFunctionNum)->importFunction.get(); }
    8976
    90     // FIXME remove these after refactoring them out. https://webkit.org/b/177472
    91     Wasm::Memory& internalMemory() { return memory()->memory(); }
    92     Wasm::CodeBlock& wasmCodeBlock() const { return *m_instance->codeBlock(); }
    93     static ptrdiff_t offsetOfWasmTable() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_wasmTable); }
    94     static ptrdiff_t offsetOfCallee() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_callee); }
    95     static ptrdiff_t offsetOfGlobals() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_globals); }
    96     static ptrdiff_t offsetOfCodeBlock() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_codeBlock); }
    97     static ptrdiff_t offsetOfWasmCodeBlock() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_wasmCodeBlock); }
    98     static ptrdiff_t offsetOfCachedStackLimit() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_cachedStackLimit); }
    99     static ptrdiff_t offsetOfWasmMemory() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_wasmMemory); }
    100     static ptrdiff_t offsetOfTopEntryFramePointer() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_topEntryFramePointer); }
    101     void* cachedStackLimit() const { RELEASE_ASSERT(m_instance->cachedStackLimit() == m_cachedStackLimit); return m_cachedStackLimit; }
    102     void setCachedStackLimit(void* limit) { m_instance->setCachedStackLimit(limit); m_cachedStackLimit = limit; }
    103     Wasm::Memory* wasmMemory() { return m_wasmMemory; }
    104     Wasm::Module& wasmModule() { return m_wasmModule; }
     77    static size_t offsetOfInstance() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_instance); }
     78    static size_t offsetOfCallee() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_callee); }
    10579
    10680protected:
    107     JSWebAssemblyInstance(VM&, Structure*, unsigned numImportFunctions, Ref<Wasm::Instance>&&);
     81    JSWebAssemblyInstance(VM&, Structure*, Ref<Wasm::Instance>&&);
    10882    void finishCreation(VM&, JSWebAssemblyModule*, JSModuleNamespaceObject*);
    10983    static void destroy(JSCell*);
    11084    static void visitChildren(JSCell*, SlotVisitor&);
    111 
    112     static size_t allocationSize(Checked<size_t> numImportFunctions)
    113     {
    114         return (offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions).unsafeGet();
    115     }
    11685
    11786private:
     
    12089    Ref<Wasm::Instance> m_instance;
    12190
    122     VM* m_vm;
    12391    WriteBarrier<JSWebAssemblyModule> m_module;
    12492    WriteBarrier<JSWebAssemblyCodeBlock> m_codeBlock;
     
    12795    WriteBarrier<JSWebAssemblyTable> m_table;
    12896    WriteBarrier<WebAssemblyToJSCallee> m_callee;
    129    
    130     // FIXME remove these after refactoring them out. https://webkit.org/b/177472
    131     void* m_cachedStackLimit { bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) };
    132     Wasm::CodeBlock* m_wasmCodeBlock { nullptr };
    133     Wasm::Module& m_wasmModule;
    134     Wasm::Memory* m_wasmMemory { nullptr };
    135     Wasm::Table* m_wasmTable { nullptr };
    136     uint64_t* m_globals { nullptr };
    137     EntryFrame** m_topEntryFramePointer { nullptr };
    138 
    139     unsigned m_numImportFunctions;
    14097};
    14198
  • trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyTable.cpp

    r223738 r224020  
    3030
    3131#include "JSCInlines.h"
     32#include "JSWebAssemblyInstance.h"
    3233
    3334namespace JSC {
     
    122123void JSWebAssemblyTable::setFunction(VM& vm, uint32_t index, WebAssemblyFunction* function)
    123124{
    124     m_table->setFunction(index, function->callableFunction(), function->instance());
     125    m_table->setFunction(index, function->callableFunction(), &function->instance()->instance());
    125126    m_jsFunctions.get()[index].set(vm, this, function);
    126127}
     
    128129void JSWebAssemblyTable::setFunction(VM& vm, uint32_t index, WebAssemblyWrapperFunction* function)
    129130{
    130     m_table->setFunction(index, function->callableFunction(), function->instance());
     131    m_table->setFunction(index, function->callableFunction(), &function->instance()->instance());
    131132    m_jsFunctions.get()[index].set(vm, this, function);
    132133}
  • trunk/Source/JavaScriptCore/wasm/js/WasmToJS.cpp

    r223738 r224020  
    4141#include "WasmContext.h"
    4242#include "WasmExceptionType.h"
     43#include "WasmInstance.h"
    4344
    4445namespace JSC { namespace Wasm {
     
    5051    // We're calling out of the current WebAssembly.Instance. That Instance has a list of all its import functions.
    5152    jit.loadWasmContextInstance(result);
    52     jit.loadPtr(JIT::Address(result, JSWebAssemblyInstance::offsetOfImportFunction(importIndex)), result);
     53    jit.loadPtr(JIT::Address(result, Instance::offsetOfImportFunction(importIndex)), result);
    5354}
    5455   
     
    8384
    8485        // Store Callee.
     86        jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR1, Instance::offsetOfOwner()), GPRInfo::argumentGPR1);
    8587        jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR1, JSWebAssemblyInstance::offsetOfCallee()), GPRInfo::argumentGPR2);
    8688        jit.storePtr(GPRInfo::argumentGPR2, JIT::Address(GPRInfo::callFrameRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register))));
     
    278280       
    279281        jit.loadWasmContextInstance(GPRInfo::argumentGPR0);
     282        jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0, Instance::offsetOfOwner()), GPRInfo::argumentGPR0);
    280283        jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0, JSWebAssemblyInstance::offsetOfCallee()), GPRInfo::argumentGPR0);
    281284        jit.storePtr(GPRInfo::argumentGPR0, JIT::Address(GPRInfo::callFrameRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register))));
     
    287290        auto noException = jit.emitExceptionCheck(*vm, AssemblyHelpers::InvertedExceptionCheck);
    288291
    289         // exception here.
     292        // Exception here.
    290293        jit.copyCalleeSavesToEntryFrameCalleeSavesBuffer(vm->topEntryFrame);
    291294        jit.move(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0);
     
    460463
    461464    jit.loadWasmContextInstance(GPRInfo::argumentGPR0);
     465    jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0, Instance::offsetOfOwner()), GPRInfo::argumentGPR0);
    462466    jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0, JSWebAssemblyInstance::offsetOfCallee()), GPRInfo::argumentGPR0);
    463467    jit.storePtr(GPRInfo::argumentGPR0, JIT::Address(GPRInfo::callFrameRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register))));
     
    629633}
    630634
    631 void* wasmToJSException(ExecState* exec, Wasm::ExceptionType type, JSWebAssemblyInstance* instance)
     635void* wasmToJSException(ExecState* exec, Wasm::ExceptionType type, Instance* wasmInstance)
    632636{
     637    JSWebAssemblyInstance* instance = wasmInstance->owner<JSWebAssemblyInstance>();
    633638    VM* vm = instance->vm();
    634639    NativeCallFrameTracer tracer(vm, exec);
  • trunk/Source/JavaScriptCore/wasm/js/WasmToJS.h

    r223738 r224020  
    4141namespace Wasm {
    4242
     43class Instance;
     44
    4345Expected<MacroAssemblerCodeRef, BindingFailure> wasmToJS(VM*, Bag<CallLinkInfo>& callLinkInfos, SignatureIndex, unsigned importIndex);
    4446
    45 void* wasmToJSException(ExecState*, Wasm::ExceptionType, JSWebAssemblyInstance*);
     47void* wasmToJSException(ExecState*, Wasm::ExceptionType, Instance*);
    4648
    4749} } // namespace JSC::Wasm
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp

    r223738 r224020  
    8484    Vector<JSValue> boxedArgs;
    8585    JSWebAssemblyInstance* instance = wasmFunction->instance();
     86    Wasm::Instance* wasmInstance = &instance->instance();
    8687    // When we don't use fast TLS to store the context, the JS
    87     // entry wrapper expects the instance as the first argument.
     88    // entry wrapper expects a JSWebAssemblyInstance as the first argument.
    8889    if (!Wasm::Context::useFastTLS())
    8990        boxedArgs.append(instance);
     
    128129
    129130    // FIXME Do away with this entire function, and only use the entrypoint generated by B3. https://bugs.webkit.org/show_bug.cgi?id=166486
    130     JSWebAssemblyInstance* prevInstance = vm.wasmContext.load();
     131    Wasm::Instance* prevWasmInstance = vm.wasmContext.load();
    131132    {
    132133        // We do the stack check here for the wrapper function because we don't
     
    138139            return JSValue::encode(throwException(exec, scope, createStackOverflowError(exec)));
    139140    }
    140     vm.wasmContext.store(instance, vm.softStackLimit());
     141    vm.wasmContext.store(wasmInstance, vm.softStackLimit());
    141142    ASSERT(wasmFunction->instance());
    142     ASSERT(wasmFunction->instance() == vm.wasmContext.load());
     143    ASSERT(&wasmFunction->instance()->instance() == vm.wasmContext.load());
    143144    EncodedJSValue rawResult = vmEntryToWasm(wasmFunction->jsEntrypoint(), &vm, &protoCallFrame);
    144145    // We need to make sure this is in a register or on the stack since it's stored in Vector<JSValue>.
     
    146147    // alive. But it's good hygiene.
    147148    instance->use();
    148     if (prevInstance != instance) {
     149    if (prevWasmInstance != wasmInstance) {
    149150        // This is just for some extra safety instead of leaving a cached
    150151        // value in there. If we ever forget to set the value to be a real
    151152        // bounds, this will force every stack overflow check to immediately
    152153        // fire.
    153         instance->setCachedStackLimit(bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()));
    154     }
    155     vm.wasmContext.store(prevInstance, vm.softStackLimit());
     154        wasmInstance->setCachedStackLimit(bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()));
     155    }
     156    vm.wasmContext.store(prevWasmInstance, vm.softStackLimit());
    156157    RETURN_IF_EXCEPTION(scope, { });
    157158
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp

    r223875 r224020  
    7878    RETURN_IF_EXCEPTION(scope, { });
    7979
    80     JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, instanceStructure, Wasm::Instance::create(Ref<Wasm::Module>(module->module()), &vm.topEntryFrame));
     80    JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, instanceStructure, Wasm::Instance::create(&vm.wasmContext, Ref<Wasm::Module>(module->module()), &vm.topEntryFrame));
    8181    RETURN_IF_EXCEPTION(scope, { });
    8282
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp

    r223907 r224020  
    111111            if (exp.kindIndex < functionImportCount) {
    112112                unsigned functionIndex = exp.kindIndex;
    113                 JSObject* functionImport = instance->importFunction(functionIndex);
     113                JSObject* functionImport = instance->instance().importFunction<WriteBarrier<JSObject>>(functionIndex)->get();
    114114                if (isWebAssemblyHostFunction(vm, functionImport))
    115115                    exportedValue = functionImport;
     
    192192        ASSERT(signature.returnType() == Wasm::Void);
    193193        if (startFunctionIndexSpace < codeBlock->functionImportCount()) {
    194             JSObject* startFunction = instance->importFunction(startFunctionIndexSpace);
     194            JSObject* startFunction = instance->instance().importFunction<WriteBarrier<JSObject>>(startFunctionIndexSpace)->get();
    195195            m_startFunction.set(vm, this, startFunction);
    196196        } else {
     
    296296            Wasm::SignatureIndex signatureIndex = module.signatureIndexFromFunctionIndexSpace(functionIndex);
    297297            if (functionIndex < codeBlock->functionImportCount()) {
    298                 JSObject* functionImport = jsCast<JSObject*>(m_instance->importFunction(functionIndex));
     298                JSObject* functionImport = m_instance->instance().importFunction<WriteBarrier<JSObject>>(functionIndex)->get();
    299299                if (isWebAssemblyHostFunction(vm, functionImport)) {
    300300                    WebAssemblyFunction* wasmFunction = jsDynamicCast<WebAssemblyFunction*>(vm, functionImport);
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp

    r223875 r224020  
    138138    auto scope = DECLARE_CATCH_SCOPE(vm);
    139139    // In order to avoid potentially recompiling a module. We first gather all the import/memory information prior to compiling code.
    140     JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure(), Wasm::Instance::create(Ref<Wasm::Module>(module->module()), &vm.topEntryFrame));
     140    JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure(), Wasm::Instance::create(&vm.wasmContext, Ref<Wasm::Module>(module->module()), &vm.topEntryFrame));
    141141    RETURN_IF_EXCEPTION(scope, reject(exec, scope, promise));
    142142
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyWrapperFunction.cpp

    r223738 r224020  
    6464    String name = "";
    6565    NativeExecutable* executable = vm.getHostFunction(callWebAssemblyWrapperFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name);
    66     WebAssemblyWrapperFunction* result = new (NotNull, allocateCell<WebAssemblyWrapperFunction>(vm.heap)) WebAssemblyWrapperFunction(vm, globalObject, globalObject->webAssemblyWrapperFunctionStructure(), Wasm::CallableFunction { signatureIndex, &instance->importFunctionInfo(importIndex)->wasmToEmbedderStubExecutableAddress } );
     66    WebAssemblyWrapperFunction* result = new (NotNull, allocateCell<WebAssemblyWrapperFunction>(vm.heap)) WebAssemblyWrapperFunction(vm, globalObject, globalObject->webAssemblyWrapperFunctionStructure(), Wasm::CallableFunction { signatureIndex, &instance->instance().importFunctionInfo(importIndex)->wasmToEmbedderStubExecutableAddress } );
    6767    const Wasm::Signature& signature = Wasm::SignatureInformation::get(signatureIndex);
    6868    result->finishCreation(vm, executable, signature.argumentCount(), name, function, instance);
Note: See TracChangeset for help on using the changeset viewer.