llgo/ssa: Alloc, BinOp(vkPtr)

This commit is contained in:
xushiwei
2024-04-28 07:08:01 +08:00
parent 475f0fa2ff
commit 5415f68c1b
4 changed files with 32 additions and 12 deletions

View File

@@ -17,15 +17,15 @@
package runtime package runtime
import ( import (
"go/types"
"unsafe" "unsafe"
"github.com/goplus/llgo/internal/abi" "github.com/goplus/llgo/internal/abi"
) )
type Kind = abi.Kind
type Type = abi.Type type Type = abi.Type
func Basic(kind types.BasicKind) *Type { func Basic(kind Kind) *Type {
return basicTypes[kind] return basicTypes[kind]
} }

View File

@@ -23,6 +23,7 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"log" "log"
"unsafe"
"github.com/goplus/llvm" "github.com/goplus/llvm"
) )
@@ -76,10 +77,12 @@ func llvmValues(vals []Expr) []llvm.Value {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Null returns a null constant expression.
func (p Program) Null(t Type) Expr { func (p Program) Null(t Type) Expr {
return Expr{llvm.ConstNull(t.ll), t} return Expr{llvm.ConstNull(t.ll), t}
} }
// BoolVal returns a boolean constant expression.
func (p Program) BoolVal(v bool) Expr { func (p Program) BoolVal(v bool) Expr {
t := p.Bool() t := p.Bool()
var bv uint64 var bv uint64
@@ -90,15 +93,19 @@ func (p Program) BoolVal(v bool) Expr {
return Expr{ret, t} return Expr{ret, t}
} }
// IntVal returns an integer constant expression.
func (p Program) IntVal(v uint64, t Type) Expr { func (p Program) IntVal(v uint64, t Type) Expr {
ret := llvm.ConstInt(t.ll, v, false) ret := llvm.ConstInt(t.ll, v, false)
return Expr{ret, t} return Expr{ret, t}
} }
// Val returns a constant expression.
func (p Program) Val(v interface{}) Expr { func (p Program) Val(v interface{}) Expr {
switch v := v.(type) { switch v := v.(type) {
case int: case int:
return p.IntVal(uint64(v), p.Int()) return p.IntVal(uint64(v), p.Int())
case uintptr:
return p.IntVal(uint64(v), p.Uintptr())
case bool: case bool:
return p.BoolVal(v) return p.BoolVal(v)
case float64: case float64:
@@ -109,6 +116,7 @@ func (p Program) Val(v interface{}) Expr {
panic("todo") panic("todo")
} }
// Const returns a constant expression.
func (b Builder) Const(v constant.Value, typ Type) Expr { func (b Builder) Const(v constant.Value, typ Type) Expr {
if v == nil { if v == nil {
return b.prog.Null(typ) return b.prog.Null(typ)
@@ -258,7 +266,7 @@ func (b Builder) BinOp(op token.Token, x, y Expr) Expr {
case vkSigned: case vkSigned:
pred := intPredOpToLLVM[op-predOpBase] pred := intPredOpToLLVM[op-predOpBase]
return Expr{llvm.CreateICmp(b.impl, pred, x.impl, y.impl), tret} return Expr{llvm.CreateICmp(b.impl, pred, x.impl, y.impl), tret}
case vkUnsigned: case vkUnsigned, vkPtr:
pred := uintPredOpToLLVM[op-predOpBase] pred := uintPredOpToLLVM[op-predOpBase]
return Expr{llvm.CreateICmp(b.impl, pred, x.impl, y.impl), tret} return Expr{llvm.CreateICmp(b.impl, pred, x.impl, y.impl), tret}
case vkFloat: case vkFloat:
@@ -386,9 +394,11 @@ func (b Builder) Alloc(t Type, heap bool) (ret Expr) {
if heap { if heap {
ret.impl = llvm.CreateAlloca(b.impl, telem.ll) ret.impl = llvm.CreateAlloca(b.impl, telem.ll)
} else { } else {
panic("todo") pkg := b.fn.pkg
size := unsafe.Sizeof(t.t)
ret = b.Call(pkg.rtFunc("Alloc"), b.prog.Val(size))
} }
// TODO: zero-initialize // TODO(xsw): zero-initialize
ret.Type = t ret.Type = t
return return
} }

View File

@@ -116,6 +116,7 @@ type aProgram struct {
anyTy Type anyTy Type
voidTy Type voidTy Type
boolTy Type boolTy Type
uintptrTy Type
intTy Type intTy Type
f64Ty Type f64Ty Type
} }
@@ -211,6 +212,14 @@ func (p Program) Int() Type {
return p.intTy return p.intTy
} }
// Uintptr returns uintptr type.
func (p Program) Uintptr() Type {
if p.uintptrTy == nil {
p.uintptrTy = p.Type(types.Typ[types.Uintptr])
}
return p.uintptrTy
}
// Float64 returns float64 type. // Float64 returns float64 type.
func (p Program) Float64() Type { func (p Program) Float64() Type {
if p.f64Ty == nil { if p.f64Ty == nil {

View File

@@ -39,6 +39,7 @@ const (
vkComplex vkComplex
vkString vkString
vkBool vkBool
vkPtr
vkFunc vkFunc
vkTuple vkTuple
vkDelayExpr = -1 vkDelayExpr = -1
@@ -237,11 +238,11 @@ func (p Program) toLLVMType(typ types.Type) Type {
case types.Complex128: case types.Complex128:
case types.String: case types.String:
case types.UnsafePointer: case types.UnsafePointer:
return &aType{p.tyVoidPtr(), typ, vkInvalid} return &aType{p.tyVoidPtr(), typ, vkPtr}
} }
case *types.Pointer: case *types.Pointer:
elem := p.Type(t.Elem()) elem := p.Type(t.Elem())
return &aType{llvm.PointerType(elem.ll, 0), typ, vkInvalid} return &aType{llvm.PointerType(elem.ll, 0), typ, vkPtr}
case *types.Interface: case *types.Interface:
return &aType{p.rtIface(), typ, vkInvalid} return &aType{p.rtIface(), typ, vkInvalid}
case *types.Slice: case *types.Slice: