build: separate compiler and libs

This commit is contained in:
Li Jie
2025-01-07 21:49:08 +08:00
parent b0123567cd
commit 1172e5bdce
559 changed files with 190 additions and 176 deletions

View 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(&copyBuf, 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
}

View 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
}
}
*/
// -----------------------------------------------------------------------------

View 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)
}
// -----------------------------------------------------------------------------

View 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)
}
}

View 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
)

View 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=

View 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;
}

View File

@@ -0,0 +1,2 @@
export LLVM_DIR=$(llvm-config --prefix)
clang -L$LLVM_DIR/lib -lclang -lc++ -I$LLVM_DIR/include astdump.cpp

View 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)
}

View 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())
}

View 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)
}
}

View 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)
}
}

View 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)
}
}

View 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)
}
}

View 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])
}

View 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: "//"}
)

View 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)])
}

View 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)
}
}
}
}

View 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())
}
}

View 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)
}
}

View 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)
}
}

View 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 }

View File

@@ -0,0 +1,8 @@
package apkg
func Max(a, b float64) float64 {
if a > b {
return a
}
return b
}

View File

@@ -0,0 +1,29 @@
; ModuleID = 'github.com/goplus/llgo/cl/_testdata/apkg'
source_filename = "github.com/goplus/llgo/cl/_testdata/apkg"
@"github.com/goplus/llgo/cl/_testdata/apkg.init$guard" = global i1 false, align 1
define double @"github.com/goplus/llgo/cl/_testdata/apkg.Max"(double %0, double %1) {
_llgo_0:
%2 = fcmp ogt double %0, %1
br i1 %2, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
ret double %0
_llgo_2: ; preds = %_llgo_0
ret double %1
}
define void @"github.com/goplus/llgo/cl/_testdata/apkg.init"() {
_llgo_0:
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testdata/apkg.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testdata/apkg.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}

View File

@@ -0,0 +1 @@
-dbg

View File

