From fc893ba4980dd793a205beb2e15092de1b67b771 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 29 Apr 2024 20:51:27 +0800 Subject: [PATCH] llgen bugfix --- cl/_testcgo/alloca/in.go | 2 +- cl/_testcgo/alloca/out.ll | 14 +++++--------- cl/_testcgo/any/in.go | 11 ++++------- cl/_testcgo/any/out.ll | 18 ++++-------------- cl/_testcgo/hello/out.ll | 4 ++-- cl/_testcgo/unreachable/out.ll | 8 ++------ internal/llgen/llgenf.go | 2 +- 7 files changed, 19 insertions(+), 40 deletions(-) diff --git a/cl/_testcgo/alloca/in.go b/cl/_testcgo/alloca/in.go index 83452977..8d601b52 100644 --- a/cl/_testcgo/alloca/in.go +++ b/cl/_testcgo/alloca/in.go @@ -10,5 +10,5 @@ func main() { s := c.String("Hi\n") s2 := c.Alloca(4) c.Memcpy(s2, unsafe.Pointer(s), 4) - c.Printf(c.String("%s"), s) + c.Printf(c.String("%s"), s2) } diff --git a/cl/_testcgo/alloca/out.ll b/cl/_testcgo/alloca/out.ll index 44cd672d..3a0f3101 100644 --- a/cl/_testcgo/alloca/out.ll +++ b/cl/_testcgo/alloca/out.ll @@ -1,9 +1,9 @@ ; ModuleID = 'main' source_filename = "main" -%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } - @"main.init$guard" = global ptr null +@0 = private unnamed_addr constant [4 x i8] c"Hi\0A\00", align 1 +@1 = private unnamed_addr constant [3 x i8] c"%s\00", align 1 define void @main.init() { _llgo_0: @@ -21,16 +21,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0 define void @main() { _llgo_0: call void @main.init() - %0 = call ptr @"github.com/goplus/llgo/internal/runtime/c.String"([4 x i8] c"Hi\0A\00") - %1 = alloca i8, i64 4, align 1 - %2 = call ptr @memcpy(ptr %1, ptr %0, i64 4) - %3 = call ptr @"github.com/goplus/llgo/internal/runtime/c.String"([3 x i8] c"%s\00") - %4 = call i32 (ptr, ...) @printf(ptr %3, ptr %0) + %0 = alloca i8, i64 4, align 1 + %1 = call ptr @memcpy(ptr %0, ptr @0, i64 4) + %2 = call i32 (ptr, ...) @printf(ptr @1, ptr %0) ret void } -declare ptr @"github.com/goplus/llgo/internal/runtime/c.String"(%"github.com/goplus/llgo/internal/runtime.String") - declare ptr @memcpy(ptr, ptr, i64) declare i32 @printf(ptr, ...) diff --git a/cl/_testcgo/any/in.go b/cl/_testcgo/any/in.go index 0c75725e..d6734fbd 100644 --- a/cl/_testcgo/any/in.go +++ b/cl/_testcgo/any/in.go @@ -1,16 +1,13 @@ package main -import _ "unsafe" +import ( + "github.com/goplus/llgo/internal/runtime/c" +) func incVal(a any) int { return a.(int) + 1 } -//go:linkname printf C.printf -func printf(format *int8, __llgo_va_list ...any) - -var format = [...]int8{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0} - func main() { - printf(&format[0], incVal(100)) + c.Printf(c.String("Hello %d\n"), incVal(100)) } diff --git a/cl/_testcgo/any/out.ll b/cl/_testcgo/any/out.ll index 132c18d4..3f495973 100644 --- a/cl/_testcgo/any/out.ll +++ b/cl/_testcgo/any/out.ll @@ -3,8 +3,8 @@ source_filename = "main" %"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } -@main.format = global ptr null @"main.init$guard" = global ptr null +@0 = private unnamed_addr constant [10 x i8] c"Hello %d\0A\00", align 1 define i64 @main.incVal(%"github.com/goplus/llgo/internal/runtime.iface" %0) { _llgo_0: @@ -21,16 +21,6 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"main.init$guard", align 1 - 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 @@ -42,14 +32,14 @@ _llgo_0: call void @main.init() %0 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(i64 100) %1 = call i64 @main.incVal(%"github.com/goplus/llgo/internal/runtime.iface" %0) - call void (ptr, ...) @printf(ptr @main.format, i64 %1) + %2 = call i32 (ptr, ...) @printf(ptr @0, i64 %1) ret void } -declare void @printf(ptr, ...) - declare i64 @"github.com/goplus/llgo/internal/runtime.I2Int"(%"github.com/goplus/llgo/internal/runtime.iface", ptr) declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64) declare %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr, i64) + +declare i32 @printf(ptr, ...) diff --git a/cl/_testcgo/hello/out.ll b/cl/_testcgo/hello/out.ll index 93ba0f3e..c8e0b5f1 100644 --- a/cl/_testcgo/hello/out.ll +++ b/cl/_testcgo/hello/out.ll @@ -31,10 +31,10 @@ 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) + call void (ptr, ...) @printf(ptr @main.format, i32 %0) ret void } declare i32 @strlen(ptr) -declare void @"github.com/goplus/llgo/cl/internal/libc.Printf"(ptr, ...) +declare void @printf(ptr, ...) diff --git a/cl/_testcgo/unreachable/out.ll b/cl/_testcgo/unreachable/out.ll index 52ff58db..1692ce52 100644 --- a/cl/_testcgo/unreachable/out.ll +++ b/cl/_testcgo/unreachable/out.ll @@ -1,9 +1,8 @@ ; ModuleID = 'main' source_filename = "main" -%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } - @"main.init$guard" = global ptr null +@0 = private unnamed_addr constant [7 x i8] c"Hello\0A\00", align 1 define void @main.foo() { _llgo_0: @@ -28,11 +27,8 @@ define void @main() { _llgo_0: call void @main.init() call void @main.foo() - %0 = call ptr @"github.com/goplus/llgo/internal/runtime/c.String"([7 x i8] c"Hello\0A\00") - %1 = call i32 (ptr, ...) @printf(ptr %0) + %0 = call i32 (ptr, ...) @printf(ptr @0) ret void } -declare ptr @"github.com/goplus/llgo/internal/runtime/c.String"(%"github.com/goplus/llgo/internal/runtime.String") - declare i32 @printf(ptr, ...) diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index 2950d77c..16990ae7 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -37,7 +37,7 @@ const ( func GenFrom(fileOrPkg string) string { cfg := &packages.Config{ - Mode: loadSyntax, + Mode: loadSyntax | packages.NeedDeps, } initial, err := packages.Load(cfg, fileOrPkg) check(err)