cl.NewPackage

This commit is contained in:
xushiwei
2023-12-10 13:43:44 +08:00
parent a6afd4640e
commit 2c2ffa5c57
3 changed files with 77 additions and 2 deletions

View File

@@ -18,12 +18,84 @@ package cl
import (
"github.com/goplus/llgo/loader"
"github.com/qiniu/x/errors"
"golang.org/x/tools/go/ssa"
llvm "tinygo.org/x/go-llvm"
)
type Config struct {
Triple string
Target llvm.TargetMachine
}
func NewPackage(pkg loader.Package, conf *Config) (ret llvm.Module, err error) {
return
type context struct {
mod llvm.Module
ctx llvm.Context
errs errors.List
}
func newContext(moduleName string, conf *Config) *context {
machine := conf.Target
targetData := machine.CreateTargetData()
ctx := llvm.NewContext()
mod := ctx.NewModule(moduleName)
mod.SetTarget(conf.Triple)
mod.SetDataLayout(targetData.String())
return &context{mod: mod, ctx: ctx}
}
func (c *context) dispose() {
panic("todo")
}
func (c *context) loadASTComments(loader.Package) {
panic("todo")
}
// createPackage builds the LLVM IR for all types, methods, and global variables
// in the given package.
func (c *context) createPackage(irbuilder llvm.Builder, pkg *ssa.Package) {
panic("todo")
}
func NewPackage(moduleName string, pkg loader.Package, conf *Config) (ret llvm.Module, err error) {
ssaPkg := pkg.SSA
ssaPkg.Build()
c := newContext(moduleName, conf)
defer c.dispose()
// Load comments such as //go:extern on globals.
c.loadASTComments(pkg)
/* TODO: gc related
// Predeclare the runtime.alloc function, which is used by the wordpack
// functionality.
c.getFunction(c.program.ImportedPackage("runtime").Members["alloc"].(*ssa.Function))
if c.NeedsStackObjects {
// Predeclare trackPointer, which is used everywhere we use runtime.alloc.
c.getFunction(c.program.ImportedPackage("runtime").Members["trackPointer"].(*ssa.Function))
}
*/
// Compile all functions, methods, and global variables in this package.
irbuilder := c.ctx.NewBuilder()
defer irbuilder.Dispose()
c.createPackage(irbuilder, ssaPkg)
/* TODO: risc-v
// Add the "target-abi" flag, which is necessary on RISC-V otherwise it will
// pick one that doesn't match the -mabi Clang flag.
if c.ABI != "" {
c.mod.AddNamedMetadataOperand("llvm.module.flags",
c.ctx.MDNode([]llvm.Metadata{
llvm.ConstInt(c.ctx.Int32Type(), 1, false).ConstantAsMetadata(), // Error on mismatch
c.ctx.MDString("target-abi"),
c.ctx.MDString(c.ABI),
}),
)
}
*/
return c.mod, c.errs.ToError()
}

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/goplus/llgo
go 1.18
require (
github.com/qiniu/x v1.13.1
golang.org/x/tools v0.16.0
tinygo.org/x/go-llvm v0.0.0-20231014233752-75a8a9fe6f74
)

2
go.sum
View File

@@ -1,3 +1,5 @@
github.com/qiniu/x v1.13.1 h1:hi7tkXFq6BWGbBpMoLV7kvA2elop69j6Kl7TlxnFAiU=
github.com/qiniu/x v1.13.1/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
tinygo.org/x/go-llvm v0.0.0-20231014233752-75a8a9fe6f74 h1:tW8XhLI9gUZLL+2pG0HYb5dc6bpMj1aqtESpizXPnMY=