Skip to content

move tinygo compatibility code #7

@luoliwoshang

Description

@luoliwoshang

move the device package update as a same pr at current repo.

The pr description at goplus/lib#16

Overview

This PR completes the migration of TinyGo v0.39.0 src/device package to goplus/lib/emb, providing embedded development support for the LLGO compiler ecosystem.

Major Changes

1. Device Package Migration

  • Migration Source: TinyGo v0.39.0 src/device package
  • Migration Scope: Migrated 17 architecture directories from TinyGo to emb/device/
  • File Count: ~1000+ device definition files covering major embedded architectures
  • Supported Architectures: ARM, ARM64, AVR, ESP, NRF, NXP, RP, SAM, STM32, Renesas, SiFive, Kendryte, RISC-V, GBA, TKey

Import Path Standardization

// Before migration
import "runtime/volatile"
import "device/stm32"

// After migration  
import "github.com/goplus/lib/emb/runtime/volatile"
import "github.com/goplus/lib/emb/device/stm32"
  • Impact Scope: 573 files updated with new import paths
  • Compatibility: Full integration with existing emb/runtime/volatile package

Compiler Intrinsic Adaptations

Inline Assembly Functions

Updated 5 files to be LLGO-compatible using //llgo:link directives:

// TinyGo (compiler built-in)
func Asm(asm string)

// LLGO adaptation
//llgo:link Asm llgo.asm
func Asm(asm string) {}
  • emb/device/asm.go - Generic inline assembly
  • emb/device/arm/arm.go - ARM architecture
  • emb/device/arm64/arm64.go - ARM64 architecture
  • emb/device/riscv/riscv.go - RISC-V architecture
  • emb/device/avr/avr.go - AVR architecture

Runtime Interrupt Handler Placeholders

Added placeholder implementations for runtime/interrupt.callHandlers in 567 device files to resolve compilation errors:

// Problematic (missing runtime, causes compilation errors)
//go:linkname callHandlers runtime/interrupt.callHandlers
func callHandlers(num int)

// Placeholder implementation (resolves compilation errors, but functionality not implemented)
// NOTE(zzy): runtime/interrupt.callHandlers is not yet implemented in LLGO
// Original linkname: //go:linkname callHandlers runtime/interrupt.callHandlers
func callHandlers(num int) {
    // TODO: implement interrupt handler dispatch mechanism
    // TinyGo implementation reference: runtime/interrupt package
}

ARM System Calls (SVCall)

Added placeholder implementations for ARM Cortex-M system calls:

func SVCall0(num uintptr) uintptr {
    // TODO(zzy): implement system call (SVCall) functionality for ARM Cortex-M
    // TinyGo compiler implementation: tinygo/compiler/inlineasm.go emitSV64Call function
    panic("TODO: SVCall0")
    return 0
}
// Similar for SVCall1, SVCall2, SVCall3, SVCall4

Related Issue: goplus/lib#18

RISC-V CSR Operations

Added placeholder implementations for Control Status Register operations:

func CSRReadUint(csr int) uint {
    // TODO(zzy): implement RISC-V CSR read operation
    // TinyGo compiler implementation: tinygo/compiler/inlineasm.go
    panic("TODO: CSRReadUint")
    return 0
}

Related Issue: goplus/lib#17

5. ARM Interrupt Placeholders

Documented existing LLGO interrupt control functions:

// DisableInterrupts/EnableInterrupts functions available in LLGO
// Implementation location: llgo/targets/device/arm/interrupts.c
// NOTE(zzy): Available for future linkage via //go:linkname
//go:linkname DisableInterrupts DisableInterrupts
func DisableInterrupts() uintptr

//go:linkname EnableInterrupts EnableInterrupts  
func EnableInterrupts(mask uintptr)

Package Validation Status

Most migrated device packages compile successfully. Three packages are excluded from validation:

  • device/riscv: picolibc compilation failure
  • device/kendryte: RISC-V64 not supported by LLGO
  • device/sifive: RISC-V32 not supported by LLGO
  • device/renesas: no corresponding LLGO target found

llgo maybe need llgo:export directive to support exporting functions with different symbol names goplus/llgo#1377

//export LPSPI2_IRQHandler
func interruptLPSPI2() {
    callHandlers(IRQ_LPSPI2)
}

current will got warning

==> //export LPSPI2_IRQHandler LPSPI2_IRQHandler
llgo: linkname LPSPI2_IRQHandler not found and ignored

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions