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():
120 record_target(false),
121 assignment_target(0),
122 self_referencing(false)
125 void VariableResolver::apply(Stage &s)
129 s.interface_blocks.clear();
130 s.content.visit(*this);
133 void VariableResolver::enter(Block &block)
135 block.variables.clear();
138 void VariableResolver::visit(VariableReference &var)
142 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
144 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
145 if(i!=block->variables.end())
146 var.declaration = i->second;
151 if(var.declaration->type_declaration)
152 members = &var.declaration->type_declaration->members.variables;
156 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
157 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
158 if(i!=blocks.end() && i->second->instance_name==var.name)
160 iface_ref = new InterfaceBlockReference;
161 iface_ref->name = var.name;
162 iface_ref->declaration = i->second;
163 members = &i->second->members.variables;
167 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
168 if(i->second->instance_name.empty())
170 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
171 if(j!=i->second->members.variables.end())
172 var.declaration = j->second;
179 if(assignment_target)
181 record_target = false;
182 assignment_target = 0;
185 assignment_target = var.declaration;
187 else if(var.declaration && var.declaration==assignment_target)
188 self_referencing = true;
191 void VariableResolver::visit(InterfaceBlockReference &iface)
193 iface.declaration = 0;
194 for(Block *block=current_block; block; block=block->parent)
196 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
197 if(i!=stage->interface_blocks.end())
199 iface.declaration = i->second;
200 members = &i->second->members.variables;
206 void VariableResolver::visit(MemberAccess &memacc)
210 memacc.left->visit(*this);
213 memacc.left = iface_ref;
216 memacc.declaration = 0;
219 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
220 if(i!=members->end())
222 memacc.declaration = i->second;
223 if(i->second->type_declaration)
224 members = &i->second->type_declaration->members.variables;
231 void VariableResolver::visit(BinaryExpression &binary)
236 SetForScope<bool> set(record_target, false);
237 binary.right->visit(*this);
241 binary.left->visit(*this);
243 binary.left = iface_ref;
248 TraversingVisitor::visit(binary);
253 void VariableResolver::visit(Assignment &assign)
256 SetFlag set(record_target);
257 assignment_target = 0;
258 assign.left->visit(*this);
261 self_referencing = false;
262 assign.right->visit(*this);
264 assign.self_referencing = (self_referencing || assign.oper!="=");
265 assign.target_declaration = assignment_target;
268 void VariableResolver::visit(StructDeclaration &strct)
270 TraversingVisitor::visit(strct);
271 stage->types[strct.name] = &strct;
274 void VariableResolver::visit(VariableDeclaration &var)
276 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
277 if(i!=stage->types.end())
278 var.type_declaration = i->second;
280 if(!block_interface.empty() && var.interface.empty())
281 var.interface = block_interface;
283 TraversingVisitor::visit(var);
284 current_block->variables[var.name] = &var;
287 void VariableResolver::visit(InterfaceBlock &iface)
289 /* Block names can't be used for any other identifiers so we can put them
290 in the same map with instance names. */
291 stage->interface_blocks[iface.name] = &iface;
292 if(!iface.instance_name.empty())
293 stage->interface_blocks[iface.instance_name] = &iface;
295 SetForScope<string> set_iface(block_interface, iface.interface);
296 TraversingVisitor::visit(iface);
300 void FunctionResolver::apply(Stage &s)
304 s.content.visit(*this);
307 void FunctionResolver::visit(FunctionCall &call)
309 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
310 if(i!=stage->functions.end())
311 call.declaration = i->second;
313 TraversingVisitor::visit(call);
316 void FunctionResolver::visit(FunctionDeclaration &func)
318 FunctionDeclaration *&stage_decl = stage->functions[func.name];
319 vector<FunctionDeclaration *> &decls = declarations[func.name];
320 if(func.definition==&func)
324 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
326 (*i)->definition = func.definition;
327 (*i)->body.body.clear();
335 func.definition = stage_decl->definition;
337 decls.push_back(&func);
339 TraversingVisitor::visit(func);
343 InterfaceGenerator::InterfaceGenerator():
345 function_scope(false),
348 iface_target_block(0)
351 string InterfaceGenerator::get_out_prefix(Stage::Type type)
353 if(type==Stage::VERTEX)
355 else if(type==Stage::GEOMETRY)
361 void InterfaceGenerator::apply(Stage &s)
364 iface_target_block = &stage->content;
366 in_prefix = get_out_prefix(stage->previous->type);
367 out_prefix = get_out_prefix(stage->type);
368 s.content.visit(*this);
369 NodeRemover().apply(s, nodes_to_remove);
372 void InterfaceGenerator::visit(Block &block)
374 SetForScope<Block *> set_block(current_block, &block);
375 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
377 assignment_insert_point = i;
378 if(&block==&stage->content)
379 iface_insert_point = i;
385 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
387 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
388 return prefix+name.substr(offset);
391 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
393 if(stage->content.variables.count(name))
396 VariableDeclaration* iface_var = new VariableDeclaration;
397 iface_var->sampling = var.sampling;
398 iface_var->interface = iface;
399 iface_var->type = var.type;
400 iface_var->type_declaration = var.type_declaration;
401 iface_var->name = name;
402 if(stage->type==Stage::GEOMETRY && !copy_block)
403 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
405 iface_var->array = var.array;
407 iface_var->array_size = var.array_size;
410 iface_var->layout = var.layout;
411 iface_var->linked_declaration = &var;
412 var.linked_declaration = iface_var;
415 iface_target_block->body.insert(iface_insert_point, iface_var);
416 iface_target_block->variables[name] = iface_var;
421 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
423 if(stage->interface_blocks.count(out_block.name))
426 InterfaceBlock *in_block = new InterfaceBlock;
427 in_block->interface = "in";
428 in_block->name = out_block.name;
429 in_block->instance_name = out_block.instance_name;
430 if(stage->type==Stage::GEOMETRY)
431 in_block->array = true;
433 in_block->array = out_block.array;
434 in_block->linked_block = &out_block;
435 out_block.linked_block = in_block;
438 SetFlag set_copy(copy_block, true);
439 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
440 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
441 out_block.members.visit(*this);
444 iface_target_block->body.insert(iface_insert_point, in_block);
445 stage->interface_blocks[in_block->name] = in_block;
446 if(!in_block->instance_name.empty())
447 stage->interface_blocks[in_block->instance_name] = in_block;
449 SetFlag set_scope(function_scope, false);
450 SetForScope<Block *> set_block(current_block, &stage->content);
451 in_block->visit(*this);
456 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
458 Assignment *assign = new Assignment;
459 VariableReference *ref = new VariableReference;
463 assign->right = right;
465 ExpressionStatement *stmt = new ExpressionStatement;
466 stmt->expression = assign;
467 current_block->body.insert(assignment_insert_point, stmt);
473 void InterfaceGenerator::visit(VariableReference &var)
475 if(var.declaration || !stage->previous)
477 /* Don't pull a variable from previous stage if we just generated an out
478 interface in this stage */
479 if(stage->content.variables.count(var.name))
482 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
483 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
484 if(i==prev_vars.end() || i->second->interface!="out")
485 i = prev_vars.find(in_prefix+var.name);
486 if(i!=prev_vars.end() && i->second->interface=="out")
488 generate_interface(*i->second, "in", i->second->name);
489 var.name = i->second->name;
493 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
494 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
495 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
497 generate_interface(*j->second);
501 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
502 if(j->second->instance_name.empty())
504 i = j->second->members.variables.find(var.name);
505 if(i!=j->second->members.variables.end())
507 generate_interface(*j->second);
513 void InterfaceGenerator::visit(VariableDeclaration &var)
517 generate_interface(var, "in", var.name);
523 if(iface_block->linked_block)
525 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
526 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
527 if(i!=linked_vars.end())
528 var.linked_declaration = i->second;
533 if(var.interface=="out")
535 /* For out variables in function scope, generate a global interface and
536 replace the local declaration with an assignment. */
537 if(function_scope && generate_interface(var, "out", var.name))
539 nodes_to_remove.insert(&var);
540 if(var.init_expression)
542 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
543 stmt.source = var.source;
544 stmt.line = var.line;
549 else if(var.interface=="in")
551 /* Try to link in variables in global scope with out variables from
553 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
555 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
556 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
557 if(i!=prev_vars.end() && i->second->interface=="out")
559 var.linked_declaration = i->second;
560 i->second->linked_declaration = &var;
565 TraversingVisitor::visit(var);
568 void InterfaceGenerator::visit(InterfaceBlock &iface)
570 if(iface.interface=="in")
572 if(!iface.linked_block && stage->previous)
574 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
575 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
576 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
578 iface.linked_block = i->second;
579 i->second->linked_block = &iface;
584 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
585 TraversingVisitor::visit(iface);
588 void InterfaceGenerator::visit(FunctionDeclaration &func)
590 SetFlag set_scope(function_scope, true);
591 // Skip parameters because they're not useful here
592 func.body.visit(*this);
595 void InterfaceGenerator::visit(Passthrough &pass)
597 vector<VariableDeclaration *> pass_vars;
599 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
600 if(i->second->interface=="in")
601 pass_vars.push_back(i->second);
605 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
606 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
609 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
610 linked = ((*j)->linked_declaration==i->second);
612 if(!linked && generate_interface(*i->second, "in", i->second->name))
613 pass_vars.push_back(i->second);
617 if(stage->type==Stage::GEOMETRY)
619 InterfaceBlockReference *ref = new InterfaceBlockReference;
622 BinaryExpression *subscript = new BinaryExpression;
623 subscript->left = ref;
624 subscript->oper = "[";
625 subscript->right = pass.subscript;
626 subscript->after = "]";
628 MemberAccess *memacc = new MemberAccess;
629 memacc->left = subscript;
630 memacc->member = "gl_Position";
632 insert_assignment("gl_Position", memacc);
635 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
637 string out_name = change_prefix((*i)->name, out_prefix);
638 generate_interface(**i, "out", out_name);
640 VariableReference *ref = new VariableReference;
641 ref->name = (*i)->name;
644 BinaryExpression *subscript = new BinaryExpression;
645 subscript->left = ref;
646 subscript->oper = "[";
647 subscript->right = pass.subscript;
648 subscript->after = "]";
649 insert_assignment(out_name, subscript);
652 insert_assignment(out_name, ref);
655 nodes_to_remove.insert(&pass);
659 DeclarationReorderer::DeclarationReorderer():
663 void DeclarationReorderer::visit(FunctionCall &call)
665 FunctionDeclaration *def = call.declaration;
667 def = def->definition;
668 if(def && !ordered_funcs.count(def))
669 needed_funcs.insert(def);
672 void DeclarationReorderer::visit(Block &block)
675 return TraversingVisitor::visit(block);
677 NodeList<Statement>::iterator struct_insert_point = block.body.end();
678 NodeList<Statement>::iterator variable_insert_point = block.body.end();
679 NodeList<Statement>::iterator function_insert_point = block.body.end();
680 unsigned unordered_func_count = 0;
681 bool ordered_any_funcs = false;
683 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
685 kind = NO_DECLARATION;
689 if(kind==STRUCT && struct_insert_point!=block.body.end())
691 block.body.insert(struct_insert_point, *i);
694 else if(kind>STRUCT && struct_insert_point==block.body.end())
695 struct_insert_point = i;
697 if(kind==VARIABLE && variable_insert_point!=block.body.end())
699 block.body.insert(variable_insert_point, *i);
702 else if(kind>VARIABLE && variable_insert_point==block.body.end())
703 variable_insert_point = i;
707 if(function_insert_point==block.body.end())
708 function_insert_point = i;
710 if(needed_funcs.empty())
712 ordered_funcs.insert(i->get());
713 if(i!=function_insert_point)
715 block.body.insert(function_insert_point, *i);
719 ++function_insert_point;
720 ordered_any_funcs = true;
723 ++unordered_func_count;
728 if(function_insert_point==i)
729 ++function_insert_point;
730 block.body.erase(i++);
735 if(i==block.body.end() && unordered_func_count)
737 if(!ordered_any_funcs)
738 // A subset of the remaining functions forms a recursive loop
739 /* TODO pick a function and move it up, adding any necessary
743 i = function_insert_point;
744 unordered_func_count = 0;
749 void DeclarationReorderer::visit(VariableDeclaration &var)
751 TraversingVisitor::visit(var);
755 void DeclarationReorderer::visit(FunctionDeclaration &func)
757 needed_funcs.clear();
758 func.body.visit(*this);
759 needed_funcs.erase(&func);