Skip to content

Commit 6d4dfcf

Browse files
committed
compiler, runtime: move constants into shared package
Use a single package for certain constants that must be the same between the compiler and the runtime. While just using the same values in both places works, this is much more obvious and harder to mess up. It also avoids the need for comments pointing to the other location the constant is defined. And having it in code makes it possible for IDEs to analyze the source. In the future, more such constants and maybe algorithms can be added.
1 parent ac9f72b commit 6d4dfcf

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

compiler/compiler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/tinygo-org/tinygo/compiler/llvmutil"
1919
"github.com/tinygo-org/tinygo/loader"
20+
"github.com/tinygo-org/tinygo/src/tinygo"
2021
"golang.org/x/tools/go/ssa"
2122
"golang.org/x/tools/go/types/typeutil"
2223
"tinygo.org/x/go-llvm"
@@ -1869,10 +1870,9 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
18691870
}
18701871
return llvm.ConstInt(b.ctx.Int1Type(), supportsRecover, false), nil
18711872
case name == "runtime.panicStrategy":
1872-
// These constants are defined in src/runtime/panic.go.
18731873
panicStrategy := map[string]uint64{
1874-
"print": 1, // panicStrategyPrint
1875-
"trap": 2, // panicStrategyTrap
1874+
"print": tinygo.PanicStrategyPrint,
1875+
"trap": tinygo.PanicStrategyTrap,
18761876
}[b.Config.PanicStrategy]
18771877
return llvm.ConstInt(b.ctx.Int8Type(), panicStrategy, false), nil
18781878
case name == "runtime/interrupt.New":

compiler/map.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,32 @@ import (
66
"go/token"
77
"go/types"
88

9+
"github.com/tinygo-org/tinygo/src/tinygo"
910
"golang.org/x/tools/go/ssa"
1011
"tinygo.org/x/go-llvm"
1112
)
1213

13-
// constants for hashmap algorithms; must match src/runtime/hashmap.go
14-
const (
15-
hashmapAlgorithmBinary = iota
16-
hashmapAlgorithmString
17-
hashmapAlgorithmInterface
18-
)
19-
2014
// createMakeMap creates a new map object (runtime.hashmap) by allocating and
2115
// initializing an appropriately sized object.
2216
func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
2317
mapType := expr.Type().Underlying().(*types.Map)
2418
keyType := mapType.Key().Underlying()
2519
llvmValueType := b.getLLVMType(mapType.Elem().Underlying())
2620
var llvmKeyType llvm.Type
27-
var alg uint64 // must match values in src/runtime/hashmap.go
21+
var alg uint64
2822
if t, ok := keyType.(*types.Basic); ok && t.Info()&types.IsString != 0 {
2923
// String keys.
3024
llvmKeyType = b.getLLVMType(keyType)
31-
alg = hashmapAlgorithmString
25+
alg = uint64(tinygo.HashmapAlgorithmString)
3226
} else if hashmapIsBinaryKey(keyType) {
3327
// Trivially comparable keys.
3428
llvmKeyType = b.getLLVMType(keyType)
35-
alg = hashmapAlgorithmBinary
29+
alg = uint64(tinygo.HashmapAlgorithmBinary)
3630
} else {
3731
// All other keys. Implemented as map[interface{}]valueType for ease of
3832
// implementation.
3933
llvmKeyType = b.getLLVMRuntimeType("_interface")
40-
alg = hashmapAlgorithmInterface
34+
alg = uint64(tinygo.HashmapAlgorithmInterface)
4135
}
4236
keySize := b.targetData.TypeAllocSize(llvmKeyType)
4337
valueSize := b.targetData.TypeAllocSize(llvmValueType)

loader/goroot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool {
256256
"runtime/": false,
257257
"sync/": true,
258258
"testing/": true,
259+
"tinygo/": false,
259260
"unique/": false,
260261
}
261262

src/runtime/hashmap.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime
77

88
import (
99
"reflect"
10+
"tinygo"
1011
"unsafe"
1112
)
1213

@@ -22,14 +23,6 @@ type hashmap struct {
2223
keyHash func(key unsafe.Pointer, size, seed uintptr) uint32
2324
}
2425

25-
type hashmapAlgorithm uint8
26-
27-
const (
28-
hashmapAlgorithmBinary hashmapAlgorithm = iota
29-
hashmapAlgorithmString
30-
hashmapAlgorithmInterface
31-
)
32-
3326
// A hashmap bucket. A bucket is a container of 8 key/value pairs: first the
3427
// following two entries, then the 8 keys, then the 8 values. This somewhat odd
3528
// ordering is to make sure the keys and values are well aligned when one of
@@ -76,8 +69,8 @@ func hashmapMake(keySize, valueSize uintptr, sizeHint uintptr, alg uint8) *hashm
7669
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + keySize*8 + valueSize*8
7770
buckets := alloc(bucketBufSize*(1<<bucketBits), nil)
7871

