9ed935b07032e13376e68c7a99669720702aede0
[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     else if (opcodeID == op_enter && codeBlock->hasTailCalls()) {
46         // We need to insert a jump after op_enter, so recursive tail calls have somewhere to jump to.
47         // But we only want to pay that price for functions that have at least one tail call.
48         out.append(bytecodeOffset + opcodeLengths[op_enter]);
49     }
50 }
51
52 enum class ComputePreciseJumpTargetsMode {
53     FollowCodeBlockClaim,
54     ForceCompute,
55 };
56
57 template<ComputePreciseJumpTargetsMode Mode, typename Block, typename Instruction, size_t vectorSize>
58 void computePreciseJumpTargetsInternal(Block* codeBlock, Instruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, vectorSize>& out)
59 {
60     ASSERT(out.isEmpty());
61
62     // The code block has a superset of the jump targets. So if it claims to have none, we are done.
63     if (Mode == ComputePreciseJumpTargetsMode::FollowCodeBlockClaim && !codeBlock->numberOfJumpTargets())
64         return;
65     
66     for (unsigned i = codeBlock->numberOfExceptionHandlers(); i--;) {
67         out.append(codeBlock->exceptionHandler(i).target);
68         out.append(codeBlock->exceptionHandler(i).start);
69         out.append(codeBlock->exceptionHandler(i).end);
70     }
71
72     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount;) {
73         OpcodeID opcodeID = Interpreter::getOpcodeID(instructionsBegin[bytecodeOffset]);
74         getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
75         bytecodeOffset += opcodeLengths[opcodeID];
76     }
77     
78     std::sort(out.begin(), out.end());
79     
80     // We will have duplicates, and we must remove them.
81     unsigned toIndex = 0;
82     unsigned fromIndex = 0;
83     unsigned lastValue = UINT_MAX;
84     while (fromIndex < out.size()) {
85         unsigned value = out[fromIndex++];
86         if (value == lastValue)
87             continue;
88         out[toIndex++] = value;
89         lastValue = value;
90     }
91     out.shrinkCapacity(toIndex);
92 }
93
94 void computePreciseJumpTargets(CodeBlock* codeBlock, Vector<unsigned, 32>& out)
95 {
96     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, codeBlock->instructions().begin(), codeBlock->instructions().size(), out);
97 }
98
99 void computePreciseJumpTargets(CodeBlock* codeBlock, Instruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, 32>& out)
100 {
101     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, instructionsBegin, instructionCount, out);
102 }
103
104 void computePreciseJumpTargets(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned instructionCount, Vector<unsigned, 32>& out)
105 {
106     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::FollowCodeBlockClaim>(codeBlock, instructionsBegin, instructionCount, out);
107 }
108
109 void recomputePreciseJumpTargets(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned instructionCount, Vector<unsigned>& out)
110 {
111     computePreciseJumpTargetsInternal<ComputePreciseJumpTargetsMode::ForceCompute>(codeBlock, instructionsBegin, instructionCount, out);
112 }
113
114 void findJumpTargetsForBytecodeOffset(CodeBlock* codeBlock, Instruction* instructionsBegin, unsigned bytecodeOffset, Vector<unsigned, 1>& out)
115 {
116     getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
117 }
118
119 void findJumpTargetsForBytecodeOffset(UnlinkedCodeBlock* codeBlock, UnlinkedInstruction* instructionsBegin, unsigned bytecodeOffset, Vector<unsigned, 1>& out)
120 {
121     getJumpTargetsForBytecodeOffset(codeBlock, instructionsBegin, bytecodeOffset, out);
122 }
123
124 } // namespace JSC
125