63#ifndef COMPILER_DRIVER_H
64#define COMPILER_DRIVER_H
97namespace Compiler_Driver_Detail {}
146 const std::string &
code,
148 const std::string ¬e =
"",
149 const std::string &help =
"")
152 if (
not note.empty())
154 if (
not help.empty())
169 return static_cast<size_t>(-1);
176 modules.
append({source_names_.access(i), {}});
182 for (
size_t i = 0; i < parsed_modules_.
size(); ++i)
186 record_import_order_artifact();
192 merge_order_.
clear();
195 for (
size_t i = 0; i < parsed_modules_.
size(); ++i)
198 module.name = source_names_.access(i);
201 for (
size_t j = 0; j <
parsed->imports.size(); ++j)
206 module.imports.append({decl->module_name, decl->module_name_span});
208 modules.
append(std::move(module));
212 for (
size_t i = 0; i < result.issues.size(); ++i)
214 const auto &
issue = result.issues.access(i);
216 emit_driver_diagnostic(
issue.span,
218 "cannot resolve import '" +
issue.dependency_name +
"'",
219 "known sources must match import names exactly");
221 emit_driver_diagnostic(
issue.span,
223 "cyclic import involving '" +
issue.module_name +
"' and '"
224 +
issue.dependency_name +
"'",
225 "imports currently form one acyclic source-order graph");
228 for (
size_t i = 0; i < result.order.size(); ++i)
229 merge_order_.
append(result.order.access(i));
242 for (
size_t i = 0; i < merge_order_.
size(); ++i)
244 const auto module_index = merge_order_.
access(i);
245 const auto *
module = parsed_modules_.access(module_index);
246 if (module ==
nullptr)
250 first_span = module->span;
251 if (module->span.is_valid())
254 for (
size_t j = 0; j <
module->type_declarations.size(); ++j)
256 for (
size_t j = 0; j <
module->functions.size(); ++j)
257 merged_ast_->
functions.append(module->functions.access(j));
258 for (
size_t j = 0; j <
module->statements.size(); ++j)
259 merged_ast_->
statements.append(module->statements.access(j));
268 std::ostringstream
out;
270 record_text_artifact(
"diagnostics",
out.str());
275 record_text_artifact(
"driver.messages",
281 parsed_modules_.
clear();
283 for (
size_t i = 0; i < source_file_ids_.
size(); ++i)
290 if (
not resolve_import_order())
293 for (
size_t i = 0; i < parsed_modules_.
size(); ++i)
297 merge_modules_from_order();
299 record_ast_artifacts();
313 hir_module_ =
lowering.lower_module(merged_ast_);
315 return hir_module_ !=
nullptr;
321 ir_module_ =
lowering.lower_module(hir_module_);
324 if (ir_module_ ==
nullptr)
326 append_error(
"ir: null IR module produced by lowering");
337 bytecode_module_ =
lowering.lower_module(ir_module_);
338 record_text_artifact(
"bytecode",
341 if (bytecode_module_ ==
nullptr)
343 append_error(
"bytecode: null bytecode module produced by lowering");
354 record_text_artifact(
"emit-c", c_emission_.
source);
370 record_text_artifact(
"run.globals", run_output_.
globals_text);
379 std::string
message =
"run: HIR runtime execution failed";
398 record_driver_messages_artifact();
399 record_diagnostics_artifact();
407 append_error(
"driver: no input sources were provided");
411 for (
size_t i = 0; i <
inputs.size(); ++i)
414 if (find_source_index(
input.name) !=
static_cast<size_t>(-1))
416 append_error(
"driver: duplicate input source name '" +
input.name +
"'");
433 const size_t arena_size = 1 << 20)
434 : config_(
options), diagnostics_(sources_), ast_ctx_(arena_size), hir_ctx_(arena_size),
435 ir_ctx_(arena_size), bytecode_ctx_(arena_size),
436 typed_(&diagnostics_,
options.typed_semantic_options),
437 runtime_(&diagnostics_,
options.runtime_options), arena_bytes_(arena_size)
458 parsed_modules_.
clear();
459 merge_order_.
clear();
460 source_names_.
clear();
461 source_file_ids_.
clear();
466 merged_ast_ =
nullptr;
467 hir_module_ =
nullptr;
468 ir_module_ =
nullptr;
469 bytecode_module_ =
nullptr;
477 diagnostics_.
clear();
481 bytecode_ctx_.
reset();
506 if (
not parse_registered_sources())
511 if (
not run_typed_semantics())
518 append_error(
"hir: failed to lower merged AST to HIR");
526 if (
not execute_hir_runtime())
527 append_run_failure_if_needed();
539 (
void) lower_bytecode();
554 append_error(
"driver: reached unreachable action dispatch");
570 append_error(
"driver: no input file paths were provided");
574 for (
size_t i = 0; i < paths.
size(); ++i)
576 const auto &path = paths.
access(i);
577 if (find_source_index(path) !=
static_cast<size_t>(-1))
579 append_error(
"driver: duplicate input file path '" + path +
"'");
582 source_names_.
append(path);
586 if (
not parse_registered_sources())
591 if (
not run_typed_semantics())
598 append_error(
"hir: failed to lower merged AST to HIR");
606 if (
not execute_hir_runtime())
607 append_run_failure_if_needed();
618 (
void) lower_bytecode();
628 append_error(
"driver: reached unreachable file-action dispatch");
634 const std::string &text,
675 return parsed_modules_;
699 return bytecode_module_;
739 for (
size_t i = artifacts_.
size(); i > 0; --i)
740 if (artifacts_.
access(i - 1).label == label)
741 return &artifacts_.
access(i - 1);
748 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.
Shared contracts used by reusable compiler frontends and drivers.
Lowering from the current MVP typed AST into the reusable HIR model.
Lowering from the current MVP HIR into the reusable explicit IR model.
Reusable source-name module dependency resolution helpers.
Recursive-descent parser for the compiler-support MVP grammar.
Reusable runtime and structured evaluator for HIR-based interpreters.
size_t size_t int32_t * out
Arena-backed ownership context for AST nodes.
T * make(Args &&...args)
Allocates and constructs an AST node of type T.
void reset() noexcept
Reclaims all memory and destroys managed nodes.
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.
Reusable orchestration object for compiler pipeline experiments.
void append_warning(const std::string &text)
Compiler_Ast_Context ast_ctx_
void clear() noexcept
Clears all stage outputs while preserving host prelude bindings.
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.
size_t arena_size() const noexcept
Returns the arena size shared by the internal contexts.
Diagnostic_Engine diagnostics_
Compiler_HIR_Module * hir_module_
const Compiler_Bytecode_Module * bytecode_module() const noexcept
Returns the lowered bytecode module from the last run, when available.
DynArray< Source_File_Id > source_file_ids_
Interpreter_Runtime runtime_
bool execute_hir_runtime()
std::string diagnostics_text() const
Renders diagnostics from the last run as plain text.
const Compiler_Module * merged_ast_module() const noexcept
Returns the merged AST module from the last run.
DynArray< std::string > warnings_
const Compiler_Driver_Options & options() const noexcept
Returns the immutable construction-time configuration.
Compiler_Driver(const Compiler_Driver &)=delete
void append_run_failure_if_needed()
const Compiler_Driver_Run_Output & run_output() const noexcept
Returns the run summary from the last run action.
const Diagnostic_Engine & diagnostic_engine() const noexcept
Returns the diagnostic engine populated by the last run.
const Compiler_C_Backend_Emission & c_emission() const noexcept
Returns the C emission payload from the last emit-c action.
DynArray< std::string > source_names_
void emit_driver_diagnostic(const Source_Span &span, const std::string &code, const std::string &message, const std::string ¬e="", const std::string &help="")
DynArray< Compiler_Driver_Artifact > artifacts_
void record_text_artifact(const std::string &label, std::string text)
void record_driver_messages_artifact()
Compiler_Driver & operator=(const Compiler_Driver &)=delete
void record_import_order_artifact()
Compiler_Driver_Options config_
const DynArray< Compiler_Module * > & parsed_ast_modules() const noexcept
Returns the parsed per-file AST modules from the last run.
size_t find_source_index(std::string_view name) const noexcept
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by label.
DynArray< std::string > errors_
const Compiler_Typed_Semantic_Analyzer & typed_analysis() const noexcept
Returns the typed semantic analyzer state from the last run.
void record_diagnostics_artifact()
bool ok() const noexcept
Returns whether the last driver action finished successfully.
Compiler_Typed_Semantic_Analyzer typed_
bool run_typed_semantics()
Compiler_C_Backend_Emission c_emission_
Compiler_Driver_Action last_action() const noexcept
Returns the last requested action.
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.
Compiler_Driver_Run_Output run_output_
Compiler_Bytecode_Module * bytecode_module_
const DynArray< std::string > & errors() const noexcept
Returns non-diagnostic driver-level hard errors.
bool resolve_import_order()
Compiler_Bytecode_Context bytecode_ctx_
void merge_modules_from_order()
Compiler_Module * merged_ast_
bool finalize(const Compiler_Driver_Action action)
Compiler_HIR_Context hir_ctx_
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.
void record_ast_artifacts()
const DynArray< Compiler_Driver_Artifact > & artifacts() const noexcept
Returns all recorded artifacts from the last run.
void append_error(const std::string &text)
const Compiler_IR_Module * ir_module() const noexcept
Returns the lowered IR module from the last run, when available.
Compiler_Driver(const Compiler_Driver_Options &options={}, const size_t arena_size=1<< 20)
Constructs a driver with one stable set of pipeline options.
DynArray< Compiler_Module * > parsed_modules_
const Source_Manager & source_manager() const noexcept
Returns the shared source manager for the last run.
bool prepare_inputs(const DynArray< Compiler_Driver_Source > &inputs)
const Compiler_HIR_Module * hir_module() const noexcept
Returns the lowered HIR module from the last run, when available.
Compiler_IR_Module * ir_module_
const DynArray< std::string > & warnings() const noexcept
Returns non-fatal driver-level warnings.
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.
bool parse_registered_sources()
Compiler_IR_Context ir_ctx_
Compiler_Driver_Action last_action_
DynArray< size_t > merge_order_
Arena-backed ownership context for HIR nodes.
void reset() noexcept
Destroys all tracked objects and rewinds the arena.
Lowers the MVP typed AST into HIR.
Arena-backed ownership context for IR nodes.
void reset() noexcept
Destroys all tracked objects and rewinds the arena.
Lowers typed HIR into explicit-value IR.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Inference-oriented semantic pass for the MVP compiler front-end.
void analyze_module(const Compiler_Module *module)
Runs the typed semantic analysis for module.
void clear() noexcept
Clears all inferred state and substitutions.
std::string dump_inference() const
Dumps inferred function, parameter, and binding types.
const Compiler_Type_Context & type_context() const noexcept
Returns the internal type context.
Diagnostic_Builder & code(const std::string &value)
Sets the stable diagnostic code.
size_t emit() const noexcept
Finalizes the builder and returns the diagnostic index.
Diagnostic_Builder & help(const std::string &msg)
Appends a help line.
Diagnostic_Builder & note(const std::string &msg)
Appends a note line.
Diagnostic accumulator and renderer.
void clear() noexcept
Removes all accumulated diagnostics and resets counters.
void render_plain(std::ostream &out) const
Renders all diagnostics as deterministic plain text.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
Diagnostic_Builder error(const Source_Span &span, const std::string &msg)
Starts an error diagnostic.
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.
Stores source files and resolves offsets into human-readable data.
Source_File_Id load_file(const std::string &path)
Loads a file from disk and registers it.
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
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)
std::string artifact_label(const std::string &prefix, const size_t index, const std::string &name)
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.
std::string compiler_dump_module(const Compiler_Module *module)
Dumps a parsed module in a deterministic text format.
Source_Span compiler_merge_spans(const Source_Span &lhs, const Source_Span &rhs) noexcept
Merges two spans from the same source file.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
std::string compiler_dump_ir_module(const Compiler_IR_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all lowered IR in one module deterministically.
@ Import
One concrete import "name"; declaration.
Compiler_IR_Validation_Report validate_ir_module(const Compiler_IR_Module &module)
Validates all functions in one lowered IR module.
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_Resolution_Result compiler_resolve_module_order(const DynArray< Compiler_Module_Descriptor > &modules)
Resolves one dependency-first order for exact-name module imports.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
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_dump_hir_module(const Compiler_HIR_Module *module, const Compiler_Type_Context &types)
Dumps one HIR module in a deterministic text format.
Compiler_Bytecode_Validation_Report validate_bytecode_module(const Compiler_Bytecode_Module &module)
Validates all bytecode functions in one module.
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 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.
static struct argp_option options[]
Source_Span span
Source region covered by this node.
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.
Immutable configuration shared by all runs of one driver object.
Compiler_C_Backend_Options c_backend_options
Backend-C emission options used by emit-c.
Compiler_Typed_Semantic_Options typed_semantic_options
Typed semantic options shared by the driver.
Compiler_Parser_Options parser_options
Parser configuration used for every parsed file.
Interpreter_Runtime_Options runtime_options
Runtime limits used by 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.
Lowered IR module with shared global slots and functions.
One module descriptor consumed by the reusable resolver.
Node representing a complete translation unit or module.
std::string source_name
User-facing source name that produced the module.
DynArray< Compiler_Type_Decl * > type_declarations
Top-level type declarations.
DynArray< Compiler_Stmt * > statements
Optional top-level code.
DynArray< Compiler_Function_Decl * > functions
Top-level function definitions.
Parser configuration knobs for the MVP grammar.
Options controlling the typed semantic pass.
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.
Half-open byte range inside a source file.
Lazy and scalable dynamic array implementation.