79-
keyHash := hashmapKeyHashAlg(hashmapAlgorithm(alg))
80-
keyEqual := hashmapKeyEqualAlg(hashmapAlgorithm(alg))
72+
keyHash := hashmapKeyHashAlg(tinygo.HashmapAlgorithm(alg))
73+
keyEqual := hashmapKeyEqualAlg(tinygo.HashmapAlgorithm(alg))
8174

8275
return &hashmap{
8376
buckets: buckets,
@@ -119,27 +112,27 @@ func hashmapClear(m *hashmap) {
119112
}
120113
}
121114

122-
func hashmapKeyEqualAlg(alg hashmapAlgorithm) func(x, y unsafe.Pointer, n uintptr) bool {
115+
func hashmapKeyEqualAlg(alg tinygo.HashmapAlgorithm) func(x, y unsafe.Pointer, n uintptr) bool {
123116
switch alg {
124-
case hashmapAlgorithmBinary:
117+
case tinygo.HashmapAlgorithmBinary:
125118
return memequal
126-
case hashmapAlgorithmString:
119+
case tinygo.HashmapAlgorithmString:
127120
return hashmapStringEqual
128-
case hashmapAlgorithmInterface:
121+
case tinygo.HashmapAlgorithmInterface:
129122
return hashmapInterfaceEqual
130123
default:
131124
// compiler bug :(
132125
return nil
133126
}
134127
}
135128

136-
func hashmapKeyHashAlg(alg hashmapAlgorithm) func(key unsafe.Pointer, n, seed uintptr) uint32 {
129+
func hashmapKeyHashAlg(alg tinygo.HashmapAlgorithm) func(key unsafe.Pointer, n, seed uintptr) uint32 {
137130
switch alg {
138-
case hashmapAlgorithmBinary:
131+
case tinygo.HashmapAlgorithmBinary:
139132
return hash32
140-
case hashmapAlgorithmString:
133+
case tinygo.HashmapAlgorithmString:
141134
return hashmapStringPtrHash
142-
case hashmapAlgorithmInterface:
135+
case tinygo.HashmapAlgorithmInterface:
143136
return hashmapInterfacePtrHash
144137
default:
145138
// compiler bug :(

src/runtime/panic.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runtime
33
import (
44
"internal/task"
55
"runtime/interrupt"
6+
"tinygo"
67
"unsafe"
78
)
89

@@ -22,11 +23,6 @@ func tinygo_longjmp(frame *deferFrame)
2223
// Returns whether recover is supported on the current architecture.
2324
func supportsRecover() bool
2425

25-
const (
26-
panicStrategyPrint = 1
27-
panicStrategyTrap = 2
28-
)
29-
3026
// Compile intrinsic.
3127
// Returns which strategy is used. This is usually "print" but can be changed
3228
// using the -panic= compiler flag.
@@ -48,7 +44,7 @@ type deferFrame struct {
4844

4945
// Builtin function panic(msg), used as a compiler intrinsic.
5046
func _panic(message interface{}) {
51-
if panicStrategy() == panicStrategyTrap {
47+
if panicStrategy() == tinygo.PanicStrategyTrap {
5248
trap()
5349
}
5450
// Note: recover is not supported inside interrupts.
@@ -76,7 +72,7 @@ func runtimePanic(msg string) {
7672
}
7773

7874
func runtimePanicAt(addr unsafe.Pointer, msg string) {
79-
if panicStrategy() == panicStrategyTrap {
75+
if panicStrategy() == tinygo.PanicStrategyTrap {
8076
trap()
8177
}
8278
if hasReturnAddr {

src/runtime/runtime_unix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package runtime
55
import (
66
"math/bits"
77
"sync/atomic"
8+
"tinygo"
89
"unsafe"
910
)
1011

@@ -141,7 +142,7 @@ func tinygo_register_fatal_signals()
141142
//
142143
//export tinygo_handle_fatal_signal
143144
func tinygo_handle_fatal_signal(sig int32, addr uintptr) {
144-
if panicStrategy() == panicStrategyTrap {
145+
if panicStrategy() == tinygo.PanicStrategyTrap {
145146
trap()
146147
}
147148

src/tinygo/runtime.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Package tinygo contains constants used between the TinyGo compiler and
2+
// runtime.
3+
package tinygo
4+
5+
const (
6+
PanicStrategyPrint = iota + 1
7+
PanicStrategyTrap
8+
)
9+
10+
type HashmapAlgorithm uint8
11+
12+
// Constants for hashmap algorithms.
13+
const (
14+
HashmapAlgorithmBinary HashmapAlgorithm = iota
15+
HashmapAlgorithmString
16+
HashmapAlgorithmInterface
17+
)

0 commit comments

Comments
 (0)