@@ -0,0 +1,569 @@
package main
import "errors"
type Base struct {
name string
}
type E struct {
// Base
i int
}
type StructWithAllTypeFields struct {
i8 int8
i16 int16
i32 int32
i64 int64
i int
u8 uint8
u16 uint16
u32 uint32
u64 uint64
u uint
f32 float32
f64 float64
b bool
c64 complex64
c128 complex128
slice []int
arr [3]int
arr2 [3]E
s string
e E
pf *StructWithAllTypeFields // resursive
pi *int
intr Interface
m map[string]uint64
c chan int
err error
fn func(string) (int, error)
pad1 int
pad2 int
}
type Interface interface {
Foo(a []int, b string) int
}
type Struct struct{}
func (s *Struct) Foo(a []int, b string) int {
return 1
}
func FuncWithAllTypeStructParam(s StructWithAllTypeFields) {
println(&s)
// Expected:
// all variables: s
// s.i8: '\x01'
// s.i16: 2
// s.i32: 3
// s.i64: 4
// s.i: 5
// s.u8: '\x06'
// s.u16: 7
// s.u32: 8
// s.u64: 9
// s.u: 10
// s.f32: 11
// s.f64: 12
// s.b: true
// s.c64: complex64{real = 13, imag = 14}
// s.c128: complex128{real = 15, imag = 16}
// s.slice: []int{21, 22, 23}
// s.arr: [3]int{24, 25, 26}
// s.arr2: [3]github.com/goplus/llgo/compiler/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
// s.s: "hello"
// s.e: github.com/goplus/llgo/compiler/cl/_testdata/debug.E{i = 30}
// s.pad1: 100
// s.pad2: 200
s.i8 = '\b'
// Expected:
// s.i8: '\b'
// s.i16: 2
println(len(s.s), s.i8)
}
// Params is a function with all types of parameters.
func FuncWithAllTypeParams(
i8 int8,
i16 int16,
i32 int32,
i64 int64,
i int,
u8 uint8,
u16 uint16,
u32 uint32,
u64 uint64,
u uint,
f32 float32,
f64 float64,
b bool,
c64 complex64,
c128 complex128,
slice []int,
arr [3]int,
arr2 [3]E,
s string,
e E,
f StructWithAllTypeFields,
pf *StructWithAllTypeFields,
pi *int,
intr Interface,
m map[string]uint64,
c chan int,
err error,
fn func(string) (int, error),
) (int, error) {
// Expected:
// all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn
// i32: 3
// i64: 4
// i: 5
// u32: 8
// u64: 9
// u: 10
// f32: 11
// f64: 12
// slice: []int{21, 22, 23}
// arr: [3]int{24, 25, 26}
// arr2: [3]github.com/goplus/llgo/compiler/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
// slice[0]: 21
// slice[1]: 22
// slice[2]: 23
// arr[0]: 24
// arr[1]: 25
// arr[2]: 26
// arr2[0].i: 27
// arr2[1].i: 28
// arr2[2].i: 29
// e: github.com/goplus/llgo/compiler/cl/_testdata/debug.E{i = 30}
// Expected(skip):
// i8: '\b'
// i16: 2
// u8: '\x06'
// u16: 7
// b: true
println(
i8, i16, i32, i64, i, u8, u16, u32, u64, u,
f32, f64, b,
c64, c128,
slice, arr[0:],
s,
&e,
&f, pf, pi, intr, m,
c,
err,
fn,
)
i8 = 9
i16 = 10
i32 = 11
i64 = 12
i = 13
u8 = 14
u16 = 15
u32 = 16
u64 = 17
u = 18
f32 = 19
f64 = 20
b = false
c64 = 21 + 22i
c128 = 23 + 24i
slice = []int{31, 32, 33}
arr = [3]int{34, 35, 36}
arr2 = [3]E{{i: 37}, {i: 38}, {i: 39}}
s = "world"
e = E{i: 40}
println(i8, i16, i32, i64, i, u8, u16, u32, u64, u,
f32, f64, b,
c64, c128,
slice, arr[0:], &arr2,
s,
&e,
&f, pf, pi, intr, m,
c,
err,
fn,
)
// Expected:
// i8: '\t'
// i16: 10
// i32: 11
// i64: 12
// i: 13
// u8: '\x0e'
// u16: 15
// u32: 16
// u64: 17
// u: 18
// f32: 19
// f64: 20
// b: false
// c64: complex64{real = 21, imag = 22}
// c128: complex128{real = 23, imag = 24}
// slice: []int{31, 32, 33}
// arr2: [3]github.com/goplus/llgo/compiler/cl/_testdata/debug.E{{i = 37}, {i = 38}, {i = 39}}
// s: "world"
// e: github.com/goplus/llgo/compiler/cl/_testdata/debug.E{i = 40}
// Expected(skip):
// arr: [3]int{34, 35, 36}
return 1, errors.New("some error")
}
type TinyStruct struct {
I int
}
type SmallStruct struct {
I int
J int
}
type MidStruct struct {
I int
J int
K int
}
type BigStruct struct {
I int
J int
K int
L int
M int
N int
O int
P int
Q int
R int
}
func FuncStructParams(t TinyStruct, s SmallStruct, m MidStruct, b BigStruct) {
// println(&t, &s, &m, &b)
// Expected:
// all variables: t s m b
// t.I: 1
// s.I: 2
// s.J: 3
// m.I: 4
// m.J: 5
// m.K: 6
// b.I: 7
// b.J: 8
// b.K: 9
// b.L: 10
// b.M: 11
// b.N: 12
// b.O: 13
// b.P: 14
// b.Q: 15
// b.R: 16
println(t.I, s.I, s.J, m.I, m.J, m.K, b.I, b.J, b.K, b.L, b.M, b.N, b.O, b.P, b.Q, b.R)
t.I = 10
s.I = 20
s.J = 21
m.I = 40
m.J = 41
m.K = 42
b.I = 70
b.J = 71
b.K = 72
b.L = 73
b.M = 74
b.N = 75
b.O = 76
b.P = 77
b.Q = 78
b.R = 79
// Expected:
// all variables: t s m b
// t.I: 10
// s.I: 20
// s.J: 21
// m.I: 40
// m.J: 41
// m.K: 42
// b.I: 70
// b.J: 71
// b.K: 72
// b.L: 73
// b.M: 74
// b.N: 75
// b.O: 76
// b.P: 77
// b.Q: 78
// b.R: 79
println("done")
}
func FuncStructPtrParams(t *TinyStruct, s *SmallStruct, m *MidStruct, b *BigStruct) {
// Expected:
// all variables: t s m b
// t.I: 1
// s.I: 2
// s.J: 3
// m.I: 4
// m.J: 5
// m.K: 6
// b.I: 7
// b.J: 8
// b.K: 9
// b.L: 10
// b.M: 11
// b.N: 12
// b.O: 13
// b.P: 14
// b.Q: 15
// b.R: 16
println(t, s, m, b)
t.I = 10
s.I = 20
s.J = 21
m.I = 40
m.J = 41
m.K = 42
b.I = 70
b.J = 71
b.K = 72
b.L = 73
b.M = 74
b.N = 75
b.O = 76
b.P = 77
b.Q = 78
b.R = 79
// Expected:
// all variables: t s m b
// t.I: 10
// s.I: 20
// s.J: 21
// m.I: 40
// m.J: 41
// m.K: 42
// b.I: 70
// b.J: 71
// b.K: 72
// b.L: 73
// b.M: 74
// b.N: 75
// b.O: 76
// b.P: 77
// b.Q: 78
// b.R: 79
println(t.I, s.I, s.J, m.I, m.J, m.K, b.I, b.J, b.K, b.L, b.M, b.N, b.O, b.P, b.Q, b.R)
println("done")
}
func ScopeIf(branch int) {
a := 1
// Expected:
// all variables: a branch
// a: 1
if branch == 1 {
b := 2
c := 3
// Expected:
// all variables: a b c branch
// a: 1
// b: 2
// c: 3
// branch: 1
println(a, b, c)
} else {
c := 3
d := 4
// Expected:
// all variables: a c d branch
// a: 1
// c: 3
// d: 4
// branch: 0
println(a, c, d)
}
// Expected:
// all variables: a branch
// a: 1
println("a:", a)
}
func ScopeFor() {
a := 1
for i := 0; i < 10; i++ {
switch i {
case 0:
println("i is 0")
// Expected:
// all variables: i a
// i: 0
// a: 1
println("i:", i)
case 1:
println("i is 1")
// Expected:
// all variables: i a
// i: 1
// a: 1
println("i:", i)
default:
println("i is", i)
}
}
println("a:", a)
}
func ScopeSwitch(i int) {
a := 0
switch i {
case 1:
b := 1
println("i is 1")
// Expected:
// all variables: i a b
// i: 1
// a: 0
// b: 1
println("i:", i, "a:", a, "b:", b)
case 2:
c := 2
println("i is 2")
// Expected:
// all variables: i a c
// i: 2
// a: 0
// c: 2
println("i:", i, "a:", a, "c:", c)
default:
d := 3
println("i is", i)
// Expected:
// all variables: i a d
// i: 3
// a: 0
// d: 3
println("i:", i, "a:", a, "d:", d)
}
// Expected:
// all variables: a i
// a: 0
println("a:", a)
}
func main() {
FuncStructParams(TinyStruct{I: 1}, SmallStruct{I: 2, J: 3}, MidStruct{I: 4, J: 5, K: 6}, BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
FuncStructPtrParams(&TinyStruct{I: 1}, &SmallStruct{I: 2, J: 3}, &MidStruct{I: 4, J: 5, K: 6}, &BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
i := 100
s := StructWithAllTypeFields{
i8: 1,
i16: 2,
i32: 3,
i64: 4,
i: 5,
u8: 6,
u16: 7,
u32: 8,
u64: 9,
u: 10,
f32: 11,
f64: 12,
b: true,
c64: 13 + 14i,
c128: 15 + 16i,
slice: []int{21, 22, 23},
arr: [3]int{24, 25, 26},
arr2: [3]E{{i: 27}, {i: 28}, {i: 29}},
s: "hello",
e: E{i: 30},
pf: &StructWithAllTypeFields{i16: 100},
pi: &i,
intr: &Struct{},
m: map[string]uint64{"a": 31, "b": 32},
c: make(chan int),
err: errors.New("Test error"),
fn: func(s string) (int, error) {
println("fn:", s)
i = 201
return 1, errors.New("fn error")
},
pad1: 100,
pad2: 200,
}
// Expected:
// all variables: s i err
// s.i8: '\x01'
// s.i16: 2
// s.i32: 3
// s.i64: 4
// s.i: 5
// s.u8: '\x06'
// s.u16: 7
// s.u32: 8
// s.u64: 9
// s.u: 10
// s.f32: 11
// s.f64: 12
// s.b: true
// s.c64: complex64{real = 13, imag = 14}
// s.c128: complex128{real = 15, imag = 16}
// s.slice: []int{21, 22, 23}
// s.arr: [3]int{24, 25, 26}
// s.arr2: [3]github.com/goplus/llgo/compiler/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
// s.s: "hello"
// s.e: github.com/goplus/llgo/compiler/cl/_testdata/debug.E{i = 30}
// s.pf.i16: 100
// *(s.pf).i16: 100
// *(s.pi): 100
globalStructPtr = &s
globalStruct = s
println("globalInt:", globalInt)
// Expected(skip):
// all variables: globalInt globalStruct globalStructPtr s i err
println("s:", &s)
FuncWithAllTypeStructParam(s)
println("called function with struct")
i, err := FuncWithAllTypeParams(
s.i8, s.i16, s.i32, s.i64, s.i, s.u8, s.u16, s.u32, s.u64, s.u,
s.f32, s.f64, s.b,
s.c64, s.c128,
s.slice, s.arr, s.arr2,
s.s,
s.e, s,
s.pf, s.pi,
s.intr,
s.m,
s.c,
s.err,
s.fn,
)
println(i, err)
ScopeIf(1)
ScopeIf(0)
ScopeFor()
ScopeSwitch(1)
ScopeSwitch(2)
ScopeSwitch(3)
println(globalStructPtr)
println(&globalStruct)
s.i8 = 0x12
println(s.i8)
// Expected:
// all variables: s i err
// s.i8: '\x12'
// Expected(skip):
// globalStruct.i8: '\x01'
println((*globalStructPtr).i8)
println("done")
println("")
println(&s, &globalStruct, globalStructPtr.i16, globalStructPtr)
globalStructPtr = nil
}
var globalInt int = 301
var globalStruct StructWithAllTypeFields
var globalStructPtr *StructWithAllTypeFields

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,12 @@
package main
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
_ = max(1, 2)
}

View File

@@ -0,0 +1,43 @@
; ModuleID = 'main'
source_filename = "main"
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call i64 @main.max(i64 1, i64 2)
ret i32 0
}
define i64 @main.max(i64 %0, i64 %1) {
_llgo_0:
%2 = icmp sgt i64 %0, %1
br i1 %2, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
ret i64 %0
_llgo_2: ; preds = %_llgo_0
ret i64 %1
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -0,0 +1,10 @@
package main
import "github.com/goplus/llgo/compiler/cl/internal/stdio"
var hello = [...]int8{'H', 'e', 'l', 'l', 'o', '\n', 0}
func main() {
_ = stdio.Max(2, 100)
stdio.Printf(&hello[0])
}

View File

@@ -0,0 +1,47 @@
; ModuleID = 'main'
source_filename = "main"
@main.hello = global [7 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @"github.com/goplus/llgo/cl/internal/stdio.init"()
store i8 72, ptr @main.hello, align 1
store i8 101, ptr getelementptr inbounds (i8, ptr @main.hello, i64 1), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.hello, i64 2), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.hello, i64 3), align 1
store i8 111, ptr getelementptr inbounds (i8, ptr @main.hello, i64 4), align 1
store i8 10, ptr getelementptr inbounds (i8, ptr @main.hello, i64 5), align 1
store i8 0, ptr getelementptr inbounds (i8, ptr @main.hello, i64 6), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64 2, i64 100)
call void (ptr, ...) @printf(ptr @main.hello)
ret i32 0
}
declare void @"github.com/goplus/llgo/cl/internal/stdio.init"()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i64 @"github.com/goplus/llgo/cl/internal/stdio.Max"(i64, i64)
declare void @printf(ptr, ...)

View File

@@ -0,0 +1,7 @@
//go:build llgo
// +build llgo
package llgotag
func Foo() {
}

View File

@@ -0,0 +1,22 @@
; ModuleID = 'github.com/goplus/llgo/cl/_testdata/llgotag'
source_filename = "github.com/goplus/llgo/cl/_testdata/llgotag"
@"github.com/goplus/llgo/cl/_testdata/llgotag.init$guard" = global i1 false, align 1
define void @"github.com/goplus/llgo/cl/_testdata/llgotag.Foo"() {
_llgo_0:
ret void
}
define void @"github.com/goplus/llgo/cl/_testdata/llgotag.init"() {
_llgo_0:
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testdata/llgotag.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testdata/llgotag.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}

View File

@@ -0,0 +1,19 @@
package main
import _ "unsafe"
type T int
func (a T) Add(b T) T {
return a + b
}
//go:linkname printf C.printf
func printf(format *int8, __llgo_va_list ...any)
var format = [...]int8{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0}
func main() {
a := T(1)
printf(&format[0], a.Add(2))
}

View File

