sun::codegen::variables::VariableGenerator

sun::codegen::variables::VariableGenerator

class · Source (opens in a new tab)

class sun::codegen::variables::VariableGenerator

Emits variable creation, reference and assignment, the lvalue addresses assignments write through, and the static initialization of globals.

Types

Functions

codegen

codegen(const CompoundAssignmentAST &expr)

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegen(const CompoundAssignmentAST &expr)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: CompoundAssignmentAST

codegen(const T &)=delete

private · function · Source (opens in a new tab)

template <typename T>
llvm::Value * sun::codegen::variables::VariableGenerator::codegen(const T &)=delete

A node kind with its own overload must not silently bind to the ExprAST forwarder above.

Make it a compile error instead.

Related: ExprAST, BlockExprAST, VariableCreationAST, sun::ast::VariableReferenceAST, sun::ast::VariableAssignmentAST, sun::ast::ReferenceCreationAST, CompoundAssignmentAST, ExprAST

codegen(const sun::ast::ReferenceCreationAST &expr)

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegen(const sun::ast::ReferenceCreationAST &expr)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: sun::ast::ReferenceCreationAST

codegen(const BlockExprAST &block)

private · function · Source (opens in a new tab)

llvm::Value * sun::codegen::variables::VariableGenerator::codegen(const BlockExprAST &block)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: BlockExprAST

codegen(const ExprAST &expr)

private · function · Source (opens in a new tab)

llvm::Value * sun::codegen::variables::VariableGenerator::codegen(const ExprAST &expr)

What variable codegen borrows from the rest of codegen.

The BlockExprAST overload matters: without it a block would bind to codegen(const ExprAST&), which attaches an expression debug location the block path does not want.

Related: ExprAST, BlockExprAST, codegen(const ExprAST&)

codegen(const sun::ast::VariableReferenceAST &expr)

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegen(const sun::ast::VariableReferenceAST &expr)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: sun::ast::VariableReferenceAST

codegen(const sun::ast::VariableAssignmentAST &expr)

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegen(const sun::ast::VariableAssignmentAST &expr)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: sun::ast::VariableAssignmentAST

codegen(const VariableCreationAST &expr)

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegen(const VariableCreationAST &expr)

Emits LLVM instructions for this syntax node and returns its generated value.

Related: VariableCreationAST

Public Functions

VariableGenerator

VariableGenerator(const VariableGenerator &)=delete

public · function · Source (opens in a new tab)

sun::codegen::variables::VariableGenerator::VariableGenerator(const VariableGenerator &)=delete

Binds variable generation to the shared expression visitor and state.

VariableGenerator(sun::codegen::CodegenState &state, sun::codegen::CodegenVisitor &gen)

public · function · Source (opens in a new tab)

sun::codegen::variables::VariableGenerator::VariableGenerator(sun::codegen::CodegenState &state, sun::codegen::CodegenVisitor &gen)

Binds variable generation to the shared expression visitor and state.

Related: sun::codegen::CodegenState, sun::codegen::CodegenVisitor

assignToVariableSlot

public · function · Source (opens in a new tab)

void sun::codegen::variables::VariableGenerator::assignToVariableSlot(llvm::Value *slot, llvm::Value *value, const TypePtr &varType, const std::string &name)

Assigns an already-evaluated value to a variable slot (a local alloca or a global).

A compound value drops what the slot held and MOVES the source in; self-assignment emits nothing.

Related: TypePtr

codegenAddress

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegenAddress(const ExprAST &expr)

Emits the storage address of an addressable expression.

Related: ExprAST

codegenBorrowAddress

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::codegenBorrowAddress(const ExprAST &expr)

Also handles conditional reference bindings, whose address is a phi of the branches'.

Only borrow bindings take that path.

Related: ExprAST

codegenObjectPtr

public · function · Source (opens in a new tab)

std::pair< Value *, ClassType * > sun::codegen::variables::VariableGenerator::codegenObjectPtr(const ExprAST &object)

Codegens a member-access object down to (objectPtr, ClassType*), applying the generic-this fixup and unwrapping raw_ptr/static_ptr/ref to class.

ClassType* is null when the object is not class-shaped.

Related: ClassType, ExprAST

createLoadForGlobalVar

public · function · Source (opens in a new tab)

llvm::LoadInst * sun::codegen::variables::VariableGenerator::createLoadForGlobalVar(DeclarationId id)

Loads the value stored for a global declaration.

Related: DeclarationId

createLoadForLocalVar

public · function · Source (opens in a new tab)

