Merge pull request #2 from xushiwei/q

TestVar
This commit is contained in:
xushiwei
2024-04-15 05:53:23 +08:00
committed by GitHub
7 changed files with 67 additions and 16 deletions

View File

@@ -33,6 +33,9 @@ jobs:
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
test-linux:
runs-on: ubuntu-20.04
strategy:
@@ -55,3 +58,6 @@ jobs:
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...

2
go.mod
View File

@@ -6,9 +6,9 @@ require (
github.com/aykevl/go-wasm v0.0.1
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb
github.com/goplus/gop v1.2.6
github.com/goplus/llvm v0.7.0
github.com/qiniu/x v1.13.10
golang.org/x/tools v0.19.0
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318
)
require (

4
go.sum
View File

@@ -6,6 +6,8 @@ github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM=
github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.6 h1:kog3c5Js+8EopqmI4+CwueXsqibnBwYVt5q5N7juRVY=
github.com/goplus/gop v1.2.6/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/llvm v0.7.0 h1:b8XzmRA97U0V0BPSaYuZ2vw+lLO2JSpRLMtR6dAenIo=
github.com/goplus/llvm v0.7.0/go.mod h1:PeVK8GgzxwAYCiMiUAJb5wJR6xbhj989tu9oulKLLT4=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
@@ -57,5 +59,3 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318 h1:4KjZvPtcN1UwobevcGbdzeinx0L1i8HDdJu84bu7NI8=
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=

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.