Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compiler_CFG_Lowering Class Reference

Transformation engine that lowers typed HIR into reusable CFGs. More...

#include <Compiler_CFG.H>

Collaboration diagram for Aleph::Compiler_CFG_Lowering:
[legend]

Classes

struct  Loop_Targets
 Internal state for loop lowering. More...
 

Public Member Functions

 Compiler_CFG_Lowering (Compiler_CFG_Context &ctx, const Compiler_Type_Context *type_ctx=nullptr) noexcept
 Builds a CFG lowerer over the specified context.
 
const Compiler_Type_Context * type_context () const noexcept
 Returns the optional type context used by this lowerer.
 
Compiler_CFG_Function * lower_function (const Compiler_HIR_Function *function)
 Lowers one HIR function into a CFG.
 
Compiler_CFG_Module * lower_module (const Compiler_HIR_Module *module)
 Lowers one HIR module into function CFGs and optional top-level CFG.
 

Private Member Functions

Compiler_CFG_Block & block (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id id) const
 Accesses a block by ID within a function.
 
Compiler_CFG_Block_Id create_block (Compiler_CFG_Function *function, std::string label) const
 Creates a new basic block in the given function.
 
void add_edge (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id from, const Compiler_CFG_Block_Id to) const
 Adds a directed edge from one block to another.
 
void append_linear_stmt (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id id, const Compiler_HIR_Stmt *stmt) const
 Appends a linear statement to a block and updates its span.
 
void set_jump (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id from, const Compiler_CFG_Block_Id to, const Source_Span &span={}) const
 Sets a Jump terminator on a block.
 
void set_branch (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id from, const Compiler_HIR_Expr *condition, const Compiler_CFG_Block_Id then_block, const Compiler_CFG_Block_Id else_block) const
 Sets a Branch terminator on a block.
 
void set_return (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id from, const Compiler_HIR_Expr *value, const Source_Span &span={}) const
 Sets a Return terminator on a block.
 
void set_exit (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id id) const
 Sets an Exit terminator (only for the canonical exit block).
 
void set_unreachable (Compiler_CFG_Function *function, const Compiler_CFG_Block_Id id, const Source_Span &span={}) const
 Sets an Unreachable terminator on a block.
 
Compiler_CFG_Block_Id ensure_current_block (Compiler_CFG_Function *function, Compiler_CFG_Block_Id current)
 Ensures a valid current block exists, creating a "dead" one if needed.
 
Compiler_CFG_Block_Id lower_stmt (Compiler_CFG_Function *function, const Compiler_HIR_Stmt *stmt, Compiler_CFG_Block_Id current, DynArray< Loop_Targets > &loops)
 Recursively lowers an HIR statement into the CFG.
 
Compiler_CFG_Function * lower_statement_sequence (const std::string &name, const Source_Span &span, const Compiler_Type_Id type_id, const Compiler_HIR_Function *source_function, const DynArray< Compiler_HIR_Stmt * > &statements)
 Lowers a sequence of statements into a new CFG function.
 

Static Private Member Functions

static void merge_span (Source_Span &dst, const Source_Span &src)
 Utility to merge a source span into a destination span.
 

Private Attributes

Compiler_CFG_Context * cfg_ = nullptr
 CFG owner.
 
const Compiler_Type_Context * types_ = nullptr
 Type metadata source.
 
size_t if_counter_ = 0
 ID generator for conditional blocks.
 
size_t while_counter_ = 0
 ID generator for loop blocks.
 
size_t dead_counter_ = 0
 ID generator for unreachable blocks.
 

Detailed Description

Transformation engine that lowers typed HIR into reusable CFGs.

Decomposes structured control flow (if, while, blocks) into a graph of basic blocks connected by explicit terminators.

Definition at line 346 of file Compiler_CFG.H.

Constructor & Destructor Documentation

◆ Compiler_CFG_Lowering()

Aleph::Compiler_CFG_Lowering::Compiler_CFG_Lowering ( Compiler_CFG_Context &  ctx,
const Compiler_Type_Context *  type_ctx = nullptr 
)
inlineexplicitnoexcept

Builds a CFG lowerer over the specified context.

Parameters
ctxOwnership context for the produced CFG nodes.
type_ctxOptional type context used by textual dumps later.

Definition at line 641 of file Compiler_CFG.H.

Member Function Documentation

◆ add_edge()

void Aleph::Compiler_CFG_Lowering::add_edge ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  from,
const Compiler_CFG_Block_Id  to 
) const
inlineprivate

Adds a directed edge from one block to another.

Definition at line 391 of file Compiler_CFG.H.

References block(), Aleph::blossom_maximum_cardinality_matching(), and Aleph::Compiler_CFG_Detail::contains_block_id().

Referenced by set_branch().

◆ append_linear_stmt()

void Aleph::Compiler_CFG_Lowering::append_linear_stmt ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  id,
const Compiler_HIR_Stmt *  stmt 
) const
inlineprivate

Appends a linear statement to a block and updates its span.

Definition at line 405 of file Compiler_CFG.H.

References block(), Aleph::blossom_maximum_cardinality_matching(), merge_span(), and Aleph::Compiler_HIR_Node::span.

Referenced by lower_stmt().

◆ block()

Compiler_CFG_Block & Aleph::Compiler_CFG_Lowering::block ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  id 
) const
inlineprivate

Accesses a block by ID within a function.

Definition at line 362 of file Compiler_CFG.H.

References Aleph::Compiler_CFG_Function::blocks.

Referenced by add_edge(), append_linear_stmt(), create_block(), lower_statement_sequence(), set_branch(), and set_exit().

◆ create_block()

Compiler_CFG_Block_Id Aleph::Compiler_CFG_Lowering::create_block ( Compiler_CFG_Function *  function,
std::string  label 
) const
inlineprivate

