cl: callRuntimeInit; runtime: TracePanic

This commit is contained in:
xushiwei
2024-05-01 11:03:52 +08:00
parent 673b9d9a5c
commit 666808b427
31 changed files with 330 additions and 102 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"strings"
"github.com/goplus/llgo/internal/llgen"
"github.com/goplus/mod"
)
func main() {
dir, _, err := mod.FindGoMod(".")
check(err)
llgen.Verbose = false
llgenDir(dir + "/cl/_testrt")
llgenDir(dir+"/cl/_testdata", "")
}
func llgenDir(dir string, pkgPath ...string) {
fis, err := os.ReadDir(dir)
check(err)
for _, fi := range fis {
name := fi.Name()
if !fi.IsDir() || strings.HasPrefix(name, "_") {
continue
}
testDir := dir + "/" + name
fmt.Fprintln(os.Stderr, "llgen", testDir)
os.Chdir(testDir)
llgen.SmartDoFile("in.go", pkgPath...)
}
}
func check(err error) {
if err != nil {
panic(err)
}
}

View File

@@ -19,8 +19,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings"
"github.com/goplus/llgo/internal/llgen" "github.com/goplus/llgo/internal/llgen"
) )
@@ -31,24 +29,6 @@ func main() {
return return
} }
inFile := os.Args[1]
dir, _ := filepath.Split(inFile)
fname := "llgo_autogen.ll"
if inCompilerDir(dir) {
fname = "out.ll"
}
outFile := dir + fname
llgen.Init() llgen.Init()
if len(os.Args) >= 3 { llgen.SmartDoFile(os.Args[1], os.Args[2:]...)
llgen.Do(os.Args[2], inFile, outFile)
} else {
llgen.DoFile(inFile, outFile)
}
}
func inCompilerDir(dir string) bool {
dir, _ = filepath.Abs(dir)
return strings.Contains(filepath.ToSlash(dir), "/llgo/cl/")
} }

View File

@@ -18,6 +18,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call i64 @main.max(i64 1, i64 2) %0 = call i64 @main.max(i64 1, i64 2)
ret void ret void
@@ -34,3 +35,5 @@ _llgo_1: ; preds = %_llgo_0
_llgo_2: ; preds = %_llgo_0 _llgo_2: ; preds = %_llgo_0
ret i64 %1 ret i64 %1
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -27,6 +27,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64 2, i64 100) %0 = call i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64 2, i64 100)
call void (ptr, ...) @printf(ptr @main.hello) call void (ptr, ...) @printf(ptr @main.hello)
@@ -35,6 +36,8 @@ _llgo_0:
declare void @"github.com/goplus/llgo/cl/internal/stdio.init"() declare void @"github.com/goplus/llgo/cl/internal/stdio.init"()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64, i64) declare i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64, i64)
declare void @printf(ptr, ...) declare void @printf(ptr, ...)

View File

@@ -42,6 +42,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call i64 @"(main.T).Add"(i64 1, i64 2) %0 = call i64 @"(main.T).Add"(i64 1, i64 2)
call void (ptr, ...) @printf(ptr @main.format, i64 %0) call void (ptr, ...) @printf(ptr @main.format, i64 %0)
@@ -49,3 +50,5 @@ _llgo_0:
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -26,9 +26,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
call void (ptr, ...) @printf(ptr @main.hello) call void (ptr, ...) @printf(ptr @main.hello)
ret void ret void
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -29,9 +29,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
call void (ptr, ...) @printf(ptr @main.format, i64 100) call void (ptr, ...) @printf(ptr @main.format, i64 100)
ret void ret void
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -35,9 +35,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
call void @"(*main.T).Print"(ptr @main.format, i64 100) call void @"(*main.T).Print"(ptr @main.format, i64 100)
ret void ret void
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -20,6 +20,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
br i1 false, label %_llgo_1, label %_llgo_2 br i1 false, label %_llgo_1, label %_llgo_2
@@ -30,3 +31,5 @@ _llgo_1: ; preds = %_llgo_0
_llgo_2: ; preds = %_llgo_1, %_llgo_0 _llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -20,6 +20,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = load i64, ptr @main.a, align 4 %0 = load i64, ptr @main.a, align 4
%1 = add i64 %0, 1 %1 = add i64 %0, 1
@@ -27,3 +28,5 @@ _llgo_0:
%2 = load i64, ptr @main.a, align 4 %2 = load i64, ptr @main.a, align 4
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -20,6 +20,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = alloca i8, i64 4, align 1 %0 = alloca i8, i64 4, align 1
%1 = call ptr @memcpy(ptr %0, ptr @0, i64 4) %1 = call ptr @memcpy(ptr %0, ptr @0, i64 4)
@@ -27,6 +28,8 @@ _llgo_0:
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @memcpy(ptr, ptr, i64) declare ptr @memcpy(ptr, ptr, i64)
declare i32 @printf(ptr, ...) declare i32 @printf(ptr, ...)

