RegexByteCode.h 30 KB

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