Creates a new basic block in the given function.

Definition at line 381 of file Compiler_CFG.H.

References block(), Aleph::Compiler_CFG_Function::blocks, Aleph::Compiler_CFG_Block::id, and Aleph::Compiler_CFG_Block::label.

Referenced by ensure_current_block(), lower_statement_sequence(), and lower_stmt().

◆ ensure_current_block()

Compiler_CFG_Block_Id Aleph::Compiler_CFG_Lowering::ensure_current_block ( Compiler_CFG_Function *  function,
Compiler_CFG_Block_Id  current 
)
inlineprivate

Ensures a valid current block exists, creating a "dead" one if needed.

Definition at line 480 of file Compiler_CFG.H.

References Aleph::compiler_cfg_invalid_block_id(), create_block(), and dead_counter_.

Referenced by lower_stmt().

◆ lower_function()

Compiler_CFG_Function * Aleph::Compiler_CFG_Lowering::lower_function ( const Compiler_HIR_Function *  function)
inline

◆ lower_module()

Compiler_CFG_Module * Aleph::Compiler_CFG_Lowering::lower_module ( const Compiler_HIR_Module *  module)
inline

Lowers one HIR module into function CFGs and optional top-level CFG.

Parameters
moduleTyped HIR module to lower.
Returns
Complete CFG module.

Definition at line 678 of file Compiler_CFG.H.

References ah_runtime_error_unless, Aleph::blossom_maximum_cardinality_matching(), cfg_, Aleph::Compiler_CFG_Module::functions, Aleph::Compiler_HIR_Module::functions, lower_function(), lower_statement_sequence(), Aleph::Compiler_CFG_Context::make(), Aleph::Compiler_HIR_Node::span, and Aleph::Compiler_HIR_Module::statements.

◆ lower_statement_sequence()

◆ lower_stmt()

◆ merge_span()

static void Aleph::Compiler_CFG_Lowering::merge_span ( Source_Span &  dst,
const Source_Span &  src 
)
inlinestaticprivate

Utility to merge a source span into a destination span.

Definition at line 368 of file Compiler_CFG.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_hir_merge_spans(), and Aleph::Source_Span::is_valid().

Referenced by append_linear_stmt(), and set_branch().

◆ set_branch()

void Aleph::Compiler_CFG_Lowering::set_branch ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  from,
const Compiler_HIR_Expr *  condition,
const Compiler_CFG_Block_Id  then_block,
const Compiler_CFG_Block_Id  else_block 
) const
inlineprivate

Sets a Branch terminator on a block.

Definition at line 429 of file Compiler_CFG.H.

References add_edge(), ah_runtime_error_unless, block(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Branch, merge_span(), and Aleph::Compiler_HIR_Node::span.

Referenced by lower_stmt().

◆ set_exit()

void Aleph::Compiler_CFG_Lowering::set_exit ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  id 
) const
inlineprivate

Sets an Exit terminator (only for the canonical exit block).

Definition at line 461 of file Compiler_CFG.H.

References block(), Aleph::blossom_maximum_cardinality_matching(), and Aleph::Exit.

Referenced by lower_statement_sequence().

◆ set_jump()

void Aleph::Compiler_CFG_Lowering::set_jump ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  from,
const Compiler_CFG_Block_Id  to,
const Source_Span &  span = {} 
) const
inlineprivate

Sets a Jump terminator on a block.

Definition at line 415 of file Compiler_CFG.H.

Referenced by lower_statement_sequence(), and lower_stmt().

◆ set_return()

void Aleph::Compiler_CFG_Lowering::set_return ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  from,
const Compiler_HIR_Expr *  value,
const Source_Span &  span = {} 
) const
inlineprivate

Sets a Return terminator on a block.

Definition at line 446 of file Compiler_CFG.H.

Referenced by lower_stmt().

◆ set_unreachable()

void Aleph::Compiler_CFG_Lowering::set_unreachable ( Compiler_CFG_Function *  function,
const Compiler_CFG_Block_Id  id,
const Source_Span &  span = {} 
) const
inlineprivate

Sets an Unreachable terminator on a block.

Definition at line 468 of file Compiler_CFG.H.

Referenced by lower_stmt().

◆ type_context()

const Compiler_Type_Context * Aleph::Compiler_CFG_Lowering::type_context ( ) const
inlinenoexcept

Returns the optional type context used by this lowerer.

Definition at line 647 of file Compiler_CFG.H.

References types_.

Member Data Documentation

◆ cfg_

Compiler_CFG_Context* Aleph::Compiler_CFG_Lowering::cfg_ = nullptr
private

CFG owner.

Definition at line 355 of file Compiler_CFG.H.

Referenced by lower_module(), and lower_statement_sequence().

◆ dead_counter_

size_t Aleph::Compiler_CFG_Lowering::dead_counter_ = 0
private

ID generator for unreachable blocks.

Definition at line 359 of file Compiler_CFG.H.

Referenced by ensure_current_block().

◆ if_counter_

size_t Aleph::Compiler_CFG_Lowering::if_counter_ = 0
private

ID generator for conditional blocks.

Definition at line 357 of file Compiler_CFG.H.

Referenced by lower_stmt().

◆ types_

const Compiler_Type_Context* Aleph::Compiler_CFG_Lowering::types_ = nullptr
private

Type metadata source.

Definition at line 356 of file Compiler_CFG.H.

Referenced by type_context().

◆ while_counter_

size_t Aleph::Compiler_CFG_Lowering::while_counter_ = 0
private

ID generator for loop blocks.

Definition at line 358 of file Compiler_CFG.H.

Referenced by lower_stmt().


The documentation for this class was generated from the following file: