11#include <stdio.h>
22#include <stdlib.h>
33#include <inttypes.h>
4- #include <string.h>
54#include "values.h"
65#include "print.h"
76#include "runtime.h"
@@ -30,7 +29,7 @@ int main(int argc, char** argv)
3029 error_handler = & error_exit ;
3130 heap = malloc (2 * 8 * heap_size );
3231 types = malloc (sizeof (type_t ) * heap_size );
33-
32+
3433 val_t result ;
3534
3635 result = entry (heap );
@@ -42,121 +41,3 @@ int main(int argc, char** argv)
4241 free (heap );
4342 return 0 ;
4443}
45-
46- const char * val_typeof_string (int64_t t ) {
47- switch (val_typeof (t )) {
48- case T_INT : return "INT" ;
49- case T_BOOL : return "BOOL" ;
50- case T_CHAR : return "CHAR" ;
51- case T_EOF : return "EOF" ;
52- case T_VOID : return "VOID" ;
53- case T_EMPTY : return "EMPTY" ;
54- case T_BOX : return "BOX" ;
55- case T_CONS : return "CONS" ;
56- case T_VECT : return "VECT" ;
57- case T_STR : return "STR" ;
58- default : return "UNKNOWN" ;
59- }
60- }
61-
62- void step (val_t * to , val_t * * to_curr , val_t * * to_next , int count , int * t_back ) {
63- type_t t ;
64- int i ;
65- int size ;
66- for (i = 0 ; i < count ; i ++ ) {
67- t = val_typeof (* * to_curr );
68- switch (t ) {
69- case T_BOX :
70- case T_CONS :
71- case T_VECT :
72- if (val_unwrap (* val_unwrap (* * to_curr )) >= to &&
73- val_unwrap (* val_unwrap (* * to_curr )) < to + heap_size ) {
74- // this is a fwd pointer (points in to to-space), so just set
75- // curr to what it points to.
76- * * to_curr = * val_unwrap (* * to_curr );
77- * to_curr = * to_curr + 1 ;
78- } else {
79- // not a fwd pointer, copy to to_next
80- size = val_size (val_unwrap (* * to_curr ), t );
81- types [* t_back ] = t ; // enqueue type
82- * t_back = * t_back + 1 ;
83- memcpy (* to_next , val_unwrap (* * to_curr ), 8 * size ); // copy
84- * val_unwrap (* * to_curr ) = val_wrap (* to_next , t ); // fwd
85- * * to_curr = val_wrap (* to_next , t ); // update
86- * to_next = * to_next + size ;
87- * to_curr = * to_curr + 1 ;
88- };
89- break ;
90- case T_STR :
91- printf ("STRING STEP\n" );
92- break ;
93- default :
94- * to_curr = * to_curr + 1 ;
95- };
96- };
97- }
98-
99-
100- int64_t * collect_garbage (int64_t * rsp , int64_t * rbp , int64_t * rbx ) {
101- int stack_count = rbp - rsp ;
102-
103- val_t * from = heap + ((rbx < (heap + heap_size )) ? 0 : heap_size );
104- val_t * to = heap + ((rbx < (heap + heap_size )) ? heap_size : 0 );
105-
106- val_t * to_next = to ;
107- val_t * to_curr = to ;
108-
109- int t_back = 0 ;
110- int t_front = 0 ;
111-
112- // Step through everything on the stack
113- val_t * rsp_curr = rsp ;
114- step (to , & rsp_curr , & to_next , stack_count , & t_back );
115-
116- int vi ;
117- // now play catch up between to_curr and to_next
118- while (to_curr != to_next ) {
119- switch (types [t_front ++ ]) {
120- case T_VECT :
121- vi = to_curr [0 ];
122- to_curr ++ ;
123- step (to , & to_curr , & to_next , vi , & t_back );
124- break ;
125- case T_BOX :
126- step (to , & to_curr , & to_next , 1 , & t_back );
127- break ;
128- case T_CONS :
129- step (to , & to_curr , & to_next , 2 , & t_back );
130- break ;
131- case T_STR :
132- printf ("STRING!!!\n" );
133- printf ("size: %lld\n" , 1 + ((* to_curr + 1 ) / 2 ));
134- to_curr = to_curr + 3 ;
135- default :
136- to_curr ++ ;
137- break ;
138- }
139- }
140- return to_next ;
141- }
142-
143-
144- void print_memory (int64_t * rsp , int64_t * rbp , int64_t * rbx ) {
145-
146- val_t * h = heap + ((rbx < (heap + heap_size )) ? 0 : heap_size );
147-
148- int stack_count = rbp - rsp ;
149- int heap_count = rbx - h ;
150-
151- printf ("----------------------------------------------------------------\n" );
152- int i ;
153-
154- printf ("STACK:\n" );
155- for (i = 0 ; i < stack_count ; i ++ ) {
156- printf ("[%" PRIx64 "] = %016" PRIx64 ", %s\n" , (int64_t )rsp + 8 * i , rsp [i ], val_typeof_string (rsp [i ]));
157- }
158- printf ("HEAP:\n" );
159- for (i = 0 ; i < heap_count ; i ++ ) {
160- printf ("[%" PRIx64 "] = %016" PRIx64 ", %s\n" , (int64_t )h + 8 * i , h [i ], val_typeof_string (h [i ]));
161- }
162- }
0 commit comments