Commit 03febb67 authored by Rob Pike's avatar Rob Pike

clean up rearrangement of runtime functions in the source

add isNaN, isInf, NaN, Inf

SVN=125389
parent d3204ef1
......@@ -31,6 +31,10 @@ func envv(int32) string;
func frexp(float64) (int32, float64); // break fp into exp,fract
func ldexp(int32, float64) float64; // make fp from exp,fract
func modf(float64) (float64, float64); // break fp into double.double
func isInf(float64, int32) bool; // test for infinity
func isNaN(float64) bool; // test for not-a-number
func Inf(int32) float64; // return signed Inf
func NaN() float64; // return a NaN
func newmap(keysize uint32, valsize uint32,
keyalg uint32, valalg uint32,
......@@ -42,7 +46,7 @@ func mapassign2(hmap *map[any]any, key any, val any, pres bool);
func readfile(string) (string, bool); // read file into string; boolean status
func exit(int32);
func exit(int32);
export
mal
......@@ -75,6 +79,10 @@ export
frexp
ldexp
modf
isInf,
isNaN,
Inf,
NaN,
// op map
newmap
......
......@@ -3,10 +3,10 @@ char* sysimport =
"type sys._e002 {}\n"
"type sys.any 24\n"
"type sys._e003 *sys.any\n"
"type sys._o214 {_e212 sys._e003}\n"
"type sys._o247 {_e245 sys._e003}\n"
"type sys.uint32 6\n"
"type sys._i216 {_e213 sys.uint32}\n"
"type sys._e001 (sys._e002 sys._o214 sys._i216)\n"
"type sys._i249 {_e246 sys.uint32}\n"
"type sys._e001 (sys._e002 sys._o247 sys._i249)\n"
"var !sys.mal sys._e001\n"
"type sys._e005 {}\n"
"type sys._e006 {}\n"
......@@ -16,161 +16,181 @@ char* sysimport =
"type sys._e009 {}\n"
"type sys._e010 {}\n"
"type sys.int32 5\n"
"type sys._i222 {_e221 sys.int32}\n"
"type sys._e008 (sys._e009 sys._e010 sys._i222)\n"
"type sys._i255 {_e254 sys.int32}\n"
"type sys._e008 (sys._e009 sys._e010 sys._i255)\n"
"var !sys.panicl sys._e008\n"
"type sys._e012 {}\n"
"type sys._e013 {}\n"
"type sys.bool 12\n"
"type sys._i227 {_e226 sys.bool}\n"
"type sys._e011 (sys._e012 sys._e013 sys._i227)\n"
"type sys._i260 {_e259 sys.bool}\n"
"type sys._e011 (sys._e012 sys._e013 sys._i260)\n"
"var !sys.printbool sys._e011\n"
"type sys._e015 {}\n"
"type sys._e016 {}\n"
"type sys.float64 10\n"
"type sys._i232 {_e231 sys.float64}\n"
"type sys._e014 (sys._e015 sys._e016 sys._i232)\n"
"type sys._i265 {_e264 sys.float64}\n"
"type sys._e014 (sys._e015 sys._e016 sys._i265)\n"
"var !sys.printfloat sys._e014\n"
"type sys._e018 {}\n"
"type sys._e019 {}\n"
"type sys.int64 7\n"
"type sys._i237 {_e236 sys.int64}\n"
"type sys._e017 (sys._e018 sys._e019 sys._i237)\n"
"type sys._i270 {_e269 sys.int64}\n"
"type sys._e017 (sys._e018 sys._e019 sys._i270)\n"
"var !sys.printint sys._e017\n"
"type sys._e021 {}\n"
"type sys._e022 {}\n"
"type sys._e023 25\n"
"type sys.string *sys._e023\n"
"type sys._i242 {_e241 sys.string}\n"
"type sys._e020 (sys._e021 sys._e022 sys._i242)\n"
"type sys._i275 {_e274 sys.string}\n"
"type sys._e020 (sys._e021 sys._e022 sys._i275)\n"
"var !sys.printstring sys._e020\n"
"type sys._e025 {}\n"
"type sys._e026 {}\n"
"type sys.uint8 2\n"
"type sys._e027 *sys.uint8\n"
"type sys._i247 {_e246 sys._e027}\n"
"type sys._e024 (sys._e025 sys._e026 sys._i247)\n"
"type sys._i280 {_e279 sys._e027}\n"
"type sys._e024 (sys._e025 sys._e026 sys._i280)\n"
"var !sys.printpointer sys._e024\n"
"type sys._e029 {}\n"
"type sys._o254 {_e251 sys.string}\n"
"type sys._i256 {_e252 sys.string _e253 sys.string}\n"
"type sys._e028 (sys._e029 sys._o254 sys._i256)\n"
"type sys._o287 {_e284 sys.string}\n"
"type sys._i289 {_e285 sys.string _e286 sys.string}\n"
"type sys._e028 (sys._e029 sys._o287 sys._i289)\n"
"var !sys.catstring sys._e028\n"
"type sys._e031 {}\n"
"type sys._o264 {_e261 sys.int32}\n"
"type sys._i266 {_e262 sys.string _e263 sys.string}\n"
"type sys._e030 (sys._e031 sys._o264 sys._i266)\n"
"type sys._o297 {_e294 sys.int32}\n"
"type sys._i299 {_e295 sys.string _e296 sys.string}\n"
"type sys._e030 (sys._e031 sys._o297 sys._i299)\n"
"var !sys.cmpstring sys._e030\n"
"type sys._e033 {}\n"
"type sys._o275 {_e271 sys.string}\n"
"type sys._i277 {_e272 sys.string _e273 sys.int32 _e274 sys.int32}\n"
"type sys._e032 (sys._e033 sys._o275 sys._i277)\n"
"type sys._o308 {_e304 sys.string}\n"
"type sys._i310 {_e305 sys.string _e306 sys.int32 _e307 sys.int32}\n"
"type sys._e032 (sys._e033 sys._o308 sys._i310)\n"
"var !sys.slicestring sys._e032\n"
"type sys._e035 {}\n"
"type sys._o286 {_e283 sys.uint8}\n"
"type sys._i288 {_e284 sys.string _e285 sys.int32}\n"
"type sys._e034 (sys._e035 sys._o286 sys._i288)\n"
"type sys._o319 {_e316 sys.uint8}\n"
"type sys._i321 {_e317 sys.string _e318 sys.int32}\n"
"type sys._e034 (sys._e035 sys._o319 sys._i321)\n"
"var !sys.indexstring sys._e034\n"
"type sys._e037 {}\n"
"type sys._o295 {_e293 sys.string}\n"
"type sys._i297 {_e294 sys.int64}\n"
"type sys._e036 (sys._e037 sys._o295 sys._i297)\n"
"type sys._o328 {_e326 sys.string}\n"
"type sys._i330 {_e327 sys.int64}\n"
"type sys._e036 (sys._e037 sys._o328 sys._i330)\n"
"var !sys.intstring sys._e036\n"
"type sys._e039 {}\n"
"type sys._o304 {_e301 sys.string}\n"
"type sys._o337 {_e334 sys.string}\n"
"type sys._e040 *sys.uint8\n"
"type sys._i306 {_e302 sys._e040 _e303 sys.int32}\n"
"type sys._e038 (sys._e039 sys._o304 sys._i306)\n"
"type sys._i339 {_e335 sys._e040 _e336 sys.int32}\n"
"type sys._e038 (sys._e039 sys._o337 sys._i339)\n"
"var !sys.byteastring sys._e038\n"
"type sys._e042 {}\n"
"type sys._e043 <>\n"
"type sys._o315 {_e311 sys._e043}\n"
"type sys._o348 {_e344 sys._e043}\n"
"type sys._e044 *sys.uint8\n"
"type sys._e045 *sys.uint8\n"
"type sys._s322 {}\n"
"type sys._e046 *sys._s322\n"
"type sys._i317 {_e312 sys._e044 _e313 sys._e045 _e314 sys._e046}\n"
"type sys._e041 (sys._e042 sys._o315 sys._i317)\n"
"type sys._s355 {}\n"
"type sys._e046 *sys._s355\n"
"type sys._i350 {_e345 sys._e044 _e346 sys._e045 _e347 sys._e046}\n"
"type sys._e041 (sys._e042 sys._o348 sys._i350)\n"
"var !sys.mkiface sys._e041\n"
"type sys._e048 {}\n"
"type sys._o326 {_e325 sys.int32}\n"
"type sys._o359 {_e358 sys.int32}\n"
"type sys._e049 {}\n"
"type sys._e047 (sys._e048 sys._o326 sys._e049)\n"
"type sys._e047 (sys._e048 sys._o359 sys._e049)\n"
"var !sys.argc sys._e047\n"
"type sys._e051 {}\n"
"type sys._o330 {_e329 sys.int32}\n"
"type sys._o363 {_e362 sys.int32}\n"
"type sys._e052 {}\n"
"type sys._e050 (sys._e051 sys._o330 sys._e052)\n"
"type sys._e050 (sys._e051 sys._o363 sys._e052)\n"
"var !sys.envc sys._e050\n"
"type sys._e054 {}\n"
"type sys._o335 {_e333 sys.string}\n"
"type sys._i337 {_e334 sys.int32}\n"
"type sys._e053 (sys._e054 sys._o335 sys._i337)\n"
"type sys._o368 {_e366 sys.string}\n"
"type sys._i370 {_e367 sys.int32}\n"
"type sys._e053 (sys._e054 sys._o368 sys._i370)\n"
"var !sys.argv sys._e053\n"
"type sys._e056 {}\n"
"type sys._o343 {_e341 sys.string}\n"
"type sys._i345 {_e342 sys.int32}\n"
"type sys._e055 (sys._e056 sys._o343 sys._i345)\n"
"type sys._o376 {_e374 sys.string}\n"
"type sys._i378 {_e375 sys.int32}\n"
"type sys._e055 (sys._e056 sys._o376 sys._i378)\n"
"var !sys.envv sys._e055\n"
"type sys._e058 {}\n"
"type sys._o352 {_e349 sys.int32 _e350 sys.float64}\n"
"type sys._i354 {_e351 sys.float64}\n"
"type sys._e057 (sys._e058 sys._o352 sys._i354)\n"
"type sys._o385 {_e382 sys.int32 _e383 sys.float64}\n"
"type sys._i387 {_e384 sys.float64}\n"
"type sys._e057 (sys._e058 sys._o385 sys._i387)\n"
"var !sys.frexp sys._e057\n"
"type sys._e060 {}\n"
"type sys._o361 {_e358 sys.float64}\n"
"type sys._i363 {_e359 sys.int32 _e360 sys.float64}\n"
"type sys._e059 (sys._e060 sys._o361 sys._i363)\n"
"type sys._o394 {_e391 sys.float64}\n"
"type sys._i396 {_e392 sys.int32 _e393 sys.float64}\n"
"type sys._e059 (sys._e060 sys._o394 sys._i396)\n"
"var !sys.ldexp sys._e059\n"
"type sys._e062 {}\n"
"type sys._o371 {_e368 sys.float64 _e369 sys.float64}\n"
"type sys._i373 {_e370 sys.float64}\n"
"type sys._e061 (sys._e062 sys._o371 sys._i373)\n"
"type sys._o404 {_e401 sys.float64 _e402 sys.float64}\n"
"type sys._i406 {_e403 sys.float64}\n"
"type sys._e061 (sys._e062 sys._o404 sys._i406)\n"
"var !sys.modf sys._e061\n"
"type sys._e064 {}\n"
"type sys._e066 [sys.any] sys.any\n"
"type sys._e065 *sys._e066\n"
"type sys._o377 {hmap sys._e065}\n"
"type sys._i379 {keysize sys.uint32 valsize sys.uint32 keyalg sys.uint32 valalg sys.uint32 hint sys.uint32}\n"
"type sys._e063 (sys._e064 sys._o377 sys._i379)\n"
"var !sys.newmap sys._e063\n"
"type sys._o413 {_e410 sys.bool}\n"
"type sys._i415 {_e411 sys.float64 _e412 sys.int32}\n"
"type sys._e063 (sys._e064 sys._o413 sys._i415)\n"
"var !sys.isInf sys._e063\n"
"type sys._e066 {}\n"
"type sys._o422 {_e420 sys.bool}\n"
"type sys._i424 {_e421 sys.float64}\n"
"type sys._e065 (sys._e066 sys._o422 sys._i424)\n"
"var !sys.isNaN sys._e065\n"
"type sys._e068 {}\n"
"type sys._o387 {val sys.any}\n"
"type sys._e070 [sys.any] sys.any\n"
"type sys._e069 *sys._e070\n"
"type sys._i389 {hmap sys._e069 key sys.any}\n"
"type sys._e067 (sys._e068 sys._o387 sys._i389)\n"
"var !sys.mapaccess1 sys._e067\n"
"type sys._e072 {}\n"
"type sys._o394 {val sys.any pres sys.bool}\n"
"type sys._e074 [sys.any] sys.any\n"
"type sys._e073 *sys._e074\n"
"type sys._i396 {hmap sys._e073 key sys.any}\n"
"type sys._e071 (sys._e072 sys._o394 sys._i396)\n"
"var !sys.mapaccess2 sys._e071\n"
"type sys._e076 {}\n"
"type sys._o430 {_e428 sys.float64}\n"
"type sys._i432 {_e429 sys.int32}\n"
"type sys._e067 (sys._e068 sys._o430 sys._i432)\n"
"var !sys.Inf sys._e067\n"
"type sys._e070 {}\n"
"type sys._o437 {_e436 sys.float64}\n"
"type sys._e071 {}\n"
"type sys._e069 (sys._e070 sys._o437 sys._e071)\n"
"var !sys.NaN sys._e069\n"
"type sys._e073 {}\n"
"type sys._e075 [sys.any] sys.any\n"
"type sys._e074 *sys._e075\n"
"type sys._o440 {hmap sys._e074}\n"
"type sys._i442 {keysize sys.uint32 valsize sys.uint32 keyalg sys.uint32 valalg sys.uint32 hint sys.uint32}\n"
"type sys._e072 (sys._e073 sys._o440 sys._i442)\n"
"var !sys.newmap sys._e072\n"
"type sys._e077 {}\n"
"type sys._o450 {val sys.any}\n"
"type sys._e079 [sys.any] sys.any\n"
"type sys._e078 *sys._e079\n"
"type sys._i401 {hmap sys._e078 key sys.any val sys.any}\n"
"type sys._e075 (sys._e076 sys._e077 sys._i401)\n"
"var !sys.mapassign1 sys._e075\n"
"type sys._i452 {hmap sys._e078 key sys.any}\n"
"type sys._e076 (sys._e077 sys._o450 sys._i452)\n"
"var !sys.mapaccess1 sys._e076\n"
"type sys._e081 {}\n"
"type sys._e082 {}\n"
"type sys._e084 [sys.any] sys.any\n"
"type sys._e083 *sys._e084\n"
"type sys._i407 {hmap sys._e083 key sys.any val sys.any pres sys.bool}\n"
"type sys._e080 (sys._e081 sys._e082 sys._i407)\n"
"var !sys.mapassign2 sys._e080\n"
"type sys._o457 {val sys.any pres sys.bool}\n"
"type sys._e083 [sys.any] sys.any\n"
"type sys._e082 *sys._e083\n"
"type sys._i459 {hmap sys._e082 key sys.any}\n"
"type sys._e080 (sys._e081 sys._o457 sys._i459)\n"
"var !sys.mapaccess2 sys._e080\n"
"type sys._e085 {}\n"
"type sys._e086 {}\n"
"type sys._o417 {_e414 sys.string _e415 sys.bool}\n"
"type sys._i419 {_e416 sys.string}\n"
"type sys._e085 (sys._e086 sys._o417 sys._i419)\n"
"var !sys.readfile sys._e085\n"
"type sys._e088 {}\n"
"type sys._e089 {}\n"
"type sys._i424 {_e423 sys.int32}\n"
"type sys._e087 (sys._e088 sys._e089 sys._i424)\n"
"var !sys.exit sys._e087\n"
"type sys._e088 [sys.any] sys.any\n"
"type sys._e087 *sys._e088\n"
"type sys._i464 {hmap sys._e087 key sys.any val sys.any}\n"
"type sys._e084 (sys._e085 sys._e086 sys._i464)\n"
"var !sys.mapassign1 sys._e084\n"
"type sys._e090 {}\n"
"type sys._e091 {}\n"
"type sys._e093 [sys.any] sys.any\n"
"type sys._e092 *sys._e093\n"
"type sys._i470 {hmap sys._e092 key sys.any val sys.any pres sys.bool}\n"
"type sys._e089 (sys._e090 sys._e091 sys._i470)\n"
"var !sys.mapassign2 sys._e089\n"
"type sys._e095 {}\n"
"type sys._o480 {_e477 sys.string _e478 sys.bool}\n"
"type sys._i482 {_e479 sys.string}\n"
"type sys._e094 (sys._e095 sys._o480 sys._i482)\n"
"var !sys.readfile sys._e094\n"
"type sys._e097 {}\n"
"type sys._e098 {}\n"
"type sys._i487 {_e486 sys.int32}\n"
"type sys._e096 (sys._e097 sys._e098 sys._i487)\n"
"var !sys.exit sys._e096\n"
"))\n"
;
......@@ -109,3 +109,464 @@ sys·mal(uint32 n, uint8 *ret)
ret = mal(n);
FLUSH(&ret);
}
static Map* hash[1009];
static Map*
hashmap(Sigi *si, Sigs *ss)
{
int32 ns, ni;
uint32 ihash, h;
byte *sname, *iname;
Map *m;
h = ((uint32)si + (uint32)ss) % nelem(hash);
for(m=hash[h]; m!=nil; m=m->link) {
if(m->si == si && m->ss == ss) {
if(m->bad) {
throw("bad hashmap");
m = nil;
}
// prints("old hashmap\n");
return m;
}
}
ni = si[0].offset; // first word has size
m = mal(sizeof(*m) + ni*sizeof(m->fun[0]));
m->si = si;
m->ss = ss;
ni = 1; // skip first word
ns = 0;
loop1:
// pick up next name from
// interface signature
iname = si[ni].name;
if(iname == nil) {
m->link = hash[h];
hash[h] = m;
// prints("new hashmap\n");
return m;
}
ihash = si[ni].hash;
loop2:
// pick up and comapre next name
// from structure signature
sname = ss[ns].name;
if(sname == nil) {
prints((int8*)iname);
prints(": ");
throw("hashmap: failed to find method");
m->bad = 1;
m->link = hash[h];
hash[h] = m;
return nil;
}
if(ihash != ss[ns].hash ||
strcmp(sname, iname) != 0) {
ns++;
goto loop2;
}
m->fun[si[ni].offset] = ss[ns].fun;
ni++;
goto loop1;
}
void
sys·ifaces2i(Sigi *si, Sigs *ss, Map *m, void *s)
{
if(debug) {
prints("s2i sigi=");
sys·printpointer(si);
prints(" sigs=");
sys·printpointer(ss);
prints(" s=");
sys·printpointer(s);
}
if(s == nil) {
throw("ifaces2i: nil pointer");
m = nil;
FLUSH(&m);
return;
}
m = hashmap(si, ss);
if(debug) {
prints(" returning m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
dump((byte*)m, 64);
}
FLUSH(&m);
}
void
sys·ifacei2i(Sigi *si, Map *m, void *s)
{
if(debug) {
prints("i2i sigi=");
sys·printpointer(si);
prints(" m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
}
if(m == nil) {
throw("ifacei2i: nil map");
s = nil;
FLUSH(&s);
return;
}
if(m->si == nil) {
throw("ifacei2i: nil pointer");
return;
}
if(m->si != si) {
m = hashmap(si, m->ss);
FLUSH(&m);
}
if(debug) {
prints(" returning m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
dump((byte*)m, 64);
}
}
void
sys·ifacei2s(Sigs *ss, Map *m, void *s)
{
if(debug) {
prints("i2s m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
}
if(m == nil) {
throw("ifacei2s: nil map");
s = nil;
FLUSH(&s);
return;
}
if(m->ss != ss) {
dump((byte*)m, 64);
throw("ifacei2s: wrong pointer");
s = nil;
FLUSH(&s);
return;
}
}
enum
{
NANEXP = 2047<<20,
NANMASK = 2047<<20,
NANSIGN = 1<<31,
};
static uint64 uvnan = 0x7FF0000000000001;
static uint64 uvinf = 0x7FF0000000000000;
static uint64 uvneginf = 0xFFF0000000000000;
static int32
isInf(float64 d, int32 sign)
{
uint64 x;
x = *(uint64*)&d;
if(sign == 0) {
if(x == uvinf || x == uvneginf)
return 1;
return 0;
}
if(sign > 0) {
if(x == uvinf)
return 1;
return 0;
}
if(x == uvneginf)
return 1;
return 0;
}
static float64
NaN(void)
{
return *(float64*)&uvnan;
}
static int32
isNaN(float64 d)
{
uint64 x;
x = *(uint64*)&d;
return ((uint32)x>>32)==0x7FF00000 && !isInf(d, 0);
}
static float64
Inf(int32 sign)
{
if(sign < 0)
return *(float64*)&uvinf;
else
return *(float64*)&uvneginf;
}
enum
{
MASK = 0x7ffL,
SHIFT = 64-11-1,
BIAS = 1022L,
};
static float64
frexp(float64 d, int32 *ep)
{
uint64 x;
if(d == 0) {
*ep = 0;
return 0;
}
x = *(uint64*)&d;
*ep = (int32)((x >> SHIFT) & MASK) - BIAS;
x &= ~((uint64)MASK << SHIFT);
x |= (uint64)BIAS << SHIFT;
return *(float64*)&x;
}
static float64
ldexp(float64 d, int32 e)
{
uint64 x;
if(d == 0)
return 0;
x = *(uint64*)&d;
e += (int32)(x >> SHIFT) & MASK;
if(e <= 0)
return 0; /* underflow */
if(e >= MASK){ /* overflow */
if(d < 0)
return Inf(-1);
return Inf(1);
}
x &= ~((uint64)MASK << SHIFT);
x |= (uint64)e << SHIFT;
return *(float64*)&x;
}
static float64
modf(float64 d, float64 *ip)
{
float64 dd;
uint64 x;
int32 e;
if(d < 1) {
if(d < 0) {
d = modf(-d, ip);
*ip = -*ip;
return -d;
}
*ip = 0;
return d;
}
x = *(uint64*)&d;
e = (int32)((x >> SHIFT) & MASK) - BIAS;
/*
* Keep the top 11+e bits; clear the rest.
*/
if(e <= 64-11)
x &= ~((uint64)1 << (64-11-e))-1;
dd = *(float64*)&x;
*ip = dd;
return d - dd;
}
// func frexp(float64) (int32, float64); // break fp into exp,fract
void
sys·frexp(float64 din, int32 iou, float64 dou)
{
dou = frexp(din, &iou);
FLUSH(&dou);
}
//func ldexp(int32, float64) float64; // make fp from exp,fract
void
sys·ldexp(float64 din, int32 ein, float64 dou)
{
dou = ldexp(din, ein);
FLUSH(&dou);
}
//func modf(float64) (float64, float64); // break fp into double+double
float64
sys·modf(float64 din, float64 dou1, float64 dou2)
{
dou1 = modf(din, &dou2);
FLUSH(&dou2);
}
//func isinf(float64, int32 sign) bool; // test for infinity
void
sys·isInf(float64 din, int32 signin, bool out)
{
out = isInf(din, signin);
FLUSH(&out);
}
//func isnan(float64) bool; // test for NaN
void
sys·isNaN(float64 din, bool out)
{
out = isNaN(din);
FLUSH(&out);
}
//func inf(int32 sign) float64; // signed infinity
void
sys·Inf(int32 signin, float64 out)
{
out = Inf(signin);
FLUSH(&out);
}
//func nan() float64; // NaN
void
sys·NaN(float64 out)
{
out = NaN();
FLUSH(&out);
}
static int32 argc;
static uint8** argv;
static int32 envc;
static uint8** envv;
void
args(int32 c, uint8 **v)
{
argc = c;
argv = v;
envv = v + argc + 1; // skip 0 at end of argv
for (envc = 0; envv[envc] != 0; envc++)
;
}
//func argc() int32; // return number of arguments
void
sys·argc(int32 v)
{
v = argc;
FLUSH(&v);
}
//func envc() int32; // return number of environment variables
void
sys·envc(int32 v)
{
v = envc;
FLUSH(&v);
}
//func argv(i) string; // return argument i
void
sys·argv(int32 i, string s)
{
uint8* str;
int32 l;
if(i < 0 || i >= argc) {
s = emptystring;
goto out;
}
str = argv[i];
l = findnull((int8*)str);
s = mal(sizeof(s->len)+l);
s->len = l;
mcpy(s->str, str, l);
out:
FLUSH(&s);
}
//func envv(i) string; // return argument i
void
sys·envv(int32 i, string s)
{
uint8* str;
int32 l;
if(i < 0 || i >= envc) {
s = emptystring;
goto out;
}
str = envv[i];
l = findnull((int8*)str);
s = mal(sizeof(s->len)+l);
s->len = l;
mcpy(s->str, str, l);
out:
FLUSH(&s);
}
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");
if(sizeof(k) != 8) throw("bad k");
if(sizeof(l) != 8) throw("bad l");
// prints(1"check ok\n");
initsig();
}
......@@ -4,435 +4,6 @@
#include "runtime.h"
static Map* hash[1009];
static Map*
hashmap(Sigi *si, Sigs *ss)
{
int32 ns, ni;
uint32 ihash, h;
byte *sname, *iname;
Map *m;
h = ((uint32)si + (uint32)ss) % nelem(hash);
for(m=hash[h]; m!=nil; m=m->link) {
if(m->si == si && m->ss == ss) {
if(m->bad) {
throw("bad hashmap");
m = nil;
}
// prints("old hashmap\n");
return m;
}
}
ni = si[0].offset; // first word has size
m = mal(sizeof(*m) + ni*sizeof(m->fun[0]));
m->si = si;
m->ss = ss;
ni = 1; // skip first word
ns = 0;
loop1:
// pick up next name from
// interface signature
iname = si[ni].name;
if(iname == nil) {
m->link = hash[h];
hash[h] = m;
// prints("new hashmap\n");
return m;
}
ihash = si[ni].hash;
loop2:
// pick up and comapre next name
// from structure signature
sname = ss[ns].name;
if(sname == nil) {
prints((int8*)iname);
prints(": ");
throw("hashmap: failed to find method");
m->bad = 1;
m->link = hash[h];
hash[h] = m;
return nil;
}
if(ihash != ss[ns].hash ||
strcmp(sname, iname) != 0) {
ns++;
goto loop2;
}
m->fun[si[ni].offset] = ss[ns].fun;
ni++;
goto loop1;
}
void
sys·ifaces2i(Sigi *si, Sigs *ss, Map *m, void *s)
{
if(debug) {
prints("s2i sigi=");
sys·printpointer(si);
prints(" sigs=");
sys·printpointer(ss);
prints(" s=");
sys·printpointer(s);
}
if(s == nil) {
throw("ifaces2i: nil pointer");
m = nil;
FLUSH(&m);
return;
}
m = hashmap(si, ss);
if(debug) {
prints(" returning m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
dump((byte*)m, 64);
}
FLUSH(&m);
}
void
sys·ifacei2i(Sigi *si, Map *m, void *s)
{
if(debug) {
prints("i2i sigi=");
sys·printpointer(si);
prints(" m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
}
if(m == nil) {
throw("ifacei2i: nil map");
s = nil;
FLUSH(&s);
return;
}
if(m->si == nil) {
throw("ifacei2i: nil pointer");
return;
}
if(m->si != si) {
m = hashmap(si, m->ss);
FLUSH(&m);
}
if(debug) {
prints(" returning m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
dump((byte*)m, 64);
}
}
void
sys·ifacei2s(Sigs *ss, Map *m, void *s)
{
if(debug) {
prints("i2s m=");
sys·printpointer(m);
prints(" s=");
sys·printpointer(s);
prints("\n");
}
if(m == nil) {
throw("ifacei2s: nil map");
s = nil;
FLUSH(&s);
return;
}
if(m->ss != ss) {
dump((byte*)m, 64);
throw("ifacei2s: wrong pointer");
s = nil;
FLUSH(&s);
return;
}
}
enum
{
NANEXP = 2047<<20,
NANMASK = 2047<<20,
NANSIGN = 1<<31,
};
static uint64 uvnan = 0x7FF0000000000001;
static uint64 uvinf = 0x7FF0000000000000;
static uint64 uvneginf = 0xFFF0000000000000;
static int32
isInf(float64 d, int32 sign)
{
uint64 x;
x = *(uint64*)&d;
if(sign == 0) {
if(x == uvinf || x == uvneginf)
return 1;
return 0;
}
if(sign > 0) {
if(x == uvinf)
return 1;
return 0;
}
if(x == uvneginf)
return 1;
return 0;
}
static float64
NaN(void)
{
return *(float64*)&uvnan;
}
static int32
isNaN(float64 d)
{
uint64 x;
x = *(uint64*)&d;
return ((uint32)x>>32)==0x7FF00000 && !isInf(d, 0);
}
static float64
Inf(int32 sign)
{
if(sign < 0)
return *(float64*)&uvinf;
else
return *(float64*)&uvneginf;
}
enum
{
MASK = 0x7ffL,
SHIFT = 64-11-1,
BIAS = 1022L,
};
static float64
frexp(float64 d, int32 *ep)
{
uint64 x;
if(d == 0) {
*ep = 0;
return 0;
}
x = *(uint64*)&d;
*ep = (int32)((x >> SHIFT) & MASK) - BIAS;
x &= ~((uint64)MASK << SHIFT);
x |= (uint64)BIAS << SHIFT;
return *(float64*)&x;
}
static float64
ldexp(float64 d, int32 e)
{
uint64 x;
if(d == 0)
return 0;
x = *(uint64*)&d;
e += (int32)(x >> SHIFT) & MASK;
if(e <= 0)
return 0; /* underflow */
if(e >= MASK){ /* overflow */
if(d < 0)
return Inf(-1);
return Inf(1);
}
x &= ~((uint64)MASK << SHIFT);
x |= (uint64)e << SHIFT;
return *(float64*)&x;
}
static float64
modf(float64 d, float64 *ip)
{
float64 dd;
uint64 x;
int32 e;
if(d < 1) {
if(d < 0) {
d = modf(-d, ip);
*ip = -*ip;
return -d;
}
*ip = 0;
return d;
}
x = *(uint64*)&d;
e = (int32)((x >> SHIFT) & MASK) - BIAS;
/*
* Keep the top 11+e bits; clear the rest.
*/
if(e <= 64-11)
x &= ~((uint64)1 << (64-11-e))-1;
dd = *(float64*)&x;
*ip = dd;
return d - dd;
}
// func frexp(float64) (int32, float64); // break fp into exp,fract
void
sys·frexp(float64 din, int32 iou, float64 dou)
{
dou = frexp(din, &iou);
FLUSH(&dou);
}
//func ldexp(int32, float64) float64; // make fp from exp,fract
void
sys·ldexp(float64 din, int32 ein, float64 dou)
{
dou = ldexp(din, ein);
FLUSH(&dou);
}
//func modf(float64) (float64, float64); // break fp into double+double
float64
sys·modf(float64 din, float64 dou1, float64 dou2)
{
dou1 = modf(din, &dou2);
FLUSH(&dou2);
}
static int32 argc;
static uint8** argv;
static int32 envc;
static uint8** envv;
void
args(int32 c, uint8 **v)
{
argc = c;
argv = v;
envv = v + argc + 1; // skip 0 at end of argv
for (envc = 0; envv[envc] != 0; envc++)
;
}
//func argc() int32; // return number of arguments
void
sys·argc(int32 v)
{
v = argc;
FLUSH(&v);
}
//func envc() int32; // return number of environment variables
void
sys·envc(int32 v)
{
v = envc;
FLUSH(&v);
}
//func argv(i) string; // return argument i
void
sys·argv(int32 i, string s)
{
uint8* str;
int32 l;
if(i < 0 || i >= argc) {
s = emptystring;
goto out;
}
str = argv[i];
l = findnull((int8*)str);
s = mal(sizeof(s->len)+l);
s->len = l;
mcpy(s->str, str, l);
out:
FLUSH(&s);
}
//func envv(i) string; // return argument i
void
sys·envv(int32 i, string s)
{
uint8* str;
int32 l;
if(i < 0 || i >= envc) {
s = emptystring;
goto out;
}
str = envv[i];
l = findnull((int8*)str);
s = mal(sizeof(s->len)+l);
s->len = l;
mcpy(s->str, str, l);
out:
FLUSH(&s);
}
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");
if(sizeof(k) != 8) throw("bad k");
if(sizeof(l) != 8) throw("bad l");
// prints(1"check ok\n");
initsig();
}
typedef struct Link Link;
typedef struct Hmap Hmap;
typedef struct Alg Alg;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment