cl: qsort example

This commit is contained in:
xushiwei
2024-05-03 19:59:56 +08:00
parent 424dbd9261
commit 8a7ddf4dc2
2 changed files with 43 additions and 21 deletions

View File

@@ -59,6 +59,13 @@ 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 i32 @"main.main$1"(ptr, ptr) define i32 @"main.main$1"(ptr %0, ptr %1) {
_llgo_0:
%2 = load i64, ptr %0, align 4
%3 = load i64, ptr %1, align 4
%4 = sub i64 %2, %3
%5 = trunc i64 %4 to i32
ret i32 %5
}
declare i32 @printf(ptr, ...) declare i32 @printf(ptr, ...)

View File

@@ -169,7 +169,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) {
for i, n := 0, mthds.Len(); i < n; i++ { for i, n := 0, mthds.Len(); i < n; i++ {
mthd := mthds.At(i) mthd := mthds.At(i)
if ssaMthd := prog.MethodValue(mthd); ssaMthd != nil { if ssaMthd := prog.MethodValue(mthd); ssaMthd != nil {
p.compileFunc(pkg, mthd.Obj().Pkg(), ssaMthd) p.compileFunc(pkg, mthd.Obj().Pkg(), ssaMthd, false)
} }
} }
} }
@@ -193,12 +193,17 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
} }
} }
func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function) { func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function, closure bool) llssa.Function {
sig := f.Signature var sig = f.Signature
name, ftype := p.funcName(pkgTypes, f, true) var name string
if closure {
name = funcName(pkgTypes, f)
} else {
var ftype int
name, ftype = p.funcName(pkgTypes, f, true)
switch ftype { switch ftype {
case ignoredFunc, llgoInstr: // llgo extended instructions case ignoredFunc, llgoInstr: // llgo extended instructions
return return nil
} }
if debugInstr { if debugInstr {
log.Println("==> NewFunc", name, "type:", sig.Recv(), sig) log.Println("==> NewFunc", name, "type:", sig.Recv(), sig)
@@ -206,6 +211,7 @@ func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa
if ftype == cFunc { if ftype == cFunc {
sig = llssa.CFuncDecl(sig) sig = llssa.CFuncDecl(sig)
} }
}
fn := pkg.NewFunc(name, sig) fn := pkg.NewFunc(name, sig)
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
p.fn = fn p.fn = fn
@@ -233,6 +239,7 @@ func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa
phi() phi()
} }
}) })
return fn
} }
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, doInit bool) llssa.BasicBlock { func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, doInit bool) llssa.BasicBlock {
@@ -587,11 +594,15 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
} }
} }
case *ssa.Function: case *ssa.Function:
fn := p.compileFunc(p.pkg, p.goTyps, v, true)
return fn.Expr
/*
fn, ftype := p.funcOf(v) fn, ftype := p.funcOf(v)
if ftype >= llgoInstrBase { if ftype >= llgoInstrBase {
panic("can't use llgo instruction as a value") panic("can't use llgo instruction as a value")
} }
return fn.Expr return fn.Expr
*/
case *ssa.Global: case *ssa.Global:
g := p.varOf(v) g := p.varOf(v)
return g.Expr return g.Expr
@@ -676,16 +687,20 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
// Do not try to build generic (non-instantiated) functions. // Do not try to build generic (non-instantiated) functions.
continue continue
} }
ctx.compileFunc(ret, member.Pkg.Pkg, member) ctx.compileFunc(ret, member.Pkg.Pkg, member, false)
case *ssa.Type: case *ssa.Type:
ctx.compileType(ret, member) ctx.compileType(ret, member)
case *ssa.Global: case *ssa.Global:
ctx.compileGlobal(ret, member) ctx.compileGlobal(ret, member)
} }
} }
for _, ini := range ctx.inits { for len(ctx.inits) > 0 {
inits := ctx.inits
ctx.inits = nil
for _, ini := range inits {
ini() ini()
} }
}
return return
} }