From 42711c11e9a9a1f653821892aa89f96ec5bbb06e Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 30 Aug 2018 02:26:48 +0200 Subject: [PATCH] Be able to print maps to some degree Print the number of elements, or nil if it is a nil map. --- compiler.go | 2 ++ src/runtime/print.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/compiler.go b/compiler.go index 3b78ddd17..b53e5242e 100644 --- a/compiler.go +++ b/compiler.go @@ -1277,6 +1277,8 @@ func (c *Compiler) parseBuiltin(frame *Frame, args []ssa.Value, callName string) } case *types.Interface: c.builder.CreateCall(c.mod.NamedFunction("runtime.printitf"), []llvm.Value{value}, "") + case *types.Map: + c.builder.CreateCall(c.mod.NamedFunction("runtime.printmap"), []llvm.Value{value}, "") case *types.Pointer: ptrValue := c.builder.CreatePtrToInt(value, c.uintptrType, "") c.builder.CreateCall(c.mod.NamedFunction("runtime.printptr"), []llvm.Value{ptrValue}, "") diff --git a/src/runtime/print.go b/src/runtime/print.go index e341c0712..fcb09d719 100644 --- a/src/runtime/print.go +++ b/src/runtime/print.go @@ -102,7 +102,21 @@ func printitf(msg interface{}) { } } +func printmap(m *hashmap) { + print("map[") + if m == nil { + print("nil") + } else { + print(m.count) + } + putchar(']') +} + func printptr(ptr uintptr) { + if ptr == 0 { + print("nil") + return + } putchar('0') putchar('x') for i := 0; i < int(unsafe.Sizeof(ptr))*2; i++ {