]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
removed some error vm-> functions
[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(unsigned int fpu_status, 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 *native_stack);
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         void init_data_gc();
225         object *copy_untagged_object_impl(object *pointer, cell size);
226         object *copy_object_impl(object *untagged);
227         bool should_copy_p(object *untagged);
228         object *resolve_forwarding(object *untagged);
229         template <typename T> T *copy_untagged_object(T *untagged);
230         cell copy_object(cell pointer);
231         void copy_handle(cell *handle);
232         void copy_card(card *ptr, cell gen, cell here);
233         void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask);
234         void copy_gen_cards(cell gen);
235         void copy_cards();
236         void copy_stack_elements(segment *region, cell top);
237         void copy_registered_locals();
238         void copy_registered_bignums();
239         void copy_roots();
240         cell copy_next_from_nursery(cell scan);
241         cell copy_next_from_aging(cell scan);
242         cell copy_next_from_tenured(cell scan);
243         void copy_reachable_objects(cell scan, cell *end);
244         void begin_gc(cell requested_bytes);
245         void end_gc(cell gc_elapsed);
246         void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes);
247         void gc();
248         inline void vmprim_gc();
249         inline void vmprim_gc_stats();
250         void clear_gc_stats();
251         inline void vmprim_become();
252         void inline_gc(cell *gc_roots_base, cell gc_roots_size);
253         inline bool collecting_accumulation_gen_p();
254         inline object *allot_zone(zone *z, cell a);
255         inline object *allot_object(header header, cell size);
256         template <typename TYPE> TYPE *allot(cell size);
257         inline void check_data_pointer(object *pointer);
258         inline void check_tagged_pointer(cell tagged);
259         inline void vmprim_clear_gc_stats();
260
261         // local roots
262         /* If a runtime function needs to call another function which potentially
263            allocates memory, it must wrap any local variable references to Factor
264            objects in gc_root instances */
265         std::vector<cell> gc_locals;
266         std::vector<cell> gc_bignums;
267
268         // generic arrays
269         template <typename T> T *allot_array_internal(cell capacity);
270         template <typename T> bool reallot_array_in_place_p(T *array, cell capacity);
271         template <typename TYPE> TYPE *reallot_array(TYPE *array_, cell capacity);
272
273         //debug
274         bool fep_disabled;
275         bool full_output;
276         cell look_for;
277         cell obj;
278         void print_chars(string* str);
279         void print_word(word* word, cell nesting);
280         void print_factor_string(string* str);
281         void print_array(array* array, cell nesting);
282         void print_tuple(tuple *tuple, cell nesting);
283         void print_nested_obj(cell obj, fixnum nesting);
284         void print_obj(cell obj);
285         void print_objects(cell *start, cell *end);
286         void print_datastack();
287         void print_retainstack();
288         void print_stack_frame(stack_frame *frame);
289         void print_callstack();
290         void dump_cell(cell x);
291         void dump_memory(cell from, cell to);
292         void dump_zone(zone *z);
293         void dump_generations();
294         void dump_objects(cell type);
295         void find_data_references_step(cell *scan);
296         void find_data_references(cell look_for_);
297         void dump_code_heap();
298         void factorbug();
299         inline void vmprim_die();
300
301         //arrays
302         array *allot_array(cell capacity, cell fill_);
303         inline void vmprim_array();
304         cell allot_array_1(cell obj_);
305         cell allot_array_2(cell v1_, cell v2_);
306         cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
307         inline void vmprim_resize_array();
308         inline void set_array_nth(array *array, cell slot, cell value);
309
310         //strings
311         cell string_nth(string* str, cell index);
312         void set_string_nth_fast(string *str, cell index, cell ch);
313         void set_string_nth_slow(string *str_, cell index, cell ch);
314         void set_string_nth(string *str, cell index, cell ch);
315         string *allot_string_internal(cell capacity);
316         void fill_string(string *str_, cell start, cell capacity, cell fill);
317         string *allot_string(cell capacity, cell fill);
318         inline void vmprim_string();
319         bool reallot_string_in_place_p(string *str, cell capacity);
320         string* reallot_string(string *str_, cell capacity);
321         inline void vmprim_resize_string();
322         inline void vmprim_string_nth();
323         inline void vmprim_set_string_nth_fast();
324         inline void vmprim_set_string_nth_slow();
325
326         //booleans
327         void box_boolean(bool value);
328         bool to_boolean(cell value);
329         inline cell tag_boolean(cell untagged);
330
331         //byte arrays
332         byte_array *allot_byte_array(cell size);
333         inline void vmprim_byte_array();
334         inline void vmprim_uninitialized_byte_array();
335         inline void vmprim_resize_byte_array();
336
337         //tuples
338         tuple *allot_tuple(cell layout_);
339         inline void vmprim_tuple();
340         inline void vmprim_tuple_boa();
341
342         //words
343         word *allot_word(cell vocab_, cell name_);
344         inline void vmprim_word();
345         inline void vmprim_word_xt();
346         void update_word_xt(cell w_);
347         inline void vmprim_optimized_p();
348         inline void vmprim_wrapper();
349
350         //math
351         cell bignum_zero;
352         cell bignum_pos_one;
353         cell bignum_neg_one;    
354         inline void vmprim_bignum_to_fixnum();
355         inline void vmprim_float_to_fixnum();
356         inline void vmprim_fixnum_divint();
357         inline void vmprim_fixnum_divmod();
358         bignum *fixnum_to_bignum(fixnum);
359         bignum *cell_to_bignum(cell);
360         bignum *long_long_to_bignum(s64 n);
361         bignum *ulong_long_to_bignum(u64 n);
362         inline fixnum sign_mask(fixnum x);
363         inline fixnum branchless_max(fixnum x, fixnum y);
364         inline fixnum branchless_abs(fixnum x);
365         inline void vmprim_fixnum_shift();
366         inline void vmprim_fixnum_to_bignum();
367         inline void vmprim_float_to_bignum();
368         inline void vmprim_bignum_eq();
369         inline void vmprim_bignum_add();
370         inline void vmprim_bignum_subtract();
371         inline void vmprim_bignum_multiply();
372         inline void vmprim_bignum_divint();
373         inline void vmprim_bignum_divmod();
374         inline void vmprim_bignum_mod();
375         inline void vmprim_bignum_and();
376         inline void vmprim_bignum_or();
377         inline void vmprim_bignum_xor();
378         inline void vmprim_bignum_shift();
379         inline void vmprim_bignum_less();
380         inline void vmprim_bignum_lesseq();
381         inline void vmprim_bignum_greater();
382         inline void vmprim_bignum_greatereq();
383         inline void vmprim_bignum_not();
384         inline void vmprim_bignum_bitp();
385         inline void vmprim_bignum_log2();
386         unsigned int bignum_producer(unsigned int digit);
387         inline void vmprim_byte_array_to_bignum();
388         cell unbox_array_size();
389         inline void vmprim_fixnum_to_float();
390         inline void vmprim_bignum_to_float();
391         inline void vmprim_str_to_float();
392         inline void vmprim_float_to_str();
393         inline void vmprim_float_eq();
394         inline void vmprim_float_add();
395         inline void vmprim_float_subtract();
396         inline void vmprim_float_multiply();
397         inline void vmprim_float_divfloat();
398         inline void vmprim_float_mod();
399         inline void vmprim_float_less();
400         inline void vmprim_float_lesseq();
401         inline void vmprim_float_greater();
402         inline void vmprim_float_greatereq();
403         inline void vmprim_float_bits();
404         inline void vmprim_bits_float();
405         inline void vmprim_double_bits();
406         inline void vmprim_bits_double();
407         fixnum to_fixnum(cell tagged);
408         cell to_cell(cell tagged);
409         void box_signed_1(s8 n);
410         void box_unsigned_1(u8 n);
411         void box_signed_2(s16 n);
412         void box_unsigned_2(u16 n);
413         void box_signed_4(s32 n);
414         void box_unsigned_4(u32 n);
415         void box_signed_cell(fixnum integer);
416         void box_unsigned_cell(cell cell);
417         void box_signed_8(s64 n);
418         s64 to_signed_8(cell obj);
419         void box_unsigned_8(u64 n);
420         u64 to_unsigned_8(cell obj);
421         void box_float(float flo);
422         float to_float(cell value);
423         void box_double(double flo);
424         double to_double(cell value);
425         void overflow_fixnum_add(fixnum x, fixnum y);
426         void overflow_fixnum_subtract(fixnum x, fixnum y);
427         void overflow_fixnum_multiply(fixnum x, fixnum y);
428         inline cell allot_integer(fixnum x);
429         inline cell allot_cell(cell x);
430         inline cell allot_float(double n);
431         inline bignum *float_to_bignum(cell tagged);
432         inline double bignum_to_float(cell tagged);
433         inline double untag_float(cell tagged);
434         inline double untag_float_check(cell tagged);
435         inline fixnum float_to_fixnum(cell tagged);
436         inline double fixnum_to_float(cell tagged);
437         template <typename T> T *untag_check(cell value);
438         template <typename T> T *untag(cell value);
439         
440         //io
441         void init_c_io();
442         void io_error();
443         inline void vmprim_fopen();
444         inline void vmprim_fgetc();
445         inline void vmprim_fread();
446         inline void vmprim_fputc();
447         inline void vmprim_fwrite();
448         inline void vmprim_fseek();
449         inline void vmprim_fflush();
450         inline void vmprim_fclose();
451
452         //code_gc
453         void clear_free_list(heap *heap);
454         void new_heap(heap *heap, cell size);
455         void add_to_free_list(heap *heap, free_heap_block *block);
456         void build_free_list(heap *heap, cell size);
457         void assert_free_block(free_heap_block *block);
458         free_heap_block *find_free_block(heap *heap, cell size);
459         free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size);
460         heap_block *heap_allot(heap *heap, cell size);
461         void heap_free(heap *heap, heap_block *block);
462         void mark_block(heap_block *block);
463         void unmark_marked(heap *heap);
464         void free_unmarked(heap *heap, heap_iterator iter);
465         void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free);
466         cell heap_size(heap *heap);
467         cell compute_heap_forwarding(heap *heap, unordered_map<heap_block *,char *> &forwarding);
468         void compact_heap(heap *heap, unordered_map<heap_block *,char *> &forwarding);
469
470         //code_block
471         relocation_type relocation_type_of(relocation_entry r);
472         relocation_class relocation_class_of(relocation_entry r);
473         cell relocation_offset_of(relocation_entry r);
474         void flush_icache_for(code_block *block);
475         int number_of_parameters(relocation_type type);
476         void *object_xt(cell obj);
477         void *xt_pic(word *w, cell tagged_quot);
478         void *word_xt_pic(word *w);
479         void *word_xt_pic_tail(word *w);
480         void undefined_symbol();
481         void *get_rel_symbol(array *literals, cell index);
482         cell compute_relocation(relocation_entry rel, cell index, code_block *compiled);
483         void iterate_relocations(code_block *compiled, relocation_iterator iter);
484         void store_address_2_2(cell *ptr, cell value);
485         void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift);
486         void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value);
487         void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled);
488         void update_literal_references(code_block *compiled);
489         void copy_literal_references(code_block *compiled);
490         void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled);
491         void update_word_references_step(relocation_entry rel, cell index, code_block *compiled);
492         void update_word_references(code_block *compiled);
493         void update_literal_and_word_references(code_block *compiled);
494         void check_code_address(cell address);
495         void mark_code_block(code_block *compiled);
496         void mark_stack_frame_step(stack_frame *frame);
497         void mark_active_blocks(context *stacks);
498         void mark_object_code_block(object *object);
499         void relocate_code_block(code_block *compiled);
500         void fixup_labels(array *labels, code_block *compiled);
501         code_block *allot_code_block(cell size);
502         code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_);
503         inline bool stack_traces_p()
504         {
505                 return userenv[STACK_TRACES_ENV] != F;
506         }
507
508         //code_heap
509         heap code;
510         unordered_map<heap_block *,char *> forwarding;
511         void init_code_heap(cell size);
512         bool in_code_heap_p(cell ptr);
513         void jit_compile_word(cell word_, cell def_, bool relocate);
514         void iterate_code_heap(code_heap_iterator iter);
515         void copy_code_heap_roots();
516         void update_code_heap_words();
517         inline void vmprim_modify_code_heap();
518         inline void vmprim_code_room();
519         code_block *forward_xt(code_block *compiled);
520         void forward_frame_xt(stack_frame *frame);
521         void forward_object_xts();
522         void fixup_object_xts();
523         void compact_code_heap();
524         inline void check_code_pointer(cell ptr);
525
526
527         //image
528         cell code_relocation_base;
529         cell data_relocation_base;
530         void init_objects(image_header *h);
531         void load_data_heap(FILE *file, image_header *h, vm_parameters *p);
532         void load_code_heap(FILE *file, image_header *h, vm_parameters *p);
533         bool save_image(const vm_char *filename);
534         inline void vmprim_save_image();
535         inline void vmprim_save_image_and_exit();
536         void data_fixup(cell *cell);
537         template <typename T> void code_fixup(T **handle);
538         void fixup_word(word *word);
539         void fixup_quotation(quotation *quot);
540         void fixup_alien(alien *d);
541         void fixup_stack_frame(stack_frame *frame);
542         void fixup_callstack_object(callstack *stack);
543         void relocate_object(object *object);
544         void relocate_data();
545         void fixup_code_block(code_block *compiled);
546         void relocate_code();
547         void load_image(vm_parameters *p);
548
549         //callstack
550         template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator);
551         void check_frame(stack_frame *frame);
552         callstack *allot_callstack(cell size);
553         stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom);
554         stack_frame *capture_start();
555         inline void vmprim_callstack();
556         inline void vmprim_set_callstack();
557         code_block *frame_code(stack_frame *frame);
558         cell frame_type(stack_frame *frame);
559         cell frame_executing(stack_frame *frame);
560         stack_frame *frame_successor(stack_frame *frame);
561         cell frame_scan(stack_frame *frame);
562         inline void vmprim_callstack_to_array();
563         stack_frame *innermost_stack_frame(callstack *stack);
564         stack_frame *innermost_stack_frame_quot(callstack *callstack);
565         inline void vmprim_innermost_stack_frame_executing();
566         inline void vmprim_innermost_stack_frame_scan();
567         inline void vmprim_set_innermost_stack_frame_quot();
568         void save_callstack_bottom(stack_frame *callstack_bottom);
569         template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator);
570         inline void do_slots(cell obj, void (* iter)(cell *,factorvm*));
571
572
573         //alien
574         char *pinned_alien_offset(cell obj);
575         cell allot_alien(cell delegate_, cell displacement);
576         inline void vmprim_displaced_alien();
577         inline void vmprim_alien_address();
578         void *alien_pointer();
579         inline void vmprim_dlopen();
580         inline void vmprim_dlsym();
581         inline void vmprim_dlclose();
582         inline void vmprim_dll_validp();
583         char *alien_offset(cell obj);
584         char *unbox_alien();
585         void box_alien(void *ptr);
586         void to_value_struct(cell src, void *dest, cell size);
587         void box_value_struct(void *src, cell size);
588         void box_small_struct(cell x, cell y, cell size);
589         void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size);
590
591         //quotations
592         inline void vmprim_jit_compile();
593         inline void vmprim_array_to_quotation();
594         inline void vmprim_quotation_xt();
595         void set_quot_xt(quotation *quot, code_block *code);
596         void jit_compile(cell quot_, bool relocating);
597         void compile_all_words();
598         fixnum quot_code_offset_to_scan(cell quot_, cell offset);
599         cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);
600         inline void vmprim_quot_compiled_p();
601
602         //dispatch
603         cell megamorphic_cache_hits;
604         cell megamorphic_cache_misses;
605         cell search_lookup_alist(cell table, cell klass);
606         cell search_lookup_hash(cell table, cell klass, cell hashcode);
607         cell nth_superclass(tuple_layout *layout, fixnum echelon);
608         cell nth_hashcode(tuple_layout *layout, fixnum echelon);
609         cell lookup_tuple_method(cell obj, cell methods);
610         cell lookup_hi_tag_method(cell obj, cell methods);
611         cell lookup_hairy_method(cell obj, cell methods);
612         cell lookup_method(cell obj, cell methods);
613         inline void vmprim_lookup_method();
614         cell object_class(cell obj);
615         cell method_cache_hashcode(cell klass, array *array);
616         void update_method_cache(cell cache, cell klass, cell method);
617         inline void vmprim_mega_cache_miss();
618         inline void vmprim_reset_dispatch_stats();
619         inline void vmprim_dispatch_stats();
620
621         //inline cache
622         cell max_pic_size;
623         cell cold_call_to_ic_transitions;
624         cell ic_to_pic_transitions;
625         cell pic_to_mega_transitions;
626         cell pic_counts[4];  /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
627         void init_inline_caching(int max_size);
628         void deallocate_inline_cache(cell return_address);
629         cell determine_inline_cache_type(array *cache_entries);
630         void update_pic_count(cell type);
631         code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p);
632         void *megamorphic_call_stub(cell generic_word);
633         cell inline_cache_size(cell cache_entries);
634         cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
635         void update_pic_transitions(cell pic_size);
636         void *inline_cache_miss(cell return_address);
637         inline void vmprim_reset_inline_cache_stats();
638         inline void vmprim_inline_cache_stats();
639
640         //factor
641         void default_parameters(vm_parameters *p);
642         bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
643         void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv);
644         void do_stage1_init();
645         void init_factor(vm_parameters *p);
646         void pass_args_to_factor(int argc, vm_char **argv);
647         void start_factor(vm_parameters *p);
648         void start_embedded_factor(vm_parameters *p);
649         void start_standalone_factor(int argc, vm_char **argv);
650         char *factor_eval_string(char *string);
651         void factor_eval_free(char *result);
652         void factor_yield();
653         void factor_sleep(long us);
654
655         //utilities
656         void *safe_malloc(size_t size);
657         vm_char *safe_strdup(const vm_char *str);
658         void nl();
659         void print_string(const char *str);
660         void print_cell(cell x);
661         void print_cell_hex(cell x);
662         void print_cell_hex_pad(cell x);
663         void print_fixnum(fixnum x);
664         cell read_cell_hex();
665
666
667         // os-windows
668   #if defined(WINDOWS)
669         void init_ffi();
670         void ffi_dlopen(dll *dll);
671         void *ffi_dlsym(dll *dll, symbol_char *symbol);
672         void ffi_dlclose(dll *dll);
673         void sleep_micros(u64 usec);
674         long getpagesize();
675         void dealloc_segment(segment *block);
676         segment *alloc_segment(cell size);
677         const vm_char *vm_executable_path();
678         inline void vmprim_existsp();
679         const vm_char *default_image_path();
680         void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length);
681         bool windows_stat(vm_char *path);
682
683    #if defined(WINNT)
684         s64 current_micros();
685         void c_to_factor_toplevel(cell quot);
686         void open_console();
687         // next method here:
688    #endif
689   #endif
690
691 };
692
693 extern factorvm *vm;
694
695 }