RegexOptimizer.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <AK/RedBlackTree.h>
  8. #include <AK/Stack.h>
  9. #include <LibRegex/Regex.h>
  10. #include <LibRegex/RegexBytecodeStreamOptimizer.h>
  11. #if REGEX_DEBUG
  12. # include <AK/ScopeGuard.h>
  13. # include <AK/ScopeLogger.h>
  14. #endif
  15. namespace regex {
  16. using Detail::Block;
  17. template<typename Parser>
  18. void Regex<Parser>::run_optimization_passes()
  19. {
  20. parser_result.bytecode.flatten();
  21. // Rewrite fork loops as atomic groups
  22. // e.g. a*b -> (ATOMIC a*)b
  23. attempt_rewrite_loops_as_atomic_groups(split_basic_blocks(parser_result.bytecode));
  24. parser_result.bytecode.flatten();
  25. }
  26. template<typename Parser>
  27. typename Regex<Parser>::BasicBlockList Regex<Parser>::split_basic_blocks(ByteCode const& bytecode)
  28. {
  29. BasicBlockList block_boundaries;
  30. size_t end_of_last_block = 0;
  31. auto bytecode_size = bytecode.size();
  32. MatchState state;
  33. state.instruction_position = 0;
  34. auto check_jump = [&]<typename T>(OpCode const& opcode) {
  35. auto& op = static_cast<T const&>(opcode);
  36. ssize_t jump_offset = op.size() + op.offset();
  37. if (jump_offset >= 0) {
  38. block_boundaries.append({ end_of_last_block, state.instruction_position });
  39. end_of_last_block = state.instruction_position + opcode.size();
  40. } else {
  41. // This op jumps back, see if that's within this "block".
  42. if (jump_offset + state.instruction_position > end_of_last_block) {
  43. // Split the block!
  44. block_boundaries.append({ end_of_last_block, jump_offset + state.instruction_position });
  45. block_boundaries.append({ jump_offset + state.instruction_position, state.instruction_position });
  46. end_of_last_block = state.instruction_position + opcode.size();
  47. } else {
  48. // Nope, it's just a jump to another block
  49. block_boundaries.append({ end_of_last_block, state.instruction_position });
  50. end_of_last_block = state.instruction_position + opcode.size();
  51. }
  52. }
  53. };
  54. for (;;) {
  55. auto& opcode = bytecode.get_opcode(state);
  56. switch (opcode.opcode_id()) {
  57. case OpCodeId::Jump:
  58. check_jump.template operator()<OpCode_Jump>(opcode);
  59. break;
  60. case OpCodeId::JumpNonEmpty:
  61. check_jump.template operator()<OpCode_JumpNonEmpty>(opcode);
  62. break;
  63. case OpCodeId::ForkJump:
  64. check_jump.template operator()<OpCode_ForkJump>(opcode);
  65. break;
  66. case OpCodeId::ForkStay:
  67. check_jump.template operator()<OpCode_ForkStay>(opcode);
  68. break;
  69. case OpCodeId::FailForks:
  70. block_boundaries.append({ end_of_last_block, state.instruction_position });
  71. end_of_last_block = state.instruction_position + opcode.size();
  72. break;
  73. case OpCodeId::Repeat: {
  74. // Repeat produces two blocks, one containing its repeated expr, and one after that.
  75. auto repeat_start = state.instruction_position - static_cast<OpCode_Repeat const&>(opcode).offset();
  76. if (repeat_start > end_of_last_block)
  77. block_boundaries.append({ end_of_last_block, repeat_start });
  78. block_boundaries.append({ repeat_start, state.instruction_position });
  79. end_of_last_block = state.instruction_position + opcode.size();
  80. break;
  81. }
  82. default:
  83. break;
  84. }
  85. auto next_ip = state.instruction_position + opcode.size();
  86. if (next_ip < bytecode_size)
  87. state.instruction_position = next_ip;
  88. else
  89. break;
  90. }
  91. if (end_of_last_block < bytecode_size)
  92. block_boundaries.append({ end_of_last_block, bytecode_size });
  93. quick_sort(block_boundaries, [](auto& a, auto& b) { return a.start < b.start; });
  94. return block_boundaries;
  95. }
  96. enum class AtomicRewritePreconditionResult {
  97. SatisfiedWithProperHeader,
  98. SatisfiedWithEmptyHeader,
  99. NotSatisfied,
  100. };
  101. static AtomicRewritePreconditionResult block_satisfies_atomic_rewrite_precondition(ByteCode const& bytecode, Block const& repeated_block, Block const& following_block)
  102. {
  103. Vector<Vector<CompareTypeAndValuePair>> repeated_values;
  104. HashTable<size_t> active_capture_groups;
  105. MatchState state;
  106. for (state.instruction_position = repeated_block.start; state.instruction_position < repeated_block.end;) {
  107. auto& opcode = bytecode.get_opcode(state);
  108. switch (opcode.opcode_id()) {
  109. case OpCodeId::Compare: {
  110. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  111. if (repeated_values.is_empty() && any_of(compares, [](auto& compare) { return compare.type == CharacterCompareType::AnyChar; }))
  112. return AtomicRewritePreconditionResult::NotSatisfied;
  113. repeated_values.append(move(compares));
  114. break;
  115. }
  116. case OpCodeId::CheckBegin:
  117. case OpCodeId::CheckEnd:
  118. if (repeated_values.is_empty())
  119. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  120. break;
  121. case OpCodeId::CheckBoundary:
  122. // FIXME: What should we do with these? for now, let's fail.
  123. return AtomicRewritePreconditionResult::NotSatisfied;
  124. case OpCodeId::Restore:
  125. case OpCodeId::GoBack:
  126. return AtomicRewritePreconditionResult::NotSatisfied;
  127. case OpCodeId::SaveRightCaptureGroup:
  128. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  129. break;
  130. case OpCodeId::SaveLeftCaptureGroup:
  131. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  132. break;
  133. default:
  134. break;
  135. }
  136. state.instruction_position += opcode.size();
  137. }
  138. dbgln_if(REGEX_DEBUG, "Found {} entries in reference", repeated_values.size());
  139. dbgln_if(REGEX_DEBUG, "Found {} active capture groups", active_capture_groups.size());
  140. bool following_block_has_at_least_one_compare = false;
  141. // Find the first compare in the following block, it must NOT match any of the values in `repeated_values'.
  142. for (state.instruction_position = following_block.start; state.instruction_position < following_block.end;) {
  143. auto& opcode = bytecode.get_opcode(state);
  144. switch (opcode.opcode_id()) {
  145. // Note: These have to exist since we're effectively repeating the following block as well
  146. case OpCodeId::SaveRightCaptureGroup:
  147. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  148. break;
  149. case OpCodeId::SaveLeftCaptureGroup:
  150. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  151. break;
  152. case OpCodeId::Compare: {
  153. following_block_has_at_least_one_compare = true;
  154. // We found a compare, let's see what it has.
  155. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  156. if (compares.is_empty())
  157. break;
  158. if (any_of(compares, [&](auto& compare) {
  159. return compare.type == CharacterCompareType::AnyChar
  160. || (compare.type == CharacterCompareType::Reference && active_capture_groups.contains(compare.value));
  161. }))
  162. return AtomicRewritePreconditionResult::NotSatisfied;
  163. for (auto& repeated_value : repeated_values) {
  164. // FIXME: This is too naive!
  165. if (any_of(repeated_value, [](auto& compare) { return compare.type == CharacterCompareType::AnyChar; }))
  166. return AtomicRewritePreconditionResult::NotSatisfied;
  167. for (auto& repeated_compare : repeated_value) {
  168. // FIXME: This is too naive! it will miss _tons_ of cases since it doesn't check ranges!
  169. if (any_of(compares, [&](auto& compare) { return compare.type == repeated_compare.type && compare.value == repeated_compare.value; }))
  170. return AtomicRewritePreconditionResult::NotSatisfied;
  171. }
  172. }
  173. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  174. }
  175. case OpCodeId::CheckBegin:
  176. case OpCodeId::CheckEnd:
  177. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader; // Nothing can match the end!
  178. case OpCodeId::CheckBoundary:
  179. // FIXME: What should we do with these? For now, consider them a failure.
  180. return AtomicRewritePreconditionResult::NotSatisfied;
  181. default:
  182. break;
  183. }
  184. state.instruction_position += opcode.size();
  185. }
  186. if (following_block_has_at_least_one_compare)
  187. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  188. return AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader;
  189. }
  190. template<typename Parser>
  191. void Regex<Parser>::attempt_rewrite_loops_as_atomic_groups(BasicBlockList const& basic_blocks)
  192. {
  193. auto& bytecode = parser_result.bytecode;
  194. if constexpr (REGEX_DEBUG) {
  195. RegexDebug dbg;
  196. dbg.print_bytecode(*this);
  197. for (auto const& block : basic_blocks)
  198. dbgln("block from {} to {}", block.start, block.end);
  199. }
  200. // A pattern such as:
  201. // bb0 | RE0
  202. // | ForkX bb0
  203. // -------------------------
  204. // bb1 | RE1
  205. // can be rewritten as:
  206. // -------------------------
  207. // bb0 | RE0
  208. // | ForkReplaceX bb0
  209. // -------------------------
  210. // bb1 | RE1
  211. // provided that first(RE1) not-in end(RE0), which is to say
  212. // that RE1 cannot start with whatever RE0 has matched (ever).
  213. //
  214. // Alternatively, a second form of this pattern can also occur:
  215. // bb0 | *
  216. // | ForkX bb2
  217. // ------------------------
  218. // bb1 | RE0
  219. // | Jump bb0
  220. // ------------------------
  221. // bb2 | RE1
  222. // which can be transformed (with the same preconditions) to:
  223. // bb0 | *
  224. // | ForkReplaceX bb2
  225. // ------------------------
  226. // bb1 | RE0
  227. // | Jump bb0
  228. // ------------------------
  229. // bb2 | RE1
  230. enum class AlternateForm {
  231. DirectLoopWithoutHeader, // loop without proper header, a block forking to itself. i.e. the first form.
  232. DirectLoopWithoutHeaderAndEmptyFollow, // loop without proper header, a block forking to itself. i.e. the first form but with RE1 being empty.
  233. DirectLoopWithHeader, // loop with proper header, i.e. the second form.
  234. };
  235. struct CandidateBlock {
  236. Block forking_block;
  237. Optional<Block> new_target_block;
  238. AlternateForm form;
  239. };
  240. Vector<CandidateBlock> candidate_blocks;
  241. auto is_an_eligible_jump = [](OpCode const& opcode, size_t ip, size_t block_start, AlternateForm alternate_form) {
  242. switch (opcode.opcode_id()) {
  243. case OpCodeId::JumpNonEmpty: {
  244. auto const& op = static_cast<OpCode_JumpNonEmpty const&>(opcode);
  245. auto form = op.form();
  246. if (form != OpCodeId::Jump && alternate_form == AlternateForm::DirectLoopWithHeader)
  247. return false;
  248. if (form != OpCodeId::ForkJump && form != OpCodeId::ForkStay && alternate_form == AlternateForm::DirectLoopWithoutHeader)
  249. return false;
  250. return op.offset() + ip + opcode.size() == block_start;
  251. }
  252. case OpCodeId::ForkJump:
  253. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  254. return false;
  255. return static_cast<OpCode_ForkJump const&>(opcode).offset() + ip + opcode.size() == block_start;
  256. case OpCodeId::ForkStay:
  257. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  258. return false;
  259. return static_cast<OpCode_ForkStay const&>(opcode).offset() + ip + opcode.size() == block_start;
  260. case OpCodeId::Jump:
  261. // Infinite loop does *not* produce forks.
  262. if (alternate_form == AlternateForm::DirectLoopWithoutHeader)
  263. return false;
  264. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  265. return static_cast<OpCode_Jump const&>(opcode).offset() + ip + opcode.size() == block_start;
  266. VERIFY_NOT_REACHED();
  267. default:
  268. return false;
  269. }
  270. };
  271. for (size_t i = 0; i < basic_blocks.size(); ++i) {
  272. auto forking_block = basic_blocks[i];
  273. Optional<Block> fork_fallback_block;
  274. if (i + 1 < basic_blocks.size())
  275. fork_fallback_block = basic_blocks[i + 1];
  276. MatchState state;
  277. // Check if the last instruction in this block is a jump to the block itself:
  278. {
  279. state.instruction_position = forking_block.end;
  280. auto& opcode = bytecode.get_opcode(state);
  281. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithoutHeader)) {
  282. // We've found RE0 (and RE1 is just the following block, if any), let's see if the precondition applies.
  283. // if RE1 is empty, there's no first(RE1), so this is an automatic pass.
  284. if (!fork_fallback_block.has_value() || fork_fallback_block->end == fork_fallback_block->start) {
  285. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  286. break;
  287. }
  288. auto precondition = block_satisfies_atomic_rewrite_precondition(bytecode, forking_block, *fork_fallback_block);
  289. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithProperHeader) {
  290. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  291. break;
  292. }
  293. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader) {
  294. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeaderAndEmptyFollow });
  295. break;
  296. }
  297. }
  298. }
  299. // Check if the last instruction in the last block is a direct jump to this block
  300. if (fork_fallback_block.has_value()) {
  301. state.instruction_position = fork_fallback_block->end;
  302. auto& opcode = bytecode.get_opcode(state);
  303. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithHeader)) {
  304. // We've found bb1 and bb0, let's just make sure that bb0 forks to bb2.
  305. state.instruction_position = forking_block.end;
  306. auto& opcode = bytecode.get_opcode(state);
  307. if (opcode.opcode_id() == OpCodeId::ForkJump || opcode.opcode_id() == OpCodeId::ForkStay) {
  308. Optional<Block> block_following_fork_fallback;
  309. if (i + 2 < basic_blocks.size())
  310. block_following_fork_fallback = basic_blocks[i + 2];
  311. if (!block_following_fork_fallback.has_value()
  312. || block_satisfies_atomic_rewrite_precondition(bytecode, *fork_fallback_block, *block_following_fork_fallback) != AtomicRewritePreconditionResult::NotSatisfied) {
  313. candidate_blocks.append({ forking_block, {}, AlternateForm::DirectLoopWithHeader });
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. dbgln_if(REGEX_DEBUG, "Found {} candidate blocks", candidate_blocks.size());
  321. if (candidate_blocks.is_empty()) {
  322. dbgln_if(REGEX_DEBUG, "Failed to find anything for {}", pattern_value);
  323. return;
  324. }
  325. RedBlackTree<size_t, size_t> needed_patches;
  326. // Reverse the blocks, so we can patch the bytecode without messing with the latter patches.
  327. quick_sort(candidate_blocks, [](auto& a, auto& b) { return b.forking_block.start > a.forking_block.start; });
  328. for (auto& candidate : candidate_blocks) {
  329. // Note that both forms share a ForkReplace patch in forking_block.
  330. // Patch the ForkX in forking_block to be a ForkReplaceX instead.
  331. auto& opcode_id = bytecode[candidate.forking_block.end];
  332. if (opcode_id == (ByteCodeValueType)OpCodeId::ForkStay) {
  333. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  334. } else if (opcode_id == (ByteCodeValueType)OpCodeId::ForkJump) {
  335. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  336. } else if (opcode_id == (ByteCodeValueType)OpCodeId::JumpNonEmpty) {
  337. auto& jump_opcode_id = bytecode[candidate.forking_block.end + 3];
  338. if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkStay)
  339. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  340. else if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkJump)
  341. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  342. else
  343. VERIFY_NOT_REACHED();
  344. } else {
  345. VERIFY_NOT_REACHED();
  346. }
  347. }
  348. if (!needed_patches.is_empty()) {
  349. MatchState state;
  350. auto bytecode_size = bytecode.size();
  351. state.instruction_position = 0;
  352. struct Patch {
  353. ssize_t value;
  354. size_t offset;
  355. bool should_negate { false };
  356. };
  357. for (;;) {
  358. if (state.instruction_position >= bytecode_size)
  359. break;
  360. auto& opcode = bytecode.get_opcode(state);
  361. Stack<Patch, 2> patch_points;
  362. switch (opcode.opcode_id()) {
  363. case OpCodeId::Jump:
  364. patch_points.push({ static_cast<OpCode_Jump const&>(opcode).offset(), state.instruction_position + 1 });
  365. break;
  366. case OpCodeId::JumpNonEmpty:
  367. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).offset(), state.instruction_position + 1 });
  368. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).checkpoint(), state.instruction_position + 2 });
  369. break;
  370. case OpCodeId::ForkJump:
  371. patch_points.push({ static_cast<OpCode_ForkJump const&>(opcode).offset(), state.instruction_position + 1 });
  372. break;
  373. case OpCodeId::ForkStay:
  374. patch_points.push({ static_cast<OpCode_ForkStay const&>(opcode).offset(), state.instruction_position + 1 });
  375. break;
  376. case OpCodeId::Repeat:
  377. patch_points.push({ -(ssize_t) static_cast<OpCode_Repeat const&>(opcode).offset(), state.instruction_position + 1, true });
  378. break;
  379. default:
  380. break;
  381. }
  382. while (!patch_points.is_empty()) {
  383. auto& patch_point = patch_points.top();
  384. auto target_offset = patch_point.value + state.instruction_position + opcode.size();
  385. constexpr auto do_patch = [](auto& patch_it, auto& patch_point, auto& target_offset, auto& bytecode, auto ip) {
  386. if (patch_it.key() == ip)
  387. return;
  388. if (patch_point.value < 0 && target_offset <= patch_it.key() && ip > patch_it.key())
  389. bytecode[patch_point.offset] += (patch_point.should_negate ? 1 : -1) * (*patch_it);
  390. else if (patch_point.value > 0 && target_offset >= patch_it.key() && ip < patch_it.key())
  391. bytecode[patch_point.offset] += (patch_point.should_negate ? -1 : 1) * (*patch_it);
  392. };
  393. if (auto patch_it = needed_patches.find_largest_not_above_iterator(target_offset); !patch_it.is_end())
  394. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  395. else if (auto patch_it = needed_patches.find_largest_not_above_iterator(state.instruction_position); !patch_it.is_end())
  396. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  397. patch_points.pop();
  398. }
  399. state.instruction_position += opcode.size();
  400. }
  401. }
  402. if constexpr (REGEX_DEBUG) {
  403. warnln("Transformed to:");
  404. RegexDebug dbg;
  405. dbg.print_bytecode(*this);
  406. }
  407. }
  408. void Optimizer::append_alternation(ByteCode& target, ByteCode&& left, ByteCode&& right)
  409. {
  410. Array<ByteCode, 2> alternatives;
  411. alternatives[0] = move(left);
  412. alternatives[1] = move(right);
  413. append_alternation(target, alternatives);
  414. }
  415. void Optimizer::append_alternation(ByteCode& target, Span<ByteCode> alternatives)
  416. {
  417. if (alternatives.size() == 0)
  418. return;
  419. if (alternatives.size() == 1)
  420. return target.extend(move(alternatives[0]));
  421. if (all_of(alternatives, [](auto& x) { return x.is_empty(); }))
  422. return;
  423. for (auto& entry : alternatives)
  424. entry.flatten();
  425. #if REGEX_DEBUG
  426. ScopeLogger<true> log;
  427. warnln("Alternations:");
  428. RegexDebug dbg;
  429. for (auto& entry : alternatives) {
  430. warnln("----------");
  431. dbg.print_bytecode(entry);
  432. }
  433. ScopeGuard print_at_end {
  434. [&] {
  435. warnln("======================");
  436. RegexDebug dbg;
  437. dbg.print_bytecode(target);
  438. }
  439. };
  440. #endif
  441. Vector<Vector<Detail::Block>> basic_blocks;
  442. basic_blocks.ensure_capacity(alternatives.size());
  443. for (auto& entry : alternatives)
  444. basic_blocks.append(Regex<PosixBasicParser>::split_basic_blocks(entry));
  445. size_t left_skip = 0;
  446. size_t shared_block_count = basic_blocks.first().size();
  447. for (auto& entry : basic_blocks)
  448. shared_block_count = min(shared_block_count, entry.size());
  449. MatchState state;
  450. for (size_t block_index = 0; block_index < shared_block_count; block_index++) {
  451. auto& left_block = basic_blocks.first()[block_index];
  452. auto left_end = block_index + 1 == basic_blocks.first().size() ? left_block.end : basic_blocks.first()[block_index + 1].start;
  453. auto can_continue = true;
  454. for (size_t i = 1; i < alternatives.size(); ++i) {
  455. auto& right_blocks = basic_blocks[i];
  456. auto& right_block = right_blocks[block_index];
  457. auto right_end = block_index + 1 == right_blocks.size() ? right_block.end : right_blocks[block_index + 1].start;
  458. if (left_end - left_block.start != right_end - right_block.start) {
  459. can_continue = false;
  460. break;
  461. }
  462. if (alternatives[0].spans().slice(left_block.start, left_end - left_block.start) != alternatives[i].spans().slice(right_block.start, right_end - right_block.start)) {
  463. can_continue = false;
  464. break;
  465. }
  466. }
  467. if (!can_continue)
  468. break;
  469. size_t i = 0;
  470. for (auto& entry : alternatives) {
  471. auto& blocks = basic_blocks[i];
  472. auto& block = blocks[block_index];
  473. auto end = block_index + 1 == blocks.size() ? block.end : blocks[block_index + 1].start;
  474. state.instruction_position = block.start;
  475. size_t skip = 0;
  476. while (state.instruction_position < end) {
  477. auto& opcode = entry.get_opcode(state);
  478. state.instruction_position += opcode.size();
  479. skip = state.instruction_position;
  480. }
  481. left_skip = min(skip, left_skip);
  482. }
  483. }
  484. dbgln_if(REGEX_DEBUG, "Skipping {}/{} bytecode entries from {}", left_skip, 0, alternatives[0].size());
  485. if (left_skip > 0) {
  486. target.extend(alternatives[0].release_slice(basic_blocks.first().first().start, left_skip));
  487. auto first = true;
  488. for (auto& entry : alternatives) {
  489. if (first) {
  490. first = false;
  491. continue;
  492. }
  493. entry = entry.release_slice(left_skip);
  494. }
  495. }
  496. if (all_of(alternatives, [](auto& entry) { return entry.is_empty(); }))
  497. return;
  498. size_t patch_start = target.size();
  499. for (size_t i = 1; i < alternatives.size(); ++i) {
  500. target.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  501. target.empend(0u); // To be filled later.
  502. }
  503. size_t size_to_jump = 0;
  504. bool seen_one_empty = false;
  505. for (size_t i = alternatives.size(); i > 0; --i) {
  506. auto& entry = alternatives[i - 1];
  507. if (entry.is_empty()) {
  508. if (seen_one_empty)
  509. continue;
  510. seen_one_empty = true;
  511. }
  512. auto is_first = i == 1;
  513. auto instruction_size = entry.size() + (is_first ? 0 : 2); // Jump; -> +2
  514. size_to_jump += instruction_size;
  515. if (!is_first)
  516. target[patch_start + (i - 2) * 2 + 1] = size_to_jump + (alternatives.size() - i) * 2;
  517. dbgln_if(REGEX_DEBUG, "{} size = {}, cum={}", i - 1, instruction_size, size_to_jump);
  518. }
  519. seen_one_empty = false;
  520. for (size_t i = alternatives.size(); i > 0; --i) {
  521. auto& chunk = alternatives[i - 1];
  522. if (chunk.is_empty()) {
  523. if (seen_one_empty)
  524. continue;
  525. seen_one_empty = true;
  526. }
  527. ByteCode* previous_chunk = nullptr;
  528. size_t j = i - 1;
  529. auto seen_one_empty_before = chunk.is_empty();
  530. while (j >= 1) {
  531. --j;
  532. auto& candidate_chunk = alternatives[j];
  533. if (candidate_chunk.is_empty()) {
  534. if (seen_one_empty_before)
  535. continue;
  536. }
  537. previous_chunk = &candidate_chunk;
  538. break;
  539. }
  540. size_to_jump -= chunk.size() + (previous_chunk ? 2 : 0);
  541. target.extend(move(chunk));
  542. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Jump));
  543. target.empend(size_to_jump); // Jump to the _END label
  544. }
  545. }
  546. enum class LookupTableInsertionOutcome {
  547. Successful,
  548. ReplaceWithAnyChar,
  549. TemporaryInversionNeeded,
  550. PermanentInversionNeeded,
  551. CannotPlaceInTable,
  552. };
  553. static LookupTableInsertionOutcome insert_into_lookup_table(RedBlackTree<ByteCodeValueType, CharRange>& table, CompareTypeAndValuePair pair)
  554. {
  555. switch (pair.type) {
  556. case CharacterCompareType::Inverse:
  557. return LookupTableInsertionOutcome::PermanentInversionNeeded;
  558. case CharacterCompareType::TemporaryInverse:
  559. return LookupTableInsertionOutcome::TemporaryInversionNeeded;
  560. case CharacterCompareType::AnyChar:
  561. return LookupTableInsertionOutcome::ReplaceWithAnyChar;
  562. case CharacterCompareType::CharClass:
  563. return LookupTableInsertionOutcome::CannotPlaceInTable;
  564. case CharacterCompareType::Char:
  565. table.insert(pair.value, { (u32)pair.value, (u32)pair.value });
  566. break;
  567. case CharacterCompareType::CharRange: {
  568. CharRange range { pair.value };
  569. table.insert(range.from, range);
  570. break;
  571. }
  572. case CharacterCompareType::Reference:
  573. case CharacterCompareType::Property:
  574. case CharacterCompareType::GeneralCategory:
  575. case CharacterCompareType::Script:
  576. case CharacterCompareType::ScriptExtension:
  577. return LookupTableInsertionOutcome::CannotPlaceInTable;
  578. case CharacterCompareType::Undefined:
  579. case CharacterCompareType::RangeExpressionDummy:
  580. case CharacterCompareType::String:
  581. case CharacterCompareType::LookupTable:
  582. VERIFY_NOT_REACHED();
  583. }
  584. return LookupTableInsertionOutcome::Successful;
  585. }
  586. void Optimizer::append_character_class(ByteCode& target, Vector<CompareTypeAndValuePair>&& pairs)
  587. {
  588. ByteCode arguments;
  589. size_t argument_count = 0;
  590. if (pairs.size() <= 1) {
  591. for (auto& pair : pairs) {
  592. arguments.append(to_underlying(pair.type));
  593. if (pair.type != CharacterCompareType::AnyChar && pair.type != CharacterCompareType::TemporaryInverse && pair.type != CharacterCompareType::Inverse)
  594. arguments.append(pair.value);
  595. ++argument_count;
  596. }
  597. } else {
  598. RedBlackTree<ByteCodeValueType, CharRange> table;
  599. RedBlackTree<ByteCodeValueType, CharRange> inverted_table;
  600. auto* current_table = &table;
  601. auto* current_inverted_table = &inverted_table;
  602. bool invert_for_next_iteration = false;
  603. bool is_currently_inverted = false;
  604. for (auto& value : pairs) {
  605. auto should_invert_after_this_iteration = invert_for_next_iteration;
  606. invert_for_next_iteration = false;
  607. auto insertion_result = insert_into_lookup_table(*current_table, value);
  608. switch (insertion_result) {
  609. case LookupTableInsertionOutcome::Successful:
  610. break;
  611. case LookupTableInsertionOutcome::ReplaceWithAnyChar: {
  612. table.clear();
  613. inverted_table.clear();
  614. arguments.append(to_underlying(CharacterCompareType::AnyChar));
  615. ++argument_count;
  616. break;
  617. }
  618. case LookupTableInsertionOutcome::TemporaryInversionNeeded:
  619. swap(current_table, current_inverted_table);
  620. invert_for_next_iteration = true;
  621. is_currently_inverted = !is_currently_inverted;
  622. break;
  623. case LookupTableInsertionOutcome::PermanentInversionNeeded:
  624. swap(current_table, current_inverted_table);
  625. is_currently_inverted = !is_currently_inverted;
  626. break;
  627. case LookupTableInsertionOutcome::CannotPlaceInTable:
  628. if (is_currently_inverted) {
  629. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  630. ++argument_count;
  631. }
  632. arguments.append(to_underlying(value.type));
  633. arguments.append(value.value);
  634. ++argument_count;
  635. break;
  636. }
  637. if (should_invert_after_this_iteration) {
  638. swap(current_table, current_inverted_table);
  639. is_currently_inverted = !is_currently_inverted;
  640. }
  641. }
  642. auto append_table = [&](auto& table) {
  643. ++argument_count;
  644. arguments.append(to_underlying(CharacterCompareType::LookupTable));
  645. auto size_index = arguments.size();
  646. arguments.append(0);
  647. Optional<CharRange> active_range;
  648. size_t range_count = 0;
  649. for (auto& range : table) {
  650. if (!active_range.has_value()) {
  651. active_range = range;
  652. continue;
  653. }
  654. if (range.from <= active_range->to + 1 && range.to + 1 >= active_range->from) {
  655. active_range = CharRange { min(range.from, active_range->from), max(range.to, active_range->to) };
  656. } else {
  657. ++range_count;
  658. arguments.append(active_range.release_value());
  659. active_range = range;
  660. }
  661. }
  662. if (active_range.has_value()) {
  663. ++range_count;
  664. arguments.append(active_range.release_value());
  665. }
  666. arguments[size_index] = range_count;
  667. };
  668. if (!table.is_empty())
  669. append_table(table);
  670. if (!inverted_table.is_empty()) {
  671. ++argument_count;
  672. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  673. append_table(inverted_table);
  674. }
  675. }
  676. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Compare));
  677. target.empend(argument_count); // number of arguments
  678. target.empend(arguments.size()); // size of arguments
  679. target.extend(move(arguments));
  680. }
  681. template void Regex<PosixBasicParser>::run_optimization_passes();
  682. template void Regex<PosixExtendedParser>::run_optimization_passes();
  683. template void Regex<ECMA262Parser>::run_optimization_passes();
  684. }