• 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_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
6 #define V8_REGEXP_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
7 
8 #include "src/base/strings.h"
9 #include "src/codegen/ia32/assembler-ia32.h"
10 #include "src/codegen/macro-assembler.h"
11 #include "src/regexp/regexp-macro-assembler.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class V8_EXPORT_PRIVATE RegExpMacroAssemblerIA32
17     : public NativeRegExpMacroAssembler {
18  public:
19   RegExpMacroAssemblerIA32(Isolate* isolate, Zone* zone, Mode mode,
20                            int registers_to_save);
21   ~RegExpMacroAssemblerIA32() 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(base::uc16 limit, Label* on_greater) override;
32   void CheckCharacterLT(base::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(base::uc16 c, base::uc16 minus,
46                                       base::uc16 mask,
47                                       Label* on_not_equal) override;
48   void CheckCharacterInRange(base::uc16 from, base::uc16 to,
49                              Label* on_in_range) override;
50   void CheckCharacterNotInRange(base::uc16 from, base::uc16 to,
51                                 Label* on_not_in_range) override;
52   bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges,
53                                   Label* on_in_range) override;
54   bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges,
55                                      Label* on_not_in_range) override;
56   void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
57 
58   // Checks whether the given offset from the current position is before
59   // the end of the string.
60   void CheckPosition(int cp_offset, Label* on_outside_input) override;
61   bool CheckSpecialCharacterClass(StandardCharacterSet type,
62                                   Label* on_no_match) override;
63   void Fail() override;
64   Handle<HeapObject> GetCode(Handle<String> source) override;
65   void GoTo(Label* label) override;
66   void IfRegisterGE(int reg, int comparand, Label* if_ge) override;
67   void IfRegisterLT(int reg, int comparand, Label* if_lt) override;
68   void IfRegisterEqPos(int reg, Label* if_eq) override;
69   IrregexpImplementation Implementation() override;
70   void LoadCurrentCharacterUnchecked(int cp_offset,
71                                      int character_count) override;
72   void PopCurrentPosition() override;
73   void PopRegister(int register_index) override;
74   void PushBacktrack(Label* label) override;
75   void PushCurrentPosition() override;
76   void PushRegister(int register_index,
77                     StackCheckFlag check_stack_limit) override;
78   void ReadCurrentPositionFromRegister(int reg) override;
79   void ReadStackPointerFromRegister(int reg) override;
80   void SetCurrentPositionFromEnd(int by) override;
81   void SetRegister(int register_index, int to) override;
82   bool Succeed() override;
83   void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
84   void ClearRegisters(int reg_from, int reg_to) override;
85   void WriteStackPointerToRegister(int reg) override;
86 
87   // Called from RegExp if the stack-guard is triggered.
88   // If the code object is relocated, the return address is fixed before
89   // returning.
90   // {raw_code} is an Address because this is called via ExternalReference.
91   static int CheckStackGuardState(Address* return_address, Address raw_code,
92                                   Address re_frame);
93 
94  private:
95   Operand StaticVariable(const ExternalReference& ext);
96   // Offsets from ebp of function parameters and stored registers.
97   static const int kFramePointer = 0;
98   // Above the frame pointer - function parameters and return address.
99   static const int kReturn_eip = kFramePointer + kSystemPointerSize;
100   static const int kFrameAlign = kReturn_eip + kSystemPointerSize;
101   // Parameters.
102   static const int kInputString = kFrameAlign;
103   static const int kStartIndex = kInputString + kSystemPointerSize;
104   static const int kInputStart = kStartIndex + kSystemPointerSize;
105   static const int kInputEnd = kInputStart + kSystemPointerSize;
106   static const int kRegisterOutput = kInputEnd + kSystemPointerSize;
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.
110   static const int kNumOutputRegisters = kRegisterOutput + kSystemPointerSize;
111   static const int kDirectCall = kNumOutputRegisters + kSystemPointerSize;
112   static const int kIsolate = kDirectCall + kSystemPointerSize;
113   // Below the frame pointer - local stack variables.
114   // When adding local variables remember to push space for them in
115   // the frame in GetCode.
116   static const int kBackup_esi = kFramePointer - kSystemPointerSize;
117   static const int kBackup_edi = kBackup_esi - kSystemPointerSize;
118   static const int kBackup_ebx = kBackup_edi - kSystemPointerSize;
119   static const int kLastCalleeSaveRegister = kBackup_ebx;
120 
121   static const int kSuccessfulCaptures =
122       kLastCalleeSaveRegister - kSystemPointerSize;
123   static const int kStringStartMinusOne =
124       kSuccessfulCaptures - kSystemPointerSize;
125   static const int kBacktrackCount = kStringStartMinusOne - kSystemPointerSize;
126   // Stores the initial value of the regexp stack pointer in a
127   // position-independent representation (in case the regexp stack grows and
128   // thus moves).
129   static const int kRegExpStackBasePointer =
130       kBacktrackCount - kSystemPointerSize;
131   // First register address. Following registers are below it on the stack.
132   static const int kRegisterZero = kRegExpStackBasePointer - kSystemPointerSize;
133 
134   // Initial size of code buffer.
135   static const int kRegExpCodeSize = 1024;
136 
137   void PushCallerSavedRegisters();
138   void PopCallerSavedRegisters();
139 
140   // Check whether preemption has been requested.
141   void CheckPreemption();
142 
143   // Check whether we are exceeding the stack limit on the backtrack stack.
144   void CheckStackLimit();
145 
146   void CallCheckStackGuardState(Register scratch);
147   void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges);
148 
149   // The ebp-relative location of a regexp register.
150   Operand register_location(int register_index);
151 
152   // The register containing the current character after LoadCurrentCharacter.
current_character()153   static constexpr Register current_character() { return edx; }
154 
155   // The register containing the backtrack stack top. Provides a meaningful
156   // name to the register.
backtrack_stackpointer()157   static constexpr Register backtrack_stackpointer() { return ecx; }
158 
159   // Byte size of chars in the string to match (decided by the Mode argument)
char_size()160   inline int char_size() const { return static_cast<int>(mode_); }
161 
162   // Equivalent to a conditional branch to the label, unless the label
163   // is nullptr, in which case it is a conditional Backtrack.
164   void BranchOrBacktrack(Condition condition, Label* to);
165 
166   // Call and return internally in the generated code in a way that
167   // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
168   inline void SafeCall(Label* to);
169   inline void SafeReturn();
170   inline void SafeCallTarget(Label* name);
171 
172   // Pushes the value of a register on the backtrack stack. Decrements the
173   // stack pointer (ecx) by a word size and stores the register's value there.
174   inline void Push(Register source);
175 
176   // Pushes a value on the backtrack stack. Decrements the stack pointer (ecx)
177   // by a word size and stores the value there.
178   inline void Push(Immediate value);
179 
180   // Pops a value from the backtrack stack. Reads the word at the stack pointer
181   // (ecx) and increments it by a word size.
182   inline void Pop(Register target);
183 
184   void LoadRegExpStackPointerFromMemory(Register dst);
185   void StoreRegExpStackPointerToMemory(Register src, Register scratch);
186   void PushRegExpBasePointer(Register stack_pointer, Register scratch);
187   void PopRegExpBasePointer(Register stack_pointer_out, Register scratch);
188 
isolate()189   Isolate* isolate() const { return masm_->isolate(); }
190 
191   const std::unique_ptr<MacroAssembler> masm_;
192   const NoRootArrayScope no_root_array_scope_;
193 
194   // Which mode to generate code for (LATIN1 or UC16).
195   const Mode mode_;
196 
197   // One greater than maximal register index actually used.
198   int num_registers_;
199 
200   // Number of registers to output at the end (the saved registers
201   // are always 0..num_saved_registers_-1).
202   const int num_saved_registers_;
203 
204   // Labels used internally.
205   Label entry_label_;
206   Label start_label_;
207   Label success_label_;
208   Label backtrack_label_;
209   Label exit_label_;
210   Label check_preempt_label_;
211   Label stack_overflow_label_;
212   Label fallback_label_;
213 };
214 
215 }  // namespace internal
216 }  // namespace v8
217 
218 #endif  // V8_REGEXP_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
219