forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexp-bytecode-analysis.h
More file actions
117 lines (88 loc) · 3.55 KB
/
Copy pathregexp-bytecode-analysis.h
File metadata and controls
117 lines (88 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2026 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_REGEXP_REGEXP_BYTECODE_ANALYSIS_H_
#define V8_REGEXP_REGEXP_BYTECODE_ANALYSIS_H_
#include "src/common/globals.h"
#include "src/handles/handles.h"
#include "src/objects/trusted-object.h"
#include "src/utils/bit-vector.h"
#include "src/zone/zone-containers.h"
#include "src/zone/zone.h"
namespace v8 {
namespace internal {
class TrustedByteArray;
class RegExpBytecodeAnalysis : public ZoneObject {
public:
RegExpBytecodeAnalysis(Isolate* isolate, Zone* zone,
DirectHandle<TrustedByteArray> bytecode);
// Runs the analysis.
void Analyze();
void PrintBlock(int block_id);
// Queries
bool IsLoopHeader(int block_id) const;
bool IsBackEdge(int bytecode_offset) const;
bool UsesCurrentChar(int block_id) const;
bool LoadsCurrentChar(int block_id) const;
int GetBlockId(int bytecode_offset) const;
int GetEbbId(int bytecode_offset) const;
int BlockStart(int block_id) const;
int BlockEnd(int block_id) const;
private:
struct LoopInfo {
int header_block_id;
BitVector members;
// Pairs of (source_block_id, target_block_id).
ZoneVector<std::pair<int, int>> exits;
LoopInfo(int header_id, int num_blocks, Zone* zone)
: header_block_id(header_id), members(num_blocks, zone), exits(zone) {}
};
void FindBasicBlocks();
void AnalyzeControlFlow();
void ComputeLoops(const ZoneVector<std::pair<int, int>>& back_edges);
void AnalyzeDataFlow();
// Helper to iterate successors of a block.
template <typename Callback>
void ForEachSuccessor(int block_id, Callback callback,
bool include_backtrack);
int block_count() const {
return static_cast<int>(block_starts_.size()) - kSlotAtLength;
}
Zone* zone_;
Handle<TrustedByteArray> bytecode_;
int length_;
// Some arrays are sized so they are safe to access at index
// `bytecode_array.length`.
static constexpr int kSlotAtLength = 1;
// TODO(jgruber): For all bytecode_offset-indexed data structures: reduce
// overhead by only considering kBytecodeAlignment. We could also merge block
// information (such as the ebb id) into a BlockInfo struct.
// Enable backwards walks. Index: bytecode_offset.
ZoneVector<uint8_t> offset_to_prev_bytecode_;
// Backtrack bytecodes may jump to any backtrack target. We collect all
// PushBacktrack targets in the first pass.
ZoneVector<uint32_t> backtrack_targets_;
// Block boundaries. Index: block_id.
// Block[i] range is [ block_starts_[i], block_starts_[i+1] )
// The last entry in block_starts_ is equal to length_, serving as the
// end of the last block.
ZoneVector<int> block_starts_;
// Which block a bytecode belongs to. Index: bytecode_offset.
ZoneVector<int32_t> offset_to_block_id_;
// Extended basic blocks (single entry, multiple exits).
// Which EBB a bytecode belongs to. Index: bytecode_offset.
ZoneVector<int32_t> offset_to_ebb_id_;
// Predecessors for each block. Index: block_id.
ZoneVector<ZoneSet<int>> predecessors_;
// Loops found in the graph.
ZoneVector<LoopInfo> loops_;
// Analysis Results
BitVector is_loop_header_; // Index: block_id.
BitVector is_back_edge_; // Index: bytecode_offset.
BitVector uses_current_char_; // Index: block_id.
BitVector loads_current_char_; // Index: block_id.
DisallowGarbageCollection no_gc_;
};
} // namespace internal
} // namespace v8
#endif // V8_REGEXP_REGEXP_BYTECODE_ANALYSIS_H_