regexp-bytecode-generator.h (6603B)
1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_ 6 #define V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_ 7 8 #include "irregexp/imported/regexp-macro-assembler.h" 9 10 namespace v8 { 11 namespace internal { 12 13 // An assembler/generator for the Irregexp byte code. 14 class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler { 15 public: 16 // Create an assembler. Instructions and relocation information are emitted 17 // into a buffer, with the instructions starting from the beginning and the 18 // relocation information starting from the end of the buffer. See CodeDesc 19 // for a detailed comment on the layout (globals.h). 20 // 21 // The assembler allocates and grows its own buffer, and buffer_size 22 // determines the initial buffer size. The buffer is owned by the assembler 23 // and deallocated upon destruction of the assembler. 24 RegExpBytecodeGenerator(Isolate* isolate, Zone* zone); 25 ~RegExpBytecodeGenerator() override; 26 // The byte-code interpreter checks on each push anyway. 27 int stack_limit_slack_slot_count() override { return 1; } 28 bool CanReadUnaligned() const override { return false; } 29 void Bind(Label* label) override; 30 void AdvanceCurrentPosition(int by) override; // Signed cp change. 31 void PopCurrentPosition() override; 32 void PushCurrentPosition() override; 33 void Backtrack() override; 34 void GoTo(Label* label) override; 35 void PushBacktrack(Label* label) override; 36 bool Succeed() override; 37 void Fail() override; 38 void PopRegister(int register_index) override; 39 void PushRegister(int register_index, 40 StackCheckFlag check_stack_limit) override; 41 void AdvanceRegister(int reg, int by) override; // r[reg] += by. 42 void SetCurrentPositionFromEnd(int by) override; 43 void SetRegister(int register_index, int to) override; 44 void WriteCurrentPositionToRegister(int reg, int cp_offset) override; 45 void ClearRegisters(int reg_from, int reg_to) override; 46 void ReadCurrentPositionFromRegister(int reg) override; 47 void WriteStackPointerToRegister(int reg) override; 48 void ReadStackPointerFromRegister(int reg) override; 49 void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input, 50 bool check_bounds, int characters, 51 int eats_at_least) override; 52 void CheckCharacter(unsigned c, Label* on_equal) override; 53 void CheckCharacterAfterAnd(unsigned c, unsigned mask, 54 Label* on_equal) override; 55 void CheckCharacterGT(base::uc16 limit, Label* on_greater) override; 56 void CheckCharacterLT(base::uc16 limit, Label* on_less) override; 57 void CheckFixedLengthLoop(Label* on_tos_equals_current_position) override; 58 void CheckAtStart(int cp_offset, Label* on_at_start) override; 59 void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override; 60 void CheckNotCharacter(unsigned c, Label* on_not_equal) override; 61 void CheckNotCharacterAfterAnd(unsigned c, unsigned mask, 62 Label* on_not_equal) override; 63 void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus, 64 base::uc16 mask, 65 Label* on_not_equal) override; 66 void CheckCharacterInRange(base::uc16 from, base::uc16 to, 67 Label* on_in_range) override; 68 void CheckCharacterNotInRange(base::uc16 from, base::uc16 to, 69 Label* on_not_in_range) override; 70 bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges, 71 Label* on_in_range) override { 72 // Disabled in the interpreter, because 1) there is no constant pool that 73 // could store the ByteArray pointer, 2) bytecode size limits are not as 74 // restrictive as code (e.g. branch distances on arm), 3) bytecode for 75 // large character classes is already quite compact. 76 // TODO(jgruber): Consider using BytecodeArrays (with a constant pool) 77 // instead of plain ByteArrays; then we could implement 78 // CheckCharacterInRangeArray in the interpreter. 79 return false; 80 } 81 bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges, 82 Label* on_not_in_range) override { 83 return false; 84 } 85 void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override; 86 void SkipUntilBitInTable(int cp_offset, Handle<ByteArray> table, 87 Handle<ByteArray> nibble_table, 88 int advance_by) override; 89 void CheckNotBackReference(int start_reg, bool read_backward, 90 Label* on_no_match) override; 91 void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward, 92 bool unicode, 93 Label* on_no_match) override; 94 void IfRegisterLT(int register_index, int comparand, Label* if_lt) override; 95 void IfRegisterGE(int register_index, int comparand, Label* if_ge) override; 96 void IfRegisterEqPos(int register_index, Label* if_eq) override; 97 98 IrregexpImplementation Implementation() override; 99 DirectHandle<HeapObject> GetCode(DirectHandle<String> source, 100 RegExpFlags flags) override; 101 102 private: 103 void ExpandBuffer(); 104 105 // Code and bitmap emission. 106 inline void EmitOrLink(Label* label); 107 inline void Emit32(uint32_t x); 108 inline void Emit16(uint32_t x); 109 inline void Emit8(uint32_t x); 110 inline void Emit(uint32_t bc, uint32_t arg); 111 inline void Emit(uint32_t bc, int32_t arg); 112 void EmitSkipTable(DirectHandle<ByteArray> table); 113 // Bytecode buffer. 114 int length(); 115 void Copy(uint8_t* a); 116 117 // The buffer into which code and relocation info are generated. 118 static constexpr int kInitialBufferSize = 1024; 119 ZoneVector<uint8_t> buffer_; 120 121 // The program counter. 122 int pc_; 123 Label backtrack_; 124 125 int advance_current_start_; 126 int advance_current_offset_; 127 int advance_current_end_; 128 129 // Stores jump edges emitted for the bytecode (used by 130 // RegExpBytecodePeepholeOptimization). 131 // Key: jump source (offset in buffer_ where jump destination is stored). 132 // Value: jump destination (offset in buffer_ to jump to). 133 ZoneUnorderedMap<int, int> jump_edges_; 134 135 Isolate* isolate_; 136 137 static const int kInvalidPC = -1; 138 139 DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpBytecodeGenerator); 140 }; 141 142 } // namespace internal 143 } // namespace v8 144 145 #endif // V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_