From d49197cbe999d44a55878b86a6002a48cfb6829d Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 8 May 2024 11:49:19 +0800 Subject: [PATCH] llgen: support sqlite.ll --- internal/llgen/llgenf.go | 13 ++- x/sqlite/llgo.cfg | 4 +- x/sqlite/sqlite.go | 2 +- x/sqlite/sqlite.ll | 200 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 x/sqlite/sqlite.ll diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index 7326e468..11a0645d 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -75,9 +75,13 @@ func DoFile(fileOrPkg, outFile string) { func SmartDoFile(inFile string, pkgPath ...string) { dir, _ := filepath.Split(inFile) + absDir, _ := filepath.Abs(dir) + absDir = filepath.ToSlash(absDir) fname := "llgo_autogen.ll" - if inCompilerDir(dir) { + if inCompilerDir(absDir) { fname = "out.ll" + } else if inSqlite(absDir) { + fname = "sqlite.ll" } outFile := dir + fname @@ -89,6 +93,9 @@ func SmartDoFile(inFile string, pkgPath ...string) { } func inCompilerDir(dir string) bool { - dir, _ = filepath.Abs(dir) - return strings.Contains(filepath.ToSlash(dir), "/llgo/cl/") + return strings.Contains(dir, "/llgo/cl/") +} + +func inSqlite(dir string) bool { + return strings.HasSuffix(dir, "/llgo/x/sqlite") } diff --git a/x/sqlite/llgo.cfg b/x/sqlite/llgo.cfg index 718b191d..511ac2b5 100644 --- a/x/sqlite/llgo.cfg +++ b/x/sqlite/llgo.cfg @@ -4,7 +4,7 @@ "cd build.dir", "../sqlite/configure", "make", - "clang -emit-llvm -S -o ../llgo_autogen.ll -c sqlite3.c", - "zip ../llgo_autogen.lla ../llgo_autogen.ll" + "clang -emit-llvm -S -o sqlite3.ll -c sqlite3.c", + "$LLGO_LLVM_ROOT/bin/llvm-link -o ../llgo_autogen.bc ../sqlite.ll sqlite3.ll", ] } diff --git a/x/sqlite/sqlite.go b/x/sqlite/sqlite.go index 4e16c840..71fec9c0 100644 --- a/x/sqlite/sqlite.go +++ b/x/sqlite/sqlite.go @@ -29,7 +29,7 @@ type ( ) const ( - LLGoPackage = "noinit,link" + LLGoPackage = "link" ) // llgo:type C diff --git a/x/sqlite/sqlite.ll b/x/sqlite/sqlite.ll new file mode 100644 index 00000000..a8a85f19 --- /dev/null +++ b/x/sqlite/sqlite.ll @@ -0,0 +1,200 @@ +; ModuleID = 'github.com/goplus/llgo/x/sqlite' +source_filename = "github.com/goplus/llgo/x/sqlite" + +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } + +@"github.com/goplus/llgo/x/sqlite.init$guard" = global ptr null + +define ptr @"(github.com/goplus/llgo/x/sqlite.Errno).Errstr"(i32 %0) { +_llgo_0: + ret ptr null +} + +define ptr @"(*github.com/goplus/llgo/x/sqlite.Errno).Errstr"(ptr %0) { +_llgo_0: + %1 = load i32, ptr %0, align 4 + %2 = call ptr @"(github.com/goplus/llgo/x/sqlite.Errno).Errstr"(i32 %1) + ret ptr %2 +} + +define { ptr, i32 } @"github.com/goplus/llgo/x/sqlite.Open"(ptr %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = call i32 @sqlite3_open(ptr %0, ptr %1) + %3 = load ptr, ptr %1, align 8 + %mrv = insertvalue { ptr, i32 } poison, ptr %3, 0 + %mrv1 = insertvalue { ptr, i32 } %mrv, i32 %2, 1 + ret { ptr, i32 } %mrv1 +} + +define { ptr, i32 } @"github.com/goplus/llgo/x/sqlite.OpenV2"(ptr %0, i32 %1, ptr %2) { +_llgo_0: + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %4 = call i32 @sqlite3_open_v2(ptr %0, ptr %3, i32 %1, ptr %2) + %5 = load ptr, ptr %3, align 8 + %mrv = insertvalue { ptr, i32 } poison, ptr %5, 0 + %mrv1 = insertvalue { ptr, i32 } %mrv, i32 %4, 1 + ret { ptr, i32 } %mrv1 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Close"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).CloseV2"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Errcode"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define ptr @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Errmsg"(ptr %0) { +_llgo_0: + ret ptr null +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Exec"(ptr %0, ptr %1, { ptr, ptr } %2, ptr %3, ptr %4) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).ExtendedErrcode"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Prepare"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) { +_llgo_0: + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %4 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %5 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %6 = trunc i64 %5 to i32 + %7 = call i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepare"(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2) + %8 = load ptr, ptr %3, align 8 + %mrv = insertvalue { ptr, i32 } poison, ptr %8, 0 + %mrv1 = insertvalue { ptr, i32 } %mrv, i32 %7, 1 + ret { ptr, i32 } %mrv1 +} + +define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV2"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) { +_llgo_0: + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %4 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %5 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %6 = trunc i64 %5 to i32 + %7 = call i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepareV2"(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2) + %8 = load ptr, ptr %3, align 8 + %mrv = insertvalue { ptr, i32 } poison, ptr %8, 0 + %mrv1 = insertvalue { ptr, i32 } %mrv, i32 %7, 1 + ret { ptr, i32 } %mrv1 +} + +define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV3"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, i32 %2, ptr %3) { +_llgo_0: + %4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %5 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %6 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1) + %7 = trunc i64 %6 to i32 + %8 = call i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepareV3"(ptr %0, ptr %5, i32 %7, i32 %2, ptr %4, ptr %3) + %9 = load ptr, ptr %4, align 8 + %mrv = insertvalue { ptr, i32 } poison, ptr %9, 0 + %mrv1 = insertvalue { ptr, i32 } %mrv, i32 %8, 1 + ret { ptr, i32 } %mrv1 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepare"(ptr %0, ptr %1, i32 %2, ptr %3, ptr %4) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepareV2"(ptr %0, ptr %1, i32 %2, ptr %3, ptr %4) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).doPrepareV3"(ptr %0, ptr %1, i32 %2, i32 %3, ptr %4, ptr %5) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).BindInt"(ptr %0, i32 %1, i32 %2) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).BindInt64"(ptr %0, i32 %1, i64 %2) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).BindText"(ptr %0, i32 %1, ptr %2, i32 %3, { ptr, ptr } %4) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).Close"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).ColumnCount"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).ColumnInt"(ptr %0, i32 %1) { +_llgo_0: + ret i32 0 +} + +define i64 @"(*github.com/goplus/llgo/x/sqlite.Stmt).ColumnInt64"(ptr %0, i32 %1) { +_llgo_0: + ret i64 0 +} + +define ptr @"(*github.com/goplus/llgo/x/sqlite.Stmt).ColumnName"(ptr %0, i32 %1) { +_llgo_0: + ret ptr null +} + +define ptr @"(*github.com/goplus/llgo/x/sqlite.Stmt).ColumnText"(ptr %0, i32 %1) { +_llgo_0: + ret ptr null +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).Reset"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define i32 @"(*github.com/goplus/llgo/x/sqlite.Stmt).Step"(ptr %0) { +_llgo_0: + ret i32 0 +} + +define void @"github.com/goplus/llgo/x/sqlite.init"() { +_llgo_0: + %0 = load i1, ptr @"github.com/goplus/llgo/x/sqlite.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"github.com/goplus/llgo/x/sqlite.init$guard", align 1 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64) + +declare i32 @sqlite3_open(ptr, ptr) + +declare i32 @sqlite3_open_v2(ptr, ptr, i32, ptr) + +declare ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String") + +declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String")