AssemblyHelpers should not have a VM field
[WebKit-https.git] / Source / JavaScriptCore / bytecode / DOMJITAccessCasePatchpointParams.cpp
1 /*
2  * Copyright (C) 2016 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 "DOMJITAccessCasePatchpointParams.h"
28
29 #include "LinkBuffer.h"
30 #include "PolymorphicAccess.h"
31 #include "StructureStubInfo.h"
32
33 #if ENABLE(JIT)
34
35 namespace JSC {
36
37 template<typename JumpType, typename FunctionType, typename ResultType, typename... Arguments>
38 class SlowPathCallGeneratorWithArguments : public DOMJITAccessCasePatchpointParams::SlowPathCallGenerator {
39 public:
40     SlowPathCallGeneratorWithArguments(JumpType from, CCallHelpers::Label to, FunctionType function, ResultType result, std::tuple<Arguments...> arguments)
41         : m_from(from)
42         , m_to(to)
43         , m_function(function)
44         , m_result(result)
45         , m_arguments(arguments)
46     {
47     }
48
49     template<size_t... ArgumentsIndex>
50     CCallHelpers::JumpList generateImpl(AccessGenerationState& state, const RegisterSet& usedRegistersByPatchpoint, CCallHelpers& jit, std::index_sequence<ArgumentsIndex...>)
51     {
52         CCallHelpers::JumpList exceptions;
53         // We spill (1) the used registers by IC and (2) the used registers by DOMJIT::Patchpoint.
54         AccessGenerationState::SpillState spillState = state.preserveLiveRegistersToStackForCall(usedRegistersByPatchpoint);
55
56         jit.store32(
57             CCallHelpers::TrustedImm32(state.callSiteIndexForExceptionHandlingOrOriginal().bits()),
58             CCallHelpers::tagFor(static_cast<VirtualRegister>(CallFrameSlot::argumentCount)));
59
60         jit.makeSpaceOnStackForCCall();
61
62         // FIXME: Currently, we do not check any ARM EABI things here.
63         // But it is OK because a compile error happens when you pass JSValueRegs as an argument.
64         // https://bugs.webkit.org/show_bug.cgi?id=163099
65         jit.setupArgumentsWithExecState(std::get<ArgumentsIndex>(m_arguments)...);
66
67         CCallHelpers::Call operationCall = jit.call();
68         auto function = m_function;
69         jit.addLinkTask([=] (LinkBuffer& linkBuffer) {
70             linkBuffer.link(operationCall, FunctionPtr(function));
71         });
72
73         jit.setupResults(m_result);
74         jit.reclaimSpaceOnStackForCCall();
75
76         CCallHelpers::Jump noException = jit.emitExceptionCheck(state.m_vm, CCallHelpers::InvertedExceptionCheck);
77
78         state.restoreLiveRegistersFromStackForCallWithThrownException(spillState);
79         exceptions.append(jit.jump());
80
81         noException.link(&jit);
82         RegisterSet dontRestore;
83         dontRestore.set(m_result);
84         state.restoreLiveRegistersFromStackForCall(spillState, dontRestore);
85
86         return exceptions;
87     }
88
89     CCallHelpers::JumpList generate(AccessGenerationState& state, const RegisterSet& usedRegistersByPatchpoint, CCallHelpers& jit) override
90     {
91         m_from.link(&jit);
92         CCallHelpers::JumpList exceptions = generateImpl(state, usedRegistersByPatchpoint, jit, std::make_index_sequence<std::tuple_size<std::tuple<Arguments...>>::value>());
93         jit.jump().linkTo(m_to, &jit);
94         return exceptions;
95     }
96
97 protected:
98     JumpType m_from;
99     CCallHelpers::Label m_to;
100     FunctionType m_function;
101     ResultType m_result;
102     std::tuple<Arguments...> m_arguments;
103 };
104
105 #define JSC_DEFINE_CALL_OPERATIONS(OperationType, ResultType, ...) \
106     void DOMJITAccessCasePatchpointParams::addSlowPathCallImpl(CCallHelpers::JumpList from, CCallHelpers& jit, OperationType operation, ResultType result, std::tuple<__VA_ARGS__> args) \
107     { \
108         CCallHelpers::Label to = jit.label(); \
109         m_generators.append(std::make_unique<SlowPathCallGeneratorWithArguments<CCallHelpers::JumpList, OperationType, ResultType, __VA_ARGS__>>(from, to, operation, result, args)); \
110     } \
111
112 DOMJIT_SLOW_PATH_CALLS(JSC_DEFINE_CALL_OPERATIONS)
113 #undef JSC_DEFINE_CALL_OPERATIONS
114
115 CCallHelpers::JumpList DOMJITAccessCasePatchpointParams::emitSlowPathCalls(AccessGenerationState& state, const RegisterSet& usedRegistersByPatchpoint, CCallHelpers& jit)
116 {
117     CCallHelpers::JumpList exceptions;
118     for (auto& generator : m_generators)
119         exceptions.append(generator->generate(state, usedRegistersByPatchpoint, jit));
120     return exceptions;
121 }
122
123 }
124
125 #endif