Debian Codegraph @

js/src/irregexp/imported/regexp-bytecodes.h

raw
1 // Copyright 2011 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_BYTECODES_H_
6 #define V8_REGEXP_REGEXP_BYTECODES_H_
7
8 #include "irregexp/RegExpShim.h"
9
10 namespace v8 {
11 namespace internal {
12
13 // Maximum number of bytecodes that will be used (next power of 2 of actually
14 // defined bytecodes).
15 // All slots between the last actually defined bytecode and maximum id will be
16 // filled with BREAKs, indicating an invalid operation. This way using
17 // BYTECODE_MASK guarantees no OOB access to the dispatch table.
18 constexpr int kRegExpPaddedBytecodeCount = 1 << 6;
19 constexpr int BYTECODE_MASK = kRegExpPaddedBytecodeCount - 1;
20 // The first argument is packed in with the byte code in one word, but so it
21 // has 24 bits, but it can be positive and negative so only use 23 bits for
22 // positive values.
23 const unsigned int MAX_FIRST_ARG = 0x7fffffu;
24 const int BYTECODE_SHIFT = 8;
25 static_assert(1 << BYTECODE_SHIFT > BYTECODE_MASK);
26
27 // The list of bytecodes, in format: V(Name, Code, ByteLength).
28 // TODO(pthier): Argument offsets of bytecodes should be easily accessible by
29 // name or at least by position.
30 // TODO(jgruber): More precise types (e.g. int32/uint32 instead of value32).
31 #define BYTECODE_ITERATOR(V) \
32 V(BREAK, 0, 4) /* bc8 */ \
33 V(PUSH_CP, 1, 4) /* bc8 pad24 */ \
34 V(PUSH_BT, 2, 8) /* bc8 pad24 offset32 */ \
35 V(PUSH_REGISTER, 3, 4) /* bc8 reg_idx24 */ \
36 V(SET_REGISTER_TO_CP, 4, 8) /* bc8 reg_idx24 offset32 */ \
37 V(SET_CP_TO_REGISTER, 5, 4) /* bc8 reg_idx24 */ \
38 V(SET_REGISTER_TO_SP, 6, 4) /* bc8 reg_idx24 */ \
39 V(SET_SP_TO_REGISTER, 7, 4) /* bc8 reg_idx24 */ \
40 V(SET_REGISTER, 8, 8) /* bc8 reg_idx24 value32 */ \
41 V(ADVANCE_REGISTER, 9, 8) /* bc8 reg_idx24 value32 */ \
42 V(POP_CP, 10, 4) /* bc8 pad24 */ \
43 V(POP_BT, 11, 4) /* bc8 pad24 */ \
44 V(POP_REGISTER, 12, 4) /* bc8 reg_idx24 */ \
45 V(FAIL, 13, 4) /* bc8 pad24 */ \
46 V(SUCCEED, 14, 4) /* bc8 pad24 */ \
47 V(ADVANCE_CP, 15, 4) /* bc8 offset24 */ \
48 /* Jump to another bytecode given its offset. */ \
49 /* Bit Layout: */ \
50 /* 0x00 - 0x07: 0x10 (fixed) Bytecode */ \
51 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
52 /* 0x20 - 0x3F: Address of bytecode to jump to */ \
53 V(GOTO, 16, 8) /* bc8 pad24 addr32 */ \
54 /* Check if offset is in range and load character at given offset. */ \
55 /* Bit Layout: */ \
56 /* 0x00 - 0x07: 0x11 (fixed) Bytecode */ \
57 /* 0x08 - 0x1F: Offset from current position */ \
58 /* 0x20 - 0x3F: Address of bytecode when load is out of range */ \
59 V(LOAD_CURRENT_CHAR, 17, 8) /* bc8 offset24 addr32 */ \
60 /* Load character at given offset without range checks. */ \
61 /* Bit Layout: */ \
62 /* 0x00 - 0x07: 0x12 (fixed) Bytecode */ \
63 /* 0x08 - 0x1F: Offset from current position */ \
64 V(LOAD_CURRENT_CHAR_UNCHECKED, 18, 4) /* bc8 offset24 */ \
65 V(LOAD_2_CURRENT_CHARS, 19, 8) /* bc8 offset24 addr32 */ \
66 V(LOAD_2_CURRENT_CHARS_UNCHECKED, 20, 4) /* bc8 offset24 */ \
67 V(LOAD_4_CURRENT_CHARS, 21, 8) /* bc8 offset24 addr32 */ \
68 V(LOAD_4_CURRENT_CHARS_UNCHECKED, 22, 4) /* bc8 offset24 */ \
69 V(CHECK_4_CHARS, 23, 12) /* bc8 pad24 uint32 addr32 */ \
70 /* Check if current character is equal to a given character */ \
71 /* Bit Layout: */ \
72 /* 0x00 - 0x07: 0x19 (fixed) Bytecode */ \
73 /* 0x08 - 0x0F: 0x00 (unused) Padding */ \
74 /* 0x10 - 0x1F: Character to check */ \
75 /* 0x20 - 0x3F: Address of bytecode when matched */ \
76 V(CHECK_CHAR, 24, 8) /* bc8 pad8 uint16 addr32 */ \
77 V(CHECK_NOT_4_CHARS, 25, 12) /* bc8 pad24 uint32 addr32 */ \
78 V(CHECK_NOT_CHAR, 26, 8) /* bc8 pad8 uint16 addr32 */ \
79 V(AND_CHECK_4_CHARS, 27, 16) /* bc8 pad24 uint32 uint32 addr32 */ \
80 /* Checks if the current character combined with mask (bitwise and) */ \
81 /* matches a character (e.g. used when two characters in a disjunction */ \
82 /* differ by only a single bit */ \
83 /* Bit Layout: */ \
84 /* 0x00 - 0x07: 0x1c (fixed) Bytecode */ \
85 /* 0x08 - 0x0F: 0x00 (unused) Padding */ \
86 /* 0x10 - 0x1F: Character to match against (after mask aplied) */ \
87 /* 0x20 - 0x3F: Bitmask bitwise and combined with current character */ \
88 /* 0x40 - 0x5F: Address of bytecode when matched */ \
89 V(AND_CHECK_CHAR, 28, 12) /* bc8 pad8 uint16 uint32 addr32 */ \
90 V(AND_CHECK_NOT_4_CHARS, 29, 16) /* bc8 pad24 uint32 uint32 addr32 */ \
91 V(AND_CHECK_NOT_CHAR, 30, 12) /* bc8 pad8 uint16 uint32 addr32 */ \
92 V(MINUS_AND_CHECK_NOT_CHAR, 31, \
93 12) /* bc8 pad8 base::uc16 base::uc16 base::uc16 addr32 */ \
94 V(CHECK_CHAR_IN_RANGE, 32, 12) /* bc8 pad24 base::uc16 base::uc16 addr32 */ \
95 V(CHECK_CHAR_NOT_IN_RANGE, 33, \
96 12) /* bc8 pad24 base::uc16 base::uc16 addr32 */ \
97 /* Checks if the current character matches any of the characters encoded */ \
98 /* in a bit table. Similar to/inspired by boyer moore string search */ \
99 /* Bit Layout: */ \
100 /* 0x00 - 0x07: 0x22 (fixed) Bytecode */ \
101 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
102 /* 0x20 - 0x3F: Address of bytecode when bit is set */ \
103 /* 0x40 - 0xBF: Bit table */ \
104 V(CHECK_BIT_IN_TABLE, 34, 24) /* bc8 pad24 addr32 bits128 */ \
105 V(CHECK_LT, 35, 8) /* bc8 pad8 base::uc16 addr32 */ \
106 V(CHECK_GT, 36, 8) /* bc8 pad8 base::uc16 addr32 */ \
107 V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \
108 V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \
109 V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) \
110 V(CHECK_NOT_BACK_REF_BACKWARD, 40, 8) /* bc8 reg_idx24 addr32 */ \
111 V(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD, 41, 8) /* bc8 reg_idx24 addr32 */ \
112 V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) \
113 V(CHECK_NOT_REGS_EQUAL, 43, 12) /* bc8 regidx24 reg_idx32 addr32 */ \
114 V(CHECK_REGISTER_LT, 44, 12) /* bc8 reg_idx24 value32 addr32 */ \
115 V(CHECK_REGISTER_GE, 45, 12) /* bc8 reg_idx24 value32 addr32 */ \
116 V(CHECK_REGISTER_EQ_POS, 46, 8) /* bc8 reg_idx24 addr32 */ \
117 V(CHECK_AT_START, 47, 8) /* bc8 pad24 addr32 */ \
118 V(CHECK_NOT_AT_START, 48, 8) /* bc8 offset24 addr32 */ \
119 /* Checks if the current position matches top of backtrack stack */ \
120 /* Bit Layout: */ \
121 /* 0x00 - 0x07: 0x31 (fixed) Bytecode */ \
122 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
123 /* 0x20 - 0x3F: Address of bytecode when current matches tos */ \
124 V(CHECK_GREEDY, 49, 8) /* bc8 pad24 addr32 */ \
125 /* Advance character pointer by given offset and jump to another bytecode.*/ \
126 /* Bit Layout: */ \
127 /* 0x00 - 0x07: 0x32 (fixed) Bytecode */ \
128 /* 0x08 - 0x1F: Number of characters to advance */ \
129 /* 0x20 - 0x3F: Address of bytecode to jump to */ \
130 V(ADVANCE_CP_AND_GOTO, 50, 8) /* bc8 offset24 addr32 */ \
131 V(SET_CURRENT_POSITION_FROM_END, 51, 4) /* bc8 idx24 */ \
132 /* Checks if current position + given offset is in range. */ \
133 /* Bit Layout: */ \
134 /* 0x00 - 0x07: 0x34 (fixed) Bytecode */ \
135 /* 0x08 - 0x1F: Offset from current position */ \
136 /* 0x20 - 0x3F: Address of bytecode when position is out of range */ \
137 V(CHECK_CURRENT_POSITION, 52, 8) /* bc8 idx24 addr32 */ \
138 /* Combination of: */ \
139 /* LOAD_CURRENT_CHAR, CHECK_BIT_IN_TABLE and ADVANCE_CP_AND_GOTO */ \
140 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
141 /* Bit Layout: */ \
142 /* 0x00 - 0x07 0x35 (fixed) Bytecode */ \
143 /* 0x08 - 0x1F Load character offset from current position */ \
144 /* 0x20 - 0x3F Number of characters to advance */ \
145 /* 0x40 - 0xBF Bit Table */ \
146 /* 0xC0 - 0xDF Address of bytecode when character is matched */ \
147 /* 0xE0 - 0xFF Address of bytecode when no match */ \
148 V(SKIP_UNTIL_BIT_IN_TABLE, 53, 32) \
149 /* Combination of: */ \
150 /* CHECK_CURRENT_POSITION, LOAD_CURRENT_CHAR_UNCHECKED, AND_CHECK_CHAR */ \
151 /* and ADVANCE_CP_AND_GOTO */ \
152 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
153 /* Bit Layout: */ \
154 /* 0x00 - 0x07 0x36 (fixed) Bytecode */ \
155 /* 0x08 - 0x1F Load character offset from current position */ \
156 /* 0x20 - 0x2F Number of characters to advance */ \
157 /* 0x30 - 0x3F Character to match against (after mask applied) */ \
158 /* 0x40 - 0x5F: Bitmask bitwise and combined with current character */ \
159 /* 0x60 - 0x7F Minimum number of characters this pattern consumes */ \
160 /* 0x80 - 0x9F Address of bytecode when character is matched */ \
161 /* 0xA0 - 0xBF Address of bytecode when no match */ \
162 V(SKIP_UNTIL_CHAR_AND, 54, 24) \
163 /* Combination of: */ \
164 /* LOAD_CURRENT_CHAR, CHECK_CHAR and ADVANCE_CP_AND_GOTO */ \
165 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
166 /* Bit Layout: */ \
167 /* 0x00 - 0x07 0x37 (fixed) Bytecode */ \
168 /* 0x08 - 0x1F Load character offset from current position */ \
169 /* 0x20 - 0x2F Number of characters to advance */ \
170 /* 0x30 - 0x3F Character to match */ \
171 /* 0x40 - 0x5F Address of bytecode when character is matched */ \
172 /* 0x60 - 0x7F Address of bytecode when no match */ \
173 V(SKIP_UNTIL_CHAR, 55, 16) \
174 /* Combination of: */ \
175 /* CHECK_CURRENT_POSITION, LOAD_CURRENT_CHAR_UNCHECKED, CHECK_CHAR */ \
176 /* and ADVANCE_CP_AND_GOTO */ \
177 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
178 /* Bit Layout: */ \
179 /* 0x00 - 0x07 0x38 (fixed) Bytecode */ \
180 /* 0x08 - 0x1F Load character offset from current position */ \
181 /* 0x20 - 0x2F Number of characters to advance */ \
182 /* 0x30 - 0x3F Character to match */ \
183 /* 0x40 - 0x5F Minimum number of characters this pattern consumes */ \
184 /* 0x60 - 0x7F Address of bytecode when character is matched */ \
185 /* 0x80 - 0x9F Address of bytecode when no match */ \
186 V(SKIP_UNTIL_CHAR_POS_CHECKED, 56, 20) \
187 /* Combination of: */ \
188 /* LOAD_CURRENT_CHAR, CHECK_CHAR, CHECK_CHAR and ADVANCE_CP_AND_GOTO */ \
189 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
190 /* Bit Layout: */ \
191 /* 0x00 - 0x07 0x39 (fixed) Bytecode */ \
192 /* 0x08 - 0x1F Load character offset from current position */ \
193 /* 0x20 - 0x3F Number of characters to advance */ \
194 /* 0x40 - 0x4F Character to match */ \
195 /* 0x50 - 0x5F Other Character to match */ \
196 /* 0x60 - 0x7F Address of bytecode when either character is matched */ \
197 /* 0x80 - 0x9F Address of bytecode when no match */ \
198 V(SKIP_UNTIL_CHAR_OR_CHAR, 57, 20) \
199 /* Combination of: */ \
200 /* LOAD_CURRENT_CHAR, CHECK_GT, CHECK_BIT_IN_TABLE, GOTO and */ \
201 /* and ADVANCE_CP_AND_GOTO */ \
202 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
203 /* Bit Layout: */ \
204 /* 0x00 - 0x07 0x3A (fixed) Bytecode */ \
205 /* 0x08 - 0x1F Load character offset from current position */ \
206 /* 0x20 - 0x2F Number of characters to advance */ \
207 /* 0x30 - 0x3F Character to check if it is less than current char */ \
208 /* 0x40 - 0xBF Bit Table */ \
209 /* 0xC0 - 0xDF Address of bytecode when character is matched */ \
210 /* 0xE0 - 0xFF Address of bytecode when no match */ \
211 V(SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE, 58, 32)
212
213 #define COUNT(...) +1
214 static constexpr int kRegExpBytecodeCount = BYTECODE_ITERATOR(COUNT);
215 #undef COUNT
216
217 // Just making sure we assigned values above properly. They should be
218 // contiguous, strictly increasing, and start at 0.
219 // TODO(jgruber): Do not explicitly assign values, instead generate them
220 // implicitly from the list order.
221 static_assert(kRegExpBytecodeCount == 59);
222
223 #define DECLARE_BYTECODES(name, code, length) \
224 static constexpr int BC_##name = code;
225 BYTECODE_ITERATOR(DECLARE_BYTECODES)
226 #undef DECLARE_BYTECODES
227
228 static constexpr int kRegExpBytecodeLengths[] = {
229 #define DECLARE_BYTECODE_LENGTH(name, code, length) length,
230 BYTECODE_ITERATOR(DECLARE_BYTECODE_LENGTH)
231 #undef DECLARE_BYTECODE_LENGTH
232 };
233
234 inline constexpr int RegExpBytecodeLength(int bytecode) {
235 DCHECK(base::IsInRange(bytecode, 0, kRegExpBytecodeCount - 1));
236 return kRegExpBytecodeLengths[bytecode];
237 }
238
239 static constexpr const char* const kRegExpBytecodeNames[] = {
240 #define DECLARE_BYTECODE_NAME(name, ...) #name,
241 BYTECODE_ITERATOR(DECLARE_BYTECODE_NAME)
242 #undef DECLARE_BYTECODE_NAME
243 };
244
245 inline constexpr const char* RegExpBytecodeName(int bytecode) {
246 DCHECK(base::IsInRange(bytecode, 0, kRegExpBytecodeCount - 1));
247 return kRegExpBytecodeNames[bytecode];
248 }
249
250 void RegExpBytecodeDisassembleSingle(const uint8_t* code_base,
251 const uint8_t* pc);
252 void RegExpBytecodeDisassemble(const uint8_t* code_base, int length,
253 const char* pattern);
254
255 } // namespace internal
256 } // namespace v8
257
258 #endif // V8_REGEXP_REGEXP_BYTECODES_H_