runtime.h 11.2 KB
Newer Older
mb0's avatar
Up.  
mb0 committed
1
/* vim:ts=2:sw=2:expandtab */
2 3 4 5 6 7

#ifndef __RUNTIME_H
#define __RUNTIME_H

#include <stdlib.h>
#include <stdio.h>
mb0's avatar
mb0 committed
8 9
#include <stdint.h>
#include <stdarg.h>
mb0's avatar
mb0 committed
10
#include <stddef.h>
mb0's avatar
mb0 committed
11
#include <string.h>
12

Julian Kranz's avatar
Julian Kranz committed
13
#define __RT_HEAP_SIZE (8*1024*1024)
14

mb0's avatar
mb0 committed
15
#define __CHECK_HEAP(n) /* TODO: check for heap-overflow */
16
#define __ALLOC1() --hp /* TODO: check for heap-overflow */
mb0's avatar
mb0 committed
17
#define __ALLOC0() hp /* TODO: check for heap-overfl(4*1024) */
mb0's avatar
mb0 committed
18 19 20
#define __ALLOCN(n) hp-=n /* TODO: check for heap-overflow */

#define __INVOKE1(o, closure)\
21
  ((__obj(*)(__obj))((o)->label.f))(closure)
22

mb0's avatar
mb0 committed
23
#define __INVOKE2(o, closure, x)\
24
  ((__obj(*)(__obj,__obj))((o)->label.f))(closure, x)
mb0's avatar
mb0 committed
25 26

#define __INVOKE3(o, closure, x, y)\
27
  ((__obj(*)(__obj,__obj,__obj))((o)->label.f))(closure, x, y)
mb0's avatar
mb0 committed
28 29

#define __INVOKE4(o, closure, x, y, z)\
30
  ((__obj(*)(__obj,__obj,__obj,__obj))((o)->label.f))(closure, x, y, z)
mb0's avatar
mb0 committed
31

32
#define __INVOKE5(o, closure, w, x, y, z)\
33
  ((__obj(*)(__obj,__obj,__obj,__obj,__obj))((o)->label.f))(closure, w, x, y, z)
34 35

#define __INVOKE6(o, closure, v, w, x, y, z)\
36
  ((__obj(*)(__obj,__obj,__obj,__obj,__obj,__obj))((o)->label.f))(closure, v, w, x, y, z)
37 38

#define __INVOKE7(o, closure, u, v, w, x, y, z)\
39 40
  ((__obj(*)(__obj,__obj,__obj,__obj,__obj,__obj,__obj))((o)->label.f))(closure, u, v, w, x, y, z)

mb0's avatar
mb0 committed
41
#define __FCALL(f,...) f(__VA_ARGS__)
42

mb0's avatar
mb0 committed
43 44
/** ## Integers */

45
#define __INT_BEGIN(Cname)\
mb0's avatar
mb0 committed
46
  __CHECK_HEAP(1)
47 48

#define __INT_INIT(val)\
mb0's avatar
mb0 committed
49 50 51
  {__objref o = __ALLOC1();\
   o->z.header.tag = __INT;\
   o->z.value = val
52 53
   
#define __INT_END(Cname)\
mb0's avatar
mb0 committed
54
   Cname = __WRAP(o);}
55

mb0's avatar
mb0 committed
56 57
/** ## Labels */

58
#define __LABEL_BEGIN(Cname)\
mb0's avatar
mb0 committed
59
  __CHECK_HEAP(1)
60 61

#define __LABEL_INIT(val)\
mb0's avatar
mb0 committed
62 63 64
  {__objref o = __ALLOC1();\
   o->label.header.tag = __LABEL;\
   o->label.f = (__obj (*)(void)) val
65 66
   
#define __LABEL_END(Cname)\
mb0's avatar
mb0 committed
67
   Cname = __WRAP(o);}
68

mb0's avatar
mb0 committed
69 70
/** ## Closures */

71
#define __CLOSURE_BEGIN(Cname, n)\
mb0's avatar
mb0 committed
72
   __CHECK_HEAP(n+1)
73 74

