2008-06-30 12:50:36 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
dump(byte *p, int32 n)
|
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
sys·printpointer((byte*)(p[i]>>4));
|
|
|
|
sys·printpointer((byte*)(p[i]&0xf));
|
|
|
|
if((i&15) == 15)
|
|
|
|
prints("\n");
|
|
|
|
else
|
|
|
|
prints(" ");
|
|
|
|
}
|
|
|
|
if(n & 15)
|
|
|
|
prints("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prints(int8 *s)
|
|
|
|
{
|
2008-11-23 18:08:55 -07:00
|
|
|
sys·write(1, s, findnull((byte*)s));
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
2008-11-25 17:48:10 -07:00
|
|
|
// Very simple printf. Only for debugging prints.
|
|
|
|
// Do not add to this without checking with Rob.
|
|
|
|
void
|
|
|
|
printf(int8 *s, ...)
|
|
|
|
{
|
|
|
|
int8 *p, *lp;
|
|
|
|
byte *arg;
|
|
|
|
|
|
|
|
lp = p = s;
|
|
|
|
arg = (byte*)(&s+1);
|
|
|
|
for(; *p; p++) {
|
|
|
|
if(*p != '%')
|
|
|
|
continue;
|
|
|
|
if(p > lp)
|
|
|
|
sys·write(1, lp, p-lp);
|
|
|
|
p++;
|
|
|
|
switch(*p) {
|
|
|
|
case 'd':
|
|
|
|
sys·printint(*(int32*)arg);
|
|
|
|
arg += 4;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
if(((uint32)(uint64)arg)&4)
|
|
|
|
arg += 4;
|
|
|
|
sys·printint(*(int64*)arg);
|
|
|
|
arg += 8;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
sys·printhex(*(int32*)arg);
|
|
|
|
arg += 4;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
if(((uint32)(uint64)arg)&4)
|
|
|
|
arg += 4;
|
|
|
|
sys·printhex(*(int64*)arg);
|
|
|
|
arg += 8;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if(((uint32)(uint64)arg)&4)
|
|
|
|
arg += 4;
|
|
|
|
sys·printpointer(*(void**)arg);
|
|
|
|
arg += 8;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if(((uint32)(uint64)arg)&4)
|
|
|
|
arg += 4;
|
|
|
|
prints(*(int8**)arg);
|
|
|
|
arg += 8;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if(((uint32)(uint64)arg)&4)
|
|
|
|
arg += 4;
|
|
|
|
sys·printstring(*(string*)arg);
|
|
|
|
arg += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lp = p+1;
|
|
|
|
}
|
|
|
|
if(p > lp)
|
|
|
|
sys·write(1, lp, p-lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-30 12:50:36 -06:00
|
|
|
void
|
|
|
|
sys·printpc(void *p)
|
|
|
|
{
|
2008-11-25 17:48:10 -07:00
|
|
|
prints("PC=");
|
|
|
|
sys·printhex((uint64)sys·getcallerpc(p));
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sys·printbool(bool v)
|
|
|
|
{
|
|
|
|
if(v) {
|
|
|
|
sys·write(1, (byte*)"true", 4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sys·write(1, (byte*)"false", 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sys·printfloat(float64 v)
|
|
|
|
{
|
2008-07-04 19:01:35 -06:00
|
|
|
byte buf[20];
|
|
|
|
int32 e, s, i, n;
|
|
|
|
float64 h;
|
|
|
|
|
2008-11-10 15:54:10 -07:00
|
|
|
if(isNaN(v)) {
|
|
|
|
sys·write(1, "NaN", 3);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(isInf(v, 0)) {
|
|
|
|
sys·write(1, "+Inf", 4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(isInf(v, -1)) {
|
|
|
|
sys·write(1, "+Inf", 4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-27 14:33:50 -06:00
|
|
|
n = 7; // digits printed
|
2008-07-04 19:01:35 -06:00
|
|
|
e = 0; // exp
|
|
|
|
s = 0; // sign
|
|
|
|
if(v != 0) {
|
|
|
|
// sign
|
|
|
|
if(v < 0) {
|
|
|
|
v = -v;
|
|
|
|
s = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize
|
|
|
|
while(v >= 10) {
|
|
|
|
e++;
|
|
|
|
v /= 10;
|
|
|
|
}
|
|
|
|
while(v < 1) {
|
|
|
|
e--;
|
|
|
|
v *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// round
|
|
|
|
h = 5;
|
|
|
|
for(i=0; i<n; i++)
|
|
|
|
h /= 10;
|
|
|
|
v += h;
|
|
|
|
if(v >= 10) {
|
|
|
|
e++;
|
|
|
|
v /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// format +d.dddd+edd
|
|
|
|
buf[0] = '+';
|
|
|
|
if(s)
|
|
|
|
buf[0] = '-';
|
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
s = v;
|
|
|
|
buf[i+2] = s+'0';
|
|
|
|
v -= s;
|
|
|
|
v *= 10.;
|
|
|
|
}
|
|
|
|
buf[1] = buf[2];
|
|
|
|
buf[2] = '.';
|
|
|
|
|
2008-09-14 17:57:55 -06:00
|
|
|
buf[n+2] = 'e';
|
|
|
|
buf[n+3] = '+';
|
2008-07-04 19:01:35 -06:00
|
|
|
if(e < 0) {
|
|
|
|
e = -e;
|
2008-09-14 17:57:55 -06:00
|
|
|
buf[n+3] = '-';
|
2008-07-04 19:01:35 -06:00
|
|
|
}
|
2008-09-14 17:57:55 -06:00
|
|
|
|
2008-11-10 15:54:10 -07:00
|
|
|
buf[n+4] = (e/100) + '0';
|
|
|
|
buf[n+5] = (e/10)%10 + '0';
|
|
|
|
buf[n+6] = (e%10) + '0';
|
|
|
|
sys·write(1, buf, n+7);
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-11-10 15:54:10 -07:00
|
|
|
sys·printuint(uint64 v)
|
2008-06-30 12:50:36 -06:00
|
|
|
{
|
|
|
|
byte buf[100];
|
2008-11-10 15:54:10 -07:00
|
|
|
int32 i;
|
2008-06-30 12:50:36 -06:00
|
|
|
|
|
|
|
for(i=nelem(buf)-1; i>0; i--) {
|
|
|
|
buf[i] = v%10 + '0';
|
|
|
|
if(v < 10)
|
|
|
|
break;
|
|
|
|
v = v/10;
|
|
|
|
}
|
|
|
|
sys·write(1, buf+i, nelem(buf)-i);
|
|
|
|
}
|
|
|
|
|
2008-11-10 15:54:10 -07:00
|
|
|
void
|
|
|
|
sys·printint(int64 v)
|
|
|
|
{
|
|
|
|
if(v < 0) {
|
|
|
|
sys·write(1, "-", 1);
|
|
|
|
v = -v;
|
|
|
|
}
|
|
|
|
sys·printuint(v);
|
|
|
|
}
|
|
|
|
|
2008-06-30 12:50:36 -06:00
|
|
|
void
|
2008-11-25 17:48:10 -07:00
|
|
|
sys·printhex(uint64 v)
|
2008-06-30 12:50:36 -06:00
|
|
|
{
|
2008-11-25 17:48:10 -07:00
|
|
|
static int8 *dig = "0123456789abcdef";
|
2008-06-30 12:50:36 -06:00
|
|
|
byte buf[100];
|
|
|
|
int32 i;
|
|
|
|
|
2008-11-25 17:48:10 -07:00
|
|
|
i=nelem(buf);
|
|
|
|
for(; v>0; v/=16)
|
|
|
|
buf[--i] = dig[v%16];
|
|
|
|
if(i == nelem(buf))
|
|
|
|
buf[--i] = '0';
|
|
|
|
buf[--i] = 'x';
|
|
|
|
buf[--i] = '0';
|
2008-06-30 12:50:36 -06:00
|
|
|
sys·write(1, buf+i, nelem(buf)-i);
|
|
|
|
}
|
|
|
|
|
2008-11-25 17:48:10 -07:00
|
|
|
void
|
|
|
|
sys·printpointer(void *p)
|
|
|
|
{
|
|
|
|
sys·printhex((uint64)p);
|
|
|
|
}
|
|
|
|
|
2008-06-30 12:50:36 -06:00
|
|
|
void
|
|
|
|
sys·printstring(string v)
|
|
|
|
{
|
2009-01-29 18:38:58 -07:00
|
|
|
extern int32 maxstring;
|
|
|
|
|
|
|
|
if(v != nil) {
|
|
|
|
if(v->len > maxstring)
|
|
|
|
sys·write(1, "[invalid string]", 16);
|
|
|
|
else
|
|
|
|
sys·write(1, v->str, v->len);
|
|
|
|
}
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
2008-10-02 15:38:07 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
sys·printsp(void)
|
|
|
|
{
|
|
|
|
sys·write(1, " ", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sys·printnl(void)
|
|
|
|
{
|
|
|
|
sys·write(1, "\n", 1);
|
|
|
|
}
|