aProgram/aPackage/aFunction/etc

This commit is contained in:
xushiwei
2024-04-18 01:23:01 +08:00
parent 7b9086cf6d
commit 4fcffa46e1
3 changed files with 39 additions and 39 deletions

View File

@@ -28,21 +28,21 @@ import (
// //
// NB: a NamedConst is not a Value; it contains a constant Value, which // NB: a NamedConst is not a Value; it contains a constant Value, which
// it augments with the name and position of its 'const' declaration. // it augments with the name and position of its 'const' declaration.
type TyNamedConst struct { type aNamedConst struct {
} }
type NamedConst = *TyNamedConst type NamedConst = *aNamedConst
// A Global is a named Value holding the address of a package-level // A Global is a named Value holding the address of a package-level
// variable. // variable.
// //
// Pos() returns the position of the ast.ValueSpec.Names[*] // Pos() returns the position of the ast.ValueSpec.Names[*]
// identifier. // identifier.
type TyGlobal struct { type aGlobal struct {
impl llvm.Value impl llvm.Value
} }
type Global = *TyGlobal type Global = *aGlobal
// Function represents the parameters, results, and code of a function // Function represents the parameters, results, and code of a function
// or method. // or method.
@@ -93,14 +93,14 @@ type Global = *TyGlobal
// method, (*Map[K,V]).Get, TypeParams() refers to the parameters [K,V] of // method, (*Map[K,V]).Get, TypeParams() refers to the parameters [K,V] of
// the generic method. TypeArgs() refers to [string,U] or [string,int], // the generic method. TypeArgs() refers to [string,U] or [string,int],
// respectively, and is nil in the generic method. // respectively, and is nil in the generic method.
type TyFunction struct { type aFunction struct {
impl llvm.Value impl llvm.Value
prog Program prog Program
} }
type Function = *TyFunction type Function = *aFunction
func (p *TyFunction) BodyStart() *BasicBlock { func (p *aFunction) BodyStart() *BasicBlock {
body := llvm.AddBasicBlock(p.impl, "entry") body := llvm.AddBasicBlock(p.impl, "entry")
return &BasicBlock{body} return &BasicBlock{body}
} }

View File

@@ -25,7 +25,7 @@ import (
) )
// A Program is a partial or complete Go program converted to SSA form. // A Program is a partial or complete Go program converted to SSA form.
type TyProgram struct { type aProgram struct {
ctx llvm.Context ctx llvm.Context
b Builder b Builder
typs typeutil.Map typs typeutil.Map
@@ -43,7 +43,7 @@ type TyProgram struct {
voidPtrTy llvm.Type voidPtrTy llvm.Type
} }
type Program = *TyProgram type Program = *aProgram
func NewProgram(target *Target) Program { func NewProgram(target *Target) Program {
if target == nil { if target == nil {
@@ -54,16 +54,16 @@ func NewProgram(target *Target) Program {
b := ctx.NewBuilder() b := ctx.NewBuilder()
b.Finalize() b.Finalize()
td := llvm.NewTargetData("") // TODO(xsw): target config td := llvm.NewTargetData("") // TODO(xsw): target config
return &TyProgram{ctx: ctx, b: Builder{b}, target: target, td: td} return &aProgram{ctx: ctx, b: Builder{b}, target: target, td: td}
} }
func (p *TyProgram) NewPackage(name, pkgPath string) Package { func (p *aProgram) NewPackage(name, pkgPath string) Package {
mod := p.ctx.NewModule(pkgPath) mod := p.ctx.NewModule(pkgPath)
mod.Finalize() mod.Finalize()
return &TyPackage{mod, p} return &aPackage{mod, p}
} }
func (p *TyProgram) Builder() Builder { func (p *aProgram) Builder() Builder {
return p.b return p.b
} }
@@ -75,28 +75,28 @@ func (p *TyProgram) Builder() Builder {
// Members also contains entries for "init" (the synthetic package // Members also contains entries for "init" (the synthetic package
// initializer) and "init#%d", the nth declared init function, // initializer) and "init#%d", the nth declared init function,
// and unspecified other things too. // and unspecified other things too.
type TyPackage struct { type aPackage struct {
mod llvm.Module mod llvm.Module
prog Program prog Program
} }
type Package = *TyPackage type Package = *aPackage
func (p *TyPackage) NewConst(name string, val constant.Value) NamedConst { func (p *aPackage) NewConst(name string, val constant.Value) NamedConst {
return &TyNamedConst{} return &aNamedConst{}
} }
func (p *TyPackage) NewVar(name string, typ types.Type) Global { func (p *aPackage) NewVar(name string, typ types.Type) Global {
gbl := llvm.AddGlobal(p.mod, p.prog.llvmType(typ), name) gbl := llvm.AddGlobal(p.mod, p.prog.llvmType(typ), name)
return &TyGlobal{gbl} return &aGlobal{gbl}
} }
func (p *TyPackage) NewFunc(name string, sig *types.Signature) Function { func (p *aPackage) NewFunc(name string, sig *types.Signature) Function {
fn := llvm.AddFunction(p.mod, name, p.prog.llvmSignature(sig)) fn := llvm.AddFunction(p.mod, name, p.prog.llvmSignature(sig))
return &TyFunction{fn, p.prog} return &aFunction{fn, p.prog}
} }
func (p *TyPackage) String() string { func (p *aPackage) String() string {
return p.mod.String() return p.mod.String()
} }

View File

@@ -29,7 +29,7 @@ type Type struct {
} }
*/ */
func (p *TyProgram) llvmType(typ types.Type) llvm.Type { func (p *aProgram) llvmType(typ types.Type) llvm.Type {
if v := p.typs.At(typ); v != nil { if v := p.typs.At(typ); v != nil {
return v.(llvm.Type) return v.(llvm.Type)
} }
@@ -38,7 +38,7 @@ func (p *TyProgram) llvmType(typ types.Type) llvm.Type {
return ret return ret
} }
func (p *TyProgram) llvmSignature(sig *types.Signature) llvm.Type { func (p *aProgram) llvmSignature(sig *types.Signature) llvm.Type {
if v := p.typs.At(sig); v != nil { if v := p.typs.At(sig); v != nil {
return v.(llvm.Type) return v.(llvm.Type)
} }
@@ -47,56 +47,56 @@ func (p *TyProgram) llvmSignature(sig *types.Signature) llvm.Type {
return ret return ret
} }
func (p *TyProgram) tyVoidPtr() llvm.Type { func (p *aProgram) tyVoidPtr() llvm.Type {
if p.voidPtrTy.IsNil() { if p.voidPtrTy.IsNil() {
p.voidPtrTy = llvm.PointerType(p.tyVoid(), 0) p.voidPtrTy = llvm.PointerType(p.tyVoid(), 0)
} }
return p.voidPtrTy return p.voidPtrTy
} }
func (p *TyProgram) tyVoid() llvm.Type { func (p *aProgram) tyVoid() llvm.Type {
if p.voidType.IsNil() { if p.voidType.IsNil() {
p.voidType = p.ctx.VoidType() p.voidType = p.ctx.VoidType()
} }
return p.voidType return p.voidType
} }
func (p *TyProgram) tyInt() llvm.Type { func (p *aProgram) tyInt() llvm.Type {
if p.intType.IsNil() { if p.intType.IsNil() {
p.intType = llvmIntType(p.ctx, p.td.PointerSize()) p.intType = llvmIntType(p.ctx, p.td.PointerSize())
} }
return p.intType return p.intType
} }
func (p *TyProgram) tyInt8() llvm.Type { func (p *aProgram) tyInt8() llvm.Type {
if p.int8Type.IsNil() { if p.int8Type.IsNil() {
p.int8Type = p.ctx.Int8Type() p.int8Type = p.ctx.Int8Type()
} }
return p.int8Type return p.int8Type
} }
func (p *TyProgram) tyInt16() llvm.Type { func (p *aProgram) tyInt16() llvm.Type {
if p.int16Type.IsNil() { if p.int16Type.IsNil() {
p.int16Type = p.ctx.Int16Type() p.int16Type = p.ctx.Int16Type()
} }
return p.int16Type return p.int16Type
} }
func (p *TyProgram) tyInt32() llvm.Type { func (p *aProgram) tyInt32() llvm.Type {
if p.int32Type.IsNil() { if p.int32Type.IsNil() {
p.int32Type = p.ctx.Int32Type() p.int32Type = p.ctx.Int32Type()
} }
return p.int32Type return p.int32Type
} }
func (p *TyProgram) tyInt64() llvm.Type { func (p *aProgram) tyInt64() llvm.Type {
if p.int64Type.IsNil() { if p.int64Type.IsNil() {
p.int64Type = p.ctx.Int64Type() p.int64Type = p.ctx.Int64Type()
} }
return p.int64Type return p.int64Type
} }
func (p *TyProgram) toLLVMType(typ types.Type) llvm.Type { func (p *aProgram) toLLVMType(typ types.Type) llvm.Type {
switch t := typ.(type) { switch t := typ.(type) {
case *types.Basic: case *types.Basic:
switch t.Kind() { switch t.Kind() {
@@ -146,19 +146,19 @@ func llvmIntType(ctx llvm.Context, size int) llvm.Type {
return ctx.Int64Type() return ctx.Int64Type()
} }
func (p *TyProgram) toLLVMNamedStruct(name string, typ *types.Struct) llvm.Type { func (p *aProgram) toLLVMNamedStruct(name string, typ *types.Struct) llvm.Type {
t := p.ctx.StructCreateNamed(name) t := p.ctx.StructCreateNamed(name)
fields := p.toLLVMFields(typ) fields := p.toLLVMFields(typ)
t.StructSetBody(fields, false) t.StructSetBody(fields, false)
return t return t
} }
func (p *TyProgram) toLLVMStruct(typ *types.Struct) llvm.Type { func (p *aProgram) toLLVMStruct(typ *types.Struct) llvm.Type {
fields := p.toLLVMFields(typ) fields := p.toLLVMFields(typ)
return p.ctx.StructType(fields, false) return p.ctx.StructType(fields, false)
} }
func (p *TyProgram) toLLVMFields(typ *types.Struct) []llvm.Type { func (p *aProgram) toLLVMFields(typ *types.Struct) []llvm.Type {
n := typ.NumFields() n := typ.NumFields()
fields := make([]llvm.Type, n) fields := make([]llvm.Type, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@@ -167,11 +167,11 @@ func (p *TyProgram) toLLVMFields(typ *types.Struct) []llvm.Type {
return fields return fields
} }
func (p *TyProgram) toLLVMTuple(t *types.Tuple) llvm.Type { func (p *aProgram) toLLVMTuple(t *types.Tuple) llvm.Type {
return p.ctx.StructType(p.toLLVMTypes(t), false) return p.ctx.StructType(p.toLLVMTypes(t), false)
} }
func (p *TyProgram) toLLVMTypes(t *types.Tuple) []llvm.Type { func (p *aProgram) toLLVMTypes(t *types.Tuple) []llvm.Type {
n := t.Len() n := t.Len()
ret := make([]llvm.Type, n) ret := make([]llvm.Type, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@@ -180,7 +180,7 @@ func (p *TyProgram) toLLVMTypes(t *types.Tuple) []llvm.Type {
return ret return ret
} }
func (p *TyProgram) toLLVMFunc(sig *types.Signature) llvm.Type { func (p *aProgram) toLLVMFunc(sig *types.Signature) llvm.Type {
params := p.toLLVMTypes(sig.Params()) params := p.toLLVMTypes(sig.Params())
results := sig.Results() results := sig.Results()
var ret llvm.Type var ret llvm.Type
@@ -195,7 +195,7 @@ func (p *TyProgram) toLLVMFunc(sig *types.Signature) llvm.Type {
return llvm.FunctionType(ret, params, sig.Variadic()) return llvm.FunctionType(ret, params, sig.Variadic())
} }
func (p *TyProgram) toLLVMNamed(typ *types.Named) llvm.Type { func (p *aProgram) toLLVMNamed(typ *types.Named) llvm.Type {
name := typ.Obj().Name() name := typ.Obj().Name()
switch typ := typ.Underlying().(type) { switch typ := typ.Underlying().(type) {
case *types.Struct: case *types.Struct: