* lib/gcc-dg.exp (process-message): Support relative line number
[official-gcc.git] / libgo / go / runtime / print.go
blob97d595fb2fbd33a5626fb7ee6f17908ecfc58f0e
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build ignore
7 package runtime
9 import "unsafe"
11 // The compiler knows that a print of a value of this type
12 // should use printhex instead of printuint (decimal).
13 type hex uint64
15 func bytes(s string) (ret []byte) {
16 rp := (*slice)(unsafe.Pointer(&ret))
17 sp := stringStructOf(&s)
18 rp.array = sp.str
19 rp.len = sp.len
20 rp.cap = sp.len
21 return
24 var debuglock mutex
26 // The compiler emits calls to printlock and printunlock around
27 // the multiple calls that implement a single Go print or println
28 // statement. Some of the print helpers (printsp, for example)
29 // call print recursively. There is also the problem of a crash
30 // happening during the print routines and needing to acquire
31 // the print lock to print information about the crash.
32 // For both these reasons, let a thread acquire the printlock 'recursively'.
34 func printlock() {
35 mp := getg().m
36 mp.locks++ // do not reschedule between printlock++ and lock(&debuglock).
37 mp.printlock++
38 if mp.printlock == 1 {
39 lock(&debuglock)
41 mp.locks-- // now we know debuglock is held and holding up mp.locks for us.
44 func printunlock() {
45 mp := getg().m
46 mp.printlock--
47 if mp.printlock == 0 {
48 unlock(&debuglock)
52 // write to goroutine-local buffer if diverting output,
53 // or else standard error.
54 func gwrite(b []byte) {
55 if len(b) == 0 {
56 return
58 gp := getg()
59 if gp == nil || gp.writebuf == nil {
60 writeErr(b)
61 return
64 n := copy(gp.writebuf[len(gp.writebuf):cap(gp.writebuf)], b)
65 gp.writebuf = gp.writebuf[:len(gp.writebuf)+n]
68 func printsp() {
69 print(" ")
72 func printnl() {
73 print("\n")
76 func printbool(v bool) {
77 if v {
78 print("true")
79 } else {
80 print("false")
84 func printfloat(v float64) {
85 switch {
86 case v != v:
87 print("NaN")
88 return
89 case v+v == v && v > 0:
90 print("+Inf")
91 return
92 case v+v == v && v < 0:
93 print("-Inf")
94 return
97 const n = 7 // digits printed
98 var buf [n + 7]byte
99 buf[0] = '+'
100 e := 0 // exp
101 if v == 0 {
102 if 1/v < 0 {
103 buf[0] = '-'
105 } else {
106 if v < 0 {
107 v = -v
108 buf[0] = '-'
111 // normalize
112 for v >= 10 {
114 v /= 10
116 for v < 1 {
118 v *= 10
121 // round
122 h := 5.0
123 for i := 0; i < n; i++ {
124 h /= 10
126 v += h
127 if v >= 10 {
129 v /= 10
133 // format +d.dddd+edd
134 for i := 0; i < n; i++ {
135 s := int(v)
136 buf[i+2] = byte(s + '0')
137 v -= float64(s)
138 v *= 10
140 buf[1] = buf[2]
141 buf[2] = '.'
143 buf[n+2] = 'e'
144 buf[n+3] = '+'
145 if e < 0 {
146 e = -e
147 buf[n+3] = '-'
150 buf[n+4] = byte(e/100) + '0'
151 buf[n+5] = byte(e/10)%10 + '0'
152 buf[n+6] = byte(e%10) + '0'
153 gwrite(buf[:])
156 func printcomplex(c complex128) {
157 print("(", real(c), imag(c), "i)")
160 func printuint(v uint64) {
161 var buf [100]byte
162 i := len(buf)
163 for i--; i > 0; i-- {
164 buf[i] = byte(v%10 + '0')
165 if v < 10 {
166 break
168 v /= 10
170 gwrite(buf[i:])
173 func printint(v int64) {
174 if v < 0 {
175 print("-")
176 v = -v
178 printuint(uint64(v))
181 func printhex(v uint64) {
182 const dig = "0123456789abcdef"
183 var buf [100]byte
184 i := len(buf)
185 for i--; i > 0; i-- {
186 buf[i] = dig[v%16]
187 if v < 16 {
188 break
190 v /= 16
193 buf[i] = 'x'
195 buf[i] = '0'
196 gwrite(buf[i:])
199 func printpointer(p unsafe.Pointer) {
200 printhex(uint64(uintptr(p)))
203 func printstring(s string) {
204 if uintptr(len(s)) > maxstring {
205 gwrite(bytes("[string too long]"))
206 return
208 gwrite(bytes(s))
211 func printslice(s []byte) {
212 sp := (*slice)(unsafe.Pointer(&s))
213 print("[", len(s), "/", cap(s), "]")
214 printpointer(sp.array)
217 func printeface(e eface) {
218 print("(", e._type, ",", e.data, ")")
221 func printiface(i iface) {
222 print("(", i.tab, ",", i.data, ")")