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 /*---------------------------------------------------------------------------*/
21 typedef unsigned char uint8
;
22 typedef unsigned short uint16
;
23 typedef unsigned long uint32
;
27 typedef uint16 ram_addr
;
28 typedef uint16 rom_addr
;
30 // pointers are 13 bits
33 /*---------------------------------------------------------------------------*/
54 static volatile near uint8 FW_VALUE_UP @
0x33;
55 static volatile near uint8 FW_VALUE_HI @
0x33;
56 static volatile near uint8 FW_VALUE_LO @
0x33;
58 #define ACTIVITY_LED1_LAT LATB
59 #define ACTIVITY_LED1_BIT 5
60 #define ACTIVITY_LED2_LAT LATB
61 #define ACTIVITY_LED2_BIT 4
62 static volatile near bit ACTIVITY_LED1 @
((unsigned)&ACTIVITY_LED1_LAT
*8)+ACTIVITY_LED1_BIT
;
63 static volatile near bit ACTIVITY_LED2 @
((unsigned)&ACTIVITY_LED2_LAT
*8)+ACTIVITY_LED2_BIT
;
75 #define MAX_PACKET_SIZE BUFSIZ
78 char errbuf
[PCAP_ERRBUF_SIZE
];
80 #define INTERFACE "eth0"
81 char buf
[MAX_PACKET_SIZE
]; // buffer for writing
86 #include <sys/types.h>
87 #include <sys/timeb.h>
98 /*---------------------------------------------------------------------------*/
100 // miscellaneous definitions
101 // TODO put at the end ?
103 // TODO these 2 are only used in negp, use them elsewhere ?
107 #define CODE_START 0x5000
109 /*---------------------------------------------------------------------------*/
114 #define IF_TRACE(x) x
115 #define IF_GC_TRACE(x) x
118 #define IF_GC_TRACE(x)
121 /*---------------------------------------------------------------------------*/
126 #define ERROR(prim, msg) halt_with_error()
127 #define TYPE_ERROR(prim, type) halt_with_error()
131 #define ERROR(prim, msg) error (prim, msg)
132 #define TYPE_ERROR(prim, type) type_error (prim, type)
133 void error (char *prim
, char *msg
);
134 void type_error (char *prim
, char *type
);
137 /*---------------------------------------------------------------------------*/
139 // address space layout
140 // TODO document each zone, also explain that since vector space is in ram, it uses the ram primitives
142 #define MAX_VEC_ENCODING 8191
143 #define MIN_VEC_ENCODING 4096
144 #define VEC_BYTES ((MAX_VEC_ENCODING - MIN_VEC_ENCODING + 1)*4)
145 // if the pic has less than 8k of memory, start vector space lower
146 // TODO the pic actually has 2k, so change these
147 // TODO we'd only actually need 1024 or so for ram and vectors, since we can't address more. this gives us a lot of rom space
149 #define MAX_RAM_ENCODING 4095
150 #define MIN_RAM_ENCODING 512
151 #define RAM_BYTES ((MAX_RAM_ENCODING - MIN_RAM_ENCODING + 1)*4)
152 // TODO watch out if we address more than what the PIC actually has
154 #define MIN_FIXNUM_ENCODING 3
155 #define MIN_FIXNUM -1
156 #define MAX_FIXNUM 255
157 #define MIN_ROM_ENCODING (MIN_FIXNUM_ENCODING+MAX_FIXNUM-MIN_FIXNUM+1)
159 #define OBJ_TO_RAM_ADDR(o,f) (((ram_addr)((uint16)(o) - MIN_RAM_ENCODING) << 2) + (f))
160 #define OBJ_TO_ROM_ADDR(o,f) (((rom_addr)((uint16)(o) - MIN_ROM_ENCODING) << 2) + (CODE_START + 4 + (f)))
164 #define ram_get(a) *(uint8*)(a+0x200)
165 #define ram_set(a,x) *(uint8*)(a+0x200) = (x)
170 uint8 ram_mem
[RAM_BYTES
+ VEC_BYTES
];
171 #define ram_get(a) ram_mem[a]
172 #define ram_set(a,x) ram_mem[a] = (x)
176 uint8
rom_get (rom_addr a
){
177 return *(rom uint8
*)a
;
183 #define ROM_BYTES 8192
184 // TODO the new pics have 32k, change this ? minus the vm size, firmware ?
185 uint8 rom_mem
[ROM_BYTES
];
186 uint8
rom_get (rom_addr a
);
189 /*---------------------------------------------------------------------------*/
193 #define RAM_GET_FIELD0_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,0))
194 #define RAM_SET_FIELD0_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,0), val)
195 #define ROM_GET_FIELD0_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,0))
197 #define RAM_GET_GC_TAGS_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x60)
198 #define RAM_GET_GC_TAG0_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x20)
199 #define RAM_GET_GC_TAG1_MACRO(o) (RAM_GET_FIELD0_MACRO(o) & 0x40)
200 #define RAM_SET_GC_TAGS_MACRO(o,tags) \
201 (RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0x9f) | (tags)))
202 #define RAM_SET_GC_TAG0_MACRO(o,tag) \
203 RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0xdf) | (tag))
204 #define RAM_SET_GC_TAG1_MACRO(o,tag) \
205 RAM_SET_FIELD0_MACRO(o,(RAM_GET_FIELD0_MACRO(o) & 0xbf) | (tag))
207 #define RAM_GET_FIELD1_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,1))
208 #define RAM_GET_FIELD2_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,2))
209 #define RAM_GET_FIELD3_MACRO(o) ram_get (OBJ_TO_RAM_ADDR(o,3))
210 #define RAM_SET_FIELD1_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,1), val)
211 #define RAM_SET_FIELD2_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,2), val)
212 #define RAM_SET_FIELD3_MACRO(o,val) ram_set (OBJ_TO_RAM_ADDR(o,3), val)
213 #define ROM_GET_FIELD1_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,1))
214 #define ROM_GET_FIELD2_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,2))
215 #define ROM_GET_FIELD3_MACRO(o) rom_get (OBJ_TO_ROM_ADDR(o,3))
217 word
ram_get_gc_tags (obj o
);
218 word
ram_get_gc_tag0 (obj o
);
219 word
ram_get_gc_tag1 (obj o
);
220 void ram_set_gc_tags (obj o
, word tags
);
221 void ram_set_gc_tag0 (obj o
, word tag
);
222 void ram_set_gc_tag1 (obj o
, word tag
);
223 word
ram_get_field0 (obj o
);
224 word
ram_get_field1 (obj o
);
225 word
ram_get_field2 (obj o
);
226 word
ram_get_field3 (obj o
);
227 word
ram_get_fieldn (obj o
, word n
);
228 void ram_set_field0 (obj o
, word val
);
229 void ram_set_field1 (obj o
, word val
);
230 void ram_set_field2 (obj o
, word val
);
231 void ram_set_field3 (obj o
, word val
);
232 void ram_set_fieldn (obj o
, uint8 n
, word val
);
233 word
rom_get_field0 (obj o
);
234 word
rom_get_field1 (obj o
);
235 word
rom_get_field2 (obj o
);
236 word
rom_get_field3 (obj o
);
238 obj
ram_get_car (obj o
);
239 obj
rom_get_car (obj o
);
240 obj
ram_get_cdr (obj o
);
241 obj
rom_get_cdr (obj o
);
242 void ram_set_car (obj o
, obj val
);
243 void ram_set_cdr (obj o
, obj val
);
245 obj
ram_get_entry (obj o
);
246 obj
rom_get_entry (obj o
);
248 obj
get_global (uint8 i
);
249 void set_global (uint8 i
, obj o
);
251 /*---------------------------------------------------------------------------*/
259 fixnum n MIN_FIXNUM -> 3 ... MAX_FIXNUM -> 3 + (MAX_FIXNUM-MIN_FIXNUM)
260 rom object 4 + (MAX_FIXNUM-MIN_FIXNUM) ... MIN_RAM_ENCODING-1
261 ram object MIN_RAM_ENCODING ... MAX_RAM_ENCODING
262 u8vector MIN_VEC_ENCODING ... 8191
264 layout of memory allocated objects:
266 Gs represent mark bits used by the gc
268 ifdef INFINITE_PRECISION_BIGNUMS
269 bignum n 0GG***** **next** hhhhhhhh llllllll (16 bit digit)
270 TODO what to do with the gc tags for the bignums ? will this work ?
272 ifndef INFINITE_PRECISION_BIGNUMS
273 bignum n 00000000 uuuuuuuu hhhhhhhh llllllll (24 bit signed integer)
275 pair 1GGaaaaa aaaaaaaa 000ddddd dddddddd
278 gives an address space of 2^13 * 4 = 32k divided between simple objects,
281 symbol 1GG00000 00000000 00100000 00000000
283 string 1GG***** *chars** 01000000 00000000
285 u8vector 1GGxxxxx xxxxxxxx 011yyyyy yyyyyyyy
286 x is length of the vector, in bytes (stored raw, not encoded as an object)
287 y is pointer to the elements themselves (stored in vector space)
289 closure 01Gaaaaa aaaaaaaa aaaxxxxx xxxxxxxx
290 0x5ff<a<0x4000 is entry
291 x is pointer to environment
292 the reason why the environment is on the cdr (and the entry is split on 3
293 bytes) is that, when looking for a variable, a closure is considered to be a
294 pair. The compiler adds an extra offset to any variable in the closure's
295 environment, so the car of the closure (which doesn't really exist) is never
296 checked, but the cdr is followed to find the other bindings
298 continuation 1GGxxxxx xxxxxxxx 100yyyyy yyyyyyyy
299 x is parent continuation
300 y is pointer to the second half, which is a closure (contains env and entry)
302 An environment is a list of objects built out of pairs. On entry to
303 a procedure the environment is the list of parameters to which is
304 added the environment of the closure being called.
306 The first byte at the entry point of a procedure gives the arity of
309 n = 0 to 127 -> procedure has n parameters (no rest parameter)
310 n = -128 to -1 -> procedure has -n parameters, the last is
316 #define encode_bool(x) ((obj)(x))
320 // fixnum definitions in picobit-vm.h , address space layout section
322 #define ENCODE_FIXNUM(n) ((obj)(n) + (MIN_FIXNUM_ENCODING - MIN_FIXNUM))
323 #define DECODE_FIXNUM(o) ((int16)(o) - (MIN_FIXNUM_ENCODING - MIN_FIXNUM))
325 #define IN_VEC(o) ((o) >= MIN_VEC_ENCODING)
326 #define IN_RAM(o) (!IN_VEC(o) && ((o) >= MIN_RAM_ENCODING))
327 #define IN_ROM(o) (!IN_VEC(o) && !IN_RAM(o) && ((o) >= MIN_ROM_ENCODING))
329 // bignum first byte : 00Gxxxxx
330 #define BIGNUM_FIELD0 0
331 #define RAM_BIGNUM(o) ((ram_get_field0 (o) & 0xc0) == BIGNUM_FIELD0)
332 #define ROM_BIGNUM(o) ((rom_get_field0 (o) & 0xc0) == BIGNUM_FIELD0)
334 // composite first byte : 1GGxxxxx
335 #define COMPOSITE_FIELD0 0x80
336 #define RAM_COMPOSITE(o) ((ram_get_field0 (o) & 0x80) == COMPOSITE_FIELD0)
337 #define ROM_COMPOSITE(o) ((rom_get_field0 (o) & 0x80) == COMPOSITE_FIELD0)
339 // pair third byte : 000xxxxx
340 #define PAIR_FIELD2 0
341 #define RAM_PAIR(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == PAIR_FIELD2))
342 #define ROM_PAIR(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == PAIR_FIELD2))
344 // symbol third byte : 001xxxxx
345 #define SYMBOL_FIELD2 0x20
346 #define RAM_SYMBOL(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == SYMBOL_FIELD2))
347 #define ROM_SYMBOL(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == SYMBOL_FIELD2))
349 // string third byte : 010xxxxx
350 #define STRING_FIELD2 0x40
351 #define RAM_STRING(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == STRING_FIELD2))
352 #define ROM_STRING(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == STRING_FIELD2))
354 // vector third byte : 011xxxxx
355 #define VECTOR_FIELD2 0x60
356 #define RAM_VECTOR(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == VECTOR_FIELD2))
357 #define ROM_VECTOR(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == VECTOR_FIELD2))
359 // continuation third byte : 100xxxxx
360 #define CONTINUATION_FIELD2 0x80
361 #define RAM_CONTINUATION(o) (RAM_COMPOSITE (o) && ((ram_get_field2 (o) & 0xe0) == CONTINUATION_FIELD2))
362 #define ROM_CONTINUATION(o) (ROM_COMPOSITE (o) && ((rom_get_field2 (o) & 0xe0) == CONTINUATION_FIELD2))
364 // closure first byte : 01Gxxxxx
365 #define CLOSURE_FIELD0 0x40
366 #define RAM_CLOSURE(o) ((ram_get_field0 (o) & 0xc0) == CLOSURE_FIELD0)
367 #define ROM_CLOSURE(o) ((rom_get_field0 (o) & 0xc0) == CLOSURE_FIELD0)
369 /*---------------------------------------------------------------------------*/
371 // bignum definitions
373 #ifdef INFINITE_PRECISION_BIGNUMS
375 #define digit_width 16
378 typedef uint16 digit
; // TODO why these ? adds to the confusion
379 typedef uint32 two_digit
;
381 #define obj_eq(x,y) ((x) == (y))
382 #define integer_hi_set(x,y) ram_set_car (x, y)
384 #define ZERO ENCODE_FIXNUM(0)
385 #define NEG1 (ZERO-1)
386 #define POS1 (ZERO+1)
388 integer
make_integer (digit lo
, integer hi
);
389 integer
integer_hi (integer x
);
390 digit
integer_lo (integer x
);
392 integer
norm (obj prefix
, integer n
);
393 uint8
negp (integer x
);
394 int8
cmp (integer x
, integer y
);
395 uint16
integer_length (integer x
);
396 integer
shr (integer x
);
397 integer
negative_carry (integer carry
);
398 integer
shl (integer x
);
399 integer
shift_left (integer x
, uint16 n
);
400 integer
add (integer x
, integer y
);
401 integer
invert (integer x
);
402 integer
sub (integer x
, integer y
);
403 integer
neg (integer x
);
404 integer
scale (digit n
, integer x
);
405 integer
mulnonneg (integer x
, integer y
);
406 integer
divnonneg (integer x
, integer y
);
408 int16
decode_int (obj o
);
409 obj
encode_int (int16 n
);
413 /*---------------------------------------------------------------------------*/
417 // TODO explain what each tag means, with 1-2 mark bits
418 #define GC_TAG_0_LEFT (1<<5)
419 #define GC_TAG_1_LEFT (2<<5)
420 #define GC_TAG_UNMARKED (0<<5)
422 /* Number of object fields of objects in ram */
423 #define HAS_2_OBJECT_FIELDS(visit) (RAM_PAIR(visit) || RAM_CONTINUATION(visit))
424 #ifdef INFINITE_PRECISION_BIGNUMS
425 #define HAS_1_OBJECT_FIELD(visit) (RAM_COMPOSITE(visit) \
426 || RAM_CLOSURE(visit) || RAM_BIGNUM(visit))
428 #define HAS_1_OBJECT_FIELD(visit) (RAM_COMPOSITE(visit) || RAM_CLOSURE(visit))
430 // all composites except pairs and continuations have 1 object field
432 #define NIL OBJ_FALSE
434 obj free_list
; /* list of unused cells */
435 obj free_list_vec
; /* list of unused cells in vector space */
437 obj arg1
; /* root set */
445 uint8 na
; /* interpreter variables */
456 /*---------------------------------------------------------------------------*/
464 void prim_numberp ();
473 // TODO we have extra primitives, pring back geq, leq, and put them in a sensible place in the primitives
478 obj
cons (obj car
, obj cdr
);
482 void prim_set_car ();
483 void prim_set_cdr ();
486 void prim_u8vectorp ();
487 void prim_make_u8vector ();
488 void prim_u8vector_ref ();
489 void prim_u8vector_set ();
490 void prim_u8vector_length ();
491 void prim_u8vector_copy ();
495 void prim_symbolp ();
496 void prim_stringp ();
497 void prim_string2list ();
498 void prim_list2string ();
499 void prim_booleanp ();
510 void prim_led2_color ();
511 void prim_getchar_wait ();
512 void prim_putchar ();
517 void prim_network_init ();
518 void prim_network_cleanup ();
519 void prim_receive_packet_to_u8vector ();
520 void prim_send_packet_from_u8vector ();
522 /*---------------------------------------------------------------------------*/
526 #define FETCH_NEXT_BYTECODE() bytecode = rom_get (pc++)
528 #define BEGIN_DISPATCH() \
530 IF_TRACE(show_state (pc)); \
531 FETCH_NEXT_BYTECODE(); \
532 bytecode_hi4 = bytecode & 0xf0; \
533 bytecode_lo4 = bytecode & 0x0f; \
534 switch (bytecode_hi4 >> 4) {
536 #define END_DISPATCH() }
538 #define CASE(opcode) case (opcode>>4):;
540 #define DISPATCH(); goto dispatch;
542 #define PUSH_CONSTANT1 0x00
543 #define PUSH_CONSTANT2 0x10
544 #define PUSH_STACK1 0x20
545 #define PUSH_STACK2 0x30
546 #define PUSH_GLOBAL 0x40
547 #define SET_GLOBAL 0x50
550 #define LABEL_INSTR 0x80
551 #define PUSH_CONSTANT_LONG 0x90
561 #define PUSH_ARG1() push_arg1 ()
566 void pop_procedure ();
567 void handle_arity_and_rest_param ();
572 /*---------------------------------------------------------------------------*/
574 // debugging functions
577 void show_type (obj o
);
578 void show_state (rom_addr pc
);
581 /*---------------------------------------------------------------------------*/