@@ -10,29 +10,41 @@ import (
10
10
"tinygo.org/x/go-llvm"
11
11
)
12
12
13
+ // constants for hashmap algorithms; must match src/runtime/hashmap.go
14
+ const (
15
+ hashmapAlgorithmBinary = iota
16
+ hashmapAlgorithmString
17
+ hashmapAlgorithmInterface
18
+ )
19
+
13
20
// createMakeMap creates a new map object (runtime.hashmap) by allocating and
14
21
// initializing an appropriately sized object.
15
22
func (b * builder ) createMakeMap (expr * ssa.MakeMap ) (llvm.Value , error ) {
16
23
mapType := expr .Type ().Underlying ().(* types.Map )
17
24
keyType := mapType .Key ().Underlying ()
18
25
llvmValueType := b .getLLVMType (mapType .Elem ().Underlying ())
19
26
var llvmKeyType llvm.Type
27
+ var alg uint64 // must match values in src/runtime/hashmap.go
20
28
if t , ok := keyType .(* types.Basic ); ok && t .Info ()& types .IsString != 0 {
21
29
// String keys.
22
30
llvmKeyType = b .getLLVMType (keyType )
31
+ alg = hashmapAlgorithmString
23
32
} else if hashmapIsBinaryKey (keyType ) {
24
33
// Trivially comparable keys.
25
34
llvmKeyType = b .getLLVMType (keyType )
35
+ alg = hashmapAlgorithmBinary
26
36
} else {
27
37
// All other keys. Implemented as map[interface{}]valueType for ease of
28
38
// implementation.
29
39
llvmKeyType = b .getLLVMRuntimeType ("_interface" )
40
+ alg = hashmapAlgorithmInterface
30
41
}
31
42
keySize := b .targetData .TypeAllocSize (llvmKeyType )
32
43
valueSize := b .targetData .TypeAllocSize (llvmValueType )
33
44
llvmKeySize := llvm .ConstInt (b .ctx .Int8Type (), keySize , false )
34
45
llvmValueSize := llvm .ConstInt (b .ctx .Int8Type (), valueSize , false )
35
46
sizeHint := llvm .ConstInt (b .uintptrType , 8 , false )
47
+ algEnum := llvm .ConstInt (b .ctx .Int8Type (), alg , false )
36
48
if expr .Reserve != nil {
37
49
sizeHint = b .getValue (expr .Reserve )
38
50
var err error
@@ -41,7 +53,7 @@ func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
41
53
return llvm.Value {}, err
42
54
}
43
55
}
44
- hashmap := b .createRuntimeCall ("hashmapMake" , []llvm.Value {llvmKeySize , llvmValueSize , sizeHint }, "" )
56
+ hashmap := b .createRuntimeCall ("hashmapMake" , []llvm.Value {llvmKeySize , llvmValueSize , sizeHint , algEnum }, "" )
45
57
return hashmap , nil
46
58
}
47
59
0 commit comments