From 32bfb3d57e19799142dba23b023f7607cf30c982 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 8 Jun 2024 16:31:52 +0800 Subject: [PATCH] runtime.Defer: remove proc --- c/c.go | 10 +-- cl/_testgo/defer/out.ll | 126 ++++++++++++++++++------------------ internal/llgen/llgenf.go | 5 ++ internal/runtime/z_defer.go | 16 +---- ssa/eh.go | 14 ++-- 5 files changed, 81 insertions(+), 90 deletions(-) diff --git a/c/c.go b/c/c.go index 9726879f..b3239ccd 100644 --- a/c/c.go +++ b/c/c.go @@ -76,11 +76,6 @@ func GoStringData(string) *Char // ----------------------------------------------------------------------------- -//go:linkname Remove C.remove -func Remove(path *Char) Int - -// ----------------------------------------------------------------------------- - //go:linkname AllocaSigjmpBuf llgo.sigjmpbuf func AllocaSigjmpBuf() Pointer @@ -128,6 +123,11 @@ func Fflush(fp FilePtr) Int // ----------------------------------------------------------------------------- +//go:linkname Remove C.remove +func Remove(path *Char) Int + +// ----------------------------------------------------------------------------- + //go:linkname Time C.time func Time(*int32) int32 diff --git a/cl/_testgo/defer/out.ll b/cl/_testgo/defer/out.ll index 0a15d5ef..dddf331e 100644 --- a/cl/_testgo/defer/out.ll +++ b/cl/_testgo/defer/out.ll @@ -2,7 +2,7 @@ source_filename = "main" %"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } -%"github.com/goplus/llgo/internal/runtime.Defer" = type { { ptr, ptr }, i64, ptr, i64 } +%"github.com/goplus/llgo/internal/runtime.Defer" = type { i64, ptr, i64 } @"main.init$guard" = global ptr null @__llgo_argc = global ptr null @@ -39,72 +39,70 @@ define i32 @main(i32 %0, ptr %1) { _llgo_0: %2 = load ptr, ptr @__llgo_defer, align 8 %3 = call ptr @pthread_getspecific(ptr %2) - %4 = alloca i8, i64 40, align 1 + %4 = alloca i8, i64 24, align 1 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 0 - store ptr null, ptr %5, align 8 + store i64 0, ptr %5, align 4 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 1 - store i64 0, ptr %6, align 4 - %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 2 - store ptr %3, ptr %7, align 8 - %8 = call i32 @pthread_setspecific(ptr %2, ptr %4) + store ptr %3, ptr %6, align 8 + %7 = call i32 @pthread_setspecific(ptr %2, ptr %4) store i32 %0, ptr @__llgo_argc, align 4 store ptr %1, ptr @__llgo_argv, align 8 call void @"github.com/goplus/llgo/internal/runtime.init"() call void @main.init() - %9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 1 - %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 3 - %11 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 - %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %11, i32 0, i32 0 - store ptr @0, ptr %12, align 8 - %13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %11, i32 0, i32 1 - store i64 5, ptr %13, align 4 - %14 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %11, align 8 - %15 = call i1 @main.f(%"github.com/goplus/llgo/internal/runtime.String" %14) - br i1 %15, label %_llgo_2, label %_llgo_3 + %8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 0 + %9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, i32 0, i32 2 + %10 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %10, i32 0, i32 0 + store ptr @0, ptr %11, align 8 + %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %10, i32 0, i32 1 + store i64 5, ptr %12, align 4 + %13 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %10, align 8 + %14 = call i1 @main.f(%"github.com/goplus/llgo/internal/runtime.String" %13) + br i1 %14, label %_llgo_2, label %_llgo_3 _llgo_1: ; No predecessors! ret i32 0 _llgo_2: ; preds = %_llgo_0 - %16 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 - %17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 0 - store ptr @1, ptr %17, align 8 - %18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1 - store i64 5, ptr %18, align 4 - %19 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8 - %20 = load i64, ptr %9, align 4 - %21 = or i64 %20, 1 - store i64 %21, ptr %9, align 4 - %22 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 - %23 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %22, i32 0, i32 0 - store ptr @2, ptr %23, align 8 - %24 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %22, i32 0, i32 1 - store i64 3, ptr %24, align 4 - %25 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %22, align 8 - %26 = load i64, ptr %9, align 4 - %27 = or i64 %26, 2 - store i64 %27, ptr %9, align 4 - store i64 0, ptr %10, align 4 + %15 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %16 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %15, i32 0, i32 0 + store ptr @1, ptr %16, align 8 + %17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %15, i32 0, i32 1 + store i64 5, ptr %17, align 4 + %18 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %15, align 8 + %19 = load i64, ptr %8, align 4 + %20 = or i64 %19, 1 + store i64 %20, ptr %8, align 4 + %21 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %22 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %21, i32 0, i32 0 + store ptr @2, ptr %22, align 8 + %23 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %21, i32 0, i32 1 + store i64 3, ptr %23, align 4 + %24 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %21, align 8 + %25 = load i64, ptr %8, align 4 + %26 = or i64 %25, 2 + store i64 %26, ptr %8, align 4 + store i64 0, ptr %9, align 4 br label %_llgo_4 _llgo_3: ; preds = %_llgo_0 - %28 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 - %29 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %28, i32 0, i32 0 - store ptr @3, ptr %29, align 8 - %30 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %28, i32 0, i32 1 - store i64 5, ptr %30, align 4 - %31 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %28, align 8 - %32 = load i64, ptr %9, align 4 - %33 = or i64 %32, 4 - store i64 %33, ptr %9, align 4 - store i64 1, ptr %10, align 4 + %27 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %28 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %27, i32 0, i32 0 + store ptr @3, ptr %28, align 8 + %29 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %27, i32 0, i32 1 + store i64 5, ptr %29, align 4 + %30 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %27, align 8 + %31 = load i64, ptr %8, align 4 + %32 = or i64 %31, 4 + store i64 %32, ptr %8, align 4 + store i64 1, ptr %9, align 4 br label %_llgo_4 _llgo_4: ; preds = %_llgo_3, %_llgo_2 - %34 = load i64, ptr %9, align 4 - %35 = and i64 %34, 4 - %36 = icmp ne i64 %35, 0 - br i1 %36, label %_llgo_7, label %_llgo_8 + %33 = load i64, ptr %8, align 4 + %34 = and i64 %33, 4 + %35 = icmp ne i64 %34, 0 + br i1 %35, label %_llgo_7, label %_llgo_8 _llgo_5: ; preds = %_llgo_12 ret i32 0 @@ -113,37 +111,37 @@ _llgo_6: ; preds = %_llgo_12 ret i32 0 _llgo_7: ; preds = %_llgo_4 - call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %31) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %30) call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) br label %_llgo_8 _llgo_8: ; preds = %_llgo_7, %_llgo_4 - %37 = and i64 %34, 2 - %38 = icmp ne i64 %37, 0 - br i1 %38, label %_llgo_9, label %_llgo_10 + %36 = and i64 %33, 2 + %37 = icmp ne i64 %36, 0 + br i1 %37, label %_llgo_9, label %_llgo_10 _llgo_9: ; preds = %_llgo_8 - call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %25) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %24) call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) br label %_llgo_10 _llgo_10: ; preds = %_llgo_9, %_llgo_8 - %39 = and i64 %34, 1 - %40 = icmp ne i64 %39, 0 - br i1 %40, label %_llgo_11, label %_llgo_12 + %38 = and i64 %33, 1 + %39 = icmp ne i64 %38, 0 + br i1 %39, label %_llgo_11, label %_llgo_12 _llgo_11: ; preds = %_llgo_10 - call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %19) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %18) call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) br label %_llgo_12 _llgo_12: ; preds = %_llgo_11, %_llgo_10 call void @"main.main$1"() - %41 = load %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, align 8 - %42 = extractvalue %"github.com/goplus/llgo/internal/runtime.Defer" %41, 2 - %43 = call i32 @pthread_setspecific(ptr %2, ptr %42) - %44 = load i64, ptr %10, align 4 - switch i64 %44, label %_llgo_5 [ + %40 = load %"github.com/goplus/llgo/internal/runtime.Defer", ptr %4, align 8 + %41 = extractvalue %"github.com/goplus/llgo/internal/runtime.Defer" %40, 2 + %42 = call i32 @pthread_setspecific(ptr %2, i64 %41) + %43 = load i64, ptr %9, align 4 + switch i64 %43, label %_llgo_5 [ i64 1, label %_llgo_6 ] } diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index cabbc234..369eabd5 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -107,6 +107,11 @@ func SmartDoFile(inFile string, pkgPath ...string) { } outFile := dir + fname + b, err := os.ReadFile(outFile) + if err == nil && len(b) == 1 && b[0] == ';' { + return // skip to gen + } + if len(pkgPath) > 0 { Do(pkgPath[0], inFile, outFile) } else { diff --git a/internal/runtime/z_defer.go b/internal/runtime/z_defer.go index 007d4888..fce4230e 100644 --- a/internal/runtime/z_defer.go +++ b/internal/runtime/z_defer.go @@ -18,17 +18,7 @@ package runtime // Defer presents defer statements in a function. type Defer struct { - proc func(uintptr) - bits uintptr - link *Defer - rund int // index of RunDefers -} - -// DeferProc calls deferred statements. -func DeferProc(d *Defer) { - for d != nil { - d.proc(d.bits) - d = d.link - _ = d.rund - } + Bits uintptr + Link *Defer + Rund int // index of RunDefers } diff --git a/ssa/eh.go b/ssa/eh.go index f6beca0c..eb6d3be7 100644 --- a/ssa/eh.go +++ b/ssa/eh.go @@ -130,25 +130,23 @@ func (b Builder) getDefer() *aDefer { self := b.Func if self.defer_ == nil { // TODO(xsw): if in pkg.init? - // 0: proc func(uintptr) - // 1: bits uintptr - // 2: link *Defer - // 3: rund int + // 0: bits uintptr + // 1: link *Defer + // 2: rund int b.SetBlockEx(b.blk, AtStart, false) prog := b.Prog key := b.deferKey() - deferfn := prog.Null(prog.VoidPtr()) zero := prog.Val(uintptr(0)) link := b.pthreadGetspecific(key) - ptr := b.aggregateAlloca(prog.Defer(), deferfn.impl, zero.impl, link.impl) + ptr := b.aggregateAlloca(prog.Defer(), zero.impl, link.impl) deferData := Expr{ptr, prog.DeferPtr()} b.pthreadSetspecific(key, deferData) b.SetBlockEx(b.blk, AtEnd, false) self.defer_ = &aDefer{ key: key, data: deferData, - bitsPtr: b.FieldAddr(deferData, 1), - rundPtr: b.FieldAddr(deferData, 3), + bitsPtr: b.FieldAddr(deferData, 0), + rundPtr: b.FieldAddr(deferData, 2), procBlk: self.MakeBlock(), } }