OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_REGEXP_ASSEMBLER_H_ | 5 #ifndef VM_REGEXP_ASSEMBLER_H_ |
6 #define VM_REGEXP_ASSEMBLER_H_ | 6 #define VM_REGEXP_ASSEMBLER_H_ |
7 | 7 |
8 #include "vm/assembler.h" | 8 // SNIP |
9 #include "vm/intermediate_language.h" | |
10 #include "vm/object.h" | |
11 | 9 |
12 namespace dart { | 10 namespace dart { |
13 | 11 |
14 // Utility function for the DotPrinter | 12 // SNIP |
15 void PrintUtf16(uint16_t c); | |
16 | 13 |
17 | 14 class RegExpMacroAssembler { |
18 /// Convenience wrapper around a BlockEntryInstr pointer. | |
19 class BlockLabel : public ValueObject { | |
20 public: | |
21 BlockLabel() | |
22 : block_(new JoinEntryInstr(-1, -1)), | |
23 is_bound_(false), | |
24 is_linked_(false) { } | |
25 | |
26 BlockLabel(const BlockLabel& that) | |
27 : ValueObject(), | |
28 block_(that.block_), | |
29 is_bound_(that.is_bound_), | |
30 is_linked_(that.is_linked_) { } | |
31 | |
32 BlockLabel& operator=(const BlockLabel& that) { | |
33 block_ = that.block_; | |
34 is_bound_ = that.is_bound_; | |
35 is_linked_ = that.is_linked_; | |
36 return *this; | |
37 } | |
38 | |
39 JoinEntryInstr* block() const { return block_; } | |
40 | |
41 bool IsBound() const { return is_bound_; } | |
42 void SetBound(intptr_t block_id) { | |
43 ASSERT(!is_bound_); | |
44 block_->set_block_id(block_id); | |
45 is_bound_ = true; | |
46 } | |
47 | |
48 bool IsLinked() const { return !is_bound_ && is_linked_; } | |
49 void SetLinked() { | |
50 is_linked_ = true; | |
51 } | |
52 | |
53 intptr_t Position() const { | |
54 ASSERT(IsBound()); | |
55 return block_->block_id(); | |
56 } | |
57 | |
58 private: | |
59 JoinEntryInstr* block_; | |
60 | |
61 bool is_bound_; | |
62 bool is_linked_; | |
63 }; | |
64 | |
65 | |
66 class RegExpMacroAssembler : public ZoneAllocated { | |
67 public: | 15 public: |
68 // The implementation must be able to handle at least: | 16 // The implementation must be able to handle at least: |
69 static const intptr_t kMaxRegister = (1 << 16) - 1; | 17 static const int kMaxRegister = (1 << 16) - 1; |
70 static const intptr_t kMaxCPOffset = (1 << 15) - 1; | 18 static const int kMaxCPOffset = (1 << 15) - 1; |
71 static const intptr_t kMinCPOffset = -(1 << 15); | 19 static const int kMinCPOffset = -(1 << 15); |
72 | 20 |
73 static const intptr_t kTableSizeBits = 7; | 21 static const int kTableSizeBits = 7; |
74 static const intptr_t kTableSize = 1 << kTableSizeBits; | 22 static const int kTableSize = 1 << kTableSizeBits; |
75 static const intptr_t kTableMask = kTableSize - 1; | 23 static const int kTableMask = kTableSize - 1; |
76 | 24 |
77 enum { | 25 enum IrregexpImplementation { |
78 kParamStringIndex = 0, | 26 kIA32Implementation, |
79 kParamStartOffsetIndex, | 27 kARMImplementation, |
80 kParamCount | 28 kARM64Implementation, |
| 29 kMIPSImplementation, |
| 30 kX64Implementation, |
| 31 kX87Implementation, |
| 32 kBytecodeImplementation |
81 }; | 33 }; |
82 | 34 |
83 enum IrregexpImplementation { | 35 enum StackCheckFlag { |
84 kIRImplementation | 36 kNoStackLimitCheck = false, |
| 37 kCheckStackLimit = true |
85 }; | 38 }; |
86 | 39 |
87 explicit RegExpMacroAssembler(Isolate* isolate); | 40 explicit RegExpMacroAssembler(Zone* zone); |
88 virtual ~RegExpMacroAssembler(); | 41 virtual ~RegExpMacroAssembler(); |
89 // The maximal number of pushes between stack checks. Users must supply | 42 // The maximal number of pushes between stack checks. Users must supply |
90 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck) | 43 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck) |
91 // at least once for every stack_limit() pushes that are executed. | 44 // at least once for every stack_limit() pushes that are executed. |
92 virtual intptr_t stack_limit_slack() = 0; | 45 virtual int stack_limit_slack() = 0; |
93 virtual bool CanReadUnaligned() = 0; | 46 virtual bool CanReadUnaligned() = 0; |
94 virtual void AdvanceCurrentPosition(intptr_t by) = 0; // Signed cp change. | 47 virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change. |
95 virtual void AdvanceRegister(intptr_t reg, intptr_t by) = 0; // r[reg] += by. | 48 virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by. |
96 // Continues execution from the position pushed on the top of the backtrack | 49 // Continues execution from the position pushed on the top of the backtrack |
97 // stack by an earlier PushBacktrack(BlockLabel*). | 50 // stack by an earlier PushBacktrack(Label*). |
98 virtual void Backtrack() = 0; | 51 virtual void Backtrack() = 0; |
99 virtual void BindBlock(BlockLabel* label) = 0; | 52 virtual void Bind(Label* label) = 0; |
100 virtual void CheckAtStart(BlockLabel* on_at_start) = 0; | 53 virtual void CheckAtStart(Label* on_at_start) = 0; |
101 // Dispatch after looking the current character up in a 2-bits-per-entry | 54 // Dispatch after looking the current character up in a 2-bits-per-entry |
102 // map. The destinations vector has up to 4 labels. | 55 // map. The destinations vector has up to 4 labels. |
103 virtual void CheckCharacter(unsigned c, BlockLabel* on_equal) = 0; | 56 virtual void CheckCharacter(unsigned c, Label* on_equal) = 0; |
104 // Bitwise and the current character with the given constant and then | 57 // Bitwise and the current character with the given constant and then |
105 // check for a match with c. | 58 // check for a match with c. |
106 virtual void CheckCharacterAfterAnd(unsigned c, | 59 virtual void CheckCharacterAfterAnd(unsigned c, |
107 unsigned and_with, | 60 unsigned and_with, |
108 BlockLabel* on_equal) = 0; | 61 Label* on_equal) = 0; |
109 virtual void CheckCharacterGT(uint16_t limit, BlockLabel* on_greater) = 0; | 62 virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0; |
110 virtual void CheckCharacterLT(uint16_t limit, BlockLabel* on_less) = 0; | 63 virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0; |
111 virtual void CheckGreedyLoop(BlockLabel* on_tos_equals_current_position) = 0; | 64 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0; |
112 virtual void CheckNotAtStart(BlockLabel* on_not_at_start) = 0; | 65 virtual void CheckNotAtStart(Label* on_not_at_start) = 0; |
113 virtual void CheckNotBackReference( | 66 virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0; |
114 intptr_t start_reg, BlockLabel* on_no_match) = 0; | 67 virtual void CheckNotBackReferenceIgnoreCase(int start_reg, |
115 virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg, | 68 Label* on_no_match) = 0; |
116 BlockLabel* on_no_match) = 0; | |
117 // Check the current character for a match with a literal character. If we | 69 // Check the current character for a match with a literal character. If we |
118 // fail to match then goto the on_failure label. End of input always | 70 // fail to match then goto the on_failure label. End of input always |
119 // matches. If the label is NULL then we should pop a backtrack address off | 71 // matches. If the label is NULL then we should pop a backtrack address off |
120 // the stack and go to that. | 72 // the stack and go to that. |
121 virtual void CheckNotCharacter(unsigned c, BlockLabel* on_not_equal) = 0; | 73 virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0; |
122 virtual void CheckNotCharacterAfterAnd(unsigned c, | 74 virtual void CheckNotCharacterAfterAnd(unsigned c, |
123 unsigned and_with, | 75 unsigned and_with, |
124 BlockLabel* on_not_equal) = 0; | 76 Label* on_not_equal) = 0; |
125 // Subtract a constant from the current character, then and with the given | 77 // Subtract a constant from the current character, then and with the given |
126 // constant and then check for a match with c. | 78 // constant and then check for a match with c. |
127 virtual void CheckNotCharacterAfterMinusAnd(uint16_t c, | 79 virtual void CheckNotCharacterAfterMinusAnd(uc16 c, |
128 uint16_t minus, | 80 uc16 minus, |
129 uint16_t and_with, | 81 uc16 and_with, |
130 BlockLabel* on_not_equal) = 0; | 82 Label* on_not_equal) = 0; |
131 virtual void CheckCharacterInRange(uint16_t from, | 83 virtual void CheckCharacterInRange(uc16 from, |
132 uint16_t to, // Both inclusive. | 84 uc16 to, // Both inclusive. |
133 BlockLabel* on_in_range) = 0; | 85 Label* on_in_range) = 0; |
134 virtual void CheckCharacterNotInRange(uint16_t from, | 86 virtual void CheckCharacterNotInRange(uc16 from, |
135 uint16_t to, // Both inclusive. | 87 uc16 to, // Both inclusive. |
136 BlockLabel* on_not_in_range) = 0; | 88 Label* on_not_in_range) = 0; |
137 | 89 |
138 // The current character (modulus the kTableSize) is looked up in the byte | 90 // The current character (modulus the kTableSize) is looked up in the byte |
139 // array, and if the found byte is non-zero, we jump to the on_bit_set label. | 91 // array, and if the found byte is non-zero, we jump to the on_bit_set label. |
140 virtual void CheckBitInTable(const TypedData& table, | 92 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) = 0; |
141 BlockLabel* on_bit_set) = 0; | |
142 | 93 |
143 // Checks whether the given offset from the current position is before | 94 // Checks whether the given offset from the current position is before |
144 // the end of the string. May overwrite the current character. | 95 // the end of the string. May overwrite the current character. |
145 virtual void CheckPosition(intptr_t cp_offset, BlockLabel* on_outside_input) { | 96 virtual void CheckPosition(int cp_offset, Label* on_outside_input) { |
146 LoadCurrentCharacter(cp_offset, on_outside_input, true); | 97 LoadCurrentCharacter(cp_offset, on_outside_input, true); |
147 } | 98 } |
148 // Check whether a standard/default character class matches the current | 99 // Check whether a standard/default character class matches the current |
149 // character. Returns false if the type of special character class does | 100 // character. Returns false if the type of special character class does |
150 // not have custom support. | 101 // not have custom support. |
151 // May clobber the current loaded character. | 102 // May clobber the current loaded character. |
152 virtual bool CheckSpecialCharacterClass(uint16_t type, | 103 virtual bool CheckSpecialCharacterClass(uc16 type, |
153 BlockLabel* on_no_match) { | 104 Label* on_no_match) { |
154 return false; | 105 return false; |
155 } | 106 } |
156 virtual void Fail() = 0; | 107 virtual void Fail() = 0; |
| 108 virtual Handle<HeapObject> GetCode(Handle<String> source) = 0; |
| 109 virtual void GoTo(Label* label) = 0; |
157 // Check whether a register is >= a given constant and go to a label if it | 110 // Check whether a register is >= a given constant and go to a label if it |
158 // is. Backtracks instead if the label is NULL. | 111 // is. Backtracks instead if the label is NULL. |
159 virtual void IfRegisterGE( | 112 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0; |
160 intptr_t reg, intptr_t comparand, BlockLabel* if_ge) = 0; | |
161 // Check whether a register is < a given constant and go to a label if it is. | 113 // Check whether a register is < a given constant and go to a label if it is. |
162 // Backtracks instead if the label is NULL. | 114 // Backtracks instead if the label is NULL. |
163 virtual void IfRegisterLT( | 115 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0; |
164 intptr_t reg, intptr_t comparand, BlockLabel* if_lt) = 0; | |
165 // Check whether a register is == to the current position and go to a | 116 // Check whether a register is == to the current position and go to a |
166 // label if it is. | 117 // label if it is. |
167 virtual void IfRegisterEqPos(intptr_t reg, BlockLabel* if_eq) = 0; | 118 virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0; |
168 virtual IrregexpImplementation Implementation() = 0; | 119 virtual IrregexpImplementation Implementation() = 0; |
169 // The assembler is closed, iff there is no current instruction assigned. | 120 virtual void LoadCurrentCharacter(int cp_offset, |
170 virtual bool IsClosed() const = 0; | 121 Label* on_end_of_input, |
171 // Jump to the target label without setting it as the current instruction. | |
172 virtual void GoTo(BlockLabel* to) = 0; | |
173 virtual void LoadCurrentCharacter(intptr_t cp_offset, | |
174 BlockLabel* on_end_of_input, | |
175 bool check_bounds = true, | 122 bool check_bounds = true, |
176 intptr_t characters = 1) = 0; | 123 int characters = 1) = 0; |
177 virtual void PopCurrentPosition() = 0; | 124 virtual void PopCurrentPosition() = 0; |
178 virtual void PopRegister(intptr_t register_index) = 0; | 125 virtual void PopRegister(int register_index) = 0; |
179 // Prints string within the generated code. Used for debugging. | |
180 virtual void Print(const char* str) = 0; | |
181 // Prints all emitted blocks. | |
182 virtual void PrintBlocks() = 0; | |
183 // Pushes the label on the backtrack stack, so that a following Backtrack | 126 // Pushes the label on the backtrack stack, so that a following Backtrack |
184 // will go to this label. Always checks the backtrack stack limit. | 127 // will go to this label. Always checks the backtrack stack limit. |
185 virtual void PushBacktrack(BlockLabel* label) = 0; | 128 virtual void PushBacktrack(Label* label) = 0; |
186 virtual void PushCurrentPosition() = 0; | 129 virtual void PushCurrentPosition() = 0; |
187 virtual void PushRegister(intptr_t register_index) = 0; | 130 virtual void PushRegister(int register_index, |
188 virtual void ReadCurrentPositionFromRegister(intptr_t reg) = 0; | 131 StackCheckFlag check_stack_limit) = 0; |
189 virtual void ReadStackPointerFromRegister(intptr_t reg) = 0; | 132 virtual void ReadCurrentPositionFromRegister(int reg) = 0; |
190 virtual void SetCurrentPositionFromEnd(intptr_t by) = 0; | 133 virtual void ReadStackPointerFromRegister(int reg) = 0; |
191 virtual void SetRegister(intptr_t register_index, intptr_t to) = 0; | 134 virtual void SetCurrentPositionFromEnd(int by) = 0; |
| 135 virtual void SetRegister(int register_index, int to) = 0; |
192 // Return whether the matching (with a global regexp) will be restarted. | 136 // Return whether the matching (with a global regexp) will be restarted. |
193 virtual bool Succeed() = 0; | 137 virtual bool Succeed() = 0; |
194 virtual void WriteCurrentPositionToRegister( | 138 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0; |
195 intptr_t reg, intptr_t cp_offset) = 0; | 139 virtual void ClearRegisters(int reg_from, int reg_to) = 0; |
196 virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to) = 0; | 140 virtual void WriteStackPointerToRegister(int reg) = 0; |
197 virtual void WriteStackPointerToRegister(intptr_t reg) = 0; | |
198 | 141 |
199 // Controls the generation of large inlined constants in the code. | 142 // Controls the generation of large inlined constants in the code. |
200 void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; } | 143 void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; } |
201 bool slow_safe() { return slow_safe_compiler_; } | 144 bool slow_safe() { return slow_safe_compiler_; } |
202 | 145 |
203 enum GlobalMode { NOT_GLOBAL, GLOBAL, GLOBAL_NO_ZERO_LENGTH_CHECK }; | 146 enum GlobalMode { NOT_GLOBAL, GLOBAL, GLOBAL_NO_ZERO_LENGTH_CHECK }; |
204 // Set whether the regular expression has the global flag. Exiting due to | 147 // Set whether the regular expression has the global flag. Exiting due to |
205 // a failure in a global regexp may still mean success overall. | 148 // a failure in a global regexp may still mean success overall. |
206 inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; } | 149 inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; } |
207 inline bool global() { return global_mode_ != NOT_GLOBAL; } | 150 inline bool global() { return global_mode_ != NOT_GLOBAL; } |
208 inline bool global_with_zero_length_check() { | 151 inline bool global_with_zero_length_check() { |
209 return global_mode_ == GLOBAL; | 152 return global_mode_ == GLOBAL; |
210 } | 153 } |
211 | 154 |
212 Isolate* isolate() const { return isolate_; } | 155 Zone* zone() const { return zone_; } |
213 | 156 |
214 private: | 157 private: |
215 bool slow_safe_compiler_; | 158 bool slow_safe_compiler_; |
216 bool global_mode_; | 159 bool global_mode_; |
217 Isolate* isolate_; | 160 Zone* zone_; |
218 }; | 161 }; |
219 | 162 |
220 | 163 // SNIP |
221 class IRRegExpMacroAssembler : public RegExpMacroAssembler { | |
222 public: | |
223 // Type of input string to generate code for. | |
224 enum Mode { ASCII = 1, UC16 = 2 }; | |
225 | |
226 // Result of calling generated native RegExp code. | |
227 // RETRY: Something significant changed during execution, and the matching | |
228 // should be retried from scratch. | |
229 // EXCEPTION: Something failed during execution. If no exception has been | |
230 // thrown, it's an internal out-of-memory, and the caller should | |
231 // throw the exception. | |
232 // FAILURE: Matching failed. | |
233 // SUCCESS: Matching succeeded, and the output array has been filled with | |
234 // capture positions. | |
235 enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 }; | |
236 | |
237 IRRegExpMacroAssembler(intptr_t specialization_cid, | |
238 intptr_t capture_count, | |
239 const ParsedFunction* parsed_function, | |
240 const ZoneGrowableArray<const ICData*>& ic_data_array, | |
241 Isolate* isolate); | |
242 virtual ~IRRegExpMacroAssembler(); | |
243 | |
244 virtual bool CanReadUnaligned(); | |
245 | |
246 // Compares two-byte strings case insensitively. | |
247 // Called from generated RegExp code. | |
248 static RawBool* CaseInsensitiveCompareUC16( | |
249 RawString* str_raw, | |
250 RawSmi* lhs_index_raw, | |
251 RawSmi* rhs_index_raw, | |
252 RawSmi* length_raw); | |
253 | |
254 static RawArray* Execute(const Function& function, | |
255 const String& input, | |
256 const Smi& start_offset, | |
257 Isolate* isolate); | |
258 | |
259 virtual bool IsClosed() const { return (current_instruction_ == NULL); } | |
260 | |
261 virtual intptr_t stack_limit_slack(); | |
262 virtual void AdvanceCurrentPosition(intptr_t by); | |
263 virtual void AdvanceRegister(intptr_t reg, intptr_t by); | |
264 virtual void Backtrack(); | |
265 virtual void BindBlock(BlockLabel* label); | |
266 virtual void CheckAtStart(BlockLabel* on_at_start); | |
267 virtual void CheckCharacter(uint32_t c, BlockLabel* on_equal); | |
268 virtual void CheckCharacterAfterAnd(uint32_t c, | |
269 uint32_t mask, | |
270 BlockLabel* on_equal); | |
271 virtual void CheckCharacterGT(uint16_t limit, BlockLabel* on_greater); | |
272 virtual void CheckCharacterLT(uint16_t limit, BlockLabel* on_less); | |
273 // A "greedy loop" is a loop that is both greedy and with a simple | |
274 // body. It has a particularly simple implementation. | |
275 virtual void CheckGreedyLoop(BlockLabel* on_tos_equals_current_position); | |
276 virtual void CheckNotAtStart(BlockLabel* on_not_at_start); | |
277 virtual void CheckNotBackReference(intptr_t start_reg, | |
278 BlockLabel* on_no_match); | |
279 virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg, | |
280 BlockLabel* on_no_match); | |
281 virtual void CheckNotCharacter(uint32_t c, BlockLabel* on_not_equal); | |
282 virtual void CheckNotCharacterAfterAnd(uint32_t c, | |
283 uint32_t mask, | |
284 BlockLabel* on_not_equal); | |
285 virtual void CheckNotCharacterAfterMinusAnd(uint16_t c, | |
286 uint16_t minus, | |
287 uint16_t mask, | |
288 BlockLabel* on_not_equal); | |
289 virtual void CheckCharacterInRange(uint16_t from, | |
290 uint16_t to, | |
291 BlockLabel* on_in_range); | |
292 virtual void CheckCharacterNotInRange(uint16_t from, | |
293 uint16_t to, | |
294 BlockLabel* on_not_in_range); | |
295 virtual void CheckBitInTable(const TypedData& table, BlockLabel* on_bit_set); | |
296 | |
297 // Checks whether the given offset from the current position is before | |
298 // the end of the string. | |
299 virtual void CheckPosition(intptr_t cp_offset, BlockLabel* on_outside_input); | |
300 virtual bool CheckSpecialCharacterClass( | |
301 uint16_t type, BlockLabel* on_no_match); | |
302 virtual void Fail(); | |
303 virtual void IfRegisterGE(intptr_t reg, | |
304 intptr_t comparand, BlockLabel* if_ge); | |
305 virtual void IfRegisterLT(intptr_t reg, | |
306 intptr_t comparand, BlockLabel* if_lt); | |
307 virtual void IfRegisterEqPos(intptr_t reg, BlockLabel* if_eq); | |
308 virtual IrregexpImplementation Implementation(); | |
309 virtual void GoTo(BlockLabel* to); | |
310 virtual void LoadCurrentCharacter(intptr_t cp_offset, | |
311 BlockLabel* on_end_of_input, | |
312 bool check_bounds = true, | |
313 intptr_t characters = 1); | |
314 virtual void PopCurrentPosition(); | |
315 virtual void PopRegister(intptr_t register_index); | |
316 virtual void Print(const char* str); | |
317 virtual void PushBacktrack(BlockLabel* label); | |
318 virtual void PushCurrentPosition(); | |
319 virtual void PushRegister(intptr_t register_index); | |
320 virtual void ReadCurrentPositionFromRegister(intptr_t reg); | |
321 virtual void ReadStackPointerFromRegister(intptr_t reg); | |
322 virtual void SetCurrentPositionFromEnd(intptr_t by); | |
323 virtual void SetRegister(intptr_t register_index, intptr_t to); | |
324 virtual bool Succeed(); | |
325 virtual void WriteCurrentPositionToRegister(intptr_t reg, intptr_t cp_offset); | |
326 virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to); | |
327 virtual void WriteStackPointerToRegister(intptr_t reg); | |
328 | |
329 virtual void PrintBlocks(); | |
330 | |
331 IndirectGotoInstr* backtrack_goto() const { return backtrack_goto_; } | |
332 GraphEntryInstr* graph_entry() const { return entry_block_; } | |
333 | |
334 intptr_t num_stack_locals() const { return local_id_.Count(); } | |
335 intptr_t num_blocks() const { return block_id_.Count(); } | |
336 | |
337 // Generate a dispatch block implementing backtracking. Must be done after | |
338 // graph construction. | |
339 void GenerateBacktrackBlock(); | |
340 | |
341 private: | |
342 // Generate the contents of preset blocks. The entry block is the entry point | |
343 // of the generated code. | |
344 void GenerateEntryBlock(); | |
345 // Copies capture indices into the result area and returns true. | |
346 void GenerateSuccessBlock(); | |
347 // Returns false. | |
348 void GenerateExitBlock(); | |
349 | |
350 enum ComparisonKind { | |
351 kEQ, | |
352 kNE, | |
353 kLT, | |
354 kGT, | |
355 kLTE, | |
356 kGTE, | |
357 }; | |
358 | |
359 struct InstanceCallDescriptor { | |
360 // Standard (i.e. most non-Smi) functions. | |
361 explicit InstanceCallDescriptor(const String& name) | |
362 : name(name), | |
363 token_kind(Token::kILLEGAL), | |
364 checked_argument_count(1) { } | |
365 | |
366 InstanceCallDescriptor(const String& name, | |
367 Token::Kind token_kind, | |
368 intptr_t checked_argument_count) | |
369 : name(name), | |
370 token_kind(token_kind), | |
371 checked_argument_count(checked_argument_count) { } | |
372 | |
373 // Special cases for Smi and indexing functions. | |
374 static InstanceCallDescriptor FromToken(Token::Kind token_kind) { | |
375 switch (token_kind) { | |
376 case Token::kEQ: return InstanceCallDescriptor( | |
377 Symbols::EqualOperator(), token_kind, 2); | |
378 case Token::kADD: return InstanceCallDescriptor( | |
379 Symbols::Plus(), token_kind, 2); | |
380 case Token::kSUB: return InstanceCallDescriptor( | |
381 Symbols::Minus(), token_kind, 2); | |
382 case Token::kBIT_OR: return InstanceCallDescriptor( | |
383 Symbols::BitOr(), token_kind, 2); | |
384 case Token::kBIT_AND: return InstanceCallDescriptor( | |
385 Symbols::BitAnd(), token_kind, 2); | |
386 case Token::kLT: return InstanceCallDescriptor( | |
387 Symbols::LAngleBracket(), token_kind, 2); | |
388 case Token::kLTE: return InstanceCallDescriptor( | |
389 Symbols::LessEqualOperator(), token_kind, 2); | |
390 case Token::kGT: return InstanceCallDescriptor( | |
391 Symbols::RAngleBracket(), token_kind, 2); | |
392 case Token::kGTE: return InstanceCallDescriptor( | |
393 Symbols::GreaterEqualOperator(), token_kind, 2); | |
394 case Token::kNEGATE: return InstanceCallDescriptor( | |
395 Symbols::UnaryMinus(), token_kind, 1); | |
396 case Token::kINDEX: return InstanceCallDescriptor( | |
397 Symbols::IndexToken(), token_kind, 2); | |
398 case Token::kASSIGN_INDEX: return InstanceCallDescriptor( | |
399 Symbols::AssignIndexToken(), token_kind, 3); | |
400 default: | |
401 UNREACHABLE(); | |
402 } | |
403 UNREACHABLE(); | |
404 return InstanceCallDescriptor(Symbols::Empty()); | |
405 } | |
406 | |
407 const String& name; | |
408 Token::Kind token_kind; | |
409 intptr_t checked_argument_count; | |
410 }; | |
411 | |
412 LocalVariable* Local(const String& name); | |
413 LocalVariable* Parameter(const String& name, intptr_t index) const; | |
414 | |
415 ConstantInstr* Int64Constant(int64_t value) const; | |
416 ConstantInstr* Uint64Constant(uint64_t value) const; | |
417 ConstantInstr* BoolConstant(bool value) const; | |
418 ConstantInstr* StringConstant(const char* value) const; | |
419 | |
420 // The word character map static member of the RegExp class. | |
421 // Byte map of one byte characters with a 0xff if the character is a word | |
422 // character (digit, letter or underscore) and 0x00 otherwise. | |
423 // Used by generated RegExp code. | |
424 ConstantInstr* WordCharacterMapConstant() const; | |
425 | |
426 ComparisonInstr* Comparison(ComparisonKind kind, | |
427 Definition* lhs, Definition* rhs); | |
428 | |
429 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, | |
430 PushArgumentInstr* arg1) const; | |
431 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, | |
432 PushArgumentInstr* arg1, | |
433 PushArgumentInstr* arg2) const; | |
434 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, | |
435 PushArgumentInstr* arg1, | |
436 PushArgumentInstr* arg2, | |
437 PushArgumentInstr* arg3) const; | |
438 InstanceCallInstr* InstanceCall( | |
439 const InstanceCallDescriptor& desc, | |
440 ZoneGrowableArray<PushArgumentInstr*>* arguments) const; | |
441 | |
442 StaticCallInstr* StaticCall(const Function& function) const; | |
443 StaticCallInstr* StaticCall(const Function& function, | |
444 PushArgumentInstr* arg1) const; | |
445 StaticCallInstr* StaticCall(const Function& function, | |
446 PushArgumentInstr* arg1, | |
447 PushArgumentInstr* arg2) const; | |
448 StaticCallInstr* StaticCall( | |
449 const Function& function, | |
450 ZoneGrowableArray<PushArgumentInstr*>* arguments) const; | |
451 | |
452 // Creates a new block consisting simply of a goto to dst. | |
453 TargetEntryInstr* TargetWithJoinGoto(JoinEntryInstr* dst); | |
454 IndirectEntryInstr* IndirectWithJoinGoto(JoinEntryInstr* dst); | |
455 | |
456 // Adds, respectively subtracts lhs and rhs and returns the result. | |
457 Definition* Add(PushArgumentInstr* lhs, PushArgumentInstr* rhs); | |
458 Definition* Sub(PushArgumentInstr* lhs, PushArgumentInstr* rhs); | |
459 | |
460 LoadLocalInstr* LoadLocal(LocalVariable* local) const; | |
461 void StoreLocal(LocalVariable* local, Value* value); | |
462 | |
463 PushArgumentInstr* PushArgument(Value* value); | |
464 PushArgumentInstr* PushLocal(LocalVariable* local); | |
465 | |
466 // Load a number of characters at the given offset from the | |
467 // current position, into the current-character register. | |
468 void LoadCurrentCharacterUnchecked(intptr_t cp_offset, | |
469 intptr_t character_count); | |
470 | |
471 // Returns the character within the passed string at the specified index. | |
472 Value* CharacterAt(Definition* index); | |
473 | |
474 // Load a number of characters starting from index in the pattern string. | |
475 Value* LoadCodeUnitsAt(Value* pattern, | |
476 Value* index, | |
477 intptr_t character_count); | |
478 | |
479 // Check whether preemption has been requested. | |
480 void CheckPreemption(); | |
481 | |
482 // Byte size of chars in the string to match (decided by the Mode argument) | |
483 inline intptr_t char_size() { return static_cast<int>(mode_); } | |
484 | |
485 // Equivalent to a conditional branch to the label, unless the label | |
486 // is NULL, in which case it is a conditional Backtrack. | |
487 void BranchOrBacktrack(ComparisonInstr* comparison, | |
488 BlockLabel* true_successor); | |
489 | |
490 // Set up all local variables and parameters. | |
491 void InitializeLocals(); | |
492 | |
493 // Allocates a new local, and returns the appropriate id for placing it | |
494 // on the stack. | |
495 intptr_t GetNextLocalIndex(); | |
496 | |
497 // We never have any copied parameters. | |
498 intptr_t num_copied_params() const { | |
499 return 0; | |
500 } | |
501 | |
502 // Return the position register at the specified index, creating it if | |
503 // necessary. Note that the number of such registers can exceed the amount | |
504 // required by the number of output captures. | |
505 LocalVariable* position_register(intptr_t index); | |
506 | |
507 void set_current_instruction(Instruction* instruction); | |
508 | |
509 // The following functions are responsible for appending instructions | |
510 // to the current instruction in various ways. The most simple one | |
511 // is AppendInstruction, which simply appends an instruction and performs | |
512 // bookkeeping. | |
513 void AppendInstruction(Instruction* instruction); | |
514 // Similar to AppendInstruction, but closes the current block by | |
515 // setting current_instruction_ to NULL. | |
516 void CloseBlockWith(Instruction* instruction); | |
517 // Appends definition and allocates a temp index for the result. | |
518 Value* Bind(Definition* definition); | |
519 // Loads and binds a local variable. | |
520 Value* BindLoadLocal(const LocalVariable& local); | |
521 | |
522 // Appends the definition. | |
523 void Do(Definition* definition); | |
524 // Closes the current block with a jump to the specified block. | |
525 void GoTo(JoinEntryInstr* to); | |
526 | |
527 // Accessors for our local stack_. | |
528 void PushStack(Definition* definition); | |
529 Value* PopStack(); | |
530 | |
531 // Prints the specified argument. Used for debugging. | |
532 void Print(PushArgumentInstr* argument); | |
533 | |
534 // A utility class tracking ids of various objects such as blocks, temps, etc. | |
535 class IdAllocator : public ValueObject { | |
536 public: | |
537 IdAllocator() : next_id(0) { } | |
538 | |
539 intptr_t Count() const { return next_id; } | |
540 intptr_t Alloc(intptr_t count = 1) { | |
541 ASSERT(count >= 0); | |
542 intptr_t current_id = next_id; | |
543 next_id += count; | |
544 return current_id; | |
545 } | |
546 void Dealloc(intptr_t count = 1) { | |
547 ASSERT(count <= next_id); | |
548 next_id -= count; | |
549 } | |
550 | |
551 private: | |
552 intptr_t next_id; | |
553 }; | |
554 | |
555 // Which mode to generate code for (ASCII or UC16). | |
556 Mode mode_; | |
557 | |
558 // Which specific string class to generate code for. | |
559 intptr_t specialization_cid_; | |
560 | |
561 // Block entries used internally. | |
562 GraphEntryInstr* entry_block_; | |
563 JoinEntryInstr* start_block_; | |
564 JoinEntryInstr* success_block_; | |
565 JoinEntryInstr* exit_block_; | |
566 | |
567 // Shared backtracking block. | |
568 JoinEntryInstr* backtrack_block_; | |
569 // Single indirect goto instruction which performs all backtracking. | |
570 IndirectGotoInstr* backtrack_goto_; | |
571 | |
572 const ParsedFunction* parsed_function_; | |
573 const ZoneGrowableArray<const ICData*>& ic_data_array_; | |
574 | |
575 // All created blocks are contained within this set. Used for printing | |
576 // the generated code. | |
577 GrowableArray<BlockEntryInstr*> blocks_; | |
578 | |
579 // The current instruction to link to when new code is emitted. | |
580 Instruction* current_instruction_; | |
581 | |
582 // A list, acting as the runtime stack for both backtrack locations and | |
583 // stored positions within the string. | |
584 LocalVariable* stack_; | |
585 | |
586 // Stores the current character within the string. | |
587 LocalVariable* current_character_; | |
588 | |
589 // Stores the current location within the string as a negative offset | |
590 // from the end of the string. | |
591 LocalVariable* current_position_; | |
592 | |
593 // The string being processed, passed as a function parameter. | |
594 LocalVariable* string_param_; | |
595 | |
596 // Stores the length of string_param_. | |
597 LocalVariable* string_param_length_; | |
598 | |
599 // The start index within the string, passed as a function parameter. | |
600 LocalVariable* start_index_param_; | |
601 | |
602 // An assortment of utility variables. | |
603 LocalVariable* capture_length_; | |
604 LocalVariable* match_start_index_; | |
605 LocalVariable* capture_start_index_; | |
606 LocalVariable* match_end_index_; | |
607 LocalVariable* char_in_capture_; | |
608 LocalVariable* char_in_match_; | |
609 | |
610 LocalVariable* result_; | |
611 | |
612 // Stored positions containing group bounds. Generated as needed. | |
613 const intptr_t position_registers_count_; | |
614 GrowableArray<LocalVariable*> position_registers_; | |
615 | |
616 // The actual array object used as the stack. | |
617 GrowableObjectArray& stack_array_; | |
618 | |
619 IdAllocator block_id_; | |
620 IdAllocator temp_id_; | |
621 IdAllocator arg_id_; | |
622 IdAllocator local_id_; | |
623 IdAllocator indirect_id_; | |
624 }; | |
625 | |
626 | 164 |
627 } // namespace dart | 165 } // namespace dart |
628 | 166 |
629 #endif // VM_REGEXP_ASSEMBLER_H_ | 167 #endif // VM_REGEXP_ASSEMBLER_H_ |
OLD | NEW |