summaryrefslogtreecommitdiff
path: root/src/video_core/shader/ast.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/shader/ast.cpp')
-rw-r--r--src/video_core/shader/ast.cpp348
1 files changed, 332 insertions, 16 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
12namespace VideoCommon::Shader { 12namespace VideoCommon::Shader {
13 13
14ASTZipper::ASTZipper() = default;
15ASTZipper::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
26void 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
39void 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
52void 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
66void ASTZipper::SetParent(ASTNode new_parent) {
67 ASTNode current = first;
68 while (current) {
69 current->parent = new_parent;
70 current = current->next;
71 }
72}
73
74void 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
86void 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
113void 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
135void 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
14class ExprPrinter final { 155class ExprPrinter final {
15public: 156public:
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
338void 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
387bool ASTManager::IndirectlyRelated(ASTNode first, ASTNode second) {
388 return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second));
389}
390
391bool 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
421void 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
438void 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
457void 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