]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.cpp
Move unused aggregate detection to UnusedVariableDetector
[libs/gl.git] / source / programcompiler.cpp
1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "error.h"
5 #include "program.h"
6 #include "programcompiler.h"
7 #include "shader.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 using namespace ProgramSyntax;
15
16 ProgramCompiler::ProgramCompiler():
17         module(0)
18 { }
19
20 void ProgramCompiler::compile(const string &source)
21 {
22         module = &parser.parse(source);
23         process();
24 }
25
26 void ProgramCompiler::compile(IO::Base &io)
27 {
28         module = &parser.parse(io);
29         process();
30 }
31
32 void ProgramCompiler::add_shaders(Program &program)
33 {
34         if(!module)
35                 throw invalid_operation("ProgramCompiler::add_shaders");
36
37         string head = "#version 150\n";
38         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
39         {
40                 if(i->type==VERTEX)
41                         program.attach_shader_owned(new VertexShader(head+create_source(*i)));
42                 else if(i->type==GEOMETRY)
43                         program.attach_shader_owned(new GeometryShader(head+create_source(*i)));
44                 else if(i->type==FRAGMENT)
45                         program.attach_shader_owned(new FragmentShader(head+create_source(*i)));
46         }
47
48         program.bind_attribute(VERTEX4, "vertex");
49         program.bind_attribute(NORMAL3, "normal");
50         program.bind_attribute(COLOR4_FLOAT, "color");
51         program.bind_attribute(TEXCOORD4, "texcoord");
52 }
53
54 void ProgramCompiler::process()
55 {
56         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
57                 generate(*i);
58         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
59         {
60                 if(optimize(*i))
61                         i = module->stages.begin();
62                 else
63                         ++i;
64         }
65 }
66
67 void ProgramCompiler::generate(Stage &stage)
68 {
69         inject_block(stage.content, module->shared.content);
70
71         apply<VariableResolver>(stage);
72         apply<InterfaceGenerator>(stage);
73         apply<VariableResolver>(stage);
74         apply<VariableRenamer>(stage);
75 }
76
77 bool ProgramCompiler::optimize(Stage &stage)
78 {
79         UnusedVariableLocator unused_locator;
80         unused_locator.apply(stage);
81
82         NodeRemover remover;
83         remover.to_remove = unused_locator.unused_nodes;
84         remover.apply(stage);
85
86         return !unused_locator.unused_nodes.empty();
87 }
88
89 void ProgramCompiler::inject_block(Block &target, const Block &source)
90 {
91         list<NodePtr<Node> >::iterator insert_point = target.body.begin();
92         for(list<NodePtr<Node> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
93                 target.body.insert(insert_point, (*i)->clone());
94 }
95
96 template<typename T>
97 void ProgramCompiler::apply(Stage &stage)
98 {
99         T visitor;
100         visitor.apply(stage);
101 }
102
103 string ProgramCompiler::create_source(Stage &stage)
104 {
105         Formatter formatter;
106         formatter.apply(stage);
107         return formatter.formatted;
108 }
109
110
111 ProgramCompiler::Visitor::Visitor():
112         stage(0)
113 { }
114
115 void ProgramCompiler::Visitor::apply(Stage &s)
116 {
117         SetForScope<Stage *> set(stage, &s);
118         stage->content.visit(*this);
119 }
120
121
122 ProgramCompiler::Formatter::Formatter():
123         indent(0),
124         parameter_list(false),
125         else_if(false)
126 { }
127
128 void ProgramCompiler::Formatter::visit(Literal &literal)
129 {
130         formatted += literal.token;
131 }
132
133 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
134 {
135         formatted += '(';
136         parexpr.expression->visit(*this);
137         formatted += ')';
138 }
139
140 void ProgramCompiler::Formatter::visit(VariableReference &var)
141 {
142         formatted += var.name;
143 }
144
145 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
146 {
147         memacc.left->visit(*this);
148         formatted += format(".%s", memacc.member);
149 }
150
151 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
152 {
153         if(unary.prefix)
154                 formatted += unary.oper;
155         unary.expression->visit(*this);
156         if(!unary.prefix)
157                 formatted += unary.oper;
158 }
159
160 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
161 {
162         binary.left->visit(*this);
163         if(binary.assignment)
164                 formatted += format(" %s ", binary.oper);
165         else
166                 formatted += binary.oper;
167         binary.right->visit(*this);
168         formatted += binary.after;
169 }
170
171 void ProgramCompiler::Formatter::visit(FunctionCall &call)
172 {
173         formatted += format("%s(", call.name);
174         for(vector<NodePtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
175         {
176                 if(i!=call.arguments.begin())
177                         formatted += ", ";
178                 (*i)->visit(*this);
179         }
180         formatted += ')';
181 }
182
183 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
184 {
185         expr.expression->visit(*this);
186         formatted += ';';
187 }
188
189 void ProgramCompiler::Formatter::visit(Block &block)
190 {
191         if(block.use_braces)
192         {
193                 if(else_if)
194                 {
195                         formatted += '\n';
196                         else_if = false;
197                 }
198                 formatted += format("%s{\n", string(indent*2, ' '));
199         }
200
201         bool change_indent = (!formatted.empty() && !else_if);
202         indent += change_indent;
203         string spaces(indent*2, ' ');
204         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
205         {
206                 if(i!=block.body.begin())
207                         formatted += '\n';
208                 if(!else_if)
209                         formatted += spaces;
210                 (*i)->visit(*this);
211         }
212         indent -= change_indent;
213
214         if(block.use_braces)
215                 formatted += format("\n%s}", string(indent*2, ' '));
216 }
217
218 void ProgramCompiler::Formatter::visit(Layout &layout)
219 {
220         formatted += "layout(";
221         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
222         {
223                 if(i!=layout.qualifiers.begin())
224                         formatted += ", ";
225                 formatted += i->identifier;
226                 if(!i->value.empty())
227                         formatted += format("=%s", i->value);
228         }
229         formatted += format(") %s;", layout.interface);
230 }
231
232 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
233 {
234         formatted += format("struct %s\n", strct.name);
235         strct.members.visit(*this);
236         formatted += ';';
237 }
238
239 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
240 {
241         if(var.constant)
242                 formatted += "const ";
243         if(!var.sampling.empty())
244                 formatted += format("%s ", var.sampling);
245         if(!var.interface.empty())
246                 formatted += format("%s ", var.interface);
247         formatted += format("%s %s", var.type, var.name);
248         if(var.array)
249         {
250                 formatted += '[';
251                 if(var.array_size)
252                         var.array_size->visit(*this);
253                 formatted += ']';
254         }
255         if(var.init_expression)
256         {
257                 formatted += " = ";
258                 var.init_expression->visit(*this);
259         }
260         if(!parameter_list)
261                 formatted += ';';
262 }
263
264 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
265 {
266         formatted += format("%s %s\n", iface.interface, iface.name);
267         iface.members.visit(*this);
268         formatted += ';';
269 }
270
271 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
272 {
273         formatted += format("%s %s(", func.return_type, func.name);
274         for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
275         {
276                 if(i!=func.parameters.begin())
277                         formatted += ", ";
278                 SetFlag set(parameter_list);
279                 (*i)->visit(*this);
280         }
281         formatted += ')';
282         if(func.definition)
283         {
284                 formatted += '\n';
285                 func.body.visit(*this);
286         }
287         else
288                 formatted += ';';
289 }
290
291 void ProgramCompiler::Formatter::visit(Conditional &cond)
292 {
293         if(else_if)
294         {
295                 formatted += ' ';
296                 else_if = false;
297         }
298
299         formatted += "if(";
300         cond.condition->visit(*this);
301         formatted += ")\n";
302
303         cond.body.visit(*this);
304         if(!cond.else_body.body.empty())
305         {
306                 formatted += format("\n%selse", string(indent*2, ' '));
307                 SetFlag set(else_if);
308                 cond.else_body.visit(*this);
309         }
310 }
311
312 void ProgramCompiler::Formatter::visit(Iteration &iter)
313 {
314         formatted += "for(";
315         iter.init_statement->visit(*this);
316         formatted += ' ';
317         iter.condition->visit(*this);
318         formatted += "; ";
319         iter.loop_expression->visit(*this);
320         formatted += ")\n";
321         iter.body.visit(*this);
322 }
323
324 void ProgramCompiler::Formatter::visit(Return &ret)
325 {
326         formatted += "return ";
327         ret.expression->visit(*this);
328         formatted += ';';
329 }
330
331
332 ProgramCompiler::VariableResolver::VariableResolver():
333         anonymous(false)
334 { }
335
336 void ProgramCompiler::VariableResolver::visit(Block &block)
337 {
338         blocks.push_back(&block);
339         block.variables.clear();
340         TraversingVisitor::visit(block);
341         blocks.pop_back();
342 }
343
344 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
345 {
346         var.declaration = 0;
347         type = 0;
348         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
349         {
350                 --i;
351                 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
352                 if(j!=(*i)->variables.end())
353                 {
354                         var.declaration = j->second;
355                         type = j->second->type_declaration;
356                         break;
357                 }
358         }
359 }
360
361 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
362 {
363         type = 0;
364         TraversingVisitor::visit(memacc);
365         memacc.declaration = 0;
366         if(type)
367         {
368                 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
369                 if(i!=type->members.variables.end())
370                 {
371                         memacc.declaration = i->second;
372                         type = i->second->type_declaration;
373                 }
374                 else
375                         type = 0;
376         }
377 }
378
379 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
380 {
381         if(binary.oper=="[")
382         {
383                 binary.right->visit(*this);
384                 type = 0;
385                 binary.left->visit(*this);
386         }
387         else
388         {
389                 TraversingVisitor::visit(binary);
390                 type = 0;
391         }
392 }
393
394 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
395 {
396         TraversingVisitor::visit(strct);
397         blocks.back()->types[strct.name] = &strct;
398 }
399
400 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
401 {
402         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
403         {
404                 --i;
405                 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
406                 if(j!=(*i)->types.end())
407                         var.type_declaration = j->second;
408         }
409
410         TraversingVisitor::visit(var);
411         blocks.back()->variables[var.name] = &var;
412         if(anonymous && blocks.size()>1)
413                 blocks[blocks.size()-2]->variables[var.name] = &var;
414 }
415
416 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
417 {
418         SetFlag set(anonymous);
419         TraversingVisitor::visit(iface);
420 }
421
422
423 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
424         scope_level(0),
425         remove_node(false)
426 { }
427
428 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
429 {
430         if(type==VERTEX)
431                 return "_vs_out_";
432         else if(type==GEOMETRY)
433                 return "_gs_out_";
434         else
435                 return string();
436 }
437
438 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
439 {
440         SetForScope<Stage *> set(stage, &s);
441         if(stage->previous)
442                 in_prefix = get_out_prefix(stage->previous->type);
443         out_prefix = get_out_prefix(stage->type);
444         stage->content.visit(*this);
445 }
446
447 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
448 {
449         SetForScope<unsigned> set(scope_level, scope_level+1);
450         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
451         {
452                 (*i)->visit(*this);
453
454                 if(scope_level==1)
455                 {
456                         for(map<string, VariableDeclaration *>::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
457                         {
458                                 list<NodePtr<Node> >::iterator k = block.body.insert(i, j->second);
459                                 (*k)->visit(*this);
460                         }
461                         iface_declarations.clear();
462                 }
463
464                 for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
465                         block.body.insert(i, *j);
466                 insert_nodes.clear();
467
468                 if(remove_node)
469                         block.body.erase(i++);
470                 else
471                         ++i;
472                 remove_node = false;
473         }
474 }
475
476 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
477 {
478         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
479         return prefix+name.substr(offset);
480 }
481
482 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
483 {
484         const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
485         if(stage_vars.count(name) || iface_declarations.count(name))
486                 return false;
487
488         VariableDeclaration* iface_var = new VariableDeclaration;
489         iface_var->sampling = var.sampling;
490         iface_var->interface = iface;
491         iface_var->type = var.type;
492         iface_var->type_declaration = var.type_declaration;
493         iface_var->name = name;
494         iface_var->array = (var.array || (stage->type==GEOMETRY && iface=="in"));
495         iface_var->array_size = var.array_size;
496         if(iface=="in")
497                 iface_var->linked_declaration = &var;
498         iface_declarations[name] = iface_var;
499
500         return true;
501 }
502
503 void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
504 {
505         BinaryExpression *assign = new BinaryExpression;
506         VariableReference *ref = new VariableReference;
507         ref->name = left;
508         assign->left = ref;
509         assign->oper = "=";
510         assign->right = right;
511         assign->assignment = true;
512
513         ExpressionStatement *stmt = new ExpressionStatement;
514         stmt->expression = assign;
515         insert_nodes.push_back(stmt);
516 }
517
518 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
519 {
520         if(var.declaration || !stage->previous)
521                 return;
522         if(iface_declarations.count(var.name))
523                 return;
524
525         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
526         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
527         if(i==prev_out.end())
528                 i = prev_out.find(in_prefix+var.name);
529         if(i!=prev_out.end())
530                 generate_interface(*i->second, "in", var.name);
531 }
532
533 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
534 {
535         if(var.interface=="out")
536         {
537                 if(scope_level==1)
538                         stage->out_variables[var.name] = &var;
539                 else if(generate_interface(var, "out", change_prefix(var.name, string())))
540                 {
541                         remove_node = true;
542                         if(var.init_expression)
543                                 insert_assignment(var.name, var.init_expression->clone());
544                 }
545         }
546         else if(var.interface=="in")
547         {
548                 stage->in_variables[var.name] = &var;
549                 if(var.linked_declaration)
550                         var.linked_declaration->linked_declaration = &var;
551                 else if(stage->previous)
552                 {
553                         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
554                         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
555                         if(i!=prev_out.end())
556                         {
557                                 var.linked_declaration = i->second;
558                                 i->second->linked_declaration = &var;
559                         }
560                 }
561         }
562
563         TraversingVisitor::visit(var);
564 }
565
566 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
567 {
568         vector<VariableDeclaration *> pass_vars;
569
570         for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
571                 pass_vars.push_back(i->second);
572         for(map<string, VariableDeclaration *>::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
573                 if(i->second->interface=="in")
574                         pass_vars.push_back(i->second);
575
576         if(stage->previous)
577         {
578                 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
579                 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
580                 {
581                         bool linked = false;
582                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
583                                 linked = ((*j)->linked_declaration==i->second);
584
585                         if(!linked && generate_interface(*i->second, "in", i->second->name))
586                                 pass_vars.push_back(i->second);
587                 }
588         }
589
590         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
591         {
592                 string out_name = change_prefix((*i)->name, out_prefix);
593                 generate_interface(**i, "out", out_name);
594
595                 VariableReference *ref = new VariableReference;
596                 ref->name = (*i)->name;
597                 if(pass.subscript)
598                 {
599                         BinaryExpression *subscript = new BinaryExpression;
600                         subscript->left = ref;
601                         subscript->oper = "[";
602                         subscript->right = pass.subscript;
603                         subscript->after = "]";
604                         insert_assignment(out_name, subscript);
605                 }
606                 else
607                         insert_assignment(out_name, ref);
608         }
609
610         remove_node = true;
611 }
612
613
614 void ProgramCompiler::VariableRenamer::visit(VariableReference &var)
615 {
616         if(var.declaration)
617                 var.name = var.declaration->name;
618 }
619
620 void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
621 {
622         if(var.linked_declaration)
623                 var.name = var.linked_declaration->name;
624         TraversingVisitor::visit(var);
625 }
626
627
628 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
629         aggregate(0),
630         assignment(false),
631         record_target(false),
632         assignment_target(0),
633         indeterminate_target(false),
634         self_referencing(false)
635 { }
636
637 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
638 {
639         if(record_target)
640         {
641                 if(assignment_target)
642                         indeterminate_target = true;
643                 else
644                         assignment_target = var.declaration;
645         }
646         else
647         {
648                 unused_nodes.erase(var.declaration);
649
650                 map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
651                 if(i!=assignments.end())
652                 {
653                         unused_nodes.erase(i->second);
654                         assignments.erase(i);
655                 }
656
657                 if(assignment && var.declaration==assignment_target)
658                         self_referencing = true;
659         }
660
661         map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
662         if(i!=aggregates.end())
663                 unused_nodes.erase(i->second);
664 }
665
666 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
667 {
668         TraversingVisitor::visit(memacc);
669         unused_nodes.erase(memacc.declaration);
670 }
671
672 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
673 {
674         if(binary.assignment)
675         {
676                 assignment = true;
677                 {
678                         SetFlag set(record_target);
679                         binary.left->visit(*this);
680                 }
681                 if(binary.oper!="=")
682                         self_referencing = true;
683                 binary.right->visit(*this);
684         }
685         else if(record_target && binary.oper=="[")
686         {
687                 binary.left->visit(*this);
688                 SetForScope<bool> set(record_target, false);
689                 binary.right->visit(*this);
690         }
691         else
692                 TraversingVisitor::visit(binary);
693 }
694
695 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
696 {
697         assignment = false;
698         assignment_target = 0;
699         indeterminate_target = false;
700         self_referencing = false;
701         TraversingVisitor::visit(expr);
702         if(assignment && assignment_target && !indeterminate_target)
703         {
704                 Node *&assign = assignments[assignment_target];
705                 if(self_referencing)
706                         unused_nodes.erase(assign);
707                 else if(assign)
708                         unused_nodes.insert(assign);
709                 assign = &expr;
710                 if(assignment_target->interface=="out" && (stage->type==FRAGMENT || assignment_target->linked_declaration))
711                         unused_nodes.erase(assignment_target);
712                 else
713                         unused_nodes.insert(&expr);
714         }
715         assignment = false;
716 }
717
718 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
719 {
720         SetForScope<Node *> set(aggregate, &strct);
721         unused_nodes.insert(&strct);
722         TraversingVisitor::visit(strct);
723 }
724
725 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
726 {
727         if(aggregate)
728                 aggregates[&var] = aggregate;
729         else
730         {
731                 unused_nodes.insert(&var);
732                 if(var.init_expression)
733                 {
734                         unused_nodes.insert(&*var.init_expression);
735                         assignments[&var] = &*var.init_expression;
736                 }
737         }
738         unused_nodes.erase(var.type_declaration);
739         TraversingVisitor::visit(var);
740 }
741
742 void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
743 {
744         SetForScope<Node *> set(aggregate, &iface);
745         unused_nodes.insert(&iface);
746         TraversingVisitor::visit(iface);
747 }
748
749
750 void ProgramCompiler::NodeRemover::visit(Block &block)
751 {
752         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
753         {
754                 (*i)->visit(*this);
755                 if(to_remove.count(&**i))
756                         block.body.erase(i++);
757                 else
758                         ++i;
759         }
760 }
761
762 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
763 {
764         if(to_remove.count(&var))
765         {
766                 stage->in_variables.erase(var.name);
767                 stage->out_variables.erase(var.name);
768                 if(var.linked_declaration)
769                         var.linked_declaration->linked_declaration = 0;
770         }
771         else if(var.init_expression && to_remove.count(&*var.init_expression))
772                 var.init_expression = 0;
773 }
774
775 } // namespace GL
776 } // namespace Msp