1 /* file: "picobit-vm.h" */
4 * Copyright 2004-2009 by Marc Feeley and Vincent St-Amour, All Rights Reserved.
12 #define INFINITE_PRECISION_BIGNUMS
14 /*---------------------------------------------------------------------------*/
19 // these types are already defined in SIXPIC
23 typedef unsigned char uint8
;
24 typedef unsigned short uint16
;
25 typedef unsigned long uint32
;
30 typedef uint16 ram_addr
;
31 typedef uint16 rom_addr
;
33 // pointers are 13 bits
36 /*---------------------------------------------------------------------------*/
65 static volatile near uint8 FW_VALUE_UP @
0x33;
66 static volatile near uint8 FW_VALUE_HI @
0x33;
67 static volatile near uint8 FW_VALUE_LO @
0x33;
69 #define ACTIVITY_LED1_LAT LATB
70 #define ACTIVITY_LED1_BIT 5
71 #define ACTIVITY_LED2_LAT LATB
72 #define ACTIVITY_LED2_BIT 4
73 static volatile near bit ACTIVITY_LED1 @
((unsigned)&ACTIVITY_LED1_LAT
*8)+ACTIVITY_LED1_BIT
;
74 static volatile near bit ACTIVITY_LED2 @
((unsigned)&ACTIVITY_LED2_LAT
*8)+ACTIVITY_LED2_BIT
;
86 #define MAX_PACKET_SIZE BUFSIZ
89 char errbuf
[PCAP_ERRBUF_SIZE
];
91 #define INTERFACE "eth0"
92 char buf
[MAX_PACKET_SIZE
]; // buffer for writing
97 #include <sys/types.h>
98 #include <sys/timeb.h>
103 #include <sys/time.h>
109 /*---------------------------------------------------------------------------*/
111 // miscellaneous definitions
112 // TODO put at the end ?
114 // TODO these 2 are only used in negp, use them elsewhere ?
118 #define CODE_START 0x8000
120 /*---------------------------------------------------------------------------*/
125 #define IF_TRACE(x) x
126 #define IF_GC_TRACE(x) x
129 #define IF_GC_TRACE(x)
132 /*---------------------------------------------------------------------------*/
137 void halt_with_error () {while(1);}
141 #define ERROR(prim, msg) error (prim, msg)
142 #define TYPE_ERROR(prim, type) type_error (prim, type)
143 void error (char *prim
, char *msg
);
144 void type_error (char *prim
, char *type
);
146 #define ERROR(prim, msg) halt_with_error()
147 #define TYPE_ERROR(prim, type) halt_with_error()
150 /*---------------------------------------------------------------------------*/
152 // address space layout
153 // TODO document each zone, also explain that since vector space is in ram, it uses the ram primitives
155 #define MAX_VEC_ENCODING 2047
156 #define MIN_VEC_ENCODING 1280
157 #define VEC_BYTES ((MAX_VEC_ENCODING - MIN_VEC_ENCODING + 1)*4)
158 // if the pic has less than 8k of memory, start vector space lower
160 #define MAX_RAM_ENCODING 1279
161 #define MIN_RAM_ENCODING 512
162 #define RAM_BYTES ((MAX_RAM_ENCODING - MIN_RAM_ENCODING + 1)*4)
164 #define MIN_FIXNUM_ENCODING 3
165 #define MIN_FIXNUM -1
166 #define MAX_FIXNUM 255
167 #define MIN_ROM_ENCODING (MIN_FIXNUM_ENCODING + MAX_FIXNUM - MIN_FIXNUM + 1)
170 uint16
OBJ_TO_RAM_ADDR(uint16 o
, uint8 f
) {return ((((o
) - MIN_RAM_ENCODING
) << 2) + (f
));}
171 uint16
OBJ_TO_ROM_ADDR(uint16 o
, uint8 f
) {return ((((o
) - MIN_ROM_ENCODING
) << 2) + (CODE_START
+ 4 + (f
)));}
173 #define OBJ_TO_RAM_ADDR(o,f) ((((o) - MIN_RAM_ENCODING) << 2) + (f))
174 #define OBJ_TO_ROM_ADDR(o,f) ((((o) - MIN_ROM_ENCODING) << 2) + (CODE_START + 4 + (f)))
179 uint8
ram_get(uint16 a
) { return *(a
+0x200); }
180 void ram_set(uint16 a
, uint8 x
) { *(a
+0x200) = (x
); }
182 #define ram_get(a) *(a+0x200)
183 #define ram_set(a,x) *(a+0x200) = (x)
189 uint8
ram_get(uint16 a
) {return *(uint8
*)(a
+0x200);}
190 void ram_set(uint16 a
, uint8 x
) {*(uint8
*)(a
+0x200) = (x
);}
192 #define ram_get(a) *(uint8*)(a+0x200)
193 #define ram_set(a,x) *(uint8*)(a+0x200) = (x)
199 uint8
ram_get(uint16 a
) {
203 void ram_set(uint16 a
, uint8 x
) {
210 uint8 ram_mem
[RAM_BYTES
+ VEC_BYTES
];
211 #define ram_get(a) ram_mem[a]
212 #define ram_set(a,x) ram_mem[a] = (x)
216 uint8
rom_get (rom_addr a
){
217 return *(rom uint8
*)a
;
221 uint8
rom_get (rom_addr a
){
222 return flash_read(a
);
227 #define ROM_BYTES 8192
228 uint8 rom_mem
[ROM_BYTES
] =
231 #define PUTCHAR_LIGHT_not
233 0xFB, 0xD7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x32
234 , 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
235 , 0x08, 0x50, 0x80, 0x16, 0xFE, 0xE8, 0x00, 0xFC
236 , 0x32, 0x80, 0x2D, 0xFE, 0xFC, 0x31, 0x80, 0x43
237 , 0xFE, 0xFC, 0x33, 0x80, 0x2D, 0xFE, 0xFC, 0x31
238 , 0x80, 0x43, 0xFE, 0x90, 0x16, 0x01, 0x20, 0xFC
239 , 0x32, 0xE3, 0xB0, 0x37, 0x09, 0xF3, 0xFF, 0x20
240 , 0xFC, 0x33, 0xE3, 0xB0, 0x40, 0x0A, 0xF3, 0xFF
241 , 0x08, 0xF3, 0xFF, 0x01, 0x40, 0x21, 0xD1, 0x00
242 , 0x02, 0xC0, 0x4C, 0x71, 0x01, 0x20, 0x50, 0x90
243 , 0x51, 0x00, 0xF1, 0x40, 0xD8, 0xB0, 0x59, 0x90
247 0xFB, 0xD7, 0x00, 0x00, 0x80, 0x08, 0xFE, 0xE8
248 , 0x00, 0xF6, 0xF5, 0x90, 0x08
251 uint8
rom_get (rom_addr a
) {
252 return rom_mem
[a
-CODE_START
];
257 uint8
RAM_GET_FIELD0_MACRO(uint16 o
) {return ram_get (OBJ_TO_RAM_ADDR(o
,0));}
258 void RAM_SET_FIELD0_MACRO(uint16 o
, uint8 val
) {ram_set (OBJ_TO_RAM_ADDR(o
,0), val
);}
259 uint8
ROM_GET_FIELD0_MACRO(uint16 o
) {return rom_get (OBJ_TO_ROM_ADDR(o
,0));}
261 #define RAM_GET_FIELD0_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,0))
262 #define RAM_SET_FIELD0_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,0), val)
263 #define ROM_GET_FIELD0_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,0))
267 uint8
RAM_GET_GC_TAGS_MACRO(uint16 o
) {return (RAM_GET_FIELD0_MACRO(o
) & 0x60);}
268 uint8
RAM_GET_GC_TAG0_MACRO(uint16 o
) {return (RAM_GET_FIELD0_MACRO(o
) & 0x20);}
269 uint8
RAM_GET_GC_TAG1_MACRO(uint16 o
) {return (RAM_GET_FIELD0_MACRO(o
) & 0x40);}
270 void RAM_SET_GC_TAGS_MACRO(uint16 o
, uint8 tags
) {(RAM_SET_FIELD0_MACRO(o
,(RAM_GET_FIELD0_MACRO(o
) & 0x9f) | (tags
)));}
271 void RAM_SET_GC_TAG0_MACRO(uint16 o
, uint8 tag
) {RAM_SET_FIELD0_MACRO(o
,(RAM_GET_FIELD0_MACRO(o
) & 0xdf) | (tag
));}
272 void RAM_SET_GC_TAG1_MACRO(uint16 o
, uint8 tag
) {RAM_SET_FIELD0_MACRO(o
,(RAM_GET_FIELD0_MACRO(o
) & 0xbf) | (tag
));}
274 #define RAM_GET_GC_TAGS_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x60)
275 #define RAM_GET_GC_TAG0_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x20)
276 #define RAM_GET_GC_TAG1_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x40)
277 #define RAM_SET_GC_TAGS_MACRO(o,tags) \
278 (RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0x9f) | (tags)))
279 #define RAM_SET_GC_TAG0_MACRO(o,tag) \
280 RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0xdf) | (tag))
281 #define RAM_SET_GC_TAG1_MACRO(o,tag) \
282 RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0xbf) | (tag))
286 uint8
RAM_GET_FIELD1_MACRO(uint16 o
) {return ram_get (OBJ_TO_RAM_ADDR(o
,1));}
287 uint8
RAM_GET_FIELD2_MACRO(uint16 o
) {return ram_get (OBJ_TO_RAM_ADDR(o
,2));}
288 uint8
RAM_GET_FIELD3_MACRO(uint16 o
) {return ram_get (OBJ_TO_RAM_ADDR(o
,3));}
289 void RAM_SET_FIELD1_MACRO(uint16 o
, uint8 val
) {ram_set (OBJ_TO_RAM_ADDR(o
,1), val
);}
290 void RAM_SET_FIELD2_MACRO(uint16 o
, uint8 val
) {ram_set (OBJ_TO_RAM_ADDR(o
,2), val
);}
291 void RAM_SET_FIELD3_MACRO(uint16 o
, uint8 val
) {ram_set (OBJ_TO_RAM_ADDR(o
,3), val
);}
292 uint8
ROM_GET_FIELD1_MACRO(uint16 o
) {return rom_get (OBJ_TO_ROM_ADDR(o
,1));}
293 uint8
ROM_GET_FIELD2_MACRO(uint16 o
) {return rom_get (OBJ_TO_ROM_ADDR(o
,2));}
294 uint8
ROM_GET_FIELD3_MACRO(uint16 o
) {return rom_get (OBJ_TO_ROM_ADDR(o
,3));}
296 #define RAM_GET_FIELD1_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,1))
297 #define RAM_GET_FIELD2_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,2))
298 #define RAM_GET_FIELD3_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,3))
299 #define RAM_SET_FIELD1_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,1), val)
300 #define RAM_SET_FIELD2_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,2), val)
301 #define RAM_SET_FIELD3_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,3), val)
302 #define ROM_GET_FIELD1_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,1))
303 #define ROM_GET_FIELD2_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,2))
304 #define ROM_GET_FIELD3_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,3))
307 word
ram_get_gc_tags (obj o
) { return RAM_GET_GC_TAGS_MACRO(o
); }
308 word
ram_get_gc_tag0 (obj o
) { return RAM_GET_GC_TAG0_MACRO(o
); }
309 word
ram_get_gc_tag1 (obj o
) { return RAM_GET_GC_TAG1_MACRO(o
); }
310 void ram_set_gc_tags (obj o
, word tags
) { RAM_SET_GC_TAGS_MACRO(o
, tags
); }
311 void ram_set_gc_tag0 (obj o
, word tag
) { RAM_SET_GC_TAG0_MACRO(o
,tag
); }
312 void ram_set_gc_tag1 (obj o
, word tag
) { RAM_SET_GC_TAG1_MACRO(o
,tag
); }
313 word
ram_get_field0 (obj o
) { return RAM_GET_FIELD0_MACRO(o
); }
314 word
ram_get_field1 (obj o
) { return RAM_GET_FIELD1_MACRO(o
); }
315 word
ram_get_field2 (obj o
) { return RAM_GET_FIELD2_MACRO(o
); }
316 word
ram_get_field3 (obj o
) { return RAM_GET_FIELD3_MACRO(o
); }
317 void ram_set_field0 (obj o
, word val
) { RAM_SET_FIELD0_MACRO(o
,val
); }
318 void ram_set_field1 (obj o
, word val
) { RAM_SET_FIELD1_MACRO(o
,val
); }
319 void ram_set_field2 (obj o
, word val
) { RAM_SET_FIELD2_MACRO(o
,val
); }
320 void ram_set_field3 (obj o
, word val
) { RAM_SET_FIELD3_MACRO(o
,val
); }
321 word
rom_get_field0 (obj o
) { return ROM_GET_FIELD0_MACRO(o
); }
322 word
rom_get_field1 (obj o
) { return ROM_GET_FIELD1_MACRO(o
); }
323 word
rom_get_field2 (obj o
) { return ROM_GET_FIELD2_MACRO(o
); }
324 word
rom_get_field3 (obj o
) { return ROM_GET_FIELD3_MACRO(o
); }
326 obj
ram_get_car (obj o
);
327 obj
rom_get_car (obj o
);
328 obj
ram_get_cdr (obj o
);
329 obj
rom_get_cdr (obj o
);
330 void ram_set_car (obj o
, obj val
);
331 void ram_set_cdr (obj o
, obj val
);
333 obj
ram_get_entry (obj o
);
334 obj
rom_get_entry (obj o
);
336 obj
get_global (uint8 i
);
337 void set_global (uint8 i
, obj o
);
340 /*---------------------------------------------------------------------------*/
348 fixnum n MIN_FIXNUM -> 3 ... MAX_FIXNUM -> 3 + (MAX_FIXNUM-MIN_FIXNUM)
349 rom object 4 + (MAX_FIXNUM-MIN_FIXNUM) ... MIN_RAM_ENCODING-1
350 ram object MIN_RAM_ENCODING ... MAX_RAM_ENCODING
351 u8vector MIN_VEC_ENCODING ... 8191
353 layout of memory allocated objects:
355 Gs represent mark bits used by the gc
357 ifdef INFINITE_PRECISION_BIGNUMS
358 bignum n 0GG***** **next** hhhhhhhh llllllll (16 bit digit)
359 TODO what to do with the gc tags for the bignums ? will this work ?
361 ifndef INFINITE_PRECISION_BIGNUMS
362 bignum n 00000000 uuuuuuuu hhhhhhhh llllllll (24 bit signed integer)
363 TODO doesn't work properly for the moment. only 16 bits are usable now
365 pair 1GGaaaaa aaaaaaaa 000ddddd dddddddd
368 gives an address space of 2^13 * 4 = 32k divided between simple objects,
371 symbol 1GG00000 00000000 00100000 00000000
373 string 1GG***** *chars** 01000000 00000000
375 u8vector 1GGxxxxx xxxxxxxx 011yyyyy yyyyyyyy
376 x is length of the vector, in bytes (stored raw, not encoded as an object)
377 y is pointer to the elements themselves (stored in vector space)
379 closure 01Gaaaaa aaaaaaaa aaaxxxxx xxxxxxxx
380 0x5ff<a<0x4000 is entry
381 x is pointer to environment
382 the reason why the environment is on the cdr (and the entry is split on 3
383 bytes) is that, when looking for a variable, a closure is considered to be a
384 pair. The compiler adds an extra offset to any variable in the closure's
385 environment, so the car of the closure (which doesn't really exist) is never
386 checked, but the cdr is followed to find the other bindings
388 continuation 1GGxxxxx xxxxxxxx 100yyyyy yyyyyyyy
389 x is parent continuation
390 y is pointer to the second half, which is a closure (contains env and entry)
392 An environment is a list of objects built out of pairs. On entry to
393 a procedure the environment is the list of parameters to which is
394 added the environment of the closure being called.
396 The first byte at the entry point of a procedure gives the arity of
399 n = 0 to 127 -> procedure has n parameters (no rest parameter)
400 n = -128 to -1 -> procedure has -n parameters, the last is
406 #define encode_bool(x) (x)
410 // fixnum definitions in picobit-vm.h , address space layout section
413 uint16
ENCODE_FIXNUM(uint8 n
) {return ((n
) + (MIN_FIXNUM_ENCODING
- MIN_FIXNUM
));}
414 uint8
DECODE_FIXNUM(uint16 o
) {return ((o
) - (MIN_FIXNUM_ENCODING
- MIN_FIXNUM
));}
416 #define ENCODE_FIXNUM(n) ((n) + (MIN_FIXNUM_ENCODING - MIN_FIXNUM))
417 #define DECODE_FIXNUM(o) ((o) - (MIN_FIXNUM_ENCODING - MIN_FIXNUM))
421 uint8
IN_VEC(uint16 o
) {return ((o
) >= MIN_VEC_ENCODING
);}
422 uint8
IN_RAM(uint16 o
) {return (!IN_VEC(o
) && ((o
) >= MIN_RAM_ENCODING
));}
423 uint8
IN_ROM(uint16 o
) {return (!IN_VEC(o
) && !IN_RAM(o
) && ((o
) >= MIN_ROM_ENCODING
));}
425 #define IN_VEC(o) ((o) >= MIN_VEC_ENCODING)
426 #define IN_RAM(o) (!IN_VEC(o) && ((o) >= MIN_RAM_ENCODING))
427 #define IN_ROM(o) (!IN_VEC(o) && !IN_RAM(o) && ((o) >= MIN_ROM_ENCODING))
430 // bignum first byte : 00Gxxxxx
431 #define BIGNUM_FIELD0 0
433 uint8
RAM_BIGNUM(uint16 o
) {return ((ram_get_field0 (o
) & 0xc0) == BIGNUM_FIELD0
);}
434 uint8
ROM_BIGNUM(uint16 o
) {return ((rom_get_field0 (o
) & 0xc0) == BIGNUM_FIELD0
);}
436 #define RAM_BIGNUM(o) ((ram_get_field0 (o) & 0xc0) == BIGNUM_FIELD0)
437 #define ROM_BIGNUM(o) ((rom_get_field0 (o) & 0xc0) == BIGNUM_FIELD0)
440 // composite first byte : 1GGxxxxx
441 #define COMPOSITE_FIELD0 0x80
443 uint8
RAM_COMPOSITE(uint16 o
) {return ((ram_get_field0 (o
) & 0x80) == COMPOSITE_FIELD0
);}
444 uint8
ROM_COMPOSITE(uint16 o
) {return ((rom_get_field0 (o
) & 0x80) == COMPOSITE_FIELD0
);}
446 #define RAM_COMPOSITE(o) ((ram_get_field0 (o) & 0x80) == COMPOSITE_FIELD0)
447 #define ROM_COMPOSITE(o) ((rom_get_field0 (o) & 0x80) == COMPOSITE_FIELD0)
450 // pair third byte : 000xxxxx
451 #define PAIR_FIELD2 0
453 uint8
RAM_PAIR(uint16 o
) {return (RAM_COMPOSITE (o
) && ((ram_get_field2 (o
) & 0xe0) == PAIR_FIELD2
));}
454 uint8
ROM_PAIR(uint16 o
) {return (ROM_COMPOSITE (o
) && ((rom_get_field2 (o
) & 0xe0) == PAIR_FIELD2
));}
456 #define RAM_PAIR(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == PAIR_FIELD2))
457 #define ROM_PAIR(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == PAIR_FIELD2))
460 // symbol third byte : 001xxxxx
461 #define SYMBOL_FIELD2 0x20
463 uint8
RAM_SYMBOL(uint16 o
) {return (RAM_COMPOSITE (o
) && ((ram_get_field2 (o
) & 0xe0) == SYMBOL_FIELD2
));}
464 uint8
ROM_SYMBOL(uint16 o
) {return (ROM_COMPOSITE (o
) && ((rom_get_field2 (o
) & 0xe0) == SYMBOL_FIELD2
));}
466 #define RAM_SYMBOL(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == SYMBOL_FIELD2))
467 #define ROM_SYMBOL(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == SYMBOL_FIELD2))
470 // string third byte : 010xxxxx
471 #define STRING_FIELD2 0x40
473 uint8
RAM_STRING(uint16 o
) {return (RAM_COMPOSITE (o
) && ((ram_get_field2 (o
) & 0xe0) == STRING_FIELD2
));}
474 uint8
ROM_STRING(uint16 o
) {return (ROM_COMPOSITE (o
) && ((rom_get_field2 (o
) & 0xe0) == STRING_FIELD2
));}
476 #define RAM_STRING(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == STRING_FIELD2))
477 #define ROM_STRING(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == STRING_FIELD2))
480 // vector third byte : 011xxxxx
481 #define VECTOR_FIELD2 0x60
483 uint8
RAM_VECTOR(uint16 o
) {return (RAM_COMPOSITE (o
) && ((ram_get_field2 (o
) & 0xe0) == VECTOR_FIELD2
));}
484 uint8
ROM_VECTOR(uint16 o
) {return (ROM_COMPOSITE (o
) && ((rom_get_field2 (o
) & 0xe0) == VECTOR_FIELD2
));}
486 #define RAM_VECTOR(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == VECTOR_FIELD2))
487 #define ROM_VECTOR(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == VECTOR_FIELD2))
490 // continuation third byte : 100xxxxx
491 #define CONTINUATION_FIELD2 0x80
493 uint8
RAM_CONTINUATION(uint16 o
) {return (RAM_COMPOSITE (o
) && ((ram_get_field2 (o
) & 0xe0) == CONTINUATION_FIELD2
));}
494 uint8
ROM_CONTINUATION(uint16 o
) {return (ROM_COMPOSITE (o
) && ((rom_get_field2 (o
) & 0xe0) == CONTINUATION_FIELD2
));}
496 #define RAM_CONTINUATION(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == CONTINUATION_FIELD2))
497 #define ROM_CONTINUATION(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == CONTINUATION_FIELD2))
500 // closure first byte : 01Gxxxxx
501 #define CLOSURE_FIELD0 0x40
503 uint8
RAM_CLOSURE(uint16 o
) {return ((ram_get_field0 (o
) & 0xc0) == CLOSURE_FIELD0
);}
504 uint8
ROM_CLOSURE(uint16 o
) {return ((rom_get_field0 (o
) & 0xc0) == CLOSURE_FIELD0
);}
506 #define RAM_CLOSURE(o) ((ram_get_field0 (o) & 0xc0) == CLOSURE_FIELD0)
507 #define ROM_CLOSURE(o) ((rom_get_field0 (o) & 0xc0) == CLOSURE_FIELD0)
510 /*---------------------------------------------------------------------------*/
512 // bignum definitions
514 #ifdef INFINITE_PRECISION_BIGNUMS
516 #define digit_width 16
519 typedef uint16 digit
; // TODO why these ? adds to the confusion
520 typedef uint32 two_digit
;
522 #define obj_eq(x,y) ((x) == (y))
523 #define integer_hi_set(x,y) ram_set_car (x, y)
525 #define ZERO ENCODE_FIXNUM(0)
526 #define NEG1 (ZERO-1)
527 #define POS1 (ZERO+1)
529 integer
make_integer (digit lo
, integer hi
);
530 integer
integer_hi (integer x
);
531 digit
integer_lo (integer x
);
533 integer
norm (obj prefix
, integer n
);
534 uint8
negp (integer x
);
535 uint8
cmp (integer x
, integer y
);
536 uint16
integer_length (integer x
);
537 integer
shr (integer x
);
538 integer
negative_carry (integer carry
);
539 integer
shl (integer x
);
540 integer
shift_left (integer x
, uint16 n
);
541 integer
add (integer x
, integer y
);
542 integer
invert (integer x
);
543 integer
sub (integer x
, integer y
);
544 integer
neg (integer x
);
545 integer
scale (digit n
, integer x
);
546 integer
mulnonneg (integer x
, integer y
);
547 integer
divnonneg (integer x
, integer y
);
549 uint16
decode_int (obj o
);
550 obj
encode_int (uint16 n
);
554 /*---------------------------------------------------------------------------*/
558 // TODO explain what each tag means, with 1-2 mark bits
559 #define GC_TAG_0_LEFT (1<<5)
560 #define GC_TAG_1_LEFT (2<<5)
561 #define GC_TAG_UNMARKED (0<<5)
563 /* Number of object fields of objects in ram */
565 uint8
HAS_2_OBJECT_FIELDS(uint16 visit
) {return (RAM_PAIR(visit
) || RAM_CONTINUATION(visit
));}
566 #ifdef INFINITE_PRECISION_BIGNUMS
567 uint8
HAS_1_OBJECT_FIELD(uint16 visit
) {return (RAM_COMPOSITE(visit
) || RAM_CLOSURE(visit
) || RAM_BIGNUM(visit
));}
569 uint8
HAS_1_OBJECT_FIELD(uint16 visit
) {return (RAM_COMPOSITE(visit
) || RAM_CLOSURE(visit
));}
573 #define HAS_2_OBJECT_FIELDS(visit) (RAM_PAIR(visit) || RAM_CONTINUATION(visit))
574 #ifdef INFINITE_PRECISION_BIGNUMS
575 #define HAS_1_OBJECT_FIELD(visit) (RAM_COMPOSITE(visit) \
576 || RAM_CLOSURE(visit) || RAM_BIGNUM(visit))
578 #define HAS_1_OBJECT_FIELD(visit) (RAM_COMPOSITE(visit) || RAM_CLOSURE(visit))
581 // all composites except pairs and continuations have 1 object field
583 #define NIL OBJ_FALSE
585 obj free_list
; /* list of unused cells */
586 obj free_list_vec
; /* list of unused cells in vector space */
588 obj arg1
; /* root set */
596 uint8 na
; /* interpreter variables */
607 /*---------------------------------------------------------------------------*/
615 void prim_numberp ();
624 // TODO we have extra primitives, pring back geq, leq, and put them in a sensible place in the primitives
629 obj
cons (obj car
, obj cdr
);
633 void prim_set_car ();
634 void prim_set_cdr ();
637 void prim_u8vectorp ();
638 void prim_make_u8vector ();
639 void prim_u8vector_ref ();
640 void prim_u8vector_set ();
641 void prim_u8vector_length ();
642 void prim_u8vector_copy ();
646 void prim_symbolp ();
647 void prim_stringp ();
648 void prim_string2list ();
649 void prim_list2string ();
650 void prim_booleanp ();
657 uint32
read_clock ();
661 void prim_led2_color ();
662 void prim_getchar_wait ();
663 void prim_putchar ();
668 void prim_network_init ();
669 void prim_network_cleanup ();
670 void prim_receive_packet_to_u8vector ();
671 void prim_send_packet_from_u8vector ();
673 /*---------------------------------------------------------------------------*/
677 #define FETCH_NEXT_BYTECODE() bytecode = rom_get (pc++)
679 #define PUSH_CONSTANT1 0x0
680 #define PUSH_CONSTANT2 0x1
681 #define PUSH_STACK1 0x2
682 #define PUSH_STACK2 0x3
683 #define PUSH_GLOBAL 0x4
684 #define SET_GLOBAL 0x5
687 #define LABEL_INSTR 0x8
688 #define PUSH_CONSTANT_LONG 0x9
700 void pop_procedure ();
701 void handle_arity_and_rest_param ();
706 /*---------------------------------------------------------------------------*/
708 // debugging functions
711 void show_type (obj o
);
712 void show_state (rom_addr pc
);
715 /*---------------------------------------------------------------------------*/