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.
29
#include "UnlinkedCodeBlock.h"
30
#include <wtf/Vector.h>
34
// FIXME: Create UnlinkedCodeBlock inside UnlinkedCodeBlockGenerator.
35
// https://bugs.webkit.org/show_bug.cgi?id=207212
36
class UnlinkedCodeBlockGenerator {
37
WTF_MAKE_FAST_ALLOCATED;
38
WTF_MAKE_NONCOPYABLE(UnlinkedCodeBlockGenerator)
40
UnlinkedCodeBlockGenerator(VM& vm, UnlinkedCodeBlock* codeBlock)
42
, m_codeBlock(vm, codeBlock)
46
VM& vm() { return m_vm; }
48
bool isConstructor() const { return m_codeBlock->isConstructor(); }
49
ConstructorKind constructorKind() const { return m_codeBlock->constructorKind(); }
50
SuperBinding superBinding() const { return m_codeBlock->superBinding(); }
51
JSParserScriptMode scriptMode() const { return m_codeBlock->scriptMode(); }
52
NeedsClassFieldInitializer needsClassFieldInitializer() const { return m_codeBlock->needsClassFieldInitializer(); }
53
bool isStrictMode() const { return m_codeBlock->isStrictMode(); }
54
bool usesEval() const { return m_codeBlock->usesEval(); }
55
SourceParseMode parseMode() const { return m_codeBlock->parseMode(); }
56
bool isArrowFunction() { return m_codeBlock->isArrowFunction(); }
57
DerivedContextType derivedContextType() const { return m_codeBlock->derivedContextType(); }
58
EvalContextType evalContextType() const { return m_codeBlock->evalContextType(); }
59
bool isArrowFunctionContext() const { return m_codeBlock->isArrowFunctionContext(); }
60
bool isClassContext() const { return m_codeBlock->isClassContext(); }
61
int numCalleeLocals() const { return m_codeBlock->m_numCalleeLocals; }
62
int numVars() const { return m_codeBlock->m_numVars; }
63
unsigned numParameters() const { return m_codeBlock->numParameters(); }
64
VirtualRegister thisRegister() const { return m_codeBlock->thisRegister(); }
65
VirtualRegister scopeRegister() const { return m_codeBlock->scopeRegister(); }
66
bool wasCompiledWithDebuggingOpcodes() const { return m_codeBlock->wasCompiledWithDebuggingOpcodes(); }
67
bool hasCheckpoints() const { return m_codeBlock->hasCheckpoints(); }
68
bool hasTailCalls() const { return m_codeBlock->hasTailCalls(); }
70
// Updating UnlinkedCodeBlock.
71
void setHasCheckpoints() { m_codeBlock->setHasCheckpoints(); }
72
void setHasTailCalls() { m_codeBlock->setHasTailCalls(); }
73
void setNumCalleeLocals(int numCalleeLocals) { m_codeBlock->m_numCalleeLocals = numCalleeLocals; }
74
void setNumVars(int numVars) { m_codeBlock->m_numVars = numVars; }
75
void setThisRegister(VirtualRegister thisRegister) { m_codeBlock->setThisRegister(thisRegister); }
76
void setScopeRegister(VirtualRegister thisRegister) { m_codeBlock->setScopeRegister(thisRegister); }
77
void setNumParameters(int newValue) { m_codeBlock->setNumParameters(newValue); }
79
UnlinkedMetadataTable& metadata() { return m_codeBlock->metadata(); }
80
void addExpressionInfo(unsigned instructionOffset, int divot, int startOffset, int endOffset, unsigned line, unsigned column);
81
void addTypeProfilerExpressionInfo(unsigned instructionOffset, unsigned startDivot, unsigned endDivot);
82
void addOpProfileControlFlowBytecodeOffset(InstructionStream::Offset offset)
84
m_opProfileControlFlowBytecodeOffsets.append(offset);
87
size_t numberOfJumpTargets() const { return m_jumpTargets.size(); }
88
void addJumpTarget(unsigned jumpTarget) { m_jumpTargets.append(jumpTarget); }
89
unsigned jumpTarget(int index) const { return m_jumpTargets[index]; }
90
unsigned lastJumpTarget() const { return m_jumpTargets.last(); }
92
size_t numberOfSwitchJumpTables() const { return m_switchJumpTables.size(); }
93
UnlinkedSimpleJumpTable& addSwitchJumpTable() { m_switchJumpTables.append(UnlinkedSimpleJumpTable()); return m_switchJumpTables.last(); }
94
UnlinkedSimpleJumpTable& switchJumpTable(int tableIndex) { return m_switchJumpTables[tableIndex]; }
96
size_t numberOfStringSwitchJumpTables() const { return m_stringSwitchJumpTables.size(); }
97
UnlinkedStringJumpTable& addStringSwitchJumpTable() { m_stringSwitchJumpTables.append(UnlinkedStringJumpTable()); return m_stringSwitchJumpTables.last(); }
98
UnlinkedStringJumpTable& stringSwitchJumpTable(int tableIndex) { return m_stringSwitchJumpTables[tableIndex]; }
100
size_t numberOfExceptionHandlers() const { return m_exceptionHandlers.size(); }
101
UnlinkedHandlerInfo& exceptionHandler(int index) { return m_exceptionHandlers[index]; }
102
void addExceptionHandler(const UnlinkedHandlerInfo& handler) { m_exceptionHandlers.append(handler); }
103
UnlinkedHandlerInfo* handlerForBytecodeIndex(BytecodeIndex, RequiredHandler = RequiredHandler::AnyHandler);
104
UnlinkedHandlerInfo* handlerForIndex(unsigned, RequiredHandler = RequiredHandler::AnyHandler);
106
BitVector& bitVector(size_t i) { return m_bitVectors[i]; }
107
unsigned addBitVector(BitVector&& bitVector)
109
m_bitVectors.append(WTFMove(bitVector));
110
return m_bitVectors.size() - 1;
113
unsigned numberOfConstantIdentifierSets() const { return m_constantIdentifierSets.size(); }
114
const Vector<ConstantIdentifierSetEntry>& constantIdentifierSets() { return m_constantIdentifierSets; }
115
void addSetConstant(IdentifierSet& set)
117
ASSERT(m_vm.heap.isDeferred());
118
unsigned result = m_constantRegisters.size();
119
m_constantRegisters.append(WriteBarrier<Unknown>());
120
m_constantsSourceCodeRepresentation.append(SourceCodeRepresentation::Other);
121
m_constantIdentifierSets.append(ConstantIdentifierSetEntry(set, result));
124
const WriteBarrier<Unknown>& constantRegister(VirtualRegister reg) const { return m_constantRegisters[reg.toConstantIndex()]; }
125
const Vector<WriteBarrier<Unknown>>& constantRegisters() { return m_constantRegisters; }
126
ALWAYS_INLINE JSValue getConstant(VirtualRegister reg) const { return m_constantRegisters[reg.toConstantIndex()].get(); }
127
const Vector<SourceCodeRepresentation>& constantsSourceCodeRepresentation() { return m_constantsSourceCodeRepresentation; }
129
unsigned addConstant(JSValue v, SourceCodeRepresentation sourceCodeRepresentation = SourceCodeRepresentation::Other)
131
ASSERT(m_vm.heap.isDeferred());
132
unsigned result = m_constantRegisters.size();
133
m_constantRegisters.append(WriteBarrier<Unknown>());
134
m_constantRegisters.last().setWithoutWriteBarrier(v);
135
m_constantsSourceCodeRepresentation.append(sourceCodeRepresentation);
138
unsigned addConstant(LinkTimeConstant linkTimeConstant)
140
ASSERT(m_vm.heap.isDeferred());
141
unsigned result = m_constantRegisters.size();
142
m_constantRegisters.append(WriteBarrier<Unknown>());
143
m_constantRegisters.last().setWithoutWriteBarrier(jsNumber(static_cast<int32_t>(linkTimeConstant)));
144
m_constantsSourceCodeRepresentation.append(SourceCodeRepresentation::LinkTimeConstant);
148
unsigned addFunctionDecl(UnlinkedFunctionExecutable* n)
150
ASSERT(m_vm.heap.isDeferred());
151
unsigned size = m_functionDecls.size();
152
m_functionDecls.append(WriteBarrier<UnlinkedFunctionExecutable>());
153
m_functionDecls.last().setWithoutWriteBarrier(n);
157
unsigned addFunctionExpr(UnlinkedFunctionExecutable* n)
159
unsigned size = m_functionExprs.size();
160
m_functionExprs.append(WriteBarrier<UnlinkedFunctionExecutable>());
161
m_functionExprs.last().setWithoutWriteBarrier(n);
165
size_t numberOfIdentifiers() const { return m_identifiers.size(); }
166
const Identifier& identifier(int index) const { return m_identifiers[index]; }
167
void addIdentifier(const Identifier& i) { return m_identifiers.append(i); }
169
using OutOfLineJumpTargets = HashMap<InstructionStream::Offset, int>;
170
void addOutOfLineJumpTarget(InstructionStream::Offset, int target);
171
int outOfLineJumpOffset(InstructionStream::Offset);
172
int outOfLineJumpOffset(const InstructionStream::Ref& instruction)
174
return outOfLineJumpOffset(instruction.offset());
176
OutOfLineJumpTargets replaceOutOfLineJumpTargets()
178
OutOfLineJumpTargets newJumpTargets;
179
std::swap(m_outOfLineJumpTargets, newJumpTargets);
180
return newJumpTargets;
183
size_t metadataSizeInBytes() { return m_codeBlock->metadataSizeInBytes(); }
185
void getLineAndColumn(const ExpressionRangeInfo&, unsigned& line, unsigned& column) const;
187
void applyModification(BytecodeRewriter&, InstructionStreamWriter&);
189
void finalize(std::unique_ptr<InstructionStream>);
191
void dump(PrintStream&) const;
195
Strong<UnlinkedCodeBlock> m_codeBlock;
197
Vector<InstructionStream::Offset> m_jumpTargets;
198
Vector<Identifier> m_identifiers;
199
Vector<WriteBarrier<Unknown>> m_constantRegisters;
200
Vector<SourceCodeRepresentation> m_constantsSourceCodeRepresentation;
201
Vector<WriteBarrier<UnlinkedFunctionExecutable>> m_functionDecls;
202
Vector<WriteBarrier<UnlinkedFunctionExecutable>> m_functionExprs;
203
Vector<ExpressionRangeInfo> m_expressionInfo;
204
OutOfLineJumpTargets m_outOfLineJumpTargets;
206
Vector<UnlinkedHandlerInfo> m_exceptionHandlers;
207
Vector<UnlinkedSimpleJumpTable> m_switchJumpTables;
208
Vector<UnlinkedStringJumpTable> m_stringSwitchJumpTables;
209
Vector<ExpressionRangeInfo::FatPosition> m_expressionInfoFatPositions;
210
HashMap<unsigned, UnlinkedCodeBlock::RareData::TypeProfilerExpressionRange> m_typeProfilerInfoMap;
211
Vector<InstructionStream::Offset> m_opProfileControlFlowBytecodeOffsets;
212
Vector<BitVector> m_bitVectors;
213
Vector<ConstantIdentifierSetEntry> m_constantIdentifierSets;