View File

@@ -28,6 +28,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello() %0 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello()
%1 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %0) %1 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %0)
@@ -40,6 +41,8 @@ _llgo_0:
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64) declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice") declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice")
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String") declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")

View File

@@ -29,6 +29,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 2) %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 2)
%1 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr %0, i64 100) %1 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr %0, i64 100)
@@ -41,6 +42,8 @@ declare i64 @"github.com/goplus/llgo/internal/runtime.I2Int"(%"github.com/goplus
declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64) declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr, i64) declare %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr, i64)
declare i32 @printf(ptr, ...) declare i32 @printf(ptr, ...)

View File

@@ -26,6 +26,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%1 = getelementptr inbounds i64, ptr %0, i64 0 %1 = getelementptr inbounds i64, ptr %0, i64 0
@@ -52,6 +53,8 @@ _llgo_0:
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64) declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64)
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr, i64, i64) declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr, i64, i64)

View File

@@ -19,9 +19,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
call void (ptr, ...) @printf(ptr @0) call void (ptr, ...) @printf(ptr @0)
ret void ret void
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -29,12 +29,15 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call i32 @strlen(ptr @main.format) %0 = call i32 @strlen(ptr @main.format)
call void (ptr, ...) @printf(ptr @main.format, i32 %0) call void (ptr, ...) @printf(ptr @main.format, i32 %0)
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @strlen(ptr) declare i32 @strlen(ptr)
declare void @printf(ptr, ...) declare void @printf(ptr, ...)

View File

@@ -18,9 +18,12 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() %0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"()
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"()

5
cl/_testrt/panic/in.go Normal file
View File

@@ -0,0 +1,5 @@
package main
func main() {
panic("panic message")
}

39
cl/_testrt/panic/out.ll Normal file
View File

@@ -0,0 +1,39 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
@"main.init$guard" = global ptr null
@0 = private unnamed_addr constant [14 x i8] c"panic message\00", align 1
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
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define void @main() {
_llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%0 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 13)
%1 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %0)
call void @"github.com/goplus/llgo/internal/runtime.TracePanic"(%"github.com/goplus/llgo/internal/runtime.iface" %1)
unreachable
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
declare %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.TracePanic"(%"github.com/goplus/llgo/internal/runtime.iface")

View File

@@ -29,6 +29,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call i32 @strlen(ptr @main.format) %0 = call i32 @strlen(ptr @main.format)
call void (ptr, ...) @printf(ptr @main.format, i32 %0) call void (ptr, ...) @printf(ptr @main.format, i32 %0)
@@ -38,3 +39,5 @@ _llgo_0:
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare i32 @strlen(ptr) declare i32 @strlen(ptr)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -56,6 +56,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = alloca %main.Foo, align 8 %0 = alloca %main.Foo, align 8
%1 = getelementptr inbounds %main.Foo, ptr %0, i32 0, i32 0 %1 = getelementptr inbounds %main.Foo, ptr %0, i32 0, i32 0
@@ -68,3 +69,5 @@ _llgo_0:
} }
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -21,6 +21,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%1 = getelementptr inbounds i64, ptr %0, i64 0 %1 = getelementptr inbounds i64, ptr %0, i64 0
@@ -60,6 +61,8 @@ _llgo_3: ; preds = %_llgo_1
ret i64 %2 ret i64 %2
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64) declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64)
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr, i64, i64) declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr, i64, i64)

View File

@@ -45,6 +45,7 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%1 = getelementptr inbounds { i32, i1 }, ptr %0, i32 0, i32 0 %1 = getelementptr inbounds { i32, i1 }, ptr %0, i32 0, i32 0
@@ -57,4 +58,6 @@ _llgo_0:
declare void @printf(ptr, ...) declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64) declare ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64)

View File

