2008-06-05 20:38:39 -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"
|
|
|
|
|
2008-11-05 22:50:28 -07:00
|
|
|
int32 panicking = 0;
|
2009-03-30 01:01:07 -06:00
|
|
|
int32 maxround = sizeof(uintptr);
|
2009-10-13 23:48:03 -06:00
|
|
|
int32 fd = 1;
|
2008-06-05 20:38:39 -06:00
|
|
|
|
2008-09-22 14:47:53 -06:00
|
|
|
int32
|
|
|
|
gotraceback(void)
|
|
|
|
{
|
|
|
|
byte *p;
|
|
|
|
|
|
|
|
p = getenv("GOTRACEBACK");
|
|
|
|
if(p == nil || p[0] == '\0')
|
|
|
|
return 1; // default is on
|
|
|
|
return atoi(p);
|
|
|
|
}
|
|
|
|
|
2008-06-16 18:04:30 -06:00
|
|
|
void
|
2010-01-25 19:52:55 -07:00
|
|
|
·panicl(int32 lno)
|
2008-06-16 18:04:30 -06:00
|
|
|
{
|
2008-06-19 00:17:39 -06:00
|
|
|
uint8 *sp;
|
|
|
|
|
2009-10-13 23:48:03 -06:00
|
|
|
fd = 2;
|
2009-03-31 16:45:12 -06:00
|
|
|
if(panicking) {
|
|
|
|
printf("double panic\n");
|
2009-05-08 16:21:41 -06:00
|
|
|
exit(3);
|
2009-03-31 16:45:12 -06:00
|
|
|
}
|
|
|
|
panicking++;
|
|
|
|
|
2009-03-31 18:33:04 -06:00
|
|
|
printf("\npanic PC=%X\n", (uint64)(uintptr)&lno);
|
2008-06-19 00:17:39 -06:00
|
|
|
sp = (uint8*)&lno;
|
2008-09-22 14:47:53 -06:00
|
|
|
if(gotraceback()){
|
2010-01-25 19:52:55 -07:00
|
|
|
traceback(·getcallerpc(&lno), sp, g);
|
2008-09-22 14:47:53 -06:00
|
|
|
tracebackothers(g);
|
|
|
|
}
|
2009-05-08 16:21:41 -06:00
|
|
|
breakpoint(); // so we can grab it in a debugger
|
|
|
|
exit(2);
|
2008-06-16 18:04:30 -06:00
|
|
|
}
|
|
|
|
|
2008-09-12 17:48:35 -06:00
|
|
|
void
|
2010-01-25 19:52:55 -07:00
|
|
|
·throwindex(void)
|
2008-09-12 17:48:35 -06:00
|
|
|
{
|
|
|
|
throw("index out of range");
|
|
|
|
}
|
|
|
|
|
2009-08-28 13:37:39 -06:00
|
|
|
void
|
2010-01-25 19:52:55 -07:00
|
|
|
·throwslice(void)
|
2009-08-28 13:37:39 -06:00
|
|
|
{
|
|
|
|
throw("slice out of range");
|
|
|
|
}
|
|
|
|
|
2008-09-12 17:48:35 -06:00
|
|
|
void
|
2010-01-25 19:52:55 -07:00
|
|
|
·throwreturn(void)
|
2008-09-12 17:48:35 -06:00
|
|
|
{
|
|
|
|
throw("no return at end of a typed function");
|
|
|
|
}
|
|
|
|
|
2009-05-28 17:00:55 -06:00
|
|
|
void
|
2010-01-25 19:52:55 -07:00
|
|
|
·throwinit(void)
|
2009-05-28 17:00:55 -06:00
|
|
|
{
|
|
|
|
throw("recursive call during initialization");
|
|
|
|
}
|
|
|
|
|
2008-06-16 23:34:50 -06:00
|
|
|
void
|
2008-06-05 20:38:39 -06:00
|
|
|
throw(int8 *s)
|
|
|
|
{
|
2009-10-13 23:48:03 -06:00
|
|
|
fd = 2;
|
2009-03-31 18:33:04 -06:00
|
|
|
printf("throw: %s\n", s);
|
2010-01-25 19:52:55 -07:00
|
|
|
·panicl(-1);
|
2009-03-31 18:33:04 -06:00
|
|
|
*(int32*)0 = 0; // not reached
|
2009-05-08 16:21:41 -06:00
|
|
|
exit(1); // even more not reached
|
2008-06-05 20:38:39 -06:00
|
|
|
}
|
|
|
|
|
2008-06-15 21:24:30 -06:00
|
|
|
void
|
2008-06-05 20:38:39 -06:00
|
|
|
mcpy(byte *t, byte *f, uint32 n)
|
|
|
|
{
|
|
|
|
while(n > 0) {
|
|
|
|
*t = *f;
|
|
|
|
t++;
|
|
|
|
f++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-26 13:36:21 -07:00
|
|
|
int32
|
|
|
|
mcmp(byte *s1, byte *s2, uint32 n)
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
byte c1, c2;
|
|
|
|
|
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
if(c1 < c2)
|
|
|
|
return -1;
|
|
|
|
if(c1 > c2)
|
|
|
|
return +1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:08:55 -07:00
|
|
|
byte*
|
|
|
|
mchr(byte *p, byte c, byte *ep)
|
|
|
|
{
|
|
|
|
for(; p < ep; p++)
|
|
|
|
if(*p == c)
|
|
|
|
return p;
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2008-07-13 15:29:46 -06:00
|
|
|
uint32
|
|
|
|
rnd(uint32 n, uint32 m)
|
|
|
|
{
|
|
|
|
uint32 r;
|
|
|
|
|
2008-11-05 22:50:28 -07:00
|
|
|
if(m > maxround)
|
|
|
|
m = maxround;
|
2008-07-13 15:29:46 -06:00
|
|
|
r = n % m;
|
|
|
|
if(r)
|
|
|
|
n += m-r;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:30:47 -06:00
|
|
|
static int32 argc;
|
|
|
|
static uint8** argv;
|
2009-01-16 15:58:14 -07:00
|
|
|
|
2009-08-25 16:54:25 -06:00
|
|
|
Slice os·Args;
|
|
|
|
Slice os·Envs;
|
2008-06-30 16:30:47 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
args(int32 c, uint8 **v)
|
|
|
|
{
|
|
|
|
argc = c;
|
|
|
|
argv = v;
|
|
|
|
}
|
|
|
|
|
2009-01-16 15:58:14 -07:00
|
|
|
void
|
|
|
|
goargs(void)
|
2008-08-04 17:43:49 -06:00
|
|
|
{
|
2009-04-09 19:16:21 -06:00
|
|
|
String *gargv;
|
|
|
|
String *genvv;
|
2009-01-16 15:58:14 -07:00
|
|
|
int32 i, envc;
|
|
|
|
|
2009-01-26 18:37:05 -07:00
|
|
|
for(envc=0; argv[argc+1+envc] != 0; envc++)
|
|
|
|
;
|
|
|
|
|
|
|
|
gargv = malloc(argc*sizeof gargv[0]);
|
|
|
|
genvv = malloc(envc*sizeof genvv[0]);
|
|
|
|
|
|
|
|
for(i=0; i<argc; i++)
|
|
|
|
gargv[i] = gostring(argv[i]);
|
2009-05-08 16:21:41 -06:00
|
|
|
os·Args.array = (byte*)gargv;
|
2009-08-25 16:54:25 -06:00
|
|
|
os·Args.len = argc;
|
2009-05-08 16:21:41 -06:00
|
|
|
os·Args.cap = argc;
|
2009-01-16 15:58:14 -07:00
|
|
|
|
2009-01-26 18:37:05 -07:00
|
|
|
for(i=0; i<envc; i++)
|
|
|
|
genvv[i] = gostring(argv[argc+1+i]);
|
2009-05-08 16:21:41 -06:00
|
|
|
os·Envs.array = (byte*)genvv;
|
2009-08-25 16:54:25 -06:00
|
|
|
os·Envs.len = envc;
|
2009-05-08 16:21:41 -06:00
|
|
|
os·Envs.cap = envc;
|
2008-08-04 17:43:49 -06:00
|
|
|
}
|
|
|
|
|
2010-01-06 18:58:55 -07:00
|
|
|
// Atomic add and return new value.
|
|
|
|
uint32
|
|
|
|
xadd(uint32 volatile *val, int32 delta)
|
|
|
|
{
|
|
|
|
uint32 oval, nval;
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
oval = *val;
|
|
|
|
nval = oval + delta;
|
|
|
|
if(cas(val, oval, nval))
|
|
|
|
return nval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
byte*
|
|
|
|
getenv(int8 *s)
|
|
|
|
{
|
|
|
|
int32 i, j, len;
|
|
|
|
byte *v, *bs;
|
2009-04-09 19:16:21 -06:00
|
|
|
String* envv;
|
2009-01-16 15:58:14 -07:00
|
|
|
int32 envc;
|
2008-08-05 15:21:42 -06:00
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
bs = (byte*)s;
|
2008-11-23 18:08:55 -07:00
|
|
|
len = findnull(bs);
|
2009-05-08 16:21:41 -06:00
|
|
|
envv = (String*)os·Envs.array;
|
2009-08-25 16:54:25 -06:00
|
|
|
envc = os·Envs.len;
|
2008-08-04 17:43:49 -06:00
|
|
|
for(i=0; i<envc; i++){
|
2009-04-09 19:16:21 -06:00
|
|
|
if(envv[i].len <= len)
|
2009-03-30 01:01:07 -06:00
|
|
|
continue;
|
2009-04-09 19:16:21 -06:00
|
|
|
v = envv[i].str;
|
2008-08-04 17:43:49 -06:00
|
|
|
for(j=0; j<len; j++)
|
|
|
|
if(bs[j] != v[j])
|
|
|
|
goto nomatch;
|
|
|
|
if(v[len] != '=')
|
|
|
|
goto nomatch;
|
|
|
|
return v+len+1;
|
|
|
|
nomatch:;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-01-16 15:58:14 -07:00
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
int32
|
|
|
|
atoi(byte *p)
|
|
|
|
{
|
|
|
|
int32 n;
|
2008-08-05 15:21:42 -06:00
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
n = 0;
|
|
|
|
while('0' <= *p && *p <= '9')
|
|
|
|
n = n*10 + *p++ - '0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-08-05 15:18:47 -06:00
|
|
|
void
|
2008-06-30 16:30:47 -06:00
|
|
|
check(void)
|
|
|
|
{
|
|
|
|
int8 a;
|
|
|
|
uint8 b;
|
|
|
|
int16 c;
|
|
|
|
uint16 d;
|
|
|
|
int32 e;
|
|
|
|
uint32 f;
|
|
|
|
int64 g;
|
|
|
|
uint64 h;
|
|
|
|
float32 i;
|
|
|
|
float64 j;
|
|
|
|
void* k;
|
|
|
|
uint16* l;
|
|
|
|
|
|
|
|
if(sizeof(a) != 1) throw("bad a");
|
|
|
|
if(sizeof(b) != 1) throw("bad b");
|
|
|
|
if(sizeof(c) != 2) throw("bad c");
|
|
|
|
if(sizeof(d) != 2) throw("bad d");
|
|
|
|
if(sizeof(e) != 4) throw("bad e");
|
|
|
|
if(sizeof(f) != 4) throw("bad f");
|
|
|
|
if(sizeof(g) != 8) throw("bad g");
|
|
|
|
if(sizeof(h) != 8) throw("bad h");
|
|
|
|
if(sizeof(i) != 4) throw("bad i");
|
|
|
|
if(sizeof(j) != 8) throw("bad j");
|
2009-03-30 01:01:07 -06:00
|
|
|
if(sizeof(k) != sizeof(uintptr)) throw("bad k");
|
|
|
|
if(sizeof(l) != sizeof(uintptr)) throw("bad l");
|
2008-06-30 16:30:47 -06:00
|
|
|
// prints(1"check ok\n");
|
2008-08-04 17:43:49 -06:00
|
|
|
|
|
|
|
uint32 z;
|
|
|
|
z = 1;
|
|
|
|
if(!cas(&z, 1, 2))
|
|
|
|
throw("cas1");
|
|
|
|
if(z != 2)
|
|
|
|
throw("cas2");
|
2008-08-05 15:21:42 -06:00
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
z = 4;
|
|
|
|
if(cas(&z, 5, 6))
|
|
|
|
throw("cas3");
|
|
|
|
if(z != 4)
|
|
|
|
throw("cas4");
|
|
|
|
|
2008-06-30 16:30:47 -06:00
|
|
|
initsig();
|
|
|
|
}
|
2008-07-08 18:19:17 -06:00
|
|
|
|
2008-07-13 15:29:46 -06:00
|
|
|
/*
|
|
|
|
* map and chan helpers for
|
|
|
|
* dealing with unknown types
|
|
|
|
*/
|
2009-06-04 22:09:06 -06:00
|
|
|
static uintptr
|
2008-07-13 15:29:46 -06:00
|
|
|
memhash(uint32 s, void *a)
|
|
|
|
{
|
2008-11-13 11:35:44 -07:00
|
|
|
byte *b;
|
2009-06-04 22:09:06 -06:00
|
|
|
uintptr hash;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
b = a;
|
2009-06-04 22:09:06 -06:00
|
|
|
if(sizeof(hash) == 4)
|
|
|
|
hash = 2860486313U;
|
|
|
|
else
|
|
|
|
hash = 33054211828000289ULL;
|
2008-11-13 11:35:44 -07:00
|
|
|
while(s > 0) {
|
2009-06-04 22:09:06 -06:00
|
|
|
if(sizeof(hash) == 4)
|
|
|
|
hash = (hash ^ *b) * 3267000013UL;
|
|
|
|
else
|
|
|
|
hash = (hash ^ *b) * 23344194077549503ULL;
|
2008-11-13 11:35:44 -07:00
|
|
|
b++;
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
return hash;
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
memequal(uint32 s, void *a, void *b)
|
|
|
|
{
|
|
|
|
byte *ba, *bb;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
ba = a;
|
|
|
|
bb = b;
|
|
|
|
for(i=0; i<s; i++)
|
|
|
|
if(ba[i] != bb[i])
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
memprint(uint32 s, void *a)
|
|
|
|
{
|
|
|
|
uint64 v;
|
|
|
|
|
|
|
|
v = 0xbadb00b;
|
|
|
|
switch(s) {
|
|
|
|
case 1:
|
|
|
|
v = *(uint8*)a;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
v = *(uint16*)a;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
v = *(uint32*)a;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
v = *(uint64*)a;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-25 19:52:55 -07:00
|
|
|
·printint(v);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
memcopy(uint32 s, void *a, void *b)
|
|
|
|
{
|
|
|
|
byte *ba, *bb;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
ba = a;
|
|
|
|
bb = b;
|
|
|
|
if(bb == nil) {
|
|
|
|
for(i=0; i<s; i++)
|
|
|
|
ba[i] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(i=0; i<s; i++)
|
|
|
|
ba[i] = bb[i];
|
|
|
|
}
|
|
|
|
|
2009-06-04 22:09:06 -06:00
|
|
|
static uintptr
|
2009-04-09 19:16:21 -06:00
|
|
|
strhash(uint32 s, String *a)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-11-14 11:57:48 -07:00
|
|
|
USED(s);
|
2009-04-09 19:16:21 -06:00
|
|
|
return memhash((*a).len, (*a).str);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
2009-04-09 19:16:21 -06:00
|
|
|
strequal(uint32 s, String *a, String *b)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-08-05 15:18:47 -06:00
|
|
|
USED(s);
|
2008-07-13 15:29:46 -06:00
|
|
|
return cmpstring(*a, *b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-04-09 19:16:21 -06:00
|
|
|
strprint(uint32 s, String *a)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-08-05 15:18:47 -06:00
|
|
|
USED(s);
|
2010-01-25 19:52:55 -07:00
|
|
|
·printstring(*a);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-06-04 22:09:06 -06:00
|
|
|
static uintptr
|
2009-01-26 10:56:42 -07:00
|
|
|
interhash(uint32 s, Iface *a)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-08-05 15:18:47 -06:00
|
|
|
USED(s);
|
2009-01-26 10:56:42 -07:00
|
|
|
return ifacehash(*a);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-01-26 10:56:42 -07:00
|
|
|
static void
|
|
|
|
interprint(uint32 s, Iface *a)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2009-01-26 10:56:42 -07:00
|
|
|
USED(s);
|
2010-01-25 19:52:55 -07:00
|
|
|
·printiface(*a);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
2009-01-26 10:56:42 -07:00
|
|
|
interequal(uint32 s, Iface *a, Iface *b)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2009-01-26 10:56:42 -07:00
|
|
|
USED(s);
|
|
|
|
return ifaceeq(*a, *b);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-06-04 22:09:06 -06:00
|
|
|
static uintptr
|
2009-05-20 15:57:55 -06:00
|
|
|
nilinterhash(uint32 s, Eface *a)
|
|
|
|
{
|
|
|
|
USED(s);
|
|
|
|
return efacehash(*a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nilinterprint(uint32 s, Eface *a)
|
|
|
|
{
|
|
|
|
USED(s);
|
2010-01-25 19:52:55 -07:00
|
|
|
·printeface(*a);
|
2009-05-20 15:57:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
nilinterequal(uint32 s, Eface *a, Eface *b)
|
|
|
|
{
|
|
|
|
USED(s);
|
|
|
|
return efaceeq(*a, *b);
|
|
|
|
}
|
|
|
|
|
2009-06-04 22:09:06 -06:00
|
|
|
uintptr
|
2009-01-26 10:56:42 -07:00
|
|
|
nohash(uint32 s, void *a)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2009-01-26 10:56:42 -07:00
|
|
|
USED(s);
|
|
|
|
USED(a);
|
|
|
|
throw("hash of unhashable type");
|
|
|
|
return 0;
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-01-26 10:56:42 -07:00
|
|
|
uint32
|
|
|
|
noequal(uint32 s, void *a, void *b)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-08-05 15:18:47 -06:00
|
|
|
USED(s);
|
2009-01-26 10:56:42 -07:00
|
|
|
USED(a);
|
|
|
|
USED(b);
|
|
|
|
throw("comparing uncomparable types");
|
|
|
|
return 0;
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-01-26 13:36:21 -07:00
|
|
|
static void
|
|
|
|
noprint(uint32 s, void *a)
|
|
|
|
{
|
|
|
|
USED(s);
|
|
|
|
USED(a);
|
|
|
|
throw("print of unprintable type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nocopy(uint32 s, void *a, void *b)
|
|
|
|
{
|
|
|
|
USED(s);
|
|
|
|
USED(a);
|
|
|
|
USED(b);
|
|
|
|
throw("copy of uncopyable type");
|
|
|
|
}
|
|
|
|
|
2008-07-13 15:29:46 -06:00
|
|
|
Alg
|
2008-12-19 13:05:22 -07:00
|
|
|
algarray[] =
|
|
|
|
{
|
2009-01-26 10:56:42 -07:00
|
|
|
[AMEM] { memhash, memequal, memprint, memcopy },
|
|
|
|
[ANOEQ] { nohash, noequal, memprint, memcopy },
|
|
|
|
[ASTRING] { strhash, strequal, strprint, memcopy },
|
|
|
|
[AINTER] { interhash, interequal, interprint, memcopy },
|
2009-05-20 15:57:55 -06:00
|
|
|
[ANILINTER] { nilinterhash, nilinterequal, nilinterprint, memcopy },
|
2009-01-26 13:36:21 -07:00
|
|
|
[AFAKE] { nohash, noequal, noprint, nocopy },
|
2008-07-13 15:29:46 -06:00
|
|
|
};
|
2008-11-20 18:32:18 -07:00
|
|
|
|
2009-03-30 01:01:07 -06:00
|
|
|
#pragma textflag 7
|
|
|
|
void
|
|
|
|
FLUSH(void *v)
|
|
|
|
{
|
|
|
|
USED(v);
|
|
|
|
}
|
|
|
|
|