This commit is contained in:
agra
2026-02-11 01:43:30 +02:00
parent 25e1372731
commit 89fc6427c4
6 changed files with 236 additions and 27 deletions

View File

@@ -4,11 +4,15 @@ const c = llvm.c;
pub const Builtins = struct {
printf_fn: c.LLVMValueRef,
calloc_fn: c.LLVMValueRef,
malloc_fn: c.LLVMValueRef,
free_fn: c.LLVMValueRef,
memcpy_fn: c.LLVMValueRef,
pub fn init(module: c.LLVMModuleRef, ctx: c.LLVMContextRef) Builtins {
const ptr_type = c.LLVMPointerTypeInContext(ctx, 0);
const i64_type = c.LLVMInt64TypeInContext(ctx);
const i32_type = c.LLVMInt32TypeInContext(ctx);
const void_type = c.LLVMVoidTypeInContext(ctx);
// Declare: int printf(const char*, ...)
var printf_params = [_]c.LLVMTypeRef{ptr_type};
@@ -20,6 +24,27 @@ pub const Builtins = struct {
const calloc_type = c.LLVMFunctionType(ptr_type, &calloc_params, 2, 0);
const calloc_fn = c.LLVMAddFunction(module, "calloc", calloc_type);
return .{ .printf_fn = printf_fn, .calloc_fn = calloc_fn };
// Declare: void* malloc(size_t size)
var malloc_params = [_]c.LLVMTypeRef{i64_type};
const malloc_type = c.LLVMFunctionType(ptr_type, &malloc_params, 1, 0);
const malloc_fn = c.LLVMAddFunction(module, "malloc", malloc_type);
// Declare: void free(void* ptr)
var free_params = [_]c.LLVMTypeRef{ptr_type};
const free_type = c.LLVMFunctionType(void_type, &free_params, 1, 0);
const free_fn = c.LLVMAddFunction(module, "free", free_type);
// Declare: void* memcpy(void* dst, const void* src, size_t n)
var memcpy_params = [_]c.LLVMTypeRef{ ptr_type, ptr_type, i64_type };
const memcpy_type = c.LLVMFunctionType(ptr_type, &memcpy_params, 3, 0);
const memcpy_fn = c.LLVMAddFunction(module, "memcpy", memcpy_type);
return .{
.printf_fn = printf_fn,
.calloc_fn = calloc_fn,
.malloc_fn = malloc_fn,
.free_fn = free_fn,
.memcpy_fn = memcpy_fn,
};
}
};