47#ifndef COMPILER_GENERIC_DRIVER_H
48#define COMPILER_GENERIC_DRIVER_H
119 std::ostringstream
out;
131 if (modules.is_empty()
or order.is_empty())
230 append_error(
"bytecode: null bytecode module produced by lowering");
266 std::string
message =
"run: HIR runtime execution failed";
299 std::ifstream
in(path);
303 std::ostringstream
out;
382 append_error(
"hir: failed to lower frontend program to HIR");
418 append_error(
"driver: reached unreachable action dispatch");
427 for (
size_t i = 0; i < paths.
size(); ++i)
443 const std::string &text,
509 for (
size_t i =
artifacts_.size(); i > 0; --i)
518 std::ostringstream
out;
Reusable bytecode format and lowering from Compiler_IR_Model.H.
Portable C backend that emits standalone C from Compiler_IR_Model.H.
Reusable interface for language frontends that plug into common drivers.
Reusable binding helpers for linked module surfaces.
Reusable linking helpers for module surfaces and direct imports.
Reusable per-module cache for top-level name resolution.
Reusable namespace-separated semantic environment for modules.
size_t size_t int32_t * out
Arena-backed ownership context for bytecode functions and modules.
void reset() noexcept
Resets the arena and destroys all tracked objects.
Lowers explicit IR into register-based bytecode.
Abstract language frontend that can feed reusable drivers.
virtual bool analyze()=0
Runs semantic analysis on the parsed program.
virtual const Compiler_Type_Context * type_context() const noexcept=0
Returns the optional shared type context for dumps and backends.
virtual bool lower_hir()=0
Lowers the analyzed program into common HIR.
virtual const DynArray< std::string > & errors() const noexcept=0
Returns hard frontend-level errors outside the diagnostic engine.
virtual const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept=0
Finds one frontend artifact by exact label.
virtual const DynArray< Compiler_Driver_Artifact > & artifacts() const noexcept=0
Returns all frontend-produced artifacts from the last run.
virtual const DynArray< Compiler_Module_Descriptor > & module_descriptors() const noexcept
Returns parsed module descriptors for reusable import orchestration.
virtual void clear() noexcept=0
Clears all frontend-owned state from the previous run.
virtual bool load_sources(const DynArray< Compiler_Driver_Source > &inputs)=0
Registers the ordered input sources for the next run.
virtual const DynArray< size_t > & module_merge_order() const noexcept
Returns the dependency-first merge order from the last parse.
virtual const Compiler_IR_Module * ir_module() const noexcept=0
Returns the lowered IR module from the last run, when available.
virtual const Compiler_HIR_Module * hir_module() const noexcept=0
Returns the lowered HIR module from the last run, when available.
virtual const DynArray< Compiler_Module_Metadata > & module_metadata() const noexcept
Returns reusable metadata for the top-level surface of each module.
virtual bool lower_ir()=0
Lowers the analyzed program into common IR.
virtual const DynArray< std::string > & warnings() const noexcept=0
Returns non-fatal frontend-level warnings.
virtual Diagnostic_Engine * diagnostic_engine() noexcept=0
Returns the diagnostic engine populated by the frontend.
virtual bool parse()=0
Parses all previously loaded sources.
Reusable orchestration object built on top of Compiler_Frontend.
bool execute_hir_runtime()
bool execute_sources(const DynArray< Compiler_Driver_Source > &inputs, const Compiler_Driver_Action action=Compiler_Driver_Action::Run)
Runs the pipeline over in-memory sources.
bool execute_files(const DynArray< std::string > &paths, const Compiler_Driver_Action action=Compiler_Driver_Action::Run)
Runs the pipeline over source files loaded from disk.
std::string diagnostics_text() const
Renders diagnostics from the frontend as plain text.
const Compiler_Frontend & frontend_impl() const noexcept
Returns the frontend used by this driver.
Compiler_Generic_Driver & operator=(const Compiler_Generic_Driver &)=delete
Compiler_Bytecode_Context bytecode_ctx_
void record_module_name_table_artifact()
void record_diagnostics_artifact()
const Compiler_Generic_Driver_Options & options() const noexcept
Returns the immutable driver configuration.
Compiler_Driver_Run_Output run_output_
void collect_frontend_state()
const DynArray< Compiler_Driver_Artifact > & artifacts() const noexcept
Returns all recorded artifacts from the last run.
void record_module_bindings_artifact()
const DynArray< std::string > & errors() const noexcept
Returns non-diagnostic driver-level hard errors.
const DynArray< std::string > & warnings() const noexcept
Returns non-fatal driver-level warnings.
void record_driver_messages_artifact()
void record_module_metadata_artifact()
DynArray< std::string > warnings_
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by exact label.
void record_text_artifact(const std::string &label, std::string text)
void record_import_order_artifact()
Compiler_Bytecode_Module * bytecode_module_
bool bind_host_function(const std::string &name, const Interpreter_Host_Function callback, void *user_data=nullptr)
Binds one host function into the persistent runtime prelude.
Compiler_Frontend * frontend_
const Compiler_Bytecode_Module * bytecode_module() const noexcept
Returns the lowered bytecode module from the last run, when available.
void append_run_failure_if_needed()
bool ok() const noexcept
Returns whether the last driver action finished successfully.
Compiler_C_Backend_Emission c_emission_
bool finalize(const Compiler_Driver_Action action)
const Compiler_C_Backend_Emission & c_emission() const noexcept
Returns the C emission payload from the last emit-c action.
void clear() noexcept
Clears all driver and frontend state while preserving host bindings.
DynArray< std::string > errors_
DynArray< Compiler_Driver_Artifact > artifacts_
Compiler_Generic_Driver(const Compiler_Generic_Driver &)=delete
Compiler_Driver_Action last_action() const noexcept
Returns the last requested action.
Compiler_Driver_Action last_action_
void append_error(const std::string &text)
size_t arena_size() const noexcept
Returns the configured bytecode arena size.
Interpreter_Runtime runtime_
void record_module_semantic_environment_artifact()
Compiler_Generic_Driver_Options config_
void record_module_linkage_artifact()
static bool read_file_text(const std::string &path, std::string &out_text)
const Compiler_Driver_Run_Output & run_output() const noexcept
Returns the run summary from the last run action.
bool execute_source(const std::string &name, const std::string &text, const Compiler_Driver_Action action=Compiler_Driver_Action::Run)
Convenience overload for a single in-memory source.
Compiler_Generic_Driver(Compiler_Frontend &frontend_impl, const Compiler_Generic_Driver_Options &options={}, const size_t arena_size=1<< 20)
Builds one generic driver over a concrete frontend implementation.
void render_plain(std::ostream &out) const
Renders all diagnostics as deterministic plain text.
void clear() noexcept
Empties the container.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
Structured runtime for executing typed HIR.
void set_diagnostics(Diagnostic_Engine *dx) noexcept
Sets the optional diagnostic sink for runtime failures.
bool bind_host_function(const std::string &name, const Interpreter_Host_Function callback, void *user_data=nullptr)
Defines one host function in the persistent prelude.
void reset() noexcept
Clears globals and the call stack while preserving host bindings.
Interpreter_Execution_Result evaluate_module(const Compiler_HIR_Module *module)
Evaluates one HIR module and leaves its globals installed.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
std::string render_messages(const DynArray< std::string > &errors, const DynArray< std::string > &warnings)
bool merge_report(const std::string &stage, const Report &report, DynArray< std::string > &errors, DynArray< std::string > &warnings)
std::string render_run_result(const Compiler_Driver_Run_Output &output)
void append_artifact(DynArray< Compiler_Driver_Artifact > &artifacts, const std::string &label, std::string text)
Main namespace for Aleph-w library functions.
Compiler_Driver_Action
High-level actions supported by compiler pipeline drivers.
@ Emit_C
Lower to IR and emit portable C.
@ IR
Stop after lowering to explicit IR.
@ Parse_Only
Stop after parsing and publish AST/frontend artifacts.
@ Run
Execute the lowered HIR module with Interpreter_Runtime.
@ Emit_Bytecode
Lower to IR and emit reusable bytecode.
@ Sema_Only
Stop after semantic analysis.
@ HIR
Stop after lowering to typed HIR.
Compiler_Module_Semantic_Environment compiler_build_module_semantic_environment(const Compiler_Module_Name_Table &table)
Builds one namespace-separated semantic environment from a name table.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
std::string compiler_render_module_metadata(const DynArray< Compiler_Module_Metadata > &modules)
Renders one deterministic text artifact for module metadata.
std::string compiler_render_module_semantic_environment(const Compiler_Module_Semantic_Environment &env)
Renders one deterministic text artifact for semantic environments.
and
Check uniqueness with explicit hash + equality functors.
std::string compiler_render_module_order(const DynArray< size_t > &order, const DynArray< Compiler_Module_Descriptor > &modules)
Renders one deterministic text artifact for a module order.
Compiler_Module_Name_Table compiler_build_module_name_table(const Compiler_Module_Binding_Result &bindings)
Builds one reusable per-module name table from bindings.
std::string compiler_dump_bytecode_module(const Compiler_Bytecode_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all lowered bytecode in one module deterministically.
std::string compiler_render_module_bindings(const Compiler_Module_Binding_Result &result)
Renders one deterministic text artifact for bound module names.
Compiler_Bytecode_Validation_Report validate_bytecode_module(const Compiler_Bytecode_Module &module)
Validates all bytecode functions in one module.
Compiler_Module_Link_Result compiler_link_module_surfaces(const DynArray< Compiler_Module_Descriptor > &descriptors, const DynArray< Compiler_Module_Metadata > &metadata, const DynArray< size_t > &order)
Links direct-import module surfaces into visible top-level views.
Compiler_Module_Binding_Result compiler_bind_module_surfaces(const Compiler_Module_Link_Result &linkage)
Binds linked module surfaces into one deterministic name view.
std::string compiler_render_module_linkage(const Compiler_Module_Link_Result &result)
Renders one deterministic text artifact for linked module surfaces.
bool(*)(void *user_data, const DynArray< Interpreter_Value > &arguments, Interpreter_Value &result, Interpreter_Runtime_Error &error) Interpreter_Host_Function
Host callback signature used by runtime built-ins or embeddings.
std::string compiler_render_module_name_table(const Compiler_Module_Name_Table &table)
Renders one deterministic text artifact for cached module names.
std::string interpreter_dump_globals(const Interpreter_Runtime &runtime)
Dumps the globals currently installed in one runtime.
std::string interpreter_value_to_string(const Interpreter_Value &value)
Formats one runtime value deterministically for dumps and tests.
Compiler_C_Backend_Emission compiler_emit_c_module(const Compiler_IR_Module *module, const Compiler_C_Backend_Options &options={}, const Compiler_Type_Context *types=nullptr)
Convenience wrapper that emits one IR module to portable C.
Bytecode module sharing global slot layout with the source IR module.
Result of one C backend emission request.
std::string source
Final generated C translation unit.
Options that control one C backend emission request.
Stable text artifact produced during one pipeline run.
Structured output of a run action.
std::string value_text
Stable rendering of the produced value.
std::string globals_text
Stable dump of the final globals environment.
Interpreter_Execution_Result result
Raw runtime result.
bool executed
Whether the current driver action actually executed code.
Configuration shared by all runs of Compiler_Generic_Driver.
Compiler_C_Backend_Options c_backend_options
Backend-C emission options used by emit-c.
Interpreter_Runtime_Options runtime_options
Runtime limits used by run.
Interpreter_Runtime_Error error
Structured runtime failure when flow == Runtime_Error.
Interpreter_Value value
Produced runtime value when relevant.
bool ok() const noexcept
Returns whether this result represents a runtime failure.
std::string code
Stable runtime code such as RUN004.
std::string message
Human-readable runtime message.
Runtime options for the HIR evaluator.