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(FunctionDeclaration &func)
29 vector<FunctionDeclaration *> &decls = functions[func.name];
32 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
34 (*i)->definition = func.definition;
35 (*i)->body.body.clear();
38 decls.push_back(&func);
41 void DeclarationCombiner::visit(VariableDeclaration &var)
43 VariableDeclaration *&ptr = variables[var.name];
47 if(var.init_expression)
48 ptr->init_expression = var.init_expression;
53 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
56 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
59 j->has_value = i->value;
65 ptr->layout->qualifiers.push_back(*i);
69 ptr->layout = var.layout;
71 nodes_to_remove.insert(&var);
78 ConstantSpecializer::ConstantSpecializer():
82 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
85 stage.content.visit(*this);
88 void ConstantSpecializer::visit(VariableDeclaration &var)
90 bool specializable = false;
93 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
94 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
95 if(i->name=="constant_id")
100 else if(i->value==-1)
101 i->value = hash32(var.name)&0x7FFFFFFF;
105 if(qualifiers.empty())
109 if(specializable && values)
111 map<string, int>::const_iterator i = values->find(var.name);
114 RefPtr<Literal> literal = new Literal;
117 literal->token = (i->second ? "true" : "false");
118 literal->value = static_cast<bool>(i->second);
120 else if(var.type=="int")
122 literal->token = lexical_cast<string>(i->second);
123 literal->value = i->second;
125 var.init_expression = literal;
131 void BlockResolver::enter(Block &block)
133 block.parent = current_block;
137 VariableResolver::VariableResolver():
141 record_target(false),
142 assignment_target(0),
143 self_referencing(false)
146 void VariableResolver::apply(Stage &s)
149 Stage *builtin_stage = get_builtins(s.type);
150 builtins = (builtin_stage ? &builtin_stage->content : 0);
151 s.content.visit(*this);
154 Block *VariableResolver::next_block(Block &block)
156 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
159 void VariableResolver::enter(Block &block)
161 block.variables.clear();
164 void VariableResolver::visit(VariableReference &var)
168 for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
170 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
171 if(i!=block->variables.end())
172 var.declaration = i->second;
177 if(var.declaration->type_declaration)
178 members = &var.declaration->type_declaration->members.variables;
182 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
183 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
184 if(i!=blocks.end() && i->second->instance_name==var.name)
186 iface_ref = new InterfaceBlockReference;
187 iface_ref->name = var.name;
188 iface_ref->declaration = i->second;
189 members = &i->second->members.variables;
193 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
194 if(i->second->instance_name.empty())
196 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
197 if(j!=i->second->members.variables.end())
198 var.declaration = j->second;
205 if(assignment_target)
207 record_target = false;
208 assignment_target = 0;
211 assignment_target = var.declaration;
213 else if(var.declaration && var.declaration==assignment_target)
214 self_referencing = true;
217 void VariableResolver::visit(InterfaceBlockReference &iface)
219 iface.declaration = 0;
220 for(Block *block=current_block; block; block=next_block(*block))
222 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
223 if(i!=stage->interface_blocks.end())
225 iface.declaration = i->second;
226 members = &i->second->members.variables;
232 void VariableResolver::visit(MemberAccess &memacc)
236 memacc.left->visit(*this);
239 memacc.left = iface_ref;
242 memacc.declaration = 0;
245 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
246 if(i!=members->end())
248 memacc.declaration = i->second;
249 if(i->second->type_declaration)
250 members = &i->second->type_declaration->members.variables;
257 void VariableResolver::visit(BinaryExpression &binary)
262 SetForScope<bool> set(record_target, false);
263 binary.right->visit(*this);
267 binary.left->visit(*this);
269 binary.left = iface_ref;
274 TraversingVisitor::visit(binary);
279 void VariableResolver::visit(Assignment &assign)
282 SetFlag set(record_target);
283 assignment_target = 0;
284 assign.left->visit(*this);
287 self_referencing = false;
288 assign.right->visit(*this);
290 assign.self_referencing = (self_referencing || assign.oper!="=");
291 assign.target_declaration = assignment_target;
294 void VariableResolver::visit(StructDeclaration &strct)
296 TraversingVisitor::visit(strct);
297 stage->types[strct.name] = &strct;
300 void VariableResolver::visit(VariableDeclaration &var)
302 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
303 if(i!=stage->types.end())
304 var.type_declaration = i->second;
306 if(!block_interface.empty() && var.interface.empty())
307 var.interface = block_interface;
309 TraversingVisitor::visit(var);
310 current_block->variables[var.name] = &var;
313 void VariableResolver::visit(InterfaceBlock &iface)
315 /* Block names can't be used for any other identifiers so we can put them
316 in the same map with instance names. */
317 stage->interface_blocks[iface.name] = &iface;
318 if(!iface.instance_name.empty())
319 stage->interface_blocks[iface.instance_name] = &iface;
321 SetForScope<string> set_iface(block_interface, iface.interface);
322 TraversingVisitor::visit(iface);
326 void FunctionResolver::visit(FunctionCall &call)
328 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
329 if(i!=functions.end())
330 call.declaration = i->second.back();
332 TraversingVisitor::visit(call);
335 void FunctionResolver::visit(FunctionDeclaration &func)
337 vector<FunctionDeclaration *> &decls = functions[func.name];
340 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
341 (*i)->definition = func.definition;
343 decls.push_back(&func);
345 else if(!decls.empty() && decls.back()->definition)
346 func.definition = decls.back()->definition;
348 decls.push_back(&func);
350 TraversingVisitor::visit(func);
354 InterfaceGenerator::InterfaceGenerator():
356 function_scope(false),
359 iface_target_block(0)
362 string InterfaceGenerator::get_out_prefix(Stage::Type type)
364 if(type==Stage::VERTEX)
366 else if(type==Stage::GEOMETRY)
372 void InterfaceGenerator::apply(Stage &s)
375 iface_target_block = &stage->content;
377 in_prefix = get_out_prefix(stage->previous->type);
378 out_prefix = get_out_prefix(stage->type);
379 s.content.visit(*this);
380 NodeRemover().apply(s, nodes_to_remove);
383 void InterfaceGenerator::visit(Block &block)
385 SetForScope<Block *> set_block(current_block, &block);
386 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
388 assignment_insert_point = i;
389 if(&block==&stage->content)
390 iface_insert_point = i;
396 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
398 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
399 return prefix+name.substr(offset);
402 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
404 if(stage->content.variables.count(name))
407 VariableDeclaration* iface_var = new VariableDeclaration;
408 iface_var->sampling = var.sampling;
409 iface_var->interface = iface;
410 iface_var->type = var.type;
411 iface_var->type_declaration = var.type_declaration;
412 iface_var->name = name;
413 if(stage->type==Stage::GEOMETRY && !copy_block)
414 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
416 iface_var->array = var.array;
418 iface_var->array_size = var.array_size;
421 iface_var->layout = var.layout;
422 iface_var->linked_declaration = &var;
423 var.linked_declaration = iface_var;
426 iface_target_block->body.insert(iface_insert_point, iface_var);
427 iface_target_block->variables[name] = iface_var;
432 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
434 if(stage->interface_blocks.count(out_block.name))
437 InterfaceBlock *in_block = new InterfaceBlock;
438 in_block->interface = "in";
439 in_block->name = out_block.name;
440 in_block->instance_name = out_block.instance_name;
441 if(stage->type==Stage::GEOMETRY)
442 in_block->array = true;
444 in_block->array = out_block.array;
445 in_block->linked_block = &out_block;
446 out_block.linked_block = in_block;
449 SetFlag set_copy(copy_block, true);
450 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
451 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
452 out_block.members.visit(*this);
455 iface_target_block->body.insert(iface_insert_point, in_block);
456 stage->interface_blocks[in_block->name] = in_block;
457 if(!in_block->instance_name.empty())
458 stage->interface_blocks[in_block->instance_name] = in_block;
460 SetFlag set_scope(function_scope, false);
461 SetForScope<Block *> set_block(current_block, &stage->content);
462 in_block->visit(*this);
467 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
469 Assignment *assign = new Assignment;
470 VariableReference *ref = new VariableReference;
474 assign->right = right;
476 ExpressionStatement *stmt = new ExpressionStatement;
477 stmt->expression = assign;
478 current_block->body.insert(assignment_insert_point, stmt);
484 void InterfaceGenerator::visit(VariableReference &var)
486 if(var.declaration || !stage->previous)
488 /* Don't pull a variable from previous stage if we just generated an out
489 interface in this stage */
490 if(stage->content.variables.count(var.name))
493 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
494 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
495 if(i==prev_vars.end() || i->second->interface!="out")
496 i = prev_vars.find(in_prefix+var.name);
497 if(i!=prev_vars.end() && i->second->interface=="out")
499 generate_interface(*i->second, "in", i->second->name);
500 var.name = i->second->name;
504 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
505 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
506 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
508 generate_interface(*j->second);
512 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
513 if(j->second->instance_name.empty())
515 i = j->second->members.variables.find(var.name);
516 if(i!=j->second->members.variables.end())
518 generate_interface(*j->second);
524 void InterfaceGenerator::visit(VariableDeclaration &var)
528 generate_interface(var, "in", var.name);
534 if(iface_block->linked_block)
536 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
537 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
538 if(i!=linked_vars.end())
539 var.linked_declaration = i->second;
544 if(var.interface=="out")
546 /* For out variables in function scope, generate a global interface and
547 replace the local declaration with an assignment. */
548 if(function_scope && generate_interface(var, "out", var.name))
550 nodes_to_remove.insert(&var);
551 if(var.init_expression)
553 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
554 stmt.source = var.source;
555 stmt.line = var.line;
560 else if(var.interface=="in")
562 /* Try to link in variables in global scope with out variables from
564 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
566 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
567 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
568 if(i!=prev_vars.end() && i->second->interface=="out")
570 var.linked_declaration = i->second;
571 i->second->linked_declaration = &var;
576 TraversingVisitor::visit(var);
579 void InterfaceGenerator::visit(InterfaceBlock &iface)
581 if(iface.interface=="in")
583 if(!iface.linked_block && stage->previous)
585 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
586 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
587 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
589 iface.linked_block = i->second;
590 i->second->linked_block = &iface;
595 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
596 TraversingVisitor::visit(iface);
599 void InterfaceGenerator::visit(FunctionDeclaration &func)
601 SetFlag set_scope(function_scope, true);
602 // Skip parameters because they're not useful here
603 func.body.visit(*this);
606 void InterfaceGenerator::visit(Passthrough &pass)
608 vector<VariableDeclaration *> pass_vars;
610 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
611 if(i->second->interface=="in")
612 pass_vars.push_back(i->second);
616 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
617 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
620 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
621 linked = ((*j)->linked_declaration==i->second);
623 if(!linked && generate_interface(*i->second, "in", i->second->name))
624 pass_vars.push_back(i->second);
628 if(stage->type==Stage::GEOMETRY)
630 InterfaceBlockReference *ref = new InterfaceBlockReference;
633 BinaryExpression *subscript = new BinaryExpression;
634 subscript->left = ref;
635 subscript->oper = "[";
636 subscript->right = pass.subscript;
637 subscript->after = "]";
639 MemberAccess *memacc = new MemberAccess;
640 memacc->left = subscript;
641 memacc->member = "gl_Position";
643 insert_assignment("gl_Position", memacc);
646 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
648 string out_name = change_prefix((*i)->name, out_prefix);
649 generate_interface(**i, "out", out_name);
651 VariableReference *ref = new VariableReference;
652 ref->name = (*i)->name;
655 BinaryExpression *subscript = new BinaryExpression;
656 subscript->left = ref;
657 subscript->oper = "[";
658 subscript->right = pass.subscript;
659 subscript->after = "]";
660 insert_assignment(out_name, subscript);
663 insert_assignment(out_name, ref);
666 nodes_to_remove.insert(&pass);
670 DeclarationReorderer::DeclarationReorderer():
674 void DeclarationReorderer::visit(FunctionCall &call)
676 FunctionDeclaration *def = call.declaration;
678 def = def->definition;
679 if(def && !ordered_funcs.count(def))
680 needed_funcs.insert(def);
683 void DeclarationReorderer::visit(Block &block)
686 return TraversingVisitor::visit(block);
688 NodeList<Statement>::iterator struct_insert_point = block.body.end();
689 NodeList<Statement>::iterator variable_insert_point = block.body.end();
690 NodeList<Statement>::iterator function_insert_point = block.body.end();
691 unsigned unordered_func_count = 0;
692 bool ordered_any_funcs = false;
694 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
696 kind = NO_DECLARATION;
700 if(kind==STRUCT && struct_insert_point!=block.body.end())
702 block.body.insert(struct_insert_point, *i);
705 else if(kind>STRUCT && struct_insert_point==block.body.end())
706 struct_insert_point = i;
708 if(kind==VARIABLE && variable_insert_point!=block.body.end())
710 block.body.insert(variable_insert_point, *i);
713 else if(kind>VARIABLE && variable_insert_point==block.body.end())
714 variable_insert_point = i;
718 if(function_insert_point==block.body.end())
719 function_insert_point = i;
721 if(needed_funcs.empty())
723 ordered_funcs.insert(i->get());
724 if(i!=function_insert_point)
726 block.body.insert(function_insert_point, *i);
730 ++function_insert_point;
731 ordered_any_funcs = true;
734 ++unordered_func_count;
739 if(function_insert_point==i)
740 ++function_insert_point;
741 block.body.erase(i++);
746 if(i==block.body.end() && unordered_func_count)
748 if(!ordered_any_funcs)
749 // A subset of the remaining functions forms a recursive loop
750 /* TODO pick a function and move it up, adding any necessary
754 i = function_insert_point;
755 unordered_func_count = 0;
760 void DeclarationReorderer::visit(VariableDeclaration &var)
762 TraversingVisitor::visit(var);
766 void DeclarationReorderer::visit(FunctionDeclaration &func)
768 needed_funcs.clear();
769 func.body.visit(*this);
770 needed_funcs.erase(&func);