ast.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string>
  5. #include <string_view>
  6. #include <fmt/format.h>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "video_core/shader/ast.h"
  10. #include "video_core/shader/expr.h"
  11. namespace VideoCommon::Shader {
  12. ASTZipper::ASTZipper() = default;
  13. void ASTZipper::Init(const ASTNode new_first, const ASTNode parent) {
  14. ASSERT(new_first->manager == nullptr);
  15. first = new_first;
  16. last = new_first;
  17. ASTNode current = first;
  18. while (current) {
  19. current->manager = this;
  20. current->parent = parent;
  21. last = current;
  22. current = current->next;
  23. }
  24. }
  25. void ASTZipper::PushBack(const ASTNode new_node) {
  26. ASSERT(new_node->manager == nullptr);
  27. new_node->previous = last;
  28. if (last) {
  29. last->next = new_node;
  30. }
  31. new_node->next.reset();
  32. last = new_node;
  33. if (!first) {
  34. first = new_node;
  35. }
  36. new_node->manager = this;
  37. }
  38. void ASTZipper::PushFront(const ASTNode new_node) {
  39. ASSERT(new_node->manager == nullptr);
  40. new_node->previous.reset();
  41. new_node->next = first;
  42. if (first) {
  43. first->previous = new_node;
  44. }
  45. if (last == first) {
  46. last = new_node;
  47. }
  48. first = new_node;
  49. new_node->manager = this;
  50. }
  51. void ASTZipper::InsertAfter(const ASTNode new_node, const ASTNode at_node) {
  52. ASSERT(new_node->manager == nullptr);
  53. if (!at_node) {
  54. PushFront(new_node);
  55. return;
  56. }
  57. const ASTNode next = at_node->next;
  58. if (next) {
  59. next->previous = new_node;
  60. }
  61. new_node->previous = at_node;
  62. if (at_node == last) {
  63. last = new_node;
  64. }
  65. new_node->next = next;
  66. at_node->next = new_node;
  67. new_node->manager = this;
  68. }
  69. void ASTZipper::InsertBefore(const ASTNode new_node, const ASTNode at_node) {
  70. ASSERT(new_node->manager == nullptr);
  71. if (!at_node) {
  72. PushBack(new_node);
  73. return;
  74. }
  75. const ASTNode previous = at_node->previous;
  76. if (previous) {
  77. previous->next = new_node;
  78. }
  79. new_node->next = at_node;
  80. if (at_node == first) {
  81. first = new_node;
  82. }
  83. new_node->previous = previous;
  84. at_node->previous = new_node;
  85. new_node->manager = this;
  86. }
  87. void ASTZipper::DetachTail(ASTNode node) {
  88. ASSERT(node->manager == this);
  89. if (node == first) {
  90. first.reset();
  91. last.reset();
  92. return;
  93. }
  94. last = node->previous;
  95. last->next.reset();
  96. node->previous.reset();
  97. ASTNode current = std::move(node);
  98. while (current) {
  99. current->manager = nullptr;
  100. current->parent.reset();
  101. current = current->next;
  102. }
  103. }
  104. void ASTZipper::DetachSegment(const ASTNode start, const ASTNode end) {
  105. ASSERT(start->manager == this && end->manager == this);
  106. if (start == end) {
  107. DetachSingle(start);
  108. return;
  109. }
  110. const ASTNode prev = start->previous;
  111. const ASTNode post = end->next;
  112. if (!prev) {
  113. first = post;
  114. } else {
  115. prev->next = post;
  116. }
  117. if (!post) {
  118. last = prev;
  119. } else {
  120. post->previous = prev;
  121. }
  122. start->previous.reset();
  123. end->next.reset();
  124. ASTNode current = start;
  125. bool found = false;
  126. while (current) {
  127. current->manager = nullptr;
  128. current->parent.reset();
  129. found |= current == end;
  130. current = current->next;
  131. }
  132. ASSERT(found);
  133. }
  134. void ASTZipper::DetachSingle(const ASTNode node) {
  135. ASSERT(node->manager == this);
  136. const ASTNode prev = node->previous;
  137. const ASTNode post = node->next;
  138. node->previous.reset();
  139. node->next.reset();
  140. if (!prev) {
  141. first = post;
  142. } else {
  143. prev->next = post;
  144. }
  145. if (!post) {
  146. last = prev;
  147. } else {
  148. post->previous = prev;
  149. }
  150. node->manager = nullptr;
  151. node->parent.reset();
  152. }
  153. void ASTZipper::Remove(const ASTNode node) {
  154. ASSERT(node->manager == this);
  155. const ASTNode next = node->next;
  156. const ASTNode previous = node->previous;
  157. if (previous) {
  158. previous->next = next;
  159. }
  160. if (next) {
  161. next->previous = previous;
  162. }
  163. node->parent.reset();
  164. node->manager = nullptr;
  165. if (node == last) {
  166. last = previous;
  167. }
  168. if (node == first) {
  169. first = next;
  170. }
  171. }
  172. class ExprPrinter final {
  173. public:
  174. void operator()(const ExprAnd& expr) {
  175. inner += "( ";
  176. std::visit(*this, *expr.operand1);
  177. inner += " && ";
  178. std::visit(*this, *expr.operand2);
  179. inner += ')';
  180. }
  181. void operator()(const ExprOr& expr) {
  182. inner += "( ";
  183. std::visit(*this, *expr.operand1);
  184. inner += " || ";
  185. std::visit(*this, *expr.operand2);
  186. inner += ')';
  187. }
  188. void operator()(const ExprNot& expr) {
  189. inner += "!";
  190. std::visit(*this, *expr.operand1);
  191. }
  192. void operator()(const ExprPredicate& expr) {
  193. inner += fmt::format("P{}", expr.predicate);
  194. }
  195. void operator()(const ExprCondCode& expr) {
  196. inner += fmt::format("CC{}", expr.cc);
  197. }
  198. void operator()(const ExprVar& expr) {
  199. inner += fmt::format("V{}", expr.var_index);
  200. }
  201. void operator()(const ExprBoolean& expr) {
  202. inner += expr.value ? "true" : "false";
  203. }
  204. void operator()(const ExprGprEqual& expr) {
  205. inner += fmt::format("(gpr_{} == {})", expr.gpr, expr.value);
  206. }
  207. const std::string& GetResult() const {
  208. return inner;
  209. }
  210. private:
  211. std::string inner;
  212. };
  213. class ASTPrinter {
  214. public:
  215. void operator()(const ASTProgram& ast) {
  216. scope++;
  217. inner += "program {\n";
  218. ASTNode current = ast.nodes.GetFirst();
  219. while (current) {
  220. Visit(current);
  221. current = current->GetNext();
  222. }
  223. inner += "}\n";
  224. scope--;
  225. }
  226. void operator()(const ASTIfThen& ast) {
  227. ExprPrinter expr_parser{};
  228. std::visit(expr_parser, *ast.condition);
  229. inner += fmt::format("{}if ({}) {{\n", Indent(), expr_parser.GetResult());
  230. scope++;
  231. ASTNode current = ast.nodes.GetFirst();
  232. while (current) {
  233. Visit(current);
  234. current = current->GetNext();
  235. }
  236. scope--;
  237. inner += fmt::format("{}}}\n", Indent());
  238. }
  239. void operator()(const ASTIfElse& ast) {
  240. inner += Indent();
  241. inner += "else {\n";
  242. scope++;
  243. ASTNode current = ast.nodes.GetFirst();
  244. while (current) {
  245. Visit(current);
  246. current = current->GetNext();
  247. }
  248. scope--;
  249. inner += Indent();
  250. inner += "}\n";
  251. }
  252. void operator()(const ASTBlockEncoded& ast) {
  253. inner += fmt::format("{}Block({}, {});\n", Indent(), ast.start, ast.end);
  254. }
  255. void operator()([[maybe_unused]] const ASTBlockDecoded& ast) {
  256. inner += Indent();
  257. inner += "Block;\n";
  258. }
  259. void operator()(const ASTVarSet& ast) {
  260. ExprPrinter expr_parser{};
  261. std::visit(expr_parser, *ast.condition);
  262. inner += fmt::format("{}V{} := {};\n", Indent(), ast.index, expr_parser.GetResult());
  263. }
  264. void operator()(const ASTLabel& ast) {
  265. inner += fmt::format("Label_{}:\n", ast.index);
  266. }
  267. void operator()(const ASTGoto& ast) {
  268. ExprPrinter expr_parser{};
  269. std::visit(expr_parser, *ast.condition);
  270. inner +=
  271. fmt::format("{}({}) -> goto Label_{};\n", Indent(), expr_parser.GetResult(), ast.label);
  272. }
  273. void operator()(const ASTDoWhile& ast) {
  274. ExprPrinter expr_parser{};
  275. std::visit(expr_parser, *ast.condition);
  276. inner += fmt::format("{}do {{\n", Indent());
  277. scope++;
  278. ASTNode current = ast.nodes.GetFirst();
  279. while (current) {
  280. Visit(current);
  281. current = current->GetNext();
  282. }
  283. scope--;
  284. inner += fmt::format("{}}} while ({});\n", Indent(), expr_parser.GetResult());
  285. }
  286. void operator()(const ASTReturn& ast) {
  287. ExprPrinter expr_parser{};
  288. std::visit(expr_parser, *ast.condition);
  289. inner += fmt::format("{}({}) -> {};\n", Indent(), expr_parser.GetResult(),
  290. ast.kills ? "discard" : "exit");
  291. }
  292. void operator()(const ASTBreak& ast) {
  293. ExprPrinter expr_parser{};
  294. std::visit(expr_parser, *ast.condition);
  295. inner += fmt::format("{}({}) -> break;\n", Indent(), expr_parser.GetResult());
  296. }
  297. void Visit(const ASTNode& node) {
  298. std::visit(*this, *node->GetInnerData());
  299. }
  300. const std::string& GetResult() const {
  301. return inner;
  302. }
  303. private:
  304. std::string_view Indent() {
  305. if (space_segment_scope == scope) {
  306. return space_segment;
  307. }
  308. // Ensure that we don't exceed our view.
  309. ASSERT(scope * 2 < spaces.size());
  310. space_segment = spaces.substr(0, scope * 2);
  311. space_segment_scope = scope;
  312. return space_segment;
  313. }
  314. std::string inner{};
  315. std::string_view space_segment;
  316. u32 scope{};
  317. u32 space_segment_scope{};
  318. static constexpr std::string_view spaces{" "};
  319. };
  320. std::string ASTManager::Print() const {
  321. ASTPrinter printer{};
  322. printer.Visit(main_node);
  323. return printer.GetResult();
  324. }
  325. ASTManager::ASTManager(bool do_full_decompile, bool disable_else_derivation_)
  326. : full_decompile{do_full_decompile}, disable_else_derivation{disable_else_derivation_} {}
  327. ASTManager::~ASTManager() {
  328. Clear();
  329. }
  330. void ASTManager::Init() {
  331. main_node = ASTBase::Make<ASTProgram>(ASTNode{});
  332. program = std::get_if<ASTProgram>(main_node->GetInnerData());
  333. false_condition = MakeExpr<ExprBoolean>(false);
  334. }
  335. void ASTManager::DeclareLabel(u32 address) {
  336. const auto pair = labels_map.emplace(address, labels_count);
  337. if (pair.second) {
  338. labels_count++;
  339. labels.resize(labels_count);
  340. }
  341. }
  342. void ASTManager::InsertLabel(u32 address) {
  343. const u32 index = labels_map[address];
  344. const ASTNode label = ASTBase::Make<ASTLabel>(main_node, index);
  345. labels[index] = label;
  346. program->nodes.PushBack(label);
  347. }
  348. void ASTManager::InsertGoto(Expr condition, u32 address) {
  349. const u32 index = labels_map[address];
  350. const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, std::move(condition), index);
  351. gotos.push_back(goto_node);
  352. program->nodes.PushBack(goto_node);
  353. }
  354. void ASTManager::InsertBlock(u32 start_address, u32 end_address) {
  355. ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address);
  356. program->nodes.PushBack(std::move(block));
  357. }
  358. void ASTManager::InsertReturn(Expr condition, bool kills) {
  359. ASTNode node = ASTBase::Make<ASTReturn>(main_node, std::move(condition), kills);
  360. program->nodes.PushBack(std::move(node));
  361. }
  362. // The decompile algorithm is based on
  363. // "Taming control flow: A structured approach to eliminating goto statements"
  364. // by AM Erosa, LJ Hendren 1994. In general, the idea is to get gotos to be
  365. // on the same structured level as the label which they jump to. This is done,
  366. // through outward/inward movements and lifting. Once they are at the same
  367. // level, you can enclose them in an "if" structure or a "do-while" structure.
  368. void ASTManager::Decompile() {
  369. auto it = gotos.begin();
  370. while (it != gotos.end()) {
  371. const ASTNode goto_node = *it;
  372. const auto label_index = goto_node->GetGotoLabel();
  373. if (!label_index) {
  374. return;
  375. }
  376. const ASTNode label = labels[*label_index];
  377. if (!full_decompile) {
  378. // We only decompile backward jumps
  379. if (!IsBackwardsJump(goto_node, label)) {
  380. it++;
  381. continue;
  382. }
  383. }
  384. if (IndirectlyRelated(goto_node, label)) {
  385. while (!DirectlyRelated(goto_node, label)) {
  386. MoveOutward(goto_node);
  387. }
  388. }
  389. if (DirectlyRelated(goto_node, label)) {
  390. u32 goto_level = goto_node->GetLevel();
  391. const u32 label_level = label->GetLevel();
  392. while (label_level < goto_level) {
  393. MoveOutward(goto_node);
  394. goto_level--;
  395. }
  396. // TODO(Blinkhawk): Implement Lifting and Inward Movements
  397. }
  398. if (label->GetParent() == goto_node->GetParent()) {
  399. bool is_loop = false;
  400. ASTNode current = goto_node->GetPrevious();
  401. while (current) {
  402. if (current == label) {
  403. is_loop = true;
  404. break;
  405. }
  406. current = current->GetPrevious();
  407. }
  408. if (is_loop) {
  409. EncloseDoWhile(goto_node, label);
  410. } else {
  411. EncloseIfThen(goto_node, label);
  412. }
  413. it = gotos.erase(it);
  414. continue;
  415. }
  416. it++;
  417. }
  418. if (full_decompile) {
  419. for (const ASTNode& label : labels) {
  420. auto& manager = label->GetManager();
  421. manager.Remove(label);
  422. }
  423. labels.clear();
  424. } else {
  425. auto label_it = labels.begin();
  426. while (label_it != labels.end()) {
  427. bool can_remove = true;
  428. ASTNode label = *label_it;
  429. for (const ASTNode& goto_node : gotos) {
  430. const auto label_index = goto_node->GetGotoLabel();
  431. if (!label_index) {
  432. return;
  433. }
  434. ASTNode& glabel = labels[*label_index];
  435. if (glabel == label) {
  436. can_remove = false;
  437. break;
  438. }
  439. }
  440. if (can_remove) {
  441. label->MarkLabelUnused();
  442. }
  443. }
  444. }
  445. }
  446. bool ASTManager::IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const {
  447. u32 goto_level = goto_node->GetLevel();
  448. u32 label_level = label_node->GetLevel();
  449. while (goto_level > label_level) {
  450. goto_level--;
  451. goto_node = goto_node->GetParent();
  452. }
  453. while (label_level > goto_level) {
  454. label_level--;
  455. label_node = label_node->GetParent();
  456. }
  457. while (goto_node->GetParent() != label_node->GetParent()) {
  458. goto_node = goto_node->GetParent();
  459. label_node = label_node->GetParent();
  460. }
  461. ASTNode current = goto_node->GetPrevious();
  462. while (current) {
  463. if (current == label_node) {
  464. return true;
  465. }
  466. current = current->GetPrevious();
  467. }
  468. return false;
  469. }
  470. bool ASTManager::IndirectlyRelated(const ASTNode& first, const ASTNode& second) const {
  471. return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second));
  472. }
  473. bool ASTManager::DirectlyRelated(const ASTNode& first, const ASTNode& second) const {
  474. if (first->GetParent() == second->GetParent()) {
  475. return false;
  476. }
  477. const u32 first_level = first->GetLevel();
  478. const u32 second_level = second->GetLevel();
  479. u32 min_level;
  480. u32 max_level;
  481. ASTNode max;
  482. ASTNode min;
  483. if (first_level > second_level) {
  484. min_level = second_level;
  485. min = second;
  486. max_level = first_level;
  487. max = first;
  488. } else {
  489. min_level = first_level;
  490. min = first;
  491. max_level = second_level;
  492. max = second;
  493. }
  494. while (max_level > min_level) {
  495. max_level--;
  496. max = max->GetParent();
  497. }
  498. return min->GetParent() == max->GetParent();
  499. }
  500. void ASTManager::ShowCurrentState(std::string_view state) const {
  501. LOG_CRITICAL(HW_GPU, "\nState {}:\n\n{}\n", state, Print());
  502. SanityCheck();
  503. }
  504. void ASTManager::SanityCheck() const {
  505. for (const auto& label : labels) {
  506. if (!label->GetParent()) {
  507. LOG_CRITICAL(HW_GPU, "Sanity Check Failed");
  508. }
  509. }
  510. }
  511. void ASTManager::EncloseDoWhile(ASTNode goto_node, ASTNode label) {
  512. ASTZipper& zipper = goto_node->GetManager();
  513. const ASTNode loop_start = label->GetNext();
  514. if (loop_start == goto_node) {
  515. zipper.Remove(goto_node);
  516. return;
  517. }
  518. const ASTNode parent = label->GetParent();
  519. const Expr condition = goto_node->GetGotoCondition();
  520. zipper.DetachSegment(loop_start, goto_node);
  521. const ASTNode do_while_node = ASTBase::Make<ASTDoWhile>(parent, condition);
  522. ASTZipper* sub_zipper = do_while_node->GetSubNodes();
  523. sub_zipper->Init(loop_start, do_while_node);
  524. zipper.InsertAfter(do_while_node, label);
  525. sub_zipper->Remove(goto_node);
  526. }
  527. void ASTManager::EncloseIfThen(ASTNode goto_node, ASTNode label) {
  528. ASTZipper& zipper = goto_node->GetManager();
  529. const ASTNode if_end = label->GetPrevious();
  530. if (if_end == goto_node) {
  531. zipper.Remove(goto_node);
  532. return;
  533. }
  534. const ASTNode prev = goto_node->GetPrevious();
  535. const Expr condition = goto_node->GetGotoCondition();
  536. bool do_else = false;
  537. if (!disable_else_derivation && prev->IsIfThen()) {
  538. const Expr if_condition = prev->GetIfCondition();
  539. do_else = ExprAreEqual(if_condition, condition);
  540. }
  541. const ASTNode parent = label->GetParent();
  542. zipper.DetachSegment(goto_node, if_end);
  543. ASTNode if_node;
  544. if (do_else) {
  545. if_node = ASTBase::Make<ASTIfElse>(parent);
  546. } else {
  547. Expr neg_condition = MakeExprNot(condition);
  548. if_node = ASTBase::Make<ASTIfThen>(parent, neg_condition);
  549. }
  550. ASTZipper* sub_zipper = if_node->GetSubNodes();
  551. sub_zipper->Init(goto_node, if_node);
  552. zipper.InsertAfter(if_node, prev);
  553. sub_zipper->Remove(goto_node);
  554. }
  555. void ASTManager::MoveOutward(ASTNode goto_node) {
  556. ASTZipper& zipper = goto_node->GetManager();
  557. const ASTNode parent = goto_node->GetParent();
  558. ASTZipper& zipper2 = parent->GetManager();
  559. const ASTNode grandpa = parent->GetParent();
  560. const bool is_loop = parent->IsLoop();
  561. const bool is_else = parent->IsIfElse();
  562. const bool is_if = parent->IsIfThen();
  563. const ASTNode prev = goto_node->GetPrevious();
  564. const ASTNode post = goto_node->GetNext();
  565. const Expr condition = goto_node->GetGotoCondition();
  566. zipper.DetachSingle(goto_node);
  567. if (is_loop) {
  568. const u32 var_index = NewVariable();
  569. const Expr var_condition = MakeExpr<ExprVar>(var_index);
  570. const ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition);
  571. const ASTNode var_node_init = ASTBase::Make<ASTVarSet>(parent, var_index, false_condition);
  572. zipper2.InsertBefore(var_node_init, parent);
  573. zipper.InsertAfter(var_node, prev);
  574. goto_node->SetGotoCondition(var_condition);
  575. const ASTNode break_node = ASTBase::Make<ASTBreak>(parent, var_condition);
  576. zipper.InsertAfter(break_node, var_node);
  577. } else if (is_if || is_else) {
  578. const u32 var_index = NewVariable();
  579. const Expr var_condition = MakeExpr<ExprVar>(var_index);
  580. const ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition);
  581. const ASTNode var_node_init = ASTBase::Make<ASTVarSet>(parent, var_index, false_condition);
  582. if (is_if) {
  583. zipper2.InsertBefore(var_node_init, parent);
  584. } else {
  585. zipper2.InsertBefore(var_node_init, parent->GetPrevious());
  586. }
  587. zipper.InsertAfter(var_node, prev);
  588. goto_node->SetGotoCondition(var_condition);
  589. if (post) {
  590. zipper.DetachTail(post);
  591. const ASTNode if_node = ASTBase::Make<ASTIfThen>(parent, MakeExprNot(var_condition));
  592. ASTZipper* sub_zipper = if_node->GetSubNodes();
  593. sub_zipper->Init(post, if_node);
  594. zipper.InsertAfter(if_node, var_node);
  595. }
  596. } else {
  597. UNREACHABLE();
  598. }
  599. const ASTNode next = parent->GetNext();
  600. if (is_if && next && next->IsIfElse()) {
  601. zipper2.InsertAfter(goto_node, next);
  602. goto_node->SetParent(grandpa);
  603. return;
  604. }
  605. zipper2.InsertAfter(goto_node, parent);
  606. goto_node->SetParent(grandpa);
  607. }
  608. class ASTClearer {
  609. public:
  610. ASTClearer() = default;
  611. void operator()(const ASTProgram& ast) {
  612. ASTNode current = ast.nodes.GetFirst();
  613. while (current) {
  614. Visit(current);
  615. current = current->GetNext();
  616. }
  617. }
  618. void operator()(const ASTIfThen& ast) {
  619. ASTNode current = ast.nodes.GetFirst();
  620. while (current) {
  621. Visit(current);
  622. current = current->GetNext();
  623. }
  624. }
  625. void operator()(const ASTIfElse& ast) {
  626. ASTNode current = ast.nodes.GetFirst();
  627. while (current) {
  628. Visit(current);
  629. current = current->GetNext();
  630. }
  631. }
  632. void operator()([[maybe_unused]] const ASTBlockEncoded& ast) {}
  633. void operator()(ASTBlockDecoded& ast) {
  634. ast.nodes.clear();
  635. }
  636. void operator()([[maybe_unused]] const ASTVarSet& ast) {}
  637. void operator()([[maybe_unused]] const ASTLabel& ast) {}
  638. void operator()([[maybe_unused]] const ASTGoto& ast) {}
  639. void operator()(const ASTDoWhile& ast) {
  640. ASTNode current = ast.nodes.GetFirst();
  641. while (current) {
  642. Visit(current);
  643. current = current->GetNext();
  644. }
  645. }
  646. void operator()([[maybe_unused]] const ASTReturn& ast) {}
  647. void operator()([[maybe_unused]] const ASTBreak& ast) {}
  648. void Visit(const ASTNode& node) {
  649. std::visit(*this, *node->GetInnerData());
  650. node->Clear();
  651. }
  652. };
  653. void ASTManager::Clear() {
  654. if (!main_node) {
  655. return;
  656. }
  657. ASTClearer clearer{};
  658. clearer.Visit(main_node);
  659. main_node.reset();
  660. program = nullptr;
  661. labels_map.clear();
  662. labels.clear();
  663. gotos.clear();
  664. }
  665. } // namespace VideoCommon::Shader