2020-08-11 00:08:48 -07:00
|
|
|
#include "CodeGenerator.h"
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include "StoredToken.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/LegacyFunctionOrder.h"
|
|
|
|
|
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
|
|
|
|
|
#include "bscript/compiler/ast/Program.h"
|
|
|
|
|
#include "bscript/compiler/ast/ProgramParameterList.h"
|
|
|
|
|
#include "bscript/compiler/ast/TopLevelStatements.h"
|
|
|
|
|
#include "bscript/compiler/ast/UserFunction.h"
|
|
|
|
|
#include "bscript/compiler/codegen/InstructionEmitter.h"
|
|
|
|
|
#include "bscript/compiler/codegen/InstructionGenerator.h"
|
|
|
|
|
#include "bscript/compiler/codegen/ModuleDeclarationRegistrar.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceFileIdentifier.h"
|
|
|
|
|
#include "bscript/compiler/model/CompilerWorkspace.h"
|
|
|
|
|
#include "bscript/compiler/model/FlowControlLabel.h"
|
|
|
|
|
#include "bscript/compiler/representation/CompiledScript.h"
|
|
|
|
|
#include "bscript/compiler/representation/ExportedFunction.h"
|
|
|
|
|
#include "bscript/compiler/representation/ModuleDescriptor.h"
|
|
|
|
|
#include "bscript/compiler/representation/ModuleFunctionDescriptor.h"
|
2020-08-11 00:08:48 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
std::unique_ptr<CompiledScript> CodeGenerator::generate(
|
2020-08-24 01:47:38 -07:00
|
|
|
std::unique_ptr<CompilerWorkspace> workspace, const LegacyFunctionOrder* legacy_function_order )
|
2020-08-11 00:08:48 -07:00
|
|
|
{
|
2020-08-28 22:45:07 -07:00
|
|
|
auto program_info =
|
|
|
|
|
workspace->program
|
|
|
|
|
? std::unique_ptr<CompiledScript::ProgramInfo>( new CompiledScript::ProgramInfo{
|
|
|
|
|
static_cast<unsigned>( workspace->program->parameter_list().children.size() ) } )
|
|
|
|
|
: std::unique_ptr<CompiledScript::ProgramInfo>();
|
2020-08-11 00:08:48 -07:00
|
|
|
|
|
|
|
|
CodeSection code;
|
|
|
|
|
DataSection data;
|
|
|
|
|
|
2020-09-12 14:26:58 -07:00
|
|
|
std::vector<std::string> debug_filenames;
|
|
|
|
|
// For parity with OG compiler, debug file #0 is always empty.
|
|
|
|
|
debug_filenames.emplace_back( "" );
|
|
|
|
|
for ( auto& ident : workspace->referenced_source_file_identifiers )
|
|
|
|
|
{
|
|
|
|
|
debug_filenames.push_back( ident->pathname );
|
|
|
|
|
}
|
|
|
|
|
DebugStore debug( std::move( debug_filenames ) );
|
|
|
|
|
|
2020-08-11 00:08:48 -07:00
|
|
|
ExportedFunctions exported_functions;
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
ModuleDeclarationRegistrar module_declaration_registrar;
|
|
|
|
|
|
2020-09-12 14:26:58 -07:00
|
|
|
InstructionEmitter instruction_emitter( code, data, debug, exported_functions,
|
2020-08-24 01:47:38 -07:00
|
|
|
module_declaration_registrar );
|
|
|
|
|
CodeGenerator generator( instruction_emitter, module_declaration_registrar );
|
|
|
|
|
|
|
|
|
|
generator.register_module_functions( *workspace, legacy_function_order );
|
2020-08-11 00:08:48 -07:00
|
|
|
|
2020-08-31 18:46:27 -07:00
|
|
|
sort_user_functions( *workspace, legacy_function_order );
|
|
|
|
|
|
2020-08-11 00:08:48 -07:00
|
|
|
generator.generate_instructions( *workspace );
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
std::vector<ModuleDescriptor> module_descriptors =
|
|
|
|
|
module_declaration_registrar.take_module_descriptors();
|
|
|
|
|
|
2020-08-11 00:08:48 -07:00
|
|
|
return std::make_unique<CompiledScript>(
|
2020-09-12 14:26:58 -07:00
|
|
|
std::move( code ), std::move( data ), std::move( debug ), std::move( exported_functions ),
|
2020-08-11 00:08:48 -07:00
|
|
|
std::move( workspace->global_variable_names ), std::move( module_descriptors ),
|
|
|
|
|
std::move( program_info ), std::move( workspace->referenced_source_file_identifiers ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
CodeGenerator::CodeGenerator( InstructionEmitter& emitter,
|
|
|
|
|
ModuleDeclarationRegistrar& module_declaration_registrar )
|
|
|
|
|
: module_declaration_registrar( module_declaration_registrar ),
|
|
|
|
|
emitter( emitter ),
|
2020-08-12 01:30:06 -07:00
|
|
|
emit( emitter )
|
2020-08-11 00:08:48 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:53:03 -07:00
|
|
|
void CodeGenerator::generate_instructions( CompilerWorkspace& workspace )
|
2020-08-11 00:08:48 -07:00
|
|
|
{
|
2020-09-12 14:26:58 -07:00
|
|
|
emitter.debug_statementbegin();
|
|
|
|
|
emitter.debug_file_line( 0, 1 );
|
|
|
|
|
|
2020-08-31 18:46:27 -07:00
|
|
|
std::map<std::string, FlowControlLabel> user_function_labels;
|
|
|
|
|
InstructionGenerator outer_instruction_generator( emitter, user_function_labels, false );
|
|
|
|
|
InstructionGenerator function_instruction_generator( emitter, user_function_labels, true );
|
|
|
|
|
|
2020-08-28 22:45:07 -07:00
|
|
|
workspace.top_level_statements->accept( outer_instruction_generator );
|
|
|
|
|
|
|
|
|
|
if ( auto& program = workspace.program )
|
|
|
|
|
{
|
|
|
|
|
program->accept( outer_instruction_generator );
|
|
|
|
|
}
|
2020-08-17 22:53:03 -07:00
|
|
|
|
2020-08-12 01:30:06 -07:00
|
|
|
emit.progend();
|
2020-08-31 18:46:27 -07:00
|
|
|
|
|
|
|
|
for ( auto& user_function : workspace.user_functions )
|
|
|
|
|
{
|
|
|
|
|
user_function->accept( function_instruction_generator );
|
|
|
|
|
}
|
2020-08-11 00:08:48 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
void CodeGenerator::register_module_functions( CompilerWorkspace& workspace,
|
|
|
|
|
const LegacyFunctionOrder* legacy_function_order )
|
|
|
|
|
{
|
|
|
|
|
if ( legacy_function_order )
|
|
|
|
|
{
|
|
|
|
|
register_module_functions_as_legacy( workspace );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
register_module_functions_alphabetically( workspace );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::register_module_functions_as_legacy( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
for ( auto& module_function : workspace.module_function_declarations )
|
|
|
|
|
{
|
|
|
|
|
// we might assign IDs to more modules that we actually use,
|
|
|
|
|
// but this will help us compare output with the original compiler.
|
|
|
|
|
module_declaration_registrar.register_module( *module_function );
|
|
|
|
|
}
|
|
|
|
|
for ( const auto& decl : workspace.module_functions_in_legacy_order )
|
|
|
|
|
{
|
|
|
|
|
module_declaration_registrar.register_modulefunc( *decl );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::register_module_functions_alphabetically( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
sort_module_functions_by_module_name( workspace );
|
|
|
|
|
for ( auto& module_function : workspace.referenced_module_function_declarations )
|
|
|
|
|
{
|
|
|
|
|
module_declaration_registrar.register_module( *module_function );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort_module_functions_alphabetically( workspace );
|
|
|
|
|
for ( const auto& decl : workspace.referenced_module_function_declarations )
|
|
|
|
|
{
|
|
|
|
|
module_declaration_registrar.register_modulefunc( *decl );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::sort_module_functions_by_module_name( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
auto sortByModuleName = []( ModuleFunctionDeclaration* d1,
|
|
|
|
|
ModuleFunctionDeclaration* d2 ) -> bool {
|
|
|
|
|
return d1->module_name < d2->module_name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort( workspace.referenced_module_function_declarations.begin(),
|
|
|
|
|
workspace.referenced_module_function_declarations.end(), sortByModuleName );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::sort_module_functions_alphabetically( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
auto sortByModuleFunctionName = []( ModuleFunctionDeclaration* d1,
|
|
|
|
|
ModuleFunctionDeclaration* d2 ) -> bool {
|
|
|
|
|
return d1->name < d2->name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort( workspace.referenced_module_function_declarations.begin(),
|
|
|
|
|
workspace.referenced_module_function_declarations.end(), sortByModuleFunctionName );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 18:46:27 -07:00
|
|
|
void CodeGenerator::sort_user_functions( CompilerWorkspace& workspace,
|
|
|
|
|
const LegacyFunctionOrder* legacy_function_order )
|
|
|
|
|
{
|
|
|
|
|
if ( legacy_function_order )
|
|
|
|
|
{
|
|
|
|
|
sort_user_functions_as_legacy( workspace, *legacy_function_order );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sort_user_functions_alphabetically( workspace );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::sort_user_functions_as_legacy(
|
|
|
|
|
CompilerWorkspace& workspace, const LegacyFunctionOrder& legacy_function_order )
|
|
|
|
|
{
|
|
|
|
|
std::map<std::string, size_t> c1_order;
|
|
|
|
|
for ( auto& n : legacy_function_order.userfunc_emit_order )
|
|
|
|
|
{
|
|
|
|
|
c1_order[n] = c1_order.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto sortAsLegacy = [c1_order]( const std::unique_ptr<UserFunction>& s1,
|
|
|
|
|
const std::unique_ptr<UserFunction>& s2 ) -> bool {
|
|
|
|
|
auto itr1 = c1_order.find( s1->name );
|
|
|
|
|
auto itr2 = c1_order.find( s2->name );
|
|
|
|
|
if ( itr1 != c1_order.end() && itr1 != c1_order.end() )
|
|
|
|
|
{
|
|
|
|
|
return ( *itr1 ).second < ( *itr2 ).second;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return s1->name < s2->name;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
std::sort( workspace.user_functions.begin(), workspace.user_functions.end(), sortAsLegacy );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::sort_user_functions_alphabetically( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
auto sortByName = []( const std::unique_ptr<UserFunction>& s1,
|
|
|
|
|
const std::unique_ptr<UserFunction>& s2 ) -> bool {
|
|
|
|
|
return s1->name < s2->name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort( workspace.user_functions.begin(), workspace.user_functions.end(), sortByName );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 00:08:48 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|