+#include <msp/core/hash.h>
#include <msp/core/raii.h>
+#include <msp/strings/lexicalcast.h>
#include "builtin.h"
#include "generate.h"
}
-void BlockResolver::enter(Block &block)
+ConstantSpecializer::ConstantSpecializer():
+ values(0)
+{ }
+
+void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
{
- block.parent = current_block;
+ values = v;
+ stage.content.visit(*this);
}
-void BlockResolver::visit(InterfaceBlock &iface)
+void ConstantSpecializer::visit(VariableDeclaration &var)
{
- current_block->interfaces.insert(&iface);
- TraversingVisitor::visit(iface);
+ bool specializable = false;
+ if(var.layout)
+ {
+ vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
+ for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
+ if(i->name=="constant_id")
+ {
+ specializable = true;
+ if(values)
+ qualifiers.erase(i);
+ else if(i->value==-1)
+ i->value = hash32(var.name)&0x7FFFFFFF;
+ break;
+ }
+
+ if(qualifiers.empty())
+ var.layout = 0;
+ }
+
+ if(specializable && values)
+ {
+ map<string, int>::const_iterator i = values->find(var.name);
+ if(i!=values->end())
+ {
+ RefPtr<Literal> literal = new Literal;
+ if(var.type=="bool")
+ {
+ literal->token = (i->second ? "true" : "false");
+ literal->value = static_cast<bool>(i->second);
+ }
+ else if(var.type=="int")
+ {
+ literal->token = lexical_cast<string>(i->second);
+ literal->value = i->second;
+ }
+ var.init_expression = literal;
+ }
+ }
+}
+
+
+void BlockHierarchyResolver::enter(Block &block)
+{
+ block.parent = current_block;
}
VariableResolver::VariableResolver():
+ stage(0),
+ builtins(0),
+ members(0),
record_target(false),
assignment_target(0),
self_referencing(false)
{ }
-void VariableResolver::apply(Stage &stage)
+void VariableResolver::apply(Stage &s)
{
- Stage *builtin_stage = get_builtins(stage.type);
+ stage = &s;
+ Stage *builtin_stage = get_builtins(s.type);
builtins = (builtin_stage ? &builtin_stage->content : 0);
- stage.content.visit(*this);
+ s.content.visit(*this);
}
Block *VariableResolver::next_block(Block &block)
void VariableResolver::visit(VariableReference &var)
{
var.declaration = 0;
- type = 0;
- for(Block *block=current_block; block; block=next_block(*block))
+ members = 0;
+ for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
{
map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
if(i!=block->variables.end())
var.declaration = i->second;
- else
+ }
+
+ if(var.declaration)
+ {
+ if(var.declaration->type_declaration)
+ members = &var.declaration->type_declaration->members.variables;
+ }
+ else
+ {
+ const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
+ map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
+ if(i!=blocks.end() && i->second->instance_name==var.name)
{
- const set<InterfaceBlock *> &ifaces = block->interfaces;
- for(set<InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
- {
- i = (*j)->members.variables.find(var.name);
- if(i!=(*j)->members.variables.end())
- var.declaration = i->second;
- }
+ iface_ref = new InterfaceBlockReference;
+ iface_ref->name = var.name;
+ iface_ref->declaration = i->second;
+ members = &i->second->members.variables;
}
-
- if(var.declaration)
+ else
{
- type = var.declaration->type_declaration;
- break;
+ for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
+ if(i->second->instance_name.empty())
+ {
+ map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
+ if(j!=i->second->members.variables.end())
+ var.declaration = j->second;
+ }
}
}
self_referencing = true;
}
+void VariableResolver::visit(InterfaceBlockReference &iface)
+{
+ iface.declaration = 0;
+ for(Block *block=current_block; block; block=next_block(*block))
+ {
+ map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
+ if(i!=stage->interface_blocks.end())
+ {
+ iface.declaration = i->second;
+ members = &i->second->members.variables;
+ break;
+ }
+ }
+}
+
void VariableResolver::visit(MemberAccess &memacc)
{
- type = 0;
- TraversingVisitor::visit(memacc);
+ members = 0;
+ iface_ref = 0;
+ memacc.left->visit(*this);
+
+ if(iface_ref)
+ memacc.left = iface_ref;
+ iface_ref = 0;
+
memacc.declaration = 0;
- if(type)
+ if(members)
{
- map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
- if(i!=type->members.variables.end())
+ map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
+ if(i!=members->end())
{
memacc.declaration = i->second;
- type = i->second->type_declaration;
+ if(i->second->type_declaration)
+ members = &i->second->type_declaration->members.variables;
}
else
- type = 0;
+ members = 0;
}
}
SetForScope<bool> set(record_target, false);
binary.right->visit(*this);
}
- type = 0;
+ members = 0;
+ iface_ref = 0;
binary.left->visit(*this);
+ if(iface_ref)
+ binary.left = iface_ref;
+ iface_ref = 0;
}
else
{
TraversingVisitor::visit(binary);
- type = 0;
+ members = 0;
}
}
void VariableResolver::visit(StructDeclaration &strct)
{
TraversingVisitor::visit(strct);
- current_block->types[strct.name] = &strct;
+ stage->types[strct.name] = &strct;
}
void VariableResolver::visit(VariableDeclaration &var)
{
- for(Block *block=current_block; block; block=next_block(*block))
- {
- map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
- if(j!=block->types.end())
- var.type_declaration = j->second;
- }
+ map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
+ if(i!=stage->types.end())
+ var.type_declaration = i->second;
if(!block_interface.empty() && var.interface.empty())
var.interface = block_interface;
void VariableResolver::visit(InterfaceBlock &iface)
{
+ /* Block names can't be used for any other identifiers so we can put them
+ in the same map with instance names. */
+ stage->interface_blocks[iface.name] = &iface;
+ if(!iface.instance_name.empty())
+ stage->interface_blocks[iface.instance_name] = &iface;
+
SetForScope<string> set_iface(block_interface, iface.interface);
TraversingVisitor::visit(iface);
}
InterfaceGenerator::InterfaceGenerator():
- stage(0)
+ stage(0),
+ function_scope(false),
+ iface_block(0),
+ copy_block(false),
+ iface_target_block(0)
{ }
string InterfaceGenerator::get_out_prefix(Stage::Type type)
void InterfaceGenerator::apply(Stage &s)
{
stage = &s;
+ iface_target_block = &stage->content;
if(stage->previous)
in_prefix = get_out_prefix(stage->previous->type);
out_prefix = get_out_prefix(stage->type);
iface_var->type = var.type;
iface_var->type_declaration = var.type_declaration;
iface_var->name = name;
- if(stage->type==Stage::GEOMETRY)
+ if(stage->type==Stage::GEOMETRY && !copy_block)
iface_var->array = ((var.array && var.interface!="in") || iface=="in");
else
iface_var->array = var.array;
iface_var->array_size = var.array_size;
if(iface=="in")
{
+ iface_var->layout = var.layout;
iface_var->linked_declaration = &var;
var.linked_declaration = iface_var;
}
- stage->content.body.insert(iface_insert_point, iface_var);
- stage->content.variables[name] = iface_var;
+
+ iface_target_block->body.insert(iface_insert_point, iface_var);
+ iface_target_block->variables[name] = iface_var;
+
+ return true;
+}
+
+bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
+{
+ if(stage->interface_blocks.count(out_block.name))
+ return false;
+
+ InterfaceBlock *in_block = new InterfaceBlock;
+ in_block->interface = "in";
+ in_block->name = out_block.name;
+ in_block->instance_name = out_block.instance_name;
+ if(stage->type==Stage::GEOMETRY)
+ in_block->array = true;
+ else
+ in_block->array = out_block.array;
+ in_block->linked_block = &out_block;
+ out_block.linked_block = in_block;
+
+ {
+ SetFlag set_copy(copy_block, true);
+ SetForScope<Block *> set_target(iface_target_block, &in_block->members);
+ SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
+ out_block.members.visit(*this);
+ }
+
+ iface_target_block->body.insert(iface_insert_point, in_block);
+ stage->interface_blocks[in_block->name] = in_block;
+ if(!in_block->instance_name.empty())
+ stage->interface_blocks[in_block->instance_name] = in_block;
+
+ SetFlag set_scope(function_scope, false);
+ SetForScope<Block *> set_block(current_block, &stage->content);
+ in_block->visit(*this);
return true;
}
{
generate_interface(*i->second, "in", i->second->name);
var.name = i->second->name;
+ return;
}
+
+ const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
+ map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
+ if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
+ {
+ generate_interface(*j->second);
+ return;
+ }
+
+ for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
+ if(j->second->instance_name.empty())
+ {
+ i = j->second->members.variables.find(var.name);
+ if(i!=j->second->members.variables.end())
+ {
+ generate_interface(*j->second);
+ return;
+ }
+ }
}
void InterfaceGenerator::visit(VariableDeclaration &var)
{
+ if(copy_block)
+ {
+ generate_interface(var, "in", var.name);
+ return;
+ }
+
+ if(iface_block)
+ {
+ if(iface_block->linked_block)
+ {
+ const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
+ map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
+ if(i!=linked_vars.end())
+ var.linked_declaration = i->second;
+ }
+ return;
+ }
+
if(var.interface=="out")
{
- if(current_block!=&stage->content && generate_interface(var, "out", change_prefix(var.name, string())))
+ /* For out variables in function scope, generate a global interface and
+ replace the local declaration with an assignment. */
+ if(function_scope && generate_interface(var, "out", var.name))
{
nodes_to_remove.insert(&var);
if(var.init_expression)
}
else if(var.interface=="in")
{
- if(!var.linked_declaration && stage->previous)
+ /* Try to link in variables in global scope with out variables from
+ previous stage */
+ if(current_block==&stage->content && !var.linked_declaration && stage->previous)
{
const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
TraversingVisitor::visit(var);
}
+void InterfaceGenerator::visit(InterfaceBlock &iface)
+{
+ if(iface.interface=="in")
+ {
+ if(!iface.linked_block && stage->previous)
+ {
+ const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
+ map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
+ if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
+ {
+ iface.linked_block = i->second;
+ i->second->linked_block = &iface;
+ }
+ }
+ }
+
+ SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+ TraversingVisitor::visit(iface);
+}
+
void InterfaceGenerator::visit(FunctionDeclaration &func)
{
+ SetFlag set_scope(function_scope, true);
// Skip parameters because they're not useful here
func.body.visit(*this);
}
if(stage->type==Stage::GEOMETRY)
{
- VariableReference *ref = new VariableReference;
+ InterfaceBlockReference *ref = new InterfaceBlockReference;
ref->name = "gl_in";
BinaryExpression *subscript = new BinaryExpression;