1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
6 #include "programcompiler.h"
14 using namespace ProgramSyntax;
16 ProgramCompiler::ProgramCompiler():
20 void ProgramCompiler::compile(const string &source)
22 module = &parser.parse(source);
26 void ProgramCompiler::compile(IO::Base &io)
28 module = &parser.parse(io);
32 void ProgramCompiler::add_shaders(Program &program)
35 throw invalid_operation("ProgramCompiler::add_shaders");
37 string head = "#version 150\n";
38 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
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)));
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");
54 void ProgramCompiler::process()
56 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
58 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
61 i = module->stages.begin();
67 void ProgramCompiler::generate(Stage &stage)
69 inject_block(stage.content, module->shared.content);
71 apply<VariableResolver>(stage);
72 apply<InterfaceGenerator>(stage);
73 apply<VariableResolver>(stage);
74 apply<VariableRenamer>(stage);
77 bool ProgramCompiler::optimize(Stage &stage)
79 UnusedVariableLocator unused_locator;
80 unused_locator.apply(stage);
83 remover.to_remove = unused_locator.unused_nodes;
86 return remover.n_removed;
89 void ProgramCompiler::inject_block(Block &target, const Block &source)
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());
97 void ProgramCompiler::apply(Stage &stage)
100 visitor.apply(stage);
103 string ProgramCompiler::create_source(Stage &stage)
106 formatter.apply(stage);
107 return formatter.formatted;
111 ProgramCompiler::Visitor::Visitor():
115 void ProgramCompiler::Visitor::apply(Stage &s)
117 SetForScope<Stage *> set(stage, &s);
118 stage->content.visit(*this);
122 ProgramCompiler::Formatter::Formatter():
124 parameter_list(false),
128 void ProgramCompiler::Formatter::visit(Literal &literal)
130 formatted += literal.token;
133 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
136 parexpr.expression->visit(*this);
140 void ProgramCompiler::Formatter::visit(VariableReference &var)
142 formatted += var.name;
145 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
147 memacc.left->visit(*this);
148 formatted += format(".%s", memacc.member);
151 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
154 formatted += unary.oper;
155 unary.expression->visit(*this);
157 formatted += unary.oper;
160 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
162 binary.left->visit(*this);
163 if(binary.assignment)
164 formatted += format(" %s ", binary.oper);
166 formatted += binary.oper;
167 binary.right->visit(*this);
168 formatted += binary.after;
171 void ProgramCompiler::Formatter::visit(FunctionCall &call)
173 formatted += format("%s(", call.name);
174 for(vector<NodePtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
176 if(i!=call.arguments.begin())
183 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
185 expr.expression->visit(*this);
189 void ProgramCompiler::Formatter::visit(Block &block)
198 formatted += format("%s{\n", string(indent*2, ' '));
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)
206 if(i!=block.body.begin())
212 indent -= change_indent;
215 formatted += format("\n%s}", string(indent*2, ' '));
218 void ProgramCompiler::Formatter::visit(Layout &layout)
220 formatted += "layout(";
221 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
223 if(i!=layout.qualifiers.begin())
225 formatted += i->identifier;
226 if(!i->value.empty())
227 formatted += format("=%s", i->value);
229 formatted += format(") %s;", layout.interface);
232 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
234 formatted += format("struct %s\n", strct.name);
235 strct.members.visit(*this);
239 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
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);
252 var.array_size->visit(*this);
255 if(var.init_expression)
258 var.init_expression->visit(*this);
264 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
266 formatted += format("%s %s\n", iface.interface, iface.name);
267 iface.members.visit(*this);
271 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
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)
276 if(i!=func.parameters.begin())
278 SetFlag set(parameter_list);
285 func.body.visit(*this);
291 void ProgramCompiler::Formatter::visit(Conditional &cond)
300 cond.condition->visit(*this);
303 cond.body.visit(*this);
304 if(!cond.else_body.body.empty())
306 formatted += format("\n%selse", string(indent*2, ' '));
307 SetFlag set(else_if);
308 cond.else_body.visit(*this);
312 void ProgramCompiler::Formatter::visit(Iteration &iter)
315 iter.init_statement->visit(*this);
317 iter.condition->visit(*this);
319 iter.loop_expression->visit(*this);
321 iter.body.visit(*this);
324 void ProgramCompiler::Formatter::visit(Return &ret)
326 formatted += "return ";
327 ret.expression->visit(*this);
332 ProgramCompiler::VariableResolver::VariableResolver():
336 void ProgramCompiler::VariableResolver::visit(Block &block)
338 blocks.push_back(&block);
339 block.variables.clear();
340 TraversingVisitor::visit(block);
344 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
348 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
351 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
352 if(j!=(*i)->variables.end())
354 var.declaration = j->second;
355 type = j->second->type_declaration;
361 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
364 TraversingVisitor::visit(memacc);
365 memacc.declaration = 0;
368 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
369 if(i!=type->members.variables.end())
371 memacc.declaration = i->second;
372 type = i->second->type_declaration;
379 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
383 binary.right->visit(*this);
385 binary.left->visit(*this);
389 TraversingVisitor::visit(binary);
394 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
396 TraversingVisitor::visit(strct);
397 blocks.back()->types[strct.name] = &strct;
400 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
402 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
405 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
406 if(j!=(*i)->types.end())
407 var.type_declaration = j->second;
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;
416 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
418 SetFlag set(anonymous);
419 TraversingVisitor::visit(iface);
423 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
428 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
432 else if(type==GEOMETRY)
438 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
440 SetForScope<Stage *> set(stage, &s);
442 in_prefix = get_out_prefix(stage->previous->type);
443 out_prefix = get_out_prefix(stage->type);
444 stage->content.visit(*this);
447 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
449 SetForScope<unsigned> set(scope_level, scope_level+1);
450 for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
456 for(map<string, VariableDeclaration *>::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
458 list<NodePtr<Node> >::iterator k = block.body.insert(i, j->second);
461 iface_declarations.clear();
464 for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
465 block.body.insert(i, *j);
466 insert_nodes.clear();
469 block.body.erase(i++);
476 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
478 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
479 return prefix+name.substr(offset);
482 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
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))
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;
497 iface_var->linked_declaration = &var;
498 iface_declarations[name] = iface_var;
503 void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
505 BinaryExpression *assign = new BinaryExpression;
506 VariableReference *ref = new VariableReference;
510 assign->right = right;
511 assign->assignment = true;
513 ExpressionStatement *stmt = new ExpressionStatement;
514 stmt->expression = assign;
515 insert_nodes.push_back(stmt);
518 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
520 if(var.declaration || !stage->previous)
522 if(iface_declarations.count(var.name))
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);
533 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
535 if(var.interface=="out")
538 stage->out_variables[var.name] = &var;
539 else if(generate_interface(var, "out", change_prefix(var.name, string())))
542 if(var.init_expression)
543 insert_assignment(var.name, var.init_expression->clone());
546 else if(var.interface=="in")
548 stage->in_variables[var.name] = &var;
549 if(var.linked_declaration)
550 var.linked_declaration->linked_declaration = &var;
551 else if(stage->previous)
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())
557 var.linked_declaration = i->second;
558 i->second->linked_declaration = &var;
563 TraversingVisitor::visit(var);
566 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
568 vector<VariableDeclaration *> pass_vars;
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);
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)
582 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
583 linked = ((*j)->linked_declaration==i->second);
585 if(!linked && generate_interface(*i->second, "in", i->second->name))
586 pass_vars.push_back(i->second);
590 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
592 string out_name = change_prefix((*i)->name, out_prefix);
593 generate_interface(**i, "out", out_name);
595 VariableReference *ref = new VariableReference;
596 ref->name = (*i)->name;
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);
607 insert_assignment(out_name, ref);
614 void ProgramCompiler::VariableRenamer::visit(VariableReference &var)
617 var.name = var.declaration->name;
620 void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
622 if(var.linked_declaration)
623 var.name = var.linked_declaration->name;
624 TraversingVisitor::visit(var);
628 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
633 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
636 assignment_target = var.declaration;
639 unused_nodes.erase(var.declaration);
640 map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
641 if(i!=assignments.end())
643 unused_nodes.erase(i->second);
644 assignments.erase(i);
649 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
651 TraversingVisitor::visit(memacc);
652 unused_nodes.erase(memacc.declaration);
655 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
657 if(binary.assignment)
659 binary.right->visit(*this);
661 binary.left->visit(*this);
664 TraversingVisitor::visit(binary);
667 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
670 assignment_target = 0;
671 TraversingVisitor::visit(expr);
672 if(assignment && assignment_target)
674 Node *&assign = assignments[assignment_target];
676 unused_nodes.insert(assign);
678 if(assignment_target->interface!="out" || (stage->type!=FRAGMENT && !assignment_target->linked_declaration))
679 unused_nodes.insert(&expr);
681 unused_nodes.erase(assignment_target);
686 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
688 unused_nodes.insert(&var);
689 TraversingVisitor::visit(var);
693 ProgramCompiler::NodeRemover::NodeRemover():
695 immutable_block(false),
699 void ProgramCompiler::NodeRemover::visit(Block &block)
701 remove_block = immutable_block;
702 for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
704 bool remove = to_remove.count(&**i);
706 remove_block = false;
709 if(remove ? !immutable_block : remove_block)
711 block.body.erase(i++);
719 void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct)
721 SetFlag set(immutable_block);
722 TraversingVisitor::visit(strct);
725 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
727 if(to_remove.count(&var))
729 stage->in_variables.erase(var.name);
730 stage->out_variables.erase(var.name);
731 if(var.linked_declaration)
732 var.linked_declaration->linked_declaration = 0;
736 void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface)
738 SetFlag set(immutable_block);
739 TraversingVisitor::visit(iface);