@@ -0,0 +1,58 @@
; ModuleID = 'main'
source_filename = "main"
@main.format = global [10 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define i64 @main.T.Add(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define i64 @"main.(*T).Add"(ptr %0, i64 %1) {
_llgo_0:
%2 = load i64, ptr %0, align 4
%3 = call i64 @main.T.Add(i64 %2, i64 %1)
ret i64 %3
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store i8 72, ptr @main.format, align 1
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 3), align 1
store i8 111, ptr getelementptr inbounds (i8, ptr @main.format, i64 4), align 1
store i8 32, ptr getelementptr inbounds (i8, ptr @main.format, i64 5), align 1
store i8 37, ptr getelementptr inbounds (i8, ptr @main.format, i64 6), align 1
store i8 100, ptr getelementptr inbounds (i8, ptr @main.format, i64 7), align 1
store i8 10, ptr getelementptr inbounds (i8, ptr @main.format, i64 8), align 1
store i8 0, ptr getelementptr inbounds (i8, ptr @main.format, i64 9), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call i64 @main.T.Add(i64 1, i64 2)
call void (ptr, ...) @printf(ptr @main.format, i64 %2)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare void @printf(ptr, ...)

View File

@@ -0,0 +1,277 @@
package main
import (
"unsafe"
"github.com/goplus/llgo/c"
)
func gwrite(b []byte) {
if len(b) == 0 {
return
}
for _, v := range b {
c.Printf(c.Str("%c"), v)
}
}
func printbool(v bool) {
if v {
printstring("true")
} else {
printstring("false")
}
}
func printfloat(v float64) {
switch {
case v != v:
printstring("NaN")
return
case v+v == v && v > 0:
printstring("+Inf")
return
case v+v == v && v < 0:
printstring("-Inf")
return
}
const n = 7 // digits printed
var buf [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := 5.0
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float64(s)
v *= 10
}
buf[1] = buf[2]
buf[2] = '.'
buf[n+2] = 'e'
buf[n+3] = '+'
if e < 0 {
e = -e
buf[n+3] = '-'
}
buf[n+4] = byte(e/100) + '0'
buf[n+5] = byte(e/10)%10 + '0'
buf[n+6] = byte(e%10) + '0'
gwrite(buf[:])
}
func printuint(v uint64) {
var buf [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = byte(v%10 + '0')
if v < 10 {
break
}
v /= 10
}
gwrite(buf[i:])
}
func printint(v int64) {
if v < 0 {
printstring("-")
v = -v
}
printuint(uint64(v))
}
var minhexdigits = 0
func printhex(v uint64) {
const dig = "0123456789abcdef"
var buf [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = dig[v%16]
if v < 16 && len(buf)-i >= minhexdigits {
break
}
v /= 16
}
i--
buf[i] = 'x'
i--
buf[i] = '0'
gwrite(buf[i:])
}
func printsp() {
printstring(" ")
}
func printnl() {
printstring("\n")
}
func printstring(s string) {
gwrite(bytes(s))
}
type slice struct {
array unsafe.Pointer
len int
cap int
}
type stringStruct struct {
str unsafe.Pointer
len int
}
func stringStructOf(sp *string) *stringStruct {
return (*stringStruct)(unsafe.Pointer(sp))
}
func bytes(s string) (ret []byte) {
rp := (*slice)(unsafe.Pointer(&ret))
sp := stringStructOf(&s)
rp.array = sp.str
rp.len = sp.len
rp.cap = sp.len
return
}
func main() {
printstring("llgo")
printnl()
printuint(1024)
printnl()
printhex(0x1234abcf)
printnl()
prinxor(1)
printnl()
prinsub(100)
printnl()
prinusub(1<<64 - 1)
printnl()
prinfsub(100.1)
printnl()
printany(float32(1e9))
printnl()
printany(float64(2e9))
printnl()
var b bool = true
if b == true && b != false {
println("check bool", b)
}
n1 := 0b1001
n2 := 0b0011
println("check &^", n1&^n2 == 0b1000, n2&^n1 == 0b0010)
println(true, false, 'a', 'A', rune('中'),
int8(1), int16(2), int32(3), int64(4), 5,
uint8(1), uint16(2), uint32(3), uint64(4), uintptr(5),
"llgo")
println(1 + 2i)
}
func println(args ...any) {
for i, v := range args {
if i != 0 {
printstring(" ")
}
printany(v)
}
printnl()
}
func printany(v any) {
switch v := v.(type) {
case bool:
printbool(v)
case int:
printint(int64(v))
case int8:
printint(int64(v))
case int16:
printint(int64(v))
case int32:
printint(int64(v))
case int64:
printint(int64(v))
case uint:
printuint(uint64(v))
case uint8:
printuint(uint64(v))
case uint16:
printuint(uint64(v))
case uint32:
printuint(uint64(v))
case uint64:
printuint(uint64(v))
case uintptr:
printuint(uint64(v))
case float32:
printfloat(float64(v))
case float64:
printfloat(float64(v))
case complex64:
printstring("(")
printfloat(float64(real(v)))
printfloat(float64(imag(v)))
printstring("i)")
case complex128:
printstring("(")
printfloat(real(v))
printfloat(imag(v))
printstring("i)")
case string:
printstring(v)
}
}
func prinxor(n int64) {
printint(^n)
}
func prinsub(n int64) {
printint(-n)
}
func prinusub(n uint64) {
printuint(-n)
}
func prinfsub(n float64) {
printfloat(-n)
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
package main
import _ "unsafe"
//go:linkname printf C.printf
func printf(format *int8, __llgo_va_list ...any)
var hello = [...]int8{'H', 'e', 'l', 'l', 'o', '\n', 0}
func main() {
printf(&hello[0])
}

View File

@@ -0,0 +1,41 @@
; ModuleID = 'main'
source_filename = "main"
@main.hello = global [7 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store i8 72, ptr @main.hello, align 1
store i8 101, ptr getelementptr inbounds (i8, ptr @main.hello, i64 1), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.hello, i64 2), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.hello, i64 3), align 1
store i8 111, ptr getelementptr inbounds (i8, ptr @main.hello, i64 4), align 1
store i8 10, ptr getelementptr inbounds (i8, ptr @main.hello, i64 5), align 1
store i8 0, ptr getelementptr inbounds (i8, ptr @main.hello, i64 6), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void (ptr, ...) @printf(ptr @main.hello)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare void @printf(ptr, ...)

View File

@@ -0,0 +1,12 @@
package main
import _ "unsafe"
//go:linkname printf C.printf
func printf(format *int8, __llgo_va_list ...any)
var format = [...]int8{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0}
func main() {
printf(&format[0], 100)
}

View File

@@ -0,0 +1,44 @@
; ModuleID = 'main'
source_filename = "main"
@main.format = global [10 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store i8 72, ptr @main.format, align 1
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 3), align 1
store i8 111, ptr getelementptr inbounds (i8, ptr @main.format, i64 4), align 1
store i8 32, ptr getelementptr inbounds (i8, ptr @main.format, i64 5), align 1
store i8 37, ptr getelementptr inbounds (i8, ptr @main.format, i64 6), align 1
store i8 100, ptr getelementptr inbounds (i8, ptr @main.format, i64 7), align 1
store i8 10, ptr getelementptr inbounds (i8, ptr @main.format, i64 8), align 1
store i8 0, ptr getelementptr inbounds (i8, ptr @main.format, i64 9), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void (ptr, ...) @printf(ptr @main.format, i64 100)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare void @printf(ptr, ...)

View File

@@ -0,0 +1,19 @@
package main
import _ "unsafe"
//go:linkname printf C.printf
func printf(format *int8, __llgo_va_list ...any)
type T int8
func (f *T) Print(v int) {
printf((*int8)(f), v)
}
var format = [...]T{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0}
func main() {
f := &format[0]
f.Print(100)
}

View File

@@ -0,0 +1,50 @@
; ModuleID = 'main'
source_filename = "main"
@main.format = global [10 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @"main.(*T).Print"(ptr %0, i64 %1) {
_llgo_0:
call void (ptr, ...) @printf(ptr %0, i64 %1)
ret void
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store i8 72, ptr @main.format, align 1
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 3), align 1
store i8 111, ptr getelementptr inbounds (i8, ptr @main.format, i64 4), align 1
store i8 32, ptr getelementptr inbounds (i8, ptr @main.format, i64 5), align 1
store i8 37, ptr getelementptr inbounds (i8, ptr @main.format, i64 6), align 1
store i8 100, ptr getelementptr inbounds (i8, ptr @main.format, i64 7), align 1
store i8 10, ptr getelementptr inbounds (i8, ptr @main.format, i64 8), align 1
store i8 0, ptr getelementptr inbounds (i8, ptr @main.format, i64 9), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void @"main.(*T).Print"(ptr @main.format, i64 100)
ret i32 0
}
declare void @printf(ptr, ...)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -0,0 +1,13 @@
package main
import "github.com/goplus/llgo/c"
func f(a c.Uint) c.Uint {
a++
return a
}
func main() {
var a c.Uint = 100
c.Printf(c.Str("Hello, %u\n"), f(a))
}

View File

@@ -0,0 +1,41 @@
; ModuleID = 'main'
source_filename = "main"
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [11 x i8] c"Hello, %u\0A\00", align 1
define i32 @main.f(i32 %0) {
_llgo_0:
%1 = add i32 %0, 1
ret i32 %1
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call i32 @main.f(i32 100)
%3 = call i32 (ptr, ...) @printf(ptr @0, i32 %2)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @printf(ptr, ...)

View File

@@ -0,0 +1,11 @@
package main
const c = 100
var a float64 = 1
func main() {
if c > 100 {
a = 0
}
}

View File

@@ -0,0 +1,39 @@
; ModuleID = 'main'
source_filename = "main"
@main.a = global double 0.000000e+00, align 8
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store double 1.000000e+00, ptr @main.a, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
br i1 false, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
store double 0.000000e+00, ptr @main.a, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -0,0 +1,23 @@
package main
import (
"unicode/utf8"
)
func main() {
var str = "中abcd"
for i := 0; i < len(str); {
r, n := utf8.DecodeRuneInString(str[i:])
i += n
println(r)
}
println(index(2) == 3)
}
var array = [...]uint8{
1, 2, 3, 4, 5, 6, 7, 8,
}
func index(n int8) uint8 {
return array[n]
}

View File

@@ -0,0 +1,92 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@main.array = global [8 x i8] zeroinitializer, align 1
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [7 x i8] c"\E4\B8\ADabcd", align 1
define i8 @main.index(i8 %0) {
_llgo_0:
%1 = sext i8 %0 to i64
%2 = icmp slt i64 %1, 0
%3 = icmp sge i64 %1, 8
%4 = or i1 %3, %2
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %4)
%5 = getelementptr inbounds i8, ptr @main.array, i64 %1
%6 = load i8, ptr %5, align 1
ret i8 %6
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @"unicode/utf8.init"()
store i8 1, ptr @main.array, align 1
store i8 2, ptr getelementptr inbounds (i8, ptr @main.array, i64 1), align 1
store i8 3, ptr getelementptr inbounds (i8, ptr @main.array, i64 2), align 1
store i8 4, ptr getelementptr inbounds (i8, ptr @main.array, i64 3), align 1
store i8 5, ptr getelementptr inbounds (i8, ptr @main.array, i64 4), align 1
store i8 6, ptr getelementptr inbounds (i8, ptr @main.array, i64 5), align 1
store i8 7, ptr getelementptr inbounds (i8, ptr @main.array, i64 6), align 1
store i8 8, ptr getelementptr inbounds (i8, ptr @main.array, i64 7), align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
br label %_llgo_1
_llgo_1: ; preds = %_llgo_2, %_llgo_0
%2 = phi i64 [ 0, %_llgo_0 ], [ %8, %_llgo_2 ]
%3 = icmp slt i64 %2, 7
br i1 %3, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1
%4 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 7 }, i64 %2, i64 7)
%5 = call { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String" %4)
%6 = extractvalue { i32, i64 } %5, 0
%7 = extractvalue { i32, i64 } %5, 1
%8 = add i64 %2, %7
%9 = sext i32 %6 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %9)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
br label %_llgo_1
_llgo_3: ; preds = %_llgo_1
%10 = call i8 @main.index(i8 2)
%11 = icmp eq i8 %10, 3
call void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1 %11)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
declare void @"unicode/utf8.init"()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64)
declare { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1)

