Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
6 : #define V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
7 :
8 : #include "src/macro-assembler.h"
9 : #include "src/regexp/regexp-macro-assembler.h"
10 : #include "src/x64/assembler-x64.h"
11 : #include "src/zone/zone-chunk-list.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 : #ifndef V8_INTERPRETED_REGEXP
17 :
18 : class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler {
19 : public:
20 : RegExpMacroAssemblerX64(Isolate* isolate, Zone* zone, Mode mode,
21 : int registers_to_save);
22 : ~RegExpMacroAssemblerX64() override;
23 : int stack_limit_slack() override;
24 : void AdvanceCurrentPosition(int by) override;
25 : void AdvanceRegister(int reg, int by) override;
26 : void Backtrack() override;
27 : void Bind(Label* label) override;
28 : void CheckAtStart(Label* on_at_start) override;
29 : void CheckCharacter(uint32_t c, Label* on_equal) override;
30 : void CheckCharacterAfterAnd(uint32_t c, uint32_t mask,
31 : Label* on_equal) override;
32 : void CheckCharacterGT(uc16 limit, Label* on_greater) override;
33 : void CheckCharacterLT(uc16 limit, Label* on_less) override;
34 : // A "greedy loop" is a loop that is both greedy and with a simple
35 : // body. It has a particularly simple implementation.
36 : void CheckGreedyLoop(Label* on_tos_equals_current_position) override;
37 : void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override;
38 : void CheckNotBackReference(int start_reg, bool read_backward,
39 : Label* on_no_match) override;
40 : void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
41 : bool unicode,
42 : Label* on_no_match) override;
43 : void CheckNotCharacter(uint32_t c, Label* on_not_equal) override;
44 : void CheckNotCharacterAfterAnd(uint32_t c, uint32_t mask,
45 : Label* on_not_equal) override;
46 : void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 mask,
47 : Label* on_not_equal) override;
48 : void CheckCharacterInRange(uc16 from, uc16 to, Label* on_in_range) override;
49 : void CheckCharacterNotInRange(uc16 from, uc16 to,
50 : Label* on_not_in_range) override;
51 : void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
52 :
53 : // Checks whether the given offset from the current position is before
54 : // the end of the string.
55 : void CheckPosition(int cp_offset, Label* on_outside_input) override;
56 : bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match) override;
57 : void Fail() override;
58 : Handle<HeapObject> GetCode(Handle<String> source) override;
59 : void GoTo(Label* label) override;
60 : void IfRegisterGE(int reg, int comparand, Label* if_ge) override;
61 : void IfRegisterLT(int reg, int comparand, Label* if_lt) override;
62 : void IfRegisterEqPos(int reg, Label* if_eq) override;
63 : IrregexpImplementation Implementation() override;
64 : void LoadCurrentCharacter(int cp_offset, Label* on_end_of_input,
65 : bool check_bounds = true,
66 : int characters = 1) override;
67 : void PopCurrentPosition() override;
68 : void PopRegister(int register_index) override;
69 : void PushBacktrack(Label* label) override;
70 : void PushCurrentPosition() override;
71 : void PushRegister(int register_index,
72 : StackCheckFlag check_stack_limit) override;
73 : void ReadCurrentPositionFromRegister(int reg) override;
74 : void ReadStackPointerFromRegister(int reg) override;
75 : void SetCurrentPositionFromEnd(int by) override;
76 : void SetRegister(int register_index, int to) override;
77 : bool Succeed() override;
78 : void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
79 : void ClearRegisters(int reg_from, int reg_to) override;
80 : void WriteStackPointerToRegister(int reg) override;
81 :
82 : // Called from RegExp if the stack-guard is triggered.
83 : // If the code object is relocated, the return address is fixed before
84 : // returning.
85 : // {raw_code} is an Address because this is called via ExternalReference.
86 : static int CheckStackGuardState(Address* return_address, Address raw_code,
87 : Address re_frame);
88 :
89 : private:
90 : // Offsets from rbp of function parameters and stored registers.
91 : static const int kFramePointer = 0;
92 : // Above the frame pointer - function parameters and return address.
93 : static const int kReturn_eip = kFramePointer + kRegisterSize;
94 : static const int kFrameAlign = kReturn_eip + kRegisterSize;
95 :
96 : #ifdef _WIN64
97 : // Parameters (first four passed as registers, but with room on stack).
98 : // In Microsoft 64-bit Calling Convention, there is room on the callers
99 : // stack (before the return address) to spill parameter registers. We
100 : // use this space to store the register passed parameters.
101 : static const int kInputString = kFrameAlign;
102 : // StartIndex is passed as 32 bit int.
103 : static const int kStartIndex = kInputString + kRegisterSize;
104 : static const int kInputStart = kStartIndex + kRegisterSize;
105 : static const int kInputEnd = kInputStart + kRegisterSize;
106 : static const int kRegisterOutput = kInputEnd + kRegisterSize;
107 : // For the case of global regular expression, we have room to store at least
108 : // one set of capture results. For the case of non-global regexp, we ignore
109 : // this value. NumOutputRegisters is passed as 32-bit value. The upper
110 : // 32 bit of this 64-bit stack slot may contain garbage.
111 : static const int kNumOutputRegisters = kRegisterOutput + kRegisterSize;
112 : static const int kStackHighEnd = kNumOutputRegisters + kRegisterSize;
113 : // DirectCall is passed as 32 bit int (values 0 or 1).
114 : static const int kDirectCall = kStackHighEnd + kRegisterSize;
115 : static const int kIsolate = kDirectCall + kRegisterSize;
116 : #else
117 : // In AMD64 ABI Calling Convention, the first six integer parameters
118 : // are passed as registers, and caller must allocate space on the stack
119 : // if it wants them stored. We push the parameters after the frame pointer.
120 : static const int kInputString = kFramePointer - kRegisterSize;
121 : static const int kStartIndex = kInputString - kRegisterSize;
122 : static const int kInputStart = kStartIndex - kRegisterSize;
123 : static const int kInputEnd = kInputStart - kRegisterSize;
124 : static const int kRegisterOutput = kInputEnd - kRegisterSize;
125 :
126 : // For the case of global regular expression, we have room to store at least
127 : // one set of capture results. For the case of non-global regexp, we ignore
128 : // this value.
129 : static const int kNumOutputRegisters = kRegisterOutput - kRegisterSize;
130 : static const int kStackHighEnd = kFrameAlign;
131 : static const int kDirectCall = kStackHighEnd + kRegisterSize;
132 : static const int kIsolate = kDirectCall + kRegisterSize;
133 : #endif
134 :
135 : #ifdef _WIN64
136 : // Microsoft calling convention has three callee-saved registers
137 : // (that we are using). We push these after the frame pointer.
138 : static const int kBackup_rsi = kFramePointer - kRegisterSize;
139 : static const int kBackup_rdi = kBackup_rsi - kRegisterSize;
140 : static const int kBackup_rbx = kBackup_rdi - kRegisterSize;
141 : static const int kLastCalleeSaveRegister = kBackup_rbx;
142 : #else
143 : // AMD64 Calling Convention has only one callee-save register that
144 : // we use. We push this after the frame pointer (and after the
145 : // parameters).
146 : static const int kBackup_rbx = kNumOutputRegisters - kRegisterSize;
147 : static const int kLastCalleeSaveRegister = kBackup_rbx;
148 : #endif
149 :
150 : static const int kSuccessfulCaptures =
151 : kLastCalleeSaveRegister - kSystemPointerSize;
152 : // When adding local variables remember to push space for them in
153 : // the frame in GetCode.
154 : static const int kStringStartMinusOne =
155 : kSuccessfulCaptures - kSystemPointerSize;
156 :
157 : // First register address. Following registers are below it on the stack.
158 : static const int kRegisterZero = kStringStartMinusOne - kSystemPointerSize;
159 :
160 : // Initial size of code buffer.
161 : static const int kRegExpCodeSize = 1024;
162 :
163 : // Load a number of characters at the given offset from the
164 : // current position, into the current-character register.
165 : void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
166 :
167 : // Check whether preemption has been requested.
168 : void CheckPreemption();
169 :
170 : // Check whether we are exceeding the stack limit on the backtrack stack.
171 : void CheckStackLimit();
172 :
173 : // Generate a call to CheckStackGuardState.
174 : void CallCheckStackGuardState();
175 :
176 : // The rbp-relative location of a regexp register.
177 : Operand register_location(int register_index);
178 :
179 : // The register containing the current character after LoadCurrentCharacter.
180 : inline Register current_character() { return rdx; }
181 :
182 : // The register containing the backtrack stack top. Provides a meaningful
183 : // name to the register.
184 : inline Register backtrack_stackpointer() { return rcx; }
185 :
186 : // The registers containing a self pointer to this code's Code object.
187 : inline Register code_object_pointer() { return r8; }
188 :
189 : // Byte size of chars in the string to match (decided by the Mode argument)
190 1247562 : inline int char_size() { return static_cast<int>(mode_); }
191 :
192 : // Equivalent to a conditional branch to the label, unless the label
193 : // is nullptr, in which case it is a conditional Backtrack.
194 : void BranchOrBacktrack(Condition condition, Label* to);
195 :
196 : void MarkPositionForCodeRelativeFixup() {
197 1191148 : code_relative_fixup_positions_.push_back(masm_.pc_offset());
198 : }
199 :
200 : void FixupCodeRelativePositions();
201 :
202 : // Call and return internally in the generated code in a way that
203 : // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
204 : inline void SafeCall(Label* to);
205 : inline void SafeCallTarget(Label* label);
206 : inline void SafeReturn();
207 :
208 : // Pushes the value of a register on the backtrack stack. Decrements the
209 : // stack pointer (rcx) by a word size and stores the register's value there.
210 : inline void Push(Register source);
211 :
212 : // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
213 : // by a word size and stores the value there.
214 : inline void Push(Immediate value);
215 :
216 : // Pushes the Code object relative offset of a label on the backtrack stack
217 : // (i.e., a backtrack target). Decrements the stack pointer (rcx)
218 : // by a word size and stores the value there.
219 : inline void Push(Label* label);
220 :
221 : // Pops a value from the backtrack stack. Reads the word at the stack pointer
222 : // (rcx) and increments it by a word size.
223 : inline void Pop(Register target);
224 :
225 : // Drops the top value from the backtrack stack without reading it.
226 : // Increments the stack pointer (rcx) by a word size.
227 : inline void Drop();
228 :
229 : inline void ReadPositionFromRegister(Register dst, int reg);
230 :
231 1326941 : Isolate* isolate() const { return masm_.isolate(); }
232 :
233 : MacroAssembler masm_;
234 : NoRootArrayScope no_root_array_scope_;
235 :
236 : ZoneChunkList<int> code_relative_fixup_positions_;
237 :
238 : // Which mode to generate code for (LATIN1 or UC16).
239 : Mode mode_;
240 :
241 : // One greater than maximal register index actually used.
242 : int num_registers_;
243 :
244 : // Number of registers to output at the end (the saved registers
245 : // are always 0..num_saved_registers_-1)
246 : int num_saved_registers_;
247 :
248 : // Labels used internally.
249 : Label entry_label_;
250 : Label start_label_;
251 : Label success_label_;
252 : Label backtrack_label_;
253 : Label exit_label_;
254 : Label check_preempt_label_;
255 : Label stack_overflow_label_;
256 : };
257 :
258 : #endif // V8_INTERPRETED_REGEXP
259 :
260 : } // namespace internal
261 : } // namespace v8
262 :
263 : #endif // V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
|