OSDN Git Service

runtime: Make builtin print exactly match gc builtin print.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-print.c
1 /* go-print.c -- support for the go print statement.
2
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6
7 #include <math.h>
8 #include <stdint.h>
9 #include <stdio.h>
10
11 #include "array.h"
12 #include "go-panic.h"
13 #include "go-string.h"
14 #include "interface.h"
15
16 /* This implements the various little functions which are called by
17    the predeclared functions print/println/panic/panicln.  */
18
19 void
20 __go_print_space ()
21 {
22   putc (' ', stderr);
23 }
24
25 void
26 __go_print_nl ()
27 {
28   putc ('\n', stderr);
29 }
30
31 void
32 __go_print_string (struct __go_string val)
33 {
34   fprintf (stderr, "%.*s", (int) val.__length, (const char *) val.__data);
35 }
36
37 void
38 __go_print_uint64 (uint64_t val)
39 {
40   fprintf (stderr, "%llu", (unsigned long long) val);
41 }
42
43 void
44 __go_print_int64 (int64_t val)
45 {
46   fprintf (stderr, "%lld", (long long) val);
47 }
48
49 void
50 __go_print_double (double v)
51 {
52   char buf[20];
53   int e, s, i, n;
54   double h;
55
56   if (isnan (v))
57     {
58       fputs ("NaN", stderr);
59       return;
60     }
61   if (isinf (v))
62     {
63       putc (v < 0 ? '-' : '+', stderr);
64       fputs ("Inf", stderr);
65       return;
66     }
67
68   /* The number of digits printed.  */
69   n = 7;
70   /* The exponent.  */
71   e = 0;
72   /* The sign.  */
73   s = 0;
74   if (v != 0)
75     {
76       if (v < 0)
77         {
78           v = -v;
79           s = 1;
80         }
81
82       /* Normalize.  */
83       while (v >= 10)
84         {
85           ++e;
86           v /= 10;
87         }
88       while (v < 1)
89         {
90           --e;
91           v *= 10;
92         }
93
94       /* Round.  */
95       h = 5;
96       for (i = 0; i < n; ++i)
97         h /= 10;
98
99       v += h;
100       if (v >= 10)
101         {
102           ++e;
103           v /= 10;
104         }
105     }
106
107   /* The format is +d.dddd+edd.  */
108   buf[0] = s ? '-' : '+';
109   for (i = 0; i < n; ++i)
110     {
111       int d;
112
113       d = v;
114       buf[i + 2] = d + '0';
115       v -= d;
116       v *= 10;
117     }
118   buf[1] = buf[2];
119   buf[2] = '.';
120
121   buf[n + 2] = 'e';
122   buf[n + 3] = e < 0 ? '-' : '+';
123   if (e < 0)
124     e = - e;
125   buf[n + 4] = e / 100 + '0';
126   buf[n + 5] = (e / 10) % 10 + '0';
127   buf[n + 6] = e % 10 + '0';
128   buf[n + 7] = '\0';
129   fputs (buf, stderr);
130 }
131
132 void
133 __go_print_complex (__complex double val)
134 {
135   putc ('(', stderr);
136   __go_print_double (__builtin_creal (val));
137   __go_print_double (__builtin_cimag (val));
138   fputs ("i)", stderr);
139 }
140
141 void
142 __go_print_bool (_Bool val)
143 {
144   fputs (val ? "true" : "false", stderr);
145 }
146
147 void
148 __go_print_pointer (void *val)
149 {
150   fprintf (stderr, "0x%lx", (unsigned long) (uintptr_t) val);
151 }
152
153 void
154 __go_print_empty_interface (struct __go_empty_interface e)
155 {
156   fprintf (stderr, "(%p,%p)", e.__type_descriptor, e.__object);
157 }
158
159 void
160 __go_print_interface (struct __go_interface i)
161 {
162   fprintf (stderr, "(%p,%p)", i.__methods, i.__object);
163 }
164
165 void
166 __go_print_slice (struct __go_open_array val)
167 {
168   fprintf (stderr, "[%d/%d]", val.__count, val.__capacity);
169   __go_print_pointer (val.__values);
170 }