1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
13 void DeclarationCombiner::apply(Stage &stage)
15 stage.content.visit(*this);
16 NodeRemover().apply(stage, nodes_to_remove);
19 void DeclarationCombiner::visit(Block &block)
24 TraversingVisitor::visit(block);
27 void DeclarationCombiner::visit(VariableDeclaration &var)
29 VariableDeclaration *&ptr = variables[var.name];
33 if(var.init_expression)
34 ptr->init_expression = var.init_expression;
39 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
42 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
45 j->has_value = i->value;
51 ptr->layout->qualifiers.push_back(*i);
55 ptr->layout = var.layout;
57 nodes_to_remove.insert(&var);
64 ConstantSpecializer::ConstantSpecializer():
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
71 stage.content.visit(*this);
74 void ConstantSpecializer::visit(VariableDeclaration &var)
76 bool specializable = false;
79 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
80 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
81 if(i->name=="constant_id")
87 i->value = hash32(var.name)&0x7FFFFFFF;
91 if(qualifiers.empty())
95 if(specializable && values)
97 map<string, int>::const_iterator i = values->find(var.name);
100 RefPtr<Literal> literal = new Literal;
102 literal->token = (i->second ? "true" : "false");
103 else if(var.type=="int")
104 literal->token = lexical_cast<string>(i->second);
105 var.init_expression = literal;
111 void BlockHierarchyResolver::enter(Block &block)
113 block.parent = current_block;
117 VariableResolver::VariableResolver():
121 record_target(false),
122 assignment_target(0),
123 self_referencing(false)
126 void VariableResolver::apply(Stage &s)
130 s.interface_blocks.clear();
131 Stage *builtin_stage = get_builtins(s.type);
132 builtins = (builtin_stage ? &builtin_stage->content : 0);
133 s.content.visit(*this);
136 Block *VariableResolver::next_block(Block &block)
138 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
141 void VariableResolver::enter(Block &block)
143 block.variables.clear();
146 void VariableResolver::visit(VariableReference &var)
150 for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
152 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
153 if(i!=block->variables.end())
154 var.declaration = i->second;
159 if(var.declaration->type_declaration)
160 members = &var.declaration->type_declaration->members.variables;
164 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
165 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
166 if(i!=blocks.end() && i->second->instance_name==var.name)
168 iface_ref = new InterfaceBlockReference;
169 iface_ref->name = var.name;
170 iface_ref->declaration = i->second;
171 members = &i->second->members.variables;
175 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
176 if(i->second->instance_name.empty())
178 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
179 if(j!=i->second->members.variables.end())
180 var.declaration = j->second;
187 if(assignment_target)
189 record_target = false;
190 assignment_target = 0;
193 assignment_target = var.declaration;
195 else if(var.declaration && var.declaration==assignment_target)
196 self_referencing = true;
199 void VariableResolver::visit(InterfaceBlockReference &iface)
201 iface.declaration = 0;
202 for(Block *block=current_block; block; block=next_block(*block))
204 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
205 if(i!=stage->interface_blocks.end())
207 iface.declaration = i->second;
208 members = &i->second->members.variables;
214 void VariableResolver::visit(MemberAccess &memacc)
218 memacc.left->visit(*this);
221 memacc.left = iface_ref;
224 memacc.declaration = 0;
227 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
228 if(i!=members->end())
230 memacc.declaration = i->second;
231 if(i->second->type_declaration)
232 members = &i->second->type_declaration->members.variables;
239 void VariableResolver::visit(BinaryExpression &binary)
244 SetForScope<bool> set(record_target, false);
245 binary.right->visit(*this);
249 binary.left->visit(*this);
251 binary.left = iface_ref;
256 TraversingVisitor::visit(binary);
261 void VariableResolver::visit(Assignment &assign)
264 SetFlag set(record_target);
265 assignment_target = 0;
266 assign.left->visit(*this);
269 self_referencing = false;
270 assign.right->visit(*this);
272 assign.self_referencing = (self_referencing || assign.oper!="=");
273 assign.target_declaration = assignment_target;
276 void VariableResolver::visit(StructDeclaration &strct)
278 TraversingVisitor::visit(strct);
279 stage->types[strct.name] = &strct;
282 void VariableResolver::visit(VariableDeclaration &var)
284 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
285 if(i!=stage->types.end())
286 var.type_declaration = i->second;
288 if(!block_interface.empty() && var.interface.empty())
289 var.interface = block_interface;
291 TraversingVisitor::visit(var);
292 current_block->variables[var.name] = &var;
295 void VariableResolver::visit(InterfaceBlock &iface)
297 /* Block names can't be used for any other identifiers so we can put them
298 in the same map with instance names. */
299 stage->interface_blocks[iface.name] = &iface;
300 if(!iface.instance_name.empty())
301 stage->interface_blocks[iface.instance_name] = &iface;
303 SetForScope<string> set_iface(block_interface, iface.interface);
304 TraversingVisitor::visit(iface);
308 void FunctionResolver::apply(Stage &s)
312 s.content.visit(*this);
315 void FunctionResolver::visit(FunctionCall &call)
317 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
318 if(i!=stage->functions.end())
319 call.declaration = i->second;
321 TraversingVisitor::visit(call);
324 void FunctionResolver::visit(FunctionDeclaration &func)
326 FunctionDeclaration *&stage_decl = stage->functions[func.name];
327 vector<FunctionDeclaration *> &decls = declarations[func.name];
328 if(func.definition==&func)
332 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
334 (*i)->definition = func.definition;
335 (*i)->body.body.clear();
343 func.definition = stage_decl->definition;
345 decls.push_back(&func);
347 TraversingVisitor::visit(func);
351 InterfaceGenerator::InterfaceGenerator():
353 function_scope(false),
356 iface_target_block(0)
359 string InterfaceGenerator::get_out_prefix(Stage::Type type)
361 if(type==Stage::VERTEX)
363 else if(type==Stage::GEOMETRY)
369 void InterfaceGenerator::apply(Stage &s)
372 iface_target_block = &stage->content;
374 in_prefix = get_out_prefix(stage->previous->type);
375 out_prefix = get_out_prefix(stage->type);
376 s.content.visit(*this);
377 NodeRemover().apply(s, nodes_to_remove);
380 void InterfaceGenerator::visit(Block &block)
382 SetForScope<Block *> set_block(current_block, &block);
383 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
385 assignment_insert_point = i;
386 if(&block==&stage->content)
387 iface_insert_point = i;
393 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
395 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
396 return prefix+name.substr(offset);
399 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
401 if(stage->content.variables.count(name))
404 VariableDeclaration* iface_var = new VariableDeclaration;
405 iface_var->sampling = var.sampling;
406 iface_var->interface = iface;
407 iface_var->type = var.type;
408 iface_var->type_declaration = var.type_declaration;
409 iface_var->name = name;
410 if(stage->type==Stage::GEOMETRY && !copy_block)
411 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
413 iface_var->array = var.array;
415 iface_var->array_size = var.array_size;
418 iface_var->layout = var.layout;
419 iface_var->linked_declaration = &var;
420 var.linked_declaration = iface_var;
423 iface_target_block->body.insert(iface_insert_point, iface_var);
424 iface_target_block->variables[name] = iface_var;
429 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
431 if(stage->interface_blocks.count(out_block.name))
434 InterfaceBlock *in_block = new InterfaceBlock;
435 in_block->interface = "in";
436 in_block->name = out_block.name;
437 in_block->instance_name = out_block.instance_name;
438 if(stage->type==Stage::GEOMETRY)
439 in_block->array = true;
441 in_block->array = out_block.array;
442 in_block->linked_block = &out_block;
443 out_block.linked_block = in_block;
446 SetFlag set_copy(copy_block, true);
447 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
448 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
449 out_block.members.visit(*this);
452 iface_target_block->body.insert(iface_insert_point, in_block);
453 stage->interface_blocks[in_block->name] = in_block;
454 if(!in_block->instance_name.empty())
455 stage->interface_blocks[in_block->instance_name] = in_block;
457 SetFlag set_scope(function_scope, false);
458 SetForScope<Block *> set_block(current_block, &stage->content);
459 in_block->visit(*this);
464 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
466 Assignment *assign = new Assignment;
467 VariableReference *ref = new VariableReference;
471 assign->right = right;
473 ExpressionStatement *stmt = new ExpressionStatement;
474 stmt->expression = assign;
475 current_block->body.insert(assignment_insert_point, stmt);
481 void InterfaceGenerator::visit(VariableReference &var)
483 if(var.declaration || !stage->previous)
485 /* Don't pull a variable from previous stage if we just generated an out
486 interface in this stage */
487 if(stage->content.variables.count(var.name))
490 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
491 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
492 if(i==prev_vars.end() || i->second->interface!="out")
493 i = prev_vars.find(in_prefix+var.name);
494 if(i!=prev_vars.end() && i->second->interface=="out")
496 generate_interface(*i->second, "in", i->second->name);
497 var.name = i->second->name;
501 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
502 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
503 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
505 generate_interface(*j->second);
509 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
510 if(j->second->instance_name.empty())
512 i = j->second->members.variables.find(var.name);
513 if(i!=j->second->members.variables.end())
515 generate_interface(*j->second);
521 void InterfaceGenerator::visit(VariableDeclaration &var)
525 generate_interface(var, "in", var.name);
531 if(iface_block->linked_block)
533 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
534 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
535 if(i!=linked_vars.end())
536 var.linked_declaration = i->second;
541 if(var.interface=="out")
543 /* For out variables in function scope, generate a global interface and
544 replace the local declaration with an assignment. */
545 if(function_scope && generate_interface(var, "out", var.name))
547 nodes_to_remove.insert(&var);
548 if(var.init_expression)
550 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
551 stmt.source = var.source;
552 stmt.line = var.line;
557 else if(var.interface=="in")
559 /* Try to link in variables in global scope with out variables from
561 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
563 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
564 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
565 if(i!=prev_vars.end() && i->second->interface=="out")
567 var.linked_declaration = i->second;
568 i->second->linked_declaration = &var;
573 TraversingVisitor::visit(var);
576 void InterfaceGenerator::visit(InterfaceBlock &iface)
578 if(iface.interface=="in")
580 if(!iface.linked_block && stage->previous)
582 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
583 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
584 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
586 iface.linked_block = i->second;
587 i->second->linked_block = &iface;
592 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
593 TraversingVisitor::visit(iface);
596 void InterfaceGenerator::visit(FunctionDeclaration &func)
598 SetFlag set_scope(function_scope, true);
599 // Skip parameters because they're not useful here
600 func.body.visit(*this);
603 void InterfaceGenerator::visit(Passthrough &pass)
605 vector<VariableDeclaration *> pass_vars;
607 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
608 if(i->second->interface=="in")
609 pass_vars.push_back(i->second);
613 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
614 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
617 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
618 linked = ((*j)->linked_declaration==i->second);
620 if(!linked && generate_interface(*i->second, "in", i->second->name))
621 pass_vars.push_back(i->second);
625 if(stage->type==Stage::GEOMETRY)
627 InterfaceBlockReference *ref = new InterfaceBlockReference;
630 BinaryExpression *subscript = new BinaryExpression;
631 subscript->left = ref;
632 subscript->oper = "[";
633 subscript->right = pass.subscript;
634 subscript->after = "]";
636 MemberAccess *memacc = new MemberAccess;
637 memacc->left = subscript;
638 memacc->member = "gl_Position";
640 insert_assignment("gl_Position", memacc);
643 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
645 string out_name = change_prefix((*i)->name, out_prefix);
646 generate_interface(**i, "out", out_name);
648 VariableReference *ref = new VariableReference;
649 ref->name = (*i)->name;
652 BinaryExpression *subscript = new BinaryExpression;
653 subscript->left = ref;
654 subscript->oper = "[";
655 subscript->right = pass.subscript;
656 subscript->after = "]";
657 insert_assignment(out_name, subscript);
660 insert_assignment(out_name, ref);
663 nodes_to_remove.insert(&pass);
667 DeclarationReorderer::DeclarationReorderer():
671 void DeclarationReorderer::visit(FunctionCall &call)
673 FunctionDeclaration *def = call.declaration;
675 def = def->definition;
676 if(def && !ordered_funcs.count(def))
677 needed_funcs.insert(def);
680 void DeclarationReorderer::visit(Block &block)
683 return TraversingVisitor::visit(block);
685 NodeList<Statement>::iterator struct_insert_point = block.body.end();
686 NodeList<Statement>::iterator variable_insert_point = block.body.end();
687 NodeList<Statement>::iterator function_insert_point = block.body.end();
688 unsigned unordered_func_count = 0;
689 bool ordered_any_funcs = false;
691 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
693 kind = NO_DECLARATION;
697 if(kind==STRUCT && struct_insert_point!=block.body.end())
699 block.body.insert(struct_insert_point, *i);
702 else if(kind>STRUCT && struct_insert_point==block.body.end())
703 struct_insert_point = i;
705 if(kind==VARIABLE && variable_insert_point!=block.body.end())
707 block.body.insert(variable_insert_point, *i);
710 else if(kind>VARIABLE && variable_insert_point==block.body.end())
711 variable_insert_point = i;
715 if(function_insert_point==block.body.end())
716 function_insert_point = i;
718 if(needed_funcs.empty())
720 ordered_funcs.insert(i->get());
721 if(i!=function_insert_point)
723 block.body.insert(function_insert_point, *i);
727 ++function_insert_point;
728 ordered_any_funcs = true;
731 ++unordered_func_count;
736 if(function_insert_point==i)
737 ++function_insert_point;
738 block.body.erase(i++);
743 if(i==block.body.end() && unordered_func_count)
745 if(!ordered_any_funcs)
746 // A subset of the remaining functions forms a recursive loop
747 /* TODO pick a function and move it up, adding any necessary
751 i = function_insert_point;
752 unordered_func_count = 0;
757 void DeclarationReorderer::visit(VariableDeclaration &var)
759 TraversingVisitor::visit(var);
763 void DeclarationReorderer::visit(FunctionDeclaration &func)
765 needed_funcs.clear();
766 func.body.visit(*this);
767 needed_funcs.erase(&func);