This commit is contained in:
xushiwei
2024-04-15 05:48:48 +08:00
parent 5e4aa53624
commit a110a1f632
6 changed files with 61 additions and 16 deletions

View File

@@ -17,7 +17,7 @@
package gossa
import (
llvm "tinygo.org/x/go-llvm"
"github.com/goplus/llvm"
)
// A NamedConst is a Member of a Package representing a package-level

42
gossa/gossa_test.go Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gossa
import (
"go/types"
"testing"
"github.com/goplus/llvm"
)
func assertPkg(t *testing.T, p *Package) {
ctx := llvm.NewContext()
buf := llvm.WriteBitcodeToMemoryBuffer(p.mod)
mod, err := ctx.ParseIR(buf)
// buf.Dispose()
if err != nil {
t.Fatal("ctx.ParseIR:", err)
}
_ = mod
}
func TestVar(t *testing.T) {
prog := NewProgram("")
pkg := prog.NewPackage("foo", "foo")
pkg.NewVar("a", types.Typ[types.Int])
assertPkg(t, pkg)
}

View File

@@ -21,10 +21,9 @@ import (
"go/types"
"io"
"os"
"runtime"
"github.com/goplus/llvm"
"golang.org/x/tools/go/types/typeutil"
llvm "tinygo.org/x/go-llvm"
)
// A Program is a partial or complete Go program converted to SSA form.
@@ -42,16 +41,15 @@ type Program struct {
func NewProgram(targetRep string) *Program {
ctx := llvm.NewContext()
runtime.SetFinalizer(ctx.C, (llvm.Context).Dispose)
ctx.Finalize()
td := llvm.NewTargetData(targetRep)
return &Program{ctx: ctx, td: td}
}
func (p *Program) NewPackage(pkg *types.Package) *Package {
name := pkg.Path()
mod := p.ctx.NewModule(name)
runtime.SetFinalizer(mod.C, (llvm.Module).Dispose)
return &Package{mod, pkg, p}
func (p *Program) NewPackage(name, pkgPath string) *Package {
mod := p.ctx.NewModule(pkgPath)
mod.Finalize()
return &Package{mod, p}
}
// A Package is a single analyzed Go package containing Members for
@@ -64,7 +62,6 @@ func (p *Program) NewPackage(pkg *types.Package) *Package {
// and unspecified other things too.
type Package struct {
mod llvm.Module
pkg *types.Package
prog *Program
}
@@ -85,9 +82,15 @@ func (p *Package) NewFunc(name string, sig *types.Signature) *Function {
return &Function{}
}
func (p *Package) WriteTo(w io.Writer) (int64, error) {
func (p *Package) Bytes() []byte {
buf := llvm.WriteBitcodeToMemoryBuffer(p.mod)
n, err := w.Write(buf.Bytes()) // TODO(xsw): reduce copy of bytes
ret := buf.Bytes()
buf.Dispose()
return ret
}
func (p *Package) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(p.Bytes())
return int64(n), err
}

View File

@@ -19,7 +19,7 @@ package gossa
import (
"go/types"
llvm "tinygo.org/x/go-llvm"
"github.com/goplus/llvm"
)
// A Type is a Member of a Package representing a package-level named type.