From e884221fad77f71079a11ab6fc29cc9674ebea81 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 23 Aug 2018 23:14:54 +0200 Subject: [PATCH] Implement len() for map types --- compiler.go | 7 +++++++ src/runtime/hashmap.go | 1 + 2 files changed, 8 insertions(+) diff --git a/compiler.go b/compiler.go index 259bd15c..52d718d4 100644 --- a/compiler.go +++ b/compiler.go @@ -1095,6 +1095,13 @@ func (c *Compiler) parseBuiltin(frame *Frame, args []ssa.Value, callName string) default: return llvm.Value{}, errors.New("todo: len: unknown basic type") } + case *types.Map: + indices := []llvm.Value{ + llvm.ConstInt(llvm.Int32Type(), 0, false), + llvm.ConstInt(llvm.Int32Type(), 2, false), // hashmap.count + } + ptr := c.builder.CreateGEP(value, indices, "lenptr") + return c.builder.CreateLoad(ptr, "len"), nil case *types.Slice: return c.builder.CreateExtractValue(value, 1, "len"), nil default: diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go index 74eb9c70..33dcbdb2 100644 --- a/src/runtime/hashmap.go +++ b/src/runtime/hashmap.go @@ -98,6 +98,7 @@ func hashmapSet(m *hashmap, key string, value unsafe.Pointer) { bucket = bucket.next } if emptySlotKey != nil { + m.count++ *emptySlotKey = key memcpy(emptySlotValue, value, uintptr(m.valueSize)) *emptySlotTophash = tophash