aboutsummaryrefslogtreecommitdiffhomepage
path: root/compiler
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-11-15 09:09:04 +0100
committerAyke <[email protected]>2024-11-15 10:11:57 +0100
commit6d4dfcf72fb5dd4cc9130c3b883fd313a5b8dac3 (patch)
tree95845071ea74b4a29f455be3afeac633edba905e /compiler
parentac9f72be617e6e72423fa7afd6bcd2aaf9377d27 (diff)
downloadtinygo-6d4dfcf72fb5dd4cc9130c3b883fd313a5b8dac3.tar.gz
tinygo-6d4dfcf72fb5dd4cc9130c3b883fd313a5b8dac3.zip
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.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/compiler.go6
-rw-r--r--compiler/map.go16
2 files changed, 8 insertions, 14 deletions
diff --git a/compiler/compiler.go b/compiler/compiler.go
index 981993fe7..8b5f0d1cb 100644
--- a/compiler/compiler.go
+++ b/compiler/compiler.go
@@ -17,6 +17,7 @@ import (
"github.com/tinygo-org/tinygo/compiler/llvmutil"
"github.com/tinygo-org/tinygo/loader"
+ "github.com/tinygo-org/tinygo/src/tinygo"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/types/typeutil"
"tinygo.org/x/go-llvm"
@@ -1869,10 +1870,9 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
}
return llvm.ConstInt(b.ctx.Int1Type(), supportsRecover, false), nil
case name == "runtime.panicStrategy":
- // These constants are defined in src/runtime/panic.go.
panicStrategy := map[string]uint64{
- "print": 1, // panicStrategyPrint
- "trap": 2, // panicStrategyTrap
+ "print": tinygo.PanicStrategyPrint,
+ "trap": tinygo.PanicStrategyTrap,
}[b.Config.PanicStrategy]
return llvm.ConstInt(b.ctx.Int8Type(), panicStrategy, false), nil
case name == "runtime/interrupt.New":
diff --git a/compiler/map.go b/compiler/map.go
index b4c526723..71fb3f0da 100644
--- a/compiler/map.go
+++ b/compiler/map.go
@@ -6,17 +6,11 @@ import (
"go/token"
"go/types"
+ "github.com/tinygo-org/tinygo/src/tinygo"
"golang.org/x/tools/go/ssa"
"tinygo.org/x/go-llvm"
)
-// constants for hashmap algorithms; must match src/runtime/hashmap.go
-const (
- hashmapAlgorithmBinary = iota
- hashmapAlgorithmString
- hashmapAlgorithmInterface
-)
-
// createMakeMap creates a new map object (runtime.hashmap) by allocating and
// initializing an appropriately sized object.
func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
@@ -24,20 +18,20 @@ func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
keyType := mapType.Key().Underlying()
llvmValueType := b.getLLVMType(mapType.Elem().Underlying())
var llvmKeyType llvm.Type
- var alg uint64 // must match values in src/runtime/hashmap.go
+ var alg uint64
if t, ok := keyType.(*types.Basic); ok && t.Info()&types.IsString != 0 {
// String keys.
llvmKeyType = b.getLLVMType(keyType)
- alg = hashmapAlgorithmString
+ alg = uint64(tinygo.HashmapAlgorithmString)
} else if hashmapIsBinaryKey(keyType) {
// Trivially comparable keys.
llvmKeyType = b.getLLVMType(keyType)
- alg = hashmapAlgorithmBinary
+ alg = uint64(tinygo.HashmapAlgorithmBinary)
} else {
// All other keys. Implemented as map[interface{}]valueType for ease of
// implementation.
llvmKeyType = b.getLLVMRuntimeType("_interface")
- alg = hashmapAlgorithmInterface
+ alg = uint64(tinygo.HashmapAlgorithmInterface)
}
keySize := b.targetData.TypeAllocSize(llvmKeyType)
valueSize := b.targetData.TypeAllocSize(llvmValueType)