V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
regexp-macro-assembler-x64.h
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 
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 = kLastCalleeSaveRegister - kPointerSize;
151  // When adding local variables remember to push space for them in
152  // the frame in GetCode.
153  static const int kStringStartMinusOne = kSuccessfulCaptures - kPointerSize;
154 
155  // First register address. Following registers are below it on the stack.
156  static const int kRegisterZero = kStringStartMinusOne - kPointerSize;
157 
158  // Initial size of code buffer.
159  static const size_t kRegExpCodeSize = 1024;
160 
161  // Load a number of characters at the given offset from the
162  // current position, into the current-character register.
163  void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
164 
165  // Check whether preemption has been requested.
166  void CheckPreemption();
167 
168  // Check whether we are exceeding the stack limit on the backtrack stack.
169  void CheckStackLimit();
170 
171  // Generate a call to CheckStackGuardState.
172  void CallCheckStackGuardState();
173 
174  // The rbp-relative location of a regexp register.
175  Operand register_location(int register_index);
176 
177  // The register containing the current character after LoadCurrentCharacter.
178  inline Register current_character() { return rdx; }
179 
180  // The register containing the backtrack stack top. Provides a meaningful
181  // name to the register.
182  inline Register backtrack_stackpointer() { return rcx; }
183 
184  // The registers containing a self pointer to this code's Code object.
185  inline Register code_object_pointer() { return r8; }
186 
187  // Byte size of chars in the string to match (decided by the Mode argument)
188  inline int char_size() { return static_cast<int>(mode_); }
189 
190  // Equivalent to a conditional branch to the label, unless the label
191  // is nullptr, in which case it is a conditional Backtrack.
192  void BranchOrBacktrack(Condition condition, Label* to);
193 
194  void MarkPositionForCodeRelativeFixup() {
195  code_relative_fixup_positions_.push_back(masm_.pc_offset());
196  }
197 
198  void FixupCodeRelativePositions();
199 
200  // Call and return internally in the generated code in a way that
201  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
202  inline void SafeCall(Label* to);
203  inline void SafeCallTarget(Label* label);
204  inline void SafeReturn();
205 
206  // Pushes the value of a register on the backtrack stack. Decrements the
207  // stack pointer (rcx) by a word size and stores the register's value there.
208  inline void Push(Register source);
209 
210  // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
211  // by a word size and stores the value there.
212  inline void Push(Immediate value);
213 
214  // Pushes the Code object relative offset of a label on the backtrack stack
215  // (i.e., a backtrack target). Decrements the stack pointer (rcx)
216  // by a word size and stores the value there.
217  inline void Push(Label* label);
218 
219  // Pops a value from the backtrack stack. Reads the word at the stack pointer
220  // (rcx) and increments it by a word size.
221  inline void Pop(Register target);
222 
223  // Drops the top value from the backtrack stack without reading it.
224  // Increments the stack pointer (rcx) by a word size.
225  inline void Drop();
226 
227  inline void ReadPositionFromRegister(Register dst, int reg);
228 
229  Isolate* isolate() const { return masm_.isolate(); }
230 
231  MacroAssembler masm_;
232  NoRootArrayScope no_root_array_scope_;
233 
234  ZoneChunkList<int> code_relative_fixup_positions_;
235 
236  // Which mode to generate code for (LATIN1 or UC16).
237  Mode mode_;
238 
239  // One greater than maximal register index actually used.
240  int num_registers_;
241 
242  // Number of registers to output at the end (the saved registers
243  // are always 0..num_saved_registers_-1)
244  int num_saved_registers_;
245 
246  // Labels used internally.
247  Label entry_label_;
248  Label start_label_;
249  Label success_label_;
250  Label backtrack_label_;
251  Label exit_label_;
252  Label check_preempt_label_;
253  Label stack_overflow_label_;
254 };
255 
256 #endif // V8_INTERPRETED_REGEXP
257 
258 } // namespace internal
259 } // namespace v8
260 
261 #endif // V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
Definition: libplatform.h:13