build: separate compiler and libs
This commit is contained in:
259
compiler/chore/_deprecated/ar/ar.go
Normal file
259
compiler/chore/_deprecated/ar/ar.go
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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 ar
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"debug/elf"
|
||||
"debug/pe"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
wasm "github.com/aykevl/go-wasm"
|
||||
"github.com/goplus/llgo/xtool/ar"
|
||||
)
|
||||
|
||||
// Create creates an arcive for static linking from a list of object files
|
||||
// given as a parameter. It is equivalent to the following command:
|
||||
//
|
||||
// ar -rcs <archivePath> <objs...>
|
||||
func Create(arfile string, objs []string) error {
|
||||
f, err := os.Create(arfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = Make(f, objs)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
os.Remove(arfile)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Make creates an arcive for static linking from a list of object files
|
||||
// given as a parameter. It is equivalent to the following command:
|
||||
//
|
||||
// ar -rcs <archivePath> <objs...>
|
||||
func Make(arfile io.WriteSeeker, objs []string) error {
|
||||
// Open the archive file.
|
||||
arwriter := ar.NewWriter(arfile)
|
||||
err := arwriter.WriteGlobalHeader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Open all object files and read the symbols for the symbol table.
|
||||
symbolTable := []struct {
|
||||
name string // symbol name
|
||||
fileIndex int // index into objfiles
|
||||
}{}
|
||||
archiveOffsets := make([]int32, len(objs))
|
||||
for i, objpath := range objs {
|
||||
objfile, err := os.Open(objpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Read the symbols and add them to the symbol table.
|
||||
if dbg, err := elf.NewFile(objfile); err == nil {
|
||||
symbols, err := dbg.Symbols()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, symbol := range symbols {
|
||||
bind := elf.ST_BIND(symbol.Info)
|
||||
if bind != elf.STB_GLOBAL && bind != elf.STB_WEAK {
|
||||
// Don't include local symbols (STB_LOCAL).
|
||||
continue
|
||||
}
|
||||
if elf.ST_TYPE(symbol.Info) != elf.STT_FUNC && elf.ST_TYPE(symbol.Info) != elf.STT_OBJECT {
|
||||
// Not a function.
|
||||
continue
|
||||
}
|
||||
// Include in archive.
|
||||
symbolTable = append(symbolTable, struct {
|
||||
name string
|
||||
fileIndex int
|
||||
}{symbol.Name, i})
|
||||
}
|
||||
} else if dbg, err := pe.NewFile(objfile); err == nil {
|
||||
for _, symbol := range dbg.Symbols {
|
||||
if symbol.StorageClass != 2 {
|
||||
continue
|
||||
}
|
||||
if symbol.SectionNumber == 0 {
|
||||
continue
|
||||
}
|
||||
symbolTable = append(symbolTable, struct {
|
||||
name string
|
||||
fileIndex int
|
||||
}{symbol.Name, i})
|
||||
}
|
||||
} else if dbg, err := wasm.Parse(objfile); err == nil {
|
||||
for _, s := range dbg.Sections {
|
||||
switch section := s.(type) {
|
||||
case *wasm.SectionImport:
|
||||
for _, ln := range section.Entries {
|
||||
|
||||
if ln.Kind != wasm.ExtKindFunction {
|
||||
// Not a function
|
||||
continue
|
||||
}
|
||||
symbolTable = append(symbolTable, struct {
|
||||
name string
|
||||
fileIndex int
|
||||
}{ln.Field, i})
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("failed to open file %s as WASM, ELF or PE/COFF: %w", objpath, err)
|
||||
}
|
||||
|
||||
// Close file, to avoid issues with too many open files (especially on
|
||||
// MacOS X).
|
||||
objfile.Close()
|
||||
}
|
||||
|
||||
// Create the symbol table buffer.
|
||||
// For some (sparse) details on the file format:
|
||||
// https://en.wikipedia.org/wiki/Ar_(Unix)#System_V_(or_GNU)_variant
|
||||
buf := &bytes.Buffer{}
|
||||
binary.Write(buf, binary.BigEndian, int32(len(symbolTable)))
|
||||
for range symbolTable {
|
||||
// This is a placeholder index, it will be updated after all files have
|
||||
// been written to the archive (see the end of this function).
|
||||
err = binary.Write(buf, binary.BigEndian, int32(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, sym := range symbolTable {
|
||||
_, err := buf.Write([]byte(sym.name + "\x00"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for buf.Len()%2 != 0 {
|
||||
// The symbol table must be aligned.
|
||||
// This appears to be required by lld.
|
||||
buf.WriteByte(0)
|
||||
}
|
||||
|
||||
// Write the symbol table.
|
||||
err = arwriter.WriteHeader(&ar.Header{
|
||||
Name: "/",
|
||||
ModTime: time.Unix(0, 0),
|
||||
Uid: 0,
|
||||
Gid: 0,
|
||||
Mode: 0,
|
||||
Size: int64(buf.Len()),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Keep track of the start of the symbol table.
|
||||
symbolTableStart, err := arfile.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write symbol table contents.
|
||||
_, err = arfile.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add all object files to the archive.
|
||||
var copyBuf bytes.Buffer
|
||||
for i, objpath := range objs {
|
||||
objfile, err := os.Open(objpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer objfile.Close()
|
||||
|
||||
// Store the start index, for when we'll update the symbol table with
|
||||
// the correct file start indices.
|
||||
offset, err := arfile.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if int64(int32(offset)) != offset {
|
||||
return errors.New("large archives (4GB+) not supported")
|
||||
}
|
||||
archiveOffsets[i] = int32(offset)
|
||||
|
||||
// Write the file header.
|
||||
st, err := objfile.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = arwriter.WriteHeader(&ar.Header{
|
||||
Name: filepath.Base(objfile.Name()),
|
||||
ModTime: time.Unix(0, 0),
|
||||
Uid: 0,
|
||||
Gid: 0,
|
||||
Mode: 0644,
|
||||
Size: st.Size(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Copy the file contents into the archive.
|
||||
// First load all contents into a buffer, then write it all in one go to
|
||||
// the archive file. This is a bit complicated, but is necessary because
|
||||
// io.Copy can't deal with files that are of an odd size.
|
||||
copyBuf.Reset()
|
||||
n, err := io.Copy(©Buf, objfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not copy object file into ar file: %w", err)
|
||||
}
|
||||
if n != st.Size() {
|
||||
return errors.New("file modified during ar creation")
|
||||
}
|
||||
_, err = arwriter.Write(copyBuf.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not copy object file into ar file: %w", err)
|
||||
}
|
||||
|
||||
// File is not needed anymore.
|
||||
objfile.Close()
|
||||
}
|
||||
|
||||
// Create symbol indices.
|
||||
indicesBuf := &bytes.Buffer{}
|
||||
for _, sym := range symbolTable {
|
||||
err = binary.Write(indicesBuf, binary.BigEndian, archiveOffsets[sym.fileIndex])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Overwrite placeholder indices.
|
||||
if _, err = arfile.Seek(symbolTableStart+4, io.SeekStart); err == nil {
|
||||
_, err = arfile.Write(indicesBuf.Bytes())
|
||||
}
|
||||
return err
|
||||
}
|
||||
122
compiler/chore/_deprecated/clang/parser/pages.go
Normal file
122
compiler/chore/_deprecated/clang/parser/pages.go
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 parser
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
const pageSize = 1024 * 1024
|
||||
|
||||
type PagedWriter struct {
|
||||
pages []*[pageSize]byte
|
||||
last *[pageSize]byte
|
||||
off int
|
||||
}
|
||||
|
||||
func NewPagedWriter() *PagedWriter {
|
||||
return &PagedWriter{last: new([pageSize]byte)}
|
||||
}
|
||||
|
||||
func (p *PagedWriter) Write(buf []byte) (written int, err error) {
|
||||
for {
|
||||
n := copy(p.last[p.off:], buf[written:])
|
||||
written += n
|
||||
if written >= len(buf) {
|
||||
p.off += n
|
||||
return
|
||||
}
|
||||
p.pages = append(p.pages, p.last)
|
||||
p.last, p.off = new([pageSize]byte), 0
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PagedWriter) Len() int {
|
||||
return len(p.pages)*pageSize + p.off
|
||||
}
|
||||
|
||||
func (p *PagedWriter) Bytes() []byte {
|
||||
out, n := make([]byte, p.Len()), 0
|
||||
for _, page := range p.pages {
|
||||
n += copy(out[n:], page[:])
|
||||
}
|
||||
copy(out[n:], p.last[:p.off])
|
||||
return out
|
||||
}
|
||||
|
||||
/*
|
||||
func (p *PagedWriter) ToReader() *PagedReader {
|
||||
return &PagedReader{src: p, curr: p.getPage(0)}
|
||||
}
|
||||
|
||||
func (p *PagedWriter) getPage(ipage int) []byte {
|
||||
if ipage == len(p.pages) { // last page
|
||||
return p.last[:p.off]
|
||||
}
|
||||
return p.pages[ipage][:]
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type PagedReader struct {
|
||||
src *PagedWriter
|
||||
curr []byte
|
||||
off int
|
||||
ipage int
|
||||
}
|
||||
|
||||
func (p *PagedReader) WriteTo(w io.Writer) (written int64, err error) {
|
||||
n, err := w.Write(p.curr[p.off:])
|
||||
written = int64(n)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
src, ipage := p.src, p.ipage
|
||||
for {
|
||||
if ipage == len(src.pages) { // last page
|
||||
p.ipage, p.off = ipage, len(p.curr)
|
||||
return
|
||||
}
|
||||
ipage++
|
||||
page := src.getPage(ipage)
|
||||
n, err = w.Write(page)
|
||||
written += int64(n)
|
||||
if err != nil {
|
||||
p.ipage, p.curr, p.off = ipage, page, n
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PagedReader) Read(buf []byte) (nread int, err error) {
|
||||
for {
|
||||
n := copy(buf[nread:], p.curr[p.off:])
|
||||
nread += n
|
||||
p.off += n
|
||||
if nread >= len(buf) {
|
||||
return
|
||||
}
|
||||
src := p.src
|
||||
if p.ipage == len(src.pages) { // last page
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
p.ipage++
|
||||
p.curr, p.off = src.getPage(p.ipage), 0
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
104
compiler/chore/_deprecated/clang/parser/parse.go
Normal file
104
compiler/chore/_deprecated/clang/parser/parse.go
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/goplus/llgo/xtool/clang/ast"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type Mode uint
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type ParseError struct {
|
||||
Err error
|
||||
Stderr []byte
|
||||
}
|
||||
|
||||
func (p *ParseError) Error() string {
|
||||
if len(p.Stderr) > 0 {
|
||||
return string(p.Stderr)
|
||||
}
|
||||
return p.Err.Error()
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type Config struct {
|
||||
Json *[]byte
|
||||
Flags []string
|
||||
Stderr bool
|
||||
}
|
||||
|
||||
func DumpAST(filename string, conf *Config) (result []byte, warning []byte, err error) {
|
||||
if conf == nil {
|
||||
conf = new(Config)
|
||||
}
|
||||
skiperr := strings.HasSuffix(filename, "vfprintf.c.i")
|
||||
stdout := NewPagedWriter()
|
||||
stderr := new(bytes.Buffer)
|
||||
args := []string{"-Xclang", "-ast-dump=json", "-fsyntax-only", filename}
|
||||
if len(conf.Flags) != 0 {
|
||||
args = append(conf.Flags, args...)
|
||||
}
|
||||
cmd := exec.Command("clang", args...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = stdout
|
||||
if conf.Stderr && !skiperr {
|
||||
cmd.Stderr = os.Stderr
|
||||
} else {
|
||||
cmd.Stderr = stderr
|
||||
}
|
||||
err = cmd.Run()
|
||||
errmsg := stderr.Bytes()
|
||||
if err != nil && !skiperr {
|
||||
return nil, nil, &ParseError{Err: err, Stderr: errmsg}
|
||||
}
|
||||
return stdout.Bytes(), errmsg, nil
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
func ParseFileEx(filename string, mode Mode, conf *Config) (file *ast.Node, warning []byte, err error) {
|
||||
out, warning, err := DumpAST(filename, conf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if conf != nil && conf.Json != nil {
|
||||
*conf.Json = out
|
||||
}
|
||||
file = new(ast.Node)
|
||||
err = json.Unmarshal(out, file)
|
||||
if err != nil {
|
||||
err = &ParseError{Err: err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ParseFile(filename string, mode Mode) (file *ast.Node, warning []byte, err error) {
|
||||
return ParseFileEx(filename, mode, nil)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
71
compiler/chore/_deprecated/clangast/clangast.go
Normal file
71
compiler/chore/_deprecated/clangast/clangast.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/_xtool/clang/parser"
|
||||
)
|
||||
|
||||
var (
|
||||
dump = flag.Bool("dump", false, "dump AST")
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: clangast [-dump] source.i\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
if flag.NArg() < 1 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
var file = flag.Arg(0)
|
||||
var err error
|
||||
if *dump {
|
||||
doc, _, e := parser.DumpAST(file, nil)
|
||||
if e == nil {
|
||||
os.Stdout.Write(doc)
|
||||
return
|
||||
}
|
||||
err = e
|
||||
} else {
|
||||
doc, _, e := parser.ParseFile(file, 0)
|
||||
if e == nil {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
check(enc.Encode(doc))
|
||||
return
|
||||
}
|
||||
err = e
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
14
compiler/chore/_deprecated/go.mod
Normal file
14
compiler/chore/_deprecated/go.mod
Normal file
@@ -0,0 +1,14 @@
|
||||
module github.com/goplus/llgo/_xtool
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/aykevl/go-wasm v0.0.1
|
||||
github.com/goplus/llgo v0.9.0
|
||||
github.com/json-iterator/go v1.1.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
)
|
||||
19
compiler/chore/_deprecated/go.sum
Normal file
19
compiler/chore/_deprecated/go.sum
Normal file
@@ -0,0 +1,19 @@
|
||||
github.com/aykevl/go-wasm v0.0.1 h1:lPxy8l48P39W7I0tLrtCrLfZBOUq9IWZ7odGdyJP2AM=
|
||||
github.com/aykevl/go-wasm v0.0.1/go.mod h1:b4nggwg3lEkNKOU4wzhtLKz2q2sLxSHFnc98aGt6z/Y=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/goplus/llgo v0.9.0 h1:yaJzQperGUafEaHc9VlVQVskIngacoTNweEXY0GRi0Q=
|
||||
github.com/goplus/llgo v0.9.0/go.mod h1:M3UwiYdPZFyx7m2J0+6Ti1dYVA3uOO1WvSBocuE8N7M=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
63
compiler/chore/_xtool/astdump/astdump.cpp
Normal file
63
compiler/chore/_xtool/astdump/astdump.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <clang-c/Index.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
CXChildVisitResult visit(CXCursor c, CXCursor parent, CXClientData client_data);
|
||||
|
||||
void printAST(CXCursor cursor, unsigned int depth = 0) {
|
||||
CXString cursorKind = clang_getCursorKindSpelling(clang_getCursorKind(cursor));
|
||||
CXString cursorSpelling = clang_getCursorSpelling(cursor);
|
||||
|
||||
for (unsigned int i = 0; i < depth; ++i) {
|
||||
std::cout << " ";
|
||||
}
|
||||
|
||||
std::cout << clang_getCString(cursorKind) << ": "
|
||||
<< clang_getCString(cursorSpelling) << std::endl;
|
||||
|
||||
clang_disposeString(cursorKind);
|
||||
clang_disposeString(cursorSpelling);
|
||||
|
||||
CXCursor child;
|
||||
clang_visitChildren(
|
||||
cursor,
|
||||
visit,
|
||||
&depth
|
||||
);
|
||||
}
|
||||
|
||||
CXChildVisitResult visit(CXCursor c, CXCursor parent, CXClientData client_data) {
|
||||
unsigned int* depth = (unsigned int*)client_data;
|
||||
printAST(c, *depth + 1);
|
||||
return CXChildVisit_Continue;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <header_file>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
CXIndex index = clang_createIndex(0, 0);
|
||||
CXTranslationUnit unit = clang_parseTranslationUnit(
|
||||
index,
|
||||
argv[1],
|
||||
nullptr, 0,
|
||||
nullptr, 0,
|
||||
CXTranslationUnit_None
|
||||
);
|
||||
|
||||
if (unit == nullptr) {
|
||||
std::cerr << "Unable to parse translation unit. Quitting." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
CXCursor cursor = clang_getTranslationUnitCursor(unit);
|
||||
std::cout << "AST for " << argv[1] << ":" << std::endl;
|
||||
printAST(cursor);
|
||||
|
||||
clang_disposeTranslationUnit(unit);
|
||||
clang_disposeIndex(index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
compiler/chore/_xtool/astdump/build.sh
Executable file
2
compiler/chore/_xtool/astdump/build.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
export LLVM_DIR=$(llvm-config --prefix)
|
||||
clang -L$LLVM_DIR/lib -lclang -lc++ -I$LLVM_DIR/include astdump.cpp
|
||||
221
compiler/chore/_xtool/castdump/castdump.go
Normal file
221
compiler/chore/_xtool/castdump/castdump.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
Depth c.Uint
|
||||
Unit *clang.TranslationUnit
|
||||
}
|
||||
|
||||
var accessMap = map[clang.CXXAccessSpecifier]string{
|
||||
clang.CXXInvalidAccessSpecifier: "invalid",
|
||||
clang.CXXPublic: "public",
|
||||
clang.CXXProtected: "protected",
|
||||
clang.CXXPrivate: "private",
|
||||
}
|
||||
|
||||
func printIndent(depth c.Uint) {
|
||||
fmt.Print(strings.Repeat(" ", int(depth)))
|
||||
}
|
||||
|
||||
func accessToString(spec clang.CXXAccessSpecifier) string {
|
||||
if str, ok := accessMap[spec]; ok {
|
||||
return str
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func visit(cursor, parent clang.Cursor, ClientData c.Pointer) clang.ChildVisitResult {
|
||||
data := (*Data)(ClientData)
|
||||
printAST(cursor, data)
|
||||
return clang.ChildVisit_Continue
|
||||
}
|
||||
|
||||
func printType(t clang.Type, data *Data) {
|
||||
printIndent(data.Depth)
|
||||
|
||||
typeSpell := t.String()
|
||||
typeKind := t.Kind.String()
|
||||
|
||||
if t.Kind == clang.TypeInvalid {
|
||||
} else if t.Kind == clang.TypeUnexposed {
|
||||
c.Printf(c.Str("<UnexposedType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
} else if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin {
|
||||
c.Printf(c.Str("<BuiltinType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
} else if t.Kind > clang.TypeComplex {
|
||||
c.Printf(c.Str("<ComplexType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
}
|
||||
|
||||
data.Depth++
|
||||
switch t.Kind {
|
||||
case clang.TypePointer:
|
||||
printType(t.PointeeType(), data)
|
||||
case clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray, clang.TypeConstantArray:
|
||||
printType(t.ArrayElementType(), data)
|
||||
case clang.TypeTypedef:
|
||||
printType(t.TypeDeclaration().TypedefDeclUnderlyingType(), data)
|
||||
case clang.TypeElaborated:
|
||||
printType(t.NamedType(), data)
|
||||
case clang.TypeFunctionProto:
|
||||
printType(t.ResultType(), data)
|
||||
for i := 0; i < int(t.NumArgTypes()); i++ {
|
||||
printType(t.ArgType(c.Uint(i)), data)
|
||||
}
|
||||
}
|
||||
data.Depth--
|
||||
|
||||
typeKind.Dispose()
|
||||
typeSpell.Dispose()
|
||||
}
|
||||
|
||||
func printLocation(cursor clang.Cursor) {
|
||||
loc := cursor.Location()
|
||||
var file clang.File
|
||||
var line, column c.Uint
|
||||
|
||||
loc.SpellingLocation(&file, &line, &column, nil)
|
||||
filename := file.FileName()
|
||||
defer filename.Dispose()
|
||||
|
||||
c.Printf(c.Str("(Loc:%s:%d:%d)\n"), filename.CStr(), line, column)
|
||||
}
|
||||
|
||||
func printAccess(cursor clang.Cursor) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s %s"), kind.CStr(), spell.CStr(), c.AllocaCStr(accessToString(cursor.CXXAccessSpecifier())))
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printMacro(cursor clang.Cursor, unit *clang.TranslationUnit) {
|
||||
kind := cursor.Kind.String()
|
||||
defer kind.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: "), kind.CStr())
|
||||
ran := cursor.Extent()
|
||||
var numTokens c.Uint
|
||||
var tokens *clang.Token
|
||||
unit.Tokenize(ran, &tokens, &numTokens)
|
||||
defer unit.DisposeTokens(tokens, numTokens)
|
||||
|
||||
tokensSlice := unsafe.Slice(tokens, int(numTokens))
|
||||
for _, tok := range tokensSlice {
|
||||
tokStr := unit.Token(tok)
|
||||
c.Printf(c.Str("%s "), tokStr.CStr())
|
||||
tokStr.Dispose()
|
||||
}
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printFunc(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
symbol := cursor.Mangling()
|
||||
defer symbol.Dispose()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s (Symbol: %s)"), kind.CStr(), spell.CStr(), symbol.CStr())
|
||||
printLocation(cursor)
|
||||
printType(cursor.Type(), data)
|
||||
}
|
||||
|
||||
func printEnumConstant(cursor clang.Cursor) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s:%lld"), kind.CStr(), spell.CStr(), cursor.EnumConstantDeclValue())
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printDefault(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
// node which has type
|
||||
if cursor.Type().Kind != clang.TypeInvalid {
|
||||
c.Printf(c.Str("%s: %s"), kind.CStr(), spell.CStr())
|
||||
printLocation(cursor)
|
||||
printType(cursor.Type(), data)
|
||||
} else {
|
||||
c.Printf(c.Str("%s: %s\n"), kind.CStr(), spell.CStr())
|
||||
}
|
||||
}
|
||||
|
||||
func printAST(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
|
||||
printIndent(data.Depth)
|
||||
|
||||
switch cursor.Kind {
|
||||
case clang.CursorCXXAccessSpecifier:
|
||||
printAccess(cursor)
|
||||
case clang.CursorMacroDefinition:
|
||||
printMacro(cursor, data.Unit)
|
||||
case clang.CursorFunctionDecl, clang.CursorCXXMethod, clang.CursorConstructor, clang.CursorDestructor:
|
||||
printFunc(cursor, data)
|
||||
case clang.CursorEnumConstantDecl:
|
||||
printEnumConstant(cursor)
|
||||
default:
|
||||
printDefault(cursor, data)
|
||||
}
|
||||
|
||||
data.Depth++
|
||||
clang.VisitChildren(cursor, visit, c.Pointer(data))
|
||||
data.Depth--
|
||||
|
||||
kind.Dispose()
|
||||
spell.Dispose()
|
||||
}
|
||||
|
||||
func main() {
|
||||
if c.Argc != 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: castdump <headerFile>")
|
||||
return
|
||||
}
|
||||
|
||||
args := make([]*c.Char, 3)
|
||||
args[0] = c.Str("-x")
|
||||
args[1] = c.Str("c++")
|
||||
args[2] = c.Str("-std=c++11")
|
||||
|
||||
sourceFile := *c.Advance(c.Argv, 1)
|
||||
index := clang.CreateIndex(0, 0)
|
||||
unit := index.ParseTranslationUnit(
|
||||
sourceFile,
|
||||
unsafe.SliceData(args), 3,
|
||||
nil, 0,
|
||||
clang.DetailedPreprocessingRecord,
|
||||
)
|
||||
defer index.Dispose()
|
||||
defer unit.Dispose()
|
||||
|
||||
if unit == nil {
|
||||
println("Unable to parse translation unit. Quitting.")
|
||||
c.Exit(1)
|
||||
}
|
||||
|
||||
cursor := unit.Cursor()
|
||||
|
||||
Data := &Data{
|
||||
Depth: 0,
|
||||
Unit: unit,
|
||||
}
|
||||
printAST(cursor, Data)
|
||||
}
|
||||
60
compiler/chore/_xtool/pydump/pydump.go
Normal file
60
compiler/chore/_xtool/pydump/pydump.go
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/inspect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if c.Argc < 2 {
|
||||
c.Fprintf(c.Stderr, c.Str("Usage: pydump <pythonLibPath>\n"))
|
||||
return
|
||||
}
|
||||
pyLib := c.Index(c.Argv, 1)
|
||||
|
||||
py.Initialize()
|
||||
|
||||
root := cjson.Object()
|
||||
root.SetItem(c.Str("name"), cjson.String(pyLib))
|
||||
|
||||
items := cjson.Array()
|
||||
mod := py.ImportModule(pyLib)
|
||||
keys := mod.ModuleGetDict().DictKeys()
|
||||
for i, n := 0, keys.ListLen(); i < n; i++ {
|
||||
key := keys.ListItem(i)
|
||||
val := mod.GetAttr(key)
|
||||
doc := val.GetAttrString(c.Str("__doc__"))
|
||||
sym := cjson.Object()
|
||||
sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr()))
|
||||
sym.SetItem(c.Str("name"), cjson.String(key.CStr()))
|
||||
if doc != nil {
|
||||
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
|
||||
}
|
||||
if val.Callable() != 0 {
|
||||
sig := inspect.Signature(val)
|
||||
sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr()))
|
||||
}
|
||||
items.AddItem(sym)
|
||||
}
|
||||
root.SetItem(c.Str("items"), items)
|
||||
|
||||
c.Printf(c.Str("%s\n"), root.CStr())
|
||||
}
|
||||
56
compiler/chore/ardump/ardump.go
Normal file
56
compiler/chore/ardump/ardump.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/xtool/ar"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: ardump xxx.a")
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(os.Args[1])
|
||||
check(err)
|
||||
defer f.Close()
|
||||
|
||||
r, err := ar.NewReader(f)
|
||||
check(err)
|
||||
for {
|
||||
hdr, err := r.Next()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.Println(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
fmt.Println(hdr.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
41
compiler/chore/clangpp/clangpp.go
Normal file
41
compiler/chore/clangpp/clangpp.go
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/xtool/clang/preprocessor"
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: clangpp source.c\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
infile := os.Args[1]
|
||||
outfile := infile + ".i"
|
||||
if err := preprocessor.Do(infile, outfile, nil); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
47
compiler/chore/dylibdeps/dylibdeps.go
Normal file
47
compiler/chore/dylibdeps/dylibdeps.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"debug/macho"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: dylibdeps exefile")
|
||||
return
|
||||
}
|
||||
exe := os.Args[1]
|
||||
|
||||
file, err := macho.Open(exe)
|
||||
check(err)
|
||||
|
||||
defer file.Close()
|
||||
for _, load := range file.Loads {
|
||||
if dylib, ok := load.(*macho.Dylib); ok {
|
||||
fmt.Println(dylib.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
59
compiler/chore/gentests/gentests.go
Normal file
59
compiler/chore/gentests/gentests.go
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/goplus/llgo/compiler/internal/llgen"
|
||||
"github.com/goplus/mod"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dir, _, err := mod.FindGoMod(".")
|
||||
check(err)
|
||||
|
||||
llgenDir(dir + "/cl/_testlibc")
|
||||
llgenDir(dir + "/cl/_testlibgo")
|
||||
llgenDir(dir + "/cl/_testrt")
|
||||
llgenDir(dir + "/cl/_testgo")
|
||||
llgenDir(dir + "/cl/_testpy")
|
||||
llgenDir(dir + "/cl/_testdata")
|
||||
}
|
||||
|
||||
func llgenDir(dir string) {
|
||||
fis, err := os.ReadDir(dir)
|
||||
check(err)
|
||||
for _, fi := range fis {
|
||||
name := fi.Name()
|
||||
if !fi.IsDir() || strings.HasPrefix(name, "_") {
|
||||
continue
|
||||
}
|
||||
testDir := dir + "/" + name
|
||||
fmt.Fprintln(os.Stderr, "llgen", testDir)
|
||||
check(os.Chdir(testDir))
|
||||
llgen.SmartDoFile(testDir)
|
||||
}
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
34
compiler/chore/llgen/llgen.go
Normal file
34
compiler/chore/llgen/llgen.go
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/compiler/internal/llgen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if len(flag.Args()) != 1 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: llgen [flags] <pkg>")
|
||||
return
|
||||
}
|
||||
llgen.SmartDoFile(flag.Args()[0])
|
||||
}
|
||||
249
compiler/chore/llpyg/llpyg.go
Normal file
249
compiler/chore/llpyg/llpyg.go
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/goplus/gogen"
|
||||
"github.com/goplus/llgo/compiler/chore/llpyg/pysig"
|
||||
"github.com/goplus/llgo/compiler/ssa"
|
||||
)
|
||||
|
||||
type symbol struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Doc string `json:"doc"`
|
||||
Sig string `json:"sig"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type module struct {
|
||||
Name string `json:"name"`
|
||||
Items []*symbol `json:"items"`
|
||||
}
|
||||
|
||||
func pydump(pyLib string) (mod module) {
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command("pydump", pyLib)
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Run()
|
||||
|
||||
json.Unmarshal(out.Bytes(), &mod)
|
||||
return
|
||||
}
|
||||
|
||||
func pysigfetch(pyLib string, names []string) (mod module) {
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command("pysigfetch", pyLib, "-")
|
||||
cmd.Stdin = strings.NewReader(strings.Join(names, " "))
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Run()
|
||||
|
||||
json.Unmarshal(out.Bytes(), &mod)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: llpyg <pythonLibPath>")
|
||||
return
|
||||
}
|
||||
pyLib := os.Args[1]
|
||||
|
||||
mod := pydump(pyLib)
|
||||
if mod.Name != pyLib {
|
||||
log.Printf("import module %s failed\n", pyLib)
|
||||
os.Exit(1)
|
||||
}
|
||||
pkg := gogen.NewPackage("", pkgName(pyLib), nil)
|
||||
pkg.Import("unsafe").MarkForceUsed(pkg) // import _ "unsafe"
|
||||
py := pkg.Import("github.com/goplus/llgo/py") // import "github.com/goplus/llgo/py"
|
||||
|
||||
f := func(cb *gogen.CodeBuilder) int {
|
||||
cb.Val("py." + mod.Name)
|
||||
return 1
|
||||
}
|
||||
defs := pkg.NewConstDefs(pkg.Types.Scope())
|
||||
defs.New(f, 0, 0, nil, "LLGoPackage")
|
||||
|
||||
obj := py.Ref("Object").(*types.TypeName).Type().(*types.Named)
|
||||
objPtr := types.NewPointer(obj)
|
||||
ret := types.NewTuple(pkg.NewParam(0, "", objPtr))
|
||||
|
||||
ctx := &context{pkg, obj, objPtr, ret, nil, py}
|
||||
ctx.genMod(pkg, &mod)
|
||||
skips := ctx.skips
|
||||
if n := len(skips); n > 0 {
|
||||
log.Printf("==> There are %d signatures not found, fetch from doc site\n", n)
|
||||
mod = pysigfetch(pyLib, skips)
|
||||
ctx.skips = skips[:0]
|
||||
ctx.genMod(pkg, &mod)
|
||||
if len(mod.Items) > 0 {
|
||||
skips = ctx.skips
|
||||
}
|
||||
if n := len(skips); n > 0 {
|
||||
log.Printf("==> Skip %d symbols:\n%v\n", n, skips)
|
||||
}
|
||||
}
|
||||
|
||||
pkg.WriteTo(os.Stdout)
|
||||
}
|
||||
|
||||
func pkgName(pyLib string) string {
|
||||
if pos := strings.LastIndexByte(pyLib, '.'); pos >= 0 {
|
||||
return pyLib[pos+1:]
|
||||
}
|
||||
return pyLib
|
||||
}
|
||||
|
||||
type context struct {
|
||||
pkg *gogen.Package
|
||||
obj *types.Named
|
||||
objPtr *types.Pointer
|
||||
ret *types.Tuple
|
||||
skips []string
|
||||
py gogen.PkgRef
|
||||
}
|
||||
|
||||
func (ctx *context) genMod(pkg *gogen.Package, mod *module) {
|
||||
for _, sym := range mod.Items {
|
||||
switch sym.Type {
|
||||
case "builtin_function_or_method", "function", "method", "ufunc", "method-wrapper":
|
||||
ctx.genFunc(pkg, sym)
|
||||
case "str", "float", "bool", "type", "dict", "tuple", "list", "object", "module",
|
||||
"int", "set", "frozenset", "flags", "bool_", "pybind11_type", "layout",
|
||||
"memory_format", "qscheme", "dtype", "tensortype", "ellipsis": // skip
|
||||
case "": // pysigfetch: page not found
|
||||
ctx.skips = append(ctx.skips, sym.Name)
|
||||
default:
|
||||
t := sym.Type
|
||||
if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') && !strings.HasSuffix(t, "_info") {
|
||||
log.Panicln("unsupport type:", sym.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
|
||||
name, symSig := sym.Name, sym.Sig
|
||||
if len(name) == 0 || name[0] == '_' {
|
||||
return
|
||||
}
|
||||
if symSig == "<NULL>" {
|
||||
ctx.skips = append(ctx.skips, name)
|
||||
return
|
||||
}
|
||||
params, variadic := ctx.genParams(pkg, symSig)
|
||||
name = genName(name, -1)
|
||||
sig := types.NewSignatureType(nil, nil, nil, params, ctx.ret, variadic)
|
||||
fn := pkg.NewFuncDecl(token.NoPos, name, sig)
|
||||
list := ctx.genDoc(sym.Doc)
|
||||
if sym.URL != "" {
|
||||
if len(list) > 0 {
|
||||
list = append(list, emptyCommentLine)
|
||||
}
|
||||
list = append(list, genSee(sym.URL))
|
||||
}
|
||||
if len(list) > 0 {
|
||||
list = append(list, emptyCommentLine)
|
||||
}
|
||||
list = append(list, ctx.genLinkname(name, sym))
|
||||
fn.SetComments(pkg, &ast.CommentGroup{List: list})
|
||||
// fn.BodyStart(pkg).End()
|
||||
}
|
||||
|
||||
func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, bool) {
|
||||
args := pysig.Parse(sig)
|
||||
if len(args) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
n := len(args)
|
||||
objPtr := ctx.objPtr
|
||||
list := make([]*types.Var, 0, n)
|
||||
for i := 0; i < n; i++ {
|
||||
name := args[i].Name
|
||||
if name == "/" {
|
||||
continue
|
||||
}
|
||||
if name == "*" || name == "\\*" {
|
||||
break
|
||||
}
|
||||
if strings.HasPrefix(name, "*") {
|
||||
if name[1] != '*' {
|
||||
list = append(list, ssa.VArg())
|
||||
return types.NewTuple(list...), true
|
||||
}
|
||||
return types.NewTuple(list...), false
|
||||
}
|
||||
list = append(list, pkg.NewParam(0, genName(name, 0), objPtr))
|
||||
}
|
||||
return types.NewTuple(list...), false
|
||||
}
|
||||
|
||||
func genName(name string, idxDontTitle int) string {
|
||||
parts := strings.Split(name, "_")
|
||||
for i, part := range parts {
|
||||
if i != idxDontTitle && part != "" {
|
||||
if c := part[0]; c >= 'a' && c <= 'z' {
|
||||
part = string(c+'A'-'a') + part[1:]
|
||||
}
|
||||
parts[i] = part
|
||||
}
|
||||
}
|
||||
name = strings.Join(parts, "")
|
||||
switch name {
|
||||
case "default", "func", "var", "range", "":
|
||||
name += "_"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (ctx *context) genLinkname(name string, sym *symbol) *ast.Comment {
|
||||
return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name}
|
||||
}
|
||||
|
||||
func (ctx *context) genDoc(doc string) []*ast.Comment {
|
||||
if doc == "" {
|
||||
return make([]*ast.Comment, 0, 4)
|
||||
}
|
||||
lines := strings.Split(doc, "\n")
|
||||
list := make([]*ast.Comment, len(lines), len(lines)+4)
|
||||
for i, line := range lines {
|
||||
list[i] = &ast.Comment{Text: "// " + line}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func genSee(url string) *ast.Comment {
|
||||
return &ast.Comment{Text: "// See " + url}
|
||||
}
|
||||
|
||||
var (
|
||||
emptyCommentLine = &ast.Comment{Text: "//"}
|
||||
)
|
||||
100
compiler/chore/llpyg/pysig/parse.go
Normal file
100
compiler/chore/llpyg/pysig/parse.go
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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 pysig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Arg struct {
|
||||
Name string
|
||||
Type string
|
||||
DefVal string
|
||||
}
|
||||
|
||||
// Parse parses a Python function signature.
|
||||
func Parse(sig string) (args []*Arg) {
|
||||
sig = strings.TrimPrefix(sig, "(")
|
||||
for {
|
||||
pos := strings.IndexAny(sig, ",:=)")
|
||||
if pos <= 0 {
|
||||
return
|
||||
}
|
||||
arg := &Arg{Name: strings.TrimSpace(sig[:pos])}
|
||||
args = append(args, arg)
|
||||
c := sig[pos]
|
||||
sig = sig[pos+1:]
|
||||
switch c {
|
||||
case ',':
|
||||
continue
|
||||
case ':':
|
||||
arg.Type, sig = parseType(sig)
|
||||
if strings.HasPrefix(sig, "=") {
|
||||
arg.DefVal, sig = parseDefVal(sig[1:])
|
||||
}
|
||||
case '=':
|
||||
arg.DefVal, sig = parseDefVal(sig)
|
||||
case ')':
|
||||
return
|
||||
}
|
||||
sig = strings.TrimPrefix(sig, ",")
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
allSpecials = "([<'\""
|
||||
)
|
||||
|
||||
var pairStops = map[byte]string{
|
||||
'(': ")" + allSpecials,
|
||||
'[': "]" + allSpecials,
|
||||
'<': ">" + allSpecials,
|
||||
'\'': "'" + allSpecials,
|
||||
'"': "\"",
|
||||
}
|
||||
|
||||
func parseText(sig string, stops string) (left string) {
|
||||
for {
|
||||
pos := strings.IndexAny(sig, stops)
|
||||
if pos < 0 {
|
||||
return sig
|
||||
}
|
||||
if c := sig[pos]; c != stops[0] {
|
||||
if pstop, ok := pairStops[c]; ok {
|
||||
sig = strings.TrimPrefix(parseText(sig[pos+1:], pstop), pstop[:1])
|
||||
continue
|
||||
}
|
||||
}
|
||||
return sig[pos:]
|
||||
}
|
||||
}
|
||||
|
||||
// stops: "=,)"
|
||||
func parseType(sig string) (string, string) {
|
||||
left := parseText(sig, "=,)"+allSpecials)
|
||||
return resultOf(sig, left), left
|
||||
}
|
||||
|
||||
// stops: ",)"
|
||||
func parseDefVal(sig string) (string, string) {
|
||||
left := parseText(sig, ",)"+allSpecials)
|
||||
return resultOf(sig, left), left
|
||||
}
|
||||
|
||||
func resultOf(sig, left string) string {
|
||||
return strings.TrimSpace(sig[:len(sig)-len(left)])
|
||||
}
|
||||
52
compiler/chore/llpyg/pysig/parse_test.go
Normal file
52
compiler/chore/llpyg/pysig/parse_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 pysig
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
type testCase struct {
|
||||
sig string
|
||||
args []*Arg
|
||||
}
|
||||
cases := []testCase{
|
||||
{"(start=None, *, unit: 'str | None' = None) -> 'TimedeltaIndex'", []*Arg{
|
||||
{Name: "start", DefVal: "None"},
|
||||
{Name: "*"},
|
||||
{Name: "unit", Type: "'str | None'", DefVal: "None"},
|
||||
}},
|
||||
{"()", nil},
|
||||
{"(a =", []*Arg{{Name: "a"}}},
|
||||
{"(a) -> int", []*Arg{{Name: "a"}}},
|
||||
{"(a: int)", []*Arg{{Name: "a", Type: "int"}}},
|
||||
{"(a: int = 1, b: float)", []*Arg{{Name: "a", Type: "int", DefVal: "1"}, {Name: "b", Type: "float"}}},
|
||||
{"(a = <1>, b = 2.0)", []*Arg{{Name: "a", DefVal: "<1>"}, {Name: "b", DefVal: "2.0"}}},
|
||||
{"(a: 'Suffixes' = ('_x', '_y'))", []*Arg{{Name: "a", Type: "'Suffixes'", DefVal: "('_x', '_y')"}}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
args := Parse(c.sig)
|
||||
if len(args) != len(c.args) {
|
||||
t.Fatalf("%s: len(args) = %v, want %v", c.sig, len(args), len(c.args))
|
||||
}
|
||||
for i, arg := range args {
|
||||
want := c.args[i]
|
||||
if arg.Name != want.Name || arg.Type != want.Type || arg.DefVal != want.DefVal {
|
||||
t.Fatalf("%s: args[%v] = %v, want %v", c.sig, i, arg, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
compiler/chore/llvmtargets/llvm_targets.go
Normal file
18
compiler/chore/llvmtargets/llvm_targets.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/goplus/llvm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
llvm.InitializeAllTargetInfos()
|
||||
llvm.InitializeAllTargets()
|
||||
llvm.InitializeAllTargetMCs()
|
||||
llvm.InitializeNativeTarget()
|
||||
fmt.Println("targets:")
|
||||
for it := llvm.FirstTarget(); it.C != nil; it = it.NextTarget() {
|
||||
fmt.Printf("- %s: %s\n", it.Name(), it.Description())
|
||||
}
|
||||
}
|
||||
66
compiler/chore/nmdump/nmdump.go
Normal file
66
compiler/chore/nmdump/nmdump.go
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/xtool/env/llvm"
|
||||
nmtool "github.com/goplus/llgo/xtool/nm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: nmdump [flags] libfile")
|
||||
return
|
||||
}
|
||||
|
||||
nm := llvm.New("").Nm()
|
||||
|
||||
var flags []string
|
||||
libfile := os.Args[len(os.Args)-1]
|
||||
if len(os.Args) > 2 {
|
||||
flags = os.Args[1 : len(os.Args)-1]
|
||||
}
|
||||
|
||||
items, err := nm.List(libfile, flags...)
|
||||
|
||||
for _, item := range items {
|
||||
if item.File != "" {
|
||||
fmt.Printf("\n%s:\n", item.File)
|
||||
}
|
||||
for _, sym := range item.Symbols {
|
||||
var versionInfo string
|
||||
switch sym.VersionType {
|
||||
case nmtool.VersionSpecific:
|
||||
versionInfo = fmt.Sprintf("@%s", sym.Version)
|
||||
case nmtool.VersionDefault:
|
||||
versionInfo = fmt.Sprintf("@@%s", sym.Version)
|
||||
}
|
||||
if sym.FAddr {
|
||||
fmt.Printf("%016x %c %s%s\n", sym.Addr, sym.Type, sym.Name, versionInfo)
|
||||
} else {
|
||||
fmt.Printf("%16s %c %s%s\n", "", sym.Type, sym.Name, versionInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
120
compiler/chore/nmindex/nmindex.go
Normal file
120
compiler/chore/nmindex/nmindex.go
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/llgo/xtool/env/llvm"
|
||||
"github.com/goplus/llgo/xtool/nm/nmindex"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprint(os.Stderr, `Usage:
|
||||
nmindex <command> [arguments]
|
||||
|
||||
The commands are:
|
||||
|
||||
mk Create index file
|
||||
q Query a symbol
|
||||
|
||||
`)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := os.Args[1]
|
||||
switch cmd {
|
||||
case "mk":
|
||||
makeIndex()
|
||||
case "q":
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Fprint(os.Stderr, "Usage: nmindex q <symbol>\n")
|
||||
return
|
||||
}
|
||||
query(os.Args[2])
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown command: %s\n", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func makeIndex() {
|
||||
env := llvm.New("")
|
||||
idxDir := indexDir()
|
||||
os.MkdirAll(idxDir, 0755)
|
||||
|
||||
b := nmindex.NewIndexBuilder(env.Nm())
|
||||
libDirs := []string{
|
||||
usrLib(false),
|
||||
usrLib(true),
|
||||
stdLib("LLGO_STDROOT"),
|
||||
stdLib("LLGO_USRROOT"),
|
||||
pythonLib(),
|
||||
}
|
||||
err := b.Index(libDirs, idxDir, func(path string) {
|
||||
fmt.Println("==>", path)
|
||||
})
|
||||
check(err)
|
||||
}
|
||||
|
||||
func query(q string) {
|
||||
if len(q) > 0 {
|
||||
if c := q[0]; c != '*' && c != '_' {
|
||||
q = "_" + q
|
||||
}
|
||||
}
|
||||
files, err := nmindex.Query(indexDir(), q)
|
||||
check(err)
|
||||
for _, f := range files {
|
||||
fmt.Printf("%s:\n", f.ArFile)
|
||||
for _, item := range f.Items {
|
||||
fmt.Printf(" %c %s %s\n", item.Type, item.Symbol, item.ObjFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func indexDir() string {
|
||||
home, err := os.UserHomeDir()
|
||||
check(err)
|
||||
return home + "/.llgo/nmindex"
|
||||
}
|
||||
|
||||
func stdLib(where string) string {
|
||||
dir := os.Getenv(where)
|
||||
if dir != "" {
|
||||
dir += "/lib"
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
func usrLib(local bool) string {
|
||||
if local {
|
||||
return "/usr/local/lib"
|
||||
}
|
||||
return "/usr/lib"
|
||||
}
|
||||
|
||||
func pythonLib() string {
|
||||
return os.Getenv("LLGO_PYTHON_ROOT")
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
201
compiler/chore/ssadump/ssadump.go
Normal file
201
compiler/chore/ssadump/ssadump.go
Normal file
@@ -0,0 +1,201 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
|
||||
package main // import "golang.org/x/tools/cmd/ssadump"
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"go/types"
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
|
||||
"golang.org/x/tools/go/buildutil"
|
||||
"golang.org/x/tools/go/packages"
|
||||
"golang.org/x/tools/go/ssa"
|
||||
"golang.org/x/tools/go/ssa/interp"
|
||||
"golang.org/x/tools/go/ssa/ssautil"
|
||||
)
|
||||
|
||||
const (
|
||||
loadFiles = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles
|
||||
loadImports = loadFiles | packages.NeedImports
|
||||
loadTypes = loadImports | packages.NeedTypes | packages.NeedTypesSizes
|
||||
loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo
|
||||
)
|
||||
|
||||
// flags
|
||||
var (
|
||||
mode = ssa.BuilderMode(0)
|
||||
|
||||
testFlag = flag.Bool("test", false, "include implicit test packages and executables")
|
||||
|
||||
runFlag = flag.Bool("run", false, "interpret the SSA program")
|
||||
|
||||
interpFlag = flag.String("interp", "", `Options controlling the SSA test interpreter.
|
||||
The value is a sequence of zero or more more of these letters:
|
||||
R disable [R]ecover() from panic; show interpreter crash instead.
|
||||
T [T]race execution of the program. Best for single-threaded programs!
|
||||
`)
|
||||
|
||||
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
|
||||
args stringListValue
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.Var(&mode, "build", ssa.BuilderModeDoc)
|
||||
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
|
||||
flag.Var(&args, "arg", "add argument to interpreted program")
|
||||
}
|
||||
|
||||
const usage = `SSA builder and interpreter.
|
||||
Usage: ssadump [-build=[DBCSNFLG]] [-test] [-run] [-interp=[TR]] [-arg=...] package...
|
||||
Use -help flag to display options.
|
||||
|
||||
Examples:
|
||||
% ssadump -build=F hello.go # dump SSA form of a single package
|
||||
% ssadump -build=F -test fmt # dump SSA form of a package and its tests
|
||||
% ssadump -run -interp=T hello.go # interpret a program, with tracing
|
||||
|
||||
The -run flag causes ssadump to build the code in a runnable form and run the first
|
||||
package named main.
|
||||
|
||||
Interpretation of the standard "testing" package is no longer supported.
|
||||
`
|
||||
|
||||
func main() {
|
||||
if err := doMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ssadump: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func doMain() error {
|
||||
flag.Parse()
|
||||
if len(flag.Args()) == 0 {
|
||||
fmt.Fprint(os.Stderr, usage)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfg := &packages.Config{
|
||||
Mode: loadSyntax,
|
||||
Tests: *testFlag,
|
||||
}
|
||||
|
||||
// Choose types.Sizes from conf.Build.
|
||||
// TODO(adonovan): remove this when go/packages provides a better way.
|
||||
var wordSize int64 = 8
|
||||
switch build.Default.GOARCH {
|
||||
case "386", "arm":
|
||||
wordSize = 4
|
||||
}
|
||||
sizes := &types.StdSizes{
|
||||
MaxAlign: 8,
|
||||
WordSize: wordSize,
|
||||
}
|
||||
|
||||
var interpMode interp.Mode
|
||||
for _, c := range *interpFlag {
|
||||
switch c {
|
||||
case 'T':
|
||||
interpMode |= interp.EnableTracing
|
||||
case 'R':
|
||||
interpMode |= interp.DisableRecover
|
||||
default:
|
||||
return fmt.Errorf("unknown -interp option: '%c'", c)
|
||||
}
|
||||
}
|
||||
|
||||
// Profiling support.
|
||||
if *cpuprofile != "" {
|
||||
f, err := os.Create(*cpuprofile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pprof.StartCPUProfile(f)
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
// Load, parse and type-check the initial packages,
|
||||
// and, if -run, their dependencies.
|
||||
if *runFlag {
|
||||
cfg.Mode = loadSyntax | packages.NeedDeps
|
||||
}
|
||||
initial, err := packages.Load(cfg, flag.Args()...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(initial) == 0 {
|
||||
return fmt.Errorf("no packages")
|
||||
}
|
||||
if packages.PrintErrors(initial) > 0 {
|
||||
return fmt.Errorf("packages contain errors")
|
||||
}
|
||||
|
||||
// Turn on instantiating generics during build if the program will be run.
|
||||
if *runFlag {
|
||||
mode |= ssa.InstantiateGenerics
|
||||
}
|
||||
|
||||
// Create SSA-form program representation.
|
||||
prog, pkgs := ssautil.AllPackages(initial, mode)
|
||||
|
||||
for i, p := range pkgs {
|
||||
if p == nil {
|
||||
return fmt.Errorf("cannot build SSA for package %s", initial[i])
|
||||
}
|
||||
}
|
||||
|
||||
if !*runFlag {
|
||||
// Build and display only the initial packages
|
||||
// (and synthetic wrappers).
|
||||
for _, p := range pkgs {
|
||||
p.Build()
|
||||
}
|
||||
|
||||
} else {
|
||||
// Run the interpreter.
|
||||
// Build SSA for all packages.
|
||||
prog.Build()
|
||||
|
||||
// Earlier versions of the interpreter needed the runtime
|
||||
// package; however, interp cannot handle unsafe constructs
|
||||
// used during runtime's package initialization at the moment.
|
||||
// The key construct blocking support is:
|
||||
// *((*T)(unsafe.Pointer(p)))
|
||||
// Unfortunately, this means only trivial programs can be
|
||||
// interpreted by ssadump.
|
||||
if prog.ImportedPackage("runtime") != nil {
|
||||
return fmt.Errorf("-run: program depends on runtime package (interpreter can run only trivial programs)")
|
||||
}
|
||||
|
||||
if runtime.GOARCH != build.Default.GOARCH {
|
||||
return fmt.Errorf("cross-interpretation is not supported (target has GOARCH %s, interpreter has %s)",
|
||||
build.Default.GOARCH, runtime.GOARCH)
|
||||
}
|
||||
|
||||
// Run first main package.
|
||||
for _, main := range ssautil.MainPackages(pkgs) {
|
||||
fmt.Fprintf(os.Stderr, "Running: %s\n", main.Pkg.Path())
|
||||
os.Exit(interp.Interpret(main, interpMode, sizes, main.Pkg.Path(), args))
|
||||
}
|
||||
return fmt.Errorf("no main package")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// stringListValue is a flag.Value that accumulates strings.
|
||||
// e.g. --flag=one --flag=two would produce []string{"one", "two"}.
|
||||
type stringListValue []string
|
||||
|
||||
func (ss *stringListValue) Get() interface{} { return []string(*ss) }
|
||||
|
||||
func (ss *stringListValue) String() string { return fmt.Sprintf("%q", *ss) }
|
||||
|
||||
func (ss *stringListValue) Set(s string) error { *ss = append(*ss, s); return nil }
|
||||
Reference in New Issue
Block a user