Skip to content

Commit fb3d98c

Browse files
committed
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.
1 parent 0206645 commit fb3d98c

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

builder/library.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,27 +149,24 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
149149
if config.ABI() != "" {
150150
args = append(args, "-mabi="+config.ABI())
151151
}
152-
if strings.HasPrefix(target, "arm") || strings.HasPrefix(target, "thumb") {
152+
switch compileopts.CanonicalArchName(target) {
153+
case "arm":
153154
if strings.Split(target, "-")[2] == "linux" {
154155
args = append(args, "-fno-unwind-tables", "-fno-asynchronous-unwind-tables")
155156
} else {
156157
args = append(args, "-fshort-enums", "-fomit-frame-pointer", "-mfloat-abi=soft", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables")
157158
}
158-
}
159-
if strings.HasPrefix(target, "avr") {
159+
case "avr":
160160
// AVR defaults to C float and double both being 32-bit. This deviates
161161
// from what most code (and certainly compiler-rt) expects. So we need
162162
// to force the compiler to use 64-bit floating point numbers for
163163
// double.
164164
args = append(args, "-mdouble=64")
165-
}
166-
if strings.HasPrefix(target, "riscv32-") {
165+
case "riscv32":
167166
args = append(args, "-march=rv32imac", "-fforce-enable-int128")
168-
}
169-
if strings.HasPrefix(target, "riscv64-") {
167+
case "riscv64":
170168
args = append(args, "-march=rv64gc")
171-
}
172-
if strings.HasPrefix(target, "mips") {
169+
case "mips":
173170
args = append(args, "-fno-pic")
174171
}
175172

compileopts/config.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,13 @@ func (c *Config) RP2040BootPatch() bool {
207207
return false
208208
}
209209

210-
// MuslArchitecture returns the architecture name as used in musl libc. It is
211-
// usually the same as the first part of the LLVM triple, but not always.
212-
func MuslArchitecture(triple string) string {
210+
// Return a canonicalized architecture name, so we don't have to deal with arm*
211+
// vs thumb* vs arm64.
212+
func CanonicalArchName(triple string) string {
213213
arch := strings.Split(triple, "-")[0]
214+
if arch == "arm64" {
215+
return "aarch64"
216+
}
214217
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
215218
return "arm"
216219
}
@@ -220,6 +223,12 @@ func MuslArchitecture(triple string) string {
220223
return arch
221224
}
222225

226+
// MuslArchitecture returns the architecture name as used in musl libc. It is
227+
// usually the same as the first part of the LLVM triple, but not always.
228+
func MuslArchitecture(triple string) string {
229+
return CanonicalArchName(triple)
230+
}
231+
223232
// LibcPath returns the path to the libc directory. The libc path will be either
224233
// a precompiled libc shipped with a TinyGo build, or a libc path in the cache
225234
// directory (which might not yet be built).

compiler/llvm.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/big"
88
"strings"
99

10+
"github.com/tinygo-org/tinygo/compileopts"
1011
"github.com/tinygo-org/tinygo/compiler/llvmutil"
1112
"tinygo.org/x/go-llvm"
1213
)
@@ -422,17 +423,7 @@ func (c *compilerContext) getPointerBitmap(typ llvm.Type, pos token.Pos) *big.In
422423
// architecture names ("armv6", "thumbv7m", etc) merged into a single
423424
// architecture name ("arm").
424425
func (c *compilerContext) archFamily() string {
425-
arch := strings.Split(c.Triple, "-")[0]
426-
if strings.HasPrefix(arch, "arm64") {
427-
return "aarch64"
428-
}
429-
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
430-
return "arm"
431-
}
432-
if arch == "mipsel" {
433-
return "mips"
434-
}
435-
return arch
426+
return compileopts.CanonicalArchName(c.Triple)
436427
}
437428

438429
// isThumb returns whether we're in ARM or in Thumb mode. It panics if the

0 commit comments

Comments
 (0)