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;
103 literal->token = (i->second ? "true" : "false");
104 literal->value = static_cast<bool>(i->second);
106 else if(var.type=="int")
108 literal->token = lexical_cast<string>(i->second);
109 literal->value = i->second;
111 var.init_expression = literal;
117 void BlockHierarchyResolver::enter(Block &block)
119 block.parent = current_block;
123 VariableResolver::VariableResolver():
127 record_target(false),
128 assignment_target(0),
129 self_referencing(false)
132 void VariableResolver::apply(Stage &s)
135 Stage *builtin_stage = get_builtins(s.type);
136 builtins = (builtin_stage ? &builtin_stage->content : 0);
137 s.content.visit(*this);
140 Block *VariableResolver::next_block(Block &block)
142 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
145 void VariableResolver::enter(Block &block)
147 block.variables.clear();
150 void VariableResolver::visit(VariableReference &var)
154 for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
156 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
157 if(i!=block->variables.end())
158 var.declaration = i->second;
163 if(var.declaration->type_declaration)
164 members = &var.declaration->type_declaration->members.variables;
168 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
169 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
170 if(i!=blocks.end() && i->second->instance_name==var.name)
172 iface_ref = new InterfaceBlockReference;
173 iface_ref->name = var.name;
174 iface_ref->declaration = i->second;
175 members = &i->second->members.variables;
179 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
180 if(i->second->instance_name.empty())
182 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
183 if(j!=i->second->members.variables.end())
184 var.declaration = j->second;
191 if(assignment_target)
193 record_target = false;
194 assignment_target = 0;
197 assignment_target = var.declaration;
199 else if(var.declaration && var.declaration==assignment_target)
200 self_referencing = true;
203 void VariableResolver::visit(InterfaceBlockReference &iface)
205 iface.declaration = 0;
206 for(Block *block=current_block; block; block=next_block(*block))
208 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
209 if(i!=stage->interface_blocks.end())
211 iface.declaration = i->second;
212 members = &i->second->members.variables;
218 void VariableResolver::visit(MemberAccess &memacc)
222 memacc.left->visit(*this);
225 memacc.left = iface_ref;
228 memacc.declaration = 0;
231 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
232 if(i!=members->end())
234 memacc.declaration = i->second;
235 if(i->second->type_declaration)
236 members = &i->second->type_declaration->members.variables;
243 void VariableResolver::visit(BinaryExpression &binary)
248 SetForScope<bool> set(record_target, false);
249 binary.right->visit(*this);
253 binary.left->visit(*this);
255 binary.left = iface_ref;
260 TraversingVisitor::visit(binary);
265 void VariableResolver::visit(Assignment &assign)
268 SetFlag set(record_target);
269 assignment_target = 0;
270 assign.left->visit(*this);
273 self_referencing = false;
274 assign.right->visit(*this);
276 assign.self_referencing = (self_referencing || assign.oper!="=");
277 assign.target_declaration = assignment_target;
280 void VariableResolver::visit(StructDeclaration &strct)
282 TraversingVisitor::visit(strct);
283 stage->types[strct.name] = &strct;
286 void VariableResolver::visit(VariableDeclaration &var)
288 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
289 if(i!=stage->types.end())
290 var.type_declaration = i->second;
292 if(!block_interface.empty() && var.interface.empty())
293 var.interface = block_interface;
295 TraversingVisitor::visit(var);
296 current_block->variables[var.name] = &var;
299 void VariableResolver::visit(InterfaceBlock &iface)
301 /* Block names can't be used for any other identifiers so we can put them
302 in the same map with instance names. */
303 stage->interface_blocks[iface.name] = &iface;
304 if(!iface.instance_name.empty())
305 stage->interface_blocks[iface.instance_name] = &iface;
307 SetForScope<string> set_iface(block_interface, iface.interface);
308 TraversingVisitor::visit(iface);
312 void FunctionResolver::visit(FunctionCall &call)
314 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
315 if(i!=functions.end())
316 call.declaration = i->second.back();
318 TraversingVisitor::visit(call);
321 void FunctionResolver::visit(FunctionDeclaration &func)
323 vector<FunctionDeclaration *> &decls = functions[func.name];
324 if(func.definition==&func)
326 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
328 (*i)->definition = func.definition;
329 (*i)->body.body.clear();
332 else if(!decls.empty() && decls.back()->definition)
333 func.definition = decls.back()->definition;
334 decls.push_back(&func);
336 TraversingVisitor::visit(func);
340 InterfaceGenerator::InterfaceGenerator():
342 function_scope(false),
345 iface_target_block(0)
348 string InterfaceGenerator::get_out_prefix(Stage::Type type)
350 if(type==Stage::VERTEX)
352 else if(type==Stage::GEOMETRY)
358 void InterfaceGenerator::apply(Stage &s)
361 iface_target_block = &stage->content;
363 in_prefix = get_out_prefix(stage->previous->type);
364 out_prefix = get_out_prefix(stage->type);
365 s.content.visit(*this);
366 NodeRemover().apply(s, nodes_to_remove);
369 void InterfaceGenerator::visit(Block &block)
371 SetForScope<Block *> set_block(current_block, &block);
372 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
374 assignment_insert_point = i;
375 if(&block==&stage->content)
376 iface_insert_point = i;
382 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
384 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
385 return prefix+name.substr(offset);
388 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
390 if(stage->content.variables.count(name))
393 VariableDeclaration* iface_var = new VariableDeclaration;
394 iface_var->sampling = var.sampling;
395 iface_var->interface = iface;
396 iface_var->type = var.type;
397 iface_var->type_declaration = var.type_declaration;
398 iface_var->name = name;
399 if(stage->type==Stage::GEOMETRY && !copy_block)
400 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
402 iface_var->array = var.array;
404 iface_var->array_size = var.array_size;
407 iface_var->layout = var.layout;
408 iface_var->linked_declaration = &var;
409 var.linked_declaration = iface_var;
412 iface_target_block->body.insert(iface_insert_point, iface_var);
413 iface_target_block->variables[name] = iface_var;
418 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
420 if(stage->interface_blocks.count(out_block.name))
423 InterfaceBlock *in_block = new InterfaceBlock;
424 in_block->interface = "in";
425 in_block->name = out_block.name;
426 in_block->instance_name = out_block.instance_name;
427 if(stage->type==Stage::GEOMETRY)
428 in_block->array = true;
430 in_block->array = out_block.array;
431 in_block->linked_block = &out_block;
432 out_block.linked_block = in_block;
435 SetFlag set_copy(copy_block, true);
436 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
437 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
438 out_block.members.visit(*this);
441 iface_target_block->body.insert(iface_insert_point, in_block);
442 stage->interface_blocks[in_block->name] = in_block;
443 if(!in_block->instance_name.empty())
444 stage->interface_blocks[in_block->instance_name] = in_block;
446 SetFlag set_scope(function_scope, false);
447 SetForScope<Block *> set_block(current_block, &stage->content);
448 in_block->visit(*this);
453 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
455 Assignment *assign = new Assignment;
456 VariableReference *ref = new VariableReference;
460 assign->right = right;
462 ExpressionStatement *stmt = new ExpressionStatement;
463 stmt->expression = assign;
464 current_block->body.insert(assignment_insert_point, stmt);
470 void InterfaceGenerator::visit(VariableReference &var)
472 if(var.declaration || !stage->previous)
474 /* Don't pull a variable from previous stage if we just generated an out
475 interface in this stage */
476 if(stage->content.variables.count(var.name))
479 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
480 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
481 if(i==prev_vars.end() || i->second->interface!="out")
482 i = prev_vars.find(in_prefix+var.name);
483 if(i!=prev_vars.end() && i->second->interface=="out")
485 generate_interface(*i->second, "in", i->second->name);
486 var.name = i->second->name;
490 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
491 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
492 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
494 generate_interface(*j->second);
498 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
499 if(j->second->instance_name.empty())
501 i = j->second->members.variables.find(var.name);
502 if(i!=j->second->members.variables.end())
504 generate_interface(*j->second);
510 void InterfaceGenerator::visit(VariableDeclaration &var)
514 generate_interface(var, "in", var.name);
520 if(iface_block->linked_block)
522 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
523 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
524 if(i!=linked_vars.end())
525 var.linked_declaration = i->second;
530 if(var.interface=="out")
532 /* For out variables in function scope, generate a global interface and
533 replace the local declaration with an assignment. */
534 if(function_scope && generate_interface(var, "out", var.name))
536 nodes_to_remove.insert(&var);
537 if(var.init_expression)
539 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
540 stmt.source = var.source;
541 stmt.line = var.line;
546 else if(var.interface=="in")
548 /* Try to link in variables in global scope with out variables from
550 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
552 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
553 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
554 if(i!=prev_vars.end() && i->second->interface=="out")
556 var.linked_declaration = i->second;
557 i->second->linked_declaration = &var;
562 TraversingVisitor::visit(var);
565 void InterfaceGenerator::visit(InterfaceBlock &iface)
567 if(iface.interface=="in")
569 if(!iface.linked_block && stage->previous)
571 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
572 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
573 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
575 iface.linked_block = i->second;
576 i->second->linked_block = &iface;
581 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
582 TraversingVisitor::visit(iface);
585 void InterfaceGenerator::visit(FunctionDeclaration &func)
587 SetFlag set_scope(function_scope, true);
588 // Skip parameters because they're not useful here
589 func.body.visit(*this);
592 void InterfaceGenerator::visit(Passthrough &pass)
594 vector<VariableDeclaration *> pass_vars;
596 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
597 if(i->second->interface=="in")
598 pass_vars.push_back(i->second);
602 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
603 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
606 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
607 linked = ((*j)->linked_declaration==i->second);
609 if(!linked && generate_interface(*i->second, "in", i->second->name))
610 pass_vars.push_back(i->second);
614 if(stage->type==Stage::GEOMETRY)
616 InterfaceBlockReference *ref = new InterfaceBlockReference;
619 BinaryExpression *subscript = new BinaryExpression;
620 subscript->left = ref;
621 subscript->oper = "[";
622 subscript->right = pass.subscript;
623 subscript->after = "]";
625 MemberAccess *memacc = new MemberAccess;
626 memacc->left = subscript;
627 memacc->member = "gl_Position";
629 insert_assignment("gl_Position", memacc);
632 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
634 string out_name = change_prefix((*i)->name, out_prefix);
635 generate_interface(**i, "out", out_name);
637 VariableReference *ref = new VariableReference;
638 ref->name = (*i)->name;
641 BinaryExpression *subscript = new BinaryExpression;
642 subscript->left = ref;
643 subscript->oper = "[";
644 subscript->right = pass.subscript;
645 subscript->after = "]";
646 insert_assignment(out_name, subscript);
649 insert_assignment(out_name, ref);
652 nodes_to_remove.insert(&pass);
656 DeclarationReorderer::DeclarationReorderer():
660 void DeclarationReorderer::visit(FunctionCall &call)
662 FunctionDeclaration *def = call.declaration;
664 def = def->definition;
665 if(def && !ordered_funcs.count(def))
666 needed_funcs.insert(def);
669 void DeclarationReorderer::visit(Block &block)
672 return TraversingVisitor::visit(block);
674 NodeList<Statement>::iterator struct_insert_point = block.body.end();
675 NodeList<Statement>::iterator variable_insert_point = block.body.end();
676 NodeList<Statement>::iterator function_insert_point = block.body.end();
677 unsigned unordered_func_count = 0;
678 bool ordered_any_funcs = false;
680 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
682 kind = NO_DECLARATION;
686 if(kind==STRUCT && struct_insert_point!=block.body.end())
688 block.body.insert(struct_insert_point, *i);
691 else if(kind>STRUCT && struct_insert_point==block.body.end())
692 struct_insert_point = i;
694 if(kind==VARIABLE && variable_insert_point!=block.body.end())
696 block.body.insert(variable_insert_point, *i);
699 else if(kind>VARIABLE && variable_insert_point==block.body.end())
700 variable_insert_point = i;
704 if(function_insert_point==block.body.end())
705 function_insert_point = i;
707 if(needed_funcs.empty())
709 ordered_funcs.insert(i->get());
710 if(i!=function_insert_point)
712 block.body.insert(function_insert_point, *i);
716 ++function_insert_point;
717 ordered_any_funcs = true;
720 ++unordered_func_count;
725 if(function_insert_point==i)
726 ++function_insert_point;
727 block.body.erase(i++);
732 if(i==block.body.end() && unordered_func_count)
734 if(!ordered_any_funcs)
735 // A subset of the remaining functions forms a recursive loop
736 /* TODO pick a function and move it up, adding any necessary
740 i = function_insert_point;
741 unordered_func_count = 0;
746 void DeclarationReorderer::visit(VariableDeclaration &var)
748 TraversingVisitor::visit(var);
752 void DeclarationReorderer::visit(FunctionDeclaration &func)
754 needed_funcs.clear();
755 func.body.visit(*this);
756 needed_funcs.erase(&func);