// Copyright 2017 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_BUILTINS_BUILTINS_REGEXP_GEN_H_ #define V8_BUILTINS_BUILTINS_REGEXP_GEN_H_ #include "src/base/optional.h" #include "src/codegen/code-stub-assembler.h" #include "src/common/message-template.h" namespace v8 { namespace internal { class RegExpBuiltinsAssembler : public CodeStubAssembler { public: explicit RegExpBuiltinsAssembler(compiler::CodeAssemblerState* state) : CodeStubAssembler(state) {} // Create and initialize a RegExp object. TNode RegExpCreate(TNode context, TNode native_context, TNode regexp_string, TNode flags); TNode RegExpCreate(TNode context, TNode initial_map, TNode regexp_string, TNode flags); TNode IsRegExp(TNode context, TNode maybe_receiver); TNode SmiZero(); TNode IntPtrZero(); TNode LoadCodeObjectEntry(TNode code); // Allocate a RegExpResult with the given length (the number of captures, // including the match itself), index (the index where the match starts), // and input string. TNode AllocateRegExpResult( TNode context, TNode length, TNode index, TNode input, TNode* elements_out = nullptr); TNode FastLoadLastIndexBeforeSmiCheck(TNode regexp); TNode FastLoadLastIndex(TNode regexp) { return CAST(FastLoadLastIndexBeforeSmiCheck(regexp)); } TNode SlowLoadLastIndex(TNode context, TNode regexp); TNode LoadLastIndex(TNode context, TNode regexp, bool is_fastpath); void FastStoreLastIndex(TNode regexp, TNode value); void SlowStoreLastIndex(SloppyTNode context, SloppyTNode regexp, SloppyTNode value); void StoreLastIndex(TNode context, TNode regexp, TNode value, bool is_fastpath); // Loads {var_string_start} and {var_string_end} with the corresponding // offsets into the given {string_data}. void GetStringPointers(Node* const string_data, Node* const offset, Node* const last_index, Node* const string_length, String::Encoding encoding, Variable* var_string_start, Variable* var_string_end); // Low level logic around the actual call into pattern matching code. TNode RegExpExecInternal(TNode context, TNode regexp, TNode string, TNode last_index, TNode match_info); TNode ConstructNewResultFromMatchInfo( TNode context, TNode maybe_regexp, TNode match_info, TNode string); TNode RegExpPrototypeExecBodyWithoutResult( TNode context, TNode maybe_regexp, TNode string, Label* if_didnotmatch, const bool is_fastpath); TNode RegExpPrototypeExecBodyWithoutResultFast( TNode context, TNode maybe_regexp, TNode string, Label* if_didnotmatch); TNode RegExpPrototypeExecBody(TNode context, TNode maybe_regexp, TNode string, const bool is_fastpath); TNode IsReceiverInitialRegExpPrototype(SloppyTNode context, SloppyTNode receiver); // Fast path check logic. // // Are you afraid? If not, you should be. // // It's complicated. Fast path checks protect certain assumptions, e.g. that // relevant properties on the regexp prototype (such as exec, @@split, global) // are unmodified. // // These assumptions differ by callsite. For example, RegExpPrototypeExec // cares whether the exec property has been modified; but it's totally fine // to modify other prototype properties. On the other hand, // StringPrototypeSplit does care very much whether @@split has been changed. // // We want to keep regexp execution on the fast path as much as possible. // Ideally, we could simply check if the regexp prototype has been modified; // yet common web frameworks routinely mutate it for various reasons. But most // of these mutations should happen in a way that still allows us to remain // on the fast path. To support this, the fast path check logic necessarily // becomes more involved. // // There are multiple knobs to twiddle for regexp fast path checks. We support // checks that completely ignore the prototype, checks that verify specific // properties on the prototype (the caller must ensure it passes in the right // ones), and strict checks that additionally ensure the prototype is // unchanged (we use these when we'd have to check multiple properties we // don't care too much about, e.g. all individual flag getters). using DescriptorIndexNameValue = PrototypeCheckAssembler::DescriptorIndexNameValue; void BranchIfFastRegExp( TNode context, TNode object, TNode map, PrototypeCheckAssembler::Flags prototype_check_flags, base::Optional additional_property_to_check, Label* if_isunmodified, Label* if_ismodified); // Strict: Does not tolerate any changes to the prototype map. // Permissive: Allows changes to the prototype map except for the exec // property. void BranchIfFastRegExp_Strict(TNode context, TNode object, Label* if_isunmodified, Label* if_ismodified); void BranchIfFastRegExp_Permissive(TNode context, TNode object, Label* if_isunmodified, Label* if_ismodified); // Performs fast path checks on the given object itself, but omits prototype // checks. Node* IsFastRegExpNoPrototype(SloppyTNode context, SloppyTNode object); Node* IsFastRegExpNoPrototype(SloppyTNode context, SloppyTNode object, SloppyTNode map); // For debugging only. Uses a slow GetProperty call to fetch object.exec. TNode IsFastRegExpWithOriginalExec(TNode context, TNode object); void BranchIfFastRegExpResult(Node* const context, Node* const object, Label* if_isunmodified, Label* if_ismodified); TNode FlagsGetter(TNode context, TNode regexp, const bool is_fastpath); TNode FastFlagGetter(TNode regexp, JSRegExp::Flag flag); TNode FastFlagGetterGlobal(TNode regexp) { return FastFlagGetter(regexp, JSRegExp::kGlobal); } TNode FastFlagGetterUnicode(TNode regexp) { return FastFlagGetter(regexp, JSRegExp::kUnicode); } TNode SlowFlagGetter(TNode context, TNode regexp, JSRegExp::Flag flag); TNode FlagGetter(TNode context, TNode regexp, JSRegExp::Flag flag, bool is_fastpath); Node* RegExpInitialize(Node* const context, Node* const regexp, Node* const maybe_pattern, Node* const maybe_flags); TNode RegExpExec(TNode context, Node* regexp, Node* string); TNode AdvanceStringIndex(SloppyTNode string, SloppyTNode index, SloppyTNode is_unicode, bool is_fastpath); TNode AdvanceStringIndexFast(TNode string, TNode index, TNode is_unicode) { return CAST(AdvanceStringIndex(string, index, is_unicode, true)); } TNode RegExpPrototypeMatchBody(TNode context, TNode regexp, TNode const string, const bool is_fastpath); void RegExpPrototypeSearchBodyFast(TNode context, TNode regexp, TNode string); void RegExpPrototypeSearchBodySlow(TNode context, Node* const regexp, Node* const string); void RegExpPrototypeSplitBody(TNode context, TNode regexp, TNode const string, TNode const limit); }; class RegExpMatchAllAssembler : public RegExpBuiltinsAssembler { public: explicit RegExpMatchAllAssembler(compiler::CodeAssemblerState* state) : RegExpBuiltinsAssembler(state) {} TNode CreateRegExpStringIterator(TNode native_context, TNode regexp, TNode string, TNode global, TNode full_unicode); void Generate(TNode context, TNode native_context, TNode receiver, TNode maybe_string); }; } // namespace internal } // namespace v8 #endif // V8_BUILTINS_BUILTINS_REGEXP_GEN_H_