Revived from samber/llvm_dart_binding
- Dart 3 compatible ✅
For platform specific implementation, check platforms.dart
- Tested: Mac 💻
- ALL platforms are supported with some configuration, including iOS and Android. Theoretically, not tested.
- compiled c file of LLVM
- .so for Linux and Android
- .dylib for Mac and iOS (included in /lib/c/)
- set the file path in platforms.dart
- extra steps for mobile builds, need to include the c file in Assets
The Dart LLVM bindings provide a direct interface to the LLVM C-API, enabling Dart developers to construct and manipulate LLVM IR (Intermediate Representation) efficiently. This library is useful for building compilers, static analyzers, or other tools that need to generate or manipulate low-level code.
- clone
[email protected]:monki1/llvm_dart.git
dependencies:
llvm:
path: /path/to/llvm_dart/
Below are some examples of how to use the Dart LLVM bindings:
import 'package:llvm/llvm.dart';
void main() {
final module = Module('my_module');
print('Created module: ${module.identifier}');
}
import 'package:llvm/llvm.dart';
void main() {
final module = Module('my_module');
final int64 = IntType(64);
final functionType = FunctionType(int64, [int64, int64]);
final sumFunction = module.addFunction('sum', functionType);
print('Added function: ${sumFunction}');
}
import 'package:llvm/llvm.dart';
void main() {
final module = Module('my_module');
final int64 = IntType(64);
final functionType = FunctionType(int64, [int64, int64]);
final function = module.addFunction('sum', functionType);
final bb = function.appendBasicBlock('entry');
final builder = InstructionBuilder()..atEndOf(bb);
final addResult = builder.add(function.getParam(0), function.getParam(1), 'tmp');
builder.returnValue(addResult);
llvm.linkInMcJit();
final engine = ExecutionEngine.forModule(module);
final result = engine.run(function, [
GenericValue.ofInt(int64, 3),
GenericValue.ofInt(int64, 4),
]);
print('Result from JIT function: ${result.toInt}');
llvm.dispose();
}
This library exposes several classes that correspond to LLVM's core concepts:
Context
: Management of the global compilation context.Module
: Container for functions and global variables.FunctionType
,IntType
: LLVM type representations.Function
,BasicBlock
,Value
: Components of functions.MemoryBuffer
: Handling of raw memory for modules.InstructionBuilder
: Tool for constructing instructions within basic blocks.ExecutionEngine
: Execution of compiled code.Allocator
: Memory management for FFI operations.
Contributions to the llvm
package are welcome. Please read the contributing guide on the GitHub repository for guidelines on pull requests, coding standards, and more.
This library is distributed under the MIT license. Full license text is available in the LICENSE file in the GitHub repository.