]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
moved gc_locals accessors into vm
[factor.git] / vm / vm.hpp
1 namespace factor
2 {
3
4 struct factorvm {
5
6         // if you change this struct, also change vm.factor k--------
7         context *stack_chain; 
8         zone nursery; /* new objects are allocated here */
9         cell cards_offset;
10         cell decks_offset;
11 #ifndef FACTOR_64
12         cell __padding__ ;   // align to 8 byte boundary
13 #endif
14         cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */
15 #ifndef FACTOR_64
16         cell __padding2__;   // not sure why we need this, bootstrap doesn't work without it
17 #endif
18
19         // segments
20         inline cell align_page(cell a);
21
22         // contexts
23         cell ds_size, rs_size;
24         context *unused_contexts;
25         void reset_datastack();
26         void reset_retainstack();
27         void fix_stacks();
28         void save_stacks();
29         context *alloc_context();
30         void dealloc_context(context *old_context);
31         void nest_stacks();
32         void unnest_stacks();
33         void init_stacks(cell ds_size_, cell rs_size_);
34         bool stack_to_array(cell bottom, cell top);
35         cell array_to_stack(array *array, cell bottom);
36         inline void vmprim_datastack();
37         inline void vmprim_retainstack();
38         inline void vmprim_set_datastack();
39         inline void vmprim_set_retainstack();
40         inline void vmprim_check_datastack();
41
42         // run
43         cell T;  /* Canonical T object. It's just a word */
44         inline void vmprim_getenv();
45         inline void vmprim_setenv();
46         inline void vmprim_exit();
47         inline void vmprim_micros();
48         inline void vmprim_sleep();
49         inline void vmprim_set_slot();
50         inline void vmprim_load_locals();
51         cell clone_object(cell obj_);
52         inline void vmprim_clone();
53
54         // profiler
55         bool profiling_p;
56         void init_profiler();
57         code_block *compile_profiling_stub(cell word_);
58         void set_profiling(bool profiling);
59         inline void vmprim_profiling();
60
61         // errors
62         void out_of_memory();
63         void fatal_error(const char* msg, cell tagged);
64         void critical_error(const char* msg, cell tagged);
65         void throw_error(cell error, stack_frame *callstack_top);
66         void not_implemented_error();
67         bool in_page(cell fault, cell area, cell area_size, int offset);
68         void memory_protection_error(cell addr, stack_frame *native_stack);
69         void signal_error(int signal, stack_frame *native_stack);
70         void divide_by_zero_error();
71         void fp_trap_error(stack_frame *signal_callstack_top);
72         inline void vmprim_call_clear();
73         inline void vmprim_unimplemented();
74         void memory_signal_handler_impl();
75         void misc_signal_handler_impl();
76         void fp_signal_handler_impl();
77         void type_error(cell type, cell tagged);
78         void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top);
79
80         //callstack
81
82         // bignum
83         int bignum_equal_p(bignum * x, bignum * y);
84         enum bignum_comparison bignum_compare(bignum * x, bignum * y);
85         bignum *bignum_add(bignum * x, bignum * y);
86         bignum *bignum_subtract(bignum * x, bignum * y);
87         bignum *bignum_multiply(bignum * x, bignum * y);
88         void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder);
89         bignum *bignum_quotient(bignum * numerator, bignum * denominator);
90         bignum *bignum_remainder(bignum * numerator, bignum * denominator);
91         cell bignum_to_cell(bignum * bignum);
92         fixnum bignum_to_fixnum(bignum * bignum);
93         s64 bignum_to_long_long(bignum * bignum);
94         u64 bignum_to_ulong_long(bignum * bignum);
95         double bignum_to_double(bignum * bignum);
96         bignum *double_to_bignum(double x);
97         int bignum_equal_p_unsigned(bignum * x, bignum * y);
98         enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y);
99         bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p);
100         bignum *bignum_subtract_unsigned(bignum * x, bignum * y);
101         bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p);
102         bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p);
103         void bignum_destructive_add(bignum * bignum, bignum_digit_type n);
104         void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor);
105         void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, 
106                                                                                                   bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p);
107         void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q);
108         bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, 
109                                                                                          bignum_digit_type guess, bignum_digit_type * u_start);
110         void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, 
111                                                                                                    bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p);
112         void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left);
113         void bignum_destructive_unnormalization(bignum * bignum, int shift_right);
114         bignum_digit_type bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, 
115                                                                                   bignum_digit_type v, bignum_digit_type * q) /* return value */;
116         bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, 
117                                                                                                    bignum_digit_type guess, bignum_digit_type * u);
118         void bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, 
119                                                                                                   bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p);
120         bignum_digit_type bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator);
121         bignum * bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p);
122         bignum *bignum_digit_to_bignum(bignum_digit_type digit, int negative_p);
123         bignum *allot_bignum(bignum_length_type length, int negative_p);
124         bignum * allot_bignum_zeroed(bignum_length_type length, int negative_p);
125         bignum *bignum_shorten_length(bignum * bignum, bignum_length_type length);
126         bignum *bignum_trim(bignum * bignum);
127         bignum *bignum_new_sign(bignum * x, int negative_p);
128         bignum *bignum_maybe_new_sign(bignum * x, int negative_p);
129         void bignum_destructive_copy(bignum * source, bignum * target);
130         bignum *bignum_bitwise_not(bignum * x);
131         bignum *bignum_arithmetic_shift(bignum * arg1, fixnum n);
132         bignum *bignum_bitwise_and(bignum * arg1, bignum * arg2);
133         bignum *bignum_bitwise_ior(bignum * arg1, bignum * arg2);
134         bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2);
135         bignum *bignum_magnitude_ash(bignum * arg1, fixnum n);
136         bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2);
137         bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2);
138         bignum *bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2);
139         void bignum_negate_magnitude(bignum * arg);
140         bignum *bignum_integer_length(bignum * x);
141         int bignum_logbitp(int shift, bignum * arg);
142         int bignum_unsigned_logbitp(int shift, bignum * bignum);
143         bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm *), unsigned int radix, int negative_p);
144
145         //data_heap
146         bool secure_gc;  /* Set by the -securegc command line argument */
147         bool gc_off; /* GC is off during heap walking */
148         data_heap *data;
149         /* A heap walk allows useful things to be done, like finding all
150            references to an object for debugging purposes. */
151         cell heap_scan_ptr;
152         cell init_zone(zone *z, cell size, cell start);
153         void init_card_decks();
154         data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size);
155         data_heap *grow_data_heap(data_heap *data, cell requested_bytes);
156         void dealloc_data_heap(data_heap *data);
157         void clear_cards(cell from, cell to);
158         void clear_decks(cell from, cell to);
159         void clear_allot_markers(cell from, cell to);
160         void reset_generation(cell i);
161         void reset_generations(cell from, cell to);
162         void set_data_heap(data_heap *data_);
163         void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_);
164         cell untagged_object_size(object *pointer);
165         cell unaligned_object_size(object *pointer);
166         inline void vmprim_size();
167         cell binary_payload_start(object *pointer);
168         inline void vmprim_data_room();
169         void begin_scan();
170         void end_scan();
171         inline void vmprim_begin_scan();
172         cell next_object();
173         inline void vmprim_next_object();
174         inline void vmprim_end_scan();
175         template<typename T> void each_object(T &functor);
176         cell find_all_words();
177         cell object_size(cell tagged);
178
179         
180         //write barrier
181         cell allot_markers_offset;
182         inline card *addr_to_card(cell a);
183         inline cell card_to_addr(card *c);
184         inline cell card_offset(card *c);
185         inline card_deck *addr_to_deck(cell a);
186         inline cell deck_to_addr(card_deck *c);
187         inline card *deck_to_card(card_deck *d);
188         inline card *addr_to_allot_marker(object *a);
189         inline void write_barrier(object *obj);
190         inline void allot_barrier(object *address);
191
192
193         //data_gc
194         /* used during garbage collection only */
195         zone *newspace;
196         bool performing_gc;
197         bool performing_compaction;
198         cell collecting_gen;
199
200         /* if true, we are collecting aging space for the second time, so if it is still
201            full, we go on to collect tenured */
202         bool collecting_aging_again;
203
204         /* in case a generation fills up in the middle of a gc, we jump back
205            up to try collecting the next generation. */
206         jmp_buf gc_jmp;
207
208         gc_stats stats[max_gen_count];
209         u64 cards_scanned;
210         u64 decks_scanned;
211         u64 card_scan_time;
212         cell code_heap_scans;
213
214         /* What generation was being collected when copy_code_heap_roots() was last
215            called? Until the next call to add_code_block(), future
216            collections of younger generations don't have to touch the code
217            heap. */
218         cell last_code_heap_scan;
219
220         /* sometimes we grow the heap */
221         bool growing_data_heap;
222         data_heap *old_data_heap;
223
224         DEFPUSHPOP(gc_local_,gc_locals)
225
226
227         void init_data_gc();
228         object *copy_untagged_object_impl(object *pointer, cell size);
229         object *copy_object_impl(object *untagged);
230         bool should_copy_p(object *untagged);
231         object *resolve_forwarding(object *untagged);
232         template <typename T> T *copy_untagged_object(T *untagged);
233         cell copy_object(cell pointer);
234         void copy_handle(cell *handle);
235         void copy_card(card *ptr, cell gen, cell here);
236         void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask);
237         void copy_gen_cards(cell gen);
238         void copy_cards();
239         void copy_stack_elements(segment *region, cell top);
240         void copy_registered_locals();
241         void copy_registered_bignums();
242         void copy_roots();
243         cell copy_next_from_nursery(cell scan);
244         cell copy_next_from_aging(cell scan);
245         cell copy_next_from_tenured(cell scan);
246         void copy_reachable_objects(cell scan, cell *end);
247         void begin_gc(cell requested_bytes);
248         void end_gc(cell gc_elapsed);
249         void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes);
250         void gc();
251         inline void vmprim_gc();
252         inline void vmprim_gc_stats();
253         void clear_gc_stats();
254         inline void vmprim_become();
255         void inline_gc(cell *gc_roots_base, cell gc_roots_size);
256         inline bool collecting_accumulation_gen_p();
257         inline object *allot_zone(zone *z, cell a);
258         inline object *allot_object(header header, cell size);
259         template <typename TYPE> TYPE *allot(cell size);
260         inline void check_data_pointer(object *pointer);
261         inline void check_tagged_pointer(cell tagged);
262         inline void vmprim_clear_gc_stats();
263
264         // local roots
265         /* If a runtime function needs to call another function which potentially
266            allocates memory, it must wrap any local variable references to Factor
267            objects in gc_root instances */
268         std::vector<cell> gc_locals;
269         std::vector<cell> gc_bignums;
270
271         // generic arrays
272         template <typename T> T *allot_array_internal(cell capacity);
273         template <typename T> bool reallot_array_in_place_p(T *array, cell capacity);
274         template <typename TYPE> TYPE *reallot_array(TYPE *array_, cell capacity);
275
276         //debug
277         bool fep_disabled;
278         bool full_output;
279         cell look_for;
280         cell obj;
281         void print_chars(string* str);
282         void print_word(word* word, cell nesting);
283         void print_factor_string(string* str);
284         void print_array(array* array, cell nesting);
285         void print_tuple(tuple *tuple, cell nesting);
286         void print_nested_obj(cell obj, fixnum nesting);
287         void print_obj(cell obj);
288         void print_objects(cell *start, cell *end);
289         void print_datastack();
290         void print_retainstack();
291         void print_stack_frame(stack_frame *frame);
292         void print_callstack();
293         void dump_cell(cell x);
294         void dump_memory(cell from, cell to);
295         void dump_zone(zone *z);
296         void dump_generations();
297         void dump_objects(cell type);
298         void find_data_references_step(cell *scan);
299         void find_data_references(cell look_for_);
300         void dump_code_heap();
301         void factorbug();
302         inline void vmprim_die();
303
304         //arrays
305         array *allot_array(cell capacity, cell fill_);
306         inline void vmprim_array();
307         cell allot_array_1(cell obj_);
308         cell allot_array_2(cell v1_, cell v2_);
309         cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
310         inline void vmprim_resize_array();
311         inline void set_array_nth(array *array, cell slot, cell value);
312
313         //strings
314         cell string_nth(string* str, cell index);
315         void set_string_nth_fast(string *str, cell index, cell ch);
316         void set_string_nth_slow(string *str_, cell index, cell ch);
317         void set_string_nth(string *str, cell index, cell ch);
318         string *allot_string_internal(cell capacity);
319         void fill_string(string *str_, cell start, cell capacity, cell fill);
320         string *allot_string(cell capacity, cell fill);
321         inline void vmprim_string();
322         bool reallot_string_in_place_p(string *str, cell capacity);
323         string* reallot_string(string *str_, cell capacity);
324         inline void vmprim_resize_string();
325         inline void vmprim_string_nth();
326         inline void vmprim_set_string_nth_fast();
327         inline void vmprim_set_string_nth_slow();
328
329         //booleans
330         void box_boolean(bool value);
331         bool to_boolean(cell value);
332         inline cell tag_boolean(cell untagged);
333
334         //byte arrays
335         byte_array *allot_byte_array(cell size);
336         inline void vmprim_byte_array();
337         inline void vmprim_uninitialized_byte_array();
338         inline void vmprim_resize_byte_array();
339
340         //tuples
341         tuple *allot_tuple(cell layout_);
342         inline void vmprim_tuple();
343         inline void vmprim_tuple_boa();
344
345         //words
346         word *allot_word(cell vocab_, cell name_);
347         inline void vmprim_word();
348         inline void vmprim_word_xt();
349         void update_word_xt(cell w_);
350         inline void vmprim_optimized_p();
351         inline void vmprim_wrapper();
352
353         //math
354         cell bignum_zero;
355         cell bignum_pos_one;
356         cell bignum_neg_one;    
357         inline void vmprim_bignum_to_fixnum();
358         inline void vmprim_float_to_fixnum();
359         inline void vmprim_fixnum_divint();
360         inline void vmprim_fixnum_divmod();
361         bignum *fixnum_to_bignum(fixnum);
362         bignum *cell_to_bignum(cell);
363         bignum *long_long_to_bignum(s64 n);
364         bignum *ulong_long_to_bignum(u64 n);
365         inline fixnum sign_mask(fixnum x);
366         inline fixnum branchless_max(fixnum x, fixnum y);
367         inline fixnum branchless_abs(fixnum x);
368         inline void vmprim_fixnum_shift();
369         inline void vmprim_fixnum_to_bignum();
370         inline void vmprim_float_to_bignum();
371         inline void vmprim_bignum_eq();
372         inline void vmprim_bignum_add();
373         inline void vmprim_bignum_subtract();
374         inline void vmprim_bignum_multiply();
375         inline void vmprim_bignum_divint();
376         inline void vmprim_bignum_divmod();
377         inline void vmprim_bignum_mod();
378         inline void vmprim_bignum_and();
379         inline void vmprim_bignum_or();
380         inline void vmprim_bignum_xor();
381         inline void vmprim_bignum_shift();
382         inline void vmprim_bignum_less();
383         inline void vmprim_bignum_lesseq();
384         inline void vmprim_bignum_greater();
385         inline void vmprim_bignum_greatereq();
386         inline void vmprim_bignum_not();
387         inline void vmprim_bignum_bitp();
388         inline void vmprim_bignum_log2();
389         unsigned int bignum_producer(unsigned int digit);
390         inline void vmprim_byte_array_to_bignum();
391         cell unbox_array_size();
392         inline void vmprim_fixnum_to_float();
393         inline void vmprim_bignum_to_float();
394         inline void vmprim_str_to_float();
395         inline void vmprim_float_to_str();
396         inline void vmprim_float_eq();
397         inline void vmprim_float_add();
398         inline void vmprim_float_subtract();
399         inline void vmprim_float_multiply();
400         inline void vmprim_float_divfloat();
401         inline void vmprim_float_mod();
402         inline void vmprim_float_less();
403         inline void vmprim_float_lesseq();
404         inline void vmprim_float_greater();
405         inline void vmprim_float_greatereq();
406         inline void vmprim_float_bits();
407         inline void vmprim_bits_float();
408         inline void vmprim_double_bits();
409         inline void vmprim_bits_double();
410         fixnum to_fixnum(cell tagged);
411         cell to_cell(cell tagged);
412         void box_signed_1(s8 n);
413         void box_unsigned_1(u8 n);
414         void box_signed_2(s16 n);
415         void box_unsigned_2(u16 n);
416         void box_signed_4(s32 n);
417         void box_unsigned_4(u32 n);
418         void box_signed_cell(fixnum integer);
419         void box_unsigned_cell(cell cell);
420         void box_signed_8(s64 n);
421         s64 to_signed_8(cell obj);
422         void box_unsigned_8(u64 n);
423         u64 to_unsigned_8(cell obj);
424         void box_float(float flo);
425         float to_float(cell value);
426         void box_double(double flo);
427         double to_double(cell value);
428         void overflow_fixnum_add(fixnum x, fixnum y);
429         void overflow_fixnum_subtract(fixnum x, fixnum y);
430         void overflow_fixnum_multiply(fixnum x, fixnum y);
431         inline cell allot_integer(fixnum x);
432         inline cell allot_cell(cell x);
433         inline cell allot_float(double n);
434         inline bignum *float_to_bignum(cell tagged);
435         inline double bignum_to_float(cell tagged);
436         inline double untag_float(cell tagged);
437         inline double untag_float_check(cell tagged);
438         inline fixnum float_to_fixnum(cell tagged);
439         inline double fixnum_to_float(cell tagged);
440         template <typename T> T *untag_check(cell value);
441         template <typename T> T *untag(cell value);
442         
443         //io
444         void init_c_io();
445         void io_error();
446         inline void vmprim_fopen();
447         inline void vmprim_fgetc();
448         inline void vmprim_fread();
449         inline void vmprim_fputc();
450         inline void vmprim_fwrite();
451         inline void vmprim_fseek();
452         inline void vmprim_fflush();
453         inline void vmprim_fclose();
454
455         //code_gc
456         void clear_free_list(heap *heap);
457         void new_heap(heap *heap, cell size);
458         void add_to_free_list(heap *heap, free_heap_block *block);
459         void build_free_list(heap *heap, cell size);
460         void assert_free_block(free_heap_block *block);
461         free_heap_block *find_free_block(heap *heap, cell size);
462         free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size);
463         heap_block *heap_allot(heap *heap, cell size);
464         void heap_free(heap *heap, heap_block *block);
465         void mark_block(heap_block *block);
466         void unmark_marked(heap *heap);
467         void free_unmarked(heap *heap, heap_iterator iter);
468         void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free);
469         cell heap_size(heap *heap);
470         cell compute_heap_forwarding(heap *heap, unordered_map<heap_block *,char *> &forwarding);
471         void compact_heap(heap *heap, unordered_map<heap_block *,char *> &forwarding);
472
473         //code_block
474         relocation_type relocation_type_of(relocation_entry r);
475         relocation_class relocation_class_of(relocation_entry r);
476         cell relocation_offset_of(relocation_entry r);
477         void flush_icache_for(code_block *block);
478         int number_of_parameters(relocation_type type);
479         void *object_xt(cell obj);
480         void *xt_pic(word *w, cell tagged_quot);
481         void *word_xt_pic(word *w);
482         void *word_xt_pic_tail(word *w);
483         void undefined_symbol();
484         void *get_rel_symbol(array *literals, cell index);
485         cell compute_relocation(relocation_entry rel, cell index, code_block *compiled);
486         void iterate_relocations(code_block *compiled, relocation_iterator iter);
487         void store_address_2_2(cell *ptr, cell value);
488         void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift);
489         void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value);
490         void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled);
491         void update_literal_references(code_block *compiled);
492         void copy_literal_references(code_block *compiled);
493         void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled);
494         void update_word_references_step(relocation_entry rel, cell index, code_block *compiled);
495         void update_word_references(code_block *compiled);
496         void update_literal_and_word_references(code_block *compiled);
497         void check_code_address(cell address);
498         void mark_code_block(code_block *compiled);
499         void mark_stack_frame_step(stack_frame *frame);
500         void mark_active_blocks(context *stacks);
501         void mark_object_code_block(object *object);
502         void relocate_code_block(code_block *compiled);
503         void fixup_labels(array *labels, code_block *compiled);
504         code_block *allot_code_block(cell size);
505         code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_);
506         inline bool stack_traces_p()
507         {
508                 return userenv[STACK_TRACES_ENV] != F;
509         }
510
511         //code_heap
512         heap code;
513         unordered_map<heap_block *,char *> forwarding;
514         void init_code_heap(cell size);
515         bool in_code_heap_p(cell ptr);
516         void jit_compile_word(cell word_, cell def_, bool relocate);
517         void iterate_code_heap(code_heap_iterator iter);
518         void copy_code_heap_roots();
519         void update_code_heap_words();
520         inline void vmprim_modify_code_heap();
521         inline void vmprim_code_room();
522         code_block *forward_xt(code_block *compiled);
523         void forward_frame_xt(stack_frame *frame);
524         void forward_object_xts();
525         void fixup_object_xts();
526         void compact_code_heap();
527         inline void check_code_pointer(cell ptr);
528
529
530         //image
531         cell code_relocation_base;
532         cell data_relocation_base;
533         void init_objects(image_header *h);
534         void load_data_heap(FILE *file, image_header *h, vm_parameters *p);
535         void load_code_heap(FILE *file, image_header *h, vm_parameters *p);
536         bool save_image(const vm_char *filename);
537         inline void vmprim_save_image();
538         inline void vmprim_save_image_and_exit();
539         void data_fixup(cell *cell);
540         template <typename T> void code_fixup(T **handle);
541         void fixup_word(word *word);
542         void fixup_quotation(quotation *quot);
543         void fixup_alien(alien *d);
544         void fixup_stack_frame(stack_frame *frame);
545         void fixup_callstack_object(callstack *stack);
546         void relocate_object(object *object);
547         void relocate_data();
548         void fixup_code_block(code_block *compiled);
549         void relocate_code();
550         void load_image(vm_parameters *p);
551
552         //callstack
553         template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator);
554         void check_frame(stack_frame *frame);
555         callstack *allot_callstack(cell size);
556         stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom);
557         stack_frame *capture_start();
558         inline void vmprim_callstack();
559         inline void vmprim_set_callstack();
560         code_block *frame_code(stack_frame *frame);
561         cell frame_type(stack_frame *frame);
562         cell frame_executing(stack_frame *frame);
563         stack_frame *frame_successor(stack_frame *frame);
564         cell frame_scan(stack_frame *frame);
565         inline void vmprim_callstack_to_array();
566         stack_frame *innermost_stack_frame(callstack *stack);
567         stack_frame *innermost_stack_frame_quot(callstack *callstack);
568         inline void vmprim_innermost_stack_frame_executing();
569         inline void vmprim_innermost_stack_frame_scan();
570         inline void vmprim_set_innermost_stack_frame_quot();
571         void save_callstack_bottom(stack_frame *callstack_bottom);
572         template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator);
573         inline void do_slots(cell obj, void (* iter)(cell *,factorvm*));
574
575
576         //alien
577         char *pinned_alien_offset(cell obj);
578         cell allot_alien(cell delegate_, cell displacement);
579         inline void vmprim_displaced_alien();
580         inline void vmprim_alien_address();
581         void *alien_pointer();
582         inline void vmprim_dlopen();
583         inline void vmprim_dlsym();
584         inline void vmprim_dlclose();
585         inline void vmprim_dll_validp();
586         char *alien_offset(cell obj);
587         char *unbox_alien();
588         void box_alien(void *ptr);
589         void to_value_struct(cell src, void *dest, cell size);
590         void box_value_struct(void *src, cell size);
591         void box_small_struct(cell x, cell y, cell size);
592         void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size);
593
594         //quotations
595         inline void vmprim_jit_compile();
596         inline void vmprim_array_to_quotation();
597         inline void vmprim_quotation_xt();
598         void set_quot_xt(quotation *quot, code_block *code);
599         void jit_compile(cell quot_, bool relocating);
600         void compile_all_words();
601         fixnum quot_code_offset_to_scan(cell quot_, cell offset);
602         cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);
603         inline void vmprim_quot_compiled_p();
604
605         //dispatch
606         cell megamorphic_cache_hits;
607         cell megamorphic_cache_misses;
608         cell search_lookup_alist(cell table, cell klass);
609         cell search_lookup_hash(cell table, cell klass, cell hashcode);
610         cell nth_superclass(tuple_layout *layout, fixnum echelon);
611         cell nth_hashcode(tuple_layout *layout, fixnum echelon);
612         cell lookup_tuple_method(cell obj, cell methods);
613         cell lookup_hi_tag_method(cell obj, cell methods);
614         cell lookup_hairy_method(cell obj, cell methods);
615         cell lookup_method(cell obj, cell methods);
616         inline void vmprim_lookup_method();
617         cell object_class(cell obj);
618         cell method_cache_hashcode(cell klass, array *array);
619         void update_method_cache(cell cache, cell klass, cell method);
620         inline void vmprim_mega_cache_miss();
621         inline void vmprim_reset_dispatch_stats();
622         inline void vmprim_dispatch_stats();
623
624         //inline cache
625         cell max_pic_size;
626         cell cold_call_to_ic_transitions;
627         cell ic_to_pic_transitions;
628         cell pic_to_mega_transitions;
629         cell pic_counts[4];  /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
630         void init_inline_caching(int max_size);
631         void deallocate_inline_cache(cell return_address);
632         cell determine_inline_cache_type(array *cache_entries);
633         void update_pic_count(cell type);
634         code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p);
635         void *megamorphic_call_stub(cell generic_word);
636         cell inline_cache_size(cell cache_entries);
637         cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
638         void update_pic_transitions(cell pic_size);
639         void *inline_cache_miss(cell return_address);
640         inline void vmprim_reset_inline_cache_stats();
641         inline void vmprim_inline_cache_stats();
642
643         //factor
644         void default_parameters(vm_parameters *p);
645         bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
646         void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv);
647         void do_stage1_init();
648         void init_factor(vm_parameters *p);
649         void pass_args_to_factor(int argc, vm_char **argv);
650         void start_factor(vm_parameters *p);
651         void start_embedded_factor(vm_parameters *p);
652         void start_standalone_factor(int argc, vm_char **argv);
653         char *factor_eval_string(char *string);
654         void factor_eval_free(char *result);
655         void factor_yield();
656         void factor_sleep(long us);
657
658         //utilities
659         void *safe_malloc(size_t size);
660         vm_char *safe_strdup(const vm_char *str);
661         void nl();
662         void print_string(const char *str);
663         void print_cell(cell x);
664         void print_cell_hex(cell x);
665         void print_cell_hex_pad(cell x);
666         void print_fixnum(fixnum x);
667         cell read_cell_hex();
668
669
670         // os-windows
671   #if defined(WINDOWS)
672         void init_ffi();
673         void ffi_dlopen(dll *dll);
674         void *ffi_dlsym(dll *dll, symbol_char *symbol);
675         void ffi_dlclose(dll *dll);
676         void sleep_micros(u64 usec);
677         long getpagesize();
678         void dealloc_segment(segment *block);
679         segment *alloc_segment(cell size);
680         const vm_char *vm_executable_path();
681         inline void vmprim_existsp();
682         const vm_char *default_image_path();
683         void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length);
684         bool windows_stat(vm_char *path);
685
686    #if defined(WINNT)
687         s64 current_micros();
688         void c_to_factor_toplevel(cell quot);
689         void open_console();
690         // next method here:
691    #endif
692   #endif
693
694 };
695
696 extern factorvm *vm;
697
698 }