View File

@@ -0,0 +1,13 @@
package main
import "github.com/goplus/llgo/c"
func test(a ...any) {
for _, v := range a {
c.Printf(c.Str("%d\n"), v.(int))
}
}
func main() {
test(1, 2, 3)
}

View File

@@ -0,0 +1,140 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr }
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@_llgo_int = linkonce global ptr null, align 8
@0 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@1 = private unnamed_addr constant [21 x i8] c"type assertion failed", align 1
@_llgo_string = linkonce global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @"main.init$after"()
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 48)
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 0
%4 = load ptr, ptr @_llgo_int, align 8
%5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0
%6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr inttoptr (i64 1 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %6, ptr %3, align 8
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 1
%8 = load ptr, ptr @_llgo_int, align 8
%9 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %8, 0
%10 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %9, ptr inttoptr (i64 2 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %10, ptr %7, align 8
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 2
%12 = load ptr, ptr @_llgo_int, align 8
%13 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %12, 0
%14 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %13, ptr inttoptr (i64 3 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %14, ptr %11, align 8
%15 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %2, 0
%16 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %15, i64 3, 1
%17 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %16, i64 3, 2
call void @main.test(%"github.com/goplus/llgo/internal/runtime.Slice" %17)
ret i32 0
}
define void @main.test(%"github.com/goplus/llgo/internal/runtime.Slice" %0) {
_llgo_0:
%1 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %0, 1
br label %_llgo_1
_llgo_1: ; preds = %_llgo_4, %_llgo_0
%2 = phi i64 [ -1, %_llgo_0 ], [ %3, %_llgo_4 ]
%3 = add i64 %2, 1
%4 = icmp slt i64 %3, %1
br i1 %4, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1
%5 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %0, 0
%6 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %0, 1
%7 = icmp slt i64 %3, 0
%8 = icmp sge i64 %3, %6
%9 = or i1 %8, %7
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %9)
%10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %5, i64 %3
%11 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %10, align 8
%12 = extractvalue %"github.com/goplus/llgo/internal/runtime.eface" %11, 0
%13 = load ptr, ptr @_llgo_int, align 8
%14 = icmp eq ptr %12, %13
br i1 %14, label %_llgo_4, label %_llgo_5
_llgo_3: ; preds = %_llgo_1
ret void
_llgo_4: ; preds = %_llgo_2
%15 = extractvalue %"github.com/goplus/llgo/internal/runtime.eface" %11, 1
%16 = ptrtoint ptr %15 to i64
%17 = call i32 (ptr, ...) @printf(ptr @0, i64 %16)
br label %_llgo_1
_llgo_5: ; preds = %_llgo_2
%18 = load ptr, ptr @_llgo_string, align 8
%19 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 21 }, ptr %19, align 8
%20 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %18, 0
%21 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %20, ptr %19, 1
call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %21)
unreachable
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.init$after"() {
_llgo_0:
%0 = load ptr, ptr @_llgo_int, align 8
%1 = icmp eq ptr %0, null
br i1 %1, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 34)
store ptr %2, ptr @_llgo_int, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
%3 = load ptr, ptr @_llgo_string, align 8
%4 = icmp eq ptr %3, null
br i1 %4, label %_llgo_3, label %_llgo_4
_llgo_3: ; preds = %_llgo_2
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24)
store ptr %5, ptr @_llgo_string, align 8
br label %_llgo_4
_llgo_4: ; preds = %_llgo_3, %_llgo_2
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface")
declare i32 @printf(ptr, ...)

View File

@@ -0,0 +1,8 @@
package main
var a = 100
func main() {
a++
_ = a
}

View File

@@ -0,0 +1,36 @@
; ModuleID = 'main'
source_filename = "main"
@main.a = global i64 0, align 8
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
store i64 100, ptr @main.a, align 4
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = load i64, ptr @main.a, align 4
%3 = add i64 %2, 1
store i64 %3, ptr @main.a, align 4
%4 = load i64, ptr @main.a, align 4
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -0,0 +1,16 @@
package foo
func f(s string) bool {
return len(s) > 2
}
func Loop() {
for i := 0; i < 3; i++ {
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
return
}
}
}

View File

@@ -0,0 +1,7 @@
0: always
6: cond
1: loop
2: loop
4: loop
3: cond
5: cond

View File

@@ -0,0 +1,7 @@
package foo
func Loop() {
for i := 0; i < 3; i++ {
println(i)
}
}

View File

@@ -0,0 +1,4 @@
0: always
1: loop
2: loop
3: always

View File

@@ -0,0 +1,20 @@
package main
func f(s string) bool {
return len(s) > 2
}
func main() {
defer func() {
println("hi")
}()
for i := 0; i < 3; i++ {
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
return
}
}
defer println("bye")
}

View File

@@ -0,0 +1,7 @@
0: always
1: cond
2: loop
3: loop
5: loop
4: cond
6: cond

View File

@@ -0,0 +1,18 @@
package main
func f(s string) bool {
return len(s) > 2
}
func main() {
defer func() {
println("hi")
}()
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
return
}
defer println("bye")
}

View File

@@ -0,0 +1,4 @@
0: always
1: cond
2: cond
3: cond

View File

@@ -0,0 +1,52 @@
package main
func f() float64 {
return 1.0
}
func main() {
var v = f()
const n = 7 // digits printed
var buf [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := 5.0
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float64(s)
v *= 10
}
}

View File

@@ -0,0 +1,18 @@
0: always
1: cond
3: cond
4: cond
5: cond
7: loop
6: loop
10: loop
8: loop
9: cond
11: loop
12: loop
13: cond
14: cond
2: cond
15: loop
16: loop
17: always

View File