llvm::LoadInst * sun::codegen::variables::VariableGenerator::createLoadForLocalVar(DeclarationId id)

Loads the value stored for a local declaration.

Related: DeclarationId

createLoadForRef

public · function · Source (opens in a new tab)

llvm::Value * sun::codegen::variables::VariableGenerator::createLoadForRef(DeclarationId id, const sun::types::ReferenceType &refType)

Loads a value through the reference stored for a declaration.

Related: DeclarationId, sun::types::ReferenceType

createStoreForRef

public · function · Source (opens in a new tab)

void sun::codegen::variables::VariableGenerator::createStoreForRef(DeclarationId id, const sun::types::ReferenceType &refType, llvm::Value *value)

Stores a value through the reference bound to a declaration.

Related: DeclarationId, sun::types::ReferenceType

declareBlockGlobals

public · function · Source (opens in a new tab)

void sun::codegen::variables::VariableGenerator::declareBlockGlobals(const BlockExprAST &block)

Creates the storage of every global a block declares before any body is emitted, because a function may use a global declared further down.

Function signatures must already be declared: a global holding a lambda emits the lambda's body here.

Related: BlockExprAST

emitStaticInitFunction

public · function · Source (opens in a new tab)

void sun::codegen::variables::VariableGenerator::emitStaticInitFunction(uint32_t initOrder, const std::string &bundleHash)

Emits the static initialization function for the globals that could not be constant-initialized.

Call after all top-level codegen, before main.

initOrder places the function among those of other bundles: lower values run first, and a module's value is one more than the highest among its imports. bundleHash names the bundle being built and is empty for a program; a bundle's function runs at most once even when its code reaches a program through several imports.

findGlobal

public · function · Source (opens in a new tab)

llvm::GlobalVariable * sun::codegen::variables::VariableGenerator::findGlobal(DeclarationId id) const

Find storage for the global selected during semantic analysis.

Related: DeclarationId

operator=

public · function · Source (opens in a new tab)

VariableGenerator & sun::codegen::variables::VariableGenerator::operator=(const VariableGenerator &)=delete

Disallows assignment so ownership and object identity cannot be duplicated.

Related: VariableGenerator

tryCodegenAddress

public · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::tryCodegenAddress(const ExprAST &expr)

The storage address of an assignable expression.

tryCodegenAddress returns nullptr for shapes with no addressable slot (class index targets, slices, closure captures, temporaries); codegenAddress throws instead. Neither ever spills a value to a temporary alloca.

Related: ExprAST

Private Functions

bindGlobal

private · function · Source (opens in a new tab)

llvm::GlobalVariable * sun::codegen::variables::VariableGenerator::bindGlobal(DeclarationId id, llvm::GlobalVariable *global)

Bind a created or imported global to its source declaration.

Related: DeclarationId

buildLlvmConstant

private · function · Source (opens in a new tab)

llvm::Constant * sun::codegen::variables::VariableGenerator::buildLlvmConstant(const sun::semantic_analysis::constants::ConstantValue &value, llvm::Type *type, const sun::support::Position &location)

Builds the LLVM constant of type type for a value analysis computed.

A string adds a private byte array to the module for its text. Fails compilation at location when the value does not have the shape of the type, which means analysis and code generation disagree.

Related: sun::semantic_analysis::constants::ConstantValue, sun::support::Position

classes

private · function · Source (opens in a new tab)

sun::codegen::classes::ClassGenerator & sun::codegen::variables::VariableGenerator::classes()

Provides the generator for class storage and method operations.

Related: sun::codegen::classes::ClassGenerator

convertToVariableType

private · function · Source (opens in a new tab)

llvm::Value * sun::codegen::variables::VariableGenerator::convertToVariableType(llvm::Value *value, llvm::Type *varType, const TypePtr &valueSunType)

Brings a number to the type of the variable it initializes: a narrower integer widens by the signedness of valueSunType, a wider one keeps its low bits, and a float is rounded between the two float widths.

Any other value is returned unchanged.

Related: TypePtr

createEntryBlockAlloca

private · function · Source (opens in a new tab)

llvm::AllocaInst * sun::codegen::variables::VariableGenerator::createEntryBlockAlloca(llvm::Function *func, llvm::StringRef varName, llvm::Type *type)

Allocates local storage in the function entry block.

createGlobalVariable

private · function · Source (opens in a new tab)

GlobalVariable * sun::codegen::variables::VariableGenerator::createGlobalVariable(DeclarationId id, const std::string &name, llvm::Type *type, llvm::Constant *initializer)