#define __CLOSURE_ADD(value)\
mb0's avatar
mb0 committed
75 76
   {__objref o = __ALLOC1();\
    *o = *(__UNWRAP(value));}
77 78

#define __CLOSURE_END(Cname, n)\
mb0's avatar
mb0 committed
79 80 81 82 83
   {__objref o = __ALLOC1();\
    o->closure.header.tag = __CLOSURE;\
    o->closure.sz = n;\
    o->closure.env = o+1;\
    Cname = __WRAP(o);}
84 85 86

#define __CLOSURE_REF(Cname, n) (__WRAP(&(Cname->closure.env[n])))

mb0's avatar
mb0 committed
87 88
/** ## Records */

mb0's avatar
Up.  
mb0 committed
89 90 91
/* PERF: A more efficient data-structure for records is needed.
 * maybe versioned arrays or some kind of list/stack with the
 * most recently added fields on-top. */
mb0's avatar
mb0 committed
92
#define __RECORD_BEGIN(Cname, n)\
mb0's avatar
mb0 committed
93
  __CHECK_HEAP(n+1)
mb0's avatar
mb0 committed
94

mb0's avatar
mb0 committed
95 96 97 98 99
#define __RECORD_ADD(field, value)\
  {__objref o = __ALLOC1();\
   o->tagged.header.tag = __TAGGED;\
   o->tagged.tag = field;\
   o->tagged.payload = value;}
mb0's avatar
mb0 committed
100 101

#define __RECORD_END(Cname, n)\
mb0's avatar
mb0 committed
102 103 104 105 106 107 108
  {__objref o = __ALLOC1();\
   o->record.header.tag = __RECORD;\
   o->record.sz = n;\
   o->record.fields = o+1;\
   Cname = __WRAP(o);}

#define __RECORD_BEGIN_UPDATE(Cdst, Csrc)\
109
  {__recordCloneFields((struct __s_record*)Csrc);\
mb0's avatar
mb0 committed
110 111 112 113 114 115 116 117 118 119 120
   __word n = Csrc->record.sz

#define __RECORD_UPDATE(tag, value)\
   n += __recordUpdate(__ALLOC0(),n,tag,value)

#define __RECORD_END_UPDATE(Cdst)\
  {__objref o = __ALLOC1();\
   o->record.header.tag = __RECORD;\
   o->record.sz = n;\
   o->record.fields = o+1;\
   Cdst = __WRAP(o);}}
mb0's avatar
mb0 committed
121 122

#define __RECORD_SELECT(Cname, field)\
123
  __recordLookup(((struct __s_record*)Cname), field)->tagged.payload
mb0's avatar
Up.  
mb0 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

/** ## Ropes/Strings */

#define __ROPE_BEGIN(Cname) /* TODO: CHECK HEAP */

