X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=5bb84269f30abe4fcd38198cef0b6a2ae416b2f8;hp=e102349a6482f0dc4a679ee325561210948dd52b;hb=3c46227c280bc4cd8a4487c648f2f080d5b4f7ea;hpb=35646cafcdff8be76f6ef96018415d9ddec6f77c diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index e102349a..5bb84269 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -35,6 +35,8 @@ namespace GL { using namespace ProgramSyntax; +// XXX For some reason global declarations are emitted for otherwise undeclared local variables + ProgramCompiler::ProgramCompiler(): resources(0), module(0) @@ -130,8 +132,8 @@ Stage *ProgramCompiler::get_builtins(StageType type) void ProgramCompiler::append_module(ProgramSyntax::Module &mod) { - list imports = apply >(mod.shared); - for(list::iterator i=imports.begin(); i!=imports.end(); ++i) + vector imports = apply >(mod.shared); + for(vector::iterator i=imports.begin(); i!=imports.end(); ++i) import((*i)->module); apply(mod.shared, set(imports.begin(), imports.end())); @@ -205,8 +207,8 @@ void ProgramCompiler::generate(Stage &stage) apply(stage); apply(stage); apply(stage); - apply(stage); apply(stage); + apply(stage); apply(stage); } @@ -861,6 +863,7 @@ void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ExpressionStatement *stmt = new ExpressionStatement; stmt->expression = assign; + stmt->visit(*this); insert_nodes.push_back(stmt); } @@ -876,7 +879,10 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var) if(i==prev_out.end()) i = prev_out.find(in_prefix+var.name); if(i!=prev_out.end()) - generate_interface(*i->second, "in", var.name); + { + generate_interface(*i->second, "in", i->second->name); + var.name = i->second->name; + } } void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) @@ -889,7 +895,10 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var) { remove_node = true; if(var.init_expression) + { insert_assignment(var.name, var.init_expression->clone()); + return; + } } } else if(var.interface=="in") @@ -978,28 +987,31 @@ void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass) } -void ProgramCompiler::VariableRenamer::visit(VariableReference &var) -{ - if(var.declaration) - var.name = var.declaration->name; -} - -void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) -{ - if(var.linked_declaration) - var.name = var.linked_declaration->name; - TraversingVisitor::visit(var); -} - - ProgramCompiler::DeclarationReorderer::DeclarationReorderer(): + scope_level(0), kind(NO_DECLARATION) { } +void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call) +{ + FunctionDeclaration *def = call.declaration; + if(def) + def = def->definition; + if(def && !ordered_funcs.count(def)) + needed_funcs.insert(def); +} + void ProgramCompiler::DeclarationReorderer::visit(Block &block) { + SetForScope set(scope_level, scope_level+1); + if(scope_level>1) + return Visitor::visit(block); + list >::iterator struct_insert_point = block.body.end(); list >::iterator variable_insert_point = block.body.end(); + list >::iterator function_insert_point = block.body.end(); + unsigned unordered_func_count = 0; + bool ordered_any_funcs = false; for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { @@ -1023,13 +1035,64 @@ void ProgramCompiler::DeclarationReorderer::visit(Block &block) else if(kind>VARIABLE && variable_insert_point==block.body.end()) variable_insert_point = i; + if(kind==FUNCTION) + { + if(function_insert_point==block.body.end()) + function_insert_point = i; + + if(needed_funcs.empty()) + { + ordered_funcs.insert(i->get()); + if(i!=function_insert_point) + { + block.body.insert(function_insert_point, *i); + moved = true; + } + else + ++function_insert_point; + ordered_any_funcs = true; + } + else + ++unordered_func_count; + } + if(moved) + { + if(function_insert_point==i) + ++function_insert_point; block.body.erase(i++); + } else ++i; + + if(i==block.body.end() && unordered_func_count) + { + if(!ordered_any_funcs) + // A subset of the remaining functions forms a recursive loop + /* TODO pick a function and move it up, adding any necessary + declarations */ + break; + + i = function_insert_point; + unordered_func_count = 0; + } } } +void ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var) +{ + Visitor::visit(var); + kind = VARIABLE; +} + +void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func) +{ + needed_funcs.clear(); + func.body.visit(*this); + needed_funcs.erase(&func); + kind = FUNCTION; +} + ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator(): in_function(0)