Creates and registers LLVM storage for a global with a known value.

Related: DeclarationId

debugDeclareLocal

private · function · Source (opens in a new tab)

void sun::codegen::variables::VariableGenerator::debugDeclareLocal(llvm::AllocaInst *alloca, const std::string &name, const TypePtr &type, const sun::support::Position &loc)

Associates local storage with its source variable for debugging.

Related: TypePtr, sun::support::Position

emitCompoundOpValue

private · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::emitCompoundOpValue(const CompoundAssignmentAST &expr, llvm::Value *cur, llvm::Type *slotTy, const TypePtr &slotSunType)

Compound assignment: address-once -> load -> op -> store.

Related: CompoundAssignmentAST, TypePtr

emitImageGlobal

private · function · Source (opens in a new tab)

llvm::GlobalVariable * sun::codegen::variables::VariableGenerator::emitImageGlobal(const VariableCreationAST &expr, llvm::Type *varType, const sun::semantic_analysis::constants::ConstantValue &value)

Creates storage for a file-scope variable whose value analysis computed, and writes that value into the program image.

No code is emitted.

Related: VariableCreationAST, sun::semantic_analysis::constants::ConstantValue

emitStartupGlobal

private · function · Source (opens in a new tab)

GlobalVariable * sun::codegen::variables::VariableGenerator::emitStartupGlobal(const VariableCreationAST &expr, llvm::Type *varType)

Creates zeroed storage for a file-scope variable whose value cannot be computed at compile time, and queues its initializer for the startup function.

A class value always takes this path: its constructor runs at startup.

Related: VariableCreationAST

functionGen

private · function · Source (opens in a new tab)

sun::codegen::functions::FunctionGenerator & sun::codegen::variables::VariableGenerator::functionGen()

Provides the generator for function bodies and callable values.

Related: sun::codegen::functions::FunctionGenerator

functions

private · function · Source (opens in a new tab)

sun::codegen::functions::FunctionRegistry & sun::codegen::variables::VariableGenerator::functions()

Provides the registry of generated functions and their metadata.

Related: sun::codegen::functions::FunctionRegistry

genFunctionVariable

private · function · Source (opens in a new tab)

Value * sun::codegen::variables::VariableGenerator::genFunctionVariable(const VariableCreationAST &expr)

Creates storage for a variable holding a callable value.

Related: VariableCreationAST

genLocalVar

private · function · Source (opens in a new tab)

llvm::Value * sun::codegen::variables::VariableGenerator::genLocalVar(const VariableCreationAST &expr, llvm::Type *varType)

Allocates and initializes storage for a local variable.

Related: VariableCreationAST

scopes

private · function · Source (opens in a new tab)

sun::codegen::scopes::ScopeManager & sun::codegen::variables::VariableGenerator::scopes()

Provides the scope manager responsible for variable storage and cleanup.

Related: sun::codegen::scopes::ScopeManager

Private Fields

ctx

private · variable · Source (opens in a new tab)

sun::codegen::CodegenContext& sun::codegen::variables::VariableGenerator::ctx

No documentation comment.

Related: sun::codegen::CodegenContext

debugInfo

private · variable · Source (opens in a new tab)

sun::codegen::DebugInfoBuilder& sun::codegen::variables::VariableGenerator::debugInfo

No documentation comment.

Related: sun::codegen::DebugInfoBuilder

gen_

private · variable · Source (opens in a new tab)

sun::codegen::CodegenVisitor& sun::codegen::variables::VariableGenerator::gen_

No documentation comment.

Related: sun::codegen::CodegenVisitor

globals_

private · variable · Source (opens in a new tab)

std::unordered_map<DeclarationId, llvm::WeakTrackingVH> sun::codegen::variables::VariableGenerator::globals_

No documentation comment.

Related: DeclarationId

module

private · variable · Source (opens in a new tab)

llvm::Module* sun::codegen::variables::VariableGenerator::module

No documentation comment.

state_

private · variable · Source (opens in a new tab)

sun::codegen::CodegenState& sun::codegen::variables::VariableGenerator::state_

No documentation comment.

Related: sun::codegen::CodegenState

staticInits

private · variable · Source (opens in a new tab)

std::vector<StaticInitInfo> sun::codegen::variables::VariableGenerator::staticInits

No documentation comment.

Related: StaticInitInfo

typeResolver

private · variable · Source (opens in a new tab)

sun::codegen::LLVMTypeResolver& sun::codegen::variables::VariableGenerator::typeResolver

No documentation comment.

Related: sun::codegen::LLVMTypeResolver