@@ -0,0 +1,17 @@
package main
func f(s string) bool {
return len(s) > 2
}
func main() {
defer func() {
println("hi")
}()
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
}
defer println("bye")
}

View File

@@ -0,0 +1,5 @@
0: always
1: cond
2: cond
4: cond
3: always

View File

@@ -0,0 +1,17 @@
package main
func Foo(s string) int {
return len(s)
}
func Test() {
j := 0
for i := 0; i < 10000000; i++ {
j += Foo("hello")
}
println(j)
}
func main() {
Test()
}

View File

@@ -0,0 +1,66 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@"main.init$guard" = global i1 false, align 1
@0 = private unnamed_addr constant [5 x i8] c"hello", align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" %0) {
_llgo_0:
%1 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %0, 1
ret i64 %1
}
define void @main.Test() {
_llgo_0:
br label %_llgo_1
_llgo_1: ; preds = %_llgo_2, %_llgo_0
%0 = phi i64 [ 0, %_llgo_0 ], [ %4, %_llgo_2 ]
%1 = phi i64 [ 0, %_llgo_0 ], [ %5, %_llgo_2 ]
%2 = icmp slt i64 %1, 10000000
br i1 %2, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1
%3 = call i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 5 })
%4 = add i64 %0, %3
%5 = add i64 %1, 1
br label %_llgo_1
_llgo_3: ; preds = %_llgo_1
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %0)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void @main.Test()
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

View File

@@ -0,0 +1,55 @@
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
// C.CString example
cstr := C.CString("Hello, World!")
C.puts(cstr)
// C.CBytes example
bytes := []byte{65, 66, 67, 68} // ABCD
cbytes := C.CBytes(bytes)
// C.GoString example
gostr := C.GoString(cstr)
println("Converted back to Go string: ", gostr)
// C.GoStringN example (with length limit)
gostringN := C.GoStringN(cstr, 5) // only take first 5 characters
println("Length-limited string: ", gostringN)
// C.GoBytes example
gobytes := C.GoBytes(cbytes, 4) // 4 is the length
println("Converted back to Go byte slice: ", gobytes)
// C math library examples
x := 2.0
// Calculate square root
sqrtResult := C.sqrt(C.double(x))
fmt.Printf("sqrt(%v) = %v\n", x, float64(sqrtResult))
// Calculate sine
sinResult := C.sin(C.double(x))
fmt.Printf("sin(%v) = %v\n", x, float64(sinResult))
// Calculate cosine
cosResult := C.cos(C.double(x))
fmt.Printf("cos(%v) = %v\n", x, float64(cosResult))
// Calculate natural logarithm
logResult := C.log(C.double(x))
fmt.Printf("log(%v) = %v\n", x, float64(logResult))
C.free(unsafe.Pointer(cstr))
C.free(cbytes)
}

View File