@@ -25,10 +25,13 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main() { define void @main() {
_llgo_0: _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
call void @main.foo() call void @main.foo()
%0 = call i32 (ptr, ...) @printf(ptr @0) %0 = call i32 (ptr, ...) @printf(ptr @0)
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @printf(ptr, ...) declare i32 @printf(ptr, ...)

View File

@@ -231,8 +231,9 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, doInit bo
ret := p.fn.Block(block.Index) ret := p.fn.Block(block.Index)
b.SetBlock(ret) b.SetBlock(ret)
if doInit { if doInit {
fn := p.pkg.FuncOf("main.init") pkg := p.pkg
b.Call(fn.Expr) callRuntimeInit(b, pkg)
b.Call(pkg.FuncOf("main.init").Expr)
} }
for _, instr := range block.Instrs { for _, instr := range block.Instrs {
p.compileInstr(b, instr) p.compileInstr(b, instr)
@@ -240,6 +241,15 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, doInit bo
return ret return ret
} }
const (
RuntimeInit = llssa.PkgRuntime + ".init"
)
func callRuntimeInit(b llssa.Builder, pkg llssa.Package) {
fn := pkg.NewFunc(RuntimeInit, types.NewSignatureType(nil, nil, nil, nil, nil, false))
b.Call(fn.Expr)
}
func isAny(t types.Type) bool { func isAny(t types.Type) bool {
if t, ok := t.(*types.Interface); ok { if t, ok := t.(*types.Interface); ok {
return t.Empty() return t.Empty()

View File

@@ -66,7 +66,9 @@ func Gen(pkgPath, inFile string, src any) string {
&types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions) &types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions)
check(err) check(err)
if Verbose {
ssaPkg.WriteTo(os.Stderr) ssaPkg.WriteTo(os.Stderr)
}
prog := llssa.NewProgram(nil) prog := llssa.NewProgram(nil)
ret, err := cl.NewPackage(prog, ssaPkg, files) ret, err := cl.NewPackage(prog, ssaPkg, files)
@@ -80,3 +82,7 @@ func check(err error) {
panic(err) panic(err)
} }
} }
var (
Verbose = true
)

View File

@@ -19,6 +19,8 @@ package llgen
import ( import (
"go/types" "go/types"
"os" "os"
"path/filepath"
"strings"
"github.com/goplus/llgo/cl" "github.com/goplus/llgo/cl"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
@@ -66,3 +68,23 @@ func DoFile(fileOrPkg, outFile string) {
err := os.WriteFile(outFile, []byte(ret), 0644) err := os.WriteFile(outFile, []byte(ret), 0644)
check(err) check(err)
} }
func SmartDoFile(inFile string, pkgPath ...string) {
dir, _ := filepath.Split(inFile)
fname := "llgo_autogen.ll"
if inCompilerDir(dir) {
fname = "out.ll"
}
outFile := dir + fname
if len(pkgPath) > 0 {
Do(pkgPath[0], inFile, outFile)
} else {
DoFile(inFile, outFile)
}
}
func inCompilerDir(dir string) bool {
dir, _ = filepath.Abs(dir)
return strings.Contains(filepath.ToSlash(dir), "/llgo/cl/")
}

View File

@@ -13,6 +13,9 @@ source_filename = "github.com/goplus/llgo/internal/runtime"
@"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null @"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null
@"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null
@0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1 @0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1
@1 = private unnamed_addr constant [19 x i8] c"MakeAnyString(%p)\0A\00", align 1
@2 = private unnamed_addr constant [11 x i8] c"Panic(%p)\0A\00", align 1
@3 = private unnamed_addr constant [11 x i8] c"panic: %s\0A\00", align 1
define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) { define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) {
_llgo_0: _llgo_0:
@@ -108,51 +111,52 @@ _llgo_1: ; preds = %_llgo_0
_llgo_2: ; preds = %_llgo_0 _llgo_2: ; preds = %_llgo_0
%11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 20) %11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 20)
%12 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %11) %12 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %11)
call void @"github.com/goplus/llgo/internal/runtime.TracePanic"(%"github.com/goplus/llgo/internal/runtime.iface" %12)
unreachable unreachable
} }
define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAny"(ptr %0, ptr %1) { define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAny"(ptr %0, ptr %1) {
_llgo_0: _llgo_0:
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 0 %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 0
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %4 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 0 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 1
%6 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 2
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 1 %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 4
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 2 %8 = getelementptr inbounds i64, ptr %7, i64 0
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 4
%10 = getelementptr inbounds i64, ptr %9, i64 0
store ptr %6, ptr %5, align 8
store ptr %0, ptr %7, align 8
store i32 0, ptr %8, align 4
store i64 0, ptr %10, align 4
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
store ptr %4, ptr %3, align 8 store ptr %4, ptr %3, align 8
store ptr %0, ptr %5, align 8
store i32 0, ptr %6, align 4
store i64 0, ptr %8, align 4
%9 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
%10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 0
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 1
store ptr %2, ptr %10, align 8
store ptr %1, ptr %11, align 8 store ptr %1, ptr %11, align 8
%12 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, align 8 %12 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, align 8
ret %"github.com/goplus/llgo/internal/runtime.iface" %12 ret %"github.com/goplus/llgo/internal/runtime.iface" %12
} }
define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr %0, i64 %1) { define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr %0, i64 %1) {
_llgo_0: _llgo_0:
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 0 %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 0
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %4 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 0 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 1
%6 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 2
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 1 %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 4
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 2 %8 = getelementptr inbounds i64, ptr %7, i64 0
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 4
%10 = getelementptr inbounds i64, ptr %9, i64 0
store ptr %6, ptr %5, align 8
store ptr %0, ptr %7, align 8
store i32 0, ptr %8, align 4
store i64 0, ptr %10, align 4
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
%12 = inttoptr i64 %1 to ptr
store ptr %4, ptr %3, align 8 store ptr %4, ptr %3, align 8
store ptr %0, ptr %5, align 8
store i32 0, ptr %6, align 4
store i64 0, ptr %8, align 4
%9 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
%10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 0
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 1
%12 = inttoptr i64 %1 to ptr
store ptr %2, ptr %10, align 8
store ptr %12, ptr %11, align 8 store ptr %12, ptr %11, align 8
%13 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, align 8 %13 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, align 8
ret %"github.com/goplus/llgo/internal/runtime.iface" %13 ret %"github.com/goplus/llgo/internal/runtime.iface" %13
} }
@@ -160,45 +164,46 @@ define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo
_llgo_0: _llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8 store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24)
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 0 %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 0
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 0 %5 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8
%6 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 1 %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 2
%8 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24) %8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 4
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 2 %9 = getelementptr inbounds i64, ptr %8, i64 0
%10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %4, i32 0, i32 4 store ptr %5, ptr %4, align 8
%11 = getelementptr inbounds i64, ptr %10, i64 0 store ptr %2, ptr %6, align 8
store ptr %6, ptr %5, align 8 store i32 0, ptr %7, align 4
store ptr %8, ptr %7, align 8 store i64 0, ptr %9, align 4
store i32 0, ptr %9, align 4 %10 = call i32 (ptr, ...) @printf(ptr @1, ptr %2)
store i64 0, ptr %11, align 4 %11 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
%12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1 %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, i32 0, i32 0
store ptr %4, ptr %3, align 8 %13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, i32 0, i32 1
store ptr %1, ptr %12, align 8 store ptr %3, ptr %12, align 8
%13 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, align 8 store ptr %1, ptr %13, align 8
ret %"github.com/goplus/llgo/internal/runtime.iface" %13 %14 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, align 8
ret %"github.com/goplus/llgo/internal/runtime.iface" %14
} }
define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeInterface"(ptr %0, ptr %1, ptr %2) { define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeInterface"(ptr %0, ptr %1, ptr %2) {
_llgo_0: _llgo_0:
%3 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %3, i32 0, i32 0 %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 0
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %5, i32 0, i32 0 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 2
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %5, i32 0, i32 1 %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 4
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %5, i32 0, i32 2 %8 = getelementptr inbounds i64, ptr %7, i64 0
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %5, i32 0, i32 4 store ptr %0, ptr %4, align 8
%10 = getelementptr inbounds i64, ptr %9, i64 0 store ptr %1, ptr %5, align 8
store ptr %0, ptr %6, align 8 store i32 0, ptr %6, align 4
store ptr %1, ptr %7, align 8 store i64 0, ptr %8, align 4
store i32 0, ptr %8, align 4 %9 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
store i64 0, ptr %10, align 4 %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 0
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %3, i32 0, i32 1 %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, i32 0, i32 1
store ptr %5, ptr %4, align 8 store ptr %3, ptr %10, align 8
store ptr %2, ptr %11, align 8 store ptr %2, ptr %11, align 8
%12 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %3, align 8 %12 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %9, align 8
ret %"github.com/goplus/llgo/internal/runtime.iface" %12 ret %"github.com/goplus/llgo/internal/runtime.iface" %12
} }
@@ -281,6 +286,41 @@ _llgo_0:
ret i64 %3 ret i64 %3
} }
define void @"github.com/goplus/llgo/internal/runtime.TracePanic"(%"github.com/goplus/llgo/internal/runtime.iface" %0) {
_llgo_0:
%1 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
store %"github.com/goplus/llgo/internal/runtime.iface" %0, ptr %1, align 8
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 0
%3 = load ptr, ptr %2, align 8
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1
%5 = load ptr, ptr %4, align 8
%6 = call i32 (ptr, ...) @printf(ptr @2, ptr %5)
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 0
%8 = load ptr, ptr %7, align 8
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %8, i32 0, i32 1
%10 = load ptr, ptr %9, align 8
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %10, i32 0, i32 6
%12 = load i8, ptr %11, align 1
%13 = sext i8 %12 to i64
%14 = icmp eq i64 %13, 24
br i1 %14, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_2, %_llgo_0
ret void
_llgo_2: ; preds = %_llgo_0
%15 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 1
%16 = load ptr, ptr %15, align 8
%17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1
%18 = load i64, ptr %17, align 4
%19 = add i64 %18, 1
%20 = alloca i8, i64 %19, align 1
%21 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8
%22 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %20, %"github.com/goplus/llgo/internal/runtime.String" %21)
%23 = call i32 (ptr, ...) @printf(ptr @3, ptr %22)
br label %_llgo_1
}
define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) { define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) {
_llgo_0: _llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
@@ -386,6 +426,8 @@ declare ptr @malloc(i64)
declare ptr @memcpy(ptr, ptr, i64) declare ptr @memcpy(ptr, ptr, i64)
declare i32 @printf(ptr, ...)
declare i32 @rand() declare i32 @rand()
declare void @"github.com/goplus/llgo/internal/abi.init"() declare void @"github.com/goplus/llgo/internal/abi.init"()

View File

@@ -19,6 +19,7 @@ package runtime
import ( import (
"unsafe" "unsafe"
"github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime/c" "github.com/goplus/llgo/internal/runtime/c"
) )
@@ -27,18 +28,15 @@ func Alloc(size uintptr) unsafe.Pointer {
return c.Malloc(size) return c.Malloc(size)
} }
/* // TracePanic prints panic message.
// Panic panics with a value. func TracePanic(v Interface) {
func Panic(v Interface) { c.Printf(c.Str("Panic(%p)\n"), v.tab._type)
c.Printf(c.String("Panic!!!\n"))
kind := abi.Kind(v.tab._type.Kind_) kind := abi.Kind(v.tab._type.Kind_)
switch { switch {
case kind == abi.String: case kind == abi.String:
s := (*String)(v.data) s := (*String)(v.data)
cs := c.Alloca(uintptr(s.len) + 1) cs := c.Alloca(uintptr(s.len) + 1)
c.Memcpy(cs, s.data, uintptr(s.len)) c.Printf(c.Str("panic: %s\n"), CStrCopy(cs, *s))
(*[1 << 30]int8)(cs)[s.len] = 0
c.Printf(c.String("%s\n"), cs)
} }
// TODO(xsw): other message type
} }
*/

