1 | /* |
2 | * Copyright (C) 2009-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 | #pragma once |
27 | |
28 | #if ENABLE(YARR_JIT) |
29 | |
30 | #include "MacroAssemblerCodeRef.h" |
31 | #include "MatchResult.h" |
32 | #include "Yarr.h" |
33 | #include "YarrPattern.h" |
34 | |
35 | #define YARR_CALL |
36 | |
37 | namespace JSC { |
38 | |
39 | class VM; |
40 | class ExecutablePool; |
41 | |
42 | namespace Yarr { |
43 | |
44 | enum class JITFailureReason : uint8_t { |
45 | DecodeSurrogatePair, |
46 | BackReference, |
47 | ForwardReference, |
48 | VariableCountedParenthesisWithNonZeroMinimum, |
49 | ParenthesizedSubpattern, |
50 | FixedCountParenthesizedSubpattern, |
51 | ParenthesisNestedTooDeep, |
52 | ExecutableMemoryAllocationFailure, |
53 | }; |
54 | |
55 | class YarrCodeBlock { |
56 | WTF_MAKE_FAST_ALLOCATED; |
57 | WTF_MAKE_NONCOPYABLE(YarrCodeBlock); |
58 | |
59 | // Technically freeParenContext and parenContextSize are only used if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS) is set. Fortunately, all the calling conventions we support have caller save argument registers. |
60 | using YarrJITCode8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
61 | using YarrJITCode16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
62 | using YarrJITCodeMatchOnly8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
63 | using YarrJITCodeMatchOnly16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
64 | |
65 | public: |
66 | YarrCodeBlock() = default; |
67 | |
68 | void setFallBackWithFailureReason(JITFailureReason failureReason) { m_failureReason = failureReason; } |
69 | Optional<JITFailureReason> failureReason() { return m_failureReason; } |
70 | |
71 | bool has8BitCode() { return m_ref8.size(); } |
72 | bool has16BitCode() { return m_ref16.size(); } |
73 | void set8BitCode(MacroAssemblerCodeRef<Yarr8BitPtrTag> ref) { m_ref8 = ref; } |
74 | void set16BitCode(MacroAssemblerCodeRef<Yarr16BitPtrTag> ref) { m_ref16 = ref; } |
75 | |
76 | bool has8BitCodeMatchOnly() { return m_matchOnly8.size(); } |
77 | bool has16BitCodeMatchOnly() { return m_matchOnly16.size(); } |
78 | void set8BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> matchOnly) { m_matchOnly8 = matchOnly; } |
79 | void set16BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> matchOnly) { m_matchOnly16 = matchOnly; } |
80 | |
81 | bool usesPatternContextBuffer() { return m_usesPatternContextBuffer; } |
82 | #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS) |
83 | void setUsesPatternContextBuffer() { m_usesPatternContextBuffer = true; } |
84 | #endif |
85 | |
86 | MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) |
87 | { |
88 | ASSERT(has8BitCode()); |
89 | return MatchResult(untagCFunctionPtr<YarrJITCode8, Yarr8BitPtrTag>(m_ref8.code().executableAddress())(input, start, length, output, freeParenContext, parenContextSize)); |
90 | } |
91 | |
92 | MatchResult execute(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) |
93 | { |
94 | ASSERT(has16BitCode()); |
95 | return MatchResult(untagCFunctionPtr<YarrJITCode16, Yarr16BitPtrTag>(m_ref16.code().executableAddress())(input, start, length, output, freeParenContext, parenContextSize)); |
96 | } |
97 | |
98 | MatchResult execute(const LChar* input, unsigned start, unsigned length, void* freeParenContext, unsigned parenContextSize) |
99 | { |
100 | ASSERT(has8BitCodeMatchOnly()); |
101 | return MatchResult(untagCFunctionPtr<YarrJITCodeMatchOnly8, YarrMatchOnly8BitPtrTag>(m_matchOnly8.code().executableAddress())(input, start, length, 0, freeParenContext, parenContextSize)); |
102 | } |
103 | |
104 | MatchResult execute(const UChar* input, unsigned start, unsigned length, void* freeParenContext, unsigned parenContextSize) |
105 | { |
106 | ASSERT(has16BitCodeMatchOnly()); |
107 | return MatchResult(untagCFunctionPtr<YarrJITCodeMatchOnly16, YarrMatchOnly16BitPtrTag>(m_matchOnly16.code().executableAddress())(input, start, length, 0, freeParenContext, parenContextSize)); |
108 | } |
109 | |
110 | #if ENABLE(REGEXP_TRACING) |
111 | void *get8BitMatchOnlyAddr() |
112 | { |
113 | if (!has8BitCodeMatchOnly()) |
114 | return 0; |
115 | |
116 | return m_matchOnly8.code().executableAddress(); |
117 | } |
118 | |
119 | void *get16BitMatchOnlyAddr() |
120 | { |
121 | if (!has16BitCodeMatchOnly()) |
122 | return 0; |
123 | |
124 | return m_matchOnly16.code().executableAddress(); |
125 | } |
126 | |
127 | void *get8BitMatchAddr() |
128 | { |
129 | if (!has8BitCode()) |
130 | return 0; |
131 | |
132 | return m_ref8.code().executableAddress(); |
133 | } |
134 | |
135 | void *get16BitMatchAddr() |
136 | { |
137 | if (!has16BitCode()) |
138 | return 0; |
139 | |
140 | return m_ref16.code().executableAddress(); |
141 | } |
142 | #endif |
143 | |
144 | size_t size() const |
145 | { |
146 | return m_ref8.size() + m_ref16.size() + m_matchOnly8.size() + m_matchOnly16.size(); |
147 | } |
148 | |
149 | void clear() |
150 | { |
151 | m_ref8 = MacroAssemblerCodeRef<Yarr8BitPtrTag>(); |
152 | m_ref16 = MacroAssemblerCodeRef<Yarr16BitPtrTag>(); |
153 | m_matchOnly8 = MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag>(); |
154 | m_matchOnly16 = MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag>(); |
155 | m_failureReason = WTF::nullopt; |
156 | } |
157 | |
158 | private: |
159 | MacroAssemblerCodeRef<Yarr8BitPtrTag> m_ref8; |
160 | MacroAssemblerCodeRef<Yarr16BitPtrTag> m_ref16; |
161 | MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> m_matchOnly8; |
162 | MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> m_matchOnly16; |
163 | bool m_usesPatternContextBuffer { false }; |
164 | Optional<JITFailureReason> m_failureReason; |
165 | }; |
166 | |
167 | enum YarrJITCompileMode { |
168 | MatchOnly, |
169 | IncludeSubpatterns |
170 | }; |
171 | void jitCompile(YarrPattern&, String& patternString, YarrCharSize, VM*, YarrCodeBlock& jitObject, YarrJITCompileMode = IncludeSubpatterns); |
172 | |
173 | } } // namespace JSC::Yarr |
174 | |
175 | #endif |
176 | |