2020-08-10 04:54:59 -07:00
|
|
|
#include "Optimizer.h"
|
|
|
|
|
|
2020-08-14 23:01:19 -07:00
|
|
|
#include "clib/logfacility.h"
|
2020-08-10 04:54:59 -07:00
|
|
|
#include "compiler/Report.h"
|
2020-08-24 01:47:38 -07:00
|
|
|
#include "compiler/ast/TopLevelStatements.h"
|
2020-08-26 19:24:47 -07:00
|
|
|
#include "compiler/ast/UnaryOperator.h"
|
|
|
|
|
#include "compiler/ast/ValueConsumer.h"
|
2020-08-10 04:54:59 -07:00
|
|
|
#include "compiler/model/CompilerWorkspace.h"
|
2020-08-24 01:47:38 -07:00
|
|
|
#include "compiler/optimizer/ReferencedFunctionGatherer.h"
|
2020-08-26 19:24:47 -07:00
|
|
|
#include "compiler/optimizer/UnaryOperatorOptimizer.h"
|
2020-08-10 04:54:59 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
Optimizer::Optimizer( Report& report )
|
|
|
|
|
: report( report )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
void Optimizer::optimize( CompilerWorkspace& workspace )
|
2020-08-10 04:54:59 -07:00
|
|
|
{
|
2020-08-24 01:47:38 -07:00
|
|
|
ReferencedFunctionGatherer gatherer( workspace.module_function_declarations );
|
|
|
|
|
workspace.top_level_statements->accept( gatherer );
|
|
|
|
|
workspace.referenced_module_function_declarations =
|
|
|
|
|
gatherer.take_referenced_module_function_declarations();
|
2020-08-10 04:54:59 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-26 19:24:47 -07:00
|
|
|
void Optimizer::visit_children( Node& node )
|
|
|
|
|
{
|
|
|
|
|
unsigned i = 0;
|
|
|
|
|
for ( auto& child : node.children )
|
|
|
|
|
{
|
|
|
|
|
child->accept( *this );
|
|
|
|
|
|
|
|
|
|
if ( optimized_replacement )
|
|
|
|
|
{
|
|
|
|
|
node.children[i] = std::move( optimized_replacement );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Optimizer::visit_unary_operator( UnaryOperator& unary_operator )
|
|
|
|
|
{
|
|
|
|
|
visit_children( unary_operator );
|
|
|
|
|
|
|
|
|
|
optimized_replacement = UnaryOperatorOptimizer( unary_operator ).optimize();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 04:54:59 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|