Unreviewed, rolling out r223691 and r223729.
[WebKit-https.git] / Source / JavaScriptCore / bytecode / PreciseJumpTargets.cpp
1 /*
2  * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "PreciseJumpTargets.h"
28
29 #include "InterpreterInlines.h"
30 #include "JSCInlines.h"
31 #include "PreciseJumpTargetsInlines.h"
32
33 namespace JSC {
34
35 template <size_t vectorSize, typename Block, typename Instruction>
36 static void getJumpTargetsForBytecodeOffset(Block* codeBlock, Instruction* instructionsBegin, unsigned bytecodeOffset, Vector<unsigned, vectorSize>& out)
37 {
38     OpcodeID opcodeID = Interpreter::getOpcodeID(instructionsBegin[bytecodeOffset]);
39     extractStoredJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, [&](int32_t& relativeOffset) {
40         out.append(bytecodeOffset + relativeOffset);
41     });
42     // op_loop_hint does not have jump target stored in bytecode instructions.
43     if (opcodeID == op_loop_hint)
44         out.append(bytecodeOffset);
45 }
46
47 enum class ComputePreciseJumpTargetsMode {
48     FollowCodeBlockClaim,
49     ForceCompute,
50 };
51
52 template<ComputePreciseJumpTargetsMode Mode, typename Block, typename Instruction, size_t vectorSize>
53 void computePreciseJumpTargetsInternal(Block* codeBlock, Instruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, vectorSize>& out)
54 {
55     ASSERT(out.isEmpty());
56     
57     // We will derive a superset of the jump targets that the code block thinks it has.
58     // So, if the code block claims there are none, then we are done.
59     if (Mode == ComputePreciseJumpTargetsMode::FollowCodeBlockClaim && !codeBlock->numberOfJumpTargets())
60         return;
61     
62     for (unsigned i = codeBlock->numberOfExceptionHandlers(); i--;) {
63         out.append(codeBlock->exceptionHandler(i).target);
64         out.append(codeBlock->exceptionHandler(i).start);
65         out.append(codeBlock->exceptionHandler(i).end);
66     }
67
68     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount;) {
69         OpcodeID opcodeID = Interpreter::getOpcodeID(instructionsBegin[bytecodeOffset]);
70         getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
71         bytecodeOffset += opcodeLengths[opcodeID];
72     }
73     
74     std::sort(out.begin(), out.end());
75     
76     // We will have duplicates, and we must remove them.
77     unsigned toIndex = 0;
78     unsigned fromIndex = 0;
79     unsigned lastValue = UINT_MAX;
80     while (fromIndex < out.size()) {
81         unsigned value = out[fromIndex++];
82         if (value == lastValue)
83             continue;
84         out[toIndex++] = value;
85         lastValue = value;
86     }
87     out.shrinkCapacity(toIndex);
88 }
89
90 void computePreciseJumpTargets(CodeBlock* codeBlock, Vector<unsigned, 32>& out)
91 {
92     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, codeBlock->instructions().begin(), codeBlock->instructions().size(), out);
93 }
94
95 void computePreciseJumpTargets(CodeBlock* codeBlock, Instruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, 32>& out)
96 {
97     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, instructionsBegin, instructionCount, out);
98 }
99
100 void computePreciseJumpTargets(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, 32>& out)
101 {
102     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, instructionsBegin, instructionCount, out);
103 }
104
105 void recomputePreciseJumpTargets(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned instructionCount, Vector<unsigned>& out)
106 {
107     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::ForceCompute>(codeBlock, instructionsBegin, instructionCount, out);
108 }
109
110 void findJumpTargetsForBytecodeOffset(CodeBlock* codeBlock, Instruction* instructionsBegin, unsigned bytecodeOffset, Vector<unsigned, 1>& out)
111 {
112     getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
113 }
114
115 void findJumpTargetsForBytecodeOffset(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned bytecodeOffset, Vector<unsigned, 1>& out)
116 {
117     getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
118 }
119
120 } // namespace JSC
121