https://github.com/tinybiggames/libllvm
libLLVM brings the full power of LLVM's compilation infrastructure directly to Delphi, providing native bindings for code generation, optimization, and linking with clean, Pascal-style integration.
https://github.com/tinybiggames/libllvm
codegen compiler compiler-construction compiler-design delphi lld llvm llvm-bindings pascal win64 windows11
Last synced: 4 months ago
JSON representation
libLLVM brings the full power of LLVM's compilation infrastructure directly to Delphi, providing native bindings for code generation, optimization, and linking with clean, Pascal-style integration.
- Host: GitHub
- URL: https://github.com/tinybiggames/libllvm
- Owner: tinyBigGAMES
- License: bsd-3-clause
- Created: 2025-08-31T18:23:28.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-08-31T19:34:56.000Z (4 months ago)
- Last Synced: 2025-08-31T20:33:41.951Z (4 months ago)
- Topics: codegen, compiler, compiler-construction, compiler-design, delphi, lld, llvm, llvm-bindings, pascal, win64, windows11
- Language: Pascal
- Homepage:
- Size: 19.9 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README

[](https://discord.gg/tinyBigGAMES) [](https://bsky.app/profile/tinybiggames.com)
> π§ **libLLVM is Work in Progress**
>
> libLLVM is currently under active development and evolving quickly. Some features described in this documentation may be incomplete, experimental, or subject to significant changes as the project matures.
>
> We welcome your feedback, ideas, and issue reports β your input will directly influence the direction and quality of libLLVM as we strive to build the ultimate LLVM integration for Delphi.
## LLVM Power, Delphi Simplicity
libLLVM brings the full power of **LLVM's compilation infrastructure directly to Delphi**, providing native bindings for code generation, optimization, and linking with clean, Pascal-style integration.
### Why libLLVM?
- **π Native LLVM Integration** - Direct access to LLVM's world-class compilation and optimization engine
- **π Built-in LLD Support** - Integrated linking with LLVM's modern linker for all target platforms
- **π Delphi-Native Design** - Clean Pascal syntax with proper resource management and UTF-8 handling
- **β‘ Zero-Copy Interop** - Efficient string marshalling and memory management for high-performance compilation
- **π§ Production Ready** - Robust error handling, proper cleanup, and battle-tested integration patterns
## Quick Example
```pascal
procedure CompileAndLink();
var
LArgs: TArray;
LRC: Integer;
LCanRunAgain: Boolean;
begin
// Generate LLVM IR and compile to object file
GenerateIRAndCompile('HelloWorld.ll', 'HelloWorld.obj');
// Link with LLD
LArgs := [
'lld-link',
'/nologo',
'/subsystem:console',
'/entry:main',
'/out:HelloWorld.exe',
'HelloWorld.obj',
'kernel32.lib',
'msvcrt.lib',
'legacy_stdio_definitions.lib'
];
LRC := LLDLink(LArgs, 'coff', LCanRunAgain);
if LRC = 0 then
Writeln('β
Compilation successful!')
else
Writeln('β Compilation failed with code: ', LRC);
end;
```
## Key Features
### π― **Complete LLVM Bindings**
- Full LLVM-C API coverage for contexts, modules, and IR generation
- Target machine support for all LLVM-supported architectures
- Memory buffer management with automatic cleanup
- Comprehensive error handling with proper Pascal exceptions
### π **Integrated LLD Linking**
```pascal
function LLDLink(const AArgs: array of string; const AFlavor: string;
out ACanRunAgain: Boolean): Integer;
var
LUTF8Args: TArray;
LArgv: TArray;
begin
// Robust UTF-8 string handling
SetLength(LUTF8Args, Length(AArgs));
SetLength(LArgv, Length(AArgs) + 1);
// Convert and null-terminate for C interop
for LIdx := 0 to High(AArgs) do
begin
LUTF8Args[LIdx] := UTF8String(AArgs[LIdx]);
LArgv[LIdx] := PUTF8Char(LUTF8Args[LIdx]);
end;
LArgv[High(LArgv)] := nil;
Result := LLD_Link(Length(LUTF8Args), @LArgv[0],
PUTF8Char(UTF8String(AFlavor)), @LCan);
end;
```
### βοΈ **Resource Management**
```pascal
// Automatic cleanup with proper try/finally blocks
LCtx := LLVMContextCreate();
LMod := nil;
LTM := nil;
try
// LLVM operations...
LMod := LLVMModuleCreateWithNameInContext(AsUTF8('hello'), LCtx);
LTM := LLVMCreateTargetMachine(/* ... */);
// Code generation...
finally
if LTM <> nil then LLVMDisposeTargetMachine(LTM);
if LMod <> nil then LLVMDisposeModule(LMod);
if LCtx <> nil then LLVMContextDispose(LCtx);
end;
```
### ποΈ **Modern Architecture Support**
```pascal
// Multi-target compilation
LLVMInitializeX86TargetInfo(); // x86/x64
LLVMInitializeARMTargetInfo(); // ARM/ARM64
LLVMInitializeWebAssemblyTarget(); // WebAssembly
// Flexible target specification
LTripleStr := 'x86_64-pc-windows-msvc'; // Windows
LTripleStr := 'x86_64-unknown-linux-gnu'; // Linux
LTripleStr := 'aarch64-apple-darwin'; // macOS ARM64
```
### π¦ **Cross-Platform Linking**
```pascal
// Windows COFF linking
LRC := LLDLink(LWindowsArgs, 'coff', LCanRunAgain);
// Linux ELF linking
LRC := LLDLink(LLinuxArgs, 'elf', LCanRunAgain);
// macOS Mach-O linking
LRC := LLDLink(LMacArgs, 'darwin', LCanRunAgain);
// WebAssembly linking
LRC := LLDLink(LWasmArgs, 'wasm', LCanRunAgain);
```
## Getting Started
1. **Include the LLVM headers** in your Delphi project
2. **Initialize target architectures** you plan to support
3. **Create LLVM contexts and modules** for your compilation units
4. **Generate IR or parse existing LLVM IR** files
5. **Compile to object files** using target machines
6. **Link with LLD** for final executable generation
## Projects using libLLVM
Hereβs a list of projects built with libLLVM. Want yours featured? Add it and submit a PR, or get in touch! And donβt forget to share what youβre working on in Show and tell.
- [LLVM-Simple-Calculator](https://github.com/hsauro/LLVM-Simple-Calculator)
- [LLVM-Simple-Calculator-Using-TLLVM](https://github.com/hsauro/LLVM-Simple-Calculator-Using-TLLVM)
---
**Built with β€οΈ by [tinyBigGAMES](https://tinybiggames.com)**
*"Where LLVM meets Pascal"*