X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=1c8af82aa88b0b6e0426749a793aae55c4174532;hb=085ebad01690fafedeafe4bfe8421c9588f0238e;hp=7c619830c1733f43160323172ff19605a69632e4;hpb=68af3c2b81cfe9a780bc2637d56e906b066ccaf4;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 7c619830..1c8af82a 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -99,9 +99,15 @@ void ConstantSpecializer::visit(VariableDeclaration &var) { RefPtr literal = new Literal; if(var.type=="bool") + { literal->token = (i->second ? "true" : "false"); + literal->value = static_cast(i->second); + } else if(var.type=="int") + { literal->token = lexical_cast(i->second); + literal->value = i->second; + } var.init_expression = literal; } } @@ -114,6 +120,102 @@ void BlockHierarchyResolver::enter(Block &block) } +TypeResolver::TypeResolver(): + stage(0) +{ } + +void TypeResolver::apply(Stage &s) +{ + stage = &s; + s.types.clear(); + s.content.visit(*this); +} + +TypeDeclaration *TypeResolver::resolve_type(const string &name) +{ + map::iterator i = stage->types.find(name); + if(i!=stage->types.end()) + { + map::iterator j = alias_map.find(i->second); + return (j!=alias_map.end() ? j->second : i->second); + } + else + return 0; +} + +void TypeResolver::visit(Block &block) +{ + for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + { + if(!block.parent) + type_insert_point = i; + (*i)->visit(*this); + } +} + +void TypeResolver::visit(BasicTypeDeclaration &type) +{ + type.base_type = resolve_type(type.base); + + if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type) + if(BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type)) + if(basic_base->kind==BasicTypeDeclaration::VECTOR) + { + type.kind = BasicTypeDeclaration::MATRIX; + type.size |= basic_base->size<<16; + } + + if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type) + alias_map[&type] = type.base_type; + else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type) + array_types[type.base_type] = &type; + + stage->types.insert(make_pair(type.name, &type)); +} + +void TypeResolver::visit(ImageTypeDeclaration &type) +{ + type.base_type = resolve_type(type.base); + stage->types.insert(make_pair(type.name, &type)); +} + +void TypeResolver::visit(StructDeclaration &strct) +{ + stage->types.insert(make_pair(strct.name, &strct)); + TraversingVisitor::visit(strct); +} + +void TypeResolver::visit(VariableDeclaration &var) +{ + TypeDeclaration *type = resolve_type(var.type); + if(var.array && type) + { + map::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; + } + var.type_declaration = type; +} + +void TypeResolver::visit(FunctionDeclaration &func) +{ + func.return_type_declaration = resolve_type(func.return_type); + TraversingVisitor::visit(func); +} + + VariableResolver::VariableResolver(): stage(0), r_members(0), @@ -125,7 +227,6 @@ VariableResolver::VariableResolver(): void VariableResolver::apply(Stage &s) { stage = &s; - s.types.clear(); s.interface_blocks.clear(); s.content.visit(*this); } @@ -149,20 +250,17 @@ void VariableResolver::visit(VariableReference &var) var.declaration = i->second; } - if(var.declaration) - { - if(var.declaration->type_declaration) - r_members = &var.declaration->type_declaration->members.variables; - } - else + if(!var.declaration) { const map &blocks = stage->interface_blocks; - map::const_iterator i = blocks.find(var.name); - if(i!=blocks.end() && i->second->instance_name==var.name) + map::const_iterator i = blocks.find("_"+var.name); + if(i!=blocks.end()) { /* The name refers to an interface block with an instance name rather than a variable. Prepare a new syntax tree node accordingly. */ r_iface_ref = new InterfaceBlockReference; + r_iface_ref->source = var.source; + r_iface_ref->line = var.line; r_iface_ref->name = var.name; r_iface_ref->declaration = i->second; r_members = &i->second->members.variables; @@ -180,6 +278,10 @@ void VariableResolver::visit(VariableReference &var) } } + if(var.declaration) + if(StructDeclaration *strct = dynamic_cast(var.declaration->type_declaration)) + r_members = &strct->members.variables; + if(record_target) { if(r_assignment_target) @@ -199,15 +301,11 @@ void VariableResolver::visit(VariableReference &var) void VariableResolver::visit(InterfaceBlockReference &iface) { iface.declaration = 0; - for(Block *block=current_block; block; block=block->parent) + map::iterator i = stage->interface_blocks.find("_"+iface.name); + if(i!=stage->interface_blocks.end()) { - map::iterator i = stage->interface_blocks.find(iface.name); - if(i!=stage->interface_blocks.end()) - { - iface.declaration = i->second; - r_members = &i->second->members.variables; - break; - } + iface.declaration = i->second; + r_members = &i->second->members.variables; } } @@ -228,8 +326,8 @@ void VariableResolver::visit(MemberAccess &memacc) if(i!=r_members->end()) { memacc.declaration = i->second; - if(i->second->type_declaration) - r_members = &i->second->type_declaration->members.variables; + if(StructDeclaration *strct = dynamic_cast(i->second->type_declaration)) + r_members = &strct->members.variables; } else r_members = 0; @@ -292,38 +390,371 @@ void VariableResolver::visit(FunctionCall &call) r_iface_ref = 0; } -void VariableResolver::visit(StructDeclaration &strct) -{ - TraversingVisitor::visit(strct); - stage->types[strct.name] = &strct; -} - void VariableResolver::visit(VariableDeclaration &var) { - map::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; TraversingVisitor::visit(var); - current_block->variables[var.name] = &var; + current_block->variables.insert(make_pair(var.name, &var)); } 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; + /* Block names can be reused in different interfaces. Prefix the name with + the first character of the interface to avoid conflicts. */ + stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface)); if(!iface.instance_name.empty()) - stage->interface_blocks[iface.instance_name] = &iface; + stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface)); SetForScope set_iface(block_interface, iface.interface); TraversingVisitor::visit(iface); } +bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT); +} + +bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX); +} + +BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type) +{ + if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY) + { + BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type); + return (basic_base ? get_element_type(*basic_base) : 0); + } + else + return &type; +} + +bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to) +{ + if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT) + return from.size<=to.size; + else if(from.kind!=to.kind) + return false; + else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size) + { + BasicTypeDeclaration *from_base = dynamic_cast(from.base_type); + BasicTypeDeclaration *to_base = dynamic_cast(to.base_type); + return (from_base && to_base && can_convert(*from_base, *to_base)); + } + else + return false; +} + +ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right) +{ + if(&left==&right) + return SAME_TYPE; + else if(can_convert(left, right)) + return LEFT_CONVERTIBLE; + else if(can_convert(right, left)) + return RIGHT_CONVERTIBLE; + else + return NOT_COMPATIBLE; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if((*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclaration &type) +{ + RefPtr call = new FunctionCall; + call->name = type.name; + call->constructor = true; + call->arguments.push_back(0); + call->arguments.back() = expr; + call->type = &type; + expr = call; +} + +bool ExpressionResolver::convert_to_element(RefPtr &expr, BasicTypeDeclaration &elem_type) +{ + if(BasicTypeDeclaration *expr_type = dynamic_cast(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(to_type) + { + convert_to(expr, *to_type); + return true; + } + } + + return false; +} + +void ExpressionResolver::visit(Literal &literal) +{ + if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::BOOL, 1); + else if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::INT, 32); + else if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::FLOAT, 32); +} + +void ExpressionResolver::visit(ParenthesizedExpression &parexpr) +{ + TraversingVisitor::visit(parexpr); + + parexpr.type = parexpr.expression->type; + parexpr.lvalue = parexpr.expression->lvalue; +} + +void ExpressionResolver::visit(VariableReference &var) +{ + if(var.declaration) + var.type = var.declaration->type_declaration; + var.lvalue = true; +} + +void ExpressionResolver::visit(InterfaceBlockReference &iface) +{ + iface.lvalue = true; +} + +void ExpressionResolver::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + + if(memacc.declaration) + memacc.type = memacc.declaration->type_declaration; + memacc.lvalue = memacc.left->lvalue; +} + +void ExpressionResolver::visit(UnaryExpression &unary) +{ + TraversingVisitor::visit(unary); + + BasicTypeDeclaration *basic = dynamic_cast(unary.expression->type); + if(!basic) + return; + + char oper = unary.oper->token[0]; + if(oper=='!') + { + if(basic->kind==BasicTypeDeclaration::BOOL) + unary.type = basic; + } + else if(oper=='~') + { + if(basic->kind==BasicTypeDeclaration::INT) + unary.type = basic; + } + else if(oper=='+' || oper=='-') + { + BasicTypeDeclaration *elem = get_element_type(*basic); + if(elem && is_scalar(*elem)) + unary.type = basic; + } + unary.lvalue = unary.expression->lvalue; +} + +void ExpressionResolver::visit(BinaryExpression &binary) +{ + TraversingVisitor::visit(binary); + + /* Binary operators are only defined for basic types (not for image or + structure types). */ + BasicTypeDeclaration *basic_left = dynamic_cast(binary.left->type); + BasicTypeDeclaration *basic_right = dynamic_cast(binary.right->type); + if(!basic_left || !basic_right) + return; + + binary.lvalue = false; + + char oper = binary.oper->token[0]; + if(oper=='[') + { + /* Subscripting operates on vectors, matrices and arrays, and the right + operand must be an integer. */ + if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = basic_left->base_type; + binary.lvalue = binary.left->lvalue; + return; + } + else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY) + // No other binary operator can be used with arrays. + return; + + BasicTypeDeclaration *elem_left = get_element_type(*basic_left); + BasicTypeDeclaration *elem_right = get_element_type(*basic_right); + if(!elem_left || !elem_right) + return; + + Compatibility compat = get_compatibility(*basic_left, *basic_right); + Compatibility elem_compat = get_compatibility(*elem_left, *elem_right); + if(elem_compat==NOT_COMPATIBLE) + return; + + char oper2 = binary.oper->token[1]; + if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>')) + { + /* Relational operators compare two scalar integer or floating-point + values. */ + if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE) + return; + + binary.type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if((oper=='=' || oper=='!') && oper2=='=') + { + // Equality comparison can be done on any compatible types. + if(compat==NOT_COMPATIBLE) + return; + + binary.type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if(oper2=='&' || oper2=='|' || oper2=='^') + { + // Logical operators can only be applied to booleans. + if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL) + return; + + binary.type = basic_left; + } + else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2) + { + // Bitwise operators and modulo can only be applied to integers. + if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left); + } + else if((oper=='<' || oper=='>') && oper2==oper) + { + // Shifts only apply to integers. + if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = basic_left; + } + else if(oper=='+' || oper=='-' || oper=='*' || oper=='/') + { + // Arithmetic operators require scalar elements. + if(!is_scalar(*elem_left) || !is_scalar(*elem_right)) + return; + + if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) && + (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX)) + { + /* Multiplication has special rules when at least one operand is a + matrix and the other is a vector or a matrix. */ + unsigned left_columns = basic_left->size&0xFFFF; + unsigned right_rows = basic_right->size; + if(basic_right->kind==BasicTypeDeclaration::MATRIX) + right_rows >>= 16; + if(left_columns!=right_rows) + return; + + BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left); + + if(basic_left->kind==BasicTypeDeclaration::VECTOR) + binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF); + else if(basic_right->kind==BasicTypeDeclaration::VECTOR) + binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16); + else + binary.type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF)); + } + else if(compat==NOT_COMPATIBLE) + { + // Arithmetic between scalars and matrices or vectors is supported. + if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right)) + binary.type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right); + else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right)) + binary.type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left); + else + return; + } + else if(compat==LEFT_CONVERTIBLE) + binary.type = basic_right; + else + binary.type = basic_left; + } + else + return; + + bool converted = true; + if(compat==LEFT_CONVERTIBLE) + convert_to(binary.left, *basic_right); + else if(compat==RIGHT_CONVERTIBLE) + convert_to(binary.right, *basic_left); + else if(elem_compat==LEFT_CONVERTIBLE) + converted = convert_to_element(binary.left, *elem_right); + else if(elem_compat==RIGHT_CONVERTIBLE) + converted = convert_to_element(binary.right, *elem_left); + + if(!converted) + binary.type = 0; +} + +void ExpressionResolver::visit(Assignment &assign) +{ + TraversingVisitor::visit(assign); + assign.type = assign.left->type; + assign.lvalue = true; +} + +void ExpressionResolver::visit(FunctionCall &call) +{ + TraversingVisitor::visit(call); + + if(call.declaration) + call.type = call.declaration->return_type_declaration; + else if(call.constructor) + { + map::const_iterator i=stage->types.find(call.name); + call.type = (i!=stage->types.end() ? i->second : 0); + } + call.lvalue = false; +} + +void ExpressionResolver::visit(BasicTypeDeclaration &type) +{ + basic_types.push_back(&type); +} + +void ExpressionResolver::visit(VariableDeclaration &var) +{ + TraversingVisitor::visit(var); + if(!var.init_expression) + return; + + BasicTypeDeclaration *var_basic = dynamic_cast(var.type_declaration); + BasicTypeDeclaration *init_basic = dynamic_cast(var.init_expression->type); + if(!var_basic || !init_basic) + return; + + Compatibility compat = get_compatibility(*var_basic, *init_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(var.init_expression, *var_basic); +} + + void FunctionResolver::apply(Stage &s) { stage = &s; @@ -443,14 +874,14 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration } iface_target_block->body.insert(iface_insert_point, iface_var); - iface_target_block->variables[name] = iface_var; + iface_target_block->variables.insert(make_pair(name, iface_var)); return iface_var; } InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block) { - if(stage->interface_blocks.count(out_block.name)) + if(stage->interface_blocks.count("in"+out_block.name)) return 0; InterfaceBlock *in_block = new InterfaceBlock; @@ -472,9 +903,9 @@ InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block } iface_target_block->body.insert(iface_insert_point, in_block); - stage->interface_blocks[in_block->name] = in_block; + stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block)); if(!in_block->instance_name.empty()) - stage->interface_blocks[in_block->instance_name] = in_block; + stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block)); SetFlag set_scope(function_scope, false); SetForScope set_block(current_block, &stage->content); @@ -521,10 +952,12 @@ void InterfaceGenerator::visit(VariableReference &var) } const map &prev_blocks = stage->previous->interface_blocks; - map::const_iterator j = prev_blocks.find(var.name); - if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name) + map::const_iterator j = prev_blocks.find("_"+var.name); + if(j!=prev_blocks.end() && j->second->interface=="out") { generate_interface(*j->second); + /* Let VariableResolver convert the variable reference into an interface + block reference. */ return; } @@ -556,7 +989,10 @@ void InterfaceGenerator::visit(VariableDeclaration &var) const map &linked_vars = iface_block->linked_block->members.variables; map::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; } @@ -565,8 +1001,11 @@ void InterfaceGenerator::visit(VariableDeclaration &var) { /* For output 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)) + VariableDeclaration *out_var = 0; + if(function_scope && (out_var=generate_interface(var, "out", var.name))) { + out_var->source = var.source; + out_var->line = var.line; nodes_to_remove.insert(&var); if(var.init_expression) { @@ -605,8 +1044,8 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) if(!iface.linked_block && stage->previous) { const map &prev_blocks = stage->previous->interface_blocks; - map::const_iterator i = prev_blocks.find(iface.name); - if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name) + map::const_iterator i = prev_blocks.find("out"+iface.name); + if(i!=prev_blocks.end()) { iface.linked_block = i->second; i->second->linked_block = &iface;