#define __ROPE_CONCAT(a,b)\
  {__objref o = __ALLOC1();\
   o->ropebranch.header.tag = __ROPEBRANCH;\
   o->ropebranch.left = a;\
   o->ropebranch.right = b;

#define __ROPE_FROMCSTRING(s)\
  {__objref o = __ALLOC1();\
   __int len = strlen(s);\
   __int n = len/sizeof(__unwrapped_obj);\
   n = ((len % sizeof(__unwrapped_obj)) == 0) ? n : n + 1;\
   o->ropeleaf.header.tag = __ROPELEAF;\
   o->ropeleaf.sz = len;\
   __objref p = __ALLOCN(n);\
   memcpy(p,s,len);\
   o->ropeleaf.blob = (__char*)p;

#define __ROPE_END(Cname)\
   Cname = __WRAP(o);}

mb0's avatar
mb0 committed
149 150
/** ## Bitvectors */

mb0's avatar
mb0 committed
151 152 153 154 155 156 157 158 159 160 161
#define __BV_BEGIN(Cname, n)\
  __CHECK_HEAP(1)

#define __BV_INIT(value)\
  {__objref o = __ALLOC1();\
   o->bv.header.tag = __BV;\
   o->bv.vec = value;\

#define __BV_END(Cname, n)\
   o->bv.sz = n;\
   Cname = __WRAP(o);}
mb0's avatar
mb0 committed
162 163 164

/** ## Tagged values (datatypes) */

mb0's avatar
mb0 committed
165 166 167 168 169 170 171 172 173 174 175
#define __TAGGED_BEGIN(Cname)\
  __CHECK_HEAP(1)

#define __TAGGED_INIT(con, value)\
  {__objref o = __ALLOC1();\
   o->tagged.header.tag = __TAGGED;\
   o->tagged.tag = con;\
   o->tagged.payload = value;

#define __TAGGED_END(Cname)\
   Cname = __WRAP(o);}
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#define __TAGGED_BEGIN(Cname)\
  __CHECK_HEAP(1)

/** ## Blobs */

#define __BLOB_BEGIN(Cname)\
  __CHECK_HEAP(1)

#define __BLOB_INIT(buf, size)\
  {__objref o = __ALLOC1();\
   o->blob.header.tag = __BLOB;\
   o->blob.blob = buf;\
   o->blob.sz = size;

#define __BLOB_END(Cname)\
   Cname = __WRAP(o);}

194 195 196
#define __LOCAL0(Cname) __obj Cname
#define __LOCAL(Cname, body) __obj Cname = body

mb0's avatar
mb0 committed
197
typedef union __header __header;
198 199 200 201
typedef union __unwrapped_obj __unwrapped_obj;
typedef union __wrapped_obj __wrapped_obj;
typedef __unwrapped_obj* __objref;
typedef __wrapped_obj* __obj;
mb0's avatar
mb0 committed
202 203
typedef uint64_t __word;
typedef int64_t __int;
204
typedef uint8_t __char;
205 206 207 208

enum __tag {
  __CLOSURE,
  __BV,
209
  __INTEGER,
mb0's avatar
mb0 committed
210 211
  __TAGGED,
  __RECORD,
mb0's avatar
mb0 committed
212 213
  __NIL,
  __BLOB,
mb0's avatar
Up.  
mb0 committed
214 215
  __ROPELEAF,
  __ROPEBRANCH,
216 217 218 219 220 221
  __LABEL
};

union __header {
  enum __tag tag;
  __obj ignored;
mb0's avatar
mb0 committed
222
} __attribute__((aligned(8)));
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
struct __s_unwrapped_immediate {
  __header header;
};
struct __s_unwrapped_tagged {
  __header header;
  __word tag;
  __obj payload;
};
struct __s_unwrapped_closure {
  __header header;
  __word sz;
  __objref env;
};
struct __s_unwrapped_record {
  __header header;
  __word sz;
  __objref fields;
};
struct __s_unwrapped_bv {
  __header header;
  __word sz;
  __word vec;
};
struct __s_unwrapped_label {
  __header header;
  __obj (*f)(void);
};
struct __s_unwrapped_blob {
  __header header;
  __char* blob;
  __word sz;
};
struct __s_unwrapped_ropeleaf {
  __header header;
  __char* blob;
  __word sz;
};
struct __s_unwrapped_ropebranch {
  __header header;
  __obj left;
  __obj right;
};
struct __s_unwrapped_int {
  __header header;
  __int value;
} z;

271
union __unwrapped_obj {
272 273 274 275 276 277 278 279 280 281
  struct __s_unwrapped_immediate object;
  struct __s_unwrapped_tagged tagged;
  struct __s_unwrapped_closure closure;
  struct __s_unwrapped_record record;
  struct __s_unwrapped_bv bv;
  struct __s_unwrapped_label label;
  struct __s_unwrapped_blob blob;
  struct __s_unwrapped_ropeleaf ropeleaf;
  struct __s_unwrapped_ropebranch ropebranch;
  struct __s_unwrapped_int z;
mb0's avatar
mb0 committed
282
} __attribute__((aligned(8)));
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
struct __s_tagged {
  __word tag;
  __obj payload;
};
struct __s_closure {
  __word sz;
  __objref env;
};
struct __s_record {
  __word sz;
  __objref fields;
};
struct __s_bv {
  __word sz;
  __word vec;
};
struct __s_label {
  __obj (*f)(void);
};
struct __s_blob {
  __char* blob;
  __word sz;
};
struct __s_ropeleaf {
  __char* blob;
  __word sz;
};
struct __s_ropebranch {
  __obj left;
  __obj right;
};
struct __s_int {
  __int value;
};

319
union __wrapped_obj {
320 321 322 323 324 325 326 327 328
  struct __s_tagged tagged;
  struct __s_closure closure;
  struct __s_record record;
  struct __s_bv bv;
  struct __s_label label;
  struct __s_blob blob;
  struct __s_ropeleaf ropeleaf;
  struct __s_ropebranch ropebranch;
  struct __s_int z;
mb0's avatar
mb0 committed
329
} __attribute__((aligned(8)));
330

mb0's avatar
mb0 committed
331 332
#define __WRAP(x) ((__obj)(((__header*)x)+1))
#define __UNWRAP(x) ((__objref)(((__header*)x)-1))
333
#define __TAG(x) ((__UNWRAP((__obj)x))->object.header.tag)
334 335

#ifndef RELAXEDFATAL
mb0's avatar
mb0 committed
336
#define __FATAL(type) __fatal("%s:%d:%s",__FILE__,__LINE__,#type)
337 338 339 340 341
#else
#define __FATAL(type)\
  {printf("%s:%d:%s\n",__FILE__,__LINE__,#type);\
   return (__UNIT);}
#endif
342

mb0's avatar
mb0 committed
343
void __fatal(char*,...) __attribute__((noreturn));
344 345 346 347 348
extern __unwrapped_obj heap[__RT_HEAP_SIZE] __attribute__((aligned(8)));
extern __objref hp;
extern __obj __UNIT;
extern __obj __TRUE;
extern __obj __FALSE;
mb0's avatar
mb0 committed
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/* ## Constructor tags */

#define __RESERVED 0
@constructors@

/* ## Record field selectors */

#define ___blob 0
@fields@

/* ## Exported functions */

@exports@

/* ## Primitive runtime functions */

mb0's avatar
Up.  
mb0 committed
366 367
const __char* __tagName(__word);
const __char* __fieldName(__word);
368

mb0's avatar
Up.  
mb0 committed
369 370 371 372
__obj __halt(__obj,__obj);
__obj __print(__obj);
__obj __println(__obj);
__obj __traceln(__obj(*)(__obj,__obj),const char*,__obj);
373

374
static inline __objref __recordLookup (struct __s_record* record, __word field) {
mb0's avatar
mb0 committed
375 376 377 378 379
  __word i, sz = record->sz;
  __objref fields = record->fields;
  for (i = 0; i < sz; i++) {
    __objref o = &fields[i];
    if (o->tagged.tag == field)
mb0's avatar
mb0 committed
380
       return (o);
mb0's avatar
mb0 committed
381
  }
382
  if (field < __NFIELDS)
383
    __fatal((char*) "record-field '%s' not found",__fieldName(field));
384
  else
385
    __fatal((char*) "record-field '%zu' not found",field);
mb0's avatar
mb0 committed
386 387
}

mb0's avatar
mb0 committed
388 389 390 391 392 393 394
static inline __word __recordUpdate (__objref fields, __word n, __word field, __obj value) {
  __word i;
  for (i = 0; i < n; i++) {
    __objref o = &fields[i];
    if (o->tagged.tag == field) {
       /* Overwriting already exisiting field */
       o->tagged.payload = value;
mb0's avatar
mb0 committed
395
       return (0);
mb0's avatar
mb0 committed
396 397 398 399 400 401 402
    }
  }
  /* Allocating new record field */
  __objref o = __ALLOC1();
  o->tagged.header.tag = __TAGGED;
  o->tagged.tag = field;
  o->tagged.payload = value;
mb0's avatar
mb0 committed
403
  return (1);
mb0's avatar
mb0 committed
404 405
}

406
static inline void __recordCloneFields (struct __s_record* record) {
mb0's avatar
mb0 committed
407 408 409 410 411
  __word sz = record->sz;
  __objref fields = __ALLOCN(sz);
  memcpy(fields, record->fields, sz*sizeof(__unwrapped_obj));
}

mb0's avatar
Up.  
mb0 committed
412 413 414 415 416 417 418 419 420
/* #define __DECON(o) (o)->tagged.payload */
/* PERF */
static inline __obj __DECON (__obj o) {
  switch (__TAG(o)) {
    case __TAGGED: return (o->tagged.payload);
    default: return o;
  }
}

mb0's avatar
mb0 committed
421 422
static inline __word __CASETAG (__obj o) {
  switch (__TAG(o)) {
423
    case __INTEGER:
mb0's avatar
mb0 committed
424
      return ((__word)o->z.value);
mb0's avatar
mb0 committed
425
    case __TAGGED:
mb0's avatar
mb0 committed
426
      return (o->tagged.tag);
mb0's avatar
mb0 committed
427
    case __BV:
mb0's avatar
mb0 committed
428
      return (o->bv.vec);
mb0's avatar
mb0 committed
429
    default:
430
      __fatal((char*) "__CASETAG() applied to non-tagged object");
mb0's avatar
mb0 committed
431 432 433
  }
}

mb0's avatar
mb0 committed
434 435 436 437 438 439
static inline __word __CASETAGCON (__obj o) { return o->tagged.tag; }

static inline __word __CASETAGVEC (__obj o) { return o->bv.vec; }

static inline __word __CASETAGINT (__obj o) { return o->z.value; }

440 441 442 443 444 445 446 447 448 449 450 451
static inline int __isTrue (__obj o) {
   return (o == __TRUE); /* TODO: or isBitVec(o)&&value='1' */
}

static inline int __isFalse (__obj o) {
   return (o == __FALSE); /* TODO: or isBitVec(o)&&value='0' */
}

static inline void __resetHeap() {
  hp = &heap[__RT_HEAP_SIZE];
}

mb0's avatar
Up.  
mb0 committed
452 453 454 455 456 457
__obj __consume8(__obj);
__obj __unconsume8(__obj);
__obj __consume16(__obj);
__obj __unconsume16(__obj);
__obj __consume32(__obj);
__obj __unconsume32(__obj);
mb0's avatar
mb0 committed
458 459 460 461
__obj __slice(__obj,__obj,__obj);
__obj __concat(__obj,__obj);
__obj __equal(__obj,__obj);
__obj __and(__obj,__obj);
mb0's avatar
Up.  
mb0 committed
462
__obj __or(__obj,__obj);
mb0's avatar
Foo.  
mb0 committed
463 464
__obj __sx(__obj);
__obj __zx(__obj);
mb0's avatar
mb0 committed
465 466 467 468
__obj __raise(__obj);
__obj __not(__obj);
__obj __isNil(__obj);
__obj __printState();
mb0's avatar
Up.  
mb0 committed
469 470 471 472
__obj __concatstring(__obj,__obj);
__obj __showbitvec(__obj);
__obj __showint(__obj);
__obj __flattenstring(__obj,char*,__word);
mb0's avatar
mb0 committed
473

mb0's avatar
Up  
mb0 committed
474 475 476 477 478 479 480 481 482
/** ## Operations on integers */

__obj __eqi(__obj,__obj);
__obj __lti(__obj,__obj);
__obj __lei(__obj,__obj);
__obj __addi(__obj,__obj);
__obj __subi(__obj,__obj);
__obj __muli(__obj,__obj);

mb0's avatar
mb0 committed
483 484 485
/* ## API helpers */

int ___isNil(__obj);
486
__obj __runWithState(__obj(*)(__obj,__obj),__obj);
mb0's avatar
mb0 committed
487
__obj __evalPure(__obj(*)(__obj,__obj),__obj);
mb0's avatar
Up.  
mb0 committed
488
__obj __eval(__obj(*)(__obj,__obj),__char*,__word);
489
__word __decode(__obj(*)(__obj,__obj),__char*,__word,__obj*);
mb0's avatar
Up.  
mb0 committed
490
__obj __pretty(__obj(*)(__obj,__obj),__obj,char*,__word);
mb0's avatar
Foo.  
mb0 committed
491
__obj __translate(__obj(*)(__obj,__obj),__obj);
mb0's avatar
mb0 committed
492 493

#endif /* __RUNTIME_H */
494