@@ -0,0 +1,404 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr }
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
@"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1
@_cgo_62905c3ec377_Cfunc__Cmalloc = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_cos = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_free = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_log = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_puts = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_sin = external global i8, align 1
@_cgo_62905c3ec377_Cfunc_sqrt = external global i8, align 1
@main._cgo_62905c3ec377_Cfunc__Cmalloc = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_cos = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_free = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_log = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_puts = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_sin = global ptr null, align 8
@main._cgo_62905c3ec377_Cfunc_sqrt = global ptr null, align 8
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [13 x i8] c"Hello, World!", align 1
@1 = private unnamed_addr constant [29 x i8] c"Converted back to Go string: ", align 1
@2 = private unnamed_addr constant [23 x i8] c"Length-limited string: ", align 1
@3 = private unnamed_addr constant [33 x i8] c"Converted back to Go byte slice: ", align 1
@_llgo_float64 = linkonce global ptr null, align 8
@4 = private unnamed_addr constant [14 x i8] c"sqrt(%v) = %v\0A", align 1
@5 = private unnamed_addr constant [13 x i8] c"sin(%v) = %v\0A", align 1
@6 = private unnamed_addr constant [13 x i8] c"cos(%v) = %v\0A", align 1
@7 = private unnamed_addr constant [13 x i8] c"log(%v) = %v\0A", align 1
@_llgo_byte = linkonce global ptr null, align 8
@"[]_llgo_byte" = linkonce global ptr null, align 8
@_llgo_Pointer = linkonce global ptr null, align 8
define double @main._Cfunc_cos(double %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_cos, align 8
%3 = load ptr, ptr %2, align 8
%4 = call double %3(double %0)
ret double %4
}
define [0 x i8] @main._Cfunc_free(ptr %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_free, align 8
%3 = load ptr, ptr %2, align 8
%4 = call [0 x i8] %3(ptr %0)
ret [0 x i8] %4
}
define double @main._Cfunc_log(double %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_log, align 8
%3 = load ptr, ptr %2, align 8
%4 = call double %3(double %0)
ret double %4
}
define i32 @main._Cfunc_puts(ptr %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_puts, align 8
%3 = load ptr, ptr %2, align 8
%4 = call i32 %3(ptr %0)
ret i32 %4
}
define double @main._Cfunc_sin(double %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_sin, align 8
%3 = load ptr, ptr %2, align 8
%4 = call double %3(double %0)
ret double %4
}
define double @main._Cfunc_sqrt(double %0) {
_llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_sqrt, align 8
%3 = load ptr, ptr %2, align 8
%4 = call double %3(double %0)
ret double %4
}
define ptr @main._Cgo_ptr(ptr %0) {
_llgo_0:
ret ptr %0
}
declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface")
declare void @main._cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface")
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @syscall.init()
call void @fmt.init()
call void @"main.init$after"()
store ptr @_cgo_62905c3ec377_Cfunc_cos, ptr @main._cgo_62905c3ec377_Cfunc_cos, align 8
store ptr @_cgo_62905c3ec377_Cfunc_free, ptr @main._cgo_62905c3ec377_Cfunc_free, align 8
store ptr @_cgo_62905c3ec377_Cfunc_log, ptr @main._cgo_62905c3ec377_Cfunc_log, align 8
store ptr @_cgo_62905c3ec377_Cfunc_puts, ptr @main._cgo_62905c3ec377_Cfunc_puts, align 8
store ptr @_cgo_62905c3ec377_Cfunc_sin, ptr @main._cgo_62905c3ec377_Cfunc_sin, align 8
store ptr @_cgo_62905c3ec377_Cfunc_sqrt, ptr @main._cgo_62905c3ec377_Cfunc_sqrt, align 8
store ptr @_cgo_62905c3ec377_Cfunc__Cmalloc, ptr @main._cgo_62905c3ec377_Cfunc__Cmalloc, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.CString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 13 })
store ptr %3, ptr %2, align 8
%4 = load ptr, ptr %2, align 8
%5 = call i32 @main._Cfunc_puts(ptr %4)
%6 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 24)
%7 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 4)
%8 = getelementptr inbounds i8, ptr %7, i64 0
store i8 65, ptr %8, align 1
%9 = getelementptr inbounds i8, ptr %7, i64 1
store i8 66, ptr %9, align 1
%10 = getelementptr inbounds i8, ptr %7, i64 2
store i8 67, ptr %10, align 1
%11 = getelementptr inbounds i8, ptr %7, i64 3
store i8 68, ptr %11, align 1
%12 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %7, 0
%13 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %12, i64 4, 1
%14 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %13, i64 4, 2
store %"github.com/goplus/llgo/internal/runtime.Slice" %14, ptr %6, align 8
%15 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%16 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%17 = getelementptr inbounds { ptr }, ptr %16, i32 0, i32 0
store ptr %6, ptr %17, align 8
%18 = insertvalue { ptr, ptr } { ptr @"main.main$1", ptr undef }, ptr %16, 1
%19 = extractvalue { ptr, ptr } %18, 1
%20 = extractvalue { ptr, ptr } %18, 0
%21 = call ptr %20(ptr %19)
store ptr %21, ptr %15, align 8
%22 = load ptr, ptr %2, align 8
%23 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoString"(ptr %22)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 29 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %23)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%24 = load ptr, ptr %2, align 8
%25 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoStringN"(ptr %24, i32 5)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 23 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %25)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%26 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%27 = getelementptr inbounds { ptr }, ptr %26, i32 0, i32 0
store ptr %15, ptr %27, align 8
%28 = insertvalue { ptr, ptr } { ptr @"main.main$2", ptr undef }, ptr %26, 1
%29 = extractvalue { ptr, ptr } %28, 1
%30 = extractvalue { ptr, ptr } %28, 0
%31 = call %"github.com/goplus/llgo/internal/runtime.Slice" %30(ptr %29)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @3, i64 33 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice" %31)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%32 = call double @main._Cfunc_sqrt(double 2.000000e+00)
%33 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
%34 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %33, i64 0
%35 = load ptr, ptr @_llgo_float64, align 8
%36 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %35, 0
%37 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %36, ptr inttoptr (i64 4611686018427387904 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %37, ptr %34, align 8
%38 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %33, i64 1
%39 = load ptr, ptr @_llgo_float64, align 8
%40 = bitcast double %32 to i64
%41 = inttoptr i64 %40 to ptr
%42 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %39, 0
%43 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %42, ptr %41, 1
store %"github.com/goplus/llgo/internal/runtime.eface" %43, ptr %38, align 8
%44 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %33, 0
%45 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %44, i64 2, 1
%46 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %45, i64 2, 2
%47 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @4, i64 14 }, %"github.com/goplus/llgo/internal/runtime.Slice" %46)
%48 = call double @main._Cfunc_sin(double 2.000000e+00)
%49 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
%50 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %49, i64 0
%51 = load ptr, ptr @_llgo_float64, align 8
%52 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %51, 0
%53 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %52, ptr inttoptr (i64 4611686018427387904 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %53, ptr %50, align 8
%54 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %49, i64 1
%55 = load ptr, ptr @_llgo_float64, align 8
%56 = bitcast double %48 to i64
%57 = inttoptr i64 %56 to ptr
%58 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %55, 0
%59 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %58, ptr %57, 1
store %"github.com/goplus/llgo/internal/runtime.eface" %59, ptr %54, align 8
%60 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %49, 0
%61 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %60, i64 2, 1
%62 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %61, i64 2, 2
%63 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @5, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %62)
%64 = call double @main._Cfunc_cos(double 2.000000e+00)
%65 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
%66 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %65, i64 0
%67 = load ptr, ptr @_llgo_float64, align 8
%68 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %67, 0
%69 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %68, ptr inttoptr (i64 4611686018427387904 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %69, ptr %66, align 8
%70 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %65, i64 1
%71 = load ptr, ptr @_llgo_float64, align 8
%72 = bitcast double %64 to i64
%73 = inttoptr i64 %72 to ptr
%74 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %71, 0
%75 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %74, ptr %73, 1
store %"github.com/goplus/llgo/internal/runtime.eface" %75, ptr %70, align 8
%76 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %65, 0
%77 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %76, i64 2, 1
%78 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %77, i64 2, 2
%79 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @6, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %78)
%80 = call double @main._Cfunc_log(double 2.000000e+00)
%81 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
%82 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %81, i64 0
%83 = load ptr, ptr @_llgo_float64, align 8
%84 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %83, 0
%85 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %84, ptr inttoptr (i64 4611686018427387904 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %85, ptr %82, align 8
%86 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %81, i64 1
%87 = load ptr, ptr @_llgo_float64, align 8
%88 = bitcast double %80 to i64
%89 = inttoptr i64 %88 to ptr
%90 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %87, 0
%91 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %90, ptr %89, 1
store %"github.com/goplus/llgo/internal/runtime.eface" %91, ptr %86, align 8
%92 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %81, 0
%93 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %92, i64 2, 1
%94 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %93, i64 2, 2
%95 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @7, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %94)
%96 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%97 = getelementptr inbounds { ptr }, ptr %96, i32 0, i32 0
store ptr %2, ptr %97, align 8
%98 = insertvalue { ptr, ptr } { ptr @"main.main$3", ptr undef }, ptr %96, 1
%99 = extractvalue { ptr, ptr } %98, 1
%100 = extractvalue { ptr, ptr } %98, 0
call void %100(ptr %99)
%101 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%102 = getelementptr inbounds { ptr }, ptr %101, i32 0, i32 0
store ptr %15, ptr %102, align 8
%103 = insertvalue { ptr, ptr } { ptr @"main.main$4", ptr undef }, ptr %101, 1
%104 = extractvalue { ptr, ptr } %103, 1
%105 = extractvalue { ptr, ptr } %103, 0
call void %105(ptr %104)
ret i32 0
}
define ptr @"main.main$1"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %2, align 8
%4 = load ptr, ptr @_llgo_byte, align 8
%5 = load ptr, ptr @"[]_llgo_byte", align 8
%6 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 24)
store %"github.com/goplus/llgo/internal/runtime.Slice" %3, ptr %6, align 8
%7 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %5, 0
%8 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %7, ptr %6, 1
%9 = call ptr @"github.com/goplus/llgo/internal/runtime.CBytes"(%"github.com/goplus/llgo/internal/runtime.Slice" %3)
ret ptr %9
}
define %"github.com/goplus/llgo/internal/runtime.Slice" @"main.main$2"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load ptr, ptr %2, align 8
%4 = load ptr, ptr @_llgo_Pointer, align 8
%5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0
%6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1
%7 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.GoBytes"(ptr %3, i32 4)
ret %"github.com/goplus/llgo/internal/runtime.Slice" %7
}
define void @"main.main$3"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load ptr, ptr %2, align 8
%4 = load ptr, ptr @_llgo_Pointer, align 8
%5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0
%6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1
%7 = call [0 x i8] @main._Cfunc_free(ptr %3)
ret void
}
define void @"main.main$4"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load ptr, ptr %2, align 8
%4 = load ptr, ptr @_llgo_Pointer, align 8
%5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0
%6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1
%7 = call [0 x i8] @main._Cfunc_free(ptr %3)
ret void
}
declare void @runtime.throw(%"github.com/goplus/llgo/internal/runtime.String")
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @syscall.init()
declare void @fmt.init()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.CString"(%"github.com/goplus/llgo/internal/runtime.String")
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoString"(ptr)
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoStringN"(ptr, i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice")
define void @"main.init$after"() {
_llgo_0:
%0 = load ptr, ptr @_llgo_float64, align 8
%1 = icmp eq ptr %0, null
br i1 %1, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 46)
store ptr %2, ptr @_llgo_float64, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
%3 = load ptr, ptr @_llgo_byte, align 8
%4 = icmp eq ptr %3, null
br i1 %4, label %_llgo_3, label %_llgo_4
_llgo_3: ; preds = %_llgo_2
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 40)
store ptr %5, ptr @_llgo_byte, align 8
br label %_llgo_4
_llgo_4: ; preds = %_llgo_3, %_llgo_2
%6 = load ptr, ptr @"[]_llgo_byte", align 8
%7 = icmp eq ptr %6, null
br i1 %7, label %_llgo_5, label %_llgo_6
_llgo_5: ; preds = %_llgo_4
%8 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 40)
%9 = call ptr @"github.com/goplus/llgo/internal/runtime.SliceOf"(ptr %8)
store ptr %9, ptr @"[]_llgo_byte", align 8
br label %_llgo_6
_llgo_6: ; preds = %_llgo_5, %_llgo_4
%10 = load ptr, ptr @_llgo_Pointer, align 8
%11 = icmp eq ptr %10, null
br i1 %11, label %_llgo_7, label %_llgo_8
_llgo_7: ; preds = %_llgo_6
%12 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 58)
call void @"github.com/goplus/llgo/internal/runtime.SetDirectIface"(ptr %12)
store ptr %12, ptr @_llgo_Pointer, align 8
br label %_llgo_8
_llgo_8: ; preds = %_llgo_7, %_llgo_6
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64)
declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String", %"github.com/goplus/llgo/internal/runtime.Slice")
declare ptr @"github.com/goplus/llgo/internal/runtime.SliceOf"(ptr)
declare ptr @"github.com/goplus/llgo/internal/runtime.CBytes"(%"github.com/goplus/llgo/internal/runtime.Slice")
declare void @"github.com/goplus/llgo/internal/runtime.SetDirectIface"(ptr)
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.GoBytes"(ptr, i64)

View File

@@ -0,0 +1,15 @@
package main
/*
#include "in.h"
*/
import "C"
import "fmt"
func main() {
r := C.test_structs(&C.s4{a: 1}, &C.s8{a: 1, b: 2}, &C.s12{a: 1, b: 2, c: 3}, &C.s16{a: 1, b: 2, c: 3, d: 4}, &C.s20{a: 1, b: 2, c: 3, d: 4, e: 5})
fmt.Println(r)
if r != 35 {
panic("test_structs failed")
}
}

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include "in.h"
int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20) {
printf("s4.a: %d\n", s4->a);
printf("s8.a: %d, s8.b: %d\n", s8->a, s8->b);
printf("s12.a: %d, s12.b: %d, s12.c: %d\n", s12->a, s12->b, s12->c);
printf("s16.a: %d, s16.b: %d, s16.c: %d, s16.d: %d\n", s16->a, s16->b, s16->c, s16->d);
printf("s20.a: %d, s20.b: %d, s20.c: %d, s20.d: %d, s20.e: %d\n", s20->a, s20->b, s20->c, s20->d, s20->e);
return s4->a + s8->a + s8->b + s12->a + s12->b + s12->c + s16->a + s16->b + s16->c + s16->d + s20->a + s20->b + s20->c + s20->d + s20->e;
}

View File

