createMakeMap creates a new map object (runtime.hashmap) by allocating and initializing an appropriately sized object.
(expr *ssa.MakeMap)
| 14 | // createMakeMap creates a new map object (runtime.hashmap) by allocating and |
| 15 | // initializing an appropriately sized object. |
| 16 | func (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. |
no test coverage detected