diff options
| author | 2019-06-27 18:57:47 -0400 | |
|---|---|---|
| committer | 2019-10-04 18:52:48 -0400 | |
| commit | 4fde66e6094b57201d208b8abd3d7715341cd5db (patch) | |
| tree | 627cc2bfd8a03fb05e28654885b7d752e8bcfad8 | |
| parent | shader_ir: Initial Decompile Setup (diff) | |
| download | yuzu-4fde66e6094b57201d208b8abd3d7715341cd5db.tar.gz yuzu-4fde66e6094b57201d208b8abd3d7715341cd5db.tar.xz yuzu-4fde66e6094b57201d208b8abd3d7715341cd5db.zip | |
shader_ir: Add basic goto elimination
| -rw-r--r-- | src/video_core/shader/ast.cpp | 348 | ||||
| -rw-r--r-- | src/video_core/shader/ast.h | 174 |
2 files changed, 484 insertions, 38 deletions
diff --git a/src/video_core/shader/ast.cpp b/src/video_core/shader/ast.cpp index 5d0e85f42..56a1b29f3 100644 --- a/src/video_core/shader/ast.cpp +++ b/src/video_core/shader/ast.cpp | |||
| @@ -11,6 +11,147 @@ | |||
| 11 | 11 | ||
| 12 | namespace VideoCommon::Shader { | 12 | namespace VideoCommon::Shader { |
| 13 | 13 | ||
| 14 | ASTZipper::ASTZipper() = default; | ||
| 15 | ASTZipper::ASTZipper(ASTNode new_first) : first{}, last{} { | ||
| 16 | first = new_first; | ||
| 17 | last = new_first; | ||
| 18 | ASTNode current = first; | ||
| 19 | while (current) { | ||
| 20 | current->manager = this; | ||
| 21 | last = current; | ||
| 22 | current = current->next; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | void ASTZipper::PushBack(ASTNode new_node) { | ||
| 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 | |||
| 39 | void ASTZipper::PushFront(ASTNode new_node) { | ||
| 40 | new_node->previous.reset(); | ||
| 41 | new_node->next = first; | ||
| 42 | if (first) { | ||
| 43 | first->previous = first; | ||
| 44 | } | ||
| 45 | first = new_node; | ||
| 46 | if (!last) { | ||
| 47 | last = new_node; | ||
| 48 | } | ||
| 49 | new_node->manager = this; | ||
| 50 | } | ||
| 51 | |||
| 52 | void ASTZipper::InsertAfter(ASTNode new_node, ASTNode at_node) { | ||
| 53 | if (!at_node) { | ||
| 54 | PushFront(new_node); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | new_node->previous = at_node; | ||
| 58 | if (at_node == last) { | ||
| 59 | last = new_node; | ||
| 60 | } | ||
| 61 | new_node->next = at_node->next; | ||
| 62 | at_node->next = new_node; | ||
| 63 | new_node->manager = this; | ||
| 64 | } | ||
| 65 | |||
| 66 | void ASTZipper::SetParent(ASTNode new_parent) { | ||
| 67 | ASTNode current = first; | ||
| 68 | while (current) { | ||
| 69 | current->parent = new_parent; | ||
| 70 | current = current->next; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void ASTZipper::DetachTail(ASTNode node) { | ||
| 75 | ASSERT(node->manager == this); | ||
| 76 | if (node == first) { | ||
| 77 | first.reset(); | ||
| 78 | last.reset(); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | last = node->previous; | ||
| 83 | node->previous.reset(); | ||
| 84 | } | ||
| 85 | |||
| 86 | void ASTZipper::DetachSegment(ASTNode start, ASTNode end) { | ||
| 87 | ASSERT(start->manager == this && end->manager == this); | ||
| 88 | ASTNode prev = start->previous; | ||
| 89 | ASTNode post = end->next; | ||
| 90 | if (!prev) { | ||
| 91 | first = post; | ||
| 92 | } else { | ||
| 93 | prev->next = post; | ||
| 94 | } | ||
| 95 | if (!post) { | ||
| 96 | last = prev; | ||
| 97 | } else { | ||
| 98 | post->previous = prev; | ||
| 99 | } | ||
| 100 | start->previous.reset(); | ||
| 101 | end->next.reset(); | ||
| 102 | ASTNode current = start; | ||
| 103 | bool found = false; | ||
| 104 | while (current) { | ||
| 105 | current->manager = nullptr; | ||
| 106 | current->parent.reset(); | ||
| 107 | found |= current == end; | ||
| 108 | current = current->next; | ||
| 109 | } | ||
| 110 | ASSERT(found); | ||
| 111 | } | ||
| 112 | |||
| 113 | void ASTZipper::DetachSingle(ASTNode node) { | ||
| 114 | ASSERT(node->manager == this); | ||
| 115 | ASTNode prev = node->previous; | ||
| 116 | ASTNode post = node->next; | ||
| 117 | node->previous.reset(); | ||
| 118 | node->next.reset(); | ||
| 119 | if (!prev) { | ||
| 120 | first = post; | ||
| 121 | } else { | ||
| 122 | prev->next = post; | ||
| 123 | } | ||
| 124 | if (!post) { | ||
| 125 | last = prev; | ||
| 126 | } else { | ||
| 127 | post->previous = prev; | ||
| 128 | } | ||
| 129 | |||
| 130 | node->manager = nullptr; | ||
| 131 | node->parent.reset(); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | void ASTZipper::Remove(ASTNode node) { | ||
| 136 | ASSERT(node->manager == this); | ||
| 137 | ASTNode next = node->next; | ||
| 138 | ASTNode previous = node->previous; | ||
| 139 | if (previous) { | ||
| 140 | previous->next = next; | ||
| 141 | } | ||
| 142 | if (next) { | ||
| 143 | next->previous = previous; | ||
| 144 | } | ||
| 145 | node->parent.reset(); | ||
| 146 | node->manager = nullptr; | ||
| 147 | if (node == last) { | ||
| 148 | last = previous; | ||
| 149 | } | ||
| 150 | if (node == first) { | ||
| 151 | first = next; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 14 | class ExprPrinter final { | 155 | class ExprPrinter final { |
| 15 | public: | 156 | public: |
| 16 | ExprPrinter() = default; | 157 | ExprPrinter() = default; |
| @@ -72,32 +213,39 @@ public: | |||
| 72 | void operator()(ASTProgram& ast) { | 213 | void operator()(ASTProgram& ast) { |
| 73 | scope++; | 214 | scope++; |
| 74 | inner += "program {\n"; | 215 | inner += "program {\n"; |
| 75 | for (ASTNode& node : ast.nodes) { | 216 | ASTNode current = ast.nodes.GetFirst(); |
| 76 | Visit(node); | 217 | while (current) { |
| 218 | Visit(current); | ||
| 219 | current = current->GetNext(); | ||
| 77 | } | 220 | } |
| 78 | inner += "}\n"; | 221 | inner += "}\n"; |
| 79 | scope--; | 222 | scope--; |
| 80 | } | 223 | } |
| 81 | 224 | ||
| 82 | void operator()(ASTIf& ast) { | 225 | void operator()(ASTIfThen& ast) { |
| 83 | ExprPrinter expr_parser{}; | 226 | ExprPrinter expr_parser{}; |
| 84 | std::visit(expr_parser, *ast.condition); | 227 | std::visit(expr_parser, *ast.condition); |
| 85 | inner += Ident() + "if (" + expr_parser.GetResult() + ") {\n"; | 228 | inner += Ident() + "if (" + expr_parser.GetResult() + ") {\n"; |
| 86 | scope++; | 229 | scope++; |
| 87 | for (auto& node : ast.then_nodes) { | 230 | ASTNode current = ast.nodes.GetFirst(); |
| 88 | Visit(node); | 231 | while (current) { |
| 232 | Visit(current); | ||
| 233 | current = current->GetNext(); | ||
| 89 | } | 234 | } |
| 90 | scope--; | 235 | scope--; |
| 91 | if (ast.else_nodes.size() > 0) { | 236 | inner += Ident() + "}\n"; |
| 92 | inner += Ident() + "} else {\n"; | 237 | } |
| 93 | scope++; | 238 | |
| 94 | for (auto& node : ast.else_nodes) { | 239 | void operator()(ASTIfElse& ast) { |
| 95 | Visit(node); | 240 | inner += Ident() + "else {\n"; |
| 96 | } | 241 | scope++; |
| 97 | scope--; | 242 | ASTNode current = ast.nodes.GetFirst(); |
| 98 | } else { | 243 | while (current) { |
| 99 | inner += Ident() + "}\n"; | 244 | Visit(current); |
| 245 | current = current->GetNext(); | ||
| 100 | } | 246 | } |
| 247 | scope--; | ||
| 248 | inner += Ident() + "}\n"; | ||
| 101 | } | 249 | } |
| 102 | 250 | ||
| 103 | void operator()(ASTBlockEncoded& ast) { | 251 | void operator()(ASTBlockEncoded& ast) { |
| @@ -128,8 +276,10 @@ public: | |||
| 128 | std::visit(expr_parser, *ast.condition); | 276 | std::visit(expr_parser, *ast.condition); |
| 129 | inner += Ident() + "do {\n"; | 277 | inner += Ident() + "do {\n"; |
| 130 | scope++; | 278 | scope++; |
| 131 | for (auto& node : ast.loop_nodes) { | 279 | ASTNode current = ast.nodes.GetFirst(); |
| 132 | Visit(node); | 280 | while (current) { |
| 281 | Visit(current); | ||
| 282 | current = current->GetNext(); | ||
| 133 | } | 283 | } |
| 134 | scope--; | 284 | scope--; |
| 135 | inner += Ident() + "} while (" + expr_parser.GetResult() + ")\n"; | 285 | inner += Ident() + "} while (" + expr_parser.GetResult() + ")\n"; |
| @@ -142,6 +292,12 @@ public: | |||
| 142 | (ast.kills ? "discard" : "exit") + ";\n"; | 292 | (ast.kills ? "discard" : "exit") + ";\n"; |
| 143 | } | 293 | } |
| 144 | 294 | ||
| 295 | void operator()(ASTBreak& ast) { | ||
| 296 | ExprPrinter expr_parser{}; | ||
| 297 | std::visit(expr_parser, *ast.condition); | ||
| 298 | inner += Ident() + "(" + expr_parser.GetResult() + ") -> break;\n"; | ||
| 299 | } | ||
| 300 | |||
| 145 | std::string& Ident() { | 301 | std::string& Ident() { |
| 146 | if (memo_scope == scope) { | 302 | if (memo_scope == scope) { |
| 147 | return tabs_memo; | 303 | return tabs_memo; |
| @@ -177,4 +333,164 @@ std::string ASTManager::Print() { | |||
| 177 | return printer.GetResult(); | 333 | return printer.GetResult(); |
| 178 | } | 334 | } |
| 179 | 335 | ||
| 336 | #pragma optimize("", off) | ||
| 337 | |||
| 338 | void ASTManager::Decompile() { | ||
| 339 | auto it = gotos.begin(); | ||
| 340 | while (it != gotos.end()) { | ||
| 341 | ASTNode goto_node = *it; | ||
| 342 | u32 label_index = goto_node->GetGotoLabel(); | ||
| 343 | ASTNode label = labels[label_index]; | ||
| 344 | if (IndirectlyRelated(goto_node, label)) { | ||
| 345 | while (!DirectlyRelated(goto_node, label)) { | ||
| 346 | MoveOutward(goto_node); | ||
| 347 | } | ||
| 348 | } | ||
| 349 | if (DirectlyRelated(goto_node, label)) { | ||
| 350 | u32 goto_level = goto_node->GetLevel(); | ||
| 351 | u32 label_level = goto_node->GetLevel(); | ||
| 352 | while (label_level > goto_level) { | ||
| 353 | MoveOutward(goto_node); | ||
| 354 | goto_level++; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | if (label->GetParent() == goto_node->GetParent()) { | ||
| 358 | bool is_loop = false; | ||
| 359 | ASTNode current = goto_node->GetPrevious(); | ||
| 360 | while (current) { | ||
| 361 | if (current == label) { | ||
| 362 | is_loop = true; | ||
| 363 | break; | ||
| 364 | } | ||
| 365 | current = current->GetPrevious(); | ||
| 366 | } | ||
| 367 | |||
| 368 | if (is_loop) { | ||
| 369 | EncloseDoWhile(goto_node, label); | ||
| 370 | } else { | ||
| 371 | EncloseIfThen(goto_node, label); | ||
| 372 | } | ||
| 373 | it = gotos.erase(it); | ||
| 374 | continue; | ||
| 375 | } | ||
| 376 | it++; | ||
| 377 | } | ||
| 378 | /* | ||
| 379 | for (ASTNode label : labels) { | ||
| 380 | auto& manager = label->GetManager(); | ||
| 381 | manager.Remove(label); | ||
| 382 | } | ||
| 383 | labels.clear(); | ||
| 384 | */ | ||
| 385 | } | ||
| 386 | |||
| 387 | bool ASTManager::IndirectlyRelated(ASTNode first, ASTNode second) { | ||
| 388 | return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second)); | ||
| 389 | } | ||
| 390 | |||
| 391 | bool ASTManager::DirectlyRelated(ASTNode first, ASTNode second) { | ||
| 392 | if (first->GetParent() == second->GetParent()) { | ||
| 393 | return false; | ||
| 394 | } | ||
| 395 | u32 first_level = first->GetLevel(); | ||
| 396 | u32 second_level = second->GetLevel(); | ||
| 397 | u32 min_level; | ||
| 398 | u32 max_level; | ||
| 399 | ASTNode max; | ||
| 400 | ASTNode min; | ||
| 401 | if (first_level > second_level) { | ||
| 402 | min_level = second_level; | ||
| 403 | min = second; | ||
| 404 | max_level = first_level; | ||
| 405 | max = first; | ||
| 406 | } else { | ||
| 407 | min_level = first_level; | ||
| 408 | min = first; | ||
| 409 | max_level = second_level; | ||
| 410 | max = second; | ||
| 411 | } | ||
| 412 | |||
| 413 | while (min_level < max_level) { | ||
| 414 | min_level++; | ||
| 415 | min = min->GetParent(); | ||
| 416 | } | ||
| 417 | |||
| 418 | return (min->GetParent() == max->GetParent()); | ||
| 419 | } | ||
| 420 | |||
| 421 | void ASTManager::EncloseDoWhile(ASTNode goto_node, ASTNode label) { | ||
| 422 | ASTZipper& zipper = goto_node->GetManager(); | ||
| 423 | ASTNode loop_start = label->GetNext(); | ||
| 424 | if (loop_start == goto_node) { | ||
| 425 | zipper.Remove(goto_node); | ||
| 426 | return; | ||
| 427 | } | ||
| 428 | ASTNode parent = label->GetParent(); | ||
| 429 | Expr condition = goto_node->GetGotoCondition(); | ||
| 430 | zipper.DetachSegment(loop_start, goto_node); | ||
| 431 | ASTNode do_while_node = ASTBase::Make<ASTDoWhile>(parent, condition, ASTZipper(loop_start)); | ||
| 432 | zipper.InsertAfter(do_while_node, label); | ||
| 433 | ASTZipper* sub_zipper = do_while_node->GetSubNodes(); | ||
| 434 | sub_zipper->SetParent(do_while_node); | ||
| 435 | sub_zipper->Remove(goto_node); | ||
| 436 | } | ||
| 437 | |||
| 438 | void ASTManager::EncloseIfThen(ASTNode goto_node, ASTNode label) { | ||
| 439 | ASTZipper& zipper = goto_node->GetManager(); | ||
| 440 | ASTNode if_end = label->GetPrevious(); | ||
| 441 | if (if_end == goto_node) { | ||
| 442 | zipper.Remove(goto_node); | ||
| 443 | return; | ||
| 444 | } | ||
| 445 | ASTNode prev = goto_node->GetPrevious(); | ||
| 446 | ASTNode parent = label->GetParent(); | ||
| 447 | Expr condition = goto_node->GetGotoCondition(); | ||
| 448 | Expr neg_condition = MakeExpr<ExprNot>(condition); | ||
| 449 | zipper.DetachSegment(goto_node, if_end); | ||
| 450 | ASTNode if_node = ASTBase::Make<ASTIfThen>(parent, condition, ASTZipper(goto_node)); | ||
| 451 | zipper.InsertAfter(if_node, prev); | ||
| 452 | ASTZipper* sub_zipper = if_node->GetSubNodes(); | ||
| 453 | sub_zipper->SetParent(if_node); | ||
| 454 | sub_zipper->Remove(goto_node); | ||
| 455 | } | ||
| 456 | |||
| 457 | void ASTManager::MoveOutward(ASTNode goto_node) { | ||
| 458 | ASTZipper& zipper = goto_node->GetManager(); | ||
| 459 | ASTNode parent = goto_node->GetParent(); | ||
| 460 | bool is_loop = parent->IsLoop(); | ||
| 461 | bool is_if = parent->IsIfThen() || parent->IsIfElse(); | ||
| 462 | |||
| 463 | ASTNode prev = goto_node->GetPrevious(); | ||
| 464 | |||
| 465 | Expr condition = goto_node->GetGotoCondition(); | ||
| 466 | u32 var_index = NewVariable(); | ||
| 467 | Expr var_condition = MakeExpr<ExprVar>(var_index); | ||
| 468 | ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition); | ||
| 469 | zipper.DetachSingle(goto_node); | ||
| 470 | zipper.InsertAfter(var_node, prev); | ||
| 471 | goto_node->SetGotoCondition(var_condition); | ||
| 472 | if (is_loop) { | ||
| 473 | ASTNode break_node = ASTBase::Make<ASTBreak>(parent, var_condition); | ||
| 474 | zipper.InsertAfter(break_node, var_node); | ||
| 475 | } else if (is_if) { | ||
| 476 | ASTNode post = var_node->GetNext(); | ||
| 477 | if (post) { | ||
| 478 | zipper.DetachTail(post); | ||
| 479 | ASTNode if_node = ASTBase::Make<ASTIfThen>(parent, var_condition, ASTZipper(post)); | ||
| 480 | zipper.InsertAfter(if_node, var_node); | ||
| 481 | ASTZipper* sub_zipper = if_node->GetSubNodes(); | ||
| 482 | sub_zipper->SetParent(if_node); | ||
| 483 | } | ||
| 484 | } else { | ||
| 485 | UNREACHABLE(); | ||
| 486 | } | ||
| 487 | ASTZipper& zipper2 = parent->GetManager(); | ||
| 488 | ASTNode next = parent->GetNext(); | ||
| 489 | if (is_if && next && next->IsIfElse()) { | ||
| 490 | zipper2.InsertAfter(goto_node, next); | ||
| 491 | return; | ||
| 492 | } | ||
| 493 | zipper2.InsertAfter(goto_node, parent); | ||
| 494 | } | ||
| 495 | |||
| 180 | } // namespace VideoCommon::Shader | 496 | } // namespace VideoCommon::Shader |
diff --git a/src/video_core/shader/ast.h b/src/video_core/shader/ast.h index ca71543fb..22ac8884c 100644 --- a/src/video_core/shader/ast.h +++ b/src/video_core/shader/ast.h | |||
| @@ -18,32 +18,71 @@ namespace VideoCommon::Shader { | |||
| 18 | 18 | ||
| 19 | class ASTBase; | 19 | class ASTBase; |
| 20 | class ASTProgram; | 20 | class ASTProgram; |
| 21 | class ASTIf; | 21 | class ASTIfThen; |
| 22 | class ASTIfElse; | ||
| 22 | class ASTBlockEncoded; | 23 | class ASTBlockEncoded; |
| 23 | class ASTVarSet; | 24 | class ASTVarSet; |
| 24 | class ASTGoto; | 25 | class ASTGoto; |
| 25 | class ASTLabel; | 26 | class ASTLabel; |
| 26 | class ASTDoWhile; | 27 | class ASTDoWhile; |
| 27 | class ASTReturn; | 28 | class ASTReturn; |
| 29 | class ASTBreak; | ||
| 28 | 30 | ||
| 29 | using ASTData = std::variant<ASTProgram, ASTIf, ASTBlockEncoded, ASTVarSet, ASTGoto, ASTLabel, | 31 | using ASTData = std::variant<ASTProgram, ASTIfThen, ASTIfElse, ASTBlockEncoded, ASTVarSet, ASTGoto, |
| 30 | ASTDoWhile, ASTReturn>; | 32 | ASTLabel, ASTDoWhile, ASTReturn, ASTBreak>; |
| 31 | 33 | ||
| 32 | using ASTNode = std::shared_ptr<ASTBase>; | 34 | using ASTNode = std::shared_ptr<ASTBase>; |
| 33 | 35 | ||
| 36 | enum class ASTZipperType : u32 { | ||
| 37 | Program, | ||
| 38 | IfThen, | ||
| 39 | IfElse, | ||
| 40 | Loop, | ||
| 41 | }; | ||
| 42 | |||
| 43 | class ASTZipper final { | ||
| 44 | public: | ||
| 45 | ASTZipper(); | ||
| 46 | ASTZipper(ASTNode first); | ||
| 47 | |||
| 48 | ASTNode GetFirst() { | ||
| 49 | return first; | ||
| 50 | } | ||
| 51 | |||
| 52 | ASTNode GetLast() { | ||
| 53 | return last; | ||
| 54 | } | ||
| 55 | |||
| 56 | void PushBack(ASTNode new_node); | ||
| 57 | void PushFront(ASTNode new_node); | ||
| 58 | void InsertAfter(ASTNode new_node, ASTNode at_node); | ||
| 59 | void SetParent(ASTNode new_parent); | ||
| 60 | void DetachTail(ASTNode node); | ||
| 61 | void DetachSingle(ASTNode node); | ||
| 62 | void DetachSegment(ASTNode start, ASTNode end); | ||
| 63 | void Remove(ASTNode node); | ||
| 64 | |||
| 65 | ASTNode first{}; | ||
| 66 | ASTNode last{}; | ||
| 67 | }; | ||
| 68 | |||
| 34 | class ASTProgram { | 69 | class ASTProgram { |
| 35 | public: | 70 | public: |
| 36 | ASTProgram() = default; | 71 | ASTProgram() : nodes{} {}; |
| 37 | std::list<ASTNode> nodes; | 72 | ASTZipper nodes; |
| 38 | }; | 73 | }; |
| 39 | 74 | ||
| 40 | class ASTIf { | 75 | class ASTIfThen { |
| 41 | public: | 76 | public: |
| 42 | ASTIf(Expr condition, std::list<ASTNode> then_nodes, std::list<ASTNode> else_nodes) | 77 | ASTIfThen(Expr condition, ASTZipper nodes) : condition(condition), nodes{nodes} {} |
| 43 | : condition(condition), then_nodes{then_nodes}, else_nodes{then_nodes} {} | ||
| 44 | Expr condition; | 78 | Expr condition; |
| 45 | std::list<ASTNode> then_nodes; | 79 | ASTZipper nodes; |
| 46 | std::list<ASTNode> else_nodes; | 80 | }; |
| 81 | |||
| 82 | class ASTIfElse { | ||
| 83 | public: | ||
| 84 | ASTIfElse(ASTZipper nodes) : nodes{nodes} {} | ||
| 85 | ASTZipper nodes; | ||
| 47 | }; | 86 | }; |
| 48 | 87 | ||
| 49 | class ASTBlockEncoded { | 88 | class ASTBlockEncoded { |
| @@ -75,10 +114,9 @@ public: | |||
| 75 | 114 | ||
| 76 | class ASTDoWhile { | 115 | class ASTDoWhile { |
| 77 | public: | 116 | public: |
| 78 | ASTDoWhile(Expr condition, std::list<ASTNode> loop_nodes) | 117 | ASTDoWhile(Expr condition, ASTZipper nodes) : condition(condition), nodes{nodes} {} |
| 79 | : condition(condition), loop_nodes{loop_nodes} {} | ||
| 80 | Expr condition; | 118 | Expr condition; |
| 81 | std::list<ASTNode> loop_nodes; | 119 | ASTZipper nodes; |
| 82 | }; | 120 | }; |
| 83 | 121 | ||
| 84 | class ASTReturn { | 122 | class ASTReturn { |
| @@ -88,6 +126,12 @@ public: | |||
| 88 | bool kills; | 126 | bool kills; |
| 89 | }; | 127 | }; |
| 90 | 128 | ||
| 129 | class ASTBreak { | ||
| 130 | public: | ||
| 131 | ASTBreak(Expr condition) : condition{condition} {} | ||
| 132 | Expr condition; | ||
| 133 | }; | ||
| 134 | |||
| 91 | class ASTBase { | 135 | class ASTBase { |
| 92 | public: | 136 | public: |
| 93 | explicit ASTBase(ASTNode parent, ASTData data) : parent{parent}, data{data} {} | 137 | explicit ASTBase(ASTNode parent, ASTData data) : parent{parent}, data{data} {} |
| @@ -111,9 +155,9 @@ public: | |||
| 111 | 155 | ||
| 112 | u32 GetLevel() const { | 156 | u32 GetLevel() const { |
| 113 | u32 level = 0; | 157 | u32 level = 0; |
| 114 | auto next = parent; | 158 | auto next_parent = parent; |
| 115 | while (next) { | 159 | while (next_parent) { |
| 116 | next = next->GetParent(); | 160 | next_parent = next_parent->GetParent(); |
| 117 | level++; | 161 | level++; |
| 118 | } | 162 | } |
| 119 | return level; | 163 | return level; |
| @@ -123,15 +167,83 @@ public: | |||
| 123 | return &data; | 167 | return &data; |
| 124 | } | 168 | } |
| 125 | 169 | ||
| 170 | ASTNode GetNext() { | ||
| 171 | return next; | ||
| 172 | } | ||
| 173 | |||
| 174 | ASTNode GetPrevious() { | ||
| 175 | return previous; | ||
| 176 | } | ||
| 177 | |||
| 178 | ASTZipper& GetManager() { | ||
| 179 | return *manager; | ||
| 180 | } | ||
| 181 | |||
| 182 | u32 GetGotoLabel() const { | ||
| 183 | auto inner = std::get_if<ASTGoto>(&data); | ||
| 184 | if (inner) { | ||
| 185 | return inner->label; | ||
| 186 | } | ||
| 187 | return -1; | ||
| 188 | } | ||
| 189 | |||
| 190 | Expr GetGotoCondition() const { | ||
| 191 | auto inner = std::get_if<ASTGoto>(&data); | ||
| 192 | if (inner) { | ||
| 193 | return inner->condition; | ||
| 194 | } | ||
| 195 | return nullptr; | ||
| 196 | } | ||
| 197 | |||
| 198 | void SetGotoCondition(Expr new_condition) { | ||
| 199 | auto inner = std::get_if<ASTGoto>(&data); | ||
| 200 | if (inner) { | ||
| 201 | inner->condition = new_condition; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | bool IsIfThen() const { | ||
| 206 | return std::holds_alternative<ASTIfThen>(data); | ||
| 207 | } | ||
| 208 | |||
| 209 | bool IsIfElse() const { | ||
| 210 | return std::holds_alternative<ASTIfElse>(data); | ||
| 211 | } | ||
| 212 | |||
| 213 | bool IsLoop() const { | ||
| 214 | return std::holds_alternative<ASTDoWhile>(data); | ||
| 215 | } | ||
| 216 | |||
| 217 | ASTZipper* GetSubNodes() { | ||
| 218 | if (std::holds_alternative<ASTProgram>(data)) { | ||
| 219 | return &std::get_if<ASTProgram>(&data)->nodes; | ||
| 220 | } | ||
| 221 | if (std::holds_alternative<ASTIfThen>(data)) { | ||
| 222 | return &std::get_if<ASTIfThen>(&data)->nodes; | ||
| 223 | } | ||
| 224 | if (std::holds_alternative<ASTIfElse>(data)) { | ||
| 225 | return &std::get_if<ASTIfElse>(&data)->nodes; | ||
| 226 | } | ||
| 227 | if (std::holds_alternative<ASTDoWhile>(data)) { | ||
| 228 | return &std::get_if<ASTDoWhile>(&data)->nodes; | ||
| 229 | } | ||
| 230 | return nullptr; | ||
| 231 | } | ||
| 232 | |||
| 126 | private: | 233 | private: |
| 234 | friend class ASTZipper; | ||
| 235 | |||
| 127 | ASTData data; | 236 | ASTData data; |
| 128 | ASTNode parent; | 237 | ASTNode parent; |
| 238 | ASTNode next{}; | ||
| 239 | ASTNode previous{}; | ||
| 240 | ASTZipper* manager{}; | ||
| 129 | }; | 241 | }; |
| 130 | 242 | ||
| 131 | class ASTManager final { | 243 | class ASTManager final { |
| 132 | public: | 244 | public: |
| 133 | explicit ASTManager() { | 245 | explicit ASTManager() { |
| 134 | main_node = ASTBase::Make<ASTProgram>(nullptr); | 246 | main_node = ASTBase::Make<ASTProgram>(ASTNode{}); |
| 135 | program = std::get_if<ASTProgram>(main_node->GetInnerData()); | 247 | program = std::get_if<ASTProgram>(main_node->GetInnerData()); |
| 136 | } | 248 | } |
| 137 | 249 | ||
| @@ -147,31 +259,49 @@ public: | |||
| 147 | u32 index = labels_map[address]; | 259 | u32 index = labels_map[address]; |
| 148 | ASTNode label = ASTBase::Make<ASTLabel>(main_node, index); | 260 | ASTNode label = ASTBase::Make<ASTLabel>(main_node, index); |
| 149 | labels[index] = label; | 261 | labels[index] = label; |
| 150 | program->nodes.push_back(label); | 262 | program->nodes.PushBack(label); |
| 151 | } | 263 | } |
| 152 | 264 | ||
| 153 | void InsertGoto(Expr condition, u32 address) { | 265 | void InsertGoto(Expr condition, u32 address) { |
| 154 | u32 index = labels_map[address]; | 266 | u32 index = labels_map[address]; |
| 155 | ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index); | 267 | ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index); |
| 156 | gotos.push_back(goto_node); | 268 | gotos.push_back(goto_node); |
| 157 | program->nodes.push_back(goto_node); | 269 | program->nodes.PushBack(goto_node); |
| 158 | } | 270 | } |
| 159 | 271 | ||
| 160 | void InsertBlock(u32 start_address, u32 end_address) { | 272 | void InsertBlock(u32 start_address, u32 end_address) { |
| 161 | ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address); | 273 | ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address); |
| 162 | program->nodes.push_back(block); | 274 | program->nodes.PushBack(block); |
| 163 | } | 275 | } |
| 164 | 276 | ||
| 165 | void InsertReturn(Expr condition, bool kills) { | 277 | void InsertReturn(Expr condition, bool kills) { |
| 166 | ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills); | 278 | ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills); |
| 167 | program->nodes.push_back(node); | 279 | program->nodes.PushBack(node); |
| 168 | } | 280 | } |
| 169 | 281 | ||
| 170 | std::string Print(); | 282 | std::string Print(); |
| 171 | 283 | ||
| 172 | void Decompile() {} | 284 | void Decompile(); |
| 285 | |||
| 286 | |||
| 173 | 287 | ||
| 174 | private: | 288 | private: |
| 289 | bool IndirectlyRelated(ASTNode first, ASTNode second); | ||
| 290 | |||
| 291 | bool DirectlyRelated(ASTNode first, ASTNode second); | ||
| 292 | |||
| 293 | void EncloseDoWhile(ASTNode goto_node, ASTNode label); | ||
| 294 | |||
| 295 | void EncloseIfThen(ASTNode goto_node, ASTNode label); | ||
| 296 | |||
| 297 | void MoveOutward(ASTNode goto_node) ; | ||
| 298 | |||
| 299 | u32 NewVariable() { | ||
| 300 | u32 new_var = variables; | ||
| 301 | variables++; | ||
| 302 | return new_var; | ||
| 303 | } | ||
| 304 | |||
| 175 | std::unordered_map<u32, u32> labels_map{}; | 305 | std::unordered_map<u32, u32> labels_map{}; |
| 176 | u32 labels_count{}; | 306 | u32 labels_count{}; |
| 177 | std::vector<ASTNode> labels{}; | 307 | std::vector<ASTNode> labels{}; |