@@ -0,0 +1,33 @@
#pragma once
typedef struct {
int a;
} s4;
typedef struct {
int a;
int b;
} s8;
typedef struct {
int a;
int b;
int c;
} s12;
typedef struct {
int a;
int b;
int c;
int d;
} s16;
typedef struct {
int a;
int b;
int c;
int d;
int e;
} s20;
extern int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20);

View File

@@ -0,0 +1,179 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr }
%main._Ctype_struct___3 = type { i32 }
%main._Ctype_struct___4 = type { i32, i32 }
%main._Ctype_struct___0 = type { i32, i32, i32 }
%main._Ctype_struct___1 = type { i32, i32, i32, i32 }
%main._Ctype_struct___2 = type { i32, i32, i32, i32, i32 }
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1
@_cgo_35faaf752e93_Cfunc_test_structs = external global i8, align 1
@main._cgo_35faaf752e93_Cfunc_test_structs = global ptr null, align 8
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@_llgo_main._Ctype_int = linkonce global ptr null, align 8
@0 = private unnamed_addr constant [4 x i8] c"main", align 1
@1 = private unnamed_addr constant [10 x i8] c"_Ctype_int", align 1
@_llgo_int32 = linkonce global ptr null, align 8
@2 = private unnamed_addr constant [19 x i8] c"test_structs failed", align 1
@_llgo_string = linkonce global ptr null, align 8
define i32 @main._Cfunc_test_structs(ptr %0, ptr %1, ptr %2, ptr %3, ptr %4) {
_llgo_0:
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%6 = load ptr, ptr @main._cgo_35faaf752e93_Cfunc_test_structs, align 8
%7 = load ptr, ptr %6, align 8
%8 = call i32 %7(ptr %0, ptr %1, ptr %2, ptr %3, ptr %4)
ret i32 %8
}
define ptr @main._Cgo_ptr(ptr %0) {
_llgo_0:
ret ptr %0
}
declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface")
declare void @main._cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface")
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @syscall.init()
call void @fmt.init()
call void @"main.init$after"()
store ptr @_cgo_35faaf752e93_Cfunc_test_structs, ptr @main._cgo_35faaf752e93_Cfunc_test_structs, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 4)
%3 = getelementptr inbounds %main._Ctype_struct___3, ptr %2, i32 0, i32 0
store i32 1, ptr %3, align 4
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
%5 = getelementptr inbounds %main._Ctype_struct___4, ptr %4, i32 0, i32 0
%6 = getelementptr inbounds %main._Ctype_struct___4, ptr %4, i32 0, i32 1
store i32 1, ptr %5, align 4
store i32 2, ptr %6, align 4
%7 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 12)
%8 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 0
%9 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 1
%10 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 2
store i32 1, ptr %8, align 4
store i32 2, ptr %9, align 4
store i32 3, ptr %10, align 4
%11 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
%12 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 0
%13 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 1
%14 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 2
%15 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 3
store i32 1, ptr %12, align 4
store i32 2, ptr %13, align 4
store i32 3, ptr %14, align 4
store i32 4, ptr %15, align 4
%16 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 20)
%17 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 0
%18 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 1
%19 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 2
%20 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 3
%21 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 4
store i32 1, ptr %17, align 4
store i32 2, ptr %18, align 4
store i32 3, ptr %19, align 4
store i32 4, ptr %20, align 4
store i32 5, ptr %21, align 4
%22 = call i32 @main._Cfunc_test_structs(ptr %2, ptr %4, ptr %7, ptr %11, ptr %16)
%23 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
%24 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %23, i64 0
%25 = load ptr, ptr @_llgo_main._Ctype_int, align 8
%26 = sext i32 %22 to i64
%27 = inttoptr i64 %26 to ptr
%28 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %25, 0
%29 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %28, ptr %27, 1
store %"github.com/goplus/llgo/internal/runtime.eface" %29, ptr %24, align 8
%30 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %23, 0
%31 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %30, i64 1, 1
%32 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %31, i64 1, 2
%33 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice" %32)
%34 = icmp ne i32 %22, 35
br i1 %34, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%35 = load ptr, ptr @_llgo_string, align 8
%36 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 19 }, ptr %36, align 8
%37 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %35, 0
%38 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %37, ptr %36, 1
call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %38)
unreachable
_llgo_2: ; preds = %_llgo_0
ret i32 0
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @syscall.init()
declare void @fmt.init()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
define void @"main.init$after"() {
_llgo_0:
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.NewNamed"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 4 }, %"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 10 }, i64 5, i64 4, i64 0, i64 0)
store ptr %0, ptr @_llgo_main._Ctype_int, align 8
%1 = load ptr, ptr @_llgo_int32, align 8
%2 = icmp eq ptr %1, null
br i1 %2, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 37)
store ptr %3, ptr @_llgo_int32, align 8
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
%4 = load ptr, ptr @_llgo_int32, align 8
call void @"github.com/goplus/llgo/internal/runtime.InitNamed"(ptr %0, ptr %4, { ptr, i64, i64 } zeroinitializer, { ptr, i64, i64 } zeroinitializer)
%5 = load ptr, ptr @_llgo_string, align 8
%6 = icmp eq ptr %5, null
br i1 %6, label %_llgo_3, label %_llgo_4
_llgo_3: ; preds = %_llgo_2
%7 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24)
store ptr %7, ptr @_llgo_string, align 8
br label %_llgo_4
_llgo_4: ; preds = %_llgo_3, %_llgo_2
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.NewNamed"(%"github.com/goplus/llgo/internal/runtime.String", %"github.com/goplus/llgo/internal/runtime.String", i64, i64, i64, i64)
declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.InitNamed"(ptr, ptr, %"github.com/goplus/llgo/internal/runtime.Slice", %"github.com/goplus/llgo/internal/runtime.Slice")
declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice")
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface")

View File

@@ -0,0 +1,11 @@
package main
/*
#include <stdlib.h>
*/
import "C"
func main() {
p := C.malloc(1024)
defer C.free(p)
}

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,16 @@
package main
/*
#cgo CFLAGS: -DBAR
#include <stdio.h>
#include "foo.h"
static void foo(Foo* f) {
printf("foo in bar: %d\n", f->a);
}
*/
import "C"
func Bar(f *C.Foo) {
C.print_foo(f)
C.foo(f)
}

View File

@@ -0,0 +1,157 @@
package main
/*
#cgo windows,!amd64 CFLAGS: -D_WIN32
#cgo !windows CFLAGS: -D_POSIX
#cgo windows,amd64 CFLAGS: -D_WIN64
#cgo linux,amd64 CFLAGS: -D_LINUX64
#cgo !windows,amd64 CFLAGS: -D_UNIX64
#cgo pkg-config: python3-embed
#include <stdio.h>
#include <Python.h>
#include "foo.h"
typedef struct {
int a;
} s4;
typedef struct {
int a;
int b;
} s8;
typedef struct {
int a;
int b;
int c;
} s12;
typedef struct {
int a;
int b;
int c;
int d;
} s16;
typedef struct {
int a;
int b;
int c;
int d;
int e;
} s20;
static int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20) {
printf("s4.a: %d\n", s4->a);
printf("s8.a: %d, s8.b: %d\n", s8->a, s8->b);
printf("s12.a: %d, s12.b: %d, s12.c: %d\n", s12->a, s12->b, s12->c);
printf("s16.a: %d, s16.b: %d, s16.c: %d, s16.d: %d\n", s16->a, s16->b, s16->c, s16->d);
printf("s20.a: %d, s20.b: %d, s20.c: %d, s20.d: %d, s20.e: %d\n", s20->a, s20->b, s20->c, s20->d, s20->e);
return s4->a + s8->a + s8->b + s12->a + s12->b + s12->c + s16->a + s16->b + s16->c + s16->d + s20->a + s20->b + s20->c + s20->d + s20->e;
}
static void test_macros() {
#ifdef FOO
printf("FOO is defined\n");
#endif
#ifdef BAR
printf("BAR is defined\n");
#endif
#ifdef _WIN32
printf("WIN32 is defined\n");
#endif
#ifdef _POSIX
printf("POSIX is defined\n");
#endif
#ifdef _WIN64
printf("WIN64 is defined\n");
#endif
#ifdef _LINUX64
printf("LINUX64 is defined\n");
#endif
#ifdef _UNIX64
printf("UNIX64 is defined\n");
#endif
}
#define MY_VERSION "1.0.0"
#define MY_CODE 0x12345678
static void test_void() {
printf("test_void\n");
}
typedef int (*Cb)(int);
extern int go_callback(int);
extern int c_callback(int i);
static void test_callback(Cb cb) {
printf("test_callback, cb: %p, go_callback: %p, c_callback: %p\n", cb, go_callback, c_callback);
printf("test_callback, *cb: %p, *go_callback: %p, *c_callback: %p\n", *(void**)cb, *(void**)(go_callback), *(void**)(c_callback));
printf("cb result: %d\n", cb(123));
printf("done\n");
}
extern int go_callback_not_use_in_go(int);
static void run_callback() {
test_callback(c_callback);
test_callback(go_callback_not_use_in_go);
}
*/
import "C"
import (
"fmt"
"unsafe"
"github.com/goplus/llgo/compiler/cl/_testgo/cgofull/pymod1"
"github.com/goplus/llgo/compiler/cl/_testgo/cgofull/pymod2"
)
//export go_callback_not_use_in_go
func go_callback_not_use_in_go(i C.int) C.int {
return i + 1
}
//export go_callback
func go_callback(i C.int) C.int {
return i + 1
}
func main() {
runPy()
f := &C.Foo{a: 1}
Foo(f)
Bar(f)
C.test_macros()
r := C.test_structs(&C.s4{a: 1}, &C.s8{a: 1, b: 2}, &C.s12{a: 1, b: 2, c: 3}, &C.s16{a: 1, b: 2, c: 3, d: 4}, &C.s20{a: 1, b: 2, c: 3, d: 4, e: 5})
fmt.Println(r)
if r != 35 {
panic("test_structs failed")
}
fmt.Println(C.MY_VERSION)
fmt.Println(int(C.MY_CODE))
C.test_void()
println("call run_callback")
C.run_callback()
// test _Cgo_ptr and _cgoCheckResult
println("call with go_callback")
C.test_callback((C.Cb)(C.go_callback))
println("call with c_callback")
C.test_callback((C.Cb)(C.c_callback))
}
func runPy() {
Initialize()
defer Finalize()
Run("print('Hello, Python!')")
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0)
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0)
// test _Cgo_use
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(C.PyComplex_FromDoubles(C.double(1.23), C.double(4.56)))), C.stdout, 0)
}

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include "foo.h"
void print_foo(Foo *f)
{
printf("print_foo: %d\n", f->a);
}
int c_callback(int i)
{
return i + 1;
}

