Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler,runtime: fix multiple definitions of a single function #352

Merged
merged 2 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,9 @@ func (c *Compiler) parseFunc(frame *Frame) {
if c.DumpSSA {
fmt.Printf("\nfunc %s:\n", frame.fn.Function)
}
if !frame.fn.LLVMFn.IsDeclaration() {
panic("function is already defined: " + frame.fn.LLVMFn.Name())
}
if !frame.fn.IsExported() {
frame.fn.LLVMFn.SetLinkage(llvm.InternalLinkage)
frame.fn.LLVMFn.SetUnnamedAddr(true)
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
if goroot == "" {
return errors.New("cannot locate $GOROOT, please set it manually")
}
tags := spec.BuildTags
major, minor := getGorootVersion(goroot)
if major != 1 {
if major == 0 {
return errors.New("could not read version from GOROOT: " + goroot)
}
return fmt.Errorf("expected major version 1, got go%d.%d", major, minor)
}
for i := 1; i <= minor; i++ {
tags = append(tags, fmt.Sprintf("go1.%d", i))
}
compilerConfig := compiler.Config{
Triple: spec.Triple,
CPU: spec.CPU,
Expand All @@ -94,7 +105,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
TINYGOROOT: root,
GOROOT: goroot,
GOPATH: getGopath(),
BuildTags: spec.BuildTags,
BuildTags: tags,
}
c, err := compiler.NewCompiler(pkgName, compilerConfig)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ func decodeUTF8(s string, index uintptr) (rune, uintptr) {
}
}

// indexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
//go:linkname indexByte strings.IndexByte
func indexByte(s string, c byte) int {
// indexByteString returns the index of the first instance of c in s, or -1 if c
// is not present in s.
//go:linkname indexByteString internal/bytealg.IndexByteString
func indexByteString(s string, c byte) int {
for i := 0; i < len(s); i++ {
if s[i] == c {
return i
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/strings_go111.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !go1.12

package runtime

// indexByte provides compatibility with Go 1.11.
// See the following:
// https://github.com/tinygo-org/tinygo/issues/351
// https://github.com/golang/go/commit/ad4a58e31501bce5de2aad90a620eaecdc1eecb8
//go:linkname indexByte strings.IndexByte
func indexByte(s string, c byte) int {
return indexByteString(s, c)
}
25 changes: 25 additions & 0 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
)

Expand Down Expand Up @@ -368,3 +370,26 @@ func isGoroot(goroot string) bool {
_, err := os.Stat(filepath.Join(goroot, "src", "runtime", "internal", "sys", "zversion.go"))
return err == nil
}

// getGorootVersion returns the major and minor version for a given GOROOT path.
// If the goroot cannot be determined, (0, 0) is returned.
func getGorootVersion(goroot string) (major, minor int) {
data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
if err != nil {
return
}
s := string(data)
if s[:2] != "go" {
return
}
parts := strings.Split(s[2:], ".")
if len(parts) < 2 {
return
}

// Ignore the errors, strconv.Atoi will return 0 on most errors and we
// don't really handle errors here anyway.
major, _ = strconv.Atoi(parts[0])
minor, _ = strconv.Atoi(parts[1])
return
}
6 changes: 6 additions & 0 deletions testdata/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import (
"fmt"
"math/rand"
"os"
"strings"
)

func main() {
// package os, fmt
fmt.Println("stdin: ", os.Stdin.Fd())
fmt.Println("stdout:", os.Stdout.Fd())
fmt.Println("stderr:", os.Stderr.Fd())

// package math/rand
fmt.Println("pseudorandom number:", rand.Int31())

// package strings
fmt.Println("strings.IndexByte:", strings.IndexByte("asdf", 'd'))
}
1 change: 1 addition & 0 deletions testdata/stdlib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ stdin: 0
stdout: 1
stderr: 2
pseudorandom number: 1298498081
strings.IndexByte: 2