1 #include <msp/core/raii.h>
11 void DeclarationCombiner::apply(Stage &stage)
13 stage.content.visit(*this);
14 NodeRemover().apply(stage, nodes_to_remove);
17 void DeclarationCombiner::visit(Block &block)
22 TraversingVisitor::visit(block);
25 void DeclarationCombiner::visit(FunctionDeclaration &func)
27 vector<FunctionDeclaration *> &decls = functions[func.name];
30 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
32 (*i)->definition = func.definition;
33 (*i)->body.body.clear();
36 decls.push_back(&func);
39 void DeclarationCombiner::visit(VariableDeclaration &var)
41 VariableDeclaration *&ptr = variables[var.name];
45 if(var.init_expression)
46 ptr->init_expression = var.init_expression;
51 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
54 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
57 j->has_value = i->value;
63 ptr->layout->qualifiers.push_back(*i);
67 ptr->layout = var.layout;
69 nodes_to_remove.insert(&var);
76 void BlockResolver::enter(Block &block)
78 block.parent = current_block;
82 VariableResolver::VariableResolver():
88 self_referencing(false)
91 void VariableResolver::apply(Stage &s)
94 Stage *builtin_stage = get_builtins(s.type);
95 builtins = (builtin_stage ? &builtin_stage->content : 0);
96 s.content.visit(*this);
99 Block *VariableResolver::next_block(Block &block)
101 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
104 void VariableResolver::enter(Block &block)
106 block.variables.clear();
109 void VariableResolver::visit(VariableReference &var)
113 for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
115 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
116 if(i!=block->variables.end())
117 var.declaration = i->second;
122 if(var.declaration->type_declaration)
123 members = &var.declaration->type_declaration->members.variables;
127 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
128 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
129 if(i!=blocks.end() && i->second->instance_name==var.name)
131 iface_ref = new InterfaceBlockReference;
132 iface_ref->name = var.name;
133 iface_ref->declaration = i->second;
134 members = &i->second->members.variables;
138 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
139 if(i->second->instance_name.empty())
141 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
142 if(j!=i->second->members.variables.end())
143 var.declaration = j->second;
150 if(assignment_target)
152 record_target = false;
153 assignment_target = 0;
156 assignment_target = var.declaration;
158 else if(var.declaration && var.declaration==assignment_target)
159 self_referencing = true;
162 void VariableResolver::visit(InterfaceBlockReference &iface)
164 iface.declaration = 0;
165 for(Block *block=current_block; block; block=next_block(*block))
167 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
168 if(i!=stage->interface_blocks.end())
170 iface.declaration = i->second;
171 members = &i->second->members.variables;
177 void VariableResolver::visit(MemberAccess &memacc)
181 memacc.left->visit(*this);
184 memacc.left = iface_ref;
187 memacc.declaration = 0;
190 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
191 if(i!=members->end())
193 memacc.declaration = i->second;
194 if(i->second->type_declaration)
195 members = &i->second->type_declaration->members.variables;
202 void VariableResolver::visit(BinaryExpression &binary)
207 SetForScope<bool> set(record_target, false);
208 binary.right->visit(*this);
212 binary.left->visit(*this);
214 binary.left = iface_ref;
219 TraversingVisitor::visit(binary);
224 void VariableResolver::visit(Assignment &assign)
227 SetFlag set(record_target);
228 assignment_target = 0;
229 assign.left->visit(*this);
232 self_referencing = false;
233 assign.right->visit(*this);
235 assign.self_referencing = (self_referencing || assign.oper!="=");
236 assign.target_declaration = assignment_target;
239 void VariableResolver::visit(StructDeclaration &strct)
241 TraversingVisitor::visit(strct);
242 stage->types[strct.name] = &strct;
245 void VariableResolver::visit(VariableDeclaration &var)
247 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
248 if(i!=stage->types.end())
249 var.type_declaration = i->second;
251 if(!block_interface.empty() && var.interface.empty())
252 var.interface = block_interface;
254 TraversingVisitor::visit(var);
255 current_block->variables[var.name] = &var;
258 void VariableResolver::visit(InterfaceBlock &iface)
260 /* Block names can't be used for any other identifiers so we can put them
261 in the same map with instance names. */
262 stage->interface_blocks[iface.name] = &iface;
263 if(!iface.instance_name.empty())
264 stage->interface_blocks[iface.instance_name] = &iface;
266 SetForScope<string> set_iface(block_interface, iface.interface);
267 TraversingVisitor::visit(iface);
271 void FunctionResolver::visit(FunctionCall &call)
273 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
274 if(i!=functions.end())
275 call.declaration = i->second.back();
277 TraversingVisitor::visit(call);
280 void FunctionResolver::visit(FunctionDeclaration &func)
282 vector<FunctionDeclaration *> &decls = functions[func.name];
285 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
286 (*i)->definition = func.definition;
288 decls.push_back(&func);
290 else if(!decls.empty() && decls.back()->definition)
291 func.definition = decls.back()->definition;
293 decls.push_back(&func);
295 TraversingVisitor::visit(func);
299 InterfaceGenerator::InterfaceGenerator():
301 function_scope(false),
304 iface_target_block(0)
307 string InterfaceGenerator::get_out_prefix(Stage::Type type)
309 if(type==Stage::VERTEX)
311 else if(type==Stage::GEOMETRY)
317 void InterfaceGenerator::apply(Stage &s)
320 iface_target_block = &stage->content;
322 in_prefix = get_out_prefix(stage->previous->type);
323 out_prefix = get_out_prefix(stage->type);
324 s.content.visit(*this);
325 NodeRemover().apply(s, nodes_to_remove);
328 void InterfaceGenerator::visit(Block &block)
330 SetForScope<Block *> set_block(current_block, &block);
331 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
333 assignment_insert_point = i;
334 if(&block==&stage->content)
335 iface_insert_point = i;
341 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
343 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
344 return prefix+name.substr(offset);
347 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
349 if(stage->content.variables.count(name))
352 VariableDeclaration* iface_var = new VariableDeclaration;
353 iface_var->sampling = var.sampling;
354 iface_var->interface = iface;
355 iface_var->type = var.type;
356 iface_var->type_declaration = var.type_declaration;
357 iface_var->name = name;
358 if(stage->type==Stage::GEOMETRY && !copy_block)
359 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
361 iface_var->array = var.array;
363 iface_var->array_size = var.array_size;
366 iface_var->linked_declaration = &var;
367 var.linked_declaration = iface_var;
370 iface_target_block->body.insert(iface_insert_point, iface_var);
371 iface_target_block->variables[name] = iface_var;
376 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
378 if(stage->interface_blocks.count(out_block.name))
381 InterfaceBlock *in_block = new InterfaceBlock;
382 in_block->interface = "in";
383 in_block->name = out_block.name;
384 in_block->instance_name = out_block.instance_name;
385 if(stage->type==Stage::GEOMETRY)
386 in_block->array = true;
388 in_block->array = out_block.array;
389 in_block->linked_block = &out_block;
390 out_block.linked_block = in_block;
393 SetFlag set_copy(copy_block, true);
394 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
395 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
396 out_block.members.visit(*this);
399 iface_target_block->body.insert(iface_insert_point, in_block);
400 stage->interface_blocks[in_block->name] = in_block;
401 if(!in_block->instance_name.empty())
402 stage->interface_blocks[in_block->instance_name] = in_block;
404 SetFlag set_scope(function_scope, false);
405 SetForScope<Block *> set_block(current_block, &stage->content);
406 in_block->visit(*this);
411 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
413 Assignment *assign = new Assignment;
414 VariableReference *ref = new VariableReference;
418 assign->right = right;
420 ExpressionStatement *stmt = new ExpressionStatement;
421 stmt->expression = assign;
422 current_block->body.insert(assignment_insert_point, stmt);
428 void InterfaceGenerator::visit(VariableReference &var)
430 if(var.declaration || !stage->previous)
432 /* Don't pull a variable from previous stage if we just generated an out
433 interface in this stage */
434 if(stage->content.variables.count(var.name))
437 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
438 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
439 if(i==prev_vars.end() || i->second->interface!="out")
440 i = prev_vars.find(in_prefix+var.name);
441 if(i!=prev_vars.end() && i->second->interface=="out")
443 generate_interface(*i->second, "in", i->second->name);
444 var.name = i->second->name;
448 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
449 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
450 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
452 generate_interface(*j->second);
456 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
457 if(j->second->instance_name.empty())
459 i = j->second->members.variables.find(var.name);
460 if(i!=j->second->members.variables.end())
462 generate_interface(*j->second);
468 void InterfaceGenerator::visit(VariableDeclaration &var)
472 generate_interface(var, "in", var.name);
478 if(iface_block->linked_block)
480 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
481 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
482 if(i!=linked_vars.end())
483 var.linked_declaration = i->second;
488 if(var.interface=="out")
490 /* For out variables in function scope, generate a global interface and
491 replace the local declaration with an assignment. */
492 if(function_scope && generate_interface(var, "out", var.name))
494 nodes_to_remove.insert(&var);
495 if(var.init_expression)
497 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
498 stmt.source = var.source;
499 stmt.line = var.line;
504 else if(var.interface=="in")
506 /* Try to link in variables in global scope with out variables from
508 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
510 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
511 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
512 if(i!=prev_vars.end() && i->second->interface=="out")
514 var.linked_declaration = i->second;
515 i->second->linked_declaration = &var;
520 TraversingVisitor::visit(var);
523 void InterfaceGenerator::visit(InterfaceBlock &iface)
525 if(iface.interface=="in")
527 if(!iface.linked_block && stage->previous)
529 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
530 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
531 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
533 iface.linked_block = i->second;
534 i->second->linked_block = &iface;
539 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
540 TraversingVisitor::visit(iface);
543 void InterfaceGenerator::visit(FunctionDeclaration &func)
545 SetFlag set_scope(function_scope, true);
546 // Skip parameters because they're not useful here
547 func.body.visit(*this);
550 void InterfaceGenerator::visit(Passthrough &pass)
552 vector<VariableDeclaration *> pass_vars;
554 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
555 if(i->second->interface=="in")
556 pass_vars.push_back(i->second);
560 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
561 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
564 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
565 linked = ((*j)->linked_declaration==i->second);
567 if(!linked && generate_interface(*i->second, "in", i->second->name))
568 pass_vars.push_back(i->second);
572 if(stage->type==Stage::GEOMETRY)
574 InterfaceBlockReference *ref = new InterfaceBlockReference;
577 BinaryExpression *subscript = new BinaryExpression;
578 subscript->left = ref;
579 subscript->oper = "[";
580 subscript->right = pass.subscript;
581 subscript->after = "]";
583 MemberAccess *memacc = new MemberAccess;
584 memacc->left = subscript;
585 memacc->member = "gl_Position";
587 insert_assignment("gl_Position", memacc);
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);
610 nodes_to_remove.insert(&pass);
614 DeclarationReorderer::DeclarationReorderer():
618 void DeclarationReorderer::visit(FunctionCall &call)
620 FunctionDeclaration *def = call.declaration;
622 def = def->definition;
623 if(def && !ordered_funcs.count(def))
624 needed_funcs.insert(def);
627 void DeclarationReorderer::visit(Block &block)
630 return TraversingVisitor::visit(block);
632 NodeList<Statement>::iterator struct_insert_point = block.body.end();
633 NodeList<Statement>::iterator variable_insert_point = block.body.end();
634 NodeList<Statement>::iterator function_insert_point = block.body.end();
635 unsigned unordered_func_count = 0;
636 bool ordered_any_funcs = false;
638 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
640 kind = NO_DECLARATION;
644 if(kind==STRUCT && struct_insert_point!=block.body.end())
646 block.body.insert(struct_insert_point, *i);
649 else if(kind>STRUCT && struct_insert_point==block.body.end())
650 struct_insert_point = i;
652 if(kind==VARIABLE && variable_insert_point!=block.body.end())
654 block.body.insert(variable_insert_point, *i);
657 else if(kind>VARIABLE && variable_insert_point==block.body.end())
658 variable_insert_point = i;
662 if(function_insert_point==block.body.end())
663 function_insert_point = i;
665 if(needed_funcs.empty())
667 ordered_funcs.insert(i->get());
668 if(i!=function_insert_point)
670 block.body.insert(function_insert_point, *i);
674 ++function_insert_point;
675 ordered_any_funcs = true;
678 ++unordered_func_count;
683 if(function_insert_point==i)
684 ++function_insert_point;
685 block.body.erase(i++);
690 if(i==block.body.end() && unordered_func_count)
692 if(!ordered_any_funcs)
693 // A subset of the remaining functions forms a recursive loop
694 /* TODO pick a function and move it up, adding any necessary
698 i = function_insert_point;
699 unordered_func_count = 0;
704 void DeclarationReorderer::visit(VariableDeclaration &var)
706 TraversingVisitor::visit(var);
710 void DeclarationReorderer::visit(FunctionDeclaration &func)
712 needed_funcs.clear();
713 func.body.visit(*this);
714 needed_funcs.erase(&func);