OLD | NEW |
(Empty) | |
| 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 #include "vm/regexp_assembler.h" |
| 6 |
| 7 #include "vm/bit_vector.h" |
| 8 #include "vm/compiler.h" |
| 9 #include "vm/dart_entry.h" |
| 10 #include "vm/flow_graph_builder.h" |
| 11 #include "vm/il_printer.h" |
| 12 #include "vm/object_store.h" |
| 13 #include "vm/regexp.h" |
| 14 #include "vm/resolver.h" |
| 15 #include "vm/stack_frame.h" |
| 16 #include "vm/unibrow-inl.h" |
| 17 #include "vm/unicode.h" |
| 18 |
| 19 #define I isolate() |
| 20 |
| 21 // Debugging output macros. TAG() is called at the head of each interesting |
| 22 // function and prints its name during execution if irregexp tracing is enabled. |
| 23 #define TAG() if (FLAG_trace_irregexp) { TAG_(); } |
| 24 #define TAG_() \ |
| 25 Print(PushArgument( \ |
| 26 Bind(new(I) ConstantInstr(String::ZoneHandle(I, String::Concat( \ |
| 27 String::Handle(String::New("TAG: ")), \ |
| 28 String::Handle(String::New(__FUNCTION__)), Heap::kOld)))))); |
| 29 |
| 30 #define PRINT(arg) if (FLAG_trace_irregexp) { Print(arg); } |
| 31 |
| 32 namespace dart { |
| 33 |
| 34 DEFINE_FLAG(bool, trace_irregexp, false, "Trace irregexps"); |
| 35 |
| 36 |
| 37 static const intptr_t kInvalidTryIndex = -1; |
| 38 static const intptr_t kNoTokenPos = -1; |
| 39 |
| 40 |
| 41 void PrintUtf16(uint16_t c) { |
| 42 const char* format = (0x20 <= c && c <= 0x7F) ? |
| 43 "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x"; |
| 44 OS::Print(format, c); |
| 45 } |
| 46 |
| 47 |
| 48 /* |
| 49 * This assembler uses the following main local variables: |
| 50 * - stack_: A pointer to a growable list which we use as an all-purpose stack |
| 51 * storing backtracking offsets, positions & stored register values. |
| 52 * - current_character_: Stores the currently loaded characters (possibly more |
| 53 * than one). |
| 54 * - current_position_: The current position within the string, stored as a |
| 55 * negative offset from the end of the string (i.e. the |
| 56 * position corresponding to str[0] is -str.length). |
| 57 * Note that current_position_ is *not* byte-based, unlike |
| 58 * original V8 code. |
| 59 * |
| 60 * Results are returned though an array of capture indices, stored at |
| 61 * matches_param_. A null array specifies a failure to match. The match indices |
| 62 * [start_inclusive, end_exclusive] for capture group i are stored at positions |
| 63 * matches_param_[i * 2] and matches_param_[i * 2 + 1], respectively. Match |
| 64 * indices of -1 denote non-matched groups. Note that we store these indices |
| 65 * as a negative offset from the end of the string in position_registers_ |
| 66 * during processing, and convert them to standard indexes when copying them |
| 67 * to matches_param_ on successful match. |
| 68 */ |
| 69 |
| 70 RegExpMacroAssembler::RegExpMacroAssembler(Isolate* isolate) |
| 71 : slow_safe_compiler_(false), |
| 72 global_mode_(NOT_GLOBAL), |
| 73 isolate_(isolate) { |
| 74 } |
| 75 |
| 76 |
| 77 RegExpMacroAssembler::~RegExpMacroAssembler() { |
| 78 } |
| 79 |
| 80 |
| 81 IRRegExpMacroAssembler::IRRegExpMacroAssembler( |
| 82 intptr_t specialization_cid, |
| 83 intptr_t capture_count, |
| 84 const ParsedFunction* parsed_function, |
| 85 const ZoneGrowableArray<const ICData*>& ic_data_array, |
| 86 Isolate* isolate) |
| 87 : RegExpMacroAssembler(isolate), |
| 88 specialization_cid_(specialization_cid), |
| 89 parsed_function_(parsed_function), |
| 90 ic_data_array_(ic_data_array), |
| 91 current_instruction_(NULL), |
| 92 stack_(NULL), |
| 93 current_character_(NULL), |
| 94 current_position_(NULL), |
| 95 string_param_(NULL), |
| 96 string_param_length_(NULL), |
| 97 start_index_param_(NULL), |
| 98 position_registers_count_((capture_count + 1) * 2), |
| 99 stack_array_(GrowableObjectArray::ZoneHandle( |
| 100 isolate, GrowableObjectArray::New(16, Heap::kOld))) { |
| 101 switch (specialization_cid) { |
| 102 case kOneByteStringCid: |
| 103 case kExternalOneByteStringCid: mode_ = ASCII; break; |
| 104 case kTwoByteStringCid: |
| 105 case kExternalTwoByteStringCid: mode_ = UC16; break; |
| 106 default: UNREACHABLE(); |
| 107 } |
| 108 |
| 109 InitializeLocals(); |
| 110 |
| 111 // Create and generate all preset blocks. |
| 112 entry_block_ = |
| 113 new(isolate) GraphEntryInstr( |
| 114 parsed_function_, |
| 115 new(isolate) TargetEntryInstr(block_id_.Alloc(), kInvalidTryIndex), |
| 116 Isolate::kNoDeoptId); |
| 117 start_block_ = |
| 118 new(isolate) JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex); |
| 119 success_block_ = |
| 120 new(isolate) JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex); |
| 121 backtrack_block_ = |
| 122 new(isolate) JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex); |
| 123 exit_block_ = |
| 124 new(isolate) JoinEntryInstr(block_id_.Alloc(), kInvalidTryIndex); |
| 125 |
| 126 GenerateEntryBlock(); |
| 127 GenerateSuccessBlock(); |
| 128 GenerateBacktrackBlock(); |
| 129 GenerateExitBlock(); |
| 130 |
| 131 blocks_.Add(entry_block_); |
| 132 blocks_.Add(entry_block_->normal_entry()); |
| 133 blocks_.Add(start_block_); |
| 134 blocks_.Add(success_block_); |
| 135 blocks_.Add(backtrack_block_); |
| 136 blocks_.Add(exit_block_); |
| 137 |
| 138 // Begin emission at the start_block_. |
| 139 set_current_instruction(start_block_); |
| 140 } |
| 141 |
| 142 |
| 143 IRRegExpMacroAssembler::~IRRegExpMacroAssembler() { } |
| 144 |
| 145 |
| 146 void IRRegExpMacroAssembler::InitializeLocals() { |
| 147 // Create local variables and parameters. |
| 148 stack_ = Local(Symbols::stack_()); |
| 149 current_character_ = Local(Symbols::current_character_()); |
| 150 current_position_ = Local(Symbols::current_position_()); |
| 151 string_param_length_ = Local(Symbols::string_param_length_()); |
| 152 capture_length_ = Local(Symbols::capture_length_()); |
| 153 match_start_index_ = Local(Symbols::match_start_index_()); |
| 154 capture_start_index_ = Local(Symbols::capture_start_index_()); |
| 155 match_end_index_ = Local(Symbols::match_end_index_()); |
| 156 char_in_capture_ = Local(Symbols::char_in_capture_()); |
| 157 char_in_match_ = Local(Symbols::char_in_match_()); |
| 158 result_ = Local(Symbols::result_()); |
| 159 |
| 160 string_param_ = Parameter(Symbols::string_param_(), 0); |
| 161 start_index_param_ = Parameter(Symbols::start_index_param_(), 1); |
| 162 |
| 163 // Reserve space for all captured group positions. Note that more might |
| 164 // be created on the fly for internal use. |
| 165 for (intptr_t i = 0; i < position_registers_count_; i++) { |
| 166 position_register(i); |
| 167 } |
| 168 } |
| 169 |
| 170 |
| 171 void IRRegExpMacroAssembler::GenerateEntryBlock() { |
| 172 set_current_instruction(entry_block_->normal_entry()); |
| 173 TAG(); |
| 174 |
| 175 // Generate a local list variable which we will use as a backtracking stack. |
| 176 |
| 177 StoreLocal(stack_, Bind(new(I) ConstantInstr(stack_array_))); |
| 178 Do(InstanceCall(InstanceCallDescriptor(Symbols::clear()), PushLocal(stack_))); |
| 179 |
| 180 // Store string.length. |
| 181 PushArgumentInstr* string_push = PushLocal(string_param_); |
| 182 |
| 183 StoreLocal(string_param_length_, |
| 184 Bind(InstanceCall(InstanceCallDescriptor( |
| 185 String::ZoneHandle( |
| 186 Field::GetterSymbol(Symbols::Length()))), |
| 187 string_push))); |
| 188 |
| 189 // Initialize all capture registers. |
| 190 ClearRegisters(0, position_registers_count_ - 1); |
| 191 |
| 192 // Store (start_index - string.length) as the current position (since it's a |
| 193 // negative offset from the end of the string). |
| 194 PushArgumentInstr* start_index_push = PushLocal(start_index_param_); |
| 195 PushArgumentInstr* length_push = PushLocal(string_param_length_); |
| 196 |
| 197 StoreLocal(current_position_, Bind(Sub(start_index_push, length_push))); |
| 198 |
| 199 // Jump to the start block. |
| 200 current_instruction_->Goto(start_block_); |
| 201 } |
| 202 |
| 203 |
| 204 void IRRegExpMacroAssembler::GenerateBacktrackBlock() { |
| 205 set_current_instruction(backtrack_block_); |
| 206 TAG(); |
| 207 Backtrack(); |
| 208 } |
| 209 |
| 210 |
| 211 void IRRegExpMacroAssembler::GenerateSuccessBlock() { |
| 212 set_current_instruction(success_block_); |
| 213 TAG(); |
| 214 |
| 215 Definition* type_args_null_def = new(I) ConstantInstr( |
| 216 TypeArguments::ZoneHandle(I, TypeArguments::null())); |
| 217 PushArgumentInstr* type_arg_push = PushArgument(Bind(type_args_null_def)); |
| 218 PushArgumentInstr* length_push = |
| 219 PushArgument(Bind(Uint64Constant(position_registers_count_))); |
| 220 |
| 221 const Library& lib = Library::Handle(Library::CoreLibrary()); |
| 222 const Class& list_class = Class::Handle( |
| 223 lib.LookupCoreClass(Symbols::List())); |
| 224 const Function& list_ctor = |
| 225 Function::ZoneHandle(I, list_class.LookupFactory(Symbols::ListFactory())); |
| 226 |
| 227 // TODO(jgruber): Use CreateArrayInstr and StoreIndexed instead. |
| 228 StoreLocal(result_, Bind(StaticCall(list_ctor, type_arg_push, length_push))); |
| 229 |
| 230 // Store captured offsets in the `matches` parameter. |
| 231 // TODO(jgruber): Eliminate position_register locals and access `matches` |
| 232 // directly. |
| 233 for (intptr_t i = 0; i < position_registers_count_; i++) { |
| 234 PushArgumentInstr* matches_push = PushLocal(result_); |
| 235 PushArgumentInstr* index_push = PushArgument(Bind(Uint64Constant(i))); |
| 236 |
| 237 // Convert negative offsets from the end of the string to string indices. |
| 238 PushArgumentInstr* offset_push = PushLocal(position_register(i)); |
| 239 PushArgumentInstr* len_push = PushLocal(string_param_length_); |
| 240 PushArgumentInstr* value_push = |
| 241 PushArgument(Bind(Add(offset_push, len_push))); |
| 242 |
| 243 Do(InstanceCall(InstanceCallDescriptor::FromToken(Token::kASSIGN_INDEX), |
| 244 matches_push, |
| 245 index_push, |
| 246 value_push)); |
| 247 } |
| 248 |
| 249 // Print the result if tracing. |
| 250 PRINT(PushLocal(result_)); |
| 251 |
| 252 // Return true on success. |
| 253 AppendInstruction(new(I) ReturnInstr(kNoTokenPos, Bind(LoadLocal(result_)))); |
| 254 } |
| 255 |
| 256 |
| 257 void IRRegExpMacroAssembler::GenerateExitBlock() { |
| 258 set_current_instruction(exit_block_); |
| 259 TAG(); |
| 260 |
| 261 // Return false on failure. |
| 262 AppendInstruction(new(I) ReturnInstr(kNoTokenPos, Bind(LoadLocal(result_)))); |
| 263 } |
| 264 |
| 265 |
| 266 #if defined(TARGET_ARCH_ARM64) || \ |
| 267 defined(TARGET_ARCH_ARM) || \ |
| 268 defined(TARGET_ARCH_MIPS) |
| 269 // Disabling unaligned accesses forces the regexp engine to load characters one |
| 270 // by one instead of up to 4 at once, along with the associated performance hit. |
| 271 // TODO(jgruber): Be less conservative about disabling unaligned accesses. |
| 272 // For instance, ARMv6 supports unaligned accesses. Once it is enabled here, |
| 273 // update LoadCodeUnitsInstr methods for the appropriate architectures. |
| 274 static const bool kEnableUnalignedAccesses = false; |
| 275 #else |
| 276 static const bool kEnableUnalignedAccesses = true; |
| 277 #endif |
| 278 bool IRRegExpMacroAssembler::CanReadUnaligned() { |
| 279 return kEnableUnalignedAccesses && !slow_safe(); |
| 280 } |
| 281 |
| 282 |
| 283 RawArray* IRRegExpMacroAssembler::Execute( |
| 284 const Function& function, |
| 285 const String& input, |
| 286 const Smi& start_offset, |
| 287 Isolate* isolate) { |
| 288 // Create the argument list. |
| 289 const Array& args = Array::Handle(Array::New(2)); |
| 290 args.SetAt(0, input); |
| 291 args.SetAt(1, start_offset); |
| 292 |
| 293 // And finally call the generated code. |
| 294 |
| 295 const Object& retval = |
| 296 Object::Handle(isolate, DartEntry::InvokeFunction(function, args)); |
| 297 if (retval.IsError()) { |
| 298 const Error& error = Error::Cast(retval); |
| 299 OS::Print("%s\n", error.ToErrorCString()); |
| 300 // Should never happen. |
| 301 UNREACHABLE(); |
| 302 } |
| 303 |
| 304 if (retval.IsNull()) { |
| 305 return Array::null(); |
| 306 } |
| 307 |
| 308 ASSERT(retval.IsArray()); |
| 309 return Array::Cast(retval).raw(); |
| 310 } |
| 311 |
| 312 |
| 313 RawBool* IRRegExpMacroAssembler::CaseInsensitiveCompareUC16( |
| 314 RawString* str_raw, |
| 315 RawSmi* lhs_index_raw, |
| 316 RawSmi* rhs_index_raw, |
| 317 RawSmi* length_raw) { |
| 318 const String& str = String::Handle(str_raw); |
| 319 const Smi& lhs_index = Smi::Handle(lhs_index_raw); |
| 320 const Smi& rhs_index = Smi::Handle(rhs_index_raw); |
| 321 const Smi& length = Smi::Handle(length_raw); |
| 322 |
| 323 // TODO(jgruber): Optimize as single instance. V8 has this as an |
| 324 // isolate member. |
| 325 unibrow::Mapping<unibrow::Ecma262Canonicalize> canonicalize; |
| 326 |
| 327 for (intptr_t i = 0; i < length.Value(); i++) { |
| 328 int32_t c1 = str.CharAt(lhs_index.Value() + i); |
| 329 int32_t c2 = str.CharAt(rhs_index.Value() + i); |
| 330 if (c1 != c2) { |
| 331 int32_t s1[1] = { c1 }; |
| 332 canonicalize.get(c1, '\0', s1); |
| 333 if (s1[0] != c2) { |
| 334 int32_t s2[1] = { c2 }; |
| 335 canonicalize.get(c2, '\0', s2); |
| 336 if (s1[0] != s2[0]) { |
| 337 return Bool::False().raw(); |
| 338 } |
| 339 } |
| 340 } |
| 341 } |
| 342 return Bool::True().raw(); |
| 343 } |
| 344 |
| 345 |
| 346 LocalVariable* IRRegExpMacroAssembler::Parameter(const String& name, |
| 347 intptr_t index) const { |
| 348 const Type& local_type = Type::ZoneHandle(I, Type::DynamicType()); |
| 349 LocalVariable* local = |
| 350 new(I) LocalVariable(kNoTokenPos, name, local_type); |
| 351 |
| 352 intptr_t param_frame_index = kParamEndSlotFromFp + kParamCount - index; |
| 353 local->set_index(param_frame_index); |
| 354 |
| 355 return local; |
| 356 } |
| 357 |
| 358 |
| 359 LocalVariable* IRRegExpMacroAssembler::Local(const String& name) { |
| 360 const Type& local_type = Type::ZoneHandle(I, Type::DynamicType()); |
| 361 LocalVariable* local = |
| 362 new(I) LocalVariable(kNoTokenPos, name, local_type); |
| 363 local->set_index(GetNextLocalIndex()); |
| 364 |
| 365 return local; |
| 366 } |
| 367 |
| 368 |
| 369 ConstantInstr* IRRegExpMacroAssembler::Int64Constant(int64_t value) const { |
| 370 return new(I) ConstantInstr( |
| 371 Integer::ZoneHandle(I, Integer::New(value, Heap::kOld))); |
| 372 } |
| 373 |
| 374 |
| 375 ConstantInstr* IRRegExpMacroAssembler::Uint64Constant(uint64_t value) const { |
| 376 return new(I) ConstantInstr( |
| 377 Integer::ZoneHandle(I, Integer::NewFromUint64(value, Heap::kOld))); |
| 378 } |
| 379 |
| 380 |
| 381 ConstantInstr* IRRegExpMacroAssembler::BoolConstant(bool value) const { |
| 382 return new(I) ConstantInstr(value ? Bool::True() : Bool::False()); |
| 383 } |
| 384 |
| 385 |
| 386 ConstantInstr* IRRegExpMacroAssembler::StringConstant(const char* value) const { |
| 387 return new(I) ConstantInstr( |
| 388 String::ZoneHandle(I, String::New(value, Heap::kOld))); |
| 389 } |
| 390 |
| 391 |
| 392 ConstantInstr* IRRegExpMacroAssembler::WordCharacterMapConstant() const { |
| 393 const Library& lib = Library::Handle(I, Library::CoreLibrary()); |
| 394 const Class& regexp_class = Class::Handle(I, |
| 395 lib.LookupClassAllowPrivate(Symbols::JSSyntaxRegExp())); |
| 396 const Field& word_character_field = Field::ZoneHandle(I, |
| 397 regexp_class.LookupStaticField(Symbols::_wordCharacterMap())); |
| 398 ASSERT(!word_character_field.IsNull()); |
| 399 |
| 400 if (word_character_field.IsUninitialized()) { |
| 401 word_character_field.EvaluateInitializer(); |
| 402 } |
| 403 ASSERT(!word_character_field.IsUninitialized()); |
| 404 |
| 405 return new(I) ConstantInstr( |
| 406 Instance::ZoneHandle(I, word_character_field.value())); |
| 407 } |
| 408 |
| 409 |
| 410 ComparisonInstr* IRRegExpMacroAssembler::Comparison( |
| 411 ComparisonKind kind, Definition* lhs, Definition* rhs) { |
| 412 Token::Kind strict_comparison = Token::kEQ_STRICT; |
| 413 Token::Kind intermediate_operator = Token::kILLEGAL; |
| 414 switch (kind) { |
| 415 case kEQ: |
| 416 intermediate_operator = Token::kEQ; |
| 417 break; |
| 418 case kNE: |
| 419 intermediate_operator = Token::kEQ; |
| 420 strict_comparison = Token::kNE_STRICT; |
| 421 break; |
| 422 case kLT: |
| 423 intermediate_operator = Token::kLT; |
| 424 break; |
| 425 case kGT: |
| 426 intermediate_operator = Token::kGT; |
| 427 break; |
| 428 case kLTE: |
| 429 intermediate_operator = Token::kLTE; |
| 430 break; |
| 431 case kGTE: |
| 432 intermediate_operator = Token::kGTE; |
| 433 break; |
| 434 default: |
| 435 UNREACHABLE(); |
| 436 } |
| 437 |
| 438 ASSERT(intermediate_operator != Token::kILLEGAL); |
| 439 |
| 440 PushArgumentInstr* lhs_push = PushArgument(Bind(lhs)); |
| 441 PushArgumentInstr* rhs_push = PushArgument(Bind(rhs)); |
| 442 |
| 443 Value* lhs_value = |
| 444 Bind(InstanceCall( |
| 445 InstanceCallDescriptor::FromToken(intermediate_operator), |
| 446 lhs_push, |
| 447 rhs_push)); |
| 448 Value* rhs_value = Bind(BoolConstant(true)); |
| 449 |
| 450 return new(I) StrictCompareInstr(kNoTokenPos, strict_comparison, |
| 451 lhs_value, rhs_value, true); |
| 452 } |
| 453 |
| 454 |
| 455 StaticCallInstr* IRRegExpMacroAssembler::StaticCall( |
| 456 const Function& function) const { |
| 457 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 458 new(I) ZoneGrowableArray<PushArgumentInstr*>(0); |
| 459 return StaticCall(function, arguments); |
| 460 } |
| 461 |
| 462 |
| 463 StaticCallInstr* IRRegExpMacroAssembler::StaticCall( |
| 464 const Function& function, |
| 465 PushArgumentInstr* arg1) const { |
| 466 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 467 new(I) ZoneGrowableArray<PushArgumentInstr*>(1); |
| 468 arguments->Add(arg1); |
| 469 |
| 470 return StaticCall(function, arguments); |
| 471 } |
| 472 |
| 473 |
| 474 StaticCallInstr* IRRegExpMacroAssembler::StaticCall( |
| 475 const Function& function, |
| 476 PushArgumentInstr* arg1, |
| 477 PushArgumentInstr* arg2) const { |
| 478 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 479 new(I) ZoneGrowableArray<PushArgumentInstr*>(2); |
| 480 arguments->Add(arg1); |
| 481 arguments->Add(arg2); |
| 482 |
| 483 return StaticCall(function, arguments); |
| 484 } |
| 485 |
| 486 |
| 487 StaticCallInstr* IRRegExpMacroAssembler::StaticCall( |
| 488 const Function& function, |
| 489 ZoneGrowableArray<PushArgumentInstr*>* arguments) const { |
| 490 return new(I) StaticCallInstr(kNoTokenPos, |
| 491 function, |
| 492 Object::null_array(), |
| 493 arguments, |
| 494 ic_data_array_); |
| 495 } |
| 496 |
| 497 |
| 498 InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall( |
| 499 const InstanceCallDescriptor& desc, |
| 500 PushArgumentInstr* arg1) const { |
| 501 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 502 new(I) ZoneGrowableArray<PushArgumentInstr*>(1); |
| 503 arguments->Add(arg1); |
| 504 |
| 505 return InstanceCall(desc, arguments); |
| 506 } |
| 507 |
| 508 |
| 509 InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall( |
| 510 const InstanceCallDescriptor& desc, |
| 511 PushArgumentInstr* arg1, |
| 512 PushArgumentInstr* arg2) const { |
| 513 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 514 new(I) ZoneGrowableArray<PushArgumentInstr*>(2); |
| 515 arguments->Add(arg1); |
| 516 arguments->Add(arg2); |
| 517 |
| 518 return InstanceCall(desc, arguments); |
| 519 } |
| 520 |
| 521 |
| 522 InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall( |
| 523 const InstanceCallDescriptor& desc, |
| 524 PushArgumentInstr* arg1, |
| 525 PushArgumentInstr* arg2, |
| 526 PushArgumentInstr* arg3) const { |
| 527 ZoneGrowableArray<PushArgumentInstr*>* arguments = |
| 528 new(I) ZoneGrowableArray<PushArgumentInstr*>(3); |
| 529 arguments->Add(arg1); |
| 530 arguments->Add(arg2); |
| 531 arguments->Add(arg3); |
| 532 |
| 533 return InstanceCall(desc, arguments); |
| 534 } |
| 535 |
| 536 |
| 537 InstanceCallInstr* IRRegExpMacroAssembler::InstanceCall( |
| 538 const InstanceCallDescriptor& desc, |
| 539 ZoneGrowableArray<PushArgumentInstr*> *arguments) const { |
| 540 return |
| 541 new(I) InstanceCallInstr(kNoTokenPos, |
| 542 desc.name, |
| 543 desc.token_kind, |
| 544 arguments, |
| 545 Object::null_array(), |
| 546 desc.checked_argument_count, |
| 547 ic_data_array_); |
| 548 } |
| 549 |
| 550 |
| 551 LoadLocalInstr* IRRegExpMacroAssembler::LoadLocal(LocalVariable* local) const { |
| 552 return new(I) LoadLocalInstr(*local); |
| 553 } |
| 554 |
| 555 |
| 556 void IRRegExpMacroAssembler::StoreLocal(LocalVariable* local, |
| 557 Value* value) { |
| 558 Do(new(I) StoreLocalInstr(*local, value)); |
| 559 } |
| 560 |
| 561 |
| 562 void IRRegExpMacroAssembler::set_current_instruction(Instruction* instruction) { |
| 563 current_instruction_ = instruction; |
| 564 } |
| 565 |
| 566 |
| 567 Value* IRRegExpMacroAssembler::Bind(Definition* definition) { |
| 568 AppendInstruction(definition); |
| 569 definition->set_temp_index(temp_id_.Alloc()); |
| 570 |
| 571 return new(I) Value(definition); |
| 572 } |
| 573 |
| 574 |
| 575 void IRRegExpMacroAssembler::Do(Definition* definition) { |
| 576 AppendInstruction(definition); |
| 577 } |
| 578 |
| 579 // In some cases, the V8 irregexp engine generates unreachable code by emitting |
| 580 // a jmp not followed by a bind. We cannot do the same, since it is impossible |
| 581 // to append to a block following a jmp. In such cases, assume that we are doing |
| 582 // the correct thing, but output a warning when tracing. |
| 583 #define HANDLE_DEAD_CODE_EMISSION() \ |
| 584 if (current_instruction_ == NULL) { \ |
| 585 if (FLAG_trace_irregexp) { \ |
| 586 OS::Print("WARNING: Attempting to append to a closed assembler. " \ |
| 587 "This could be either a bug or generation of dead code " \ |
| 588 "inherited from V8.\n"); \ |
| 589 } \ |
| 590 BlockLabel dummy; \ |
| 591 BindBlock(&dummy); \ |
| 592 } |
| 593 |
| 594 void IRRegExpMacroAssembler::AppendInstruction(Instruction* instruction) { |
| 595 HANDLE_DEAD_CODE_EMISSION(); |
| 596 |
| 597 ASSERT(current_instruction_ != NULL); |
| 598 ASSERT(current_instruction_->next() == NULL); |
| 599 |
| 600 temp_id_.Dealloc(instruction->InputCount()); |
| 601 arg_id_.Dealloc(instruction->ArgumentCount()); |
| 602 |
| 603 current_instruction_->LinkTo(instruction); |
| 604 set_current_instruction(instruction); |
| 605 } |
| 606 |
| 607 |
| 608 void IRRegExpMacroAssembler::CloseBlockWith(Instruction* instruction) { |
| 609 HANDLE_DEAD_CODE_EMISSION(); |
| 610 |
| 611 ASSERT(current_instruction_ != NULL); |
| 612 ASSERT(current_instruction_->next() == NULL); |
| 613 |
| 614 temp_id_.Dealloc(instruction->InputCount()); |
| 615 arg_id_.Dealloc(instruction->ArgumentCount()); |
| 616 |
| 617 current_instruction_->LinkTo(instruction); |
| 618 set_current_instruction(NULL); |
| 619 } |
| 620 |
| 621 |
| 622 void IRRegExpMacroAssembler::GoTo(BlockLabel* to) { |
| 623 if (to == NULL) { |
| 624 Backtrack(); |
| 625 } else { |
| 626 to->SetLinked(); |
| 627 GoTo(to->block()); |
| 628 } |
| 629 } |
| 630 |
| 631 |
| 632 // Closes the current block with a goto, and unsets current_instruction_. |
| 633 // BindBlock() must be called before emission can continue. |
| 634 void IRRegExpMacroAssembler::GoTo(JoinEntryInstr* to) { |
| 635 HANDLE_DEAD_CODE_EMISSION(); |
| 636 |
| 637 ASSERT(current_instruction_ != NULL); |
| 638 ASSERT(current_instruction_->next() == NULL); |
| 639 current_instruction_->Goto(to); |
| 640 set_current_instruction(NULL); |
| 641 } |
| 642 |
| 643 |
| 644 PushArgumentInstr* IRRegExpMacroAssembler::PushArgument(Value* value) { |
| 645 arg_id_.Alloc(); |
| 646 PushArgumentInstr* push = new(I) PushArgumentInstr(value); |
| 647 // Do *not* use Do() for push argument instructions. |
| 648 AppendInstruction(push); |
| 649 return push; |
| 650 } |
| 651 |
| 652 |
| 653 PushArgumentInstr* IRRegExpMacroAssembler::PushLocal(LocalVariable* local) { |
| 654 return PushArgument(Bind(LoadLocal(local))); |
| 655 } |
| 656 |
| 657 |
| 658 void IRRegExpMacroAssembler::Print(const char* str) { |
| 659 Print(PushArgument( |
| 660 Bind(new(I) ConstantInstr( |
| 661 String::ZoneHandle(I, String::New(str, Heap::kOld)))))); |
| 662 } |
| 663 |
| 664 |
| 665 void IRRegExpMacroAssembler::Print(PushArgumentInstr* argument) { |
| 666 const Library& lib = Library::Handle(Library::CoreLibrary()); |
| 667 const Function& print_fn = Function::ZoneHandle( |
| 668 I, lib.LookupFunctionAllowPrivate(Symbols::print())); |
| 669 Do(StaticCall(print_fn, argument)); |
| 670 } |
| 671 |
| 672 |
| 673 void IRRegExpMacroAssembler::PrintBlocks() { |
| 674 for (intptr_t i = 0; i < blocks_.length(); i++) { |
| 675 FlowGraphPrinter::PrintBlock(blocks_[i], false); |
| 676 } |
| 677 } |
| 678 |
| 679 |
| 680 intptr_t IRRegExpMacroAssembler::stack_limit_slack() { |
| 681 return 32; |
| 682 } |
| 683 |
| 684 |
| 685 void IRRegExpMacroAssembler::AdvanceCurrentPosition(intptr_t by) { |
| 686 TAG(); |
| 687 if (by != 0) { |
| 688 PushArgumentInstr* cur_pos_push = PushLocal(current_position_); |
| 689 PushArgumentInstr* by_push = PushArgument(Bind(Int64Constant(by))); |
| 690 |
| 691 Value* new_pos_value = Bind(Add(cur_pos_push, by_push)); |
| 692 StoreLocal(current_position_, new_pos_value); |
| 693 } |
| 694 } |
| 695 |
| 696 |
| 697 void IRRegExpMacroAssembler::AdvanceRegister(intptr_t reg, intptr_t by) { |
| 698 TAG(); |
| 699 ASSERT(reg >= 0); |
| 700 ASSERT(reg < position_registers_.length()); |
| 701 |
| 702 if (by != 0) { |
| 703 PushArgumentInstr* reg_push = PushLocal(position_register(reg)); |
| 704 PushArgumentInstr* by_push = PushArgument(Bind(Int64Constant(by))); |
| 705 StoreLocal(position_register(reg), Bind(Add(reg_push, by_push))); |
| 706 } |
| 707 } |
| 708 |
| 709 |
| 710 void IRRegExpMacroAssembler::Backtrack() { |
| 711 TAG(); |
| 712 CheckPreemption(); |
| 713 |
| 714 GrowableObjectArray& offsets = GrowableObjectArray::ZoneHandle( |
| 715 I, GrowableObjectArray::New(Heap::kOld)); |
| 716 |
| 717 PushArgumentInstr* block_offsets_push = |
| 718 PushArgument(Bind(new(I) ConstantInstr(offsets))); |
| 719 PushArgumentInstr* block_id_push = PushArgument(PopStack()); |
| 720 |
| 721 Value* offset_value = |
| 722 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX), |
| 723 block_offsets_push, |
| 724 block_id_push)); |
| 725 |
| 726 IndirectGotoInstr* igoto = new(I) IndirectGotoInstr(&offsets, offset_value); |
| 727 CloseBlockWith(igoto); |
| 728 igotos_.Add(igoto); |
| 729 } |
| 730 |
| 731 |
| 732 // A BindBlock is analogous to assigning a label to a basic block. |
| 733 // If the BlockLabel does not yet contain a block, it is created. |
| 734 // If there is a current instruction, append a goto to the bound block. |
| 735 void IRRegExpMacroAssembler::BindBlock(BlockLabel* label) { |
| 736 ASSERT(!label->IsBound()); |
| 737 ASSERT(label->block()->next() == NULL); |
| 738 |
| 739 label->SetBound(block_id_.Alloc()); |
| 740 blocks_.Add(label->block()); |
| 741 |
| 742 if (current_instruction_ != NULL) { |
| 743 GoTo(label); |
| 744 } |
| 745 set_current_instruction(label->block()); |
| 746 |
| 747 // Print the id of the current block if tracing. |
| 748 PRINT(PushArgument(Bind(Uint64Constant(label->block()->block_id())))); |
| 749 } |
| 750 |
| 751 |
| 752 intptr_t IRRegExpMacroAssembler::GetNextLocalIndex() { |
| 753 intptr_t id = local_id_.Alloc(); |
| 754 return kFirstLocalSlotFromFp - id; |
| 755 } |
| 756 |
| 757 |
| 758 LocalVariable* IRRegExpMacroAssembler::position_register(intptr_t index) { |
| 759 // Create position registers as needed. |
| 760 for (intptr_t i = position_registers_.length(); i < index + 1; i++) { |
| 761 position_registers_.Add(Local(Symbols::position_registers_())); |
| 762 } |
| 763 |
| 764 return position_registers_[index]; |
| 765 } |
| 766 |
| 767 |
| 768 // TODO(jgruber): Move the offset table outside to avoid having to keep |
| 769 // the assembler around until after code generation; both function or regexp |
| 770 // would work. |
| 771 void IRRegExpMacroAssembler::FinalizeBlockOffsetTable() { |
| 772 for (intptr_t i = 0; i < igotos_.length(); i++) { |
| 773 IndirectGotoInstr* igoto = igotos_[i]; |
| 774 igoto->SetOffsetCount(I, indirect_id_.Count()); |
| 775 |
| 776 for (intptr_t j = 0; j < igoto->SuccessorCount(); j++) { |
| 777 TargetEntryInstr* target = igoto->SuccessorAt(j); |
| 778 |
| 779 // Optimizations might have modified the immediate target block, but |
| 780 // it must end with a goto to the indirect entry. |
| 781 Instruction* instr = target; |
| 782 while (instr != NULL && !instr->IsGoto()) { |
| 783 instr = instr->next(); |
| 784 } |
| 785 ASSERT(instr->IsGoto()); |
| 786 |
| 787 IndirectEntryInstr* ientry = |
| 788 instr->AsGoto()->successor()->AsIndirectEntry(); |
| 789 ASSERT(ientry != NULL); |
| 790 |
| 791 // The intermediate block was possibly compacted, check both it and the |
| 792 // final indirect entry for a valid offset. If neither are valid, then |
| 793 // the indirect entry is unreachable. |
| 794 intptr_t offset = |
| 795 (target->offset() > 0) ? target->offset() : ientry->offset(); |
| 796 if (offset > 0) { |
| 797 intptr_t adjusted_offset = |
| 798 offset - Assembler::EntryPointToPcMarkerOffset(); |
| 799 igoto->SetOffsetAt(I, ientry->indirect_id(), adjusted_offset); |
| 800 } |
| 801 } |
| 802 } |
| 803 } |
| 804 |
| 805 void IRRegExpMacroAssembler::FinalizeIndirectGotos() { |
| 806 for (intptr_t i = 0; i < igotos_.length(); i++) { |
| 807 for (intptr_t j = 0; j < entry_block_->indirect_entries().length(); j++) { |
| 808 igotos_.At(i)->AddSuccessor( |
| 809 TargetWithJoinGoto(entry_block_->indirect_entries().At(j))); |
| 810 } |
| 811 } |
| 812 } |
| 813 |
| 814 |
| 815 void IRRegExpMacroAssembler::CheckCharacter(uint32_t c, BlockLabel* on_equal) { |
| 816 TAG(); |
| 817 Definition* cur_char_def = LoadLocal(current_character_); |
| 818 Definition* char_def = Uint64Constant(c); |
| 819 |
| 820 BranchOrBacktrack(Comparison(kEQ, cur_char_def, char_def), |
| 821 on_equal); |
| 822 } |
| 823 |
| 824 |
| 825 void IRRegExpMacroAssembler::CheckCharacterGT(uint16_t limit, |
| 826 BlockLabel* on_greater) { |
| 827 TAG(); |
| 828 BranchOrBacktrack(Comparison(kGT, |
| 829 LoadLocal(current_character_), |
| 830 Uint64Constant(limit)), |
| 831 on_greater); |
| 832 } |
| 833 |
| 834 |
| 835 void IRRegExpMacroAssembler::CheckAtStart(BlockLabel* on_at_start) { |
| 836 TAG(); |
| 837 |
| 838 BlockLabel not_at_start; |
| 839 |
| 840 // Did we start the match at the start of the string at all? |
| 841 BranchOrBacktrack(Comparison(kNE, |
| 842 LoadLocal(start_index_param_), |
| 843 Uint64Constant(0)), |
| 844 ¬_at_start); |
| 845 |
| 846 // If we did, are we still at the start of the input, i.e. is |
| 847 // (offset == string_length * -1)? |
| 848 Definition* neg_len_def = |
| 849 InstanceCall(InstanceCallDescriptor::FromToken(Token::kNEGATE), |
| 850 PushLocal(string_param_length_)); |
| 851 Definition* offset_def = LoadLocal(current_position_); |
| 852 BranchOrBacktrack(Comparison(kEQ, neg_len_def, offset_def), |
| 853 on_at_start); |
| 854 |
| 855 BindBlock(¬_at_start); |
| 856 } |
| 857 |
| 858 |
| 859 void IRRegExpMacroAssembler::CheckNotAtStart(BlockLabel* on_not_at_start) { |
| 860 TAG(); |
| 861 |
| 862 // Did we start the match at the start of the string at all? |
| 863 BranchOrBacktrack(Comparison(kNE, |
| 864 LoadLocal(start_index_param_), |
| 865 Uint64Constant(0)), |
| 866 on_not_at_start); |
| 867 |
| 868 // If we did, are we still at the start of the input, i.e. is |
| 869 // (offset == string_length * -1)? |
| 870 Definition* neg_len_def = |
| 871 InstanceCall(InstanceCallDescriptor::FromToken(Token::kNEGATE), |
| 872 PushLocal(string_param_length_)); |
| 873 Definition* offset_def = LoadLocal(current_position_); |
| 874 BranchOrBacktrack(Comparison(kNE, neg_len_def, offset_def), |
| 875 on_not_at_start); |
| 876 } |
| 877 |
| 878 |
| 879 void IRRegExpMacroAssembler::CheckCharacterLT(uint16_t limit, |
| 880 BlockLabel* on_less) { |
| 881 TAG(); |
| 882 BranchOrBacktrack(Comparison(kLT, |
| 883 LoadLocal(current_character_), |
| 884 Uint64Constant(limit)), |
| 885 on_less); |
| 886 } |
| 887 |
| 888 |
| 889 void IRRegExpMacroAssembler::CheckGreedyLoop(BlockLabel* on_equal) { |
| 890 TAG(); |
| 891 |
| 892 BlockLabel fallthrough; |
| 893 |
| 894 PushArgumentInstr* stack_push = PushLocal(stack_); |
| 895 Definition* stack_tip_def = InstanceCall( |
| 896 InstanceCallDescriptor(String::ZoneHandle( |
| 897 I, Field::GetterSymbol(Symbols::last()))), |
| 898 stack_push); |
| 899 Definition* cur_pos_def = LoadLocal(current_position_); |
| 900 |
| 901 BranchOrBacktrack(Comparison(kNE, stack_tip_def, cur_pos_def), |
| 902 &fallthrough); |
| 903 |
| 904 // Pop, throwing away the value. |
| 905 stack_push = PushLocal(stack_); |
| 906 Do(InstanceCall(InstanceCallDescriptor(Symbols::removeLast()), |
| 907 stack_push)); |
| 908 |
| 909 BranchOrBacktrack(NULL, on_equal); |
| 910 |
| 911 BindBlock(&fallthrough); |
| 912 } |
| 913 |
| 914 |
| 915 void IRRegExpMacroAssembler::CheckNotBackReferenceIgnoreCase( |
| 916 intptr_t start_reg, |
| 917 BlockLabel* on_no_match) { |
| 918 TAG(); |
| 919 ASSERT(start_reg + 1 <= position_registers_.length()); |
| 920 |
| 921 BlockLabel fallthrough; |
| 922 |
| 923 PushArgumentInstr* end_push = PushLocal(position_register(start_reg + 1)); |
| 924 PushArgumentInstr* start_push = PushLocal(position_register(start_reg)); |
| 925 StoreLocal(capture_length_, Bind(Sub(end_push, start_push))); |
| 926 |
| 927 // The length of a capture should not be negative. This can only happen |
| 928 // if the end of the capture is unrecorded, or at a point earlier than |
| 929 // the start of the capture. |
| 930 // BranchOrBacktrack(less, on_no_match); |
| 931 |
| 932 BranchOrBacktrack(Comparison(kLT, |
| 933 LoadLocal(capture_length_), |
| 934 Uint64Constant(0)), |
| 935 on_no_match); |
| 936 |
| 937 // If length is zero, either the capture is empty or it is completely |
| 938 // uncaptured. In either case succeed immediately. |
| 939 BranchOrBacktrack(Comparison(kEQ, |
| 940 LoadLocal(capture_length_), |
| 941 Uint64Constant(0)), |
| 942 &fallthrough); |
| 943 |
| 944 |
| 945 // Check that there are sufficient characters left in the input. |
| 946 PushArgumentInstr* pos_push = PushLocal(current_position_); |
| 947 PushArgumentInstr* len_push = PushLocal(capture_length_); |
| 948 BranchOrBacktrack( |
| 949 Comparison(kGT, |
| 950 InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD), |
| 951 pos_push, |
| 952 len_push), |
| 953 Uint64Constant(0)), |
| 954 on_no_match); |
| 955 |
| 956 pos_push = PushLocal(current_position_); |
| 957 len_push = PushLocal(string_param_length_); |
| 958 StoreLocal(match_start_index_, Bind(Add(pos_push, len_push))); |
| 959 |
| 960 pos_push = PushLocal(position_register(start_reg)); |
| 961 len_push = PushLocal(string_param_length_); |
| 962 StoreLocal(capture_start_index_, Bind(Add(pos_push, len_push))); |
| 963 |
| 964 pos_push = PushLocal(match_start_index_); |
| 965 len_push = PushLocal(capture_length_); |
| 966 StoreLocal(match_end_index_, Bind(Add(pos_push, len_push))); |
| 967 |
| 968 BlockLabel success; |
| 969 if (mode_ == ASCII) { |
| 970 BlockLabel loop_increment; |
| 971 BlockLabel loop; |
| 972 BindBlock(&loop); |
| 973 |
| 974 StoreLocal(char_in_capture_, CharacterAt(LoadLocal(capture_start_index_))); |
| 975 StoreLocal(char_in_match_, CharacterAt(LoadLocal(match_start_index_))); |
| 976 |
| 977 BranchOrBacktrack(Comparison(kEQ, |
| 978 LoadLocal(char_in_capture_), |
| 979 LoadLocal(char_in_match_)), |
| 980 &loop_increment); |
| 981 |
| 982 // Mismatch, try case-insensitive match (converting letters to lower-case). |
| 983 PushArgumentInstr* match_char_push = PushLocal(char_in_match_); |
| 984 PushArgumentInstr* mask_push = PushArgument(Bind(Uint64Constant(0x20))); |
| 985 StoreLocal(char_in_match_, |
| 986 Bind(InstanceCall( |
| 987 InstanceCallDescriptor::FromToken(Token::kBIT_OR), |
| 988 match_char_push, |
| 989 mask_push))); |
| 990 |
| 991 BlockLabel convert_capture; |
| 992 BlockLabel on_not_in_range; |
| 993 BranchOrBacktrack(Comparison(kLT, |
| 994 LoadLocal(char_in_match_), |
| 995 Uint64Constant('a')), |
| 996 &on_not_in_range); |
| 997 BranchOrBacktrack(Comparison(kGT, |
| 998 LoadLocal(char_in_match_), |
| 999 Uint64Constant('z')), |
| 1000 &on_not_in_range); |
| 1001 GoTo(&convert_capture); |
| 1002 BindBlock(&on_not_in_range); |
| 1003 |
| 1004 // Latin-1: Check for values in range [224,254] but not 247. |
| 1005 BranchOrBacktrack(Comparison(kLT, |
| 1006 LoadLocal(char_in_match_), |
| 1007 Uint64Constant(224)), |
| 1008 on_no_match); |
| 1009 BranchOrBacktrack(Comparison(kGT, |
| 1010 LoadLocal(char_in_match_), |
| 1011 Uint64Constant(254)), |
| 1012 on_no_match); |
| 1013 |
| 1014 BranchOrBacktrack(Comparison(kEQ, |
| 1015 LoadLocal(char_in_match_), |
| 1016 Uint64Constant(247)), |
| 1017 on_no_match); |
| 1018 |
| 1019 // Also convert capture character. |
| 1020 BindBlock(&convert_capture); |
| 1021 |
| 1022 PushArgumentInstr* capture_char_push = PushLocal(char_in_capture_); |
| 1023 mask_push = PushArgument(Bind(Uint64Constant(0x20))); |
| 1024 StoreLocal(char_in_capture_, |
| 1025 Bind(InstanceCall( |
| 1026 InstanceCallDescriptor::FromToken(Token::kBIT_OR), |
| 1027 capture_char_push, |
| 1028 mask_push))); |
| 1029 |
| 1030 BranchOrBacktrack(Comparison(kNE, |
| 1031 LoadLocal(char_in_match_), |
| 1032 LoadLocal(char_in_capture_)), |
| 1033 on_no_match); |
| 1034 |
| 1035 BindBlock(&loop_increment); |
| 1036 |
| 1037 // Increment pointers into match and capture strings. |
| 1038 StoreLocal(capture_start_index_, Bind(Add( |
| 1039 PushLocal(capture_start_index_), |
| 1040 PushArgument(Bind(Uint64Constant(1)))))); |
| 1041 StoreLocal(match_start_index_, Bind(Add( |
| 1042 PushLocal(match_start_index_), |
| 1043 PushArgument(Bind(Uint64Constant(1)))))); |
| 1044 |
| 1045 // Compare to end of match, and loop if not done. |
| 1046 BranchOrBacktrack(Comparison(kLT, |
| 1047 LoadLocal(match_start_index_), |
| 1048 LoadLocal(match_end_index_)), |
| 1049 &loop); |
| 1050 } else { |
| 1051 ASSERT(mode_ == UC16); |
| 1052 |
| 1053 Value* string_value = Bind(LoadLocal(string_param_)); |
| 1054 Value* lhs_index_value = Bind(LoadLocal(match_start_index_)); |
| 1055 Value* rhs_index_value = Bind(LoadLocal(capture_start_index_)); |
| 1056 Value* length_value = Bind(LoadLocal(capture_length_)); |
| 1057 |
| 1058 Definition* is_match_def = |
| 1059 new(I) CaseInsensitiveCompareUC16Instr( |
| 1060 string_value, |
| 1061 lhs_index_value, |
| 1062 rhs_index_value, |
| 1063 length_value, |
| 1064 specialization_cid_); |
| 1065 |
| 1066 BranchOrBacktrack(Comparison(kNE, is_match_def, BoolConstant(true)), |
| 1067 on_no_match); |
| 1068 } |
| 1069 |
| 1070 BindBlock(&success); |
| 1071 |
| 1072 // Move current character position to position after match. |
| 1073 PushArgumentInstr* match_end_push = PushLocal(match_end_index_); |
| 1074 len_push = PushLocal(string_param_length_); |
| 1075 StoreLocal(current_position_, Bind(Sub(match_end_push, len_push))); |
| 1076 |
| 1077 BindBlock(&fallthrough); |
| 1078 } |
| 1079 |
| 1080 |
| 1081 void IRRegExpMacroAssembler::CheckNotBackReference( |
| 1082 intptr_t start_reg, |
| 1083 BlockLabel* on_no_match) { |
| 1084 TAG(); |
| 1085 ASSERT(start_reg + 1 <= position_registers_.length()); |
| 1086 |
| 1087 BlockLabel fallthrough; |
| 1088 BlockLabel success; |
| 1089 |
| 1090 // Find length of back-referenced capture. |
| 1091 PushArgumentInstr* end_push = PushLocal(position_register(start_reg + 1)); |
| 1092 PushArgumentInstr* start_push = PushLocal(position_register(start_reg)); |
| 1093 StoreLocal(capture_length_, Bind(Sub(end_push, start_push))); |
| 1094 |
| 1095 // Fail on partial or illegal capture (start of capture after end of capture). |
| 1096 BranchOrBacktrack(Comparison(kLT, |
| 1097 LoadLocal(capture_length_), |
| 1098 Uint64Constant(0)), |
| 1099 on_no_match); |
| 1100 |
| 1101 // Succeed on empty capture (including no capture) |
| 1102 BranchOrBacktrack(Comparison(kEQ, |
| 1103 LoadLocal(capture_length_), |
| 1104 Uint64Constant(0)), |
| 1105 &fallthrough); |
| 1106 |
| 1107 // Check that there are sufficient characters left in the input. |
| 1108 PushArgumentInstr* pos_push = PushLocal(current_position_); |
| 1109 PushArgumentInstr* len_push = PushLocal(capture_length_); |
| 1110 BranchOrBacktrack( |
| 1111 Comparison(kGT, |
| 1112 InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD), |
| 1113 pos_push, |
| 1114 len_push), |
| 1115 Uint64Constant(0)), |
| 1116 on_no_match); |
| 1117 |
| 1118 // Compute pointers to match string and capture string. |
| 1119 pos_push = PushLocal(current_position_); |
| 1120 len_push = PushLocal(string_param_length_); |
| 1121 StoreLocal(match_start_index_, Bind(Add(pos_push, len_push))); |
| 1122 |
| 1123 pos_push = PushLocal(position_register(start_reg)); |
| 1124 len_push = PushLocal(string_param_length_); |
| 1125 StoreLocal(capture_start_index_, Bind(Add(pos_push, len_push))); |
| 1126 |
| 1127 pos_push = PushLocal(match_start_index_); |
| 1128 len_push = PushLocal(capture_length_); |
| 1129 StoreLocal(match_end_index_, Bind(Add(pos_push, len_push))); |
| 1130 |
| 1131 BlockLabel loop; |
| 1132 BindBlock(&loop); |
| 1133 |
| 1134 StoreLocal(char_in_capture_, CharacterAt(LoadLocal(capture_start_index_))); |
| 1135 StoreLocal(char_in_match_, CharacterAt(LoadLocal(match_start_index_))); |
| 1136 |
| 1137 BranchOrBacktrack(Comparison(kNE, |
| 1138 LoadLocal(char_in_capture_), |
| 1139 LoadLocal(char_in_match_)), |
| 1140 on_no_match); |
| 1141 |
| 1142 // Increment pointers into capture and match string. |
| 1143 StoreLocal(capture_start_index_, Bind(Add( |
| 1144 PushLocal(capture_start_index_), |
| 1145 PushArgument(Bind(Uint64Constant(1)))))); |
| 1146 StoreLocal(match_start_index_, Bind(Add( |
| 1147 PushLocal(match_start_index_), |
| 1148 PushArgument(Bind(Uint64Constant(1)))))); |
| 1149 |
| 1150 // Check if we have reached end of match area. |
| 1151 BranchOrBacktrack(Comparison(kLT, |
| 1152 LoadLocal(match_start_index_), |
| 1153 LoadLocal(match_end_index_)), |
| 1154 &loop); |
| 1155 |
| 1156 BindBlock(&success); |
| 1157 |
| 1158 // Move current character position to position after match. |
| 1159 PushArgumentInstr* match_end_push = PushLocal(match_end_index_); |
| 1160 len_push = PushLocal(string_param_length_); |
| 1161 StoreLocal(current_position_, Bind(Sub(match_end_push, len_push))); |
| 1162 |
| 1163 BindBlock(&fallthrough); |
| 1164 } |
| 1165 |
| 1166 |
| 1167 void IRRegExpMacroAssembler::CheckNotCharacter(uint32_t c, |
| 1168 BlockLabel* on_not_equal) { |
| 1169 TAG(); |
| 1170 BranchOrBacktrack(Comparison(kNE, |
| 1171 LoadLocal(current_character_), |
| 1172 Uint64Constant(c)), |
| 1173 on_not_equal); |
| 1174 } |
| 1175 |
| 1176 |
| 1177 void IRRegExpMacroAssembler::CheckCharacterAfterAnd(uint32_t c, |
| 1178 uint32_t mask, |
| 1179 BlockLabel* on_equal) { |
| 1180 TAG(); |
| 1181 |
| 1182 Definition* actual_def = LoadLocal(current_character_); |
| 1183 Definition* expected_def = Uint64Constant(c); |
| 1184 |
| 1185 PushArgumentInstr* actual_push = PushArgument(Bind(actual_def)); |
| 1186 PushArgumentInstr* mask_push = PushArgument(Bind(Uint64Constant(mask))); |
| 1187 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND), |
| 1188 actual_push, |
| 1189 mask_push); |
| 1190 |
| 1191 BranchOrBacktrack(Comparison(kEQ, actual_def, expected_def), on_equal); |
| 1192 } |
| 1193 |
| 1194 |
| 1195 void IRRegExpMacroAssembler::CheckNotCharacterAfterAnd( |
| 1196 uint32_t c, |
| 1197 uint32_t mask, |
| 1198 BlockLabel* on_not_equal) { |
| 1199 TAG(); |
| 1200 |
| 1201 Definition* actual_def = LoadLocal(current_character_); |
| 1202 Definition* expected_def = Uint64Constant(c); |
| 1203 |
| 1204 PushArgumentInstr* actual_push = PushArgument(Bind(actual_def)); |
| 1205 PushArgumentInstr* mask_push = PushArgument(Bind(Uint64Constant(mask))); |
| 1206 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND), |
| 1207 actual_push, |
| 1208 mask_push); |
| 1209 |
| 1210 BranchOrBacktrack(Comparison(kNE, actual_def, expected_def), on_not_equal); |
| 1211 } |
| 1212 |
| 1213 |
| 1214 void IRRegExpMacroAssembler::CheckNotCharacterAfterMinusAnd( |
| 1215 uint16_t c, |
| 1216 uint16_t minus, |
| 1217 uint16_t mask, |
| 1218 BlockLabel* on_not_equal) { |
| 1219 TAG(); |
| 1220 ASSERT(minus < Utf16::kMaxCodeUnit); // NOLINT |
| 1221 |
| 1222 Definition* actual_def = LoadLocal(current_character_); |
| 1223 Definition* expected_def = Uint64Constant(c); |
| 1224 |
| 1225 PushArgumentInstr* actual_push = PushArgument(Bind(actual_def)); |
| 1226 PushArgumentInstr* minus_push = PushArgument(Bind(Uint64Constant(minus))); |
| 1227 |
| 1228 actual_push = PushArgument(Bind(Sub(actual_push, minus_push))); |
| 1229 PushArgumentInstr* mask_push = PushArgument(Bind(Uint64Constant(mask))); |
| 1230 actual_def = InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND), |
| 1231 actual_push, |
| 1232 mask_push); |
| 1233 |
| 1234 BranchOrBacktrack(Comparison(kNE, actual_def, expected_def), on_not_equal); |
| 1235 } |
| 1236 |
| 1237 |
| 1238 void IRRegExpMacroAssembler::CheckCharacterInRange( |
| 1239 uint16_t from, |
| 1240 uint16_t to, |
| 1241 BlockLabel* on_in_range) { |
| 1242 TAG(); |
| 1243 ASSERT(from <= to); |
| 1244 |
| 1245 // TODO(jgruber): All range comparisons could be done cheaper with unsigned |
| 1246 // compares. This pattern repeats in various places. |
| 1247 |
| 1248 BlockLabel on_not_in_range; |
| 1249 BranchOrBacktrack(Comparison(kLT, |
| 1250 LoadLocal(current_character_), |
| 1251 Uint64Constant(from)), |
| 1252 &on_not_in_range); |
| 1253 BranchOrBacktrack(Comparison(kGT, |
| 1254 LoadLocal(current_character_), |
| 1255 Uint64Constant(to)), |
| 1256 &on_not_in_range); |
| 1257 BranchOrBacktrack(NULL, on_in_range); |
| 1258 |
| 1259 BindBlock(&on_not_in_range); |
| 1260 } |
| 1261 |
| 1262 |
| 1263 void IRRegExpMacroAssembler::CheckCharacterNotInRange( |
| 1264 uint16_t from, |
| 1265 uint16_t to, |
| 1266 BlockLabel* on_not_in_range) { |
| 1267 TAG(); |
| 1268 ASSERT(from <= to); |
| 1269 |
| 1270 BranchOrBacktrack(Comparison(kLT, |
| 1271 LoadLocal(current_character_), |
| 1272 Uint64Constant(from)), |
| 1273 on_not_in_range); |
| 1274 |
| 1275 BranchOrBacktrack(Comparison(kGT, |
| 1276 LoadLocal(current_character_), |
| 1277 Uint64Constant(to)), |
| 1278 on_not_in_range); |
| 1279 } |
| 1280 |
| 1281 |
| 1282 void IRRegExpMacroAssembler::CheckBitInTable( |
| 1283 const TypedData& table, |
| 1284 BlockLabel* on_bit_set) { |
| 1285 TAG(); |
| 1286 |
| 1287 PushArgumentInstr* table_push = |
| 1288 PushArgument(Bind(new(I) ConstantInstr(table))); |
| 1289 PushArgumentInstr* index_push = PushLocal(current_character_); |
| 1290 |
| 1291 if (mode_ != ASCII || kTableMask != Symbols::kMaxOneCharCodeSymbol) { |
| 1292 PushArgumentInstr* mask_push = |
| 1293 PushArgument(Bind(Uint64Constant(kTableSize - 1))); |
| 1294 index_push = PushArgument( |
| 1295 Bind(InstanceCall(InstanceCallDescriptor::FromToken(Token::kBIT_AND), |
| 1296 index_push, |
| 1297 mask_push))); |
| 1298 } |
| 1299 |
| 1300 Definition* byte_def = |
| 1301 InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX), |
| 1302 table_push, |
| 1303 index_push); |
| 1304 Definition* zero_def = Int64Constant(0); |
| 1305 |
| 1306 BranchOrBacktrack(Comparison(kNE, byte_def, zero_def), on_bit_set); |
| 1307 } |
| 1308 |
| 1309 |
| 1310 bool IRRegExpMacroAssembler::CheckSpecialCharacterClass( |
| 1311 uint16_t type, |
| 1312 BlockLabel* on_no_match) { |
| 1313 TAG(); |
| 1314 |
| 1315 // Range checks (c in min..max) are generally implemented by an unsigned |
| 1316 // (c - min) <= (max - min) check |
| 1317 switch (type) { |
| 1318 case 's': |
| 1319 // Match space-characters |
| 1320 if (mode_ == ASCII) { |
| 1321 // One byte space characters are '\t'..'\r', ' ' and \u00a0. |
| 1322 BlockLabel success; |
| 1323 // Space (' '). |
| 1324 BranchOrBacktrack(Comparison(kEQ, |
| 1325 LoadLocal(current_character_), |
| 1326 Uint64Constant(' ')), |
| 1327 &success); |
| 1328 // Check range 0x09..0x0d. |
| 1329 CheckCharacterInRange('\t', '\r', &success); |
| 1330 // \u00a0 (NBSP). |
| 1331 BranchOrBacktrack(Comparison(kNE, |
| 1332 LoadLocal(current_character_), |
| 1333 Uint64Constant(0x00a0)), |
| 1334 on_no_match); |
| 1335 BindBlock(&success); |
| 1336 return true; |
| 1337 } |
| 1338 return false; |
| 1339 case 'S': |
| 1340 // The emitted code for generic character classes is good enough. |
| 1341 return false; |
| 1342 case 'd': |
| 1343 // Match ASCII digits ('0'..'9') |
| 1344 CheckCharacterNotInRange('0', '9', on_no_match); |
| 1345 return true; |
| 1346 case 'D': |
| 1347 // Match non ASCII-digits |
| 1348 CheckCharacterInRange('0', '9', on_no_match); |
| 1349 return true; |
| 1350 case '.': { |
| 1351 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029) |
| 1352 BranchOrBacktrack(Comparison(kEQ, |
| 1353 LoadLocal(current_character_), |
| 1354 Uint64Constant('\n')), |
| 1355 on_no_match); |
| 1356 BranchOrBacktrack(Comparison(kEQ, |
| 1357 LoadLocal(current_character_), |
| 1358 Uint64Constant('\r')), |
| 1359 on_no_match); |
| 1360 if (mode_ == UC16) { |
| 1361 BranchOrBacktrack(Comparison(kEQ, |
| 1362 LoadLocal(current_character_), |
| 1363 Uint64Constant(0x2028)), |
| 1364 on_no_match); |
| 1365 BranchOrBacktrack(Comparison(kEQ, |
| 1366 LoadLocal(current_character_), |
| 1367 Uint64Constant(0x2029)), |
| 1368 on_no_match); |
| 1369 } |
| 1370 return true; |
| 1371 } |
| 1372 case 'w': { |
| 1373 if (mode_ != ASCII) { |
| 1374 // Table is 128 entries, so all ASCII characters can be tested. |
| 1375 BranchOrBacktrack(Comparison(kGT, |
| 1376 LoadLocal(current_character_), |
| 1377 Uint64Constant('z')), |
| 1378 on_no_match); |
| 1379 } |
| 1380 |
| 1381 PushArgumentInstr* table_push = |
| 1382 PushArgument(Bind(WordCharacterMapConstant())); |
| 1383 PushArgumentInstr* index_push = PushLocal(current_character_); |
| 1384 |
| 1385 Definition* byte_def = |
| 1386 InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX), |
| 1387 table_push, |
| 1388 index_push); |
| 1389 Definition* zero_def = Int64Constant(0); |
| 1390 |
| 1391 BranchOrBacktrack(Comparison(kEQ, byte_def, zero_def), on_no_match); |
| 1392 |
| 1393 return true; |
| 1394 } |
| 1395 case 'W': { |
| 1396 BlockLabel done; |
| 1397 if (mode_ != ASCII) { |
| 1398 // Table is 128 entries, so all ASCII characters can be tested. |
| 1399 BranchOrBacktrack(Comparison(kGT, |
| 1400 LoadLocal(current_character_), |
| 1401 Uint64Constant('z')), |
| 1402 &done); |
| 1403 } |
| 1404 |
| 1405 // TODO(jgruber): Refactor to use CheckBitInTable if possible. |
| 1406 |
| 1407 PushArgumentInstr* table_push = |
| 1408 PushArgument(Bind(WordCharacterMapConstant())); |
| 1409 PushArgumentInstr* index_push = PushLocal(current_character_); |
| 1410 |
| 1411 Definition* byte_def = |
| 1412 InstanceCall(InstanceCallDescriptor::FromToken(Token::kINDEX), |
| 1413 table_push, |
| 1414 index_push); |
| 1415 Definition* zero_def = Int64Constant(0); |
| 1416 |
| 1417 BranchOrBacktrack(Comparison(kNE, byte_def, zero_def), on_no_match); |
| 1418 |
| 1419 if (mode_ != ASCII) { |
| 1420 BindBlock(&done); |
| 1421 } |
| 1422 return true; |
| 1423 } |
| 1424 // Non-standard classes (with no syntactic shorthand) used internally. |
| 1425 case '*': |
| 1426 // Match any character. |
| 1427 return true; |
| 1428 case 'n': { |
| 1429 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 or 0x2029). |
| 1430 // The opposite of '.'. |
| 1431 BlockLabel success; |
| 1432 BranchOrBacktrack(Comparison(kEQ, |
| 1433 LoadLocal(current_character_), |
| 1434 Uint64Constant('\n')), |
| 1435 &success); |
| 1436 BranchOrBacktrack(Comparison(kEQ, |
| 1437 LoadLocal(current_character_), |
| 1438 Uint64Constant('\r')), |
| 1439 &success); |
| 1440 if (mode_ == UC16) { |
| 1441 BranchOrBacktrack(Comparison(kEQ, |
| 1442 LoadLocal(current_character_), |
| 1443 Uint64Constant(0x2028)), |
| 1444 &success); |
| 1445 BranchOrBacktrack(Comparison(kEQ, |
| 1446 LoadLocal(current_character_), |
| 1447 Uint64Constant(0x2029)), |
| 1448 &success); |
| 1449 } |
| 1450 BranchOrBacktrack(NULL, on_no_match); |
| 1451 BindBlock(&success); |
| 1452 return true; |
| 1453 } |
| 1454 // No custom implementation (yet): s(uint16_t), S(uint16_t). |
| 1455 default: |
| 1456 return false; |
| 1457 } |
| 1458 } |
| 1459 |
| 1460 |
| 1461 void IRRegExpMacroAssembler::Fail() { |
| 1462 TAG(); |
| 1463 ASSERT(FAILURE == 0); // Return value for failure is zero. |
| 1464 if (!global()) { |
| 1465 UNREACHABLE(); // Dart regexps are always global. |
| 1466 } |
| 1467 GoTo(exit_block_); |
| 1468 } |
| 1469 |
| 1470 |
| 1471 void IRRegExpMacroAssembler::IfRegisterGE(intptr_t reg, |
| 1472 intptr_t comparand, |
| 1473 BlockLabel* if_ge) { |
| 1474 TAG(); |
| 1475 BranchOrBacktrack(Comparison(kGTE, |
| 1476 LoadLocal(position_register(reg)), |
| 1477 Int64Constant(comparand)), |
| 1478 if_ge); |
| 1479 } |
| 1480 |
| 1481 |
| 1482 void IRRegExpMacroAssembler::IfRegisterLT(intptr_t reg, |
| 1483 intptr_t comparand, |
| 1484 BlockLabel* if_lt) { |
| 1485 TAG(); |
| 1486 BranchOrBacktrack(Comparison(kLT, |
| 1487 LoadLocal(position_register(reg)), |
| 1488 Int64Constant(comparand)), |
| 1489 if_lt); |
| 1490 } |
| 1491 |
| 1492 |
| 1493 void IRRegExpMacroAssembler::IfRegisterEqPos(intptr_t reg, |
| 1494 BlockLabel* if_eq) { |
| 1495 TAG(); |
| 1496 BranchOrBacktrack(Comparison(kEQ, |
| 1497 LoadLocal(position_register(reg)), |
| 1498 LoadLocal(current_position_)), |
| 1499 if_eq); |
| 1500 } |
| 1501 |
| 1502 |
| 1503 RegExpMacroAssembler::IrregexpImplementation |
| 1504 IRRegExpMacroAssembler::Implementation() { |
| 1505 return kIRImplementation; |
| 1506 } |
| 1507 |
| 1508 |
| 1509 void IRRegExpMacroAssembler::LoadCurrentCharacter(intptr_t cp_offset, |
| 1510 BlockLabel* on_end_of_input, |
| 1511 bool check_bounds, |
| 1512 intptr_t characters) { |
| 1513 TAG(); |
| 1514 ASSERT(cp_offset >= -1); // ^ and \b can look behind one character. |
| 1515 ASSERT(cp_offset < (1<<30)); // Be sane! (And ensure negation works) |
| 1516 if (check_bounds) { |
| 1517 CheckPosition(cp_offset + characters - 1, on_end_of_input); |
| 1518 } |
| 1519 LoadCurrentCharacterUnchecked(cp_offset, characters); |
| 1520 } |
| 1521 |
| 1522 |
| 1523 void IRRegExpMacroAssembler::PopCurrentPosition() { |
| 1524 TAG(); |
| 1525 StoreLocal(current_position_, PopStack()); |
| 1526 } |
| 1527 |
| 1528 |
| 1529 void IRRegExpMacroAssembler::PopRegister(intptr_t register_index) { |
| 1530 TAG(); |
| 1531 ASSERT(register_index < position_registers_.length()); |
| 1532 StoreLocal(position_register(register_index), PopStack()); |
| 1533 } |
| 1534 |
| 1535 |
| 1536 void IRRegExpMacroAssembler::PushStack(Definition *definition) { |
| 1537 PushArgumentInstr* stack_push = PushLocal(stack_); |
| 1538 PushArgumentInstr* value_push = PushArgument(Bind(definition)); |
| 1539 Do(InstanceCall(InstanceCallDescriptor(Symbols::add()), |
| 1540 stack_push, |
| 1541 value_push)); |
| 1542 } |
| 1543 |
| 1544 |
| 1545 Value* IRRegExpMacroAssembler::PopStack() { |
| 1546 PushArgumentInstr* stack_push = PushLocal(stack_); |
| 1547 return Bind(InstanceCall(InstanceCallDescriptor(Symbols::removeLast()), |
| 1548 stack_push)); |
| 1549 } |
| 1550 |
| 1551 |
| 1552 // Pushes the location corresponding to label to the backtracking stack. |
| 1553 void IRRegExpMacroAssembler::PushBacktrack(BlockLabel* label) { |
| 1554 TAG(); |
| 1555 |
| 1556 // Ensure that targets of indirect jumps are never accessed through a |
| 1557 // normal control flow instructions by creating a new block for each backtrack |
| 1558 // target. |
| 1559 IndirectEntryInstr* indirect_target = IndirectWithJoinGoto(label->block()); |
| 1560 |
| 1561 // Add a fake edge from the graph entry for data flow analysis. |
| 1562 entry_block_->AddIndirectEntry(indirect_target); |
| 1563 |
| 1564 ConstantInstr* offset = Uint64Constant(indirect_target->indirect_id()); |
| 1565 PushStack(offset); |
| 1566 } |
| 1567 |
| 1568 |
| 1569 void IRRegExpMacroAssembler::PushCurrentPosition() { |
| 1570 TAG(); |
| 1571 PushStack(LoadLocal(current_position_)); |
| 1572 } |
| 1573 |
| 1574 |
| 1575 void IRRegExpMacroAssembler::PushRegister(intptr_t register_index) { |
| 1576 TAG(); |
| 1577 PushStack(LoadLocal(position_register(register_index))); |
| 1578 } |
| 1579 |
| 1580 |
| 1581 void IRRegExpMacroAssembler::ReadCurrentPositionFromRegister(intptr_t reg) { |
| 1582 TAG(); |
| 1583 StoreLocal(current_position_, Bind(LoadLocal(position_register(reg)))); |
| 1584 } |
| 1585 |
| 1586 // Resets the size of the stack to the value stored in reg. |
| 1587 void IRRegExpMacroAssembler::ReadStackPointerFromRegister(intptr_t reg) { |
| 1588 TAG(); |
| 1589 ASSERT(reg < position_registers_.length()); |
| 1590 |
| 1591 PushArgumentInstr* stack_push = PushLocal(stack_); |
| 1592 PushArgumentInstr* length_push = PushLocal(position_register(reg)); |
| 1593 |
| 1594 Do(InstanceCall(InstanceCallDescriptor( |
| 1595 String::ZoneHandle( |
| 1596 I, Field::SetterSymbol(Symbols::Length()))), |
| 1597 stack_push, |
| 1598 length_push)); |
| 1599 } |
| 1600 |
| 1601 void IRRegExpMacroAssembler::SetCurrentPositionFromEnd(intptr_t by) { |
| 1602 TAG(); |
| 1603 |
| 1604 BlockLabel after_position; |
| 1605 |
| 1606 Definition* cur_pos_def = LoadLocal(current_position_); |
| 1607 Definition* by_value_def = Int64Constant(-by); |
| 1608 |
| 1609 BranchOrBacktrack(Comparison(kGTE, cur_pos_def, by_value_def), |
| 1610 &after_position); |
| 1611 |
| 1612 StoreLocal(current_position_, Bind(Int64Constant(-by))); |
| 1613 |
| 1614 // On RegExp code entry (where this operation is used), the character before |
| 1615 // the current position is expected to be already loaded. |
| 1616 // We have advanced the position, so it's safe to read backwards. |
| 1617 LoadCurrentCharacterUnchecked(-1, 1); |
| 1618 |
| 1619 BindBlock(&after_position); |
| 1620 } |
| 1621 |
| 1622 |
| 1623 void IRRegExpMacroAssembler::SetRegister(intptr_t register_index, intptr_t to) { |
| 1624 TAG(); |
| 1625 // Reserved for positions! |
| 1626 ASSERT(register_index >= position_registers_count_); |
| 1627 StoreLocal(position_register(register_index), Bind(Int64Constant(to))); |
| 1628 } |
| 1629 |
| 1630 |
| 1631 bool IRRegExpMacroAssembler::Succeed() { |
| 1632 TAG(); |
| 1633 GoTo(success_block_); |
| 1634 return global(); |
| 1635 } |
| 1636 |
| 1637 |
| 1638 void IRRegExpMacroAssembler::WriteCurrentPositionToRegister( |
| 1639 intptr_t reg, intptr_t cp_offset) { |
| 1640 TAG(); |
| 1641 |
| 1642 PushArgumentInstr* pos_push = PushLocal(current_position_); |
| 1643 PushArgumentInstr* off_push = |
| 1644 PushArgument(Bind(Int64Constant(cp_offset))); |
| 1645 |
| 1646 // Push the negative offset; these are converted to positive string positions |
| 1647 // within the success block. |
| 1648 StoreLocal(position_register(reg), Bind(Add(pos_push, off_push))); |
| 1649 } |
| 1650 |
| 1651 |
| 1652 void IRRegExpMacroAssembler::ClearRegisters( |
| 1653 intptr_t reg_from, intptr_t reg_to) { |
| 1654 TAG(); |
| 1655 |
| 1656 ASSERT(reg_from <= reg_to); |
| 1657 ASSERT(reg_to < position_registers_.length()); |
| 1658 |
| 1659 // In order to clear registers to a final result value of -1, set them to |
| 1660 // (-1 - string length), the offset of -1 from the end of the string. |
| 1661 |
| 1662 for (intptr_t reg = reg_from; reg <= reg_to; reg++) { |
| 1663 PushArgumentInstr* minus_one_push = |
| 1664 PushArgument(Bind(Int64Constant(-1))); |
| 1665 PushArgumentInstr* length_push = PushLocal(string_param_length_); |
| 1666 |
| 1667 StoreLocal(position_register(reg), Bind(Sub(minus_one_push, length_push))); |
| 1668 } |
| 1669 } |
| 1670 |
| 1671 |
| 1672 void IRRegExpMacroAssembler::WriteStackPointerToRegister(intptr_t reg) { |
| 1673 TAG(); |
| 1674 |
| 1675 PushArgumentInstr* stack_push = PushLocal(stack_); |
| 1676 Value* length_value = |
| 1677 Bind(InstanceCall(InstanceCallDescriptor( |
| 1678 String::ZoneHandle( |
| 1679 I, Field::GetterSymbol(Symbols::Length()))), |
| 1680 stack_push)); |
| 1681 |
| 1682 StoreLocal(position_register(reg), length_value); |
| 1683 } |
| 1684 |
| 1685 |
| 1686 // Private methods: |
| 1687 |
| 1688 |
| 1689 void IRRegExpMacroAssembler::CheckPosition(intptr_t cp_offset, |
| 1690 BlockLabel* on_outside_input) { |
| 1691 TAG(); |
| 1692 Definition* curpos_def = LoadLocal(current_position_); |
| 1693 Definition* cp_off_def = Int64Constant(-cp_offset); |
| 1694 |
| 1695 // If (current_position_ < -cp_offset), we are in bounds. |
| 1696 // Remember, current_position_ is a negative offset from the string end. |
| 1697 |
| 1698 BranchOrBacktrack(Comparison(kGTE, curpos_def, cp_off_def), |
| 1699 on_outside_input); |
| 1700 } |
| 1701 |
| 1702 |
| 1703 void IRRegExpMacroAssembler::BranchOrBacktrack( |
| 1704 ComparisonInstr* comparison, |
| 1705 BlockLabel* true_successor) { |
| 1706 if (comparison == NULL) { // No condition |
| 1707 if (true_successor == NULL) { |
| 1708 Backtrack(); |
| 1709 return; |
| 1710 } |
| 1711 GoTo(true_successor); |
| 1712 return; |
| 1713 } |
| 1714 |
| 1715 // If no successor block has been passed in, backtrack. |
| 1716 JoinEntryInstr* true_successor_block = backtrack_block_; |
| 1717 if (true_successor != NULL) { |
| 1718 true_successor->SetLinked(); |
| 1719 true_successor_block = true_successor->block(); |
| 1720 } |
| 1721 ASSERT(true_successor_block != NULL); |
| 1722 |
| 1723 // If the condition is not true, fall through to a new block. |
| 1724 BlockLabel fallthrough; |
| 1725 |
| 1726 BranchInstr* branch = new(I) BranchInstr(comparison); |
| 1727 *branch->true_successor_address() = |
| 1728 TargetWithJoinGoto(true_successor_block); |
| 1729 *branch->false_successor_address() = |
| 1730 TargetWithJoinGoto(fallthrough.block()); |
| 1731 |
| 1732 CloseBlockWith(branch); |
| 1733 BindBlock(&fallthrough); |
| 1734 } |
| 1735 |
| 1736 |
| 1737 TargetEntryInstr* IRRegExpMacroAssembler::TargetWithJoinGoto( |
| 1738 JoinEntryInstr* dst) { |
| 1739 TargetEntryInstr* target = new(I) TargetEntryInstr( |
| 1740 block_id_.Alloc(), kInvalidTryIndex); |
| 1741 blocks_.Add(target); |
| 1742 |
| 1743 target->AppendInstruction(new(I) GotoInstr(dst)); |
| 1744 |
| 1745 return target; |
| 1746 } |
| 1747 |
| 1748 |
| 1749 IndirectEntryInstr* IRRegExpMacroAssembler::IndirectWithJoinGoto( |
| 1750 JoinEntryInstr* dst) { |
| 1751 IndirectEntryInstr* target = new(I) IndirectEntryInstr( |
| 1752 block_id_.Alloc(), indirect_id_.Alloc(), kInvalidTryIndex); |
| 1753 blocks_.Add(target); |
| 1754 |
| 1755 target->AppendInstruction(new(I) GotoInstr(dst)); |
| 1756 |
| 1757 return target; |
| 1758 } |
| 1759 |
| 1760 |
| 1761 void IRRegExpMacroAssembler::CheckPreemption() { |
| 1762 TAG(); |
| 1763 AppendInstruction(new(I) CheckStackOverflowInstr(kNoTokenPos, 0)); |
| 1764 } |
| 1765 |
| 1766 |
| 1767 Definition* IRRegExpMacroAssembler::Add( |
| 1768 PushArgumentInstr* lhs, |
| 1769 PushArgumentInstr* rhs) { |
| 1770 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kADD), lhs, rhs); |
| 1771 } |
| 1772 |
| 1773 |
| 1774 Definition* IRRegExpMacroAssembler::Sub( |
| 1775 PushArgumentInstr* lhs, |
| 1776 PushArgumentInstr* rhs) { |
| 1777 return InstanceCall(InstanceCallDescriptor::FromToken(Token::kSUB), lhs, rhs); |
| 1778 } |
| 1779 |
| 1780 |
| 1781 static const String& codeUnitsAtName(intptr_t characters) { |
| 1782 switch (characters) { |
| 1783 case 1: return Library::PrivateCoreLibName(Symbols::_oneCodeUnitAt()); |
| 1784 case 2: return Library::PrivateCoreLibName(Symbols::_twoCodeUnitsAt()); |
| 1785 case 4: return Library::PrivateCoreLibName(Symbols::_fourCodeUnitsAt()); |
| 1786 } |
| 1787 UNREACHABLE(); |
| 1788 return String::Handle(); |
| 1789 } |
| 1790 |
| 1791 |
| 1792 void IRRegExpMacroAssembler::LoadCurrentCharacterUnchecked( |
| 1793 intptr_t cp_offset, intptr_t characters) { |
| 1794 TAG(); |
| 1795 |
| 1796 if (mode_ == ASCII) { |
| 1797 ASSERT(characters == 1 || characters == 2 || characters == 4); |
| 1798 } else { |
| 1799 ASSERT(mode_ == UC16); |
| 1800 ASSERT(characters == 1 || characters == 2); |
| 1801 } |
| 1802 |
| 1803 // LoadLocal pattern_param_ |
| 1804 // PushArgument() |
| 1805 PushArgumentInstr* pattern_push = PushLocal(string_param_); |
| 1806 |
| 1807 // Calculate the addressed string index as |
| 1808 // cp_offset + current_position_ + string_param_length_ |
| 1809 PushArgumentInstr* cp_offset_push = |
| 1810 PushArgument(Bind(Int64Constant(cp_offset))); |
| 1811 PushArgumentInstr* cur_pos_push = PushLocal(current_position_); |
| 1812 |
| 1813 PushArgumentInstr* partial_sum_push = |
| 1814 PushArgument(Bind(Add(cp_offset_push, cur_pos_push))); |
| 1815 PushArgumentInstr* length_push = PushLocal(string_param_length_); |
| 1816 |
| 1817 PushArgumentInstr* pos_push = |
| 1818 PushArgument(Bind(Add(partial_sum_push, length_push))); |
| 1819 |
| 1820 // InstanceCall(codeUnitAt, t0, t0) |
| 1821 const String& name = codeUnitsAtName(characters); |
| 1822 Value* code_unit_value = |
| 1823 Bind(InstanceCall(InstanceCallDescriptor(name), |
| 1824 pattern_push, |
| 1825 pos_push)); |
| 1826 |
| 1827 // StoreLocal(current_character_) |
| 1828 StoreLocal(current_character_, code_unit_value); |
| 1829 |
| 1830 PRINT(PushLocal(current_character_)); |
| 1831 } |
| 1832 |
| 1833 |
| 1834 Value* IRRegExpMacroAssembler::CharacterAt(Definition* index) { |
| 1835 PushArgumentInstr* pattern_push = PushLocal(string_param_); |
| 1836 PushArgumentInstr* index_push = PushArgument(Bind(index)); |
| 1837 |
| 1838 return Bind(InstanceCall(InstanceCallDescriptor(codeUnitsAtName(1)), |
| 1839 pattern_push, |
| 1840 index_push)); |
| 1841 } |
| 1842 |
| 1843 |
| 1844 #undef __ |
| 1845 |
| 1846 } // namespace dart |
OLD | NEW |