1 // TODO at least, use #include and #define, but #define would require me to start all over...
3 // no forward declarations
4 // Scheme-style hex literals
5 // no explicit casts (intermediate variables instead)
6 // removed functions that interact with the firmware, and with rom
7 // had some case labels that were not literals (but could have been constant folded) in the dispatch
9 /* typedef char int8; */
10 /* typedef short int16; */
11 /* typedef long int32; */
12 /* typedef unsigned char uint8; */
13 /* typedef unsigned short uint16; */
14 /* typedef unsigned long uint32; */
16 /* typedef uint8 word; */
18 /* typedef uint16 ram_addr; */
19 /* typedef uint16 rom_addr; */
40 void halt_with_error (){
45 /* typedef int16 obj; */ // TODO actually has 21 bits
46 /* int8 rom_get (int16 a){ */
47 /* return /\* *(int8*) *\/a; // TODO had rom, but caused problems */
48 /* } */ // TODO now a predefined routine
49 /* int8 ram_get_gc_tags (int16 o); */
50 /* int8 ram_get_gc_tag0 (int16 o); */
51 /* int8 ram_get_gc_tag1 (int16 o); */
52 /* void ram_set_gc_tags (int16 o, int8 tags); */
53 /* void ram_set_gc_tag0 (int16 o, int8 tag); */
54 /* void ram_set_gc_tag1 (int16 o, int8 tag); */
55 /* int8 ram_get_field0 (int16 o); */
56 /* int8 ram_get_field1 (int16 o); */
57 /* int8 ram_get_field2 (int16 o); */
58 /* int8 ram_get_field3 (int16 o); */
59 /* int8 ram_get_fieldn (int16 o, int8 n); */
60 /* void ram_set_field0 (int16 o, int8 val); */
61 /* void ram_set_field1 (int16 o, int8 val); */
62 /* void ram_set_field2 (int16 o, int8 val); */
63 /* void ram_set_field3 (int16 o, int8 val); */
64 /* void ram_set_fieldn (int16 o, int8 n, int8 val); */
65 /* int8 rom_get_field0 (int16 o); */
66 /* int8 rom_get_field1 (int16 o); */
67 /* int8 rom_get_field2 (int16 o); */
68 /* int8 rom_get_field3 (int16 o); */
69 int8
ram_get_gc_tags (int16 o
) {
71 return (*(((t2
<< 2))+#x200) & #x60);
73 int8
ram_get_gc_tag0 (int16 o
) {
75 return (*(((t2
<< 2))+#x200) & #x20);
77 int8
ram_get_gc_tag1 (int16 o
) {
79 return (*(((t2
<< 2))+#x200) & #x40);
81 void ram_set_gc_tags (int16 o
, int8 tags
) {
83 (*(((t2
<< 2) + (0))+#x200) = ((*(((t2 << 2) + (0))+#x200) & #x9f) | (tags)));
85 void ram_set_gc_tag0 (int16 o
, int8 tag
) {
87 *(((t2
<< 2) + (0))+#x200) = ((*(((t2 << 2) + (0))+#x200) & #xdf) | (tag));
89 void ram_set_gc_tag1 (int16 o
, int8 tag
) {
91 *(((t2
<< 2) + (0))+#x200) = ((*(((t2 << 2) + (0))+#x200) & #xbf) | (tag));
93 int8
ram_get_field0 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (0))+#x200); }
94 int8
ram_get_field1 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (1))+#x200); }
95 int8
ram_get_field2 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (2))+#x200); }
96 int8
ram_get_field3 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (3))+#x200); }
97 int8
ram_get_fieldn (int16 o
, int8 n
) {
99 case 0: return ram_get_field0 (o
);
100 case 1: return ram_get_field1 (o
);
101 case 2: return ram_get_field2 (o
);
102 case 3: return ram_get_field3 (o
);
105 void ram_set_field0 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (0))+#x200) = (val); }
106 void ram_set_field1 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (1))+#x200) = (val); }
107 void ram_set_field2 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (2))+#x200) = (val); }
108 void ram_set_field3 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (3))+#x200) = (val); }
109 void ram_set_fieldn (int16 o
, int8 n
, int8 val
) {
111 case 0: ram_set_field0 (o
, val
); break;
112 case 1: ram_set_field1 (o
, val
); break;
113 case 2: ram_set_field2 (o
, val
); break;
114 case 3: ram_set_field3 (o
, val
); break;
117 int8
rom_get_field0 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x5000 + 4 + (0)))); }
118 int8
rom_get_field1 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x5000 + 4 + (1)))); }
119 int8
rom_get_field2 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x5000 + 4 + (2)))); }
120 int8
rom_get_field3 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x5000 + 4 + (3)))); }
123 /* int16 ram_get_car (int16 o); */
124 /* int16 rom_get_car (int16 o); */
125 /* int16 ram_get_cdr (int16 o); */
126 /* int16 rom_get_cdr (int16 o); */
127 /* void ram_set_car (int16 o, int16 val); */
128 /* void ram_set_cdr (int16 o, int16 val); */
130 /* int16 ram_get_entry (int16 o); */
131 /* int16 rom_get_entry (int16 o); */
133 int16
ram_get_car (int16 o
)
134 { int16 tmp
= (ram_get_field0 (o
) & #x1f); return (tmp << 8) | ram_get_field1 (o); }
135 int16
rom_get_car (int16 o
)
136 { int16 tmp
= (rom_get_field0 (o
) & #x1f); return (tmp << 8) | rom_get_field1 (o); }
137 int16
ram_get_cdr (int16 o
)
138 { int16 tmp
= (ram_get_field2 (o
) & #x1f); return (tmp << 8) | ram_get_field3 (o); }
139 int16
rom_get_cdr (int16 o
)
140 { int16 tmp
= (rom_get_field2 (o
) & #x1f); return (tmp << 8) | rom_get_field3 (o); }
142 void ram_set_car (int16 o
, int16 val
) {
143 ram_set_field0 (o
, (val
>> 8) | (ram_get_field0 (o
) & #xe0));
144 ram_set_field1 (o
, val
& #xff);
146 void ram_set_cdr (int16 o
, int16 val
) {
147 ram_set_field2 (o
, (val
>> 8) | (ram_get_field2 (o
) & #xe0));
148 ram_set_field3 (o
, val
& #xff);
152 int16
ram_get_entry (int16 o
) {
153 int16 tmp
= (ram_get_field0 (o
) & #x1f);
155 | (ram_get_field1 (o
) << 3)
156 | (ram_get_field2 (o
) >> 5));
158 int16
rom_get_entry (int16 o
){
159 int16 tmp
= (rom_get_field0 (o
) & #x1f);
161 | (rom_get_field1 (o
) << 3)
162 | (rom_get_field2 (o
) >> 5));
166 /* int16 get_global (int8 i); */
167 /* void set_global (int8 i, int16 o); */
169 int16
get_global (int8 i
) {
172 return ram_get_cdr (512 + (i
>> 1));
174 return ram_get_car (512 + (i
>> 1));
176 void set_global (int8 i
, int16 o
) {
178 ram_set_cdr (512 + (i
>> 1), o
);
180 ram_set_car (512 + (i
>> 1), o
);
186 void mark (int16 temp
) {
192 if ((!((temp
) >= 4096) && ((temp
) >= 512))) {
202 if (((((ram_get_field0 (visit
) & #x80) == #x80) || ((ram_get_field0 (visit) & #xc0) == #x40) || ((ram_get_field0 (visit) & #xc0) == 0)) && ram_get_gc_tag0 (visit))
203 || (((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == 0)) || (((ram_get_field0 (visit) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x80)))
204 && (ram_get_gc_tags (visit
) != (0<<5))))
207 if (((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == 0)) || (((ram_get_field0 (visit) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x80)))) {
212 temp
= ram_get_cdr (visit
);
214 if ((!((temp
) >= 4096) && ((temp
) >= 512))) {
216 int16 tmp
= 2; // TODO literals should be int, but that's wasteful
217 ram_set_gc_tags (visit
, (tmp
<<5));
218 ram_set_cdr (visit
, stack
);
227 if ((((ram_get_field0 (visit
) & #x80) == #x80) || ((ram_get_field0 (visit) & #xc0) == #x40) || ((ram_get_field0 (visit) & #xc0) == 0))) {
233 if (((ram_get_field0 (visit
) & #xc0) == #x40))
234 temp
= ram_get_cdr (visit
);
236 temp
= ram_get_car (visit
);
238 if ((!((temp
) >= 4096) && ((temp
) >= 512))) {
241 ram_set_gc_tag0 (visit
, (tmp
<<5));
242 if (((ram_get_field0 (visit
) & #xc0) == #x40))
243 ram_set_cdr (visit
, stack
);
245 ram_set_car (visit
, stack
);
255 ram_set_gc_tag0 (visit
, (tmp
<<5));
263 if (((((ram_get_field0 (stack
) & #x80) == #x80) && ((ram_get_field2 (stack) & #xe0) == 0)) || (((ram_get_field0 (stack) & #x80) == #x80) && ((ram_get_field2 (stack) & #xe0) == #x80))) && ram_get_gc_tag1 (stack)) {
266 temp
= ram_get_cdr (stack
);
267 ram_set_cdr (stack
, visit
);
271 ram_set_gc_tag1(visit
, (0<<5));
277 if (((ram_get_field0 (stack
) & #xc0) == #x40)) {
281 temp
= ram_get_cdr (stack
);
282 ram_set_cdr (stack
, visit
);
291 temp
= ram_get_car (stack
);
292 ram_set_car (stack
, visit
);
316 while (visit
>= (512 + ((glovars
+ 1) >> 1))) {
319 if ((((ram_get_field0 (visit
) & #x80) == #x80)
320 && (ram_get_gc_tags (visit
) == (0<<5)))
321 || !(ram_get_gc_tags (visit
) & (tmp
<<5))) {
323 if ((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x60))) {
325 int16 o
= ram_get_cdr (visit
);
326 int16 i
= ram_get_car (visit
);
327 ram_set_car (o
, free_list_vec
);
328 ram_set_cdr (o
, (i
+ 3) >> 2);
332 ram_set_car (visit
, free_list
);
336 if (((ram_get_field0 (visit
) & #x80) == #x80))
337 ram_set_gc_tags (visit
, (0<<5));
339 ram_set_gc_tag0 (visit
, (0<<5));
369 for (i
=0; i
<glovars
; i
++)
370 mark (get_global (i
));
376 int16
alloc_ram_cell () {
383 if (free_list
== 0) {
393 free_list
= ram_get_car (o
);
398 int16
alloc_ram_cell_init (int8 f0
, int8 f1
, int8 f2
, int8 f3
) {
399 int16 o
= alloc_ram_cell ();
401 ram_set_field0 (o
, f0
);
402 ram_set_field1 (o
, f1
);
403 ram_set_field2 (o
, f2
);
404 ram_set_field3 (o
, f3
);
409 int16
alloc_vec_cell (int16 n
) {
410 int16 o
= free_list_vec
;
419 while ((ram_get_cdr (o
) * 4) < n
) {
437 if (((ram_get_cdr(o
) * 4) - n
) < 4) {
439 ram_set_car (prec
, ram_get_car (o
));
441 free_list_vec
= ram_get_car (o
);
446 int16 new_free
= o
+ (n
+ 3) >> 2;
448 ram_set_car (prec
, new_free
);
450 free_list_vec
= new_free
;
451 ram_set_car (new_free
, ram_get_car (o
));
452 ram_set_cdr (new_free
, ram_get_cdr (o
) - (n
+ 3) >> 2);
460 /* typedef int16 integer; */
461 /* typedef int16 digit; */
462 /* typedef int32 two_digit; */
463 /* int16 make_integer (int16 lo, int16 hi); */
464 /* int16 integer_hi (int16 x); */
465 /* int16 integer_lo (int16 x); */
466 int16
make_integer (int16 lo
, int16 hi
) {
467 return alloc_ram_cell_init (0 | (hi
>> 8), hi
, lo
>> 8, lo
);
470 int16
integer_hi (int16 x
) {
471 if ((!((x
) >= 4096) && ((x
) >= 512)))
472 return ram_get_car (x
);
473 else if ((!((x
) >= 4096) && !(!((x
) >= 4096) && ((x
) >= 512)) && ((x
) >= (3 +255 - -1 +1))))
474 return rom_get_car (x
);
475 else if (x
< (3 - -1)){
476 return ((0 + (3 - -1))-1);
479 return (0 + (3 - -1));
483 int16
integer_lo (int16 x
) {
484 int16 t
= ram_get_field2 (x
);
485 if ((!((x
) >= 4096) && ((x
) >= 512)))
486 return (t
<< 8) + ram_get_field3 (x
);
487 else if ((!((x
) >= 4096) && !(!((x
) >= 4096) && ((x
) >= 512)) && ((x
) >= (3 +255 - -1 +1))))
488 return (t
<< 8) + rom_get_field3 (x
);
494 /* int16 norm (int16 prefix, int16 n); */
495 /* int8 negp (int16 x); */
496 /* int8 cmp (int16 x, int16 y); */
497 /* int16 integer_length (int16 x); */
498 /* int16 shr (int16 x); */
499 /* int16 negative_carry (int16 carry); */
500 /* int16 shl (int16 x); */
501 /* int16 shift_left (int16 x, int16 n); */
502 /* int16 add (int16 x, int16 y); */
503 /* int16 invert (int16 x); */
504 /* int16 sub (int16 x, int16 y); */
505 /* int16 neg (int16 x); */
506 /* int16 scale (int16 n, int16 x); */
507 /* int16 mulnonneg (int16 x, int16 y); */
508 /* int16 divnonneg (int16 x, int16 y); */
510 int32
decode_int (int16 o
) {
515 if (o
<= (3 + (255 - -1)))
516 return (o
- (3 - -1));
518 if ((!((o
) >= 4096) && ((o
) >= 512))) {
519 if (!((ram_get_field0 (o
) & #xc0) == 0))
521 return ram_get_field3 (o
);
523 else if ((!((o
) >= 4096) && !(!((o
) >= 4096) && ((o
) >= 512)) && ((o
) >= (3 +255 - -1 +1)))) {
524 if (!((rom_get_field0 (o
) & #xc0) == 0))
526 return rom_get_field3 (o
);
532 /* int32 decode_int (int16 o); */
533 /* int16 encode_int (int32 n); */
535 int16
norm (int16 prefix
, int16 n
) {
539 while (prefix
!= 0) {
540 int16 d
= integer_lo (prefix
);
543 prefix
= integer_hi (temp
);
545 if (((n
) == ((0 + (3 - -1))))) {
551 else if (((n
) == (((0 + (3 - -1))-1)))) {
553 if (d
>= (tmp
<<16) + -1) {
554 int16 t
= d
- (tmp
<< 16);
560 ram_set_car (temp
, n
);
567 int8
negp (int16 x
) {
572 if (((x
) == ((0 + (3 - -1))))) return 0;
573 } while (!((x
) == (((0 + (3 - -1))-1))));
578 int8
cmp (int16 x
, int16 y
) {
586 if (((x
) == ((0 + (3 - -1)))) || ((x
) == (((0 + (3 - -1))-1)))) {
588 { if (negp (y
)) result
= 1; else result
= -1; }
592 if (((y
) == ((0 + (3 - -1)))) || ((y
) == (((0 + (3 - -1))-1)))) {
593 if (negp (x
)) result
= -1; else result
= 1;
597 xlo
= integer_lo (x
);
598 ylo
= integer_lo (y
);
602 { if (xlo
< ylo
) result
= -1; else result
= 1; }
607 int16
integer_length (int16 x
) {
615 while (!(((next
= integer_hi (x
))) == ((0 + (3 - -1))))) {
630 int16
shr (int16 x
) {
637 if (((x
) == ((0 + (3 - -1)))) || ((x
) == (((0 + (3 - -1))-1)))) {
638 result
= norm (result
, x
);
645 result
= make_integer ((d
>> 1) |
646 ((integer_lo (x
) & 1) ? (tmp
<<15) : 0), // TODO only shifting by literals is permitted, so had to change the 16 -1 to 15
653 int16
negative_carry (int16 carry
) {
655 return ((0 + (3 - -1))-1);
657 return (0 + (3 - -1));
660 int16
shl (int16 x
) {
663 int16 negc
= (0 + (3 - -1));
669 if (((x
) == (negc
))) {
670 result
= norm (result
, x
);
678 negc
= negative_carry (d
& (tmp
<<15));
679 result
= make_integer ((d
<< 1) | ((temp
) == (3)), result
); // TODO was ((0 + (3 - -1))-1)
685 int16
shift_left (int16 x
, int16 n
) {
688 if (((x
) == ((0 + (3 - -1)))))
691 while (n
& (16 -1)) {
697 x
= make_integer (0, x
);
704 int16
add (int16 x
, int16 y
) {
707 int16 negc
= (0 + (3 - -1));
713 if (((x
) == (negc
))) {
714 result
= norm (result
, y
);
718 if (((y
) == (negc
))) {
719 result
= norm (result
, x
);
727 if (((negc
) == ((0 + (3 - -1)))))
728 negc
= negative_carry (dx
< dy
);
731 negc
= negative_carry (dx
<= dy
);
737 result
= make_integer (dx
, result
);
743 int16
invert (int16 x
) {
744 if (((x
) == ((0 + (3 - -1)))))
745 return ((0 + (3 - -1))-1);
747 return (0 + (3 - -1));
750 int16
sub (int16 x
, int16 y
) {
752 int16 negc
= ((0 + (3 - -1))-1);
758 if (((x
) == (negc
)) && (((y
) == ((0 + (3 - -1)))) || ((y
) == (((0 + (3 - -1))-1))))) {
759 result
= norm (result
, invert (y
));
763 if (((y
) == (invert (negc
)))) {
764 result
= norm (result
, x
);
769 dy
= ~integer_lo (y
);
772 if (((negc
) == ((0 + (3 - -1)))))
773 negc
= negative_carry (dx
< dy
);
776 negc
= negative_carry (dx
<= dy
);
782 result
= make_integer (dx
, result
);
788 int16
neg (int16 x
) {
791 return sub ((0 + (3 - -1)), x
);
794 int16
scale (int16 n
, int16 x
) {
801 if ((n
== 0) || ((x
) == ((0 + (3 - -1)))))
802 return (0 + (3 - -1));
811 if (((x
) == ((0 + (3 - -1))))){
813 result
= norm (result
, (carry
+ (3 - -1)));
815 result
= norm (result
, make_integer (carry
, (0 + (3 - -1))));
819 if (((x
) == (((0 + (3 - -1))-1)))) {
822 if (carry
>= ((tmp
<<16) + -1))
823 result
= norm (result
, ((carry
& #xff) + (3 - -1)));
825 result
= norm (result
, make_integer (carry
, ((0 + (3 - -1))-1)));
829 int32 tmp1
= integer_lo (x
);
830 m
= tmp1
* n
+ carry
;
835 result
= make_integer (tmp2
, result
);
841 int16
mulnonneg (int16 x
, int16 y
) {
846 int16 s
= scale (integer_lo (x
), y
);
849 result
= make_integer (integer_lo (s
), result
);
853 if (((x
) == ((0 + (3 - -1)))))
856 s
= add (s
, scale (integer_lo (x
), y
));
859 return norm (result
, s
);
863 int16
divnonneg (int16 x
, int16 y
) {
867 int16 result
= (0 + (3 - -1));
868 int16 lx
= integer_length (x
);
869 int16 ly
= integer_length (y
);
874 y
= shift_left (y
, lx
);
877 result
= shl (result
);
878 if (cmp (x
, y
) >= 0) {
880 result
= add (((0 + (3 - -1))+1), result
);
889 int16
bitwise_ior (int16 x
, int16 y
) {
895 if (((x
) == ((0 + (3 - -1)))))
896 return norm(result
, y
);
897 if (((x
) == (((0 + (3 - -1))-1))))
898 return norm(result
, x
);
899 result
= make_integer(integer_lo(x
) | integer_lo(y
),
906 int16
bitwise_xor (int16 x
, int16 y
) {
912 if (((x
) == ((0 + (3 - -1)))))
913 return norm(result
, y
);
914 if (((x
) == (((0 + (3 - -1))-1))))
915 return norm(result
, x
);
916 result
= make_integer(integer_lo(x
) ^ integer_lo(y
),
925 int16
encode_int (int32 n
) {
926 if (n
>= -1 && n
<= 255) {
928 return (tmp
+ (3 - -1));
931 return alloc_ram_cell_init (0, (0 + (3 - -1)), n
>> 8, n
);
933 void decode_2_int_args () {
934 a1
= decode_int (arg1
);
935 a2
= decode_int (arg2
);
942 void prim_numberp () {
944 && arg1
<= (3 + (255 - -1)))
947 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))){
948 arg1
= (ram_get_field0 (arg1
) & #xc0) == 0;
950 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
951 arg1
= (rom_get_field0 (arg1
) & #xc0) == 0;
958 arg1
= add (arg1
, arg2
);
968 arg1
= sub (arg1
, arg2
);
980 arg1
= mulnonneg (a1
? neg(arg1
) : arg1
,
981 a2
? neg(arg2
) : arg2
);
993 if (((arg2
) == (((0) + (3 - -1)))))
997 arg1
= divnonneg (a1
? neg(arg1
) : arg1
,
998 a2
? neg(arg2
) : arg2
);
1012 if (((arg2
) == (((0) + (3 - -1)))))
1014 if (negp(arg1
) || negp(arg2
))
1018 arg3
= divnonneg (arg1
, arg2
);
1019 arg4
= mulnonneg (arg2
, arg3
);
1020 arg1
= sub(arg1
, arg4
);
1043 arg1
= ((cmp (arg1
, arg2
) == 0));
1053 arg1
= ((cmp (arg1
, arg2
) < 0));
1063 arg1
= ((cmp (arg1
, arg2
) > 0));
1073 arg1
= ((cmp (arg1
, arg2
) <= 0));
1084 arg1
= ((cmp (arg1
, arg2
) >= 0));
1094 arg1
= bitwise_ior(arg1
, arg2
);
1104 arg1
= bitwise_xor(arg1
, arg2
);
1118 void prim_pairp () {
1119 if ((!((arg1
) >= 4096) && ((arg1
) >= 512)))
1120 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0))));
1121 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1122 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0))));
1127 int16
cons (int16 car
, int16 cdr
) {
1128 return alloc_ram_cell_init (#x80 | (car >> 8),
1135 arg1
= cons (arg1
, arg2
);
1140 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1141 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1143 arg1
= ram_get_car (arg1
);
1145 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1146 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0)))
1148 arg1
= rom_get_car (arg1
);
1155 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1156 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1158 arg1
= ram_get_cdr (arg1
);
1160 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1161 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0)))
1163 arg1
= rom_get_cdr (arg1
);
1169 void prim_set_car () {
1170 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1171 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1174 ram_set_car (arg1
, arg2
);
1182 void prim_set_cdr () {
1183 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1184 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1187 ram_set_cdr (arg1
, arg2
);
1195 void prim_nullp () {
1196 arg1
= ((arg1
== 2));
1203 void prim_u8vectorp () {
1204 if ((!((arg1
) >= 4096) && ((arg1
) >= 512)))
1205 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60))));
1206 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1207 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60))));
1212 void prim_make_u8vector () {
1213 decode_2_int_args ();
1218 arg3
= alloc_vec_cell (a1
);
1219 arg1
= alloc_ram_cell_init (#x80 | (a1 >> 8),
1220 a1
& #xff, #x60 | (arg3 >> 8),
1225 ram_set_field0 (arg3
, a2
);
1226 ram_set_field1 (arg3
, a2
);
1227 ram_set_field2 (arg3
, a2
);
1228 ram_set_field3 (arg3
, a2
);
1233 void prim_u8vector_ref () {
1234 a2
= decode_int (arg2
);
1236 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1237 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1239 if ((ram_get_car (arg1
) <= a2
) || (a2
< 0))
1241 arg1
= ram_get_cdr (arg1
);
1243 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1244 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)))
1246 if ((rom_get_car (arg1
) <= a2
) || (a2
< 0))
1248 arg1
= rom_get_cdr (arg1
);
1253 if (((arg1
) >= 4096)) {
1257 arg1
= encode_int (ram_get_fieldn (arg1
, a2
));
1261 arg1
= rom_get_cdr (arg1
);
1264 arg1
= rom_get_car (arg1
);
1272 void prim_u8vector_set () {
1273 a2
= decode_int (arg2
);
1274 a3
= decode_int (arg3
);
1279 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1280 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1282 if ((ram_get_car (arg1
) <= a2
) || (a2
< 0))
1284 arg1
= ram_get_cdr (arg1
);
1292 ram_set_fieldn (arg1
, a2
, a3
);
1299 void prim_u8vector_length () {
1300 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1301 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1303 arg1
= encode_int (ram_get_car (arg1
));
1305 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1306 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)))
1308 arg1
= encode_int (rom_get_car (arg1
));
1314 void prim_u8vector_copy () {
1318 a1
= decode_int (arg2
);
1319 a2
= decode_int (arg4
);
1320 a3
= decode_int (arg5
);
1323 if ((!((arg1
) >= 4096) && ((arg1
) >= 512)) && (!((arg3
) >= 4096) && ((arg3
) >= 512))) {
1324 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)) || !(((ram_get_field0 (arg3) & #x80) == #x80) && ((ram_get_field2 (arg3) & #xe0) == #x60)))
1326 if ((ram_get_car (arg1
) < (a1
+ a3
)) || (a1
< 0) ||
1327 (ram_get_car (arg3
) < (a2
+ a3
)) || (a2
< 0))
1331 arg1
= ram_get_cdr (arg1
);
1334 arg3
= ram_get_cdr (arg3
);
1340 ram_set_fieldn (arg3
, a2
, ram_get_fieldn (arg1
, a1
));
1351 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))) && (!((arg3
) >= 4096) && ((arg3
) >= 512))) {
1352 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)) || !(((ram_get_field0 (arg3) & #x80) == #x80) && ((ram_get_field2 (arg3) & #xe0) == #x60)))
1354 if ((rom_get_car (arg1
) < (a1
+ a3
)) || (a1
< 0) ||
1355 (ram_get_car (arg3
) < (a2
+ a3
)) || (a2
< 0))
1358 arg1
= rom_get_cdr (arg1
);
1360 arg1
= rom_get_cdr (arg1
);
1362 arg3
= ram_get_cdr (arg3
);
1367 ram_set_fieldn (arg3
, a2
, decode_int (rom_get_car (arg1
)));
1369 arg1
= rom_get_cdr (arg1
);
1390 arg1
= ((arg1
== arg2
));
1395 arg1
= ((arg1
== 0));
1398 void prim_symbolp () {
1399 if ((!((arg1
) >= 4096) && ((arg1
) >= 512)))
1400 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x20))));
1401 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1402 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x20))));
1407 void prim_stringp () {
1408 if ((!((arg1
) >= 4096) && ((arg1
) >= 512)))
1409 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x40))));
1410 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1411 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x40))));
1416 void prim_string2list () {
1417 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1418 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x40)))
1421 arg1
= ram_get_car (arg1
);
1423 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1424 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x40)))
1427 arg1
= rom_get_car (arg1
);
1433 void prim_list2string () {
1434 arg1
= alloc_ram_cell_init (#x80 | ((arg1 & #x1f00) >> 8),
1439 void prim_booleanp () {
1440 arg1
= ((arg1
< 2));
1447 void prim_print () {
1455 int32
read_clock () {
1459 /* now = from_now( 0 ); */
1463 void prim_clock () {
1464 arg1
= encode_int (read_clock ());
1467 void prim_motor () {
1468 decode_2_int_args ();
1470 if (a1
< 1 || a1
> 2 || a2
< -100 || a2
> 100)
1474 /* MOTOR_set( a1, a2 ); */
1488 decode_2_int_args ();
1489 a3
= decode_int (arg3
);
1491 if (a1
< 1 || a1
> 3 || a2
< 0 || a3
< 0)
1495 /* LED_set( a1, a2, a3 ); */
1509 void prim_led2_color () {
1510 a1
= decode_int (arg1
);
1512 if (a1
< 0 || a1
> 1)
1516 /* LED2_color_set( a1 ); */
1528 void prim_getchar_wait () {
1529 decode_2_int_args();
1530 a1
= read_clock () + a1
;
1532 if (a1
< 0 || a2
< 1 || a2
> 3)
1538 /* serial_port_set ports; */
1539 /* ports = serial_rx_wait_with_timeout( a2, a1 ); */
1540 /* if (ports != 0) */
1541 /* arg1 = encode_int (serial_rx_read( ports )); */
1546 void prim_putchar () {
1547 decode_2_int_args ();
1549 if (a1
< 0 || a1
> 255 || a2
< 1 || a2
> 3)
1553 /* serial_tx_write( a2, a1 ); */
1567 decode_2_int_args ();
1569 if (a1
< 1 || a1
> 255 || a2
< 0)
1573 /* beep( a1, from_now( a2 ) ); */
1589 a1
= decode_int (arg1
);
1591 if (a1
< 1 || a1
> 3)
1595 /* x = adc( a1 ); */
1596 arg1
= encode_int (x
);
1599 void prim_sernum () {
1603 /* x = serial_num (); */
1610 arg1
= encode_int (x
);
1617 void prim_network_init () {
1625 void prim_network_cleanup () {
1631 void prim_receive_packet_to_u8vector () {
1633 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1637 void prim_send_packet_from_u8vector () {
1641 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1644 a2
= decode_int (arg2
);
1648 if (ram_get_car (arg1
) < a2
)
1651 arg1
= ram_get_cdr (arg1
);
1655 /* void push_arg1 (); */
1657 /* void pop_procedure (); */
1658 /* void handle_arity_and_rest_param (); */
1659 /* void build_env (); */
1660 /* void save_cont (); */
1661 /* void interpreter (); */
1664 env
= cons (arg1
, env
);
1669 int16 o
= ram_get_car (env
);
1670 env
= ram_get_cdr (env
);
1674 void pop_procedure () {
1677 if ((!((arg1
) >= 4096) && ((arg1
) >= 512))) {
1678 if (!((ram_get_field0 (arg1
) & #xc0) == #x40))
1681 entry
= ram_get_entry (arg1
) + #x5000;
1683 else if ((!((arg1
) >= 4096) && !(!((arg1
) >= 4096) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1684 if (!((rom_get_field0 (arg1
) & #xc0) == #x40))
1687 entry
= rom_get_entry (arg1
) + #x5000;
1693 void handle_arity_and_rest_param () {
1696 np
= rom_get (entry
++);
1698 if ((np
& #x80) == 0) {
1713 arg3
= cons (arg4
, arg3
);
1719 arg1
= cons (arg3
, arg1
);
1728 arg1
= cons (arg3
, arg1
);
1738 arg3
= alloc_ram_cell_init (#x40 | (pc >> 11),
1740 ((pc
& #x0007) << 5) | (env >> 8),
1742 cont
= alloc_ram_cell_init (#x80 | (cont >> 8),
1743 cont
& #xff, #x80 | (arg3 >> 8),
1748 void init_ram_heap () {
1754 while (o
> (512 + (glovars
+ 1) >> 1)) {
1757 ram_set_gc_tags (o
, (0<<5));
1758 ram_set_car (o
, free_list
);
1763 free_list_vec
= 4096;
1764 ram_set_car (free_list_vec
, 0);
1768 ram_set_cdr (free_list_vec
, ((8191 - 4096 + 1)*4) >> 2);
1770 for (i
=0; i
<glovars
; i
++)
1782 void interpreter () {
1783 int16 tmp
= rom_get (#x5000 +2);
1784 pc
= (#x5000 + 4) + (tmp << 2);
1786 glovars
= rom_get (#x5000 +3);
1790 dispatch
: ; bytecode
= rom_get (pc
++); bytecode_hi4
= bytecode
& #xf0; bytecode_lo4 = bytecode & #x0f; switch (bytecode_hi4 >> 4) {;
1793 case 0: // TODO used to be #x00 >> 4
1797 arg1
= bytecode_lo4
;
1807 arg1
= bytecode_lo4
+16;
1820 while (bytecode_lo4
!= 0) {
1821 arg1
= ram_get_cdr (arg1
);
1825 arg1
= ram_get_car (arg1
);
1840 while (bytecode_lo4
!= 0) {
1841 arg1
= ram_get_cdr (arg1
);
1845 arg1
= ram_get_car (arg1
);
1856 arg1
= get_global (bytecode_lo4
);
1867 set_global (bytecode_lo4
, pop());
1879 handle_arity_and_rest_param ();
1898 handle_arity_and_rest_param ();
1911 switch (bytecode_lo4
) {
1913 bytecode
= rom_get (pc
++);
1916 bytecode
= rom_get (pc
++);
1921 entry
= (arg2
<< 8) + bytecode
+ #x5000;
1924 na
= rom_get (entry
++);
1938 bytecode
= rom_get (pc
++);
1941 bytecode
= rom_get (pc
++);
1946 entry
= (arg2
<< 8) + bytecode
+ #x5000;
1949 na
= rom_get (entry
++);
1962 bytecode
= rom_get (pc
++);
1965 bytecode
= rom_get (pc
++);
1970 pc
= (arg2
<< 8) + bytecode
+ #x5000;
1975 bytecode
= rom_get (pc
++);
1978 bytecode
= rom_get (pc
++);
1984 pc
= (arg2
<< 8) + bytecode
+ #x5000;
1989 bytecode
= rom_get (pc
++);
1992 bytecode
= rom_get (pc
++);
1998 entry
= (arg2
<< 8) | bytecode
;
2000 arg1
= alloc_ram_cell_init (#x40 | (arg2 >> 3),
2001 ((arg2
& #x07) << 5) | (bytecode >> 3),
2002 ((bytecode
) << 5) |((arg3 ἀ) >>8),
2013 bytecode
= rom_get (pc
++);
2018 entry
= pc
+ bytecode
+ #x5000;
2021 na
= rom_get (entry
++);
2034 bytecode
= rom_get (pc
++);
2039 entry
= pc
+ bytecode
+ #x5000;
2042 na
= rom_get (entry
++);
2054 bytecode
= rom_get (pc
++);
2058 pc
= pc
+ bytecode
+ #x5000;
2063 bytecode
= rom_get (pc
++);
2069 pc
= pc
+ bytecode
+ #x5000;
2074 bytecode
= rom_get (pc
++);
2080 entry
= pc
+ bytecode
;
2082 arg1
= alloc_ram_cell_init (#x40 | (arg2 >> 3),
2083 ((arg2
& #x07) << 5) | (bytecode >> 3),
2084 ((bytecode
) <<5) |((arg3 ἀ) >>8),
2093 bytecode
= rom_get (pc
++);
2097 arg1
= get_global (bytecode
);
2104 bytecode
= rom_get (pc
++);
2108 set_global (bytecode
, pop());
2120 bytecode
= rom_get (pc
++);
2124 arg1
= (bytecode_lo4
<< 8) | bytecode
;
2144 switch (bytecode_lo4
) {
2146 arg1
= pop(); prim_numberp (); push_arg1 (); break;
2148 arg2
= pop(); arg1
= pop(); prim_add (); push_arg1 (); break;
2150 arg2
= pop(); arg1
= pop(); prim_sub (); push_arg1 (); break;
2152 arg2
= pop(); arg1
= pop(); prim_mul (); push_arg1 (); break;
2154 arg2
= pop(); arg1
= pop(); prim_div (); push_arg1 (); break;
2156 arg2
= pop(); arg1
= pop(); prim_rem (); push_arg1 (); break;
2158 arg1
= pop(); prim_neg (); push_arg1 (); break;
2160 arg2
= pop(); arg1
= pop(); prim_eq (); push_arg1 (); break;
2162 arg2
= pop(); arg1
= pop(); prim_lt (); push_arg1 (); break;
2164 arg2
= pop(); arg1
= pop(); prim_leq (); push_arg1 (); break;
2166 arg2
= pop(); arg1
= pop(); prim_gt (); push_arg1 (); break;
2168 arg2
= pop(); arg1
= pop(); prim_geq (); push_arg1 (); break;
2170 arg1
= pop(); prim_pairp (); push_arg1 (); break;
2172 arg2
= pop(); arg1
= pop(); prim_cons (); push_arg1 (); break;
2174 arg1
= pop(); prim_car (); push_arg1 (); break;
2176 arg1
= pop(); prim_cdr (); push_arg1 (); break;
2186 switch (bytecode_lo4
) {
2188 arg2
= pop(); arg1
= pop(); prim_set_car (); break;
2190 arg2
= pop(); arg1
= pop(); prim_set_cdr (); break;
2192 arg1
= pop(); prim_nullp (); push_arg1 (); break;
2194 arg2
= pop(); arg1
= pop(); prim_eqp (); push_arg1 (); break;
2196 arg1
= pop(); prim_not (); push_arg1 (); break;
2213 handle_arity_and_rest_param ();
2228 arg2
= ram_get_cdr (cont
);
2230 pc
= ram_get_entry (arg2
);
2232 env
= ram_get_cdr (arg2
);
2233 cont
= ram_get_car (cont
);
2244 arg1
= pop(); prim_symbolp (); push_arg1 (); break;
2247 arg1
= pop(); prim_stringp (); push_arg1 (); break;
2250 arg1
= pop(); prim_string2list (); push_arg1 (); break;
2253 arg1
= pop(); prim_list2string (); push_arg1 (); break;
2256 arg2
= pop(); arg1
= pop(); prim_make_u8vector (); push_arg1 (); break;
2259 arg2
= pop(); arg1
= pop(); prim_u8vector_ref (); push_arg1 (); break;
2262 arg3
= pop(); arg2
= pop(); arg1
= pop(); prim_u8vector_set (); break;
2272 switch (bytecode_lo4
) {
2280 prim_clock (); push_arg1 (); break;
2283 arg2
= pop(); arg1
= pop(); prim_motor (); break;
2286 arg3
= pop(); arg2
= pop(); arg1
= pop(); prim_led (); ;break;
2289 arg1
= pop(); prim_led2_color (); break;
2292 arg2
= pop(); arg1
= pop(); prim_getchar_wait (); push_arg1 (); break;
2295 arg2
= pop(); arg1
= pop(); prim_putchar (); break;
2298 arg2
= pop(); arg1
= pop(); prim_beep (); break;
2301 arg1
= pop(); prim_adc (); push_arg1 (); break;
2304 arg1
= pop(); prim_u8vectorp (); push_arg1 (); break;
2307 prim_sernum (); push_arg1 (); break;
2310 arg1
= pop(); prim_u8vector_length (); push_arg1 (); break;
2313 arg5
= pop(); arg4
= pop(); arg3
= pop(); arg2
= pop(); arg1
= pop();
2314 prim_u8vector_copy (); break;
2329 arg2
= ram_get_cdr (cont
);
2330 pc
= ram_get_entry (arg2
);
2331 env
= ram_get_cdr (arg2
);
2332 cont
= ram_get_car (cont
);
2346 switch (bytecode_lo4
) {
2349 arg1
= pop(); prim_booleanp (); push_arg1 (); break;
2352 prim_network_init (); break;
2355 prim_network_cleanup (); break;
2358 arg1
= pop(); prim_receive_packet_to_u8vector (); push_arg1 (); break;
2361 arg2
= pop(); arg1
= pop(); prim_send_packet_from_u8vector ();
2362 push_arg1 (); break;
2364 arg2
= pop(); arg1
= pop(); prim_ior (); push_arg1 (); break;
2367 arg2
= pop(); arg1
= pop(); prim_xor (); push_arg1 (); break;