Flutter Engine
The Flutter Engine
regexp_assembler_ir.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#if !defined(DART_PRECOMPILED_RUNTIME)
6
8
9#include <utility>
10
11#include "platform/unicode.h"
12#include "vm/bit_vector.h"
17#include "vm/dart_entry.h"
18#include "vm/longjump.h"
19#include "vm/object_store.h"
20#include "vm/regexp.h"
21#include "vm/resolver.h"
22#include "vm/runtime_entry.h"
23#include "vm/stack_frame.h"
24
25#define Z zone()
26
27// Debugging output macros. TAG() is called at the head of each interesting
28// function and prints its name during execution if irregexp tracing is enabled.
29#define TAG() \
30 if (FLAG_trace_irregexp) { \
31 TAG_(); \
32 }
33#define TAG_() \
34 Print(Bind(new (Z) ConstantInstr(String::ZoneHandle( \
35 Z, Symbols::FromConcat(thread_, String::Handle(String::New("TAG: ")), \
36 String::Handle(String::New(__FUNCTION__)))))));
37
38#define PRINT(arg) \
39 if (FLAG_trace_irregexp) { \
40 Print(arg); \
41 }
42
43namespace dart {
44
45/*
46 * This assembler uses the following main local variables:
47 * - stack_: A pointer to a growable list which we use as an all-purpose stack
48 * storing backtracking offsets, positions & stored register values.
49 * - current_character_: Stores the currently loaded characters (possibly more
50 * than one).
51 * - current_position_: The current position within the string, stored as a
52 * negative offset from the end of the string (i.e. the
53 * position corresponding to str[0] is -str.length).
54 * Note that current_position_ is *not* byte-based, unlike
55 * original V8 code.
56 *
57 * Results are returned though an array of capture indices, stored at
58 * matches_param_. A null array specifies a failure to match. The match indices
59 * [start_inclusive, end_exclusive] for capture group i are stored at positions
60 * matches_param_[i * 2] and matches_param_[i * 2 + 1], respectively. Match
61 * indices of -1 denote non-matched groups. Note that we store these indices
62 * as a negative offset from the end of the string in registers_array_
63 * during processing, and convert them to standard indexes when copying them
64 * to matches_param_ on successful match.
65 */
67 intptr_t specialization_cid,
68 intptr_t capture_count,
69 const ParsedFunction* parsed_function,
70 const ZoneGrowableArray<const ICData*>& ic_data_array,
71 intptr_t osr_id,
72 Zone* zone)
74 thread_(Thread::Current()),
75 specialization_cid_(specialization_cid),
76 parsed_function_(parsed_function),
77 ic_data_array_(ic_data_array),
78 current_instruction_(nullptr),
79 stack_(nullptr),
80 stack_pointer_(nullptr),
81 current_character_(nullptr),
82 current_position_(nullptr),
83 string_param_(nullptr),
84 string_param_length_(nullptr),
85 start_index_param_(nullptr),
86 registers_count_(0),
87 saved_registers_count_((capture_count + 1) * 2),
88 // B0 is taken by GraphEntry thus block ids must start at 1.
89 block_id_(1) {
90 switch (specialization_cid) {
91 case kOneByteStringCid:
92 mode_ = ASCII;
93 break;
94 case kTwoByteStringCid:
95 mode_ = UC16;
96 break;
97 default:
99 }
100
101 InitializeLocals();
102
103 // Create and generate all preset blocks.
104 entry_block_ = new (zone) GraphEntryInstr(*parsed_function_, osr_id);
105
106 auto function_entry = new (zone) FunctionEntryInstr(
107 entry_block_, block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
108 entry_block_->set_normal_entry(function_entry);
109
110 start_block_ = new (zone)
111 JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
112 success_block_ = new (zone)
113 JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
114 backtrack_block_ = new (zone)
115 JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
116 exit_block_ = new (zone)
117 JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
118
119 GenerateEntryBlock();
120 GenerateSuccessBlock();
121 GenerateExitBlock();
122
123 blocks_.Add(entry_block_);
124 blocks_.Add(entry_block_->normal_entry());
125 blocks_.Add(start_block_);
126 blocks_.Add(success_block_);
127 blocks_.Add(backtrack_block_);
128 blocks_.Add(exit_block_);
129
130 // Begin emission at the start_block_.
131 set_current_instruction(start_block_);
132}
133
135
136void IRRegExpMacroAssembler::InitializeLocals() {
137 // All generated functions are expected to have a current-context variable.
138 // This variable is unused in irregexp functions.
139 parsed_function_->current_context_var()->set_index(
140 VariableIndex(GetNextLocalIndex()));
141
142 // Create local variables and parameters.
143 stack_ = Local(Symbols::stack());
144 stack_pointer_ = Local(Symbols::stack_pointer());
145 registers_ = Local(Symbols::position_registers());
146 current_character_ = Local(Symbols::current_character());
147 current_position_ = Local(Symbols::current_position());
148 string_param_length_ = Local(Symbols::string_param_length());
149 capture_length_ = Local(Symbols::capture_length());
150 match_start_index_ = Local(Symbols::match_start_index());
151 capture_start_index_ = Local(Symbols::capture_start_index());
152 match_end_index_ = Local(Symbols::match_end_index());
153 char_in_capture_ = Local(Symbols::char_in_capture());
154 char_in_match_ = Local(Symbols::char_in_match());
155 index_temp_ = Local(Symbols::index_temp());
156 result_ = Local(Symbols::c_result());
157
158 string_param_ = Parameter(Symbols::string_param(),
160 start_index_param_ = Parameter(Symbols::start_index_param(),
162}
163
164void IRRegExpMacroAssembler::GenerateEntryBlock() {
165 set_current_instruction(entry_block_->normal_entry());
166 TAG();
167
168 // Store string.length.
169 Value* string_push = PushLocal(string_param_);
170
171 StoreLocal(string_param_length_,
172 Bind(InstanceCall(InstanceCallDescriptor(String::ZoneHandle(
173 Field::GetterSymbol(Symbols::Length()))),
174 string_push)));
175
176 // Store (start_index - string.length) as the current position (since it's a
177 // negative offset from the end of the string).
178 Value* start_index_push = PushLocal(start_index_param_);
179 Value* length_push = PushLocal(string_param_length_);
180
181 StoreLocal(current_position_, Bind(Sub(start_index_push, length_push)));
182
183 {
184 const Library& lib = Library::Handle(Library::CoreLibrary());
185 const Class& regexp_class =
186 Class::Handle(lib.LookupClassAllowPrivate(Symbols::_RegExp()));
187 const Function& get_registers_function = Function::ZoneHandle(
188 Z, regexp_class.LookupFunctionAllowPrivate(Symbols::_getRegisters()));
189
190 // The "0" placeholder constant will be replaced with correct value
191 // determined at the end of regexp graph construction in Finalization.
192 num_registers_constant_instr =
193 new (Z) ConstantInstr(Integer::ZoneHandle(Z, Integer::NewCanonical(0)));
194 StoreLocal(registers_, Bind(StaticCall(get_registers_function,
195 Bind(num_registers_constant_instr),
196 ICData::kStatic)));
197
198 const Field& backtracking_stack_field =
199 Field::ZoneHandle(Z, regexp_class.LookupStaticFieldAllowPrivate(
200 Symbols::_backtrackingStack()));
201 StoreLocal(stack_, Bind(LoadStaticField(backtracking_stack_field,
202 /*calls_initializer=*/true)));
203 }
204 ClearRegisters(0, saved_registers_count_ - 1);
205
206 StoreLocal(stack_pointer_, Bind(Int64Constant(-1)));
207
208 // Jump to the start block.
209 current_instruction_->Goto(start_block_);
210}
211
213 set_current_instruction(backtrack_block_);
214 TAG();
215 CheckPreemption(/*is_backtrack=*/true);
216
217 const intptr_t entries_count = entry_block_->indirect_entries().length();
218
219 Value* block_id_push = Bind(PopStack());
220 backtrack_goto_ = new (Z) IndirectGotoInstr(entries_count, block_id_push);
221 CloseBlockWith(backtrack_goto_);
222
223 // Add an edge from the "indirect" goto to each of the targets.
224 for (intptr_t j = 0; j < entries_count; j++) {
225 backtrack_goto_->AddSuccessor(
226 TargetWithJoinGoto(entry_block_->indirect_entries().At(j)));
227 }
228}
229
230void IRRegExpMacroAssembler::GenerateSuccessBlock() {
231 set_current_instruction(success_block_);
232 TAG();
233
235 Z, IsolateGroup::Current()->object_store()->type_argument_int())));
236 Value* length = Bind(Uint64Constant(saved_registers_count_));
237 Value* array = Bind(new (Z) CreateArrayInstr(InstructionSource(), type,
238 length, GetNextDeoptId()));
239 StoreLocal(result_, array);
240
241 // Store captured offsets in the `matches` parameter.
242 for (intptr_t i = 0; i < saved_registers_count_; i++) {
243 Value* matches_push = PushLocal(result_);
244 Value* index_push = Bind(Uint64Constant(i));
245
246 // Convert negative offsets from the end of the string to string indices.
247 // TODO(zerny): use positive offsets from the get-go.
248 Value* offset_push = LoadRegister(i);
249 Value* len_push = PushLocal(string_param_length_);
250 Value* value_push = Bind(Add(offset_push, len_push));
251
252 Do(InstanceCall(InstanceCallDescriptor::FromToken(Token::kASSIGN_INDEX),
253 matches_push, index_push, value_push));
254 }
255
256 // Print the result if tracing.
257 PRINT(PushLocal(result_));
258
259 // Return true on success.
260 AppendInstruction(new (Z) DartReturnInstr(
261 InstructionSource(), Bind(LoadLocal(result_)), GetNextDeoptId()));
262}
263
264void IRRegExpMacroAssembler::GenerateExitBlock() {
265 set_current_instruction(exit_block_);
266 TAG();
267
268 // Return false on failure.
269 AppendInstruction(new (Z) DartReturnInstr(
270 InstructionSource(), Bind(LoadLocal(result_)), GetNextDeoptId()));
271}
272
274 ASSERT(registers_count_ >= saved_registers_count_);
275
276 ConstantInstr* new_constant = Int64Constant(registers_count_);
277 new_constant->set_temp_index(num_registers_constant_instr->temp_index());
278 num_registers_constant_instr->ReplaceWith(new_constant, /*iterator=*/nullptr);
279}
280
282 return !slow_safe();
283}
284
286 const String& input,
287 const Smi& start_offset,
288 bool sticky,
289 Zone* zone) {
290 const intptr_t cid = input.GetClassId();
291 const Function& fun = Function::Handle(regexp.function(cid, sticky));
292 ASSERT(!fun.IsNull());
293 // Create the argument list.
294 const Array& args =
299
300 // And finally call the generated code.
301
302 const Object& retval =
304 if (retval.IsLanguageError()) {
305 Exceptions::ThrowCompileTimeError(LanguageError::Cast(retval));
306 UNREACHABLE();
307 }
308 if (retval.IsError()) {
309 Exceptions::PropagateError(Error::Cast(retval));
310 }
311
312 if (retval.IsNull()) {
313 return Array::null();
314 }
315
316 ASSERT(retval.IsArray());
317 return Array::Cast(retval).ptr();
318}
319
320LocalVariable* IRRegExpMacroAssembler::Parameter(const String& name,
321 intptr_t index) const {
323 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
324 name, Object::dynamic_type());
325
326 intptr_t param_frame_index = kParamCount - index;
327 local->set_index(VariableIndex(param_frame_index));
328
329 return local;
330}
331
332LocalVariable* IRRegExpMacroAssembler::Local(const String& name) {
333 LocalVariable* local =
334 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
335 name, Object::dynamic_type());
336 local->set_index(VariableIndex(GetNextLocalIndex()));
337
338 return local;
339}
340
341ConstantInstr* IRRegExpMacroAssembler::Int64Constant(int64_t value) const {
342 return new (Z)
344}
345
346ConstantInstr* IRRegExpMacroAssembler::Uint64Constant(uint64_t value) const {
347 ASSERT(value < static_cast<uint64_t>(kMaxInt64));
348 return Int64Constant(static_cast<int64_t>(value));
349}
350
351ConstantInstr* IRRegExpMacroAssembler::BoolConstant(bool value) const {
352 return new (Z) ConstantInstr(value ? Bool::True() : Bool::False());
353}
354
355ConstantInstr* IRRegExpMacroAssembler::StringConstant(const char* value) const {
356 return new (Z)
358}
359
360ConstantInstr* IRRegExpMacroAssembler::WordCharacterMapConstant() const {
361 const Library& lib = Library::Handle(Z, Library::CoreLibrary());
362 const Class& regexp_class =
363 Class::Handle(Z, lib.LookupClassAllowPrivate(Symbols::_RegExp()));
364 const Field& word_character_field = Field::ZoneHandle(
365 Z,
366 regexp_class.LookupStaticFieldAllowPrivate(Symbols::_wordCharacterMap()));
367 ASSERT(!word_character_field.IsNull());
368
369 DEBUG_ASSERT(Thread::Current()->TopErrorHandlerIsSetJump());
370
371 const auto& value =
372 Object::Handle(Z, word_character_field.StaticConstFieldValue());
373 if (value.IsError()) {
374 Report::LongJump(Error::Cast(value));
375 }
376 return new (Z)
377 ConstantInstr(Instance::ZoneHandle(Z, Instance::RawCast(value.ptr())));
378}
379
380ComparisonInstr* IRRegExpMacroAssembler::Comparison(ComparisonKind kind,
381 Value* lhs,
382 Value* rhs) {
383 Token::Kind strict_comparison = Token::kEQ_STRICT;
384 Token::Kind intermediate_operator = Token::kILLEGAL;
385 switch (kind) {
386 case kEQ:
387 intermediate_operator = Token::kEQ;
388 break;
389 case kNE:
390 intermediate_operator = Token::kEQ;
391 strict_comparison = Token::kNE_STRICT;
392 break;
393 case kLT:
394 intermediate_operator = Token::kLT;
395 break;
396 case kGT:
397 intermediate_operator = Token::kGT;
398 break;
399 case kLTE:
400 intermediate_operator = Token::kLTE;
401 break;
402 case kGTE:
403 intermediate_operator = Token::kGTE;
404 break;
405 default:
406 UNREACHABLE();
407 }
408
409 ASSERT(intermediate_operator != Token::kILLEGAL);
410
411 Value* lhs_value = Bind(InstanceCall(
412 InstanceCallDescriptor::FromToken(intermediate_operator), lhs, rhs));
413 Value* rhs_value = Bind(BoolConstant(true));
414
415 return new (Z)
416 StrictCompareInstr(InstructionSource(), strict_comparison, lhs_value,
417 rhs_value, true, GetNextDeoptId());
418}
419
420ComparisonInstr* IRRegExpMacroAssembler::Comparison(ComparisonKind kind,
421 Definition* lhs,
422 Definition* rhs) {
423 Value* lhs_push = Bind(lhs);
424 Value* rhs_push = Bind(rhs);
425 return Comparison(kind, lhs_push, rhs_push);
426}
427
428StaticCallInstr* IRRegExpMacroAssembler::StaticCall(
429 const Function& function,
430 ICData::RebindRule rebind_rule) const {
431 InputsArray arguments(Z, 0);
432 return StaticCall(function, std::move(arguments), rebind_rule);
433}
434
435StaticCallInstr* IRRegExpMacroAssembler::StaticCall(
436 const Function& function,
437 Value* arg1,
438 ICData::RebindRule rebind_rule) const {
439 InputsArray arguments(Z, 1);
440 arguments.Add(arg1);
441
442 return StaticCall(function, std::move(arguments), rebind_rule);
443}
444
445StaticCallInstr* IRRegExpMacroAssembler::StaticCall(
446 const Function& function,
447 Value* arg1,
448 Value* arg2,
449 ICData::RebindRule rebind_rule) const {
450 InputsArray arguments(Z, 2);
451 arguments.Add(arg1);
452 arguments.Add(arg2);
453
454 return StaticCall(function, std::move(arguments), rebind_rule);
455}
456
457StaticCallInstr* IRRegExpMacroAssembler::StaticCall(
458 const Function& function,
459 InputsArray&& arguments,
460 ICData::RebindRule rebind_rule) const {
461 const intptr_t kTypeArgsLen = 0;
462 return new (Z) StaticCallInstr(InstructionSource(), function, kTypeArgsLen,
463 Object::null_array(), std::move(arguments),
464 ic_data_array_, GetNextDeoptId(), rebind_rule);
465}
466
467InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall(
468 const InstanceCallDescriptor& desc,
469 Value* arg1) const {
470 InputsArray arguments(Z, 1);
471 arguments.Add(arg1);
472
473 return InstanceCall(desc, std::move(arguments));
474}
475
476InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall(
477 const InstanceCallDescriptor& desc,
478 Value* arg1,
479 Value* arg2) const {
480 InputsArray arguments(Z, 2);
481 arguments.Add(arg1);
482 arguments.Add(arg2);
483
484 return InstanceCall(desc, std::move(arguments));
485}
486
487InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall(
488 const InstanceCallDescriptor& desc,
489 Value* arg1,
490 Value* arg2,
491 Value* arg3) const {
492 InputsArray arguments(Z, 3);
493 arguments.Add(arg1);
494 arguments.Add(arg2);
495 arguments.Add(arg3);
496
497 return InstanceCall(desc, std::move(arguments));
498}
499
500InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall(
501 const InstanceCallDescriptor& desc,
502 InputsArray&& arguments) const {
503 const intptr_t kTypeArgsLen = 0;
504 return new (Z) InstanceCallInstr(
505 InstructionSource(), desc.name, desc.token_kind, std::move(arguments),
506 kTypeArgsLen, Object::null_array(), desc.checked_argument_count,
507 ic_data_array_, GetNextDeoptId());
508}
509
510LoadLocalInstr* IRRegExpMacroAssembler::LoadLocal(LocalVariable* local) const {
511 return new (Z) LoadLocalInstr(*local, InstructionSource());
512}
513
514void IRRegExpMacroAssembler::StoreLocal(LocalVariable* local, Value* value) {
515 Do(new (Z) StoreLocalInstr(*local, value, InstructionSource()));
516}
517
518LoadStaticFieldInstr* IRRegExpMacroAssembler::LoadStaticField(
519 const Field& field,
520 bool calls_initializer) const {
521 return new (Z) LoadStaticFieldInstr(field, InstructionSource(),
522 calls_initializer, GetNextDeoptId());
523}
524
525void IRRegExpMacroAssembler::set_current_instruction(Instruction* instruction) {
526 current_instruction_ = instruction;
527}
528
529Value* IRRegExpMacroAssembler::Bind(Definition* definition) {
530 AppendInstruction(definition);
531 definition->set_temp_index(temp_id_.Alloc());
532
533 return new (Z) Value(definition);
534}
535
536void IRRegExpMacroAssembler::Do(Definition* definition) {
537 AppendInstruction(definition);
538}
539
540Value* IRRegExpMacroAssembler::BindLoadLocal(const LocalVariable& local) {
541 ASSERT(!local.is_captured());
542 return Bind(new (Z) LoadLocalInstr(local, InstructionSource()));
543}
544
545// In some cases, the V8 irregexp engine generates unreachable code by emitting
546// a jmp not followed by a bind. We cannot do the same, since it is impossible
547// to append to a block following a jmp. In such cases, assume that we are doing
548// the correct thing, but output a warning when tracing.
549#define HANDLE_DEAD_CODE_EMISSION() \
550 if (current_instruction_ == nullptr) { \
551 if (FLAG_trace_irregexp) { \
552 OS::PrintErr( \
553 "WARNING: Attempting to append to a closed assembler. " \
554 "This could be either a bug or generation of dead code " \
555 "inherited from V8.\n"); \
556 } \
557 BlockLabel dummy; \
558 BindBlock(&dummy); \
559 }
560
561void IRRegExpMacroAssembler::AppendInstruction(Instruction* instruction) {
563
564 ASSERT(current_instruction_ != nullptr);
565 ASSERT(current_instruction_->next() == nullptr);
566
567 temp_id_.Dealloc(instruction->InputCount());
568
569 current_instruction_->LinkTo(instruction);
570 set_current_instruction(instruction);
571}
572
573void IRRegExpMacroAssembler::CloseBlockWith(Instruction* instruction) {
575
576 ASSERT(current_instruction_ != nullptr);
577 ASSERT(current_instruction_->next() == nullptr);
578
579 temp_id_.Dealloc(instruction->InputCount());
580
581 current_instruction_->LinkTo(instruction);
582 set_current_instruction(nullptr);
583}
584
586 if (to == nullptr) {
587 Backtrack();
588 } else {
589 to->SetLinked();
590 GoTo(to->block());
591 }
592}
593
594// Closes the current block with a goto, and unsets current_instruction_.
595// BindBlock() must be called before emission can continue.
598
599 ASSERT(current_instruction_ != nullptr);
600 ASSERT(current_instruction_->next() == nullptr);
601 current_instruction_->Goto(to);
602 set_current_instruction(nullptr);
603}
604
605Value* IRRegExpMacroAssembler::PushLocal(LocalVariable* local) {
606 return Bind(LoadLocal(local));
607}
608
609void IRRegExpMacroAssembler::Print(const char* str) {
610 Print(Bind(new (Z) ConstantInstr(
612}
613
614void IRRegExpMacroAssembler::Print(Value* argument) {
616 const Function& print_fn =
618 Do(StaticCall(print_fn, argument, ICData::kStatic));
619}
620
622 for (intptr_t i = 0; i < blocks_.length(); i++) {
623 FlowGraphPrinter::PrintBlock(blocks_[i], false);
624 }
625}
626
628 return 32;
629}
630
632 TAG();
633 if (by != 0) {
634 Value* cur_pos_push = PushLocal(current_position_);
635 Value* by_push = Bind(Int64Constant(by));
636
637 Value* new_pos_value = Bind(Add(cur_pos_push, by_push));
638 StoreLocal(current_position_, new_pos_value);
639 }
640}
641
642void IRRegExpMacroAssembler::AdvanceRegister(intptr_t reg, intptr_t by) {
643 TAG();
644 ASSERT(reg >= 0);
645 ASSERT(reg < registers_count_);
646
647 if (by != 0) {
648 Value* registers_push = PushLocal(registers_);
649 Value* index_push = PushRegisterIndex(reg);
650 Value* reg_push = LoadRegister(reg);
651 Value* by_push = Bind(Int64Constant(by));
652 Value* value_push = Bind(Add(reg_push, by_push));
653 StoreRegister(registers_push, index_push, value_push);
654 }
655}
656
658 TAG();
659 GoTo(backtrack_block_);
660}
661
662// A BindBlock is analogous to assigning a label to a basic block.
663// If the BlockLabel does not yet contain a block, it is created.
664// If there is a current instruction, append a goto to the bound block.
666 ASSERT(!label->is_bound());
667 ASSERT(label->block()->next() == nullptr);
668
669 label->BindTo(block_id_.Alloc());
670 blocks_.Add(label->block());
671
672 if (current_instruction_ != nullptr) {
673 GoTo(label);
674 }
675 set_current_instruction(label->block());
676
677 // Print the id of the current block if tracing.
678 PRINT(Bind(Uint64Constant(label->block()->block_id())));
679}
680
681intptr_t IRRegExpMacroAssembler::GetNextLocalIndex() {
682 intptr_t id = local_id_.Alloc();
683 return -id;
684}
685
686Value* IRRegExpMacroAssembler::LoadRegister(intptr_t index) {
687 Value* registers_push = PushLocal(registers_);
688 Value* index_push = PushRegisterIndex(index);
689 return Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX),
690 registers_push, index_push));
691}
692
693void IRRegExpMacroAssembler::StoreRegister(intptr_t index, intptr_t value) {
694 Value* registers_push = PushLocal(registers_);
695 Value* index_push = PushRegisterIndex(index);
696 Value* value_push = Bind(Uint64Constant(value));
697 StoreRegister(registers_push, index_push, value_push);
698}
699
700void IRRegExpMacroAssembler::StoreRegister(Value* registers,
701 Value* index,
702 Value* value) {
703 TAG();
704 Do(InstanceCall(InstanceCallDescriptor::FromToken(Token::kASSIGN_INDEX),
705 registers, index, value));
706}
707
708Value* IRRegExpMacroAssembler::PushRegisterIndex(intptr_t index) {
709 if (registers_count_ <= index) {
710 registers_count_ = index + 1;
711 }
712 return Bind(Uint64Constant(index));
713}
714
716 TAG();
717 Definition* cur_char_def = LoadLocal(current_character_);
718 Definition* char_def = Uint64Constant(c);
719
720 BranchOrBacktrack(Comparison(kEQ, cur_char_def, char_def), on_equal);
721}
722
724 BlockLabel* on_greater) {
725 TAG();
726 BranchOrBacktrack(
727 Comparison(kGT, LoadLocal(current_character_), Uint64Constant(limit)),
728 on_greater);
729}
730
732 TAG();
733
734 // Are we at the start of the input, i.e. is (offset == string_length * -1)?
735 Definition* neg_len_def =
736 InstanceCall(InstanceCallDescriptor::FromToken(Token::kNEGATE),
737 PushLocal(string_param_length_));
738 Definition* offset_def = LoadLocal(current_position_);
739 BranchOrBacktrack(Comparison(kEQ, neg_len_def, offset_def), on_at_start);
740}
741
742// cp_offset => offset from the current (character) pointer
743// This offset may be negative due to traversing backwards during lookbehind.
745 BlockLabel* on_not_at_start) {
746 TAG();
747
748 // Are we at the start of the input, i.e. is (offset == string_length * -1)?
749 auto neg_len_def =
750 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kNEGATE),
751 PushLocal(string_param_length_)));
752 auto current_pos_def = PushLocal(current_position_);
753 auto cp_offset_def = Bind(Int64Constant(cp_offset));
754 auto offset_def = Bind(Add(current_pos_def, cp_offset_def));
755 BranchOrBacktrack(Comparison(kNE, neg_len_def, offset_def), on_not_at_start);
756}
757
759 BlockLabel* on_less) {
760 TAG();
761 BranchOrBacktrack(
762 Comparison(kLT, LoadLocal(current_character_), Uint64Constant(limit)),
763 on_less);
764}
765
767 TAG();
768
769 BlockLabel fallthrough;
770
771 Definition* head = PeekStack();
772 Definition* cur_pos_def = LoadLocal(current_position_);
773 BranchOrBacktrack(Comparison(kNE, head, cur_pos_def), &fallthrough);
774
775 // Pop, throwing away the value.
776 Do(PopStack());
777
778 BranchOrBacktrack(nullptr, on_equal);
779
780 BindBlock(&fallthrough);
781}
782
784 intptr_t start_reg,
785 bool read_backward,
786 bool unicode,
787 BlockLabel* on_no_match) {
788 TAG();
789 ASSERT(start_reg + 1 <= registers_count_);
790
791 BlockLabel fallthrough;
792
793 Value* end_push = LoadRegister(start_reg + 1);
794 Value* start_push = LoadRegister(start_reg);
795 StoreLocal(capture_length_, Bind(Sub(end_push, start_push)));
796
797 // The length of a capture should not be negative. This can only happen
798 // if the end of the capture is unrecorded, or at a point earlier than
799 // the start of the capture.
800 // BranchOrBacktrack(less, on_no_match);
801
802 BranchOrBacktrack(
803 Comparison(kLT, LoadLocal(capture_length_), Uint64Constant(0)),
804 on_no_match);
805
806 // If length is zero, either the capture is empty or it is completely
807 // uncaptured. In either case succeed immediately.
808 BranchOrBacktrack(
809 Comparison(kEQ, LoadLocal(capture_length_), Uint64Constant(0)),
810 &fallthrough);
811
812 Value* pos_push = nullptr;
813 Value* len_push = nullptr;
814
815 if (!read_backward) {
816 // Check that there are sufficient characters left in the input.
817 pos_push = PushLocal(current_position_);
818 len_push = PushLocal(capture_length_);
819 BranchOrBacktrack(
820 Comparison(kGT,
821 InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD),
822 pos_push, len_push),
823 Uint64Constant(0)),
824 on_no_match);
825 }
826
827 pos_push = PushLocal(current_position_);
828 len_push = PushLocal(string_param_length_);
829 StoreLocal(match_start_index_, Bind(Add(pos_push, len_push)));
830
831 if (read_backward) {
832 // First check that there are enough characters before this point in
833 // the string that we can match the backreference.
834 BranchOrBacktrack(Comparison(kLT, LoadLocal(match_start_index_),
835 LoadLocal(capture_length_)),
836 on_no_match);
837
838 // The string to check is before the current position, not at it.
839 pos_push = PushLocal(match_start_index_);
840 len_push = PushLocal(capture_length_);
841 StoreLocal(match_start_index_, Bind(Sub(pos_push, len_push)));
842 }
843
844 pos_push = LoadRegister(start_reg);
845 len_push = PushLocal(string_param_length_);
846 StoreLocal(capture_start_index_, Bind(Add(pos_push, len_push)));
847
848 pos_push = PushLocal(match_start_index_);
849 len_push = PushLocal(capture_length_);
850 StoreLocal(match_end_index_, Bind(Add(pos_push, len_push)));
851
852 BlockLabel success;
853 if (mode_ == ASCII) {
854 BlockLabel loop_increment;
855 BlockLabel loop;
856 BindBlock(&loop);
857
858 StoreLocal(char_in_capture_, CharacterAt(capture_start_index_));
859 StoreLocal(char_in_match_, CharacterAt(match_start_index_));
860
861 BranchOrBacktrack(
862 Comparison(kEQ, LoadLocal(char_in_capture_), LoadLocal(char_in_match_)),
863 &loop_increment);
864
865 // Mismatch, try case-insensitive match (converting letters to lower-case).
866 Value* match_char_push = PushLocal(char_in_match_);
867 Value* mask_push = Bind(Uint64Constant(0x20));
868 StoreLocal(
869 char_in_match_,
870 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_OR),
871 match_char_push, mask_push)));
872
873 BlockLabel convert_capture;
874 BlockLabel on_not_in_range;
875 BranchOrBacktrack(
876 Comparison(kLT, LoadLocal(char_in_match_), Uint64Constant('a')),
877 &on_not_in_range);
878 BranchOrBacktrack(
879 Comparison(kGT, LoadLocal(char_in_match_), Uint64Constant('z')),
880 &on_not_in_range);
881 GoTo(&convert_capture);
882 BindBlock(&on_not_in_range);
883
884 // Latin-1: Check for values in range [224,254] but not 247.
885 BranchOrBacktrack(
886 Comparison(kLT, LoadLocal(char_in_match_), Uint64Constant(224)),
887 on_no_match);
888 BranchOrBacktrack(
889 Comparison(kGT, LoadLocal(char_in_match_), Uint64Constant(254)),
890 on_no_match);
891
892 BranchOrBacktrack(
893 Comparison(kEQ, LoadLocal(char_in_match_), Uint64Constant(247)),
894 on_no_match);
895
896 // Also convert capture character.
897 BindBlock(&convert_capture);
898
899 Value* capture_char_push = PushLocal(char_in_capture_);
900 mask_push = Bind(Uint64Constant(0x20));
901 StoreLocal(
902 char_in_capture_,
903 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_OR),
904 capture_char_push, mask_push)));
905
906 BranchOrBacktrack(
907 Comparison(kNE, LoadLocal(char_in_match_), LoadLocal(char_in_capture_)),
908 on_no_match);
909
910 BindBlock(&loop_increment);
911
912 // Increment indexes into capture and match strings.
913 Value* index_push = PushLocal(capture_start_index_);
914 Value* inc_push = Bind(Uint64Constant(1));
915 StoreLocal(capture_start_index_, Bind(Add(index_push, inc_push)));
916
917 index_push = PushLocal(match_start_index_);
918 inc_push = Bind(Uint64Constant(1));
919 StoreLocal(match_start_index_, Bind(Add(index_push, inc_push)));
920
921 // Compare to end of match, and loop if not done.
922 BranchOrBacktrack(Comparison(kLT, LoadLocal(match_start_index_),
923 LoadLocal(match_end_index_)),
924 &loop);
925 } else {
926 ASSERT(mode_ == UC16);
927
928 Value* string_value = Bind(LoadLocal(string_param_));
929 Value* lhs_index_value = Bind(LoadLocal(match_start_index_));
930 Value* rhs_index_value = Bind(LoadLocal(capture_start_index_));
931 Value* length_value = Bind(LoadLocal(capture_length_));
932
933 Definition* is_match_def;
934
935 is_match_def = new (Z) CaseInsensitiveCompareInstr(
936 string_value, lhs_index_value, rhs_index_value, length_value,
937 /*handle_surrogates=*/unicode, specialization_cid_);
938
939 BranchOrBacktrack(Comparison(kNE, is_match_def, BoolConstant(true)),
940 on_no_match);
941 }
942
943 BindBlock(&success);
944
945 if (read_backward) {
946 // Move current character position to start of match.
947 pos_push = PushLocal(current_position_);
948 len_push = PushLocal(capture_length_);
949 StoreLocal(current_position_, Bind(Sub(pos_push, len_push)));
950 } else {
951 // Move current character position to position after match.
952 Value* match_end_push = PushLocal(match_end_index_);
953 len_push = PushLocal(string_param_length_);
954 StoreLocal(current_position_, Bind(Sub(match_end_push, len_push)));
955 }
956
957 BindBlock(&fallthrough);
958}
959
961 bool read_backward,
962 BlockLabel* on_no_match) {
963 TAG();
964 ASSERT(start_reg + 1 <= registers_count_);
965
966 BlockLabel fallthrough;
967 BlockLabel success;
968
969 // Find length of back-referenced capture.
970 Value* end_push = LoadRegister(start_reg + 1);
971 Value* start_push = LoadRegister(start_reg);
972 StoreLocal(capture_length_, Bind(Sub(end_push, start_push)));
973
974 // Fail on partial or illegal capture (start of capture after end of capture).
975 BranchOrBacktrack(
976 Comparison(kLT, LoadLocal(capture_length_), Uint64Constant(0)),
977 on_no_match);
978
979 // Succeed on empty capture (including no capture)
980 BranchOrBacktrack(
981 Comparison(kEQ, LoadLocal(capture_length_), Uint64Constant(0)),
982 &fallthrough);
983
984 Value* pos_push = nullptr;
985 Value* len_push = nullptr;
986
987 if (!read_backward) {
988 // Check that there are sufficient characters left in the input.
989 pos_push = PushLocal(current_position_);
990 len_push = PushLocal(capture_length_);
991 BranchOrBacktrack(
992 Comparison(kGT,
993 InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD),
994 pos_push, len_push),
995 Uint64Constant(0)),
996 on_no_match);
997 }
998
999 // Compute pointers to match string and capture string.
1000 pos_push = PushLocal(current_position_);
1001 len_push = PushLocal(string_param_length_);
1002 StoreLocal(match_start_index_, Bind(Add(pos_push, len_push)));
1003
1004 if (read_backward) {
1005 // First check that there are enough characters before this point in
1006 // the string that we can match the backreference.
1007 BranchOrBacktrack(Comparison(kLT, LoadLocal(match_start_index_),
1008 LoadLocal(capture_length_)),
1009 on_no_match);
1010
1011 // The string to check is before the current position, not at it.
1012 pos_push = PushLocal(match_start_index_);
1013 len_push = PushLocal(capture_length_);
1014 StoreLocal(match_start_index_, Bind(Sub(pos_push, len_push)));
1015 }
1016
1017 pos_push = LoadRegister(start_reg);
1018 len_push = PushLocal(string_param_length_);
1019 StoreLocal(capture_start_index_, Bind(Add(pos_push, len_push)));
1020
1021 pos_push = PushLocal(match_start_index_);
1022 len_push = PushLocal(capture_length_);
1023 StoreLocal(match_end_index_, Bind(Add(pos_push, len_push)));
1024
1025 BlockLabel loop;
1026 BindBlock(&loop);
1027
1028 StoreLocal(char_in_capture_, CharacterAt(capture_start_index_));
1029 StoreLocal(char_in_match_, CharacterAt(match_start_index_));
1030
1031 BranchOrBacktrack(
1032 Comparison(kNE, LoadLocal(char_in_capture_), LoadLocal(char_in_match_)),
1033 on_no_match);
1034
1035 // Increment indexes into capture and match strings.
1036 Value* index_push = PushLocal(capture_start_index_);
1037 Value* inc_push = Bind(Uint64Constant(1));
1038 StoreLocal(capture_start_index_, Bind(Add(index_push, inc_push)));
1039
1040 index_push = PushLocal(match_start_index_);
1041 inc_push = Bind(Uint64Constant(1));
1042 StoreLocal(match_start_index_, Bind(Add(index_push, inc_push)));
1043
1044 // Check if we have reached end of match area.
1045 BranchOrBacktrack(Comparison(kLT, LoadLocal(match_start_index_),
1046 LoadLocal(match_end_index_)),
1047 &loop);
1048
1049 BindBlock(&success);
1050
1051 if (read_backward) {
1052 // Move current character position to start of match.
1053 pos_push = PushLocal(current_position_);
1054 len_push = PushLocal(capture_length_);
1055 StoreLocal(current_position_, Bind(Sub(pos_push, len_push)));
1056 } else {
1057 // Move current character position to position after match.
1058 Value* match_end_push = PushLocal(match_end_index_);
1059 len_push = PushLocal(string_param_length_);
1060 StoreLocal(current_position_, Bind(Sub(match_end_push, len_push)));
1061 }
1062
1063 BindBlock(&fallthrough);
1064}
1065
1067 BlockLabel* on_not_equal) {
1068 TAG();
1069 BranchOrBacktrack(
1070 Comparison(kNE, LoadLocal(current_character_), Uint64Constant(c)),
1071 on_not_equal);
1072}
1073
1075 uint32_t mask,
1076 BlockLabel* on_equal) {
1077 TAG();
1078
1079 Definition* actual_def = LoadLocal(current_character_);
1080
1081 Value* actual_push = Bind(actual_def);
1082 Value* mask_push = Bind(Uint64Constant(mask));
1083 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND),
1084 actual_push, mask_push);
1085 Definition* expected_def = Uint64Constant(c);
1086
1087 BranchOrBacktrack(Comparison(kEQ, actual_def, expected_def), on_equal);
1088}
1089
1091 uint32_t c,
1092 uint32_t mask,
1093 BlockLabel* on_not_equal) {
1094 TAG();
1095
1096 Definition* actual_def = LoadLocal(current_character_);
1097
1098 Value* actual_push = Bind(actual_def);
1099 Value* mask_push = Bind(Uint64Constant(mask));
1100 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND),
1101 actual_push, mask_push);
1102 Definition* expected_def = Uint64Constant(c);
1103
1104 BranchOrBacktrack(Comparison(kNE, actual_def, expected_def), on_not_equal);
1105}
1106
1108 uint16_t c,
1109 uint16_t minus,
1110 uint16_t mask,
1111 BlockLabel* on_not_equal) {
1112 TAG();
1113 ASSERT(minus < Utf16::kMaxCodeUnit); // NOLINT
1114
1115 Definition* actual_def = LoadLocal(current_character_);
1116
1117 Value* actual_push = Bind(actual_def);
1118 Value* minus_push = Bind(Uint64Constant(minus));
1119
1120 actual_push = Bind(Sub(actual_push, minus_push));
1121 Value* mask_push = Bind(Uint64Constant(mask));
1122 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND),
1123 actual_push, mask_push);
1124 Definition* expected_def = Uint64Constant(c);
1125
1126 BranchOrBacktrack(Comparison(kNE, actual_def, expected_def), on_not_equal);
1127}
1128
1130 uint16_t to,
1131 BlockLabel* on_in_range) {
1132 TAG();
1133 ASSERT(from <= to);
1134
1135 // TODO(zerny): All range comparisons could be done cheaper with unsigned
1136 // compares. This pattern repeats in various places.
1137
1138 BlockLabel on_not_in_range;
1139 BranchOrBacktrack(
1140 Comparison(kLT, LoadLocal(current_character_), Uint64Constant(from)),
1141 &on_not_in_range);
1142 BranchOrBacktrack(
1143 Comparison(kGT, LoadLocal(current_character_), Uint64Constant(to)),
1144 &on_not_in_range);
1145 BranchOrBacktrack(nullptr, on_in_range);
1146
1147 BindBlock(&on_not_in_range);
1148}
1149
1151 uint16_t from,
1152 uint16_t to,
1153 BlockLabel* on_not_in_range) {
1154 TAG();
1155 ASSERT(from <= to);
1156
1157 BranchOrBacktrack(
1158 Comparison(kLT, LoadLocal(current_character_), Uint64Constant(from)),
1159 on_not_in_range);
1160
1161 BranchOrBacktrack(
1162 Comparison(kGT, LoadLocal(current_character_), Uint64Constant(to)),
1163 on_not_in_range);
1164}
1165
1167 BlockLabel* on_bit_set) {
1168 TAG();
1169
1170 Value* table_push = Bind(new (Z) ConstantInstr(table));
1171 Value* index_push = PushLocal(current_character_);
1172
1173 if (mode_ != ASCII || kTableMask != Symbols::kMaxOneCharCodeSymbol) {
1174 Value* mask_push = Bind(Uint64Constant(kTableSize - 1));
1175 index_push =
1176 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND),
1177 index_push, mask_push));
1178 }
1179
1180 Definition* byte_def = InstanceCall(
1181 InstanceCallDescriptor::FromToken(Token::kINDEX), table_push, index_push);
1182 Definition* zero_def = Int64Constant(0);
1183
1184 BranchOrBacktrack(Comparison(kNE, byte_def, zero_def), on_bit_set);
1185}
1186
1188 uint16_t type,
1189 BlockLabel* on_no_match) {
1190 TAG();
1191
1192 // Range checks (c in min..max) are generally implemented by an unsigned
1193 // (c - min) <= (max - min) check
1194 switch (type) {
1195 case 's':
1196 // Match space-characters
1197 if (mode_ == ASCII) {
1198 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
1199 BlockLabel success;
1200 // Space (' ').
1201 BranchOrBacktrack(
1202 Comparison(kEQ, LoadLocal(current_character_), Uint64Constant(' ')),
1203 &success);
1204 // Check range 0x09..0x0d.
1205 CheckCharacterInRange('\t', '\r', &success);
1206 // \u00a0 (NBSP).
1207 BranchOrBacktrack(Comparison(kNE, LoadLocal(current_character_),
1208 Uint64Constant(0x00a0)),
1209 on_no_match);
1210 BindBlock(&success);
1211 return true;
1212 }
1213 return false;
1214 case 'S':
1215 // The emitted code for generic character classes is good enough.
1216 return false;
1217 case 'd':
1218 // Match ASCII digits ('0'..'9')
1219 CheckCharacterNotInRange('0', '9', on_no_match);
1220 return true;
1221 case 'D':
1222 // Match non ASCII-digits
1223 CheckCharacterInRange('0', '9', on_no_match);
1224 return true;
1225 case '.': {
1226 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
1227 BranchOrBacktrack(
1228 Comparison(kEQ, LoadLocal(current_character_), Uint64Constant('\n')),
1229 on_no_match);
1230 BranchOrBacktrack(
1231 Comparison(kEQ, LoadLocal(current_character_), Uint64Constant('\r')),
1232 on_no_match);
1233 if (mode_ == UC16) {
1234 BranchOrBacktrack(Comparison(kEQ, LoadLocal(current_character_),
1235 Uint64Constant(0x2028)),
1236 on_no_match);
1237 BranchOrBacktrack(Comparison(kEQ, LoadLocal(current_character_),
1238 Uint64Constant(0x2029)),
1239 on_no_match);
1240 }
1241 return true;
1242 }
1243 case 'w': {
1244 if (mode_ != ASCII) {
1245 // Table is 128 entries, so all ASCII characters can be tested.
1246 BranchOrBacktrack(
1247 Comparison(kGT, LoadLocal(current_character_), Uint64Constant('z')),
1248 on_no_match);
1249 }
1250
1251 Value* table_push = Bind(WordCharacterMapConstant());
1252 Value* index_push = PushLocal(current_character_);
1253
1254 Definition* byte_def =
1255 InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX),
1256 table_push, index_push);
1257 Definition* zero_def = Int64Constant(0);
1258
1259 BranchOrBacktrack(Comparison(kEQ, byte_def, zero_def), on_no_match);
1260
1261 return true;
1262 }
1263 case 'W': {
1265 if (mode_ != ASCII) {
1266 // Table is 128 entries, so all ASCII characters can be tested.
1267 BranchOrBacktrack(
1268 Comparison(kGT, LoadLocal(current_character_), Uint64Constant('z')),
1269 &done);
1270 }
1271
1272 // TODO(zerny): Refactor to use CheckBitInTable if possible.
1273
1274 Value* table_push = Bind(WordCharacterMapConstant());
1275 Value* index_push = PushLocal(current_character_);
1276
1277 Definition* byte_def =
1278 InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX),
1279 table_push, index_push);
1280 Definition* zero_def = Int64Constant(0);
1281
1282 BranchOrBacktrack(Comparison(kNE, byte_def, zero_def), on_no_match);
1283
1284 if (mode_ != ASCII) {
1285 BindBlock(&done);
1286 }
1287 return true;
1288 }
1289 // Non-standard classes (with no syntactic shorthand) used internally.
1290 case '*':
1291 // Match any character.
1292 return true;
1293 case 'n': {
1294 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 or 0x2029).
1295 // The opposite of '.'.
1296 BlockLabel success;
1297 BranchOrBacktrack(
1298 Comparison(kEQ, LoadLocal(current_character_), Uint64Constant('\n')),
1299 &success);
1300 BranchOrBacktrack(
1301 Comparison(kEQ, LoadLocal(current_character_), Uint64Constant('\r')),
1302 &success);
1303 if (mode_ == UC16) {
1304 BranchOrBacktrack(Comparison(kEQ, LoadLocal(current_character_),
1305 Uint64Constant(0x2028)),
1306 &success);
1307 BranchOrBacktrack(Comparison(kEQ, LoadLocal(current_character_),
1308 Uint64Constant(0x2029)),
1309 &success);
1310 }
1311 BranchOrBacktrack(nullptr, on_no_match);
1312 BindBlock(&success);
1313 return true;
1314 }
1315 // No custom implementation (yet): s(uint16_t), S(uint16_t).
1316 default:
1317 return false;
1318 }
1319}
1320
1322 TAG();
1323 ASSERT(FAILURE == 0); // Return value for failure is zero.
1324 if (!global()) {
1325 UNREACHABLE(); // Dart regexps are always global.
1326 }
1327 GoTo(exit_block_);
1328}
1329
1331 intptr_t comparand,
1332 BlockLabel* if_ge) {
1333 TAG();
1334 Value* reg_push = LoadRegister(reg);
1335 Value* pos = Bind(Int64Constant(comparand));
1336 BranchOrBacktrack(Comparison(kGTE, reg_push, pos), if_ge);
1337}
1338
1340 intptr_t comparand,
1341 BlockLabel* if_lt) {
1342 TAG();
1343 Value* reg_push = LoadRegister(reg);
1344 Value* pos = Bind(Int64Constant(comparand));
1345 BranchOrBacktrack(Comparison(kLT, reg_push, pos), if_lt);
1346}
1347
1349 TAG();
1350 Value* reg_push = LoadRegister(reg);
1351 Value* pos = Bind(LoadLocal(current_position_));
1352 BranchOrBacktrack(Comparison(kEQ, reg_push, pos), if_eq);
1353}
1354
1357 return kIRImplementation;
1358}
1359
1361 BlockLabel* on_end_of_input,
1362 bool check_bounds,
1363 intptr_t characters) {
1364 TAG();
1365 ASSERT(cp_offset < (1 << 30)); // Be sane! (And ensure negation works)
1366 if (check_bounds) {
1367 if (cp_offset >= 0) {
1368 CheckPosition(cp_offset + characters - 1, on_end_of_input);
1369 } else {
1370 CheckPosition(cp_offset, on_end_of_input);
1371 }
1372 }
1373 LoadCurrentCharacterUnchecked(cp_offset, characters);
1374}
1375
1377 TAG();
1378 StoreLocal(current_position_, Bind(PopStack()));
1379}
1380
1382 TAG();
1383 ASSERT(reg < registers_count_);
1384 Value* registers_push = PushLocal(registers_);
1385 Value* index_push = PushRegisterIndex(reg);
1386 Value* pop_push = Bind(PopStack());
1387 StoreRegister(registers_push, index_push, pop_push);
1388}
1389
1390void IRRegExpMacroAssembler::PushStack(Definition* definition) {
1391 Value* stack_push = PushLocal(stack_);
1392 Value* stack_pointer_push = PushLocal(stack_pointer_);
1393 StoreLocal(stack_pointer_,
1394 Bind(Add(stack_pointer_push, Bind(Uint64Constant(1)))));
1395 stack_pointer_push = PushLocal(stack_pointer_);
1396 // TODO(zerny): bind value and push could break stack discipline.
1397 Value* value_push = Bind(definition);
1398 Do(InstanceCall(InstanceCallDescriptor::FromToken(Token::kASSIGN_INDEX),
1399 stack_push, stack_pointer_push, value_push));
1400}
1401
1402Definition* IRRegExpMacroAssembler::PopStack() {
1403 Value* stack_push = PushLocal(stack_);
1404 Value* stack_pointer_push1 = PushLocal(stack_pointer_);
1405 Value* stack_pointer_push2 = PushLocal(stack_pointer_);
1406 StoreLocal(stack_pointer_,
1407 Bind(Sub(stack_pointer_push2, Bind(Uint64Constant(1)))));
1408 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX),
1409 stack_push, stack_pointer_push1);
1410}
1411
1412Definition* IRRegExpMacroAssembler::PeekStack() {
1413 Value* stack_push = PushLocal(stack_);
1414 Value* stack_pointer_push = PushLocal(stack_pointer_);
1415 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX),
1416 stack_push, stack_pointer_push);
1417}
1418
1419// Pushes the location corresponding to label to the backtracking stack.
1421 TAG();
1422
1423 // Ensure that targets of indirect jumps are never accessed through a
1424 // normal control flow instructions by creating a new block for each backtrack
1425 // target.
1426 IndirectEntryInstr* indirect_target = IndirectWithJoinGoto(label->block());
1427
1428 // Add a fake edge from the graph entry for data flow analysis.
1429 entry_block_->AddIndirectEntry(indirect_target);
1430
1431 ConstantInstr* offset = Uint64Constant(indirect_target->indirect_id());
1432 PushStack(offset);
1433 CheckStackLimit();
1434}
1435
1437 TAG();
1438 PushStack(LoadLocal(current_position_));
1439}
1440
1442 TAG();
1443 // TODO(zerny): Refactor PushStack so it can be reused here.
1444 Value* stack_push = PushLocal(stack_);
1445 Value* stack_pointer_push = PushLocal(stack_pointer_);
1446 StoreLocal(stack_pointer_,
1447 Bind(Add(stack_pointer_push, Bind(Uint64Constant(1)))));
1448 stack_pointer_push = PushLocal(stack_pointer_);
1449 // TODO(zerny): bind value and push could break stack discipline.
1450 Value* value_push = LoadRegister(reg);
1451 Do(InstanceCall(InstanceCallDescriptor::FromToken(Token::kASSIGN_INDEX),
1452 stack_push, stack_pointer_push, value_push));
1453 CheckStackLimit();
1454}
1455
1456// Checks that (stack.capacity - stack_limit_slack) > stack_pointer.
1457// This ensures that up to stack_limit_slack stack pushes can be
1458// done without exhausting the stack space. If the check fails the
1459// stack will be grown.
1460void IRRegExpMacroAssembler::CheckStackLimit() {
1461 TAG();
1462 Value* stack_push = PushLocal(stack_);
1463 Value* length_push =
1464 Bind(InstanceCall(InstanceCallDescriptor(String::ZoneHandle(
1465 Field::GetterSymbol(Symbols::Length()))),
1466 stack_push));
1467 Value* capacity_push =
1468 Bind(Sub(length_push, Bind(Uint64Constant(stack_limit_slack()))));
1469 Value* stack_pointer_push = PushLocal(stack_pointer_);
1470 BranchInstr* branch = new (Z) BranchInstr(
1471 Comparison(kGT, capacity_push, stack_pointer_push), GetNextDeoptId());
1472 CloseBlockWith(branch);
1473
1474 BlockLabel grow_stack;
1475 BlockLabel fallthrough;
1476 *branch->true_successor_address() = TargetWithJoinGoto(fallthrough.block());
1477 *branch->false_successor_address() = TargetWithJoinGoto(grow_stack.block());
1478
1479 BindBlock(&grow_stack);
1480 GrowStack();
1481
1482 BindBlock(&fallthrough);
1483}
1484
1485void IRRegExpMacroAssembler::GrowStack() {
1486 TAG();
1487 const Library& lib = Library::Handle(Library::CoreLibrary());
1488 const Class& regexp_class =
1489 Class::Handle(lib.LookupClassAllowPrivate(Symbols::_RegExp()));
1490
1491 const Function& grow_backtracking_stack_function =
1492 Function::ZoneHandle(Z, regexp_class.LookupFunctionAllowPrivate(
1493 Symbols::_growBacktrackingStack()));
1494 StoreLocal(stack_, Bind(StaticCall(grow_backtracking_stack_function,
1495 ICData::kStatic)));
1496}
1497
1499 TAG();
1500 StoreLocal(current_position_, LoadRegister(reg));
1501}
1502
1503// Resets the tip of the stack to the value stored in reg.
1505 TAG();
1506 ASSERT(reg < registers_count_);
1507 StoreLocal(stack_pointer_, LoadRegister(reg));
1508}
1509
1511 TAG();
1512
1513 BlockLabel after_position;
1514
1515 Definition* cur_pos_def = LoadLocal(current_position_);
1516 Definition* by_value_def = Int64Constant(-by);
1517
1518 BranchOrBacktrack(Comparison(kGTE, cur_pos_def, by_value_def),
1519 &after_position);
1520
1521 StoreLocal(current_position_, Bind(Int64Constant(-by)));
1522
1523 // On RegExp code entry (where this operation is used), the character before
1524 // the current position is expected to be already loaded.
1525 // We have advanced the position, so it's safe to read backwards.
1526 LoadCurrentCharacterUnchecked(-1, 1);
1527
1528 BindBlock(&after_position);
1529}
1530
1531void IRRegExpMacroAssembler::SetRegister(intptr_t reg, intptr_t to) {
1532 TAG();
1533 // Reserved for positions!
1534 ASSERT(reg >= saved_registers_count_);
1535 StoreRegister(reg, to);
1536}
1537
1539 TAG();
1540 GoTo(success_block_);
1541 return global();
1542}
1543
1545 intptr_t reg,
1546 intptr_t cp_offset) {
1547 TAG();
1548
1549 Value* registers_push = PushLocal(registers_);
1550 Value* index_push = PushRegisterIndex(reg);
1551 Value* pos_push = PushLocal(current_position_);
1552 Value* off_push = Bind(Int64Constant(cp_offset));
1553 Value* neg_off_push = Bind(Add(pos_push, off_push));
1554 // Push the negative offset; these are converted to positive string positions
1555 // within the success block.
1556 StoreRegister(registers_push, index_push, neg_off_push);
1557}
1558
1560 intptr_t reg_to) {
1561 TAG();
1562
1563 ASSERT(reg_from <= reg_to);
1564
1565 // In order to clear registers to a final result value of -1, set them to
1566 // (-1 - string length), the offset of -1 from the end of the string.
1567
1568 for (intptr_t reg = reg_from; reg <= reg_to; reg++) {
1569 Value* registers_push = PushLocal(registers_);
1570 Value* index_push = PushRegisterIndex(reg);
1571 Value* minus_one_push = Bind(Int64Constant(-1));
1572 Value* length_push = PushLocal(string_param_length_);
1573 Value* value_push = Bind(Sub(minus_one_push, length_push));
1574 StoreRegister(registers_push, index_push, value_push);
1575 }
1576}
1577
1579 TAG();
1580
1581 Value* registers_push = PushLocal(registers_);
1582 Value* index_push = PushRegisterIndex(reg);
1583 Value* tip_push = PushLocal(stack_pointer_);
1584 StoreRegister(registers_push, index_push, tip_push);
1585}
1586
1587// Private methods:
1588
1590 BlockLabel* on_outside_input) {
1591 TAG();
1592 if (cp_offset >= 0) {
1593 Definition* curpos_def = LoadLocal(current_position_);
1594 Definition* cp_off_def = Int64Constant(-cp_offset);
1595 // If (current_position_ < -cp_offset), we are in bounds.
1596 // Remember, current_position_ is a negative offset from the string end.
1597
1598 BranchOrBacktrack(Comparison(kGTE, curpos_def, cp_off_def),
1599 on_outside_input);
1600 } else {
1601 // We need to see if there's enough characters left in the string to go
1602 // back cp_offset characters, so get the normalized position and then
1603 // make sure that (normalized_position >= -cp_offset).
1604 Value* pos_push = PushLocal(current_position_);
1605 Value* len_push = PushLocal(string_param_length_);
1606 BranchOrBacktrack(
1607 Comparison(kLT, Add(pos_push, len_push), Uint64Constant(-cp_offset)),
1608 on_outside_input);
1609 }
1610}
1611
1612void IRRegExpMacroAssembler::BranchOrBacktrack(ComparisonInstr* comparison,
1613 BlockLabel* true_successor) {
1614 if (comparison == nullptr) { // No condition
1615 if (true_successor == nullptr) {
1616 Backtrack();
1617 return;
1618 }
1619 GoTo(true_successor);
1620 return;
1621 }
1622
1623 // If no successor block has been passed in, backtrack.
1624 JoinEntryInstr* true_successor_block = backtrack_block_;
1625 if (true_successor != nullptr) {
1626 true_successor->SetLinked();
1627 true_successor_block = true_successor->block();
1628 }
1629 ASSERT(true_successor_block != nullptr);
1630
1631 // If the condition is not true, fall through to a new block.
1632 BlockLabel fallthrough;
1633
1634 BranchInstr* branch = new (Z) BranchInstr(comparison, GetNextDeoptId());
1635 *branch->true_successor_address() = TargetWithJoinGoto(true_successor_block);
1636 *branch->false_successor_address() = TargetWithJoinGoto(fallthrough.block());
1637
1638 CloseBlockWith(branch);
1639 BindBlock(&fallthrough);
1640}
1641
1642TargetEntryInstr* IRRegExpMacroAssembler::TargetWithJoinGoto(
1643 JoinEntryInstr* dst) {
1644 TargetEntryInstr* target = new (Z)
1645 TargetEntryInstr(block_id_.Alloc(), kInvalidTryIndex, GetNextDeoptId());
1646 blocks_.Add(target);
1647
1648 target->AppendInstruction(new (Z) GotoInstr(dst, GetNextDeoptId()));
1649
1650 return target;
1651}
1652
1653IndirectEntryInstr* IRRegExpMacroAssembler::IndirectWithJoinGoto(
1654 JoinEntryInstr* dst) {
1655 IndirectEntryInstr* target =
1656 new (Z) IndirectEntryInstr(block_id_.Alloc(), indirect_id_.Alloc(),
1657 kInvalidTryIndex, GetNextDeoptId());
1658 blocks_.Add(target);
1659
1660 target->AppendInstruction(new (Z) GotoInstr(dst, GetNextDeoptId()));
1661
1662 return target;
1663}
1664
1665void IRRegExpMacroAssembler::CheckPreemption(bool is_backtrack) {
1666 TAG();
1667
1668 // We don't have the loop_depth available when compiling regexps, but
1669 // we set loop_depth to a non-zero value because this instruction does
1670 // not act as an OSR entry outside loops.
1671 AppendInstruction(new (Z) CheckStackOverflowInstr(
1672 InstructionSource(),
1673 /*stack_depth=*/0,
1674 /*loop_depth=*/1, GetNextDeoptId(),
1677}
1678
1679Definition* IRRegExpMacroAssembler::Add(Value* lhs, Value* rhs) {
1680 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD), lhs, rhs);
1681}
1682
1683Definition* IRRegExpMacroAssembler::Sub(Value* lhs, Value* rhs) {
1684 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kSUB), lhs, rhs);
1685}
1686
1687void IRRegExpMacroAssembler::LoadCurrentCharacterUnchecked(
1688 intptr_t cp_offset,
1689 intptr_t characters) {
1690 TAG();
1691
1692 ASSERT(characters == 1 || CanReadUnaligned());
1693 if (mode_ == ASCII) {
1694 ASSERT(characters == 1 || characters == 2 || characters == 4);
1695 } else {
1696 ASSERT(mode_ == UC16);
1697 ASSERT(characters == 1 || characters == 2);
1698 }
1699
1700 // Calculate the addressed string index as:
1701 // cp_offset + current_position_ + string_param_length_
1702 // TODO(zerny): Avoid generating 'add' instance-calls here.
1703 Value* off_arg = Bind(Int64Constant(cp_offset));
1704 Value* pos_arg = BindLoadLocal(*current_position_);
1705 Value* off_pos_arg = Bind(Add(off_arg, pos_arg));
1706 Value* len_arg = BindLoadLocal(*string_param_length_);
1707 // Index is stored in a temporary local so that we can later load it safely.
1708 StoreLocal(index_temp_, Bind(Add(off_pos_arg, len_arg)));
1709
1710 // Load and store the code units.
1711 Value* code_unit_value = LoadCodeUnitsAt(index_temp_, characters);
1712 StoreLocal(current_character_, code_unit_value);
1713 PRINT(PushLocal(current_character_));
1714}
1715
1716Value* IRRegExpMacroAssembler::CharacterAt(LocalVariable* index) {
1717 return LoadCodeUnitsAt(index, 1);
1718}
1719
1720Value* IRRegExpMacroAssembler::LoadCodeUnitsAt(LocalVariable* index,
1721 intptr_t characters) {
1722 // Bind the pattern as the load receiver.
1723 Value* pattern_val = BindLoadLocal(*string_param_);
1724
1725 // Here pattern_val might be untagged so this must not trigger a GC.
1726 Value* index_val = BindLoadLocal(*index);
1727
1728 return Bind(new (Z)
1729 LoadCodeUnitsInstr(pattern_val, index_val, characters,
1730 specialization_cid_, InstructionSource()));
1731}
1732
1733#undef __
1734
1735} // namespace dart
1736
1737#endif // !defined(DART_PRECOMPILED_RUNTIME)
static void done(const char *config, const char *src, const char *srcOptions, const char *name)
Definition: DM.cpp:263
SkPoint pos
void check_bounds(skiatest::Reporter *reporter, const SkPath &path)
Definition: ShadowTest.cpp:183
SI F table(const skcms_Curve *curve, F v)
#define UNREACHABLE()
Definition: assert.h:248
#define DEBUG_ASSERT(cond)
Definition: assert.h:321
GLenum type
static ArrayPtr New(intptr_t len, Heap::Space space=Heap::kNew)
Definition: object.h:10959
intptr_t block_id() const
Definition: il.h:1661
Convenience wrapper around a BlockEntryInstr pointer.
bool is_bound() const
void BindTo(intptr_t pos)
JoinEntryInstr * block() const
static const Bool & False()
Definition: object.h:10799
static const Bool & True()
Definition: object.h:10797
TargetEntryInstr ** false_successor_address()
Definition: il.h:4051
TargetEntryInstr ** true_successor_address()
Definition: il.h:4050
static ObjectPtr InvokeFunction(const Function &function, const Array &arguments)
Definition: dart_entry.cc:31
void ReplaceWith(Definition *other, ForwardInstructionIterator *iterator)
Definition: il.cc:1662
void set_temp_index(intptr_t index)
Definition: il.h:2499
intptr_t temp_index() const
Definition: il.h:2498
static DART_NORETURN void ThrowCompileTimeError(const LanguageError &error)
Definition: exceptions.cc:1112
static DART_NORETURN void PropagateError(const Error &error)
Definition: exceptions.cc:1003
static StringPtr GetterSymbol(const String &field_name)
Definition: object.cc:11796
static void PrintBlock(BlockEntryInstr *block, bool print_locations)
Definition: il_printer.cc:1701
FunctionEntryInstr * normal_entry() const
Definition: il.h:2001
void AddIndirectEntry(IndirectEntryInstr *entry)
Definition: il.h:1970
void set_normal_entry(FunctionEntryInstr *entry)
Definition: il.h:2003
const GrowableArray< IndirectEntryInstr * > & indirect_entries() const
Definition: il.h:2016
@ kOld
Definition: heap.h:39
virtual void CheckNotCharacter(uint32_t c, BlockLabel *on_not_equal)
virtual void CheckCharacterNotInRange(uint16_t from, uint16_t to, BlockLabel *on_not_in_range)
virtual void CheckCharacterInRange(uint16_t from, uint16_t to, BlockLabel *on_in_range)
virtual void BindBlock(BlockLabel *label)
virtual void CheckCharacterGT(uint16_t limit, BlockLabel *on_greater)
virtual void IfRegisterLT(intptr_t reg, intptr_t comparand, BlockLabel *if_lt)
virtual void IfRegisterGE(intptr_t reg, intptr_t comparand, BlockLabel *if_ge)
IRRegExpMacroAssembler(intptr_t specialization_cid, intptr_t capture_count, const ParsedFunction *parsed_function, const ZoneGrowableArray< const ICData * > &ic_data_array, intptr_t osr_id, Zone *zone)
virtual void SetRegister(intptr_t register_index, intptr_t to)
virtual void CheckAtStart(BlockLabel *on_at_start)
virtual void CheckCharacterLT(uint16_t limit, BlockLabel *on_less)
virtual void GoTo(BlockLabel *to)
virtual IrregexpImplementation Implementation()
virtual void CheckNotAtStart(intptr_t cp_offset, BlockLabel *on_not_at_start)
virtual void Print(const char *str)
virtual void CheckGreedyLoop(BlockLabel *on_tos_equals_current_position)
static ArrayPtr Execute(const RegExp &regexp, const String &input, const Smi &start_offset, bool sticky, Zone *zone)
virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to)
virtual void AdvanceCurrentPosition(intptr_t by)
virtual void CheckCharacterAfterAnd(uint32_t c, uint32_t mask, BlockLabel *on_equal)
virtual void IfRegisterEqPos(intptr_t reg, BlockLabel *if_eq)
virtual void AdvanceRegister(intptr_t reg, intptr_t by)
virtual void PushRegister(intptr_t register_index)
virtual void CheckNotCharacterAfterAnd(uint32_t c, uint32_t mask, BlockLabel *on_not_equal)
virtual void CheckCharacter(uint32_t c, BlockLabel *on_equal)
virtual void ReadCurrentPositionFromRegister(intptr_t reg)
virtual void CheckNotCharacterAfterMinusAnd(uint16_t c, uint16_t minus, uint16_t mask, BlockLabel *on_not_equal)
virtual void CheckPosition(intptr_t cp_offset, BlockLabel *on_outside_input)
virtual void SetCurrentPositionFromEnd(intptr_t by)
virtual void CheckNotBackReference(intptr_t start_reg, bool read_backward, BlockLabel *on_no_match)
virtual void ReadStackPointerFromRegister(intptr_t reg)
virtual bool CheckSpecialCharacterClass(uint16_t type, BlockLabel *on_no_match)
virtual void CheckBitInTable(const TypedData &table, BlockLabel *on_bit_set)
virtual void LoadCurrentCharacter(intptr_t cp_offset, BlockLabel *on_end_of_input, bool check_bounds=true, intptr_t characters=1)
virtual void WriteStackPointerToRegister(intptr_t reg)
virtual void PushBacktrack(BlockLabel *label)
virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg, bool read_backward, bool unicode, BlockLabel *on_no_match)
virtual void WriteCurrentPositionToRegister(intptr_t reg, intptr_t cp_offset)
virtual void PopRegister(intptr_t register_index)
intptr_t indirect_id() const
Definition: il.h:2314
void AddSuccessor(TargetEntryInstr *successor)
Definition: il.h:3812
Instruction * next() const
Definition: il.h:1093
void LinkTo(Instruction *next)
Definition: il.h:1108
void Goto(JoinEntryInstr *entry)
Definition: il.cc:2030
static IntegerPtr NewCanonical(const String &str)
Definition: object.cc:22999
static IsolateGroup * Current()
Definition: isolate.h:539
static LibraryPtr CoreLibrary()
Definition: object.cc:14787
FunctionPtr LookupFunctionAllowPrivate(const String &name) const
Definition: object.cc:14084
void set_index(VariableIndex index)
Definition: scopes.h:208
static ObjectPtr null()
Definition: object.h:433
intptr_t GetClassId() const
Definition: object.h:341
bool IsNull() const
Definition: object.h:363
static Object & Handle()
Definition: object.h:407
static ObjectPtr RawCast(ObjectPtr obj)
Definition: object.h:325
static Object & ZoneHandle()
Definition: object.h:419
LocalVariable * current_context_var() const
Definition: parser.h:128
static constexpr intptr_t kTableMask
static constexpr intptr_t kTableSize
FunctionPtr function(intptr_t cid, bool sticky) const
Definition: object.h:12836
static DART_NORETURN void LongJump(const Error &error)
Definition: report.cc:86
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition: object.cc:23698
@ kMaxOneCharCodeSymbol
Definition: symbols.h:577
static Thread * Current()
Definition: thread.h:362
static constexpr int32_t kMaxCodeUnit
Definition: unicode.h:158
Definition: il.h:75
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint8_t value
uint32_t * target
Dart_NativeFunction function
Definition: fuchsia.cc:51
size_t length
Definition: dart_vm.cc:33
constexpr int64_t kMaxInt64
Definition: globals.h:486
const char *const name
GrowableArray< Value * > InputsArray
Definition: il.h:901
const intptr_t cid
static constexpr intptr_t kInvalidTryIndex
dst
Definition: cp.py:12
def print(*args, **kwargs)
Definition: run_tests.py:49
#define TAG()
#define Z
#define PRINT(arg)
#define HANDLE_DEAD_CODE_EMISSION()
SeparatedVector2 offset
const uintptr_t id