mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-01 09:37:48 +00:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fbc2099ee3 | |||
| 95d895646a | |||
| 35fb594f8f | |||
| 01f6aff422 | |||
| 7dd5839f47 | |||
| 403fee7e06 | |||
| 7657238c24 | |||
| 3cba36f2ba | |||
| 93d5269fef | |||
| 4b477fad55 | |||
| 6360e318a7 | |||
| 0757eb5919 | |||
| 013a71aa3d | |||
| f0091b31b5 | |||
| 709a296150 | |||
| f7b2a2c977 | |||
| 6ae4b43eb2 | |||
| bece6b9648 | |||
| 553f00bdb8 | |||
| a789108926 | |||
| d63ce0646c | |||
| 003211b4ff | |||
| e6720d7ddc | |||
| dfef168139 | |||
| 101f2e519b | |||
| eb34afde4b | |||
| 63f2a3dfe9 | |||
| fb23e9c212 | |||
| 222c4c75b1 | |||
| 5a509f5bfe | |||
| 2e46275c45 | |||
| 1d34f868da | |||
| f0904779a5 | |||
| 70f1064f36 |
+38
-22
@@ -1,14 +1,29 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.11"
|
||||
matrix:
|
||||
include:
|
||||
- dist: xenial
|
||||
go: "1.11"
|
||||
|
||||
before_install:
|
||||
- echo "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main" | sudo tee -a /etc/apt/sources.list
|
||||
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu trusty main" | sudo tee -a /etc/apt/sources.list
|
||||
- sudo apt-get update -qq
|
||||
- sudo apt-get install llvm-7-dev clang-7 libclang-7-dev binutils-arm-none-eabi qemu-system-arm --allow-unauthenticated -y
|
||||
- sudo ln -s /usr/bin/clang-7 /usr/local/bin/cc # work around missing -no-pie in old GCC version
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
- sourceline: 'ppa:ubuntu-toolchain-r'
|
||||
- sourceline: 'deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-7 main'
|
||||
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
|
||||
packages:
|
||||
- llvm-7-dev
|
||||
- clang-7
|
||||
- libclang-7-dev
|
||||
- gcc-arm-linux-gnueabi
|
||||
- binutils-arm-none-eabi
|
||||
- libc6-dev-armel-cross
|
||||
- gcc-aarch64-linux-gnu
|
||||
- libc6-dev-arm64-cross
|
||||
- qemu-system-arm
|
||||
- qemu-user
|
||||
- gcc-avr
|
||||
- avr-libc
|
||||
|
||||
install:
|
||||
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
|
||||
@@ -18,17 +33,18 @@ script:
|
||||
- go install github.com/tinygo-org/tinygo
|
||||
- go test -v .
|
||||
- make gen-device
|
||||
- tinygo build -o blinky1.nrf.elf -target=pca10040 examples/blinky1
|
||||
- tinygo build -o blinky2.nrf.elf -target=pca10040 examples/blinky2
|
||||
- tinygo build -o blinky2 examples/blinky2
|
||||
- tinygo build -o test.nrf.elf -target=pca10040 examples/test
|
||||
- tinygo build -o blinky1.nrf51.elf -target=microbit examples/echo
|
||||
- tinygo build -o test.nrf.elf -target=nrf52840-mdk examples/blinky1
|
||||
- tinygo build -o blinky1.nrf51d.elf -target=pca10031 examples/blinky1
|
||||
- tinygo build -o blinky1.stm32.elf -target=bluepill examples/blinky1
|
||||
- tinygo build -o blinky1.avr.o -target=arduino examples/blinky1 # TODO: avr-as/avr-gcc doesn't work
|
||||
- tinygo build -o blinky1.reel.elf -target=reelboard examples/blinky1
|
||||
- tinygo build -o blinky2.reel.elf -target=reelboard examples/blinky2
|
||||
- tinygo build -o blinky1.pca10056.elf -target=pca10056 examples/blinky1
|
||||
- tinygo build -o blinky2.pca10056.elf -target=pca10056 examples/blinky2
|
||||
- tinygo build -o blinky1.samd21.elf -target=itsybitsy-m0 examples/blinky1
|
||||
- tinygo build -size short -o blinky1.nrf.elf -target=pca10040 examples/blinky1
|
||||
- tinygo build -size short -o blinky2.nrf.elf -target=pca10040 examples/blinky2
|
||||
- tinygo build -size short -o blinky2 examples/blinky2
|
||||
- tinygo build -size short -o test.nrf.elf -target=pca10040 examples/test
|
||||
- tinygo build -size short -o blinky1.nrf51.elf -target=microbit examples/echo
|
||||
- tinygo build -size short -o test.nrf.elf -target=nrf52840-mdk examples/blinky1
|
||||
- tinygo build -size short -o blinky1.nrf51d.elf -target=pca10031 examples/blinky1
|
||||
- tinygo build -size short -o blinky1.stm32.elf -target=bluepill examples/blinky1
|
||||
- tinygo build -size short -o blinky1.avr.elf -target=arduino examples/blinky1
|
||||
- tinygo build -size short -o blinky1.avr.elf -target=digispark examples/blinky1
|
||||
- tinygo build -size short -o blinky1.reel.elf -target=reelboard examples/blinky1
|
||||
- tinygo build -size short -o blinky2.reel.elf -target=reelboard examples/blinky2
|
||||
- tinygo build -size short -o blinky1.pca10056.elf -target=pca10056 examples/blinky1
|
||||
- tinygo build -size short -o blinky2.pca10056.elf -target=pca10056 examples/blinky2
|
||||
- tinygo build -size short -o blinky1.samd21.elf -target=itsybitsy-m0 examples/blinky1
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
0.2.0
|
||||
---
|
||||
- **command line**
|
||||
- add version subcommand
|
||||
- **compiler**
|
||||
- fix a bug in floating point comparisons with NaN values
|
||||
- fix a bug when calling `panic` in package initialization code
|
||||
- add support for comparing `complex64` and `complex128`
|
||||
- **cgo**
|
||||
- add support for external globals
|
||||
- add support for pointers and function pointers
|
||||
- **standard library**
|
||||
- `fmt`: initial support, `fmt.Println` works
|
||||
- `math`: support for most/all functions
|
||||
- `os`: initial support (only stdin/stdout/stderr)
|
||||
- `reflect`: initial support
|
||||
- `syscall`: add support for amd64, arm, and arm64
|
||||
@@ -0,0 +1,48 @@
|
||||
# How to contribute
|
||||
|
||||
Thank you for your interest in improving TinyGo.
|
||||
|
||||
We would like your help to make this project better, so we appreciate any contributions. See if one of the following descriptions matches your situation:
|
||||
|
||||
### New to TinyGo
|
||||
|
||||
We'd love to get your feedback on getting started with TinyGo. Run into any difficulty, confusion, or anything else? You are not alone. We want to know about your experience, so we can help the next people. Please open a Github issue with your questions, or you can also get in touch directly with us on our Slack channel at [https://gophers.slack.com/messages/CDJD3SUP6](https://gophers.slack.com/messages/CDJD3SUP6).
|
||||
|
||||
### Something in TinyGo is not working as you expect
|
||||
|
||||
Please open a Github issue with your problem, and we will be happy to assist.
|
||||
|
||||
### Something in Go that you want/need does not appear to be in TinyGo
|
||||
|
||||
We probably have not implemented it yet. Please take a look at our [Roadmap](https://github.com/tinygo-org/tinygo/wiki/Roadmap). Your pull request adding the functionality to TinyGo would be greatly appreciated.
|
||||
|
||||
A long tail of small (and large) language features haven't been implemented yet. In almost all cases, the compiler will show a `todo:` error from `compiler/compiler.go` when you try to use it. You can try implementing it, or open a bug report with a small code sample that fails to compile.
|
||||
|
||||
### Some specific hardware you want to use does not appear to be in TinyGo
|
||||
|
||||
As above, we probably have not implemented it yet. Your contribution adding the hardware support to TinyGo would be greatly appreciated.
|
||||
|
||||
Lots of targets/boards are still unsupported. Adding an architecture often requires a few compiler changes, but if the architecture is supported you can try implementing support for a new chip or board in `src/runtime`. For details, see [this wiki entry on adding archs/chips/boards](https://github.com/tinygo-org/tinygo/wiki/Adding-a-new-board).
|
||||
|
||||
Microcontrollers have lots of peripherals (I2C, SPI, ADC, etc.) and many don't have an implementation yet in the `machine` package. Adding support for new peripherals is very useful.
|
||||
|
||||
## How to use our Github repository
|
||||
|
||||
The `master` branch of this repo will always have the latest released version of TinyGo. All of the active development work for the next release will take place in the `dev` branch. TinyGo will use semantic versioning and will create a tag/release for each release.
|
||||
|
||||
Here is how to contribute back some code or documentation:
|
||||
|
||||
- Fork repo
|
||||
- Create a feature branch off of the `dev` branch
|
||||
- Make some useful change
|
||||
- Make sure the tests still pass
|
||||
- Submit a pull request against the `dev` branch.
|
||||
- Be kind
|
||||
|
||||
## How to run tests
|
||||
|
||||
To run the tests:
|
||||
|
||||
```
|
||||
make test
|
||||
```
|
||||
+4
-1
@@ -4,12 +4,15 @@ FROM golang:latest AS tinygo-base
|
||||
RUN wget -O- https://apt.llvm.org/llvm-snapshot.gpg.key| apt-key add - && \
|
||||
echo "deb http://apt.llvm.org/stretch/ llvm-toolchain-stretch-7 main" >> /etc/apt/sources.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y llvm-7-dev libclang-7-dev
|
||||
apt-get install -y llvm-7-dev libclang-7-dev git
|
||||
|
||||
RUN wget -O- https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
|
||||
|
||||
COPY . /go/src/github.com/tinygo-org/tinygo
|
||||
|
||||
RUN cd /go/src/github.com/tinygo-org/tinygo/ && \
|
||||
git submodule update --init
|
||||
|
||||
RUN cd /go/src/github.com/tinygo-org/tinygo/ && \
|
||||
dep ensure --vendor-only && \
|
||||
go install /go/src/github.com/tinygo-org/tinygo/
|
||||
|
||||
Generated
+3
-2
@@ -3,7 +3,7 @@
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:84316faef4ea12d34dde3b3e6dab682715a23b1c2bb8ab82cec9ab619766e214"
|
||||
digest = "1:ba70784a3deee74c0ca3c87bcac3c2f93d3b2d27d8f237b768c358b45ba47da8"
|
||||
name = "golang.org/x/tools"
|
||||
packages = [
|
||||
"go/ast/astutil",
|
||||
@@ -11,7 +11,7 @@
|
||||
"go/types/typeutil",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "3e7aa9e59977626dc60433e9aeadf1bb63d28295"
|
||||
revision = "40960b6deb8ecdb8bcde6a8f44722731939b8ddc"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
@@ -25,6 +25,7 @@
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
input-imports = [
|
||||
"golang.org/x/tools/go/ast/astutil",
|
||||
"golang.org/x/tools/go/ssa",
|
||||
"tinygo.org/x/go-llvm",
|
||||
]
|
||||
|
||||
@@ -82,7 +82,7 @@ clean:
|
||||
@rm -rf build
|
||||
|
||||
fmt:
|
||||
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/runtime ./src/sync
|
||||
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/os ./src/runtime ./src/sync
|
||||
@go fmt ./testdata/*.go
|
||||
|
||||
test:
|
||||
|
||||
@@ -1,145 +1,70 @@
|
||||
# TinyGo - Go compiler for microcontrollers
|
||||
# TinyGo - Go compiler for small places
|
||||
|
||||
[](https://travis-ci.com/tinygo-org/tinygo)
|
||||
[](https://travis-ci.com/tinygo-org/tinygo)
|
||||
|
||||
> We never expected Go to be an embedded language and so it's got serious
|
||||
> problems [...].
|
||||
TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (WASM), and command-line tools.
|
||||
|
||||
-- Rob Pike, [GopherCon 2014 Opening Keynote](https://www.youtube.com/watch?v=VoS7DsT1rdM&feature=youtu.be&t=2799)
|
||||
It reuses libraries used by the [Go language tools](https://golang.org/pkg/go/) alongside [LLVM](http://llvm.org) to provide an alternative way to compile programs written in the Go programming language.
|
||||
|
||||
TinyGo is a project to bring Go to microcontrollers and small systems with a
|
||||
single processor core. It is similar to [emgo](https://github.com/ziutek/emgo)
|
||||
but a major difference is that I want to keep the Go memory model (which implies
|
||||
garbage collection of some sort). Another difference is that TinyGo uses LLVM
|
||||
internally instead of emitting C, which hopefully leads to smaller and more
|
||||
efficient code and certainly leads to more flexibility.
|
||||
|
||||
My original reasoning was: if [Python](https://micropython.org/) can run on
|
||||
microcontrollers, then certainly [Go](https://golang.org/) should be able to and
|
||||
run on even lower level micros.
|
||||
|
||||
Example program (blinky):
|
||||
Here is an example program that blinks the built-in LED when run directly on any supported board with onboard LED:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
"machine"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
led := machine.GPIO{machine.LED}
|
||||
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
|
||||
for {
|
||||
led.Low()
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
led := machine.GPIO{machine.LED}
|
||||
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
|
||||
for {
|
||||
led.Low()
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
|
||||
led.High()
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
}
|
||||
led.High()
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Currently supported features:
|
||||
The above program can be compiled and run without modification on an Arduino Uno, an Adafruit ItsyBitsy M0, or any of the supported boards that have a built-in LED, just by setting the correct TinyGo compiler target. For example, this compiles and flashes an Arduino Uno:
|
||||
|
||||
* control flow
|
||||
* many (but not all) basic types: most ints, floats, strings, structs
|
||||
* function calling
|
||||
* interfaces for basic types (with type switches and asserts)
|
||||
* goroutines (very initial support)
|
||||
* function pointers (non-blocking)
|
||||
* interface methods
|
||||
* standard library (but most packages won't work due to missing language
|
||||
features)
|
||||
* slices (partially)
|
||||
* maps (very rough, unfinished)
|
||||
* defer
|
||||
* closures
|
||||
* bound methods
|
||||
* complex numbers (except for arithmetic)
|
||||
* channels (with some limitations)
|
||||
|
||||
Not yet supported:
|
||||
|
||||
* select
|
||||
* complex arithmetic
|
||||
* garbage collection
|
||||
* recover
|
||||
* introspection (if it ever gets implemented)
|
||||
* ...
|
||||
```shell
|
||||
tinygo flash -target arduino examples/blinky1
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
See the [getting started instructions](https://tinygo.org/getting-started/).
|
||||
See the [getting started instructions](https://tinygo.org/getting-started/) for information on how to install TinyGo, as well as how to run the TinyGo compiler using our Docker container.
|
||||
|
||||
### Running with Docker
|
||||
## Supported boards/targets
|
||||
|
||||
A docker container exists for easy access to the `tinygo` CLI:
|
||||
You can compile TinyGo programs for microcontrollers, WebAssembly and Linux.
|
||||
|
||||
```sh
|
||||
$ docker run --rm -v $(pwd):/src tinygo/tinygo tinygo build -o /src/wasm.wasm -target wasm examples/wasm
|
||||
```
|
||||
The following microcontroller boards are currently supported:
|
||||
|
||||
Note that you cannot run `tinygo flash` from inside the docker container,
|
||||
so it is less useful for microcontroller development.
|
||||
* [Adafruit ItsyBitsy M0](https://www.adafruit.com/product/3727)
|
||||
* [Arduino Uno](https://store.arduino.cc/arduino-uno-rev3)
|
||||
* [BBC:Microbit](https://microbit.org/)
|
||||
* [ST Micro STM32F103XX "Bluepill"](http://wiki.stm32duino.com/index.php?title=Blue_Pill)
|
||||
* [Digispark](http://digistump.com/products/1)
|
||||
* [Nordic Semiconductor PCA10031](https://www.nordicsemi.com/eng/Products/nRF51-Dongle)
|
||||
* [Nordic Semiconductor PCA10040](https://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF52-DK)
|
||||
* [Nordic Semiconductor PCA10056](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK)
|
||||
* [Makerdiary nRF52840-MDK](https://wiki.makerdiary.com/nrf52840-mdk/)
|
||||
* [Phytec reel board](https://www.phytec.eu/product-eu/internet-of-things/reelboard/)
|
||||
|
||||
## Supported targets
|
||||
For more information, see [this list of boards](https://tinygo.org/microcontrollers/). Pull requests for additional support are welcome!
|
||||
|
||||
The following architectures/systems are currently supported:
|
||||
## Currently supported features:
|
||||
|
||||
* ARM (Cortex-M)
|
||||
* AVR (Arduino Uno)
|
||||
* Linux
|
||||
* WebAssembly
|
||||
|
||||
For more information, see [this list of targets and
|
||||
boards](https://tinygo.org/targets/). Pull requests for
|
||||
broader support are welcome!
|
||||
|
||||
## Analysis and optimizations
|
||||
|
||||
The goal is to reduce code size (and increase performance) by performing all
|
||||
kinds of whole-program analysis passes. The official Go compiler doesn't do a
|
||||
whole lot of analysis (except for escape analysis) because it needs to be fast,
|
||||
but embedded programs are necessarily smaller so it becomes practical. And I
|
||||
think especially program size can be reduced by a large margin when actually
|
||||
trying to optimize for it.
|
||||
|
||||
Implemented compiler passes:
|
||||
|
||||
* Analyse which functions are blocking. Blocking functions are functions that
|
||||
call sleep, chan send, etc. Its parents are also blocking.
|
||||
* Analyse whether the scheduler is needed. It is only needed when there are
|
||||
`go` statements for blocking functions.
|
||||
* Analyse whether a given type switch or type assert is possible with
|
||||
[type-based alias analysis](https://en.wikipedia.org/wiki/Alias_analysis#Type-based_alias_analysis).
|
||||
I would like to use flow-based alias analysis in the future, if feasible.
|
||||
* Do basic dead code elimination of functions. This pass makes later passes
|
||||
better and probably improves compile time as well.
|
||||
|
||||
## Scope
|
||||
|
||||
Goals:
|
||||
|
||||
* Have very small binary sizes. Don't pay for what you don't use.
|
||||
* Support for most common microcontroller boards.
|
||||
* Be usable on the web using WebAssembly.
|
||||
* Good CGo support, with no more overhead than a regular function call.
|
||||
* Support most standard library packages and compile most Go code without
|
||||
modification.
|
||||
|
||||
Non-goals:
|
||||
|
||||
* Using more than one core.
|
||||
* Be efficient while using zillions of goroutines. However, good goroutine
|
||||
support is certainly a goal.
|
||||
* Be as fast as `gc`. However, LLVM will probably be better at optimizing
|
||||
certain things so TinyGo might actually turn out to be faster for number
|
||||
crunching.
|
||||
* Be able to compile every Go program out there.
|
||||
For a description of currently supported Go language features, please see [https://tinygo.org/lang-support/](https://tinygo.org/lang-support/).
|
||||
|
||||
## Documentation
|
||||
|
||||
Documentation is currently maintained on a dedicated web site located at [https://tinygo.org/](https://tinygo.org/).
|
||||
Documentation is located on our web site at [https://tinygo.org/](https://tinygo.org/).
|
||||
|
||||
You can find the web site code at [https://github.com/tinygo-org/tinygo-site](https://github.com/tinygo-org/tinygo-site).
|
||||
|
||||
@@ -154,26 +79,37 @@ should arrive fairly quickly (under 1 min): https://invite.slack.golangbridge.or
|
||||
|
||||
## Contributing
|
||||
|
||||
Patches are welcome!
|
||||
Your contributions are welcome!
|
||||
|
||||
If you want to contribute, here are some suggestions:
|
||||
Please take a look at our [CONTRIBUTING.md](./CONTRIBUTING.md) document for details.
|
||||
|
||||
* A long tail of small (and large) language features haven't been implemented
|
||||
yet. In almost all cases, the compiler will show a `todo:` error from
|
||||
`compiler/compiler.go` when you try to use it. You can try implementing it,
|
||||
or open a bug report with a small code sample that fails to compile.
|
||||
* Lots of targets/boards are still unsupported. Adding an architecture often
|
||||
requires a few compiler changes, but if the architecture is supported you
|
||||
can try implementing support for a new chip or board in `src/runtime`. For
|
||||
details, see [this wiki entry on adding
|
||||
archs/chips/boards](https://github.com/tinygo-org/tinygo/wiki/Adding-a-new-board).
|
||||
* Microcontrollers have lots of peripherals and many don't have an
|
||||
implementation yet in the `machine` package. Adding support for new
|
||||
peripherals is very useful.
|
||||
* Just raising bugs for things you'd like to see implemented is also a form of
|
||||
contributing! It helps prioritization.
|
||||
## Project Scope
|
||||
|
||||
Goals:
|
||||
|
||||
* Have very small binary sizes. Don't pay for what you don't use.
|
||||
* Support for most common microcontroller boards.
|
||||
* Be usable on the web using WebAssembly.
|
||||
* Good CGo support, with no more overhead than a regular function call.
|
||||
* Support most standard library packages and compile most Go code without modification.
|
||||
|
||||
Non-goals:
|
||||
|
||||
* Using more than one core.
|
||||
* Be efficient while using zillions of goroutines. However, good goroutine support is certainly a goal.
|
||||
* Be as fast as `gc`. However, LLVM will probably be better at optimizing certain things so TinyGo might actually turn out to be faster for number crunching.
|
||||
* Be able to compile every Go program out there.
|
||||
|
||||
## Why this project exists
|
||||
|
||||
> We never expected Go to be an embedded language and so its got serious problems...
|
||||
|
||||
-- Rob Pike, [GopherCon 2014 Opening Keynote](https://www.youtube.com/watch?v=VoS7DsT1rdM&feature=youtu.be&t=2799)
|
||||
|
||||
TinyGo is a project to bring Go to microcontrollers and small systems with a single processor core. It is similar to [emgo](https://github.com/ziutek/emgo) but a major difference is that we want to keep the Go memory model (which implies garbage collection of some sort). Another difference is that TinyGo uses LLVM internally instead of emitting C, which hopefully leads to smaller and more efficient code and certainly leads to more flexibility.
|
||||
|
||||
The original reasoning was: if [Python](https://micropython.org/) can run on microcontrollers, then certainly [Go](https://golang.org/) should be able to run on even lower level micros.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the BSD 3-clause license, just like the
|
||||
[Go project](https://golang.org/LICENSE) itself.
|
||||
This project is licensed under the BSD 3-clause license, just like the [Go project](https://golang.org/LICENSE) itself.
|
||||
|
||||
+90
-32
@@ -110,7 +110,9 @@ func NewCompiler(pkgName string, config Config) (*Compiler, error) {
|
||||
c.mod.SetTarget(config.Triple)
|
||||
c.mod.SetDataLayout(c.targetData.String())
|
||||
c.builder = c.ctx.NewBuilder()
|
||||
c.dibuilder = llvm.NewDIBuilder(c.mod)
|
||||
if c.Debug {
|
||||
c.dibuilder = llvm.NewDIBuilder(c.mod)
|
||||
}
|
||||
|
||||
c.uintptrType = c.ctx.IntType(c.targetData.PointerSize() * 8)
|
||||
if c.targetData.PointerSize() <= 4 {
|
||||
@@ -214,13 +216,15 @@ func (c *Compiler) Compile(mainPath string) error {
|
||||
c.ir.SimpleDCE()
|
||||
|
||||
// Initialize debug information.
|
||||
c.cu = c.dibuilder.CreateCompileUnit(llvm.DICompileUnit{
|
||||
Language: llvm.DW_LANG_Go,
|
||||
File: mainPath,
|
||||
Dir: "",
|
||||
Producer: "TinyGo",
|
||||
Optimized: true,
|
||||
})
|
||||
if c.Debug {
|
||||
c.cu = c.dibuilder.CreateCompileUnit(llvm.DICompileUnit{
|
||||
Language: llvm.DW_LANG_Go,
|
||||
File: mainPath,
|
||||
Dir: "",
|
||||
Producer: "TinyGo",
|
||||
Optimized: true,
|
||||
})
|
||||
}
|
||||
|
||||
var frames []*Frame
|
||||
|
||||
@@ -373,14 +377,16 @@ func (c *Compiler) Compile(mainPath string) error {
|
||||
c.mod.NamedFunction("runtime.scheduler").SetLinkage(llvm.ExternalLinkage)
|
||||
|
||||
// see: https://reviews.llvm.org/D18355
|
||||
c.mod.AddNamedMetadataOperand("llvm.module.flags",
|
||||
c.ctx.MDNode([]llvm.Metadata{
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 1, false).ConstantAsMetadata(), // Error on mismatch
|
||||
llvm.GlobalContext().MDString("Debug Info Version"),
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 3, false).ConstantAsMetadata(), // DWARF version
|
||||
}),
|
||||
)
|
||||
c.dibuilder.Finalize()
|
||||
if c.Debug {
|
||||
c.mod.AddNamedMetadataOperand("llvm.module.flags",
|
||||
c.ctx.MDNode([]llvm.Metadata{
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 1, false).ConstantAsMetadata(), // Error on mismatch
|
||||
llvm.GlobalContext().MDString("Debug Info Version"),
|
||||
llvm.ConstInt(c.ctx.Int32Type(), 3, false).ConstantAsMetadata(), // DWARF version
|
||||
}),
|
||||
)
|
||||
c.dibuilder.Finalize()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1319,7 +1325,7 @@ func (c *Compiler) parseInstr(frame *Frame, instr ssa.Instruction) error {
|
||||
return nil
|
||||
}
|
||||
store := c.builder.CreateStore(llvmVal, llvmAddr)
|
||||
valType := instr.Addr.Type().(*types.Pointer).Elem()
|
||||
valType := instr.Addr.Type().Underlying().(*types.Pointer).Elem()
|
||||
if c.ir.IsVolatile(valType) {
|
||||
// Volatile store, for memory-mapped registers.
|
||||
store.SetVolatile(true)
|
||||
@@ -1680,6 +1686,11 @@ func (c *Compiler) parseCall(frame *Frame, instr *ssa.CallCommon) (llvm.Value, e
|
||||
return c.builder.CreateCall(target, args, ""), nil
|
||||
}
|
||||
|
||||
switch fn.RelString(nil) {
|
||||
case "syscall.Syscall", "syscall.Syscall6":
|
||||
return c.emitSyscall(frame, instr)
|
||||
}
|
||||
|
||||
targetFunc := c.ir.GetFunction(fn)
|
||||
if targetFunc.LLVMFn.IsNil() {
|
||||
return llvm.Value{}, c.makeError(instr.Pos(), "undefined function: "+targetFunc.LinkName())
|
||||
@@ -1946,7 +1957,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) {
|
||||
var bufptr, buflen llvm.Value
|
||||
switch ptrTyp := expr.X.Type().Underlying().(type) {
|
||||
case *types.Pointer:
|
||||
typ := expr.X.Type().(*types.Pointer).Elem().Underlying()
|
||||
typ := expr.X.Type().Underlying().(*types.Pointer).Elem().Underlying()
|
||||
switch typ := typ.(type) {
|
||||
case *types.Array:
|
||||
bufptr = val
|
||||
@@ -2400,7 +2411,7 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
|
||||
return c.builder.CreateICmp(llvm.IntUGE, x, y, ""), nil
|
||||
}
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on integer: "+op.String())
|
||||
panic("binop on integer: " + op.String())
|
||||
}
|
||||
} else if typ.Info()&types.IsFloat != 0 {
|
||||
// Operations on floats
|
||||
@@ -2413,22 +2424,40 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
|
||||
return c.builder.CreateFMul(x, y, ""), nil
|
||||
case token.QUO: // /
|
||||
return c.builder.CreateFDiv(x, y, ""), nil
|
||||
case token.REM: // %
|
||||
return c.builder.CreateFRem(x, y, ""), nil
|
||||
case token.EQL: // ==
|
||||
return c.builder.CreateFCmp(llvm.FloatOEQ, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatUEQ, x, y, ""), nil
|
||||
case token.NEQ: // !=
|
||||
return c.builder.CreateFCmp(llvm.FloatONE, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatUNE, x, y, ""), nil
|
||||
case token.LSS: // <
|
||||
return c.builder.CreateFCmp(llvm.FloatOLT, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatULT, x, y, ""), nil
|
||||
case token.LEQ: // <=
|
||||
return c.builder.CreateFCmp(llvm.FloatOLE, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatULE, x, y, ""), nil
|
||||
case token.GTR: // >
|
||||
return c.builder.CreateFCmp(llvm.FloatOGT, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatUGT, x, y, ""), nil
|
||||
case token.GEQ: // >=
|
||||
return c.builder.CreateFCmp(llvm.FloatOGE, x, y, ""), nil
|
||||
return c.builder.CreateFCmp(llvm.FloatUGE, x, y, ""), nil
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on float: "+op.String())
|
||||
panic("binop on float: " + op.String())
|
||||
}
|
||||
} else if typ.Info()&types.IsComplex != 0 {
|
||||
indexr := llvm.ConstInt(c.ctx.Int32Type(), 0, false)
|
||||
indexi := llvm.ConstInt(c.ctx.Int32Type(), 1, false)
|
||||
r1 := c.builder.CreateExtractElement(x, indexr, "r1")
|
||||
r2 := c.builder.CreateExtractElement(y, indexr, "r2")
|
||||
i1 := c.builder.CreateExtractElement(x, indexi, "i1")
|
||||
i2 := c.builder.CreateExtractElement(y, indexi, "i2")
|
||||
switch op {
|
||||
case token.EQL: // ==
|
||||
req := c.builder.CreateFCmp(llvm.FloatOEQ, r1, r2, "")
|
||||
ieq := c.builder.CreateFCmp(llvm.FloatOEQ, i1, i2, "")
|
||||
return c.builder.CreateAnd(req, ieq, ""), nil
|
||||
case token.NEQ: // !=
|
||||
req := c.builder.CreateFCmp(llvm.FloatOEQ, r1, r2, "")
|
||||
ieq := c.builder.CreateFCmp(llvm.FloatOEQ, i1, i2, "")
|
||||
neq := c.builder.CreateAnd(req, ieq, "")
|
||||
return c.builder.CreateNot(neq, ""), nil
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on complex number: "+op.String())
|
||||
}
|
||||
} else if typ.Info()&types.IsBoolean != 0 {
|
||||
// Operations on booleans
|
||||
@@ -2438,7 +2467,7 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
|
||||
case token.NEQ: // !=
|
||||
return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on boolean: "+op.String())
|
||||
panic("binop on bool: " + op.String())
|
||||
}
|
||||
} else if typ.Kind() == types.UnsafePointer {
|
||||
// Operations on pointers
|
||||
@@ -2448,7 +2477,7 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
|
||||
case token.NEQ: // !=
|
||||
return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on pointer: "+op.String())
|
||||
panic("binop on pointer: " + op.String())
|
||||
}
|
||||
} else if typ.Info()&types.IsString != 0 {
|
||||
// Operations on strings
|
||||
@@ -2471,7 +2500,7 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
|
||||
case token.GEQ: // >=
|
||||
return c.createRuntimeCall("stringLess", []llvm.Value{y, x}, ""), nil
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(pos, "todo: binop on string: "+op.String())
|
||||
panic("binop on string: " + op.String())
|
||||
}
|
||||
} else {
|
||||
return llvm.Value{}, c.makeError(pos, "todo: unknown basic type in binop: "+typ.String())
|
||||
@@ -2627,6 +2656,19 @@ func (c *Compiler) parseConst(prefix string, expr *ssa.Const) (llvm.Value, error
|
||||
} else if typ.Info()&types.IsFloat != 0 {
|
||||
n, _ := constant.Float64Val(expr.Value)
|
||||
return llvm.ConstFloat(llvmType, n), nil
|
||||
} else if typ.Kind() == types.Complex64 {
|
||||
r, err := c.parseConst(prefix, ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float32]))
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
i, err := c.parseConst(prefix, ssa.NewConst(constant.Imag(expr.Value), types.Typ[types.Float32]))
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
cplx := llvm.Undef(llvm.VectorType(c.ctx.FloatType(), 2))
|
||||
cplx = c.builder.CreateInsertElement(cplx, r, llvm.ConstInt(c.ctx.Int8Type(), 0, false), "")
|
||||
cplx = c.builder.CreateInsertElement(cplx, i, llvm.ConstInt(c.ctx.Int8Type(), 1, false), "")
|
||||
return cplx, nil
|
||||
} else if typ.Kind() == types.Complex128 {
|
||||
r, err := c.parseConst(prefix, ssa.NewConst(constant.Real(expr.Value), types.Typ[types.Float64]))
|
||||
if err != nil {
|
||||
@@ -2643,6 +2685,12 @@ func (c *Compiler) parseConst(prefix string, expr *ssa.Const) (llvm.Value, error
|
||||
} else {
|
||||
return llvm.Value{}, errors.New("todo: unknown constant: " + expr.String())
|
||||
}
|
||||
case *types.Chan:
|
||||
sig, err := c.getLLVMType(expr.Type())
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
return c.getZeroValue(sig)
|
||||
case *types.Signature:
|
||||
if expr.Value != nil {
|
||||
return llvm.Value{}, errors.New("non-nil signature constant")
|
||||
@@ -2938,10 +2986,20 @@ func (c *Compiler) parseUnOp(frame *Frame, unop *ssa.UnOp) (llvm.Value, error) {
|
||||
return llvm.Value{}, c.makeError(unop.Pos(), "todo: unknown type for negate: "+unop.X.Type().Underlying().String())
|
||||
}
|
||||
case token.MUL: // *x, dereference pointer
|
||||
valType := unop.X.Type().(*types.Pointer).Elem()
|
||||
valType := unop.X.Type().Underlying().(*types.Pointer).Elem()
|
||||
if c.targetData.TypeAllocSize(x.Type().ElementType()) == 0 {
|
||||
// zero-length data
|
||||
return c.getZeroValue(x.Type().ElementType())
|
||||
} else if strings.HasSuffix(unop.X.String(), "$funcaddr") {
|
||||
// CGo function pointer. The cgo part has rewritten CGo function
|
||||
// pointers as stub global variables of the form:
|
||||
// var C.add unsafe.Pointer
|
||||
// Instead of a load from the global, create a bitcast of the
|
||||
// function pointer itself.
|
||||
global := c.ir.GetGlobal(unop.X.(*ssa.Global))
|
||||
name := global.LinkName()[:len(global.LinkName())-len("$funcaddr")]
|
||||
fn := c.mod.NamedFunction(name)
|
||||
return c.builder.CreateBitCast(fn, c.i8ptrType, ""), nil
|
||||
} else {
|
||||
load := c.builder.CreateLoad(x, "")
|
||||
if c.ir.IsVolatile(valType) {
|
||||
|
||||
@@ -104,15 +104,6 @@ func (t *typeInfo) getMethod(signature *signatureInfo) *methodInfo {
|
||||
panic("could not find method")
|
||||
}
|
||||
|
||||
// id returns the fully-qualified type name including import path, removing the
|
||||
// $type suffix.
|
||||
func (t *typeInfo) id() string {
|
||||
if !strings.HasSuffix(t.name, "$type") {
|
||||
panic("concrete type does not have $type suffix: " + t.name)
|
||||
}
|
||||
return t.name[:len(t.name)-len("$type")]
|
||||
}
|
||||
|
||||
// typeInfoSlice implements sort.Slice, sorting the most commonly used types
|
||||
// first.
|
||||
type typeInfoSlice []*typeInfo
|
||||
@@ -415,7 +406,7 @@ func (p *lowerInterfacesPass) run() {
|
||||
for _, t := range p.types {
|
||||
typeSlice = append(typeSlice, t)
|
||||
}
|
||||
sort.Sort(typeSlice)
|
||||
sort.Sort(sort.Reverse(typeSlice))
|
||||
|
||||
// A type code must fit in 16 bits.
|
||||
if len(typeSlice) >= 1<<16 {
|
||||
@@ -423,9 +414,7 @@ func (p *lowerInterfacesPass) run() {
|
||||
}
|
||||
|
||||
// Assign a type code for each type.
|
||||
for i, t := range typeSlice {
|
||||
t.num = uint64(i + 1)
|
||||
}
|
||||
p.assignTypeCodes(typeSlice)
|
||||
|
||||
// Replace each call to runtime.makeInterface with the constant type code.
|
||||
for _, use := range makeInterfaceUses {
|
||||
@@ -691,7 +680,7 @@ func (p *lowerInterfacesPass) createInterfaceMethodFunc(itf *interfaceInfo, sign
|
||||
|
||||
// Define all possible functions that can be called.
|
||||
for _, typ := range itf.types {
|
||||
bb := llvm.AddBasicBlock(fn, typ.id())
|
||||
bb := llvm.AddBasicBlock(fn, typ.name)
|
||||
sw.AddCase(llvm.ConstInt(p.uintptrType, typ.num, false), bb)
|
||||
|
||||
// The function we will redirect to when the interface has this type.
|
||||
|
||||
+98
-3
@@ -8,6 +8,8 @@ package compiler
|
||||
import (
|
||||
"go/token"
|
||||
"go/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/ir"
|
||||
"golang.org/x/tools/go/ssa"
|
||||
@@ -51,7 +53,7 @@ func (c *Compiler) parseMakeInterface(val llvm.Value, typ types.Type, global str
|
||||
itfValue = c.builder.CreateIntToPtr(val, c.i8ptrType, "makeinterface.cast.int")
|
||||
case llvm.PointerTypeKind:
|
||||
itfValue = c.builder.CreateBitCast(val, c.i8ptrType, "makeinterface.cast.ptr")
|
||||
case llvm.StructTypeKind:
|
||||
case llvm.StructTypeKind, llvm.FloatTypeKind, llvm.DoubleTypeKind, llvm.VectorTypeKind:
|
||||
// A bitcast would be useful here, but bitcast doesn't allow
|
||||
// aggregate types. So we'll bitcast it using an alloca.
|
||||
// Hopefully this will get optimized away.
|
||||
@@ -79,14 +81,107 @@ func (c *Compiler) parseMakeInterface(val llvm.Value, typ types.Type, global str
|
||||
// It returns a pointer to an external global which should be replaced with the
|
||||
// real type in the interface lowering pass.
|
||||
func (c *Compiler) getTypeCode(typ types.Type) llvm.Value {
|
||||
global := c.mod.NamedGlobal(typ.String() + "$type")
|
||||
globalName := "type:" + getTypeCodeName(typ)
|
||||
global := c.mod.NamedGlobal(globalName)
|
||||
if global.IsNil() {
|
||||
global = llvm.AddGlobal(c.mod, c.ctx.Int8Type(), typ.String()+"$type")
|
||||
global = llvm.AddGlobal(c.mod, c.ctx.Int8Type(), globalName)
|
||||
global.SetGlobalConstant(true)
|
||||
}
|
||||
return global
|
||||
}
|
||||
|
||||
// getTypeCodeName returns a name for this type that can be used in the
|
||||
// interface lowering pass to assign type codes as expected by the reflect
|
||||
// package. See getTypeCodeNum.
|
||||
func getTypeCodeName(t types.Type) string {
|
||||
name := ""
|
||||
if named, ok := t.(*types.Named); ok {
|
||||
name = "~" + named.String() + ":"
|
||||
t = t.Underlying()
|
||||
}
|
||||
switch t := t.(type) {
|
||||
case *types.Array:
|
||||
return "array:" + name + strconv.FormatInt(t.Len(), 10) + ":" + getTypeCodeName(t.Elem())
|
||||
case *types.Basic:
|
||||
var kind string
|
||||
switch t.Kind() {
|
||||
case types.Bool:
|
||||
kind = "bool"
|
||||
case types.Int:
|
||||
kind = "int"
|
||||
case types.Int8:
|
||||
kind = "int8"
|
||||
case types.Int16:
|
||||
kind = "int16"
|
||||
case types.Int32:
|
||||
kind = "int32"
|
||||
case types.Int64:
|
||||
kind = "int64"
|
||||
case types.Uint:
|
||||
kind = "uint"
|
||||
case types.Uint8:
|
||||
kind = "uint8"
|
||||
case types.Uint16:
|
||||
kind = "uint16"
|
||||
case types.Uint32:
|
||||
kind = "uint32"
|
||||
case types.Uint64:
|
||||
kind = "uint64"
|
||||
case types.Uintptr:
|
||||
kind = "uintptr"
|
||||
case types.Float32:
|
||||
kind = "float32"
|
||||
case types.Float64:
|
||||
kind = "float64"
|
||||
case types.Complex64:
|
||||
kind = "complex64"
|
||||
case types.Complex128:
|
||||
kind = "complex128"
|
||||
case types.String:
|
||||
kind = "string"
|
||||
case types.UnsafePointer:
|
||||
kind = "unsafeptr"
|
||||
default:
|
||||
panic("unknown basic type: " + t.Name())
|
||||
}
|
||||
return "basic:" + name + kind
|
||||
case *types.Chan:
|
||||
return "chan:" + name + getTypeCodeName(t.Elem())
|
||||
case *types.Interface:
|
||||
methods := make([]string, t.NumMethods())
|
||||
for i := 0; i < t.NumMethods(); i++ {
|
||||
methods[i] = getTypeCodeName(t.Method(i).Type())
|
||||
}
|
||||
return "interface:" + name + "{" + strings.Join(methods, ",") + "}"
|
||||
case *types.Map:
|
||||
keyType := getTypeCodeName(t.Key())
|
||||
elemType := getTypeCodeName(t.Elem())
|
||||
return "map:" + name + "{" + keyType + "," + elemType + "}"
|
||||
case *types.Pointer:
|
||||
return "pointer:" + name + getTypeCodeName(t.Elem())
|
||||
case *types.Signature:
|
||||
params := make([]string, t.Params().Len())
|
||||
for i := 0; i < t.Params().Len(); i++ {
|
||||
params[i] = getTypeCodeName(t.Params().At(i).Type())
|
||||
}
|
||||
results := make([]string, t.Results().Len())
|
||||
for i := 0; i < t.Results().Len(); i++ {
|
||||
results[i] = getTypeCodeName(t.Results().At(i).Type())
|
||||
}
|
||||
return "func:" + name + "{" + strings.Join(params, ",") + "}{" + strings.Join(results, ",") + "}"
|
||||
case *types.Slice:
|
||||
return "slice:" + name + getTypeCodeName(t.Elem())
|
||||
case *types.Struct:
|
||||
elems := make([]string, t.NumFields())
|
||||
for i := 0; i < t.NumFields(); i++ {
|
||||
elems[i] = getTypeCodeName(t.Field(i).Type())
|
||||
}
|
||||
return "struct:" + name + "{" + strings.Join(elems, ",") + "}"
|
||||
default:
|
||||
panic("unknown type: " + t.String())
|
||||
}
|
||||
}
|
||||
|
||||
// getTypeMethodSet returns a reference (GEP) to a global method set. This
|
||||
// method set should be unreferenced after the interface lowering pass.
|
||||
func (c *Compiler) getTypeMethodSet(typ types.Type) (llvm.Value, error) {
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
package compiler
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var basicTypes = map[string]int64{
|
||||
"bool": 1,
|
||||
"int": 2,
|
||||
"int8": 3,
|
||||
"int16": 4,
|
||||
"int32": 5,
|
||||
"int64": 6,
|
||||
"uint": 7,
|
||||
"uint8": 8,
|
||||
"uint16": 9,
|
||||
"uint32": 10,
|
||||
"uint64": 11,
|
||||
"uintptr": 12,
|
||||
"float32": 13,
|
||||
"float64": 14,
|
||||
"complex64": 15,
|
||||
"complex128": 16,
|
||||
"string": 17,
|
||||
"unsafeptr": 18,
|
||||
}
|
||||
|
||||
func (c *Compiler) assignTypeCodes(typeSlice typeInfoSlice) {
|
||||
fn := c.mod.NamedFunction("reflect.ValueOf")
|
||||
if fn.IsNil() {
|
||||
// reflect.ValueOf is never used, so we can use the most efficient
|
||||
// encoding possible.
|
||||
for i, t := range typeSlice {
|
||||
t.num = uint64(i + 1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Assign typecodes the way the reflect package expects.
|
||||
fallbackIndex := 1
|
||||
namedTypes := make(map[string]int)
|
||||
for _, t := range typeSlice {
|
||||
if t.name[:5] != "type:" {
|
||||
panic("expected type name to start with 'type:'")
|
||||
}
|
||||
num := c.getTypeCodeNum(t.name[5:], &fallbackIndex, namedTypes)
|
||||
if num.BitLen() > c.uintptrType.IntTypeWidth() || !num.IsUint64() {
|
||||
// TODO: support this in some way, using a side table for example.
|
||||
// That's less efficient but better than not working at all.
|
||||
// Particularly important on systems with 16-bit pointers (e.g.
|
||||
// AVR).
|
||||
panic("compiler: could not store type code number inside interface type code")
|
||||
}
|
||||
t.num = num.Uint64()
|
||||
}
|
||||
}
|
||||
|
||||
// getTypeCodeNum returns the typecode for a given type as expected by the
|
||||
// reflect package. Also see getTypeCodeName, which serializes types to a string
|
||||
// based on a types.Type value for this function.
|
||||
func (c *Compiler) getTypeCodeNum(id string, fallbackIndex *int, namedTypes map[string]int) *big.Int {
|
||||
// Note: see src/reflect/type.go for bit allocations.
|
||||
// A type can be named or unnamed. Example of both:
|
||||
// basic:~foo:uint64
|
||||
// basic:uint64
|
||||
// Extract the class (basic, slice, pointer, etc.), the name, and the
|
||||
// contents of this type ID string. Allocate bits based on that, as
|
||||
// src/runtime/types.go expects.
|
||||
class := id[:strings.IndexByte(id, ':')]
|
||||
value := id[len(class)+1:]
|
||||
name := ""
|
||||
if value[0] == '~' {
|
||||
name = value[1:strings.IndexByte(value, ':')]
|
||||
value = value[len(name)+2:]
|
||||
}
|
||||
if class == "basic" {
|
||||
// Basic types follow the following bit pattern:
|
||||
// ...xxxxx0
|
||||
// where xxxxx is allocated for the 18 possible basic types and all the
|
||||
// upper bits are used to indicate the named type.
|
||||
num, ok := basicTypes[value]
|
||||
if !ok {
|
||||
panic("invalid basic type: " + id)
|
||||
}
|
||||
if name != "" {
|
||||
// This type is named, set the upper bits to the name ID.
|
||||
num |= int64(getNamedTypeNum(namedTypes, name)) << 5
|
||||
}
|
||||
return big.NewInt(num << 1)
|
||||
} else {
|
||||
// Complex types use the following bit pattern:
|
||||
// ...nxxx1
|
||||
// where xxx indicates the complex type (any non-basic type). The upper
|
||||
// bits contain whatever the type contains. Types that wrap a single
|
||||
// other type (channel, interface, pointer, slice) just contain the bits
|
||||
// of the wrapped type. Other types (like struct) have a different
|
||||
// method of encoding the contents of the type.
|
||||
var num *big.Int
|
||||
var classNumber int64
|
||||
switch class {
|
||||
case "chan":
|
||||
num = c.getTypeCodeNum(value, fallbackIndex, namedTypes)
|
||||
classNumber = 0
|
||||
case "interface":
|
||||
num = big.NewInt(int64(*fallbackIndex))
|
||||
*fallbackIndex++
|
||||
classNumber = 1
|
||||
case "pointer":
|
||||
num = c.getTypeCodeNum(value, fallbackIndex, namedTypes)
|
||||
classNumber = 2
|
||||
case "slice":
|
||||
num = c.getTypeCodeNum(value, fallbackIndex, namedTypes)
|
||||
classNumber = 3
|
||||
case "array":
|
||||
num = big.NewInt(int64(*fallbackIndex))
|
||||
*fallbackIndex++
|
||||
classNumber = 4
|
||||
case "func":
|
||||
num = big.NewInt(int64(*fallbackIndex))
|
||||
*fallbackIndex++
|
||||
classNumber = 5
|
||||
case "map":
|
||||
num = big.NewInt(int64(*fallbackIndex))
|
||||
*fallbackIndex++
|
||||
classNumber = 6
|
||||
case "struct":
|
||||
num = big.NewInt(int64(*fallbackIndex))
|
||||
*fallbackIndex++
|
||||
classNumber = 7
|
||||
default:
|
||||
panic("unknown type kind: " + id)
|
||||
}
|
||||
if name == "" {
|
||||
num.Lsh(num, 5).Or(num, big.NewInt((classNumber<<1)+1))
|
||||
} else {
|
||||
// TODO: store num in a sidetable
|
||||
num = big.NewInt(int64(getNamedTypeNum(namedTypes, name))<<1 | 1)
|
||||
num.Lsh(num, 4).Or(num, big.NewInt((classNumber<<1)+1))
|
||||
}
|
||||
return num
|
||||
}
|
||||
}
|
||||
|
||||
// getNamedTypeNum returns an appropriate (unique) number for the given named
|
||||
// type. If the name already has a number that number is returned, else a new
|
||||
// number is returned. The number is always non-zero.
|
||||
func getNamedTypeNum(namedTypes map[string]int, name string) int {
|
||||
if num, ok := namedTypes[name]; ok {
|
||||
return num
|
||||
} else {
|
||||
num = len(namedTypes) + 1
|
||||
namedTypes[name] = num
|
||||
return num
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package compiler
|
||||
|
||||
// This file implements the syscall.Syscall and syscall.Syscall6 instructions as
|
||||
// compiler builtins.
|
||||
|
||||
import (
|
||||
"go/constant"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/tools/go/ssa"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// emitSyscall emits an inline system call instruction, depending on the target
|
||||
// OS/arch.
|
||||
func (c *Compiler) emitSyscall(frame *Frame, call *ssa.CallCommon) (llvm.Value, error) {
|
||||
num, _ := constant.Uint64Val(call.Args[0].(*ssa.Const).Value)
|
||||
var syscallResult llvm.Value
|
||||
switch {
|
||||
case c.GOARCH == "amd64" && c.GOOS == "linux":
|
||||
// Sources:
|
||||
// https://stackoverflow.com/a/2538212
|
||||
// https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux#syscall
|
||||
args := []llvm.Value{llvm.ConstInt(c.uintptrType, num, false)}
|
||||
argTypes := []llvm.Type{c.uintptrType}
|
||||
// Constraints will look something like:
|
||||
// "={rax},0,{rdi},{rsi},{rdx},{r10},{r8},{r9},~{rcx},~{r11}"
|
||||
constraints := "={rax},0"
|
||||
for i, arg := range call.Args[1:] {
|
||||
constraints += "," + [...]string{
|
||||
"{rdi}",
|
||||
"{rsi}",
|
||||
"{rdx}",
|
||||
"{r10}",
|
||||
"{r8}",
|
||||
"{r9}",
|
||||
}[i]
|
||||
llvmValue, err := c.parseExpr(frame, arg)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
args = append(args, llvmValue)
|
||||
argTypes = append(argTypes, llvmValue.Type())
|
||||
}
|
||||
constraints += ",~{rcx},~{r11}"
|
||||
fnType := llvm.FunctionType(c.uintptrType, argTypes, false)
|
||||
target := llvm.InlineAsm(fnType, "syscall", constraints, true, false, llvm.InlineAsmDialectIntel)
|
||||
syscallResult = c.builder.CreateCall(target, args, "")
|
||||
case c.GOARCH == "arm" && c.GOOS == "linux":
|
||||
// Implement the EABI system call convention for Linux.
|
||||
// Source: syscall(2) man page.
|
||||
args := []llvm.Value{}
|
||||
argTypes := []llvm.Type{}
|
||||
// Constraints will look something like:
|
||||
// ={r0},0,{r1},{r2},{r7},~{r3}
|
||||
constraints := "={r0}"
|
||||
for i, arg := range call.Args[1:] {
|
||||
constraints += "," + [...]string{
|
||||
"0", // tie to output
|
||||
"{r1}",
|
||||
"{r2}",
|
||||
"{r3}",
|
||||
"{r4}",
|
||||
"{r5}",
|
||||
"{r6}",
|
||||
}[i]
|
||||
llvmValue, err := c.parseExpr(frame, arg)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
args = append(args, llvmValue)
|
||||
argTypes = append(argTypes, llvmValue.Type())
|
||||
}
|
||||
args = append(args, llvm.ConstInt(c.uintptrType, num, false))
|
||||
argTypes = append(argTypes, c.uintptrType)
|
||||
constraints += ",{r7}" // syscall number
|
||||
for i := len(call.Args) - 1; i < 4; i++ {
|
||||
// r0-r3 get clobbered after the syscall returns
|
||||
constraints += ",~{r" + strconv.Itoa(i) + "}"
|
||||
}
|
||||
fnType := llvm.FunctionType(c.uintptrType, argTypes, false)
|
||||
target := llvm.InlineAsm(fnType, "svc #0", constraints, true, false, 0)
|
||||
syscallResult = c.builder.CreateCall(target, args, "")
|
||||
case c.GOARCH == "arm64" && c.GOOS == "linux":
|
||||
// Source: syscall(2) man page.
|
||||
args := []llvm.Value{}
|
||||
argTypes := []llvm.Type{}
|
||||
// Constraints will look something like:
|
||||
// ={x0},0,{x1},{x2},{x8},~{x3},~{x4},~{x5},~{x6},~{x7},~{x16},~{x17}
|
||||
constraints := "={x0}"
|
||||
for i, arg := range call.Args[1:] {
|
||||
constraints += "," + [...]string{
|
||||
"0", // tie to output
|
||||
"{x1}",
|
||||
"{x2}",
|
||||
"{x3}",
|
||||
"{x4}",
|
||||
"{x5}",
|
||||
}[i]
|
||||
llvmValue, err := c.parseExpr(frame, arg)
|
||||
if err != nil {
|
||||
return llvm.Value{}, err
|
||||
}
|
||||
args = append(args, llvmValue)
|
||||
argTypes = append(argTypes, llvmValue.Type())
|
||||
}
|
||||
args = append(args, llvm.ConstInt(c.uintptrType, num, false))
|
||||
argTypes = append(argTypes, c.uintptrType)
|
||||
constraints += ",{x8}" // syscall number
|
||||
for i := len(call.Args) - 1; i < 8; i++ {
|
||||
// x0-x7 may get clobbered during the syscall following the aarch64
|
||||
// calling convention.
|
||||
constraints += ",~{x" + strconv.Itoa(i) + "}"
|
||||
}
|
||||
constraints += ",~{x16},~{x17}" // scratch registers
|
||||
fnType := llvm.FunctionType(c.uintptrType, argTypes, false)
|
||||
target := llvm.InlineAsm(fnType, "svc #0", constraints, true, false, 0)
|
||||
syscallResult = c.builder.CreateCall(target, args, "")
|
||||
default:
|
||||
return llvm.Value{}, c.makeError(call.Pos(), "unknown GOOS/GOARCH for syscall: "+c.GOOS+"/"+c.GOARCH)
|
||||
}
|
||||
// Return values: r0, r1, err uintptr
|
||||
// Pseudocode:
|
||||
// var err uintptr
|
||||
// if syscallResult < 0 && syscallResult > -4096 {
|
||||
// err = -syscallResult
|
||||
// }
|
||||
// return syscallResult, 0, err
|
||||
zero := llvm.ConstInt(c.uintptrType, 0, false)
|
||||
inrange1 := c.builder.CreateICmp(llvm.IntSLT, syscallResult, llvm.ConstInt(c.uintptrType, 0, false), "")
|
||||
inrange2 := c.builder.CreateICmp(llvm.IntSGT, syscallResult, llvm.ConstInt(c.uintptrType, 0xfffffffffffff000, true), "") // -4096
|
||||
hasError := c.builder.CreateAnd(inrange1, inrange2, "")
|
||||
errResult := c.builder.CreateSelect(hasError, c.builder.CreateNot(syscallResult, ""), zero, "syscallError")
|
||||
retval := llvm.Undef(llvm.StructType([]llvm.Type{c.uintptrType, c.uintptrType, c.uintptrType}, false))
|
||||
retval = c.builder.CreateInsertValue(retval, syscallResult, 0, "")
|
||||
retval = c.builder.CreateInsertValue(retval, zero, 1, "")
|
||||
retval = c.builder.CreateInsertValue(retval, errResult, 2, "")
|
||||
return retval, nil
|
||||
}
|
||||
+2
-1
@@ -308,7 +308,8 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
|
||||
ret = llvm.ConstInsertValue(ret, retLen, []uint32{2}) // cap
|
||||
fr.locals[inst] = &LocalValue{fr.Eval, ret}
|
||||
case callee.Name() == "runtime.makeInterface":
|
||||
fr.locals[inst] = &LocalValue{fr.Eval, llvm.ConstPtrToInt(inst.Operand(0), fr.TargetData.IntPtrType())}
|
||||
uintptrType := callee.Type().Context().IntType(fr.TargetData.PointerSize() * 8)
|
||||
fr.locals[inst] = &LocalValue{fr.Eval, llvm.ConstPtrToInt(inst.Operand(0), uintptrType)}
|
||||
case strings.HasPrefix(callee.Name(), "runtime.print") || callee.Name() == "runtime._panic":
|
||||
// This are all print instructions, which necessarily have side
|
||||
// effects but no results.
|
||||
|
||||
+3
-2
@@ -63,14 +63,15 @@ func Run(mod llvm.Module, targetData llvm.TargetData, debug bool) error {
|
||||
return errors.New("expected all instructions in " + name + " to be *.init() calls")
|
||||
}
|
||||
pkgName := initName[:len(initName)-5]
|
||||
_, err := e.Function(call.CalledValue(), []Value{&LocalValue{e, undefPtr}, &LocalValue{e, undefPtr}}, pkgName)
|
||||
fn := call.CalledValue()
|
||||
call.EraseFromParentAsInstruction()
|
||||
_, err := e.Function(fn, []Value{&LocalValue{e, undefPtr}, &LocalValue{e, undefPtr}}, pkgName)
|
||||
if err == ErrUnreachable {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
call.EraseFromParentAsInstruction()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+21
-7
@@ -24,6 +24,13 @@ type sideEffectResult struct {
|
||||
// returns whether this function has side effects and if it does, which globals
|
||||
// it mentions anywhere in this function or any called functions.
|
||||
func (e *Eval) hasSideEffects(fn llvm.Value) *sideEffectResult {
|
||||
switch fn.Name() {
|
||||
case "runtime.alloc":
|
||||
// Cannot be scanned but can be interpreted.
|
||||
return &sideEffectResult{severity: sideEffectNone}
|
||||
case "runtime._panic":
|
||||
return &sideEffectResult{severity: sideEffectLimited}
|
||||
}
|
||||
if e.sideEffectFuncs == nil {
|
||||
e.sideEffectFuncs = make(map[llvm.Value]*sideEffectResult)
|
||||
}
|
||||
@@ -73,25 +80,32 @@ func (e *Eval) hasSideEffects(fn llvm.Value) *sideEffectResult {
|
||||
result.updateSeverity(sideEffectAll)
|
||||
continue
|
||||
}
|
||||
name := child.Name()
|
||||
if child.IsDeclaration() {
|
||||
if name == "runtime.makeInterface" {
|
||||
switch child.Name() {
|
||||
case "runtime.makeInterface":
|
||||
// Can be interpreted so does not have side effects.
|
||||
continue
|
||||
}
|
||||
// External function call. Assume only limited side effects
|
||||
// (no affected globals, etc.).
|
||||
if result.hasLocalSideEffects(dirtyLocals, inst) {
|
||||
if e.hasLocalSideEffects(dirtyLocals, inst) {
|
||||
result.updateSeverity(sideEffectLimited)
|
||||
}
|
||||
continue
|
||||
}
|
||||
childSideEffects := e.hasSideEffects(fn)
|
||||
childSideEffects := e.hasSideEffects(child)
|
||||
switch childSideEffects.severity {
|
||||
case sideEffectInProgress, sideEffectNone:
|
||||
// no side effects or recursive function - continue scanning
|
||||
case sideEffectLimited:
|
||||
// The return value may be problematic.
|
||||
if e.hasLocalSideEffects(dirtyLocals, inst) {
|
||||
result.updateSeverity(sideEffectLimited)
|
||||
}
|
||||
case sideEffectAll:
|
||||
result.updateSeverity(sideEffectAll)
|
||||
default:
|
||||
result.update(childSideEffects)
|
||||
panic("unreachable")
|
||||
}
|
||||
case llvm.Load, llvm.Store:
|
||||
if inst.IsVolatile() {
|
||||
@@ -118,7 +132,7 @@ func (e *Eval) hasSideEffects(fn llvm.Value) *sideEffectResult {
|
||||
// hasLocalSideEffects checks whether the given instruction flows into a branch
|
||||
// or return instruction, in which case the whole function must be marked as
|
||||
// having side effects and be called at runtime.
|
||||
func (r *sideEffectResult) hasLocalSideEffects(dirtyLocals map[llvm.Value]struct{}, inst llvm.Value) bool {
|
||||
func (e *Eval) hasLocalSideEffects(dirtyLocals map[llvm.Value]struct{}, inst llvm.Value) bool {
|
||||
if _, ok := dirtyLocals[inst]; ok {
|
||||
// It is already known that this local is dirty.
|
||||
return true
|
||||
@@ -156,7 +170,7 @@ func (r *sideEffectResult) hasLocalSideEffects(dirtyLocals map[llvm.Value]struct
|
||||
// For a list:
|
||||
// https://godoc.org/github.com/llvm-mirror/llvm/bindings/go/llvm#Opcode
|
||||
dirtyLocals[user] = struct{}{}
|
||||
if r.hasLocalSideEffects(dirtyLocals, user) {
|
||||
if e.hasLocalSideEffects(dirtyLocals, user) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,10 +269,16 @@ func (f *Function) parsePragmas() {
|
||||
}
|
||||
if decl, ok := f.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil {
|
||||
for _, comment := range decl.Doc.List {
|
||||
if !strings.HasPrefix(comment.Text, "//go:") {
|
||||
text := comment.Text
|
||||
if strings.HasPrefix(text, "//export ") {
|
||||
// Rewrite '//export' to '//go:export' for compatibility with
|
||||
// gc.
|
||||
text = "//go:" + text[2:]
|
||||
}
|
||||
if !strings.HasPrefix(text, "//go:") {
|
||||
continue
|
||||
}
|
||||
parts := strings.Fields(comment.Text)
|
||||
parts := strings.Fields(text)
|
||||
switch parts[0] {
|
||||
case "//go:export":
|
||||
if len(parts) != 2 {
|
||||
@@ -322,7 +328,7 @@ func (f *Function) IsNoBounds() bool {
|
||||
|
||||
// Return true iff this function is externally visible.
|
||||
func (f *Function) IsExported() bool {
|
||||
return f.exported
|
||||
return f.exported || f.CName() != ""
|
||||
}
|
||||
|
||||
// Return true for functions annotated with //go:interrupt. The function name is
|
||||
@@ -330,7 +336,7 @@ func (f *Function) IsExported() bool {
|
||||
//
|
||||
// On some platforms (like AVR), interrupts need a special compiler flag.
|
||||
func (f *Function) IsInterrupt() bool {
|
||||
return f.exported
|
||||
return f.interrupt
|
||||
}
|
||||
|
||||
// Return the link name for this function.
|
||||
@@ -389,11 +395,25 @@ func (g *Global) LinkName() string {
|
||||
if g.linkName != "" {
|
||||
return g.linkName
|
||||
}
|
||||
if name := g.CName(); name != "" {
|
||||
return name
|
||||
}
|
||||
return g.RelString(nil)
|
||||
}
|
||||
|
||||
func (g *Global) IsExtern() bool {
|
||||
return g.extern
|
||||
return g.extern || g.CName() != ""
|
||||
}
|
||||
|
||||
// Return the name of the C global if this is a CGo wrapper. Otherwise, return a
|
||||
// zero-length string.
|
||||
func (g *Global) CName() string {
|
||||
name := g.Name()
|
||||
if strings.HasPrefix(name, "C.") {
|
||||
// created by ../loader/cgo.go
|
||||
return name[2:]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (g *Global) Initializer() Value {
|
||||
|
||||
+2
-1
@@ -68,10 +68,11 @@ func (p *Program) SimpleDCE() {
|
||||
// functions.
|
||||
main := p.mainPkg.Members["main"].(*ssa.Function)
|
||||
runtimePkg := p.Program.ImportedPackage("runtime")
|
||||
mathPkg := p.Program.ImportedPackage("math")
|
||||
p.GetFunction(main).flag = true
|
||||
worklist := []*ssa.Function{main}
|
||||
for _, f := range p.Functions {
|
||||
if f.exported || f.Synthetic == "package initializer" || f.Pkg == runtimePkg {
|
||||
if f.exported || f.Synthetic == "package initializer" || f.Pkg == runtimePkg || (f.Pkg == mathPkg && f.Pkg != nil) {
|
||||
if f.flag || isCGoInternal(f.Name()) {
|
||||
continue
|
||||
}
|
||||
|
||||
+158
-106
@@ -9,36 +9,41 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
)
|
||||
|
||||
// fileInfo holds all Cgo-related information of a given *ast.File.
|
||||
type fileInfo struct {
|
||||
*ast.File
|
||||
filename string
|
||||
functions []*functionInfo
|
||||
typedefs []*typedefInfo
|
||||
functions map[string]*functionInfo
|
||||
globals map[string]*globalInfo
|
||||
typedefs map[string]*typedefInfo
|
||||
importCPos token.Pos
|
||||
}
|
||||
|
||||
// functionInfo stores some information about a Cgo function found by libclang
|
||||
// and declared in the AST.
|
||||
type functionInfo struct {
|
||||
name string
|
||||
args []paramInfo
|
||||
result string
|
||||
args []paramInfo
|
||||
results *ast.FieldList
|
||||
}
|
||||
|
||||
// paramInfo is a parameter of a Cgo function (see functionInfo).
|
||||
type paramInfo struct {
|
||||
name string
|
||||
typeName string
|
||||
typeExpr ast.Expr
|
||||
}
|
||||
|
||||
// typedefInfo contains information about a single typedef in C.
|
||||
type typedefInfo struct {
|
||||
newName string // newly defined type name
|
||||
oldName string // old type name, may be something like "unsigned long long"
|
||||
size int // size in bytes
|
||||
typeExpr ast.Expr
|
||||
}
|
||||
|
||||
// globalInfo contains information about a declared global variable in C.
|
||||
type globalInfo struct {
|
||||
typeExpr ast.Expr
|
||||
}
|
||||
|
||||
// cgoAliases list type aliases between Go and C, for types that are equivalent
|
||||
@@ -75,8 +80,11 @@ typedef unsigned long long _Cgo_ulonglong;
|
||||
// comment with libclang, and modifies the AST to use this information.
|
||||
func (p *Package) processCgo(filename string, f *ast.File, cflags []string) error {
|
||||
info := &fileInfo{
|
||||
File: f,
|
||||
filename: filename,
|
||||
File: f,
|
||||
filename: filename,
|
||||
functions: map[string]*functionInfo{},
|
||||
globals: map[string]*globalInfo{},
|
||||
typedefs: map[string]*typedefInfo{},
|
||||
}
|
||||
|
||||
// Find `import "C"` statements in the file.
|
||||
@@ -122,6 +130,12 @@ func (p *Package) processCgo(filename string, f *ast.File, cflags []string) erro
|
||||
// Declare functions found by libclang.
|
||||
info.addFuncDecls()
|
||||
|
||||
// Declare stub function pointer values found by libclang.
|
||||
info.addFuncPtrDecls()
|
||||
|
||||
// Declare globals found by libclang.
|
||||
info.addVarDecls()
|
||||
|
||||
// Forward C types to Go types (like C.uint32_t -> uint32).
|
||||
info.addTypeAliases()
|
||||
|
||||
@@ -129,7 +143,7 @@ func (p *Package) processCgo(filename string, f *ast.File, cflags []string) erro
|
||||
info.addTypedefs()
|
||||
|
||||
// Patch the AST to use the declared types and functions.
|
||||
ast.Inspect(f, info.walker)
|
||||
f = astutil.Apply(f, info.walker, nil).(*ast.File)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -139,16 +153,22 @@ func (p *Package) processCgo(filename string, f *ast.File, cflags []string) erro
|
||||
func (info *fileInfo) addFuncDecls() {
|
||||
// TODO: replace all uses of importCPos with the real locations from
|
||||
// libclang.
|
||||
for _, fn := range info.functions {
|
||||
names := make([]string, 0, len(info.functions))
|
||||
for name := range info.functions {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
fn := info.functions[name]
|
||||
obj := &ast.Object{
|
||||
Kind: ast.Fun,
|
||||
Name: mapCgoType(fn.name),
|
||||
Name: "C." + name,
|
||||
}
|
||||
args := make([]*ast.Field, len(fn.args))
|
||||
decl := &ast.FuncDecl{
|
||||
Name: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: mapCgoType(fn.name),
|
||||
Name: "C." + name,
|
||||
Obj: obj,
|
||||
},
|
||||
Type: &ast.FuncType{
|
||||
@@ -158,16 +178,7 @@ func (info *fileInfo) addFuncDecls() {
|
||||
List: args,
|
||||
Closing: info.importCPos,
|
||||
},
|
||||
Results: &ast.FieldList{
|
||||
List: []*ast.Field{
|
||||
&ast.Field{
|
||||
Type: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: mapCgoType(fn.result),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Results: fn.results,
|
||||
},
|
||||
}
|
||||
obj.Decl = decl
|
||||
@@ -179,21 +190,107 @@ func (info *fileInfo) addFuncDecls() {
|
||||
Name: arg.name,
|
||||
Obj: &ast.Object{
|
||||
Kind: ast.Var,
|
||||
Name: mapCgoType(arg.name),
|
||||
Name: arg.name,
|
||||
Decl: decl,
|
||||
},
|
||||
},
|
||||
},
|
||||
Type: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: mapCgoType(arg.typeName),
|
||||
},
|
||||
Type: arg.typeExpr,
|
||||
}
|
||||
}
|
||||
info.Decls = append(info.Decls, decl)
|
||||
}
|
||||
}
|
||||
|
||||
// addFuncPtrDecls creates stub declarations of function pointer values. These
|
||||
// values will later be replaced with the real values in the compiler.
|
||||
// It adds code like the following to the AST:
|
||||
//
|
||||
// var (
|
||||
// C.add unsafe.Pointer
|
||||
// C.mul unsafe.Pointer
|
||||
// // ...
|
||||
// )
|
||||
func (info *fileInfo) addFuncPtrDecls() {
|
||||
gen := &ast.GenDecl{
|
||||
TokPos: info.importCPos,
|
||||
Tok: token.VAR,
|
||||
Lparen: info.importCPos,
|
||||
Rparen: info.importCPos,
|
||||
}
|
||||
names := make([]string, 0, len(info.functions))
|
||||
for name := range info.functions {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
obj := &ast.Object{
|
||||
Kind: ast.Typ,
|
||||
Name: "C." + name + "$funcaddr",
|
||||
}
|
||||
valueSpec := &ast.ValueSpec{
|
||||
Names: []*ast.Ident{&ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "C." + name + "$funcaddr",
|
||||
Obj: obj,
|
||||
}},
|
||||
Type: &ast.SelectorExpr{
|
||||
X: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "unsafe",
|
||||
},
|
||||
Sel: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "Pointer",
|
||||
},
|
||||
},
|
||||
}
|
||||
obj.Decl = valueSpec
|
||||
gen.Specs = append(gen.Specs, valueSpec)
|
||||
}
|
||||
info.Decls = append(info.Decls, gen)
|
||||
}
|
||||
|
||||
// addVarDecls declares external C globals in the Go source.
|
||||
// It adds code like the following to the AST:
|
||||
//
|
||||
// var (
|
||||
// C.globalInt int
|
||||
// C.globalBool bool
|
||||
// // ...
|
||||
// )
|
||||
func (info *fileInfo) addVarDecls() {
|
||||
gen := &ast.GenDecl{
|
||||
TokPos: info.importCPos,
|
||||
Tok: token.VAR,
|
||||
Lparen: info.importCPos,
|
||||
Rparen: info.importCPos,
|
||||
}
|
||||
names := make([]string, 0, len(info.globals))
|
||||
for name := range info.globals {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
global := info.globals[name]
|
||||
obj := &ast.Object{
|
||||
Kind: ast.Typ,
|
||||
Name: "C." + name,
|
||||
}
|
||||
valueSpec := &ast.ValueSpec{
|
||||
Names: []*ast.Ident{&ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "C." + name,
|
||||
Obj: obj,
|
||||
}},
|
||||
Type: global.typeExpr,
|
||||
}
|
||||
obj.Decl = valueSpec
|
||||
gen.Specs = append(gen.Specs, valueSpec)
|
||||
}
|
||||
info.Decls = append(info.Decls, gen)
|
||||
}
|
||||
|
||||
// addTypeAliases aliases some built-in Go types with their equivalent C types.
|
||||
// It adds code like the following to the AST:
|
||||
//
|
||||
@@ -243,55 +340,32 @@ func (info *fileInfo) addTypedefs() {
|
||||
TokPos: info.importCPos,
|
||||
Tok: token.TYPE,
|
||||
}
|
||||
for _, typedef := range info.typedefs {
|
||||
newType := mapCgoType(typedef.newName)
|
||||
oldType := mapCgoType(typedef.oldName)
|
||||
switch oldType {
|
||||
// TODO: plain char (may be signed or unsigned)
|
||||
case "C.schar", "C.short", "C.int", "C.long", "C.longlong":
|
||||
switch typedef.size {
|
||||
case 1:
|
||||
oldType = "int8"
|
||||
case 2:
|
||||
oldType = "int16"
|
||||
case 4:
|
||||
oldType = "int32"
|
||||
case 8:
|
||||
oldType = "int64"
|
||||
}
|
||||
case "C.uchar", "C.ushort", "C.uint", "C.ulong", "C.ulonglong":
|
||||
switch typedef.size {
|
||||
case 1:
|
||||
oldType = "uint8"
|
||||
case 2:
|
||||
oldType = "uint16"
|
||||
case 4:
|
||||
oldType = "uint32"
|
||||
case 8:
|
||||
oldType = "uint64"
|
||||
}
|
||||
names := make([]string, 0, len(info.typedefs))
|
||||
for name := range info.typedefs {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
typedef := info.typedefs[name]
|
||||
typeName := "C." + name
|
||||
if strings.HasPrefix(name, "_Cgo_") {
|
||||
typeName = "C." + name[len("_Cgo_"):]
|
||||
}
|
||||
if strings.HasPrefix(newType, "C._Cgo_") {
|
||||
newType = "C." + newType[len("C._Cgo_"):]
|
||||
}
|
||||
if _, ok := cgoAliases[newType]; ok {
|
||||
if _, ok := cgoAliases[typeName]; ok {
|
||||
// This is a type that also exists in Go (defined in stdint.h).
|
||||
continue
|
||||
}
|
||||
obj := &ast.Object{
|
||||
Kind: ast.Typ,
|
||||
Name: newType,
|
||||
Name: typeName,
|
||||
}
|
||||
typeSpec := &ast.TypeSpec{
|
||||
Name: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: newType,
|
||||
Name: typeName,
|
||||
Obj: obj,
|
||||
},
|
||||
Type: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: oldType,
|
||||
},
|
||||
Type: typedef.typeExpr,
|
||||
}
|
||||
obj.Decl = typeSpec
|
||||
gen.Specs = append(gen.Specs, typeSpec)
|
||||
@@ -299,12 +373,12 @@ func (info *fileInfo) addTypedefs() {
|
||||
info.Decls = append(info.Decls, gen)
|
||||
}
|
||||
|
||||
// walker replaces all "C".<something> call expressions to literal
|
||||
// "C.<something>" expressions. This is impossible to write in Go (a dot cannot
|
||||
// be used in the middle of a name) so is used as a new namespace for C call
|
||||
// expressions.
|
||||
func (info *fileInfo) walker(node ast.Node) bool {
|
||||
switch node := node.(type) {
|
||||
// walker replaces all "C".<something> expressions to literal "C.<something>"
|
||||
// expressions. Such expressions are impossible to write in Go (a dot cannot be
|
||||
// used in the middle of a name) so in practice all C identifiers live in a
|
||||
// separate namespace (no _Cgo_ hacks like in gc).
|
||||
func (info *fileInfo) walker(cursor *astutil.Cursor) bool {
|
||||
switch node := cursor.Node().(type) {
|
||||
case *ast.CallExpr:
|
||||
fun, ok := node.Fun.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
@@ -314,49 +388,27 @@ func (info *fileInfo) walker(node ast.Node) bool {
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if x.Name == "C" {
|
||||
if _, ok := info.functions[fun.Sel.Name]; ok && x.Name == "C" {
|
||||
node.Fun = &ast.Ident{
|
||||
NamePos: x.NamePos,
|
||||
Name: mapCgoType(fun.Sel.Name),
|
||||
Name: "C." + fun.Sel.Name,
|
||||
}
|
||||
}
|
||||
case *ast.ValueSpec:
|
||||
typ, ok := node.Type.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
x, ok := typ.X.(*ast.Ident)
|
||||
case *ast.SelectorExpr:
|
||||
x, ok := node.X.(*ast.Ident)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if x.Name == "C" {
|
||||
node.Type = &ast.Ident{
|
||||
NamePos: x.NamePos,
|
||||
Name: mapCgoType(typ.Sel.Name),
|
||||
name := "C." + node.Sel.Name
|
||||
if _, ok := info.functions[node.Sel.Name]; ok {
|
||||
name += "$funcaddr"
|
||||
}
|
||||
cursor.Replace(&ast.Ident{
|
||||
NamePos: x.NamePos,
|
||||
Name: name,
|
||||
})
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// mapCgoType converts a C type name into a Go type name with a "C." prefix.
|
||||
func mapCgoType(t string) string {
|
||||
switch t {
|
||||
case "signed char":
|
||||
return "C.schar"
|
||||
case "long long":
|
||||
return "C.longlong"
|
||||
case "unsigned char":
|
||||
return "C.schar"
|
||||
case "unsigned short":
|
||||
return "C.ushort"
|
||||
case "unsigned int":
|
||||
return "C.uint"
|
||||
case "unsigned long":
|
||||
return "C.ulong"
|
||||
case "unsigned long long":
|
||||
return "C.ulonglong"
|
||||
default:
|
||||
return "C." + t
|
||||
}
|
||||
}
|
||||
|
||||
+121
-15
@@ -5,6 +5,10 @@ package loader
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -88,30 +92,74 @@ func tinygo_clang_visitor(c, parent C.CXCursor, client_data C.CXClientData) C.in
|
||||
if C.clang_isFunctionTypeVariadic(cursorType) != 0 {
|
||||
return C.CXChildVisit_Continue // not supported
|
||||
}
|
||||
numArgs := C.clang_Cursor_getNumArguments(c)
|
||||
fn := &functionInfo{name: name}
|
||||
info.functions = append(info.functions, fn)
|
||||
for i := C.int(0); i < numArgs; i++ {
|
||||
numArgs := int(C.clang_Cursor_getNumArguments(c))
|
||||
fn := &functionInfo{}
|
||||
info.functions[name] = fn
|
||||
for i := 0; i < numArgs; i++ {
|
||||
arg := C.clang_Cursor_getArgument(c, C.uint(i))
|
||||
argName := getString(C.clang_getCursorSpelling(arg))
|
||||
argType := C.clang_getArgType(cursorType, C.uint(i))
|
||||
argTypeName := getString(C.clang_getTypeSpelling(argType))
|
||||
fn.args = append(fn.args, paramInfo{argName, argTypeName})
|
||||
if argName == "" {
|
||||
argName = "$" + strconv.Itoa(i)
|
||||
}
|
||||
fn.args = append(fn.args, paramInfo{
|
||||
name: argName,
|
||||
typeExpr: info.makeASTType(argType),
|
||||
})
|
||||
}
|
||||
resultType := C.clang_getCursorResultType(c)
|
||||
resultTypeName := getString(C.clang_getTypeSpelling(resultType))
|
||||
fn.result = resultTypeName
|
||||
if resultType.kind != C.CXType_Void {
|
||||
fn.results = &ast.FieldList{
|
||||
List: []*ast.Field{
|
||||
&ast.Field{
|
||||
Type: info.makeASTType(resultType),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
case C.CXCursor_TypedefDecl:
|
||||
typedefType := C.clang_getCursorType(c)
|
||||
name := getString(C.clang_getTypedefName(typedefType))
|
||||
underlyingType := C.clang_getTypedefDeclUnderlyingType(c)
|
||||
underlyingTypeName := getString(C.clang_getTypeSpelling(underlyingType))
|
||||
typeSize := C.clang_Type_getSizeOf(underlyingType)
|
||||
info.typedefs = append(info.typedefs, &typedefInfo{
|
||||
newName: name,
|
||||
oldName: underlyingTypeName,
|
||||
size: int(typeSize),
|
||||
})
|
||||
expr := info.makeASTType(underlyingType)
|
||||
if strings.HasPrefix(name, "_Cgo_") {
|
||||
expr := expr.(*ast.Ident)
|
||||
typeSize := C.clang_Type_getSizeOf(underlyingType)
|
||||
switch expr.Name {
|
||||
// TODO: plain char (may be signed or unsigned)
|
||||
case "C.schar", "C.short", "C.int", "C.long", "C.longlong":
|
||||
switch typeSize {
|
||||
case 1:
|
||||
expr.Name = "int8"
|
||||
case 2:
|
||||
expr.Name = "int16"
|
||||
case 4:
|
||||
expr.Name = "int32"
|
||||
case 8:
|
||||
expr.Name = "int64"
|
||||
}
|
||||
case "C.uchar", "C.ushort", "C.uint", "C.ulong", "C.ulonglong":
|
||||
switch typeSize {
|
||||
case 1:
|
||||
expr.Name = "uint8"
|
||||
case 2:
|
||||
expr.Name = "uint16"
|
||||
case 4:
|
||||
expr.Name = "uint32"
|
||||
case 8:
|
||||
expr.Name = "uint64"
|
||||
}
|
||||
}
|
||||
}
|
||||
info.typedefs[name] = &typedefInfo{
|
||||
typeExpr: expr,
|
||||
}
|
||||
case C.CXCursor_VarDecl:
|
||||
name := getString(C.clang_getCursorSpelling(c))
|
||||
cursorType := C.clang_getCursorType(c)
|
||||
info.globals[name] = &globalInfo{
|
||||
typeExpr: info.makeASTType(cursorType),
|
||||
}
|
||||
}
|
||||
return C.CXChildVisit_Continue
|
||||
}
|
||||
@@ -122,3 +170,61 @@ func getString(clangString C.CXString) (s string) {
|
||||
C.clang_disposeString(clangString)
|
||||
return
|
||||
}
|
||||
|
||||
// makeASTType return the ast.Expr for the given libclang type. In other words,
|
||||
// it converts a libclang type to a type in the Go AST.
|
||||
func (info *fileInfo) makeASTType(typ C.CXType) ast.Expr {
|
||||
var typeName string
|
||||
switch typ.kind {
|
||||
case C.CXType_SChar:
|
||||
typeName = "schar"
|
||||
case C.CXType_UChar:
|
||||
typeName = "uchar"
|
||||
case C.CXType_Short:
|
||||
typeName = "short"
|
||||
case C.CXType_UShort:
|
||||
typeName = "ushort"
|
||||
case C.CXType_Int:
|
||||
typeName = "int"
|
||||
case C.CXType_UInt:
|
||||
typeName = "uint"
|
||||
case C.CXType_Long:
|
||||
typeName = "long"
|
||||
case C.CXType_ULong:
|
||||
typeName = "ulong"
|
||||
case C.CXType_LongLong:
|
||||
typeName = "longlong"
|
||||
case C.CXType_ULongLong:
|
||||
typeName = "ulonglong"
|
||||
case C.CXType_Pointer:
|
||||
return &ast.StarExpr{
|
||||
Star: info.importCPos,
|
||||
X: info.makeASTType(C.clang_getPointeeType(typ)),
|
||||
}
|
||||
case C.CXType_FunctionProto:
|
||||
// Be compatible with gc, which uses the *[0]byte type for function
|
||||
// pointer types.
|
||||
// Return type [0]byte because this is a function type, not a pointer to
|
||||
// this function type.
|
||||
return &ast.ArrayType{
|
||||
Lbrack: info.importCPos,
|
||||
Len: &ast.BasicLit{
|
||||
ValuePos: info.importCPos,
|
||||
Kind: token.INT,
|
||||
Value: "0",
|
||||
},
|
||||
Elt: &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "byte",
|
||||
},
|
||||
}
|
||||
default:
|
||||
// Fallback, probably incorrect but at least the error points to an odd
|
||||
// type name.
|
||||
typeName = getString(C.clang_getTypeSpelling(typ))
|
||||
}
|
||||
return &ast.Ident{
|
||||
NamePos: info.importCPos,
|
||||
Name: "C." + typeName,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@@ -455,6 +456,8 @@ func Run(pkgName, target string, config *BuildConfig) error {
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, "TinyGo is a Go compiler for small places.")
|
||||
fmt.Fprintln(os.Stderr, "version:", version)
|
||||
fmt.Fprintf(os.Stderr, "usage: %s command [-printir] [-target=<target>] -o <output> <input>\n", os.Args[0])
|
||||
fmt.Fprintln(os.Stderr, "\ncommands:")
|
||||
fmt.Fprintln(os.Stderr, " build: compile packages and dependencies")
|
||||
@@ -601,6 +604,8 @@ func main() {
|
||||
}
|
||||
case "help":
|
||||
usage()
|
||||
case "version":
|
||||
fmt.Printf("tinygo version %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
|
||||
default:
|
||||
fmt.Fprintln(os.Stderr, "Unknown command:", command)
|
||||
usage()
|
||||
|
||||
+29
-2
@@ -23,6 +23,9 @@ func TestCompiler(t *testing.T) {
|
||||
}
|
||||
|
||||
dirMatches, err := filepath.Glob(TESTDATA + "/*/main.go")
|
||||
if err != nil {
|
||||
t.Fatal("could not read test packages:", err)
|
||||
}
|
||||
if len(matches) == 0 || len(dirMatches) == 0 {
|
||||
t.Fatal("no test files found")
|
||||
}
|
||||
@@ -39,14 +42,38 @@ func TestCompiler(t *testing.T) {
|
||||
}
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
t.Log("running tests on the host...")
|
||||
t.Log("running tests on host...")
|
||||
for _, path := range matches {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
runTest(path, tmpdir, "", t)
|
||||
})
|
||||
}
|
||||
|
||||
t.Log("running tests on the qemu target...")
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
|
||||
t.Log("running tests for linux/arm...")
|
||||
for _, path := range matches {
|
||||
if path == "testdata/cgo/" {
|
||||
continue // TODO: improve CGo
|
||||
}
|
||||
t.Run(path, func(t *testing.T) {
|
||||
runTest(path, tmpdir, "arm--linux-gnueabi", t)
|
||||
})
|
||||
}
|
||||
|
||||
t.Log("running tests for linux/arm64...")
|
||||
for _, path := range matches {
|
||||
if path == "testdata/cgo/" {
|
||||
continue // TODO: improve CGo
|
||||
}
|
||||
t.Run(path, func(t *testing.T) {
|
||||
runTest(path, tmpdir, "aarch64--linux-gnueabi", t)
|
||||
})
|
||||
}
|
||||
|
||||
t.Log("running tests for emulated cortex-m3...")
|
||||
for _, path := range matches {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
runTest(path, tmpdir, "qemu", t)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Package os implements a subset of the Go "os" package. See
|
||||
// https://godoc.org/os for details.
|
||||
//
|
||||
// Note that the current implementation is blocking. This limitation should be
|
||||
// removed in a future version.
|
||||
package os
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Portable analogs of some common system call errors.
|
||||
var (
|
||||
ErrUnsupported = errors.New("operation not supported")
|
||||
)
|
||||
|
||||
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
|
||||
// standard output, and standard error file descriptors.
|
||||
var (
|
||||
Stdin = &File{0, "/dev/stdin"}
|
||||
Stdout = &File{1, "/dev/stdout"}
|
||||
Stderr = &File{2, "/dev/stderr"}
|
||||
)
|
||||
|
||||
// File represents an open file descriptor.
|
||||
type File struct {
|
||||
fd uintptr
|
||||
name string
|
||||
}
|
||||
|
||||
// NewFile returns a new File with the given file descriptor and name.
|
||||
func NewFile(fd uintptr, name string) *File {
|
||||
return &File{fd, name}
|
||||
}
|
||||
|
||||
// Fd returns the integer Unix file descriptor referencing the open file. The
|
||||
// file descriptor is valid only until f.Close is called.
|
||||
func (f *File) Fd() uintptr {
|
||||
return f.fd
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// +build linux
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Read reads up to len(b) bytes from the File. It returns the number of bytes
|
||||
// read and any error encountered. At end of file, Read returns 0, io.EOF.
|
||||
func (f *File) Read(b []byte) (n int, err error) {
|
||||
return syscall.Read(int(f.fd), b)
|
||||
}
|
||||
|
||||
// Write writes len(b) bytes to the File. It returns the number of bytes written
|
||||
// and an error, if any. Write returns a non-nil error when n != len(b).
|
||||
func (f *File) Write(b []byte) (n int, err error) {
|
||||
return syscall.Write(int(f.fd), b)
|
||||
}
|
||||
|
||||
// Close closes the File, rendering it unusable for I/O.
|
||||
func (f *File) Close() error {
|
||||
return syscall.Close(int(f.fd))
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// +build wasm
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
// Read is unsupported on this system.
|
||||
func (f *File) Read(b []byte) (n int, err error) {
|
||||
return 0, ErrUnsupported
|
||||
}
|
||||
|
||||
// Write writes len(b) bytes to the output. It returns the number of bytes
|
||||
// written or an error if this file is not stdout or stderr.
|
||||
func (f *File) Write(b []byte) (n int, err error) {
|
||||
switch f.fd {
|
||||
case Stdout.fd, Stderr.fd:
|
||||
for _, c := range b {
|
||||
putchar(c)
|
||||
}
|
||||
return len(b), nil
|
||||
default:
|
||||
return 0, ErrUnsupported
|
||||
}
|
||||
}
|
||||
|
||||
// Close is unsupported on this system.
|
||||
func (f *File) Close() error {
|
||||
return ErrUnsupported
|
||||
}
|
||||
|
||||
//go:linkname putchar runtime.putchar
|
||||
func putchar(c byte)
|
||||
+137
-11
@@ -1,9 +1,29 @@
|
||||
package reflect
|
||||
|
||||
// A Kind is the number that the compiler uses for this type.
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// The compiler uses a compact encoding to store type information. Unlike the
|
||||
// main Go compiler, most of the types are stored directly in the type code.
|
||||
//
|
||||
// Not used directly. These types are all replaced with the number the compiler
|
||||
// uses internally for the type.
|
||||
// Type code bit allocation:
|
||||
// xxxxx0: basic types, where xxxxx is the basic type number (never 0).
|
||||
// The higher bits indicate the named type, if any.
|
||||
// nxxx1: complex types, where n indicates whether this is a named type (named
|
||||
// if set) and xxx contains the type kind number:
|
||||
// 0 (0001): Chan
|
||||
// 1 (0011): Interface
|
||||
// 2 (0101): Ptr
|
||||
// 3 (0111): Slice
|
||||
// 4 (1001): Array
|
||||
// 5 (1011): Func
|
||||
// 6 (1101): Map
|
||||
// 7 (1111): Struct
|
||||
// The higher bits are either the contents of the type depending on the
|
||||
// type (if n is clear) or indicate the number of the named type (if n
|
||||
// is set).
|
||||
|
||||
type Kind uintptr
|
||||
|
||||
// Copied from reflect/type.go
|
||||
@@ -26,18 +46,82 @@ const (
|
||||
Float64
|
||||
Complex64
|
||||
Complex128
|
||||
Array
|
||||
String
|
||||
UnsafePointer
|
||||
Chan
|
||||
Func
|
||||
Interface
|
||||
Map
|
||||
Ptr
|
||||
Slice
|
||||
String
|
||||
Array
|
||||
Func
|
||||
Map
|
||||
Struct
|
||||
UnsafePointer
|
||||
)
|
||||
|
||||
func (k Kind) String() string {
|
||||
switch k {
|
||||
case Bool:
|
||||
return "bool"
|
||||
case Int:
|
||||
return "int"
|
||||
case Int8:
|
||||
return "int8"
|
||||
case Int16:
|
||||
return "int16"
|
||||
case Int32:
|
||||
return "int32"
|
||||
case Int64:
|
||||
return "int64"
|
||||
case Uint:
|
||||
return "uint"
|
||||
case Uint8:
|
||||
return "uint8"
|
||||
case Uint16:
|
||||
return "uint16"
|
||||
case Uint32:
|
||||
return "uint32"
|
||||
case Uint64:
|
||||
return "uint64"
|
||||
case Uintptr:
|
||||
return "uintptr"
|
||||
case Float32:
|
||||
return "float32"
|
||||
case Float64:
|
||||
return "float64"
|
||||
case Complex64:
|
||||
return "complex64"
|
||||
case Complex128:
|
||||
return "complex128"
|
||||
case String:
|
||||
return "string"
|
||||
case UnsafePointer:
|
||||
return "unsafe.Pointer"
|
||||
case Chan:
|
||||
return "chan"
|
||||
case Interface:
|
||||
return "interface"
|
||||
case Ptr:
|
||||
return "ptr"
|
||||
case Slice:
|
||||
return "slice"
|
||||
case Array:
|
||||
return "array"
|
||||
case Func:
|
||||
return "func"
|
||||
case Map:
|
||||
return "map"
|
||||
case Struct:
|
||||
return "struct"
|
||||
default:
|
||||
return "invalid"
|
||||
}
|
||||
}
|
||||
|
||||
// basicType returns a new Type for this kind if Kind is a basic type.
|
||||
func (k Kind) basicType() Type {
|
||||
return Type(k << 1)
|
||||
}
|
||||
|
||||
// The typecode as used in an interface{}.
|
||||
type Type uintptr
|
||||
|
||||
@@ -50,11 +134,24 @@ func (t Type) String() string {
|
||||
}
|
||||
|
||||
func (t Type) Kind() Kind {
|
||||
return Invalid // TODO
|
||||
if t % 2 == 0 {
|
||||
// basic type
|
||||
return Kind((t >> 1) % 32)
|
||||
} else {
|
||||
return Kind(t >> 1) % 8 + 19
|
||||
}
|
||||
}
|
||||
|
||||
func (t Type) Elem() Type {
|
||||
panic("unimplemented: (reflect.Type).Elem()")
|
||||
switch t.Kind() {
|
||||
case Chan, Ptr, Slice:
|
||||
if (t >> 4) % 2 != 0 {
|
||||
panic("unimplemented: (reflect.Type).Elem() for named types")
|
||||
}
|
||||
return t >> 5
|
||||
default: // not implemented: Array, Map
|
||||
panic("unimplemented: (reflect.Type).Elem()")
|
||||
}
|
||||
}
|
||||
|
||||
func (t Type) Field(i int) StructField {
|
||||
@@ -74,7 +171,36 @@ func (t Type) NumField() int {
|
||||
}
|
||||
|
||||
func (t Type) Size() uintptr {
|
||||
panic("unimplemented: (reflect.Type).Size()")
|
||||
switch t.Kind() {
|
||||
case Bool, Int8, Uint8:
|
||||
return 1
|
||||
case Int16, Uint16:
|
||||
return 2
|
||||
case Int32, Uint32:
|
||||
return 4
|
||||
case Int64, Uint64:
|
||||
return 8
|
||||
case Int, Uint:
|
||||
return unsafe.Sizeof(int(0))
|
||||
case Uintptr:
|
||||
return unsafe.Sizeof(uintptr(0))
|
||||
case Float32:
|
||||
return 4
|
||||
case Float64:
|
||||
return 8
|
||||
case Complex64:
|
||||
return 8
|
||||
case Complex128:
|
||||
return 16
|
||||
case String:
|
||||
return unsafe.Sizeof(StringHeader{})
|
||||
case UnsafePointer, Chan, Map, Ptr:
|
||||
return unsafe.Sizeof(uintptr(0))
|
||||
case Slice:
|
||||
return unsafe.Sizeof(SliceHeader{})
|
||||
default:
|
||||
panic("unimplemented: size of type")
|
||||
}
|
||||
}
|
||||
|
||||
type StructField struct {
|
||||
|
||||
+396
-31
@@ -1,42 +1,94 @@
|
||||
package reflect
|
||||
|
||||
import (
|
||||
_ "unsafe" // for go:linkname
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// This is the same thing as an interface{}.
|
||||
type Value struct {
|
||||
typecode Type
|
||||
value *uint8
|
||||
value unsafe.Pointer
|
||||
indirect bool
|
||||
}
|
||||
|
||||
func Indirect(v Value) Value {
|
||||
return v
|
||||
if v.Kind() != Ptr {
|
||||
return v
|
||||
}
|
||||
return v.Elem()
|
||||
}
|
||||
|
||||
func ValueOf(i interface{}) Value
|
||||
|
||||
//go:linkname _ValueOf reflect.ValueOf
|
||||
func _ValueOf(i Value) Value {
|
||||
return i
|
||||
func ValueOf(i interface{}) Value {
|
||||
v := (*interfaceHeader)(unsafe.Pointer(&i))
|
||||
return Value{
|
||||
typecode: v.typecode,
|
||||
value: v.value,
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Interface() interface{}
|
||||
func (v Value) Interface() interface{} {
|
||||
i := interfaceHeader{
|
||||
typecode: v.typecode,
|
||||
value: v.value,
|
||||
}
|
||||
if v.indirect && 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.Type().Size(); j != 0; j-- {
|
||||
value = (value << 8) | uintptr(*(*uint8)(unsafe.Pointer(uintptr(v.value) + j - 1)))
|
||||
}
|
||||
i.value = unsafe.Pointer(value)
|
||||
}
|
||||
return *(*interface{})(unsafe.Pointer(&i))
|
||||
}
|
||||
|
||||
func (v Value) Type() Type {
|
||||
return v.typecode
|
||||
}
|
||||
|
||||
func (v Value) Kind() Kind {
|
||||
return Invalid // TODO
|
||||
return v.Type().Kind()
|
||||
}
|
||||
|
||||
func (v Value) IsNil() bool {
|
||||
panic("unimplemented: (reflect.Value).IsNil()")
|
||||
switch v.Kind() {
|
||||
case Chan, Map, Ptr:
|
||||
return v.value == nil
|
||||
case Func:
|
||||
if v.value == nil {
|
||||
return true
|
||||
}
|
||||
fn := (*funcHeader)(v.value)
|
||||
return fn.Code == nil
|
||||
case Slice:
|
||||
if v.value == nil {
|
||||
return true
|
||||
}
|
||||
slice := (*SliceHeader)(v.value)
|
||||
return slice.Data == 0
|
||||
case Interface:
|
||||
if v.value == nil {
|
||||
return true
|
||||
}
|
||||
itf := (*interfaceHeader)(v.value)
|
||||
return itf.value == nil
|
||||
default:
|
||||
panic(&ValueError{"IsNil"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Pointer() uintptr {
|
||||
panic("unimplemented: (reflect.Value).Pointer()")
|
||||
switch v.Kind() {
|
||||
case Chan, Map, Ptr, UnsafePointer:
|
||||
return uintptr(v.value)
|
||||
case Slice:
|
||||
slice := (*SliceHeader)(v.value)
|
||||
return slice.Data
|
||||
case Func:
|
||||
panic("unimplemented: (reflect.Value).Pointer()")
|
||||
default: // not implemented: Func
|
||||
panic(&ValueError{"Pointer"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) IsValid() bool {
|
||||
@@ -44,7 +96,8 @@ func (v Value) IsValid() bool {
|
||||
}
|
||||
|
||||
func (v Value) CanInterface() bool {
|
||||
panic("unimplemented: (reflect.Value).CanInterface()")
|
||||
// No Value types of private data can be constructed at the moment.
|
||||
return true
|
||||
}
|
||||
|
||||
func (v Value) CanAddr() bool {
|
||||
@@ -56,31 +109,160 @@ func (v Value) Addr() Value {
|
||||
}
|
||||
|
||||
func (v Value) CanSet() bool {
|
||||
panic("unimplemented: (reflect.Value).CanSet()")
|
||||
return v.indirect
|
||||
}
|
||||
|
||||
func (v Value) Bool() bool {
|
||||
panic("unimplemented: (reflect.Value).Bool()")
|
||||
switch v.Kind() {
|
||||
case Bool:
|
||||
if v.indirect {
|
||||
return *((*bool)(v.value))
|
||||
} else {
|
||||
return uintptr(v.value) != 0
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Bool"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Int() int64 {
|
||||
panic("unimplemented: (reflect.Value).Int()")
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
if v.indirect || unsafe.Sizeof(int(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int)(v.value))
|
||||
} else {
|
||||
return int64(int(uintptr(v.value)))
|
||||
}
|
||||
case Int8:
|
||||
if v.indirect {
|
||||
return int64(*(*int8)(v.value))
|
||||
} else {
|
||||
return int64(int8(uintptr(v.value)))
|
||||
}
|
||||
case Int16:
|
||||
if v.indirect {
|
||||
return int64(*(*int16)(v.value))
|
||||
} else {
|
||||
return int64(int16(uintptr(v.value)))
|
||||
}
|
||||
case Int32:
|
||||
if v.indirect || unsafe.Sizeof(int32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int32)(v.value))
|
||||
} else {
|
||||
return int64(int32(uintptr(v.value)))
|
||||
}
|
||||
case Int64:
|
||||
if v.indirect || unsafe.Sizeof(int64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int64)(v.value))
|
||||
} else {
|
||||
return int64(int64(uintptr(v.value)))
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Int"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Uint() uint64 {
|
||||
panic("unimplemented: (reflect.Value).Uint()")
|
||||
switch v.Kind() {
|
||||
case Uintptr:
|
||||
if v.indirect {
|
||||
return uint64(*(*uintptr)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint8:
|
||||
if v.indirect {
|
||||
return uint64(*(*uint8)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint16:
|
||||
if v.indirect {
|
||||
return uint64(*(*uint16)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint:
|
||||
if v.indirect || unsafe.Sizeof(uint(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint32:
|
||||
if v.indirect || unsafe.Sizeof(uint32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint32)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint64:
|
||||
if v.indirect || unsafe.Sizeof(uint64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint64)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Uint"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Float() float64 {
|
||||
panic("unimplemented: (reflect.Value).Float()")
|
||||
switch v.Kind() {
|
||||
case Float32:
|
||||
if v.indirect || unsafe.Sizeof(float32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// The float is stored as an external value on systems with 16-bit
|
||||
// pointers.
|
||||
return float64(*(*float32)(v.value))
|
||||
} else {
|
||||
// The float is directly stored in the interface value on systems
|
||||
// with 32-bit and 64-bit pointers.
|
||||
return float64(*(*float32)(unsafe.Pointer(&v.value)))
|
||||
}
|
||||
case Float64:
|
||||
if v.indirect || unsafe.Sizeof(float64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// For systems with 16-bit and 32-bit pointers.
|
||||
return *(*float64)(v.value)
|
||||
} else {
|
||||
// The float is directly stored in the interface value on systems
|
||||
// with 64-bit pointers.
|
||||
return *(*float64)(unsafe.Pointer(&v.value))
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Float"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Complex() complex128 {
|
||||
panic("unimplemented: (reflect.Value).Complex()")
|
||||
switch v.Kind() {
|
||||
case Complex64:
|
||||
if v.indirect || unsafe.Sizeof(complex64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// The complex number is stored as an external value on systems with
|
||||
// 16-bit and 32-bit pointers.
|
||||
return complex128(*(*complex64)(v.value))
|
||||
} else {
|
||||
// The complex number is directly stored in the interface value on
|
||||
// systems with 64-bit pointers.
|
||||
return complex128(*(*complex64)(unsafe.Pointer(&v.value)))
|
||||
}
|
||||
case Complex128:
|
||||
// This is a 128-bit value, which is always stored as an external value.
|
||||
// It may be stored in the pointer directly on very uncommon
|
||||
// architectures with 128-bit pointers, however.
|
||||
return *(*complex128)(v.value)
|
||||
default:
|
||||
panic(&ValueError{"Complex"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) String() string {
|
||||
panic("unimplemented: (reflect.Value).String()")
|
||||
switch v.Kind() {
|
||||
case String:
|
||||
// A string value is always bigger than a pointer as it is made of a
|
||||
// pointer and a length.
|
||||
return *(*string)(v.value)
|
||||
default:
|
||||
// Special case because of the special treatment of .String() in Go.
|
||||
return "<T>"
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Bytes() []byte {
|
||||
@@ -92,7 +274,25 @@ func (v Value) Slice(i, j int) Value {
|
||||
}
|
||||
|
||||
func (v Value) Len() int {
|
||||
panic("unimplemented: (reflect.Value).Len()")
|
||||
t := v.Type()
|
||||
switch t.Kind() {
|
||||
case Slice:
|
||||
return int((*SliceHeader)(v.value).Len)
|
||||
case String:
|
||||
return int((*StringHeader)(v.value).Len)
|
||||
default: // Array, Chan, Map
|
||||
panic("unimplemented: (reflect.Value).Len()")
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Cap() int {
|
||||
t := v.Type()
|
||||
switch t.Kind() {
|
||||
case Slice:
|
||||
return int((*SliceHeader)(v.value).Cap)
|
||||
default: // Array, Chan
|
||||
panic("unimplemented: (reflect.Value).Cap()")
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) NumField() int {
|
||||
@@ -100,7 +300,23 @@ func (v Value) NumField() int {
|
||||
}
|
||||
|
||||
func (v Value) Elem() Value {
|
||||
panic("unimplemented: (reflect.Value).Elem()")
|
||||
switch v.Kind() {
|
||||
case Ptr:
|
||||
ptr := v.value
|
||||
if v.indirect {
|
||||
ptr = *(*unsafe.Pointer)(ptr)
|
||||
}
|
||||
if ptr == nil {
|
||||
return Value{}
|
||||
}
|
||||
return Value{
|
||||
typecode: v.Type().Elem(),
|
||||
value: ptr,
|
||||
indirect: true,
|
||||
}
|
||||
default: // not implemented: Interface
|
||||
panic(&ValueError{"Elem"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Field(i int) Value {
|
||||
@@ -108,7 +324,37 @@ func (v Value) Field(i int) Value {
|
||||
}
|
||||
|
||||
func (v Value) Index(i int) Value {
|
||||
panic("unimplemented: (reflect.Value).Index()")
|
||||
switch v.Kind() {
|
||||
case Slice:
|
||||
// Extract an element from the slice.
|
||||
slice := *(*SliceHeader)(v.value)
|
||||
if uint(i) >= uint(slice.Len) {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
elem := Value{
|
||||
typecode: v.Type().Elem(),
|
||||
indirect: true,
|
||||
}
|
||||
addr := uintptr(slice.Data) + elem.Type().Size() * uintptr(i) // pointer to new value
|
||||
elem.value = unsafe.Pointer(addr)
|
||||
return elem
|
||||
case String:
|
||||
// Extract a character from a string.
|
||||
// A string is never stored directly in the interface, but always as a
|
||||
// pointer to the string value.
|
||||
s := *(*StringHeader)(v.value)
|
||||
if uint(i) >= uint(s.Len) {
|
||||
panic("reflect: string index out of range")
|
||||
}
|
||||
return Value{
|
||||
typecode: Uint8.basicType(),
|
||||
value: unsafe.Pointer(uintptr(*(*uint8)(unsafe.Pointer(s.Data + uintptr(i))))),
|
||||
}
|
||||
case Array:
|
||||
panic("unimplemented: (reflect.Value).Index()")
|
||||
default:
|
||||
panic(&ValueError{"Index"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) MapKeys() []Value {
|
||||
@@ -120,33 +366,152 @@ func (v Value) MapIndex(key Value) Value {
|
||||
}
|
||||
|
||||
func (v Value) Set(x Value) {
|
||||
panic("unimplemented: (reflect.Value).Set()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
if v.Type() != x.Type() {
|
||||
if v.Kind() == Interface {
|
||||
panic("reflect: unimplemented: assigning to interface of different type")
|
||||
} else {
|
||||
panic("reflect: cannot assign")
|
||||
}
|
||||
}
|
||||
size := v.Type().Size()
|
||||
xptr := x.value
|
||||
if size <= unsafe.Sizeof(uintptr(0)) && !x.indirect {
|
||||
value := x.value
|
||||
xptr = unsafe.Pointer(&value)
|
||||
}
|
||||
memcpy(v.value, xptr, size)
|
||||
}
|
||||
|
||||
func (v Value) SetBool(x bool) {
|
||||
panic("unimplemented: (reflect.Value).SetBool()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Bool:
|
||||
*(*bool)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetBool"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetInt(x int64) {
|
||||
panic("unimplemented: (reflect.Value).SetInt()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
*(*int)(v.value) = int(x)
|
||||
case Int8:
|
||||
*(*int8)(v.value) = int8(x)
|
||||
case Int16:
|
||||
*(*int16)(v.value) = int16(x)
|
||||
case Int32:
|
||||
*(*int32)(v.value) = int32(x)
|
||||
case Int64:
|
||||
*(*int64)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetInt"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetUint(x uint64) {
|
||||
panic("unimplemented: (reflect.Value).SetUint()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Uint:
|
||||
*(*uint)(v.value) = uint(x)
|
||||
case Uint8:
|
||||
*(*uint8)(v.value) = uint8(x)
|
||||
case Uint16:
|
||||
*(*uint16)(v.value) = uint16(x)
|
||||
case Uint32:
|
||||
*(*uint32)(v.value) = uint32(x)
|
||||
case Uint64:
|
||||
*(*uint64)(v.value) = x
|
||||
case Uintptr:
|
||||
*(*uintptr)(v.value) = uintptr(x)
|
||||
default:
|
||||
panic(&ValueError{"SetUint"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetFloat(x float64) {
|
||||
panic("unimplemented: (reflect.Value).SetFloat()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Float32:
|
||||
*(*float32)(v.value) = float32(x)
|
||||
case Float64:
|
||||
*(*float64)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetFloat"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetComplex(x complex128) {
|
||||
panic("unimplemented: (reflect.Value).SetComplex()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Complex64:
|
||||
*(*complex64)(v.value) = complex64(x)
|
||||
case Complex128:
|
||||
*(*complex128)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetComplex"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetString(x string) {
|
||||
panic("unimplemented: (reflect.Value).SetString()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case String:
|
||||
*(*string)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetString"})
|
||||
}
|
||||
}
|
||||
|
||||
func MakeSlice(typ Type, len, cap int) Value {
|
||||
panic("unimplemented: reflect.MakeSlice()")
|
||||
}
|
||||
|
||||
type funcHeader struct {
|
||||
Context unsafe.Pointer
|
||||
Code unsafe.Pointer
|
||||
}
|
||||
|
||||
// This is the same thing as an interface{}.
|
||||
type interfaceHeader struct {
|
||||
typecode Type
|
||||
value unsafe.Pointer
|
||||
}
|
||||
|
||||
type SliceHeader struct {
|
||||
Data uintptr
|
||||
Len uintptr
|
||||
Cap uintptr
|
||||
}
|
||||
|
||||
type StringHeader struct {
|
||||
Data uintptr
|
||||
Len uintptr
|
||||
}
|
||||
|
||||
type ValueError struct {
|
||||
Method string
|
||||
}
|
||||
|
||||
func (e *ValueError) Error() string {
|
||||
return "reflect: call of reflect.Value." + e.Method + " on invalid type"
|
||||
}
|
||||
|
||||
//go:linkname memcpy runtime.memcpy
|
||||
func memcpy(dst, src unsafe.Pointer, size uintptr)
|
||||
|
||||
@@ -5,7 +5,9 @@ const GOARCH = "amd64"
|
||||
// The bitness of the CPU (e.g. 8, 32, 64).
|
||||
const TargetBits = 64
|
||||
|
||||
// Align on word boundary.
|
||||
// Align a pointer.
|
||||
// Note that some amd64 instructions (like movaps) expect 16-byte aligned
|
||||
// memory, thus the result must be 16-byte aligned.
|
||||
func align(ptr uintptr) uintptr {
|
||||
return (ptr + 7) &^ 7
|
||||
return (ptr + 15) &^ 15
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ package runtime
|
||||
// Interfaces are represented as a pair of {typecode, value}, where value can be
|
||||
// anything (including non-pointers).
|
||||
|
||||
import "unsafe"
|
||||
|
||||
type _interface struct {
|
||||
typecode uintptr
|
||||
value *uint8
|
||||
value unsafe.Pointer
|
||||
}
|
||||
|
||||
// Return true iff both interfaces are equal.
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
package runtime
|
||||
|
||||
// This file redirects math stubs to their fallback implementation.
|
||||
// TODO: use optimized versions if possible.
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
//go:linkname math_Asin math.Asin
|
||||
func math_Asin(x float64) float64 { return math_asin(x) }
|
||||
|
||||
//go:linkname math_asin math.asin
|
||||
func math_asin(x float64) float64
|
||||
|
||||
//go:linkname math_Asinh math.Asinh
|
||||
func math_Asinh(x float64) float64 { return math_asinh(x) }
|
||||
|
||||
//go:linkname math_asinh math.asinh
|
||||
func math_asinh(x float64) float64
|
||||
|
||||
//go:linkname math_Acos math.Acos
|
||||
func math_Acos(x float64) float64 { return math_acos(x) }
|
||||
|
||||
//go:linkname math_acos math.acos
|
||||
func math_acos(x float64) float64
|
||||
|
||||
//go:linkname math_Acosh math.Acosh
|
||||
func math_Acosh(x float64) float64 { return math_acosh(x) }
|
||||
|
||||
//go:linkname math_acosh math.acosh
|
||||
func math_acosh(x float64) float64
|
||||
|
||||
//go:linkname math_Atan math.Atan
|
||||
func math_Atan(x float64) float64 { return math_atan(x) }
|
||||
|
||||
//go:linkname math_atan math.atan
|
||||
func math_atan(x float64) float64
|
||||
|
||||
//go:linkname math_Atanh math.Atanh
|
||||
func math_Atanh(x float64) float64 { return math_atanh(x) }
|
||||
|
||||
//go:linkname math_atanh math.atanh
|
||||
func math_atanh(x float64) float64
|
||||
|
||||
//go:linkname math_Atan2 math.Atan2
|
||||
func math_Atan2(y, x float64) float64 { return math_atan2(y, x) }
|
||||
|
||||
//go:linkname math_atan2 math.atan2
|
||||
func math_atan2(y, x float64) float64
|
||||
|
||||
//go:linkname math_Cbrt math.Cbrt
|
||||
func math_Cbrt(x float64) float64 { return math_cbrt(x) }
|
||||
|
||||
//go:linkname math_cbrt math.cbrt
|
||||
func math_cbrt(x float64) float64
|
||||
|
||||
//go:linkname math_Ceil math.Ceil
|
||||
func math_Ceil(x float64) float64 { return math_ceil(x) }
|
||||
|
||||
//go:linkname math_ceil math.ceil
|
||||
func math_ceil(x float64) float64
|
||||
|
||||
//go:linkname math_Cos math.Cos
|
||||
func math_Cos(x float64) float64 { return math_cos(x) }
|
||||
|
||||
//go:linkname math_cos math.cos
|
||||
func math_cos(x float64) float64
|
||||
|
||||
//go:linkname math_Cosh math.Cosh
|
||||
func math_Cosh(x float64) float64 { return math_cosh(x) }
|
||||
|
||||
//go:linkname math_cosh math.cosh
|
||||
func math_cosh(x float64) float64
|
||||
|
||||
//go:linkname math_Erf math.Erf
|
||||
func math_Erf(x float64) float64 { return math_erf(x) }
|
||||
|
||||
//go:linkname math_erf math.erf
|
||||
func math_erf(x float64) float64
|
||||
|
||||
//go:linkname math_Erfc math.Erfc
|
||||
func math_Erfc(x float64) float64 { return math_erfc(x) }
|
||||
|
||||
//go:linkname math_erfc math.erfc
|
||||
func math_erfc(x float64) float64
|
||||
|
||||
//go:linkname math_Exp math.Exp
|
||||
func math_Exp(x float64) float64 { return math_exp(x) }
|
||||
|
||||
//go:linkname math_exp math.exp
|
||||
func math_exp(x float64) float64
|
||||
|
||||
//go:linkname math_Expm1 math.Expm1
|
||||
func math_Expm1(x float64) float64 { return math_expm1(x) }
|
||||
|
||||
//go:linkname math_expm1 math.expm1
|
||||
func math_expm1(x float64) float64
|
||||
|
||||
//go:linkname math_Exp2 math.Exp2
|
||||
func math_Exp2(x float64) float64 { return math_exp2(x) }
|
||||
|
||||
//go:linkname math_exp2 math.exp2
|
||||
func math_exp2(x float64) float64
|
||||
|
||||
//go:linkname math_Floor math.Floor
|
||||
func math_Floor(x float64) float64 { return math_floor(x) }
|
||||
|
||||
//go:linkname math_floor math.floor
|
||||
func math_floor(x float64) float64
|
||||
|
||||
//go:linkname math_Frexp math.Frexp
|
||||
func math_Frexp(x float64) (float64, int) { return math_frexp(x) }
|
||||
|
||||
//go:linkname math_frexp math.frexp
|
||||
func math_frexp(x float64) (float64, int)
|
||||
|
||||
//go:linkname math_Hypot math.Hypot
|
||||
func math_Hypot(p, q float64) float64 { return math_hypot(p, q) }
|
||||
|
||||
//go:linkname math_hypot math.hypot
|
||||
func math_hypot(p, q float64) float64
|
||||
|
||||
//go:linkname math_Ldexp math.Ldexp
|
||||
func math_Ldexp(frac float64, exp int) float64 { return math_ldexp(frac, exp) }
|
||||
|
||||
//go:linkname math_ldexp math.ldexp
|
||||
func math_ldexp(frac float64, exp int) float64
|
||||
|
||||
//go:linkname math_Log math.Log
|
||||
func math_Log(x float64) float64 { return math_log(x) }
|
||||
|
||||
//go:linkname math_log math.log
|
||||
func math_log(x float64) float64
|
||||
|
||||
//go:linkname math_Log1p math.Log1p
|
||||
func math_Log1p(x float64) float64 { return math_log1p(x) }
|
||||
|
||||
//go:linkname math_log1p math.log1p
|
||||
func math_log1p(x float64) float64
|
||||
|
||||
//go:linkname math_Log10 math.Log10
|
||||
func math_Log10(x float64) float64 { return math_log10(x) }
|
||||
|
||||
//go:linkname math_log10 math.log10
|
||||
func math_log10(x float64) float64
|
||||
|
||||
//go:linkname math_Log2 math.Log2
|
||||
func math_Log2(x float64) float64 { return math_log2(x) }
|
||||
|
||||
//go:linkname math_log2 math.log2
|
||||
func math_log2(x float64) float64
|
||||
|
||||
//go:linkname math_Max math.Max
|
||||
func math_Max(x, y float64) float64 { return math_max(x, y) }
|
||||
|
||||
//go:linkname math_max math.max
|
||||
func math_max(x, y float64) float64
|
||||
|
||||
//go:linkname math_Min math.Min
|
||||
func math_Min(x, y float64) float64 { return math_min(x, y) }
|
||||
|
||||
//go:linkname math_min math.min
|
||||
func math_min(x, y float64) float64
|
||||
|
||||
//go:linkname math_Mod math.Mod
|
||||
func math_Mod(x, y float64) float64 { return math_mod(x, y) }
|
||||
|
||||
//go:linkname math_mod math.mod
|
||||
func math_mod(x, y float64) float64
|
||||
|
||||
//go:linkname math_Modf math.Modf
|
||||
func math_Modf(x float64) (float64, float64) { return math_modf(x) }
|
||||
|
||||
//go:linkname math_modf math.modf
|
||||
func math_modf(x float64) (float64, float64)
|
||||
|
||||
//go:linkname math_Pow math.Pow
|
||||
func math_Pow(x, y float64) float64 { return math_pow(x, y) }
|
||||
|
||||
//go:linkname math_pow math.pow
|
||||
func math_pow(x, y float64) float64
|
||||
|
||||
//go:linkname math_Remainder math.Remainder
|
||||
func math_Remainder(x, y float64) float64 { return math_remainder(x, y) }
|
||||
|
||||
//go:linkname math_remainder math.remainder
|
||||
func math_remainder(x, y float64) float64
|
||||
|
||||
//go:linkname math_Sin math.Sin
|
||||
func math_Sin(x float64) float64 { return math_sin(x) }
|
||||
|
||||
//go:linkname math_sin math.sin
|
||||
func math_sin(x float64) float64
|
||||
|
||||
//go:linkname math_Sinh math.Sinh
|
||||
func math_Sinh(x float64) float64 { return math_sinh(x) }
|
||||
|
||||
//go:linkname math_sinh math.sinh
|
||||
func math_sinh(x float64) float64
|
||||
|
||||
//go:linkname math_Sqrt math.Sqrt
|
||||
func math_Sqrt(x float64) float64 { return math_sqrt(x) }
|
||||
|
||||
//go:linkname math_sqrt math.sqrt
|
||||
func math_sqrt(x float64) float64
|
||||
|
||||
//go:linkname math_Tan math.Tan
|
||||
func math_Tan(x float64) float64 { return math_tan(x) }
|
||||
|
||||
//go:linkname math_tan math.tan
|
||||
func math_tan(x float64) float64
|
||||
|
||||
//go:linkname math_Tanh math.Tanh
|
||||
func math_Tanh(x float64) float64 { return math_tanh(x) }
|
||||
|
||||
//go:linkname math_tanh math.tanh
|
||||
func math_tanh(x float64) float64
|
||||
|
||||
//go:linkname math_Trunc math.Trunc
|
||||
func math_Trunc(x float64) float64 { return math_trunc(x) }
|
||||
|
||||
//go:linkname math_trunc math.trunc
|
||||
func math_trunc(x float64) float64
|
||||
@@ -1,13 +0,0 @@
|
||||
package runtime
|
||||
|
||||
// This file implements syscall.Syscall and the like.
|
||||
|
||||
//go:linkname syscall_Syscall syscall.Syscall
|
||||
func syscall_Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err uintptr) {
|
||||
panic("syscall")
|
||||
}
|
||||
|
||||
//go:linkname syscall_Syscall6 syscall.Syscall6
|
||||
func syscall_Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err uintptr) {
|
||||
panic("syscall6")
|
||||
}
|
||||
@@ -211,22 +211,24 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
|
||||
BuildTags: []string{goos, goarch},
|
||||
Compiler: commands["clang"],
|
||||
Linker: "cc",
|
||||
LDFlags: []string{"-no-pie"}, // WARNING: clang < 5.0 requires -nopie
|
||||
LDFlags: []string{"-no-pie", "-Wl,--gc-sections"}, // WARNING: clang < 5.0 requires -nopie
|
||||
Objcopy: "objcopy",
|
||||
GDB: "gdb",
|
||||
GDBCmds: []string{"run"},
|
||||
}
|
||||
if goarch != runtime.GOARCH {
|
||||
// Some educated guesses as to how to invoke helper programs.
|
||||
if goarch == "arm" {
|
||||
if goarch == "arm" && goos == "linux" {
|
||||
spec.Linker = "arm-linux-gnueabi-gcc"
|
||||
spec.Objcopy = "arm-linux-gnueabi-objcopy"
|
||||
spec.GDB = "arm-linux-gnueabi-gdb"
|
||||
spec.Emulator = []string{"qemu-arm", "-L", "/usr/arm-linux-gnueabi"}
|
||||
}
|
||||
if goarch == "arm64" {
|
||||
if goarch == "arm64" && goos == "linux" {
|
||||
spec.Linker = "aarch64-linux-gnu-gcc"
|
||||
spec.Objcopy = "aarch64-linux-gnu-objcopy"
|
||||
spec.GDB = "aarch64-linux-gnu-gdb"
|
||||
spec.Emulator = []string{"qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"}
|
||||
}
|
||||
if goarch == "386" {
|
||||
spec.CFlags = []string{"-m32"}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"build-tags": ["nrf52", "nrf"],
|
||||
"cflags": [
|
||||
"--target=armv7em-none-eabi",
|
||||
"-mfloat-abi=soft",
|
||||
"-Qunused-arguments",
|
||||
"-DNRF52832_XXAA",
|
||||
"-Ilib/CMSIS/CMSIS/Include"
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"build-tags": ["nrf52840", "nrf"],
|
||||
"cflags": [
|
||||
"--target=armv7em-none-eabi",
|
||||
"-mfloat-abi=soft",
|
||||
"-Qunused-arguments",
|
||||
"-DNRF52840_XXAA",
|
||||
"-Ilib/CMSIS/CMSIS/Include"
|
||||
|
||||
Vendored
+17
@@ -47,6 +47,20 @@ func main() {
|
||||
println(s2 == Struct2{"foo", 0.0, 7})
|
||||
println(s2 == Struct2{"foo", 1.0, 5})
|
||||
println(s2 == Struct2{"foo", 1.0, 7})
|
||||
|
||||
println("complex numbers")
|
||||
println(c64 == 3+2i)
|
||||
println(c64 == 4+2i)
|
||||
println(c64 == 3+3i)
|
||||
println(c64 != 3+2i)
|
||||
println(c64 != 4+2i)
|
||||
println(c64 != 3+3i)
|
||||
println(c128 == 3+2i)
|
||||
println(c128 == 4+2i)
|
||||
println(c128 == 3+3i)
|
||||
println(c128 != 3+2i)
|
||||
println(c128 != 4+2i)
|
||||
println(c128 != 3+3i)
|
||||
}
|
||||
|
||||
var x = true
|
||||
@@ -58,6 +72,9 @@ var s2 = Struct2{"foo", 0.0, 5}
|
||||
|
||||
var a1 = [2]int{1, 2}
|
||||
|
||||
var c64 = 3 + 2i
|
||||
var c128 = 4 + 3i
|
||||
|
||||
type Int int
|
||||
|
||||
type Struct1 struct {
|
||||
|
||||
Vendored
+13
@@ -41,3 +41,16 @@ true
|
||||
false
|
||||
true
|
||||
false
|
||||
complex numbers
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
Vendored
+10
@@ -1,5 +1,7 @@
|
||||
#include "main.h"
|
||||
|
||||
int global = 3;
|
||||
|
||||
int fortytwo() {
|
||||
return 42;
|
||||
}
|
||||
@@ -7,3 +9,11 @@ int fortytwo() {
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int doCallback(int a, int b, binop_t callback) {
|
||||
return callback(a, b);
|
||||
}
|
||||
|
||||
void store(int value, int *ptr) {
|
||||
*ptr = value;
|
||||
}
|
||||
|
||||
Vendored
+17
@@ -3,6 +3,7 @@ package main
|
||||
/*
|
||||
int fortytwo(void);
|
||||
#include "main.h"
|
||||
int mul(int, int);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
@@ -16,4 +17,20 @@ func main() {
|
||||
println("myint size:", int(unsafe.Sizeof(x)))
|
||||
var y C.longlong = -(1 << 40)
|
||||
println("longlong:", y)
|
||||
println("global:", C.global)
|
||||
var ptr C.intPointer
|
||||
var n C.int = 15
|
||||
ptr = C.intPointer(&n)
|
||||
println("15:", *ptr)
|
||||
C.store(25, &n)
|
||||
println("25:", *ptr)
|
||||
cb := C.binop_t(C.add)
|
||||
println("callback 1:", C.doCallback(20, 30, cb))
|
||||
cb = C.binop_t(C.mul)
|
||||
println("callback 2:", C.doCallback(20, 30, cb))
|
||||
}
|
||||
|
||||
//export mul
|
||||
func mul(a, b C.int) C.int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
Vendored
+9
@@ -1,2 +1,11 @@
|
||||
typedef short myint;
|
||||
int add(int a, int b);
|
||||
typedef int (*binop_t) (int, int);
|
||||
int doCallback(int a, int b, binop_t cb);
|
||||
typedef int * intPointer;
|
||||
extern int global;
|
||||
void store(int value, int *ptr);
|
||||
|
||||
// test duplicate definitions
|
||||
int add(int a, int b);
|
||||
extern int global;
|
||||
|
||||
Vendored
+5
@@ -3,3 +3,8 @@ add: 8
|
||||
myint: 3 5
|
||||
myint size: 2
|
||||
longlong: -1099511627776
|
||||
global: 3
|
||||
15: 15
|
||||
25: 25
|
||||
callback 1: 50
|
||||
callback 2: 600
|
||||
|
||||
Vendored
+46
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import "math"
|
||||
|
||||
func main() {
|
||||
for _, n := range []float64{0.3, 1.5, 2.6, -1.1, -3.1, -3.8} {
|
||||
println("n:", n)
|
||||
println(" asin: ", math.Asin(n))
|
||||
println(" asinh: ", math.Asinh(n))
|
||||
println(" acos: ", math.Acos(n))
|
||||
println(" acosh: ", math.Acosh(n))
|
||||
println(" atan: ", math.Atan(n))
|
||||
println(" atanh: ", math.Atanh(n))
|
||||
println(" atan2: ", math.Atan2(n, 0.2))
|
||||
println(" cbrt: ", math.Cbrt(n))
|
||||
println(" ceil: ", math.Ceil(n))
|
||||
println(" cos: ", math.Cos(n))
|
||||
println(" cosh: ", math.Cosh(n))
|
||||
println(" erf: ", math.Erf(n))
|
||||
println(" erfc: ", math.Erfc(n))
|
||||
println(" exp: ", math.Exp(n))
|
||||
println(" expm1: ", math.Expm1(n))
|
||||
println(" exp2: ", math.Exp2(n))
|
||||
println(" floor: ", math.Floor(n))
|
||||
f, e := math.Frexp(n)
|
||||
println(" frexp: ", f, e)
|
||||
println(" hypot: ", math.Hypot(n, n*2))
|
||||
println(" ldexp: ", math.Ldexp(n, 2))
|
||||
println(" log: ", math.Log(n))
|
||||
println(" log1p: ", math.Log1p(n))
|
||||
println(" log10: ", math.Log10(n))
|
||||
println(" log2: ", math.Log2(n))
|
||||
println(" max: ", math.Max(n, n+1))
|
||||
println(" min: ", math.Min(n, n+1))
|
||||
println(" mod: ", math.Mod(n, n+1))
|
||||
i, f := math.Modf(n)
|
||||
println(" modf: ", i, f)
|
||||
println(" pow: ", math.Pow(n, n))
|
||||
println(" remainder:", math.Remainder(n, n+0.2))
|
||||
println(" sin: ", math.Sin(n))
|
||||
println(" sinh: ", math.Sinh(n))
|
||||
println(" tan: ", math.Tan(n))
|
||||
println(" tanh: ", math.Tanh(n))
|
||||
println(" trunc: ", math.Trunc(n))
|
||||
}
|
||||
}
|
||||
Vendored
+216
@@ -0,0 +1,216 @@
|
||||
n: +3.000000e-001
|
||||
asin: +3.046927e-001
|
||||
asinh: +2.956730e-001
|
||||
acos: +1.266104e+000
|
||||
acosh: NaN
|
||||
atan: +2.914568e-001
|
||||
atanh: +3.095196e-001
|
||||
atan2: +9.827937e-001
|
||||
cbrt: +6.694330e-001
|
||||
ceil: +1.000000e+000
|
||||
cos: +9.553365e-001
|
||||
cosh: +1.045339e+000
|
||||
erf: +3.286268e-001
|
||||
erfc: +6.713732e-001
|
||||
exp: +1.349859e+000
|
||||
expm1: +3.498588e-001
|
||||
exp2: +1.231144e+000
|
||||
floor: +0.000000e+000
|
||||
frexp: +6.000000e-001 -1
|
||||
hypot: +6.708204e-001
|
||||
ldexp: +1.200000e+000
|
||||
log: -1.203973e+000
|
||||
log1p: +2.623643e-001
|
||||
log10: -5.228787e-001
|
||||
log2: -1.736966e+000
|
||||
max: +1.300000e+000
|
||||
min: +3.000000e-001
|
||||
mod: +3.000000e-001
|
||||
modf: +0.000000e+000 +3.000000e-001
|
||||
pow: +6.968453e-001
|
||||
remainder: -2.000000e-001
|
||||
sin: +2.955202e-001
|
||||
sinh: +3.045203e-001
|
||||
tan: +3.093362e-001
|
||||
tanh: +2.913126e-001
|
||||
trunc: +0.000000e+000
|
||||
n: +1.500000e+000
|
||||
asin: NaN
|
||||
asinh: +1.194763e+000
|
||||
acos: NaN
|
||||
acosh: +9.624237e-001
|
||||
atan: +9.827937e-001
|
||||
atanh: NaN
|
||||
atan2: +1.438245e+000
|
||||
cbrt: +1.144714e+000
|
||||
ceil: +2.000000e+000
|
||||
cos: +7.073720e-002
|
||||
cosh: +2.352410e+000
|
||||
erf: +9.661051e-001
|
||||
erfc: +3.389485e-002
|
||||
exp: +4.481689e+000
|
||||
expm1: +3.481689e+000
|
||||
exp2: +2.828427e+000
|
||||
floor: +1.000000e+000
|
||||
frexp: +7.500000e-001 1
|
||||
hypot: +3.354102e+000
|
||||
ldexp: +6.000000e+000
|
||||
log: +4.054651e-001
|
||||
log1p: +9.162907e-001
|
||||
log10: +1.760913e-001
|
||||
log2: +5.849625e-001
|
||||
max: +2.500000e+000
|
||||
min: +1.500000e+000
|
||||
mod: +1.500000e+000
|
||||
modf: +1.000000e+000 +5.000000e-001
|
||||
pow: +1.837117e+000
|
||||
remainder: -2.000000e-001
|
||||
sin: +9.974950e-001
|
||||
sinh: +2.129279e+000
|
||||
tan: +1.410142e+001
|
||||
tanh: +9.051483e-001
|
||||
trunc: +1.000000e+000
|
||||
n: +2.600000e+000
|
||||
asin: NaN
|
||||
asinh: +1.683743e+000
|
||||
acos: NaN
|
||||
acosh: +1.609438e+000
|
||||
atan: +1.203622e+000
|
||||
atanh: NaN
|
||||
atan2: +1.494024e+000
|
||||
cbrt: +1.375069e+000
|
||||
ceil: +3.000000e+000
|
||||
cos: -8.568888e-001
|
||||
cosh: +6.769006e+000
|
||||
erf: +9.997640e-001
|
||||
erfc: +2.360344e-004
|
||||
exp: +1.346374e+001
|
||||
expm1: +1.246374e+001
|
||||
exp2: +6.062866e+000
|
||||
floor: +2.000000e+000
|
||||
frexp: +6.500000e-001 2
|
||||
hypot: +5.813777e+000
|
||||
ldexp: +1.040000e+001
|
||||
log: +9.555114e-001
|
||||
log1p: +1.280934e+000
|
||||
log10: +4.149733e-001
|
||||
log2: +1.378512e+000
|
||||
max: +3.600000e+000
|
||||
min: +2.600000e+000
|
||||
mod: +2.600000e+000
|
||||
modf: +2.000000e+000 +6.000000e-001
|
||||
pow: +1.199308e+001
|
||||
remainder: -2.000000e-001
|
||||
sin: +5.155014e-001
|
||||
sinh: +6.694732e+000
|
||||
tan: -6.015966e-001
|
||||
tanh: +9.890274e-001
|
||||
trunc: +2.000000e+000
|
||||
n: -1.100000e+000
|
||||
asin: NaN
|
||||
asinh: -9.503469e-001
|
||||
acos: NaN
|
||||
acosh: NaN
|
||||
atan: -8.329813e-001
|
||||
atanh: NaN
|
||||
atan2: -1.390943e+000
|
||||
cbrt: -1.032280e+000
|
||||
ceil: -1.000000e+000
|
||||
cos: +4.535961e-001
|
||||
cosh: +1.668519e+000
|
||||
erf: -8.802051e-001
|
||||
erfc: +1.880205e+000
|
||||
exp: +3.328711e-001
|
||||
expm1: -6.671289e-001
|
||||
exp2: +4.665165e-001
|
||||
floor: -2.000000e+000
|
||||
frexp: -5.500000e-001 1
|
||||
hypot: +2.459675e+000
|
||||
ldexp: -4.400000e+000
|
||||
log: NaN
|
||||
log1p: NaN
|
||||
log10: NaN
|
||||
log2: NaN
|
||||
max: -1.000000e-001
|
||||
min: -1.100000e+000
|
||||
mod: -1.000000e-001
|
||||
modf: -1.000000e+000 -1.000000e-001
|
||||
pow: NaN
|
||||
remainder: -2.000000e-001
|
||||
sin: -8.912074e-001
|
||||
sinh: -1.335647e+000
|
||||
tan: -1.964760e+000
|
||||
tanh: -8.004990e-001
|
||||
trunc: -1.000000e+000
|
||||
n: -3.100000e+000
|
||||
asin: NaN
|
||||
asinh: -1.849604e+000
|
||||
acos: NaN
|
||||
acosh: NaN
|
||||
atan: -1.258754e+000
|
||||
atanh: NaN
|
||||
atan2: -1.506369e+000
|
||||
cbrt: -1.458100e+000
|
||||
ceil: -3.000000e+000
|
||||
cos: -9.991352e-001
|
||||
cosh: +1.112150e+001
|
||||
erf: -9.999884e-001
|
||||
erfc: +1.999988e+000
|
||||
exp: +4.504920e-002
|
||||
expm1: -9.549508e-001
|
||||
exp2: +1.166291e-001
|
||||
floor: -4.000000e+000
|
||||
frexp: -7.750000e-001 2
|
||||
hypot: +6.931811e+000
|
||||
ldexp: -1.240000e+001
|
||||
log: NaN
|
||||
log1p: NaN
|
||||
log10: NaN
|
||||
log2: NaN
|
||||
max: -2.100000e+000
|
||||
min: -3.100000e+000
|
||||
mod: -1.000000e+000
|
||||
modf: -3.000000e+000 -1.000000e-001
|
||||
pow: NaN
|
||||
remainder: -2.000000e-001
|
||||
sin: -4.158066e-002
|
||||
sinh: -1.107645e+001
|
||||
tan: +4.161665e-002
|
||||
tanh: -9.959494e-001
|
||||
trunc: -3.000000e+000
|
||||
n: -3.800000e+000
|
||||
asin: NaN
|
||||
asinh: -2.045028e+000
|
||||
acos: NaN
|
||||
acosh: NaN
|
||||
atan: -1.313473e+000
|
||||
atanh: NaN
|
||||
atan2: -1.518213e+000
|
||||
cbrt: -1.560491e+000
|
||||
ceil: -3.000000e+000
|
||||
cos: -7.909677e-001
|
||||
cosh: +2.236178e+001
|
||||
erf: -9.999999e-001
|
||||
erfc: +2.000000e+000
|
||||
exp: +2.237077e-002
|
||||
expm1: -9.776292e-001
|
||||
exp2: +7.179365e-002
|
||||
floor: -4.000000e+000
|
||||
frexp: -9.500000e-001 2
|
||||
hypot: +8.497058e+000
|
||||
ldexp: -1.520000e+001
|
||||
log: NaN
|
||||
log1p: NaN
|
||||
log10: NaN
|
||||
log2: NaN
|
||||
max: -2.800000e+000
|
||||
min: -3.800000e+000
|
||||
mod: -1.000000e+000
|
||||
modf: -3.000000e+000 -8.000000e-001
|
||||
pow: NaN
|
||||
remainder: -2.000000e-001
|
||||
sin: +6.118579e-001
|
||||
sinh: -2.233941e+001
|
||||
tan: -7.735561e-001
|
||||
tanh: -9.989996e-001
|
||||
trunc: -3.000000e+000
|
||||
Vendored
+292
@@ -0,0 +1,292 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type (
|
||||
myint int
|
||||
myslice []byte
|
||||
myslice2 []myint
|
||||
)
|
||||
|
||||
func main() {
|
||||
println("matching types")
|
||||
println(reflect.TypeOf(int(3)) == reflect.TypeOf(int(5)))
|
||||
println(reflect.TypeOf(int(3)) == reflect.TypeOf(uint(5)))
|
||||
println(reflect.TypeOf(myint(3)) == reflect.TypeOf(int(5)))
|
||||
println(reflect.TypeOf(myslice{}) == reflect.TypeOf([]byte{}))
|
||||
println(reflect.TypeOf(myslice2{}) == reflect.TypeOf([]myint{}))
|
||||
println(reflect.TypeOf(myslice2{}) == reflect.TypeOf([]int{}))
|
||||
|
||||
println("\nvalues of interfaces")
|
||||
var zeroSlice []byte
|
||||
var zeroFunc func()
|
||||
var zeroMap map[string]int
|
||||
var zeroChan chan int
|
||||
n := 42
|
||||
for _, v := range []interface{}{
|
||||
// basic types
|
||||
true,
|
||||
false,
|
||||
int(2000),
|
||||
int(-2000),
|
||||
uint(2000),
|
||||
int8(-3),
|
||||
int8(3),
|
||||
uint8(200),
|
||||
int16(-300),
|
||||
int16(300),
|
||||
uint16(50000),
|
||||
int32(7 << 20),
|
||||
int32(-7 << 20),
|
||||
uint32(7 << 20),
|
||||
int64(9 << 40),
|
||||
int64(-9 << 40),
|
||||
uint64(9 << 40),
|
||||
uintptr(12345),
|
||||
float32(3.14),
|
||||
float64(3.14),
|
||||
complex64(1.2 + 0.3i),
|
||||
complex128(1.3 + 0.4i),
|
||||
myint(32),
|
||||
"foo",
|
||||
unsafe.Pointer(new(int)),
|
||||
// channels
|
||||
zeroChan,
|
||||
// pointers
|
||||
new(int),
|
||||
new(error),
|
||||
&n,
|
||||
// slices
|
||||
[]byte{1, 2, 3},
|
||||
make([]uint8, 2, 5),
|
||||
[]rune{3, 5},
|
||||
[]string{"xyz", "Z"},
|
||||
zeroSlice,
|
||||
[]byte{},
|
||||
[]float32{1, 1.32},
|
||||
[]float64{1, 1.64},
|
||||
[]complex64{1, 1.64 + 0.3i},
|
||||
[]complex128{1, 1.128 + 0.4i},
|
||||
// array
|
||||
[4]int{1, 2, 3, 4},
|
||||
// functions
|
||||
zeroFunc,
|
||||
emptyFunc,
|
||||
// maps
|
||||
zeroMap,
|
||||
map[string]int{},
|
||||
// structs
|
||||
struct{}{},
|
||||
struct{ error }{},
|
||||
} {
|
||||
showValue(reflect.ValueOf(v), "")
|
||||
}
|
||||
|
||||
// test sizes
|
||||
println("\nsizes:")
|
||||
println("int8", int(reflect.TypeOf(int8(0)).Size()))
|
||||
println("int16", int(reflect.TypeOf(int16(0)).Size()))
|
||||
println("int32", int(reflect.TypeOf(int32(0)).Size()))
|
||||
println("int64", int(reflect.TypeOf(int64(0)).Size()))
|
||||
println("uint8", int(reflect.TypeOf(uint8(0)).Size()))
|
||||
println("uint16", int(reflect.TypeOf(uint16(0)).Size()))
|
||||
println("uint32", int(reflect.TypeOf(uint32(0)).Size()))
|
||||
println("uint64", int(reflect.TypeOf(uint64(0)).Size()))
|
||||
println("float32", int(reflect.TypeOf(float32(0)).Size()))
|
||||
println("float64", int(reflect.TypeOf(float64(0)).Size()))
|
||||
println("complex64", int(reflect.TypeOf(complex64(0)).Size()))
|
||||
println("complex128", int(reflect.TypeOf(complex128(0)).Size()))
|
||||
assertSize(reflect.TypeOf(uintptr(0)).Size() == unsafe.Sizeof(uintptr(0)), "uintptr")
|
||||
assertSize(reflect.TypeOf("").Size() == unsafe.Sizeof(""), "string")
|
||||
assertSize(reflect.TypeOf(new(int)).Size() == unsafe.Sizeof(new(int)), "*int")
|
||||
|
||||
// SetBool
|
||||
rv := reflect.ValueOf(new(bool)).Elem()
|
||||
rv.SetBool(true)
|
||||
if rv.Bool() != true {
|
||||
panic("could not set bool with SetBool()")
|
||||
}
|
||||
|
||||
// SetInt
|
||||
for _, v := range []interface{}{
|
||||
new(int),
|
||||
new(int8),
|
||||
new(int16),
|
||||
new(int32),
|
||||
new(int64),
|
||||
} {
|
||||
rv := reflect.ValueOf(v).Elem()
|
||||
rv.SetInt(99)
|
||||
if rv.Int() != 99 {
|
||||
panic("could not set integer with SetInt()")
|
||||
}
|
||||
}
|
||||
|
||||
// SetUint
|
||||
for _, v := range []interface{}{
|
||||
new(uint),
|
||||
new(uint8),
|
||||
new(uint16),
|
||||
new(uint32),
|
||||
new(uint64),
|
||||
new(uintptr),
|
||||
} {
|
||||
rv := reflect.ValueOf(v).Elem()
|
||||
rv.SetUint(99)
|
||||
if rv.Uint() != 99 {
|
||||
panic("could not set integer with SetUint()")
|
||||
}
|
||||
}
|
||||
|
||||
// SetFloat
|
||||
for _, v := range []interface{}{
|
||||
new(float32),
|
||||
new(float64),
|
||||
} {
|
||||
rv := reflect.ValueOf(v).Elem()
|
||||
rv.SetFloat(2.25)
|
||||
if rv.Float() != 2.25 {
|
||||
panic("could not set float with SetFloat()")
|
||||
}
|
||||
}
|
||||
|
||||
// SetComplex
|
||||
for _, v := range []interface{}{
|
||||
new(complex64),
|
||||
new(complex128),
|
||||
} {
|
||||
rv := reflect.ValueOf(v).Elem()
|
||||
rv.SetComplex(3 + 2i)
|
||||
if rv.Complex() != 3+2i {
|
||||
panic("could not set complex with SetComplex()")
|
||||
}
|
||||
}
|
||||
|
||||
// SetString
|
||||
rv = reflect.ValueOf(new(string)).Elem()
|
||||
rv.SetString("foo")
|
||||
if rv.String() != "foo" {
|
||||
panic("could not set string with SetString()")
|
||||
}
|
||||
|
||||
// Set int
|
||||
rv = reflect.ValueOf(new(int)).Elem()
|
||||
rv.SetInt(33)
|
||||
rv.Set(reflect.ValueOf(22))
|
||||
if rv.Int() != 22 {
|
||||
panic("could not set int with Set()")
|
||||
}
|
||||
|
||||
// Set uint8
|
||||
rv = reflect.ValueOf(new(uint8)).Elem()
|
||||
rv.SetUint(33)
|
||||
rv.Set(reflect.ValueOf(uint8(22)))
|
||||
if rv.Uint() != 22 {
|
||||
panic("could not set uint8 with Set()")
|
||||
}
|
||||
|
||||
// Set string
|
||||
rv = reflect.ValueOf(new(string)).Elem()
|
||||
rv.SetString("foo")
|
||||
rv.Set(reflect.ValueOf("bar"))
|
||||
if rv.String() != "bar" {
|
||||
panic("could not set string with Set()")
|
||||
}
|
||||
|
||||
// Set complex128
|
||||
rv = reflect.ValueOf(new(complex128)).Elem()
|
||||
rv.SetComplex(3 + 2i)
|
||||
rv.Set(reflect.ValueOf(4 + 8i))
|
||||
if rv.Complex() != 4+8i {
|
||||
panic("could not set complex128 with Set()")
|
||||
}
|
||||
|
||||
// Set to slice
|
||||
rv = reflect.ValueOf([]int{3, 5})
|
||||
rv.Index(1).SetInt(7)
|
||||
if rv.Index(1).Int() != 7 {
|
||||
panic("could not set int in slice")
|
||||
}
|
||||
rv.Index(1).Set(reflect.ValueOf(8))
|
||||
if rv.Index(1).Int() != 8 {
|
||||
panic("could not set int in slice")
|
||||
}
|
||||
if rv.Len() != 2 || rv.Index(0).Int() != 3 {
|
||||
panic("slice was changed while setting part of it")
|
||||
}
|
||||
}
|
||||
|
||||
func emptyFunc() {
|
||||
}
|
||||
|
||||
func showValue(rv reflect.Value, indent string) {
|
||||
rt := rv.Type()
|
||||
if rt.Kind() != rv.Kind() {
|
||||
panic("type kind is different from value kind")
|
||||
}
|
||||
print(indent+"reflect type: ", rt.Kind().String())
|
||||
if rv.CanSet() {
|
||||
print(" settable=", rv.CanSet())
|
||||
}
|
||||
println()
|
||||
switch rt.Kind() {
|
||||
case reflect.Bool:
|
||||
println(indent+" bool:", rv.Bool())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
println(indent+" int:", rv.Int())
|
||||
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
println(indent+" uint:", rv.Uint())
|
||||
case reflect.Float32, reflect.Float64:
|
||||
println(indent+" float:", rv.Float())
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
println(indent+" complex:", rv.Complex())
|
||||
case reflect.String:
|
||||
println(indent+" string:", rv.String(), rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
showValue(rv.Index(i), indent+" ")
|
||||
}
|
||||
case reflect.UnsafePointer:
|
||||
println(indent+" pointer:", rv.Pointer() != 0)
|
||||
case reflect.Array:
|
||||
println(indent + " array")
|
||||
case reflect.Chan:
|
||||
println(indent+" chan:", rt.Elem().Kind().String())
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
case reflect.Func:
|
||||
println(indent + " func")
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
case reflect.Interface:
|
||||
println(indent + " interface")
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
case reflect.Map:
|
||||
println(indent + " map")
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
case reflect.Ptr:
|
||||
println(indent+" pointer:", rv.Pointer() != 0, rt.Elem().Kind().String())
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
if !rv.IsNil() {
|
||||
showValue(rv.Elem(), indent+" ")
|
||||
}
|
||||
case reflect.Slice:
|
||||
println(indent+" slice:", rt.Elem().Kind().String(), rv.Len(), rv.Cap())
|
||||
println(indent+" pointer:", rv.Pointer() != 0)
|
||||
println(indent+" nil:", rv.IsNil())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
println(indent+" indexing:", i)
|
||||
showValue(rv.Index(i), indent+" ")
|
||||
}
|
||||
case reflect.Struct:
|
||||
println(indent + " struct")
|
||||
default:
|
||||
println(indent + " unknown type kind!")
|
||||
}
|
||||
}
|
||||
|
||||
func assertSize(ok bool, typ string) {
|
||||
if !ok {
|
||||
panic("size mismatch for type " + typ)
|
||||
}
|
||||
}
|
||||
Vendored
+215
@@ -0,0 +1,215 @@
|
||||
matching types
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
|
||||
values of interfaces
|
||||
reflect type: bool
|
||||
bool: true
|
||||
reflect type: bool
|
||||
bool: false
|
||||
reflect type: int
|
||||
int: 2000
|
||||
reflect type: int
|
||||
int: -2000
|
||||
reflect type: uint
|
||||
uint: 2000
|
||||
reflect type: int8
|
||||
int: -3
|
||||
reflect type: int8
|
||||
int: 3
|
||||
reflect type: uint8
|
||||
uint: 200
|
||||
reflect type: int16
|
||||
int: -300
|
||||
reflect type: int16
|
||||
int: 300
|
||||
reflect type: uint16
|
||||
uint: 50000
|
||||
reflect type: int32
|
||||
int: 7340032
|
||||
reflect type: int32
|
||||
int: -7340032
|
||||
reflect type: uint32
|
||||
uint: 7340032
|
||||
reflect type: int64
|
||||
int: 9895604649984
|
||||
reflect type: int64
|
||||
int: -9895604649984
|
||||
reflect type: uint64
|
||||
uint: 9895604649984
|
||||
reflect type: uintptr
|
||||
uint: 12345
|
||||
reflect type: float32
|
||||
float: +3.140000e+000
|
||||
reflect type: float64
|
||||
float: +3.140000e+000
|
||||
reflect type: complex64
|
||||
complex: (+1.200000e+000+3.000000e-001i)
|
||||
reflect type: complex128
|
||||
complex: (+1.300000e+000+4.000000e-001i)
|
||||
reflect type: int
|
||||
int: 32
|
||||
reflect type: string
|
||||
string: foo 3
|
||||
reflect type: uint8
|
||||
uint: 102
|
||||
reflect type: uint8
|
||||
uint: 111
|
||||
reflect type: uint8
|
||||
uint: 111
|
||||
reflect type: unsafe.Pointer
|
||||
pointer: true
|
||||
reflect type: chan
|
||||
chan: int
|
||||
nil: true
|
||||
reflect type: ptr
|
||||
pointer: true int
|
||||
nil: false
|
||||
reflect type: int settable=true
|
||||
int: 0
|
||||
reflect type: ptr
|
||||
pointer: true interface
|
||||
nil: false
|
||||
reflect type: interface settable=true
|
||||
interface
|
||||
nil: true
|
||||
reflect type: ptr
|
||||
pointer: true int
|
||||
nil: false
|
||||
reflect type: int settable=true
|
||||
int: 42
|
||||
reflect type: slice
|
||||
slice: uint8 3 3
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: uint8 settable=true
|
||||
uint: 1
|
||||
indexing: 1
|
||||
reflect type: uint8 settable=true
|
||||
uint: 2
|
||||
indexing: 2
|
||||
reflect type: uint8 settable=true
|
||||
uint: 3
|
||||
reflect type: slice
|
||||
slice: uint8 2 5
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: uint8 settable=true
|
||||
uint: 0
|
||||
indexing: 1
|
||||
reflect type: uint8 settable=true
|
||||
uint: 0
|
||||
reflect type: slice
|
||||
slice: int32 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: int32 settable=true
|
||||
int: 3
|
||||
indexing: 1
|
||||
reflect type: int32 settable=true
|
||||
int: 5
|
||||
reflect type: slice
|
||||
slice: string 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: string settable=true
|
||||
string: xyz 3
|
||||
reflect type: uint8
|
||||
uint: 120
|
||||
reflect type: uint8
|
||||
uint: 121
|
||||
reflect type: uint8
|
||||
uint: 122
|
||||
indexing: 1
|
||||
reflect type: string settable=true
|
||||
string: Z 1
|
||||
reflect type: uint8
|
||||
uint: 90
|
||||
reflect type: slice
|
||||
slice: uint8 0 0
|
||||
pointer: false
|
||||
nil: true
|
||||
reflect type: slice
|
||||
slice: uint8 0 0
|
||||
pointer: true
|
||||
nil: false
|
||||
reflect type: slice
|
||||
slice: float32 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: float32 settable=true
|
||||
float: +1.000000e+000
|
||||
indexing: 1
|
||||
reflect type: float32 settable=true
|
||||
float: +1.320000e+000
|
||||
reflect type: slice
|
||||
slice: float64 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: float64 settable=true
|
||||
float: +1.000000e+000
|
||||
indexing: 1
|
||||
reflect type: float64 settable=true
|
||||
float: +1.640000e+000
|
||||
reflect type: slice
|
||||
slice: complex64 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: complex64 settable=true
|
||||
complex: (+1.000000e+000+0.000000e+000i)
|
||||
indexing: 1
|
||||
reflect type: complex64 settable=true
|
||||
complex: (+1.640000e+000+3.000000e-001i)
|
||||
reflect type: slice
|
||||
slice: complex128 2 2
|
||||
pointer: true
|
||||
nil: false
|
||||
indexing: 0
|
||||
reflect type: complex128 settable=true
|
||||
complex: (+1.000000e+000+0.000000e+000i)
|
||||
indexing: 1
|
||||
reflect type: complex128 settable=true
|
||||
complex: (+1.128000e+000+4.000000e-001i)
|
||||
reflect type: array
|
||||
array
|
||||
reflect type: func
|
||||
func
|
||||
nil: true
|
||||
reflect type: func
|
||||
func
|
||||
nil: false
|
||||
reflect type: map
|
||||
map
|
||||
nil: true
|
||||
reflect type: map
|
||||
map
|
||||
nil: false
|
||||
reflect type: struct
|
||||
struct
|
||||
reflect type: struct
|
||||
struct
|
||||
|
||||
sizes:
|
||||
int8 1
|
||||
int16 2
|
||||
int32 4
|
||||
int64 8
|
||||
uint8 1
|
||||
uint16 2
|
||||
uint32 4
|
||||
uint64 8
|
||||
float32 4
|
||||
float64 8
|
||||
complex64 8
|
||||
complex128 16
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("stdin: ", os.Stdin.Fd())
|
||||
fmt.Println("stdout:", os.Stdout.Fd())
|
||||
fmt.Println("stderr:", os.Stderr.Fd())
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
stdin: 0
|
||||
stdout: 1
|
||||
stderr: 2
|
||||
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
// version of this package.
|
||||
// Update this value before release of new version of software.
|
||||
const version = "0.2.0"
|
||||
Reference in New Issue
Block a user