1/*
2 * Copyright (C) 2012-2018 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 "GCAwareJITStubRoutine.h"
28
29#if ENABLE(JIT)
30
31#include "CodeBlock.h"
32#include "DFGCommonData.h"
33#include "Heap.h"
34#include "VM.h"
35#include "JITStubRoutineSet.h"
36#include "JSCInlines.h"
37#include "SlotVisitor.h"
38#include "Structure.h"
39#include <wtf/RefPtr.h>
40
41namespace JSC {
42
43GCAwareJITStubRoutine::GCAwareJITStubRoutine(
44 const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code, VM& vm)
45 : JITStubRoutine(code)
46{
47 vm.heap.m_jitStubRoutines->add(this);
48}
49
50GCAwareJITStubRoutine::~GCAwareJITStubRoutine() { }
51
52void GCAwareJITStubRoutine::observeZeroRefCount()
53{
54 if (m_isJettisoned) {
55 // This case is needed for when the system shuts down. It may be that
56 // the JIT stub routine set gets deleted before we get around to deleting
57 // this guy. In that case the GC informs us that we're jettisoned already
58 // and that we should delete ourselves as soon as the ref count reaches
59 // zero.
60 delete this;
61 return;
62 }
63
64 RELEASE_ASSERT(!m_refCount);
65
66 m_isJettisoned = true;
67}
68
69void GCAwareJITStubRoutine::deleteFromGC()
70{
71 ASSERT(m_isJettisoned);
72 ASSERT(!m_refCount);
73 ASSERT(!m_mayBeExecuting);
74
75 delete this;
76}
77
78void GCAwareJITStubRoutine::markRequiredObjectsInternal(SlotVisitor&)
79{
80}
81
82MarkingGCAwareJITStubRoutine::MarkingGCAwareJITStubRoutine(
83 const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code, VM& vm, const JSCell* owner,
84 const Vector<JSCell*>& cells)
85 : GCAwareJITStubRoutine(code, vm)
86 , m_cells(cells.size())
87{
88 for (unsigned i = cells.size(); i--;)
89 m_cells[i].set(vm, owner, cells[i]);
90}
91
92MarkingGCAwareJITStubRoutine::~MarkingGCAwareJITStubRoutine()
93{
94}
95
96void MarkingGCAwareJITStubRoutine::markRequiredObjectsInternal(SlotVisitor& visitor)
97{
98 for (auto& entry : m_cells)
99 visitor.append(entry);
100}
101
102
103GCAwareJITStubRoutineWithExceptionHandler::GCAwareJITStubRoutineWithExceptionHandler(
104 const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code, VM& vm, const JSCell* owner, const Vector<JSCell*>& cells,
105 CodeBlock* codeBlockForExceptionHandlers, CallSiteIndex exceptionHandlerCallSiteIndex)
106 : MarkingGCAwareJITStubRoutine(code, vm, owner, cells)
107 , m_codeBlockWithExceptionHandler(codeBlockForExceptionHandlers)
108 , m_exceptionHandlerCallSiteIndex(exceptionHandlerCallSiteIndex)
109{
110 RELEASE_ASSERT(m_codeBlockWithExceptionHandler);
111 ASSERT(!!m_codeBlockWithExceptionHandler->handlerForIndex(exceptionHandlerCallSiteIndex.bits()));
112}
113
114void GCAwareJITStubRoutineWithExceptionHandler::aboutToDie()
115{
116 m_codeBlockWithExceptionHandler = nullptr;
117}
118
119void GCAwareJITStubRoutineWithExceptionHandler::observeZeroRefCount()
120{
121#if ENABLE(DFG_JIT)
122 if (m_codeBlockWithExceptionHandler) {
123 m_codeBlockWithExceptionHandler->jitCode()->dfgCommon()->removeCallSiteIndex(m_exceptionHandlerCallSiteIndex);
124 m_codeBlockWithExceptionHandler->removeExceptionHandlerForCallSite(m_exceptionHandlerCallSiteIndex);
125 m_codeBlockWithExceptionHandler = nullptr;
126 }
127#endif
128
129 Base::observeZeroRefCount();
130}
131
132
133Ref<JITStubRoutine> createJITStubRoutine(
134 const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code,
135 VM& vm,
136 const JSCell* owner,
137 bool makesCalls,
138 const Vector<JSCell*>& cells,
139 CodeBlock* codeBlockForExceptionHandlers,
140 CallSiteIndex exceptionHandlerCallSiteIndex)
141{
142 if (!makesCalls)
143 return adoptRef(*new JITStubRoutine(code));
144
145 if (codeBlockForExceptionHandlers) {
146 RELEASE_ASSERT(JITCode::isOptimizingJIT(codeBlockForExceptionHandlers->jitType()));
147 return adoptRef(*new GCAwareJITStubRoutineWithExceptionHandler(code, vm, owner, cells, codeBlockForExceptionHandlers, exceptionHandlerCallSiteIndex));
148 }
149
150 if (cells.isEmpty())
151 return adoptRef(*new GCAwareJITStubRoutine(code, vm));
152
153 return adoptRef(*new MarkingGCAwareJITStubRoutine(code, vm, owner, cells));
154}
155
156} // namespace JSC
157
158#endif // ENABLE(JIT)
159
160