blob: 8cbd274a00ee701dbea7b8096b9a569d35769bdd [file] [log] [blame]
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// 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
28#ifndef V8_REGEXP_MACRO_ASSEMBLER_H_
29#define V8_REGEXP_MACRO_ASSEMBLER_H_
30
31namespace v8 { namespace internal {
32
33
34struct DisjunctDecisionRow {
35 RegExpCharacterClass cc;
36 Label* on_match;
37};
38
39
40class RegExpMacroAssembler {
41 public:
42 enum IrregexpImplementation {
43 kIA32Implementation,
44 kARMImplementation,
45 kBytecodeImplementation};
46
47 RegExpMacroAssembler();
48 virtual ~RegExpMacroAssembler();
49 virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change.
50 virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by.
51 virtual void Backtrack() = 0;
52 virtual void Bind(Label* label) = 0;
53 // Check the current character against a bitmap. The range of the current
54 // character must be from start to start + length_of_bitmap_in_bits.
55 virtual void CheckBitmap(
56 uc16 start, // The bitmap is indexed from this character.
57 Label* bitmap, // Where the bitmap is emitted.
58 Label* on_zero) = 0; // Where to go if the bit is 0. Fall through on 1.
59 // Dispatch after looking the current character up in a 2-bits-per-entry
60 // map. The destinations vector has up to 4 labels.
61 virtual void CheckCharacter(uc16 c, Label* on_equal) = 0;
62 virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0;
63 virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0;
64 // Check the current character for a match with a literal string. If we
65 // fail to match then goto the on_failure label. End of input always
66 // matches. If the label is NULL then we should pop a backtrack address off
67 // the stack abnd go to that.
68 virtual void CheckCharacters(
69 Vector<const uc16> str,
70 int cp_offset,
71 Label* on_failure) = 0;
72 // Check the current input position against a register. If the register is
73 // equal to the current position then go to the label. If the label is NULL
74 // then backtrack instead.
75 virtual void CheckCurrentPosition(
76 int register_index,
77 Label* on_equal) = 0;
78 virtual void CheckNotAtStart(Label* on_not_at_start) = 0;
79 virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0;
80 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
81 Label* on_no_match) = 0;
82 // Check the current character for a match with a literal character. If we
83 // fail to match then goto the on_failure label. End of input always
84 // matches. If the label is NULL then we should pop a backtrack address off
85 // the stack and go to that.
86 virtual void CheckNotCharacter(uc16 c, Label* on_not_equal) = 0;
87 // Bitwise or the current character with the given constant and then
88 // check for a match with c.
89 virtual void CheckNotCharacterAfterOr(uc16 c,
90 uc16 or_with,
91 Label* on_not_equal) = 0;
92 // Subtract a constant from the current character, then or with the given
93 // constant and then check for a match with c.
94 virtual void CheckNotCharacterAfterMinusOr(uc16 c,
95 uc16 minus_then_or_with,
96 Label* on_not_equal) = 0;
97 virtual void CheckNotRegistersEqual(int reg1,
98 int reg2,
99 Label* on_not_equal) = 0;
100 // Dispatch after looking the current character up in a byte map. The
101 // destinations vector has up to 256 labels.
102 virtual void DispatchByteMap(
103 uc16 start,
104 Label* byte_map,
105 const Vector<Label*>& destinations) = 0;
106 virtual void DispatchHalfNibbleMap(
107 uc16 start,
108 Label* half_nibble_map,
109 const Vector<Label*>& destinations) = 0;
110 // Dispatch after looking the high byte of the current character up in a byte
111 // map. The destinations vector has up to 256 labels.
112 virtual void DispatchHighByteMap(
113 byte start,
114 Label* byte_map,
115 const Vector<Label*>& destinations) = 0;
116 virtual void EmitOrLink(Label* label) = 0;
117 virtual void Fail() = 0;
118 virtual Handle<Object> GetCode() = 0;
119 virtual void GoTo(Label* label) = 0;
120 // Check whether a register is >= a given constant and go to a label if it
121 // is. Backtracks instead if the label is NULL.
122 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0;
123 // Check whether a register is < a given constant and go to a label if it is.
124 // Backtracks instead if the label is NULL.
125 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0;
126 virtual IrregexpImplementation Implementation() = 0;
127 virtual void LoadCurrentCharacter(int cp_offset, Label* on_end_of_input) = 0;
128 virtual void PopCurrentPosition() = 0;
129 virtual void PopRegister(int register_index) = 0;
130 virtual void PushBacktrack(Label* label) = 0;
131 virtual void PushCurrentPosition() = 0;
132 virtual void PushRegister(int register_index) = 0;
133 virtual void ReadCurrentPositionFromRegister(int reg) = 0;
134 virtual void ReadStackPointerFromRegister(int reg) = 0;
135 virtual void SetRegister(int register_index, int to) = 0;
136 virtual void Succeed() = 0;
137 virtual void WriteCurrentPositionToRegister(int reg) = 0;
138 virtual void WriteStackPointerToRegister(int reg) = 0;
139
140 private:
141};
142
143
144struct ArraySlice {
145 public:
146 ArraySlice(Handle<ByteArray> array, size_t offset)
147 : array_(array), offset_(offset) {}
148 Handle<ByteArray> array() { return array_; }
149 // Offset in the byte array data.
150 size_t offset() { return offset_; }
151 // Offset from the ByteArray pointer.
152 size_t base_offset() {
153 return ByteArray::kHeaderSize - kHeapObjectTag + offset_;
154 }
155 void* location() {
156 return reinterpret_cast<void*>(array_->GetDataStartAddress() + offset_);
157 }
158 template <typename T>
159 T& at(int idx) {
160 return reinterpret_cast<T*>(array_->GetDataStartAddress() + offset_)[idx];
161 }
162 private:
163 Handle<ByteArray> array_;
164 size_t offset_;
165};
166
167
168class ByteArrayProvider {
169 public:
170 explicit ByteArrayProvider(unsigned int initial_size);
171 // Provides a place to put "size" elements of size "element_size".
172 // The information can be stored in the provided ByteArray at the "offset".
173 // The offset is aligned to the element size.
174 ArraySlice GetBuffer(unsigned int size,
175 unsigned int element_size);
176 template <typename T>
177 ArraySlice GetBuffer(Vector<T> values);
178 private:
179 size_t byte_array_size_;
180 Handle<ByteArray> current_byte_array_;
181 int current_byte_array_free_offset_;
182};
183
184} } // namespace v8::internal
185
186#endif // V8_REGEXP_MACRO_ASSEMBLER_H_