RegexByteCode.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "RegexBytecodeStreamOptimizer.h"
  8. #include "RegexMatch.h"
  9. #include "RegexOptions.h"
  10. #include <AK/DisjointChunks.h>
  11. #include <AK/Format.h>
  12. #include <AK/Forward.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/OwnPtr.h>
  16. #include <AK/Traits.h>
  17. #include <AK/TypeCasts.h>
  18. #include <AK/Types.h>
  19. #include <AK/Vector.h>
  20. #include <LibUnicode/Forward.h>
  21. namespace regex {
  22. using ByteCodeValueType = u64;
  23. #define ENUMERATE_OPCODES \
  24. __ENUMERATE_OPCODE(Compare) \
  25. __ENUMERATE_OPCODE(Jump) \
  26. __ENUMERATE_OPCODE(JumpNonEmpty) \
  27. __ENUMERATE_OPCODE(ForkJump) \
  28. __ENUMERATE_OPCODE(ForkStay) \
  29. __ENUMERATE_OPCODE(ForkReplaceJump) \
  30. __ENUMERATE_OPCODE(ForkReplaceStay) \
  31. __ENUMERATE_OPCODE(FailForks) \
  32. __ENUMERATE_OPCODE(SaveLeftCaptureGroup) \
  33. __ENUMERATE_OPCODE(SaveRightCaptureGroup) \
  34. __ENUMERATE_OPCODE(SaveRightNamedCaptureGroup) \
  35. __ENUMERATE_OPCODE(CheckBegin) \
  36. __ENUMERATE_OPCODE(CheckEnd) \
  37. __ENUMERATE_OPCODE(CheckBoundary) \
  38. __ENUMERATE_OPCODE(Save) \
  39. __ENUMERATE_OPCODE(Restore) \
  40. __ENUMERATE_OPCODE(GoBack) \
  41. __ENUMERATE_OPCODE(ClearCaptureGroup) \
  42. __ENUMERATE_OPCODE(Repeat) \
  43. __ENUMERATE_OPCODE(ResetRepeat) \
  44. __ENUMERATE_OPCODE(Checkpoint) \
  45. __ENUMERATE_OPCODE(Exit)
  46. // clang-format off
  47. enum class OpCodeId : ByteCodeValueType {
  48. #define __ENUMERATE_OPCODE(x) x,
  49. ENUMERATE_OPCODES
  50. #undef __ENUMERATE_OPCODE
  51. First = Compare,
  52. Last = Exit,
  53. };
  54. // clang-format on
  55. #define ENUMERATE_CHARACTER_COMPARE_TYPES \
  56. __ENUMERATE_CHARACTER_COMPARE_TYPE(Undefined) \
  57. __ENUMERATE_CHARACTER_COMPARE_TYPE(Inverse) \
  58. __ENUMERATE_CHARACTER_COMPARE_TYPE(TemporaryInverse) \
  59. __ENUMERATE_CHARACTER_COMPARE_TYPE(AnyChar) \
  60. __ENUMERATE_CHARACTER_COMPARE_TYPE(Char) \
  61. __ENUMERATE_CHARACTER_COMPARE_TYPE(String) \
  62. __ENUMERATE_CHARACTER_COMPARE_TYPE(CharClass) \
  63. __ENUMERATE_CHARACTER_COMPARE_TYPE(CharRange) \
  64. __ENUMERATE_CHARACTER_COMPARE_TYPE(Reference) \
  65. __ENUMERATE_CHARACTER_COMPARE_TYPE(Property) \
  66. __ENUMERATE_CHARACTER_COMPARE_TYPE(GeneralCategory) \
  67. __ENUMERATE_CHARACTER_COMPARE_TYPE(Script) \
  68. __ENUMERATE_CHARACTER_COMPARE_TYPE(ScriptExtension) \
  69. __ENUMERATE_CHARACTER_COMPARE_TYPE(RangeExpressionDummy) \
  70. __ENUMERATE_CHARACTER_COMPARE_TYPE(LookupTable)
  71. enum class CharacterCompareType : ByteCodeValueType {
  72. #define __ENUMERATE_CHARACTER_COMPARE_TYPE(x) x,
  73. ENUMERATE_CHARACTER_COMPARE_TYPES
  74. #undef __ENUMERATE_CHARACTER_COMPARE_TYPE
  75. };
  76. #define ENUMERATE_CHARACTER_CLASSES \
  77. __ENUMERATE_CHARACTER_CLASS(Alnum) \
  78. __ENUMERATE_CHARACTER_CLASS(Cntrl) \
  79. __ENUMERATE_CHARACTER_CLASS(Lower) \
  80. __ENUMERATE_CHARACTER_CLASS(Space) \
  81. __ENUMERATE_CHARACTER_CLASS(Alpha) \
  82. __ENUMERATE_CHARACTER_CLASS(Digit) \
  83. __ENUMERATE_CHARACTER_CLASS(Print) \
  84. __ENUMERATE_CHARACTER_CLASS(Upper) \
  85. __ENUMERATE_CHARACTER_CLASS(Blank) \
  86. __ENUMERATE_CHARACTER_CLASS(Graph) \
  87. __ENUMERATE_CHARACTER_CLASS(Punct) \
  88. __ENUMERATE_CHARACTER_CLASS(Word) \
  89. __ENUMERATE_CHARACTER_CLASS(Xdigit)
  90. enum class CharClass : ByteCodeValueType {
  91. #define __ENUMERATE_CHARACTER_CLASS(x) x,
  92. ENUMERATE_CHARACTER_CLASSES
  93. #undef __ENUMERATE_CHARACTER_CLASS
  94. };
  95. #define ENUMERATE_BOUNDARY_CHECK_TYPES \
  96. __ENUMERATE_BOUNDARY_CHECK_TYPE(Word) \
  97. __ENUMERATE_BOUNDARY_CHECK_TYPE(NonWord)
  98. enum class BoundaryCheckType : ByteCodeValueType {
  99. #define __ENUMERATE_BOUNDARY_CHECK_TYPE(x) x,
  100. ENUMERATE_BOUNDARY_CHECK_TYPES
  101. #undef __ENUMERATE_BOUNDARY_CHECK_TYPE
  102. };
  103. struct CharRange {
  104. u32 const from;
  105. u32 const to;
  106. CharRange(u64 value)
  107. : from(value >> 32)
  108. , to(value & 0xffffffff)
  109. {
  110. }
  111. CharRange(u32 from, u32 to)
  112. : from(from)
  113. , to(to)
  114. {
  115. }
  116. operator ByteCodeValueType() const { return ((u64)from << 32) | to; }
  117. };
  118. struct CompareTypeAndValuePair {
  119. CharacterCompareType type;
  120. ByteCodeValueType value;
  121. };
  122. class OpCode;
  123. class ByteCode : public DisjointChunks<ByteCodeValueType> {
  124. using Base = DisjointChunks<ByteCodeValueType>;
  125. public:
  126. ByteCode()
  127. {
  128. ensure_opcodes_initialized();
  129. }
  130. ByteCode(ByteCode const&) = default;
  131. virtual ~ByteCode() = default;
  132. ByteCode& operator=(ByteCode&&) = default;
  133. ByteCode& operator=(Base&& value)
  134. {
  135. static_cast<Base&>(*this) = move(value);
  136. return *this;
  137. }
  138. template<typename... Args>
  139. void empend(Args&&... args)
  140. {
  141. if (is_empty())
  142. Base::append({});
  143. Base::last_chunk().empend(forward<Args>(args)...);
  144. }
  145. template<typename T>
  146. void append(T&& value)
  147. {
  148. if (is_empty())
  149. Base::append({});
  150. Base::last_chunk().append(forward<T>(value));
  151. }
  152. template<typename T>
  153. void prepend(T&& value)
  154. {
  155. if (is_empty())
  156. return append(forward<T>(value));
  157. Base::first_chunk().prepend(forward<T>(value));
  158. }
  159. void last_chunk() const = delete;
  160. void first_chunk() const = delete;
  161. void insert_bytecode_compare_values(Vector<CompareTypeAndValuePair>&& pairs)
  162. {
  163. Optimizer::append_character_class(*this, move(pairs));
  164. }
  165. void insert_bytecode_check_boundary(BoundaryCheckType type)
  166. {
  167. ByteCode bytecode;
  168. bytecode.empend((ByteCodeValueType)OpCodeId::CheckBoundary);
  169. bytecode.empend((ByteCodeValueType)type);
  170. extend(move(bytecode));
  171. }
  172. void insert_bytecode_clear_capture_group(size_t index)
  173. {
  174. empend(static_cast<ByteCodeValueType>(OpCodeId::ClearCaptureGroup));
  175. empend(index);
  176. }
  177. void insert_bytecode_compare_string(StringView view)
  178. {
  179. ByteCode bytecode;
  180. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::Compare));
  181. bytecode.empend(static_cast<u64>(1)); // number of arguments
  182. ByteCode arguments;
  183. arguments.empend(static_cast<ByteCodeValueType>(CharacterCompareType::String));
  184. arguments.insert_string(view);
  185. bytecode.empend(arguments.size()); // size of arguments
  186. bytecode.extend(move(arguments));
  187. extend(move(bytecode));
  188. }
  189. void insert_bytecode_group_capture_left(size_t capture_groups_count)
  190. {
  191. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveLeftCaptureGroup));
  192. empend(capture_groups_count);
  193. }
  194. void insert_bytecode_group_capture_right(size_t capture_groups_count)
  195. {
  196. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveRightCaptureGroup));
  197. empend(capture_groups_count);
  198. }
  199. void insert_bytecode_group_capture_right(size_t capture_groups_count, StringView name)
  200. {
  201. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveRightNamedCaptureGroup));
  202. empend(reinterpret_cast<ByteCodeValueType>(name.characters_without_null_termination()));
  203. empend(name.length());
  204. empend(capture_groups_count);
  205. }
  206. enum class LookAroundType {
  207. LookAhead,
  208. LookBehind,
  209. NegatedLookAhead,
  210. NegatedLookBehind,
  211. };
  212. void insert_bytecode_lookaround(ByteCode&& lookaround_body, LookAroundType type, size_t match_length = 0)
  213. {
  214. // FIXME: The save stack will grow infinitely with repeated failures
  215. // as we do not discard that on failure (we don't necessarily know how many to pop with the current architecture).
  216. switch (type) {
  217. case LookAroundType::LookAhead: {
  218. // SAVE
  219. // REGEXP BODY
  220. // RESTORE
  221. empend((ByteCodeValueType)OpCodeId::Save);
  222. extend(move(lookaround_body));
  223. empend((ByteCodeValueType)OpCodeId::Restore);
  224. return;
  225. }
  226. case LookAroundType::NegatedLookAhead: {
  227. // JUMP _A
  228. // LABEL _L
  229. // REGEXP BODY
  230. // FAIL 2
  231. // LABEL _A
  232. // SAVE
  233. // FORKJUMP _L
  234. // RESTORE
  235. auto body_length = lookaround_body.size();
  236. empend((ByteCodeValueType)OpCodeId::Jump);
  237. empend((ByteCodeValueType)body_length + 2); // JUMP to label _A
  238. extend(move(lookaround_body));
  239. empend((ByteCodeValueType)OpCodeId::FailForks);
  240. empend((ByteCodeValueType)2); // Fail two forks
  241. empend((ByteCodeValueType)OpCodeId::Save);
  242. empend((ByteCodeValueType)OpCodeId::ForkJump);
  243. empend((ByteCodeValueType) - (body_length + 5)); // JUMP to label _L
  244. empend((ByteCodeValueType)OpCodeId::Restore);
  245. return;
  246. }
  247. case LookAroundType::LookBehind:
  248. // SAVE
  249. // GOBACK match_length(BODY)
  250. // REGEXP BODY
  251. // RESTORE
  252. empend((ByteCodeValueType)OpCodeId::Save);
  253. empend((ByteCodeValueType)OpCodeId::GoBack);
  254. empend((ByteCodeValueType)match_length);
  255. extend(move(lookaround_body));
  256. empend((ByteCodeValueType)OpCodeId::Restore);
  257. return;
  258. case LookAroundType::NegatedLookBehind: {
  259. // JUMP _A
  260. // LABEL _L
  261. // GOBACK match_length(BODY)
  262. // REGEXP BODY
  263. // FAIL 2
  264. // LABEL _A
  265. // SAVE
  266. // FORKJUMP _L
  267. // RESTORE
  268. auto body_length = lookaround_body.size();
  269. empend((ByteCodeValueType)OpCodeId::Jump);
  270. empend((ByteCodeValueType)body_length + 4); // JUMP to label _A
  271. empend((ByteCodeValueType)OpCodeId::GoBack);
  272. empend((ByteCodeValueType)match_length);
  273. extend(move(lookaround_body));
  274. empend((ByteCodeValueType)OpCodeId::FailForks);
  275. empend((ByteCodeValueType)2); // Fail two forks
  276. empend((ByteCodeValueType)OpCodeId::Save);
  277. empend((ByteCodeValueType)OpCodeId::ForkJump);
  278. empend((ByteCodeValueType) - (body_length + 7)); // JUMP to label _L
  279. empend((ByteCodeValueType)OpCodeId::Restore);
  280. return;
  281. }
  282. }
  283. VERIFY_NOT_REACHED();
  284. }
  285. void insert_bytecode_alternation(ByteCode&& left, ByteCode&& right)
  286. {
  287. // FORKJUMP _ALT
  288. // REGEXP ALT2
  289. // JUMP _END
  290. // LABEL _ALT
  291. // REGEXP ALT1
  292. // LABEL _END
  293. // Optimisation: Eliminate extra work by unifying common pre-and-postfix exprs.
  294. Optimizer::append_alternation(*this, move(left), move(right));
  295. }
  296. template<typename T>
  297. static void transform_bytecode_repetition_min_max(ByteCode& bytecode_to_repeat, T minimum, Optional<T> maximum, size_t min_repetition_mark_id, size_t max_repetition_mark_id, bool greedy = true) requires(IsIntegral<T>)
  298. {
  299. if (!maximum.has_value()) {
  300. if (minimum == 0)
  301. return transform_bytecode_repetition_any(bytecode_to_repeat, greedy);
  302. if (minimum == 1)
  303. return transform_bytecode_repetition_min_one(bytecode_to_repeat, greedy);
  304. }
  305. ByteCode new_bytecode;
  306. new_bytecode.insert_bytecode_repetition_n(bytecode_to_repeat, minimum, min_repetition_mark_id);
  307. if (maximum.has_value()) {
  308. // (REPEAT REGEXP MIN)
  309. // LABEL _MAX_LOOP |
  310. // FORK END |
  311. // REGEXP |
  312. // REPEAT _MAX_LOOP MAX-MIN | if max > min
  313. // FORK END |
  314. // REGEXP |
  315. // LABEL END |
  316. // RESET _MAX_LOOP |
  317. auto jump_kind = static_cast<ByteCodeValueType>(greedy ? OpCodeId::ForkStay : OpCodeId::ForkJump);
  318. if (maximum.value() > minimum) {
  319. new_bytecode.empend(jump_kind);
  320. new_bytecode.empend((ByteCodeValueType)0); // Placeholder for the jump target.
  321. auto pre_loop_fork_jump_index = new_bytecode.size();
  322. new_bytecode.extend(bytecode_to_repeat);
  323. auto repetitions = maximum.value() - minimum;
  324. auto fork_jump_address = new_bytecode.size();
  325. if (repetitions > 1) {
  326. new_bytecode.empend((ByteCodeValueType)OpCodeId::Repeat);
  327. new_bytecode.empend(bytecode_to_repeat.size() + 2);
  328. new_bytecode.empend(static_cast<ByteCodeValueType>(repetitions - 1));
  329. new_bytecode.empend(max_repetition_mark_id);
  330. new_bytecode.empend(jump_kind);
  331. new_bytecode.empend((ByteCodeValueType)0); // Placeholder for the jump target.
  332. auto post_loop_fork_jump_index = new_bytecode.size();
  333. new_bytecode.extend(bytecode_to_repeat);
  334. fork_jump_address = new_bytecode.size();
  335. new_bytecode[post_loop_fork_jump_index - 1] = (ByteCodeValueType)(fork_jump_address - post_loop_fork_jump_index);
  336. new_bytecode.empend((ByteCodeValueType)OpCodeId::ResetRepeat);
  337. new_bytecode.empend((ByteCodeValueType)max_repetition_mark_id);
  338. }
  339. new_bytecode[pre_loop_fork_jump_index - 1] = (ByteCodeValueType)(fork_jump_address - pre_loop_fork_jump_index);
  340. }
  341. } else {
  342. // no maximum value set, repeat finding if possible:
  343. // (REPEAT REGEXP MIN)
  344. // LABEL _START
  345. // CHECKPOINT _C
  346. // REGEXP
  347. // JUMP_NONEMPTY _C _START FORK
  348. // Note: This is only safe because REPEAT will leave one iteration outside (see repetition_n)
  349. new_bytecode.insert(new_bytecode.size() - bytecode_to_repeat.size(), (ByteCodeValueType)OpCodeId::Checkpoint);
  350. auto jump_kind = static_cast<ByteCodeValueType>(greedy ? OpCodeId::ForkJump : OpCodeId::ForkStay);
  351. new_bytecode.empend((ByteCodeValueType)OpCodeId::JumpNonEmpty);
  352. new_bytecode.empend(-bytecode_to_repeat.size() - 4 - 1); // Jump to the last iteration
  353. new_bytecode.empend(-bytecode_to_repeat.size() - 4 - 1); // if _C is not empty.
  354. new_bytecode.empend(jump_kind);
  355. }
  356. bytecode_to_repeat = move(new_bytecode);
  357. }
  358. template<typename T>
  359. void insert_bytecode_repetition_n(ByteCode& bytecode_to_repeat, T n, size_t repetition_mark_id) requires(IsIntegral<T>)
  360. {
  361. // LABEL _LOOP
  362. // REGEXP
  363. // REPEAT _LOOP N-1
  364. // REGEXP
  365. if (n == 0)
  366. return;
  367. // Note: this bytecode layout allows callers to repeat the last REGEXP instruction without the
  368. // REPEAT instruction forcing another loop.
  369. extend(bytecode_to_repeat);
  370. if (n > 1) {
  371. empend(static_cast<ByteCodeValueType>(OpCodeId::Repeat));
  372. empend(bytecode_to_repeat.size());
  373. empend(static_cast<ByteCodeValueType>(n - 1));
  374. empend(repetition_mark_id);
  375. extend(bytecode_to_repeat);
  376. }
  377. }
  378. static void transform_bytecode_repetition_min_one(ByteCode& bytecode_to_repeat, bool greedy)
  379. {
  380. // LABEL _START = -bytecode_to_repeat.size()
  381. // CHECKPOINT _C
  382. // REGEXP
  383. // JUMP_NONEMPTY _C _START FORKSTAY (FORKJUMP -> Greedy)
  384. bytecode_to_repeat.prepend((ByteCodeValueType)OpCodeId::Checkpoint);
  385. bytecode_to_repeat.empend((ByteCodeValueType)OpCodeId::JumpNonEmpty);
  386. bytecode_to_repeat.empend(-bytecode_to_repeat.size() - 3); // Jump to the _START label...
  387. bytecode_to_repeat.empend(-bytecode_to_repeat.size() - 2); // ...if _C is not empty
  388. if (greedy)
  389. bytecode_to_repeat.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  390. else
  391. bytecode_to_repeat.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  392. }
  393. static void transform_bytecode_repetition_any(ByteCode& bytecode_to_repeat, bool greedy)
  394. {
  395. // LABEL _START
  396. // FORKJUMP _END (FORKSTAY -> Greedy)
  397. // CHECKPOINT _C
  398. // REGEXP
  399. // JUMP_NONEMPTY _C _START JUMP
  400. // LABEL _END
  401. // LABEL _START = m_bytes.size();
  402. ByteCode bytecode;
  403. if (greedy)
  404. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  405. else
  406. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  407. bytecode.empend(bytecode_to_repeat.size() + 1 + 4); // Jump to the _END label
  408. auto c_label = bytecode.size();
  409. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::Checkpoint));
  410. bytecode.extend(bytecode_to_repeat);
  411. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::JumpNonEmpty));
  412. bytecode.empend(-bytecode.size() - 3); // Jump(...) to the _START label...
  413. bytecode.empend(c_label - bytecode.size() - 2); // ...only if _C passes.
  414. bytecode.empend((ByteCodeValueType)OpCodeId::Jump);
  415. // LABEL _END = bytecode.size()
  416. bytecode_to_repeat = move(bytecode);
  417. }
  418. static void transform_bytecode_repetition_zero_or_one(ByteCode& bytecode_to_repeat, bool greedy)
  419. {
  420. // FORKJUMP _END (FORKSTAY -> Greedy)
  421. // REGEXP
  422. // LABEL _END
  423. ByteCode bytecode;
  424. if (greedy)
  425. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  426. else
  427. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  428. bytecode.empend(bytecode_to_repeat.size()); // Jump to the _END label
  429. bytecode.extend(move(bytecode_to_repeat));
  430. // LABEL _END = bytecode.size()
  431. bytecode_to_repeat = move(bytecode);
  432. }
  433. OpCode& get_opcode(MatchState& state) const;
  434. private:
  435. void insert_string(StringView view)
  436. {
  437. empend((ByteCodeValueType)view.length());
  438. for (size_t i = 0; i < view.length(); ++i)
  439. empend((ByteCodeValueType)view[i]);
  440. }
  441. void ensure_opcodes_initialized();
  442. ALWAYS_INLINE OpCode& get_opcode_by_id(OpCodeId id) const;
  443. static OwnPtr<OpCode> s_opcodes[(size_t)OpCodeId::Last + 1];
  444. static bool s_opcodes_initialized;
  445. };
  446. #define ENUMERATE_EXECUTION_RESULTS \
  447. __ENUMERATE_EXECUTION_RESULT(Continue) \
  448. __ENUMERATE_EXECUTION_RESULT(Fork_PrioHigh) \
  449. __ENUMERATE_EXECUTION_RESULT(Fork_PrioLow) \
  450. __ENUMERATE_EXECUTION_RESULT(Failed) \
  451. __ENUMERATE_EXECUTION_RESULT(Failed_ExecuteLowPrioForks) \
  452. __ENUMERATE_EXECUTION_RESULT(Succeeded)
  453. enum class ExecutionResult : u8 {
  454. #define __ENUMERATE_EXECUTION_RESULT(x) x,
  455. ENUMERATE_EXECUTION_RESULTS
  456. #undef __ENUMERATE_EXECUTION_RESULT
  457. };
  458. char const* execution_result_name(ExecutionResult result);
  459. char const* opcode_id_name(OpCodeId opcode_id);
  460. char const* boundary_check_type_name(BoundaryCheckType);
  461. char const* character_compare_type_name(CharacterCompareType result);
  462. char const* execution_result_name(ExecutionResult result);
  463. class OpCode {
  464. public:
  465. OpCode() = default;
  466. virtual ~OpCode() = default;
  467. virtual OpCodeId opcode_id() const = 0;
  468. virtual size_t size() const = 0;
  469. virtual ExecutionResult execute(MatchInput const& input, MatchState& state) const = 0;
  470. ALWAYS_INLINE ByteCodeValueType argument(size_t offset) const
  471. {
  472. VERIFY(state().instruction_position + offset <= m_bytecode->size());
  473. return m_bytecode->at(state().instruction_position + 1 + offset);
  474. }
  475. ALWAYS_INLINE char const* name() const;
  476. static char const* name(OpCodeId);
  477. ALWAYS_INLINE void set_state(MatchState& state) { m_state = &state; }
  478. ALWAYS_INLINE void set_bytecode(ByteCode& bytecode) { m_bytecode = &bytecode; }
  479. ALWAYS_INLINE MatchState const& state() const
  480. {
  481. VERIFY(m_state);
  482. return *m_state;
  483. }
  484. String to_string() const
  485. {
  486. return String::formatted("[{:#02X}] {}", (int)opcode_id(), name(opcode_id()));
  487. }
  488. virtual String arguments_string() const = 0;
  489. ALWAYS_INLINE ByteCode const& bytecode() const { return *m_bytecode; }
  490. protected:
  491. ByteCode* m_bytecode { nullptr };
  492. MatchState* m_state { nullptr };
  493. };
  494. class OpCode_Exit final : public OpCode {
  495. public:
  496. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  497. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Exit; }
  498. ALWAYS_INLINE size_t size() const override { return 1; }
  499. String arguments_string() const override { return String::empty(); }
  500. };
  501. class OpCode_FailForks final : public OpCode {
  502. public:
  503. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  504. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::FailForks; }
  505. ALWAYS_INLINE size_t size() const override { return 2; }
  506. ALWAYS_INLINE size_t count() const { return argument(0); }
  507. String arguments_string() const override { return String::formatted("count={}", count()); }
  508. };
  509. class OpCode_Save final : public OpCode {
  510. public:
  511. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  512. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Save; }
  513. ALWAYS_INLINE size_t size() const override { return 1; }
  514. String arguments_string() const override { return String::empty(); }
  515. };
  516. class OpCode_Restore final : public OpCode {
  517. public:
  518. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  519. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Restore; }
  520. ALWAYS_INLINE size_t size() const override { return 1; }
  521. String arguments_string() const override { return String::empty(); }
  522. };
  523. class OpCode_GoBack final : public OpCode {
  524. public:
  525. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  526. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::GoBack; }
  527. ALWAYS_INLINE size_t size() const override { return 2; }
  528. ALWAYS_INLINE size_t count() const { return argument(0); }
  529. String arguments_string() const override { return String::formatted("count={}", count()); }
  530. };
  531. class OpCode_Jump final : public OpCode {
  532. public:
  533. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  534. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Jump; }
  535. ALWAYS_INLINE size_t size() const override { return 2; }
  536. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  537. String arguments_string() const override
  538. {
  539. return String::formatted("offset={} [&{}]", offset(), state().instruction_position + size() + offset());
  540. }
  541. };
  542. class OpCode_ForkJump : public OpCode {
  543. public:
  544. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  545. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkJump; }
  546. ALWAYS_INLINE size_t size() const override { return 2; }
  547. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  548. String arguments_string() const override
  549. {
  550. return String::formatted("offset={} [&{}], sp: {}", offset(), state().instruction_position + size() + offset(), state().string_position);
  551. }
  552. };
  553. class OpCode_ForkReplaceJump final : public OpCode_ForkJump {
  554. public:
  555. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  556. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkReplaceJump; }
  557. };
  558. class OpCode_ForkStay : public OpCode {
  559. public:
  560. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  561. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkStay; }
  562. ALWAYS_INLINE size_t size() const override { return 2; }
  563. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  564. String arguments_string() const override
  565. {
  566. return String::formatted("offset={} [&{}], sp: {}", offset(), state().instruction_position + size() + offset(), state().string_position);
  567. }
  568. };
  569. class OpCode_ForkReplaceStay final : public OpCode_ForkStay {
  570. public:
  571. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  572. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkReplaceStay; }
  573. };
  574. class OpCode_CheckBegin final : public OpCode {
  575. public:
  576. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  577. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckBegin; }
  578. ALWAYS_INLINE size_t size() const override { return 1; }
  579. String arguments_string() const override { return String::empty(); }
  580. };
  581. class OpCode_CheckEnd final : public OpCode {
  582. public:
  583. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  584. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckEnd; }
  585. ALWAYS_INLINE size_t size() const override { return 1; }
  586. String arguments_string() const override { return String::empty(); }
  587. };
  588. class OpCode_CheckBoundary final : public OpCode {
  589. public:
  590. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  591. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckBoundary; }
  592. ALWAYS_INLINE size_t size() const override { return 2; }
  593. ALWAYS_INLINE size_t arguments_count() const { return 1; }
  594. ALWAYS_INLINE BoundaryCheckType type() const { return static_cast<BoundaryCheckType>(argument(0)); }
  595. String arguments_string() const override { return String::formatted("kind={} ({})", (long unsigned int)argument(0), boundary_check_type_name(type())); }
  596. };
  597. class OpCode_ClearCaptureGroup final : public OpCode {
  598. public:
  599. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  600. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ClearCaptureGroup; }
  601. ALWAYS_INLINE size_t size() const override { return 2; }
  602. ALWAYS_INLINE size_t id() const { return argument(0); }
  603. String arguments_string() const override { return String::formatted("id={}", id()); }
  604. };
  605. class OpCode_SaveLeftCaptureGroup final : public OpCode {
  606. public:
  607. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  608. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveLeftCaptureGroup; }
  609. ALWAYS_INLINE size_t size() const override { return 2; }
  610. ALWAYS_INLINE size_t id() const { return argument(0); }
  611. String arguments_string() const override { return String::formatted("id={}", id()); }
  612. };
  613. class OpCode_SaveRightCaptureGroup final : public OpCode {
  614. public:
  615. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  616. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveRightCaptureGroup; }
  617. ALWAYS_INLINE size_t size() const override { return 2; }
  618. ALWAYS_INLINE size_t id() const { return argument(0); }
  619. String arguments_string() const override { return String::formatted("id={}", id()); }
  620. };
  621. class OpCode_SaveRightNamedCaptureGroup final : public OpCode {
  622. public:
  623. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  624. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveRightNamedCaptureGroup; }
  625. ALWAYS_INLINE size_t size() const override { return 4; }
  626. ALWAYS_INLINE StringView name() const { return { reinterpret_cast<char*>(argument(0)), length() }; }
  627. ALWAYS_INLINE size_t length() const { return argument(1); }
  628. ALWAYS_INLINE size_t id() const { return argument(2); }
  629. String arguments_string() const override
  630. {
  631. return String::formatted("name={}, length={}", name(), length());
  632. }
  633. };
  634. class OpCode_Compare final : public OpCode {
  635. public:
  636. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  637. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Compare; }
  638. ALWAYS_INLINE size_t size() const override { return arguments_size() + 3; }
  639. ALWAYS_INLINE size_t arguments_count() const { return argument(0); }
  640. ALWAYS_INLINE size_t arguments_size() const { return argument(1); }
  641. String arguments_string() const override;
  642. Vector<String> variable_arguments_to_string(Optional<MatchInput> input = {}) const;
  643. Vector<CompareTypeAndValuePair> flat_compares() const;
  644. private:
  645. ALWAYS_INLINE static void compare_char(MatchInput const& input, MatchState& state, u32 ch1, bool inverse, bool& inverse_matched);
  646. ALWAYS_INLINE static bool compare_string(MatchInput const& input, MatchState& state, RegexStringView str, bool& had_zero_length_match);
  647. ALWAYS_INLINE static void compare_character_class(MatchInput const& input, MatchState& state, CharClass character_class, u32 ch, bool inverse, bool& inverse_matched);
  648. ALWAYS_INLINE static void compare_character_range(MatchInput const& input, MatchState& state, u32 from, u32 to, u32 ch, bool inverse, bool& inverse_matched);
  649. ALWAYS_INLINE static void compare_property(MatchInput const& input, MatchState& state, Unicode::Property property, bool inverse, bool& inverse_matched);
  650. ALWAYS_INLINE static void compare_general_category(MatchInput const& input, MatchState& state, Unicode::GeneralCategory general_category, bool inverse, bool& inverse_matched);
  651. ALWAYS_INLINE static void compare_script(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched);
  652. ALWAYS_INLINE static void compare_script_extension(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched);
  653. };
  654. class OpCode_Repeat : public OpCode {
  655. public:
  656. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  657. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Repeat; }
  658. ALWAYS_INLINE size_t size() const override { return 4; }
  659. ALWAYS_INLINE size_t offset() const { return argument(0); }
  660. ALWAYS_INLINE u64 count() const { return argument(1); }
  661. ALWAYS_INLINE size_t id() const { return argument(2); }
  662. String arguments_string() const override
  663. {
  664. auto reps = id() < state().repetition_marks.size() ? state().repetition_marks.at(id()) : 0;
  665. return String::formatted("offset={} count={} id={} rep={}, sp: {}", offset(), count() + 1, id(), reps + 1, state().string_position);
  666. }
  667. };
  668. class OpCode_ResetRepeat : public OpCode {
  669. public:
  670. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  671. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ResetRepeat; }
  672. ALWAYS_INLINE size_t size() const override { return 2; }
  673. ALWAYS_INLINE size_t id() const { return argument(0); }
  674. String arguments_string() const override
  675. {
  676. auto reps = id() < state().repetition_marks.size() ? state().repetition_marks.at(id()) : 0;
  677. return String::formatted("id={} rep={}", id(), reps + 1);
  678. }
  679. };
  680. class OpCode_Checkpoint final : public OpCode {
  681. public:
  682. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  683. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Checkpoint; }
  684. ALWAYS_INLINE size_t size() const override { return 1; }
  685. String arguments_string() const override { return String::empty(); }
  686. };
  687. class OpCode_JumpNonEmpty final : public OpCode {
  688. public:
  689. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  690. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::JumpNonEmpty; }
  691. ALWAYS_INLINE size_t size() const override { return 4; }
  692. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  693. ALWAYS_INLINE ssize_t checkpoint() const { return argument(1); }
  694. ALWAYS_INLINE OpCodeId form() const { return (OpCodeId)argument(2); }
  695. String arguments_string() const override
  696. {
  697. return String::formatted("{} offset={} [&{}], cp={} [&{}]",
  698. opcode_id_name(form()),
  699. offset(), state().instruction_position + size() + offset(),
  700. checkpoint(), state().instruction_position + size() + checkpoint());
  701. }
  702. };
  703. template<typename T>
  704. bool is(OpCode const&);
  705. template<typename T>
  706. ALWAYS_INLINE bool is(OpCode const&)
  707. {
  708. return false;
  709. }
  710. template<typename T>
  711. ALWAYS_INLINE bool is(OpCode const* opcode)
  712. {
  713. return is<T>(*opcode);
  714. }
  715. template<>
  716. ALWAYS_INLINE bool is<OpCode_ForkStay>(OpCode const& opcode)
  717. {
  718. return opcode.opcode_id() == OpCodeId::ForkStay;
  719. }
  720. template<>
  721. ALWAYS_INLINE bool is<OpCode_Exit>(OpCode const& opcode)
  722. {
  723. return opcode.opcode_id() == OpCodeId::Exit;
  724. }
  725. template<>
  726. ALWAYS_INLINE bool is<OpCode_Compare>(OpCode const& opcode)
  727. {
  728. return opcode.opcode_id() == OpCodeId::Compare;
  729. }
  730. template<typename T>
  731. ALWAYS_INLINE T const& to(OpCode const& opcode)
  732. {
  733. return verify_cast<T>(opcode);
  734. }
  735. template<typename T>
  736. ALWAYS_INLINE T* to(OpCode* opcode)
  737. {
  738. return verify_cast<T>(opcode);
  739. }
  740. template<typename T>
  741. ALWAYS_INLINE T const* to(OpCode const* opcode)
  742. {
  743. return verify_cast<T>(opcode);
  744. }
  745. template<typename T>
  746. ALWAYS_INLINE T& to(OpCode& opcode)
  747. {
  748. return verify_cast<T>(opcode);
  749. }
  750. }