View File

@@ -0,0 +1,16 @@
package main
/*
#cgo CFLAGS: -DFOO
#include <stdio.h>
#include "foo.h"
static void foo(Foo* f) {
printf("foo in bar: %d\n", f->a);
}
*/
import "C"
func Foo(f *C.Foo) {
C.print_foo(f)
C.foo(f)
}

View File

@@ -0,0 +1,7 @@
#pragma once
typedef struct {
int a;
} Foo;
extern void print_foo(Foo* f);

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,24 @@
package main
/*
#cgo pkg-config: python3-embed
#include <Python.h>
*/
import "C"
import "fmt"
func Initialize() {
C.Py_Initialize()
}
func Finalize() {
C.Py_Finalize()
}
func Run(code string) error {
if C.PyRun_SimpleString(C.CString(code)) != 0 {
C.PyErr_Print()
return fmt.Errorf("failed to run code")
}
return nil
}

View File

@@ -0,0 +1,11 @@
package pymod1
/*
#cgo pkg-config: python3-embed
#include <Python.h>
*/
import "C"
func Float(f float64) *C.PyObject {
return C.PyFloat_FromDouble(C.double(f))
}

View File

@@ -0,0 +1,11 @@
package pymod2
/*
#cgo pkg-config: python3-embed
#include <Python.h>
*/
import "C"
func Long(l int64) *C.PyObject {
return C.PyLong_FromLongLong(C.longlong(l))
}

View File

@@ -0,0 +1,31 @@
package main
/*
#cgo pkg-config: python3-embed
#include <stdio.h>
#include <Python.h>
void test_stdout() {
printf("stdout ptr: %p\n", stdout);
fputs("outputs to stdout\n", stdout);
}
*/
import "C"
import (
"unsafe"
"github.com/goplus/llgo/c"
)
func main() {
C.test_stdout()
C.fputs((*C.char)(unsafe.Pointer(c.Str("hello\n"))), C.stdout)
C.Py_Initialize()
defer C.Py_Finalize()
C.PyObject_Print(C.Py_True, C.stdout, 0)
C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout)
C.PyObject_Print(C.Py_False, C.stdout, 0)
C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout)
C.PyObject_Print(C.Py_None, C.stdout, 0)
C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout)
}

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,13 @@
package main
/*
#cgo pkg-config: python3-embed
#include <Python.h>
*/
import "C"
func main() {
C.Py_Initialize()
defer C.Py_Finalize()
C.PyRun_SimpleString(C.CString("print('Hello, Python!')"))
}

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,19 @@
package main
func main() {
ch := make(chan int, 10)
var v any = ch
println(ch, len(ch), cap(ch), v)
go func() {
ch <- 100
}()
n := <-ch
println(n)
ch2 := make(chan int, 10)
go func() {
close(ch2)
}()
n2, ok := <-ch2
println(n2, ok)
}

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,15 @@
package main
type T func(n int)
func main() {
var env string = "env"
var v1 T = func(i int) {
println("func", i)
}
var v2 T = func(i int) {
println("closure", i, env)
}
v1(100)
v2(200)
}

View File

@@ -0,0 +1,88 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
%main.T = type { ptr, ptr }
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [3 x i8] c"env", align 1
@1 = private unnamed_addr constant [4 x i8] c"func", align 1
@2 = private unnamed_addr constant [7 x i8] c"closure", align 1
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 3 }, ptr %2, align 8
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%4 = getelementptr inbounds { ptr }, ptr %3, i32 0, i32 0
store ptr %2, ptr %4, align 8
%5 = insertvalue { ptr, ptr } { ptr @"main.main$2", ptr undef }, ptr %3, 1
%6 = alloca %main.T, align 8
store { ptr, ptr } %5, ptr %6, align 8
%7 = load %main.T, ptr %6, align 8
call void @"__llgo_stub.main.main$1"(ptr null, i64 100)
%8 = extractvalue %main.T %7, 1
%9 = extractvalue %main.T %7, 0
call void %9(ptr %8, i64 200)
ret i32 0
}
define void @"main.main$1"(i64 %0) {
_llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 4 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %0)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"main.main$2"(ptr %0, i64 %1) {
_llgo_0:
%2 = load { ptr }, ptr %0, align 8
%3 = extractvalue { ptr } %2, 0
%4 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %3, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 7 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %4)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define linkonce void @"__llgo_stub.main.main$1"(ptr %0, i64 %1) {
_llgo_0:
tail call void @"main.main$1"(i64 %1)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)

View File

@@ -0,0 +1,11 @@
package main
func main() {
x := 1
f := func(i int) func(int) {
return func(i int) {
println("closure", i, x)
}
}
f(1)(2)
}

View File

@@ -0,0 +1,80 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [7 x i8] c"closure", align 1
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
store i64 1, ptr %2, align 4
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%4 = getelementptr inbounds { ptr }, ptr %3, i32 0, i32 0
store ptr %2, ptr %4, align 8
%5 = insertvalue { ptr, ptr } { ptr @"main.main$1", ptr undef }, ptr %3, 1
%6 = extractvalue { ptr, ptr } %5, 1
%7 = extractvalue { ptr, ptr } %5, 0
%8 = call { ptr, ptr } %7(ptr %6, i64 1)
%9 = extractvalue { ptr, ptr } %8, 1
%10 = extractvalue { ptr, ptr } %8, 0
call void %10(ptr %9, i64 2)
ret i32 0
}
define { ptr, ptr } @"main.main$1"(ptr %0, i64 %1) {
_llgo_0:
%2 = load { ptr }, ptr %0, align 8
%3 = extractvalue { ptr } %2, 0
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%5 = getelementptr inbounds { ptr }, ptr %4, i32 0, i32 0
store ptr %3, ptr %5, align 8
%6 = insertvalue { ptr, ptr } { ptr @"main.main$1$1", ptr undef }, ptr %4, 1
ret { ptr, ptr } %6
}
define void @"main.main$1$1"(ptr %0, i64 %1) {
_llgo_0:
%2 = load { ptr }, ptr %0, align 8
%3 = extractvalue { ptr } %2, 0
%4 = load i64, ptr %3, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 7 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %4)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)

View File

@@ -0,0 +1,9 @@
package main
func main() {
var i64 int64 = 1
var u64 uint64 = 1
var c int64 = i64 + (1.0 / (1.0 / 10))
var d uint64 = u64 + (1.0 / (1.0 / 10))
println(u64, i64, c, d)
}

View File

@@ -0,0 +1,44 @@
; ModuleID = 'main'
source_filename = "main"
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 11)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 11)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)

View File

@@ -0,0 +1,18 @@
package main
func f(s string) bool {
return len(s) > 2
}
func main() {
defer func() {
println("hi")
}()
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
return
}
defer println("bye")
}

View File

@@ -0,0 +1 @@
;

View File

@@ -0,0 +1,15 @@
package main
func f(s string) bool {
return len(s) > 2
}
func main() {
if s := "hello"; f(s) {
defer println(s)
} else {
defer println("world")
return
}
defer println("bye")
}

View File

@@ -0,0 +1 @@
;

Some files were not shown because too many files have changed in this diff Show More