diff --git a/compiler/compiler.go b/compiler/compiler.go index 5ead43b5..f6ae3cb0 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1541,6 +1541,12 @@ func (b *builder) createBuiltin(argTypes []types.Type, argValues []llvm.Value, c case *types.Pointer: ptrValue := b.CreatePtrToInt(value, b.uintptrType, "") b.createRuntimeCall("printptr", []llvm.Value{ptrValue}, "") + case *types.Slice: + bufptr := b.CreateExtractValue(value, 0, "") + buflen := b.CreateExtractValue(value, 1, "") + bufcap := b.CreateExtractValue(value, 2, "") + ptrValue := b.CreatePtrToInt(bufptr, b.uintptrType, "") + b.createRuntimeCall("printslice", []llvm.Value{ptrValue, buflen, bufcap}, "") default: return llvm.Value{}, b.makeError(pos, "unknown arg type: "+typ.String()) } diff --git a/src/runtime/print.go b/src/runtime/print.go index 2c78c822..b09e1b3b 100644 --- a/src/runtime/print.go +++ b/src/runtime/print.go @@ -39,6 +39,17 @@ func printint8(n int8) { } } +func printuint(n uintptr) { + switch unsafe.Sizeof(n) { + case 2: + printuint16(uint16(n)) + case 4: + printuint32(uint32(n)) + case 8: + printuint64(uint64(n)) + } +} + func printuint16(n uint16) { printuint32(uint32(n)) } @@ -323,14 +334,7 @@ func printitf(msg interface{}) { // cast to underlying type itf := *(*_interface)(unsafe.Pointer(&msg)) putchar('(') - switch unsafe.Sizeof(itf.typecode) { - case 2: - printuint16(uint16(itf.typecode)) - case 4: - printuint32(uint32(itf.typecode)) - case 8: - printuint64(uint64(itf.typecode)) - } + printuint(uintptr(itf.typecode)) putchar(':') print(itf.value) putchar(')') @@ -372,3 +376,12 @@ func printbool(b bool) { printstring("false") } } + +func printslice(ptr, len_, cap_ uintptr) { + putchar('[') + printuint(len_) + putchar('/') + printuint(cap_) + putchar(']') + printptr(ptr) +} diff --git a/testdata/print.go b/testdata/print.go index 2b5f0a09..7f7f843c 100644 --- a/testdata/print.go +++ b/testdata/print.go @@ -45,4 +45,8 @@ func main() { // print bool println(true, false) + + // print slice + println([]byte(nil)) + println([]int(nil)) } diff --git a/testdata/print.txt b/testdata/print.txt index 372eda95..116de945 100644 --- a/testdata/print.txt +++ b/testdata/print.txt @@ -21,3 +21,5 @@ a b c (0:nil) map[2] true false +[0/0]nil +[0/0]nil