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