RegexOptimizer.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/QuickSort.h>
  8. #include <AK/RedBlackTree.h>
  9. #include <AK/Stack.h>
  10. #include <LibRegex/Regex.h>
  11. #include <LibRegex/RegexBytecodeStreamOptimizer.h>
  12. #if REGEX_DEBUG
  13. # include <AK/ScopeGuard.h>
  14. # include <AK/ScopeLogger.h>
  15. #endif
  16. namespace regex {
  17. using Detail::Block;
  18. template<typename Parser>
  19. void Regex<Parser>::run_optimization_passes()
  20. {
  21. parser_result.bytecode.flatten();
  22. // Rewrite fork loops as atomic groups
  23. // e.g. a*b -> (ATOMIC a*)b
  24. attempt_rewrite_loops_as_atomic_groups(split_basic_blocks(parser_result.bytecode));
  25. parser_result.bytecode.flatten();
  26. }
  27. template<typename Parser>
  28. typename Regex<Parser>::BasicBlockList Regex<Parser>::split_basic_blocks(ByteCode const& bytecode)
  29. {
  30. BasicBlockList block_boundaries;
  31. size_t end_of_last_block = 0;
  32. auto bytecode_size = bytecode.size();
  33. MatchState state;
  34. state.instruction_position = 0;
  35. auto check_jump = [&]<typename T>(OpCode const& opcode) {
  36. auto& op = static_cast<T const&>(opcode);
  37. ssize_t jump_offset = op.size() + op.offset();
  38. if (jump_offset >= 0) {
  39. block_boundaries.append({ end_of_last_block, state.instruction_position });
  40. end_of_last_block = state.instruction_position + opcode.size();
  41. } else {
  42. // This op jumps back, see if that's within this "block".
  43. if (jump_offset + state.instruction_position > end_of_last_block) {
  44. // Split the block!
  45. block_boundaries.append({ end_of_last_block, jump_offset + state.instruction_position });
  46. block_boundaries.append({ jump_offset + state.instruction_position, state.instruction_position });
  47. end_of_last_block = state.instruction_position + opcode.size();
  48. } else {
  49. // Nope, it's just a jump to another block
  50. block_boundaries.append({ end_of_last_block, state.instruction_position });
  51. end_of_last_block = state.instruction_position + opcode.size();
  52. }
  53. }
  54. };
  55. for (;;) {
  56. auto& opcode = bytecode.get_opcode(state);
  57. switch (opcode.opcode_id()) {
  58. case OpCodeId::Jump:
  59. check_jump.template operator()<OpCode_Jump>(opcode);
  60. break;
  61. case OpCodeId::JumpNonEmpty:
  62. check_jump.template operator()<OpCode_JumpNonEmpty>(opcode);
  63. break;
  64. case OpCodeId::ForkJump:
  65. check_jump.template operator()<OpCode_ForkJump>(opcode);
  66. break;
  67. case OpCodeId::ForkStay:
  68. check_jump.template operator()<OpCode_ForkStay>(opcode);
  69. break;
  70. case OpCodeId::FailForks:
  71. block_boundaries.append({ end_of_last_block, state.instruction_position });
  72. end_of_last_block = state.instruction_position + opcode.size();
  73. break;
  74. case OpCodeId::Repeat: {
  75. // Repeat produces two blocks, one containing its repeated expr, and one after that.
  76. auto repeat_start = state.instruction_position - static_cast<OpCode_Repeat const&>(opcode).offset();
  77. if (repeat_start > end_of_last_block)
  78. block_boundaries.append({ end_of_last_block, repeat_start });
  79. block_boundaries.append({ repeat_start, state.instruction_position });
  80. end_of_last_block = state.instruction_position + opcode.size();
  81. break;
  82. }
  83. default:
  84. break;
  85. }
  86. auto next_ip = state.instruction_position + opcode.size();
  87. if (next_ip < bytecode_size)
  88. state.instruction_position = next_ip;
  89. else
  90. break;
  91. }
  92. if (end_of_last_block < bytecode_size)
  93. block_boundaries.append({ end_of_last_block, bytecode_size });
  94. quick_sort(block_boundaries, [](auto& a, auto& b) { return a.start < b.start; });
  95. return block_boundaries;
  96. }
  97. static bool has_overlap(Vector<CompareTypeAndValuePair> const& lhs, Vector<CompareTypeAndValuePair> const& rhs)
  98. {
  99. // We have to fully interpret the two sequences to determine if they overlap (that is, keep track of inversion state and what ranges they cover).
  100. bool inverse { false };
  101. bool temporary_inverse { false };
  102. bool reset_temporary_inverse { false };
  103. auto current_lhs_inversion_state = [&]() -> bool { return temporary_inverse ^ inverse; };
  104. RedBlackTree<u32, u32> lhs_ranges;
  105. RedBlackTree<u32, u32> lhs_negated_ranges;
  106. HashTable<CharClass> lhs_char_classes;
  107. HashTable<CharClass> lhs_negated_char_classes;
  108. auto range_contains = [&]<typename T>(T& value) -> bool {
  109. u32 start;
  110. u32 end;
  111. if constexpr (IsSame<T, CharRange>) {
  112. start = value.from;
  113. end = value.to;
  114. } else {
  115. start = value;
  116. end = value;
  117. }
  118. auto* max = lhs_ranges.find_smallest_not_below(start);
  119. return max && *max <= end;
  120. };
  121. auto char_class_contains = [&](CharClass const& value) -> bool {
  122. if (lhs_char_classes.contains(value))
  123. return true;
  124. if (lhs_negated_char_classes.contains(value))
  125. return false;
  126. // This char class might match something in the ranges we have, and checking that is far too expensive, so just bail out.
  127. return true;
  128. };
  129. for (auto const& pair : lhs) {
  130. if (reset_temporary_inverse) {
  131. reset_temporary_inverse = false;
  132. temporary_inverse = false;
  133. } else {
  134. reset_temporary_inverse = true;
  135. }
  136. switch (pair.type) {
  137. case CharacterCompareType::Inverse:
  138. inverse = !inverse;
  139. break;
  140. case CharacterCompareType::TemporaryInverse:
  141. temporary_inverse = true;
  142. reset_temporary_inverse = true;
  143. break;
  144. case CharacterCompareType::AnyChar:
  145. // Special case: if not inverted, AnyChar is always in the range.
  146. if (!current_lhs_inversion_state())
  147. return true;
  148. break;
  149. case CharacterCompareType::Char:
  150. if (!current_lhs_inversion_state())
  151. lhs_ranges.insert(pair.value, pair.value);
  152. else
  153. lhs_negated_ranges.insert(pair.value, pair.value);
  154. break;
  155. case CharacterCompareType::String:
  156. // FIXME: We just need to look at the last character of this string, but we only have the first character here.
  157. // Just bail out to avoid false positives.
  158. return true;
  159. case CharacterCompareType::CharClass:
  160. if (!current_lhs_inversion_state())
  161. lhs_char_classes.set(static_cast<CharClass>(pair.value));
  162. else
  163. lhs_negated_char_classes.set(static_cast<CharClass>(pair.value));
  164. break;
  165. case CharacterCompareType::CharRange: {
  166. auto range = CharRange(pair.value);
  167. if (!current_lhs_inversion_state())
  168. lhs_ranges.insert(range.from, range.to);
  169. else
  170. lhs_negated_ranges.insert(range.from, range.to);
  171. break;
  172. }
  173. case CharacterCompareType::LookupTable:
  174. // We've transformed this into a series of ranges in flat_compares(), so bail out if we see it.
  175. return true;
  176. case CharacterCompareType::Reference:
  177. // We've handled this before coming here.
  178. break;
  179. case CharacterCompareType::Property:
  180. case CharacterCompareType::GeneralCategory:
  181. case CharacterCompareType::Script:
  182. case CharacterCompareType::ScriptExtension:
  183. case CharacterCompareType::And:
  184. case CharacterCompareType::Or:
  185. case CharacterCompareType::EndAndOr:
  186. // FIXME: These are too difficult to handle, so bail out.
  187. return true;
  188. case CharacterCompareType::Undefined:
  189. case CharacterCompareType::RangeExpressionDummy:
  190. // These do not occur in valid bytecode.
  191. VERIFY_NOT_REACHED();
  192. }
  193. }
  194. if constexpr (REGEX_DEBUG) {
  195. dbgln("lhs ranges:");
  196. for (auto it = lhs_ranges.begin(); it != lhs_ranges.end(); ++it)
  197. dbgln(" {}..{}", it.key(), *it);
  198. dbgln("lhs negated ranges:");
  199. for (auto it = lhs_negated_ranges.begin(); it != lhs_negated_ranges.end(); ++it)
  200. dbgln(" {}..{}", it.key(), *it);
  201. }
  202. for (auto const& pair : rhs) {
  203. if (reset_temporary_inverse) {
  204. reset_temporary_inverse = false;
  205. temporary_inverse = false;
  206. } else {
  207. reset_temporary_inverse = true;
  208. }
  209. dbgln_if(REGEX_DEBUG, "check {} ({})...", character_compare_type_name(pair.type), pair.value);
  210. switch (pair.type) {
  211. case CharacterCompareType::Inverse:
  212. inverse = !inverse;
  213. break;
  214. case CharacterCompareType::TemporaryInverse:
  215. temporary_inverse = true;
  216. reset_temporary_inverse = true;
  217. break;
  218. case CharacterCompareType::AnyChar:
  219. // Special case: if not inverted, AnyChar is always in the range.
  220. if (!current_lhs_inversion_state())
  221. return true;
  222. break;
  223. case CharacterCompareType::Char:
  224. if (current_lhs_inversion_state() ^ range_contains(pair.value))
  225. return true;
  226. break;
  227. case CharacterCompareType::String:
  228. // FIXME: We just need to look at the last character of this string, but we only have the first character here.
  229. // Just bail out to avoid false positives.
  230. return true;
  231. case CharacterCompareType::CharClass:
  232. if (current_lhs_inversion_state() ^ char_class_contains(static_cast<CharClass>(pair.value)))
  233. return true;
  234. break;
  235. case CharacterCompareType::CharRange: {
  236. auto range = CharRange(pair.value);
  237. if (current_lhs_inversion_state() ^ range_contains(range))
  238. return true;
  239. break;
  240. }
  241. case CharacterCompareType::LookupTable:
  242. // We've transformed this into a series of ranges in flat_compares(), so bail out if we see it.
  243. return true;
  244. case CharacterCompareType::Reference:
  245. // We've handled this before coming here.
  246. break;
  247. case CharacterCompareType::Property:
  248. case CharacterCompareType::GeneralCategory:
  249. case CharacterCompareType::Script:
  250. case CharacterCompareType::ScriptExtension:
  251. case CharacterCompareType::And:
  252. case CharacterCompareType::Or:
  253. case CharacterCompareType::EndAndOr:
  254. // FIXME: These are too difficult to handle, so bail out.
  255. return true;
  256. case CharacterCompareType::Undefined:
  257. case CharacterCompareType::RangeExpressionDummy:
  258. // These do not occur in valid bytecode.
  259. VERIFY_NOT_REACHED();
  260. }
  261. }
  262. return false;
  263. }
  264. enum class AtomicRewritePreconditionResult {
  265. SatisfiedWithProperHeader,
  266. SatisfiedWithEmptyHeader,
  267. NotSatisfied,
  268. };
  269. static AtomicRewritePreconditionResult block_satisfies_atomic_rewrite_precondition(ByteCode const& bytecode, Block const& repeated_block, Block const& following_block)
  270. {
  271. Vector<Vector<CompareTypeAndValuePair>> repeated_values;
  272. HashTable<size_t> active_capture_groups;
  273. MatchState state;
  274. auto has_seen_actionable_opcode = false;
  275. for (state.instruction_position = repeated_block.start; state.instruction_position < repeated_block.end;) {
  276. auto& opcode = bytecode.get_opcode(state);
  277. switch (opcode.opcode_id()) {
  278. case OpCodeId::Compare: {
  279. has_seen_actionable_opcode = true;
  280. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  281. if (repeated_values.is_empty() && any_of(compares, [](auto& compare) { return compare.type == CharacterCompareType::AnyChar; }))
  282. return AtomicRewritePreconditionResult::NotSatisfied;
  283. repeated_values.append(move(compares));
  284. break;
  285. }
  286. case OpCodeId::CheckBegin:
  287. case OpCodeId::CheckEnd:
  288. has_seen_actionable_opcode = true;
  289. if (repeated_values.is_empty())
  290. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  291. break;
  292. case OpCodeId::CheckBoundary:
  293. // FIXME: What should we do with these? for now, let's fail.
  294. return AtomicRewritePreconditionResult::NotSatisfied;
  295. case OpCodeId::Restore:
  296. case OpCodeId::GoBack:
  297. return AtomicRewritePreconditionResult::NotSatisfied;
  298. case OpCodeId::SaveRightCaptureGroup:
  299. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  300. break;
  301. case OpCodeId::SaveLeftCaptureGroup:
  302. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  303. break;
  304. case OpCodeId::ForkJump:
  305. case OpCodeId::ForkReplaceJump:
  306. case OpCodeId::JumpNonEmpty:
  307. // We could attempt to recursively resolve the follow set, but pretending that this just goes nowhere is faster.
  308. if (!has_seen_actionable_opcode)
  309. return AtomicRewritePreconditionResult::NotSatisfied;
  310. break;
  311. default:
  312. break;
  313. }
  314. state.instruction_position += opcode.size();
  315. }
  316. dbgln_if(REGEX_DEBUG, "Found {} entries in reference", repeated_values.size());
  317. dbgln_if(REGEX_DEBUG, "Found {} active capture groups", active_capture_groups.size());
  318. bool following_block_has_at_least_one_compare = false;
  319. // Find the first compare in the following block, it must NOT match any of the values in `repeated_values'.
  320. auto final_instruction = following_block.start;
  321. for (state.instruction_position = following_block.start; state.instruction_position < following_block.end;) {
  322. final_instruction = state.instruction_position;
  323. auto& opcode = bytecode.get_opcode(state);
  324. switch (opcode.opcode_id()) {
  325. // Note: These have to exist since we're effectively repeating the following block as well
  326. case OpCodeId::SaveRightCaptureGroup:
  327. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  328. break;
  329. case OpCodeId::SaveLeftCaptureGroup:
  330. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  331. break;
  332. case OpCodeId::Compare: {
  333. following_block_has_at_least_one_compare = true;
  334. // We found a compare, let's see what it has.
  335. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  336. if (compares.is_empty())
  337. break;
  338. if (any_of(compares, [&](auto& compare) {
  339. return compare.type == CharacterCompareType::AnyChar
  340. || (compare.type == CharacterCompareType::Reference && active_capture_groups.contains(compare.value));
  341. }))
  342. return AtomicRewritePreconditionResult::NotSatisfied;
  343. if (any_of(repeated_values, [&](auto& repeated_value) { return has_overlap(compares, repeated_value); }))
  344. return AtomicRewritePreconditionResult::NotSatisfied;
  345. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  346. }
  347. case OpCodeId::CheckBegin:
  348. case OpCodeId::CheckEnd:
  349. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader; // Nothing can match the end!
  350. case OpCodeId::CheckBoundary:
  351. // FIXME: What should we do with these? For now, consider them a failure.
  352. return AtomicRewritePreconditionResult::NotSatisfied;
  353. case OpCodeId::ForkJump:
  354. case OpCodeId::ForkReplaceJump:
  355. case OpCodeId::JumpNonEmpty:
  356. // See note in the previous switch, same cases.
  357. if (!following_block_has_at_least_one_compare)
  358. return AtomicRewritePreconditionResult::NotSatisfied;
  359. break;
  360. default:
  361. break;
  362. }
  363. state.instruction_position += opcode.size();
  364. }
  365. // If the following block falls through, we can't rewrite it.
  366. state.instruction_position = final_instruction;
  367. switch (bytecode.get_opcode(state).opcode_id()) {
  368. case OpCodeId::Jump:
  369. case OpCodeId::JumpNonEmpty:
  370. case OpCodeId::ForkJump:
  371. case OpCodeId::ForkReplaceJump:
  372. break;
  373. default:
  374. return AtomicRewritePreconditionResult::NotSatisfied;
  375. }
  376. if (following_block_has_at_least_one_compare)
  377. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  378. return AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader;
  379. }
  380. template<typename Parser>
  381. void Regex<Parser>::attempt_rewrite_loops_as_atomic_groups(BasicBlockList const& basic_blocks)
  382. {
  383. auto& bytecode = parser_result.bytecode;
  384. if constexpr (REGEX_DEBUG) {
  385. RegexDebug dbg;
  386. dbg.print_bytecode(*this);
  387. for (auto const& block : basic_blocks)
  388. dbgln("block from {} to {}", block.start, block.end);
  389. }
  390. // A pattern such as:
  391. // bb0 | RE0
  392. // | ForkX bb0
  393. // -------------------------
  394. // bb1 | RE1
  395. // can be rewritten as:
  396. // -------------------------
  397. // bb0 | RE0
  398. // | ForkReplaceX bb0
  399. // -------------------------
  400. // bb1 | RE1
  401. // provided that first(RE1) not-in end(RE0), which is to say
  402. // that RE1 cannot start with whatever RE0 has matched (ever).
  403. //
  404. // Alternatively, a second form of this pattern can also occur:
  405. // bb0 | *
  406. // | ForkX bb2
  407. // ------------------------
  408. // bb1 | RE0
  409. // | Jump bb0
  410. // ------------------------
  411. // bb2 | RE1
  412. // which can be transformed (with the same preconditions) to:
  413. // bb0 | *
  414. // | ForkReplaceX bb2
  415. // ------------------------
  416. // bb1 | RE0
  417. // | Jump bb0
  418. // ------------------------
  419. // bb2 | RE1
  420. enum class AlternateForm {
  421. DirectLoopWithoutHeader, // loop without proper header, a block forking to itself. i.e. the first form.
  422. DirectLoopWithoutHeaderAndEmptyFollow, // loop without proper header, a block forking to itself. i.e. the first form but with RE1 being empty.
  423. DirectLoopWithHeader, // loop with proper header, i.e. the second form.
  424. };
  425. struct CandidateBlock {
  426. Block forking_block;
  427. Optional<Block> new_target_block;
  428. AlternateForm form;
  429. };
  430. Vector<CandidateBlock> candidate_blocks;
  431. auto is_an_eligible_jump = [](OpCode const& opcode, size_t ip, size_t block_start, AlternateForm alternate_form) {
  432. switch (opcode.opcode_id()) {
  433. case OpCodeId::JumpNonEmpty: {
  434. auto const& op = static_cast<OpCode_JumpNonEmpty const&>(opcode);
  435. auto form = op.form();
  436. if (form != OpCodeId::Jump && alternate_form == AlternateForm::DirectLoopWithHeader)
  437. return false;
  438. if (form != OpCodeId::ForkJump && form != OpCodeId::ForkStay && alternate_form == AlternateForm::DirectLoopWithoutHeader)
  439. return false;
  440. return op.offset() + ip + opcode.size() == block_start;
  441. }
  442. case OpCodeId::ForkJump:
  443. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  444. return false;
  445. return static_cast<OpCode_ForkJump const&>(opcode).offset() + ip + opcode.size() == block_start;
  446. case OpCodeId::ForkStay:
  447. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  448. return false;
  449. return static_cast<OpCode_ForkStay const&>(opcode).offset() + ip + opcode.size() == block_start;
  450. case OpCodeId::Jump:
  451. // Infinite loop does *not* produce forks.
  452. if (alternate_form == AlternateForm::DirectLoopWithoutHeader)
  453. return false;
  454. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  455. return static_cast<OpCode_Jump const&>(opcode).offset() + ip + opcode.size() == block_start;
  456. VERIFY_NOT_REACHED();
  457. default:
  458. return false;
  459. }
  460. };
  461. for (size_t i = 0; i < basic_blocks.size(); ++i) {
  462. auto forking_block = basic_blocks[i];
  463. Optional<Block> fork_fallback_block;
  464. if (i + 1 < basic_blocks.size())
  465. fork_fallback_block = basic_blocks[i + 1];
  466. MatchState state;
  467. // Check if the last instruction in this block is a jump to the block itself:
  468. {
  469. state.instruction_position = forking_block.end;
  470. auto& opcode = bytecode.get_opcode(state);
  471. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithoutHeader)) {
  472. // We've found RE0 (and RE1 is just the following block, if any), let's see if the precondition applies.
  473. // if RE1 is empty, there's no first(RE1), so this is an automatic pass.
  474. if (!fork_fallback_block.has_value()
  475. || (fork_fallback_block->end == fork_fallback_block->start && block_satisfies_atomic_rewrite_precondition(bytecode, forking_block, *fork_fallback_block) != AtomicRewritePreconditionResult::NotSatisfied)) {
  476. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  477. break;
  478. }
  479. auto precondition = block_satisfies_atomic_rewrite_precondition(bytecode, forking_block, *fork_fallback_block);
  480. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithProperHeader) {
  481. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  482. break;
  483. }
  484. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader) {
  485. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeaderAndEmptyFollow });
  486. break;
  487. }
  488. }
  489. }
  490. // Check if the last instruction in the last block is a direct jump to this block
  491. if (fork_fallback_block.has_value()) {
  492. state.instruction_position = fork_fallback_block->end;
  493. auto& opcode = bytecode.get_opcode(state);
  494. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithHeader)) {
  495. // We've found bb1 and bb0, let's just make sure that bb0 forks to bb2.
  496. state.instruction_position = forking_block.end;
  497. auto& opcode = bytecode.get_opcode(state);
  498. if (opcode.opcode_id() == OpCodeId::ForkJump || opcode.opcode_id() == OpCodeId::ForkStay) {
  499. Optional<Block> block_following_fork_fallback;
  500. if (i + 2 < basic_blocks.size())
  501. block_following_fork_fallback = basic_blocks[i + 2];
  502. if (!block_following_fork_fallback.has_value()
  503. || block_satisfies_atomic_rewrite_precondition(bytecode, *fork_fallback_block, *block_following_fork_fallback) != AtomicRewritePreconditionResult::NotSatisfied) {
  504. candidate_blocks.append({ forking_block, {}, AlternateForm::DirectLoopWithHeader });
  505. break;
  506. }
  507. }
  508. }
  509. }
  510. }
  511. dbgln_if(REGEX_DEBUG, "Found {} candidate blocks", candidate_blocks.size());
  512. if (candidate_blocks.is_empty()) {
  513. dbgln_if(REGEX_DEBUG, "Failed to find anything for {}", pattern_value);
  514. return;
  515. }
  516. RedBlackTree<size_t, size_t> needed_patches;
  517. // Reverse the blocks, so we can patch the bytecode without messing with the latter patches.
  518. quick_sort(candidate_blocks, [](auto& a, auto& b) { return b.forking_block.start > a.forking_block.start; });
  519. for (auto& candidate : candidate_blocks) {
  520. // Note that both forms share a ForkReplace patch in forking_block.
  521. // Patch the ForkX in forking_block to be a ForkReplaceX instead.
  522. auto& opcode_id = bytecode[candidate.forking_block.end];
  523. if (opcode_id == (ByteCodeValueType)OpCodeId::ForkStay) {
  524. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  525. } else if (opcode_id == (ByteCodeValueType)OpCodeId::ForkJump) {
  526. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  527. } else if (opcode_id == (ByteCodeValueType)OpCodeId::JumpNonEmpty) {
  528. auto& jump_opcode_id = bytecode[candidate.forking_block.end + 3];
  529. if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkStay)
  530. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  531. else if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkJump)
  532. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  533. else
  534. VERIFY_NOT_REACHED();
  535. } else {
  536. VERIFY_NOT_REACHED();
  537. }
  538. }
  539. if (!needed_patches.is_empty()) {
  540. MatchState state;
  541. auto bytecode_size = bytecode.size();
  542. state.instruction_position = 0;
  543. struct Patch {
  544. ssize_t value;
  545. size_t offset;
  546. bool should_negate { false };
  547. };
  548. for (;;) {
  549. if (state.instruction_position >= bytecode_size)
  550. break;
  551. auto& opcode = bytecode.get_opcode(state);
  552. Stack<Patch, 2> patch_points;
  553. switch (opcode.opcode_id()) {
  554. case OpCodeId::Jump:
  555. patch_points.push({ static_cast<OpCode_Jump const&>(opcode).offset(), state.instruction_position + 1 });
  556. break;
  557. case OpCodeId::JumpNonEmpty:
  558. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).offset(), state.instruction_position + 1 });
  559. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).checkpoint(), state.instruction_position + 2 });
  560. break;
  561. case OpCodeId::ForkJump:
  562. patch_points.push({ static_cast<OpCode_ForkJump const&>(opcode).offset(), state.instruction_position + 1 });
  563. break;
  564. case OpCodeId::ForkStay:
  565. patch_points.push({ static_cast<OpCode_ForkStay const&>(opcode).offset(), state.instruction_position + 1 });
  566. break;
  567. case OpCodeId::Repeat:
  568. patch_points.push({ -(ssize_t) static_cast<OpCode_Repeat const&>(opcode).offset(), state.instruction_position + 1, true });
  569. break;
  570. default:
  571. break;
  572. }
  573. while (!patch_points.is_empty()) {
  574. auto& patch_point = patch_points.top();
  575. auto target_offset = patch_point.value + state.instruction_position + opcode.size();
  576. constexpr auto do_patch = [](auto& patch_it, auto& patch_point, auto& target_offset, auto& bytecode, auto ip) {
  577. if (patch_it.key() == ip)
  578. return;
  579. if (patch_point.value < 0 && target_offset <= patch_it.key() && ip > patch_it.key())
  580. bytecode[patch_point.offset] += (patch_point.should_negate ? 1 : -1) * (*patch_it);
  581. else if (patch_point.value > 0 && target_offset >= patch_it.key() && ip < patch_it.key())
  582. bytecode[patch_point.offset] += (patch_point.should_negate ? -1 : 1) * (*patch_it);
  583. };
  584. if (auto patch_it = needed_patches.find_largest_not_above_iterator(target_offset); !patch_it.is_end())
  585. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  586. else if (auto patch_it = needed_patches.find_largest_not_above_iterator(state.instruction_position); !patch_it.is_end())
  587. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  588. patch_points.pop();
  589. }
  590. state.instruction_position += opcode.size();
  591. }
  592. }
  593. if constexpr (REGEX_DEBUG) {
  594. warnln("Transformed to:");
  595. RegexDebug dbg;
  596. dbg.print_bytecode(*this);
  597. }
  598. }
  599. void Optimizer::append_alternation(ByteCode& target, ByteCode&& left, ByteCode&& right)
  600. {
  601. Array<ByteCode, 2> alternatives;
  602. alternatives[0] = move(left);
  603. alternatives[1] = move(right);
  604. append_alternation(target, alternatives);
  605. }
  606. void Optimizer::append_alternation(ByteCode& target, Span<ByteCode> alternatives)
  607. {
  608. if (alternatives.size() == 0)
  609. return;
  610. if (alternatives.size() == 1)
  611. return target.extend(move(alternatives[0]));
  612. if (all_of(alternatives, [](auto& x) { return x.is_empty(); }))
  613. return;
  614. for (auto& entry : alternatives)
  615. entry.flatten();
  616. #if REGEX_DEBUG
  617. ScopeLogger<true> log;
  618. warnln("Alternations:");
  619. RegexDebug dbg;
  620. for (auto& entry : alternatives) {
  621. warnln("----------");
  622. dbg.print_bytecode(entry);
  623. }
  624. ScopeGuard print_at_end {
  625. [&] {
  626. warnln("======================");
  627. RegexDebug dbg;
  628. dbg.print_bytecode(target);
  629. }
  630. };
  631. #endif
  632. Vector<Vector<Detail::Block>> basic_blocks;
  633. basic_blocks.ensure_capacity(alternatives.size());
  634. for (auto& entry : alternatives)
  635. basic_blocks.append(Regex<PosixBasicParser>::split_basic_blocks(entry));
  636. size_t left_skip = 0;
  637. size_t shared_block_count = basic_blocks.first().size();
  638. for (auto& entry : basic_blocks)
  639. shared_block_count = min(shared_block_count, entry.size());
  640. MatchState state;
  641. for (size_t block_index = 0; block_index < shared_block_count; block_index++) {
  642. auto& left_block = basic_blocks.first()[block_index];
  643. auto left_end = block_index + 1 == basic_blocks.first().size() ? left_block.end : basic_blocks.first()[block_index + 1].start;
  644. auto can_continue = true;
  645. for (size_t i = 1; i < alternatives.size(); ++i) {
  646. auto& right_blocks = basic_blocks[i];
  647. auto& right_block = right_blocks[block_index];
  648. auto right_end = block_index + 1 == right_blocks.size() ? right_block.end : right_blocks[block_index + 1].start;
  649. if (left_end - left_block.start != right_end - right_block.start) {
  650. can_continue = false;
  651. break;
  652. }
  653. 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)) {
  654. can_continue = false;
  655. break;
  656. }
  657. }
  658. if (!can_continue)
  659. break;
  660. size_t i = 0;
  661. for (auto& entry : alternatives) {
  662. auto& blocks = basic_blocks[i++];
  663. auto& block = blocks[block_index];
  664. auto end = block_index + 1 == blocks.size() ? block.end : blocks[block_index + 1].start;
  665. state.instruction_position = block.start;
  666. size_t skip = 0;
  667. while (state.instruction_position < end) {
  668. auto& opcode = entry.get_opcode(state);
  669. state.instruction_position += opcode.size();
  670. skip = state.instruction_position;
  671. }
  672. left_skip = min(skip, left_skip);
  673. }
  674. }
  675. dbgln_if(REGEX_DEBUG, "Skipping {}/{} bytecode entries from {}", left_skip, 0, alternatives[0].size());
  676. if (left_skip > 0) {
  677. target.extend(alternatives[0].release_slice(basic_blocks.first().first().start, left_skip));
  678. auto first = true;
  679. for (auto& entry : alternatives) {
  680. if (first) {
  681. first = false;
  682. continue;
  683. }
  684. entry = entry.release_slice(left_skip);
  685. }
  686. }
  687. if (all_of(alternatives, [](auto& entry) { return entry.is_empty(); }))
  688. return;
  689. size_t patch_start = target.size();
  690. for (size_t i = 1; i < alternatives.size(); ++i) {
  691. target.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  692. target.empend(0u); // To be filled later.
  693. }
  694. size_t size_to_jump = 0;
  695. bool seen_one_empty = false;
  696. for (size_t i = alternatives.size(); i > 0; --i) {
  697. auto& entry = alternatives[i - 1];
  698. if (entry.is_empty()) {
  699. if (seen_one_empty)
  700. continue;
  701. seen_one_empty = true;
  702. }
  703. auto is_first = i == 1;
  704. auto instruction_size = entry.size() + (is_first ? 0 : 2); // Jump; -> +2
  705. size_to_jump += instruction_size;
  706. if (!is_first)
  707. target[patch_start + (i - 2) * 2 + 1] = size_to_jump + (alternatives.size() - i) * 2;
  708. dbgln_if(REGEX_DEBUG, "{} size = {}, cum={}", i - 1, instruction_size, size_to_jump);
  709. }
  710. seen_one_empty = false;
  711. for (size_t i = alternatives.size(); i > 0; --i) {
  712. auto& chunk = alternatives[i - 1];
  713. if (chunk.is_empty()) {
  714. if (seen_one_empty)
  715. continue;
  716. seen_one_empty = true;
  717. }
  718. ByteCode* previous_chunk = nullptr;
  719. size_t j = i - 1;
  720. auto seen_one_empty_before = chunk.is_empty();
  721. while (j >= 1) {
  722. --j;
  723. auto& candidate_chunk = alternatives[j];
  724. if (candidate_chunk.is_empty()) {
  725. if (seen_one_empty_before)
  726. continue;
  727. }
  728. previous_chunk = &candidate_chunk;
  729. break;
  730. }
  731. size_to_jump -= chunk.size() + (previous_chunk ? 2 : 0);
  732. target.extend(move(chunk));
  733. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Jump));
  734. target.empend(size_to_jump); // Jump to the _END label
  735. }
  736. }
  737. enum class LookupTableInsertionOutcome {
  738. Successful,
  739. ReplaceWithAnyChar,
  740. TemporaryInversionNeeded,
  741. PermanentInversionNeeded,
  742. FlushOnInsertion,
  743. FinishFlushOnInsertion,
  744. CannotPlaceInTable,
  745. };
  746. static LookupTableInsertionOutcome insert_into_lookup_table(RedBlackTree<ByteCodeValueType, CharRange>& table, CompareTypeAndValuePair pair)
  747. {
  748. switch (pair.type) {
  749. case CharacterCompareType::Inverse:
  750. return LookupTableInsertionOutcome::PermanentInversionNeeded;
  751. case CharacterCompareType::TemporaryInverse:
  752. return LookupTableInsertionOutcome::TemporaryInversionNeeded;
  753. case CharacterCompareType::AnyChar:
  754. return LookupTableInsertionOutcome::ReplaceWithAnyChar;
  755. case CharacterCompareType::CharClass:
  756. return LookupTableInsertionOutcome::CannotPlaceInTable;
  757. case CharacterCompareType::Char:
  758. table.insert(pair.value, { (u32)pair.value, (u32)pair.value });
  759. break;
  760. case CharacterCompareType::CharRange: {
  761. CharRange range { pair.value };
  762. table.insert(range.from, range);
  763. break;
  764. }
  765. case CharacterCompareType::EndAndOr:
  766. return LookupTableInsertionOutcome::FinishFlushOnInsertion;
  767. case CharacterCompareType::And:
  768. return LookupTableInsertionOutcome::FlushOnInsertion;
  769. case CharacterCompareType::Reference:
  770. case CharacterCompareType::Property:
  771. case CharacterCompareType::GeneralCategory:
  772. case CharacterCompareType::Script:
  773. case CharacterCompareType::ScriptExtension:
  774. case CharacterCompareType::Or:
  775. return LookupTableInsertionOutcome::CannotPlaceInTable;
  776. case CharacterCompareType::Undefined:
  777. case CharacterCompareType::RangeExpressionDummy:
  778. case CharacterCompareType::String:
  779. case CharacterCompareType::LookupTable:
  780. VERIFY_NOT_REACHED();
  781. }
  782. return LookupTableInsertionOutcome::Successful;
  783. }
  784. void Optimizer::append_character_class(ByteCode& target, Vector<CompareTypeAndValuePair>&& pairs)
  785. {
  786. ByteCode arguments;
  787. size_t argument_count = 0;
  788. if (pairs.size() <= 1) {
  789. for (auto& pair : pairs) {
  790. arguments.append(to_underlying(pair.type));
  791. if (pair.type != CharacterCompareType::AnyChar
  792. && pair.type != CharacterCompareType::TemporaryInverse
  793. && pair.type != CharacterCompareType::Inverse
  794. && pair.type != CharacterCompareType::And
  795. && pair.type != CharacterCompareType::Or
  796. && pair.type != CharacterCompareType::EndAndOr)
  797. arguments.append(pair.value);
  798. ++argument_count;
  799. }
  800. } else {
  801. RedBlackTree<ByteCodeValueType, CharRange> table;
  802. RedBlackTree<ByteCodeValueType, CharRange> inverted_table;
  803. auto* current_table = &table;
  804. auto* current_inverted_table = &inverted_table;
  805. bool invert_for_next_iteration = false;
  806. bool is_currently_inverted = false;
  807. auto flush_tables = [&] {
  808. auto append_table = [&](auto& table) {
  809. ++argument_count;
  810. arguments.append(to_underlying(CharacterCompareType::LookupTable));
  811. auto size_index = arguments.size();
  812. arguments.append(0);
  813. Optional<CharRange> active_range;
  814. size_t range_count = 0;
  815. for (auto& range : table) {
  816. if (!active_range.has_value()) {
  817. active_range = range;
  818. continue;
  819. }
  820. if (range.from <= active_range->to + 1 && range.to + 1 >= active_range->from) {
  821. active_range = CharRange { min(range.from, active_range->from), max(range.to, active_range->to) };
  822. } else {
  823. ++range_count;
  824. arguments.append(active_range.release_value());
  825. active_range = range;
  826. }
  827. }
  828. if (active_range.has_value()) {
  829. ++range_count;
  830. arguments.append(active_range.release_value());
  831. }
  832. arguments[size_index] = range_count;
  833. };
  834. auto contains_regular_table = !table.is_empty();
  835. auto contains_inverted_table = !inverted_table.is_empty();
  836. if (contains_regular_table)
  837. append_table(table);
  838. if (contains_inverted_table) {
  839. ++argument_count;
  840. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  841. append_table(inverted_table);
  842. }
  843. table.clear();
  844. inverted_table.clear();
  845. };
  846. auto flush_on_every_insertion = false;
  847. for (auto& value : pairs) {
  848. auto should_invert_after_this_iteration = invert_for_next_iteration;
  849. invert_for_next_iteration = false;
  850. auto insertion_result = insert_into_lookup_table(*current_table, value);
  851. switch (insertion_result) {
  852. case LookupTableInsertionOutcome::Successful:
  853. if (flush_on_every_insertion)
  854. flush_tables();
  855. break;
  856. case LookupTableInsertionOutcome::ReplaceWithAnyChar: {
  857. table.clear();
  858. inverted_table.clear();
  859. arguments.append(to_underlying(CharacterCompareType::AnyChar));
  860. ++argument_count;
  861. break;
  862. }
  863. case LookupTableInsertionOutcome::TemporaryInversionNeeded:
  864. swap(current_table, current_inverted_table);
  865. invert_for_next_iteration = true;
  866. is_currently_inverted = !is_currently_inverted;
  867. break;
  868. case LookupTableInsertionOutcome::PermanentInversionNeeded:
  869. flush_tables();
  870. arguments.append(to_underlying(CharacterCompareType::Inverse));
  871. ++argument_count;
  872. break;
  873. case LookupTableInsertionOutcome::FlushOnInsertion:
  874. case LookupTableInsertionOutcome::FinishFlushOnInsertion:
  875. flush_tables();
  876. flush_on_every_insertion = insertion_result == LookupTableInsertionOutcome::FlushOnInsertion;
  877. [[fallthrough]];
  878. case LookupTableInsertionOutcome::CannotPlaceInTable:
  879. if (is_currently_inverted) {
  880. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  881. ++argument_count;
  882. }
  883. arguments.append(to_underlying(value.type));
  884. if (value.type != CharacterCompareType::AnyChar
  885. && value.type != CharacterCompareType::TemporaryInverse
  886. && value.type != CharacterCompareType::Inverse
  887. && value.type != CharacterCompareType::And
  888. && value.type != CharacterCompareType::Or
  889. && value.type != CharacterCompareType::EndAndOr)
  890. arguments.append(value.value);
  891. ++argument_count;
  892. break;
  893. }
  894. if (should_invert_after_this_iteration) {
  895. swap(current_table, current_inverted_table);
  896. is_currently_inverted = !is_currently_inverted;
  897. }
  898. }
  899. flush_tables();
  900. }
  901. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Compare));
  902. target.empend(argument_count); // number of arguments
  903. target.empend(arguments.size()); // size of arguments
  904. target.extend(move(arguments));
  905. }
  906. template void Regex<PosixBasicParser>::run_optimization_passes();
  907. template void Regex<PosixExtendedParser>::run_optimization_passes();
  908. template void Regex<ECMA262Parser>::run_optimization_passes();
  909. }