From 33716a33854a01f2c6497be5ececfb6fd98edb13 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 26 Apr 2024 05:51:30 +0800 Subject: [PATCH] cl: fake libc --- cl/_testcgo/hello/in.go | 10 ++++++++++ cl/_testcgo/hello/out.ll | 43 ++++++++++++++++++++++++++++++++++++++++ cl/internal/libc/libc.go | 14 +++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 cl/_testcgo/hello/in.go create mode 100644 cl/_testcgo/hello/out.ll create mode 100644 cl/internal/libc/libc.go diff --git a/cl/_testcgo/hello/in.go b/cl/_testcgo/hello/in.go new file mode 100644 index 00000000..e79caece --- /dev/null +++ b/cl/_testcgo/hello/in.go @@ -0,0 +1,10 @@ +package main + +import "github.com/goplus/llgo/cl/internal/libc" + +var format = [...]int8{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0} + +func main() { + sfmt := &format[0] + libc.Printf(sfmt, libc.Strlen(sfmt)) +} diff --git a/cl/_testcgo/hello/out.ll b/cl/_testcgo/hello/out.ll new file mode 100644 index 00000000..0975dbc3 --- /dev/null +++ b/cl/_testcgo/hello/out.ll @@ -0,0 +1,43 @@ +; ModuleID = 'main' +source_filename = "main" + +@main.format = global ptr null +@"main.init$guard" = global ptr null + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @"github.com/goplus/llgo/cl/internal/libc.init"() + store i8 72, ptr @main.format, align 1 + store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1 + store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1 + store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 3), align 1 + store i8 111, ptr getelementptr inbounds (i8, ptr @main.format, i64 4), align 1 + store i8 32, ptr getelementptr inbounds (i8, ptr @main.format, i64 5), align 1 + store i8 37, ptr getelementptr inbounds (i8, ptr @main.format, i64 6), align 1 + store i8 100, ptr getelementptr inbounds (i8, ptr @main.format, i64 7), align 1 + store i8 10, ptr getelementptr inbounds (i8, ptr @main.format, i64 8), align 1 + store i8 0, ptr getelementptr inbounds (i8, ptr @main.format, i64 9), align 1 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define void @main() { +_llgo_0: + call void @main.init() + %0 = call i32 @strlen(ptr @main.format) + call void (ptr, ...) @"github.com/goplus/llgo/cl/internal/libc.Printf"(ptr @main.format, i32 %0) + ret void +} + +declare void @"github.com/goplus/llgo/cl/internal/libc.init"() + +declare i32 @strlen(ptr) + +declare void @"github.com/goplus/llgo/cl/internal/libc.Printf"(ptr, ...) diff --git a/cl/internal/libc/libc.go b/cl/internal/libc/libc.go new file mode 100644 index 00000000..ef5d228a --- /dev/null +++ b/cl/internal/libc/libc.go @@ -0,0 +1,14 @@ +package libc + +import "C" +import _ "unsafe" + +const ( + LLGoPackage = true +) + +//go:linkname Printf printf +func Printf(format *int8, __llgo_va_list ...any) + +//go:linkname Strlen strlen +func Strlen(str *int8) C.int