View File

@@ -20,6 +20,7 @@ import (
"unsafe" "unsafe"
"github.com/goplus/llgo/internal/abi" "github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime/c"
) )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -35,29 +36,35 @@ var (
type Interface = iface type Interface = iface
func MakeAnyInt(typ *Type, data uintptr) Interface { func MakeAnyInt(typ *Type, data uintptr) Interface {
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{ return Interface{
tab: &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}, tab: tab,
data: unsafe.Pointer(data), data: unsafe.Pointer(data),
} }
} }
func MakeAnyString(data string) Interface { func MakeAnyString(data string) Interface {
typ := Basic(abi.String)
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
c.Printf(c.Str("MakeAnyString(%p)\n"), typ)
return Interface{ return Interface{
tab: &itab{inter: TyAny, _type: Basic(abi.String), hash: 0, fun: [1]uintptr{0}}, tab: tab,
data: unsafe.Pointer(&data), data: unsafe.Pointer(&data),
} }
} }
func MakeAny(typ *Type, data unsafe.Pointer) Interface { func MakeAny(typ *Type, data unsafe.Pointer) Interface {
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{ return Interface{
tab: &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}, tab: tab,
data: data, data: data,
} }
} }
func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface { func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface {
tab := &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{ return Interface{
tab: &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}}, tab: tab,
data: data, data: data,
} }
} }

View File

@@ -73,7 +73,9 @@ func (b Builder) Panic(v Expr) {
if debugInstr { if debugInstr {
log.Printf("Panic %v\n", v.impl) log.Printf("Panic %v\n", v.impl)
} }
b.impl.CreateUnreachable() // TODO(xsw): pass v pkg := b.fn.pkg
b.Call(pkg.rtFunc("TracePanic"), v)
b.impl.CreateUnreachable()
} }
// Unreachable emits an unreachable instruction. // Unreachable emits an unreachable instruction.