From e9d5d1945f110d5307036af0d4b3f0c3ebfe3d2a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Jun 2018 19:38:11 +0300 Subject: [PATCH] Reorder functions in dependency order Otherwise the inliner might inline a function to a point before its original definition. This can break references to other functions declared after the inlining point, causing the unused function eliminator to eventually remove them. --- source/programcompiler.cpp | 68 ++++++++++++++++++++++++++++++++++++++ source/programcompiler.h | 8 +++-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index fbcc6598..0aed5936 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -987,13 +987,30 @@ void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass) 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(); ) { @@ -1017,13 +1034,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) diff --git a/source/programcompiler.h b/source/programcompiler.h index b14c03d8..7e8d3373 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -169,17 +169,21 @@ private: FUNCTION }; + unsigned scope_level; DeclarationKind kind; + std::set ordered_funcs; + std::set needed_funcs; DeclarationReorderer(); using Visitor::visit; virtual void visit(ProgramSyntax::Block &); + virtual void visit(ProgramSyntax::FunctionCall &); virtual void visit(ProgramSyntax::InterfaceLayout &) { kind = LAYOUT; } virtual void visit(ProgramSyntax::StructDeclaration &) { kind = STRUCT; } - virtual void visit(ProgramSyntax::VariableDeclaration &) { kind = VARIABLE; } + virtual void visit(ProgramSyntax::VariableDeclaration &); virtual void visit(ProgramSyntax::InterfaceBlock &) { kind = VARIABLE; } - virtual void visit(ProgramSyntax::FunctionDeclaration &) { kind = FUNCTION; } + virtual void visit(ProgramSyntax::FunctionDeclaration &); }; struct InlineableFunctionLocator: Visitor -- 2.45.2