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