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