#include "config.h"
#include "WasmLLIntPlan.h"
#if ENABLE(WEBASSEMBLY)
#include "B3Compilation.h"
#include "BytecodeDumper.h"
#include "CalleeBits.h"
#include "JSToWasm.h"
#include "LLIntThunks.h"
#include "LinkBuffer.h"
#include "WasmCallee.h"
#include "WasmLLIntGenerator.h"
#include "WasmSignatureInlines.h"
namespace JSC { namespace Wasm {
LLIntPlan::LLIntPlan(Context* context, Vector<uint8_t>&& source, AsyncWork work, CompletionTask&& task)
: Base(context, WTFMove(source), work, WTFMove(task))
{
if (parseAndValidateModule(m_source.data(), m_source.size()))
prepare();
}
LLIntPlan::LLIntPlan(Context* context, Ref<ModuleInformation> info, const Ref<LLIntCallee>* callees, CompletionTask&& task)
: Base(context, WTFMove(info), AsyncWork::FullCompile, WTFMove(task))
, m_callees(callees)
{
ASSERT(m_callees || !m_moduleInformation->functions.size());
prepare();
m_currentIndex = m_moduleInformation->functions.size();
}
bool LLIntPlan::prepareImpl()
{
const auto& functions = m_moduleInformation->functions;
if (!tryReserveCapacity(m_wasmInternalFunctions, functions.size(), " WebAssembly functions"))
return false;
m_wasmInternalFunctions.resize(functions.size());
return true;
}
void LLIntPlan::compileFunction(uint32_t functionIndex)
{
const auto& function = m_moduleInformation->functions[functionIndex];
SignatureIndex signatureIndex = m_moduleInformation->internalFunctionSignatureIndices[functionIndex];
const Signature& signature = SignatureInformation::get(signatureIndex);
unsigned functionIndexSpace = m_wasmToWasmExitStubs.size() + functionIndex;
ASSERT_UNUSED(functionIndexSpace, m_moduleInformation->signatureIndexFromFunctionIndexSpace(functionIndexSpace) == signatureIndex);
m_unlinkedWasmToWasmCalls[functionIndex] = Vector<UnlinkedWasmToWasmCall>();
Expected<std::unique_ptr<FunctionCodeBlock>, String> parseAndCompileResult = parseAndCompileBytecode(function.data.data(), function.data.size(), signature, m_moduleInformation.get(), functionIndex);
if (UNLIKELY(!parseAndCompileResult)) {
auto locker = holdLock(m_lock);
if (!m_errorMessage) {
fail(locker, makeString(parseAndCompileResult.error(), ", in function at index ", String::number(functionIndex))); }
m_currentIndex = m_moduleInformation->functions.size();
return;
}
m_wasmInternalFunctions[functionIndex] = WTFMove(*parseAndCompileResult);
}
void LLIntPlan::didCompleteCompilation(const AbstractLocker& locker)
{
unsigned functionCount = m_wasmInternalFunctions.size();
if (!m_callees && functionCount) {
CCallHelpers jit;
m_calleesVector.resize(functionCount);
Vector<CCallHelpers::Label> entrypoints(functionCount);
Vector<CCallHelpers::Jump> jumps(functionCount);
for (unsigned i = 0; i < functionCount; ++i) {
size_t functionIndexSpace = i + m_moduleInformation->importFunctionCount();
if (UNLIKELY(Options::dumpGeneratedWasmBytecodes()))
BytecodeDumper::dumpBlock(m_wasmInternalFunctions[i].get(), m_moduleInformation, WTF::dataFile());
m_calleesVector[i] = LLIntCallee::create(WTFMove(m_wasmInternalFunctions[i]), functionIndexSpace, m_moduleInformation->nameSection->get(functionIndexSpace));
entrypoints[i] = jit.label();
#if CPU(X86_64)
CCallHelpers::Address calleeSlot(CCallHelpers::stackPointerRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register)) - sizeof(CPURegister));
#elif CPU(ARM64)
CCallHelpers::Address calleeSlot(CCallHelpers::stackPointerRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register)) - sizeof(CallerFrameAndPC));
#else
#error Unsupported architecture.
#endif
jit.storePtr(CCallHelpers::TrustedImmPtr(CalleeBits::boxWasm(m_calleesVector[i].ptr())), calleeSlot);
jumps[i] = jit.jump();
}
LinkBuffer linkBuffer(jit, GLOBAL_THUNK_ID, JITCompilationCanFail);
if (UNLIKELY(linkBuffer.didFailToAllocate())) {
Base::fail(locker, "Out of executable memory in Wasm LLInt entry thunks");
return;
}
for (unsigned i = 0; i < functionCount; ++i) {
m_calleesVector[i]->setEntrypoint(linkBuffer.locationOf<WasmEntryPtrTag>(entrypoints[i]));
linkBuffer.link<JITThunkPtrTag>(jumps[i], CodeLocationLabel<JITThunkPtrTag>(LLInt::wasmFunctionEntryThunk().code()));
}
m_entryThunks = FINALIZE_CODE(linkBuffer, B3CompilationPtrTag, "Wasm LLInt entry thunks");
m_callees = m_calleesVector.data();
}
if (m_asyncWork == AsyncWork::Validation)
return;
for (uint32_t functionIndex = 0; functionIndex < m_moduleInformation->functions.size(); functionIndex++) {
if (m_exportedFunctionIndices.contains(functionIndex) || m_moduleInformation->referencedFunctions().contains(functionIndex)) {
SignatureIndex signatureIndex = m_moduleInformation->internalFunctionSignatureIndices[functionIndex];
const Signature& signature = SignatureInformation::get(signatureIndex);
CCallHelpers jit;
MemoryMode mode = MemoryMode::BoundsChecking;
std::unique_ptr<InternalFunction> function = createJSToWasmWrapper(jit, signature, &m_unlinkedWasmToWasmCalls[functionIndex], m_moduleInformation.get(), mode, functionIndex);
LinkBuffer linkBuffer(jit, nullptr, JITCompilationCanFail);
if (UNLIKELY(linkBuffer.didFailToAllocate())) {
Base::fail(locker, makeString("Out of executable memory in function entrypoint at index ", String::number(functionIndex)));
return;
}
function->entrypoint.compilation = makeUnique<B3::Compilation>(
FINALIZE_CODE(linkBuffer, B3CompilationPtrTag, "Embedder->WebAssembly entrypoint[%i] %s", functionIndex, signature.toString().ascii().data()),
nullptr);
Ref<EmbedderEntrypointCallee> callee = EmbedderEntrypointCallee::create(WTFMove(function->entrypoint));
if (function->calleeMoveLocation)
MacroAssembler::repatchPointer(function->calleeMoveLocation, CalleeBits::boxWasm(callee.ptr()));
auto result = m_embedderCallees.add(functionIndex, WTFMove(callee));
ASSERT_UNUSED(result, result.isNewEntry);
}
}
for (auto& unlinked : m_unlinkedWasmToWasmCalls) {
for (auto& call : unlinked) {
MacroAssemblerCodePtr<WasmEntryPtrTag> executableAddress;
if (m_moduleInformation->isImportedFunctionFromFunctionIndexSpace(call.functionIndexSpace)) {
executableAddress = m_wasmToWasmExitStubs.at(call.functionIndexSpace).code();
} else
executableAddress = m_callees[call.functionIndexSpace - m_moduleInformation->importFunctionCount()]->entrypoint();
MacroAssembler::repatchNearCall(call.callLocation, CodeLocationLabel<WasmEntryPtrTag>(executableAddress));
}
}
}
void LLIntPlan::work(CompilationEffort effort)
{
switch (m_state) {
case State::Prepared:
compileFunctions(effort);
break;
case State::Compiled:
break;
default:
break;
}
}
bool LLIntPlan::didReceiveFunctionData(unsigned, const FunctionData&)
{
return true;
}
} }
#endif // ENABLE(WEBASSEMBLY)