Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / arm64 / regexp-macro-assembler-arm64.h
1 // Copyright 2013 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_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_
6 #define V8_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_
7
8 #include "arm64/assembler-arm64.h"
9 #include "arm64/assembler-arm64-inl.h"
10 #include "macro-assembler.h"
11
12 namespace v8 {
13 namespace internal {
14
15
16 #ifndef V8_INTERPRETED_REGEXP
17 class RegExpMacroAssemblerARM64: public NativeRegExpMacroAssembler {
18  public:
19   RegExpMacroAssemblerARM64(Mode mode, int registers_to_save, Zone* zone);
20   virtual ~RegExpMacroAssemblerARM64();
21   virtual int stack_limit_slack();
22   virtual void AdvanceCurrentPosition(int by);
23   virtual void AdvanceRegister(int reg, int by);
24   virtual void Backtrack();
25   virtual void Bind(Label* label);
26   virtual void CheckAtStart(Label* on_at_start);
27   virtual void CheckCharacter(unsigned c, Label* on_equal);
28   virtual void CheckCharacterAfterAnd(unsigned c,
29                                       unsigned mask,
30                                       Label* on_equal);
31   virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
32   virtual void CheckCharacterLT(uc16 limit, Label* on_less);
33   virtual void CheckCharacters(Vector<const uc16> str,
34                                int cp_offset,
35                                Label* on_failure,
36                                bool check_end_of_string);
37   // A "greedy loop" is a loop that is both greedy and with a simple
38   // body. It has a particularly simple implementation.
39   virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
40   virtual void CheckNotAtStart(Label* on_not_at_start);
41   virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
42   virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
43                                                Label* on_no_match);
44   virtual void CheckNotCharacter(unsigned c, Label* on_not_equal);
45   virtual void CheckNotCharacterAfterAnd(unsigned c,
46                                          unsigned mask,
47                                          Label* on_not_equal);
48   virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
49                                               uc16 minus,
50                                               uc16 mask,
51                                               Label* on_not_equal);
52   virtual void CheckCharacterInRange(uc16 from,
53                                      uc16 to,
54                                      Label* on_in_range);
55   virtual void CheckCharacterNotInRange(uc16 from,
56                                         uc16 to,
57                                         Label* on_not_in_range);
58   virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
59
60   // Checks whether the given offset from the current position is before
61   // the end of the string.
62   virtual void CheckPosition(int cp_offset, Label* on_outside_input);
63   virtual bool CheckSpecialCharacterClass(uc16 type,
64                                           Label* on_no_match);
65   virtual void Fail();
66   virtual Handle<HeapObject> GetCode(Handle<String> source);
67   virtual void GoTo(Label* label);
68   virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
69   virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
70   virtual void IfRegisterEqPos(int reg, Label* if_eq);
71   virtual IrregexpImplementation Implementation();
72   virtual void LoadCurrentCharacter(int cp_offset,
73                                     Label* on_end_of_input,
74                                     bool check_bounds = true,
75                                     int characters = 1);
76   virtual void PopCurrentPosition();
77   virtual void PopRegister(int register_index);
78   virtual void PushBacktrack(Label* label);
79   virtual void PushCurrentPosition();
80   virtual void PushRegister(int register_index,
81                             StackCheckFlag check_stack_limit);
82   virtual void ReadCurrentPositionFromRegister(int reg);
83   virtual void ReadStackPointerFromRegister(int reg);
84   virtual void SetCurrentPositionFromEnd(int by);
85   virtual void SetRegister(int register_index, int to);
86   virtual bool Succeed();
87   virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
88   virtual void ClearRegisters(int reg_from, int reg_to);
89   virtual void WriteStackPointerToRegister(int reg);
90   virtual bool CanReadUnaligned();
91
92   // Called from RegExp if the stack-guard is triggered.
93   // If the code object is relocated, the return address is fixed before
94   // returning.
95   static int CheckStackGuardState(Address* return_address,
96                                   Code* re_code,
97                                   Address re_frame,
98                                   int start_offset,
99                                   const byte** input_start,
100                                   const byte** input_end);
101
102  private:
103   // Above the frame pointer - Stored registers and stack passed parameters.
104   // Callee-saved registers x19-x29, where x29 is the old frame pointer.
105   static const int kCalleeSavedRegisters = 0;
106   // Return address.
107   // It is placed above the 11 callee-saved registers.
108   static const int kReturnAddress = kCalleeSavedRegisters + 11 * kPointerSize;
109   static const int kSecondaryReturnAddress = kReturnAddress + kPointerSize;
110   // Stack parameter placed by caller.
111   static const int kIsolate = kSecondaryReturnAddress + kPointerSize;
112
113   // Below the frame pointer.
114   // Register parameters stored by setup code.
115   static const int kDirectCall = kCalleeSavedRegisters - kPointerSize;
116   static const int kStackBase = kDirectCall - kPointerSize;
117   static const int kOutputSize = kStackBase - kPointerSize;
118   static const int kInput = kOutputSize - kPointerSize;
119   // When adding local variables remember to push space for them in
120   // the frame in GetCode.
121   static const int kSuccessCounter = kInput - kPointerSize;
122   // First position register address on the stack. Following positions are
123   // below it. A position is a 32 bit value.
124   static const int kFirstRegisterOnStack = kSuccessCounter - kWRegSize;
125   // A capture is a 64 bit value holding two position.
126   static const int kFirstCaptureOnStack = kSuccessCounter - kXRegSize;
127
128   // Initial size of code buffer.
129   static const size_t kRegExpCodeSize = 1024;
130
131   // When initializing registers to a non-position value we can unroll
132   // the loop. Set the limit of registers to unroll.
133   static const int kNumRegistersToUnroll = 16;
134
135   // We are using x0 to x7 as a register cache. Each hardware register must
136   // contain one capture, that is two 32 bit registers. We can cache at most
137   // 16 registers.
138   static const int kNumCachedRegisters = 16;
139
140   // Load a number of characters at the given offset from the
141   // current position, into the current-character register.
142   void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
143
144   // Check whether preemption has been requested.
145   void CheckPreemption();
146
147   // Check whether we are exceeding the stack limit on the backtrack stack.
148   void CheckStackLimit();
149
150   // Generate a call to CheckStackGuardState.
151   void CallCheckStackGuardState(Register scratch);
152
153   // Location of a 32 bit position register.
154   MemOperand register_location(int register_index);
155
156   // Location of a 64 bit capture, combining two position registers.
157   MemOperand capture_location(int register_index, Register scratch);
158
159   // Register holding the current input position as negative offset from
160   // the end of the string.
161   Register current_input_offset() { return w21; }
162
163   // The register containing the current character after LoadCurrentCharacter.
164   Register current_character() { return w22; }
165
166   // Register holding address of the end of the input string.
167   Register input_end() { return x25; }
168
169   // Register holding address of the start of the input string.
170   Register input_start() { return x26; }
171
172   // Register holding the offset from the start of the string where we should
173   // start matching.
174   Register start_offset() { return w27; }
175
176   // Pointer to the output array's first element.
177   Register output_array() { return x28; }
178
179   // Register holding the frame address. Local variables, parameters and
180   // regexp registers are addressed relative to this.
181   Register frame_pointer() { return fp; }
182
183   // The register containing the backtrack stack top. Provides a meaningful
184   // name to the register.
185   Register backtrack_stackpointer() { return x23; }
186
187   // Register holding pointer to the current code object.
188   Register code_pointer() { return x20; }
189
190   // Register holding the value used for clearing capture registers.
191   Register non_position_value() { return w24; }
192   // The top 32 bit of this register is used to store this value
193   // twice. This is used for clearing more than one register at a time.
194   Register twice_non_position_value() { return x24; }
195
196   // Byte size of chars in the string to match (decided by the Mode argument)
197   int char_size() { return static_cast<int>(mode_); }
198
199   // Equivalent to a conditional branch to the label, unless the label
200   // is NULL, in which case it is a conditional Backtrack.
201   void BranchOrBacktrack(Condition condition, Label* to);
202
203   // Compares reg against immmediate before calling BranchOrBacktrack.
204   // It makes use of the Cbz and Cbnz instructions.
205   void CompareAndBranchOrBacktrack(Register reg,
206                                    int immediate,
207                                    Condition condition,
208                                    Label* to);
209
210   inline void CallIf(Label* to, Condition condition);
211
212   // Save and restore the link register on the stack in a way that
213   // is GC-safe.
214   inline void SaveLinkRegister();
215   inline void RestoreLinkRegister();
216
217   // Pushes the value of a register on the backtrack stack. Decrements the
218   // stack pointer by a word size and stores the register's value there.
219   inline void Push(Register source);
220
221   // Pops a value from the backtrack stack. Reads the word at the stack pointer
222   // and increments it by a word size.
223   inline void Pop(Register target);
224
225   // This state indicates where the register actually is.
226   enum RegisterState {
227     STACKED,     // Resides in memory.
228     CACHED_LSW,  // Least Significant Word of a 64 bit hardware register.
229     CACHED_MSW   // Most Significant Word of a 64 bit hardware register.
230   };
231
232   RegisterState GetRegisterState(int register_index) {
233     ASSERT(register_index >= 0);
234     if (register_index >= kNumCachedRegisters) {
235       return STACKED;
236     } else {
237       if ((register_index % 2) == 0) {
238         return CACHED_LSW;
239       } else {
240         return CACHED_MSW;
241       }
242     }
243   }
244
245   // Store helper that takes the state of the register into account.
246   inline void StoreRegister(int register_index, Register source);
247
248   // Returns a hardware W register that holds the value of the capture
249   // register.
250   //
251   // This function will try to use an existing cache register (w0-w7) for the
252   // result. Otherwise, it will load the value into maybe_result.
253   //
254   // If the returned register is anything other than maybe_result, calling code
255   // must not write to it.
256   inline Register GetRegister(int register_index, Register maybe_result);
257
258   // Returns the harware register (x0-x7) holding the value of the capture
259   // register.
260   // This assumes that the state of the register is not STACKED.
261   inline Register GetCachedRegister(int register_index);
262
263   Isolate* isolate() const { return masm_->isolate(); }
264
265   MacroAssembler* masm_;
266
267   // Which mode to generate code for (ASCII or UC16).
268   Mode mode_;
269
270   // One greater than maximal register index actually used.
271   int num_registers_;
272
273   // Number of registers to output at the end (the saved registers
274   // are always 0..num_saved_registers_-1)
275   int num_saved_registers_;
276
277   // Labels used internally.
278   Label entry_label_;
279   Label start_label_;
280   Label success_label_;
281   Label backtrack_label_;
282   Label exit_label_;
283   Label check_preempt_label_;
284   Label stack_overflow_label_;
285 };
286
287 #endif  // V8_INTERPRETED_REGEXP
288
289
290 }}  // namespace v8::internal
291
292 #endif  // V8_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_