v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
regexp-bytecode-generator.h
Go to the documentation of this file.
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 "src/base/strings.h"
9#include "src/codegen/label.h"
11
12namespace v8 {
13namespace internal {
14
15// An assembler/generator for the Irregexp byte code.
17 public:
18 // Create an assembler. Instructions and relocation information are emitted
19 // into a buffer, with the instructions starting from the beginning and the
20 // relocation information starting from the end of the buffer. See CodeDesc
21 // for a detailed comment on the layout (globals.h).
22 //
23 // The assembler allocates and grows its own buffer, and buffer_size
24 // determines the initial buffer size. The buffer is owned by the assembler
25 // 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_slot_count() override { return 1; }
30 bool CanReadUnaligned() const override { return false; }
31 void Bind(Label* label) override;
32 void AdvanceCurrentPosition(int by) override; // Signed cp change.
33 void PopCurrentPosition() override;
34 void PushCurrentPosition() override;
35 void Backtrack() override;
36 void GoTo(Label* label) override;
37 void PushBacktrack(Label* label) override;
38 bool Succeed() override;
39 void Fail() override;
40 void PopRegister(int register_index) override;
41 void PushRegister(int register_index,
42 StackCheckFlag check_stack_limit) override;
43 void AdvanceRegister(int reg, int by) override; // r[reg] += by.
44 void SetCurrentPositionFromEnd(int by) override;
45 void SetRegister(int register_index, int to) override;
46 void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
47 void ClearRegisters(int reg_from, int reg_to) override;
48 void ReadCurrentPositionFromRegister(int reg) override;
49 void WriteStackPointerToRegister(int reg) override;
50 void ReadStackPointerFromRegister(int reg) override;
51 void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input,
52 bool check_bounds, int characters,
53 int eats_at_least) override;
54 void CheckCharacter(unsigned c, Label* on_equal) override;
55 void CheckCharacterAfterAnd(unsigned c, unsigned mask,
56 Label* on_equal) override;
57 void CheckCharacterGT(base::uc16 limit, Label* on_greater) override;
58 void CheckCharacterLT(base::uc16 limit, Label* on_less) override;
59 void CheckGreedyLoop(Label* on_tos_equals_current_position) override;
60 void CheckAtStart(int cp_offset, Label* on_at_start) override;
61 void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override;
62 void CheckNotCharacter(unsigned c, Label* on_not_equal) override;
63 void CheckNotCharacterAfterAnd(unsigned c, unsigned mask,
64 Label* on_not_equal) override;
65 void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus,
67 Label* on_not_equal) override;
68 void CheckCharacterInRange(base::uc16 from, base::uc16 to,
69 Label* on_in_range) override;
70 void CheckCharacterNotInRange(base::uc16 from, base::uc16 to,
71 Label* on_not_in_range) override;
73 Label* on_in_range) override {
74 // Disabled in the interpreter, because 1) there is no constant pool that
75 // could store the ByteArray pointer, 2) bytecode size limits are not as
76 // restrictive as code (e.g. branch distances on arm), 3) bytecode for
77 // large character classes is already quite compact.
78 // TODO(jgruber): Consider using BytecodeArrays (with a constant pool)
79 // instead of plain ByteArrays; then we could implement
80 // CheckCharacterInRangeArray in the interpreter.
81 return false;
82 }
84 Label* on_not_in_range) override {
85 return false;
86 }
87 void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
88 void SkipUntilBitInTable(int cp_offset, Handle<ByteArray> table,
89 Handle<ByteArray> nibble_table,
90 int advance_by) override;
91 void CheckNotBackReference(int start_reg, bool read_backward,
92 Label* on_no_match) override;
93 void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
94 bool unicode,
95 Label* on_no_match) override;
96 void IfRegisterLT(int register_index, int comparand, Label* if_lt) override;
97 void IfRegisterGE(int register_index, int comparand, Label* if_ge) override;
98 void IfRegisterEqPos(int register_index, Label* if_eq) override;
99
100 IrregexpImplementation Implementation() override;
102 RegExpFlags flags) override;
103
104 private:
105 void ExpandBuffer();
106
107 // Code and bitmap emission.
108 inline void EmitOrLink(Label* label);
109 inline void Emit32(uint32_t x);
110 inline void Emit16(uint32_t x);
111 inline void Emit8(uint32_t x);
112 inline void Emit(uint32_t bc, uint32_t arg);
113 inline void Emit(uint32_t bc, int32_t arg);
114 void EmitSkipTable(DirectHandle<ByteArray> table);
115 // Bytecode buffer.
116 int length();
117 void Copy(uint8_t* a);
118
119 // The buffer into which code and relocation info are generated.
120 static constexpr int kInitialBufferSize = 1024;
122
123 // The program counter.
124 int pc_;
126
130
131 // Stores jump edges emitted for the bytecode (used by
132 // RegExpBytecodePeepholeOptimization).
133 // Key: jump source (offset in buffer_ where jump destination is stored).
134 // Value: jump destination (offset in buffer_ to jump to).
136
138
139 static const int kInvalidPC = -1;
140
142};
143
144} // namespace internal
145} // namespace v8
146
147#endif // V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_
bool CheckCharacterInRangeArray(const ZoneList< CharacterRange > *ranges, Label *on_in_range) override
DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpBytecodeGenerator)
bool CheckCharacterNotInRangeArray(const ZoneList< CharacterRange > *ranges, Label *on_not_in_range) override
Label label
LiftoffRegister reg
int x
uint32_t const mask
uint16_t uc16
Definition strings.h:18
#define V8_EXPORT_PRIVATE
Definition macros.h:460