regexp-bytecode-generator.h 6.19 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_
#define V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_
7

8
#include "src/base/strings.h"
9
#include "src/codegen/label.h"
10
#include "src/regexp/regexp-macro-assembler.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16
// An assembler/generator for the Irregexp byte code.
class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler {
17
 public:
18 19 20 21 22
  // Create an assembler. Instructions and relocation information are emitted
  // into a buffer, with the instructions starting from the beginning and the
  // relocation information starting from the end of the buffer. See CodeDesc
  // for a detailed comment on the layout (globals.h).
  //
23 24 25
  // The assembler allocates and grows its own buffer, and buffer_size
  // determines the initial buffer size. The buffer is owned by the assembler
  // and deallocated upon destruction of the assembler.
26
  RegExpBytecodeGenerator(Isolate* isolate, Zone* zone);
27
  ~RegExpBytecodeGenerator() override;
28
  // The byte-code interpreter checks on each push anyway.
29
  int stack_limit_slack() override { return 1; }
30
  bool CanReadUnaligned() const override { return false; }
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  void Bind(Label* label) override;
  void AdvanceCurrentPosition(int by) override;  // Signed cp change.
  void PopCurrentPosition() override;
  void PushCurrentPosition() override;
  void Backtrack() override;
  void GoTo(Label* label) override;
  void PushBacktrack(Label* label) override;
  bool Succeed() override;
  void Fail() override;
  void PopRegister(int register_index) override;
  void PushRegister(int register_index,
                    StackCheckFlag check_stack_limit) override;
  void AdvanceRegister(int reg, int by) override;  // r[reg] += by.
  void SetCurrentPositionFromEnd(int by) override;
  void SetRegister(int register_index, int to) override;
  void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
  void ClearRegisters(int reg_from, int reg_to) override;
  void ReadCurrentPositionFromRegister(int reg) override;
  void WriteStackPointerToRegister(int reg) override;
  void ReadStackPointerFromRegister(int reg) override;
  void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input,
                                bool check_bounds, int characters,
                                int eats_at_least) override;
  void CheckCharacter(unsigned c, Label* on_equal) override;
  void CheckCharacterAfterAnd(unsigned c, unsigned mask,
                              Label* on_equal) override;
57 58
  void CheckCharacterGT(base::uc16 limit, Label* on_greater) override;
  void CheckCharacterLT(base::uc16 limit, Label* on_less) override;
59 60 61 62 63 64
  void CheckGreedyLoop(Label* on_tos_equals_current_position) override;
  void CheckAtStart(int cp_offset, Label* on_at_start) override;
  void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override;
  void CheckNotCharacter(unsigned c, Label* on_not_equal) override;
  void CheckNotCharacterAfterAnd(unsigned c, unsigned mask,
                                 Label* on_not_equal) override;
65 66
  void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus,
                                      base::uc16 mask,
67
                                      Label* on_not_equal) override;
68 69 70
  void CheckCharacterInRange(base::uc16 from, base::uc16 to,
                             Label* on_in_range) override;
  void CheckCharacterNotInRange(base::uc16 from, base::uc16 to,
71
                                Label* on_not_in_range) override;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges,
                                  Label* on_in_range) override {
    // Disabled in the interpreter, because 1) there is no constant pool that
    // could store the ByteArray pointer, 2) bytecode size limits are not as
    // restrictive as code (e.g. branch distances on arm), 3) bytecode for
    // large character classes is already quite compact.
    // TODO(jgruber): Consider using BytecodeArrays (with a constant pool)
    // instead of plain ByteArrays; then we could implement
    // CheckCharacterInRangeArray in the interpreter.
    return false;
  }
  bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges,
                                     Label* on_not_in_range) override {
    return false;
  }
87 88 89 90
  void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
  void CheckNotBackReference(int start_reg, bool read_backward,
                             Label* on_no_match) override;
  void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
91
                                       bool unicode,
92 93 94 95
                                       Label* on_no_match) override;
  void IfRegisterLT(int register_index, int comparand, Label* if_lt) override;
  void IfRegisterGE(int register_index, int comparand, Label* if_ge) override;
  void IfRegisterEqPos(int register_index, Label* if_eq) override;
96

97 98
  IrregexpImplementation Implementation() override;
  Handle<HeapObject> GetCode(Handle<String> source) override;
99

100
 private:
101 102
  void ExpandBuffer();

103
  // Code and bitmap emission.
104
  inline void EmitOrLink(Label* label);
105 106
  inline void Emit32(uint32_t x);
  inline void Emit16(uint32_t x);
107
  inline void Emit8(uint32_t x);
108
  inline void Emit(uint32_t bc, uint32_t arg);
109
  inline void Emit(uint32_t bc, int32_t arg);
110 111
  // Bytecode buffer.
  int length();
112
  void Copy(byte* a);
113 114

  // The buffer into which code and relocation info are generated.
115 116 117
  static constexpr int kInitialBufferSize = 1024;
  ZoneVector<byte> buffer_;

118 119
  // The program counter.
  int pc_;
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
120
  Label backtrack_;
121

122 123 124 125
  int advance_current_start_;
  int advance_current_offset_;
  int advance_current_end_;

126 127 128 129 130 131
  // Stores jump edges emitted for the bytecode (used by
  // RegExpBytecodePeepholeOptimization).
  // Key: jump source (offset in buffer_ where jump destination is stored).
  // Value: jump destination (offset in buffer_ to jump to).
  ZoneUnorderedMap<int, int> jump_edges_;

132 133
  Isolate* isolate_;

134 135
  static const int kInvalidPC = -1;

136
  DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpBytecodeGenerator);
137 138
};

139 140
}  // namespace internal
}  // namespace v8
141

142
#endif  // V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_