MCPcopy Index your code
hub / github.com/tinygo-org/tinygo / createMakeMap

Method createMakeMap

compiler/map.go:16–52  ·  view source on GitHub ↗

createMakeMap creates a new map object (runtime.hashmap) by allocating and initializing an appropriately sized object.

(expr *ssa.MakeMap)

Source from the content-addressed store, hash-verified

14// createMakeMap creates a new map object (runtime.hashmap) by allocating and
15// initializing an appropriately sized object.
16func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
17 mapType := expr.Type().Underlying().(*types.Map)
18 keyType := mapType.Key().Underlying()
19 llvmValueType := b.getLLVMType(mapType.Elem().Underlying())
20 var llvmKeyType llvm.Type
21 var alg uint64
22 if t, ok := keyType.(*types.Basic); ok && t.Info()&types.IsString != 0 {
23 // String keys.
24 llvmKeyType = b.getLLVMType(keyType)
25 alg = uint64(tinygo.HashmapAlgorithmString)
26 } else if hashmapIsBinaryKey(keyType) {
27 // Trivially comparable keys.
28 llvmKeyType = b.getLLVMType(keyType)
29 alg = uint64(tinygo.HashmapAlgorithmBinary)
30 } else {
31 // All other keys. Implemented as map[interface{}]valueType for ease of
32 // implementation.
33 llvmKeyType = b.getLLVMRuntimeType("_interface")
34 alg = uint64(tinygo.HashmapAlgorithmInterface)
35 }
36 keySize := b.targetData.TypeAllocSize(llvmKeyType)
37 valueSize := b.targetData.TypeAllocSize(llvmValueType)
38 llvmKeySize := llvm.ConstInt(b.uintptrType, keySize, false)
39 llvmValueSize := llvm.ConstInt(b.uintptrType, valueSize, false)
40 sizeHint := llvm.ConstInt(b.uintptrType, 8, false)
41 algEnum := llvm.ConstInt(b.ctx.Int8Type(), alg, false)
42 if expr.Reserve != nil {
43 sizeHint = b.getValue(expr.Reserve, getPos(expr))
44 var err error
45 sizeHint, err = b.createConvert(expr.Reserve.Type(), types.Typ[types.Uintptr], sizeHint, expr.Pos())
46 if err != nil {
47 return llvm.Value{}, err
48 }
49 }
50 hashmap := b.createRuntimeCall("hashmapMake", []llvm.Value{llvmKeySize, llvmValueSize, sizeHint, algEnum}, "")
51 return hashmap, nil
52}
53
54// createMapLookup returns the value in a map. It calls a runtime function
55// depending on the map key type to load the map value and its comma-ok value.

Callers 1

createExprMethod · 0.95

Calls 12

getValueMethod · 0.95
createConvertMethod · 0.95
createRuntimeCallMethod · 0.95
hashmapIsBinaryKeyFunction · 0.85
getLLVMTypeMethod · 0.80
InfoMethod · 0.80
getLLVMRuntimeTypeMethod · 0.80
getPosFunction · 0.70
KeyMethod · 0.65
ElemMethod · 0.65
PosMethod · 0.65
TypeMethod · 0.45

Tested by

no test coverage detected