Skip to content

all: pretend to be linux/arm in baremetal targets #247

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

Merged
merged 1 commit into from
Mar 23, 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
37 changes: 32 additions & 5 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ func (c *Compiler) selectGC() string {
func (c *Compiler) Compile(mainPath string) error {
// Prefix the GOPATH with the system GOROOT, as GOROOT is already set to
// the TinyGo root.
gopath := c.GOPATH
if gopath == "" {
gopath = runtime.GOROOT()
overlayGopath := c.GOPATH
if overlayGopath == "" {
overlayGopath = runtime.GOROOT()
} else {
gopath = runtime.GOROOT() + string(filepath.ListSeparator) + gopath
overlayGopath = runtime.GOROOT() + string(filepath.ListSeparator) + overlayGopath
}

wd, err := os.Getwd()
Expand All @@ -175,15 +175,42 @@ func (c *Compiler) Compile(mainPath string) error {
}
lprogram := &loader.Program{
Build: &build.Context{
GOARCH: c.GOARCH,
GOOS: c.GOOS,
GOROOT: runtime.GOROOT(),
GOPATH: c.GOPATH,
CgoEnabled: true,
UseAllFiles: false,
Compiler: "gc", // must be one of the recognized compilers
BuildTags: append([]string{"tinygo", "gc." + c.selectGC()}, c.BuildTags...),
},
OverlayBuild: &build.Context{
GOARCH: c.GOARCH,
GOOS: c.GOOS,
GOROOT: c.RootDir,
GOPATH: gopath,
GOPATH: overlayGopath,
CgoEnabled: true,
UseAllFiles: false,
Compiler: "gc", // must be one of the recognized compilers
BuildTags: append([]string{"tinygo", "gc." + c.selectGC()}, c.BuildTags...),
},
ShouldOverlay: func(path string) bool {
switch path {
case "machine", "os", "reflect", "runtime", "sync":
return true
default:
if strings.HasPrefix(path, "device/") || strings.HasPrefix(path, "examples/") {
return true
} else if path == "syscall" {
for _, tag := range c.BuildTags {
if tag == "avr" || tag == "cortexm" {
return true
}
}
}
}
return false
},
TypeChecker: types.Config{
Sizes: &StdSizes{
IntSize: int64(c.targetData.TypeAllocSize(c.intType)),
Expand Down
22 changes: 14 additions & 8 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (

// Program holds all packages and some metadata about the program as a whole.
type Program struct {
Build *build.Context
Packages map[string]*Package
sorted []*Package
fset *token.FileSet
TypeChecker types.Config
Dir string // current working directory (for error reporting)
CFlags []string
Build *build.Context
OverlayBuild *build.Context
ShouldOverlay func(path string) bool
Packages map[string]*Package
sorted []*Package
fset *token.FileSet
TypeChecker types.Config
Dir string // current working directory (for error reporting)
CFlags []string
}

// Package holds a loaded package, its imports, and its parsed files.
Expand All @@ -42,7 +44,11 @@ func (p *Program) Import(path, srcDir string) (*Package, error) {
}

// Load this package.
buildPkg, err := p.Build.Import(path, srcDir, build.ImportComment)
ctx := p.Build
if p.ShouldOverlay(path) {
ctx = p.OverlayBuild
}
buildPkg, err := ctx.Import(path, srcDir, build.ImportComment)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion src/os/file_wasm.go → src/os/file_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build wasm
// +build avr cortexm wasm

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/file_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin linux
// +build darwin linux,!avr,!cortexm

package os

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/arch_arm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build arm,!avr,!cortexm

package runtime

const GOARCH = "arm"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build tinygo.arm
// +build cortexm

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/arch_wasm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build wasm,!tinygo.arm,!avr
// +build wasm

package runtime

Expand Down
28 changes: 0 additions & 28 deletions src/runtime/override_js.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build tinygo.arm
// +build cortexm

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin linux
// +build darwin linux,!avr,!cortexm

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build wasm,!tinygo.arm,!avr
// +build wasm

package runtime

Expand Down
24 changes: 24 additions & 0 deletions src/syscall/str.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2009 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.

package syscall

func itoa(val int) string { // do it here rather than with fmt to avoid dependency
if val < 0 {
return "-" + uitoa(uint(-val))
}
return uitoa(uint(val))
}

func uitoa(val uint) string {
var buf [32]byte // big enough for int64
i := len(buf) - 1
for val >= 10 {
buf[i] = byte(val%10 + '0')
i--
val /= 10
}
buf[i] = byte(val + '0')
return string(buf[i:])
}
148 changes: 148 additions & 0 deletions src/syscall/syscall_baremetal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package syscall

// Most code here has been copied from the Go sources:
// https://github.com/golang/go/blob/go1.12/src/syscall/syscall_js.go
// It has the following copyright note:
//
// Copyright 2018 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.

// An Errno is an unsigned number describing an error condition.
// It implements the error interface. The zero Errno is by convention
// a non-error, so code to convert from Errno to error should use:
// err = nil
// if errno != 0 {
// err = errno
// }
type Errno uintptr

func (e Errno) Error() string {
if 0 <= int(e) && int(e) < len(errorstr) {
s := errorstr[e]
if s != "" {
return s
}
}
return "errno " + itoa(int(e))
}

func (e Errno) Temporary() bool {
return e == EINTR || e == EMFILE || e.Timeout()
}

func (e Errno) Timeout() bool {
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
}

// A Signal is a number describing a process signal.
// It implements the os.Signal interface.
type Signal int

const (
_ Signal = iota
SIGCHLD
SIGINT
SIGKILL
SIGTRAP
SIGQUIT
SIGTERM
)

// File system

const (
Stdin = 0
Stdout = 1
Stderr = 2
)

const (
O_RDONLY = 0
O_WRONLY = 1
O_RDWR = 2

O_CREAT = 0100
O_CREATE = O_CREAT
O_TRUNC = 01000
O_APPEND = 02000
O_EXCL = 0200
O_SYNC = 010000

O_CLOEXEC = 0
)

func Getenv(key string) (value string, found bool) {
return "", false // stub
}

func Open(path string, mode int, perm uint32) (fd int, err error) {
return 0, ENOSYS
}

func Read(fd int, p []byte) (n int, err error) {
return 0, ENOSYS
}

func Seek(fd int, offset int64, whence int) (off int64, err error) {
return 0, ENOSYS
}

func Close(fd int) (err error) {
return ENOSYS
}

// Processes

type WaitStatus uint32

func (w WaitStatus) Exited() bool { return false }
func (w WaitStatus) ExitStatus() int { return 0 }
func (w WaitStatus) Signaled() bool { return false }
func (w WaitStatus) Signal() Signal { return 0 }
func (w WaitStatus) CoreDump() bool { return false }
func (w WaitStatus) Stopped() bool { return false }
func (w WaitStatus) Continued() bool { return false }
func (w WaitStatus) StopSignal() Signal { return 0 }
func (w WaitStatus) TrapCause() int { return 0 }

// XXX made up
type Rusage struct {
Utime Timeval
Stime Timeval
}

// XXX made up
type ProcAttr struct {
Dir string
Env []string
Files []uintptr
Sys *SysProcAttr
}

type SysProcAttr struct {
}

func Getegid() int { return 1 }
func Geteuid() int { return 1 }
func Getgid() int { return 1 }
func Getgroups() ([]int, error) { return []int{1}, nil }
func Getppid() int { return 2 }
func Getpid() int { return 3 }
func Gettimeofday(tv *Timeval) error { return ENOSYS }
func Getuid() int { return 1 }
func Kill(pid int, signum Signal) error { return ENOSYS }
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
return 0, ENOSYS
}
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
return 0, 0, ENOSYS
}
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
return 0, ENOSYS
}

type Timeval struct {
Sec int64
Usec int64
}
Loading