+#include <algorithm>
#include <msp/core/hash.h>
#include <msp/core/raii.h>
#include <msp/strings/lexicalcast.h>
+#include <msp/strings/utils.h>
#include "builtin.h"
#include "generate.h"
void BlockHierarchyResolver::enter(Block &block)
{
+ r_any_resolved |= (current_block!=block.parent);
block.parent = current_block;
}
TypeResolver::TypeResolver():
- stage(0)
+ stage(0),
+ iface_block(0),
+ r_any_resolved(false)
{ }
-void TypeResolver::apply(Stage &s)
+bool TypeResolver::apply(Stage &s)
{
stage = &s;
s.types.clear();
+ r_any_resolved = false;
s.content.visit(*this);
+ return r_any_resolved;
}
-TypeDeclaration *TypeResolver::resolve_type(const string &name)
+TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
{
+ map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+ if(i!=array_types.end())
+ return i->second;
+
+ BasicTypeDeclaration *array = new BasicTypeDeclaration;
+ array->source = BUILTIN_SOURCE;
+ array->name = type.name+"[]";
+ array->kind = BasicTypeDeclaration::ARRAY;
+ array->base = type.name;
+ array->base_type = &type;
+ stage->content.body.insert(type_insert_point, array);
+ array_types[&type] = array;
+ return array;
+}
+
+void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
+{
+ TypeDeclaration *resolved = 0;
map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
if(i!=stage->types.end())
{
map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
- return (j!=alias_map.end() ? j->second : i->second);
+ resolved = (j!=alias_map.end() ? j->second : i->second);
}
- else
- return 0;
+
+ if(resolved && array)
+ resolved = get_or_create_array_type(*resolved);
+
+ r_any_resolved |= (resolved!=type);
+ type=resolved;
}
void TypeResolver::visit(Block &block)
void TypeResolver::visit(BasicTypeDeclaration &type)
{
- type.base_type = resolve_type(type.base);
+ resolve_type(type.base_type, type.base, false);
if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
if(basic_base->kind==BasicTypeDeclaration::VECTOR)
{
type.kind = BasicTypeDeclaration::MATRIX;
+ /* A matrix's base type is its column vector type. This will put
+ the column vector's size, i.e. the matrix's row count, in the high
+ half of the size. */
type.size |= basic_base->size<<16;
}
void TypeResolver::visit(ImageTypeDeclaration &type)
{
- type.base_type = resolve_type(type.base);
+ resolve_type(type.base_type, type.base, false);
stage->types.insert(make_pair(type.name, &type));
}
void TypeResolver::visit(VariableDeclaration &var)
{
- TypeDeclaration *type = resolve_type(var.type);
- if(var.array && type)
+ resolve_type(var.type_declaration, var.type, var.array);
+ if(iface_block && var.interface==iface_block->interface)
+ var.interface.clear();
+}
+
+void TypeResolver::visit(InterfaceBlock &iface)
+{
+ if(iface.members)
{
- map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
- if(i==array_types.end())
- {
- BasicTypeDeclaration *array = new BasicTypeDeclaration;
- array->source = BUILTIN_SOURCE;
- array->name = type->name+"[]";
- array->kind = BasicTypeDeclaration::ARRAY;
- array->base = type->name;
- array->base_type = type;
- stage->content.body.insert(type_insert_point, array);
- array->visit(*this);
- type = array;
- }
- else
- type = i->second;
+ SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+ iface.members->visit(*this);
+
+ StructDeclaration *strct = new StructDeclaration;
+ strct->source = INTERNAL_SOURCE;
+ strct->name = format("_%s_%s", iface.interface, iface.name);
+ strct->members.body.splice(strct->members.body.begin(), iface.members->body);
+ stage->content.body.insert(type_insert_point, strct);
+ stage->types.insert(make_pair(strct->name, strct));
+
+ iface.members = 0;
+ strct->interface_block = &iface;
+ iface.struct_declaration = strct;
}
- var.type_declaration = type;
+
+ TypeDeclaration *type = iface.struct_declaration;
+ if(type && iface.array)
+ type = get_or_create_array_type(*type);
+ r_any_resolved = (type!=iface.type_declaration);
+ iface.type_declaration = type;
}
void TypeResolver::visit(FunctionDeclaration &func)
{
- func.return_type_declaration = resolve_type(func.return_type);
+ resolve_type(func.return_type_declaration, func.return_type, false);
TraversingVisitor::visit(func);
}
stage(0),
r_any_resolved(false),
record_target(false),
- r_self_referencing(false),
- r_assignment_target(0)
+ r_self_referencing(false)
{ }
bool VariableResolver::apply(Stage &s)
block.variables.clear();
}
-void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
+void VariableResolver::visit(RefPtr<Expression> &expr)
{
r_replacement_expr = 0;
expr->visit(*this);
if(r_replacement_expr)
+ {
expr = r_replacement_expr;
+ /* Don't record assignment target when doing a replacement, because chain
+ information won't be correct. */
+ r_assignment_target.declaration = 0;
+ r_any_resolved = true;
+ }
r_replacement_expr = 0;
}
+void VariableResolver::check_assignment_target(Statement *declaration)
+{
+ if(record_target)
+ {
+ if(r_assignment_target.declaration)
+ {
+ /* More than one reference found in assignment target. Unable to
+ determine what the primary target is. */
+ record_target = false;
+ r_assignment_target.declaration = 0;
+ }
+ else
+ r_assignment_target.declaration = declaration;
+ }
+ // TODO This check is overly broad and may prevent some optimizations.
+ else if(declaration && declaration==r_assignment_target.declaration)
+ r_self_referencing = true;
+}
+
void VariableResolver::visit(VariableReference &var)
{
VariableDeclaration *declaration = 0;
{
// Look for the variable in anonymous interface blocks.
for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
- if(i->second->instance_name.empty())
+ if(i->second->instance_name.empty() && i->second->struct_declaration)
{
- map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
- if(j!=i->second->members.variables.end())
+ const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
+ map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
+ if(j!=iface_vars.end())
declaration = j->second;
}
}
r_any_resolved |= (declaration!=var.declaration);
var.declaration = declaration;
- if(record_target)
- {
- if(r_assignment_target)
- {
- /* More than one variable reference found in assignment target.
- Unable to determine what the primary target is. */
- record_target = false;
- r_assignment_target = 0;
- }
- else
- r_assignment_target = var.declaration;
- }
- else if(var.declaration && var.declaration==r_assignment_target)
- r_self_referencing = true;
+ check_assignment_target(var.declaration);
}
void VariableResolver::visit(InterfaceBlockReference &iface)
InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
r_any_resolved |= (declaration!=iface.declaration);
iface.declaration = declaration;
+
+ check_assignment_target(iface.declaration);
+}
+
+void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
+{
+ if(r_assignment_target.chain_len<7)
+ r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
+ ++r_assignment_target.chain_len;
}
void VariableResolver::visit(MemberAccess &memacc)
{
- visit_and_replace(memacc.left);
+ TraversingVisitor::visit(memacc);
- map<string, VariableDeclaration *> *members = 0;
+ VariableDeclaration *declaration = 0;
if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
- members = &strct->members.variables;
- else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
{
- if(iface_ref->declaration)
- members = &iface_ref->declaration->members.variables;
- }
+ map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
+ if(i!=strct->members.variables.end())
+ {
+ declaration = i->second;
- VariableDeclaration *declaration = 0;
- if(members)
+ if(record_target)
+ {
+ unsigned index = 0;
+ for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
+ ++index;
+
+ add_to_chain(Assignment::Target::MEMBER, index);
+ }
+ }
+ }
+ else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
{
- map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
- if(i!=members->end())
- declaration = i->second;
+ bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
+ bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
+ if(scalar_swizzle || vector_swizzle)
+ {
+ static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
+
+ bool ok = true;
+ UInt8 components[4] = { };
+ for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
+ ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
+
+ if(ok)
+ {
+ Swizzle *swizzle = new Swizzle;
+ swizzle->source = memacc.source;
+ swizzle->line = memacc.line;
+ swizzle->oper = memacc.oper;
+ swizzle->left = memacc.left;
+ swizzle->component_group = memacc.member;
+ swizzle->count = memacc.member.size();
+ copy(components, components+memacc.member.size(), swizzle->components);
+ r_replacement_expr = swizzle;
+ }
+ }
}
r_any_resolved |= (declaration!=memacc.declaration);
memacc.declaration = declaration;
}
-void VariableResolver::visit(UnaryExpression &unary)
+void VariableResolver::visit(Swizzle &swizzle)
{
- visit_and_replace(unary.expression);
+ TraversingVisitor::visit(swizzle);
+
+ if(record_target)
+ {
+ unsigned mask = 0;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ mask |= 1<<swizzle.components[i];
+ add_to_chain(Assignment::Target::SWIZZLE, mask);
+ }
}
void VariableResolver::visit(BinaryExpression &binary)
/* The subscript expression is not a part of the primary assignment
target. */
SetFlag set(record_target, false);
- visit_and_replace(binary.right);
+ visit(binary.right);
+ }
+ visit(binary.left);
+
+ if(record_target)
+ {
+ unsigned index = 0x3F;
+ if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
+ if(literal_subscript->value.check_type<int>())
+ index = literal_subscript->value.value<int>();
+ add_to_chain(Assignment::Target::ARRAY, index);
}
- visit_and_replace(binary.left);
}
else
- {
- visit_and_replace(binary.left);
- visit_and_replace(binary.right);
- }
+ TraversingVisitor::visit(binary);
}
void VariableResolver::visit(Assignment &assign)
{
{
SetFlag set(record_target);
- r_assignment_target = 0;
- visit_and_replace(assign.left);
- r_any_resolved |= (r_assignment_target!=assign.target_declaration);
- assign.target_declaration = r_assignment_target;
+ r_assignment_target = Assignment::Target();
+ visit(assign.left);
+ r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
+ assign.target = r_assignment_target;
}
r_self_referencing = false;
- visit_and_replace(assign.right);
+ visit(assign.right);
assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
}
-void VariableResolver::visit(FunctionCall &call)
-{
- for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
- visit_and_replace(*i);
-}
-
void VariableResolver::visit(VariableDeclaration &var)
{
- if(!block_interface.empty() && var.interface.empty())
- var.interface = block_interface;
-
TraversingVisitor::visit(var);
current_block->variables.insert(make_pair(var.name, &var));
}
if(!iface.instance_name.empty())
stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
- SetForScope<string> set_iface(block_interface, iface.interface);
TraversingVisitor::visit(iface);
}
bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
{
- if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
+ if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
{
BasicTypeDeclaration *to_type = &elem_type;
- if(is_vector_or_matrix(*expr_type))
- to_type = find_type(elem_type, expr_type->kind, expr_type->size);
+ if(is_vector_or_matrix(*expr_basic))
+ to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
if(to_type)
{
convert_to(expr, *to_type);
void ExpressionResolver::visit(InterfaceBlockReference &iface)
{
- resolve(iface, 0, true);
+ if(iface.declaration)
+ resolve(iface, iface.declaration->type_declaration, true);
}
void ExpressionResolver::visit(MemberAccess &memacc)
resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
}
+void ExpressionResolver::visit(Swizzle &swizzle)
+{
+ TraversingVisitor::visit(swizzle);
+
+ if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
+ {
+ BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
+ if(swizzle.count==1)
+ resolve(swizzle, left_elem, swizzle.left->lvalue);
+ else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
+ resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
+ }
+}
+
void ExpressionResolver::visit(UnaryExpression &unary)
{
TraversingVisitor::visit(unary);
}
-void FunctionResolver::apply(Stage &s)
+bool FunctionResolver::apply(Stage &s)
{
stage = &s;
s.functions.clear();
+ r_any_resolved = false;
s.content.visit(*this);
+ return r_any_resolved;
}
void FunctionResolver::visit(FunctionCall &call)
{
- map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
- if(i!=stage->functions.end())
- call.declaration = i->second;
+ string arg_types;
+ bool has_signature = true;
+ for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
+ {
+ if((*i)->type)
+ append(arg_types, ",", (*i)->type->name);
+ else
+ has_signature = false;
+ }
+
+ FunctionDeclaration *declaration = 0;
+ if(has_signature)
+ {
+ map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
+ declaration = (i!=stage->functions.end() ? i->second : 0);
+ }
+ r_any_resolved |= (declaration!=call.declaration);
+ call.declaration = declaration;
TraversingVisitor::visit(call);
}
void FunctionResolver::visit(FunctionDeclaration &func)
{
- FunctionDeclaration *&stage_decl = stage->functions[func.name];
- vector<FunctionDeclaration *> &decls = declarations[func.name];
+ if(func.signature.empty())
+ {
+ string param_types;
+ for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ {
+ if((*i)->type_declaration)
+ append(param_types, ",", (*i)->type_declaration->name);
+ else
+ return;
+ }
+ func.signature = format("(%s)", param_types);
+ r_any_resolved = true;
+ }
+
+ string key = func.name+func.signature;
+ FunctionDeclaration *&stage_decl = stage->functions[key];
+ vector<FunctionDeclaration *> &decls = declarations[key];
if(func.definition==&func)
{
stage_decl = &func;
// Set all previous declarations to use this definition.
for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
{
+ r_any_resolved |= (func.definition!=(*i)->definition);
(*i)->definition = func.definition;
(*i)->body.body.clear();
}
}
else
{
- func.definition = 0;
+ FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
+ r_any_resolved |= (definition!=func.definition);
+ func.definition = definition;
+
if(!stage_decl)
stage_decl = &func;
- else
- func.definition = stage_decl->definition;
}
decls.push_back(&func);
InterfaceGenerator::InterfaceGenerator():
stage(0),
function_scope(false),
- iface_block(0),
copy_block(false),
iface_target_block(0)
{ }
if(stage->content.variables.count(name))
return 0;
+ if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
+ return 0;
+
VariableDeclaration* iface_var = new VariableDeclaration;
iface_var->sampling = var.sampling;
iface_var->interface = iface;
iface_target_block->body.insert(iface_insert_point, iface_var);
iface_target_block->variables.insert(make_pair(name, iface_var));
+ if(iface_target_block==&stage->content && iface=="in")
+ declared_inputs.push_back(iface_var);
return iface_var;
}
InterfaceBlock *in_block = new InterfaceBlock;
in_block->interface = "in";
in_block->name = out_block.name;
+ in_block->members = new Block;
in_block->instance_name = out_block.instance_name;
if(stage->type==Stage::GEOMETRY)
in_block->array = true;
{
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);
+ SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
+ SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
+ if(out_block.struct_declaration)
+ out_block.struct_declaration->members.visit(*this);
+ else if(out_block.members)
+ out_block.members->visit(*this);
}
iface_target_block->body.insert(iface_insert_point, in_block);
i = prev_vars.find(in_prefix+var.name);
if(i!=prev_vars.end() && i->second->interface=="out")
{
- generate_interface(*i->second, "in", i->second->name);
- var.name = i->second->name;
+ if(stage->type==Stage::GEOMETRY && i->second->array)
+ stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+ format("Can't access '%s' through automatic interface because it's an array", var.name)));
+ else
+ {
+ generate_interface(*i->second, "in", i->second->name);
+ var.name = i->second->name;
+ }
return;
}
}
for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
- if(j->second->instance_name.empty())
+ if(j->second->instance_name.empty() && j->second->struct_declaration)
{
- i = j->second->members.variables.find(var.name);
- if(i!=j->second->members.variables.end())
+ const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
+ i = iface_vars.find(var.name);
+ if(i!=iface_vars.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)
- {
- // Link all variables to their counterparts in the 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;
- var.linked_declaration->linked_declaration = &var;
- }
- }
- return;
- }
-
- if(var.interface=="out")
+ else if(var.interface=="out")
{
/* For output variables in function scope, generate a global interface
and replace the local declaration with an assignment. */
}
}
}
- else if(var.interface=="in")
+ else if(var.interface=="in" && current_block==&stage->content)
{
+ declared_inputs.push_back(&var);
+
/* Try to link input variables in global scope with output variables from
previous stage. */
- if(current_block==&stage->content && !var.linked_declaration && stage->previous)
+ if(!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);
}
}
- SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
TraversingVisitor::visit(iface);
}
void InterfaceGenerator::visit(Passthrough &pass)
{
- vector<VariableDeclaration *> pass_vars;
-
- // Pass through all input variables of this stage.
- for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
- if(i->second->interface=="in")
- pass_vars.push_back(i->second);
+ // Pass through all input variables declared so far.
+ vector<VariableDeclaration *> pass_vars = declared_inputs;
if(stage->previous)
{