mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-27 15:18:41 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 63e3c3ec24 |
@@ -108,7 +108,7 @@ jobs:
|
||||
- image: golang:1.25-bullseye
|
||||
steps:
|
||||
- test-linux:
|
||||
llvm: "20"
|
||||
llvm: "21"
|
||||
resource_class: large
|
||||
|
||||
workflows:
|
||||
|
||||
@@ -16,15 +16,14 @@ jobs:
|
||||
name: build-macos
|
||||
strategy:
|
||||
matrix:
|
||||
# macos-14: arm64 (oldest supported version as of 18-11-2025)
|
||||
# macos-15-intel: amd64 (last intel version to be supported by github runners)
|
||||
# See https://github.com/actions/runner-images/issues/13046
|
||||
os: [macos-14, macos-15-intel]
|
||||
# macos-13: amd64 (oldest supported version as of 18-10-2024)
|
||||
# macos-14: arm64 (oldest arm64 version)
|
||||
os: [macos-13, macos-14]
|
||||
include:
|
||||
- os: macos-13
|
||||
goarch: amd64
|
||||
- os: macos-14
|
||||
goarch: arm64
|
||||
- os: macos-15-intel
|
||||
goarch: amd64
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Install Dependencies
|
||||
@@ -40,7 +39,7 @@ jobs:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.1'
|
||||
cache: true
|
||||
- name: Restore LLVM source cache
|
||||
uses: actions/cache/restore@v4
|
||||
@@ -118,7 +117,7 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
matrix:
|
||||
version: [16, 17, 18, 19, 20]
|
||||
version: [16, 17, 18, 19, 20, 21]
|
||||
steps:
|
||||
- name: Set up Homebrew
|
||||
uses: Homebrew/actions/setup-homebrew@master
|
||||
@@ -135,15 +134,15 @@ jobs:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.1'
|
||||
cache: true
|
||||
- name: Build TinyGo (LLVM ${{ matrix.version }})
|
||||
run: go install -tags=llvm${{ matrix.version }}
|
||||
- name: Check binary
|
||||
run: tinygo version
|
||||
- name: Build TinyGo (default LLVM)
|
||||
if: matrix.version == 20
|
||||
if: matrix.version == 21
|
||||
run: go install
|
||||
- name: Check binary
|
||||
if: matrix.version == 20
|
||||
if: matrix.version == 21
|
||||
run: tinygo version
|
||||
|
||||
@@ -31,7 +31,7 @@ jobs:
|
||||
sudo rm -rf /usr/local/share/boost
|
||||
df -h
|
||||
- name: Check out the repo
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Set up Docker Buildx
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v6
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
@@ -66,3 +66,45 @@ jobs:
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
- name: Trigger Drivers repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/drivers/actions/workflows/build.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
- name: Trigger Bluetooth repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/bluetooth/actions/workflows/linux.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
- name: Trigger TinyFS repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/tinyfs/actions/workflows/build.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
- name: Trigger TinyFont repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/tinyfont/actions/workflows/build.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
- name: Trigger TinyDraw repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/tinydraw/actions/workflows/build.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
- name: Trigger TinyTerm repo build on Github Actions
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer ${{secrets.GHA_ACCESS_TOKEN}}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/tinygo-org/tinyterm/actions/workflows/build.yml/dispatches \
|
||||
-d '{"ref": "dev"}'
|
||||
|
||||
+10
-10
@@ -31,7 +31,7 @@ jobs:
|
||||
# We're not on a multi-user machine, so this is safe.
|
||||
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Extract TinyGo version
|
||||
@@ -131,13 +131,13 @@ jobs:
|
||||
needs: build-linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Install wasmtime
|
||||
uses: bytecodealliance/actions/wasmtime/setup@v1
|
||||
@@ -164,7 +164,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install apt dependencies
|
||||
@@ -179,9 +179,9 @@ jobs:
|
||||
simavr \
|
||||
ninja-build
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v4
|
||||
@@ -284,7 +284,7 @@ jobs:
|
||||
needs: build-linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
- name: Get TinyGo version
|
||||
id: version
|
||||
run: ./.github/workflows/tinygo-extract-version.sh | tee -a "$GITHUB_OUTPUT"
|
||||
@@ -296,9 +296,9 @@ jobs:
|
||||
g++-${{ matrix.toolchain }} \
|
||||
libc6-dev-${{ matrix.libc }}-cross
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Restore LLVM source cache
|
||||
uses: actions/cache/restore@v4
|
||||
|
||||
@@ -25,7 +25,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Check out the repo
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Set up Docker Buildx
|
||||
@@ -52,7 +52,7 @@ jobs:
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v6
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
target: tinygo-llvm-build
|
||||
context: .
|
||||
|
||||
@@ -21,7 +21,7 @@ jobs:
|
||||
# See: https://github.com/tinygo-org/tinygo/pull/4516#issuecomment-2416363668
|
||||
run: sudo apt-get remove llvm-18
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
- name: Pull musl, bdwgc
|
||||
run: |
|
||||
git submodule update --init lib/musl lib/bdwgc
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
# still works after checking out the dev branch (that is, when going from LLVM
|
||||
# 16 to LLVM 17 for example, both Clang 16 and Clang 17 are installed).
|
||||
|
||||
echo 'deb https://apt.llvm.org/noble/ llvm-toolchain-noble-20 main' | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
echo 'deb https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
sudo apt-get update
|
||||
sudo apt-get install --no-install-recommends -y \
|
||||
llvm-20-dev \
|
||||
clang-20 \
|
||||
libclang-20-dev \
|
||||
lld-20
|
||||
llvm-21-dev \
|
||||
clang-21 \
|
||||
libclang-21-dev \
|
||||
lld-21
|
||||
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
run: |
|
||||
echo "$HOME/go/bin" >> $GITHUB_PATH
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # fetch all history (no sparse checkout)
|
||||
submodules: true
|
||||
|
||||
@@ -31,7 +31,7 @@ jobs:
|
||||
run: |
|
||||
scoop install ninja binaryen
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Extract TinyGo version
|
||||
@@ -39,9 +39,9 @@ jobs:
|
||||
shell: bash
|
||||
run: ./.github/workflows/tinygo-extract-version.sh | tee -a "$GITHUB_OUTPUT"
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Restore cached LLVM source
|
||||
uses: actions/cache/restore@v4
|
||||
@@ -143,11 +143,11 @@ jobs:
|
||||
run: |
|
||||
scoop install binaryen
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Download TinyGo build
|
||||
uses: actions/download-artifact@v4
|
||||
@@ -173,11 +173,11 @@ jobs:
|
||||
maximum-size: 24GB
|
||||
disk-root: "C:"
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Download TinyGo build
|
||||
uses: actions/download-artifact@v4
|
||||
@@ -209,11 +209,11 @@ jobs:
|
||||
run: |
|
||||
scoop install binaryen && scoop install wasmtime@29.0.1
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v6
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25.5'
|
||||
go-version: '1.25.0'
|
||||
cache: true
|
||||
- name: Download TinyGo build
|
||||
uses: actions/download-artifact@v4
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
url = https://github.com/WebAssembly/wasi-libc
|
||||
[submodule "lib/picolibc"]
|
||||
path = lib/picolibc
|
||||
url = https://github.com/picolibc/picolibc.git
|
||||
url = https://github.com/keith-packard/picolibc.git
|
||||
[submodule "lib/stm32-svd"]
|
||||
path = lib/stm32-svd
|
||||
url = https://github.com/tinygo-org/stm32-svd
|
||||
|
||||
-101
@@ -1,104 +1,3 @@
|
||||
0.40.1
|
||||
---
|
||||
* **machine**
|
||||
- nrf: fix flash writes when SoftDevice is enabled
|
||||
* **runtime**
|
||||
- runtime: avoid fixed math/rand sequence on RP2040/RP2350 (#5124)
|
||||
- runtime: add calls to initRand() during run() for all schedulers
|
||||
- runtime: call initRand() before initHeap() during initialization
|
||||
- runtime: use rand_hwrng hardwareRand for RP2040/RP2350 (#5135)
|
||||
* **libs**
|
||||
- picolibc: use updated location for git repo
|
||||
|
||||
0.40.0
|
||||
---
|
||||
* **general**
|
||||
- all: add full LLVM 20 support
|
||||
- core: feat: enable //go:linkname pragma for globals
|
||||
- core: feature: Add flag to ignore go compatibility matrix (#5078)
|
||||
- chore: update version for 0.40 development cycle
|
||||
* **compiler**
|
||||
- emit an error when the actual arch doesn't match GOARCH
|
||||
- mark string parameters as readonly
|
||||
- use Tarjan's SCC algorithm to detect loops for defer
|
||||
- lower "large stack" limit to 16kb
|
||||
* **core**
|
||||
- shrink bdwgc library
|
||||
- Fix linker errors for runtime.vgetrandom and crypto/internal/sysrand.fatal
|
||||
- fix: add TryLock to sync.RWMutex
|
||||
- fix: correct linter issues exposed by the fix in #4679
|
||||
- fix: don't hardcode success return state
|
||||
- fix: expand RTT debugger compatibility
|
||||
- internal/task (threads): save stack bounds instead of scanning under a lock
|
||||
- internal/task: create detached threads and fix error handling
|
||||
- interp: better errors when debugging interp
|
||||
- transform (gc): create stack slots in callers of external functions
|
||||
- internal/task: prevent semaphore resource leak for threads scheduler
|
||||
* **machine**
|
||||
- cortexm: optimize code size for the HardFault_Handler
|
||||
- fe310: add I2C pins for the HiFive1b
|
||||
- clarify WriteAt semantics of BlockDevice
|
||||
- fix deprecated AsmFull comment (#5005)
|
||||
- make sure DMA buffers do not escape unnecessarily
|
||||
- only enable USB-CDC when needed
|
||||
- use larger SPI MAXCNT on nrf52833 and nrf52840
|
||||
- fix: update m.queuedBytes when clamping output to avoid corrupting sentBytes
|
||||
- fix: use int64 in ReadTemperature to avoid overflow
|
||||
- fix(rp2): disable DBGPAUSE on startup
|
||||
- fix(rp2): possible integer overflow while computing factors for SPI baudrate
|
||||
- fix(rp2): reset spinlocks at startup
|
||||
- fix(rp2): switch spinlock busy loop to wfe
|
||||
- fix(rp2): use side-effect-free spinlocks
|
||||
- nrf: add ADC_VDDH which is an ADC pin for VDDH
|
||||
- nrf: don't block SPI transfer
|
||||
- nrf: don't set PSELN, it's ignored in single ended mode anyway
|
||||
- nrf: fix typo in ADC configuration
|
||||
- nrf: refactor SoftDevice enabled check
|
||||
- nrf: rename pwmPin to adcPin
|
||||
- nrf: support flash operations while the SoftDevice is enabled
|
||||
- rp2040: allow writing to the UART inside interrupts
|
||||
- machine,nrf528: stop the bus only once on I2C bus error and ensures the first error is returned
|
||||
* **net**
|
||||
- update submodule to latest commits
|
||||
* **runtime**
|
||||
- (avr): fix infinite longjmp loop if stack is aligned to 256 bytes
|
||||
- (gc_blocks.go): clear full size of allocation
|
||||
- (gc_blocks.go): make sweep branchless
|
||||
- (gc_blocks.go): simplify scanning logic
|
||||
- (gc_blocks.go): use a linked stack to scan marked objects
|
||||
- (gc_blocks.go): use best-fit allocation
|
||||
- (gc_boehm.go): fix world already stopped check
|
||||
- (wasm): scan the system stack
|
||||
- fix sleep duration for long sleeps
|
||||
- remove copied code for nrf52840
|
||||
- src/syscall: update src buffer after write
|
||||
- wasm: fix C realloc and optimize it a bit
|
||||
* **targets**
|
||||
- add xiao-esp32s3 board target
|
||||
- Add ESP32-S3 support (#5091)
|
||||
- Added Gopher ARCADE board
|
||||
- Create "pico2-ice" target board (#5062)
|
||||
* **build/test**
|
||||
- Add testing.T.Context() and testing.B.Context()
|
||||
- create separate go.mod file for testing dependencies for wasm tests that use Chromium headless browser to avoid use of older incompatible version.
|
||||
- go back to normal scheduler instead of tasks scheduler for macos CI
|
||||
- update CI to use Go 1.25.5
|
||||
- update macOS GH actions builds to handle sunset of macOS 13
|
||||
- use task scheduler on macOS builds to avoid test race condition lockups
|
||||
- update all CI builds to use latest stable Go release. Also update some of the actions to their latest releases.
|
||||
- update GH actions builds to use Go 1.25.4
|
||||
- uninstall cmake before install
|
||||
- fix: point the submodule for musl-lib to a mirror in the TinyGo GitHub org
|
||||
- fix: remove macOS 15 from CI build matrix (conflicts with macOS 14 build)
|
||||
- fix: separate host expected bytes from device intended bytes
|
||||
- fix/typo: makeESPFirmwareImage
|
||||
- make: GNUmakefile: shrink TinyGo binaries on Linux
|
||||
- move the directory list into a variable
|
||||
- several improvements to the macOS GH actions build
|
||||
- Fix for #4678: top-level 'make lint' wasn't working
|
||||
- fix: increase the timeout for chromedp to connect to the headless browser used for running the wasm tests.
|
||||
- testdata: some more packages for the test corpus
|
||||
|
||||
0.39.0
|
||||
---
|
||||
* **general**
|
||||
|
||||
+1
-6
@@ -481,7 +481,6 @@ TEST_IOFS := false
|
||||
endif
|
||||
|
||||
TEST_SKIP_FLAG := -skip='TestExtraMethods|TestParseAndBytesRoundTrip/P256/Generic'
|
||||
TEST_ADDITIONAL_FLAGS ?=
|
||||
|
||||
# Test known-working standard library packages.
|
||||
# TODO: parallelize, and only show failing tests (no implied -v flag).
|
||||
@@ -489,7 +488,7 @@ TEST_ADDITIONAL_FLAGS ?=
|
||||
tinygo-test:
|
||||
@# TestExtraMethods: used by many crypto packages and uses reflect.Type.Method which is not implemented.
|
||||
@# TestParseAndBytesRoundTrip/P256/Generic: relies on t.Skip() which is not implemented
|
||||
$(TINYGO) test $(TEST_ADDITIONAL_FLAGS) $(TEST_SKIP_FLAG) $(TEST_PACKAGES_HOST) $(TEST_PACKAGES_SLOW)
|
||||
$(TINYGO) test $(TEST_SKIP_FLAG) $(TEST_PACKAGES_HOST) $(TEST_PACKAGES_SLOW)
|
||||
@# io/fs requires os.ReadDir, not yet supported on windows or wasi. It also
|
||||
@# requires a large stack-size. Hence, io/fs is only run conditionally.
|
||||
@# For more details, see the comments on issue #3143.
|
||||
@@ -622,8 +621,6 @@ smoketest: testchdir
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=feather-rp2040 examples/device-id
|
||||
@$(MD5SUM) test.hex
|
||||
$(TINYGO) build -size short -o test.hex -target=pico2-ice examples/blinky1
|
||||
@$(MD5SUM) test.hex
|
||||
# test simulated boards on play.tinygo.org
|
||||
ifneq ($(WASM), 0)
|
||||
GOOS=js GOARCH=wasm $(TINYGO) build -size short -o test.wasm -tags=arduino examples/blinky1
|
||||
@@ -915,8 +912,6 @@ ifneq ($(XTENSA), 0)
|
||||
@$(MD5SUM) test.bin
|
||||
$(TINYGO) build -size short -o test.bin -target mch2022 examples/machinetest
|
||||
@$(MD5SUM) test.bin
|
||||
$(TINYGO) build -size short -o test.bin -target=xiao-esp32s3 examples/blinky1
|
||||
@$(MD5SUM) test.bin
|
||||
endif
|
||||
$(TINYGO) build -size short -o test.bin -target=esp-c3-32s-kit examples/blinky1
|
||||
@$(MD5SUM) test.bin
|
||||
|
||||
+3
-6
@@ -30,10 +30,7 @@ var BoehmGC = Library{
|
||||
// Use a minimal environment.
|
||||
"-DNO_MSGBOX_ON_ERROR", // don't call MessageBoxA on Windows
|
||||
"-DDONT_USE_ATEXIT",
|
||||
"-DNO_GETENV", // smaller binary, more predictable configuration
|
||||
"-DNO_CLOCK", // don't use system clock
|
||||
"-DNO_DEBUGGING", // reduce code size
|
||||
"-DGC_NO_FINALIZATION", // finalization is not used at the moment
|
||||
"-DNO_GETENV",
|
||||
|
||||
// Special flag to work around the lack of __data_start in ld.lld.
|
||||
// TODO: try to fix this in LLVM/lld directly so we don't have to
|
||||
@@ -42,8 +39,6 @@ var BoehmGC = Library{
|
||||
|
||||
// Do not scan the stack. We have our own mechanism to do this.
|
||||
"-DSTACK_NOT_SCANNED",
|
||||
"-DNO_PROC_STAT", // we scan the stack manually (don't read /proc/self/stat on Linux)
|
||||
"-DSTACKBOTTOM=0", // dummy value, we scan the stack manually
|
||||
|
||||
// Assertions can be enabled while debugging GC issues.
|
||||
//"-DGC_ASSERTIONS",
|
||||
@@ -68,6 +63,7 @@ var BoehmGC = Library{
|
||||
"blacklst.c",
|
||||
"dbg_mlc.c",
|
||||
"dyn_load.c",
|
||||
"finalize.c",
|
||||
"headers.c",
|
||||
"mach_dep.c",
|
||||
"malloc.c",
|
||||
@@ -75,6 +71,7 @@ var BoehmGC = Library{
|
||||
"mark_rts.c",
|
||||
"misc.c",
|
||||
"new_hblk.c",
|
||||
"obj_map.c",
|
||||
"os_dep.c",
|
||||
"reclaim.c",
|
||||
}
|
||||
|
||||
+1
-1
@@ -1042,7 +1042,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
case "esp32", "esp32-img", "esp32c3", "esp32s3", "esp8266":
|
||||
case "esp32", "esp32-img", "esp32c3", "esp8266":
|
||||
// Special format for the ESP family of chips (parsed by the ROM
|
||||
// bootloader).
|
||||
result.Binary = filepath.Join(tmpdir, "main"+outext)
|
||||
|
||||
@@ -28,7 +28,6 @@ func TestClangAttributes(t *testing.T) {
|
||||
"cortex-m4",
|
||||
"cortex-m7",
|
||||
"esp32c3",
|
||||
"esp32s3",
|
||||
"fe310",
|
||||
"gameboy-advance",
|
||||
"k210",
|
||||
|
||||
+4
-7
@@ -33,13 +33,10 @@ func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.GoCompatibility {
|
||||
if gorootMajor != 1 || gorootMinor < minorMin || gorootMinor > minorMax {
|
||||
// Note: when this gets updated, also update the Go compatibility matrix:
|
||||
// https://github.com/tinygo-org/tinygo-site/blob/dev/content/docs/reference/go-compat-matrix.md
|
||||
return nil, fmt.Errorf("requires go version 1.%d through 1.%d, got go%d.%d", minorMin, minorMax, gorootMajor, gorootMinor)
|
||||
}
|
||||
if gorootMajor != 1 || gorootMinor < minorMin || gorootMinor > minorMax {
|
||||
// Note: when this gets updated, also update the Go compatibility matrix:
|
||||
// https://github.com/tinygo-org/tinygo-site/blob/dev/content/docs/reference/go-compat-matrix.md
|
||||
return nil, fmt.Errorf("requires go version 1.%d through 1.%d, got go%d.%d", minorMin, minorMax, gorootMajor, gorootMinor)
|
||||
}
|
||||
|
||||
// Check that the Go toolchain version isn't too new, if we haven't been
|
||||
|
||||
+1
-2
@@ -100,12 +100,11 @@ func makeESPFirmwareImage(infile, outfile, format string) error {
|
||||
chip_id := map[string]uint16{
|
||||
"esp32": 0x0000,
|
||||
"esp32c3": 0x0005,
|
||||
"esp32s3": 0x0009,
|
||||
}[chip]
|
||||
|
||||
// Image header.
|
||||
switch chip {
|
||||
case "esp32", "esp32c3", "esp32s3":
|
||||
case "esp32", "esp32c3":
|
||||
// Header format:
|
||||
// https://github.com/espressif/esp-idf/blob/v4.3/components/bootloader_support/include/esp_app_format.h#L71
|
||||
// Note: not adding a SHA256 hash as the binary is modified by
|
||||
|
||||
@@ -42,9 +42,9 @@ func TestBinarySize(t *testing.T) {
|
||||
// This is a small number of very diverse targets that we want to test.
|
||||
tests := []sizeTest{
|
||||
// microcontrollers
|
||||
{"hifive1b", "examples/echo", 3668, 280, 0, 2244},
|
||||
{"microbit", "examples/serial", 2694, 342, 8, 2248},
|
||||
{"wioterminal", "examples/pininterrupt", 7187, 1489, 116, 6888},
|
||||
{"hifive1b", "examples/echo", 3884, 280, 0, 2268},
|
||||
{"microbit", "examples/serial", 2924, 388, 8, 2272},
|
||||
{"wioterminal", "examples/pininterrupt", 7365, 1491, 116, 6912},
|
||||
|
||||
// TODO: also check wasm. Right now this is difficult, because
|
||||
// wasm binaries are run through wasm-opt and therefore the
|
||||
|
||||
@@ -21,6 +21,10 @@ import (
|
||||
)
|
||||
|
||||
/*
|
||||
// Hide a warning in LLVM 21 that doesn't apply to us (appears to be a side
|
||||
// effect of how CGo processes C header files).
|
||||
#cgo CFLAGS: -Wno-deprecated-declarations
|
||||
|
||||
#include <clang-c/Index.h> // If this fails, libclang headers aren't available. Please take a look here: https://tinygo.org/docs/guides/build/
|
||||
#include <llvm/Config/llvm-config.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build !byollvm && !llvm15 && !llvm16 && !llvm17 && !llvm18 && !llvm19
|
||||
//go:build !byollvm && llvm20
|
||||
|
||||
package cgo
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build !byollvm && !llvm15 && !llvm16 && !llvm17 && !llvm18 && !llvm19 && !llvm20
|
||||
|
||||
package cgo
|
||||
|
||||
/*
|
||||
#cgo linux CFLAGS: -I/usr/include/llvm-21 -I/usr/include/llvm-c-21 -I/usr/lib/llvm-21/include
|
||||
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/llvm@21/include
|
||||
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/llvm@21/include
|
||||
#cgo freebsd CFLAGS: -I/usr/local/llvm21/include
|
||||
#cgo linux LDFLAGS: -L/usr/lib/llvm-21/lib -lclang
|
||||
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@21/lib -lclang
|
||||
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@21/lib -lclang
|
||||
#cgo freebsd LDFLAGS: -L/usr/local/llvm21/lib -lclang
|
||||
*/
|
||||
import "C"
|
||||
@@ -226,7 +226,7 @@ func (c *Config) StackSize() uint64 {
|
||||
|
||||
// MaxStackAlloc returns the size of the maximum allocation to put on the stack vs heap.
|
||||
func (c *Config) MaxStackAlloc() uint64 {
|
||||
if c.StackSize() >= 16*1024 {
|
||||
if c.StackSize() > 32*1024 {
|
||||
return 1024
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ type Options struct {
|
||||
WITPackage string // pass through to wasm-tools component embed invocation
|
||||
WITWorld string // pass through to wasm-tools component embed -w option
|
||||
ExtLDFlags []string
|
||||
GoCompatibility bool // enable to check for Go version compatibility
|
||||
}
|
||||
|
||||
// Verify performs a validation on the given options, raising an error if options are not valid.
|
||||
|
||||
+1
-1
@@ -245,7 +245,7 @@ func (b *builder) createRuntimeAssert(assert llvm.Value, blockPrefix, assertFunc
|
||||
// current insert position.
|
||||
faultBlock := b.ctx.AddBasicBlock(b.llvmFn, blockPrefix+".throw")
|
||||
nextBlock := b.insertBasicBlock(blockPrefix + ".next")
|
||||
b.currentBlockInfo.exit = nextBlock // adjust outgoing block for phi nodes
|
||||
b.blockExits[b.currentBlock] = nextBlock // adjust outgoing block for phi nodes
|
||||
|
||||
// Now branch to the out-of-bounds or the regular block.
|
||||
b.CreateCondBr(assert, faultBlock, nextBlock)
|
||||
|
||||
@@ -32,9 +32,6 @@ const (
|
||||
// Whether this is a full or partial Go parameter (int, slice, etc).
|
||||
// The extra context parameter is not a Go parameter.
|
||||
paramIsGoParam = 1 << iota
|
||||
|
||||
// Whether this is a readonly parameter (for example, a string pointer).
|
||||
paramIsReadonly
|
||||
)
|
||||
|
||||
// createRuntimeCallCommon creates a runtime call. Use createRuntimeCall or
|
||||
@@ -170,7 +167,6 @@ func (c *compilerContext) flattenAggregateType(t llvm.Type, name string, goType
|
||||
continue
|
||||
}
|
||||
suffix := strconv.Itoa(i)
|
||||
isString := false
|
||||
if goType != nil {
|
||||
// Try to come up with a good suffix for this struct field,
|
||||
// depending on which Go type it's based on.
|
||||
@@ -187,16 +183,12 @@ func (c *compilerContext) flattenAggregateType(t llvm.Type, name string, goType
|
||||
suffix = []string{"r", "i"}[i]
|
||||
case types.String:
|
||||
suffix = []string{"data", "len"}[i]
|
||||
isString = true
|
||||
}
|
||||
case *types.Signature:
|
||||
suffix = []string{"context", "funcptr"}[i]
|
||||
}
|
||||
}
|
||||
subInfos := c.flattenAggregateType(subfield, name+"."+suffix, extractSubfield(goType, i))
|
||||
if isString {
|
||||
subInfos[0].flags |= paramIsReadonly
|
||||
}
|
||||
paramInfos = append(paramInfos, subInfos...)
|
||||
}
|
||||
return paramInfos
|
||||
|
||||
+21
-42
@@ -84,6 +84,7 @@ type compilerContext struct {
|
||||
funcPtrType llvm.Type // pointer in function address space (1 for AVR, 0 elsewhere)
|
||||
funcPtrAddrSpace int
|
||||
uintptrType llvm.Type
|
||||
nocaptureAttr llvm.Attribute
|
||||
program *ssa.Program
|
||||
diagnostics []error
|
||||
functionInfos map[*ssa.Function]functionInfo
|
||||
@@ -135,6 +136,13 @@ func newCompilerContext(moduleName string, machine llvm.TargetMachine, config *C
|
||||
c.funcPtrType = dummyFunc.Type()
|
||||
dummyFunc.EraseFromParentAsFunction()
|
||||
|
||||
// The attribute "nocapture" changed to "captures(none)" in LLVM 21.
|
||||
if llvmutil.Version() < 21 {
|
||||
c.nocaptureAttr = c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)
|
||||
} else {
|
||||
c.nocaptureAttr = c.ctx.CreateEnumAttribute(llvm.AttributeKindID("captures"), 0)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -152,12 +160,10 @@ type builder struct {
|
||||
llvmFnType llvm.Type
|
||||
llvmFn llvm.Value
|
||||
info functionInfo
|
||||
locals map[ssa.Value]llvm.Value // local variables
|
||||
blockInfo []blockInfo
|
||||
locals map[ssa.Value]llvm.Value // local variables
|
||||
blockEntries map[*ssa.BasicBlock]llvm.BasicBlock // a *ssa.BasicBlock may be split up
|
||||
blockExits map[*ssa.BasicBlock]llvm.BasicBlock // these are the exit blocks
|
||||
currentBlock *ssa.BasicBlock
|
||||
currentBlockInfo *blockInfo
|
||||
tarjanStack []uint
|
||||
tarjanIndex uint
|
||||
phis []phiNode
|
||||
deferPtr llvm.Value
|
||||
deferFrame llvm.Value
|
||||
@@ -189,22 +195,11 @@ func newBuilder(c *compilerContext, irbuilder llvm.Builder, f *ssa.Function) *bu
|
||||
info: c.getFunctionInfo(f),
|
||||
locals: make(map[ssa.Value]llvm.Value),
|
||||
dilocals: make(map[*types.Var]llvm.Metadata),
|
||||
blockEntries: make(map[*ssa.BasicBlock]llvm.BasicBlock),
|
||||
blockExits: make(map[*ssa.BasicBlock]llvm.BasicBlock),
|
||||
}
|
||||
}
|
||||
|
||||
type blockInfo struct {
|
||||
// entry is the LLVM basic block corresponding to the start of this *ssa.Block.
|
||||
entry llvm.BasicBlock
|
||||
|
||||
// exit is the LLVM basic block corresponding to the end of this *ssa.Block.
|
||||
// It will be different than entry if any of the block's instructions contain internal branches.
|
||||
exit llvm.BasicBlock
|
||||
|
||||
// tarjan holds state for applying Tarjan's strongly connected components algorithm to the CFG.
|
||||
// This is used by defer.go to determine whether to stack- or heap-allocate defer data.
|
||||
tarjan tarjanNode
|
||||
}
|
||||
|
||||
type deferBuiltin struct {
|
||||
callName string
|
||||
pos token.Pos
|
||||
@@ -1233,29 +1228,14 @@ func (b *builder) createFunctionStart(intrinsic bool) {
|
||||
// intrinsic (like an atomic operation). Create the entry block
|
||||
// manually.
|
||||
entryBlock = b.ctx.AddBasicBlock(b.llvmFn, "entry")
|
||||
// Intrinsics may create internal branches (e.g. nil checks).
|
||||
// They will attempt to access b.currentBlockInfo to update the exit block.
|
||||
// Create some fake block info for them to access.
|
||||
blockInfo := []blockInfo{
|
||||
{
|
||||
entry: entryBlock,
|
||||
exit: entryBlock,
|
||||
},
|
||||
}
|
||||
b.blockInfo = blockInfo
|
||||
b.currentBlockInfo = &blockInfo[0]
|
||||
} else {
|
||||
blocks := b.fn.Blocks
|
||||
blockInfo := make([]blockInfo, len(blocks))
|
||||
for _, block := range b.fn.DomPreorder() {
|
||||
info := &blockInfo[block.Index]
|
||||
llvmBlock := b.ctx.AddBasicBlock(b.llvmFn, block.Comment)
|
||||
info.entry = llvmBlock
|
||||
info.exit = llvmBlock
|
||||
b.blockEntries[block] = llvmBlock
|
||||
b.blockExits[block] = llvmBlock
|
||||
}
|
||||
b.blockInfo = blockInfo
|
||||
// Normal functions have an entry block.
|
||||
entryBlock = blockInfo[0].entry
|
||||
entryBlock = b.blockEntries[b.fn.Blocks[0]]
|
||||
}
|
||||
b.SetInsertPointAtEnd(entryBlock)
|
||||
|
||||
@@ -1351,9 +1331,8 @@ func (b *builder) createFunction() {
|
||||
if b.DumpSSA {
|
||||
fmt.Printf("%d: %s:\n", block.Index, block.Comment)
|
||||
}
|
||||
b.SetInsertPointAtEnd(b.blockEntries[block])
|
||||
b.currentBlock = block
|
||||
b.currentBlockInfo = &b.blockInfo[block.Index]
|
||||
b.SetInsertPointAtEnd(b.currentBlockInfo.entry)
|
||||
for _, instr := range block.Instrs {
|
||||
if instr, ok := instr.(*ssa.DebugRef); ok {
|
||||
if !b.Debug {
|
||||
@@ -1413,7 +1392,7 @@ func (b *builder) createFunction() {
|
||||
block := phi.ssa.Block()
|
||||
for i, edge := range phi.ssa.Edges {
|
||||
llvmVal := b.getValue(edge, getPos(phi.ssa))
|
||||
llvmBlock := b.blockInfo[block.Preds[i].Index].exit
|
||||
llvmBlock := b.blockExits[block.Preds[i]]
|
||||
phi.llvm.AddIncoming([]llvm.Value{llvmVal}, []llvm.BasicBlock{llvmBlock})
|
||||
}
|
||||
}
|
||||
@@ -1527,11 +1506,11 @@ func (b *builder) createInstruction(instr ssa.Instruction) {
|
||||
case *ssa.If:
|
||||
cond := b.getValue(instr.Cond, getPos(instr))
|
||||
block := instr.Block()
|
||||
blockThen := b.blockInfo[block.Succs[0].Index].entry
|
||||
blockElse := b.blockInfo[block.Succs[1].Index].entry
|
||||
blockThen := b.blockEntries[block.Succs[0]]
|
||||
blockElse := b.blockEntries[block.Succs[1]]
|
||||
b.CreateCondBr(cond, blockThen, blockElse)
|
||||
case *ssa.Jump:
|
||||
blockJump := b.blockInfo[instr.Block().Succs[0].Index].entry
|
||||
blockJump := b.blockEntries[instr.Block().Succs[0]]
|
||||
b.CreateBr(blockJump)
|
||||
case *ssa.MapUpdate:
|
||||
m := b.getValue(instr.Map, getPos(instr))
|
||||
|
||||
+29
-99
@@ -100,7 +100,7 @@ func (b *builder) createLandingPad() {
|
||||
|
||||
// Continue at the 'recover' block, which returns to the parent in an
|
||||
// appropriate way.
|
||||
b.CreateBr(b.blockInfo[b.fn.Recover.Index].entry)
|
||||
b.CreateBr(b.blockEntries[b.fn.Recover])
|
||||
}
|
||||
|
||||
// Create a checkpoint (similar to setjmp). This emits inline assembly that
|
||||
@@ -234,108 +234,41 @@ func (b *builder) createInvokeCheckpoint() {
|
||||
continueBB := b.insertBasicBlock("")
|
||||
b.CreateCondBr(isZero, continueBB, b.landingpad)
|
||||
b.SetInsertPointAtEnd(continueBB)
|
||||
b.currentBlockInfo.exit = continueBB
|
||||
b.blockExits[b.currentBlock] = continueBB
|
||||
}
|
||||
|
||||
// isInLoop checks if there is a path from the current block to itself.
|
||||
// Use Tarjan's strongly connected components algorithm to search for cycles.
|
||||
// A one-node SCC is a cycle iff there is an edge from the node to itself.
|
||||
// A multi-node SCC is always a cycle.
|
||||
// https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
|
||||
func (b *builder) isInLoop() bool {
|
||||
if b.currentBlockInfo.tarjan.lowLink == 0 {
|
||||
b.strongConnect(b.currentBlock)
|
||||
}
|
||||
return b.currentBlockInfo.tarjan.cyclic
|
||||
}
|
||||
// isInLoop checks if there is a path from a basic block to itself.
|
||||
func isInLoop(start *ssa.BasicBlock) bool {
|
||||
// Use a breadth-first search to scan backwards through the block graph.
|
||||
queue := []*ssa.BasicBlock{start}
|
||||
checked := map[*ssa.BasicBlock]struct{}{}
|
||||
|
||||
func (b *builder) strongConnect(block *ssa.BasicBlock) {
|
||||
// Assign a new index.
|
||||
// Indices start from 1 so that 0 can be used as a sentinel.
|
||||
assignedIndex := b.tarjanIndex + 1
|
||||
b.tarjanIndex = assignedIndex
|
||||
for len(queue) > 0 {
|
||||
// pop a block off of the queue
|
||||
block := queue[len(queue)-1]
|
||||
queue = queue[:len(queue)-1]
|
||||
|
||||
// Apply the new index.
|
||||
blockIndex := block.Index
|
||||
node := &b.blockInfo[blockIndex].tarjan
|
||||
node.lowLink = assignedIndex
|
||||
|
||||
// Push the node onto the stack.
|
||||
node.onStack = true
|
||||
b.tarjanStack = append(b.tarjanStack, uint(blockIndex))
|
||||
|
||||
// Process the successors.
|
||||
for _, successor := range block.Succs {
|
||||
// Look up the successor's state.
|
||||
successorIndex := successor.Index
|
||||
if successorIndex == blockIndex {
|
||||
// Handle a self-cycle specially.
|
||||
node.cyclic = true
|
||||
continue
|
||||
}
|
||||
successorNode := &b.blockInfo[successorIndex].tarjan
|
||||
|
||||
switch {
|
||||
case successorNode.lowLink == 0:
|
||||
// This node has not yet been visisted.
|
||||
b.strongConnect(successor)
|
||||
|
||||
case !successorNode.onStack:
|
||||
// This node has been visited, but is in a different SCC.
|
||||
// Ignore it, and do not update lowLink.
|
||||
continue
|
||||
}
|
||||
|
||||
// Update the lowLink index.
|
||||
// This always uses the min-of-lowlink instead of using index in the on-stack case.
|
||||
// This is done for two reasons:
|
||||
// 1. The lowLink update can be shared between the new-node and on-stack cases.
|
||||
// 2. The assigned index does not need to be saved - it is only needed for root node detection.
|
||||
if successorNode.lowLink < node.lowLink {
|
||||
node.lowLink = successorNode.lowLink
|
||||
}
|
||||
}
|
||||
|
||||
if node.lowLink == assignedIndex {
|
||||
// This is a root node.
|
||||
// Pop the SCC off the stack.
|
||||
stack := b.tarjanStack
|
||||
top := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
blocks := b.blockInfo
|
||||
topNode := &blocks[top].tarjan
|
||||
topNode.onStack = false
|
||||
|
||||
if top != uint(blockIndex) {
|
||||
// The root node is not the only node in the SCC.
|
||||
// Mark all nodes in this SCC as cyclic.
|
||||
topNode.cyclic = true
|
||||
for top != uint(blockIndex) {
|
||||
top = stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
topNode = &blocks[top].tarjan
|
||||
topNode.onStack = false
|
||||
topNode.cyclic = true
|
||||
// Search through predecessors.
|
||||
// Searching backwards means that this is pretty fast when the block is close to the start of the function.
|
||||
// Defers are often placed near the start of the function.
|
||||
for _, pred := range block.Preds {
|
||||
if pred == start {
|
||||
// cycle found
|
||||
return true
|
||||
}
|
||||
|
||||
if _, ok := checked[pred]; ok {
|
||||
// block already checked
|
||||
continue
|
||||
}
|
||||
|
||||
// add to queue and checked map
|
||||
queue = append(queue, pred)
|
||||
checked[pred] = struct{}{}
|
||||
}
|
||||
|
||||
b.tarjanStack = stack
|
||||
}
|
||||
}
|
||||
|
||||
// tarjanNode holds per-block state for isInLoop and strongConnect.
|
||||
type tarjanNode struct {
|
||||
// lowLink is the index of the first visited node that is reachable from this block.
|
||||
// The lowLink indices are assigned by the SCC search, and do not correspond to b.Index.
|
||||
// A lowLink of 0 is used as a sentinel to mark a node which has not yet been visited.
|
||||
lowLink uint
|
||||
|
||||
// onStack tracks whether this node is currently on the SCC search stack.
|
||||
onStack bool
|
||||
|
||||
// cyclic indicates whether this block is in a loop.
|
||||
// If lowLink is 0, strongConnect must be called before reading this field.
|
||||
cyclic bool
|
||||
return false
|
||||
}
|
||||
|
||||
// createDefer emits a single defer instruction, to be run when this function
|
||||
@@ -477,10 +410,7 @@ func (b *builder) createDefer(instr *ssa.Defer) {
|
||||
|
||||
// Put this struct in an allocation.
|
||||
var alloca llvm.Value
|
||||
if instr.Block() != b.currentBlock {
|
||||
panic("block mismatch")
|
||||
}
|
||||
if !b.isInLoop() {
|
||||
if !isInLoop(instr.Block()) {
|
||||
// This can safely use a stack allocation.
|
||||
alloca = llvmutil.CreateEntryBlockAlloca(b.Builder, deferredCallType, "defer.alloca")
|
||||
} else {
|
||||
|
||||
@@ -38,10 +38,10 @@ func (b *builder) createInlineAsm(args []ssa.Value) (llvm.Value, error) {
|
||||
// provided immediately. For example:
|
||||
//
|
||||
// arm.AsmFull(
|
||||
// "str {value}, [{result}]",
|
||||
// "str {value}, {result}",
|
||||
// map[string]interface{}{
|
||||
// "value": 1,
|
||||
// "result": uintptr(unsafe.Pointer(&dest)),
|
||||
// "value": 1
|
||||
// "result": &dest,
|
||||
// })
|
||||
func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error) {
|
||||
asmString := constant.StringVal(instr.Args[0].(*ssa.Const).Value)
|
||||
|
||||
@@ -737,7 +737,7 @@ func (b *builder) createTypeAssert(expr *ssa.TypeAssert) llvm.Value {
|
||||
prevBlock := b.GetInsertBlock()
|
||||
okBlock := b.insertBasicBlock("typeassert.ok")
|
||||
nextBlock := b.insertBasicBlock("typeassert.next")
|
||||
b.currentBlockInfo.exit = nextBlock // adjust outgoing block for phi nodes
|
||||
b.blockExits[b.currentBlock] = nextBlock // adjust outgoing block for phi nodes
|
||||
b.CreateCondBr(commaOk, okBlock, nextBlock)
|
||||
|
||||
// Retrieve the value from the interface if the type assert was
|
||||
|
||||
@@ -27,8 +27,6 @@ func (b *builder) defineIntrinsicFunction() {
|
||||
b.createStackSaveImpl()
|
||||
case name == "runtime.KeepAlive":
|
||||
b.createKeepAliveImpl()
|
||||
case name == "machine.keepAliveNoEscape":
|
||||
b.createMachineKeepAliveImpl()
|
||||
case strings.HasPrefix(name, "runtime/volatile.Load"):
|
||||
b.createVolatileLoad()
|
||||
case strings.HasPrefix(name, "runtime/volatile.Store"):
|
||||
@@ -146,20 +144,6 @@ func (b *builder) createAbiEscapeImpl() {
|
||||
b.CreateRet(result)
|
||||
}
|
||||
|
||||
// Implement machine.keepAliveNoEscape, which makes sure the compiler keeps the
|
||||
// pointer parameter alive until this point (for GC).
|
||||
func (b *builder) createMachineKeepAliveImpl() {
|
||||
b.createFunctionStart(true)
|
||||
pointerValue := b.getValue(b.fn.Params[0], getPos(b.fn))
|
||||
|
||||
// See createKeepAliveImpl for details.
|
||||
asmType := llvm.FunctionType(b.ctx.VoidType(), []llvm.Type{b.dataPtrType}, false)
|
||||
asmFn := llvm.InlineAsm(asmType, "", "r", true, false, 0, false)
|
||||
b.createCall(asmType, asmFn, []llvm.Value{pointerValue}, "")
|
||||
|
||||
b.CreateRetVoid()
|
||||
}
|
||||
|
||||
var mathToLLVMMapping = map[string]string{
|
||||
"math.Ceil": "llvm.ceil.f64",
|
||||
"math.Exp": "llvm.exp.f64",
|
||||
|
||||
@@ -250,9 +250,6 @@ func (b *builder) createMapIteratorNext(rangeVal ssa.Value, llvmRangeVal, it llv
|
||||
func hashmapIsBinaryKey(keyType types.Type) bool {
|
||||
switch keyType := keyType.Underlying().(type) {
|
||||
case *types.Basic:
|
||||
// TODO: unsafe.Pointer is also a binary key, but to support that we
|
||||
// need to fix an issue with interp first (see
|
||||
// https://github.com/tinygo-org/tinygo/pull/4898).
|
||||
return keyType.Info()&(types.IsBoolean|types.IsInteger) != 0
|
||||
case *types.Pointer:
|
||||
return true
|
||||
|
||||
+9
-19
@@ -139,13 +139,7 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
// not.
|
||||
// (It may be safe to add the nocapture parameter to the context
|
||||
// parameter, but I'd like to stay on the safe side here).
|
||||
nocapture := c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)
|
||||
llvmFn.AddAttributeAtIndex(i+1, nocapture)
|
||||
}
|
||||
if paramInfo.flags¶mIsReadonly != 0 && paramInfo.llvmType.TypeKind() == llvm.PointerTypeKind {
|
||||
// Readonly pointer parameters (like strings) benefit from being marked as readonly.
|
||||
readonly := c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0)
|
||||
llvmFn.AddAttributeAtIndex(i+1, readonly)
|
||||
llvmFn.AddAttributeAtIndex(i+1, c.nocaptureAttr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,9 +152,7 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
// Mark it as noreturn so LLVM can optimize away code.
|
||||
llvmFn.AddFunctionAttr(c.ctx.CreateEnumAttribute(llvm.AttributeKindID("noreturn"), 0))
|
||||
case "internal/abi.NoEscape":
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
case "machine.keepAliveNoEscape", "machine.unsafeNoEscape":
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.nocaptureAttr)
|
||||
case "runtime.alloc":
|
||||
// Tell the optimizer that runtime.alloc is an allocator, meaning that it
|
||||
// returns values that are never null and never alias to an existing value.
|
||||
@@ -182,25 +174,25 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
case "runtime.sliceAppend":
|
||||
// Appending a slice will only read the to-be-appended slice, it won't
|
||||
// be modified.
|
||||
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(2, c.nocaptureAttr)
|
||||
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
|
||||
case "runtime.sliceCopy":
|
||||
// Copying a slice won't capture any of the parameters.
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("writeonly"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.nocaptureAttr)
|
||||
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
|
||||
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(2, c.nocaptureAttr)
|
||||
case "runtime.stringFromBytes":
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.nocaptureAttr)
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
|
||||
case "runtime.stringFromRunes":
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.nocaptureAttr)
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
|
||||
case "runtime.trackPointer":
|
||||
// This function is necessary for tracking pointers on the stack in a
|
||||
// portable way (see gc_stack_portable.go). Indicate to the optimizer
|
||||
// that the only thing we'll do is read the pointer.
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
|
||||
llvmFn.AddAttributeAtIndex(1, c.nocaptureAttr)
|
||||
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
|
||||
case "__mulsi3", "__divmodsi4", "__udivmodsi4":
|
||||
if strings.Split(c.Triple, "-")[0] == "avr" {
|
||||
@@ -235,11 +227,9 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
|
||||
llvmFn.AddFunctionAttr(c.ctx.CreateStringAttribute("wasm-import-name", info.wasmName))
|
||||
}
|
||||
nocaptureKind := llvm.AttributeKindID("nocapture")
|
||||
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
|
||||
for i, typ := range paramTypes {
|
||||
if typ.TypeKind() == llvm.PointerTypeKind {
|
||||
llvmFn.AddAttributeAtIndex(i+1, nocapture)
|
||||
llvmFn.AddAttributeAtIndex(i+1, c.nocaptureAttr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,14 +74,6 @@ func (b *builder) createRawSyscall(call *ssa.CallCommon) (llvm.Value, error) {
|
||||
return b.CreateCall(fnType, target, args, ""), nil
|
||||
|
||||
case b.GOARCH == "arm" && b.GOOS == "linux":
|
||||
if arch := b.archFamily(); arch != "arm" {
|
||||
// Some targets pretend to be linux/arm for compatibility but aren't
|
||||
// actually such a system. Make sure we emit an error instead of
|
||||
// creating inline assembly that will fail to compile.
|
||||
// See: https://github.com/tinygo-org/tinygo/issues/4959
|
||||
return llvm.Value{}, b.makeError(call.Pos(), "system calls are not supported: target emulates a linux/arm system on "+arch)
|
||||
}
|
||||
|
||||
// Implement the EABI system call convention for Linux.
|
||||
// Source: syscall(2) man page.
|
||||
args := []llvm.Value{}
|
||||
|
||||
-235
@@ -270,241 +270,6 @@ entry:
|
||||
ret void
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden void @main.deferInfiniteLoop(ptr %context) unnamed_addr #1 {
|
||||
entry:
|
||||
%deferPtr = alloca ptr, align 4
|
||||
store ptr null, ptr %deferPtr, align 4
|
||||
%deferframe.buf = alloca %runtime.deferFrame, align 4
|
||||
%0 = call ptr @llvm.stacksave.p0()
|
||||
call void @runtime.setupDeferFrame(ptr nonnull %deferframe.buf, ptr %0, ptr undef) #4
|
||||
br label %for.body
|
||||
|
||||
for.body: ; preds = %for.body, %entry
|
||||
%defer.next = load ptr, ptr %deferPtr, align 4
|
||||
%defer.alloc.call = call dereferenceable(12) ptr @runtime.alloc(i32 12, ptr null, ptr undef) #4
|
||||
store i32 0, ptr %defer.alloc.call, align 4
|
||||
%defer.alloc.call.repack1 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 4
|
||||
store ptr %defer.next, ptr %defer.alloc.call.repack1, align 4
|
||||
%defer.alloc.call.repack3 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 8
|
||||
store i32 8, ptr %defer.alloc.call.repack3, align 4
|
||||
store ptr %defer.alloc.call, ptr %deferPtr, align 4
|
||||
br label %for.body
|
||||
|
||||
recover: ; preds = %rundefers.end
|
||||
ret void
|
||||
|
||||
lpad: ; No predecessors!
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.loophead: ; preds = %rundefers.callback0, %lpad
|
||||
br i1 poison, label %rundefers.end, label %rundefers.loop
|
||||
|
||||
rundefers.loop: ; preds = %rundefers.loophead
|
||||
switch i32 poison, label %rundefers.default [
|
||||
i32 0, label %rundefers.callback0
|
||||
]
|
||||
|
||||
rundefers.callback0: ; preds = %rundefers.loop
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.default: ; preds = %rundefers.loop
|
||||
unreachable
|
||||
|
||||
rundefers.end: ; preds = %rundefers.loophead
|
||||
br label %recover
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden void @main.deferLoop(ptr %context) unnamed_addr #1 {
|
||||
entry:
|
||||
%deferPtr = alloca ptr, align 4
|
||||
store ptr null, ptr %deferPtr, align 4
|
||||
%deferframe.buf = alloca %runtime.deferFrame, align 4
|
||||
%0 = call ptr @llvm.stacksave.p0()
|
||||
call void @runtime.setupDeferFrame(ptr nonnull %deferframe.buf, ptr %0, ptr undef) #4
|
||||
br label %for.loop
|
||||
|
||||
for.loop: ; preds = %for.body, %entry
|
||||
%1 = phi i32 [ 0, %entry ], [ %3, %for.body ]
|
||||
%2 = icmp slt i32 %1, 10
|
||||
br i1 %2, label %for.body, label %for.done
|
||||
|
||||
for.body: ; preds = %for.loop
|
||||
%defer.next = load ptr, ptr %deferPtr, align 4
|
||||
%defer.alloc.call = call dereferenceable(12) ptr @runtime.alloc(i32 12, ptr null, ptr undef) #4
|
||||
store i32 0, ptr %defer.alloc.call, align 4
|
||||
%defer.alloc.call.repack13 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 4
|
||||
store ptr %defer.next, ptr %defer.alloc.call.repack13, align 4
|
||||
%defer.alloc.call.repack15 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 8
|
||||
store i32 %1, ptr %defer.alloc.call.repack15, align 4
|
||||
store ptr %defer.alloc.call, ptr %deferPtr, align 4
|
||||
%3 = add i32 %1, 1
|
||||
br label %for.loop
|
||||
|
||||
for.done: ; preds = %for.loop
|
||||
br label %rundefers.block
|
||||
|
||||
rundefers.after: ; preds = %rundefers.end
|
||||
call void @runtime.destroyDeferFrame(ptr nonnull %deferframe.buf, ptr undef) #4
|
||||
ret void
|
||||
|
||||
rundefers.block: ; preds = %for.done
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.loophead: ; preds = %rundefers.callback0, %rundefers.block
|
||||
%4 = load ptr, ptr %deferPtr, align 4
|
||||
%stackIsNil = icmp eq ptr %4, null
|
||||
br i1 %stackIsNil, label %rundefers.end, label %rundefers.loop
|
||||
|
||||
rundefers.loop: ; preds = %rundefers.loophead
|
||||
%stack.next.gep = getelementptr inbounds nuw i8, ptr %4, i32 4
|
||||
%stack.next = load ptr, ptr %stack.next.gep, align 4
|
||||
store ptr %stack.next, ptr %deferPtr, align 4
|
||||
%callback = load i32, ptr %4, align 4
|
||||
switch i32 %callback, label %rundefers.default [
|
||||
i32 0, label %rundefers.callback0
|
||||
]
|
||||
|
||||
rundefers.callback0: ; preds = %rundefers.loop
|
||||
%gep = getelementptr inbounds nuw i8, ptr %4, i32 8
|
||||
%param = load i32, ptr %gep, align 4
|
||||
call void @runtime.printlock(ptr undef) #4
|
||||
call void @runtime.printint32(i32 %param, ptr undef) #4
|
||||
call void @runtime.printunlock(ptr undef) #4
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.default: ; preds = %rundefers.loop
|
||||
unreachable
|
||||
|
||||
rundefers.end: ; preds = %rundefers.loophead
|
||||
br label %rundefers.after
|
||||
|
||||
recover: ; preds = %rundefers.end1
|
||||
ret void
|
||||
|
||||
lpad: ; No predecessors!
|
||||
br label %rundefers.loophead4
|
||||
|
||||
rundefers.loophead4: ; preds = %rundefers.callback010, %lpad
|
||||
br i1 poison, label %rundefers.end1, label %rundefers.loop3
|
||||
|
||||
rundefers.loop3: ; preds = %rundefers.loophead4
|
||||
switch i32 poison, label %rundefers.default2 [
|
||||
i32 0, label %rundefers.callback010
|
||||
]
|
||||
|
||||
rundefers.callback010: ; preds = %rundefers.loop3
|
||||
br label %rundefers.loophead4
|
||||
|
||||
rundefers.default2: ; preds = %rundefers.loop3
|
||||
unreachable
|
||||
|
||||
rundefers.end1: ; preds = %rundefers.loophead4
|
||||
br label %recover
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden void @main.deferBetweenLoops(ptr %context) unnamed_addr #1 {
|
||||
entry:
|
||||
%defer.alloca = alloca { i32, ptr, i32 }, align 4
|
||||
%deferPtr = alloca ptr, align 4
|
||||
store ptr null, ptr %deferPtr, align 4
|
||||
%deferframe.buf = alloca %runtime.deferFrame, align 4
|
||||
%0 = call ptr @llvm.stacksave.p0()
|
||||
call void @runtime.setupDeferFrame(ptr nonnull %deferframe.buf, ptr %0, ptr undef) #4
|
||||
br label %for.loop
|
||||
|
||||
for.loop: ; preds = %for.body, %entry
|
||||
%1 = phi i32 [ 0, %entry ], [ %3, %for.body ]
|
||||
%2 = icmp slt i32 %1, 10
|
||||
br i1 %2, label %for.body, label %for.done
|
||||
|
||||
for.body: ; preds = %for.loop
|
||||
%3 = add i32 %1, 1
|
||||
br label %for.loop
|
||||
|
||||
for.done: ; preds = %for.loop
|
||||
%defer.next = load ptr, ptr %deferPtr, align 4
|
||||
store i32 0, ptr %defer.alloca, align 4
|
||||
%defer.alloca.repack16 = getelementptr inbounds nuw i8, ptr %defer.alloca, i32 4
|
||||
store ptr %defer.next, ptr %defer.alloca.repack16, align 4
|
||||
%defer.alloca.repack18 = getelementptr inbounds nuw i8, ptr %defer.alloca, i32 8
|
||||
store i32 1, ptr %defer.alloca.repack18, align 4
|
||||
store ptr %defer.alloca, ptr %deferPtr, align 4
|
||||
br label %for.loop1
|
||||
|
||||
for.loop1: ; preds = %for.body2, %for.done
|
||||
%4 = phi i32 [ 0, %for.done ], [ %6, %for.body2 ]
|
||||
%5 = icmp slt i32 %4, 10
|
||||
br i1 %5, label %for.body2, label %for.done3
|
||||
|
||||
for.body2: ; preds = %for.loop1
|
||||
%6 = add i32 %4, 1
|
||||
br label %for.loop1
|
||||
|
||||
for.done3: ; preds = %for.loop1
|
||||
br label %rundefers.block
|
||||
|
||||
rundefers.after: ; preds = %rundefers.end
|
||||
call void @runtime.destroyDeferFrame(ptr nonnull %deferframe.buf, ptr undef) #4
|
||||
ret void
|
||||
|
||||
rundefers.block: ; preds = %for.done3
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.loophead: ; preds = %rundefers.callback0, %rundefers.block
|
||||
%7 = load ptr, ptr %deferPtr, align 4
|
||||
%stackIsNil = icmp eq ptr %7, null
|
||||
br i1 %stackIsNil, label %rundefers.end, label %rundefers.loop
|
||||
|
||||
rundefers.loop: ; preds = %rundefers.loophead
|
||||
%stack.next.gep = getelementptr inbounds nuw i8, ptr %7, i32 4
|
||||
%stack.next = load ptr, ptr %stack.next.gep, align 4
|
||||
store ptr %stack.next, ptr %deferPtr, align 4
|
||||
%callback = load i32, ptr %7, align 4
|
||||
switch i32 %callback, label %rundefers.default [
|
||||
i32 0, label %rundefers.callback0
|
||||
]
|
||||
|
||||
rundefers.callback0: ; preds = %rundefers.loop
|
||||
%gep = getelementptr inbounds nuw i8, ptr %7, i32 8
|
||||
%param = load i32, ptr %gep, align 4
|
||||
call void @runtime.printlock(ptr undef) #4
|
||||
call void @runtime.printint32(i32 %param, ptr undef) #4
|
||||
call void @runtime.printunlock(ptr undef) #4
|
||||
br label %rundefers.loophead
|
||||
|
||||
rundefers.default: ; preds = %rundefers.loop
|
||||
unreachable
|
||||
|
||||
rundefers.end: ; preds = %rundefers.loophead
|
||||
br label %rundefers.after
|
||||
|
||||
recover: ; preds = %rundefers.end4
|
||||
ret void
|
||||
|
||||
lpad: ; No predecessors!
|
||||
br label %rundefers.loophead7
|
||||
|
||||
rundefers.loophead7: ; preds = %rundefers.callback013, %lpad
|
||||
br i1 poison, label %rundefers.end4, label %rundefers.loop6
|
||||
|
||||
rundefers.loop6: ; preds = %rundefers.loophead7
|
||||
switch i32 poison, label %rundefers.default5 [
|
||||
i32 0, label %rundefers.callback013
|
||||
]
|
||||
|
||||
rundefers.callback013: ; preds = %rundefers.loop6
|
||||
br label %rundefers.loophead7
|
||||
|
||||
rundefers.default5: ; preds = %rundefers.loop6
|
||||
unreachable
|
||||
|
||||
rundefers.end4: ; preds = %rundefers.loophead7
|
||||
br label %recover
|
||||
}
|
||||
|
||||
attributes #0 = { allockind("alloc,zeroed") allocsize(0) "alloc-family"="runtime.alloc" "target-features"="+armv7-m,+hwdiv,+soft-float,+thumb-mode,-aes,-bf16,-cdecp0,-cdecp1,-cdecp2,-cdecp3,-cdecp4,-cdecp5,-cdecp6,-cdecp7,-crc,-crypto,-d32,-dotprod,-dsp,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-hwdiv-arm,-i8mm,-lob,-mve,-mve.fp,-neon,-pacbti,-ras,-sb,-sha2,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
|
||||
attributes #1 = { nounwind "target-features"="+armv7-m,+hwdiv,+soft-float,+thumb-mode,-aes,-bf16,-cdecp0,-cdecp1,-cdecp2,-cdecp3,-cdecp4,-cdecp5,-cdecp6,-cdecp7,-crc,-crypto,-d32,-dotprod,-dsp,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-hwdiv-arm,-i8mm,-lob,-mve,-mve.fp,-neon,-pacbti,-ras,-sb,-sha2,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
|
||||
attributes #2 = { "target-features"="+armv7-m,+hwdiv,+soft-float,+thumb-mode,-aes,-bf16,-cdecp0,-cdecp1,-cdecp2,-cdecp3,-cdecp4,-cdecp5,-cdecp6,-cdecp7,-crc,-crypto,-d32,-dotprod,-dsp,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-hwdiv-arm,-i8mm,-lob,-mve,-mve.fp,-neon,-pacbti,-ras,-sb,-sha2,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
|
||||
|
||||
Vendored
-20
@@ -18,23 +18,3 @@ func deferMultiple() {
|
||||
}()
|
||||
external()
|
||||
}
|
||||
|
||||
func deferInfiniteLoop() {
|
||||
for {
|
||||
defer print(8)
|
||||
}
|
||||
}
|
||||
|
||||
func deferLoop() {
|
||||
for i := 0; i < 10; i++ {
|
||||
defer print(i)
|
||||
}
|
||||
}
|
||||
|
||||
func deferBetweenLoops() {
|
||||
for i := 0; i < 10; i++ {
|
||||
}
|
||||
defer print(1)
|
||||
for i := 0; i < 10; i++ {
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -50,7 +50,7 @@ unsafe.String.throw: ; preds = %entry
|
||||
declare void @runtime.unsafeSlicePanic(ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden ptr @main.unsafeStringData(ptr readonly %s.data, i32 %s.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden ptr @main.unsafeStringData(ptr %s.data, i32 %s.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%stackalloc = alloca i8, align 1
|
||||
call void @runtime.trackPointer(ptr %s.data, ptr nonnull %stackalloc, ptr undef) #3
|
||||
|
||||
Vendored
+3
-3
@@ -77,7 +77,7 @@ entry:
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden %runtime._string @main.minString(ptr readonly %a.data, i32 %a.len, ptr readonly %b.data, i32 %b.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden %runtime._string @main.minString(ptr %a.data, i32 %a.len, ptr %b.data, i32 %b.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = insertvalue %runtime._string zeroinitializer, ptr %a.data, 0
|
||||
%1 = insertvalue %runtime._string %0, i32 %a.len, 1
|
||||
@@ -91,7 +91,7 @@ entry:
|
||||
ret %runtime._string %5
|
||||
}
|
||||
|
||||
declare i1 @runtime.stringLess(ptr readonly, i32, ptr readonly, i32, ptr) #1
|
||||
declare i1 @runtime.stringLess(ptr, i32, ptr, i32, ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i32 @main.maxInt(i32 %a, i32 %b, ptr %context) unnamed_addr #2 {
|
||||
@@ -116,7 +116,7 @@ entry:
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden %runtime._string @main.maxString(ptr readonly %a.data, i32 %a.len, ptr readonly %b.data, i32 %b.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden %runtime._string @main.maxString(ptr %a.data, i32 %a.len, ptr %b.data, i32 %b.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = insertvalue %runtime._string zeroinitializer, ptr %a.data, 0
|
||||
%1 = insertvalue %runtime._string %0, i32 %a.len, 1
|
||||
|
||||
Vendored
+8
-8
@@ -31,13 +31,13 @@ entry:
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i32 @main.stringLen(ptr readonly %s.data, i32 %s.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden i32 @main.stringLen(ptr %s.data, i32 %s.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
ret i32 %s.len
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i8 @main.stringIndex(ptr readonly %s.data, i32 %s.len, i32 %index, ptr %context) unnamed_addr #2 {
|
||||
define hidden i8 @main.stringIndex(ptr %s.data, i32 %s.len, i32 %index, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%.not = icmp ult i32 %index, %s.len
|
||||
br i1 %.not, label %lookup.next, label %lookup.throw
|
||||
@@ -55,16 +55,16 @@ lookup.throw: ; preds = %entry
|
||||
declare void @runtime.lookupPanic(ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i1 @main.stringCompareEqual(ptr readonly %s1.data, i32 %s1.len, ptr readonly %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden i1 @main.stringCompareEqual(ptr %s1.data, i32 %s1.len, ptr %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringEqual(ptr %s1.data, i32 %s1.len, ptr %s2.data, i32 %s2.len, ptr undef) #3
|
||||
ret i1 %0
|
||||
}
|
||||
|
||||
declare i1 @runtime.stringEqual(ptr readonly, i32, ptr readonly, i32, ptr) #1
|
||||
declare i1 @runtime.stringEqual(ptr, i32, ptr, i32, ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i1 @main.stringCompareUnequal(ptr readonly %s1.data, i32 %s1.len, ptr readonly %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden i1 @main.stringCompareUnequal(ptr %s1.data, i32 %s1.len, ptr %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringEqual(ptr %s1.data, i32 %s1.len, ptr %s2.data, i32 %s2.len, ptr undef) #3
|
||||
%1 = xor i1 %0, true
|
||||
@@ -72,16 +72,16 @@ entry:
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i1 @main.stringCompareLarger(ptr readonly %s1.data, i32 %s1.len, ptr readonly %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
define hidden i1 @main.stringCompareLarger(ptr %s1.data, i32 %s1.len, ptr %s2.data, i32 %s2.len, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = call i1 @runtime.stringLess(ptr %s2.data, i32 %s2.len, ptr %s1.data, i32 %s1.len, ptr undef) #3
|
||||
ret i1 %0
|
||||
}
|
||||
|
||||
declare i1 @runtime.stringLess(ptr readonly, i32, ptr readonly, i32, ptr) #1
|
||||
declare i1 @runtime.stringLess(ptr, i32, ptr, i32, ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define hidden i8 @main.stringLookup(ptr readonly %s.data, i32 %s.len, i8 %x, ptr %context) unnamed_addr #2 {
|
||||
define hidden i8 @main.stringLookup(ptr %s.data, i32 %s.len, i8 %x, ptr %context) unnamed_addr #2 {
|
||||
entry:
|
||||
%0 = zext i8 %x to i32
|
||||
%.not = icmp ugt i32 %s.len, %0
|
||||
|
||||
@@ -18,7 +18,7 @@ require (
|
||||
golang.org/x/sys v0.30.0
|
||||
golang.org/x/tools v0.30.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74
|
||||
tinygo.org/x/go-llvm v0.0.0-20250916101410-63740cfada08
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -58,5 +58,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74 h1:ovavgTdIBWCH8YWlcfq9gkpoyT1+IxMKSn+Df27QwE8=
|
||||
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=
|
||||
tinygo.org/x/go-llvm v0.0.0-20250916101410-63740cfada08 h1:vvEVYF4Qb38C25U8Ae/1QUWlCSp4pIE0PR+/BwNPvBU=
|
||||
tinygo.org/x/go-llvm v0.0.0-20250916101410-63740cfada08/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
// Version of TinyGo.
|
||||
// Update this value before release of new version of software.
|
||||
const version = "0.40.1"
|
||||
const version = "0.40.0-dev"
|
||||
|
||||
// Return TinyGo version, either in the form 0.30.0 or as a development version
|
||||
// (like 0.30.0-dev-abcd012).
|
||||
|
||||
@@ -577,7 +577,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||
// runtime instead of at compile time. But we need to
|
||||
// revert any changes made by the call first.
|
||||
if r.debug {
|
||||
fmt.Fprintln(os.Stderr, indent+"!! revert because of error:", callErr.Error())
|
||||
fmt.Fprintln(os.Stderr, indent+"!! revert because of error:", callErr.Err)
|
||||
}
|
||||
callMem.revert()
|
||||
err := r.runAtRuntime(fn, inst, locals, &mem, indent)
|
||||
|
||||
+1
-1
Submodule lib/bdwgc updated: 7577ca7c2d...1166f11f7d
@@ -1635,7 +1635,6 @@ func main() {
|
||||
cpuprofile := flag.String("cpuprofile", "", "cpuprofile output")
|
||||
monitor := flag.Bool("monitor", false, "enable serial monitor")
|
||||
baudrate := flag.Int("baudrate", 115200, "baudrate of serial monitor")
|
||||
gocompatibility := flag.Bool("go-compatibility", true, "enable to check for Go versions compatibility, you can also configure this by setting the TINYGO_GOCOMPATIBILITY environment variable")
|
||||
|
||||
// Internal flags, that are only intended for TinyGo development.
|
||||
printIR := flag.Bool("internal-printir", false, "print LLVM IR")
|
||||
@@ -1713,16 +1712,6 @@ func main() {
|
||||
ocdCommands = strings.Split(*ocdCommandsString, ",")
|
||||
}
|
||||
|
||||
val, ok := os.LookupEnv("TINYGO_GOCOMPATIBILITY")
|
||||
if ok {
|
||||
b, err := strconv.ParseBool(val)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "could not parse TINYGO_GOCOMPATIBILITY value %q: %v\n", val, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
*gocompatibility = b
|
||||
}
|
||||
|
||||
options := &compileopts.Options{
|
||||
GOOS: goenv.Get("GOOS"),
|
||||
GOARCH: goenv.Get("GOARCH"),
|
||||
@@ -1759,7 +1748,6 @@ func main() {
|
||||
Timeout: *timeout,
|
||||
WITPackage: witPackage,
|
||||
WITWorld: witWorld,
|
||||
GoCompatibility: *gocompatibility,
|
||||
}
|
||||
if *printCommands {
|
||||
options.PrintCommands = printCommand
|
||||
|
||||
@@ -48,10 +48,10 @@ func Asm(asm string)
|
||||
// recognizes template values in the form {name}, like so:
|
||||
//
|
||||
// arm.AsmFull(
|
||||
// "str {value}, [{result}]",
|
||||
// "str {value}, {result}",
|
||||
// map[string]interface{}{
|
||||
// "value": 1,
|
||||
// "result": uintptr(unsafe.Pointer(&dest)),
|
||||
// "value": 1
|
||||
// "result": &dest,
|
||||
// })
|
||||
//
|
||||
// You can use {} in the asm string (which expands to a register) to set the
|
||||
|
||||
@@ -1,6 +1,29 @@
|
||||
.syntax unified
|
||||
.cfi_sections .debug_frame
|
||||
|
||||
.section .text.HardFault_Handler
|
||||
.global HardFault_Handler
|
||||
.type HardFault_Handler, %function
|
||||
HardFault_Handler:
|
||||
.cfi_startproc
|
||||
// Put the old stack pointer in the first argument, for easy debugging. This
|
||||
// is especially useful on Cortex-M0, which supports far fewer debug
|
||||
// facilities.
|
||||
mov r0, sp
|
||||
|
||||
// Load the default stack pointer from address 0 so that we can call normal
|
||||
// functions again that expect a working stack. However, it will corrupt the
|
||||
// old stack so the function below must not attempt to recover from this
|
||||
// fault.
|
||||
movs r3, #0
|
||||
ldr r3, [r3]
|
||||
mov sp, r3
|
||||
|
||||
// Continue handling this error in Go.
|
||||
bl handleHardFault
|
||||
.cfi_endproc
|
||||
.size HardFault_Handler, .-HardFault_Handler
|
||||
|
||||
// This is a convenience function for semihosting support.
|
||||
// At some point, this should be replaced by inline assembly.
|
||||
.section .text.SemihostingCall
|
||||
|
||||
@@ -10,10 +10,10 @@ func Asm(asm string)
|
||||
// recognizes template values in the form {name}, like so:
|
||||
//
|
||||
// arm.AsmFull(
|
||||
// "str {value}, [{result}]",
|
||||
// "str {value}, {result}",
|
||||
// map[string]interface{}{
|
||||
// "value": 1,
|
||||
// "result": uintptr(unsafe.Pointer(&dest)),
|
||||
// "value": 1
|
||||
// "result": &dest,
|
||||
// })
|
||||
//
|
||||
// You can use {} in the asm string (which expands to a register) to set the
|
||||
|
||||
+3
-3
@@ -10,10 +10,10 @@ func Asm(asm string)
|
||||
// recognizes template values in the form {name}, like so:
|
||||
//
|
||||
// arm.AsmFull(
|
||||
// "str {value}, [{result}]",
|
||||
// "str {value}, {result}",
|
||||
// map[string]interface{}{
|
||||
// "value": 1,
|
||||
// "result": uintptr(unsafe.Pointer(&dest)),
|
||||
// "value": 1
|
||||
// "result": &dest,
|
||||
// })
|
||||
//
|
||||
// You can use {} in the asm string (which expands to a register) to set the
|
||||
|
||||
@@ -10,10 +10,10 @@ func Asm(asm string)
|
||||
// recognizes template values in the form {name}, like so:
|
||||
//
|
||||
// arm.AsmFull(
|
||||
// "st {value}, [{result}]",
|
||||
// "st {value}, {result}",
|
||||
// map[string]interface{}{
|
||||
// "value": 1,
|
||||
// "result": uintptr(unsafe.Pointer(&dest)),
|
||||
// "value": 1
|
||||
// "result": &dest,
|
||||
// })
|
||||
//
|
||||
// You can use {} in the asm string (which expands to a register) to set the
|
||||
|
||||
@@ -108,10 +108,6 @@ func (*stackState) unwind()
|
||||
func (t *Task) Resume() {
|
||||
// The current task must be saved and restored because this can nest on WASM with JS.
|
||||
prevTask := currentTask
|
||||
if prevTask == nil {
|
||||
// Save the system stack pointer.
|
||||
saveStackPointer()
|
||||
}
|
||||
t.gcData.swap()
|
||||
currentTask = t
|
||||
if !t.state.launched {
|
||||
@@ -127,9 +123,6 @@ func (t *Task) Resume() {
|
||||
}
|
||||
}
|
||||
|
||||
//go:linkname saveStackPointer runtime.saveStackPointer
|
||||
func saveStackPointer()
|
||||
|
||||
//export tinygo_rewind
|
||||
func (*state) rewind()
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build scheduler.tasks && (esp32 || esp32s3)
|
||||
//go:build scheduler.tasks && esp32
|
||||
|
||||
package task
|
||||
|
||||
|
||||
@@ -120,21 +120,15 @@ int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, u
|
||||
#endif
|
||||
pthread_attr_t attrs;
|
||||
pthread_attr_init(&attrs);
|
||||
pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
|
||||
pthread_attr_setstacksize(&attrs, stackSize);
|
||||
int result = pthread_create(thread, &attrs, &start_wrapper, &state);
|
||||
pthread_attr_destroy(&attrs);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Wait until the thread has been created and read all state_pass variables.
|
||||
#if __APPLE__
|
||||
dispatch_semaphore_wait(state.startlock, DISPATCH_TIME_FOREVER);
|
||||
dispatch_release(state.startlock);
|
||||
#else
|
||||
sem_wait(&state.startlock);
|
||||
sem_destroy(&state.startlock);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
|
||||
@@ -23,15 +23,16 @@ type state struct {
|
||||
// is needed to be able to scan the stack.
|
||||
stackTop uintptr
|
||||
|
||||
// Lowest address of the stack.
|
||||
// This is populated when the thread is stopped by the GC.
|
||||
stackBottom uintptr
|
||||
|
||||
// Next task in the activeTasks queue.
|
||||
QueueNext *Task
|
||||
|
||||
// Semaphore to pause/resume the thread atomically.
|
||||
pauseSem Semaphore
|
||||
|
||||
// Semaphore used for stack scanning.
|
||||
// We can't reuse pauseSem here since the thread might have been paused for
|
||||
// other reasons (for example, because it was waiting on a channel).
|
||||
gcSem Semaphore
|
||||
}
|
||||
|
||||
// Goroutine counter, starting at 0 for the main goroutine.
|
||||
@@ -95,9 +96,6 @@ func (t *Task) Resume() {
|
||||
t.state.pauseSem.Post()
|
||||
}
|
||||
|
||||
// otherGoroutines is the total number of live goroutines minus one.
|
||||
var otherGoroutines uint32
|
||||
|
||||
// Start a new OS thread.
|
||||
func start(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
|
||||
t := &Task{}
|
||||
@@ -117,7 +115,6 @@ func start(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
|
||||
}
|
||||
t.state.QueueNext = activeTasks
|
||||
activeTasks = t
|
||||
otherGoroutines++
|
||||
activeTaskLock.Unlock()
|
||||
}
|
||||
|
||||
@@ -138,7 +135,6 @@ func taskExited(t *Task) {
|
||||
break
|
||||
}
|
||||
}
|
||||
otherGoroutines--
|
||||
activeTaskLock.Unlock()
|
||||
|
||||
// Sanity check.
|
||||
@@ -147,42 +143,9 @@ func taskExited(t *Task) {
|
||||
}
|
||||
}
|
||||
|
||||
// scanWaitGroup is used to wait on until all threads have finished the current state transition.
|
||||
var scanWaitGroup waitGroup
|
||||
|
||||
type waitGroup struct {
|
||||
f Futex
|
||||
}
|
||||
|
||||
func initWaitGroup(n uint32) waitGroup {
|
||||
var wg waitGroup
|
||||
wg.f.Store(n)
|
||||
return wg
|
||||
}
|
||||
|
||||
func (wg *waitGroup) done() {
|
||||
if wg.f.Add(^uint32(0)) == 0 {
|
||||
wg.f.WakeAll()
|
||||
}
|
||||
}
|
||||
|
||||
func (wg *waitGroup) wait() {
|
||||
for {
|
||||
val := wg.f.Load()
|
||||
if val == 0 {
|
||||
return
|
||||
}
|
||||
wg.f.Wait(val)
|
||||
}
|
||||
}
|
||||
|
||||
// gcState is used to track and notify threads when the GC is stopping/resuming.
|
||||
var gcState Futex
|
||||
|
||||
const (
|
||||
gcStateResumed = iota
|
||||
gcStateStopped
|
||||
)
|
||||
// Futex to wait on until all tasks have finished scanning the stack.
|
||||
// This is basically a sync.WaitGroup.
|
||||
var scanDoneFutex Futex
|
||||
|
||||
// GC scan phase. Because we need to stop the world while scanning, this kinda
|
||||
// needs to be done in the tasks package.
|
||||
@@ -192,71 +155,65 @@ const (
|
||||
func GCStopWorldAndScan() {
|
||||
current := Current()
|
||||
|
||||
// NOTE: This does not need to be atomic.
|
||||
if gcState.Load() == gcStateResumed {
|
||||
// Don't allow new goroutines to be started while pausing/resuming threads
|
||||
// in the stop-the-world phase.
|
||||
activeTaskLock.Lock()
|
||||
// Don't allow new goroutines to be started while pausing/resuming threads
|
||||
// in the stop-the-world phase.
|
||||
activeTaskLock.Lock()
|
||||
|
||||
// Wait for threads to finish resuming.
|
||||
scanWaitGroup.wait()
|
||||
|
||||
// Change the gc state to stopped.
|
||||
// NOTE: This does not need to be atomic.
|
||||
gcState.Store(gcStateStopped)
|
||||
|
||||
// Set the number of threads to wait for.
|
||||
scanWaitGroup = initWaitGroup(otherGoroutines)
|
||||
|
||||
// Pause all other threads.
|
||||
for t := activeTasks; t != nil; t = t.state.QueueNext {
|
||||
if t != current {
|
||||
tinygo_task_send_gc_signal(t.state.thread)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the threads to finish stopping.
|
||||
scanWaitGroup.wait()
|
||||
}
|
||||
|
||||
// Scan other thread stacks.
|
||||
// Pause all other threads.
|
||||
numOtherThreads := uint32(0)
|
||||
for t := activeTasks; t != nil; t = t.state.QueueNext {
|
||||
if t != current {
|
||||
markRoots(t.state.stackBottom, t.state.stackTop)
|
||||
numOtherThreads++
|
||||
tinygo_task_send_gc_signal(t.state.thread)
|
||||
}
|
||||
}
|
||||
|
||||
// Store the number of threads to wait for in the futex.
|
||||
// This is the equivalent of doing an initial wg.Add(numOtherThreads).
|
||||
scanDoneFutex.Store(numOtherThreads)
|
||||
|
||||
// Scan the current stack, and all current registers.
|
||||
scanCurrentStack()
|
||||
|
||||
// Wake each paused thread for the first time so it will scan the stack.
|
||||
for t := activeTasks; t != nil; t = t.state.QueueNext {
|
||||
if t != current {
|
||||
t.state.gcSem.Post()
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until all threads have finished scanning their stack.
|
||||
// This is the equivalent of wg.Wait()
|
||||
for {
|
||||
val := scanDoneFutex.Load()
|
||||
if val == 0 {
|
||||
break
|
||||
}
|
||||
scanDoneFutex.Wait(val)
|
||||
}
|
||||
|
||||
// Scan all globals (implemented in the runtime).
|
||||
gcScanGlobals()
|
||||
}
|
||||
|
||||
// After the GC is done scanning, resume all other threads.
|
||||
//
|
||||
// This must only be called after a GCStopWorldAndScan call.
|
||||
func GCResumeWorld() {
|
||||
// NOTE: This does not need to be atomic.
|
||||
if gcState.Load() == gcStateResumed {
|
||||
// This is already resumed.
|
||||
return
|
||||
current := Current()
|
||||
|
||||
// Wake each paused thread for the second time, so they will resume normal
|
||||
// operation.
|
||||
for t := activeTasks; t != nil; t = t.state.QueueNext {
|
||||
if t != current {
|
||||
t.state.gcSem.Post()
|
||||
}
|
||||
}
|
||||
|
||||
// Set the wait group to track resume progress.
|
||||
scanWaitGroup = initWaitGroup(otherGoroutines)
|
||||
|
||||
// Set the state to resumed.
|
||||
gcState.Store(gcStateResumed)
|
||||
|
||||
// Wake all of the stopped threads.
|
||||
gcState.WakeAll()
|
||||
|
||||
// Allow goroutines to start and exit again.
|
||||
activeTaskLock.Unlock()
|
||||
}
|
||||
|
||||
//go:linkname markRoots runtime.markRoots
|
||||
func markRoots(start, end uintptr)
|
||||
|
||||
// Scan globals, implemented in the runtime package.
|
||||
func gcScanGlobals()
|
||||
|
||||
@@ -264,27 +221,34 @@ var stackScanLock PMutex
|
||||
|
||||
//export tinygo_task_gc_pause
|
||||
func tingyo_task_gc_pause(sig int32) {
|
||||
// Write the entrty stack pointer to the state.
|
||||
Current().state.stackBottom = uintptr(stacksave())
|
||||
// Wait until we get the signal to start scanning the stack.
|
||||
Current().state.gcSem.Wait()
|
||||
|
||||
// Notify the GC that we are stopped.
|
||||
scanWaitGroup.done()
|
||||
// Scan the thread stack.
|
||||
// Only scan a single thread stack at a time, because the GC marking phase
|
||||
// doesn't support parallelism.
|
||||
// TODO: it may be possible to call markRoots directly (without saving
|
||||
// registers) since we are in a signal handler that already saved a bunch of
|
||||
// registers. This is an optimization left for a future time.
|
||||
stackScanLock.Lock()
|
||||
scanCurrentStack()
|
||||
stackScanLock.Unlock()
|
||||
|
||||
// Wait for the GC to resume.
|
||||
for gcState.Load() == gcStateStopped {
|
||||
gcState.Wait(gcStateStopped)
|
||||
// Equivalent of wg.Done(): subtract one from the futex and if the result is
|
||||
// 0 (meaning we were the last in the waitgroup), wake the waiting thread.
|
||||
n := uint32(1)
|
||||
if scanDoneFutex.Add(-n) == 0 {
|
||||
scanDoneFutex.Wake()
|
||||
}
|
||||
|
||||
// Notify the GC that we have resumed.
|
||||
scanWaitGroup.done()
|
||||
// Wait until we get the signal we can resume normally (after the mark phase
|
||||
// has finished).
|
||||
Current().state.gcSem.Wait()
|
||||
}
|
||||
|
||||
//go:export tinygo_scanCurrentStack
|
||||
func scanCurrentStack()
|
||||
|
||||
//go:linkname stacksave runtime.stacksave
|
||||
func stacksave() unsafe.Pointer
|
||||
|
||||
// Return the highest address of the current stack.
|
||||
func StackTop() uintptr {
|
||||
return Current().state.stackTop
|
||||
|
||||
@@ -15,8 +15,8 @@ const (
|
||||
P09 Pin = 9
|
||||
P10 Pin = 10
|
||||
P11 Pin = 11
|
||||
P12 Pin = 12 // peripherals: I2C0 SDA
|
||||
P13 Pin = 13 // peripherals: I2C0 SCL
|
||||
P12 Pin = 12
|
||||
P13 Pin = 13
|
||||
P14 Pin = 14
|
||||
P15 Pin = 15
|
||||
P16 Pin = 16
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
//go:build pico2_ice
|
||||
|
||||
// Most of the info is from
|
||||
// https://pico2-ice.tinyvision.ai/md_pinout.html although
|
||||
// (2025-09-07) RP4 appears twice in that pinout - the schematic is
|
||||
// more clear. Consistent with other RPi boards, we use GPn instead of
|
||||
// RPn to reference the RPi connected pins.
|
||||
|
||||
package machine
|
||||
|
||||
// GPIO pins
|
||||
const (
|
||||
GP0 = GPIO0
|
||||
GP1 = GPIO1
|
||||
GP2 = GPIO2
|
||||
GP3 = GPIO3
|
||||
GP4 = GPIO4
|
||||
GP5 = GPIO5
|
||||
GP6 = GPIO6
|
||||
GP7 = GPIO7
|
||||
GP8 = GPIO8
|
||||
GP9 = GPIO9
|
||||
GP10 = GPIO10
|
||||
GP11 = GPIO11
|
||||
GP12 = GPIO12
|
||||
GP13 = GPIO13
|
||||
GP14 = GPIO14
|
||||
GP15 = GPIO15
|
||||
GP16 = GPIO16
|
||||
GP17 = GPIO17
|
||||
GP18 = GPIO18
|
||||
GP19 = GPIO19
|
||||
GP20 = GPIO20
|
||||
GP21 = GPIO21
|
||||
GP22 = GPIO22
|
||||
GP23 = GPIO23
|
||||
GP24 = GPIO24
|
||||
GP25 = GPIO25
|
||||
GP26 = GPIO26
|
||||
GP27 = GPIO27
|
||||
GP28 = GPIO28
|
||||
GP29 = GPIO29
|
||||
GP30 = GPIO30
|
||||
GP31 = GPIO31
|
||||
GP32 = GPIO32
|
||||
GP33 = GPIO33
|
||||
GP34 = GPIO34
|
||||
GP35 = GPIO35
|
||||
GP36 = GPIO36
|
||||
GP37 = GPIO37
|
||||
GP38 = GPIO38
|
||||
GP39 = GPIO39
|
||||
GP40 = GPIO40
|
||||
GP41 = GPIO41
|
||||
GP42 = GPIO42
|
||||
GP43 = GPIO43
|
||||
GP44 = GPIO44
|
||||
GP45 = GPIO45
|
||||
GP46 = GPIO46
|
||||
GP47 = GPIO47
|
||||
|
||||
// RPi pins shared with ICE. The ICE number is what appears on
|
||||
// the board silkscreen.
|
||||
ICE9 = GP28
|
||||
ICE11 = GP29
|
||||
ICE14 = GP7
|
||||
ICE15 = GP6
|
||||
ICE16 = GP5
|
||||
ICE17 = GP4
|
||||
ICE18 = GP27
|
||||
ICE19 = GP23
|
||||
ICE20 = GP22
|
||||
ICE21 = GP26
|
||||
ICE23 = GP25
|
||||
ICE25 = GP30
|
||||
ICE26 = GP24
|
||||
ICE27 = GP20
|
||||
|
||||
// FPGA Clock pin.
|
||||
ICE35_G0 = GP21
|
||||
|
||||
// Silkscreen & Pinout names
|
||||
ICE_SSN = ICE16
|
||||
ICE_SO = ICE14
|
||||
ICE_SI = ICE17
|
||||
ICE_CK = ICE15
|
||||
SD = GP2
|
||||
SC = GP3
|
||||
|
||||
FPGA_RSTN = GP31
|
||||
A3 = GP32
|
||||
A1 = GP33
|
||||
A4 = GP34
|
||||
A2 = GP35
|
||||
B3 = GP36
|
||||
B1 = GP37
|
||||
B4 = GP38
|
||||
B2 = GP39
|
||||
N0 = GP40 // On the board these are labeled "~0"
|
||||
N1 = GP41
|
||||
N2 = GP42
|
||||
N3 = GP43
|
||||
N4 = GP44
|
||||
N5 = GP45
|
||||
N6 = GP46
|
||||
|
||||
// Functions from Schematic.
|
||||
ICE_DONE = GP40
|
||||
USB_BOOT = GP42
|
||||
|
||||
// Button
|
||||
SW1 = GP42
|
||||
BOOTSEL = GP42
|
||||
|
||||
// Tricolor LEDs
|
||||
LED_RED = GP1
|
||||
LED_GREEN = GP0
|
||||
LED_BLUE = GP9
|
||||
|
||||
// Onboard LED
|
||||
LED = LED_GREEN
|
||||
|
||||
// Onboard crystal oscillator frequency, in MHz.
|
||||
xoscFreq = 12 // MHz
|
||||
)
|
||||
|
||||
// This board does not define default i2c pins.
|
||||
const (
|
||||
I2C0_SDA_PIN = NoPin
|
||||
I2C0_SCL_PIN = NoPin
|
||||
I2C1_SDA_PIN = NoPin
|
||||
I2C1_SCL_PIN = NoPin
|
||||
)
|
||||
|
||||
// SPI default pins
|
||||
const (
|
||||
// Default Serial Clock Bus 0 for SPI communications
|
||||
SPI0_SCK_PIN = GPIO18
|
||||
// Default Serial Out Bus 0 for SPI communications
|
||||
SPI0_SDO_PIN = GPIO19 // Tx
|
||||
// Default Serial In Bus 0 for SPI communications
|
||||
SPI0_SDI_PIN = GPIO16 // Rx
|
||||
|
||||
// Default Serial Clock Bus 1 for SPI communications
|
||||
SPI1_SCK_PIN = GPIO10
|
||||
// Default Serial Out Bus 1 for SPI communications
|
||||
SPI1_SDO_PIN = GPIO11 // Tx
|
||||
// Default Serial In Bus 1 for SPI communications
|
||||
SPI1_SDI_PIN = GPIO12 // Rx
|
||||
)
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
UART0_TX_PIN = GPIO0
|
||||
UART0_RX_PIN = GPIO1
|
||||
UART1_TX_PIN = GPIO8
|
||||
UART1_RX_PIN = GPIO9
|
||||
UART_TX_PIN = UART0_TX_PIN
|
||||
UART_RX_PIN = UART0_RX_PIN
|
||||
)
|
||||
|
||||
var DefaultUART = UART0
|
||||
|
||||
// USB identifiers
|
||||
const (
|
||||
usb_STRING_PRODUCT = "Pico2"
|
||||
usb_STRING_MANUFACTURER = "Raspberry Pi"
|
||||
)
|
||||
|
||||
var (
|
||||
usb_VID uint16 = 0x2E8A
|
||||
usb_PID uint16 = 0x000A
|
||||
)
|
||||
@@ -1,58 +0,0 @@
|
||||
//go:build xiao_esp32s3
|
||||
|
||||
// This file contains the pin mappings for the Seeed XIAO ESP32S3 boards.
|
||||
//
|
||||
// Seeed Studio XIAO ESP32S3 is an IoT mini development board based on
|
||||
// the Espressif ESP32-S3 WiFi/Bluetooth dual-mode chip.
|
||||
//
|
||||
// - https://www.seeedstudio.com/XIAO-ESP32S3-p-5627.html
|
||||
// - https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/
|
||||
|
||||
package machine
|
||||
|
||||
// Digital Pins
|
||||
const (
|
||||
D0 = GPIO1
|
||||
D1 = GPIO2
|
||||
D2 = GPIO3
|
||||
D3 = GPIO4
|
||||
D4 = GPIO5
|
||||
D5 = GPIO6
|
||||
D6 = GPIO43
|
||||
D7 = GPIO44
|
||||
D8 = GPIO7
|
||||
D9 = GPIO8
|
||||
D10 = GPIO9
|
||||
)
|
||||
|
||||
// Analog pins
|
||||
const (
|
||||
A0 = GPIO1
|
||||
A1 = GPIO2
|
||||
A2 = GPIO3
|
||||
A3 = GPIO4
|
||||
)
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
UART_RX_PIN = GPIO44
|
||||
UART_TX_PIN = GPIO43
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = GPIO5
|
||||
SCL_PIN = GPIO6
|
||||
)
|
||||
|
||||
// SPI pins
|
||||
const (
|
||||
SPI_SCK_PIN = GPIO7
|
||||
SPI_SDI_PIN = GPIO9
|
||||
SPI_SDO_PIN = GPIO8
|
||||
)
|
||||
|
||||
// Onboard LEDs
|
||||
const (
|
||||
LED = GPIO21
|
||||
)
|
||||
@@ -43,11 +43,6 @@ type BlockDevice interface {
|
||||
io.ReaderAt
|
||||
|
||||
// WriteAt writes the given number of bytes to the block device.
|
||||
//
|
||||
// This interface directly writes data to the underlying block device.
|
||||
// Different kinds of devices have different requirements: most can only
|
||||
// write data after the page has been erased, and many can only write data
|
||||
// with specific alignment (such as 4-byte alignment).
|
||||
io.WriterAt
|
||||
|
||||
// Size returns the number of bytes in this block device.
|
||||
|
||||
+1
-31
@@ -1,9 +1,6 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrTimeoutRNG = errors.New("machine: RNG Timeout")
|
||||
@@ -65,30 +62,3 @@ func (p Pin) Low() {
|
||||
type ADC struct {
|
||||
Pin Pin
|
||||
}
|
||||
|
||||
// Convert the pointer to a uintptr, to be used for memory I/O (DMA for
|
||||
// example). It also means the pointer is "gone" as far as the compiler is
|
||||
// concerned, and a GC cycle might deallocate the object. To prevent this from
|
||||
// happening, also call keepAliveNoEscape at a point after the address isn't
|
||||
// accessed anymore by the hardware.
|
||||
// The only exception is if the pointer is accessed later in a volatile way
|
||||
// (volatile read/write), which also forces the value to stay alive until that
|
||||
// point.
|
||||
//
|
||||
// This function is treated specially by the compiler to mark the 'ptr'
|
||||
// parameter as not escaping.
|
||||
//
|
||||
// TODO: this function should eventually be replaced with the proposed ptrtoaddr
|
||||
// instruction in LLVM. See:
|
||||
// https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/10
|
||||
// https://github.com/llvm/llvm-project/pull/139357
|
||||
func unsafeNoEscape(ptr unsafe.Pointer) uintptr {
|
||||
return uintptr(ptr)
|
||||
}
|
||||
|
||||
// Make sure the given pointer stays alive until this point. This is similar to
|
||||
// runtime.KeepAlive, with the difference that it won't let the pointer escape.
|
||||
// This is typically used together with unsafeNoEscape.
|
||||
//
|
||||
// This is a compiler intrinsic.
|
||||
func keepAliveNoEscape(ptr unsafe.Pointer)
|
||||
|
||||
@@ -519,10 +519,6 @@ type Serialer interface {
|
||||
RTS() bool
|
||||
}
|
||||
|
||||
func initUSB() {
|
||||
// nothing to do here
|
||||
}
|
||||
|
||||
// USB Serial/JTAG Controller
|
||||
// See esp32-c3_technical_reference_manual_en.pdf
|
||||
// pg. 736
|
||||
|
||||
@@ -1,312 +0,0 @@
|
||||
//go:build esp32s3
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/esp"
|
||||
"errors"
|
||||
"runtime/volatile"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const deviceName = esp.Device
|
||||
|
||||
const xtalClock = 40_000000 // 40MHz
|
||||
const apbClock = 80_000000 // 80MHz
|
||||
const cryptoPWMClock = 160_000000 // 160MHz
|
||||
|
||||
// GetCPUFrequency returns the current CPU frequency of the chip.
|
||||
func GetCPUFrequency() (uint32, error) {
|
||||
switch esp.SYSTEM.GetSYSCLK_CONF_SOC_CLK_SEL() {
|
||||
case 0:
|
||||
return xtalClock / (esp.SYSTEM.GetSYSCLK_CONF_PRE_DIV_CNT() + 1), nil
|
||||
case 1:
|
||||
switch esp.SYSTEM.GetCPU_PER_CONF_CPUPERIOD_SEL() {
|
||||
case 0:
|
||||
return 80e6, nil
|
||||
case 1:
|
||||
return 160e6, nil
|
||||
case 2:
|
||||
// If esp.SYSTEM.GetCPU_PER_CONF_PLL_FREQ_SEL() == 1, this is undefined
|
||||
return 240e6, nil
|
||||
}
|
||||
case 2:
|
||||
//RC Fast Clock
|
||||
return (175e5) / (esp.SYSTEM.GetSYSCLK_CONF_PRE_DIV_CNT() + 1), nil
|
||||
}
|
||||
return 0, errors.New("machine: Unable to determine current cpu frequency")
|
||||
}
|
||||
|
||||
// SetCPUFrequency sets the frequency of the CPU to one of several targets
|
||||
func SetCPUFrequency(frequency uint32) error {
|
||||
// Always assume we are on PLL. Lower frequencies can be set with a different
|
||||
// clock source, but this will change the behavior of APB clock and Crypto PWM
|
||||
// clock
|
||||
//esp.SYSTEM.SetSYSCLK_CONF_SOC_CLK_SEL(1)
|
||||
|
||||
switch frequency {
|
||||
case 80_000000:
|
||||
esp.SYSTEM.SetCPU_PER_CONF_CPUPERIOD_SEL(0)
|
||||
esp.SYSTEM.SetCPU_PER_CONF_PLL_FREQ_SEL(0) // Reduce PLL freq when possible
|
||||
return nil
|
||||
case 160_000000:
|
||||
esp.SYSTEM.SetCPU_PER_CONF_CPUPERIOD_SEL(1)
|
||||
esp.SYSTEM.SetCPU_PER_CONF_PLL_FREQ_SEL(0)
|
||||
return nil
|
||||
case 240_000000:
|
||||
esp.SYSTEM.SetCPU_PER_CONF_PLL_FREQ_SEL(1) // Increase PLL freq when needed
|
||||
esp.SYSTEM.SetCPU_PER_CONF_CPUPERIOD_SEL(2)
|
||||
return nil
|
||||
}
|
||||
return errors.New("machine: Unsupported CPU frequency selected. Supported: 80, 160, 240 MHz")
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidSPIBus = errors.New("machine: invalid SPI bus")
|
||||
)
|
||||
|
||||
const (
|
||||
PinOutput PinMode = iota
|
||||
PinInput
|
||||
PinInputPullup
|
||||
PinInputPulldown
|
||||
)
|
||||
|
||||
// Hardware pin numbers
|
||||
const (
|
||||
GPIO0 Pin = 0
|
||||
GPIO1 Pin = 1
|
||||
GPIO2 Pin = 2
|
||||
GPIO3 Pin = 3
|
||||
GPIO4 Pin = 4
|
||||
GPIO5 Pin = 5
|
||||
GPIO6 Pin = 6
|
||||
GPIO7 Pin = 7
|
||||
GPIO8 Pin = 8
|
||||
GPIO9 Pin = 9
|
||||
GPIO10 Pin = 10
|
||||
GPIO11 Pin = 11
|
||||
GPIO12 Pin = 12
|
||||
GPIO13 Pin = 13
|
||||
GPIO14 Pin = 14
|
||||
GPIO15 Pin = 15
|
||||
GPIO16 Pin = 16
|
||||
GPIO17 Pin = 17
|
||||
GPIO18 Pin = 18
|
||||
GPIO19 Pin = 19
|
||||
GPIO20 Pin = 20
|
||||
GPIO21 Pin = 21
|
||||
GPIO26 Pin = 26
|
||||
GPIO27 Pin = 27
|
||||
GPIO28 Pin = 28
|
||||
GPIO29 Pin = 29
|
||||
GPIO30 Pin = 30
|
||||
GPIO31 Pin = 31
|
||||
GPIO32 Pin = 32
|
||||
GPIO33 Pin = 33
|
||||
GPIO34 Pin = 34
|
||||
GPIO35 Pin = 35
|
||||
GPIO36 Pin = 36
|
||||
GPIO37 Pin = 37
|
||||
GPIO38 Pin = 38
|
||||
GPIO39 Pin = 39
|
||||
GPIO40 Pin = 40
|
||||
GPIO41 Pin = 41
|
||||
GPIO42 Pin = 42
|
||||
GPIO43 Pin = 43
|
||||
GPIO44 Pin = 44
|
||||
GPIO45 Pin = 45
|
||||
GPIO46 Pin = 46
|
||||
GPIO47 Pin = 47
|
||||
GPIO48 Pin = 48
|
||||
)
|
||||
|
||||
// Configure this pin with the given configuration.
|
||||
func (p Pin) Configure(config PinConfig) {
|
||||
// Output function 256 is a special value reserved for use as a regular GPIO
|
||||
// pin. Peripherals (SPI etc) can set a custom output function by calling
|
||||
// lowercase configure() instead with a signal name.
|
||||
p.configure(config, 256)
|
||||
}
|
||||
|
||||
// configure is the same as Configure, but allows for setting a specific input
|
||||
// or output signal.
|
||||
// Signals are always routed through the GPIO matrix for simplicity. Output
|
||||
// signals are configured in FUNCx_OUT_SEL_CFG which selects a particular signal
|
||||
// to output on a given pin. Input signals are configured in FUNCy_IN_SEL_CFG,
|
||||
// which sets the pin to use for a particular input signal.
|
||||
func (p Pin) configure(config PinConfig, signal uint32) {
|
||||
if p == NoPin {
|
||||
// This simplifies pin configuration in peripherals such as SPI.
|
||||
return
|
||||
}
|
||||
|
||||
ioConfig := uint32(0)
|
||||
|
||||
// MCU_SEL: Function 1 is always GPIO
|
||||
ioConfig |= (1 << esp.IO_MUX_GPIO_MCU_SEL_Pos)
|
||||
|
||||
// FUN_IE: Make this pin an input pin (always set for GPIO operation)
|
||||
ioConfig |= esp.IO_MUX_GPIO_FUN_IE
|
||||
|
||||
// DRV: Set drive strength to 20 mA as a default. Pins 17 and 18 are special
|
||||
var drive uint32
|
||||
if p == GPIO17 || p == GPIO18 {
|
||||
drive = 1 // 20 mA
|
||||
} else {
|
||||
drive = 2 // 20 mA
|
||||
}
|
||||
ioConfig |= (drive << esp.IO_MUX_GPIO_FUN_DRV_Pos)
|
||||
|
||||
// WPU/WPD: Select pull mode.
|
||||
if config.Mode == PinInputPullup {
|
||||
ioConfig |= esp.IO_MUX_GPIO_FUN_WPU
|
||||
} else if config.Mode == PinInputPulldown {
|
||||
ioConfig |= esp.IO_MUX_GPIO_FUN_WPD
|
||||
}
|
||||
|
||||
// Set configuration
|
||||
ioRegister := p.ioMuxReg()
|
||||
ioRegister.Set(ioConfig)
|
||||
|
||||
switch config.Mode {
|
||||
case PinOutput:
|
||||
// Set the 'output enable' bit.
|
||||
if p < 32 {
|
||||
esp.GPIO.ENABLE_W1TS.Set(1 << p)
|
||||
} else {
|
||||
esp.GPIO.ENABLE1_W1TS.Set(1 << (p - 32))
|
||||
}
|
||||
// Set the signal to read the output value from. It can be a peripheral
|
||||
// output signal, or the special value 256 which indicates regular GPIO
|
||||
// usage.
|
||||
p.outFunc().Set(signal)
|
||||
case PinInput, PinInputPullup, PinInputPulldown:
|
||||
// Clear the 'output enable' bit.
|
||||
if p < 32 {
|
||||
esp.GPIO.ENABLE_W1TC.Set(1 << p)
|
||||
} else {
|
||||
esp.GPIO.ENABLE1_W1TC.Set(1 << (p - 32))
|
||||
}
|
||||
if signal != 256 {
|
||||
// Signal is a peripheral function (not a simple GPIO). Connect this
|
||||
// signal to the pin.
|
||||
// Note that outFunc and inFunc work in the opposite direction.
|
||||
// outFunc configures a pin to use a given output signal, while
|
||||
// inFunc specifies a pin to use to read the signal from.
|
||||
inFunc(signal).Set(esp.GPIO_FUNC_IN_SEL_CFG_SEL | uint32(p)<<esp.GPIO_FUNC_IN_SEL_CFG_IN_SEL_Pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ioMuxReg returns the IO_MUX_n_REG register used for configuring the io mux for
|
||||
// this pin
|
||||
func (p Pin) ioMuxReg() *volatile.Register32 {
|
||||
return (*volatile.Register32)(unsafe.Add(unsafe.Pointer(&esp.IO_MUX.GPIO0), uintptr(p)*4))
|
||||
}
|
||||
|
||||
// outFunc returns the FUNCx_OUT_SEL_CFG register used for configuring the
|
||||
// output function selection.
|
||||
func (p Pin) outFunc() *volatile.Register32 {
|
||||
return (*volatile.Register32)(unsafe.Add(unsafe.Pointer(&esp.GPIO.FUNC0_OUT_SEL_CFG), uintptr(p)*4))
|
||||
}
|
||||
|
||||
// inFunc returns the FUNCy_IN_SEL_CFG register used for configuring the input
|
||||
// function selection.
|
||||
func inFunc(signal uint32) *volatile.Register32 {
|
||||
return (*volatile.Register32)(unsafe.Add(unsafe.Pointer(&esp.GPIO.FUNC0_IN_SEL_CFG), uintptr(signal)*4))
|
||||
}
|
||||
|
||||
// Set the pin to high or low.
|
||||
// Warning: only use this on an output pin!
|
||||
func (p Pin) Set(value bool) {
|
||||
if value {
|
||||
reg, mask := p.portMaskSet()
|
||||
reg.Set(mask)
|
||||
} else {
|
||||
reg, mask := p.portMaskClear()
|
||||
reg.Set(mask)
|
||||
}
|
||||
}
|
||||
|
||||
// Return the register and mask to enable a given GPIO pin. This can be used to
|
||||
// implement bit-banged drivers.
|
||||
//
|
||||
// Warning: only use this on an output pin!
|
||||
func (p Pin) PortMaskSet() (*uint32, uint32) {
|
||||
reg, mask := p.portMaskSet()
|
||||
return ®.Reg, mask
|
||||
}
|
||||
|
||||
// Return the register and mask to disable a given GPIO pin. This can be used to
|
||||
// implement bit-banged drivers.
|
||||
//
|
||||
// Warning: only use this on an output pin!
|
||||
func (p Pin) PortMaskClear() (*uint32, uint32) {
|
||||
reg, mask := p.portMaskClear()
|
||||
return ®.Reg, mask
|
||||
}
|
||||
|
||||
func (p Pin) portMaskSet() (*volatile.Register32, uint32) {
|
||||
if p < 32 {
|
||||
return &esp.GPIO.OUT_W1TS, 1 << p
|
||||
} else {
|
||||
return &esp.GPIO.OUT1_W1TS, 1 << (p - 32)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Pin) portMaskClear() (*volatile.Register32, uint32) {
|
||||
if p < 32 {
|
||||
return &esp.GPIO.OUT_W1TC, 1 << p
|
||||
} else {
|
||||
return &esp.GPIO.OUT1_W1TC, 1 << (p - 32)
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the current value of a GPIO pin when the pin is configured as an
|
||||
// input or as an output.
|
||||
func (p Pin) Get() bool {
|
||||
if p < 32 {
|
||||
return esp.GPIO.IN.Get()&(1<<p) != 0
|
||||
} else {
|
||||
return esp.GPIO.IN1.Get()&(1<<(p-32)) != 0
|
||||
}
|
||||
}
|
||||
|
||||
var DefaultUART = UART0
|
||||
|
||||
var (
|
||||
UART0 = &_UART0
|
||||
_UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
|
||||
UART1 = &_UART1
|
||||
_UART1 = UART{Bus: esp.UART1, Buffer: NewRingBuffer()}
|
||||
UART2 = &_UART2
|
||||
_UART2 = UART{Bus: esp.UART2, Buffer: NewRingBuffer()}
|
||||
)
|
||||
|
||||
type UART struct {
|
||||
Bus *esp.UART_Type
|
||||
Buffer *RingBuffer
|
||||
}
|
||||
|
||||
func (uart *UART) Configure(config UARTConfig) {
|
||||
if config.BaudRate == 0 {
|
||||
config.BaudRate = 115200
|
||||
}
|
||||
// Crystal clock source is selected by default
|
||||
uart.Bus.CLKDIV.Set(xtalClock / config.BaudRate)
|
||||
}
|
||||
|
||||
func (uart *UART) writeByte(b byte) error {
|
||||
for (uart.Bus.STATUS.Get()>>16)&0xff >= 128 {
|
||||
// Read UART_TXFIFO_CNT from the status register, which indicates how
|
||||
// many bytes there are in the transmit buffer. Wait until there are
|
||||
// less than 128 bytes in this buffer (the default buffer size).
|
||||
}
|
||||
uart.Bus.FIFO.Set(uint32(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uart *UART) flush() {}
|
||||
|
||||
// TODO: SPI
|
||||
@@ -3,9 +3,7 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/arm"
|
||||
"device/nrf"
|
||||
"errors"
|
||||
"internal/binary"
|
||||
"runtime/interrupt"
|
||||
"unsafe"
|
||||
@@ -382,10 +380,6 @@ func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
// If the length of p is not long enough it will be padded with 0xFF bytes.
|
||||
// This method assumes that the destination is already erased.
|
||||
func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
if len(p) == 0 {
|
||||
return 0, nil // nothing to do (and it would fail in sd_flash_write)
|
||||
}
|
||||
|
||||
if FlashDataStart()+uintptr(off)+uintptr(len(p)) > FlashDataEnd() {
|
||||
return 0, errFlashCannotWritePastEOF
|
||||
}
|
||||
@@ -393,35 +387,6 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
address := FlashDataStart() + uintptr(off)
|
||||
padded := flashPad(p, int(f.WriteBlockSize()))
|
||||
|
||||
// When the SoftDevice is enabled, access to the flash is restricted and
|
||||
// must go through the SoftDevice API.
|
||||
if isSoftDeviceEnabled() {
|
||||
// Call sd_flash_write, which is SVC_SOC_BASE + 9 in all the
|
||||
// SoftDevices I've checked.
|
||||
// Documentation:
|
||||
// https://docs.nordicsemi.com/bundle/s140_v6.0.0_api/page/group_n_r_f_s_o_c_f_u_n_c_t_i_o_n_s.html
|
||||
numberOfWords := len(padded) / 4 // flash access goes in 32-bit words
|
||||
result := arm.SVCall3(0x20+9, address, &padded[0], uint32(numberOfWords))
|
||||
if result != 0 {
|
||||
// Could not queue flash operation? Not sure when this can
|
||||
// happen.
|
||||
return 0, flashError
|
||||
}
|
||||
|
||||
// Wait until the SoftDevice is finished.
|
||||
flashStatus = flashStatusBusy
|
||||
for flashStatus == flashStatusBusy {
|
||||
handleSoftDeviceEvents()
|
||||
}
|
||||
|
||||
// Check whether the operation was successful.
|
||||
if flashStatus != flashStatusOk {
|
||||
flashStatus = flashStatusOk
|
||||
return 0, flashError
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
waitWhileFlashBusy()
|
||||
|
||||
nrf.NVMC.SetCONFIG_WEN(nrf.NVMC_CONFIG_WEN_Wen)
|
||||
@@ -463,40 +428,6 @@ func (f flashBlockDevice) EraseBlockSize() int64 {
|
||||
// supports this. The start and len parameters are in block numbers, use
|
||||
// EraseBlockSize to map addresses to blocks.
|
||||
func (f flashBlockDevice) EraseBlocks(start, len int64) error {
|
||||
// When the SoftDevice is enabled, access to the flash is restricted and
|
||||
// must go through the SoftDevice API.
|
||||
if isSoftDeviceEnabled() {
|
||||
for i := range uint32(len) {
|
||||
flashPage := uint32(FlashDataStart())/eraseBlockSizeValue + uint32(start) + i
|
||||
|
||||
// Call sd_flash_page_erase, which is SVC_SOC_BASE + 8 in all the
|
||||
// SoftDevices I've checked.
|
||||
// Documentation:
|
||||
// https://docs.nordicsemi.com/bundle/s140_v6.0.0_api/page/group_n_r_f_s_o_c_f_u_n_c_t_i_o_n_s.html#ga9c93dd94a138ad8b5ed3693ea38ffb3e
|
||||
result := arm.SVCall1(0x20+8, flashPage)
|
||||
if result != 0 {
|
||||
// Could not queue flash operation? Not sure when this can
|
||||
// happen.
|
||||
return flashError
|
||||
}
|
||||
|
||||
// Wait until the SoftDevice is finished.
|
||||
flashStatus = flashStatusBusy
|
||||
for flashStatus == flashStatusBusy {
|
||||
handleSoftDeviceEvents()
|
||||
}
|
||||
|
||||
// Check whether the operation was successful.
|
||||
if flashStatus != flashStatusOk {
|
||||
flashStatus = flashStatusOk
|
||||
return flashError
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SoftDevice is not used or enabled. Use NVIC directly.
|
||||
|
||||
address := FlashDataStart() + uintptr(start*f.EraseBlockSize())
|
||||
waitWhileFlashBusy()
|
||||
|
||||
@@ -516,52 +447,3 @@ func waitWhileFlashBusy() {
|
||||
for nrf.NVMC.GetREADY() != nrf.NVMC_READY_READY_Ready {
|
||||
}
|
||||
}
|
||||
|
||||
var flashError = errors.New("machine: flash operation failed")
|
||||
|
||||
const (
|
||||
flashStatusOk = iota
|
||||
flashStatusError
|
||||
flashStatusBusy
|
||||
)
|
||||
|
||||
var flashStatus uint8 = flashStatusOk
|
||||
|
||||
var sdEvent uint32
|
||||
|
||||
// Process all queued SoftDevice events. May only be called when the SoftDevice is enabled.
|
||||
//
|
||||
// Normally these are handled in the same interrupt where Bluetooth events are
|
||||
// handled. But in TinyGo, that's complicated. One option would be to put it in
|
||||
// the tinygo.org/x/bluetooth package, but that would cause a circular
|
||||
// dependency between the machine and the bluetooth package. Another would be to
|
||||
// put it here, but let the bluetooth package call handleSoftDeviceEvents, but
|
||||
// that relies on updating the bluetooth package at the same time. As a
|
||||
// compromise, these events are handled directly where they are expected (here
|
||||
// in the machine package). This works in practice since there are only very few
|
||||
// of such events (at the moment, only flash-related ones which is in the
|
||||
// machine package anyway).
|
||||
func handleSoftDeviceEvents() {
|
||||
for {
|
||||
var result uintptr
|
||||
if nrf.Device == "nrf52" || nrf.Device == "nrf52840" || nrf.Device == "nrf52833" {
|
||||
// sd_evt_get: SOC_SVC_BASE_NOT_AVAILABLE + 31
|
||||
result = arm.SVCall1(0x2C+31, &sdEvent)
|
||||
} else {
|
||||
return // TODO: nrf51 etc
|
||||
}
|
||||
if result != 0 {
|
||||
// Some error occured. The only possible error is
|
||||
// NRF_ERROR_NOT_FOUND, which means there are no more events.
|
||||
return
|
||||
}
|
||||
|
||||
// The following events are the same numbers in all SoftDevices I've checked.
|
||||
switch sdEvent {
|
||||
case 2: // NRF_EVT_FLASH_OPERATION_SUCCESS
|
||||
flashStatus = flashStatusOk
|
||||
case 3: // NRF_EVT_FLASH_OPERATION_ERROR
|
||||
flashStatus = flashStatusError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,5 +69,3 @@ const eraseBlockSizeValue = 4096
|
||||
func eraseBlockSize() int64 {
|
||||
return eraseBlockSizeValue
|
||||
}
|
||||
|
||||
const spiMaxBufferSize = 255 // from the datasheet: TXD.MAXCNT and RXD.MAXCNT
|
||||
|
||||
@@ -90,5 +90,3 @@ const eraseBlockSizeValue = 4096
|
||||
func eraseBlockSize() int64 {
|
||||
return eraseBlockSizeValue
|
||||
}
|
||||
|
||||
const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT
|
||||
|
||||
@@ -108,9 +108,3 @@ const eraseBlockSizeValue = 4096
|
||||
func eraseBlockSize() int64 {
|
||||
return eraseBlockSizeValue
|
||||
}
|
||||
|
||||
const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT
|
||||
|
||||
// ADC instance for the VDDH input pin. This pin is typically connected to USB
|
||||
// input voltage (~5V) or directly to a battery.
|
||||
var ADC_VDDH = ADC{adcVDDHPin}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
|
||||
|
||||
// Configure for a single shot to perform both write and read (as applicable)
|
||||
if len(w) != 0 {
|
||||
i2c.Bus.TXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(w)))))
|
||||
i2c.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0]))))
|
||||
i2c.Bus.TXD.MAXCNT.Set(uint32(len(w)))
|
||||
|
||||
// If no read, immediately signal stop after TX
|
||||
@@ -58,7 +58,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
|
||||
}
|
||||
}
|
||||
if len(r) != 0 {
|
||||
i2c.Bus.RXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(r)))))
|
||||
i2c.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0]))))
|
||||
i2c.Bus.RXD.MAXCNT.Set(uint32(len(r)))
|
||||
|
||||
// Auto-start Rx after Tx and Stop after Rx
|
||||
@@ -89,11 +89,6 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the w and r buffers stay alive until this point, so they won't
|
||||
// be garbage collected while the buffers are used by the hardware.
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(w)))
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(r)))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -122,7 +117,7 @@ func (i2c *I2C) Listen(addr uint8) error {
|
||||
//
|
||||
// For request events, the caller MUST call `Reply` to avoid hanging the i2c bus indefinitely.
|
||||
func (i2c *I2C) WaitForEvent(buf []byte) (evt I2CTargetEvent, count int, err error) {
|
||||
i2c.BusT.RXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(buf)))))
|
||||
i2c.BusT.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&buf[0]))))
|
||||
i2c.BusT.RXD.MAXCNT.Set(uint32(len(buf)))
|
||||
|
||||
i2c.BusT.TASKS_PREPARERX.Set(nrf.TWIS_TASKS_PREPARERX_TASKS_PREPARERX_Trigger)
|
||||
@@ -139,10 +134,6 @@ func (i2c *I2C) WaitForEvent(buf []byte) (evt I2CTargetEvent, count int, err err
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure buf stays alive until this point, so it won't be garbage
|
||||
// collected while it is used by the hardware.
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(buf)))
|
||||
|
||||
count = 0
|
||||
evt = I2CFinish
|
||||
err = nil
|
||||
@@ -172,7 +163,7 @@ func (i2c *I2C) WaitForEvent(buf []byte) (evt I2CTargetEvent, count int, err err
|
||||
|
||||
// Reply supplies the response data the controller.
|
||||
func (i2c *I2C) Reply(buf []byte) error {
|
||||
i2c.BusT.TXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(buf)))))
|
||||
i2c.BusT.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&buf[0]))))
|
||||
i2c.BusT.TXD.MAXCNT.Set(uint32(len(buf)))
|
||||
|
||||
i2c.BusT.EVENTS_STOPPED.Set(0)
|
||||
@@ -189,10 +180,6 @@ func (i2c *I2C) Reply(buf []byte) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the buffer stays alive until this point, so it won't be garbage
|
||||
// collected while it is used by the hardware.
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(buf)))
|
||||
|
||||
i2c.BusT.EVENTS_STOPPED.Set(0)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -12,8 +12,6 @@ func CPUFrequency() uint32 {
|
||||
return 64000000
|
||||
}
|
||||
|
||||
var adcVDDHPin = Pin(254) // special pin number for VDDH on the nrf52840
|
||||
|
||||
// InitADC initializes the registers needed for ADC.
|
||||
func InitADC() {
|
||||
// Enable ADC.
|
||||
@@ -28,7 +26,7 @@ func InitADC() {
|
||||
// Samples can be 1(default), 2, 4, 8, 16, 32, 64, 128, 256 samples
|
||||
func (a *ADC) Configure(config ADCConfig) {
|
||||
var configVal uint32 = nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESP_Pos |
|
||||
nrf.SAADC_CH_CONFIG_RESN_Bypass<<nrf.SAADC_CH_CONFIG_RESN_Pos |
|
||||
nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESN_Pos |
|
||||
nrf.SAADC_CH_CONFIG_REFSEL_Internal<<nrf.SAADC_CH_CONFIG_REFSEL_Pos |
|
||||
nrf.SAADC_CH_CONFIG_MODE_SE<<nrf.SAADC_CH_CONFIG_MODE_Pos
|
||||
|
||||
@@ -118,43 +116,36 @@ func (a *ADC) Configure(config ADCConfig) {
|
||||
|
||||
// Get returns the current value of an ADC pin in the range 0..0xffff.
|
||||
func (a *ADC) Get() uint16 {
|
||||
var adcPin uint32
|
||||
var pwmPin uint32
|
||||
var rawValue volatile.Register16
|
||||
|
||||
switch a.Pin {
|
||||
case 2:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput0
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput0
|
||||
case 3:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput1
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput1
|
||||
case 4:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput2
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput2
|
||||
case 5:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput3
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput3
|
||||
case 28:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput4
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput4
|
||||
case 29:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput5
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput5
|
||||
case 30:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput6
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput6
|
||||
case 31:
|
||||
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput7
|
||||
case adcVDDHPin:
|
||||
if Device == "nrf52840" {
|
||||
adcPin = 0x0D // VDDHDIV5 on the nrf52840
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
pwmPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput7
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
|
||||
// Set pin to read.
|
||||
nrf.SAADC.CH[0].PSELP.Set(adcPin)
|
||||
nrf.SAADC.CH[0].PSELN.Set(pwmPin)
|
||||
nrf.SAADC.CH[0].PSELP.Set(pwmPin)
|
||||
|
||||
// Destination for sample result.
|
||||
// Note: rawValue doesn't need to be kept alive for the GC, since the
|
||||
// volatile read later will force it to stay alive.
|
||||
nrf.SAADC.RESULT.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(&rawValue))))
|
||||
nrf.SAADC.RESULT.PTR.Set(uint32(uintptr(unsafe.Pointer(&rawValue))))
|
||||
nrf.SAADC.RESULT.MAXCNT.Set(1) // One sample
|
||||
|
||||
// Start tasks.
|
||||
@@ -310,18 +301,20 @@ func (spi *SPI) Transfer(w byte) (byte, error) {
|
||||
// padded until they fit: if len(w) > len(r) the extra bytes received will be
|
||||
// dropped and if len(w) < len(r) extra 0 bytes will be sent.
|
||||
func (spi *SPI) Tx(w, r []byte) error {
|
||||
// Unfortunately the hardware (on the nrf52832) only supports a limited
|
||||
// amount of bytes in the buffers (depending on the chip), so if either w or
|
||||
// r is longer than that the transfer needs to be broken up in pieces.
|
||||
// Unfortunately the hardware (on the nrf52832) only supports up to 255
|
||||
// bytes in the buffers, so if either w or r is longer than that the
|
||||
// transfer needs to be broken up in pieces.
|
||||
// The nrf52840 supports far larger buffers however, which isn't yet
|
||||
// supported.
|
||||
for len(r) != 0 || len(w) != 0 {
|
||||
// Prepare the SPI transfer: set the DMA pointers and lengths.
|
||||
// read buffer
|
||||
nr := uint32(len(r))
|
||||
if nr > 0 {
|
||||
if nr > spiMaxBufferSize {
|
||||
nr = spiMaxBufferSize
|
||||
if nr > 255 {
|
||||
nr = 255
|
||||
}
|
||||
spi.Bus.RXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(r)))))
|
||||
spi.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0]))))
|
||||
r = r[nr:]
|
||||
}
|
||||
spi.Bus.RXD.MAXCNT.Set(nr)
|
||||
@@ -329,10 +322,10 @@ func (spi *SPI) Tx(w, r []byte) error {
|
||||
// write buffer
|
||||
nw := uint32(len(w))
|
||||
if nw > 0 {
|
||||
if nw > spiMaxBufferSize {
|
||||
nw = spiMaxBufferSize
|
||||
if nw > 255 {
|
||||
nw = 255
|
||||
}
|
||||
spi.Bus.TXD.PTR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(w)))))
|
||||
spi.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0]))))
|
||||
w = w[nw:]
|
||||
}
|
||||
spi.Bus.TXD.MAXCNT.Set(nw)
|
||||
@@ -342,16 +335,10 @@ func (spi *SPI) Tx(w, r []byte) error {
|
||||
// finished if the transfer is send-only (a common case).
|
||||
spi.Bus.TASKS_START.Set(1)
|
||||
for spi.Bus.EVENTS_END.Get() == 0 {
|
||||
gosched()
|
||||
}
|
||||
spi.Bus.EVENTS_END.Set(0)
|
||||
}
|
||||
|
||||
// Make sure the w and r buffers stay alive for the GC until this point,
|
||||
// since they are used by the hardware but not otherwise visible.
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(r)))
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(w)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,3 @@ package machine
|
||||
func GetRNG() (ret uint32, err error) {
|
||||
return getRNG()
|
||||
}
|
||||
|
||||
func isSoftDeviceEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@ import (
|
||||
|
||||
// avoid a heap allocation in GetRNG.
|
||||
var (
|
||||
bytesAvailable uint8
|
||||
buf [4]uint8
|
||||
softdeviceEnabled uint8
|
||||
bytesAvailable uint8
|
||||
buf [4]uint8
|
||||
|
||||
errNoSoftDeviceSupport = errors.New("rng: softdevice not supported on this device")
|
||||
errNotEnoughRandomData = errors.New("rng: not enough random data available")
|
||||
@@ -23,7 +24,9 @@ var (
|
||||
func GetRNG() (ret uint32, err error) {
|
||||
// First check whether the SoftDevice is enabled.
|
||||
// sd_rand_application_bytes_available_get cannot be called when the SoftDevice is not enabled.
|
||||
if !isSoftDeviceEnabled() {
|
||||
arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled
|
||||
|
||||
if softdeviceEnabled == 0 {
|
||||
return getRNG()
|
||||
}
|
||||
|
||||
@@ -54,8 +57,3 @@ func GetRNG() (ret uint32, err error) {
|
||||
|
||||
return uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil
|
||||
}
|
||||
|
||||
// This function is defined in the runtime, but we need it too.
|
||||
//
|
||||
//go:linkname isSoftDeviceEnabled runtime.isSoftDeviceEnabled
|
||||
func isSoftDeviceEnabled() bool
|
||||
|
||||
@@ -124,31 +124,6 @@ const (
|
||||
fnXIP pinFunc = 0
|
||||
)
|
||||
|
||||
// validPins confirms that the SPI pin selection is a legitimate one
|
||||
// for the the 2040 chip.
|
||||
func (spi *SPI) validPins(config SPIConfig) error {
|
||||
var okSDI, okSDO, okSCK bool
|
||||
switch spi.Bus {
|
||||
case rp.SPI0:
|
||||
okSDI = config.SDI == 0 || config.SDI == 4 || config.SDI == 16 || config.SDI == 20
|
||||
okSDO = config.SDO == 3 || config.SDO == 7 || config.SDO == 19 || config.SDO == 23
|
||||
okSCK = config.SCK == 2 || config.SCK == 6 || config.SCK == 18 || config.SCK == 22
|
||||
case rp.SPI1:
|
||||
okSDI = config.SDI == 8 || config.SDI == 12 || config.SDI == 24 || config.SDI == 28
|
||||
okSDO = config.SDO == 11 || config.SDO == 15 || config.SDO == 27
|
||||
okSCK = config.SCK == 10 || config.SCK == 14 || config.SCK == 26
|
||||
}
|
||||
switch {
|
||||
case !okSDI:
|
||||
return errSPIInvalidSDI
|
||||
case !okSDO:
|
||||
return errSPIInvalidSDO
|
||||
case !okSCK:
|
||||
return errSPIInvalidSCK
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Configure configures the gpio pin as per mode.
|
||||
func (p Pin) Configure(config PinConfig) {
|
||||
if p == NoPin {
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
package machine
|
||||
|
||||
import "device/rp"
|
||||
|
||||
// Analog pins on RP2350a.
|
||||
const (
|
||||
ADC0 Pin = GPIO26
|
||||
@@ -14,28 +12,3 @@ const (
|
||||
// fifth ADC channel.
|
||||
thermADC = 30
|
||||
)
|
||||
|
||||
// validPins confirms that the SPI pin selection is a legitimate one
|
||||
// for the the 2350a chip.
|
||||
func (spi *SPI) validPins(config SPIConfig) error {
|
||||
var okSDI, okSDO, okSCK bool
|
||||
switch spi.Bus {
|
||||
case rp.SPI0:
|
||||
okSDI = config.SDI == 0 || config.SDI == 4 || config.SDI == 16 || config.SDI == 20
|
||||
okSDO = config.SDO == 3 || config.SDO == 7 || config.SDO == 19 || config.SDO == 23
|
||||
okSCK = config.SCK == 2 || config.SCK == 6 || config.SCK == 18 || config.SCK == 22
|
||||
case rp.SPI1:
|
||||
okSDI = config.SDI == 8 || config.SDI == 12 || config.SDI == 24 || config.SDI == 28
|
||||
okSDO = config.SDO == 11 || config.SDO == 15 || config.SDO == 27
|
||||
okSCK = config.SCK == 10 || config.SCK == 14 || config.SCK == 26
|
||||
}
|
||||
switch {
|
||||
case !okSDI:
|
||||
return errSPIInvalidSDI
|
||||
case !okSDO:
|
||||
return errSPIInvalidSDO
|
||||
case !okSCK:
|
||||
return errSPIInvalidSCK
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
package machine
|
||||
|
||||
import "device/rp"
|
||||
|
||||
// RP2350B has additional pins.
|
||||
|
||||
const (
|
||||
@@ -48,28 +46,3 @@ var (
|
||||
PWM10 = getPWMGroup(10)
|
||||
PWM11 = getPWMGroup(11)
|
||||
)
|
||||
|
||||
// validPins confirms that the SPI pin selection is a legitimate one
|
||||
// for the the 2350b chip.
|
||||
func (spi *SPI) validPins(config SPIConfig) error {
|
||||
var okSDI, okSDO, okSCK bool
|
||||
switch spi.Bus {
|
||||
case rp.SPI0:
|
||||
okSDI = config.SDI == 0 || config.SDI == 4 || config.SDI == 16 || config.SDI == 20 || config.SDI == 32 || config.SDI == 36
|
||||
okSDO = config.SDO == 3 || config.SDO == 7 || config.SDO == 19 || config.SDO == 23 || config.SDO == 35 || config.SDO == 39
|
||||
okSCK = config.SCK == 2 || config.SCK == 6 || config.SCK == 18 || config.SCK == 22 || config.SCK == 34 || config.SCK == 38
|
||||
case rp.SPI1:
|
||||
okSDI = config.SDI == 8 || config.SDI == 12 || config.SDI == 24 || config.SDI == 28 || config.SDI == 40 || config.SDI == 44
|
||||
okSDO = config.SDO == 11 || config.SDO == 15 || config.SDO == 27 || config.SDO == 31 || config.SDO == 43 || config.SDO == 47
|
||||
okSCK = config.SCK == 10 || config.SCK == 14 || config.SCK == 26 || config.SCK == 30 || config.SCK == 42 || config.SCK == 46
|
||||
}
|
||||
switch {
|
||||
case !okSDI:
|
||||
return errSPIInvalidSDI
|
||||
case !okSDO:
|
||||
return errSPIInvalidSDO
|
||||
case !okSCK:
|
||||
return errSPIInvalidSCK
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func (spi *SPI) SetBaudRate(br uint32) error {
|
||||
var prescale, postdiv uint32
|
||||
freq := CPUFrequency()
|
||||
for prescale = 2; prescale < 255; prescale += 2 {
|
||||
if uint64(freq) < uint64((prescale+2)*256)*uint64(br) {
|
||||
if freq < (prescale+2)*256*br {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -165,9 +165,27 @@ func (spi *SPI) Configure(config SPIConfig) error {
|
||||
config.SDI = SPI1_SDI_PIN
|
||||
}
|
||||
}
|
||||
if err := spi.validPins(config); err != nil {
|
||||
return err
|
||||
var okSDI, okSDO, okSCK bool
|
||||
switch spi.Bus {
|
||||
case rp.SPI0:
|
||||
okSDI = config.SDI == 0 || config.SDI == 4 || config.SDI == 16 || config.SDI == 20
|
||||
okSDO = config.SDO == 3 || config.SDO == 7 || config.SDO == 19 || config.SDO == 23
|
||||
okSCK = config.SCK == 2 || config.SCK == 6 || config.SCK == 18 || config.SCK == 22
|
||||
case rp.SPI1:
|
||||
okSDI = config.SDI == 8 || config.SDI == 12 || config.SDI == 24 || config.SDI == 28
|
||||
okSDO = config.SDO == 11 || config.SDO == 15 || config.SDO == 27
|
||||
okSCK = config.SCK == 10 || config.SCK == 14 || config.SCK == 26
|
||||
}
|
||||
|
||||
switch {
|
||||
case !okSDI:
|
||||
return errSPIInvalidSDI
|
||||
case !okSDO:
|
||||
return errSPIInvalidSDO
|
||||
case !okSCK:
|
||||
return errSPIInvalidSCK
|
||||
}
|
||||
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = defaultBaud
|
||||
}
|
||||
@@ -291,7 +309,7 @@ func (spi *SPI) tx(tx []byte) error {
|
||||
// - set data size to single bytes
|
||||
// - set the DREQ so that the DMA will fill the SPI FIFO as needed
|
||||
// - start the transfer
|
||||
ch.READ_ADDR.Set(uint32(unsafeNoEscape(unsafe.Pointer(unsafe.SliceData(tx)))))
|
||||
ch.READ_ADDR.Set(uint32(uintptr(unsafe.Pointer(&tx[0]))))
|
||||
ch.WRITE_ADDR.Set(uint32(uintptr(unsafe.Pointer(&spi.Bus.SSPDR))))
|
||||
ch.TRANS_COUNT.Set(uint32(len(tx)))
|
||||
ch.CTRL_TRIG.Set(rp.DMA_CH0_CTRL_TRIG_INCR_READ |
|
||||
@@ -310,11 +328,6 @@ func (spi *SPI) tx(tx []byte) error {
|
||||
for ch.CTRL_TRIG.Get()&rp.DMA_CH0_CTRL_TRIG_BUSY != 0 {
|
||||
}
|
||||
|
||||
// Make sure the read buffer stays alive until this point (in the unlikely
|
||||
// case the tx slice wasn't read after this function returns and a GC cycle
|
||||
// happened inbetween).
|
||||
keepAliveNoEscape(unsafe.Pointer(unsafe.SliceData(tx)))
|
||||
|
||||
// We didn't read any result values, which means the RX FIFO has likely
|
||||
// overflown. We have to clean up this mess now.
|
||||
|
||||
|
||||
@@ -6,6 +6,5 @@ package machine
|
||||
var Serial Serialer
|
||||
|
||||
func InitSerial() {
|
||||
initUSB()
|
||||
Serial = USBCDC
|
||||
}
|
||||
|
||||
@@ -19,17 +19,6 @@ var (
|
||||
USBCDC Serialer
|
||||
)
|
||||
|
||||
func initUSB() {
|
||||
enableUSBCDC()
|
||||
USBDev.Configure(UARTConfig{})
|
||||
}
|
||||
|
||||
// Using go:linkname here because there's a circular dependency between the
|
||||
// machine package and the machine/usb/cdc package.
|
||||
//
|
||||
//go:linkname enableUSBCDC machine/usb/cdc.EnableUSBCDC
|
||||
func enableUSBCDC()
|
||||
|
||||
type Serialer interface {
|
||||
WriteByte(c byte) error
|
||||
Write(data []byte) (n int, err error)
|
||||
|
||||
+10
-11
@@ -35,12 +35,12 @@ type msc struct {
|
||||
respStatus csw.Status // Response status for the last command
|
||||
sendZLP bool // Flag to indicate if a zero-length packet should be sent before sending CSW
|
||||
|
||||
cbw *CBW // Last received Command Block Wrapper
|
||||
queuedBytes uint32 // Number of bytes queued for sending
|
||||
sentBytes uint32 // Number of bytes sent
|
||||
transferBytes uint32 // Total bytes to send
|
||||
cswBuf []byte // CSW response buffer
|
||||
state mscState
|
||||
cbw *CBW // Last received Command Block Wrapper
|
||||
queuedBytes uint32 // Number of bytes queued for sending
|
||||
sentBytes uint32 // Number of bytes sent
|
||||
totalBytes uint32 // Total bytes to send
|
||||
cswBuf []byte // CSW response buffer
|
||||
state mscState
|
||||
|
||||
maxLUN uint8 // Maximum Logical Unit Number (n-1 for n LUNs)
|
||||
dev machine.BlockDevice
|
||||
@@ -160,9 +160,8 @@ func (m *msc) sendUSBPacket(b []byte) {
|
||||
func (m *msc) sendCSW(status csw.Status) {
|
||||
// Generate CSW packet into m.cswBuf and send it
|
||||
residue := uint32(0)
|
||||
expected := m.cbw.transferLength()
|
||||
if expected >= m.sentBytes {
|
||||
residue = expected - m.sentBytes
|
||||
if m.totalBytes >= m.sentBytes {
|
||||
residue = m.totalBytes - m.sentBytes
|
||||
}
|
||||
m.cbw.CSW(status, residue, m.cswBuf)
|
||||
m.state = mscStateStatusSent
|
||||
@@ -246,7 +245,7 @@ func (m *msc) run(b []byte, isEpOut bool) bool {
|
||||
|
||||
// Move on to the data transfer phase next go around (after sending the first message)
|
||||
m.state = mscStateData
|
||||
m.transferBytes = cbw.transferLength()
|
||||
m.totalBytes = cbw.transferLength()
|
||||
m.queuedBytes = 0
|
||||
m.sentBytes = 0
|
||||
m.respStatus = csw.StatusPassed
|
||||
@@ -282,7 +281,7 @@ func (m *msc) run(b []byte, isEpOut bool) bool {
|
||||
// to cycle back through this block, e.g. with TEST UNIT READY which sends only a CSW after
|
||||
// setting the sense key/add'l code/qualifier internally
|
||||
if m.state == mscStateStatus && !m.txStalled {
|
||||
if m.cbw.transferLength() > m.sentBytes && m.cbw.isIn() {
|
||||
if m.totalBytes > m.sentBytes && m.cbw.isIn() {
|
||||
// 6.7.2 The Thirteen Cases - Case 5 (Hi > Di): STALL before status
|
||||
m.stallEndpoint(usb.MSC_ENDPOINT_IN)
|
||||
} else if m.sendZLP {
|
||||
|
||||
+11
-16
@@ -21,9 +21,9 @@ func (m *msc) scsiCmdBegin() {
|
||||
return
|
||||
}
|
||||
|
||||
if m.transferBytes > 0 && m.cbw.isOut() {
|
||||
if m.totalBytes > 0 && m.cbw.isOut() {
|
||||
// Reject any other multi-packet commands
|
||||
if m.transferBytes > m.maxPacketSize {
|
||||
if m.totalBytes > m.maxPacketSize {
|
||||
m.sendScsiError(csw.StatusFailed, scsi.SenseIllegalRequest, scsi.SenseCodeInvalidCmdOpCode)
|
||||
return
|
||||
} else {
|
||||
@@ -53,7 +53,7 @@ func (m *msc) scsiCmdBegin() {
|
||||
}
|
||||
|
||||
if len(m.buf) == 0 {
|
||||
if m.transferBytes > 0 {
|
||||
if m.totalBytes > 0 {
|
||||
// 6.7.2 The Thirteen Cases - Case 4 (Hi > Dn)
|
||||
// https://usb.org/sites/default/files/usbmassbulk_10.pdf
|
||||
m.sendScsiError(csw.StatusFailed, scsi.SenseIllegalRequest, 0)
|
||||
@@ -63,7 +63,7 @@ func (m *msc) scsiCmdBegin() {
|
||||
m.state = mscStateStatus
|
||||
}
|
||||
} else {
|
||||
if m.transferBytes == 0 {
|
||||
if m.totalBytes == 0 {
|
||||
// 6.7.1 The Thirteen Cases - Case 2 (Hn < Di)
|
||||
// https://usb.org/sites/default/files/usbmassbulk_10.pdf
|
||||
m.sendScsiError(csw.StatusFailed, scsi.SenseIllegalRequest, 0)
|
||||
@@ -72,7 +72,6 @@ func (m *msc) scsiCmdBegin() {
|
||||
if m.cbw.transferLength() < uint32(len(m.buf)) {
|
||||
m.buf = m.buf[:m.cbw.transferLength()]
|
||||
}
|
||||
m.queuedBytes = uint32(len(m.buf))
|
||||
m.sendUSBPacket(m.buf)
|
||||
}
|
||||
}
|
||||
@@ -94,7 +93,7 @@ func (m *msc) scsiDataTransfer(b []byte) bool {
|
||||
// Update our sent bytes count to include the just-confirmed bytes
|
||||
m.sentBytes += m.queuedBytes
|
||||
|
||||
if m.sentBytes >= m.transferBytes {
|
||||
if m.sentBytes >= m.totalBytes {
|
||||
// Transfer complete, send CSW after transfer confirmed
|
||||
m.state = mscStateStatus
|
||||
} else if cmdType == scsi.CmdRead {
|
||||
@@ -159,8 +158,8 @@ func (m *msc) scsiCmdModeSense(cmd scsi.Cmd) {
|
||||
|
||||
// The host allows a good amount of leeway in response size
|
||||
// Reset total bytes to what we'll actually send
|
||||
if m.transferBytes > respLen {
|
||||
m.transferBytes = respLen
|
||||
if m.totalBytes > respLen {
|
||||
m.totalBytes = respLen
|
||||
m.sendZLP = true
|
||||
}
|
||||
|
||||
@@ -211,7 +210,7 @@ func (m *msc) scsiCmdRequestSense() {
|
||||
// Set the buffer size to the SCSI sense message size and clear
|
||||
m.resetBuffer(scsi.RequestSenseRespLen)
|
||||
m.queuedBytes = scsi.RequestSenseRespLen
|
||||
m.transferBytes = scsi.RequestSenseRespLen
|
||||
m.totalBytes = scsi.RequestSenseRespLen
|
||||
|
||||
// 0x70 - current error, 0x71 - deferred error (not used)
|
||||
m.buf[0] = 0xF0 // 0x70 for current error plus 0x80 for valid flag bit
|
||||
@@ -267,7 +266,7 @@ func (m *msc) scsiQueueTask(cmdType scsi.CmdType, b []byte) bool {
|
||||
switch cmdType {
|
||||
case scsi.CmdWrite:
|
||||
// If we're writing data wait until we have a full write block of data that can be processed.
|
||||
if m.queuedBytes == uint32(cap(m.blockCache)) || (m.sentBytes+m.queuedBytes >= m.transferBytes) {
|
||||
if m.queuedBytes == uint32(cap(m.blockCache)) {
|
||||
m.taskQueued = true
|
||||
}
|
||||
case scsi.CmdUnmap:
|
||||
@@ -280,11 +279,7 @@ func (m *msc) scsiQueueTask(cmdType scsi.CmdType, b []byte) bool {
|
||||
|
||||
func (m *msc) sendScsiError(status csw.Status, key scsi.Sense, code scsi.SenseCode) {
|
||||
// Generate CSW into m.cswBuf
|
||||
expected := m.cbw.transferLength()
|
||||
residue := uint32(0)
|
||||
if expected > m.sentBytes {
|
||||
residue = expected - m.sentBytes
|
||||
}
|
||||
residue := m.totalBytes - m.sentBytes
|
||||
|
||||
// Prepare to send CSW
|
||||
m.sendZLP = true // Ensure the transaction is signaled as ended before a CSW is sent
|
||||
@@ -296,7 +291,7 @@ func (m *msc) sendScsiError(status csw.Status, key scsi.Sense, code scsi.SenseCo
|
||||
m.addlSenseCode = code
|
||||
m.addlSenseQualifier = 0x00 // Not used
|
||||
|
||||
if expected > 0 && residue > 0 {
|
||||
if m.totalBytes > 0 && residue > 0 {
|
||||
if m.cbw.isIn() {
|
||||
m.stallEndpoint(usb.MSC_ENDPOINT_IN)
|
||||
} else {
|
||||
|
||||
@@ -136,13 +136,13 @@ func (m *msc) scsiEvpdInquiry(cmd scsi.Cmd, pageCode uint8) {
|
||||
|
||||
// Set total bytes to the length of our response
|
||||
m.queuedBytes = uint32(len(m.buf))
|
||||
m.transferBytes = uint32(len(m.buf))
|
||||
m.totalBytes = uint32(len(m.buf))
|
||||
}
|
||||
|
||||
func (m *msc) scsiStdInquiry(cmd scsi.Cmd) {
|
||||
m.resetBuffer(scsi.InquiryRespLen)
|
||||
m.queuedBytes = scsi.InquiryRespLen
|
||||
m.transferBytes = scsi.InquiryRespLen
|
||||
m.totalBytes = scsi.InquiryRespLen
|
||||
|
||||
// byte 0 - Device Type (0x00 for direct access block device)
|
||||
// byte 1 - Removable media bit
|
||||
|
||||
@@ -12,7 +12,7 @@ func (m *msc) scsiCmdReadWrite(cmd scsi.Cmd) {
|
||||
status := m.validateScsiReadWrite(cmd)
|
||||
if status != csw.StatusPassed {
|
||||
m.sendScsiError(status, scsi.SenseIllegalRequest, scsi.SenseCodeInvalidCmdOpCode)
|
||||
} else if m.transferBytes > 0 {
|
||||
} else if m.totalBytes > 0 {
|
||||
if cmd.CmdType() == scsi.CmdRead {
|
||||
m.scsiRead(cmd)
|
||||
} else {
|
||||
@@ -28,7 +28,7 @@ func (m *msc) scsiCmdReadWrite(cmd scsi.Cmd) {
|
||||
func (m *msc) validateScsiReadWrite(cmd scsi.Cmd) csw.Status {
|
||||
blockCount := cmd.BlockCount()
|
||||
// CBW wrapper transfer length
|
||||
if m.transferBytes == 0 {
|
||||
if m.totalBytes == 0 {
|
||||
// If the SCSI command's block count doesn't loosely match the wrapper's transfer length something's wrong
|
||||
if blockCount > 0 {
|
||||
return csw.StatusPhaseError
|
||||
@@ -50,7 +50,7 @@ func (m *msc) validateScsiReadWrite(cmd scsi.Cmd) csw.Status {
|
||||
// https://usb.org/sites/default/files/usbmassbulk_10.pdf
|
||||
return csw.StatusFailed
|
||||
}
|
||||
if m.transferBytes/blockCount == 0 {
|
||||
if m.totalBytes/blockCount == 0 {
|
||||
// Block size shouldn't be small enough to round to zero
|
||||
// 6.7.2 The Thirteen Cases - Case 7 (Hi < Di) READ(10) or
|
||||
// 6.7.3 The Thirteen Cases - Case 13 (Ho < Do) WRITE(10)
|
||||
@@ -103,7 +103,7 @@ func (m *msc) writeBlock(b []byte, lba, offset uint32) (n int, err error) {
|
||||
|
||||
func (m *msc) scsiRead(cmd scsi.Cmd) {
|
||||
// Make sure we don't exceed the buffer size
|
||||
readEnd := m.transferBytes - m.sentBytes
|
||||
readEnd := m.totalBytes - m.sentBytes
|
||||
if readEnd > m.maxPacketSize {
|
||||
readEnd = m.maxPacketSize
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func (m *msc) scsiWrite(cmd scsi.Cmd, b []byte) {
|
||||
m.sentBytes += uint32(len(b))
|
||||
}
|
||||
|
||||
if m.sentBytes >= m.transferBytes {
|
||||
if m.sentBytes >= m.totalBytes {
|
||||
// Data transfer is complete, send CSW
|
||||
m.state = mscStateStatus
|
||||
m.run([]byte{}, true)
|
||||
|
||||
@@ -60,7 +60,7 @@ func (m *msc) scsiUnmap(b []byte) {
|
||||
// FIXME: We need to handle erase block alignment
|
||||
|
||||
m.sentBytes += uint32(len(b))
|
||||
if m.sentBytes >= m.transferBytes {
|
||||
if m.sentBytes >= m.totalBytes {
|
||||
// Order 66 complete, send CSW to establish galactic empire
|
||||
m.state = mscStateStatus
|
||||
m.run([]byte{}, true)
|
||||
|
||||
@@ -3,6 +3,7 @@ package msc
|
||||
import (
|
||||
"machine"
|
||||
"machine/usb"
|
||||
"machine/usb/msc/csw"
|
||||
)
|
||||
|
||||
func setupPacketHandler(setup usb.Setup) bool {
|
||||
@@ -17,17 +18,11 @@ func (m *msc) setupPacketHandler(setup usb.Setup) bool {
|
||||
wValue := (uint16(setup.WValueH) << 8) | uint16(setup.WValueL)
|
||||
switch setup.BRequest {
|
||||
case usb.CLEAR_FEATURE:
|
||||
if setup.BmRequestType == 0x02 { // Host-to-Device | Standard | Endpoint
|
||||
ok = m.handleClearFeature(setup, wValue)
|
||||
}
|
||||
ok = m.handleClearFeature(setup, wValue)
|
||||
case usb.GET_MAX_LUN:
|
||||
if setup.BmRequestType == 0xA1 { // Device-to-Host | Class | Interface
|
||||
ok = m.handleGetMaxLun(setup, wValue)
|
||||
}
|
||||
ok = m.handleGetMaxLun(setup, wValue)
|
||||
case usb.MSC_RESET:
|
||||
if setup.BmRequestType == 0x21 { // Host-to-Device | Class | Interface
|
||||
ok = m.handleReset(setup, wValue)
|
||||
}
|
||||
ok = m.handleReset(setup, wValue)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
@@ -58,25 +53,24 @@ func (m *msc) handleClearFeature(setup usb.Setup, wValue uint16) bool {
|
||||
} else if wIndex == usb.MSC_ENDPOINT_OUT {
|
||||
m.stallEndpoint(usb.MSC_ENDPOINT_OUT)
|
||||
}
|
||||
machine.SendZlp()
|
||||
return true
|
||||
return ok
|
||||
}
|
||||
|
||||
// Clear the direction bit from the endpoint address for comparison
|
||||
wIndex := setup.WIndex & 0x7F
|
||||
|
||||
// Clear the IN/OUT stalls if addressed to the endpoint
|
||||
if wIndex == usb.MSC_ENDPOINT_IN {
|
||||
// Clear the IN/OUT stalls if addressed to the endpoint, or both if addressed to the interface
|
||||
if wIndex == usb.MSC_ENDPOINT_IN || wIndex == mscInterface {
|
||||
m.clearStallEndpoint(usb.MSC_ENDPOINT_IN)
|
||||
ok = true
|
||||
}
|
||||
if wIndex == usb.MSC_ENDPOINT_OUT {
|
||||
if wIndex == usb.MSC_ENDPOINT_OUT || wIndex == mscInterface {
|
||||
m.clearStallEndpoint(usb.MSC_ENDPOINT_OUT)
|
||||
ok = true
|
||||
}
|
||||
// Send a CSW if needed to resume after the IN endpoint stall is cleared
|
||||
if m.state == mscStateStatus && wIndex == usb.MSC_ENDPOINT_IN {
|
||||
m.sendCSW(m.respStatus)
|
||||
m.sendCSW(csw.StatusPassed)
|
||||
ok = true
|
||||
}
|
||||
|
||||
|
||||
@@ -73,14 +73,6 @@ func align(ptr uintptr) uintptr {
|
||||
//export tinygo_getCurrentStackPointer
|
||||
func getCurrentStackPointer() uintptr
|
||||
|
||||
// savedStackPointer is used to save the system stack pointer while in a goroutine.
|
||||
var savedStackPointer uintptr
|
||||
|
||||
// saveStackPointer is called by internal/task.state.Resume() to save the stack pointer before entering a goroutine from the system stack.
|
||||
func saveStackPointer() {
|
||||
savedStackPointer = getCurrentStackPointer()
|
||||
}
|
||||
|
||||
// growHeap tries to grow the heap size. It returns true if it succeeds, false
|
||||
// otherwise.
|
||||
func growHeap() bool {
|
||||
|
||||
@@ -8,21 +8,16 @@ import "unsafe"
|
||||
// code linked from other languages can allocate memory without colliding with
|
||||
// our GC allocations.
|
||||
|
||||
// Map of allocations, where the key is the allocated pointer and the value is
|
||||
// the size of the allocation.
|
||||
// TODO: make this a map[unsafe.Pointer]uintptr, since that results in slightly
|
||||
// smaller binaries. But for that to work, unsafe.Pointer needs to be seen as a
|
||||
// binary key (which it is not at the moment).
|
||||
// See https://github.com/tinygo-org/tinygo/pull/4898 for details.
|
||||
var allocs = make(map[*byte]uintptr)
|
||||
var allocs = make(map[uintptr][]byte)
|
||||
|
||||
//export malloc
|
||||
func libc_malloc(size uintptr) unsafe.Pointer {
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
ptr := alloc(size, nil)
|
||||
allocs[(*byte)(ptr)] = size
|
||||
buf := make([]byte, size)
|
||||
ptr := unsafe.Pointer(&buf[0])
|
||||
allocs[uintptr(ptr)] = buf
|
||||
return ptr
|
||||
}
|
||||
|
||||
@@ -31,8 +26,8 @@ func libc_free(ptr unsafe.Pointer) {
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
if _, ok := allocs[(*byte)(ptr)]; ok {
|
||||
delete(allocs, (*byte)(ptr))
|
||||
if _, ok := allocs[uintptr(ptr)]; ok {
|
||||
delete(allocs, uintptr(ptr))
|
||||
} else {
|
||||
panic("free: invalid pointer")
|
||||
}
|
||||
@@ -53,20 +48,18 @@ func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
|
||||
|
||||
// It's hard to optimize this to expand the current buffer with our GC, but
|
||||
// it is theoretically possible. For now, just always allocate fresh.
|
||||
// TODO: we could skip this if the new allocation is smaller than the old.
|
||||
ptr := alloc(size, nil)
|
||||
buf := make([]byte, size)
|
||||
|
||||
if oldPtr != nil {
|
||||
if oldSize, ok := allocs[(*byte)(oldPtr)]; ok {
|
||||
oldBuf := unsafe.Slice((*byte)(oldPtr), oldSize)
|
||||
newBuf := unsafe.Slice((*byte)(ptr), size)
|
||||
copy(newBuf, oldBuf)
|
||||
delete(allocs, (*byte)(oldPtr))
|
||||
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok {
|
||||
copy(buf, oldBuf)
|
||||
delete(allocs, uintptr(oldPtr))
|
||||
} else {
|
||||
panic("realloc: invalid pointer")
|
||||
}
|
||||
}
|
||||
|
||||
allocs[(*byte)(ptr)] = size
|
||||
ptr := unsafe.Pointer(&buf[0])
|
||||
allocs[uintptr(ptr)] = buf
|
||||
return ptr
|
||||
}
|
||||
|
||||
@@ -78,8 +78,5 @@ tinygo_longjmp:
|
||||
ld r28, X+
|
||||
ld r29, X+
|
||||
|
||||
; Mark that we returned from a longjmp.
|
||||
ldi r24, 1
|
||||
|
||||
; Jump to the PC (stored in the Z register)
|
||||
icall
|
||||
|
||||
+292
-402
@@ -46,15 +46,18 @@ const (
|
||||
bytesPerBlock = wordsPerBlock * unsafe.Sizeof(heapStart)
|
||||
stateBits = 2 // how many bits a block state takes (see blockState type)
|
||||
blocksPerStateByte = 8 / stateBits
|
||||
markStackSize = 8 * unsafe.Sizeof((*int)(nil)) // number of to-be-marked blocks to queue before forcing a rescan
|
||||
)
|
||||
|
||||
var (
|
||||
metadataStart unsafe.Pointer // pointer to the start of the heap metadata
|
||||
scanList *objHeader // scanList is a singly linked list of heap objects that have been marked but not scanned
|
||||
freeRanges *freeRange // freeRanges is a linked list of free block ranges
|
||||
nextAlloc gcBlock // the next block that should be tried by the allocator
|
||||
endBlock gcBlock // the block just past the end of the available space
|
||||
gcTotalAlloc uint64 // total number of bytes allocated
|
||||
gcTotalBlocks uint64 // total number of allocated blocks
|
||||
gcMallocs uint64 // total number of allocations
|
||||
gcFrees uint64 // total number of objects freed
|
||||
gcFreedBlocks uint64 // total number of freed blocks
|
||||
gcLock task.PMutex // lock to avoid race conditions on multicore systems
|
||||
)
|
||||
|
||||
@@ -63,28 +66,24 @@ var zeroSizedAlloc uint8
|
||||
|
||||
// Provide some abstraction over heap blocks.
|
||||
|
||||
// blockState stores the four states in which a block can be.
|
||||
// It holds 1 bit in each nibble.
|
||||
// When stored into a state byte, each bit in a nibble corresponds to a different block.
|
||||
// For blocks A-D, a state byte would be laid out as 0bDCBA_DCBA.
|
||||
// blockState stores the four states in which a block can be. It is two bits in
|
||||
// size.
|
||||
type blockState uint8
|
||||
|
||||
const (
|
||||
blockStateLow blockState = 1
|
||||
blockStateHigh blockState = 1 << blocksPerStateByte
|
||||
|
||||
blockStateFree blockState = 0
|
||||
blockStateHead blockState = blockStateLow
|
||||
blockStateTail blockState = blockStateHigh
|
||||
blockStateMark blockState = blockStateLow | blockStateHigh
|
||||
blockStateMask blockState = blockStateLow | blockStateHigh
|
||||
blockStateFree blockState = 0 // 00
|
||||
blockStateHead blockState = 1 // 01
|
||||
blockStateTail blockState = 2 // 10
|
||||
blockStateMark blockState = 3 // 11
|
||||
blockStateMask blockState = 3 // 11
|
||||
)
|
||||
|
||||
// blockStateEach is a mask that can be used to extract a nibble from the block state.
|
||||
const blockStateEach = 1<<blocksPerStateByte - 1
|
||||
|
||||
// The byte value of a block where every block is a 'tail' block.
|
||||
const blockStateByteAllTails = byte(blockStateTail) * blockStateEach
|
||||
const blockStateByteAllTails = 0 |
|
||||
uint8(blockStateTail<<(stateBits*3)) |
|
||||
uint8(blockStateTail<<(stateBits*2)) |
|
||||
uint8(blockStateTail<<(stateBits*1)) |
|
||||
uint8(blockStateTail<<(stateBits*0))
|
||||
|
||||
// String returns a human-readable version of the block state, for debugging.
|
||||
func (s blockState) String() string {
|
||||
@@ -181,7 +180,7 @@ func (b gcBlock) stateByte() byte {
|
||||
// Return the block state given a state byte. The state byte must have been
|
||||
// obtained using b.stateByte(), otherwise the result is incorrect.
|
||||
func (b gcBlock) stateFromByte(stateByte byte) blockState {
|
||||
return blockState(stateByte>>(b%blocksPerStateByte)) & blockStateMask
|
||||
return blockState(stateByte>>((b%blocksPerStateByte)*stateBits)) & blockStateMask
|
||||
}
|
||||
|
||||
// State returns the current block state.
|
||||
@@ -194,112 +193,36 @@ func (b gcBlock) state() blockState {
|
||||
// from head to mark.
|
||||
func (b gcBlock) setState(newState blockState) {
|
||||
stateBytePtr := (*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
|
||||
*stateBytePtr |= uint8(newState << (b % blocksPerStateByte))
|
||||
*stateBytePtr |= uint8(newState << ((b % blocksPerStateByte) * stateBits))
|
||||
if gcAsserts && b.state() != newState {
|
||||
runtimePanic("gc: setState() was not successful")
|
||||
}
|
||||
}
|
||||
|
||||
// objHeader is a structure prepended to every heap object to hold metadata.
|
||||
type objHeader struct {
|
||||
// next is the next object to scan after this.
|
||||
next *objHeader
|
||||
|
||||
// layout holds the layout bitmap used to find pointers in the object.
|
||||
layout gcLayout
|
||||
}
|
||||
|
||||
// freeRange is a node on the outer list of range lengths.
|
||||
// The free ranges are structured as two nested singly-linked lists:
|
||||
// - The outer level (freeRange) has one entry for each unique range length.
|
||||
// - The inner level (freeRangeMore) has one entry for each additional range of the same length.
|
||||
// This two-level structure ensures that insertion/removal times are proportional to the requested length.
|
||||
type freeRange struct {
|
||||
// len is the length of this free range.
|
||||
len uintptr
|
||||
|
||||
// nextLen is the next longer free range.
|
||||
nextLen *freeRange
|
||||
|
||||
// nextWithLen is the next free range with this length.
|
||||
nextWithLen *freeRangeMore
|
||||
}
|
||||
|
||||
// freeRangeMore is a node on the inner list of equal-length ranges.
|
||||
type freeRangeMore struct {
|
||||
next *freeRangeMore
|
||||
}
|
||||
|
||||
// insertFreeRange inserts a range of len blocks starting at ptr into the free list.
|
||||
func insertFreeRange(ptr unsafe.Pointer, len uintptr) {
|
||||
if gcAsserts && len == 0 {
|
||||
runtimePanic("gc: insert 0-length free range")
|
||||
// markFree sets the block state to free, no matter what state it was in before.
|
||||
func (b gcBlock) markFree() {
|
||||
stateBytePtr := (*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
|
||||
*stateBytePtr &^= uint8(blockStateMask << ((b % blocksPerStateByte) * stateBits))
|
||||
if gcAsserts && b.state() != blockStateFree {
|
||||
runtimePanic("gc: markFree() was not successful")
|
||||
}
|
||||
|
||||
// Find the insertion point by length.
|
||||
// Skip until the next range is at least the target length.
|
||||
insDst := &freeRanges
|
||||
for *insDst != nil && (*insDst).len < len {
|
||||
insDst = &(*insDst).nextLen
|
||||
}
|
||||
|
||||
// Create the new free range.
|
||||
next := *insDst
|
||||
if next != nil && next.len == len {
|
||||
// Insert into the list with this length.
|
||||
newRange := (*freeRangeMore)(ptr)
|
||||
newRange.next = next.nextWithLen
|
||||
next.nextWithLen = newRange
|
||||
} else {
|
||||
// Insert into the list of lengths.
|
||||
newRange := (*freeRange)(ptr)
|
||||
*newRange = freeRange{
|
||||
len: len,
|
||||
nextLen: next,
|
||||
nextWithLen: nil,
|
||||
}
|
||||
*insDst = newRange
|
||||
if gcAsserts {
|
||||
*(*[wordsPerBlock]uintptr)(unsafe.Pointer(b.address())) = [wordsPerBlock]uintptr{}
|
||||
}
|
||||
}
|
||||
|
||||
// popFreeRange removes a range of len blocks from the freeRanges list.
|
||||
// It returns nil if there are no sufficiently long ranges.
|
||||
func popFreeRange(len uintptr) unsafe.Pointer {
|
||||
if gcAsserts && len == 0 {
|
||||
runtimePanic("gc: pop 0-length free range")
|
||||
// unmark changes the state of the block from mark to head. It must be marked
|
||||
// before calling this function.
|
||||
func (b gcBlock) unmark() {
|
||||
if gcAsserts && b.state() != blockStateMark {
|
||||
runtimePanic("gc: unmark() on a block that is not marked")
|
||||
}
|
||||
|
||||
// Find the removal point by length.
|
||||
// Skip until the next range is at least the target length.
|
||||
remDst := &freeRanges
|
||||
for *remDst != nil && (*remDst).len < len {
|
||||
remDst = &(*remDst).nextLen
|
||||
clearMask := blockStateMask ^ blockStateHead // the bits to clear from the state
|
||||
stateBytePtr := (*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
|
||||
*stateBytePtr &^= uint8(clearMask << ((b % blocksPerStateByte) * stateBits))
|
||||
if gcAsserts && b.state() != blockStateHead {
|
||||
runtimePanic("gc: unmark() was not successful")
|
||||
}
|
||||
|
||||
rangeWithLength := *remDst
|
||||
if rangeWithLength == nil {
|
||||
// No ranges are long enough.
|
||||
return nil
|
||||
}
|
||||
removedLen := rangeWithLength.len
|
||||
|
||||
// Remove the range.
|
||||
var ptr unsafe.Pointer
|
||||
if nextWithLen := rangeWithLength.nextWithLen; nextWithLen != nil {
|
||||
// Remove from the list with this length.
|
||||
rangeWithLength.nextWithLen = nextWithLen.next
|
||||
ptr = unsafe.Pointer(nextWithLen)
|
||||
} else {
|
||||
// Remove from the list of lengths.
|
||||
*remDst = rangeWithLength.nextLen
|
||||
ptr = unsafe.Pointer(rangeWithLength)
|
||||
}
|
||||
|
||||
if removedLen > len {
|
||||
// Insert the leftover range.
|
||||
insertFreeRange(unsafe.Add(ptr, len*bytesPerBlock), removedLen-len)
|
||||
}
|
||||
return ptr
|
||||
}
|
||||
|
||||
func isOnHeap(ptr uintptr) bool {
|
||||
@@ -316,9 +239,6 @@ func initHeap() {
|
||||
// Set all block states to 'free'.
|
||||
metadataSize := heapEnd - uintptr(metadataStart)
|
||||
memzero(unsafe.Pointer(metadataStart), metadataSize)
|
||||
|
||||
// Rebuild the free ranges list.
|
||||
buildFreeRanges()
|
||||
}
|
||||
|
||||
// setHeapEnd is called to expand the heap. The heap can only grow, not shrink.
|
||||
@@ -350,9 +270,6 @@ func setHeapEnd(newHeapEnd uintptr) {
|
||||
if gcAsserts && uintptr(metadataStart) < uintptr(oldMetadataStart)+oldMetadataSize {
|
||||
runtimePanic("gc: heap did not grow enough at once")
|
||||
}
|
||||
|
||||
// Rebuild the free ranges list.
|
||||
buildFreeRanges()
|
||||
}
|
||||
|
||||
// calculateHeapAddresses initializes variables such as metadataStart and
|
||||
@@ -394,88 +311,118 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
return unsafe.Pointer(&zeroSizedAlloc)
|
||||
}
|
||||
|
||||
if preciseHeap {
|
||||
size += align(unsafe.Sizeof(layout))
|
||||
}
|
||||
|
||||
if interrupt.In() {
|
||||
runtimePanicAt(returnAddress(0), "heap alloc in interrupt")
|
||||
}
|
||||
|
||||
// Round the size up to a multiple of blocks, adding space for the header.
|
||||
rawSize := size
|
||||
size += align(unsafe.Sizeof(objHeader{}))
|
||||
size += bytesPerBlock - 1
|
||||
if size < rawSize {
|
||||
// The size overflowed.
|
||||
runtimePanicAt(returnAddress(0), "out of memory")
|
||||
}
|
||||
neededBlocks := size / bytesPerBlock
|
||||
size = neededBlocks * bytesPerBlock
|
||||
|
||||
// Make sure there are no concurrent allocations. The heap is not currently
|
||||
// designed for concurrent alloc/GC.
|
||||
gcLock.Lock()
|
||||
|
||||
// Update the total allocation counters.
|
||||
gcTotalAlloc += uint64(rawSize)
|
||||
gcTotalAlloc += uint64(size)
|
||||
gcMallocs++
|
||||
|
||||
// Acquire a range of free blocks.
|
||||
var ranGC bool
|
||||
var grewHeap bool
|
||||
var pointer unsafe.Pointer
|
||||
neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
|
||||
gcTotalBlocks += uint64(neededBlocks)
|
||||
|
||||
// Continue looping until a run of free blocks has been found that fits the
|
||||
// requested size.
|
||||
index := nextAlloc
|
||||
numFreeBlocks := uintptr(0)
|
||||
heapScanCount := uint8(0)
|
||||
for {
|
||||
pointer = popFreeRange(neededBlocks)
|
||||
if pointer != nil {
|
||||
break
|
||||
}
|
||||
|
||||
if !ranGC {
|
||||
// Run the collector and try again.
|
||||
freeBytes := runGC()
|
||||
ranGC = true
|
||||
heapSize := uintptr(metadataStart) - heapStart
|
||||
if freeBytes < heapSize/3 {
|
||||
// Ensure there is at least 33% headroom.
|
||||
// This percentage was arbitrarily chosen, and may need to
|
||||
// be tuned in the future.
|
||||
growHeap()
|
||||
if index == nextAlloc {
|
||||
if heapScanCount == 0 {
|
||||
heapScanCount = 1
|
||||
} else if heapScanCount == 1 {
|
||||
// The entire heap has been searched for free memory, but none
|
||||
// could be found. Run a garbage collection cycle to reclaim
|
||||
// free memory and try again.
|
||||
heapScanCount = 2
|
||||
freeBytes := runGC()
|
||||
heapSize := uintptr(metadataStart) - heapStart
|
||||
if freeBytes < heapSize/3 {
|
||||
// Ensure there is at least 33% headroom.
|
||||
// This percentage was arbitrarily chosen, and may need to
|
||||
// be tuned in the future.
|
||||
growHeap()
|
||||
}
|
||||
} else {
|
||||
// Even after garbage collection, no free memory could be found.
|
||||
// Try to increase heap size.
|
||||
if growHeap() {
|
||||
// Success, the heap was increased in size. Try again with a
|
||||
// larger heap.
|
||||
} else {
|
||||
// Unfortunately the heap could not be increased. This
|
||||
// happens on baremetal systems for example (where all
|
||||
// available RAM has already been dedicated to the heap).
|
||||
runtimePanicAt(returnAddress(0), "out of memory")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap around the end of the heap.
|
||||
if index == endBlock {
|
||||
index = 0
|
||||
// Reset numFreeBlocks as allocations cannot wrap.
|
||||
numFreeBlocks = 0
|
||||
// In rare cases, the initial heap might be so small that there are
|
||||
// no blocks at all. In this case, it's better to jump back to the
|
||||
// start of the loop and try again, until the GC realizes there is
|
||||
// no memory and grows the heap.
|
||||
// This can sometimes happen on WebAssembly, where the initial heap
|
||||
// is created by whatever is left on the last memory page.
|
||||
continue
|
||||
}
|
||||
|
||||
if gcDebug && !grewHeap {
|
||||
println("grow heap for request:", uint(neededBlocks))
|
||||
dumpFreeRangeCounts()
|
||||
}
|
||||
if growHeap() {
|
||||
grewHeap = true
|
||||
// Is the block we're looking at free?
|
||||
if index.state() != blockStateFree {
|
||||
// This block is in use. Try again from this point.
|
||||
numFreeBlocks = 0
|
||||
index++
|
||||
continue
|
||||
}
|
||||
numFreeBlocks++
|
||||
index++
|
||||
|
||||
// Unfortunately the heap could not be increased. This
|
||||
// happens on baremetal systems for example (where all
|
||||
// available RAM has already been dedicated to the heap).
|
||||
runtimePanicAt(returnAddress(0), "out of memory")
|
||||
// Are we finished?
|
||||
if numFreeBlocks == neededBlocks {
|
||||
// Found a big enough range of free blocks!
|
||||
nextAlloc = index
|
||||
thisAlloc := index - gcBlock(neededBlocks)
|
||||
if gcDebug {
|
||||
println("found memory:", thisAlloc.pointer(), int(size))
|
||||
}
|
||||
|
||||
// Set the following blocks as being allocated.
|
||||
thisAlloc.setState(blockStateHead)
|
||||
for i := thisAlloc + 1; i != nextAlloc; i++ {
|
||||
i.setState(blockStateTail)
|
||||
}
|
||||
|
||||
// We've claimed this allocation, now we can unlock the heap.
|
||||
gcLock.Unlock()
|
||||
|
||||
// Return a pointer to this allocation.
|
||||
pointer := thisAlloc.pointer()
|
||||
if preciseHeap {
|
||||
// Store the object layout at the start of the object.
|
||||
// TODO: this wastes a little bit of space on systems with
|
||||
// larger-than-pointer alignment requirements.
|
||||
*(*unsafe.Pointer)(pointer) = layout
|
||||
add := align(unsafe.Sizeof(layout))
|
||||
pointer = unsafe.Add(pointer, add)
|
||||
size -= add
|
||||
}
|
||||
memzero(pointer, size)
|
||||
return pointer
|
||||
}
|
||||
}
|
||||
|
||||
// Set the backing blocks as being allocated.
|
||||
block := blockFromAddr(uintptr(pointer))
|
||||
block.setState(blockStateHead)
|
||||
for i := block + 1; i != block+gcBlock(neededBlocks); i++ {
|
||||
i.setState(blockStateTail)
|
||||
}
|
||||
|
||||
// Create the object header.
|
||||
header := (*objHeader)(pointer)
|
||||
header.layout = parseGCLayout(layout)
|
||||
|
||||
// We've claimed this allocation, now we can unlock the heap.
|
||||
gcLock.Unlock()
|
||||
|
||||
// Return a pointer to this allocation.
|
||||
add := align(unsafe.Sizeof(objHeader{}))
|
||||
pointer = unsafe.Add(pointer, add)
|
||||
size -= add
|
||||
memzero(pointer, size)
|
||||
return pointer
|
||||
}
|
||||
|
||||
func realloc(ptr unsafe.Pointer, size uintptr) unsafe.Pointer {
|
||||
@@ -560,10 +507,7 @@ func runGC() (freeBytes uintptr) {
|
||||
|
||||
// Sweep phase: free all non-marked objects and unmark marked objects for
|
||||
// the next collection cycle.
|
||||
sweep()
|
||||
|
||||
// Rebuild the free ranges list.
|
||||
freeBytes = buildFreeRanges()
|
||||
freeBytes = sweep()
|
||||
|
||||
// Show how much has been sweeped, for debugging.
|
||||
if gcDebug {
|
||||
@@ -575,7 +519,8 @@ func runGC() (freeBytes uintptr) {
|
||||
|
||||
// markRoots reads all pointers from start to end (exclusive) and if they look
|
||||
// like a heap pointer and are unmarked, marks them and scans that object as
|
||||
// well (recursively). The starting address must be valid and aligned.
|
||||
// well (recursively). The start and end parameters must be valid pointers and
|
||||
// must be aligned.
|
||||
func markRoots(start, end uintptr) {
|
||||
if gcDebug {
|
||||
println("mark from", start, "to", end, int(end-start))
|
||||
@@ -587,21 +532,18 @@ func markRoots(start, end uintptr) {
|
||||
if start%unsafe.Alignof(start) != 0 {
|
||||
runtimePanic("gc: unaligned start pointer")
|
||||
}
|
||||
if end%unsafe.Alignof(end) != 0 {
|
||||
runtimePanic("gc: unaligned end pointer")
|
||||
}
|
||||
}
|
||||
|
||||
// Scan the range conservatively.
|
||||
scanConservative(start, end-start)
|
||||
}
|
||||
// Reduce the end bound to avoid reading too far on platforms where pointer alignment is smaller than pointer size.
|
||||
// If the size of the range is 0, then end will be slightly below start after this.
|
||||
end -= unsafe.Sizeof(end) - unsafe.Alignof(end)
|
||||
|
||||
// scanConservative scans all possible pointer locations in a range and marks referenced heap allocations.
|
||||
// The starting address must be valid and pointer-aligned.
|
||||
func scanConservative(addr, len uintptr) {
|
||||
for len >= unsafe.Sizeof(addr) {
|
||||
for addr := start; addr < end; addr += unsafe.Alignof(addr) {
|
||||
root := *(*uintptr)(unsafe.Pointer(addr))
|
||||
markRoot(addr, root)
|
||||
|
||||
addr += unsafe.Alignof(addr)
|
||||
len -= unsafe.Alignof(addr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -611,164 +553,159 @@ func markCurrentGoroutineStack(sp uintptr) {
|
||||
markRoot(0, sp)
|
||||
}
|
||||
|
||||
// finishMark finishes the marking process by scanning all heap objects on scanList.
|
||||
func finishMark() {
|
||||
for {
|
||||
// Remove an object from the scan list.
|
||||
obj := scanList
|
||||
if obj == nil {
|
||||
return
|
||||
}
|
||||
scanList = obj.next
|
||||
// stackOverflow is a flag which is set when the GC scans too deep while marking.
|
||||
// After it is set, all marked allocations must be re-scanned.
|
||||
var stackOverflow bool
|
||||
|
||||
// Check if the object may contain pointers.
|
||||
if obj.layout.pointerFree() {
|
||||
// startMark starts the marking process on a root and all of its children.
|
||||
func startMark(root gcBlock) {
|
||||
var stack [markStackSize]gcBlock
|
||||
stack[0] = root
|
||||
root.setState(blockStateMark)
|
||||
stackLen := 1
|
||||
for stackLen > 0 {
|
||||
// Pop a block off of the stack.
|
||||
stackLen--
|
||||
block := stack[stackLen]
|
||||
if gcDebug {
|
||||
println("stack popped, remaining stack:", stackLen)
|
||||
}
|
||||
|
||||
// Scan all pointers inside the block.
|
||||
scanner := newGCObjectScanner(block)
|
||||
if scanner.pointerFree() {
|
||||
// This object doesn't contain any pointers.
|
||||
// This is a fast path for objects like make([]int, 4096).
|
||||
// It skips the length calculation.
|
||||
continue
|
||||
}
|
||||
start, end := block.address(), block.findNext().address()
|
||||
if preciseHeap {
|
||||
// The first word of the object is just the pointer layout value.
|
||||
// Skip it.
|
||||
start += align(unsafe.Sizeof(uintptr(0)))
|
||||
}
|
||||
for addr := start; addr != end; addr += unsafe.Alignof(addr) {
|
||||
// Load the word.
|
||||
word := *(*uintptr)(unsafe.Pointer(addr))
|
||||
|
||||
// Compute the scan bounds.
|
||||
objAddr := uintptr(unsafe.Pointer(obj))
|
||||
start := objAddr + align(unsafe.Sizeof(objHeader{}))
|
||||
end := blockFromAddr(objAddr).findNext().address()
|
||||
if !scanner.nextIsPointer(word, root.address(), addr) {
|
||||
// Not a heap pointer.
|
||||
continue
|
||||
}
|
||||
|
||||
// Scan the object.
|
||||
obj.layout.scan(start, end-start)
|
||||
// Find the corresponding memory block.
|
||||
referencedBlock := blockFromAddr(word)
|
||||
|
||||
if referencedBlock.state() == blockStateFree {
|
||||
// The to-be-marked object doesn't actually exist.
|
||||
// This is probably a false positive.
|
||||
if gcDebug {
|
||||
println("found reference to free memory:", word, "at:", addr)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Move to the block's head.
|
||||
referencedBlock = referencedBlock.findHead()
|
||||
|
||||
if referencedBlock.state() == blockStateMark {
|
||||
// The block has already been marked by something else.
|
||||
continue
|
||||
}
|
||||
|
||||
// Mark block.
|
||||
if gcDebug {
|
||||
println("marking block:", referencedBlock)
|
||||
}
|
||||
referencedBlock.setState(blockStateMark)
|
||||
|
||||
if stackLen == len(stack) {
|
||||
// The stack is full.
|
||||
// It is necessary to rescan all marked blocks once we are done.
|
||||
stackOverflow = true
|
||||
if gcDebug {
|
||||
println("gc stack overflowed")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Push the pointer onto the stack to be scanned later.
|
||||
stack[stackLen] = referencedBlock
|
||||
stackLen++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// finishMark finishes the marking process by processing all stack overflows.
|
||||
func finishMark() {
|
||||
for stackOverflow {
|
||||
// Re-mark all blocks.
|
||||
stackOverflow = false
|
||||
for block := gcBlock(0); block < endBlock; block++ {
|
||||
if block.state() != blockStateMark {
|
||||
// Block is not marked, so we do not need to rescan it.
|
||||
continue
|
||||
}
|
||||
|
||||
// Re-mark the block.
|
||||
startMark(block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mark a GC root at the address addr.
|
||||
func markRoot(addr, root uintptr) {
|
||||
// Find the heap block corresponding to the root.
|
||||
if !isOnHeap(root) {
|
||||
// This is not a heap pointer.
|
||||
return
|
||||
if isOnHeap(root) {
|
||||
block := blockFromAddr(root)
|
||||
if block.state() == blockStateFree {
|
||||
// The to-be-marked object doesn't actually exist.
|
||||
// This could either be a dangling pointer (oops!) but most likely
|
||||
// just a false positive.
|
||||
return
|
||||
}
|
||||
head := block.findHead()
|
||||
if head.state() != blockStateMark {
|
||||
if gcDebug {
|
||||
println("found unmarked pointer", root, "at address", addr)
|
||||
}
|
||||
startMark(head)
|
||||
}
|
||||
}
|
||||
block := blockFromAddr(root)
|
||||
|
||||
// Find the head of the corresponding object.
|
||||
if block.state() == blockStateFree {
|
||||
// The to-be-marked object doesn't actually exist.
|
||||
// This could either be a dangling pointer (oops!) but most likely
|
||||
// just a false positive.
|
||||
return
|
||||
}
|
||||
head := block.findHead()
|
||||
|
||||
// Mark the object.
|
||||
if head.state() == blockStateMark {
|
||||
// This object is already marked.
|
||||
return
|
||||
}
|
||||
if gcDebug {
|
||||
println("found unmarked pointer", root, "at address", addr)
|
||||
}
|
||||
head.setState(blockStateMark)
|
||||
|
||||
// Add the object to the scan list.
|
||||
header := (*objHeader)(head.pointer())
|
||||
header.next = scanList
|
||||
scanList = header
|
||||
}
|
||||
|
||||
// Sweep goes through all memory and frees unmarked memory.
|
||||
func sweep() {
|
||||
metadataEnd := unsafe.Add(metadataStart, (endBlock+(blocksPerStateByte-1))/blocksPerStateByte)
|
||||
var carry byte
|
||||
for meta := metadataStart; meta != metadataEnd; meta = unsafe.Add(meta, 1) {
|
||||
// Fetch the state byte.
|
||||
stateBytePtr := (*byte)(unsafe.Pointer(meta))
|
||||
stateByte := *stateBytePtr
|
||||
|
||||
// Separate blocks by type.
|
||||
// Split the nibbles.
|
||||
// Each nibble is a mask of blocks.
|
||||
high := stateByte >> blocksPerStateByte
|
||||
low := stateByte & blockStateEach
|
||||
// Marked heads are in both nibbles.
|
||||
markedHeads := low & high
|
||||
// Unmarked heads are in the low nibble but not the high nibble.
|
||||
unmarkedHeads := low &^ high
|
||||
// Tails are in the high nibble but not the low nibble.
|
||||
tails := high &^ low
|
||||
|
||||
// Clear all tail runs after unmarked (freed) heads.
|
||||
//
|
||||
// Adding 1 to the start of a bit run will clear the run and set the next bit:
|
||||
// (2^k - 1) + 1 = 2^k
|
||||
// e.g. 0b0011 + 1 = 0b0100
|
||||
// Bitwise-and with the original mask to clear the newly set bit.
|
||||
// e.g. (0b0011 + 1) & 0b0011 = 0b0100 & 0b0011 = 0b0000
|
||||
// This will not clear bits after the run because the gap stops the carry:
|
||||
// e.g. (0b1011 + 1) & 0b1011 = 0b1100 & 0b1011 = 0b1000
|
||||
// This can clear multiple runs in a single addition:
|
||||
// e.g. (0b1101 + 0b0101) & 0b1101 = 0b10010 & 0b1101 = 0b0000
|
||||
//
|
||||
// In order to find tail run starts after unmarked heads we could use tails & (unmarkedHeads << 1).
|
||||
// It is possible omit the bitwise-and because the clear still works if the next block is not a tail.
|
||||
// A head is not a tail, so corresponding missing tail bit will stop the carry from a previous tail run.
|
||||
// As such it will set the next bit which will be cleared back away later.
|
||||
// e.g. HHTH: (0b0010 + (0b1101 << 1)) & 0b0010 = 0b11100 & 0b0010 = 0b0000
|
||||
//
|
||||
// Treat the whole heap as a single pair of integer masks.
|
||||
// This is accomplished for addition by carrying the overflow to the next state byte.
|
||||
// The unmarkedHeads << 1 is equivalent to unmarkedHeads + unmarkedHeads, so it can be merged with the sum.
|
||||
// This does not require any special work for the bitwise-and because it operates bitwise.
|
||||
tailClear := tails + (unmarkedHeads << 1) + carry
|
||||
carry = tailClear >> blocksPerStateByte
|
||||
tails &= tailClear
|
||||
|
||||
// Construct the new state byte.
|
||||
*stateBytePtr = markedHeads | (tails << blocksPerStateByte)
|
||||
}
|
||||
}
|
||||
|
||||
// buildFreeRanges rebuilds the freeRanges list.
|
||||
// This must be called after a GC sweep or heap grow.
|
||||
// It returns how many bytes are free in the heap.
|
||||
func buildFreeRanges() uintptr {
|
||||
freeRanges = nil
|
||||
block := endBlock
|
||||
var totalBlocks uintptr
|
||||
for {
|
||||
// Skip backwards over occupied blocks.
|
||||
for block > 0 && (block-1).state() != blockStateFree {
|
||||
block--
|
||||
// It returns how many bytes are free in the heap after the sweep.
|
||||
func sweep() (freeBytes uintptr) {
|
||||
freeCurrentObject := false
|
||||
var freed uint64
|
||||
for block := gcBlock(0); block < endBlock; block++ {
|
||||
switch block.state() {
|
||||
case blockStateHead:
|
||||
// Unmarked head. Free it, including all tail blocks following it.
|
||||
block.markFree()
|
||||
freeCurrentObject = true
|
||||
gcFrees++
|
||||
freed++
|
||||
case blockStateTail:
|
||||
if freeCurrentObject {
|
||||
// This is a tail object following an unmarked head.
|
||||
// Free it now.
|
||||
block.markFree()
|
||||
freed++
|
||||
}
|
||||
case blockStateMark:
|
||||
// This is a marked object. The next tail blocks must not be freed,
|
||||
// but the mark bit must be removed so the next GC cycle will
|
||||
// collect this object if it is unreferenced then.
|
||||
block.unmark()
|
||||
freeCurrentObject = false
|
||||
case blockStateFree:
|
||||
freeBytes += bytesPerBlock
|
||||
}
|
||||
if block == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Find the start of the free range.
|
||||
end := block
|
||||
for block > 0 && (block-1).state() == blockStateFree {
|
||||
block--
|
||||
}
|
||||
|
||||
// Insert the free range.
|
||||
len := uintptr(end - block)
|
||||
totalBlocks += len
|
||||
insertFreeRange(block.pointer(), len)
|
||||
}
|
||||
|
||||
if gcDebug {
|
||||
println("free ranges after rebuild:")
|
||||
dumpFreeRangeCounts()
|
||||
}
|
||||
|
||||
return totalBlocks * bytesPerBlock
|
||||
}
|
||||
|
||||
func dumpFreeRangeCounts() {
|
||||
for rangeWithLength := freeRanges; rangeWithLength != nil; rangeWithLength = rangeWithLength.nextLen {
|
||||
totalRanges := uintptr(1)
|
||||
for nextWithLen := rangeWithLength.nextWithLen; nextWithLen != nil; nextWithLen = nextWithLen.next {
|
||||
totalRanges++
|
||||
}
|
||||
println("-", uint(rangeWithLength.len), "x", uint(totalRanges))
|
||||
}
|
||||
gcFreedBlocks += freed
|
||||
freeBytes += uintptr(freed) * bytesPerBlock
|
||||
return
|
||||
}
|
||||
|
||||
// dumpHeap can be used for debugging purposes. It dumps the state of each heap
|
||||
@@ -798,75 +735,28 @@ func dumpHeap() {
|
||||
// call to ReadMemStats. This would not do GC implicitly for you.
|
||||
func ReadMemStats(m *MemStats) {
|
||||
gcLock.Lock()
|
||||
|
||||
// Calculate the raw size of the heap.
|
||||
heapEnd := heapEnd
|
||||
heapStart := heapStart
|
||||
m.Sys = uint64(heapEnd - heapStart)
|
||||
m.HeapSys = uint64(uintptr(metadataStart) - heapStart)
|
||||
metadataStart := metadataStart
|
||||
// TODO: should GCSys include objHeaders?
|
||||
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
|
||||
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||
|
||||
// Count live heads and tails.
|
||||
var liveHeads, liveTails uintptr
|
||||
endBlock := endBlock
|
||||
metadataEnd := unsafe.Add(metadataStart, (endBlock+(blocksPerStateByte-1))/blocksPerStateByte)
|
||||
for meta := metadataStart; meta != metadataEnd; meta = unsafe.Add(meta, 1) {
|
||||
// Since we are outside of a GC, nothing is marked.
|
||||
// A bit in the low nibble implies a head.
|
||||
// A bit in the high nibble implies a tail.
|
||||
stateByte := *(*byte)(unsafe.Pointer(meta))
|
||||
liveHeads += uintptr(count4LUT[stateByte&blockStateEach])
|
||||
liveTails += uintptr(count4LUT[stateByte>>blocksPerStateByte])
|
||||
m.HeapIdle = 0
|
||||
m.HeapInuse = 0
|
||||
for block := gcBlock(0); block < endBlock; block++ {
|
||||
bstate := block.state()
|
||||
if bstate == blockStateFree {
|
||||
m.HeapIdle += uint64(bytesPerBlock)
|
||||
} else {
|
||||
m.HeapInuse += uint64(bytesPerBlock)
|
||||
}
|
||||
}
|
||||
|
||||
// Add heads and tails to count live blocks.
|
||||
liveBlocks := liveHeads + liveTails
|
||||
liveBytes := uint64(liveBlocks * bytesPerBlock)
|
||||
m.HeapInuse = liveBytes
|
||||
m.HeapAlloc = liveBytes
|
||||
m.Alloc = liveBytes
|
||||
|
||||
// Subtract live blocks from total blocks to count free blocks.
|
||||
freeBlocks := uintptr(endBlock) - liveBlocks
|
||||
m.HeapIdle = uint64(freeBlocks * bytesPerBlock)
|
||||
|
||||
// Record the number of allocated objects.
|
||||
gcMallocs := gcMallocs
|
||||
m.Mallocs = gcMallocs
|
||||
|
||||
// Subtract live objects from allocated objects to count freed objects.
|
||||
m.Frees = gcMallocs - uint64(liveHeads)
|
||||
|
||||
// Record the total allocated bytes.
|
||||
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||
m.HeapSys = m.HeapInuse + m.HeapIdle
|
||||
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
|
||||
m.TotalAlloc = gcTotalAlloc
|
||||
|
||||
m.Mallocs = gcMallocs
|
||||
m.Frees = gcFrees
|
||||
m.Sys = uint64(heapEnd - heapStart)
|
||||
m.HeapAlloc = (gcTotalBlocks - gcFreedBlocks) * uint64(bytesPerBlock)
|
||||
m.Alloc = m.HeapAlloc
|
||||
gcLock.Unlock()
|
||||
}
|
||||
|
||||
// count4LUT is a lookup table used to count set bits in a 4-bit mask.
|
||||
// TODO: replace with popcnt when available
|
||||
var count4LUT = [16]uint8{
|
||||
0b0000: 0,
|
||||
0b0001: 1,
|
||||
0b0010: 1,
|
||||
0b0011: 2,
|
||||
0b0100: 1,
|
||||
0b0101: 2,
|
||||
0b0110: 2,
|
||||
0b0111: 3,
|
||||
0b1000: 1,
|
||||
0b1001: 2,
|
||||
0b1010: 2,
|
||||
0b1011: 3,
|
||||
0b1100: 2,
|
||||
0b1101: 3,
|
||||
0b1110: 3,
|
||||
0b1111: 4,
|
||||
}
|
||||
|
||||
func SetFinalizer(obj interface{}, finalizer interface{}) {
|
||||
// Unimplemented.
|
||||
}
|
||||
|
||||
+24
-2
@@ -31,6 +31,10 @@ var zeroSizedAlloc uint8
|
||||
|
||||
var gcLock task.PMutex
|
||||
|
||||
// Normally false, set to true during a GC scan when all other threads get
|
||||
// paused.
|
||||
var needsResumeWorld bool
|
||||
|
||||
func initHeap() {
|
||||
libgc_init()
|
||||
|
||||
@@ -46,6 +50,18 @@ func gcInit()
|
||||
func gcCallback() {
|
||||
// Mark globals and all stacks, and stop the world if we're using threading.
|
||||
gcMarkReachable()
|
||||
|
||||
// If we use a scheduler with parallelism (the threads scheduler for
|
||||
// example), we need to call gcResumeWorld() after scanning has finished.
|
||||
if hasParallelism {
|
||||
if needsResumeWorld {
|
||||
// Should never happen, check for it anyway.
|
||||
runtimePanic("gc: world already stopped")
|
||||
}
|
||||
|
||||
// Note that we need to resume the world after finishing the GC call.
|
||||
needsResumeWorld = true
|
||||
}
|
||||
}
|
||||
|
||||
func markRoots(start, end uintptr) {
|
||||
@@ -71,6 +87,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
}
|
||||
|
||||
gcLock.Lock()
|
||||
needsResumeWorld = false
|
||||
var ptr unsafe.Pointer
|
||||
if layout == gclayout.NoPtrs.AsPtr() {
|
||||
// This object is entirely pointer free, for example make([]int, ...).
|
||||
@@ -87,7 +104,9 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
// Memory returned from libgc_malloc has already been zeroed, so nothing
|
||||
// to do here.
|
||||
}
|
||||
gcResumeWorld()
|
||||
if needsResumeWorld {
|
||||
gcResumeWorld()
|
||||
}
|
||||
gcLock.Unlock()
|
||||
if ptr == nil {
|
||||
runtimePanic("gc: out of memory")
|
||||
@@ -102,8 +121,11 @@ func free(ptr unsafe.Pointer) {
|
||||
|
||||
func GC() {
|
||||
gcLock.Lock()
|
||||
needsResumeWorld = false
|
||||
libgc_gcollect()
|
||||
gcResumeWorld()
|
||||
if needsResumeWorld {
|
||||
gcResumeWorld()
|
||||
}
|
||||
gcLock.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,25 +6,24 @@
|
||||
|
||||
package runtime
|
||||
|
||||
import "unsafe"
|
||||
const preciseHeap = false
|
||||
|
||||
// parseGCLayout stores the layout information passed to alloc into a gcLayout value.
|
||||
// The conservative GC discards this information.
|
||||
func parseGCLayout(layout unsafe.Pointer) gcLayout {
|
||||
return gcLayout{}
|
||||
type gcObjectScanner struct {
|
||||
}
|
||||
|
||||
// gcLayout tracks pointer locations in a heap object.
|
||||
// The conservative GC treats all locations as potential pointers, so this doesn't need to store anything.
|
||||
type gcLayout struct {
|
||||
func newGCObjectScanner(block gcBlock) gcObjectScanner {
|
||||
return gcObjectScanner{}
|
||||
}
|
||||
|
||||
func (l gcLayout) pointerFree() bool {
|
||||
func (scanner *gcObjectScanner) pointerFree() bool {
|
||||
// We don't know whether this object contains pointers, so conservatively
|
||||
// return false.
|
||||
return false
|
||||
}
|
||||
|
||||
func (l gcLayout) scan(start, len uintptr) {
|
||||
scanConservative(start, len)
|
||||
// nextIsPointer returns whether this could be a pointer. Because the GC is
|
||||
// conservative, we can't do much more than check whether the object lies
|
||||
// somewhere in the heap.
|
||||
func (scanner gcObjectScanner) nextIsPointer(ptr, parent, addrOfWord uintptr) bool {
|
||||
return isOnHeap(ptr)
|
||||
}
|
||||
|
||||
+73
-78
@@ -57,96 +57,91 @@ package runtime
|
||||
|
||||
import "unsafe"
|
||||
|
||||
const sizeFieldBits = 4 + (unsafe.Sizeof(uintptr(0)) / 4)
|
||||
const preciseHeap = true
|
||||
|
||||
// parseGCLayout stores the layout information passed to alloc into a gcLayout value.
|
||||
func parseGCLayout(layout unsafe.Pointer) gcLayout {
|
||||
return gcLayout(layout)
|
||||
type gcObjectScanner struct {
|
||||
index uintptr
|
||||
size uintptr
|
||||
bitmap uintptr
|
||||
bitmapAddr unsafe.Pointer
|
||||
}
|
||||
|
||||
// gcLayout tracks pointer locations in a heap object.
|
||||
type gcLayout uintptr
|
||||
func newGCObjectScanner(block gcBlock) gcObjectScanner {
|
||||
if gcAsserts && block != block.findHead() {
|
||||
runtimePanic("gc: object scanner must start at head")
|
||||
}
|
||||
scanner := gcObjectScanner{}
|
||||
layout := *(*uintptr)(unsafe.Pointer(block.address()))
|
||||
if layout == 0 {
|
||||
// Unknown layout. Assume all words in the object could be pointers.
|
||||
// This layout value below corresponds to a slice of pointers like:
|
||||
// make(*byte, N)
|
||||
scanner.size = 1
|
||||
scanner.bitmap = 1
|
||||
} else if layout&1 != 0 {
|
||||
// Layout is stored directly in the integer value.
|
||||
// Determine format of bitfields in the integer.
|
||||
const layoutBits = uint64(unsafe.Sizeof(layout) * 8)
|
||||
var sizeFieldBits uint64
|
||||
switch layoutBits { // note: this switch should be resolved at compile time
|
||||
case 16:
|
||||
sizeFieldBits = 4
|
||||
case 32:
|
||||
sizeFieldBits = 5
|
||||
case 64:
|
||||
sizeFieldBits = 6
|
||||
default:
|
||||
runtimePanic("unknown pointer size")
|
||||
}
|
||||
|
||||
func (layout gcLayout) pointerFree() bool {
|
||||
return layout&1 != 0 && layout>>(sizeFieldBits+1) == 0
|
||||
}
|
||||
|
||||
// scan an object with this element layout.
|
||||
// The starting address must be valid and pointer-aligned.
|
||||
// The length is rounded down to a multiple of the element size.
|
||||
func (layout gcLayout) scan(start, len uintptr) {
|
||||
switch {
|
||||
case layout == 0:
|
||||
// This is an unknown layout.
|
||||
// Scan conservatively.
|
||||
// NOTE: This is *NOT* equivalent to a slice of pointers on AVR.
|
||||
scanConservative(start, len)
|
||||
|
||||
case layout&1 != 0:
|
||||
// The layout is stored directly in the integer value.
|
||||
// Extract the bitfields.
|
||||
size := uintptr(layout>>1) & (1<<sizeFieldBits - 1)
|
||||
mask := uintptr(layout) >> (1 + sizeFieldBits)
|
||||
|
||||
// Scan with the extracted mask.
|
||||
scanSimple(start, len, size*unsafe.Alignof(start), mask)
|
||||
|
||||
default:
|
||||
// The layout is stored seperately in a global object.
|
||||
// Extract the size and bitmap.
|
||||
// Extract values from the bitfields.
|
||||
// See comment at the top of this file for more information.
|
||||
scanner.size = (layout >> 1) & (1<<sizeFieldBits - 1)
|
||||
scanner.bitmap = layout >> (1 + sizeFieldBits)
|
||||
} else {
|
||||
// Layout is stored separately in a global object.
|
||||
layoutAddr := unsafe.Pointer(layout)
|
||||
size := *(*uintptr)(layoutAddr)
|
||||
bitmapPtr := unsafe.Add(layoutAddr, unsafe.Sizeof(uintptr(0)))
|
||||
bitmapLen := (size + 7) / 8
|
||||
bitmap := unsafe.Slice((*byte)(bitmapPtr), bitmapLen)
|
||||
|
||||
// Scan with the bitmap.
|
||||
scanComplex(start, len, size*unsafe.Alignof(start), bitmap)
|
||||
scanner.size = *(*uintptr)(layoutAddr)
|
||||
scanner.bitmapAddr = unsafe.Add(layoutAddr, unsafe.Sizeof(uintptr(0)))
|
||||
}
|
||||
return scanner
|
||||
}
|
||||
|
||||
// scanSimple scans an object with an integer bitmask of pointer locations.
|
||||
// The starting address must be valid and pointer-aligned.
|
||||
func scanSimple(start, len, size, mask uintptr) {
|
||||
for len >= size {
|
||||
// Scan this element.
|
||||
scanWithMask(start, mask)
|
||||
|
||||
// Move to the next element.
|
||||
start += size
|
||||
len -= size
|
||||
func (scanner *gcObjectScanner) pointerFree() bool {
|
||||
if scanner.bitmapAddr != nil {
|
||||
// While the format allows for large objects without pointers, this is
|
||||
// optimized by the compiler so if bitmapAddr is set, we know that there
|
||||
// are at least some pointers in the object.
|
||||
return false
|
||||
}
|
||||
// If the bitmap is zero, there are definitely no pointers in the object.
|
||||
return scanner.bitmap == 0
|
||||
}
|
||||
|
||||
// scanComplex scans an object with a bitmap of pointer locations.
|
||||
// The starting address must be valid and pointer-aligned.
|
||||
func scanComplex(start, len, size uintptr, bitmap []byte) {
|
||||
for len >= size {
|
||||
// Scan this element.
|
||||
for i, mask := range bitmap {
|
||||
addr := start + 8*unsafe.Alignof(start)*uintptr(i)
|
||||
scanWithMask(addr, uintptr(mask))
|
||||
func (scanner *gcObjectScanner) nextIsPointer(word, parent, addrOfWord uintptr) bool {
|
||||
index := scanner.index
|
||||
scanner.index++
|
||||
if scanner.index == scanner.size {
|
||||
scanner.index = 0
|
||||
}
|
||||
|
||||
if !isOnHeap(word) {
|
||||
// Definitely isn't a pointer.
|
||||
return false
|
||||
}
|
||||
|
||||
// Might be a pointer. Now look at the object layout to know for sure.
|
||||
if scanner.bitmapAddr != nil {
|
||||
if (*(*uint8)(unsafe.Add(scanner.bitmapAddr, index/8))>>(index%8))&1 == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// Move to the next element.
|
||||
start += size
|
||||
len -= size
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// scanWithMask scans a portion of an object with a mask of pointer locations.
|
||||
// The address must be valid and pointer-aligned.
|
||||
func scanWithMask(addr, mask uintptr) {
|
||||
// TODO: use ctz when available
|
||||
for mask != 0 {
|
||||
if mask&1 != 0 {
|
||||
// Load and mark this pointer.
|
||||
root := *(*uintptr)(unsafe.Pointer(addr))
|
||||
markRoot(addr, root)
|
||||
}
|
||||
|
||||
// Move to the next offset.
|
||||
mask >>= 1
|
||||
addr += unsafe.Alignof(addr)
|
||||
if (scanner.bitmap>>index)&1 == 0 {
|
||||
// not a pointer!
|
||||
return false
|
||||
}
|
||||
|
||||
// Probably a pointer.
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -29,6 +29,16 @@ type stackChainObject struct {
|
||||
// - The system stack (aka startup stack) is not heap allocated, so even
|
||||
// though it may be referenced it will not be scanned by default.
|
||||
//
|
||||
// Therefore, we only need to scan the system stack.
|
||||
// It is relatively easy to scan the system stack while we're on it: we can
|
||||
// simply read __stack_pointer and __global_base and scan the area in between.
|
||||
// Unfortunately, it's hard to get the system stack pointer while we're on a
|
||||
// goroutine stack. But when we're on a goroutine stack, the system stack is in
|
||||
// the scheduler which means there shouldn't be anything on the system stack
|
||||
// anyway.
|
||||
// ...I hope this assumption holds, otherwise we will need to store the system
|
||||
// stack in a global or something.
|
||||
//
|
||||
// The compiler also inserts code to store all globals in a chain via
|
||||
// stackChainStart. Luckily we don't need to scan these, as these globals are
|
||||
// stored on the goroutine stack and are therefore already getting scanned.
|
||||
@@ -40,18 +50,9 @@ func markStack() {
|
||||
// live.
|
||||
volatile.LoadUint32((*uint32)(unsafe.Pointer(&stackChainStart)))
|
||||
|
||||
// Scan the system stack.
|
||||
var sysSP uintptr
|
||||
if task.OnSystemStack() {
|
||||
// We are on the system stack.
|
||||
// Use the current stack pointer.
|
||||
sysSP = getCurrentStackPointer()
|
||||
} else {
|
||||
// We are in a goroutine.
|
||||
// Use the saved stack pointer.
|
||||
sysSP = savedStackPointer
|
||||
markRoots(getCurrentStackPointer(), stackTop)
|
||||
}
|
||||
markRoots(sysSP, stackTop)
|
||||
}
|
||||
|
||||
// trackPointer is a stub function call inserted by the compiler during IR
|
||||
|
||||
@@ -93,9 +93,6 @@ func runtimePanicAt(addr unsafe.Pointer, msg string) {
|
||||
trap()
|
||||
}
|
||||
if hasReturnAddr {
|
||||
// Note: the string "panic: runtime error at " is also used in
|
||||
// runtime_cortexm_hardfault.go. It is kept the same so that the string
|
||||
// can be deduplicated by the compiler.
|
||||
printstring("panic: runtime error at ")
|
||||
printptr(uintptr(addr) - callInstSize)
|
||||
printstring(": ")
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
//go:build baremetal && (nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt) || rp2040 || rp2350)
|
||||
//go:build baremetal && (nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt))
|
||||
|
||||
// If you update the above build constraint, you'll probably also need to update
|
||||
// src/crypto/rand/rand_baremetal.go.
|
||||
//
|
||||
// The rp2040 and rp2350 implementations are not included in src/crypto/rand/rand_baremetal.go
|
||||
// due to not being sufficiently random for the Go crypto libs.
|
||||
// However since the randomness here does not provide those same guarantees,
|
||||
// they are included in the list for hardwareRand() implementations.
|
||||
|
||||
package runtime
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build baremetal && !(nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt) || rp2040 || rp2350)
|
||||
//go:build baremetal && !(nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt))
|
||||
|
||||
package runtime
|
||||
|
||||
|
||||
@@ -57,12 +57,6 @@ func memzero(ptr unsafe.Pointer, size uintptr)
|
||||
// the current stack pointer in a platform-independent way.
|
||||
func stacksave() unsafe.Pointer
|
||||
|
||||
// Special LLVM intrinsic that returns the SP register on entry to the calling
|
||||
// function.
|
||||
//
|
||||
//export llvm.sponentry.p0
|
||||
func llvm_sponentry() unsafe.Pointer
|
||||
|
||||
//export strlen
|
||||
func strlen(ptr unsafe.Pointer) uintptr
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"device/arm"
|
||||
"device/sam"
|
||||
"machine"
|
||||
_ "machine/usb/cdc"
|
||||
"machine/usb/cdc"
|
||||
"runtime/interrupt"
|
||||
"runtime/volatile"
|
||||
"unsafe"
|
||||
@@ -26,6 +26,8 @@ func init() {
|
||||
initUSBClock()
|
||||
initADCClock()
|
||||
|
||||
cdc.EnableUSBCDC()
|
||||
machine.USBDev.Configure(machine.UARTConfig{})
|
||||
machine.InitSerial()
|
||||
}
|
||||
|
||||
@@ -271,9 +273,6 @@ func nanosecondsToTicks(ns int64) timeUnit {
|
||||
func sleepTicks(d timeUnit) {
|
||||
for d != 0 {
|
||||
ticks := uint32(d)
|
||||
if d > 0xffff_ffff {
|
||||
ticks = 0xffff_ffff
|
||||
}
|
||||
if !timerSleep(ticks) {
|
||||
// Bail out early to handle a non-time interrupt.
|
||||
return
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"device/arm"
|
||||
"device/sam"
|
||||
"machine"
|
||||
_ "machine/usb/cdc"
|
||||
"machine/usb/cdc"
|
||||
"runtime/interrupt"
|
||||
"runtime/volatile"
|
||||
)
|
||||
@@ -27,6 +27,8 @@ func init() {
|
||||
initADCClock()
|
||||
enableCache()
|
||||
|
||||
cdc.EnableUSBCDC()
|
||||
machine.USBDev.Configure(machine.UARTConfig{})
|
||||
machine.InitSerial()
|
||||
}
|
||||
|
||||
@@ -264,9 +266,6 @@ func nanosecondsToTicks(ns int64) timeUnit {
|
||||
func sleepTicks(d timeUnit) {
|
||||
for d != 0 {
|
||||
ticks := uint32(d)
|
||||
if d > 0xffff_ffff {
|
||||
ticks = 0xffff_ffff
|
||||
}
|
||||
if !timerSleep(ticks) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,25 +2,39 @@
|
||||
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// This function is called at HardFault.
|
||||
// Before this function is called, the stack pointer is reset to the initial
|
||||
// stack pointer (loaded from address 0x0) and the previous stack pointer is
|
||||
// passed as an argument to this function. This allows for easy inspection of
|
||||
// the stack the moment a HardFault occurs, but it means that the stack will be
|
||||
// corrupted by this function and thus this handler must not attempt to recover.
|
||||
//
|
||||
// For details, see:
|
||||
// https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault
|
||||
// https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
|
||||
//
|
||||
//export HardFault_Handler
|
||||
func HardFault_Handler() {
|
||||
// Obtain the stack pointer as it was on entry to the HardFault. It contains
|
||||
// the registers that were pushed by the NVIC and that we can now read back
|
||||
// to print the PC value at the time of the hard fault, for example.
|
||||
sp := (*interruptStack)(llvm_sponentry())
|
||||
|
||||
// Note: by reusing the string "panic: runtime error at " we save a little
|
||||
// bit in terms of code size as the string can be deduplicated.
|
||||
print("panic: runtime error at ", sp.PC, ": HardFault with sp=", sp)
|
||||
// TODO: try to find the cause of the hard fault. Especially on Cortex-M3
|
||||
// and higher it is possible to find more detailed information in special
|
||||
// status registers.
|
||||
//export handleHardFault
|
||||
func handleHardFault(sp *interruptStack) {
|
||||
print("fatal error: ")
|
||||
if uintptr(unsafe.Pointer(sp)) < 0x20000000 {
|
||||
print("stack overflow")
|
||||
} else {
|
||||
// TODO: try to find the cause of the hard fault. Especially on
|
||||
// Cortex-M3 and higher it is possible to find more detailed information
|
||||
// in special status registers.
|
||||
print("HardFault")
|
||||
}
|
||||
print(" with sp=", sp)
|
||||
if uintptr(unsafe.Pointer(&sp.PC)) >= 0x20000000 {
|
||||
// Only print the PC if it points into memory.
|
||||
// It may not point into memory during a stack overflow, so check that
|
||||
// first before accessing the stack.
|
||||
print(" pc=", sp.PC)
|
||||
}
|
||||
println()
|
||||
abort()
|
||||
}
|
||||
|
||||
@@ -18,13 +18,8 @@ const (
|
||||
|
||||
// See runtime_cortexm_hardfault.go
|
||||
//
|
||||
//export HardFault_Handler
|
||||
func HardFault_Handler() {
|
||||
// Obtain the stack pointer as it was on entry to the HardFault. It contains
|
||||
// the registers that were pushed by the NVIC and that we can now read back
|
||||
// to print the PC value at the time of the hard fault, for example.
|
||||
sp := (*interruptStack)(llvm_sponentry())
|
||||
|
||||
//go:export handleHardFault
|
||||
func handleHardFault(sp *interruptStack) {
|
||||
fault := GetFaultStatus()
|
||||
spValid := !fault.Bus().ImpreciseDataBusError()
|
||||
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
//go:build esp32s3
|
||||
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"device/esp"
|
||||
)
|
||||
|
||||
// This is the function called on startup after the flash (IROM/DROM) is
|
||||
// initialized and the stack pointer has been set.
|
||||
//
|
||||
//export main
|
||||
func main() {
|
||||
// This initialization configures the following things:
|
||||
// * It disables all watchdog timers. They might be useful at some point in
|
||||
// the future, but will need integration into the scheduler. For now,
|
||||
// they're all disabled.
|
||||
// * It sets the CPU frequency to 240MHz, which is the maximum speed allowed
|
||||
// for this CPU. Lower frequencies might be possible in the future, but
|
||||
// running fast and sleeping quickly is often also a good strategy to save
|
||||
// power.
|
||||
// TODO: protect certain memory regions, especially the area below the stack
|
||||
// to protect against stack overflows. See
|
||||
// esp_cpu_configure_region_protection in ESP-IDF.
|
||||
|
||||
// Disable RTC watchdog.
|
||||
esp.RTC_CNTL.WDTWPROTECT.Set(0x50D83AA1)
|
||||
esp.RTC_CNTL.WDTCONFIG0.Set(0)
|
||||
esp.RTC_CNTL.WDTWPROTECT.Set(0x0) // Re-enable write protect
|
||||
|
||||
// Disable Timer 0 watchdog.
|
||||
esp.TIMG1.WDTWPROTECT.Set(0x50D83AA1) // write protect
|
||||
esp.TIMG1.WDTCONFIG0.Set(0) // disable TG0 WDT
|
||||
esp.TIMG1.WDTWPROTECT.Set(0x0) // Re-enable write protect
|
||||
|
||||
esp.TIMG0.WDTWPROTECT.Set(0x50D83AA1) // write protect
|
||||
esp.TIMG0.WDTCONFIG0.Set(0) // disable TG0 WDT
|
||||
esp.TIMG0.WDTWPROTECT.Set(0x0) // Re-enable write protect
|
||||
|
||||
// Disable super watchdog.
|
||||
esp.RTC_CNTL.SWD_WPROTECT.Set(0x8F1D312A)
|
||||
esp.RTC_CNTL.SWD_CONF.Set(esp.RTC_CNTL_SWD_CONF_SWD_DISABLE)
|
||||
esp.RTC_CNTL.SWD_WPROTECT.Set(0x0) // Re-enable write protect
|
||||
|
||||
// Change CPU frequency from 20MHz to 80MHz, by switching from the XTAL to the
|
||||
// PLL clock source (see table "CPU Clock Frequency" in the reference manual).
|
||||
esp.SYSTEM.SetSYSCLK_CONF_SOC_CLK_SEL(1)
|
||||
|
||||
// Change CPU frequency from 80MHz to 240MHz by setting SYSTEM_PLL_FREQ_SEL to
|
||||
// 1 and SYSTEM_CPUPERIOD_SEL to 2 (see table "CPU Clock Frequency" in the
|
||||
// reference manual).
|
||||
esp.SYSTEM.SetCPU_PER_CONF_PLL_FREQ_SEL(1)
|
||||
esp.SYSTEM.SetCPU_PER_CONF_CPUPERIOD_SEL(2)
|
||||
|
||||
// Clear bss. Repeat many times while we wait for cpu/clock to stabilize
|
||||
for x := 0; x < 30; x++ {
|
||||
clearbss()
|
||||
}
|
||||
|
||||
// Initialize main system timer used for time.Now.
|
||||
initTimer()
|
||||
|
||||
// Initialize the heap, call main.main, etc.
|
||||
run()
|
||||
|
||||
// Fallback: if main ever returns, hang the CPU.
|
||||
exit(0)
|
||||
}
|
||||
|
||||
func abort() {
|
||||
// lock up forever
|
||||
print("abort called\n")
|
||||
}
|
||||
|
||||
//go:extern _vector_table
|
||||
var _vector_table [0]uintptr
|
||||
|
||||
//go:extern _sbss
|
||||
var _sbss [0]byte
|
||||
|
||||
//go:extern _ebss
|
||||
var _ebss [0]byte
|
||||
@@ -1,86 +0,0 @@
|
||||
//go:build esp32s3
|
||||
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"device/esp"
|
||||
"machine"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//type timeUnit int64
|
||||
|
||||
func putchar(c byte) {
|
||||
machine.Serial.WriteByte(c)
|
||||
}
|
||||
|
||||
func getchar() byte {
|
||||
for machine.Serial.Buffered() == 0 {
|
||||
Gosched()
|
||||
}
|
||||
v, _ := machine.Serial.ReadByte()
|
||||
return v
|
||||
}
|
||||
|
||||
func buffered() int {
|
||||
return machine.Serial.Buffered()
|
||||
}
|
||||
|
||||
// Initialize .bss: zero-initialized global variables.
|
||||
// The .data section has already been loaded by the ROM bootloader.
|
||||
func clearbss() {
|
||||
ptr := unsafe.Pointer(&_sbss)
|
||||
for ptr != unsafe.Pointer(&_ebss) {
|
||||
*(*uint32)(ptr) = 0
|
||||
ptr = unsafe.Add(ptr, 4)
|
||||
}
|
||||
}
|
||||
|
||||
func initTimer() {
|
||||
// Configure timer 0 in timer group 0, for timekeeping.
|
||||
// EN: Enable the timer.
|
||||
// INCREASE: Count up every tick (as opposed to counting down).
|
||||
// DIVIDER: 16-bit prescaler, set to 2 for dividing the APB clock by two
|
||||
// (40MHz).
|
||||
// esp.TIMG0.T0CONFIG.Set(0 << esp.TIMG_T0CONFIG_T0_EN_Pos)
|
||||
esp.TIMG0.T0CONFIG.Set(esp.TIMG_TCONFIG_EN | esp.TIMG_TCONFIG_INCREASE | 2<<esp.TIMG_TCONFIG_DIVIDER_Pos)
|
||||
// esp.TIMG0.T0CONFIG.Set(1 << esp.TIMG_T0CONFIG_T0_DIVCNT_RST_Pos)
|
||||
// esp.TIMG0.T0CONFIG.Set(esp.TIMG_T0CONFIG_T0_EN)
|
||||
|
||||
// Set the timer counter value to 0.
|
||||
esp.TIMG0.T0LOADLO.Set(0)
|
||||
esp.TIMG0.T0LOADHI.Set(0)
|
||||
esp.TIMG0.T0LOAD.Set(0) // value doesn't matter.
|
||||
}
|
||||
|
||||
func ticks() timeUnit {
|
||||
// First, update the LO and HI register pair by writing any value to the
|
||||
// register. This allows reading the pair atomically.
|
||||
esp.TIMG0.T0UPDATE.Set(0)
|
||||
// Then read the two 32-bit parts of the timer.
|
||||
return timeUnit(uint64(esp.TIMG0.T0LO.Get()) | uint64(esp.TIMG0.T0HI.Get())<<32)
|
||||
}
|
||||
|
||||
func nanosecondsToTicks(ns int64) timeUnit {
|
||||
// Calculate the number of ticks from the number of nanoseconds. At a 80MHz
|
||||
// APB clock, that's 25 nanoseconds per tick with a timer prescaler of 2:
|
||||
// 25 = 1e9 / (80MHz / 2)
|
||||
return timeUnit(ns / 25)
|
||||
}
|
||||
|
||||
func ticksToNanoseconds(ticks timeUnit) int64 {
|
||||
// See nanosecondsToTicks.
|
||||
return int64(ticks) * 25
|
||||
}
|
||||
|
||||
// sleepTicks busy-waits until the given number of ticks have passed.
|
||||
func sleepTicks(d timeUnit) {
|
||||
sleepUntil := ticks() + d
|
||||
for ticks() < sleepUntil {
|
||||
// TODO: suspend the CPU to not burn power here unnecessarily.
|
||||
}
|
||||
}
|
||||
|
||||
func exit(code int) {
|
||||
abort()
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build nrf
|
||||
//go:build nrf && !nrf52840
|
||||
|
||||
package runtime
|
||||
|
||||
@@ -78,9 +78,6 @@ func buffered() int {
|
||||
func sleepTicks(d timeUnit) {
|
||||
for d != 0 {
|
||||
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
|
||||
if d > 0x7fffff {
|
||||
ticks = 0x7fffff
|
||||
}
|
||||
rtc_sleep(ticks)
|
||||
d -= timeUnit(ticks)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,147 @@
|
||||
|
||||
package runtime
|
||||
|
||||
// This package needs to be present so that the machine package can go:linkname
|
||||
// EnableUSBCDC from it.
|
||||
import _ "machine/usb/cdc"
|
||||
import (
|
||||
"device/arm"
|
||||
"device/nrf"
|
||||
"machine"
|
||||
"machine/usb/cdc"
|
||||
"runtime/interrupt"
|
||||
"runtime/volatile"
|
||||
)
|
||||
|
||||
//go:linkname systemInit SystemInit
|
||||
func systemInit()
|
||||
|
||||
//export Reset_Handler
|
||||
func main() {
|
||||
if nrf.FPUPresent {
|
||||
arm.SCB.CPACR.Set(0) // disable FPU if it is enabled
|
||||
}
|
||||
systemInit()
|
||||
preinit()
|
||||
run()
|
||||
exit(0)
|
||||
}
|
||||
|
||||
func init() {
|
||||
cdc.EnableUSBCDC()
|
||||
machine.USBDev.Configure(machine.UARTConfig{})
|
||||
machine.InitSerial()
|
||||
initLFCLK()
|
||||
initRTC()
|
||||
}
|
||||
|
||||
func initLFCLK() {
|
||||
if machine.HasLowFrequencyCrystal {
|
||||
nrf.CLOCK.LFCLKSRC.Set(nrf.CLOCK_LFCLKSTAT_SRC_Xtal)
|
||||
}
|
||||
nrf.CLOCK.TASKS_LFCLKSTART.Set(1)
|
||||
for nrf.CLOCK.EVENTS_LFCLKSTARTED.Get() == 0 {
|
||||
}
|
||||
nrf.CLOCK.EVENTS_LFCLKSTARTED.Set(0)
|
||||
}
|
||||
|
||||
func initRTC() {
|
||||
nrf.RTC1.TASKS_START.Set(1)
|
||||
intr := interrupt.New(nrf.IRQ_RTC1, func(intr interrupt.Interrupt) {
|
||||
if nrf.RTC1.EVENTS_COMPARE[0].Get() != 0 {
|
||||
nrf.RTC1.EVENTS_COMPARE[0].Set(0)
|
||||
nrf.RTC1.INTENCLR.Set(nrf.RTC_INTENSET_COMPARE0)
|
||||
nrf.RTC1.EVENTS_COMPARE[0].Set(0)
|
||||
rtc_wakeup.Set(1)
|
||||
}
|
||||
if nrf.RTC1.EVENTS_OVRFLW.Get() != 0 {
|
||||
nrf.RTC1.EVENTS_OVRFLW.Set(0)
|
||||
rtcOverflows.Set(rtcOverflows.Get() + 1)
|
||||
}
|
||||
})
|
||||
nrf.RTC1.INTENSET.Set(nrf.RTC_INTENSET_OVRFLW)
|
||||
intr.SetPriority(0xc0) // low priority
|
||||
intr.Enable()
|
||||
}
|
||||
|
||||
func putchar(c byte) {
|
||||
machine.Serial.WriteByte(c)
|
||||
}
|
||||
|
||||
func getchar() byte {
|
||||
for machine.Serial.Buffered() == 0 {
|
||||
Gosched()
|
||||
}
|
||||
v, _ := machine.Serial.ReadByte()
|
||||
return v
|
||||
}
|
||||
|
||||
func buffered() int {
|
||||
return machine.Serial.Buffered()
|
||||
}
|
||||
|
||||
func sleepTicks(d timeUnit) {
|
||||
for d != 0 {
|
||||
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
|
||||
rtc_sleep(ticks)
|
||||
d -= timeUnit(ticks)
|
||||
}
|
||||
}
|
||||
|
||||
var rtcOverflows volatile.Register32 // number of times the RTC wrapped around
|
||||
|
||||
// ticksToNanoseconds converts RTC ticks (at 32768Hz) to nanoseconds.
|
||||
func ticksToNanoseconds(ticks timeUnit) int64 {
|
||||
// The following calculation is actually the following, but with both sides
|
||||
// reduced to reduce the risk of overflow:
|
||||
// ticks * 1e9 / 32768
|
||||
return int64(ticks) * 1953125 / 64
|
||||
}
|
||||
|
||||
// nanosecondsToTicks converts nanoseconds to RTC ticks (running at 32768Hz).
|
||||
func nanosecondsToTicks(ns int64) timeUnit {
|
||||
// The following calculation is actually the following, but with both sides
|
||||
// reduced to reduce the risk of overflow:
|
||||
// ns * 32768 / 1e9
|
||||
return timeUnit(ns * 64 / 1953125)
|
||||
}
|
||||
|
||||
// Monotonically increasing number of ticks since start.
|
||||
func ticks() timeUnit {
|
||||
// For some ways of capturing the time atomically, see this thread:
|
||||
// https://www.eevblog.com/forum/microcontrollers/correct-timing-by-timer-overflow-count/msg749617/#msg749617
|
||||
// Here, instead of re-reading the counter register if an overflow has been
|
||||
// detected, we simply try again because that results in (slightly) smaller
|
||||
// code and is perhaps easier to prove correct.
|
||||
for {
|
||||
mask := interrupt.Disable()
|
||||
counter := uint32(nrf.RTC1.COUNTER.Get())
|
||||
overflows := rtcOverflows.Get()
|
||||
hasOverflow := nrf.RTC1.EVENTS_OVRFLW.Get() != 0
|
||||
interrupt.Restore(mask)
|
||||
|
||||
if hasOverflow {
|
||||
// There was an overflow. Try again.
|
||||
continue
|
||||
}
|
||||
|
||||
// The counter is 24 bits in size, so the number of overflows form the
|
||||
// upper 32 bits (together 56 bits, which covers 71493 years at
|
||||
// 32768kHz: I'd argue good enough for most purposes).
|
||||
return timeUnit(overflows)<<24 + timeUnit(counter)
|
||||
}
|
||||
}
|
||||
|
||||
var rtc_wakeup volatile.Register8
|
||||
|
||||
func rtc_sleep(ticks uint32) {
|
||||
nrf.RTC1.INTENSET.Set(nrf.RTC_INTENSET_COMPARE0)
|
||||
rtc_wakeup.Set(0)
|
||||
if ticks == 1 {
|
||||
// Race condition (even in hardware) at ticks == 1.
|
||||
// TODO: fix this in a better way by detecting it, like the manual
|
||||
// describes.
|
||||
ticks = 2
|
||||
}
|
||||
nrf.RTC1.CC[0].Set((nrf.RTC1.COUNTER.Get() + ticks) & 0x00ffffff)
|
||||
for rtc_wakeup.Get() == 0 {
|
||||
waitForEvents()
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user