2012-05-24 22:45:37 +02: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.
|
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
#include <complex.h>
|
|
|
|
#include <math.h>
|
2012-05-24 22:45:37 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include "runtime.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "array.h"
|
2013-11-06 20:49:01 +01:00
|
|
|
#include "go-type.h"
|
2012-05-24 22:45:37 +02:00
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
extern void runtime_printlock(void)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printlock");
|
|
|
|
extern void runtime_printunlock(void)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printunlock");
|
|
|
|
extern void gwrite(Slice)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.gwrite");
|
|
|
|
extern void runtime_printint(int64)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printint");
|
|
|
|
extern void runtime_printuint(uint64)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printuint");
|
|
|
|
extern void runtime_printhex(uint64)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printhex");
|
|
|
|
extern void runtime_printfloat(float64)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printfloat");
|
|
|
|
extern void runtime_printcomplex(complex double)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printcomplex");
|
|
|
|
extern void runtime_printbool(_Bool)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printbool");
|
|
|
|
extern void runtime_printstring(String)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printstring");
|
|
|
|
extern void runtime_printpointer(void *)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printpointer");
|
|
|
|
extern void runtime_printslice(Slice)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printslice");
|
|
|
|
extern void runtime_printeface(Eface)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printeface");
|
|
|
|
extern void runtime_printiface(Iface)
|
|
|
|
__asm__(GOSYM_PREFIX "runtime.printiface");
|
2012-05-24 22:45:37 +02:00
|
|
|
|
2014-05-29 02:03:30 +02:00
|
|
|
// Clang requires this function to not be inlined (see below).
|
|
|
|
static void go_vprintf(const char*, va_list)
|
|
|
|
__attribute__((noinline));
|
2012-05-24 22:45:37 +02:00
|
|
|
|
|
|
|
static void
|
2016-10-11 01:13:39 +02:00
|
|
|
runtime_prints(const char *s)
|
2012-05-24 22:45:37 +02:00
|
|
|
{
|
2016-10-11 01:13:39 +02:00
|
|
|
Slice sl;
|
2012-10-03 07:27:36 +02:00
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
// Use memcpy to avoid const-cast warning.
|
|
|
|
memcpy(&sl.__values, &s, sizeof(char*));
|
|
|
|
sl.__count = runtime_findnull((const byte*)s);
|
|
|
|
sl.__capacity = sl.__count;
|
|
|
|
gwrite(sl);
|
2012-05-24 22:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
static void
|
|
|
|
runtime_printbyte(int8 c)
|
2012-05-24 22:45:37 +02:00
|
|
|
{
|
2016-10-11 01:13:39 +02:00
|
|
|
Slice sl;
|
2012-05-24 22:45:37 +02:00
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
sl.__values = &c;
|
|
|
|
sl.__count = 1;
|
|
|
|
sl.__capacity = 1;
|
|
|
|
gwrite(sl);
|
2012-05-24 22:45:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 02:03:30 +02:00
|
|
|
#if defined (__clang__) && (defined (__i386__) || defined (__x86_64__))
|
|
|
|
// LLVM's code generator does not currently support split stacks for vararg
|
|
|
|
// functions, so we disable the feature for this function under Clang. This
|
|
|
|
// appears to be OK as long as:
|
|
|
|
// - this function only calls non-inlined, internal-linkage (hence no dynamic
|
|
|
|
// loader) functions compiled with split stacks (i.e. go_vprintf), which can
|
|
|
|
// allocate more stack space as required;
|
|
|
|
// - this function itself does not occupy more than BACKOFF bytes of stack space
|
|
|
|
// (see libgcc/config/i386/morestack.S).
|
|
|
|
// These conditions are currently known to be satisfied by Clang on x86-32 and
|
|
|
|
// x86-64. Note that signal handlers receive slightly less stack space than they
|
|
|
|
// would normally do if they happen to be called while this function is being
|
|
|
|
// run. If this turns out to be a problem we could consider increasing BACKOFF.
|
2014-07-20 11:24:16 +02:00
|
|
|
|
2014-05-29 02:03:30 +02:00
|
|
|
void
|
|
|
|
runtime_printf(const char *s, ...)
|
|
|
|
__attribute__((no_split_stack));
|
2014-07-20 11:24:16 +02:00
|
|
|
|
|
|
|
int32
|
|
|
|
runtime_snprintf(byte *buf, int32 n, const char *s, ...)
|
|
|
|
__attribute__((no_split_stack));
|
|
|
|
|
2014-05-29 02:03:30 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-24 22:45:37 +02:00
|
|
|
void
|
|
|
|
runtime_printf(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
go_vprintf(s, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
int32
|
|
|
|
runtime_snprintf(byte *buf, int32 n, const char *s, ...)
|
|
|
|
{
|
|
|
|
G *g = runtime_g();
|
|
|
|
va_list va;
|
|
|
|
int32 m;
|
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
g->writebuf.__values = buf;
|
|
|
|
g->writebuf.__count = 0;
|
|
|
|
g->writebuf.__capacity = n-1;
|
2014-07-19 10:53:52 +02:00
|
|
|
va_start(va, s);
|
|
|
|
go_vprintf(s, va);
|
|
|
|
va_end(va);
|
2016-10-11 01:13:39 +02:00
|
|
|
m = g->writebuf.__count;
|
|
|
|
((byte*)g->writebuf.__values)[m] = '\0';
|
|
|
|
g->writebuf.__values = nil;
|
|
|
|
g->writebuf.__count = 0;
|
|
|
|
g->writebuf.__capacity = 0;
|
2014-07-19 10:53:52 +02:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2012-05-24 22:45:37 +02:00
|
|
|
// Very simple printf. Only for debugging prints.
|
|
|
|
// Do not add to this without checking with Rob.
|
|
|
|
static void
|
|
|
|
go_vprintf(const char *s, va_list va)
|
|
|
|
{
|
|
|
|
const char *p, *lp;
|
2016-10-11 01:13:39 +02:00
|
|
|
Slice sl;
|
2012-05-24 22:45:37 +02:00
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
runtime_printlock();
|
2012-05-24 22:45:37 +02:00
|
|
|
|
|
|
|
lp = p = s;
|
|
|
|
for(; *p; p++) {
|
|
|
|
if(*p != '%')
|
|
|
|
continue;
|
2016-10-11 01:13:39 +02:00
|
|
|
if(p > lp) {
|
|
|
|
// Use memcpy to avoid const-cast warning.
|
|
|
|
memcpy(&sl.__values, &lp, sizeof(char*));
|
|
|
|
sl.__count = p - lp;
|
|
|
|
sl.__capacity = p - lp;
|
|
|
|
gwrite(sl);
|
|
|
|
}
|
2012-05-24 22:45:37 +02:00
|
|
|
p++;
|
|
|
|
switch(*p) {
|
|
|
|
case 'a':
|
|
|
|
runtime_printslice(va_arg(va, Slice));
|
|
|
|
break;
|
2013-07-16 08:54:42 +02:00
|
|
|
case 'c':
|
|
|
|
runtime_printbyte(va_arg(va, int32));
|
|
|
|
break;
|
2012-05-24 22:45:37 +02:00
|
|
|
case 'd':
|
|
|
|
runtime_printint(va_arg(va, int32));
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
runtime_printint(va_arg(va, int64));
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
runtime_printeface(va_arg(va, Eface));
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
runtime_printfloat(va_arg(va, float64));
|
|
|
|
break;
|
|
|
|
case 'C':
|
2014-05-29 01:10:47 +02:00
|
|
|
runtime_printcomplex(va_arg(va, complex double));
|
2012-05-24 22:45:37 +02:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
runtime_printiface(va_arg(va, Iface));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
runtime_printpointer(va_arg(va, void*));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
runtime_prints(va_arg(va, char*));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
runtime_printstring(va_arg(va, String));
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
runtime_printbool(va_arg(va, int));
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
runtime_printuint(va_arg(va, uint64));
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
runtime_printhex(va_arg(va, uint32));
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
runtime_printhex(va_arg(va, uint64));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lp = p+1;
|
|
|
|
}
|
2016-10-11 01:13:39 +02:00
|
|
|
if(p > lp) {
|
|
|
|
// Use memcpy to avoid const-cast warning.
|
|
|
|
memcpy(&sl.__values, &lp, sizeof(char*));
|
|
|
|
sl.__count = p - lp;
|
|
|
|
sl.__capacity = p - lp;
|
|
|
|
gwrite(sl);
|
2012-05-24 22:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 01:13:39 +02:00
|
|
|
runtime_printunlock();
|
2012-05-24 22:45:37 +02:00
|
|
|
}
|