#pragma once
#if ENABLE(WEBASSEMBLY)
#include "CompilationResult.h"
#include "VM.h"
#include "WasmB3IRGenerator.h"
#include "WasmModuleInformation.h"
#include "WasmPlan.h"
#include "WasmTierUpCount.h"
#include <wtf/Bag.h>
#include <wtf/SharedTask.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/Vector.h>
namespace JSC {
class CallLinkInfo;
class JSGlobalObject;
class JSPromiseDeferred;
namespace Wasm {
class BBQPlan final : public Plan {
public:
using Base = Plan;
enum AsyncWork : uint8_t { FullCompile, Validation };
BBQPlan(VM*, Ref<ModuleInformation>, AsyncWork, CompletionTask&&);
JS_EXPORT_PRIVATE BBQPlan(VM*, Vector<uint8_t>&&, AsyncWork, CompletionTask&&);
JS_EXPORT_PRIVATE BBQPlan(VM*, const uint8_t*, size_t, AsyncWork, CompletionTask&&);
bool parseAndValidateModule();
JS_EXPORT_PRIVATE void prepare();
void compileFunctions(CompilationEffort);
template<typename Functor>
void initializeCallees(const Functor&);
Vector<Export>& exports() const
{
RELEASE_ASSERT(!failed() && !hasWork());
return m_moduleInformation->exports;
}
size_t internalFunctionCount() const
{
RELEASE_ASSERT(!failed() && !hasWork());
return m_moduleInformation->internalFunctionCount();
}
Ref<ModuleInformation>&& takeModuleInformation()
{
RELEASE_ASSERT(!failed() && !hasWork());
return WTFMove(m_moduleInformation);
}
Bag<CallLinkInfo>&& takeCallLinkInfos()
{
RELEASE_ASSERT(!failed() && !hasWork());
return WTFMove(m_callLinkInfos);
}
Vector<MacroAssemblerCodeRef>&& takeWasmToWasmExitStubs()
{
RELEASE_ASSERT(!failed() && !hasWork());
return WTFMove(m_wasmToWasmExitStubs);
}
Vector<Vector<UnlinkedWasmToWasmCall>> takeWasmToWasmCallsites()
{
RELEASE_ASSERT(!failed() && !hasWork());
return WTFMove(m_unlinkedWasmToWasmCalls);
}
Vector<TierUpCount> takeTierUpCounts()
{
RELEASE_ASSERT(!failed() && !hasWork());
return WTFMove(m_tierUpCounts);
}
enum class State : uint8_t {
Initial,
Validated,
Prepared,
Compiled,
Completed };
bool hasWork() const override
{
if (m_asyncWork == AsyncWork::Validation)
return m_state < State::Validated;
return m_state < State::Compiled;
}
void work(CompilationEffort) override;
bool hasBeenPrepared() const { return m_state >= State::Prepared; }
bool multiThreaded() const override { return hasBeenPrepared(); }
private:
class ThreadCountHolder;
friend class ThreadCountHolder;
using Base::m_lock;
void moveToState(State);
bool isComplete() const override { return m_state == State::Completed; }
void complete(const AbstractLocker&) override;
const char* stateString(State);
Bag<CallLinkInfo> m_callLinkInfos;
Vector<MacroAssemblerCodeRef> m_wasmToWasmExitStubs;
Vector<std::unique_ptr<InternalFunction>> m_wasmInternalFunctions;
HashSet<uint32_t, typename DefaultHash<uint32_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> m_exportedFunctionIndices;
HashMap<uint32_t, std::unique_ptr<InternalFunction>, typename DefaultHash<uint32_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> m_jsToWasmInternalFunctions;
Vector<CompilationContext> m_compilationContexts;
Vector<TierUpCount> m_tierUpCounts;
Vector<Vector<UnlinkedWasmToWasmCall>> m_unlinkedWasmToWasmCalls;
State m_state;
const AsyncWork m_asyncWork;
uint8_t m_numberOfActiveThreads { 0 };
uint32_t m_currentIndex { 0 };
};
} }
#endif // ENABLE(WEBASSEMBLY)