compileopts: add CanonicalArchName to centralize arch detection

It's possible to detect the architecture from the target triple, but
there are a number of exceptions that make it unpleasant to use for this
purpose. There are just too many weird exceptions (like mips vs mipsel,
and armv6m vs thumv6m vs arm64 vs aarch64) so it's better to centralize
these to canonical architecture names.

I picked the architecture names that happen to match the musl
architecture names, because those seem the most natural to me.
This commit is contained in:
Ayke van Laethem
2024-08-06 12:44:01 +02:00
committed by Ayke
parent 020664591a
commit fb3d98ce6e
3 changed files with 20 additions and 23 deletions
+6 -9
View File
@@ -149,27 +149,24 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
if config.ABI() != "" { if config.ABI() != "" {
args = append(args, "-mabi="+config.ABI()) args = append(args, "-mabi="+config.ABI())
} }
if strings.HasPrefix(target, "arm") || strings.HasPrefix(target, "thumb") { switch compileopts.CanonicalArchName(target) {
case "arm":
if strings.Split(target, "-")[2] == "linux" { if strings.Split(target, "-")[2] == "linux" {
args = append(args, "-fno-unwind-tables", "-fno-asynchronous-unwind-tables") args = append(args, "-fno-unwind-tables", "-fno-asynchronous-unwind-tables")
} else { } else {
args = append(args, "-fshort-enums", "-fomit-frame-pointer", "-mfloat-abi=soft", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables") args = append(args, "-fshort-enums", "-fomit-frame-pointer", "-mfloat-abi=soft", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables")
} }
} case "avr":
if strings.HasPrefix(target, "avr") {
// AVR defaults to C float and double both being 32-bit. This deviates // AVR defaults to C float and double both being 32-bit. This deviates
// from what most code (and certainly compiler-rt) expects. So we need // from what most code (and certainly compiler-rt) expects. So we need
// to force the compiler to use 64-bit floating point numbers for // to force the compiler to use 64-bit floating point numbers for
// double. // double.
args = append(args, "-mdouble=64") args = append(args, "-mdouble=64")
} case "riscv32":
if strings.HasPrefix(target, "riscv32-") {
args = append(args, "-march=rv32imac", "-fforce-enable-int128") args = append(args, "-march=rv32imac", "-fforce-enable-int128")
} case "riscv64":
if strings.HasPrefix(target, "riscv64-") {
args = append(args, "-march=rv64gc") args = append(args, "-march=rv64gc")
} case "mips":
if strings.HasPrefix(target, "mips") {
args = append(args, "-fno-pic") args = append(args, "-fno-pic")
} }
+12 -3
View File
@@ -207,10 +207,13 @@ func (c *Config) RP2040BootPatch() bool {
return false return false
} }
// MuslArchitecture returns the architecture name as used in musl libc. It is // Return a canonicalized architecture name, so we don't have to deal with arm*
// usually the same as the first part of the LLVM triple, but not always. // vs thumb* vs arm64.
func MuslArchitecture(triple string) string { func CanonicalArchName(triple string) string {
arch := strings.Split(triple, "-")[0] arch := strings.Split(triple, "-")[0]
if arch == "arm64" {
return "aarch64"
}
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") { if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
return "arm" return "arm"
} }
@@ -220,6 +223,12 @@ func MuslArchitecture(triple string) string {
return arch return arch
} }
// MuslArchitecture returns the architecture name as used in musl libc. It is
// usually the same as the first part of the LLVM triple, but not always.
func MuslArchitecture(triple string) string {
return CanonicalArchName(triple)
}
// LibcPath returns the path to the libc directory. The libc path will be either // LibcPath returns the path to the libc directory. The libc path will be either
// a precompiled libc shipped with a TinyGo build, or a libc path in the cache // a precompiled libc shipped with a TinyGo build, or a libc path in the cache
// directory (which might not yet be built). // directory (which might not yet be built).
+2 -11
View File
@@ -7,6 +7,7 @@ import (
"math/big" "math/big"
"strings" "strings"
"github.com/tinygo-org/tinygo/compileopts"
"github.com/tinygo-org/tinygo/compiler/llvmutil" "github.com/tinygo-org/tinygo/compiler/llvmutil"
"tinygo.org/x/go-llvm" "tinygo.org/x/go-llvm"
) )
@@ -422,17 +423,7 @@ func (c *compilerContext) getPointerBitmap(typ llvm.Type, pos token.Pos) *big.In
// architecture names ("armv6", "thumbv7m", etc) merged into a single // architecture names ("armv6", "thumbv7m", etc) merged into a single
// architecture name ("arm"). // architecture name ("arm").
func (c *compilerContext) archFamily() string { func (c *compilerContext) archFamily() string {
arch := strings.Split(c.Triple, "-")[0] return compileopts.CanonicalArchName(c.Triple)
if strings.HasPrefix(arch, "arm64") {
return "aarch64"
}
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
return "arm"
}
if arch == "mipsel" {
return "mips"
}
return arch
} }
// isThumb returns whether we're in ARM or in Thumb mode. It panics if the // isThumb returns whether we're in ARM or in Thumb mode. It panics if the