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;
81 void BlockResolver::visit(InterfaceBlock &iface)
83 /* Block names can't be used for any other identifiers so we can put them
84 in the same map with instance names. */
85 current_block->interfaces[iface.name] = &iface;
86 if(!iface.instance_name.empty())
87 current_block->interfaces[iface.instance_name] = &iface;
88 TraversingVisitor::visit(iface);
92 VariableResolver::VariableResolver():
97 self_referencing(false)
100 void VariableResolver::apply(Stage &stage)
102 Stage *builtin_stage = get_builtins(stage.type);
103 builtins = (builtin_stage ? &builtin_stage->content : 0);
104 stage.content.visit(*this);
107 Block *VariableResolver::next_block(Block &block)
109 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
112 void VariableResolver::enter(Block &block)
114 block.variables.clear();
117 void VariableResolver::visit(VariableReference &var)
121 for(Block *block=current_block; block; block=next_block(*block))
123 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
124 if(i!=block->variables.end())
125 var.declaration = i->second;
128 const map<string, InterfaceBlock *> &ifaces = block->interfaces;
129 for(map<string, InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
130 if(j->second->instance_name.empty())
132 i = j->second->members.variables.find(var.name);
133 if(i!=j->second->members.variables.end())
134 var.declaration = i->second;
140 if(var.declaration->type_declaration)
141 members = &var.declaration->type_declaration->members.variables;
145 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(var.name);
146 if(j!=block->interfaces.end() && j->second->instance_name==var.name)
148 iface_ref = new InterfaceBlockReference;
149 iface_ref->name = var.name;
150 iface_ref->declaration = j->second;
151 members = &j->second->members.variables;
158 if(assignment_target)
160 record_target = false;
161 assignment_target = 0;
164 assignment_target = var.declaration;
166 else if(var.declaration && var.declaration==assignment_target)
167 self_referencing = true;
170 void VariableResolver::visit(InterfaceBlockReference &iface)
172 iface.declaration = 0;
173 for(Block *block=current_block; block; block=next_block(*block))
175 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(iface.name);
176 if(j!=block->interfaces.end())
178 iface.declaration = j->second;
179 members = &j->second->members.variables;
185 void VariableResolver::visit(MemberAccess &memacc)
189 memacc.left->visit(*this);
192 memacc.left = iface_ref;
195 memacc.declaration = 0;
198 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
199 if(i!=members->end())
201 memacc.declaration = i->second;
202 if(i->second->type_declaration)
203 members = &i->second->type_declaration->members.variables;
210 void VariableResolver::visit(BinaryExpression &binary)
215 SetForScope<bool> set(record_target, false);
216 binary.right->visit(*this);
220 binary.left->visit(*this);
222 binary.left = iface_ref;
227 TraversingVisitor::visit(binary);
232 void VariableResolver::visit(Assignment &assign)
235 SetFlag set(record_target);
236 assignment_target = 0;
237 assign.left->visit(*this);
240 self_referencing = false;
241 assign.right->visit(*this);
243 assign.self_referencing = (self_referencing || assign.oper!="=");
244 assign.target_declaration = assignment_target;
247 void VariableResolver::visit(StructDeclaration &strct)
249 TraversingVisitor::visit(strct);
250 current_block->types[strct.name] = &strct;
253 void VariableResolver::visit(VariableDeclaration &var)
255 for(Block *block=current_block; block; block=next_block(*block))
257 map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
258 if(j!=block->types.end())
259 var.type_declaration = j->second;
262 if(!block_interface.empty() && var.interface.empty())
263 var.interface = block_interface;
265 TraversingVisitor::visit(var);
266 current_block->variables[var.name] = &var;
269 void VariableResolver::visit(InterfaceBlock &iface)
271 SetForScope<string> set_iface(block_interface, iface.interface);
272 TraversingVisitor::visit(iface);
276 void FunctionResolver::visit(FunctionCall &call)
278 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
279 if(i!=functions.end())
280 call.declaration = i->second.back();
282 TraversingVisitor::visit(call);
285 void FunctionResolver::visit(FunctionDeclaration &func)
287 vector<FunctionDeclaration *> &decls = functions[func.name];
290 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
291 (*i)->definition = func.definition;
293 decls.push_back(&func);
295 else if(!decls.empty() && decls.back()->definition)
296 func.definition = decls.back()->definition;
298 decls.push_back(&func);
300 TraversingVisitor::visit(func);
304 InterfaceGenerator::InterfaceGenerator():
306 function_scope(false),
309 iface_target_block(0)
312 string InterfaceGenerator::get_out_prefix(Stage::Type type)
314 if(type==Stage::VERTEX)
316 else if(type==Stage::GEOMETRY)
322 void InterfaceGenerator::apply(Stage &s)
325 iface_target_block = &stage->content;
327 in_prefix = get_out_prefix(stage->previous->type);
328 out_prefix = get_out_prefix(stage->type);
329 s.content.visit(*this);
330 NodeRemover().apply(s, nodes_to_remove);
333 void InterfaceGenerator::visit(Block &block)
335 SetForScope<Block *> set_block(current_block, &block);
336 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
338 assignment_insert_point = i;
339 if(&block==&stage->content)
340 iface_insert_point = i;
346 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
348 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
349 return prefix+name.substr(offset);
352 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
354 if(stage->content.variables.count(name))
357 VariableDeclaration* iface_var = new VariableDeclaration;
358 iface_var->sampling = var.sampling;
359 iface_var->interface = iface;
360 iface_var->type = var.type;
361 iface_var->type_declaration = var.type_declaration;
362 iface_var->name = name;
363 if(stage->type==Stage::GEOMETRY && !copy_block)
364 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
366 iface_var->array = var.array;
368 iface_var->array_size = var.array_size;
371 iface_var->linked_declaration = &var;
372 var.linked_declaration = iface_var;
375 iface_target_block->body.insert(iface_insert_point, iface_var);
376 iface_target_block->variables[name] = iface_var;
381 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
383 if(stage->content.interfaces.count(out_block.name))
386 InterfaceBlock *in_block = new InterfaceBlock;
387 in_block->interface = "in";
388 in_block->name = out_block.name;
389 in_block->instance_name = out_block.instance_name;
390 if(stage->type==Stage::GEOMETRY)
391 in_block->array = true;
393 in_block->array = out_block.array;
394 in_block->linked_block = &out_block;
395 out_block.linked_block = in_block;
398 SetFlag set_copy(copy_block, true);
399 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
400 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
401 out_block.members.visit(*this);
404 iface_target_block->body.insert(iface_insert_point, in_block);
405 iface_target_block->interfaces[in_block->name] = in_block;
406 if(!in_block->instance_name.empty())
407 stage->content.interfaces[in_block->instance_name] = in_block;
409 SetFlag set_scope(function_scope, false);
410 SetForScope<Block *> set_block(current_block, &stage->content);
411 in_block->visit(*this);
416 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
418 Assignment *assign = new Assignment;
419 VariableReference *ref = new VariableReference;
423 assign->right = right;
425 ExpressionStatement *stmt = new ExpressionStatement;
426 stmt->expression = assign;
427 current_block->body.insert(assignment_insert_point, stmt);
433 void InterfaceGenerator::visit(VariableReference &var)
435 if(var.declaration || !stage->previous)
437 /* Don't pull a variable from previous stage if we just generated an out
438 interface in this stage */
439 if(stage->content.variables.count(var.name))
442 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
443 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
444 if(i==prev_vars.end() || i->second->interface!="out")
445 i = prev_vars.find(in_prefix+var.name);
446 if(i!=prev_vars.end() && i->second->interface=="out")
448 generate_interface(*i->second, "in", i->second->name);
449 var.name = i->second->name;
453 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
454 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
455 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
457 generate_interface(*j->second);
461 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
462 if(j->second->instance_name.empty())
464 i = j->second->members.variables.find(var.name);
465 if(i!=j->second->members.variables.end())
467 generate_interface(*j->second);
473 void InterfaceGenerator::visit(VariableDeclaration &var)
477 generate_interface(var, "in", var.name);
483 if(iface_block->linked_block)
485 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
486 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
487 if(i!=linked_vars.end())
488 var.linked_declaration = i->second;
493 if(var.interface=="out")
495 /* For out variables in function scope, generate a global interface and
496 replace the local declaration with an assignment. */
497 if(function_scope && generate_interface(var, "out", var.name))
499 nodes_to_remove.insert(&var);
500 if(var.init_expression)
502 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
503 stmt.source = var.source;
504 stmt.line = var.line;
509 else if(var.interface=="in")
511 /* Try to link in variables in global scope with out variables from
513 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
515 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
516 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
517 if(i!=prev_vars.end() && i->second->interface=="out")
519 var.linked_declaration = i->second;
520 i->second->linked_declaration = &var;
525 TraversingVisitor::visit(var);
528 void InterfaceGenerator::visit(InterfaceBlock &iface)
530 if(iface.interface=="in")
532 if(!iface.linked_block && stage->previous)
534 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
535 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
536 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
538 iface.linked_block = i->second;
539 i->second->linked_block = &iface;
544 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
545 TraversingVisitor::visit(iface);
548 void InterfaceGenerator::visit(FunctionDeclaration &func)
550 SetFlag set_scope(function_scope, true);
551 // Skip parameters because they're not useful here
552 func.body.visit(*this);
555 void InterfaceGenerator::visit(Passthrough &pass)
557 vector<VariableDeclaration *> pass_vars;
559 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
560 if(i->second->interface=="in")
561 pass_vars.push_back(i->second);
565 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
566 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
569 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
570 linked = ((*j)->linked_declaration==i->second);
572 if(!linked && generate_interface(*i->second, "in", i->second->name))
573 pass_vars.push_back(i->second);
577 if(stage->type==Stage::GEOMETRY)
579 InterfaceBlockReference *ref = new InterfaceBlockReference;
582 BinaryExpression *subscript = new BinaryExpression;
583 subscript->left = ref;
584 subscript->oper = "[";
585 subscript->right = pass.subscript;
586 subscript->after = "]";
588 MemberAccess *memacc = new MemberAccess;
589 memacc->left = subscript;
590 memacc->member = "gl_Position";
592 insert_assignment("gl_Position", memacc);
595 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
597 string out_name = change_prefix((*i)->name, out_prefix);
598 generate_interface(**i, "out", out_name);
600 VariableReference *ref = new VariableReference;
601 ref->name = (*i)->name;
604 BinaryExpression *subscript = new BinaryExpression;
605 subscript->left = ref;
606 subscript->oper = "[";
607 subscript->right = pass.subscript;
608 subscript->after = "]";
609 insert_assignment(out_name, subscript);
612 insert_assignment(out_name, ref);
615 nodes_to_remove.insert(&pass);
619 DeclarationReorderer::DeclarationReorderer():
623 void DeclarationReorderer::visit(FunctionCall &call)
625 FunctionDeclaration *def = call.declaration;
627 def = def->definition;
628 if(def && !ordered_funcs.count(def))
629 needed_funcs.insert(def);
632 void DeclarationReorderer::visit(Block &block)
635 return TraversingVisitor::visit(block);
637 NodeList<Statement>::iterator struct_insert_point = block.body.end();
638 NodeList<Statement>::iterator variable_insert_point = block.body.end();
639 NodeList<Statement>::iterator function_insert_point = block.body.end();
640 unsigned unordered_func_count = 0;
641 bool ordered_any_funcs = false;
643 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
645 kind = NO_DECLARATION;
649 if(kind==STRUCT && struct_insert_point!=block.body.end())
651 block.body.insert(struct_insert_point, *i);
654 else if(kind>STRUCT && struct_insert_point==block.body.end())
655 struct_insert_point = i;
657 if(kind==VARIABLE && variable_insert_point!=block.body.end())
659 block.body.insert(variable_insert_point, *i);
662 else if(kind>VARIABLE && variable_insert_point==block.body.end())
663 variable_insert_point = i;
667 if(function_insert_point==block.body.end())
668 function_insert_point = i;
670 if(needed_funcs.empty())
672 ordered_funcs.insert(i->get());
673 if(i!=function_insert_point)
675 block.body.insert(function_insert_point, *i);
679 ++function_insert_point;
680 ordered_any_funcs = true;
683 ++unordered_func_count;
688 if(function_insert_point==i)
689 ++function_insert_point;
690 block.body.erase(i++);
695 if(i==block.body.end() && unordered_func_count)
697 if(!ordered_any_funcs)
698 // A subset of the remaining functions forms a recursive loop
699 /* TODO pick a function and move it up, adding any necessary
703 i = function_insert_point;
704 unordered_func_count = 0;
709 void DeclarationReorderer::visit(VariableDeclaration &var)
711 TraversingVisitor::visit(var);
715 void DeclarationReorderer::visit(FunctionDeclaration &func)
717 needed_funcs.clear();
718 func.body.visit(*this);
719 needed_funcs.erase(&func);