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