2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#if ENABLE(WEBASSEMBLY)
30
#include "BytecodeConventions.h"
31
#include "InstructionStream.h"
32
#include "MacroAssemblerCodeRef.h"
33
#include "WasmLLIntTierUpCounter.h"
34
#include <wtf/HashMap.h>
35
#include <wtf/Vector.h>
40
class LLIntOffsetsExtractor;
42
template<typename Traits>
43
class BytecodeGeneratorBase;
48
struct GeneratorTraits;
51
// FIXME: Consider merging this with LLIntCallee
52
// https://bugs.webkit.org/show_bug.cgi?id=203691
53
class FunctionCodeBlock {
54
WTF_MAKE_FAST_ALLOCATED;
55
WTF_MAKE_NONCOPYABLE(FunctionCodeBlock);
57
friend BytecodeGeneratorBase<GeneratorTraits>;
58
friend LLIntOffsetsExtractor;
59
friend class LLIntGenerator;
62
FunctionCodeBlock(uint32_t functionIndex)
63
: m_functionIndex(functionIndex)
67
uint32_t functionIndex() const { return m_functionIndex; }
68
int numVars() const { return m_numVars; }
69
int numCalleeLocals() const { return m_numCalleeLocals; }
70
uint32_t numArguments() const { return m_numArguments; }
71
const Vector<Type>& constantTypes() const { return m_constantTypes; }
72
const Vector<uint64_t>& constants() const { return m_constants; }
73
const InstructionStream& instructions() const { return *m_instructions; }
75
void setNumVars(int numVars) { m_numVars = numVars; }
76
void setNumCalleeLocals(int numCalleeLocals) { m_numCalleeLocals = numCalleeLocals; }
78
ALWAYS_INLINE uint64_t getConstant(VirtualRegister reg) const { return m_constants[reg.toConstantIndex()]; }
79
ALWAYS_INLINE Type getConstantType(VirtualRegister reg) const
81
ASSERT(Options::dumpGeneratedWasmBytecodes());
82
return m_constantTypes[reg.toConstantIndex()];
85
void setInstructions(std::unique_ptr<InstructionStream>);
86
void addJumpTarget(InstructionStream::Offset jumpTarget) { m_jumpTargets.append(jumpTarget); }
87
InstructionStream::Offset numberOfJumpTargets() { return m_jumpTargets.size(); }
88
InstructionStream::Offset lastJumpTarget() { return m_jumpTargets.last(); }
90
void addOutOfLineJumpTarget(InstructionStream::Offset, int target);
91
const Instruction* outOfLineJumpTarget(const Instruction*);
92
InstructionStream::Offset outOfLineJumpOffset(InstructionStream::Offset);
93
InstructionStream::Offset outOfLineJumpOffset(const InstructionStream::Ref& instruction)
95
return outOfLineJumpOffset(instruction.offset());
98
inline InstructionStream::Offset bytecodeOffset(const Instruction* returnAddress)
100
const auto* instructionsBegin = m_instructions->at(0).ptr();
101
const auto* instructionsEnd = reinterpret_cast<const Instruction*>(reinterpret_cast<uintptr_t>(instructionsBegin) + m_instructions->size());
102
RELEASE_ASSERT(returnAddress >= instructionsBegin && returnAddress < instructionsEnd);
103
return returnAddress - instructionsBegin;
106
LLIntTierUpCounter& tierUpCounter() { return m_tierUpCounter; }
108
unsigned addSignature(const Signature&);
109
const Signature& signature(unsigned index) const;
111
struct JumpTableEntry {
113
unsigned startOffset;
118
using JumpTable = Vector<JumpTableEntry>;
119
JumpTable& addJumpTable(size_t numberOfEntries);
120
const JumpTable& jumpTable(unsigned tableIndex) const;
121
unsigned numberOfJumpTables() const;
124
using OutOfLineJumpTargets = HashMap<InstructionStream::Offset, int>;
126
uint32_t m_functionIndex;
128
// Used for the number of WebAssembly locals, as in https://webassembly.github.io/spec/core/syntax/modules.html#syntax-local
130
// Number of VirtualRegister. The naming is unfortunate, but has to match UnlinkedCodeBlock
131
int m_numCalleeLocals { 0 };
132
uint32_t m_numArguments { 0 };
133
Vector<Type> m_constantTypes;
134
Vector<uint64_t> m_constants;
135
std::unique_ptr<InstructionStream> m_instructions;
136
const void* m_instructionsRawPointer { nullptr };
137
Vector<InstructionStream::Offset> m_jumpTargets;
138
Vector<const Signature*> m_signatures;
139
OutOfLineJumpTargets m_outOfLineJumpTargets;
140
LLIntTierUpCounter m_tierUpCounter;
141
Vector<JumpTable> m_jumpTables;
144
} } // namespace JSC::Wasm
146
#endif // ENABLE(WEBASSEMBLY)