mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-01 01:27:46 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ecaec565aa |
@@ -172,11 +172,7 @@ commands:
|
||||
key: llvm-build-11-linux-v2-assert
|
||||
paths:
|
||||
llvm-build
|
||||
- run: |
|
||||
# Note: -p=2 limits parallelism to two jobs at a time, which is
|
||||
# necessary to keep memory consumption down and avoid OOM (for a
|
||||
# 2CPU/4GB executor).
|
||||
GOFLAGS="-p=2" make ASSERT=1
|
||||
- run: make ASSERT=1
|
||||
- build-wasi-libc
|
||||
- run:
|
||||
name: "Test TinyGo"
|
||||
|
||||
@@ -107,10 +107,7 @@ fmt-check:
|
||||
@unformatted=$$(gofmt -l $(FMT_PATHS)); [ -z "$$unformatted" ] && exit 0; echo "Unformatted:"; for fn in $$unformatted; do echo " $$fn"; done; exit 1
|
||||
|
||||
|
||||
gen-device: gen-device-avr gen-device-esp gen-device-nrf gen-device-sam gen-device-sifive gen-device-kendryte gen-device-nxp
|
||||
ifneq ($(STM32), 0)
|
||||
gen-device: gen-device-stm32
|
||||
endif
|
||||
gen-device: gen-device-avr gen-device-esp gen-device-nrf gen-device-sam gen-device-sifive gen-device-stm32 gen-device-kendryte gen-device-nxp
|
||||
|
||||
gen-device-avr:
|
||||
@if [ ! -e lib/avr/README.md ]; then echo "Submodules have not been downloaded. Please download them using:\n git submodule update --init"; exit 1; fi
|
||||
@@ -180,7 +177,7 @@ tinygo:
|
||||
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags byollvm -ldflags="-X main.gitSha1=`git rev-parse --short HEAD`" .
|
||||
|
||||
test: wasi-libc
|
||||
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -buildmode exe -tags byollvm ./builder ./cgo ./compileopts ./compiler ./interp ./transform .
|
||||
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -buildmode exe -tags byollvm ./cgo ./compileopts ./compiler ./interp ./transform .
|
||||
|
||||
# Test known-working standard library packages.
|
||||
# TODO: do this in one command, parallelize, and only show failing tests (no
|
||||
@@ -260,6 +257,8 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10031 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=bluepill examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=reelboard examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=reelboard examples/blinky2
|
||||
@@ -268,10 +267,6 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10056 examples/blinky2
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10059 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10059 examples/blinky2
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=itsybitsy-m0 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=feather-m0 examples/blinky1
|
||||
@@ -280,6 +275,14 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=circuitplay-express examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco examples/blinky2
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco-1 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=feather-stm32f405 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=circuitplay-bluefruit examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=circuitplay-express examples/i2s
|
||||
@@ -304,8 +307,12 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=particle-xenon examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-f103rb examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pinetime-devkit0 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=lgt92 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=x9pro examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10056-s140v7 examples/blinky1
|
||||
@@ -332,6 +339,10 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=teensy36 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-f722ze examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-l552ze examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=p1am-100 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
# test pwm
|
||||
@@ -343,28 +354,6 @@ smoketest:
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pyportal examples/pwm
|
||||
@$(MD5SUM) test.hex
|
||||
ifneq ($(STM32), 0)
|
||||
$(TINYGO) build -size short -o test.hex -target=bluepill examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=feather-stm32f405 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=lgt92 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-f103rb examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-f722ze examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-l432kc examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=nucleo-l552ze examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco examples/blinky2
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=stm32f4disco-1 examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
endif
|
||||
ifneq ($(AVR), 0)
|
||||
$(TINYGO) build -size short -o test.hex -target=atmega1284p examples/serial
|
||||
@$(MD5SUM) test.hex
|
||||
@@ -405,7 +394,6 @@ endif
|
||||
$(TINYGO) build -size short -o test.hex -target=pca10040 -opt=0 ./testdata/stdlib.go
|
||||
@$(MD5SUM) test.hex
|
||||
|
||||
|
||||
wasmtest:
|
||||
$(GO) test ./tests/wasm
|
||||
|
||||
|
||||
+46
-66
@@ -17,6 +17,7 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
@@ -54,7 +55,6 @@ type BuildResult struct {
|
||||
type packageAction struct {
|
||||
ImportPath string
|
||||
CompilerVersion int // compiler.Version
|
||||
InterpVersion int // interp.Version
|
||||
LLVMVersion string
|
||||
Config *compiler.Config
|
||||
CFlags []string
|
||||
@@ -135,7 +135,6 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
actionID := packageAction{
|
||||
ImportPath: pkg.ImportPath,
|
||||
CompilerVersion: compiler.Version,
|
||||
InterpVersion: interp.Version,
|
||||
LLVMVersion: llvm.Version,
|
||||
Config: compilerConfig,
|
||||
CFlags: pkg.CFlags,
|
||||
@@ -180,7 +179,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
// The package has not yet been compiled, so create a job to do so.
|
||||
job := &compileJob{
|
||||
description: "compile package " + pkg.ImportPath,
|
||||
run: func(*compileJob) error {
|
||||
run: func() error {
|
||||
// Compile AST to IR. The compiler.CompilePackage function will
|
||||
// build the SSA as needed.
|
||||
mod, errs := compiler.CompilePackage(pkg.ImportPath, pkg, program.Package(pkg.Pkg), machine, compilerConfig, config.DumpSSA())
|
||||
@@ -191,21 +190,6 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
return errors.New("verification error after compiling package " + pkg.ImportPath)
|
||||
}
|
||||
|
||||
// Try to interpret package initializers at compile time.
|
||||
// It may only be possible to do this partially, in which case
|
||||
// it is completed after all IR files are linked.
|
||||
pkgInit := mod.NamedFunction(pkg.Pkg.Path() + ".init")
|
||||
if pkgInit.IsNil() {
|
||||
panic("init not found for " + pkg.Pkg.Path())
|
||||
}
|
||||
err := interp.RunFunc(pkgInit, config.DumpSSA())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
|
||||
return errors.New("verification error after interpreting " + pkgInit.Name())
|
||||
}
|
||||
|
||||
// Serialize the LLVM module as a bitcode file.
|
||||
// Write to a temporary path that is renamed to the destination
|
||||
// file to avoid race conditions with other TinyGo invocatiosn
|
||||
@@ -249,7 +233,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
programJob := &compileJob{
|
||||
description: "link+optimize packages (LTO)",
|
||||
dependencies: packageJobs,
|
||||
run: func(*compileJob) error {
|
||||
run: func() error {
|
||||
// Load and link all the bitcode files. This does not yet optimize
|
||||
// anything, it only links the bitcode files together.
|
||||
ctx := llvm.NewContext()
|
||||
@@ -369,8 +353,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
outputObjectFileJob := &compileJob{
|
||||
description: "generate output file",
|
||||
dependencies: []*compileJob{programJob},
|
||||
result: objfile,
|
||||
run: func(*compileJob) error {
|
||||
run: func() error {
|
||||
llvmBuf, err := machine.EmitToMemoryBuffer(mod, llvm.ObjectFile)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -384,32 +367,40 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
linkerDependencies := []*compileJob{outputObjectFileJob}
|
||||
executable := filepath.Join(dir, "main")
|
||||
tmppath := executable // final file
|
||||
ldflags := append(config.LDFlags(), "-o", executable)
|
||||
ldflags := append(config.LDFlags(), "-o", executable, objfile)
|
||||
|
||||
// Add compiler-rt dependency if needed. Usually this is a simple load from
|
||||
// a cache.
|
||||
if config.Target.RTLib == "compiler-rt" {
|
||||
job, err := CompilerRT.load(config.Triple(), config.CPU(), dir)
|
||||
path, job, err := CompilerRT.load(config.Triple(), config.CPU(), dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jobs = append(jobs, job.dependencies...)
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
if job != nil {
|
||||
// The library was not loaded from cache so needs to be compiled
|
||||
// (and then stored in the cache).
|
||||
jobs = append(jobs, job.dependencies...)
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
}
|
||||
ldflags = append(ldflags, path)
|
||||
}
|
||||
|
||||
// Add libc dependency if needed.
|
||||
root := goenv.Get("TINYGOROOT")
|
||||
switch config.Target.Libc {
|
||||
case "picolibc":
|
||||
job, err := Picolibc.load(config.Triple(), config.CPU(), dir)
|
||||
path, job, err := Picolibc.load(config.Triple(), config.CPU(), dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The library needs to be compiled (cache miss).
|
||||
jobs = append(jobs, job.dependencies...)
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
if job != nil {
|
||||
// The library needs to be compiled (cache miss).
|
||||
jobs = append(jobs, job.dependencies...)
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
}
|
||||
ldflags = append(ldflags, path)
|
||||
case "wasi-libc":
|
||||
path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a")
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
@@ -425,37 +416,44 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
// Add jobs to compile extra files. These files are in C or assembly and
|
||||
// contain things like the interrupt vector table and low level operations
|
||||
// such as stack switching.
|
||||
for _, path := range config.ExtraFiles() {
|
||||
for i, path := range config.ExtraFiles() {
|
||||
abspath := filepath.Join(root, path)
|
||||
outpath := filepath.Join(dir, "extra-"+strconv.Itoa(i)+"-"+filepath.Base(path)+".o")
|
||||
job := &compileJob{
|
||||
description: "compile extra file " + path,
|
||||
run: func(job *compileJob) error {
|
||||
result, err := compileAndCacheCFile(abspath, dir, config.CFlags(), config)
|
||||
job.result = result
|
||||
return err
|
||||
run: func() error {
|
||||
err := runCCompiler(config.Target.Compiler, append(config.CFlags(), "-c", "-o", outpath, abspath)...)
|
||||
if err != nil {
|
||||
return &commandError{"failed to build", path, err}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
ldflags = append(ldflags, outpath)
|
||||
}
|
||||
|
||||
// Add jobs to compile C files in all packages. This is part of CGo.
|
||||
// TODO: do this as part of building the package to be able to link the
|
||||
// bitcode files together.
|
||||
for _, pkg := range lprogram.Sorted() {
|
||||
pkg := pkg
|
||||
for _, filename := range pkg.CFiles {
|
||||
abspath := filepath.Join(pkg.Dir, filename)
|
||||
for i, pkg := range lprogram.Sorted() {
|
||||
for j, filename := range pkg.CFiles {
|
||||
file := filepath.Join(pkg.Dir, filename)
|
||||
outpath := filepath.Join(dir, "pkg"+strconv.Itoa(i)+"."+strconv.Itoa(j)+"-"+filepath.Base(file)+".o")
|
||||
job := &compileJob{
|
||||
description: "compile CGo file " + abspath,
|
||||
run: func(job *compileJob) error {
|
||||
result, err := compileAndCacheCFile(abspath, dir, pkg.CFlags, config)
|
||||
job.result = result
|
||||
return err
|
||||
description: "compile CGo file " + file,
|
||||
run: func() error {
|
||||
err := runCCompiler(config.Target.Compiler, append(config.CFlags(), "-c", "-o", outpath, file)...)
|
||||
if err != nil {
|
||||
return &commandError{"failed to build", file, err}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
jobs = append(jobs, job)
|
||||
linkerDependencies = append(linkerDependencies, job)
|
||||
ldflags = append(ldflags, outpath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,16 +468,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
|
||||
jobs = append(jobs, &compileJob{
|
||||
description: "link",
|
||||
dependencies: linkerDependencies,
|
||||
run: func(job *compileJob) error {
|
||||
for _, dependency := range job.dependencies {
|
||||
if dependency.result == "" {
|
||||
return errors.New("dependency without result: " + dependency.description)
|
||||
}
|
||||
ldflags = append(ldflags, dependency.result)
|
||||
}
|
||||
if config.Options.PrintCommands {
|
||||
fmt.Printf("%s %s\n", config.Target.Linker, strings.Join(ldflags, " "))
|
||||
}
|
||||
run: func() error {
|
||||
err = link(config.Target.Linker, ldflags...)
|
||||
if err != nil {
|
||||
return &commandError{"failed to link", executable, err}
|
||||
@@ -586,17 +575,8 @@ func optimizeProgram(mod llvm.Module, config *compileopts.Config) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if config.VerifyIR() {
|
||||
// Only verify if we really need it.
|
||||
// The IR has already been verified before writing the bitcode to disk
|
||||
// and the interp function above doesn't need to do a lot as most of the
|
||||
// package initializers have already run. Additionally, verifying this
|
||||
// linked IR is _expensive_ because dead code hasn't been removed yet,
|
||||
// easily costing a few hundred milliseconds. Therefore, only do it when
|
||||
// specifically requested.
|
||||
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
|
||||
return errors.New("verification error after interpreting runtime.initAll")
|
||||
}
|
||||
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
|
||||
return errors.New("verification error after interpreting runtime.initAll")
|
||||
}
|
||||
|
||||
if config.GOOS() != "darwin" {
|
||||
|
||||
-305
@@ -1,305 +0,0 @@
|
||||
package builder
|
||||
|
||||
// This file implements a wrapper around the C compiler (Clang) which uses a
|
||||
// build cache.
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"github.com/tinygo-org/tinygo/goenv"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// compileAndCacheCFile compiles a C or assembly file using a build cache.
|
||||
// Compiling the same file again (if nothing changed, including included header
|
||||
// files) the output is loaded from the build cache instead.
|
||||
//
|
||||
// Its operation is a bit complex (more complex than Go package build caching)
|
||||
// because the list of file dependencies is only known after the file is
|
||||
// compiled. However, luckily compilers have a flag to write a list of file
|
||||
// dependencies in Makefile syntax which can be used for caching.
|
||||
//
|
||||
// Because of this complexity, every file has in fact two cached build outputs:
|
||||
// the file itself, and the list of dependencies. Its operation is as follows:
|
||||
//
|
||||
// depfile = hash(path, compiler, cflags, ...)
|
||||
// if depfile exists:
|
||||
// outfile = hash of all files and depfile name
|
||||
// if outfile exists:
|
||||
// # cache hit
|
||||
// return outfile
|
||||
// # cache miss
|
||||
// tmpfile = compile file
|
||||
// read dependencies (side effect of compile)
|
||||
// write depfile
|
||||
// outfile = hash of all files and depfile name
|
||||
// rename tmpfile to outfile
|
||||
//
|
||||
// There are a few edge cases that are not handled:
|
||||
// - If a file is added to an include path, that file may be included instead of
|
||||
// some other file. This would be fixed by also including lookup failures in the
|
||||
// dependencies file, but I'm not aware of a compiler which does that.
|
||||
// - The Makefile syntax that compilers output has issues, see readDepFile for
|
||||
// details.
|
||||
// - A header file may be changed to add/remove an include. This invalidates the
|
||||
// depfile but without invalidating its name. For this reason, the depfile is
|
||||
// written on each new compilation (even when it seems unnecessary). However, it
|
||||
// could in rare cases lead to a stale file fetched from the cache.
|
||||
func compileAndCacheCFile(abspath, tmpdir string, cflags []string, config *compileopts.Config) (string, error) {
|
||||
// Hash input file.
|
||||
fileHash, err := hashFile(abspath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create cache key for the dependencies file.
|
||||
buf, err := json.Marshal(struct {
|
||||
Path string
|
||||
Hash string
|
||||
Compiler string
|
||||
Flags []string
|
||||
LLVMVersion string
|
||||
}{
|
||||
Path: abspath,
|
||||
Hash: fileHash,
|
||||
Compiler: config.Target.Compiler,
|
||||
Flags: config.CFlags(),
|
||||
LLVMVersion: llvm.Version,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err) // shouldn't happen
|
||||
}
|
||||
depfileNameHashBuf := sha512.Sum512_224(buf)
|
||||
depfileNameHash := hex.EncodeToString(depfileNameHashBuf[:])
|
||||
|
||||
// Load dependencies file, if possible.
|
||||
depfileName := "dep-" + depfileNameHash + ".json"
|
||||
depfileCachePath := filepath.Join(goenv.Get("GOCACHE"), depfileName)
|
||||
depfileBuf, err := ioutil.ReadFile(depfileCachePath)
|
||||
var dependencies []string // sorted list of dependency paths
|
||||
if err == nil {
|
||||
// There is a dependency file, that's great!
|
||||
// Parse it first.
|
||||
err := json.Unmarshal(depfileBuf, &dependencies)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not parse dependencies JSON: %w", err)
|
||||
}
|
||||
|
||||
// Obtain hashes of all the files listed as a dependency.
|
||||
outpath, err := makeCFileCachePath(dependencies, depfileNameHash)
|
||||
if err == nil {
|
||||
if _, err := os.Stat(outpath); err == nil {
|
||||
return outpath, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
// expected either nil or IsNotExist
|
||||
return "", err
|
||||
}
|
||||
|
||||
objTmpFile, err := ioutil.TempFile(goenv.Get("GOCACHE"), "tmp-*.o")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
objTmpFile.Close()
|
||||
depTmpFile, err := ioutil.TempFile(tmpdir, "dep-*.d")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
depTmpFile.Close()
|
||||
flags := append([]string{}, cflags...) // copy cflags
|
||||
flags = append(flags, "-MD", "-MV", "-MTdeps", "-MF", depTmpFile.Name()) // autogenerate dependencies
|
||||
flags = append(flags, "-c", "-o", objTmpFile.Name(), abspath)
|
||||
if config.Options.PrintCommands {
|
||||
fmt.Printf("%s %s\n", config.Target.Compiler, strings.Join(flags, " "))
|
||||
}
|
||||
err = runCCompiler(config.Target.Compiler, flags...)
|
||||
if err != nil {
|
||||
return "", &commandError{"failed to build", abspath, err}
|
||||
}
|
||||
|
||||
// Create sorted and uniqued slice of dependencies.
|
||||
dependencyPaths, err := readDepFile(depTmpFile.Name())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dependencyPaths = append(dependencyPaths, abspath) // necessary for .s files
|
||||
dependencySet := make(map[string]struct{}, len(dependencyPaths))
|
||||
var dependencySlice []string
|
||||
for _, path := range dependencyPaths {
|
||||
if _, ok := dependencySet[path]; ok {
|
||||
continue
|
||||
}
|
||||
dependencySet[path] = struct{}{}
|
||||
dependencySlice = append(dependencySlice, path)
|
||||
}
|
||||
sort.Strings(dependencySlice)
|
||||
|
||||
// Write dependencies file.
|
||||
f, err := ioutil.TempFile(filepath.Dir(depfileCachePath), depfileName)
|
||||
buf, err = json.MarshalIndent(dependencySlice, "", "\t")
|
||||
if err != nil {
|
||||
panic(err) // shouldn't happen
|
||||
}
|
||||
_, err = f.Write(buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = os.Rename(f.Name(), depfileCachePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Move temporary object file to final location.
|
||||
outpath, err := makeCFileCachePath(dependencySlice, depfileNameHash)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = os.Rename(objTmpFile.Name(), outpath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return outpath, nil
|
||||
}
|
||||
|
||||
// Create a cache path (a path in GOCACHE) to store the output of a compiler
|
||||
// job. This path is based on the dep file name (which is a hash of metadata
|
||||
// including compiler flags) and the hash of all input files in the paths slice.
|
||||
func makeCFileCachePath(paths []string, depfileNameHash string) (string, error) {
|
||||
// Hash all input files.
|
||||
fileHashes := make(map[string]string, len(paths))
|
||||
for _, path := range paths {
|
||||
hash, err := hashFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fileHashes[path] = hash
|
||||
}
|
||||
|
||||
// Calculate a cache key based on the above hashes.
|
||||
buf, err := json.Marshal(struct {
|
||||
DepfileHash string
|
||||
FileHashes map[string]string
|
||||
}{
|
||||
DepfileHash: depfileNameHash,
|
||||
FileHashes: fileHashes,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err) // shouldn't happen
|
||||
}
|
||||
outFileNameBuf := sha512.Sum512_224(buf)
|
||||
cacheKey := hex.EncodeToString(outFileNameBuf[:])
|
||||
|
||||
outpath := filepath.Join(goenv.Get("GOCACHE"), "obj-"+cacheKey+".o")
|
||||
return outpath, nil
|
||||
}
|
||||
|
||||
// hashFile hashes the given file path and returns the hash as a hex string.
|
||||
func hashFile(path string) (string, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to hash file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
fileHasher := sha512.New512_224()
|
||||
_, err = io.Copy(fileHasher, f)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to hash file: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(fileHasher.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// readDepFile reads a dependency file in NMake (Visual Studio make) format. The
|
||||
// file is assumed to have a single target named deps.
|
||||
//
|
||||
// There are roughly three make syntax variants:
|
||||
// - BSD make, which doesn't support any escaping. This means that many special
|
||||
// characters are not supported in file names.
|
||||
// - GNU make, which supports escaping using a backslash but when it fails to
|
||||
// find a file it tries to fall back with the literal path name (to match BSD
|
||||
// make).
|
||||
// - NMake (Visual Studio) and Jom, which simply quote the string if there are
|
||||
// any weird characters.
|
||||
// Clang supports two variants: a format that's a compromise between BSD and GNU
|
||||
// make (and is buggy to match GCC which is equally buggy), and NMake/Jom, which
|
||||
// is at least somewhat sane. This last format isn't perfect either: it does not
|
||||
// correctly handle filenames with quote marks in them. Those are generally not
|
||||
// allowed on Windows, but of course can be used on POSIX like systems. Still,
|
||||
// it's the most sane of any of the formats so readDepFile will use that format.
|
||||
func readDepFile(filename string) ([]string, error) {
|
||||
buf, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(buf) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return parseDepFile(string(buf))
|
||||
}
|
||||
|
||||
func parseDepFile(s string) ([]string, error) {
|
||||
// This function makes no attempt at parsing anything other than Clang -MD
|
||||
// -MV output.
|
||||
|
||||
// For Windows: replace CRLF with LF to make the logic below simpler.
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
|
||||
// Collapse all lines ending in a backslash. These backslashes are really
|
||||
// just a way to continue a line without making very long lines.
|
||||
s = strings.ReplaceAll(s, "\\\n", " ")
|
||||
|
||||
// Only use the first line, which is expected to begin with "deps:".
|
||||
line := strings.SplitN(s, "\n", 2)[0]
|
||||
if !strings.HasPrefix(line, "deps:") {
|
||||
return nil, errors.New("readDepFile: expected 'deps:' prefix")
|
||||
}
|
||||
line = strings.TrimSpace(line[len("deps:"):])
|
||||
|
||||
var deps []string
|
||||
for line != "" {
|
||||
if line[0] == '"' {
|
||||
// File path is quoted. Path ends with double quote.
|
||||
// This does not handle double quotes in path names, which is a
|
||||
// problem on non-Windows systems.
|
||||
line = line[1:]
|
||||
end := strings.IndexByte(line, '"')
|
||||
if end < 0 {
|
||||
return nil, errors.New("readDepFile: path is incorrectly quoted")
|
||||
}
|
||||
dep := line[:end]
|
||||
line = strings.TrimSpace(line[end+1:])
|
||||
deps = append(deps, dep)
|
||||
} else {
|
||||
// File path is not quoted. Path ends in space or EOL.
|
||||
end := strings.IndexFunc(line, unicode.IsSpace)
|
||||
if end < 0 {
|
||||
// last dependency
|
||||
deps = append(deps, line)
|
||||
break
|
||||
}
|
||||
dep := line[:end]
|
||||
line = strings.TrimSpace(line[end:])
|
||||
deps = append(deps, dep)
|
||||
}
|
||||
}
|
||||
return deps, nil
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitDepFile(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
in string
|
||||
out []string
|
||||
}{
|
||||
{`deps: foo bar`, []string{"foo", "bar"}},
|
||||
{`deps: foo "bar"`, []string{"foo", "bar"}},
|
||||
{`deps: "foo" bar`, []string{"foo", "bar"}},
|
||||
{`deps: "foo bar"`, []string{"foo bar"}},
|
||||
{`deps: "foo bar" `, []string{"foo bar"}},
|
||||
{"deps: foo\nbar", []string{"foo"}},
|
||||
{"deps: foo \\\nbar", []string{"foo", "bar"}},
|
||||
{"deps: foo\\bar \\\nbaz", []string{"foo\\bar", "baz"}},
|
||||
{"deps: foo\\bar \\\r\n baz", []string{"foo\\bar", "baz"}}, // Windows uses CRLF line endings
|
||||
} {
|
||||
out, err := parseDepFile(tc.in)
|
||||
if err != nil {
|
||||
t.Errorf("test #%d failed: %v", i, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(out, tc.out) {
|
||||
t.Errorf("test #%d failed: expected %#v but got %#v", i, tc.out, out)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-17
@@ -27,23 +27,12 @@ const (
|
||||
type compileJob struct {
|
||||
description string // description, only used for logging
|
||||
dependencies []*compileJob
|
||||
result string // result (path)
|
||||
run func(*compileJob) (err error)
|
||||
run func() error
|
||||
state jobState
|
||||
err error // error if finished
|
||||
duration time.Duration // how long it took to run this job (only set after finishing)
|
||||
}
|
||||
|
||||
// dummyCompileJob returns a new *compileJob that produces an output without
|
||||
// doing anything. This can be useful where a *compileJob producing an output is
|
||||
// expected but nothing needs to be done, for example for a load from a cache.
|
||||
func dummyCompileJob(result string) *compileJob {
|
||||
return &compileJob{
|
||||
description: "<dummy>",
|
||||
result: result,
|
||||
}
|
||||
}
|
||||
|
||||
// readyToRun returns whether this job is ready to run: it is itself not yet
|
||||
// started and all dependencies are finished.
|
||||
func (job *compileJob) readyToRun() bool {
|
||||
@@ -161,11 +150,9 @@ func nextJob(jobs []*compileJob) *compileJob {
|
||||
func jobWorker(workerChan, doneChan chan *compileJob) {
|
||||
for job := range workerChan {
|
||||
start := time.Now()
|
||||
if job.run != nil {
|
||||
err := job.run(job)
|
||||
if err != nil {
|
||||
job.err = err
|
||||
}
|
||||
err := job.run()
|
||||
if err != nil {
|
||||
job.err = err
|
||||
}
|
||||
job.duration = time.Since(start)
|
||||
doneChan <- job
|
||||
|
||||
+17
-16
@@ -42,28 +42,30 @@ func (l *Library) sourcePaths(target string) []string {
|
||||
// The resulting file is stored in the provided tmpdir, which is expected to be
|
||||
// removed after the Load call.
|
||||
func (l *Library) Load(target, tmpdir string) (path string, err error) {
|
||||
job, err := l.load(target, "", tmpdir)
|
||||
path, job, err := l.load(target, "", tmpdir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
jobs := append([]*compileJob{job}, job.dependencies...)
|
||||
err = runJobs(jobs)
|
||||
return job.result, err
|
||||
if job != nil {
|
||||
jobs := append([]*compileJob{job}, job.dependencies...)
|
||||
err = runJobs(jobs)
|
||||
}
|
||||
return path, err
|
||||
}
|
||||
|
||||
// load returns a compile job to build this library file for the given target
|
||||
// and CPU. It may return a dummy compileJob if the library build is already
|
||||
// cached. The path is stored as job.result but is only valid if the job and
|
||||
// job.dependencies have been run.
|
||||
// load returns a path to the library file for the given target, loading it from
|
||||
// cache if possible. It will return a non-zero compiler job if the library
|
||||
// wasn't cached, this job (and its dependencies) must be run before the library
|
||||
// path is valid.
|
||||
// The provided tmpdir will be used to store intermediary files and possibly the
|
||||
// output archive file, it is expected to be removed after use.
|
||||
func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error) {
|
||||
func (l *Library) load(target, cpu, tmpdir string) (path string, job *compileJob, err error) {
|
||||
// Try to load a precompiled library.
|
||||
precompiledPath := filepath.Join(goenv.Get("TINYGOROOT"), "pkg", target, l.name+".a")
|
||||
if _, err := os.Stat(precompiledPath); err == nil {
|
||||
// Found a precompiled library for this OS/architecture. Return the path
|
||||
// directly.
|
||||
return dummyCompileJob(precompiledPath), nil
|
||||
return precompiledPath, nil, nil
|
||||
}
|
||||
|
||||
var outfile string
|
||||
@@ -76,7 +78,7 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error)
|
||||
// Try to fetch this library from the cache.
|
||||
if path, err := cacheLoad(outfile, l.sourcePaths(target)); path != "" || err != nil {
|
||||
// Cache hit.
|
||||
return dummyCompileJob(path), nil
|
||||
return path, nil, err
|
||||
}
|
||||
// Cache miss, build it now.
|
||||
|
||||
@@ -84,7 +86,7 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error)
|
||||
dir := filepath.Join(tmpdir, "build-lib-"+l.name)
|
||||
err = os.Mkdir(dir, 0777)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// Precalculate the flags to the compiler invocation.
|
||||
@@ -111,8 +113,7 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error)
|
||||
arpath := filepath.Join(dir, l.name+".a")
|
||||
job = &compileJob{
|
||||
description: "ar " + l.name + ".a",
|
||||
result: arpath,
|
||||
run: func(*compileJob) error {
|
||||
run: func() error {
|
||||
// Create an archive of all object files.
|
||||
err := makeArchive(arpath, objs)
|
||||
if err != nil {
|
||||
@@ -132,7 +133,7 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error)
|
||||
objs = append(objs, objpath)
|
||||
job.dependencies = append(job.dependencies, &compileJob{
|
||||
description: "compile " + srcpath,
|
||||
run: func(*compileJob) error {
|
||||
run: func() error {
|
||||
var compileArgs []string
|
||||
compileArgs = append(compileArgs, args...)
|
||||
compileArgs = append(compileArgs, "-o", objpath, srcpath)
|
||||
@@ -145,5 +146,5 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error)
|
||||
})
|
||||
}
|
||||
|
||||
return job, nil
|
||||
return arpath, job, nil
|
||||
}
|
||||
|
||||
+14
-17
@@ -41,8 +41,7 @@ type cgoPackage struct {
|
||||
elaboratedTypes map[string]*elaboratedTypeInfo
|
||||
enums map[string]enumInfo
|
||||
anonStructNum int
|
||||
cflags []string // CFlags from #cgo lines
|
||||
ldflags []string // LDFlags from #cgo lines
|
||||
ldflags []string
|
||||
visitedFiles map[string][]byte
|
||||
}
|
||||
|
||||
@@ -158,10 +157,10 @@ typedef unsigned long long _Cgo_ulonglong;
|
||||
// Process extracts `import "C"` statements from the AST, parses the comment
|
||||
// with libclang, and modifies the AST to use this information. It returns a
|
||||
// newly created *ast.File that should be added to the list of to-be-parsed
|
||||
// files, the CFLAGS and LDFLAGS found in #cgo lines, and a map of file hashes
|
||||
// of the accessed C header files. If there is one or more error, it returns
|
||||
// these in the []error slice but still modifies the AST.
|
||||
func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string) (*ast.File, []string, []string, map[string][]byte, []error) {
|
||||
// files, the LDFLAGS for this package, and a map of file hashes of the accessed
|
||||
// C header files. If there is one or more error, it returns these in the
|
||||
// []error slice but still modifies the AST.
|
||||
func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string) (*ast.File, []string, map[string][]byte, []error) {
|
||||
p := &cgoPackage{
|
||||
dir: dir,
|
||||
fset: fset,
|
||||
@@ -176,6 +175,11 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
visitedFiles: map[string][]byte{},
|
||||
}
|
||||
|
||||
// Disable _FORTIFY_SOURCE as it causes problems on macOS.
|
||||
// Note that it is only disabled for memcpy (etc) calls made from Go, which
|
||||
// have better alternatives anyway.
|
||||
cflags = append(cflags, "-D_FORTIFY_SOURCE=0")
|
||||
|
||||
// Add a new location for the following file.
|
||||
generatedTokenPos := p.fset.AddFile(dir+"/!cgo.go", -1, 0)
|
||||
generatedTokenPos.SetLines([]int{0})
|
||||
@@ -184,7 +188,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
// Find the absolute path for this package.
|
||||
packagePath, err := filepath.Abs(fset.File(files[0].Pos()).Name())
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, []error{
|
||||
return nil, nil, nil, []error{
|
||||
scanner.Error{
|
||||
Pos: fset.Position(files[0].Pos()),
|
||||
Msg: "cgo: cannot find absolute path: " + err.Error(), // TODO: wrap this error
|
||||
@@ -359,7 +363,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
continue
|
||||
}
|
||||
makePathsAbsolute(flags, packagePath)
|
||||
p.cflags = append(p.cflags, flags...)
|
||||
cflags = append(cflags, flags...)
|
||||
case "LDFLAGS":
|
||||
flags, err := shlex.Split(value)
|
||||
if err != nil {
|
||||
@@ -382,13 +386,6 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
}
|
||||
}
|
||||
|
||||
// Define CFlags that will be used while parsing the package.
|
||||
// Disable _FORTIFY_SOURCE as it causes problems on macOS.
|
||||
// Note that it is only disabled for memcpy (etc) calls made from Go, which
|
||||
// have better alternatives anyway.
|
||||
cflagsForCGo := append([]string{"-D_FORTIFY_SOURCE=0"}, cflags...)
|
||||
cflagsForCGo = append(cflagsForCGo, p.cflags...)
|
||||
|
||||
// Process all CGo imports.
|
||||
for _, genDecl := range statements {
|
||||
cgoComment := genDecl.Doc.Text()
|
||||
@@ -398,7 +395,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
pos = genDecl.Doc.Pos()
|
||||
}
|
||||
position := fset.PositionFor(pos, true)
|
||||
p.parseFragment(cgoComment+cgoTypes, cflagsForCGo, position.Filename, position.Line)
|
||||
p.parseFragment(cgoComment+cgoTypes, cflags, position.Filename, position.Line)
|
||||
}
|
||||
|
||||
// Declare functions found by libclang.
|
||||
@@ -433,7 +430,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
|
||||
// Print the newly generated in-memory AST, for debugging.
|
||||
//ast.Print(fset, p.generated)
|
||||
|
||||
return p.generated, p.cflags, p.ldflags, p.visitedFiles, p.errors
|
||||
return p.generated, p.ldflags, p.visitedFiles, p.errors
|
||||
}
|
||||
|
||||
// makePathsAbsolute converts some common path compiler flags (-I, -L) from
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ func TestCGo(t *testing.T) {
|
||||
}
|
||||
|
||||
// Process the AST with CGo.
|
||||
cgoAST, _, _, _, cgoErrors := Process([]*ast.File{f}, "testdata", fset, cflags)
|
||||
cgoAST, _, _, cgoErrors := Process([]*ast.File{f}, "testdata", fset, cflags)
|
||||
|
||||
// Check the AST for type errors.
|
||||
var typecheckErrors []error
|
||||
|
||||
+3
-24
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
@@ -43,7 +42,7 @@ type TargetSpec struct {
|
||||
ExtraFiles []string `json:"extra-files"`
|
||||
Emulator []string `json:"emulator" override:"copy"` // inherited Emulator must not be append
|
||||
FlashCommand string `json:"flash-command"`
|
||||
GDB []string `json:"gdb"`
|
||||
GDB string `json:"gdb"`
|
||||
PortReset string `json:"flash-1200-bps-reset"`
|
||||
FlashMethod string `json:"flash-method"`
|
||||
FlashVolume string `json:"msd-volume-name"`
|
||||
@@ -230,13 +229,7 @@ func LoadTarget(target string) (*TargetSpec, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// WindowsBuildNotSupportedErr is being thrown, when goos is windows and no target has been specified.
|
||||
var WindowsBuildNotSupportedErr = errors.New("Building Windows binaries is currently not supported. Try specifying a different target")
|
||||
|
||||
func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
|
||||
if goos == "windows" {
|
||||
return nil, WindowsBuildNotSupportedErr
|
||||
}
|
||||
// No target spec available. Use the default one, useful on most systems
|
||||
// with a regular OS.
|
||||
spec := TargetSpec{
|
||||
@@ -247,7 +240,7 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
|
||||
Compiler: "clang",
|
||||
Linker: "cc",
|
||||
CFlags: []string{"--target=" + triple},
|
||||
GDB: []string{"gdb"},
|
||||
GDB: "gdb",
|
||||
PortReset: "false",
|
||||
}
|
||||
if goos == "darwin" {
|
||||
@@ -260,7 +253,7 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
|
||||
}
|
||||
if goarch != runtime.GOARCH {
|
||||
// Some educated guesses as to how to invoke helper programs.
|
||||
spec.GDB = []string{"gdb-multiarch"}
|
||||
spec.GDB = "gdb-multiarch"
|
||||
if goarch == "arm" && goos == "linux" {
|
||||
spec.CFlags = append(spec.CFlags, "--sysroot=/usr/arm-linux-gnueabihf")
|
||||
spec.Linker = "arm-linux-gnueabihf-gcc"
|
||||
@@ -278,17 +271,3 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
|
||||
}
|
||||
return &spec, nil
|
||||
}
|
||||
|
||||
// LookupGDB looks up a gdb executable.
|
||||
func (spec *TargetSpec) LookupGDB() (string, error) {
|
||||
if len(spec.GDB) == 0 {
|
||||
return "", errors.New("gdb not configured in the target specification")
|
||||
}
|
||||
for _, d := range spec.GDB {
|
||||
_, err := exec.LookPath(d)
|
||||
if err == nil {
|
||||
return d, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("no gdb found configured in the target specification (" + strings.Join(spec.GDB, ", ") + ")")
|
||||
}
|
||||
|
||||
@@ -159,9 +159,6 @@ func (b *builder) createNilCheck(inst ssa.Value, ptr llvm.Value, blockPrefix str
|
||||
}
|
||||
|
||||
switch inst := inst.(type) {
|
||||
case *ssa.Alloc:
|
||||
// An alloc is never nil.
|
||||
return
|
||||
case *ssa.IndexAddr:
|
||||
// This pointer is the result of an index operation into a slice or
|
||||
// array. Such slices/arrays are already bounds checked so the pointer
|
||||
|
||||
+10
-22
@@ -58,10 +58,10 @@ func (b *builder) createCall(fn llvm.Value, args []llvm.Value, name string) llvm
|
||||
|
||||
// Expand an argument type to a list that can be used in a function call
|
||||
// parameter list.
|
||||
func (c *compilerContext) expandFormalParamType(t llvm.Type, name string, goType types.Type) []paramInfo {
|
||||
func expandFormalParamType(t llvm.Type, name string, goType types.Type) []paramInfo {
|
||||
switch t.TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
fieldInfos := c.flattenAggregateType(t, name, goType)
|
||||
fieldInfos := flattenAggregateType(t, name, goType)
|
||||
if len(fieldInfos) <= maxFieldsPerParam {
|
||||
return fieldInfos
|
||||
} else {
|
||||
@@ -105,7 +105,7 @@ func (b *builder) expandFormalParamOffsets(t llvm.Type) []uint64 {
|
||||
func (b *builder) expandFormalParam(v llvm.Value) []llvm.Value {
|
||||
switch v.Type().TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
fieldInfos := b.flattenAggregateType(v.Type(), "", nil)
|
||||
fieldInfos := flattenAggregateType(v.Type(), "", nil)
|
||||
if len(fieldInfos) <= maxFieldsPerParam {
|
||||
fields := b.flattenAggregate(v)
|
||||
if len(fields) != len(fieldInfos) {
|
||||
@@ -124,15 +124,12 @@ func (b *builder) expandFormalParam(v llvm.Value) []llvm.Value {
|
||||
|
||||
// Try to flatten a struct type to a list of types. Returns a 1-element slice
|
||||
// with the passed in type if this is not possible.
|
||||
func (c *compilerContext) flattenAggregateType(t llvm.Type, name string, goType types.Type) []paramInfo {
|
||||
func flattenAggregateType(t llvm.Type, name string, goType types.Type) []paramInfo {
|
||||
typeFlags := getTypeFlags(goType)
|
||||
switch t.TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
var paramInfos []paramInfo
|
||||
paramInfos := make([]paramInfo, 0, t.StructElementTypesCount())
|
||||
for i, subfield := range t.StructElementTypes() {
|
||||
if c.targetData.TypeAllocSize(subfield) == 0 {
|
||||
continue
|
||||
}
|
||||
suffix := strconv.Itoa(i)
|
||||
if goType != nil {
|
||||
// Try to come up with a good suffix for this struct field,
|
||||
@@ -155,7 +152,7 @@ func (c *compilerContext) flattenAggregateType(t llvm.Type, name string, goType
|
||||
suffix = []string{"context", "funcptr"}[i]
|
||||
}
|
||||
}
|
||||
subInfos := c.flattenAggregateType(subfield, name+"."+suffix, extractSubfield(goType, i))
|
||||
subInfos := flattenAggregateType(subfield, name+"."+suffix, extractSubfield(goType, i))
|
||||
for i := range subInfos {
|
||||
subInfos[i].flags |= typeFlags
|
||||
}
|
||||
@@ -221,11 +218,8 @@ func extractSubfield(t types.Type, field int) types.Type {
|
||||
func (c *compilerContext) flattenAggregateTypeOffsets(t llvm.Type) []uint64 {
|
||||
switch t.TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
var fields []uint64
|
||||
fields := make([]uint64, 0, t.StructElementTypesCount())
|
||||
for fieldIndex, field := range t.StructElementTypes() {
|
||||
if c.targetData.TypeAllocSize(field) == 0 {
|
||||
continue
|
||||
}
|
||||
suboffsets := c.flattenAggregateTypeOffsets(field)
|
||||
offset := c.targetData.ElementOffset(t, fieldIndex)
|
||||
for i := range suboffsets {
|
||||
@@ -244,11 +238,8 @@ func (c *compilerContext) flattenAggregateTypeOffsets(t llvm.Type) []uint64 {
|
||||
func (b *builder) flattenAggregate(v llvm.Value) []llvm.Value {
|
||||
switch v.Type().TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
var fields []llvm.Value
|
||||
for i, field := range v.Type().StructElementTypes() {
|
||||
if b.targetData.TypeAllocSize(field) == 0 {
|
||||
continue
|
||||
}
|
||||
fields := make([]llvm.Value, 0, v.Type().StructElementTypesCount())
|
||||
for i := range v.Type().StructElementTypes() {
|
||||
subfield := b.CreateExtractValue(v, i, "")
|
||||
subfields := b.flattenAggregate(subfield)
|
||||
fields = append(fields, subfields...)
|
||||
@@ -275,13 +266,10 @@ func (b *builder) collapseFormalParam(t llvm.Type, fields []llvm.Value) llvm.Val
|
||||
func (b *builder) collapseFormalParamInternal(t llvm.Type, fields []llvm.Value) (llvm.Value, []llvm.Value) {
|
||||
switch t.TypeKind() {
|
||||
case llvm.StructTypeKind:
|
||||
flattened := b.flattenAggregateType(t, "", nil)
|
||||
flattened := flattenAggregateType(t, "", nil)
|
||||
if len(flattened) <= maxFieldsPerParam {
|
||||
value := llvm.ConstNull(t)
|
||||
for i, subtyp := range t.StructElementTypes() {
|
||||
if b.targetData.TypeAllocSize(subtyp) == 0 {
|
||||
continue
|
||||
}
|
||||
structField, remaining := b.collapseFormalParamInternal(subtyp, fields)
|
||||
fields = remaining
|
||||
value = b.CreateInsertValue(value, structField, i, "")
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
// Version of the compiler pacakge. Must be incremented each time the compiler
|
||||
// package changes in a way that affects the generated LLVM module.
|
||||
// This version is independent of the TinyGo version number.
|
||||
const Version = 6 // last change: fix issue 1304
|
||||
const Version = 1
|
||||
|
||||
func init() {
|
||||
llvm.InitializeAllTargets()
|
||||
@@ -781,12 +781,6 @@ func (b *builder) createFunction() {
|
||||
b.llvmFn.SetVisibility(llvm.HiddenVisibility)
|
||||
b.llvmFn.SetUnnamedAddr(true)
|
||||
}
|
||||
if b.info.exported && strings.HasPrefix(b.Triple, "wasm") {
|
||||
// Set the exported name. This is necessary for WebAssembly because
|
||||
// otherwise the function is not exported.
|
||||
functionAttr := b.ctx.CreateStringAttribute("wasm-export-name", b.info.linkName)
|
||||
b.llvmFn.AddFunctionAttr(functionAttr)
|
||||
}
|
||||
|
||||
// Some functions have a pragma controlling the inlining level.
|
||||
switch b.info.inline {
|
||||
@@ -829,7 +823,7 @@ func (b *builder) createFunction() {
|
||||
for _, param := range b.fn.Params {
|
||||
llvmType := b.getLLVMType(param.Type())
|
||||
fields := make([]llvm.Value, 0, 1)
|
||||
for _, info := range b.expandFormalParamType(llvmType, param.Name(), param.Type()) {
|
||||
for _, info := range expandFormalParamType(llvmType, param.Name(), param.Type()) {
|
||||
param := b.llvmFn.Param(llvmParamIndex)
|
||||
param.SetName(info.name)
|
||||
fields = append(fields, param)
|
||||
|
||||
+27
-15
@@ -4,6 +4,7 @@ import (
|
||||
"flag"
|
||||
"go/types"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -19,19 +20,6 @@ var flagUpdate = flag.Bool("update", false, "update tests based on test output")
|
||||
// Basic tests for the compiler. Build some Go files and compare the output with
|
||||
// the expected LLVM IR for regression testing.
|
||||
func TestCompiler(t *testing.T) {
|
||||
// Check LLVM version.
|
||||
llvmMajor, err := strconv.Atoi(strings.SplitN(llvm.Version, ".", 2)[0])
|
||||
if err != nil {
|
||||
t.Fatal("could not parse LLVM version:", llvm.Version)
|
||||
}
|
||||
if llvmMajor < 11 {
|
||||
// It is likely this version needs to be bumped in the future.
|
||||
// The goal is to at least test the LLVM version that's used by default
|
||||
// in TinyGo and (if possible without too many workarounds) also some
|
||||
// previous versions.
|
||||
t.Skip("compiler tests require LLVM 11 or above, got LLVM ", llvm.Version)
|
||||
}
|
||||
|
||||
target, err := compileopts.LoadTarget("i686--linux")
|
||||
if err != nil {
|
||||
t.Fatal("failed to load target:", err)
|
||||
@@ -59,9 +47,7 @@ func TestCompiler(t *testing.T) {
|
||||
"basic.go",
|
||||
"pointer.go",
|
||||
"slice.go",
|
||||
"string.go",
|
||||
"float.go",
|
||||
"interface.go",
|
||||
}
|
||||
|
||||
for _, testCase := range tests {
|
||||
@@ -122,6 +108,8 @@ func TestCompiler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var alignRegexp = regexp.MustCompile(", align [0-9]+$")
|
||||
|
||||
// fuzzyEqualIR returns true if the two LLVM IR strings passed in are roughly
|
||||
// equal. That means, only relevant lines are compared (excluding comments
|
||||
// etc.).
|
||||
@@ -133,6 +121,15 @@ func fuzzyEqualIR(s1, s2 string) bool {
|
||||
}
|
||||
for i, line1 := range lines1 {
|
||||
line2 := lines2[i]
|
||||
match1 := alignRegexp.MatchString(line1)
|
||||
match2 := alignRegexp.MatchString(line2)
|
||||
if match1 != match2 {
|
||||
// Only one of the lines has the align keyword. Remove it.
|
||||
// This is a change to make the test work in both LLVM 10 and LLVM
|
||||
// 11 (LLVM 11 appears to automatically add alignment everywhere).
|
||||
line1 = alignRegexp.ReplaceAllString(line1, "")
|
||||
line2 = alignRegexp.ReplaceAllString(line2, "")
|
||||
}
|
||||
if line1 != line2 {
|
||||
return false
|
||||
}
|
||||
@@ -146,6 +143,12 @@ func fuzzyEqualIR(s1, s2 string) bool {
|
||||
// stripped out.
|
||||
func filterIrrelevantIRLines(lines []string) []string {
|
||||
var out []string
|
||||
llvmVersion, err := strconv.Atoi(strings.Split(llvm.Version, ".")[0])
|
||||
if err != nil {
|
||||
// Note: this should never happen and if it does, it will always happen
|
||||
// for a particular build because llvm.Version is a constant.
|
||||
panic(err)
|
||||
}
|
||||
for _, line := range lines {
|
||||
line = strings.Split(line, ";")[0] // strip out comments/info
|
||||
line = strings.TrimRight(line, "\r ") // drop '\r' on Windows and remove trailing spaces from comments
|
||||
@@ -155,6 +158,15 @@ func filterIrrelevantIRLines(lines []string) []string {
|
||||
if strings.HasPrefix(line, "source_filename = ") {
|
||||
continue
|
||||
}
|
||||
if llvmVersion < 10 && strings.HasPrefix(line, "attributes ") {
|
||||
// Ignore attribute groups. These may change between LLVM versions.
|
||||
// Right now test outputs are for LLVM 10.
|
||||
continue
|
||||
}
|
||||
if llvmVersion < 10 && strings.HasPrefix(line, "target datalayout ") {
|
||||
// Ignore the target layout. This may change between LLVM versions.
|
||||
continue
|
||||
}
|
||||
out = append(out, line)
|
||||
}
|
||||
return out
|
||||
|
||||
+2
-2
@@ -124,13 +124,13 @@ func (c *compilerContext) getRawFuncType(typ *types.Signature) llvm.Type {
|
||||
// The receiver is not an interface, but a i8* type.
|
||||
recv = c.i8ptrType
|
||||
}
|
||||
for _, info := range c.expandFormalParamType(recv, "", nil) {
|
||||
for _, info := range expandFormalParamType(recv, "", nil) {
|
||||
paramTypes = append(paramTypes, info.llvmType)
|
||||
}
|
||||
}
|
||||
for i := 0; i < typ.Params().Len(); i++ {
|
||||
subType := c.getLLVMType(typ.Params().At(i).Type())
|
||||
for _, info := range c.expandFormalParamType(subType, "", nil) {
|
||||
for _, info := range expandFormalParamType(subType, "", nil) {
|
||||
paramTypes = append(paramTypes, info.llvmType)
|
||||
}
|
||||
}
|
||||
|
||||
+19
-28
@@ -24,7 +24,16 @@ import (
|
||||
func (b *builder) createMakeInterface(val llvm.Value, typ types.Type, pos token.Pos) llvm.Value {
|
||||
itfValue := b.emitPointerPack([]llvm.Value{val})
|
||||
itfTypeCodeGlobal := b.getTypeCode(typ)
|
||||
itfTypeCode := b.CreatePtrToInt(itfTypeCodeGlobal, b.uintptrType, "")
|
||||
itfMethodSetGlobal := b.getTypeMethodSet(typ)
|
||||
itfConcreteTypeGlobal := b.mod.NamedGlobal("typeInInterface:" + itfTypeCodeGlobal.Name())
|
||||
if itfConcreteTypeGlobal.IsNil() {
|
||||
typeInInterface := b.getLLVMRuntimeType("typeInInterface")
|
||||
itfConcreteTypeGlobal = llvm.AddGlobal(b.mod, typeInInterface, "typeInInterface:"+itfTypeCodeGlobal.Name())
|
||||
itfConcreteTypeGlobal.SetInitializer(llvm.ConstNamedStruct(typeInInterface, []llvm.Value{itfTypeCodeGlobal, itfMethodSetGlobal}))
|
||||
itfConcreteTypeGlobal.SetGlobalConstant(true)
|
||||
itfConcreteTypeGlobal.SetLinkage(llvm.LinkOnceODRLinkage)
|
||||
}
|
||||
itfTypeCode := b.CreatePtrToInt(itfConcreteTypeGlobal, b.uintptrType, "")
|
||||
itf := llvm.Undef(b.getLLVMRuntimeType("_interface"))
|
||||
itf = b.CreateInsertValue(itf, itfTypeCode, 0, "")
|
||||
itf = b.CreateInsertValue(itf, itfValue, 1, "")
|
||||
@@ -45,7 +54,6 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
|
||||
// reflect lowering simpler.
|
||||
var references llvm.Value
|
||||
var length int64
|
||||
var methodSet llvm.Value
|
||||
switch typ := typ.(type) {
|
||||
case *types.Named:
|
||||
references = c.getTypeCode(typ.Underlying())
|
||||
@@ -62,26 +70,15 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
|
||||
// Take a pointer to the typecodeID of the first field (if it exists).
|
||||
structGlobal := c.makeStructTypeFields(typ)
|
||||
references = llvm.ConstBitCast(structGlobal, global.Type())
|
||||
case *types.Interface:
|
||||
methodSetGlobal := c.getInterfaceMethodSet(typ)
|
||||
references = llvm.ConstBitCast(methodSetGlobal, global.Type())
|
||||
}
|
||||
if _, ok := typ.Underlying().(*types.Interface); !ok {
|
||||
methodSet = c.getTypeMethodSet(typ)
|
||||
}
|
||||
if !references.IsNil() || length != 0 || !methodSet.IsNil() {
|
||||
if !references.IsNil() {
|
||||
// Set the 'references' field of the runtime.typecodeID struct.
|
||||
globalValue := llvm.ConstNull(global.Type().ElementType())
|
||||
if !references.IsNil() {
|
||||
globalValue = llvm.ConstInsertValue(globalValue, references, []uint32{0})
|
||||
}
|
||||
globalValue = llvm.ConstInsertValue(globalValue, references, []uint32{0})
|
||||
if length != 0 {
|
||||
lengthValue := llvm.ConstInt(c.uintptrType, uint64(length), false)
|
||||
globalValue = llvm.ConstInsertValue(globalValue, lengthValue, []uint32{1})
|
||||
}
|
||||
if !methodSet.IsNil() {
|
||||
globalValue = llvm.ConstInsertValue(globalValue, methodSet, []uint32{2})
|
||||
}
|
||||
global.SetInitializer(globalValue)
|
||||
global.SetLinkage(llvm.LinkOnceODRLinkage)
|
||||
}
|
||||
@@ -106,8 +103,8 @@ func (c *compilerContext) makeStructTypeFields(typ *types.Struct) llvm.Value {
|
||||
fieldName.SetLinkage(llvm.PrivateLinkage)
|
||||
fieldName.SetUnnamedAddr(true)
|
||||
fieldName = llvm.ConstGEP(fieldName, []llvm.Value{
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 0, false),
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 0, false),
|
||||
llvm.ConstInt(llvm.Int32Type(), 0, false),
|
||||
llvm.ConstInt(llvm.Int32Type(), 0, false),
|
||||
})
|
||||
fieldGlobalValue = llvm.ConstInsertValue(fieldGlobalValue, fieldName, []uint32{1})
|
||||
if typ.Tag(i) != "" {
|
||||
@@ -115,8 +112,8 @@ func (c *compilerContext) makeStructTypeFields(typ *types.Struct) llvm.Value {
|
||||
fieldTag.SetLinkage(llvm.PrivateLinkage)
|
||||
fieldTag.SetUnnamedAddr(true)
|
||||
fieldTag = llvm.ConstGEP(fieldTag, []llvm.Value{
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 0, false),
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 0, false),
|
||||
llvm.ConstInt(llvm.Int32Type(), 0, false),
|
||||
llvm.ConstInt(llvm.Int32Type(), 0, false),
|
||||
})
|
||||
fieldGlobalValue = llvm.ConstInsertValue(fieldGlobalValue, fieldTag, []uint32{2})
|
||||
}
|
||||
@@ -189,7 +186,7 @@ func getTypeCodeName(t types.Type) string {
|
||||
case *types.Interface:
|
||||
methods := make([]string, t.NumMethods())
|
||||
for i := 0; i < t.NumMethods(); i++ {
|
||||
methods[i] = t.Method(i).Name() + ":" + getTypeCodeName(t.Method(i).Type())
|
||||
methods[i] = getTypeCodeName(t.Method(i).Type())
|
||||
}
|
||||
return "interface:" + "{" + strings.Join(methods, ",") + "}"
|
||||
case *types.Map:
|
||||
@@ -341,16 +338,10 @@ func (b *builder) createTypeAssert(expr *ssa.TypeAssert) llvm.Value {
|
||||
commaOk = b.createRuntimeCall("interfaceImplements", []llvm.Value{actualTypeNum, methodSet}, "")
|
||||
|
||||
} else {
|
||||
globalName := "reflect/types.type:" + getTypeCodeName(expr.AssertedType) + "$id"
|
||||
assertedTypeCodeGlobal := b.mod.NamedGlobal(globalName)
|
||||
if assertedTypeCodeGlobal.IsNil() {
|
||||
// Create a new typecode global.
|
||||
assertedTypeCodeGlobal = llvm.AddGlobal(b.mod, b.ctx.Int8Type(), globalName)
|
||||
assertedTypeCodeGlobal.SetGlobalConstant(true)
|
||||
}
|
||||
// Type assert on concrete type.
|
||||
// Call runtime.typeAssert, which will be lowered to a simple icmp or
|
||||
// const false in the interface lowering pass.
|
||||
assertedTypeCodeGlobal := b.getTypeCode(expr.AssertedType)
|
||||
commaOk = b.createRuntimeCall("typeAssert", []llvm.Value{actualTypeNum, assertedTypeCodeGlobal}, "typecode")
|
||||
}
|
||||
|
||||
@@ -456,7 +447,7 @@ func (c *compilerContext) getInterfaceInvokeWrapper(fn *ssa.Function, llvmFn llv
|
||||
// Get the expanded receiver type.
|
||||
receiverType := c.getLLVMType(fn.Signature.Recv().Type())
|
||||
var expandedReceiverType []llvm.Type
|
||||
for _, info := range c.expandFormalParamType(receiverType, "", nil) {
|
||||
for _, info := range expandFormalParamType(receiverType, "", nil) {
|
||||
expandedReceiverType = append(expandedReceiverType, info.llvmType)
|
||||
}
|
||||
|
||||
|
||||
@@ -150,9 +150,6 @@ func (s *stdSizes) Sizeof(T types.Type) int64 {
|
||||
return s.PtrSize * 2
|
||||
case *types.Pointer:
|
||||
return s.PtrSize
|
||||
case *types.Signature:
|
||||
// Func values in TinyGo are two words in size.
|
||||
return s.PtrSize * 2
|
||||
default:
|
||||
panic("unknown type: " + t.String())
|
||||
}
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ func (c *compilerContext) getFunction(fn *ssa.Function) llvm.Value {
|
||||
var paramInfos []paramInfo
|
||||
for _, param := range getParams(fn.Signature) {
|
||||
paramType := c.getLLVMType(param.Type())
|
||||
paramFragmentInfos := c.expandFormalParamType(paramType, param.Name(), param.Type())
|
||||
paramFragmentInfos := expandFormalParamType(paramType, param.Name(), param.Type())
|
||||
paramInfos = append(paramInfos, paramFragmentInfos...)
|
||||
}
|
||||
|
||||
|
||||
Vendored
-54
@@ -1,54 +0,0 @@
|
||||
// This file tests interface types and interface builtins.
|
||||
|
||||
package main
|
||||
|
||||
// Test interface construction.
|
||||
|
||||
func simpleType() interface{} {
|
||||
return 0
|
||||
}
|
||||
|
||||
func pointerType() interface{} {
|
||||
// Pointers have an element type, in this case int.
|
||||
var v *int
|
||||
return v
|
||||
}
|
||||
|
||||
func interfaceType() interface{} {
|
||||
// Interfaces can exist in interfaces, but only indirectly (through
|
||||
// pointers).
|
||||
var v *error
|
||||
return v
|
||||
}
|
||||
|
||||
func anonymousInterfaceType() interface{} {
|
||||
var v *interface {
|
||||
String() string
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Test interface builtins.
|
||||
|
||||
func isInt(itf interface{}) bool {
|
||||
_, ok := itf.(int)
|
||||
return ok
|
||||
}
|
||||
|
||||
func isError(itf interface{}) bool {
|
||||
// Interface assert on (builtin) named interface type.
|
||||
_, ok := itf.(error)
|
||||
return ok
|
||||
}
|
||||
|
||||
func isStringer(itf interface{}) bool {
|
||||
// Interface assert on anonymous interface type.
|
||||
_, ok := itf.(interface {
|
||||
String() string
|
||||
})
|
||||
return ok
|
||||
}
|
||||
|
||||
func callErrorMethod(itf error) string {
|
||||
return itf.Error()
|
||||
}
|
||||
Vendored
-100
@@ -1,100 +0,0 @@
|
||||
; ModuleID = 'interface.go'
|
||||
source_filename = "interface.go"
|
||||
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128"
|
||||
target triple = "i686--linux"
|
||||
|
||||
%runtime.typecodeID = type { %runtime.typecodeID*, i32, %runtime.interfaceMethodInfo* }
|
||||
%runtime.interfaceMethodInfo = type { i8*, i32 }
|
||||
%runtime._interface = type { i32, i8* }
|
||||
%runtime._string = type { i8*, i32 }
|
||||
|
||||
@"reflect/types.type:basic:int" = linkonce_odr constant %runtime.typecodeID zeroinitializer
|
||||
@"reflect/types.type:pointer:basic:int" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:basic:int", i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"reflect/types.type:pointer:named:error" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:named:error", i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"reflect/types.type:named:error" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:interface:{Error:func:{}{basic:string}}", i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"reflect/types.type:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* bitcast ([1 x i8*]* @"reflect/types.interface:interface{Error() string}$interface" to %runtime.typecodeID*), i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"func Error() string" = external constant i8
|
||||
@"reflect/types.interface:interface{Error() string}$interface" = linkonce_odr constant [1 x i8*] [i8* @"func Error() string"]
|
||||
@"reflect/types.type:pointer:interface:{String:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:interface:{String:func:{}{basic:string}}", i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"reflect/types.type:interface:{String:func:{}{basic:string}}" = linkonce_odr constant %runtime.typecodeID { %runtime.typecodeID* bitcast ([1 x i8*]* @"reflect/types.interface:interface{String() string}$interface" to %runtime.typecodeID*), i32 0, %runtime.interfaceMethodInfo* null }
|
||||
@"func String() string" = external constant i8
|
||||
@"reflect/types.interface:interface{String() string}$interface" = linkonce_odr constant [1 x i8*] [i8* @"func String() string"]
|
||||
@"reflect/types.type:basic:int$id" = external constant i8
|
||||
@"error$interface" = linkonce_odr constant [1 x i8*] [i8* @"func Error() string"]
|
||||
|
||||
declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*)
|
||||
|
||||
define hidden void @main.init(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret void
|
||||
}
|
||||
|
||||
define hidden %runtime._interface @main.simpleType(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret %runtime._interface { i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:basic:int" to i32), i8* null }
|
||||
}
|
||||
|
||||
define hidden %runtime._interface @main.pointerType(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret %runtime._interface { i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:pointer:basic:int" to i32), i8* null }
|
||||
}
|
||||
|
||||
define hidden %runtime._interface @main.interfaceType(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret %runtime._interface { i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:pointer:named:error" to i32), i8* null }
|
||||
}
|
||||
|
||||
define hidden %runtime._interface @main.anonymousInterfaceType(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret %runtime._interface { i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:pointer:interface:{String:func:{}{basic:string}}" to i32), i8* null }
|
||||
}
|
||||
|
||||
define hidden i1 @main.isInt(i32 %itf.typecode, i8* %itf.value, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%typecode = call i1 @runtime.typeAssert(i32 %itf.typecode, i8* nonnull @"reflect/types.type:basic:int$id", i8* undef, i8* null)
|
||||
br i1 %typecode, label %typeassert.ok, label %typeassert.next
|
||||
|
||||
typeassert.ok: ; preds = %entry
|
||||
br label %typeassert.next
|
||||
|
||||
typeassert.next: ; preds = %typeassert.ok, %entry
|
||||
ret i1 %typecode
|
||||
}
|
||||
|
||||
declare i1 @runtime.typeAssert(i32, i8* dereferenceable_or_null(1), i8*, i8*)
|
||||
|
||||
define hidden i1 @main.isError(i32 %itf.typecode, i8* %itf.value, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%0 = call i1 @runtime.interfaceImplements(i32 %itf.typecode, i8** getelementptr inbounds ([1 x i8*], [1 x i8*]* @"error$interface", i32 0, i32 0), i8* undef, i8* null)
|
||||
br i1 %0, label %typeassert.ok, label %typeassert.next
|
||||
|
||||
typeassert.ok: ; preds = %entry
|
||||
br label %typeassert.next
|
||||
|
||||
typeassert.next: ; preds = %typeassert.ok, %entry
|
||||
ret i1 %0
|
||||
}
|
||||
|
||||
declare i1 @runtime.interfaceImplements(i32, i8** dereferenceable_or_null(4), i8*, i8*)
|
||||
|
||||
define hidden i1 @main.isStringer(i32 %itf.typecode, i8* %itf.value, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%0 = call i1 @runtime.interfaceImplements(i32 %itf.typecode, i8** getelementptr inbounds ([1 x i8*], [1 x i8*]* @"reflect/types.interface:interface{String() string}$interface", i32 0, i32 0), i8* undef, i8* null)
|
||||
br i1 %0, label %typeassert.ok, label %typeassert.next
|
||||
|
||||
typeassert.ok: ; preds = %entry
|
||||
br label %typeassert.next
|
||||
|
||||
typeassert.next: ; preds = %typeassert.ok, %entry
|
||||
ret i1 %0
|
||||
}
|
||||
|
||||
define hidden %runtime._string @main.callErrorMethod(i32 %itf.typecode, i8* %itf.value, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%invoke.func = call i32 @runtime.interfaceMethod(i32 %itf.typecode, i8** getelementptr inbounds ([1 x i8*], [1 x i8*]* @"error$interface", i32 0, i32 0), i8* nonnull @"func Error() string", i8* undef, i8* null)
|
||||
%invoke.func.cast = inttoptr i32 %invoke.func to %runtime._string (i8*, i8*, i8*)*
|
||||
%0 = call %runtime._string %invoke.func.cast(i8* %itf.value, i8* undef, i8* undef)
|
||||
ret %runtime._string %0
|
||||
}
|
||||
|
||||
declare i32 @runtime.interfaceMethod(i32, i8** dereferenceable_or_null(4), i8* dereferenceable_or_null(1), i8*, i8*)
|
||||
Vendored
-16
@@ -7,19 +7,3 @@ func sliceLen(ints []int) int {
|
||||
func sliceCap(ints []int) int {
|
||||
return cap(ints)
|
||||
}
|
||||
|
||||
func sliceElement(ints []int, index int) int {
|
||||
return ints[index]
|
||||
}
|
||||
|
||||
func sliceAppendValues(ints []int) []int {
|
||||
return append(ints, 1, 2, 3)
|
||||
}
|
||||
|
||||
func sliceAppendSlice(ints, added []int) []int {
|
||||
return append(ints, added...)
|
||||
}
|
||||
|
||||
func sliceCopy(dst, src []int) int {
|
||||
return copy(dst, src)
|
||||
}
|
||||
|
||||
Vendored
-67
@@ -19,70 +19,3 @@ define hidden i32 @main.sliceCap(i32* %ints.data, i32 %ints.len, i32 %ints.cap,
|
||||
entry:
|
||||
ret i32 %ints.cap
|
||||
}
|
||||
|
||||
define hidden i32 @main.sliceElement(i32* %ints.data, i32 %ints.len, i32 %ints.cap, i32 %index, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%.not = icmp ult i32 %index, %ints.len
|
||||
br i1 %.not, label %lookup.next, label %lookup.throw
|
||||
|
||||
lookup.throw: ; preds = %entry
|
||||
call void @runtime.lookupPanic(i8* undef, i8* null)
|
||||
unreachable
|
||||
|
||||
lookup.next: ; preds = %entry
|
||||
%0 = getelementptr inbounds i32, i32* %ints.data, i32 %index
|
||||
%1 = load i32, i32* %0, align 4
|
||||
ret i32 %1
|
||||
}
|
||||
|
||||
declare void @runtime.lookupPanic(i8*, i8*)
|
||||
|
||||
define hidden { i32*, i32, i32 } @main.sliceAppendValues(i32* %ints.data, i32 %ints.len, i32 %ints.cap, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%varargs = call i8* @runtime.alloc(i32 12, i8* undef, i8* null)
|
||||
%0 = bitcast i8* %varargs to i32*
|
||||
store i32 1, i32* %0, align 4
|
||||
%1 = getelementptr inbounds i8, i8* %varargs, i32 4
|
||||
%2 = bitcast i8* %1 to i32*
|
||||
store i32 2, i32* %2, align 4
|
||||
%3 = getelementptr inbounds i8, i8* %varargs, i32 8
|
||||
%4 = bitcast i8* %3 to i32*
|
||||
store i32 3, i32* %4, align 4
|
||||
%append.srcPtr = bitcast i32* %ints.data to i8*
|
||||
%append.new = call { i8*, i32, i32 } @runtime.sliceAppend(i8* %append.srcPtr, i8* nonnull %varargs, i32 %ints.len, i32 %ints.cap, i32 3, i32 4, i8* undef, i8* null)
|
||||
%append.newPtr = extractvalue { i8*, i32, i32 } %append.new, 0
|
||||
%append.newBuf = bitcast i8* %append.newPtr to i32*
|
||||
%append.newLen = extractvalue { i8*, i32, i32 } %append.new, 1
|
||||
%append.newCap = extractvalue { i8*, i32, i32 } %append.new, 2
|
||||
%5 = insertvalue { i32*, i32, i32 } undef, i32* %append.newBuf, 0
|
||||
%6 = insertvalue { i32*, i32, i32 } %5, i32 %append.newLen, 1
|
||||
%7 = insertvalue { i32*, i32, i32 } %6, i32 %append.newCap, 2
|
||||
ret { i32*, i32, i32 } %7
|
||||
}
|
||||
|
||||
declare { i8*, i32, i32 } @runtime.sliceAppend(i8*, i8*, i32, i32, i32, i32, i8*, i8*)
|
||||
|
||||
define hidden { i32*, i32, i32 } @main.sliceAppendSlice(i32* %ints.data, i32 %ints.len, i32 %ints.cap, i32* %added.data, i32 %added.len, i32 %added.cap, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%append.srcPtr = bitcast i32* %ints.data to i8*
|
||||
%append.srcPtr1 = bitcast i32* %added.data to i8*
|
||||
%append.new = call { i8*, i32, i32 } @runtime.sliceAppend(i8* %append.srcPtr, i8* %append.srcPtr1, i32 %ints.len, i32 %ints.cap, i32 %added.len, i32 4, i8* undef, i8* null)
|
||||
%append.newPtr = extractvalue { i8*, i32, i32 } %append.new, 0
|
||||
%append.newBuf = bitcast i8* %append.newPtr to i32*
|
||||
%append.newLen = extractvalue { i8*, i32, i32 } %append.new, 1
|
||||
%append.newCap = extractvalue { i8*, i32, i32 } %append.new, 2
|
||||
%0 = insertvalue { i32*, i32, i32 } undef, i32* %append.newBuf, 0
|
||||
%1 = insertvalue { i32*, i32, i32 } %0, i32 %append.newLen, 1
|
||||
%2 = insertvalue { i32*, i32, i32 } %1, i32 %append.newCap, 2
|
||||
ret { i32*, i32, i32 } %2
|
||||
}
|
||||
|
||||
define hidden i32 @main.sliceCopy(i32* %dst.data, i32 %dst.len, i32 %dst.cap, i32* %src.data, i32 %src.len, i32 %src.cap, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%copy.dstPtr = bitcast i32* %dst.data to i8*
|
||||
%copy.srcPtr = bitcast i32* %src.data to i8*
|
||||
%copy.n = call i32 @runtime.sliceCopy(i8* %copy.dstPtr, i8* %copy.srcPtr, i32 %dst.len, i32 %src.len, i32 4, i8* undef, i8* null)
|
||||
ret i32 %copy.n
|
||||
}
|
||||
|
||||
declare i32 @runtime.sliceCopy(i8*, i8*, i32, i32, i32, i8*, i8*)
|
||||
|
||||
Vendored
-21
@@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
func stringLen(s string) int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func stringIndex(s string, index int) byte {
|
||||
return s[index]
|
||||
}
|
||||
|
||||
func stringCompareEqual(s1, s2 string) bool {
|
||||
return s1 == s2
|
||||
}
|
||||
|
||||
func stringCompareUnequal(s1, s2 string) bool {
|
||||
return s1 != s2
|
||||
}
|
||||
|
||||
func stringCompareLarger(s1, s2 string) bool {
|
||||
return s1 > s2
|
||||
}
|
||||
Vendored
-57
@@ -1,57 +0,0 @@
|
||||
; ModuleID = 'string.go'
|
||||
source_filename = "string.go"
|
||||
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128"
|
||||
target triple = "i686--linux"
|
||||
|
||||
declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*)
|
||||
|
||||
define hidden void @main.init(i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret void
|
||||
}
|
||||
|
||||
define hidden i32 @main.stringLen(i8* %s.data, i32 %s.len, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
ret i32 %s.len
|
||||
}
|
||||
|
||||
define hidden i8 @main.stringIndex(i8* %s.data, i32 %s.len, i32 %index, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%.not = icmp ult i32 %index, %s.len
|
||||
br i1 %.not, label %lookup.next, label %lookup.throw
|
||||
|
||||
lookup.throw: ; preds = %entry
|
||||
call void @runtime.lookupPanic(i8* undef, i8* null)
|
||||
unreachable
|
||||
|
||||
lookup.next: ; preds = %entry
|
||||
%0 = getelementptr inbounds i8, i8* %s.data, i32 %index
|
||||
%1 = load i8, i8* %0, align 1
|
||||
ret i8 %1
|
||||
}
|
||||
|
||||
declare void @runtime.lookupPanic(i8*, i8*)
|
||||
|
||||
define hidden i1 @main.stringCompareEqual(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringEqual(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* undef, i8* null)
|
||||
ret i1 %0
|
||||
}
|
||||
|
||||
declare i1 @runtime.stringEqual(i8*, i32, i8*, i32, i8*, i8*)
|
||||
|
||||
define hidden i1 @main.stringCompareUnequal(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringEqual(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* undef, i8* null)
|
||||
%1 = xor i1 %0, true
|
||||
ret i1 %1
|
||||
}
|
||||
|
||||
define hidden i1 @main.stringCompareLarger(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* %context, i8* %parentHandle) unnamed_addr {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringLess(i8* %s1.data, i32 %s1.len, i8* %s2.data, i32 %s2.len, i8* undef, i8* null)
|
||||
%1 = xor i1 %0, true
|
||||
ret i1 %1
|
||||
}
|
||||
|
||||
declare i1 @runtime.stringLess(i8*, i32, i8*, i32, i8*, i8*)
|
||||
@@ -20,11 +20,6 @@ var (
|
||||
errMapAlreadyCreated = errors.New("interp: map already created")
|
||||
)
|
||||
|
||||
// This is one of the errors that can be returned from toLLVMValue when the
|
||||
// passed type does not fit the data to serialize. It is recoverable by
|
||||
// serializing without a type (using rawValue.rawLLVMValue).
|
||||
var errInvalidPtrToIntSize = errors.New("interp: ptrtoint integer size does not equal pointer size")
|
||||
|
||||
func isRecoverableError(err error) bool {
|
||||
return err == errIntegerAsPointer || err == errUnsupportedInst || err == errUnsupportedRuntimeInst || err == errMapAlreadyCreated
|
||||
}
|
||||
|
||||
+5
-124
@@ -11,12 +11,6 @@ import (
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// Version of the interp package. It must be incremented whenever the interp
|
||||
// package is changed in a way that affects the output so that cached package
|
||||
// builds will be invalidated.
|
||||
// This version is independent of the TinyGo version number.
|
||||
const Version = 1
|
||||
|
||||
// Enable extra checks, which should be disabled by default.
|
||||
// This may help track down bugs by adding a few more sanity checks.
|
||||
const checks = true
|
||||
@@ -38,7 +32,9 @@ type runner struct {
|
||||
callsExecuted uint64
|
||||
}
|
||||
|
||||
func newRunner(mod llvm.Module, debug bool) *runner {
|
||||
// Run evaluates runtime.initAll function as much as possible at compile time.
|
||||
// Set debug to true if it should print output while running.
|
||||
func Run(mod llvm.Module, debug bool) error {
|
||||
r := runner{
|
||||
mod: mod,
|
||||
targetData: llvm.NewTargetData(mod.DataLayout()),
|
||||
@@ -51,13 +47,6 @@ func newRunner(mod llvm.Module, debug bool) *runner {
|
||||
r.pointerSize = uint32(r.targetData.PointerSize())
|
||||
r.i8ptrType = llvm.PointerType(mod.Context().Int8Type(), 0)
|
||||
r.maxAlign = r.targetData.PrefTypeAlignment(r.i8ptrType) // assume pointers are maximally aligned (this is not always the case)
|
||||
return &r
|
||||
}
|
||||
|
||||
// Run evaluates runtime.initAll function as much as possible at compile time.
|
||||
// Set debug to true if it should print output while running.
|
||||
func Run(mod llvm.Module, debug bool) error {
|
||||
r := newRunner(mod, debug)
|
||||
|
||||
initAll := mod.NamedFunction("runtime.initAll")
|
||||
bb := initAll.EntryBasicBlock()
|
||||
@@ -128,7 +117,7 @@ func Run(mod llvm.Module, debug bool) error {
|
||||
r.pkgName = ""
|
||||
|
||||
// Update all global variables in the LLVM module.
|
||||
mem := memoryView{r: r}
|
||||
mem := memoryView{r: &r}
|
||||
for _, obj := range r.objects {
|
||||
if obj.llvmGlobal.IsNil() {
|
||||
continue
|
||||
@@ -136,34 +125,7 @@ func Run(mod llvm.Module, debug bool) error {
|
||||
if obj.buffer == nil {
|
||||
continue
|
||||
}
|
||||
initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.Type().ElementType(), &mem)
|
||||
if err == errInvalidPtrToIntSize {
|
||||
// This can happen when a previous interp run did not have the
|
||||
// correct LLVM type for a global and made something up. In that
|
||||
// case, some fields could be written out as a series of (null)
|
||||
// bytes even though they actually contain a pointer value.
|
||||
// As a fallback, use asRawValue to get something of the correct
|
||||
// memory layout.
|
||||
initializer, err := obj.buffer.asRawValue(r).rawLLVMValue(&mem)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
initializerType := initializer.Type()
|
||||
newGlobal := llvm.AddGlobal(mod, initializerType, obj.llvmGlobal.Name()+".tmp")
|
||||
newGlobal.SetInitializer(initializer)
|
||||
newGlobal.SetLinkage(obj.llvmGlobal.Linkage())
|
||||
newGlobal.SetAlignment(obj.llvmGlobal.Alignment())
|
||||
// TODO: copy debug info, unnamed_addr, ...
|
||||
bitcast := llvm.ConstBitCast(newGlobal, obj.llvmGlobal.Type())
|
||||
obj.llvmGlobal.ReplaceAllUsesWith(bitcast)
|
||||
name := obj.llvmGlobal.Name()
|
||||
obj.llvmGlobal.EraseFromParentAsGlobal()
|
||||
newGlobal.SetName(name)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
initializer := obj.buffer.toLLVMValue(obj.llvmGlobal.Type().ElementType(), &mem)
|
||||
if checks && initializer.Type() != obj.llvmGlobal.Type().ElementType() {
|
||||
panic("initializer type mismatch")
|
||||
}
|
||||
@@ -173,87 +135,6 @@ func Run(mod llvm.Module, debug bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunFunc evaluates a single package initializer at compile time.
|
||||
// Set debug to true if it should print output while running.
|
||||
func RunFunc(fn llvm.Value, debug bool) error {
|
||||
// Create and initialize *runner object.
|
||||
mod := fn.GlobalParent()
|
||||
r := newRunner(mod, debug)
|
||||
initName := fn.Name()
|
||||
if !strings.HasSuffix(initName, ".init") {
|
||||
return errorAt(fn, "interp: unexpected function name (expected *.init)")
|
||||
}
|
||||
r.pkgName = initName[:len(initName)-len(".init")]
|
||||
|
||||
// Create new function with the interp result.
|
||||
newFn := llvm.AddFunction(mod, fn.Name()+".tmp", fn.Type().ElementType())
|
||||
newFn.SetLinkage(fn.Linkage())
|
||||
newFn.SetVisibility(fn.Visibility())
|
||||
entry := mod.Context().AddBasicBlock(newFn, "entry")
|
||||
|
||||
// Create a builder, to insert instructions that could not be evaluated at
|
||||
// compile time.
|
||||
r.builder = mod.Context().NewBuilder()
|
||||
defer r.builder.Dispose()
|
||||
r.builder.SetInsertPointAtEnd(entry)
|
||||
|
||||
// Copy debug information.
|
||||
subprogram := fn.Subprogram()
|
||||
if !subprogram.IsNil() {
|
||||
newFn.SetSubprogram(subprogram)
|
||||
r.builder.SetCurrentDebugLocation(subprogram.SubprogramLine(), 0, subprogram, llvm.Metadata{})
|
||||
}
|
||||
|
||||
// Run the initializer, filling the .init.tmp function.
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, "interp:", fn.Name())
|
||||
}
|
||||
_, pkgMem, callErr := r.run(r.getFunction(fn), nil, nil, " ")
|
||||
if callErr != nil {
|
||||
if isRecoverableError(callErr.Err) {
|
||||
// Could not finish, but could recover from it.
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, "not interpreting", r.pkgName, "because of error:", callErr.Error())
|
||||
}
|
||||
newFn.EraseFromParentAsFunction()
|
||||
return nil
|
||||
}
|
||||
return callErr
|
||||
}
|
||||
for index, obj := range pkgMem.objects {
|
||||
r.objects[index] = obj
|
||||
}
|
||||
|
||||
// Update globals with values determined while running the initializer above.
|
||||
mem := memoryView{r: r}
|
||||
for _, obj := range r.objects {
|
||||
if obj.llvmGlobal.IsNil() {
|
||||
continue
|
||||
}
|
||||
if obj.buffer == nil {
|
||||
continue
|
||||
}
|
||||
initializer, err := obj.buffer.toLLVMValue(obj.llvmGlobal.Type().ElementType(), &mem)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if checks && initializer.Type() != obj.llvmGlobal.Type().ElementType() {
|
||||
panic("initializer type mismatch")
|
||||
}
|
||||
obj.llvmGlobal.SetInitializer(initializer)
|
||||
}
|
||||
|
||||
// Finalize: remove the old init function and replace it with the new
|
||||
// (.init.tmp) function.
|
||||
r.builder.CreateRetVoid()
|
||||
fnName := fn.Name()
|
||||
fn.ReplaceAllUsesWith(newFn)
|
||||
fn.EraseFromParentAsFunction()
|
||||
newFn.SetName(fnName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getFunction returns the compiled version of the given LLVM function. It
|
||||
// compiles the function if necessary and caches the result.
|
||||
func (r *runner) getFunction(llvmFn llvm.Value) *function {
|
||||
|
||||
+16
-100
@@ -155,8 +155,11 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
// which case this call won't even get to this point but will
|
||||
// already be emitted in initAll.
|
||||
continue
|
||||
case strings.HasPrefix(callFn.name, "runtime.print") || callFn.name == "runtime._panic" || callFn.name == "runtime.hashmapGet":
|
||||
case callFn.name == "(reflect.Type).Elem" || strings.HasPrefix(callFn.name, "runtime.print") || callFn.name == "runtime._panic" || callFn.name == "runtime.hashmapGet":
|
||||
// These functions should be run at runtime. Specifically:
|
||||
// * (reflect.Type).Elem is a special function. It should
|
||||
// eventually be interpreted, but fall back to a runtime call
|
||||
// for now.
|
||||
// * Print and panic functions are best emitted directly without
|
||||
// interpreting them, otherwise we get a ton of putchar (etc.)
|
||||
// calls.
|
||||
@@ -277,58 +280,26 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
copy(dstBuf.buf[dst.offset():dst.offset()+nBytes], srcBuf.buf[src.offset():])
|
||||
dstObj.buffer = dstBuf
|
||||
mem.put(dst.index(), dstObj)
|
||||
case callFn.name == "(reflect.rawType).elem":
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"call (reflect.rawType).elem:", operands[1:])
|
||||
}
|
||||
// Extract the type code global from the first parameter.
|
||||
typecodeIDPtrToInt, err := operands[1].toLLVMValue(inst.llvmInst.Operand(0).Type(), &mem)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
typecodeID := typecodeIDPtrToInt.Operand(0)
|
||||
|
||||
// Get the type class.
|
||||
// See also: getClassAndValueFromTypeCode in transform/reflect.go.
|
||||
typecodeName := typecodeID.Name()
|
||||
const prefix = "reflect/types.type:"
|
||||
if !strings.HasPrefix(typecodeName, prefix) {
|
||||
panic("unexpected typecode name: " + typecodeName)
|
||||
}
|
||||
id := typecodeName[len(prefix):]
|
||||
class := id[:strings.IndexByte(id, ':')]
|
||||
value := id[len(class)+1:]
|
||||
if class == "named" {
|
||||
// Get the underlying type.
|
||||
class = value[:strings.IndexByte(value, ':')]
|
||||
value = value[len(class)+1:]
|
||||
}
|
||||
|
||||
// Elem() is only valid for certain type classes.
|
||||
switch class {
|
||||
case "chan", "pointer", "slice", "array":
|
||||
elementType := llvm.ConstExtractValue(typecodeID.Initializer(), []uint32{0})
|
||||
uintptrType := r.mod.Context().IntType(int(mem.r.pointerSize) * 8)
|
||||
locals[inst.localIndex] = r.getValue(llvm.ConstPtrToInt(elementType, uintptrType))
|
||||
default:
|
||||
return nil, mem, r.errorAt(inst, fmt.Errorf("(reflect.Type).Elem() called on %s type", class))
|
||||
}
|
||||
case callFn.name == "runtime.typeAssert":
|
||||
// This function must be implemented manually as it is normally
|
||||
// implemented by the interface lowering pass.
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"typeassert:", operands[1:])
|
||||
}
|
||||
assertedType, err := operands[2].toLLVMValue(inst.llvmInst.Operand(1).Type(), &mem)
|
||||
typeInInterfacePtr, err := operands[1].asPointer(r)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
actualTypePtrToInt, err := operands[1].toLLVMValue(inst.llvmInst.Operand(0).Type(), &mem)
|
||||
actualType, err := mem.load(typeInInterfacePtr, r.pointerSize).asPointer(r)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
actualType := actualTypePtrToInt.Operand(0)
|
||||
if actualType.Name()+"$id" == assertedType.Name() {
|
||||
assertedType, err := operands[2].asPointer(r)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
result := assertedType.asRawValue(r).equal(actualType.asRawValue(r))
|
||||
if result {
|
||||
locals[inst.localIndex] = literalValue{uint8(1)}
|
||||
} else {
|
||||
locals[inst.localIndex] = literalValue{uint8(0)}
|
||||
@@ -339,11 +310,11 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
}
|
||||
|
||||
// Load various values for the interface implements check below.
|
||||
typecodePtr, err := operands[1].asPointer(r)
|
||||
typeInInterfacePtr, err := operands[1].asPointer(r)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
methodSetPtr, err := mem.load(typecodePtr.addOffset(r.pointerSize*2), r.pointerSize).asPointer(r)
|
||||
methodSetPtr, err := mem.load(typeInInterfacePtr.addOffset(r.pointerSize), r.pointerSize).asPointer(r)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
@@ -378,43 +349,6 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
}
|
||||
// If assertOk is still 1, the assertion succeeded.
|
||||
locals[inst.localIndex] = literalValue{assertOk}
|
||||
case callFn.name == "runtime.interfaceMethod":
|
||||
// This builtin returns the function (which may be a thunk) to
|
||||
// invoke a method on an interface. It does not call the method.
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"interface method:", operands[1:])
|
||||
}
|
||||
|
||||
// Load the first param, which is the type code (ptrtoint of the
|
||||
// type code global).
|
||||
typecodeIDPtrToInt, err := operands[1].toLLVMValue(inst.llvmInst.Operand(0).Type(), &mem)
|
||||
if err != nil {
|
||||
return nil, mem, r.errorAt(inst, err)
|
||||
}
|
||||
typecodeID := typecodeIDPtrToInt.Operand(0).Initializer()
|
||||
|
||||
// Load the method set, which is part of the typecodeID object.
|
||||
methodSet := llvm.ConstExtractValue(typecodeID, []uint32{2}).Operand(0).Initializer()
|
||||
|
||||
// We don't need to load the interface method set.
|
||||
|
||||
// Load the signature of the to-be-called function.
|
||||
signature := inst.llvmInst.Operand(2)
|
||||
|
||||
// Iterate through all methods, looking for the one method that
|
||||
// should be returned.
|
||||
numMethods := methodSet.Type().ArrayLength()
|
||||
var method llvm.Value
|
||||
for i := 0; i < numMethods; i++ {
|
||||
methodSignature := llvm.ConstExtractValue(methodSet, []uint32{uint32(i), 0})
|
||||
if methodSignature == signature {
|
||||
method = llvm.ConstExtractValue(methodSet, []uint32{uint32(i), 1}).Operand(0)
|
||||
}
|
||||
}
|
||||
if method.IsNil() {
|
||||
return nil, mem, r.errorAt(inst, errors.New("could not find method: "+signature.Name()))
|
||||
}
|
||||
locals[inst.localIndex] = r.getValue(method)
|
||||
case callFn.name == "runtime.hashmapMake":
|
||||
// Create a new map.
|
||||
hashmapPointerType := inst.llvmInst.Type()
|
||||
@@ -548,13 +482,6 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
continue
|
||||
}
|
||||
result := mem.load(ptr, uint32(size))
|
||||
if result == nil {
|
||||
err := r.runAtRuntime(fn, inst, locals, &mem, indent)
|
||||
if err != nil {
|
||||
return nil, mem, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"load:", ptr, "->", result)
|
||||
}
|
||||
@@ -577,14 +504,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"store:", val, ptr)
|
||||
}
|
||||
ok := mem.store(val, ptr)
|
||||
if !ok {
|
||||
// Could not store the value, do it at runtime.
|
||||
err := r.runAtRuntime(fn, inst, locals, &mem, indent)
|
||||
if err != nil {
|
||||
return nil, mem, err
|
||||
}
|
||||
}
|
||||
mem.store(val, ptr)
|
||||
case llvm.Alloca:
|
||||
// Alloca normally allocates some stack memory. In the interpreter,
|
||||
// it allocates a global instead.
|
||||
@@ -916,11 +836,7 @@ func (r *runner) runAtRuntime(fn *function, inst instruction, locals []value, me
|
||||
for i := 0; i < numOperands; i++ {
|
||||
operand := inst.llvmInst.Operand(i)
|
||||
if !operand.IsAInstruction().IsNil() || !operand.IsAArgument().IsNil() {
|
||||
var err error
|
||||
operand, err = locals[fn.locals[operand]].toLLVMValue(operand.Type(), mem)
|
||||
if err != nil {
|
||||
return r.errorAt(inst, err)
|
||||
}
|
||||
operand = locals[fn.locals[operand]].toLLVMValue(operand.Type(), mem)
|
||||
}
|
||||
operands[i] = operand
|
||||
}
|
||||
|
||||
+56
-105
@@ -259,17 +259,12 @@ func (mv *memoryView) put(index uint32, obj object) {
|
||||
mv.objects[index] = obj
|
||||
}
|
||||
|
||||
// Load the value behind the given pointer. Returns nil if the pointer points to
|
||||
// an external global.
|
||||
// Load the value behind the given pointer.
|
||||
func (mv *memoryView) load(p pointerValue, size uint32) value {
|
||||
if checks && mv.hasExternalStore(p) {
|
||||
panic("interp: load from object with external store")
|
||||
}
|
||||
obj := mv.get(p.index())
|
||||
if obj.buffer == nil {
|
||||
// External global, return nil.
|
||||
return nil
|
||||
}
|
||||
if p.offset() == 0 && size == obj.size {
|
||||
return obj.buffer.clone()
|
||||
}
|
||||
@@ -285,17 +280,12 @@ func (mv *memoryView) load(p pointerValue, size uint32) value {
|
||||
|
||||
// Store to the value behind the given pointer. This overwrites the value in the
|
||||
// memory view, so that the changed value is discarded when the memory view is
|
||||
// reverted. Returns true on success, false if the object to store to is
|
||||
// external.
|
||||
func (mv *memoryView) store(v value, p pointerValue) bool {
|
||||
// reverted.
|
||||
func (mv *memoryView) store(v value, p pointerValue) {
|
||||
if checks && mv.hasExternalLoadOrStore(p) {
|
||||
panic("interp: store to object with external load/store")
|
||||
}
|
||||
obj := mv.get(p.index())
|
||||
if obj.buffer == nil {
|
||||
// External global, return false (for a failure).
|
||||
return false
|
||||
}
|
||||
if checks && p.offset()+v.len(mv.r) > obj.size {
|
||||
panic("interp: store out of bounds")
|
||||
}
|
||||
@@ -311,7 +301,6 @@ func (mv *memoryView) store(v value, p pointerValue) bool {
|
||||
}
|
||||
}
|
||||
mv.put(p.index(), obj)
|
||||
return true // success
|
||||
}
|
||||
|
||||
// value is some sort of value, comparable to a LLVM constant. It can be
|
||||
@@ -325,7 +314,7 @@ type value interface {
|
||||
asRawValue(*runner) rawValue
|
||||
Uint() uint64
|
||||
Int() int64
|
||||
toLLVMValue(llvm.Type, *memoryView) (llvm.Value, error)
|
||||
toLLVMValue(llvm.Type, *memoryView) llvm.Value
|
||||
String() string
|
||||
}
|
||||
|
||||
@@ -416,25 +405,25 @@ func (v literalValue) Int() int64 {
|
||||
}
|
||||
}
|
||||
|
||||
func (v literalValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value, error) {
|
||||
func (v literalValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) llvm.Value {
|
||||
switch llvmType.TypeKind() {
|
||||
case llvm.IntegerTypeKind:
|
||||
switch value := v.value.(type) {
|
||||
case uint64:
|
||||
return llvm.ConstInt(llvmType, value, false), nil
|
||||
return llvm.ConstInt(llvmType, value, false)
|
||||
case uint32:
|
||||
return llvm.ConstInt(llvmType, uint64(value), false), nil
|
||||
return llvm.ConstInt(llvmType, uint64(value), false)
|
||||
case uint16:
|
||||
return llvm.ConstInt(llvmType, uint64(value), false), nil
|
||||
return llvm.ConstInt(llvmType, uint64(value), false)
|
||||
case uint8:
|
||||
return llvm.ConstInt(llvmType, uint64(value), false), nil
|
||||
return llvm.ConstInt(llvmType, uint64(value), false)
|
||||
default:
|
||||
return llvm.Value{}, errors.New("interp: unknown literal type")
|
||||
panic("inpterp: unknown literal type")
|
||||
}
|
||||
case llvm.DoubleTypeKind:
|
||||
return llvm.ConstFloat(llvmType, math.Float64frombits(v.value.(uint64))), nil
|
||||
return llvm.ConstFloat(llvmType, math.Float64frombits(v.value.(uint64)))
|
||||
case llvm.FloatTypeKind:
|
||||
return llvm.ConstFloat(llvmType, float64(math.Float32frombits(v.value.(uint32)))), nil
|
||||
return llvm.ConstFloat(llvmType, float64(math.Float32frombits(v.value.(uint32))))
|
||||
default:
|
||||
return v.asRawValue(mem.r).toLLVMValue(llvmType, mem)
|
||||
}
|
||||
@@ -518,7 +507,7 @@ func (v pointerValue) llvmValue(mem *memoryView) llvm.Value {
|
||||
// toLLVMValue returns the LLVM value for this pointer, which may be a GEP or
|
||||
// bitcast. The llvm.Type parameter is optional, if omitted the pointer type may
|
||||
// be different than expected.
|
||||
func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value, error) {
|
||||
func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) llvm.Value {
|
||||
// Obtain the llvmValue, creating it if it doesn't exist yet.
|
||||
llvmValue := v.llvmValue(mem)
|
||||
if llvmValue.IsNil() {
|
||||
@@ -529,10 +518,7 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
if obj.llvmType.IsNil() {
|
||||
// Create an initializer without knowing the global type.
|
||||
// This is probably the result of a runtime.alloc call.
|
||||
initializer, err := obj.buffer.asRawValue(mem.r).rawLLVMValue(mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
initializer := obj.buffer.asRawValue(mem.r).rawLLVMValue(mem)
|
||||
globalType := initializer.Type()
|
||||
llvmValue = llvm.AddGlobal(mem.r.mod, globalType, obj.globalName)
|
||||
llvmValue.SetInitializer(initializer)
|
||||
@@ -551,12 +537,9 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
// Set the initializer for the global. Do this after creation to avoid
|
||||
// infinite recursion between creating the global and creating the
|
||||
// contents of the global (if the global contains itself).
|
||||
initializer, err := obj.buffer.toLLVMValue(globalType, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
initializer := obj.buffer.toLLVMValue(globalType, mem)
|
||||
if checks && initializer.Type() != globalType {
|
||||
return llvm.Value{}, errors.New("interp: allocated value does not match allocated type")
|
||||
panic("allocated value does not match allocated type")
|
||||
}
|
||||
llvmValue.SetInitializer(initializer)
|
||||
}
|
||||
@@ -569,7 +552,7 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
}
|
||||
|
||||
if llvmType.IsNil() {
|
||||
return llvmValue, nil
|
||||
return llvmValue
|
||||
}
|
||||
|
||||
if llvmType.TypeKind() != llvm.PointerTypeKind {
|
||||
@@ -581,7 +564,7 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
// This can be worked around by simply converting to a raw value,
|
||||
// rawValue knows how to create such structs.
|
||||
if v.offset() != 0 {
|
||||
return llvm.Value{}, errors.New("interp: offset set without known pointer type")
|
||||
panic("offset set without known pointer type")
|
||||
}
|
||||
return v.asRawValue(mem.r).toLLVMValue(llvmType, mem)
|
||||
}
|
||||
@@ -592,14 +575,14 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
if v.offset() != 0 {
|
||||
// This should never happen, if offset is non-zero, the types
|
||||
// shouldn't match.
|
||||
return llvm.Value{}, errors.New("interp: offset set while there is no way to convert the type")
|
||||
panic("offset set while there is no way to convert the type")
|
||||
}
|
||||
return llvmValue, nil
|
||||
return llvmValue
|
||||
}
|
||||
|
||||
if v.offset() == 0 {
|
||||
// Offset is zero, so we can just bitcast to get a correct pointer.
|
||||
return llvm.ConstBitCast(llvmValue, llvmType), nil
|
||||
return llvm.ConstBitCast(llvmValue, llvmType)
|
||||
}
|
||||
|
||||
// We need to make a constant GEP for pointer arithmetic.
|
||||
@@ -623,11 +606,11 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
offset -= int64(mem.r.targetData.ElementOffset(objectElementType, element))
|
||||
objectElementType = objectElementType.StructElementTypes()[element]
|
||||
default:
|
||||
return llvm.Value{}, errors.New("interp: pointer index with something other than a struct or array?")
|
||||
panic("pointer index with something other than a struct or array?")
|
||||
}
|
||||
}
|
||||
if offset < 0 {
|
||||
return llvm.Value{}, errors.New("interp: offset has somehow gone negative, this should be impossible")
|
||||
panic("offset has somehow gone negative, this should be impossible")
|
||||
}
|
||||
|
||||
// Finally do the gep, using the above computed indices.
|
||||
@@ -635,9 +618,9 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
|
||||
// the bits of the pointer are now correct, just not the type).
|
||||
gep := llvm.ConstInBoundsGEP(llvmValue, indices)
|
||||
if gep.Type() != llvmType {
|
||||
return llvm.ConstBitCast(gep, llvmType), nil
|
||||
return llvm.ConstBitCast(gep, llvmType)
|
||||
}
|
||||
return gep, nil
|
||||
return gep
|
||||
}
|
||||
|
||||
// mapValue implements a Go map which is created at compile time and stored as a
|
||||
@@ -730,11 +713,7 @@ func (b *mapBucket) create(ctx llvm.Context, nextBucket llvm.Value, mem *memoryV
|
||||
// Create data for keys.
|
||||
var keyValues []llvm.Value
|
||||
for _, key := range b.keys {
|
||||
keyValue, err := key.rawLLVMValue(mem)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
keyValues = append(keyValues, keyValue)
|
||||
keyValues = append(keyValues, key.rawLLVMValue(mem))
|
||||
}
|
||||
if len(b.keys) < 8 {
|
||||
keyValues = append(keyValues, llvm.ConstNull(llvm.ArrayType(int8Type, int(b.m.keySize)*(8-len(b.keys)))))
|
||||
@@ -747,11 +726,7 @@ func (b *mapBucket) create(ctx llvm.Context, nextBucket llvm.Value, mem *memoryV
|
||||
// Create data for values.
|
||||
var valueValues []llvm.Value
|
||||
for _, value := range b.values {
|
||||
v, err := value.rawLLVMValue(mem)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
valueValues = append(valueValues, v)
|
||||
valueValues = append(valueValues, value.rawLLVMValue(mem))
|
||||
}
|
||||
if len(b.values) < 8 {
|
||||
valueValues = append(valueValues, llvm.ConstNull(llvm.ArrayType(int8Type, int(b.m.valueSize)*(8-len(b.values)))))
|
||||
@@ -775,9 +750,9 @@ func (b *mapBucket) create(ctx llvm.Context, nextBucket llvm.Value, mem *memoryV
|
||||
return bucket
|
||||
}
|
||||
|
||||
func (v *mapValue) toLLVMValue(hashmapType llvm.Type, mem *memoryView) (llvm.Value, error) {
|
||||
func (v *mapValue) toLLVMValue(hashmapType llvm.Type, mem *memoryView) llvm.Value {
|
||||
if !v.hashmap.IsNil() {
|
||||
return v.hashmap, nil
|
||||
return v.hashmap
|
||||
}
|
||||
|
||||
// Create a slice of buckets with all the keys and values in the hashmap.
|
||||
@@ -797,12 +772,12 @@ func (v *mapValue) toLLVMValue(hashmapType llvm.Type, mem *memoryView) (llvm.Val
|
||||
copy(keyValue.buf[v.keySize/2:], literalValue{key.size}.asRawValue(v.r).buf)
|
||||
case rawValue:
|
||||
if key.hasPointer() {
|
||||
return llvm.Value{}, errors.New("interp: todo: map key with pointer")
|
||||
panic("todo: map key with pointer")
|
||||
}
|
||||
data = key.buf
|
||||
keyValue = key
|
||||
default:
|
||||
return llvm.Value{}, errors.New("interp: unknown map key type")
|
||||
panic("unknown map key type")
|
||||
}
|
||||
buf := make([]byte, len(data))
|
||||
for i, p := range data {
|
||||
@@ -846,7 +821,7 @@ func (v *mapValue) toLLVMValue(hashmapType llvm.Type, mem *memoryView) (llvm.Val
|
||||
})
|
||||
|
||||
v.hashmap = hashmap
|
||||
return v.hashmap, nil
|
||||
return v.hashmap
|
||||
}
|
||||
|
||||
// putString does a map assign operation, assuming that the map is of type
|
||||
@@ -1043,7 +1018,7 @@ func (v rawValue) equal(rhs rawValue) bool {
|
||||
// goes. The resulting value does not have a specified type, but it will be the
|
||||
// same size and have the same bytes if it was created with a provided LLVM type
|
||||
// (through toLLVMValue).
|
||||
func (v rawValue) rawLLVMValue(mem *memoryView) (llvm.Value, error) {
|
||||
func (v rawValue) rawLLVMValue(mem *memoryView) llvm.Value {
|
||||
var structFields []llvm.Value
|
||||
ctx := mem.r.mod.Context()
|
||||
int8Type := ctx.Int8Type()
|
||||
@@ -1067,16 +1042,13 @@ func (v rawValue) rawLLVMValue(mem *memoryView) (llvm.Value, error) {
|
||||
for i := uint32(0); i < uint32(len(v.buf)); {
|
||||
if v.buf[i] > 255 {
|
||||
addBytes()
|
||||
field, err := pointerValue{v.buf[i]}.toLLVMValue(llvm.Type{}, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
field := pointerValue{v.buf[i]}.toLLVMValue(llvm.Type{}, mem)
|
||||
elementType := field.Type().ElementType()
|
||||
if elementType.TypeKind() == llvm.StructTypeKind {
|
||||
// There are some special pointer types that should be used as a
|
||||
// ptrtoint, so that they can be used in certain optimizations.
|
||||
name := elementType.StructName()
|
||||
if name == "runtime.typecodeID" || name == "runtime.funcValueWithSignature" {
|
||||
if name == "runtime.typeInInterface" || name == "runtime.funcValueWithSignature" {
|
||||
uintptrType := ctx.IntType(int(mem.r.pointerSize) * 8)
|
||||
field = llvm.ConstPtrToInt(field, uintptrType)
|
||||
}
|
||||
@@ -1093,12 +1065,12 @@ func (v rawValue) rawLLVMValue(mem *memoryView) (llvm.Value, error) {
|
||||
|
||||
// Return the created data.
|
||||
if len(structFields) == 1 {
|
||||
return structFields[0], nil
|
||||
return structFields[0]
|
||||
}
|
||||
return ctx.ConstStruct(structFields, false), nil
|
||||
return ctx.ConstStruct(structFields, false)
|
||||
}
|
||||
|
||||
func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value, error) {
|
||||
func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) llvm.Value {
|
||||
isZero := true
|
||||
for _, p := range v.buf {
|
||||
if p != 0 {
|
||||
@@ -1107,7 +1079,7 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
}
|
||||
}
|
||||
if isZero {
|
||||
return llvm.ConstNull(llvmType), nil
|
||||
return llvm.ConstNull(llvmType)
|
||||
}
|
||||
switch llvmType.TypeKind() {
|
||||
case llvm.IntegerTypeKind:
|
||||
@@ -1116,17 +1088,7 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if checks && mem.r.targetData.TypeAllocSize(llvmType) != mem.r.targetData.TypeAllocSize(mem.r.i8ptrType) {
|
||||
// Probably trying to serialize a pointer to a byte array,
|
||||
// perhaps as a result of rawLLVMValue() in a previous interp
|
||||
// run.
|
||||
return llvm.Value{}, errInvalidPtrToIntSize
|
||||
}
|
||||
v, err := ptr.toLLVMValue(llvm.Type{}, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
return llvm.ConstPtrToInt(v, llvmType), nil
|
||||
return llvm.ConstPtrToInt(ptr.toLLVMValue(llvm.Type{}, mem), llvmType)
|
||||
}
|
||||
var n uint64
|
||||
switch llvmType.IntTypeWidth() {
|
||||
@@ -1146,7 +1108,7 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
default:
|
||||
panic("unknown integer size")
|
||||
}
|
||||
return llvm.ConstInt(llvmType, n, false), nil
|
||||
return llvm.ConstInt(llvmType, n, false)
|
||||
case llvm.StructTypeKind:
|
||||
fieldTypes := llvmType.StructElementTypes()
|
||||
fields := make([]llvm.Value, len(fieldTypes))
|
||||
@@ -1155,16 +1117,12 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
field := rawValue{
|
||||
buf: v.buf[offset:],
|
||||
}
|
||||
var err error
|
||||
fields[i], err = field.toLLVMValue(fieldType, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
fields[i] = field.toLLVMValue(fieldType, mem)
|
||||
}
|
||||
if llvmType.StructName() != "" {
|
||||
return llvm.ConstNamedStruct(llvmType, fields), nil
|
||||
return llvm.ConstNamedStruct(llvmType, fields)
|
||||
}
|
||||
return llvmType.Context().ConstStruct(fields, false), nil
|
||||
return llvmType.Context().ConstStruct(fields, false)
|
||||
case llvm.ArrayTypeKind:
|
||||
numElements := llvmType.ArrayLength()
|
||||
childType := llvmType.ElementType()
|
||||
@@ -1175,34 +1133,27 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
field := rawValue{
|
||||
buf: v.buf[offset:],
|
||||
}
|
||||
var err error
|
||||
fields[i], err = field.toLLVMValue(childType, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
fields[i] = field.toLLVMValue(childType, mem)
|
||||
if checks && fields[i].Type() != childType {
|
||||
panic("child type doesn't match")
|
||||
}
|
||||
}
|
||||
return llvm.ConstArray(childType, fields), nil
|
||||
return llvm.ConstArray(childType, fields)
|
||||
case llvm.PointerTypeKind:
|
||||
if v.buf[0] > 255 {
|
||||
// This is a regular pointer.
|
||||
llvmValue, err := pointerValue{v.buf[0]}.toLLVMValue(llvm.Type{}, mem)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
llvmValue := pointerValue{v.buf[0]}.toLLVMValue(llvm.Type{}, mem)
|
||||
if llvmValue.Type() != llvmType {
|
||||
llvmValue = llvm.ConstBitCast(llvmValue, llvmType)
|
||||
}
|
||||
return llvmValue, nil
|
||||
return llvmValue
|
||||
}
|
||||
// This is either a null pointer or a raw pointer for memory-mapped I/O
|
||||
// (such as 0xe000ed00).
|
||||
ptr := rawValue{v.buf[:mem.r.pointerSize]}.Uint()
|
||||
if ptr == 0 {
|
||||
// Null pointer.
|
||||
return llvm.ConstNull(llvmType), nil
|
||||
return llvm.ConstNull(llvmType)
|
||||
}
|
||||
var ptrValue llvm.Value // the underlying int
|
||||
switch mem.r.pointerSize {
|
||||
@@ -1213,19 +1164,19 @@ func (v rawValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value,
|
||||
case 2:
|
||||
ptrValue = llvm.ConstInt(llvmType.Context().Int16Type(), ptr, false)
|
||||
default:
|
||||
return llvm.Value{}, errors.New("interp: unknown pointer size")
|
||||
panic("unknown pointer size")
|
||||
}
|
||||
return llvm.ConstIntToPtr(ptrValue, llvmType), nil
|
||||
return llvm.ConstIntToPtr(ptrValue, llvmType)
|
||||
case llvm.DoubleTypeKind:
|
||||
b := rawValue{v.buf[:8]}.Uint()
|
||||
f := math.Float64frombits(b)
|
||||
return llvm.ConstFloat(llvmType, f), nil
|
||||
return llvm.ConstFloat(llvmType, f)
|
||||
case llvm.FloatTypeKind:
|
||||
b := uint32(rawValue{v.buf[:4]}.Uint())
|
||||
f := math.Float32frombits(b)
|
||||
return llvm.ConstFloat(llvmType, float64(f)), nil
|
||||
return llvm.ConstFloat(llvmType, float64(f))
|
||||
default:
|
||||
return llvm.Value{}, errors.New("interp: todo: raw value to LLVM value: " + llvmType.String())
|
||||
panic("todo: raw value to LLVM value: " + llvmType.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1414,8 +1365,8 @@ func (v localValue) Int() int64 {
|
||||
panic("interp: localValue.Int")
|
||||
}
|
||||
|
||||
func (v localValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Value, error) {
|
||||
return v.value, nil
|
||||
func (v localValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) llvm.Value {
|
||||
return v.value
|
||||
}
|
||||
|
||||
func (r *runner) getValue(llvmValue llvm.Value) value {
|
||||
|
||||
Vendored
+6
-5
@@ -1,16 +1,17 @@
|
||||
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
|
||||
target triple = "x86_64--linux"
|
||||
|
||||
%runtime.typecodeID = type { %runtime.typecodeID*, i64, %runtime.interfaceMethodInfo* }
|
||||
%runtime.typecodeID = type { %runtime.typecodeID*, i64 }
|
||||
%runtime.interfaceMethodInfo = type { i8*, i64 }
|
||||
%runtime.typeInInterface = type { %runtime.typecodeID*, %runtime.interfaceMethodInfo* }
|
||||
|
||||
@main.v1 = global i1 0
|
||||
@"reflect/types.type:named:main.foo" = private constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:basic:int", i64 0, %runtime.interfaceMethodInfo* null }
|
||||
@"reflect/types.type:named:main.foo$id" = external constant i8
|
||||
@"reflect/types.type:named:main.foo" = private constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:basic:int", i64 0 }
|
||||
@"reflect/types.type:basic:int" = external constant %runtime.typecodeID
|
||||
@"typeInInterface:reflect/types.type:named:main.foo" = private constant %runtime.typeInInterface { %runtime.typecodeID* @"reflect/types.type:named:main.foo", %runtime.interfaceMethodInfo* null }
|
||||
|
||||
|
||||
declare i1 @runtime.typeAssert(i64, i8*, i8*, i8*)
|
||||
declare i1 @runtime.typeAssert(i64, %runtime.typecodeID*, i8*, i8*)
|
||||
|
||||
define void @runtime.initAll() unnamed_addr {
|
||||
entry:
|
||||
@@ -21,7 +22,7 @@ entry:
|
||||
define internal void @main.init() unnamed_addr {
|
||||
entry:
|
||||
; Test type asserts.
|
||||
%typecode = call i1 @runtime.typeAssert(i64 ptrtoint (%runtime.typecodeID* @"reflect/types.type:named:main.foo" to i64), i8* @"reflect/types.type:named:main.foo$id", i8* undef, i8* null)
|
||||
%typecode = call i1 @runtime.typeAssert(i64 ptrtoint (%runtime.typeInInterface* @"typeInInterface:reflect/types.type:named:main.foo" to i64), %runtime.typecodeID* @"reflect/types.type:named:main.foo", i8* undef, i8* null)
|
||||
store i1 %typecode, i1* @main.v1
|
||||
ret void
|
||||
}
|
||||
|
||||
+6
-6
@@ -382,14 +382,14 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
|
||||
|
||||
// Do CGo processing.
|
||||
if len(p.CgoFiles) != 0 {
|
||||
var initialCFlags []string
|
||||
initialCFlags = append(initialCFlags, p.program.config.CFlags()...)
|
||||
initialCFlags = append(initialCFlags, "-I"+p.Dir)
|
||||
var cflags []string
|
||||
cflags = append(cflags, p.program.config.CFlags()...)
|
||||
cflags = append(cflags, "-I"+p.Dir)
|
||||
if p.program.clangHeaders != "" {
|
||||
initialCFlags = append(initialCFlags, "-Xclang", "-internal-isystem", "-Xclang", p.program.clangHeaders)
|
||||
cflags = append(cflags, "-Xclang", "-internal-isystem", "-Xclang", p.program.clangHeaders)
|
||||
}
|
||||
generated, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.program.fset, initialCFlags)
|
||||
p.CFlags = append(initialCFlags, cflags...)
|
||||
p.CFlags = cflags
|
||||
generated, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.program.fset, cflags)
|
||||
for path, hash := range accessedFiles {
|
||||
p.FileHashes[path] = hash
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ func copyFile(src, dst string) error {
|
||||
// executeCommand is a simple wrapper to exec.Cmd
|
||||
func executeCommand(options *compileopts.Options, name string, arg ...string) *exec.Cmd {
|
||||
if options.PrintCommands {
|
||||
fmt.Printf("%s %s\n", name, strings.Join(arg, " "))
|
||||
fmt.Printf("%s %s\n ", name, strings.Join(arg, " "))
|
||||
}
|
||||
return exec.Command(name, arg...)
|
||||
}
|
||||
@@ -344,9 +344,8 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gdb, err := config.Target.LookupGDB()
|
||||
if err != nil {
|
||||
return err
|
||||
if config.Target.GDB == "" {
|
||||
return errors.New("gdb not configured in the target specification")
|
||||
}
|
||||
|
||||
return builder.Build(pkgName, "", config, func(result builder.BuildResult) error {
|
||||
@@ -491,7 +490,7 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||
for _, cmd := range gdbCommands {
|
||||
params = append(params, "-ex", cmd)
|
||||
}
|
||||
cmd := executeCommand(config.Options, gdb, params...)
|
||||
cmd := executeCommand(config.Options, config.Target.GDB, params...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
+19
-11
@@ -15,6 +15,7 @@ import (
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -58,10 +59,7 @@ func TestCompiler(t *testing.T) {
|
||||
t.Run("Host", func(t *testing.T) {
|
||||
runPlatTests("", matches, t)
|
||||
if runtime.GOOS == "darwin" {
|
||||
runTest("testdata/libc/filesystem.go", "", t,
|
||||
nil, nil)
|
||||
runTest("testdata/libc/env.go", "", t,
|
||||
[]string{"ENV1=VALUE1", "ENV2=VALUE2"}, nil)
|
||||
runTest("testdata/libc/env.go", "", t, []string{"ENV1=VALUE1", "ENV2=VALUE2"}...)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -109,9 +107,7 @@ func TestCompiler(t *testing.T) {
|
||||
|
||||
t.Run("WASI", func(t *testing.T) {
|
||||
runPlatTests("wasi", matches, t)
|
||||
runTest("testdata/libc/env.go", "wasi", t,
|
||||
[]string{"--env", "ENV1=VALUE1", "--env", "ENV2=VALUE2"}, nil)
|
||||
runTest("testdata/libc/filesystem.go", "wasi", t, nil, []string{"--dir=."})
|
||||
runTest("testdata/libc/env.go", "wasi", t, []string{"ENV1=VALUE1", "ENV2=VALUE2"}...)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -123,12 +119,24 @@ func runPlatTests(target string, matches []string, t *testing.T) {
|
||||
path := path // redefine to avoid race condition
|
||||
t.Run(filepath.Base(path), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
runTest(path, target, t, nil, nil)
|
||||
runTest(path, target, t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runTest(path, target string, t *testing.T, environmentVars []string, additionalArgs []string) {
|
||||
// Due to some problems with LLD, we cannot run links in parallel, or in parallel with compiles.
|
||||
// Therefore, we put a lock around builds and run everything else in parallel.
|
||||
var buildLock sync.Mutex
|
||||
|
||||
// runBuild is a thread-safe wrapper around Build.
|
||||
func runBuild(src, out string, opts *compileopts.Options) error {
|
||||
buildLock.Lock()
|
||||
defer buildLock.Unlock()
|
||||
|
||||
return Build(src, out, opts)
|
||||
}
|
||||
|
||||
func runTest(path, target string, t *testing.T, environmentVars ...string) {
|
||||
// Get the expected output for this test.
|
||||
txtpath := path[:len(path)-3] + ".txt"
|
||||
if path[len(path)-1] == os.PathSeparator {
|
||||
@@ -164,7 +172,7 @@ func runTest(path, target string, t *testing.T, environmentVars []string, additi
|
||||
}
|
||||
|
||||
binary := filepath.Join(tmpdir, "test")
|
||||
err = Build("./"+path, binary, config)
|
||||
err = runBuild("./"+path, binary, config)
|
||||
if err != nil {
|
||||
printCompilerError(t.Log, err)
|
||||
t.Fail()
|
||||
@@ -187,7 +195,7 @@ func runTest(path, target string, t *testing.T, environmentVars []string, additi
|
||||
cmd = exec.Command(binary)
|
||||
} else {
|
||||
args := append(spec.Emulator[1:], binary)
|
||||
cmd = exec.Command(spec.Emulator[0], append(args, additionalArgs...)...)
|
||||
cmd = exec.Command(spec.Emulator[0], args...)
|
||||
}
|
||||
|
||||
if len(spec.Emulator) != 0 && spec.Emulator[0] == "wasmtime" {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
type rawState uint8
|
||||
|
||||
//export llvm.coro.resume
|
||||
func coroResume(*rawState)
|
||||
func (s *rawState) resume()
|
||||
|
||||
type state struct{ *rawState }
|
||||
|
||||
@@ -20,7 +20,7 @@ func noopState() *rawState
|
||||
|
||||
// Resume the task until it pauses or completes.
|
||||
func (t *Task) Resume() {
|
||||
coroResume(t.state.rawState)
|
||||
t.state.resume()
|
||||
}
|
||||
|
||||
// setState is used by the compiler to set the state of the function at the beginning of a function call.
|
||||
|
||||
@@ -32,7 +32,7 @@ func init() {
|
||||
|
||||
// I2C on the Arduino Nano 33.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM4_I2CM,
|
||||
SERCOM: 4,
|
||||
}
|
||||
|
||||
@@ -23,12 +23,12 @@ func init() {
|
||||
// I2C on the Circuit Playground Express.
|
||||
var (
|
||||
// external device
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM5_I2CM,
|
||||
SERCOM: 5,
|
||||
}
|
||||
// internal device
|
||||
I2C1 = &I2C{
|
||||
I2C1 = I2C{
|
||||
Bus: sam.SERCOM1_I2CM,
|
||||
SERCOM: 1,
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ const (
|
||||
|
||||
// I2C on the Feather M0.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM3_I2CM,
|
||||
SERCOM: 3,
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func init() {
|
||||
|
||||
// I2C on the Feather M4.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -120,22 +120,19 @@ const (
|
||||
|
||||
var (
|
||||
UART1 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART3,
|
||||
TxAltFuncSelector: AF7_USART1_2_3,
|
||||
RxAltFuncSelector: AF7_USART1_2_3,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART3,
|
||||
AltFuncSelector: AF7_USART1_2_3,
|
||||
}
|
||||
UART2 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART6,
|
||||
TxAltFuncSelector: AF8_USART4_5_6,
|
||||
RxAltFuncSelector: AF8_USART4_5_6,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART6,
|
||||
AltFuncSelector: AF8_USART4_5_6,
|
||||
}
|
||||
UART3 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART1,
|
||||
TxAltFuncSelector: AF7_USART1_2_3,
|
||||
RxAltFuncSelector: AF7_USART1_2_3,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART1,
|
||||
AltFuncSelector: AF7_USART1_2_3,
|
||||
}
|
||||
UART0 = UART1
|
||||
)
|
||||
@@ -230,15 +227,15 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
I2C1 = &I2C{
|
||||
I2C1 = I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: AF4_I2C1_2_3,
|
||||
}
|
||||
I2C2 = &I2C{
|
||||
I2C2 = I2C{
|
||||
Bus: stm32.I2C2,
|
||||
AltFuncSelector: AF4_I2C1_2_3,
|
||||
}
|
||||
I2C3 = &I2C{
|
||||
I2C3 = I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: AF4_I2C1_2_3,
|
||||
}
|
||||
|
||||
@@ -40,11 +40,11 @@ var (
|
||||
|
||||
// I2C on the Grand Central M4
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM3_I2CM,
|
||||
SERCOM: 3,
|
||||
}
|
||||
I2C1 = &I2C{
|
||||
I2C1 = I2C{
|
||||
Bus: sam.SERCOM6_I2CM,
|
||||
SERCOM: 6,
|
||||
}
|
||||
|
||||
@@ -10,3 +10,10 @@ var (
|
||||
Bus: sifive.QSPI1,
|
||||
}
|
||||
)
|
||||
|
||||
// I2C on the HiFive1 rev B.
|
||||
var (
|
||||
I2C0 = I2C{
|
||||
Bus: sifive.I2C0,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -75,7 +75,7 @@ const (
|
||||
|
||||
// I2C on the ItsyBitsy M0.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM3_I2CM,
|
||||
SERCOM: 3,
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func init() {
|
||||
|
||||
// I2C on the ItsyBitsy M4.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -54,18 +54,16 @@ var (
|
||||
|
||||
// Console UART (LPUSART1)
|
||||
UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.LPUART1,
|
||||
TxAltFuncSelector: 6,
|
||||
RxAltFuncSelector: 6,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.LPUART1,
|
||||
AltFuncSelector: 6,
|
||||
}
|
||||
|
||||
// Gps UART
|
||||
UART1 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART1,
|
||||
TxAltFuncSelector: 0,
|
||||
RxAltFuncSelector: 0,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART1,
|
||||
AltFuncSelector: 0,
|
||||
}
|
||||
|
||||
// SPI
|
||||
|
||||
@@ -13,3 +13,16 @@ var (
|
||||
Bus: kendryte.SPI1,
|
||||
}
|
||||
)
|
||||
|
||||
// I2C on the MAix Bit.
|
||||
var (
|
||||
I2C0 = I2C{
|
||||
Bus: kendryte.I2C0,
|
||||
}
|
||||
I2C1 = I2C{
|
||||
Bus: kendryte.I2C1,
|
||||
}
|
||||
I2C2 = I2C{
|
||||
Bus: kendryte.I2C2,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -29,7 +29,7 @@ func init() {
|
||||
|
||||
// I2C on the MatrixPortal M4
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM5_I2CM,
|
||||
SERCOM: 5,
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func init() {
|
||||
|
||||
// I2C on the Metro M4.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM5_I2CM,
|
||||
SERCOM: 5,
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ const (
|
||||
// USB CDC identifiers
|
||||
const (
|
||||
usb_STRING_PRODUCT = "Makerdiary nRF52840 MDK USB Dongle"
|
||||
usb_STRING_MANUFACTURER = "Nordic Semiconductor ASA"
|
||||
usb_STRING_MANUFACTURER = "Makerdiary"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -39,7 +39,7 @@ const (
|
||||
// USB CDC identifiers
|
||||
const (
|
||||
usb_STRING_PRODUCT = "Makerdiary nRF52840 MDK"
|
||||
usb_STRING_MANUFACTURER = "Nordic Semiconductor ASA"
|
||||
usb_STRING_MANUFACTURER = "Makerdiary"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -33,10 +33,9 @@ var (
|
||||
// debugger to be exposed as virtual COM port over USB on Nucleo boards.
|
||||
// Both UART0 and UART1 refer to USART2.
|
||||
UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART3,
|
||||
TxAltFuncSelector: UART_ALT_FN,
|
||||
RxAltFuncSelector: UART_ALT_FN,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART3,
|
||||
AltFuncSelector: UART_ALT_FN,
|
||||
}
|
||||
UART1 = &UART0
|
||||
)
|
||||
@@ -54,15 +53,6 @@ const (
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
I2C0_SCL_PIN = PB8
|
||||
I2C0_SDA_PIN = PB9
|
||||
)
|
||||
|
||||
var (
|
||||
// I2C1 is documented, alias to I2C0 as well
|
||||
I2C1 = &I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: 4,
|
||||
}
|
||||
I2C0 = I2C1
|
||||
SCL_PIN = PB6
|
||||
SDA_PIN = PB7
|
||||
)
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
// +build nucleol432kc
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"runtime/interrupt"
|
||||
)
|
||||
|
||||
const (
|
||||
LED = LED_BUILTIN
|
||||
LED_BUILTIN = LED_GREEN
|
||||
LED_GREEN = PB3
|
||||
)
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
// PA2 and PA15 are connected to the ST-Link Virtual Com Port (VCP)
|
||||
UART_TX_PIN = PA2
|
||||
UART_RX_PIN = PA15
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
// With default solder bridge settings:
|
||||
// PB6 / Arduino D5 / CN3 Pin 8 is SCL
|
||||
// PB7 / Arduino D4 / CN3 Pin 7 is SDA
|
||||
I2C0_SCL_PIN = PB6
|
||||
I2C0_SDA_PIN = PB7
|
||||
)
|
||||
|
||||
var (
|
||||
// USART2 is the hardware serial port connected to the onboard ST-LINK
|
||||
// debugger to be exposed as virtual COM port over USB on Nucleo boards.
|
||||
// Both UART0 and UART1 refer to USART2.
|
||||
UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART2,
|
||||
TxAltFuncSelector: 7,
|
||||
RxAltFuncSelector: 3,
|
||||
}
|
||||
UART1 = &UART0
|
||||
)
|
||||
|
||||
var (
|
||||
// I2C1 is documented, alias to I2C0 as well
|
||||
I2C1 = &I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: 4,
|
||||
}
|
||||
I2C0 = I2C1
|
||||
)
|
||||
|
||||
func init() {
|
||||
UART0.Interrupt = interrupt.New(stm32.IRQ_USART2, UART0.handleInterrupt)
|
||||
}
|
||||
@@ -33,28 +33,13 @@ var (
|
||||
// debugger to be exposed as virtual COM port over USB on Nucleo boards.
|
||||
// Both UART0 and UART1 refer to LPUART1.
|
||||
UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.LPUART1,
|
||||
TxAltFuncSelector: UART_ALT_FN,
|
||||
RxAltFuncSelector: UART_ALT_FN,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.LPUART1,
|
||||
AltFuncSelector: UART_ALT_FN,
|
||||
}
|
||||
UART1 = &UART0
|
||||
)
|
||||
|
||||
const (
|
||||
I2C0_SCL_PIN = PB8
|
||||
I2C0_SDA_PIN = PB9
|
||||
)
|
||||
|
||||
var (
|
||||
// I2C1 is documented, alias to I2C0 as well
|
||||
I2C1 = &I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: 4,
|
||||
}
|
||||
I2C0 = I2C1
|
||||
)
|
||||
|
||||
func init() {
|
||||
UART0.Interrupt = interrupt.New(stm32.IRQ_LPUART1, UART0.handleInterrupt)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func init() {
|
||||
|
||||
// I2C on the P1AM-100.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM0_I2CM,
|
||||
SERCOM: 0,
|
||||
}
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
// +build pca10059
|
||||
|
||||
package machine
|
||||
|
||||
// The PCA10040 has a low-frequency (32kHz) crystal oscillator on board.
|
||||
const HasLowFrequencyCrystal = true
|
||||
|
||||
// LEDs on the PCA10059 (nRF52840 dongle)
|
||||
const (
|
||||
LED Pin = LED1
|
||||
LED1 Pin = 6
|
||||
LED2 Pin = 8
|
||||
LED3 Pin = (1 << 5) | 9
|
||||
LED4 Pin = 12
|
||||
)
|
||||
|
||||
// Buttons on the PCA10059 (nRF52840 dongle)
|
||||
const (
|
||||
BUTTON Pin = BUTTON1
|
||||
BUTTON1 Pin = (1 << 5) | 6
|
||||
)
|
||||
|
||||
// ADC pins
|
||||
const (
|
||||
ADC1 Pin = 2
|
||||
ADC2 Pin = 4
|
||||
ADC3 Pin = 29
|
||||
ADC4 Pin = 31
|
||||
)
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
UART_TX_PIN Pin = NoPin
|
||||
UART_RX_PIN Pin = NoPin
|
||||
)
|
||||
|
||||
// UART0 is the USB device
|
||||
var (
|
||||
UART0 = USB
|
||||
)
|
||||
|
||||
// I2C pins (unused)
|
||||
const (
|
||||
SDA_PIN = NoPin
|
||||
SCL_PIN = NoPin
|
||||
)
|
||||
|
||||
// SPI pins (unused)
|
||||
const (
|
||||
SPI0_SCK_PIN = NoPin
|
||||
SPI0_SDO_PIN = NoPin
|
||||
SPI0_SDI_PIN = NoPin
|
||||
)
|
||||
|
||||
// USB CDC identifiers
|
||||
const (
|
||||
usb_STRING_PRODUCT = "nRF52840 Dongle"
|
||||
usb_STRING_MANUFACTURER = "Nordic Semiconductor ASA"
|
||||
)
|
||||
|
||||
var (
|
||||
usb_VID uint16 = 0x1915
|
||||
usb_PID uint16 = 0xCAFE
|
||||
)
|
||||
@@ -28,7 +28,7 @@ func init() {
|
||||
|
||||
// I2C on the ItsyBitsy M4.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ const (
|
||||
|
||||
// I2C on the PyGamer.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ func init() {
|
||||
|
||||
// I2C on the PyPortal.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM5_I2CM,
|
||||
SERCOM: 5,
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ const (
|
||||
|
||||
// I2C on the QT Py M0.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -28,10 +28,9 @@ const (
|
||||
|
||||
var (
|
||||
UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART2,
|
||||
TxAltFuncSelector: AF7_USART1_2_3,
|
||||
RxAltFuncSelector: AF7_USART1_2_3,
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART2,
|
||||
AltFuncSelector: AF7_USART1_2_3,
|
||||
}
|
||||
UART1 = &UART0
|
||||
)
|
||||
@@ -74,7 +73,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: stm32.I2C1,
|
||||
AltFuncSelector: AF4_I2C1_2_3,
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ const (
|
||||
|
||||
// I2C on the Trinket M0.
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
@@ -29,12 +29,12 @@ func init() {
|
||||
|
||||
// I2C on the Wio Terminal
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM4_I2CM,
|
||||
SERCOM: 4,
|
||||
}
|
||||
|
||||
I2C1 = &I2C{
|
||||
I2C1 = I2C{
|
||||
Bus: sam.SERCOM4_I2CM,
|
||||
SERCOM: 4,
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ const (
|
||||
|
||||
// I2C on the Xiao
|
||||
var (
|
||||
I2C0 = &I2C{
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
// +build atmega nrf sam stm32,!stm32l0 fe310 k210
|
||||
// +build avr nrf sam stm32,!stm32f7x2,!stm32l5x2,!stm32l0 fe310 k210
|
||||
|
||||
package machine
|
||||
|
||||
@@ -29,7 +29,7 @@ var (
|
||||
// Many I2C-compatible devices are organized in terms of registers. This method
|
||||
// is a shortcut to easily write to such registers. Also, it only works for
|
||||
// devices with 7-bit addresses, which is the vast majority.
|
||||
func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error {
|
||||
func (i2c I2C) WriteRegister(address uint8, register uint8, data []byte) error {
|
||||
buf := make([]uint8, len(data)+1)
|
||||
buf[0] = register
|
||||
copy(buf[1:], data)
|
||||
@@ -42,6 +42,6 @@ func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error
|
||||
// Many I2C-compatible devices are organized in terms of registers. This method
|
||||
// is a shortcut to easily read such registers. Also, it only works for devices
|
||||
// with 7-bit addresses, which is the vast majority.
|
||||
func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error {
|
||||
func (i2c I2C) ReadRegister(address uint8, register uint8, data []byte) error {
|
||||
return i2c.Tx(uint16(address), []byte{register}, data)
|
||||
}
|
||||
|
||||
@@ -9,20 +9,13 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// I2C on AVR.
|
||||
type I2C struct {
|
||||
}
|
||||
|
||||
// I2C0 is the only I2C interface on most AVRs.
|
||||
var I2C0 *I2C = nil
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
type I2CConfig struct {
|
||||
Frequency uint32
|
||||
}
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -49,7 +42,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
if len(w) != 0 {
|
||||
i2c.start(uint8(addr), true) // start transmission for writing
|
||||
for _, b := range w {
|
||||
@@ -70,7 +63,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
}
|
||||
|
||||
// start starts an I2C communication session.
|
||||
func (i2c *I2C) start(address uint8, write bool) {
|
||||
func (i2c I2C) start(address uint8, write bool) {
|
||||
// Clear TWI interrupt flag, put start condition on SDA, and enable TWI.
|
||||
avr.TWCR.Set((avr.TWCR_TWINT | avr.TWCR_TWSTA | avr.TWCR_TWEN))
|
||||
|
||||
@@ -87,7 +80,7 @@ func (i2c *I2C) start(address uint8, write bool) {
|
||||
}
|
||||
|
||||
// stop ends an I2C communication session.
|
||||
func (i2c *I2C) stop() {
|
||||
func (i2c I2C) stop() {
|
||||
// Send stop condition.
|
||||
avr.TWCR.Set(avr.TWCR_TWEN | avr.TWCR_TWINT | avr.TWCR_TWSTO)
|
||||
|
||||
@@ -97,7 +90,7 @@ func (i2c *I2C) stop() {
|
||||
}
|
||||
|
||||
// writeByte writes a single byte to the I2C bus.
|
||||
func (i2c *I2C) writeByte(data byte) {
|
||||
func (i2c I2C) writeByte(data byte) {
|
||||
// Write data to register.
|
||||
avr.TWDR.Set(data)
|
||||
|
||||
@@ -110,7 +103,7 @@ func (i2c *I2C) writeByte(data byte) {
|
||||
}
|
||||
|
||||
// readByte reads a single byte from the I2C bus.
|
||||
func (i2c *I2C) readByte() byte {
|
||||
func (i2c I2C) readByte() byte {
|
||||
// Clear TWI interrupt flag and enable TWI.
|
||||
avr.TWCR.Set(avr.TWCR_TWEN | avr.TWCR_TWINT | avr.TWCR_TWEA)
|
||||
|
||||
|
||||
@@ -669,7 +669,7 @@ const (
|
||||
const i2cTimeout = 1000
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -725,7 +725,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the I2C.
|
||||
func (i2c *I2C) SetBaudRate(br uint32) {
|
||||
func (i2c I2C) SetBaudRate(br uint32) {
|
||||
// Synchronous arithmetic baudrate, via Arduino SAMD implementation:
|
||||
// SystemCoreClock / ( 2 * baudrate) - 5 - (((SystemCoreClock / 1000000) * WIRE_RISE_TIME_NANOSECONDS) / (2 * 1000));
|
||||
baud := CPUFrequency()/(2*br) - 5 - (((CPUFrequency() / 1000000) * riseTimeNanoseconds) / (2 * 1000))
|
||||
@@ -735,7 +735,7 @@ func (i2c *I2C) SetBaudRate(br uint32) {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
var err error
|
||||
if len(w) != 0 {
|
||||
// send start/address for write
|
||||
@@ -812,7 +812,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
}
|
||||
|
||||
// WriteByte writes a single byte to the I2C bus.
|
||||
func (i2c *I2C) WriteByte(data byte) error {
|
||||
func (i2c I2C) WriteByte(data byte) error {
|
||||
// Send data byte
|
||||
i2c.Bus.DATA.Set(data)
|
||||
|
||||
@@ -837,7 +837,7 @@ func (i2c *I2C) WriteByte(data byte) error {
|
||||
}
|
||||
|
||||
// sendAddress sends the address and start signal
|
||||
func (i2c *I2C) sendAddress(address uint16, write bool) error {
|
||||
func (i2c I2C) sendAddress(address uint16, write bool) error {
|
||||
data := (address << 1)
|
||||
if !write {
|
||||
data |= 1 // set read flag
|
||||
@@ -857,7 +857,7 @@ func (i2c *I2C) sendAddress(address uint16, write bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) signalStop() error {
|
||||
func (i2c I2C) signalStop() error {
|
||||
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop command
|
||||
timeout := i2cTimeout
|
||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||
@@ -869,7 +869,7 @@ func (i2c *I2C) signalStop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) signalRead() error {
|
||||
func (i2c I2C) signalRead() error {
|
||||
i2c.Bus.CTRLB.SetBits(wireCmdRead << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Read command
|
||||
timeout := i2cTimeout
|
||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||
@@ -881,7 +881,7 @@ func (i2c *I2C) signalRead() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) readByte() byte {
|
||||
func (i2c I2C) readByte() byte {
|
||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_SB) {
|
||||
}
|
||||
return byte(i2c.Bus.DATA.Get())
|
||||
@@ -1646,7 +1646,6 @@ func (usbcdc *USBCDC) Flush() error {
|
||||
|
||||
if usbcdc.waitTxc {
|
||||
// waiting for the next flush(), because the transmission is not complete
|
||||
usbcdc.waitTxcRetryCount++
|
||||
return nil
|
||||
}
|
||||
usbcdc.waitTxc = true
|
||||
@@ -1701,7 +1700,7 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
|
||||
mask := interrupt.Disable()
|
||||
UART0.waitTxc = false
|
||||
UART0.waitTxcRetryCount = 0
|
||||
UART0.TxIdx.Set(0)
|
||||
usbcdc.TxIdx.Set(0)
|
||||
usbLineInfo.lineState = 0
|
||||
interrupt.Restore(mask)
|
||||
break
|
||||
@@ -1869,7 +1868,6 @@ func handleUSB(intr interrupt.Interrupt) {
|
||||
|
||||
// Start of frame
|
||||
if (flags & sam.USB_DEVICE_INTFLAG_SOF) > 0 {
|
||||
UART0.Flush()
|
||||
// if you want to blink LED showing traffic, this would be the place...
|
||||
}
|
||||
|
||||
@@ -1933,7 +1931,13 @@ func handleUSB(intr interrupt.Interrupt) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i == usb_CDC_ENDPOINT_IN && UART0.waitTxc {
|
||||
UART0.waitTxcRetryCount++
|
||||
}
|
||||
}
|
||||
|
||||
UART0.Flush()
|
||||
}
|
||||
|
||||
func initEndpoint(ep, config uint32) {
|
||||
|
||||
@@ -1104,7 +1104,7 @@ const (
|
||||
const i2cTimeout = 1000
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -1163,7 +1163,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the I2C.
|
||||
func (i2c *I2C) SetBaudRate(br uint32) {
|
||||
func (i2c I2C) SetBaudRate(br uint32) {
|
||||
// Synchronous arithmetic baudrate, via Adafruit SAMD51 implementation:
|
||||
// sercom->I2CM.BAUD.bit.BAUD = SERCOM_FREQ_REF / ( 2 * baudrate) - 1 ;
|
||||
baud := SERCOM_FREQ_REF/(2*br) - 1
|
||||
@@ -1173,7 +1173,7 @@ func (i2c *I2C) SetBaudRate(br uint32) {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
var err error
|
||||
if len(w) != 0 {
|
||||
// send start/address for write
|
||||
@@ -1250,7 +1250,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
}
|
||||
|
||||
// WriteByte writes a single byte to the I2C bus.
|
||||
func (i2c *I2C) WriteByte(data byte) error {
|
||||
func (i2c I2C) WriteByte(data byte) error {
|
||||
// Send data byte
|
||||
i2c.Bus.DATA.Set(data)
|
||||
|
||||
@@ -1275,7 +1275,7 @@ func (i2c *I2C) WriteByte(data byte) error {
|
||||
}
|
||||
|
||||
// sendAddress sends the address and start signal
|
||||
func (i2c *I2C) sendAddress(address uint16, write bool) error {
|
||||
func (i2c I2C) sendAddress(address uint16, write bool) error {
|
||||
data := (address << 1)
|
||||
if !write {
|
||||
data |= 1 // set read flag
|
||||
@@ -1295,7 +1295,7 @@ func (i2c *I2C) sendAddress(address uint16, write bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) signalStop() error {
|
||||
func (i2c I2C) signalStop() error {
|
||||
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop command
|
||||
timeout := i2cTimeout
|
||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||
@@ -1307,7 +1307,7 @@ func (i2c *I2C) signalStop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) signalRead() error {
|
||||
func (i2c I2C) signalRead() error {
|
||||
i2c.Bus.CTRLB.SetBits(wireCmdRead << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Read command
|
||||
timeout := i2cTimeout
|
||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||
@@ -1319,7 +1319,7 @@ func (i2c *I2C) signalRead() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) readByte() byte {
|
||||
func (i2c I2C) readByte() byte {
|
||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_SB) {
|
||||
}
|
||||
return byte(i2c.Bus.DATA.Get())
|
||||
@@ -1850,7 +1850,6 @@ func (usbcdc *USBCDC) Flush() error {
|
||||
|
||||
if usbcdc.waitTxc {
|
||||
// waiting for the next flush(), because the transmission is not complete
|
||||
usbcdc.waitTxcRetryCount++
|
||||
return nil
|
||||
}
|
||||
usbcdc.waitTxc = true
|
||||
@@ -1905,7 +1904,7 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
|
||||
mask := interrupt.Disable()
|
||||
UART0.waitTxc = false
|
||||
UART0.waitTxcRetryCount = 0
|
||||
UART0.TxIdx.Set(0)
|
||||
usbcdc.TxIdx.Set(0)
|
||||
usbLineInfo.lineState = 0
|
||||
interrupt.Restore(mask)
|
||||
break
|
||||
@@ -2075,7 +2074,6 @@ func handleUSBIRQ(interrupt.Interrupt) {
|
||||
|
||||
// Start of frame
|
||||
if (flags & sam.USB_DEVICE_INTFLAG_SOF) > 0 {
|
||||
UART0.Flush()
|
||||
// if you want to blink LED showing traffic, this would be the place...
|
||||
}
|
||||
|
||||
@@ -2139,7 +2137,13 @@ func handleUSBIRQ(interrupt.Interrupt) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i == usb_CDC_ENDPOINT_IN && UART0.waitTxc {
|
||||
UART0.waitTxcRetryCount++
|
||||
}
|
||||
}
|
||||
|
||||
UART0.Flush()
|
||||
}
|
||||
|
||||
func initEndpoint(ep, config uint32) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// +build avr,attiny
|
||||
|
||||
package machine
|
||||
|
||||
// Tx is a dummy implementation. I2C has not been implemented for ATtiny
|
||||
// devices.
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
return nil
|
||||
}
|
||||
@@ -141,3 +141,10 @@ func (a ADC) Get() uint16 {
|
||||
|
||||
return uint16(avr.ADCL.Get()) | uint16(avr.ADCH.Get())<<8
|
||||
}
|
||||
|
||||
// I2C on AVR.
|
||||
type I2C struct {
|
||||
}
|
||||
|
||||
// I2C0 is the only I2C interface on most AVRs.
|
||||
var I2C0 = I2C{}
|
||||
|
||||
@@ -5,7 +5,6 @@ package machine
|
||||
import (
|
||||
"device/sifive"
|
||||
"runtime/interrupt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
@@ -186,13 +185,9 @@ func (spi SPI) Transfer(w byte) (byte, error) {
|
||||
|
||||
// I2C on the FE310-G002.
|
||||
type I2C struct {
|
||||
Bus sifive.I2C_Type
|
||||
Bus *sifive.I2C_Type
|
||||
}
|
||||
|
||||
var (
|
||||
I2C0 = (*I2C)(unsafe.Pointer(sifive.I2C0))
|
||||
)
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
type I2CConfig struct {
|
||||
Frequency uint32
|
||||
@@ -201,7 +196,7 @@ type I2CConfig struct {
|
||||
}
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
var i2cClockFrequency uint32 = 32000000
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -233,7 +228,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
var err error
|
||||
if len(w) != 0 {
|
||||
// send start/address for write
|
||||
@@ -281,7 +276,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
}
|
||||
|
||||
// Writes a single byte to the I2C bus.
|
||||
func (i2c *I2C) writeByte(data byte) error {
|
||||
func (i2c I2C) writeByte(data byte) error {
|
||||
// Send data byte
|
||||
i2c.Bus.TXR_RXR.Set(uint32(data))
|
||||
|
||||
@@ -300,7 +295,7 @@ func (i2c *I2C) writeByte(data byte) error {
|
||||
}
|
||||
|
||||
// Reads a single byte from the I2C bus.
|
||||
func (i2c *I2C) readByte() byte {
|
||||
func (i2c I2C) readByte() byte {
|
||||
i2c.Bus.CR_SR.Set(sifive.I2C_CR_RD)
|
||||
|
||||
// wait until transmission complete
|
||||
@@ -311,7 +306,7 @@ func (i2c *I2C) readByte() byte {
|
||||
}
|
||||
|
||||
// Sends the address and start signal.
|
||||
func (i2c *I2C) sendAddress(address uint16, write bool) error {
|
||||
func (i2c I2C) sendAddress(address uint16, write bool) error {
|
||||
data := (address << 1)
|
||||
if !write {
|
||||
data |= 1 // set read flag in transmit register
|
||||
|
||||
@@ -6,7 +6,7 @@ package machine
|
||||
|
||||
var (
|
||||
SPI0 = SPI{0}
|
||||
I2C0 = &I2C{0}
|
||||
I2C0 = I2C{0}
|
||||
UART0 = UART{0}
|
||||
)
|
||||
|
||||
@@ -115,13 +115,13 @@ type I2CConfig struct {
|
||||
}
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
i2cConfigure(i2c.Bus, config.SCL, config.SDA)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
i2cTransfer(i2c.Bus, &w[0], len(w), &r[0], len(r))
|
||||
// TODO: do something with the returned error code.
|
||||
return nil
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"device/riscv"
|
||||
"errors"
|
||||
"runtime/interrupt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
@@ -494,15 +493,9 @@ func (spi SPI) Transfer(w byte) (byte, error) {
|
||||
|
||||
// I2C on the K210.
|
||||
type I2C struct {
|
||||
Bus kendryte.I2C_Type
|
||||
Bus *kendryte.I2C_Type
|
||||
}
|
||||
|
||||
var (
|
||||
I2C0 = (*I2C)(unsafe.Pointer(kendryte.I2C0))
|
||||
I2C1 = (*I2C)(unsafe.Pointer(kendryte.I2C1))
|
||||
I2C2 = (*I2C)(unsafe.Pointer(kendryte.I2C2))
|
||||
)
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
type I2CConfig struct {
|
||||
Frequency uint32
|
||||
@@ -511,7 +504,7 @@ type I2CConfig struct {
|
||||
}
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -525,7 +518,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Enable APB0 clock.
|
||||
kendryte.SYSCTL.CLK_EN_CENT.SetBits(kendryte.SYSCTL_CLK_EN_CENT_APB0_CLK_EN)
|
||||
|
||||
switch &i2c.Bus {
|
||||
switch i2c.Bus {
|
||||
case kendryte.I2C0:
|
||||
// Initialize I2C0 clock.
|
||||
kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_I2C0_CLK_EN)
|
||||
@@ -574,7 +567,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
// Set peripheral address.
|
||||
i2c.Bus.TAR.Set(uint32(addr))
|
||||
// Enable controller.
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"device/nrf"
|
||||
"errors"
|
||||
"runtime/interrupt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -204,13 +203,13 @@ func (uart *UART) handleInterrupt(interrupt.Interrupt) {
|
||||
|
||||
// I2C on the NRF.
|
||||
type I2C struct {
|
||||
Bus nrf.TWI_Type
|
||||
Bus *nrf.TWI_Type
|
||||
}
|
||||
|
||||
// There are 2 I2C interfaces on the NRF.
|
||||
var (
|
||||
I2C0 = (*I2C)(unsafe.Pointer(nrf.TWI0))
|
||||
I2C1 = (*I2C)(unsafe.Pointer(nrf.TWI1))
|
||||
I2C0 = I2C{Bus: nrf.TWI0}
|
||||
I2C1 = I2C{Bus: nrf.TWI1}
|
||||
)
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
@@ -221,7 +220,7 @@ type I2CConfig struct {
|
||||
}
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
@@ -262,7 +261,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Tx does a single I2C transaction at the specified address.
|
||||
// It clocks out the given address, writes the bytes in w, reads back len(r)
|
||||
// bytes and stores them in r, and generates a stop condition on the bus.
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) (err error) {
|
||||
i2c.Bus.ADDRESS.Set(uint32(addr))
|
||||
|
||||
if len(w) != 0 {
|
||||
@@ -300,7 +299,7 @@ cleanUp:
|
||||
// signalStop sends a stop signal when writing or tells the I2C peripheral that
|
||||
// it must generate a stop condition after the next character is retrieved when
|
||||
// reading.
|
||||
func (i2c *I2C) signalStop() {
|
||||
func (i2c I2C) signalStop() {
|
||||
i2c.Bus.TASKS_STOP.Set(1)
|
||||
for i2c.Bus.EVENTS_STOPPED.Get() == 0 {
|
||||
}
|
||||
@@ -308,7 +307,7 @@ func (i2c *I2C) signalStop() {
|
||||
}
|
||||
|
||||
// writeByte writes a single byte to the I2C bus.
|
||||
func (i2c *I2C) writeByte(data byte) error {
|
||||
func (i2c I2C) writeByte(data byte) error {
|
||||
i2c.Bus.TXD.Set(uint32(data))
|
||||
for i2c.Bus.EVENTS_TXDSENT.Get() == 0 {
|
||||
if e := i2c.Bus.EVENTS_ERROR.Get(); e != 0 {
|
||||
@@ -321,7 +320,7 @@ func (i2c *I2C) writeByte(data byte) error {
|
||||
}
|
||||
|
||||
// readByte reads a single byte from the I2C bus.
|
||||
func (i2c *I2C) readByte() (byte, error) {
|
||||
func (i2c I2C) readByte() (byte, error) {
|
||||
for i2c.Bus.EVENTS_RXDREADY.Get() == 0 {
|
||||
if e := i2c.Bus.EVENTS_ERROR.Get(); e != 0 {
|
||||
i2c.Bus.EVENTS_ERROR.Set(0)
|
||||
|
||||
@@ -24,7 +24,7 @@ func (uart UART) setPins(tx, rx Pin) {
|
||||
nrf.UART0.PSELRXD.Set(uint32(rx))
|
||||
}
|
||||
|
||||
func (i2c *I2C) setPins(scl, sda Pin) {
|
||||
func (i2c I2C) setPins(scl, sda Pin) {
|
||||
i2c.Bus.PSELSCL.Set(uint32(scl))
|
||||
i2c.Bus.PSELSDA.Set(uint32(sda))
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (uart UART) setPins(tx, rx Pin) {
|
||||
nrf.UART0.PSELRXD.Set(uint32(rx))
|
||||
}
|
||||
|
||||
func (i2c *I2C) setPins(scl, sda Pin) {
|
||||
func (i2c I2C) setPins(scl, sda Pin) {
|
||||
i2c.Bus.PSELSCL.Set(uint32(scl))
|
||||
i2c.Bus.PSELSDA.Set(uint32(sda))
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func (uart UART) setPins(tx, rx Pin) {
|
||||
nrf.UART0.PSEL.RXD.Set(uint32(rx))
|
||||
}
|
||||
|
||||
func (i2c *I2C) setPins(scl, sda Pin) {
|
||||
func (i2c I2C) setPins(scl, sda Pin) {
|
||||
i2c.Bus.PSEL.SCL.Set(uint32(scl))
|
||||
i2c.Bus.PSEL.SDA.Set(uint32(sda))
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func (uart UART) setPins(tx, rx Pin) {
|
||||
nrf.UART0.PSEL.RXD.Set(uint32(rx))
|
||||
}
|
||||
|
||||
func (i2c *I2C) setPins(scl, sda Pin) {
|
||||
func (i2c I2C) setPins(scl, sda Pin) {
|
||||
i2c.Bus.PSEL.SCL.Set(uint32(scl))
|
||||
i2c.Bus.PSEL.SDA.Set(uint32(sda))
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func (usbcdc *USBCDC) Flush() error {
|
||||
}
|
||||
|
||||
// WriteByte writes a byte of data to the USB CDC interface.
|
||||
func (usbcdc *USBCDC) WriteByte(c byte) error {
|
||||
func (usbcdc USBCDC) WriteByte(c byte) error {
|
||||
// Supposedly to handle problem with Windows USB serial ports?
|
||||
if usbLineInfo.lineState > 0 {
|
||||
ok := false
|
||||
@@ -199,7 +199,6 @@ func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) {
|
||||
if nrf.USBD.EVENTS_SOF.Get() == 1 {
|
||||
nrf.USBD.EVENTS_SOF.Set(0)
|
||||
USB.Flush()
|
||||
// if you want to blink LED showing traffic, this would be the place...
|
||||
}
|
||||
|
||||
// USBD ready event
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
// +build stm32f4 stm32f1
|
||||
// +build stm32,!stm32f7x2,!stm32l5x2,!stm32l0
|
||||
|
||||
package machine
|
||||
|
||||
// I2C implementation for 'older' STM32 MCUs, including the F1 and F4 series
|
||||
// of MCUs.
|
||||
// Peripheral abstraction layer for I2C on the stm32 family
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
@@ -29,7 +28,7 @@ const (
|
||||
flagMSL = 0x00100001
|
||||
)
|
||||
|
||||
func (i2c *I2C) hasFlag(flag uint32) bool {
|
||||
func (i2c I2C) hasFlag(flag uint32) bool {
|
||||
const mask = 0x0000FFFF
|
||||
if uint8(flag>>16) == 1 {
|
||||
return i2c.Bus.SR1.HasBits(flag & mask)
|
||||
@@ -38,18 +37,18 @@ func (i2c *I2C) hasFlag(flag uint32) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func (i2c *I2C) clearFlag(flag uint32) {
|
||||
func (i2c I2C) clearFlag(flag uint32) {
|
||||
const mask = 0x0000FFFF
|
||||
i2c.Bus.SR1.Set(^(flag & mask))
|
||||
}
|
||||
|
||||
// clearFlagADDR reads both status registers to clear any pending ADDR flags.
|
||||
func (i2c *I2C) clearFlagADDR() {
|
||||
func (i2c I2C) clearFlagADDR() {
|
||||
i2c.Bus.SR1.Get()
|
||||
i2c.Bus.SR2.Get()
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitForFlag(flag uint32, set bool) bool {
|
||||
func (i2c I2C) waitForFlag(flag uint32, set bool) bool {
|
||||
const tryMax = 10000
|
||||
hasFlag := false
|
||||
for i := 0; !hasFlag && i < tryMax; i++ {
|
||||
@@ -58,7 +57,7 @@ func (i2c *I2C) waitForFlag(flag uint32, set bool) bool {
|
||||
return hasFlag
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitForFlagOrError(flag uint32, set bool) bool {
|
||||
func (i2c I2C) waitForFlagOrError(flag uint32, set bool) bool {
|
||||
const tryMax = 10000
|
||||
hasFlag := false
|
||||
for i := 0; !hasFlag && i < tryMax; i++ {
|
||||
@@ -107,7 +106,7 @@ type I2CConfig struct {
|
||||
}
|
||||
|
||||
// Configure is intended to setup the STM32 I2C interface.
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
|
||||
// The following is the required sequence in controller mode.
|
||||
// 1. Program the peripheral input clock in I2C_CR2 Register in order to
|
||||
@@ -157,7 +156,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||
|
||||
if err := i2c.controllerTransmit(addr, w); nil != err {
|
||||
return err
|
||||
@@ -172,7 +171,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerTransmit(addr uint16, w []byte) error {
|
||||
func (i2c I2C) controllerTransmit(addr uint16, w []byte) error {
|
||||
|
||||
if !i2c.waitForFlag(flagBUSY, false) {
|
||||
return errI2CBusReadyTimeout
|
||||
@@ -224,7 +223,7 @@ func (i2c *I2C) controllerTransmit(addr uint16, w []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerRequestWrite(addr uint16, option transferOption) error {
|
||||
func (i2c I2C) controllerRequestWrite(addr uint16, option transferOption) error {
|
||||
|
||||
if frameFirstAndLast == option || frameFirst == option || frameNoOption == option {
|
||||
// generate start condition
|
||||
@@ -250,7 +249,7 @@ func (i2c *I2C) controllerRequestWrite(addr uint16, option transferOption) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerReceive(addr uint16, r []byte) error {
|
||||
func (i2c I2C) controllerReceive(addr uint16, r []byte) error {
|
||||
|
||||
if !i2c.waitForFlag(flagBUSY, false) {
|
||||
return errI2CBusReadyTimeout
|
||||
@@ -400,7 +399,7 @@ func (i2c *I2C) controllerReceive(addr uint16, r []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerRequestRead(addr uint16, option transferOption) error {
|
||||
func (i2c I2C) controllerRequestRead(addr uint16, option transferOption) error {
|
||||
|
||||
// enable ACK
|
||||
i2c.Bus.CR1.SetBits(stm32.I2C_CR1_ACK)
|
||||
@@ -1,347 +0,0 @@
|
||||
// +build stm32l5 stm32f7 stm32l4
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//go:linkname ticks runtime.ticks
|
||||
func ticks() int64
|
||||
|
||||
// I2C implementation for 'newer' STM32 MCUs, including the F7, L5 and L4
|
||||
// series of MCUs.
|
||||
//
|
||||
// Currently, only 100KHz mode is supported
|
||||
|
||||
const (
|
||||
flagBUSY = stm32.I2C_ISR_BUSY
|
||||
flagTCR = stm32.I2C_ISR_TCR
|
||||
flagRXNE = stm32.I2C_ISR_RXNE
|
||||
flagSTOPF = stm32.I2C_ISR_STOPF
|
||||
flagAF = stm32.I2C_ISR_NACKF
|
||||
flagTXIS = stm32.I2C_ISR_TXIS
|
||||
flagTXE = stm32.I2C_ISR_TXE
|
||||
)
|
||||
|
||||
const (
|
||||
MAX_NBYTE_SIZE = 255
|
||||
TIMEOUT_TICKS = 100 // 100ms
|
||||
|
||||
I2C_NO_STARTSTOP = 0x0
|
||||
I2C_GENERATE_START_WRITE = 0x80000000 | stm32.I2C_CR2_START
|
||||
I2C_GENERATE_START_READ = 0x80000000 | stm32.I2C_CR2_START | stm32.I2C_CR2_RD_WRN
|
||||
I2C_GENERATE_STOP = 0x80000000 | stm32.I2C_CR2_STOP
|
||||
)
|
||||
|
||||
type I2C struct {
|
||||
Bus *stm32.I2C_Type
|
||||
AltFuncSelector uint8
|
||||
}
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
type I2CConfig struct {
|
||||
SCL Pin
|
||||
SDA Pin
|
||||
}
|
||||
|
||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// disable I2C interface before any configuration changes
|
||||
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)
|
||||
|
||||
// enable clock for I2C
|
||||
enableAltFuncClock(unsafe.Pointer(i2c.Bus))
|
||||
|
||||
// init pins
|
||||
if config.SCL == 0 && config.SDA == 0 {
|
||||
config.SCL = I2C0_SCL_PIN
|
||||
config.SDA = I2C0_SDA_PIN
|
||||
}
|
||||
i2c.configurePins(config)
|
||||
|
||||
// Frequency range
|
||||
i2c.Bus.TIMINGR.Set(i2c.getFreqRange())
|
||||
|
||||
// Disable Own Address1 before set the Own Address1 configuration
|
||||
i2c.Bus.OAR1.ClearBits(stm32.I2C_OAR1_OA1EN)
|
||||
|
||||
// 7 bit addressing, no self address
|
||||
i2c.Bus.OAR1.Set(stm32.I2C_OAR1_OA1EN)
|
||||
|
||||
// Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process
|
||||
i2c.Bus.CR2.Set(stm32.I2C_CR2_AUTOEND | stm32.I2C_CR2_NACK)
|
||||
|
||||
// Disable Own Address2 / Dual Addressing
|
||||
i2c.Bus.OAR2.Set(0)
|
||||
|
||||
// Disable Generalcall and NoStretch, Enable peripheral
|
||||
i2c.Bus.CR1.Set(stm32.I2C_CR1_PE)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||
if len(w) > 0 {
|
||||
if err := i2c.controllerTransmit(addr, w); nil != err {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(r) > 0 {
|
||||
if err := i2c.controllerReceive(addr, r); nil != err {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||
config.SCL.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSCL}, i2c.AltFuncSelector)
|
||||
config.SDA.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSDA}, i2c.AltFuncSelector)
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerTransmit(addr uint16, w []byte) error {
|
||||
start := ticks()
|
||||
|
||||
if !i2c.waitOnFlagUntilTimeout(flagBUSY, false, start) {
|
||||
return errI2CBusReadyTimeout
|
||||
}
|
||||
|
||||
pos := 0
|
||||
xferCount := len(w)
|
||||
xferSize := uint8(xferCount)
|
||||
if xferCount > MAX_NBYTE_SIZE {
|
||||
// Large write, indicate reload
|
||||
xferSize = MAX_NBYTE_SIZE
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_RELOAD, I2C_GENERATE_START_WRITE)
|
||||
} else {
|
||||
// Small write, auto-end
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_AUTOEND, I2C_GENERATE_START_WRITE)
|
||||
}
|
||||
|
||||
for xferCount > 0 {
|
||||
if !i2c.waitOnTXISFlagUntilTimeout(start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
i2c.Bus.TXDR.Set(uint32(w[pos]))
|
||||
pos++
|
||||
xferCount--
|
||||
xferSize--
|
||||
|
||||
// If we've written the last byte of this chunk
|
||||
if xferCount != 0 && xferSize == 0 {
|
||||
// Wait for Transfer Complete Reload to be flagged
|
||||
if !i2c.waitOnFlagUntilTimeout(flagTCR, true, start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
if xferCount > MAX_NBYTE_SIZE {
|
||||
// Large write remaining, indicate reload
|
||||
xferSize = MAX_NBYTE_SIZE
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_RELOAD, I2C_NO_STARTSTOP)
|
||||
} else {
|
||||
// Small write, auto-end
|
||||
xferSize = uint8(xferCount)
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_AUTOEND, I2C_NO_STARTSTOP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !i2c.waitOnStopFlagUntilTimeout(start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
i2c.clearFlag(stm32.I2C_ISR_STOPF)
|
||||
|
||||
i2c.resetCR2()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) controllerReceive(addr uint16, r []byte) error {
|
||||
start := ticks()
|
||||
|
||||
if !i2c.waitOnFlagUntilTimeout(flagBUSY, false, start) {
|
||||
return errI2CBusReadyTimeout
|
||||
}
|
||||
|
||||
pos := 0
|
||||
xferCount := len(r)
|
||||
xferSize := uint8(xferCount)
|
||||
if xferCount > MAX_NBYTE_SIZE {
|
||||
// Large read, indicate reload
|
||||
xferSize = MAX_NBYTE_SIZE
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_RELOAD, I2C_GENERATE_START_READ)
|
||||
} else {
|
||||
// Small read, auto-end
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_AUTOEND, I2C_GENERATE_START_READ)
|
||||
}
|
||||
|
||||
for xferCount > 0 {
|
||||
if !i2c.waitOnRXNEFlagUntilTimeout(start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
r[pos] = uint8(i2c.Bus.RXDR.Get())
|
||||
pos++
|
||||
xferCount--
|
||||
xferSize--
|
||||
|
||||
// If we've read the last byte of this chunk
|
||||
if xferCount != 0 && xferSize == 0 {
|
||||
// Wait for Transfer Complete Reload to be flagged
|
||||
if !i2c.waitOnFlagUntilTimeout(flagTCR, true, start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
if xferCount > MAX_NBYTE_SIZE {
|
||||
// Large read remaining, indicate reload
|
||||
xferSize = MAX_NBYTE_SIZE
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_RELOAD, I2C_NO_STARTSTOP)
|
||||
} else {
|
||||
// Small read, auto-end
|
||||
xferSize = uint8(xferCount)
|
||||
i2c.transferConfig(addr, xferSize, stm32.I2C_CR2_AUTOEND, I2C_NO_STARTSTOP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !i2c.waitOnStopFlagUntilTimeout(start) {
|
||||
return errI2CWriteTimeout
|
||||
}
|
||||
|
||||
i2c.clearFlag(stm32.I2C_ISR_STOPF)
|
||||
|
||||
i2c.resetCR2()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitOnFlagUntilTimeout(flag uint32, set bool, startTicks int64) bool {
|
||||
for i2c.hasFlag(flag) != set {
|
||||
if (ticks() - startTicks) > TIMEOUT_TICKS {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitOnRXNEFlagUntilTimeout(startTicks int64) bool {
|
||||
for !i2c.hasFlag(flagRXNE) {
|
||||
if i2c.isAcknowledgeFailed(startTicks) {
|
||||
return false
|
||||
}
|
||||
|
||||
if i2c.hasFlag(flagSTOPF) {
|
||||
i2c.clearFlag(flagSTOPF)
|
||||
i2c.resetCR2()
|
||||
return false
|
||||
}
|
||||
|
||||
if (ticks() - startTicks) > TIMEOUT_TICKS {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitOnTXISFlagUntilTimeout(startTicks int64) bool {
|
||||
for !i2c.hasFlag(flagTXIS) {
|
||||
if i2c.isAcknowledgeFailed(startTicks) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (ticks() - startTicks) > TIMEOUT_TICKS {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (i2c *I2C) waitOnStopFlagUntilTimeout(startTicks int64) bool {
|
||||
for !i2c.hasFlag(flagSTOPF) {
|
||||
if i2c.isAcknowledgeFailed(startTicks) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (ticks() - startTicks) > TIMEOUT_TICKS {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (i2c *I2C) isAcknowledgeFailed(startTicks int64) bool {
|
||||
if i2c.hasFlag(flagAF) {
|
||||
// Wait until STOP Flag is reset
|
||||
// AutoEnd should be initiate after AF
|
||||
for !i2c.hasFlag(flagSTOPF) {
|
||||
if (ticks() - startTicks) > TIMEOUT_TICKS {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
i2c.clearFlag(flagAF)
|
||||
i2c.clearFlag(flagSTOPF)
|
||||
i2c.flushTXDR()
|
||||
i2c.resetCR2()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (i2c *I2C) flushTXDR() {
|
||||
// If a pending TXIS flag is set, write a dummy data in TXDR to clear it
|
||||
if i2c.hasFlag(flagTXIS) {
|
||||
i2c.Bus.TXDR.Set(0)
|
||||
}
|
||||
|
||||
// Flush TX register if not empty
|
||||
if !i2c.hasFlag(flagTXE) {
|
||||
i2c.clearFlag(flagTXE)
|
||||
}
|
||||
}
|
||||
|
||||
func (i2c *I2C) resetCR2() {
|
||||
i2c.Bus.CR2.ClearBits(stm32.I2C_CR2_SADD_Msk |
|
||||
stm32.I2C_CR2_HEAD10R_Msk |
|
||||
stm32.I2C_CR2_NBYTES_Msk |
|
||||
stm32.I2C_CR2_RELOAD_Msk |
|
||||
stm32.I2C_CR2_RD_WRN_Msk)
|
||||
}
|
||||
|
||||
func (i2c *I2C) transferConfig(addr uint16, size uint8, mode uint32, request uint32) {
|
||||
mask := uint32(stm32.I2C_CR2_SADD_Msk |
|
||||
stm32.I2C_CR2_NBYTES_Msk |
|
||||
stm32.I2C_CR2_RELOAD_Msk |
|
||||
stm32.I2C_CR2_AUTOEND_Msk |
|
||||
(stm32.I2C_CR2_RD_WRN & uint32(request>>(31-stm32.I2C_CR2_RD_WRN_Pos))) |
|
||||
stm32.I2C_CR2_START_Msk |
|
||||
stm32.I2C_CR2_STOP_Msk)
|
||||
|
||||
value := (uint32(addr<<1) & stm32.I2C_CR2_SADD_Msk) |
|
||||
((uint32(size) << stm32.I2C_CR2_NBYTES_Pos) & stm32.I2C_CR2_NBYTES_Msk) |
|
||||
mode | request
|
||||
|
||||
i2c.Bus.CR2.ReplaceBits(value, mask, 0)
|
||||
}
|
||||
|
||||
func (i2c *I2C) hasFlag(flag uint32) bool {
|
||||
return i2c.Bus.ISR.HasBits(flag)
|
||||
}
|
||||
|
||||
func (i2c *I2C) clearFlag(flag uint32) {
|
||||
if flag == stm32.I2C_ISR_TXE {
|
||||
i2c.Bus.ISR.SetBits(flag)
|
||||
} else {
|
||||
i2c.Bus.ICR.SetBits(flag)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build stm32,!stm32f7x2,!stm32l5x2,!stm32l4x2
|
||||
// +build stm32,!stm32f7x2,!stm32l5x2
|
||||
|
||||
package machine
|
||||
|
||||
|
||||
@@ -13,11 +13,10 @@ import (
|
||||
|
||||
// UART representation
|
||||
type UART struct {
|
||||
Buffer *RingBuffer
|
||||
Bus *stm32.USART_Type
|
||||
Interrupt interrupt.Interrupt
|
||||
TxAltFuncSelector uint8
|
||||
RxAltFuncSelector uint8
|
||||
Buffer *RingBuffer
|
||||
Bus *stm32.USART_Type
|
||||
Interrupt interrupt.Interrupt
|
||||
AltFuncSelector uint8
|
||||
|
||||
// Registers specific to the chip
|
||||
rxReg *volatile.Register32
|
||||
|
||||
@@ -201,7 +201,7 @@ func (spi SPI) configurePins(config SPIConfig) {
|
||||
// Since the first interface is named I2C1, both I2C0 and I2C1 refer to I2C1.
|
||||
// TODO: implement I2C2.
|
||||
var (
|
||||
I2C1 = (*I2C)(unsafe.Pointer(stm32.I2C1))
|
||||
I2C1 = I2C{Bus: stm32.I2C1}
|
||||
I2C0 = I2C1
|
||||
)
|
||||
|
||||
@@ -209,7 +209,7 @@ type I2C struct {
|
||||
Bus *stm32.I2C_Type
|
||||
}
|
||||
|
||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||
func (i2c I2C) configurePins(config I2CConfig) {
|
||||
if config.SDA == PB9 {
|
||||
// use alternate I2C1 pins PB8/PB9 via AFIO mapping
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_AFIOEN)
|
||||
@@ -220,7 +220,7 @@ func (i2c *I2C) configurePins(config I2CConfig) {
|
||||
config.SCL.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltOpenDrain})
|
||||
}
|
||||
|
||||
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
// pclk1 clock speed is main frequency divided by PCLK1 prescaler (div 2)
|
||||
pclk1 := CPUFrequency() / 2
|
||||
|
||||
@@ -229,7 +229,7 @@ func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
return pclk1 / 1000000
|
||||
}
|
||||
|
||||
func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
// These bits must be programmed with the maximum SCL rise time given in the
|
||||
// I2C bus specification, incremented by 1.
|
||||
// For instance: in Sm mode, the maximum allowed SCL rise time is 1000 ns.
|
||||
@@ -245,7 +245,7 @@ func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
return (freqRange + 1) << stm32.I2C_TRISE_TRISE_Pos
|
||||
}
|
||||
|
||||
func (i2c *I2C) getSpeed(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getSpeed(config I2CConfig) uint32 {
|
||||
ccr := func(pclk uint32, freq uint32, coeff uint32) uint32 {
|
||||
return (((pclk - 1) / (freq * coeff)) + 1) & stm32.I2C_CCR_CCR_Msk
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ const (
|
||||
|
||||
func (uart *UART) configurePins(config UARTConfig) {
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
|
||||
}
|
||||
|
||||
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
|
||||
@@ -116,12 +116,12 @@ type I2C struct {
|
||||
AltFuncSelector uint8
|
||||
}
|
||||
|
||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||
func (i2c I2C) configurePins(config I2CConfig) {
|
||||
config.SCL.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSCL}, i2c.AltFuncSelector)
|
||||
config.SDA.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSDA}, i2c.AltFuncSelector)
|
||||
}
|
||||
|
||||
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
// all I2C interfaces are on APB1 (42 MHz)
|
||||
clock := CPUFrequency() / 4
|
||||
// convert to MHz
|
||||
@@ -139,7 +139,7 @@ func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
return clock << stm32.I2C_CR2_FREQ_Pos
|
||||
}
|
||||
|
||||
func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
// These bits must be programmed with the maximum SCL rise time given in the
|
||||
// I2C bus specification, incremented by 1.
|
||||
// For instance: in Sm mode, the maximum allowed SCL rise time is 1000 ns.
|
||||
@@ -155,7 +155,7 @@ func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
return (freqRange + 1) << stm32.I2C_TRISE_TRISE_Pos
|
||||
}
|
||||
|
||||
func (i2c *I2C) getSpeed(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getSpeed(config I2CConfig) uint32 {
|
||||
ccr := func(pclk uint32, freq uint32, coeff uint32) uint32 {
|
||||
return (((pclk - 1) / (freq * coeff)) + 1) & stm32.I2C_CCR_CCR_Msk
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ const (
|
||||
// Configure the UART.
|
||||
func (uart *UART) configurePins(config UARTConfig) {
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
|
||||
}
|
||||
|
||||
// UART baudrate calc based on the bus and clockspeed
|
||||
@@ -128,12 +128,12 @@ type I2C struct {
|
||||
AltFuncSelector uint8
|
||||
}
|
||||
|
||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||
func (i2c I2C) configurePins(config I2CConfig) {
|
||||
config.SCL.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSCL}, i2c.AltFuncSelector)
|
||||
config.SDA.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSDA}, i2c.AltFuncSelector)
|
||||
}
|
||||
|
||||
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
// all I2C interfaces are on APB1 (42 MHz)
|
||||
clock := CPUFrequency() / 4
|
||||
// convert to MHz
|
||||
@@ -151,7 +151,7 @@ func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||
return clock << stm32.I2C_CR2_FREQ_Pos
|
||||
}
|
||||
|
||||
func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
// These bits must be programmed with the maximum SCL rise time given in the
|
||||
// I2C bus specification, incremented by 1.
|
||||
// For instance: in Sm mode, the maximum allowed SCL rise time is 1000 ns.
|
||||
@@ -167,7 +167,7 @@ func (i2c *I2C) getRiseTime(config I2CConfig) uint32 {
|
||||
return (freqRange + 1) << stm32.I2C_TRISE_TRISE_Pos
|
||||
}
|
||||
|
||||
func (i2c *I2C) getSpeed(config I2CConfig) uint32 {
|
||||
func (i2c I2C) getSpeed(config I2CConfig) uint32 {
|
||||
ccr := func(pclk uint32, freq uint32, coeff uint32) uint32 {
|
||||
return (((pclk - 1) / (freq * coeff)) + 1) & stm32.I2C_CCR_CCR_Msk
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ func CPUFrequency() uint32 {
|
||||
// Configure the UART.
|
||||
func (uart *UART) configurePins(config UARTConfig) {
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
|
||||
}
|
||||
|
||||
// UART baudrate calc based on the bus and clockspeed
|
||||
@@ -41,13 +41,3 @@ func (uart *UART) setRegisters() {
|
||||
uart.statusReg = &uart.Bus.ISR
|
||||
uart.txEmptyFlag = stm32.USART_ISR_TXE
|
||||
}
|
||||
|
||||
//---------- I2C related code
|
||||
|
||||
// Gets the value for TIMINGR register
|
||||
func (i2c *I2C) getFreqRange() uint32 {
|
||||
// This is a 'magic' value calculated by STM32CubeMX
|
||||
// for 27MHz PCLK1 (216MHz CPU Freq / 8).
|
||||
// TODO: Do calculations based on PCLK1
|
||||
return 0x00606A9B
|
||||
}
|
||||
|
||||
@@ -191,8 +191,8 @@ func enableAltFuncClock(bus unsafe.Pointer) {
|
||||
// Configure the UART.
|
||||
func (uart UART) configurePins(config UARTConfig) {
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
|
||||
}
|
||||
|
||||
// UART baudrate calc based on the bus and clockspeed
|
||||
|
||||
@@ -1,184 +0,0 @@
|
||||
// +build stm32l4
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Peripheral abstraction layer for the stm32l4
|
||||
|
||||
const (
|
||||
PA0 = portA + 0
|
||||
PA1 = portA + 1
|
||||
PA2 = portA + 2
|
||||
PA3 = portA + 3
|
||||
PA4 = portA + 4
|
||||
PA5 = portA + 5
|
||||
PA6 = portA + 6
|
||||
PA7 = portA + 7
|
||||
PA8 = portA + 8
|
||||
PA9 = portA + 9
|
||||
PA10 = portA + 10
|
||||
PA11 = portA + 11
|
||||
PA12 = portA + 12
|
||||
PA13 = portA + 13
|
||||
PA14 = portA + 14
|
||||
PA15 = portA + 15
|
||||
|
||||
PB0 = portB + 0
|
||||
PB1 = portB + 1
|
||||
PB2 = portB + 2
|
||||
PB3 = portB + 3
|
||||
PB4 = portB + 4
|
||||
PB5 = portB + 5
|
||||
PB6 = portB + 6
|
||||
PB7 = portB + 7
|
||||
PB8 = portB + 8
|
||||
PB9 = portB + 9
|
||||
PB10 = portB + 10
|
||||
PB11 = portB + 11
|
||||
PB12 = portB + 12
|
||||
PB13 = portB + 13
|
||||
PB14 = portB + 14
|
||||
PB15 = portB + 15
|
||||
|
||||
PC0 = portC + 0
|
||||
PC1 = portC + 1
|
||||
PC2 = portC + 2
|
||||
PC3 = portC + 3
|
||||
PC4 = portC + 4
|
||||
PC5 = portC + 5
|
||||
PC6 = portC + 6
|
||||
PC7 = portC + 7
|
||||
PC8 = portC + 8
|
||||
PC9 = portC + 9
|
||||
PC10 = portC + 10
|
||||
PC11 = portC + 11
|
||||
PC12 = portC + 12
|
||||
PC13 = portC + 13
|
||||
PC14 = portC + 14
|
||||
PC15 = portC + 15
|
||||
|
||||
PD0 = portD + 0
|
||||
PD1 = portD + 1
|
||||
PD2 = portD + 2
|
||||
PD3 = portD + 3
|
||||
PD4 = portD + 4
|
||||
PD5 = portD + 5
|
||||
PD6 = portD + 6
|
||||
PD7 = portD + 7
|
||||
PD8 = portD + 8
|
||||
PD9 = portD + 9
|
||||
PD10 = portD + 10
|
||||
PD11 = portD + 11
|
||||
PD12 = portD + 12
|
||||
PD13 = portD + 13
|
||||
PD14 = portD + 14
|
||||
PD15 = portD + 15
|
||||
|
||||
PE0 = portE + 0
|
||||
PE1 = portE + 1
|
||||
PE2 = portE + 2
|
||||
PE3 = portE + 3
|
||||
PE4 = portE + 4
|
||||
PE5 = portE + 5
|
||||
PE6 = portE + 6
|
||||
PE7 = portE + 7
|
||||
PE8 = portE + 8
|
||||
PE9 = portE + 9
|
||||
PE10 = portE + 10
|
||||
PE11 = portE + 11
|
||||
PE12 = portE + 12
|
||||
PE13 = portE + 13
|
||||
PE14 = portE + 14
|
||||
PE15 = portE + 15
|
||||
)
|
||||
|
||||
func (p Pin) getPort() *stm32.GPIO_Type {
|
||||
switch p / 16 {
|
||||
case 0:
|
||||
return stm32.GPIOA
|
||||
case 1:
|
||||
return stm32.GPIOB
|
||||
case 2:
|
||||
return stm32.GPIOC
|
||||
case 3:
|
||||
return stm32.GPIOD
|
||||
case 4:
|
||||
return stm32.GPIOE
|
||||
default:
|
||||
panic("machine: unknown port")
|
||||
}
|
||||
}
|
||||
|
||||
// enableClock enables the clock for this desired GPIO port.
|
||||
func (p Pin) enableClock() {
|
||||
switch p / 16 {
|
||||
case 0:
|
||||
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_GPIOAEN)
|
||||
case 1:
|
||||
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_GPIOBEN)
|
||||
case 2:
|
||||
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_GPIOCEN)
|
||||
case 3:
|
||||
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_GPIODEN)
|
||||
case 4:
|
||||
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_GPIOEEN)
|
||||
default:
|
||||
panic("machine: unknown port")
|
||||
}
|
||||
}
|
||||
|
||||
// Enable peripheral clock
|
||||
func enableAltFuncClock(bus unsafe.Pointer) {
|
||||
switch bus {
|
||||
case unsafe.Pointer(stm32.PWR): // Power interface clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN)
|
||||
case unsafe.Pointer(stm32.I2C3): // I2C3 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_I2C3EN)
|
||||
case unsafe.Pointer(stm32.I2C2): // I2C2 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_I2C2EN)
|
||||
case unsafe.Pointer(stm32.I2C1): // I2C1 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_I2C1EN)
|
||||
case unsafe.Pointer(stm32.UART4): // UART4 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_UART4EN)
|
||||
case unsafe.Pointer(stm32.USART3): // USART3 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_USART3EN)
|
||||
case unsafe.Pointer(stm32.USART2): // USART2 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_USART2EN)
|
||||
case unsafe.Pointer(stm32.SPI3): // SPI3 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_SPI3EN)
|
||||
case unsafe.Pointer(stm32.SPI2): // SPI2 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_SPI2EN)
|
||||
case unsafe.Pointer(stm32.WWDG): // Window watchdog clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_WWDGEN)
|
||||
case unsafe.Pointer(stm32.TIM7): // TIM7 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM7EN)
|
||||
case unsafe.Pointer(stm32.TIM6): // TIM6 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM6EN)
|
||||
case unsafe.Pointer(stm32.TIM3): // TIM3 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM3EN)
|
||||
case unsafe.Pointer(stm32.TIM2): // TIM2 clock enable
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM2EN)
|
||||
case unsafe.Pointer(stm32.LPTIM2): // LPTIM2 clock enable
|
||||
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPTIM2EN)
|
||||
case unsafe.Pointer(stm32.I2C4): // I2C4 clock enable
|
||||
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_I2C4EN)
|
||||
case unsafe.Pointer(stm32.LPUART1): // LPUART1 clock enable
|
||||
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPUART1EN)
|
||||
case unsafe.Pointer(stm32.TIM16): // TIM16 clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_TIM16EN)
|
||||
case unsafe.Pointer(stm32.TIM15): // TIM15 clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_TIM15EN)
|
||||
case unsafe.Pointer(stm32.SYSCFG): // System configuration controller clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_SYSCFGEN)
|
||||
case unsafe.Pointer(stm32.SPI1): // SPI1 clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_SPI1EN)
|
||||
case unsafe.Pointer(stm32.USART1): // USART1 clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_USART1EN)
|
||||
case unsafe.Pointer(stm32.TIM1): // TIM1 clock enable
|
||||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_TIM1EN)
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// +build stm32l4x2
|
||||
|
||||
package machine
|
||||
|
||||
// Peripheral abstraction layer for the stm32l4x2
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
return 80000000
|
||||
}
|
||||
|
||||
//---------- UART related code
|
||||
|
||||
// Configure the UART.
|
||||
func (uart *UART) configurePins(config UARTConfig) {
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
}
|
||||
|
||||
// UART baudrate calc based on the bus and clockspeed
|
||||
// NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
|
||||
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
|
||||
return (CPUFrequency() / baudRate)
|
||||
}
|
||||
|
||||
// Register names vary by ST processor, these are for STM L5
|
||||
func (uart *UART) setRegisters() {
|
||||
uart.rxReg = &uart.Bus.RDR
|
||||
uart.txReg = &uart.Bus.TDR
|
||||
uart.statusReg = &uart.Bus.ISR
|
||||
uart.txEmptyFlag = stm32.USART_ISR_TXE
|
||||
}
|
||||
|
||||
//---------- I2C related code
|
||||
|
||||
// Gets the value for TIMINGR register
|
||||
func (i2c *I2C) getFreqRange() uint32 {
|
||||
// This is a 'magic' value calculated by STM32CubeMX
|
||||
// for 80MHz PCLK1.
|
||||
// TODO: Do calculations based on PCLK1
|
||||
return 0x10909CEC
|
||||
}
|
||||
@@ -22,8 +22,8 @@ func (uart *UART) configurePins(config UARTConfig) {
|
||||
}
|
||||
|
||||
// enable the alternate functions on the TX and RX pins
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
|
||||
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
|
||||
}
|
||||
|
||||
// UART baudrate calc based on the bus and clockspeed
|
||||
@@ -39,13 +39,3 @@ func (uart *UART) setRegisters() {
|
||||
uart.statusReg = &uart.Bus.ISR
|
||||
uart.txEmptyFlag = stm32.USART_ISR_TXE
|
||||
}
|
||||
|
||||
//---------- I2C related code
|
||||
|
||||
// Gets the value for TIMINGR register
|
||||
func (i2c *I2C) getFreqRange() uint32 {
|
||||
// This is a 'magic' value calculated by STM32CubeMX
|
||||
// for 110MHz PCLK1.
|
||||
// TODO: Do calculations based on PCLK1
|
||||
return 0x40505681
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// +build !baremetal stm32,!stm32f7x2,!stm32l5x2,!stm32l4x2 fe310 k210 atmega
|
||||
// +build !baremetal stm32,!stm32f7x2,!stm32l5x2 fe310 k210 atmega
|
||||
|
||||
package machine
|
||||
|
||||
|
||||
+3
-39
@@ -2,7 +2,6 @@ package os
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,8 +17,9 @@ var (
|
||||
ErrExist = errors.New("file exists")
|
||||
)
|
||||
|
||||
// The following code is copied from the official implementation.
|
||||
// https://github.com/golang/go/blob/4ce6a8e89668b87dce67e2f55802903d6eb9110a/src/os/error.go#L65-L104
|
||||
func IsPermission(err error) bool {
|
||||
return err == ErrPermission
|
||||
}
|
||||
|
||||
func NewSyscallError(syscall string, err error) error {
|
||||
if err == nil {
|
||||
@@ -37,39 +37,3 @@ type SyscallError struct {
|
||||
func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() }
|
||||
|
||||
func (e *SyscallError) Unwrap() error { return e.Err }
|
||||
|
||||
func IsExist(err error) bool {
|
||||
return underlyingErrorIs(err, ErrExist)
|
||||
}
|
||||
|
||||
func IsNotExist(err error) bool {
|
||||
return underlyingErrorIs(err, ErrNotExist)
|
||||
}
|
||||
|
||||
func IsPermission(err error) bool {
|
||||
return underlyingErrorIs(err, ErrPermission)
|
||||
}
|
||||
|
||||
func underlyingErrorIs(err, target error) bool {
|
||||
// Note that this function is not errors.Is:
|
||||
// underlyingError only unwraps the specific error-wrapping types
|
||||
// that it historically did, not all errors implementing Unwrap().
|
||||
err = underlyingError(err)
|
||||
if err == target {
|
||||
return true
|
||||
}
|
||||
// To preserve prior behavior, only examine syscall errors.
|
||||
e, ok := err.(syscall.Errno)
|
||||
return ok && e.Is(target)
|
||||
}
|
||||
|
||||
// underlyingError returns the underlying error for known os error types.
|
||||
func underlyingError(err error) error {
|
||||
switch err := err.(type) {
|
||||
case *PathError:
|
||||
return err.Err
|
||||
case *SyscallError:
|
||||
return err.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+61
-10
@@ -6,7 +6,6 @@
|
||||
package os
|
||||
|
||||
import (
|
||||
"io"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -77,7 +76,7 @@ func Create(name string) (*File, error) {
|
||||
// read and any error encountered. At end of file, Read returns 0, io.EOF.
|
||||
func (f *File) Read(b []byte) (n int, err error) {
|
||||
n, err = f.handle.Read(b)
|
||||
if err != nil && err != io.EOF {
|
||||
if err != nil {
|
||||
err = &PathError{"read", f.name, err}
|
||||
}
|
||||
return
|
||||
@@ -156,17 +155,59 @@ func (e *PathError) Error() string {
|
||||
return e.Op + " " + e.Path + ": " + e.Err.Error()
|
||||
}
|
||||
|
||||
type FileMode uint32
|
||||
|
||||
// Mode constants, copied from the mainline Go source
|
||||
// https://github.com/golang/go/blob/4ce6a8e89668b87dce67e2f55802903d6eb9110a/src/os/types.go#L35-L63
|
||||
const (
|
||||
O_RDONLY int = syscall.O_RDONLY
|
||||
O_WRONLY int = syscall.O_WRONLY
|
||||
O_RDWR int = syscall.O_RDWR
|
||||
O_APPEND int = syscall.O_APPEND
|
||||
O_CREATE int = syscall.O_CREAT
|
||||
O_EXCL int = syscall.O_EXCL
|
||||
O_SYNC int = syscall.O_SYNC
|
||||
O_TRUNC int = syscall.O_TRUNC
|
||||
// The single letters are the abbreviations used by the String method's formatting.
|
||||
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
|
||||
ModeAppend // a: append-only
|
||||
ModeExclusive // l: exclusive use
|
||||
ModeTemporary // T: temporary file; Plan 9 only
|
||||
ModeSymlink // L: symbolic link
|
||||
ModeDevice // D: device file
|
||||
ModeNamedPipe // p: named pipe (FIFO)
|
||||
ModeSocket // S: Unix domain socket
|
||||
ModeSetuid // u: setuid
|
||||
ModeSetgid // g: setgid
|
||||
ModeCharDevice // c: Unix character device, when ModeDevice is set
|
||||
ModeSticky // t: sticky
|
||||
ModeIrregular // ?: non-regular file; nothing else is known about this file
|
||||
|
||||
// Mask for the type bits. For regular files, none will be set.
|
||||
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
|
||||
|
||||
ModePerm FileMode = 0777 // Unix permission bits
|
||||
)
|
||||
|
||||
// IsDir is a stub, always returning false
|
||||
func (m FileMode) IsDir() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Stub constants
|
||||
const (
|
||||
O_RDONLY int = 1
|
||||
O_WRONLY int = 2
|
||||
O_RDWR int = 4
|
||||
O_APPEND int = 8
|
||||
O_CREATE int = 16
|
||||
O_EXCL int = 32
|
||||
O_SYNC int = 64
|
||||
O_TRUNC int = 128
|
||||
)
|
||||
|
||||
// A FileInfo describes a file and is returned by Stat and Lstat.
|
||||
type FileInfo interface {
|
||||
Name() string // base name of the file
|
||||
Size() int64 // length in bytes for regular files; system-dependent for others
|
||||
Mode() FileMode // file mode bits
|
||||
// TODO ModTime() time.Time // modification time
|
||||
IsDir() bool // abbreviation for Mode().IsDir()
|
||||
Sys() interface{} // underlying data source (can return nil)
|
||||
}
|
||||
|
||||
// Stat is a stub, not yet implemented
|
||||
func Stat(name string) (FileInfo, error) {
|
||||
return nil, &PathError{"stat", name, ErrNotImplemented}
|
||||
@@ -192,6 +233,16 @@ func TempDir() string {
|
||||
return "/tmp"
|
||||
}
|
||||
|
||||
// IsExist is a stub (for now), always returning false
|
||||
func IsExist(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsNotExist is a stub (for now), always returning false
|
||||
func IsNotExist(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Getpid is a stub (for now), always returning 1
|
||||
func Getpid() int {
|
||||
return 1
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
// +build go1.16
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
type (
|
||||
DirEntry = fs.DirEntry
|
||||
FileMode = fs.FileMode
|
||||
FileInfo = fs.FileInfo
|
||||
)
|
||||
|
||||
func (f *File) ReadDir(n int) ([]DirEntry, error) {
|
||||
return nil, &PathError{"ReadDir", f.name, ErrNotImplemented}
|
||||
}
|
||||
|
||||
// The followings are copied from Go 1.16 official implementation:
|
||||
// https://github.com/golang/go/blob/go1.16/src/os/file.go
|
||||
|
||||
// ReadFile reads the named file and returns the contents.
|
||||
// A successful call returns err == nil, not err == EOF.
|
||||
// Because ReadFile reads the whole file, it does not treat an EOF from Read
|
||||
// as an error to be reported.
|
||||
func ReadFile(name string) ([]byte, error) {
|
||||
f, err := Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var size int
|
||||
if info, err := f.Stat(); err == nil {
|
||||
size64 := info.Size()
|
||||
if int64(int(size64)) == size64 {
|
||||
size = int(size64)
|
||||
}
|
||||
}
|
||||
size++ // one byte for final read at EOF
|
||||
|
||||
// If a file claims a small size, read at least 512 bytes.
|
||||
// In particular, files in Linux's /proc claim size 0 but
|
||||
// then do not work right if read in small pieces,
|
||||
// so an initial read of 1 byte would not work correctly.
|
||||
if size < 512 {
|
||||
size = 512
|
||||
}
|
||||
|
||||
data := make([]byte, 0, size)
|
||||
for {
|
||||
if len(data) >= cap(data) {
|
||||
d := append(data[:cap(data)], 0)
|
||||
data = d[:len(data)]
|
||||
}
|
||||
n, err := f.Read(data[len(data):cap(data)])
|
||||
data = data[:len(data)+n]
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
return data, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFile writes data to the named file, creating it if necessary.
|
||||
// If the file does not exist, WriteFile creates it with permissions perm (before umask);
|
||||
// otherwise WriteFile truncates it before writing, without changing permissions.
|
||||
func WriteFile(name string, data []byte, perm FileMode) error {
|
||||
f, err := OpenFile(name, O_WRONLY|O_CREATE|O_TRUNC, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = f.Write(data)
|
||||
if err1 := f.Close(); err1 != nil && err == nil {
|
||||
err = err1
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// +build !go1.16
|
||||
|
||||
package os
|
||||
|
||||
import "time"
|
||||
|
||||
// A FileInfo describes a file and is returned by Stat and Lstat.
|
||||
type FileInfo interface {
|
||||
Name() string // base name of the file
|
||||
Size() int64 // length in bytes for regular files; system-dependent for others
|
||||
Mode() FileMode // file mode bits
|
||||
ModTime() time.Time // modification time
|
||||
IsDir() bool // abbreviation for Mode().IsDir()
|
||||
Sys() interface{} // underlying data source (can return nil)
|
||||
}
|
||||
|
||||
type FileMode uint32
|
||||
|
||||
// Mode constants, copied from the mainline Go source
|
||||
// https://github.com/golang/go/blob/4ce6a8e89668b87dce67e2f55802903d6eb9110a/src/os/types.go#L35-L63
|
||||
const (
|
||||
// The single letters are the abbreviations used by the String method's formatting.
|
||||
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
|
||||
ModeAppend // a: append-only
|
||||
ModeExclusive // l: exclusive use
|
||||
ModeTemporary // T: temporary file; Plan 9 only
|
||||
ModeSymlink // L: symbolic link
|
||||
ModeDevice // D: device file
|
||||
ModeNamedPipe // p: named pipe (FIFO)
|
||||
ModeSocket // S: Unix domain socket
|
||||
ModeSetuid // u: setuid
|
||||
ModeSetgid // g: setgid
|
||||
ModeCharDevice // c: Unix character device, when ModeDevice is set
|
||||
ModeSticky // t: sticky
|
||||
ModeIrregular // ?: non-regular file; nothing else is known about this file
|
||||
|
||||
// Mask for the type bits. For regular files, none will be set.
|
||||
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
|
||||
|
||||
ModePerm FileMode = 0777 // Unix permission bits
|
||||
)
|
||||
|
||||
// IsDir is a stub, always returning false
|
||||
func (m FileMode) IsDir() bool {
|
||||
return false
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build baremetal wasm,!wasi
|
||||
// +build baremetal wasm
|
||||
|
||||
package os
|
||||
|
||||
|
||||
+1
-5
@@ -1,9 +1,8 @@
|
||||
// +build darwin linux,!baremetal freebsd,!baremetal
|
||||
// +build darwin linux,!baremetal,!wasi freebsd,!baremetal
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"io"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -78,9 +77,6 @@ type unixFileHandle uintptr
|
||||
func (f unixFileHandle) Read(b []byte) (n int, err error) {
|
||||
n, err = syscall.Read(int(f), b)
|
||||
err = handleSyscallError(err)
|
||||
if n == 0 && err == nil {
|
||||
err = io.EOF
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func Swapper(slice interface{}) func(i, j int) {
|
||||
return func(i, j int) {}
|
||||
}
|
||||
|
||||
typ := v.typecode.Elem()
|
||||
typ := v.Type().Elem()
|
||||
size := typ.Size()
|
||||
|
||||
header := (*SliceHeader)(v.value)
|
||||
|
||||
+47
-274
@@ -122,218 +122,30 @@ func (k Kind) String() string {
|
||||
}
|
||||
|
||||
// basicType returns a new Type for this kind if Kind is a basic type.
|
||||
func (k Kind) basicType() rawType {
|
||||
return rawType(k << 1)
|
||||
}
|
||||
|
||||
// The following Type type has been copied almost entirely from
|
||||
// https://github.com/golang/go/blob/go1.15/src/reflect/type.go#L27-L212.
|
||||
// Some methods have been commented out as they haven't yet been implemented.
|
||||
|
||||
// Type is the representation of a Go type.
|
||||
//
|
||||
// Not all methods apply to all kinds of types. Restrictions,
|
||||
// if any, are noted in the documentation for each method.
|
||||
// Use the Kind method to find out the kind of type before
|
||||
// calling kind-specific methods. Calling a method
|
||||
// inappropriate to the kind of type causes a run-time panic.
|
||||
//
|
||||
// Type values are comparable, such as with the == operator,
|
||||
// so they can be used as map keys.
|
||||
// Two Type values are equal if they represent identical types.
|
||||
type Type interface {
|
||||
// Methods applicable to all types.
|
||||
|
||||
// Align returns the alignment in bytes of a value of
|
||||
// this type when allocated in memory.
|
||||
Align() int
|
||||
|
||||
// FieldAlign returns the alignment in bytes of a value of
|
||||
// this type when used as a field in a struct.
|
||||
FieldAlign() int
|
||||
|
||||
// Method returns the i'th method in the type's method set.
|
||||
// It panics if i is not in the range [0, NumMethod()).
|
||||
//
|
||||
// For a non-interface type T or *T, the returned Method's Type and Func
|
||||
// fields describe a function whose first argument is the receiver.
|
||||
//
|
||||
// For an interface type, the returned Method's Type field gives the
|
||||
// method signature, without a receiver, and the Func field is nil.
|
||||
//
|
||||
// Only exported methods are accessible and they are sorted in
|
||||
// lexicographic order.
|
||||
//Method(int) Method
|
||||
|
||||
// MethodByName returns the method with that name in the type's
|
||||
// method set and a boolean indicating if the method was found.
|
||||
//
|
||||
// For a non-interface type T or *T, the returned Method's Type and Func
|
||||
// fields describe a function whose first argument is the receiver.
|
||||
//
|
||||
// For an interface type, the returned Method's Type field gives the
|
||||
// method signature, without a receiver, and the Func field is nil.
|
||||
//MethodByName(string) (Method, bool)
|
||||
|
||||
// NumMethod returns the number of exported methods in the type's method set.
|
||||
NumMethod() int
|
||||
|
||||
// Name returns the type's name within its package for a defined type.
|
||||
// For other (non-defined) types it returns the empty string.
|
||||
Name() string
|
||||
|
||||
// PkgPath returns a defined type's package path, that is, the import path
|
||||
// that uniquely identifies the package, such as "encoding/base64".
|
||||
// If the type was predeclared (string, error) or not defined (*T, struct{},
|
||||
// []int, or A where A is an alias for a non-defined type), the package path
|
||||
// will be the empty string.
|
||||
//PkgPath() string
|
||||
|
||||
// Size returns the number of bytes needed to store
|
||||
// a value of the given type; it is analogous to unsafe.Sizeof.
|
||||
Size() uintptr
|
||||
|
||||
// String returns a string representation of the type.
|
||||
// The string representation may use shortened package names
|
||||
// (e.g., base64 instead of "encoding/base64") and is not
|
||||
// guaranteed to be unique among types. To test for type identity,
|
||||
// compare the Types directly.
|
||||
String() string
|
||||
|
||||
// Kind returns the specific kind of this type.
|
||||
Kind() Kind
|
||||
|
||||
// Implements reports whether the type implements the interface type u.
|
||||
Implements(u Type) bool
|
||||
|
||||
// AssignableTo reports whether a value of the type is assignable to type u.
|
||||
AssignableTo(u Type) bool
|
||||
|
||||
// ConvertibleTo reports whether a value of the type is convertible to type u.
|
||||
ConvertibleTo(u Type) bool
|
||||
|
||||
// Comparable reports whether values of this type are comparable.
|
||||
Comparable() bool
|
||||
|
||||
// Methods applicable only to some types, depending on Kind.
|
||||
// The methods allowed for each kind are:
|
||||
//
|
||||
// Int*, Uint*, Float*, Complex*: Bits
|
||||
// Array: Elem, Len
|
||||
// Chan: ChanDir, Elem
|
||||
// Func: In, NumIn, Out, NumOut, IsVariadic.
|
||||
// Map: Key, Elem
|
||||
// Ptr: Elem
|
||||
// Slice: Elem
|
||||
// Struct: Field, FieldByIndex, FieldByName, FieldByNameFunc, NumField
|
||||
|
||||
// Bits returns the size of the type in bits.
|
||||
// It panics if the type's Kind is not one of the
|
||||
// sized or unsized Int, Uint, Float, or Complex kinds.
|
||||
Bits() int
|
||||
|
||||
// ChanDir returns a channel type's direction.
|
||||
// It panics if the type's Kind is not Chan.
|
||||
//ChanDir() ChanDir
|
||||
|
||||
// IsVariadic reports whether a function type's final input parameter
|
||||
// is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's
|
||||
// implicit actual type []T.
|
||||
//
|
||||
// For concreteness, if t represents func(x int, y ... float64), then
|
||||
//
|
||||
// t.NumIn() == 2
|
||||
// t.In(0) is the reflect.Type for "int"
|
||||
// t.In(1) is the reflect.Type for "[]float64"
|
||||
// t.IsVariadic() == true
|
||||
//
|
||||
// IsVariadic panics if the type's Kind is not Func.
|
||||
//IsVariadic() bool
|
||||
|
||||
// Elem returns a type's element type.
|
||||
// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
|
||||
Elem() Type
|
||||
|
||||
// Field returns a struct type's i'th field.
|
||||
// It panics if the type's Kind is not Struct.
|
||||
// It panics if i is not in the range [0, NumField()).
|
||||
Field(i int) StructField
|
||||
|
||||
// FieldByIndex returns the nested field corresponding
|
||||
// to the index sequence. It is equivalent to calling Field
|
||||
// successively for each index i.
|
||||
// It panics if the type's Kind is not Struct.
|
||||
//FieldByIndex(index []int) StructField
|
||||
|
||||
// FieldByName returns the struct field with the given name
|
||||
// and a boolean indicating if the field was found.
|
||||
//FieldByName(name string) (StructField, bool)
|
||||
|
||||
// FieldByNameFunc returns the struct field with a name
|
||||
// that satisfies the match function and a boolean indicating if
|
||||
// the field was found.
|
||||
//
|
||||
// FieldByNameFunc considers the fields in the struct itself
|
||||
// and then the fields in any embedded structs, in breadth first order,
|
||||
// stopping at the shallowest nesting depth containing one or more
|
||||
// fields satisfying the match function. If multiple fields at that depth
|
||||
// satisfy the match function, they cancel each other
|
||||
// and FieldByNameFunc returns no match.
|
||||
// This behavior mirrors Go's handling of name lookup in
|
||||
// structs containing embedded fields.
|
||||
//FieldByNameFunc(match func(string) bool) (StructField, bool)
|
||||
|
||||
// In returns the type of a function type's i'th input parameter.
|
||||
// It panics if the type's Kind is not Func.
|
||||
// It panics if i is not in the range [0, NumIn()).
|
||||
//In(i int) Type
|
||||
|
||||
// Key returns a map type's key type.
|
||||
// It panics if the type's Kind is not Map.
|
||||
Key() Type
|
||||
|
||||
// Len returns an array type's length.
|
||||
// It panics if the type's Kind is not Array.
|
||||
Len() int
|
||||
|
||||
// NumField returns a struct type's field count.
|
||||
// It panics if the type's Kind is not Struct.
|
||||
NumField() int
|
||||
|
||||
// NumIn returns a function type's input parameter count.
|
||||
// It panics if the type's Kind is not Func.
|
||||
//NumIn() int
|
||||
|
||||
// NumOut returns a function type's output parameter count.
|
||||
// It panics if the type's Kind is not Func.
|
||||
//NumOut() int
|
||||
|
||||
// Out returns the type of a function type's i'th output parameter.
|
||||
// It panics if the type's Kind is not Func.
|
||||
// It panics if i is not in the range [0, NumOut()).
|
||||
//Out(i int) Type
|
||||
func (k Kind) basicType() Type {
|
||||
return Type(k << 1)
|
||||
}
|
||||
|
||||
// The typecode as used in an interface{}.
|
||||
type rawType uintptr
|
||||
type Type uintptr
|
||||
|
||||
func TypeOf(i interface{}) Type {
|
||||
return ValueOf(i).typecode
|
||||
}
|
||||
|
||||
func PtrTo(t Type) Type {
|
||||
ptrType := t.(rawType)<<5 | 5 // 0b0101 == 5
|
||||
ptrType := t<<5 | 5 // 0b0101 == 5
|
||||
if ptrType>>5 != t {
|
||||
panic("reflect: PtrTo type does not fit")
|
||||
}
|
||||
return ptrType
|
||||
}
|
||||
|
||||
func (t rawType) String() string {
|
||||
func (t Type) String() string {
|
||||
return "T"
|
||||
}
|
||||
|
||||
func (t rawType) Kind() Kind {
|
||||
func (t Type) Kind() Kind {
|
||||
if t%2 == 0 {
|
||||
// basic type
|
||||
return Kind((t >> 1) % 32)
|
||||
@@ -344,18 +156,14 @@ func (t rawType) Kind() Kind {
|
||||
|
||||
// Elem returns the element type for channel, slice and array types, the
|
||||
// pointed-to value for pointer types, and the key type for map types.
|
||||
func (t rawType) Elem() Type {
|
||||
return t.elem()
|
||||
}
|
||||
|
||||
func (t rawType) elem() rawType {
|
||||
func (t Type) Elem() Type {
|
||||
switch t.Kind() {
|
||||
case Chan, Ptr, Slice:
|
||||
return t.stripPrefix()
|
||||
case Array:
|
||||
index := t.stripPrefix()
|
||||
elem, _ := readVarint(unsafe.Pointer(uintptr(unsafe.Pointer(&arrayTypesSidetable)) + uintptr(index)))
|
||||
return rawType(elem)
|
||||
return Type(elem)
|
||||
default: // not implemented: Map
|
||||
panic("unimplemented: (reflect.Type).Elem()")
|
||||
}
|
||||
@@ -367,14 +175,14 @@ func (t rawType) elem() rawType {
|
||||
// simply shifted off.
|
||||
//
|
||||
// The behavior is only defined for non-basic types.
|
||||
func (t rawType) stripPrefix() rawType {
|
||||
func (t Type) stripPrefix() Type {
|
||||
// Look at the 'n' bit in the type code (see the top of this file) to see
|
||||
// whether this is a named type.
|
||||
if (t>>4)%2 != 0 {
|
||||
// This is a named type. The data is stored in a sidetable.
|
||||
namedTypeNum := t >> 5
|
||||
n := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&namedNonBasicTypesSidetable)) + uintptr(namedTypeNum)*unsafe.Sizeof(uintptr(0))))
|
||||
return rawType(n)
|
||||
return Type(n)
|
||||
}
|
||||
// Not a named type, so the value is stored directly in the type code.
|
||||
return t >> 5
|
||||
@@ -382,23 +190,7 @@ func (t rawType) stripPrefix() rawType {
|
||||
|
||||
// Field returns the type of the i'th field of this struct type. It panics if t
|
||||
// is not a struct type.
|
||||
func (t rawType) Field(i int) StructField {
|
||||
field := t.rawField(i)
|
||||
return StructField{
|
||||
Name: field.Name,
|
||||
PkgPath: field.PkgPath,
|
||||
Type: field.Type, // note: converts rawType to Type
|
||||
Tag: field.Tag,
|
||||
Anonymous: field.Anonymous,
|
||||
Offset: field.Offset,
|
||||
}
|
||||
}
|
||||
|
||||
// rawField returns nearly the same value as Field but without converting the
|
||||
// Type member to an interface.
|
||||
//
|
||||
// For internal use only.
|
||||
func (t rawType) rawField(i int) rawStructField {
|
||||
func (t Type) Field(i int) StructField {
|
||||
if t.Kind() != Struct {
|
||||
panic(&TypeError{"Field"})
|
||||
}
|
||||
@@ -414,7 +206,7 @@ func (t rawType) rawField(i int) rawStructField {
|
||||
// efficient, but it is easy to implement.
|
||||
// Adding a jump table at the start to jump to the field directly would
|
||||
// make this much faster, but that would also impact code size.
|
||||
field := rawStructField{}
|
||||
field := StructField{}
|
||||
offset := uintptr(0)
|
||||
for fieldNum := 0; fieldNum <= i; fieldNum++ {
|
||||
// Read some flags of this field, like whether the field is an
|
||||
@@ -423,16 +215,15 @@ func (t rawType) rawField(i int) rawStructField {
|
||||
p = unsafe.Pointer(uintptr(p) + 1)
|
||||
|
||||
// Read the type of this struct field.
|
||||
var fieldTypeVal uintptr
|
||||
fieldTypeVal, p = readVarint(p)
|
||||
fieldType := rawType(fieldTypeVal)
|
||||
field.Type = fieldType
|
||||
var fieldType uintptr
|
||||
fieldType, p = readVarint(p)
|
||||
field.Type = Type(fieldType)
|
||||
|
||||
// Move Offset forward to align it to this field's alignment.
|
||||
// Assume alignment is a power of two.
|
||||
offset = align(offset, uintptr(fieldType.Align()))
|
||||
offset = align(offset, uintptr(field.Type.Align()))
|
||||
field.Offset = offset
|
||||
offset += fieldType.Size() // starting (unaligned) offset for next field
|
||||
offset += field.Type.Size() // starting (unaligned) offset for next field
|
||||
|
||||
// Read the field name.
|
||||
var nameNum uintptr
|
||||
@@ -473,7 +264,7 @@ func (t rawType) rawField(i int) rawStructField {
|
||||
// Bits returns the number of bits that this type uses. It is only valid for
|
||||
// arithmetic types (integers, floats, and complex numbers). For other types, it
|
||||
// will panic.
|
||||
func (t rawType) Bits() int {
|
||||
func (t Type) Bits() int {
|
||||
kind := t.Kind()
|
||||
if kind >= Int && kind <= Complex128 {
|
||||
return int(t.Size()) * 8
|
||||
@@ -483,7 +274,7 @@ func (t rawType) Bits() int {
|
||||
|
||||
// Len returns the number of elements in this array. It panics of the type kind
|
||||
// is not Array.
|
||||
func (t rawType) Len() int {
|
||||
func (t Type) Len() int {
|
||||
if t.Kind() != Array {
|
||||
panic(TypeError{"Len"})
|
||||
}
|
||||
@@ -499,7 +290,7 @@ func (t rawType) Len() int {
|
||||
|
||||
// NumField returns the number of fields of a struct type. It panics for other
|
||||
// type kinds.
|
||||
func (t rawType) NumField() int {
|
||||
func (t Type) NumField() int {
|
||||
if t.Kind() != Struct {
|
||||
panic(&TypeError{"NumField"})
|
||||
}
|
||||
@@ -510,7 +301,7 @@ func (t rawType) NumField() int {
|
||||
|
||||
// Size returns the size in bytes of a given type. It is similar to
|
||||
// unsafe.Sizeof.
|
||||
func (t rawType) Size() uintptr {
|
||||
func (t Type) Size() uintptr {
|
||||
switch t.Kind() {
|
||||
case Bool, Int8, Uint8:
|
||||
return 1
|
||||
@@ -533,24 +324,21 @@ func (t rawType) Size() uintptr {
|
||||
case Complex128:
|
||||
return 16
|
||||
case String:
|
||||
return unsafe.Sizeof("")
|
||||
return unsafe.Sizeof(StringHeader{})
|
||||
case UnsafePointer, Chan, Map, Ptr:
|
||||
return unsafe.Sizeof(uintptr(0))
|
||||
case Slice:
|
||||
return unsafe.Sizeof([]int{})
|
||||
return unsafe.Sizeof(SliceHeader{})
|
||||
case Interface:
|
||||
return unsafe.Sizeof(interface{}(nil))
|
||||
case Func:
|
||||
var f func()
|
||||
return unsafe.Sizeof(f)
|
||||
case Array:
|
||||
return t.elem().Size() * uintptr(t.Len())
|
||||
return t.Elem().Size() * uintptr(t.Len())
|
||||
case Struct:
|
||||
numField := t.NumField()
|
||||
if numField == 0 {
|
||||
return 0
|
||||
}
|
||||
lastField := t.rawField(numField - 1)
|
||||
lastField := t.Field(numField - 1)
|
||||
return lastField.Offset + lastField.Type.Size()
|
||||
default:
|
||||
panic("unimplemented: size of type")
|
||||
@@ -559,7 +347,7 @@ func (t rawType) Size() uintptr {
|
||||
|
||||
// Align returns the alignment of this type. It is similar to calling
|
||||
// unsafe.Alignof.
|
||||
func (t rawType) Align() int {
|
||||
func (t Type) Align() int {
|
||||
switch t.Kind() {
|
||||
case Bool, Int8, Uint8:
|
||||
return int(unsafe.Alignof(int8(0)))
|
||||
@@ -582,28 +370,25 @@ func (t rawType) Align() int {
|
||||
case Complex128:
|
||||
return int(unsafe.Alignof(complex128(0)))
|
||||
case String:
|
||||
return int(unsafe.Alignof(""))
|
||||
return int(unsafe.Alignof(StringHeader{}))
|
||||
case UnsafePointer, Chan, Map, Ptr:
|
||||
return int(unsafe.Alignof(uintptr(0)))
|
||||
case Slice:
|
||||
return int(unsafe.Alignof([]int(nil)))
|
||||
return int(unsafe.Alignof(SliceHeader{}))
|
||||
case Interface:
|
||||
return int(unsafe.Alignof(interface{}(nil)))
|
||||
case Func:
|
||||
var f func()
|
||||
return int(unsafe.Alignof(f))
|
||||
case Struct:
|
||||
numField := t.NumField()
|
||||
alignment := 1
|
||||
for i := 0; i < numField; i++ {
|
||||
fieldAlignment := t.rawField(i).Type.Align()
|
||||
fieldAlignment := t.Field(i).Type.Align()
|
||||
if fieldAlignment > alignment {
|
||||
alignment = fieldAlignment
|
||||
}
|
||||
}
|
||||
return alignment
|
||||
case Array:
|
||||
return t.elem().Align()
|
||||
return t.Elem().Align()
|
||||
default:
|
||||
panic("unimplemented: alignment of type")
|
||||
}
|
||||
@@ -611,31 +396,31 @@ func (t rawType) Align() int {
|
||||
|
||||
// FieldAlign returns the alignment if this type is used in a struct field. It
|
||||
// is currently an alias for Align() but this might change in the future.
|
||||
func (t rawType) FieldAlign() int {
|
||||
func (t Type) FieldAlign() int {
|
||||
return t.Align()
|
||||
}
|
||||
|
||||
// AssignableTo returns whether a value of type t can be assigned to a variable
|
||||
// of type u.
|
||||
func (t rawType) AssignableTo(u Type) bool {
|
||||
if t == u.(rawType) {
|
||||
// AssignableTo returns whether a value of type u can be assigned to a variable
|
||||
// of type t.
|
||||
func (t Type) AssignableTo(u Type) bool {
|
||||
if t == u {
|
||||
return true
|
||||
}
|
||||
if u.Kind() == Interface {
|
||||
panic("reflect: unimplemented: AssignableTo with interface")
|
||||
if t.Kind() == Interface {
|
||||
panic("reflect: unimplemented: assigning to interface of different type")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (t rawType) Implements(u Type) bool {
|
||||
if u.Kind() != Interface {
|
||||
func (t Type) Implements(u Type) bool {
|
||||
if t.Kind() != Interface {
|
||||
panic("reflect: non-interface type passed to Type.Implements")
|
||||
}
|
||||
return t.AssignableTo(u)
|
||||
return u.AssignableTo(t)
|
||||
}
|
||||
|
||||
// Comparable returns whether values of this type can be compared to each other.
|
||||
func (t rawType) Comparable() bool {
|
||||
func (t Type) Comparable() bool {
|
||||
switch t.Kind() {
|
||||
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
|
||||
return true
|
||||
@@ -654,7 +439,7 @@ func (t rawType) Comparable() bool {
|
||||
case Slice:
|
||||
return false
|
||||
case Array:
|
||||
return t.elem().Comparable()
|
||||
return t.Elem().Comparable()
|
||||
case Func:
|
||||
return false
|
||||
case Map:
|
||||
@@ -662,7 +447,7 @@ func (t rawType) Comparable() bool {
|
||||
case Struct:
|
||||
numField := t.NumField()
|
||||
for i := 0; i < numField; i++ {
|
||||
if !t.rawField(i).Type.Comparable() {
|
||||
if !t.Field(i).Type.Comparable() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -672,19 +457,19 @@ func (t rawType) Comparable() bool {
|
||||
}
|
||||
}
|
||||
|
||||
func (t rawType) ConvertibleTo(u Type) bool {
|
||||
func (t Type) ConvertibleTo(u Type) bool {
|
||||
panic("unimplemented: (reflect.Type).ConvertibleTo()")
|
||||
}
|
||||
|
||||
func (t rawType) NumMethod() int {
|
||||
func (t Type) NumMethod() int {
|
||||
panic("unimplemented: (reflect.Type).NumMethod()")
|
||||
}
|
||||
|
||||
func (t rawType) Name() string {
|
||||
func (t Type) Name() string {
|
||||
panic("unimplemented: (reflect.Type).Name()")
|
||||
}
|
||||
|
||||
func (t rawType) Key() Type {
|
||||
func (t Type) Key() Type {
|
||||
panic("unimplemented: (reflect.Type).Key()")
|
||||
}
|
||||
|
||||
@@ -703,18 +488,6 @@ type StructField struct {
|
||||
Offset uintptr
|
||||
}
|
||||
|
||||
// rawStructField is the same as StructField but with the Type member replaced
|
||||
// with rawType. For internal use only. Avoiding this conversion to the Type
|
||||
// interface improves code size in many cases.
|
||||
type rawStructField struct {
|
||||
Name string
|
||||
PkgPath string
|
||||
Type rawType
|
||||
Tag StructTag
|
||||
Anonymous bool
|
||||
Offset uintptr
|
||||
}
|
||||
|
||||
// A StructTag is the tag string in a struct field.
|
||||
type StructTag string
|
||||
|
||||
|
||||
+32
-75
@@ -16,7 +16,7 @@ const (
|
||||
)
|
||||
|
||||
type Value struct {
|
||||
typecode rawType
|
||||
typecode Type
|
||||
value unsafe.Pointer
|
||||
flags valueFlags
|
||||
}
|
||||
@@ -28,13 +28,6 @@ func (v Value) isIndirect() bool {
|
||||
return v.flags&valueFlagIndirect != 0
|
||||
}
|
||||
|
||||
// isExported returns whether the value represented by this Value could be
|
||||
// accessed without violating type system constraints. For example, it is not
|
||||
// set for unexported struct fields.
|
||||
func (v Value) isExported() bool {
|
||||
return v.flags&valueFlagExported != 0
|
||||
}
|
||||
|
||||
func Indirect(v Value) Value {
|
||||
if v.Kind() != Ptr {
|
||||
return v
|
||||
@@ -43,10 +36,10 @@ func Indirect(v Value) Value {
|
||||
}
|
||||
|
||||
//go:linkname composeInterface runtime.composeInterface
|
||||
func composeInterface(rawType, unsafe.Pointer) interface{}
|
||||
func composeInterface(Type, unsafe.Pointer) interface{}
|
||||
|
||||
//go:linkname decomposeInterface runtime.decomposeInterface
|
||||
func decomposeInterface(i interface{}) (rawType, unsafe.Pointer)
|
||||
func decomposeInterface(i interface{}) (Type, unsafe.Pointer)
|
||||
|
||||
func ValueOf(i interface{}) Value {
|
||||
typecode, value := decomposeInterface(i)
|
||||
@@ -58,20 +51,11 @@ func ValueOf(i interface{}) Value {
|
||||
}
|
||||
|
||||
func (v Value) Interface() interface{} {
|
||||
if !v.isExported() {
|
||||
panic("(reflect.Value).Interface: unexported")
|
||||
}
|
||||
return valueInterfaceUnsafe(v)
|
||||
}
|
||||
|
||||
// valueInterfaceUnsafe is used by the runtime to hash map keys. It should not
|
||||
// be subject to the isExported check.
|
||||
func valueInterfaceUnsafe(v Value) interface{} {
|
||||
if v.isIndirect() && v.typecode.Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
if v.isIndirect() && v.Type().Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
// Value was indirect but must be put back directly in the interface
|
||||
// value.
|
||||
var value uintptr
|
||||
for j := v.typecode.Size(); j != 0; j-- {
|
||||
for j := v.Type().Size(); j != 0; j-- {
|
||||
value = (value << 8) | uintptr(*(*uint8)(unsafe.Pointer(uintptr(v.value) + j - 1)))
|
||||
}
|
||||
v.value = unsafe.Pointer(value)
|
||||
@@ -83,16 +67,8 @@ func (v Value) Type() Type {
|
||||
return v.typecode
|
||||
}
|
||||
|
||||
// Internal function only, do not use.
|
||||
//
|
||||
// RawType returns the raw, underlying type code. It is used in the runtime
|
||||
// package and needs to be exported for the runtime package to access it.
|
||||
func (v Value) RawType() rawType {
|
||||
return v.typecode
|
||||
}
|
||||
|
||||
func (v Value) Kind() Kind {
|
||||
return v.typecode.Kind()
|
||||
return v.Type().Kind()
|
||||
}
|
||||
|
||||
// IsNil returns whether the value is the nil value. It panics if the value Kind
|
||||
@@ -156,7 +132,7 @@ func (v Value) CanInterface() bool {
|
||||
}
|
||||
|
||||
func (v Value) CanAddr() bool {
|
||||
return v.flags&(valueFlagIndirect) == valueFlagIndirect
|
||||
panic("unimplemented: (reflect.Value).CanAddr()")
|
||||
}
|
||||
|
||||
func (v Value) Addr() Value {
|
||||
@@ -337,9 +313,10 @@ func chanlen(p unsafe.Pointer) int
|
||||
// Len returns the length of this value for slices, strings, arrays, channels,
|
||||
// and maps. For other types, it panics.
|
||||
func (v Value) Len() int {
|
||||
switch v.typecode.Kind() {
|
||||
t := v.Type()
|
||||
switch t.Kind() {
|
||||
case Array:
|
||||
return v.typecode.Len()
|
||||
return v.Type().Len()
|
||||
case Chan:
|
||||
return chanlen(v.value)
|
||||
case Map:
|
||||
@@ -359,9 +336,10 @@ func chancap(p unsafe.Pointer) int
|
||||
// Cap returns the capacity of this value for arrays, channels and slices.
|
||||
// For other types, it panics.
|
||||
func (v Value) Cap() int {
|
||||
switch v.typecode.Kind() {
|
||||
t := v.Type()
|
||||
switch t.Kind() {
|
||||
case Array:
|
||||
return v.typecode.Len()
|
||||
return v.Type().Len()
|
||||
case Chan:
|
||||
return chancap(v.value)
|
||||
case Slice:
|
||||
@@ -374,7 +352,7 @@ func (v Value) Cap() int {
|
||||
// NumField returns the number of fields of this struct. It panics for other
|
||||
// value types.
|
||||
func (v Value) NumField() int {
|
||||
return v.typecode.NumField()
|
||||
return v.Type().NumField()
|
||||
}
|
||||
|
||||
func (v Value) Elem() Value {
|
||||
@@ -388,7 +366,7 @@ func (v Value) Elem() Value {
|
||||
return Value{}
|
||||
}
|
||||
return Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
value: ptr,
|
||||
flags: v.flags | valueFlagIndirect,
|
||||
}
|
||||
@@ -399,7 +377,7 @@ func (v Value) Elem() Value {
|
||||
|
||||
// Field returns the value of the i'th field of this struct.
|
||||
func (v Value) Field(i int) Value {
|
||||
structField := v.typecode.rawField(i)
|
||||
structField := v.Type().Field(i)
|
||||
flags := v.flags
|
||||
if structField.PkgPath != "" {
|
||||
// The fact that PkgPath is present means that this field is not
|
||||
@@ -407,15 +385,14 @@ func (v Value) Field(i int) Value {
|
||||
flags &^= valueFlagExported
|
||||
}
|
||||
|
||||
size := v.typecode.Size()
|
||||
fieldType := structField.Type
|
||||
fieldSize := fieldType.Size()
|
||||
size := v.Type().Size()
|
||||
fieldSize := structField.Type.Size()
|
||||
if v.isIndirect() || fieldSize > unsafe.Sizeof(uintptr(0)) {
|
||||
// v.value was already a pointer to the value and it should stay that
|
||||
// way.
|
||||
return Value{
|
||||
flags: flags,
|
||||
typecode: fieldType,
|
||||
typecode: structField.Type,
|
||||
value: unsafe.Pointer(uintptr(v.value) + structField.Offset),
|
||||
}
|
||||
}
|
||||
@@ -430,7 +407,7 @@ func (v Value) Field(i int) Value {
|
||||
// situation explicitly.
|
||||
return Value{
|
||||
flags: flags,
|
||||
typecode: fieldType,
|
||||
typecode: structField.Type,
|
||||
value: unsafe.Pointer(uintptr(0)),
|
||||
}
|
||||
}
|
||||
@@ -443,7 +420,7 @@ func (v Value) Field(i int) Value {
|
||||
value := unsafe.Pointer(loadValue(ptr, fieldSize))
|
||||
return Value{
|
||||
flags: 0,
|
||||
typecode: fieldType,
|
||||
typecode: structField.Type,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
@@ -453,7 +430,7 @@ func (v Value) Field(i int) Value {
|
||||
value := maskAndShift(uintptr(v.value), structField.Offset, fieldSize)
|
||||
return Value{
|
||||
flags: flags,
|
||||
typecode: fieldType,
|
||||
typecode: structField.Type,
|
||||
value: unsafe.Pointer(value),
|
||||
}
|
||||
}
|
||||
@@ -467,10 +444,10 @@ func (v Value) Index(i int) Value {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
elem := Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
flags: v.flags | valueFlagIndirect,
|
||||
}
|
||||
addr := uintptr(slice.Data) + elem.typecode.Size()*uintptr(i) // pointer to new value
|
||||
addr := uintptr(slice.Data) + elem.Type().Size()*uintptr(i) // pointer to new value
|
||||
elem.value = unsafe.Pointer(addr)
|
||||
return elem
|
||||
case String:
|
||||
@@ -487,13 +464,13 @@ func (v Value) Index(i int) Value {
|
||||
}
|
||||
case Array:
|
||||
// Extract an element from the array.
|
||||
elemType := v.typecode.elem()
|
||||
elemType := v.Type().Elem()
|
||||
elemSize := elemType.Size()
|
||||
size := v.typecode.Size()
|
||||
size := v.Type().Size()
|
||||
if size == 0 {
|
||||
// The element size is 0 and/or the length of the array is 0.
|
||||
return Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
flags: v.flags,
|
||||
}
|
||||
}
|
||||
@@ -504,7 +481,7 @@ func (v Value) Index(i int) Value {
|
||||
// elemSize.
|
||||
addr := uintptr(v.value) + elemSize*uintptr(i) // pointer to new value
|
||||
return Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
flags: v.flags,
|
||||
value: unsafe.Pointer(addr),
|
||||
}
|
||||
@@ -515,7 +492,7 @@ func (v Value) Index(i int) Value {
|
||||
// Load the value from the pointer.
|
||||
addr := uintptr(v.value) + elemSize*uintptr(i) // pointer to new value
|
||||
return Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
flags: v.flags,
|
||||
value: unsafe.Pointer(loadValue(unsafe.Pointer(addr), elemSize)),
|
||||
}
|
||||
@@ -526,7 +503,7 @@ func (v Value) Index(i int) Value {
|
||||
offset := elemSize * uintptr(i)
|
||||
value := maskAndShift(uintptr(v.value), offset, elemSize)
|
||||
return Value{
|
||||
typecode: v.typecode.elem(),
|
||||
typecode: v.Type().Elem(),
|
||||
flags: v.flags,
|
||||
value: unsafe.Pointer(value),
|
||||
}
|
||||
@@ -555,14 +532,6 @@ func maskAndShift(value, offset, size uintptr) uintptr {
|
||||
return (uintptr(value) >> (offset * 8)) & mask
|
||||
}
|
||||
|
||||
func (v Value) NumMethod() int {
|
||||
return v.typecode.NumMethod()
|
||||
}
|
||||
|
||||
func (v Value) OverflowFloat(x float64) bool {
|
||||
panic("unimplemented: (reflect.Value).OverflowFloat()")
|
||||
}
|
||||
|
||||
func (v Value) MapKeys() []Value {
|
||||
panic("unimplemented: (reflect.Value).MapKeys()")
|
||||
}
|
||||
@@ -592,10 +561,10 @@ func (it *MapIter) Next() bool {
|
||||
|
||||
func (v Value) Set(x Value) {
|
||||
v.checkAddressable()
|
||||
if !v.typecode.AssignableTo(x.typecode) {
|
||||
if !v.Type().AssignableTo(x.Type()) {
|
||||
panic("reflect: cannot set")
|
||||
}
|
||||
size := v.typecode.Size()
|
||||
size := v.Type().Size()
|
||||
xptr := x.value
|
||||
if size <= unsafe.Sizeof(uintptr(0)) && !x.isIndirect() {
|
||||
value := x.value
|
||||
@@ -686,18 +655,6 @@ func (v Value) SetString(x string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetBytes(x []byte) {
|
||||
panic("unimplemented: (reflect.Value).SetBytes()")
|
||||
}
|
||||
|
||||
func (v Value) SetCap(n int) {
|
||||
panic("unimplemented: (reflect.Value).SetCap()")
|
||||
}
|
||||
|
||||
func (v Value) SetLen(n int) {
|
||||
panic("unimplemented: (reflect.Value).SetLen()")
|
||||
}
|
||||
|
||||
func (v Value) checkAddressable() {
|
||||
if !v.isIndirect() {
|
||||
panic("reflect: value is not addressable")
|
||||
|
||||
+9
-16
@@ -349,55 +349,48 @@ func hashmapStringDelete(m *hashmap, key string) {
|
||||
|
||||
// Hashmap with interface keys (for everything else).
|
||||
|
||||
// This is a method that is intentionally unexported in the reflect package. It
|
||||
// is identical to the Interface() method call, except it doesn't check whether
|
||||
// a field is exported and thus allows circumventing the type system.
|
||||
// The hash function needs it as it also needs to hash unexported struct fields.
|
||||
//go:linkname valueInterfaceUnsafe reflect.valueInterfaceUnsafe
|
||||
func valueInterfaceUnsafe(v reflect.Value) interface{}
|
||||
|
||||
func hashmapInterfaceHash(itf interface{}) uint32 {
|
||||
x := reflect.ValueOf(itf)
|
||||
if x.RawType() == 0 {
|
||||
if x.Type() == 0 {
|
||||
return 0 // nil interface
|
||||
}
|
||||
|
||||
value := (*_interface)(unsafe.Pointer(&itf)).value
|
||||
ptr := value
|
||||
if x.RawType().Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
if x.Type().Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
// Value fits in pointer, so it's directly stored in the pointer.
|
||||
ptr = unsafe.Pointer(&value)
|
||||
}
|
||||
|
||||
switch x.RawType().Kind() {
|
||||
switch x.Type().Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return hashmapHash(ptr, x.RawType().Size())
|
||||
return hashmapHash(ptr, x.Type().Size())
|
||||
case reflect.Bool, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return hashmapHash(ptr, x.RawType().Size())
|
||||
return hashmapHash(ptr, x.Type().Size())
|
||||
case reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
|
||||
// It should be possible to just has the contents. However, NaN != NaN
|
||||
// so if you're using lots of NaNs as map keys (you shouldn't) then hash
|
||||
// time may become exponential. To fix that, it would be better to
|
||||
// return a random number instead:
|
||||
// https://research.swtch.com/randhash
|
||||
return hashmapHash(ptr, x.RawType().Size())
|
||||
return hashmapHash(ptr, x.Type().Size())
|
||||
case reflect.String:
|
||||
return hashmapStringHash(x.String())
|
||||
case reflect.Chan, reflect.Ptr, reflect.UnsafePointer:
|
||||
// It might seem better to just return the pointer, but that won't
|
||||
// result in an evenly distributed hashmap. Instead, hash the pointer
|
||||
// like most other types.
|
||||
return hashmapHash(ptr, x.RawType().Size())
|
||||
return hashmapHash(ptr, x.Type().Size())
|
||||
case reflect.Array:
|
||||
var hash uint32
|
||||
for i := 0; i < x.Len(); i++ {
|
||||
hash |= hashmapInterfaceHash(valueInterfaceUnsafe(x.Index(i)))
|
||||
hash |= hashmapInterfaceHash(x.Index(i).Interface())
|
||||
}
|
||||
return hash
|
||||
case reflect.Struct:
|
||||
var hash uint32
|
||||
for i := 0; i < x.NumField(); i++ {
|
||||
hash |= hashmapInterfaceHash(valueInterfaceUnsafe(x.Field(i)))
|
||||
hash |= hashmapInterfaceHash(x.Field(i).Interface())
|
||||
}
|
||||
return hash
|
||||
default:
|
||||
|
||||
+14
-10
@@ -31,21 +31,18 @@ func interfaceEqual(x, y interface{}) bool {
|
||||
}
|
||||
|
||||
func reflectValueEqual(x, y reflect.Value) bool {
|
||||
// Note: doing a x.Type() == y.Type() comparison would not work here as that
|
||||
// would introduce an infinite recursion: comparing two reflect.Type values
|
||||
// is done with this reflectValueEqual runtime call.
|
||||
if x.RawType() == 0 || y.RawType() == 0 {
|
||||
if x.Type() == 0 || y.Type() == 0 {
|
||||
// One of them is nil.
|
||||
return x.RawType() == y.RawType()
|
||||
return x.Type() == y.Type()
|
||||
}
|
||||
|
||||
if x.RawType() != y.RawType() {
|
||||
if x.Type() != y.Type() {
|
||||
// The type is not the same, which means the interfaces are definitely
|
||||
// not the same.
|
||||
return false
|
||||
}
|
||||
|
||||
switch x.RawType().Kind() {
|
||||
switch x.Type().Kind() {
|
||||
case reflect.Bool:
|
||||
return x.Bool() == y.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
@@ -110,8 +107,6 @@ type typecodeID struct {
|
||||
|
||||
// The array length, for array types.
|
||||
length uintptr
|
||||
|
||||
methodSet *interfaceMethodInfo // nil or a GEP of an array
|
||||
}
|
||||
|
||||
// structField is used by the compiler to pass information to the interface
|
||||
@@ -123,11 +118,20 @@ type structField struct {
|
||||
embedded bool
|
||||
}
|
||||
|
||||
// Pseudo type used before interface lowering. By using a struct instead of a
|
||||
// function call, this is simpler to reason about during init interpretation
|
||||
// than a function call. Also, by keeping the method set around it is easier to
|
||||
// implement interfaceImplements in the interp package.
|
||||
type typeInInterface struct {
|
||||
typecode *typecodeID // element type, underlying type, or reference to struct fields
|
||||
methodSet *interfaceMethodInfo // nil or a GEP of an array
|
||||
}
|
||||
|
||||
// Pseudo function call used during a type assert. It is used during interface
|
||||
// lowering, to assign the lowest type numbers to the types with the most type
|
||||
// asserts. Also, it is replaced with const false if this type assert can never
|
||||
// happen.
|
||||
func typeAssert(actualType uintptr, assertedType *uint8) bool
|
||||
func typeAssert(actualType uintptr, assertedType *typecodeID) bool
|
||||
|
||||
// Pseudo function call that returns whether a given type implements all methods
|
||||
// of the given interface.
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user