2020-08-11 00:08:48 -07:00
|
|
|
#include "CodeGenerator.h"
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include "StoredToken.h"
|
2024-08-10 15:27:25 +02:00
|
|
|
#include "bscript/compiler/ast/ClassDeclaration.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#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"
|
2025-07-23 23:00:34 +02:00
|
|
|
#include "bscript/compiler/codegen/AbstractSyntaxTreeStringGenerator.h"
|
2024-08-19 19:57:31 +02:00
|
|
|
#include "bscript/compiler/codegen/ClassDeclarationRegistrar.h"
|
2024-08-06 20:29:02 +02:00
|
|
|
#include "bscript/compiler/codegen/FunctionReferenceRegistrar.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#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"
|
2026-07-22 12:58:56 +02:00
|
|
|
#include "bscript/compiler/optimizer/CodeSectionOptimizer.h"
|
|
|
|
|
#include "bscript/compiler/optimizer/ShortCircuitCombiner.h"
|
2024-08-19 19:57:31 +02:00
|
|
|
#include "bscript/compiler/representation/ClassDescriptor.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/representation/CompiledScript.h"
|
|
|
|
|
#include "bscript/compiler/representation/ExportedFunction.h"
|
2024-08-06 20:29:02 +02:00
|
|
|
#include "bscript/compiler/representation/FunctionReferenceDescriptor.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#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(
|
2025-07-23 23:00:34 +02:00
|
|
|
std::unique_ptr<CompilerWorkspace> workspace, Report& report, bool generate_ast_string )
|
2020-08-11 00:08:48 -07:00
|
|
|
{
|
2020-08-28 22:45:07 -07:00
|
|
|
auto program_info =
|
|
|
|
|
workspace->program
|
2026-01-16 13:22:43 +01:00
|
|
|
? std::make_unique<CompiledScript::ProgramInfo>( CompiledScript::ProgramInfo{
|
2020-08-28 22:45:07 -07:00
|
|
|
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;
|
2024-08-06 20:29:02 +02:00
|
|
|
FunctionReferenceRegistrar function_reference_registrar;
|
2024-08-19 19:57:31 +02:00
|
|
|
ClassDeclarationRegistrar class_declaration_registrar;
|
2020-08-24 01:47:38 -07:00
|
|
|
|
2024-08-19 19:57:31 +02:00
|
|
|
InstructionEmitter instruction_emitter(
|
|
|
|
|
code, data, debug, exported_functions, module_declaration_registrar,
|
|
|
|
|
function_reference_registrar, class_declaration_registrar, report );
|
2020-08-24 01:47:38 -07:00
|
|
|
CodeGenerator generator( instruction_emitter, module_declaration_registrar );
|
|
|
|
|
|
2021-03-06 01:39:26 +01:00
|
|
|
generator.register_module_functions_alphabetically( *workspace );
|
2020-08-11 00:08:48 -07:00
|
|
|
|
2021-03-06 01:39:26 +01:00
|
|
|
sort_user_functions_alphabetically( *workspace );
|
2020-08-31 18:46:27 -07:00
|
|
|
|
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();
|
|
|
|
|
|
2024-08-06 20:29:02 +02:00
|
|
|
std::vector<FunctionReferenceDescriptor> function_references =
|
2024-09-04 13:28:50 +02:00
|
|
|
function_reference_registrar.take_descriptors( workspace->class_declaration_indexes,
|
|
|
|
|
workspace->user_function_labels );
|
2024-08-06 20:29:02 +02:00
|
|
|
|
2024-08-19 19:57:31 +02:00
|
|
|
std::vector<ClassDescriptor> class_descriptors = class_declaration_registrar.take_descriptors();
|
|
|
|
|
|
2026-07-22 12:58:56 +02:00
|
|
|
CodeSectionOptimizer{}.optimize( code );
|
|
|
|
|
|
2025-07-23 23:00:34 +02:00
|
|
|
std::string tree;
|
|
|
|
|
|
|
|
|
|
if ( generate_ast_string )
|
|
|
|
|
{
|
|
|
|
|
AbstractSyntaxTreeStringGenerator ast_generator;
|
|
|
|
|
workspace->accept( ast_generator );
|
|
|
|
|
tree = ast_generator.tree();
|
|
|
|
|
}
|
|
|
|
|
|
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 ),
|
2024-08-19 19:57:31 +02:00
|
|
|
std::move( function_references ), std::move( class_descriptors ), std::move( program_info ),
|
2025-07-23 23:00:34 +02:00
|
|
|
std::move( workspace->referenced_source_file_identifiers ), std::move( tree ) );
|
2020-08-11 00:08:48 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
CodeGenerator::CodeGenerator( InstructionEmitter& emitter,
|
2024-07-29 22:30:52 +02:00
|
|
|
ModuleDeclarationRegistrar& module_declaration_registrar )
|
|
|
|
|
: module_declaration_registrar( module_declaration_registrar ),
|
|
|
|
|
emitter( emitter ),
|
|
|
|
|
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 );
|
|
|
|
|
|
2024-08-26 22:37:22 +02:00
|
|
|
InstructionGenerator generator( emitter, workspace.user_function_labels,
|
2024-08-25 11:44:06 +02:00
|
|
|
workspace.class_declaration_indexes );
|
2020-08-31 18:46:27 -07:00
|
|
|
|
2024-07-29 22:30:52 +02:00
|
|
|
workspace.top_level_statements->accept( generator );
|
2020-08-28 22:45:07 -07:00
|
|
|
|
|
|
|
|
if ( auto& program = workspace.program )
|
|
|
|
|
{
|
2024-07-29 22:30:52 +02:00
|
|
|
program->accept( generator );
|
2020-08-28 22:45:07 -07:00
|
|
|
}
|
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 )
|
|
|
|
|
{
|
2024-07-29 22:30:52 +02:00
|
|
|
// Function expressions are handled inside InstructionGenerator visit_function_expression
|
|
|
|
|
if ( !user_function->expression )
|
|
|
|
|
{
|
|
|
|
|
user_function->accept( generator );
|
|
|
|
|
}
|
2020-08-31 18:46:27 -07:00
|
|
|
}
|
2024-08-10 15:27:25 +02:00
|
|
|
|
2024-09-04 13:28:50 +02:00
|
|
|
// The default parameter generation needs to happen after generating the
|
|
|
|
|
// instructions for the user functions, as only class method functions and
|
|
|
|
|
// user functions with a registered function reference emit their default
|
|
|
|
|
// arguments. Since a user function may be registered as a function reference
|
|
|
|
|
// only _during_ some other user function's generation, we are only sure a
|
|
|
|
|
// user function has a function reference after generating all of them.
|
|
|
|
|
for ( const auto& user_function : workspace.user_functions )
|
|
|
|
|
{
|
|
|
|
|
generator.generate_default_parameters( *user_function.get() );
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 15:27:25 +02:00
|
|
|
for ( auto& class_decl : workspace.class_declarations )
|
|
|
|
|
{
|
2024-09-04 13:28:50 +02:00
|
|
|
emitter.register_class_declaration( *class_decl, workspace.user_function_labels );
|
2024-08-10 15:27:25 +02:00
|
|
|
}
|
2020-08-11 00:08:48 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
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 )
|
|
|
|
|
{
|
2024-08-10 15:27:25 +02:00
|
|
|
auto sortByModuleName = []( ModuleFunctionDeclaration* d1, ModuleFunctionDeclaration* d2 ) -> bool
|
2024-08-11 23:01:38 +02:00
|
|
|
{ return d1->scope < d2->scope; };
|
2020-08-24 01:47:38 -07:00
|
|
|
|
|
|
|
|
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,
|
2024-08-10 15:27:25 +02:00
|
|
|
ModuleFunctionDeclaration* d2 ) -> bool
|
|
|
|
|
{ return d1->name < d2->name; };
|
2020-08-24 01:47:38 -07:00
|
|
|
|
|
|
|
|
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_alphabetically( CompilerWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
auto sortByName = []( const std::unique_ptr<UserFunction>& s1,
|
2024-08-10 15:27:25 +02:00
|
|
|
const std::unique_ptr<UserFunction>& s2 ) -> bool
|
|
|
|
|
{ return s1->name < s2->name; };
|
2020-08-31 18:46:27 -07:00
|
|
|
|
|
|
|
|
std::sort( workspace.user_functions.begin(), workspace.user_functions.end(), sortByName );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 00:08:48 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|