2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/lexicalcast.h>
14 void DeclarationCombiner::apply(Stage &stage)
16 stage.content.visit(*this);
17 NodeRemover().apply(stage, nodes_to_remove);
20 void DeclarationCombiner::visit(Block &block)
25 TraversingVisitor::visit(block);
28 void DeclarationCombiner::visit(VariableDeclaration &var)
30 VariableDeclaration *&ptr = variables[var.name];
34 if(var.init_expression)
35 ptr->init_expression = var.init_expression;
40 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
43 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
46 j->has_value = i->value;
52 ptr->layout->qualifiers.push_back(*i);
56 ptr->layout = var.layout;
58 nodes_to_remove.insert(&var);
65 ConstantSpecializer::ConstantSpecializer():
69 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
72 stage.content.visit(*this);
75 void ConstantSpecializer::visit(VariableDeclaration &var)
77 bool specializable = false;
80 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
81 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
82 if(i->name=="constant_id")
88 i->value = hash32(var.name)&0x7FFFFFFF;
92 if(qualifiers.empty())
96 if(specializable && values)
98 map<string, int>::const_iterator i = values->find(var.name);
101 RefPtr<Literal> literal = new Literal;
104 literal->token = (i->second ? "true" : "false");
105 literal->value = static_cast<bool>(i->second);
107 else if(var.type=="int")
109 literal->token = lexical_cast<string>(i->second);
110 literal->value = i->second;
112 var.init_expression = literal;
118 void BlockHierarchyResolver::enter(Block &block)
120 r_any_resolved |= (current_block!=block.parent);
121 block.parent = current_block;
125 TypeResolver::TypeResolver():
128 r_any_resolved(false)
131 bool TypeResolver::apply(Stage &s)
135 r_any_resolved = false;
136 s.content.visit(*this);
137 return r_any_resolved;
140 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
142 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
143 if(i!=array_types.end())
146 BasicTypeDeclaration *array = new BasicTypeDeclaration;
147 array->source = BUILTIN_SOURCE;
148 array->name = type.name+"[]";
149 array->kind = BasicTypeDeclaration::ARRAY;
150 array->base = type.name;
151 array->base_type = &type;
152 stage->content.body.insert(type_insert_point, array);
153 array_types[&type] = array;
157 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
159 TypeDeclaration *resolved = 0;
160 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
161 if(i!=stage->types.end())
163 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
164 resolved = (j!=alias_map.end() ? j->second : i->second);
167 if(resolved && array)
168 resolved = get_or_create_array_type(*resolved);
170 r_any_resolved |= (resolved!=type);
174 void TypeResolver::visit(Block &block)
176 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
179 type_insert_point = i;
184 void TypeResolver::visit(BasicTypeDeclaration &type)
186 resolve_type(type.base_type, type.base, false);
188 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
189 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
190 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
192 type.kind = BasicTypeDeclaration::MATRIX;
193 type.size |= basic_base->size<<16;
196 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
197 alias_map[&type] = type.base_type;
198 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
199 array_types[type.base_type] = &type;
201 stage->types.insert(make_pair(type.name, &type));
204 void TypeResolver::visit(ImageTypeDeclaration &type)
206 resolve_type(type.base_type, type.base, false);
207 stage->types.insert(make_pair(type.name, &type));
210 void TypeResolver::visit(StructDeclaration &strct)
212 stage->types.insert(make_pair(strct.name, &strct));
213 TraversingVisitor::visit(strct);
216 void TypeResolver::visit(VariableDeclaration &var)
218 resolve_type(var.type_declaration, var.type, var.array);
219 if(iface_block && var.interface==iface_block->interface)
220 var.interface.clear();
223 void TypeResolver::visit(InterfaceBlock &iface)
227 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
228 iface.members->visit(*this);
230 StructDeclaration *strct = new StructDeclaration;
231 strct->source = INTERNAL_SOURCE;
232 strct->name = format("_%s_%s", iface.interface, iface.name);
233 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
234 stage->content.body.insert(type_insert_point, strct);
235 stage->types.insert(make_pair(strct->name, strct));
238 strct->interface_block = &iface;
239 iface.struct_declaration = strct;
242 TypeDeclaration *type = iface.struct_declaration;
243 if(type && iface.array)
244 type = get_or_create_array_type(*type);
245 r_any_resolved = (type!=iface.type_declaration);
246 iface.type_declaration = type;
249 void TypeResolver::visit(FunctionDeclaration &func)
251 resolve_type(func.return_type_declaration, func.return_type, false);
252 TraversingVisitor::visit(func);
256 VariableResolver::VariableResolver():
258 r_any_resolved(false),
259 record_target(false),
260 r_self_referencing(false)
263 bool VariableResolver::apply(Stage &s)
266 s.interface_blocks.clear();
267 r_any_resolved = false;
268 s.content.visit(*this);
269 return r_any_resolved;
272 void VariableResolver::enter(Block &block)
274 block.variables.clear();
277 void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
279 r_replacement_expr = 0;
281 if(r_replacement_expr)
283 expr = r_replacement_expr;
284 /* Don't record assignment target when doing a replacement, because chain
285 information won't be correct. */
286 r_assignment_target.declaration = 0;
287 r_any_resolved = true;
289 r_replacement_expr = 0;
292 void VariableResolver::check_assignment_target(Statement *declaration)
296 if(r_assignment_target.declaration)
298 /* More than one reference found in assignment target. Unable to
299 determine what the primary target is. */
300 record_target = false;
301 r_assignment_target.declaration = 0;
304 r_assignment_target.declaration = declaration;
306 // TODO This check is overly broad and may prevent some optimizations.
307 else if(declaration && declaration==r_assignment_target.declaration)
308 r_self_referencing = true;
311 void VariableResolver::visit(VariableReference &var)
313 VariableDeclaration *declaration = 0;
315 /* Look for variable declarations in the block hierarchy first. Interface
316 blocks are always defined in the top level so we can't accidentally skip
318 for(Block *block=current_block; (!declaration && block); block=block->parent)
320 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
321 if(i!=block->variables.end())
322 declaration = i->second;
327 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
328 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
331 /* The name refers to an interface block with an instance name rather
332 than a variable. Prepare a new syntax tree node accordingly. */
333 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
334 iface_ref->source = var.source;
335 iface_ref->line = var.line;
336 iface_ref->name = var.name;
337 iface_ref->declaration = i->second;
338 r_replacement_expr = iface_ref;
342 // Look for the variable in anonymous interface blocks.
343 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
344 if(i->second->instance_name.empty() && i->second->struct_declaration)
346 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
347 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
348 if(j!=iface_vars.end())
349 declaration = j->second;
354 r_any_resolved |= (declaration!=var.declaration);
355 var.declaration = declaration;
357 check_assignment_target(var.declaration);
360 void VariableResolver::visit(InterfaceBlockReference &iface)
362 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
363 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
364 r_any_resolved |= (declaration!=iface.declaration);
365 iface.declaration = declaration;
367 check_assignment_target(iface.declaration);
370 void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
372 if(r_assignment_target.chain_len<7)
373 r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
374 ++r_assignment_target.chain_len;
377 void VariableResolver::visit(MemberAccess &memacc)
379 visit_and_replace(memacc.left);
381 VariableDeclaration *declaration = 0;
382 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
384 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
385 if(i!=strct->members.variables.end())
387 declaration = i->second;
392 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
395 add_to_chain(Assignment::Target::MEMBER, index);
399 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
401 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
402 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
403 if(scalar_swizzle || vector_swizzle)
405 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
408 UInt8 components[4] = { };
409 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
410 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
414 Swizzle *swizzle = new Swizzle;
415 swizzle->source = memacc.source;
416 swizzle->line = memacc.line;
417 swizzle->oper = memacc.oper;
418 swizzle->left = memacc.left;
419 swizzle->component_group = memacc.member;
420 swizzle->count = memacc.member.size();
421 copy(components, components+memacc.member.size(), swizzle->components);
422 r_replacement_expr = swizzle;
427 r_any_resolved |= (declaration!=memacc.declaration);
428 memacc.declaration = declaration;
431 void VariableResolver::visit(Swizzle &swizzle)
433 visit_and_replace(swizzle.left);
438 for(unsigned i=0; i<swizzle.count; ++i)
439 mask |= 1<<swizzle.components[i];
440 add_to_chain(Assignment::Target::SWIZZLE, mask);
444 void VariableResolver::visit(UnaryExpression &unary)
446 visit_and_replace(unary.expression);
449 void VariableResolver::visit(BinaryExpression &binary)
451 if(binary.oper->token[0]=='[')
454 /* The subscript expression is not a part of the primary assignment
456 SetFlag set(record_target, false);
457 visit_and_replace(binary.right);
459 visit_and_replace(binary.left);
463 unsigned index = 0x3F;
464 if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
465 if(literal_subscript->value.check_type<int>())
466 index = literal_subscript->value.value<int>();
467 add_to_chain(Assignment::Target::ARRAY, index);
472 visit_and_replace(binary.left);
473 visit_and_replace(binary.right);
477 void VariableResolver::visit(Assignment &assign)
480 SetFlag set(record_target);
481 r_assignment_target = Assignment::Target();
482 visit_and_replace(assign.left);
483 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
484 assign.target = r_assignment_target;
487 r_self_referencing = false;
488 visit_and_replace(assign.right);
489 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
492 void VariableResolver::visit(FunctionCall &call)
494 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
495 visit_and_replace(*i);
498 void VariableResolver::visit(VariableDeclaration &var)
501 var.layout->visit(*this);
503 visit_and_replace(var.array_size);
504 if(var.init_expression)
505 visit_and_replace(var.init_expression);
506 current_block->variables.insert(make_pair(var.name, &var));
509 void VariableResolver::visit(InterfaceBlock &iface)
511 /* Block names can be reused in different interfaces. Prefix the name with
512 the first character of the interface to avoid conflicts. */
513 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
514 if(!iface.instance_name.empty())
515 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
517 TraversingVisitor::visit(iface);
521 ExpressionResolver::ExpressionResolver():
523 r_any_resolved(false)
526 bool ExpressionResolver::apply(Stage &s)
529 r_any_resolved = false;
530 s.content.visit(*this);
531 return r_any_resolved;
534 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
536 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
539 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
541 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
544 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
546 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
548 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
549 return (basic_base ? get_element_type(*basic_base) : 0);
555 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
557 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
558 return from.size<=to.size;
559 else if(from.kind!=to.kind)
561 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
563 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
564 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
565 return (from_base && to_base && can_convert(*from_base, *to_base));
571 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
575 else if(can_convert(left, right))
576 return LEFT_CONVERTIBLE;
577 else if(can_convert(right, left))
578 return RIGHT_CONVERTIBLE;
580 return NOT_COMPATIBLE;
583 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
585 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
586 if((*i)->kind==kind && (*i)->size==size)
591 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
593 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
594 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
599 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
601 RefPtr<FunctionCall> call = new FunctionCall;
602 call->name = type.name;
603 call->constructor = true;
604 call->arguments.push_back(0);
605 call->arguments.back() = expr;
610 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
612 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
614 BasicTypeDeclaration *to_type = &elem_type;
615 if(is_vector_or_matrix(*expr_type))
616 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
619 convert_to(expr, *to_type);
627 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
629 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
631 expr.lvalue = lvalue;
634 void ExpressionResolver::visit(Literal &literal)
636 if(literal.value.check_type<bool>())
637 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
638 else if(literal.value.check_type<int>())
639 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
640 else if(literal.value.check_type<float>())
641 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
644 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
646 TraversingVisitor::visit(parexpr);
647 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
650 void ExpressionResolver::visit(VariableReference &var)
653 resolve(var, var.declaration->type_declaration, true);
656 void ExpressionResolver::visit(InterfaceBlockReference &iface)
658 if(iface.declaration)
659 resolve(iface, iface.declaration->type_declaration, true);
662 void ExpressionResolver::visit(MemberAccess &memacc)
664 TraversingVisitor::visit(memacc);
666 if(memacc.declaration)
667 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
670 void ExpressionResolver::visit(Swizzle &swizzle)
672 TraversingVisitor::visit(swizzle);
674 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
676 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
678 resolve(swizzle, left_elem, swizzle.left->lvalue);
679 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
680 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
684 void ExpressionResolver::visit(UnaryExpression &unary)
686 TraversingVisitor::visit(unary);
688 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
692 char oper = unary.oper->token[0];
695 if(basic->kind!=BasicTypeDeclaration::BOOL)
700 if(basic->kind!=BasicTypeDeclaration::INT)
703 else if(oper=='+' || oper=='-')
705 BasicTypeDeclaration *elem = get_element_type(*basic);
706 if(!elem || !is_scalar(*elem))
709 resolve(unary, basic, unary.expression->lvalue);
712 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
714 /* Binary operators are only defined for basic types (not for image or
716 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
717 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
718 if(!basic_left || !basic_right)
721 char oper = binary.oper->token[0];
724 /* Subscripting operates on vectors, matrices and arrays, and the right
725 operand must be an integer. */
726 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
729 resolve(binary, basic_left->base_type, binary.left->lvalue);
732 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
733 // No other binary operator can be used with arrays.
736 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
737 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
738 if(!elem_left || !elem_right)
741 Compatibility compat = get_compatibility(*basic_left, *basic_right);
742 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
743 if(elem_compat==NOT_COMPATIBLE)
745 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
748 TypeDeclaration *type = 0;
749 char oper2 = binary.oper->token[1];
750 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
752 /* Relational operators compare two scalar integer or floating-point
754 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
757 type = find_type(BasicTypeDeclaration::BOOL, 1);
759 else if((oper=='=' || oper=='!') && oper2=='=')
761 // Equality comparison can be done on any compatible types.
762 if(compat==NOT_COMPATIBLE)
765 type = find_type(BasicTypeDeclaration::BOOL, 1);
767 else if(oper2=='&' || oper2=='|' || oper2=='^')
769 // Logical operators can only be applied to booleans.
770 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
775 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
777 // Bitwise operators and modulo can only be applied to integers.
778 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
781 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
783 else if((oper=='<' || oper=='>') && oper2==oper)
785 // Shifts apply to integer scalars and vectors, with some restrictions.
786 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
788 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
789 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
790 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
794 // Don't perform conversion even if the operands are of different sizes.
797 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
799 // Arithmetic operators require scalar elements.
800 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
803 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
804 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
806 /* Multiplication has special rules when at least one operand is a
807 matrix and the other is a vector or a matrix. */
808 unsigned left_columns = basic_left->size&0xFFFF;
809 unsigned right_rows = basic_right->size;
810 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
812 if(left_columns!=right_rows)
815 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
817 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
818 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
819 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
820 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
822 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
824 else if(compat==NOT_COMPATIBLE)
826 // Arithmetic between scalars and matrices or vectors is supported.
827 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
828 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
829 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
830 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
834 else if(compat==LEFT_CONVERTIBLE)
842 if(assign && type!=basic_left)
845 bool converted = true;
846 if(compat==LEFT_CONVERTIBLE)
847 convert_to(binary.left, *basic_right);
848 else if(compat==RIGHT_CONVERTIBLE)
849 convert_to(binary.right, *basic_left);
850 else if(elem_compat==LEFT_CONVERTIBLE)
851 converted = convert_to_element(binary.left, *elem_right);
852 else if(elem_compat==RIGHT_CONVERTIBLE)
853 converted = convert_to_element(binary.right, *elem_left);
858 resolve(binary, type, assign);
861 void ExpressionResolver::visit(BinaryExpression &binary)
863 TraversingVisitor::visit(binary);
864 visit(binary, false);
867 void ExpressionResolver::visit(Assignment &assign)
869 TraversingVisitor::visit(assign);
871 if(assign.oper->token[0]!='=')
872 return visit(assign, true);
873 else if(assign.left->type!=assign.right->type)
875 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
876 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
877 if(!basic_left || !basic_right)
880 Compatibility compat = get_compatibility(*basic_left, *basic_right);
881 if(compat==RIGHT_CONVERTIBLE)
882 convert_to(assign.right, *basic_left);
883 else if(compat!=SAME_TYPE)
887 resolve(assign, assign.left->type, true);
890 void ExpressionResolver::visit(FunctionCall &call)
892 TraversingVisitor::visit(call);
894 TypeDeclaration *type = 0;
896 type = call.declaration->return_type_declaration;
897 else if(call.constructor)
899 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
900 type = (i!=stage->types.end() ? i->second : 0);
902 resolve(call, type, false);
905 void ExpressionResolver::visit(BasicTypeDeclaration &type)
907 basic_types.push_back(&type);
910 void ExpressionResolver::visit(VariableDeclaration &var)
912 TraversingVisitor::visit(var);
913 if(!var.init_expression)
916 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
917 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
918 if(!var_basic || !init_basic)
921 Compatibility compat = get_compatibility(*var_basic, *init_basic);
922 if(compat==RIGHT_CONVERTIBLE)
923 convert_to(var.init_expression, *var_basic);
927 bool FunctionResolver::apply(Stage &s)
931 r_any_resolved = false;
932 s.content.visit(*this);
933 return r_any_resolved;
936 void FunctionResolver::visit(FunctionCall &call)
938 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
939 if(i!=stage->functions.end())
940 call.declaration = i->second;
942 TraversingVisitor::visit(call);
945 void FunctionResolver::visit(FunctionDeclaration &func)
947 FunctionDeclaration *&stage_decl = stage->functions[func.name];
948 vector<FunctionDeclaration *> &decls = declarations[func.name];
949 if(func.definition==&func)
953 // Set all previous declarations to use this definition.
954 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
956 (*i)->definition = func.definition;
957 (*i)->body.body.clear();
966 func.definition = stage_decl->definition;
968 decls.push_back(&func);
970 TraversingVisitor::visit(func);
974 InterfaceGenerator::InterfaceGenerator():
976 function_scope(false),
978 iface_target_block(0)
981 string InterfaceGenerator::get_out_prefix(Stage::Type type)
983 if(type==Stage::VERTEX)
985 else if(type==Stage::GEOMETRY)
991 void InterfaceGenerator::apply(Stage &s)
994 iface_target_block = &stage->content;
996 in_prefix = get_out_prefix(stage->previous->type);
997 out_prefix = get_out_prefix(stage->type);
998 s.content.visit(*this);
999 NodeRemover().apply(s, nodes_to_remove);
1002 void InterfaceGenerator::visit(Block &block)
1004 SetForScope<Block *> set_block(current_block, &block);
1005 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1007 assignment_insert_point = i;
1008 if(&block==&stage->content)
1009 iface_insert_point = i;
1015 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
1017 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
1018 return prefix+name.substr(offset);
1021 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
1023 if(stage->content.variables.count(name))
1026 VariableDeclaration* iface_var = new VariableDeclaration;
1027 iface_var->sampling = var.sampling;
1028 iface_var->interface = iface;
1029 iface_var->type = var.type;
1030 iface_var->name = name;
1031 /* Geometry shader inputs are always arrays. But if we're bringing in an
1032 entire block, the array is on the block and not individual variables. */
1033 if(stage->type==Stage::GEOMETRY && !copy_block)
1034 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
1036 iface_var->array = var.array;
1037 if(iface_var->array)
1038 iface_var->array_size = var.array_size;
1041 iface_var->layout = var.layout;
1042 iface_var->linked_declaration = &var;
1043 var.linked_declaration = iface_var;
1046 iface_target_block->body.insert(iface_insert_point, iface_var);
1047 iface_target_block->variables.insert(make_pair(name, iface_var));
1052 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1054 if(stage->interface_blocks.count("in"+out_block.name))
1057 InterfaceBlock *in_block = new InterfaceBlock;
1058 in_block->interface = "in";
1059 in_block->name = out_block.name;
1060 in_block->members = new Block;
1061 in_block->instance_name = out_block.instance_name;
1062 if(stage->type==Stage::GEOMETRY)
1063 in_block->array = true;
1065 in_block->array = out_block.array;
1066 in_block->linked_block = &out_block;
1067 out_block.linked_block = in_block;
1070 SetFlag set_copy(copy_block, true);
1071 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1072 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1073 if(out_block.struct_declaration)
1074 out_block.struct_declaration->members.visit(*this);
1075 else if(out_block.members)
1076 out_block.members->visit(*this);
1079 iface_target_block->body.insert(iface_insert_point, in_block);
1080 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
1081 if(!in_block->instance_name.empty())
1082 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1084 SetFlag set_scope(function_scope, false);
1085 SetForScope<Block *> set_block(current_block, &stage->content);
1086 in_block->visit(*this);
1091 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1093 Assignment *assign = new Assignment;
1094 VariableReference *ref = new VariableReference;
1097 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1098 assign->right = right;
1100 ExpressionStatement *stmt = new ExpressionStatement;
1101 stmt->expression = assign;
1102 current_block->body.insert(assignment_insert_point, stmt);
1108 void InterfaceGenerator::visit(VariableReference &var)
1110 if(var.declaration || !stage->previous)
1112 /* Don't pull a variable from previous stage if we just generated an output
1113 interface in this stage */
1114 if(stage->content.variables.count(var.name))
1117 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1118 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1119 if(i==prev_vars.end() || i->second->interface!="out")
1120 i = prev_vars.find(in_prefix+var.name);
1121 if(i!=prev_vars.end() && i->second->interface=="out")
1123 generate_interface(*i->second, "in", i->second->name);
1124 var.name = i->second->name;
1128 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1129 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1130 if(j!=prev_blocks.end() && j->second->interface=="out")
1132 generate_interface(*j->second);
1133 /* Let VariableResolver convert the variable reference into an interface
1138 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1139 if(j->second->instance_name.empty() && j->second->struct_declaration)
1141 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1142 i = iface_vars.find(var.name);
1143 if(i!=iface_vars.end())
1145 generate_interface(*j->second);
1151 void InterfaceGenerator::visit(VariableDeclaration &var)
1154 generate_interface(var, "in", var.name);
1155 else if(var.interface=="out")
1157 /* For output variables in function scope, generate a global interface
1158 and replace the local declaration with an assignment. */
1159 VariableDeclaration *out_var = 0;
1160 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1162 out_var->source = var.source;
1163 out_var->line = var.line;
1164 nodes_to_remove.insert(&var);
1165 if(var.init_expression)
1167 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1168 stmt.source = var.source;
1169 stmt.line = var.line;
1174 else if(var.interface=="in")
1176 /* Try to link input variables in global scope with output variables from
1178 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1180 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1181 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1182 if(i!=prev_vars.end() && i->second->interface=="out")
1184 var.linked_declaration = i->second;
1185 i->second->linked_declaration = &var;
1190 TraversingVisitor::visit(var);
1193 void InterfaceGenerator::visit(InterfaceBlock &iface)
1195 if(iface.interface=="in")
1197 /* Try to link input blocks with output blocks sharing the same block
1198 name from previous stage. */
1199 if(!iface.linked_block && stage->previous)
1201 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1202 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1203 if(i!=prev_blocks.end())
1205 iface.linked_block = i->second;
1206 i->second->linked_block = &iface;
1211 TraversingVisitor::visit(iface);
1214 void InterfaceGenerator::visit(FunctionDeclaration &func)
1216 SetFlag set_scope(function_scope, true);
1217 // Skip parameters because they're not useful here
1218 func.body.visit(*this);
1221 void InterfaceGenerator::visit(Passthrough &pass)
1223 vector<VariableDeclaration *> pass_vars;
1225 // Pass through all input variables of this stage.
1226 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1227 if(i->second->interface=="in")
1228 pass_vars.push_back(i->second);
1232 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1233 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1235 if(i->second->interface!="out")
1238 /* Pass through output variables from the previous stage, but only
1239 those which are not already linked to an input here. */
1240 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1241 pass_vars.push_back(i->second);
1245 if(stage->type==Stage::GEOMETRY)
1247 /* Special case for geometry shader: copy gl_Position from input to
1249 InterfaceBlockReference *ref = new InterfaceBlockReference;
1250 ref->name = "gl_in";
1252 BinaryExpression *subscript = new BinaryExpression;
1253 subscript->left = ref;
1254 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1255 subscript->right = pass.subscript;
1257 MemberAccess *memacc = new MemberAccess;
1258 memacc->left = subscript;
1259 memacc->member = "gl_Position";
1261 insert_assignment("gl_Position", memacc);
1264 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1266 string out_name = change_prefix((*i)->name, out_prefix);
1267 generate_interface(**i, "out", out_name);
1269 VariableReference *ref = new VariableReference;
1270 ref->name = (*i)->name;
1273 BinaryExpression *subscript = new BinaryExpression;
1274 subscript->left = ref;
1275 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1276 subscript->right = pass.subscript;
1277 insert_assignment(out_name, subscript);
1280 insert_assignment(out_name, ref);
1283 nodes_to_remove.insert(&pass);