blob: 6040d8058ab5f817f09efc4d726d444aeabbd179 [file] [log] [blame]
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001// Copyright 2012 the V8 project authors. All rights reserved.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.org5ec48922009-05-05 07:25:34 +000028#ifndef V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
29#define V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
ager@chromium.orga74f0da2008-12-03 16:05:52 +000030
sgjesse@chromium.org6db88712011-07-11 11:41:22 +000031#include "ia32/assembler-ia32.h"
32#include "ia32/assembler-ia32-inl.h"
ulan@chromium.org32d7dba2013-04-24 10:59:06 +000033#include "macro-assembler.h"
sgjesse@chromium.org6db88712011-07-11 11:41:22 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000037
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000038#ifndef V8_INTERPRETED_REGEXP
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000039class RegExpMacroAssemblerIA32: public NativeRegExpMacroAssembler {
40 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +000041 RegExpMacroAssemblerIA32(Mode mode, int registers_to_save, Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000042 virtual ~RegExpMacroAssemblerIA32();
ager@chromium.org32912102009-01-16 10:38:43 +000043 virtual int stack_limit_slack();
ager@chromium.orga74f0da2008-12-03 16:05:52 +000044 virtual void AdvanceCurrentPosition(int by);
45 virtual void AdvanceRegister(int reg, int by);
46 virtual void Backtrack();
47 virtual void Bind(Label* label);
ager@chromium.orgddb913d2009-01-27 10:01:48 +000048 virtual void CheckAtStart(Label* on_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000049 virtual void CheckCharacter(uint32_t c, Label* on_equal);
50 virtual void CheckCharacterAfterAnd(uint32_t c,
51 uint32_t mask,
52 Label* on_equal);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000053 virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
54 virtual void CheckCharacterLT(uc16 limit, Label* on_less);
55 virtual void CheckCharacters(Vector<const uc16> str,
56 int cp_offset,
ager@chromium.org8bb60582008-12-11 12:02:20 +000057 Label* on_failure,
58 bool check_end_of_string);
ager@chromium.org32912102009-01-16 10:38:43 +000059 // A "greedy loop" is a loop that is both greedy and with a simple
60 // body. It has a particularly simple implementation.
ager@chromium.org8bb60582008-12-11 12:02:20 +000061 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000062 virtual void CheckNotAtStart(Label* on_not_at_start);
63 virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
64 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
65 Label* on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000066 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
67 virtual void CheckNotCharacterAfterAnd(uint32_t c,
68 uint32_t mask,
69 Label* on_not_equal);
70 virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
71 uc16 minus,
72 uc16 mask,
73 Label* on_not_equal);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000074 virtual void CheckCharacterInRange(uc16 from,
75 uc16 to,
76 Label* on_in_range);
77 virtual void CheckCharacterNotInRange(uc16 from,
78 uc16 to,
79 Label* on_not_in_range);
80 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
81
ager@chromium.orgddb913d2009-01-27 10:01:48 +000082 // Checks whether the given offset from the current position is before
83 // the end of the string.
84 virtual void CheckPosition(int cp_offset, Label* on_outside_input);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000085 virtual bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000086 virtual void Fail();
karlklose@chromium.org83a47282011-05-11 11:54:09 +000087 virtual Handle<HeapObject> GetCode(Handle<String> source);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088 virtual void GoTo(Label* label);
89 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
90 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
ager@chromium.org32912102009-01-16 10:38:43 +000091 virtual void IfRegisterEqPos(int reg, Label* if_eq);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000092 virtual IrregexpImplementation Implementation();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000093 virtual void LoadCurrentCharacter(int cp_offset,
94 Label* on_end_of_input,
95 bool check_bounds = true,
96 int characters = 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000097 virtual void PopCurrentPosition();
98 virtual void PopRegister(int register_index);
99 virtual void PushBacktrack(Label* label);
100 virtual void PushCurrentPosition();
ager@chromium.org32912102009-01-16 10:38:43 +0000101 virtual void PushRegister(int register_index,
102 StackCheckFlag check_stack_limit);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000103 virtual void ReadCurrentPositionFromRegister(int reg);
104 virtual void ReadStackPointerFromRegister(int reg);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000105 virtual void SetCurrentPositionFromEnd(int by);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000106 virtual void SetRegister(int register_index, int to);
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000107 virtual bool Succeed();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000108 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000109 virtual void ClearRegisters(int reg_from, int reg_to);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000110 virtual void WriteStackPointerToRegister(int reg);
111
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000112 // Called from RegExp if the stack-guard is triggered.
113 // If the code object is relocated, the return address is fixed before
114 // returning.
115 static int CheckStackGuardState(Address* return_address,
116 Code* re_code,
117 Address re_frame);
118
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000119 private:
ager@chromium.org32912102009-01-16 10:38:43 +0000120 // Offsets from ebp of function parameters and stored registers.
121 static const int kFramePointer = 0;
122 // Above the frame pointer - function parameters and return address.
123 static const int kReturn_eip = kFramePointer + kPointerSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000124 static const int kFrameAlign = kReturn_eip + kPointerSize;
125 // Parameters.
126 static const int kInputString = kFrameAlign;
127 static const int kStartIndex = kInputString + kPointerSize;
128 static const int kInputStart = kStartIndex + kPointerSize;
129 static const int kInputEnd = kInputStart + kPointerSize;
130 static const int kRegisterOutput = kInputEnd + kPointerSize;
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000131 // For the case of global regular expression, we have room to store at least
132 // one set of capture results. For the case of non-global regexp, we ignore
133 // this value.
134 static const int kNumOutputRegisters = kRegisterOutput + kPointerSize;
135 static const int kStackHighEnd = kNumOutputRegisters + kPointerSize;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000136 static const int kDirectCall = kStackHighEnd + kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 static const int kIsolate = kDirectCall + kPointerSize;
ager@chromium.org32912102009-01-16 10:38:43 +0000138 // Below the frame pointer - local stack variables.
139 // When adding local variables remember to push space for them in
140 // the frame in GetCode.
141 static const int kBackup_esi = kFramePointer - kPointerSize;
142 static const int kBackup_edi = kBackup_esi - kPointerSize;
143 static const int kBackup_ebx = kBackup_edi - kPointerSize;
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000144 static const int kSuccessfulCaptures = kBackup_ebx - kPointerSize;
145 static const int kInputStartMinusOne = kSuccessfulCaptures - kPointerSize;
ager@chromium.org32912102009-01-16 10:38:43 +0000146 // First register address. Following registers are below it on the stack.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000147 static const int kRegisterZero = kInputStartMinusOne - kPointerSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000148
149 // Initial size of code buffer.
150 static const size_t kRegExpCodeSize = 1024;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000151
ager@chromium.org32912102009-01-16 10:38:43 +0000152 // Load a number of characters at the given offset from the
153 // current position, into the current-character register.
154 void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000155
ager@chromium.org32912102009-01-16 10:38:43 +0000156 // Check whether preemption has been requested.
157 void CheckPreemption();
158
159 // Check whether we are exceeding the stack limit on the backtrack stack.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000160 void CheckStackLimit();
161
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000162 // Generate a call to CheckStackGuardState.
163 void CallCheckStackGuardState(Register scratch);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000164
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000165 // The ebp-relative location of a regexp register.
166 Operand register_location(int register_index);
167
168 // The register containing the current character after LoadCurrentCharacter.
ager@chromium.org32912102009-01-16 10:38:43 +0000169 inline Register current_character() { return edx; }
170
171 // The register containing the backtrack stack top. Provides a meaningful
172 // name to the register.
173 inline Register backtrack_stackpointer() { return ecx; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000174
175 // Byte size of chars in the string to match (decided by the Mode argument)
ager@chromium.org32912102009-01-16 10:38:43 +0000176 inline int char_size() { return static_cast<int>(mode_); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000177
178 // Equivalent to a conditional branch to the label, unless the label
179 // is NULL, in which case it is a conditional Backtrack.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000180 void BranchOrBacktrack(Condition condition, Label* to);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000181
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000182 // Call and return internally in the generated code in a way that
183 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
ager@chromium.org32912102009-01-16 10:38:43 +0000184 inline void SafeCall(Label* to);
185 inline void SafeReturn();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000186 inline void SafeCallTarget(Label* name);
ager@chromium.org32912102009-01-16 10:38:43 +0000187
188 // Pushes the value of a register on the backtrack stack. Decrements the
189 // stack pointer (ecx) by a word size and stores the register's value there.
190 inline void Push(Register source);
191
192 // Pushes a value on the backtrack stack. Decrements the stack pointer (ecx)
193 // by a word size and stores the value there.
194 inline void Push(Immediate value);
195
196 // Pops a value from the backtrack stack. Reads the word at the stack pointer
197 // (ecx) and increments it by a word size.
198 inline void Pop(Register target);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000199
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000200 Isolate* isolate() const { return masm_->isolate(); }
201
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000202 MacroAssembler* masm_;
ager@chromium.org32912102009-01-16 10:38:43 +0000203
ager@chromium.org32912102009-01-16 10:38:43 +0000204 // Which mode to generate code for (ASCII or UC16).
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000205 Mode mode_;
ager@chromium.org32912102009-01-16 10:38:43 +0000206
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000207 // One greater than maximal register index actually used.
208 int num_registers_;
ager@chromium.org32912102009-01-16 10:38:43 +0000209
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000210 // Number of registers to output at the end (the saved registers
211 // are always 0..num_saved_registers_-1)
212 int num_saved_registers_;
ager@chromium.org32912102009-01-16 10:38:43 +0000213
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000214 // Labels used internally.
215 Label entry_label_;
216 Label start_label_;
217 Label success_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000218 Label backtrack_label_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000219 Label exit_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000220 Label check_preempt_label_;
ager@chromium.org32912102009-01-16 10:38:43 +0000221 Label stack_overflow_label_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000222};
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000223#endif // V8_INTERPRETED_REGEXP
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000224
225}} // namespace v8::internal
226
ager@chromium.org5ec48922009-05-05 07:25:34 +0000227#endif // V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_