docs.rs failed to build bhc-codegen-0.2.4
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
bhc-codegen
Code generation backend for the Basel Haskell Compiler.
Overview
This crate provides the infrastructure for generating native code from the compiler's intermediate representations. The primary backend is LLVM, supporting full optimization and multi-target code generation.
Code Generation Pipeline
Standard Pipeline (Default/Server Profiles)
Core IR ──▶ LLVM IR ──▶ Object Code ──▶ Executable
Numeric Pipeline
Core IR ──▶ Tensor IR ──▶ Loop IR ──▶ LLVM IR ──▶ Object Code
Key Types
| Type | Description |
|---|---|
CodegenBackend |
Trait for code generation backends |
CodegenContext |
Context holding state during code generation |
CodegenModule |
A compilation unit in backend representation |
CodegenConfig |
Configuration for code generation |
LlvmBackend |
LLVM backend implementation |
Usage
Basic Code Generation
use ;
// Create the LLVM backend
let backend = new;
// Create a context for code generation
let config = default;
let ctx = backend.create_context?;
// Create a module
let module = ctx.create_module?;
// ... add functions and generate code ...
// Write output
module.write_to_file?;
Lowering Core IR
use ;
let backend = new;
let ctx = backend.create_context?;
// Lower Core IR to LLVM IR
let llvm_module = lower_core_module?;
// Generate object file
llvm_module.write_to_file?;
LLVM Backend Features
- Full optimization pipeline integration
- Target-specific code generation
- Debug information generation (DWARF)
- Link-time optimization (LTO) support
- Profile-guided optimization (PGO)
Output Types
| Type | Description |
|---|---|
Object |
Native object file (.o) |
Assembly |
Assembly source (.s) |
LlvmIr |
LLVM IR text (.ll) |
LlvmBitcode |
LLVM bitcode (.bc) |
Optimization Levels
use CodegenConfig;
use OptLevel;
let config = CodegenConfig ;
| Level | Description |
|---|---|
None |
No optimizations (-O0) |
Less |
Basic optimizations (-O1) |
Default |
Standard optimizations (-O2) |
Aggressive |
Aggressive optimizations (-O3) |
Size |
Optimize for size (-Os) |
SizeMin |
Minimal size (-Oz) |
Function Generation
use FunctionBuilder;
// Create a function
let func = module.add_function?;
let builder = new;
// Build basic blocks
let entry = builder.append_block;
builder.position_at_end;
// Add instructions
let sum = builder.build_add;
builder.build_return;
Calling Conventions
| Convention | Description |
|---|---|
C |
Standard C calling convention |
Fast |
Fast calling convention (caller-saved) |
Cold |
Cold function (rarely called) |
Tail |
Tail call optimized |
Debug Information
use CodegenConfig;
use DebugInfo;
let config = CodegenConfig ;
Vectorization
For numeric code, the codegen enables auto-vectorization:
use VectorizeConfig;
let config = VectorizeConfig ;
Error Types
Design Notes
- LLVM context is not thread-safe (one per thread)
- Modules can be compiled in parallel
- Target features affect generated code
- Debug info is optional and incremental
Related Crates
bhc-core- Input Core IRbhc-loop-ir- Input Loop IR (Numeric)bhc-target- Target specificationsbhc-linker- Links generated objectsbhc-driver- Compilation orchestration
Specification References
- H26-SPEC Section 3.5: Code Generation
- LLVM Language Reference Manual