]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
moved local_roots and write_barrier stuff out of inlineimpls.hpp
[factor.git] / vm / vm.hpp
1 namespace factor
2 {
3
4 struct factor_vm 
5 {
6         // First five fields accessed directly by assembler. See vm.factor
7         context *stack_chain; 
8         zone nursery; /* new objects are allocated here */
9         cell cards_offset;
10         cell decks_offset;
11         cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */
12
13         // contexts
14         cell ds_size, rs_size;
15         context *unused_contexts;
16
17         void reset_datastack();
18         void reset_retainstack();
19         void fix_stacks();
20         void save_stacks();
21         context *alloc_context();
22         void dealloc_context(context *old_context);
23         void nest_stacks();
24         void unnest_stacks();
25         void init_stacks(cell ds_size_, cell rs_size_);
26         bool stack_to_array(cell bottom, cell top);
27         cell array_to_stack(array *array, cell bottom);
28         void primitive_datastack();
29         void primitive_retainstack();
30         void primitive_set_datastack();
31         void primitive_set_retainstack();
32         void primitive_check_datastack();
33
34         // run
35         cell T;  /* Canonical T object. It's just a word */
36
37         void primitive_getenv();
38         void primitive_setenv();
39         void primitive_exit();
40         void primitive_micros();
41         void primitive_sleep();
42         void primitive_set_slot();
43         void primitive_load_locals();
44         cell clone_object(cell obj_);
45         void primitive_clone();
46
47         // profiler
48         bool profiling_p;
49
50         void init_profiler();
51         code_block *compile_profiling_stub(cell word_);
52         void set_profiling(bool profiling);
53         void primitive_profiling();
54
55         // errors
56         /* Global variables used to pass fault handler state from signal handler to
57            user-space */
58         cell signal_number;
59         cell signal_fault_addr;
60         unsigned int signal_fpu_status;
61         stack_frame *signal_callstack_top;
62
63         void out_of_memory();
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         void primitive_call_clear();
73         void primitive_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         // bignum
81         int bignum_equal_p(bignum * x, bignum * y);
82         enum bignum_comparison bignum_compare(bignum * x, bignum * y);
83         bignum *bignum_add(bignum * x, bignum * y);
84         bignum *bignum_subtract(bignum * x, bignum * y);
85         bignum *bignum_multiply(bignum * x, bignum * y);
86         void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder);
87         bignum *bignum_quotient(bignum * numerator, bignum * denominator);
88         bignum *bignum_remainder(bignum * numerator, bignum * denominator);
89         cell bignum_to_cell(bignum * bignum);
90         fixnum bignum_to_fixnum(bignum * bignum);
91         s64 bignum_to_long_long(bignum * bignum);
92         u64 bignum_to_ulong_long(bignum * bignum);
93         double bignum_to_double(bignum * bignum);
94         bignum *double_to_bignum(double x);
95         int bignum_equal_p_unsigned(bignum * x, bignum * y);
96         enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y);
97         bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p);
98         bignum *bignum_subtract_unsigned(bignum * x, bignum * y);
99         bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p);
100         bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p);
101         void bignum_destructive_add(bignum * bignum, bignum_digit_type n);
102         void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor);
103         void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, 
104                                                                                                   bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p);
105         void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q);
106         bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, 
107                                                                                          bignum_digit_type guess, bignum_digit_type * u_start);
108         void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, 
109                                                                                                    bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p);
110         void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left);
111         void bignum_destructive_unnormalization(bignum * bignum, int shift_right);
112         bignum_digit_type bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, 
113                                                                                   bignum_digit_type v, bignum_digit_type * q) /* return value */;
114         bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, 
115                                                                                                    bignum_digit_type guess, bignum_digit_type * u);
116         void bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, 
117                                                                                                   bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p);
118         bignum_digit_type bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator);
119         bignum * bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p);
120         bignum *bignum_digit_to_bignum(bignum_digit_type digit, int negative_p);
121         bignum *allot_bignum(bignum_length_type length, int negative_p);
122         bignum * allot_bignum_zeroed(bignum_length_type length, int negative_p);
123         bignum *bignum_shorten_length(bignum * bignum, bignum_length_type length);
124         bignum *bignum_trim(bignum * bignum);
125         bignum *bignum_new_sign(bignum * x, int negative_p);
126         bignum *bignum_maybe_new_sign(bignum * x, int negative_p);
127         void bignum_destructive_copy(bignum * source, bignum * target);
128         bignum *bignum_bitwise_not(bignum * x);
129         bignum *bignum_arithmetic_shift(bignum * arg1, fixnum n);
130         bignum *bignum_bitwise_and(bignum * arg1, bignum * arg2);
131         bignum *bignum_bitwise_ior(bignum * arg1, bignum * arg2);
132         bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2);
133         bignum *bignum_magnitude_ash(bignum * arg1, fixnum n);
134         bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2);
135         bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2);
136         bignum *bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2);
137         void bignum_negate_magnitude(bignum * arg);
138         bignum *bignum_integer_length(bignum * x);
139         int bignum_logbitp(int shift, bignum * arg);
140         int bignum_unsigned_logbitp(int shift, bignum * bignum);
141         bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factor_vm *), unsigned int radix, int negative_p);
142
143         //data_heap
144         bool secure_gc;  /* Set by the -securegc command line argument */
145         bool gc_off; /* GC is off during heap walking */
146         data_heap *data;
147         /* A heap walk allows useful things to be done, like finding all
148            references to an object for debugging purposes. */
149         cell heap_scan_ptr;
150
151         void init_card_decks();
152         data_heap *grow_data_heap(data_heap *data, cell requested_bytes);
153         void clear_cards(cell from, cell to);
154         void clear_decks(cell from, cell to);
155         void clear_allot_markers(cell from, cell to);
156         void reset_generation(cell i);
157         void reset_generations(cell from, cell to);
158         void set_data_heap(data_heap *data_);
159         void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_);
160         cell untagged_object_size(object *pointer);
161         cell unaligned_object_size(object *pointer);
162         void primitive_size();
163         cell binary_payload_start(object *pointer);
164         void primitive_data_room();
165         void begin_scan();
166         void end_scan();
167         void primitive_begin_scan();
168         cell next_object();
169         void primitive_next_object();
170         void primitive_end_scan();
171         template<typename T> void each_object(T &functor);
172         cell find_all_words();
173         cell object_size(cell tagged);
174
175         
176         //write barrier
177         cell allot_markers_offset;
178
179         inline card *addr_to_card(cell a)
180         {
181                 return (card*)(((cell)(a) >> card_bits) + cards_offset);
182         }
183
184         inline cell card_to_addr(card *c)
185         {
186                 return ((cell)c - cards_offset) << card_bits;
187         }
188         
189         inline cell card_offset(card *c)
190         {
191                 return *(c - (cell)data->cards + (cell)data->allot_markers);
192         }
193         
194         inline card_deck *addr_to_deck(cell a)
195         {
196                 return (card_deck *)(((cell)a >> deck_bits) + decks_offset);
197         }
198         
199         inline cell deck_to_addr(card_deck *c)
200         {
201                 return ((cell)c - decks_offset) << deck_bits;
202         }
203         
204         inline card *deck_to_card(card_deck *d)
205         {
206                 return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset);
207         }
208         
209         inline card *addr_to_allot_marker(object *a)
210         {
211                 return (card *)(((cell)a >> card_bits) + allot_markers_offset);
212         }
213
214         /* the write barrier must be called any time we are potentially storing a
215            pointer from an older generation to a younger one */
216         inline void write_barrier(object *obj)
217         {
218                 *addr_to_card((cell)obj) = card_mark_mask;
219                 *addr_to_deck((cell)obj) = card_mark_mask;
220         }
221
222         /* we need to remember the first object allocated in the card */
223         inline void allot_barrier(object *address)
224         {
225                 card *ptr = addr_to_allot_marker(address);
226                 if(*ptr == invalid_allot_marker)
227                         *ptr = ((cell)address & addr_card_mask);
228         }
229
230         // data_gc
231         /* used during garbage collection only */
232         zone *newspace;
233         bool performing_gc;
234         bool performing_compaction;
235         cell collecting_gen;
236         /* if true, we are collecting aging space for the second time, so if it is still
237            full, we go on to collect tenured */
238         bool collecting_aging_again;
239         /* in case a generation fills up in the middle of a gc, we jump back
240            up to try collecting the next generation. */
241         jmp_buf gc_jmp;
242         gc_stats stats[max_gen_count];
243         u64 cards_scanned;
244         u64 decks_scanned;
245         u64 card_scan_time;
246         cell code_heap_scans;
247         /* What generation was being collected when copy_code_heap_roots() was last
248            called? Until the next call to add_code_block(), future
249            collections of younger generations don't have to touch the code
250            heap. */
251         cell last_code_heap_scan;
252         /* sometimes we grow the heap */
253         bool growing_data_heap;
254         data_heap *old_data_heap;
255
256         void init_data_gc();
257         object *copy_untagged_object_impl(object *pointer, cell size);
258         object *copy_object_impl(object *untagged);
259         bool should_copy_p(object *untagged);
260         object *resolve_forwarding(object *untagged);
261         template <typename T> T *copy_untagged_object(T *untagged);
262         cell copy_object(cell pointer);
263         void copy_handle(cell *handle);
264         void copy_card(card *ptr, cell gen, cell here);
265         void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask);
266         void copy_gen_cards(cell gen);
267         void copy_cards();
268         void copy_stack_elements(segment *region, cell top);
269         void copy_registered_locals();
270         void copy_registered_bignums();
271         void copy_roots();
272         cell copy_next_from_nursery(cell scan);
273         cell copy_next_from_aging(cell scan);
274         cell copy_next_from_tenured(cell scan);
275         void copy_reachable_objects(cell scan, cell *end);
276         void begin_gc(cell requested_bytes);
277         void end_gc(cell gc_elapsed);
278         void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes);
279         void gc();
280         void primitive_gc();
281         void primitive_gc_stats();
282         void clear_gc_stats();
283         void primitive_become();
284         void inline_gc(cell *gc_roots_base, cell gc_roots_size);
285         inline bool collecting_accumulation_gen_p();
286         inline object *allot_zone(zone *z, cell a);
287         inline object *allot_object(header header, cell size);
288         template <typename TYPE> TYPE *allot(cell size);
289         inline void check_data_pointer(object *pointer);
290         inline void check_tagged_pointer(cell tagged);
291         void primitive_clear_gc_stats();
292
293         // local roots
294         /* If a runtime function needs to call another function which potentially
295            allocates memory, it must wrap any local variable references to Factor
296            objects in gc_root instances */
297         std::vector<cell> gc_locals;
298         std::vector<cell> gc_bignums;
299
300         // generic arrays
301         template <typename T> T *allot_array_internal(cell capacity);
302         template <typename T> bool reallot_array_in_place_p(T *array, cell capacity);
303         template <typename TYPE> TYPE *reallot_array(TYPE *array_, cell capacity);
304
305         //debug
306         bool fep_disabled;
307         bool full_output;
308         cell look_for;
309         cell obj;
310
311         void print_chars(string* str);
312         void print_word(word* word, cell nesting);
313         void print_factor_string(string* str);
314         void print_array(array* array, cell nesting);
315         void print_tuple(tuple *tuple, cell nesting);
316         void print_nested_obj(cell obj, fixnum nesting);
317         void print_obj(cell obj);
318         void print_objects(cell *start, cell *end);
319         void print_datastack();
320         void print_retainstack();
321         void print_stack_frame(stack_frame *frame);
322         void print_callstack();
323         void dump_cell(cell x);
324         void dump_memory(cell from, cell to);
325         void dump_zone(zone *z);
326         void dump_generations();
327         void dump_objects(cell type);
328         void find_data_references_step(cell *scan);
329         void find_data_references(cell look_for_);
330         void dump_code_heap();
331         void factorbug();
332         void primitive_die();
333
334         //arrays
335         array *allot_array(cell capacity, cell fill_);
336         void primitive_array();
337         cell allot_array_1(cell obj_);
338         cell allot_array_2(cell v1_, cell v2_);
339         cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
340         void primitive_resize_array();
341         inline void set_array_nth(array *array, cell slot, cell value);
342
343         //strings
344         cell string_nth(string* str, cell index);
345         void set_string_nth_fast(string *str, cell index, cell ch);
346         void set_string_nth_slow(string *str_, cell index, cell ch);
347         void set_string_nth(string *str, cell index, cell ch);
348         string *allot_string_internal(cell capacity);
349         void fill_string(string *str_, cell start, cell capacity, cell fill);
350         string *allot_string(cell capacity, cell fill);
351         void primitive_string();
352         bool reallot_string_in_place_p(string *str, cell capacity);
353         string* reallot_string(string *str_, cell capacity);
354         void primitive_resize_string();
355         void primitive_string_nth();
356         void primitive_set_string_nth_fast();
357         void primitive_set_string_nth_slow();
358
359         //booleans
360         void box_boolean(bool value);
361         bool to_boolean(cell value);
362         inline cell tag_boolean(cell untagged);
363
364         //byte arrays
365         byte_array *allot_byte_array(cell size);
366         void primitive_byte_array();
367         void primitive_uninitialized_byte_array();
368         void primitive_resize_byte_array();
369
370         //tuples
371         tuple *allot_tuple(cell layout_);
372         void primitive_tuple();
373         void primitive_tuple_boa();
374
375         //words
376         word *allot_word(cell name_, cell vocab_, cell hashcode_);
377         void primitive_word();
378         void primitive_word_xt();
379         void update_word_xt(cell w_);
380         void primitive_optimized_p();
381         void primitive_wrapper();
382
383         //math
384         cell bignum_zero;
385         cell bignum_pos_one;
386         cell bignum_neg_one;    
387
388         void primitive_bignum_to_fixnum();
389         void primitive_float_to_fixnum();
390         void primitive_fixnum_divint();
391         void primitive_fixnum_divmod();
392         bignum *fixnum_to_bignum(fixnum);
393         bignum *cell_to_bignum(cell);
394         bignum *long_long_to_bignum(s64 n);
395         bignum *ulong_long_to_bignum(u64 n);
396         inline fixnum sign_mask(fixnum x);
397         inline fixnum branchless_max(fixnum x, fixnum y);
398         inline fixnum branchless_abs(fixnum x);
399         void primitive_fixnum_shift();
400         void primitive_fixnum_to_bignum();
401         void primitive_float_to_bignum();
402         void primitive_bignum_eq();
403         void primitive_bignum_add();
404         void primitive_bignum_subtract();
405         void primitive_bignum_multiply();
406         void primitive_bignum_divint();
407         void primitive_bignum_divmod();
408         void primitive_bignum_mod();
409         void primitive_bignum_and();
410         void primitive_bignum_or();
411         void primitive_bignum_xor();
412         void primitive_bignum_shift();
413         void primitive_bignum_less();
414         void primitive_bignum_lesseq();
415         void primitive_bignum_greater();
416         void primitive_bignum_greatereq();
417         void primitive_bignum_not();
418         void primitive_bignum_bitp();
419         void primitive_bignum_log2();
420         unsigned int bignum_producer(unsigned int digit);
421         void primitive_byte_array_to_bignum();
422         cell unbox_array_size();
423         void primitive_fixnum_to_float();
424         void primitive_bignum_to_float();
425         void primitive_str_to_float();
426         void primitive_float_to_str();
427         void primitive_float_eq();
428         void primitive_float_add();
429         void primitive_float_subtract();
430         void primitive_float_multiply();
431         void primitive_float_divfloat();
432         void primitive_float_mod();
433         void primitive_float_less();
434         void primitive_float_lesseq();
435         void primitive_float_greater();
436         void primitive_float_greatereq();
437         void primitive_float_bits();
438         void primitive_bits_float();
439         void primitive_double_bits();
440         void primitive_bits_double();
441         fixnum to_fixnum(cell tagged);
442         cell to_cell(cell tagged);
443         void box_signed_1(s8 n);
444         void box_unsigned_1(u8 n);
445         void box_signed_2(s16 n);
446         void box_unsigned_2(u16 n);
447         void box_signed_4(s32 n);
448         void box_unsigned_4(u32 n);
449         void box_signed_cell(fixnum integer);
450         void box_unsigned_cell(cell cell);
451         void box_signed_8(s64 n);
452         s64 to_signed_8(cell obj);
453         void box_unsigned_8(u64 n);
454         u64 to_unsigned_8(cell obj);
455         void box_float(float flo);
456         float to_float(cell value);
457         void box_double(double flo);
458         double to_double(cell value);
459         inline void overflow_fixnum_add(fixnum x, fixnum y);
460         inline void overflow_fixnum_subtract(fixnum x, fixnum y);
461         inline void overflow_fixnum_multiply(fixnum x, fixnum y);
462         inline cell allot_integer(fixnum x);
463         inline cell allot_cell(cell x);
464         inline cell allot_float(double n);
465         inline bignum *float_to_bignum(cell tagged);
466         inline double bignum_to_float(cell tagged);
467         inline double untag_float(cell tagged);
468         inline double untag_float_check(cell tagged);
469         inline fixnum float_to_fixnum(cell tagged);
470         inline double fixnum_to_float(cell tagged);
471         template <typename T> T *untag_check(cell value);
472         template <typename T> T *untag(cell value);
473         
474         //io
475         void init_c_io();
476         void io_error();
477         void primitive_fopen();
478         void primitive_fgetc();
479         void primitive_fread();
480         void primitive_fputc();
481         void primitive_fwrite();
482         void primitive_fseek();
483         void primitive_fflush();
484         void primitive_fclose();
485
486         //code_block
487         relocation_type relocation_type_of(relocation_entry r);
488         relocation_class relocation_class_of(relocation_entry r);
489         cell relocation_offset_of(relocation_entry r);
490         void flush_icache_for(code_block *block);
491         int number_of_parameters(relocation_type type);
492         void *object_xt(cell obj);
493         void *xt_pic(word *w, cell tagged_quot);
494         void *word_xt_pic(word *w);
495         void *word_xt_pic_tail(word *w);
496         void undefined_symbol();
497         void *get_rel_symbol(array *literals, cell index);
498         cell compute_relocation(relocation_entry rel, cell index, code_block *compiled);
499         void iterate_relocations(code_block *compiled, relocation_iterator iter);
500         void store_address_2_2(cell *ptr, cell value);
501         void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift);
502         void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value);
503         void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled);
504         void update_literal_references(code_block *compiled);
505         void copy_literal_references(code_block *compiled);
506         void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled);
507         void update_word_references_step(relocation_entry rel, cell index, code_block *compiled);
508         void update_word_references(code_block *compiled);
509         void update_literal_and_word_references(code_block *compiled);
510         void check_code_address(cell address);
511         void mark_code_block(code_block *compiled);
512         void mark_stack_frame_step(stack_frame *frame);
513         void mark_active_blocks(context *stacks);
514         void mark_object_code_block(object *object);
515         void relocate_code_block(code_block *compiled);
516         void fixup_labels(array *labels, code_block *compiled);
517         code_block *allot_code_block(cell size);
518         code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_);
519         inline bool stack_traces_p()
520         {
521                 return userenv[STACK_TRACES_ENV] != F;
522         }
523
524         //code_heap
525         heap *code;
526         unordered_map<heap_block *, char *> forwarding;
527
528         void init_code_heap(cell size);
529         bool in_code_heap_p(cell ptr);
530         void jit_compile_word(cell word_, cell def_, bool relocate);
531         void iterate_code_heap(code_heap_iterator iter);
532         void copy_code_heap_roots();
533         void update_code_heap_words();
534         void primitive_modify_code_heap();
535         void primitive_code_room();
536         code_block *forward_xt(code_block *compiled);
537         void forward_frame_xt(stack_frame *frame);
538         void forward_object_xts();
539         void fixup_object_xts();
540         void compact_code_heap();
541         inline void check_code_pointer(cell ptr);
542
543         //image
544         cell code_relocation_base;
545         cell data_relocation_base;
546
547         void init_objects(image_header *h);
548         void load_data_heap(FILE *file, image_header *h, vm_parameters *p);
549         void load_code_heap(FILE *file, image_header *h, vm_parameters *p);
550         bool save_image(const vm_char *filename);
551         void primitive_save_image();
552         void primitive_save_image_and_exit();
553         void data_fixup(cell *cell);
554         template <typename T> void code_fixup(T **handle);
555         void fixup_word(word *word);
556         void fixup_quotation(quotation *quot);
557         void fixup_alien(alien *d);
558         void fixup_stack_frame(stack_frame *frame);
559         void fixup_callstack_object(callstack *stack);
560         void relocate_object(object *object);
561         void relocate_data();
562         void fixup_code_block(code_block *compiled);
563         void relocate_code();
564         void load_image(vm_parameters *p);
565
566         //callstack
567         template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator);
568         void check_frame(stack_frame *frame);
569         callstack *allot_callstack(cell size);
570         stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom);
571         stack_frame *capture_start();
572         void primitive_callstack();
573         void primitive_set_callstack();
574         code_block *frame_code(stack_frame *frame);
575         cell frame_type(stack_frame *frame);
576         cell frame_executing(stack_frame *frame);
577         stack_frame *frame_successor(stack_frame *frame);
578         cell frame_scan(stack_frame *frame);
579         void primitive_callstack_to_array();
580         stack_frame *innermost_stack_frame(callstack *stack);
581         stack_frame *innermost_stack_frame_quot(callstack *callstack);
582         void primitive_innermost_stack_frame_executing();
583         void primitive_innermost_stack_frame_scan();
584         void primitive_set_innermost_stack_frame_quot();
585         void save_callstack_bottom(stack_frame *callstack_bottom);
586         template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator);
587         inline void do_slots(cell obj, void (* iter)(cell *,factor_vm*));
588
589         //alien
590         char *pinned_alien_offset(cell obj);
591         cell allot_alien(cell delegate_, cell displacement);
592         void primitive_displaced_alien();
593         void primitive_alien_address();
594         void *alien_pointer();
595         void primitive_dlopen();
596         void primitive_dlsym();
597         void primitive_dlclose();
598         void primitive_dll_validp();
599         void primitive_vm_ptr();
600         char *alien_offset(cell obj);
601         char *unbox_alien();
602         void box_alien(void *ptr);
603         void to_value_struct(cell src, void *dest, cell size);
604         void box_value_struct(void *src, cell size);
605         void box_small_struct(cell x, cell y, cell size);
606         void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size);
607
608         //quotations
609         void primitive_jit_compile();
610         void primitive_array_to_quotation();
611         void primitive_quotation_xt();
612         void set_quot_xt(quotation *quot, code_block *code);
613         void jit_compile(cell quot_, bool relocating);
614         void compile_all_words();
615         fixnum quot_code_offset_to_scan(cell quot_, cell offset);
616         cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);
617         void primitive_quot_compiled_p();
618
619         //dispatch
620         cell megamorphic_cache_hits;
621         cell megamorphic_cache_misses;
622
623         cell search_lookup_alist(cell table, cell klass);
624         cell search_lookup_hash(cell table, cell klass, cell hashcode);
625         cell nth_superclass(tuple_layout *layout, fixnum echelon);
626         cell nth_hashcode(tuple_layout *layout, fixnum echelon);
627         cell lookup_tuple_method(cell obj, cell methods);
628         cell lookup_hi_tag_method(cell obj, cell methods);
629         cell lookup_hairy_method(cell obj, cell methods);
630         cell lookup_method(cell obj, cell methods);
631         void primitive_lookup_method();
632         cell object_class(cell obj);
633         cell method_cache_hashcode(cell klass, array *array);
634         void update_method_cache(cell cache, cell klass, cell method);
635         void primitive_mega_cache_miss();
636         void primitive_reset_dispatch_stats();
637         void primitive_dispatch_stats();
638
639         //inline cache
640         cell max_pic_size;
641         cell cold_call_to_ic_transitions;
642         cell ic_to_pic_transitions;
643         cell pic_to_mega_transitions;
644         cell pic_counts[4];  /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
645
646         void init_inline_caching(int max_size);
647         void deallocate_inline_cache(cell return_address);
648         cell determine_inline_cache_type(array *cache_entries);
649         void update_pic_count(cell type);
650         code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p);
651         void *megamorphic_call_stub(cell generic_word);
652         cell inline_cache_size(cell cache_entries);
653         cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
654         void update_pic_transitions(cell pic_size);
655         void *inline_cache_miss(cell return_address);
656         void primitive_reset_inline_cache_stats();
657         void primitive_inline_cache_stats();
658
659         //factor
660         void default_parameters(vm_parameters *p);
661         bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
662         void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv);
663         void do_stage1_init();
664         void init_factor(vm_parameters *p);
665         void pass_args_to_factor(int argc, vm_char **argv);
666         void start_factor(vm_parameters *p);
667         void start_embedded_factor(vm_parameters *p);
668         void start_standalone_factor(int argc, vm_char **argv);
669         char *factor_eval_string(char *string);
670         void factor_eval_free(char *result);
671         void factor_yield();
672         void factor_sleep(long us);
673
674         // os-*
675         void primitive_existsp();
676         void init_ffi();
677         void ffi_dlopen(dll *dll);
678         void *ffi_dlsym(dll *dll, symbol_char *symbol);
679         void ffi_dlclose(dll *dll);
680         void c_to_factor_toplevel(cell quot);
681
682         // os-windows
683   #if defined(WINDOWS)
684         void sleep_micros(u64 usec);
685         const vm_char *vm_executable_path();
686         const vm_char *default_image_path();
687         void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length);
688         bool windows_stat(vm_char *path);
689         
690    #if defined(WINNT)
691         void open_console();
692         LONG exception_handler(PEXCEPTION_POINTERS pe);
693         // next method here:    
694    #endif
695   #else  // UNIX
696         void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap);
697         void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap);
698         void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap);
699         stack_frame *uap_stack_pointer(void *uap);
700
701   #endif
702
703   #ifdef __APPLE__
704         void call_fault_handler(exception_type_t exception, exception_data_type_t code, MACH_EXC_STATE_TYPE *exc_state, MACH_THREAD_STATE_TYPE *thread_state, MACH_FLOAT_STATE_TYPE *float_state);
705   #endif
706         
707         factor_vm() 
708                 : profiling_p(false),
709                   secure_gc(false),
710                   gc_off(false),
711                   performing_gc(false),
712                   performing_compaction(false),
713                   collecting_aging_again(false),
714                   growing_data_heap(false),
715                   fep_disabled(false),
716                   full_output(false),
717                   max_pic_size(0)
718         {
719                 memset(this,0,sizeof(this)); // just to make sure
720         }
721
722 };
723
724 #ifndef FACTOR_REENTRANT
725    #define FACTOR_SINGLE_THREADED_SINGLETON
726 #endif
727
728 #ifdef FACTOR_SINGLE_THREADED_SINGLETON
729 /* calls are dispatched using the singleton vm ptr */
730   extern factor_vm *vm;
731   #define PRIMITIVE_GETVM() vm
732   #define PRIMITIVE_OVERFLOW_GETVM() vm
733   #define VM_PTR vm
734   #define ASSERTVM() 
735   #define SIGNAL_VM_PTR() vm
736 #endif
737
738 #ifdef FACTOR_SINGLE_THREADED_TESTING
739 /* calls are dispatched as per multithreaded, but checked against singleton */
740   extern factor_vm *vm;
741   #define ASSERTVM() assert(vm==myvm)
742   #define PRIMITIVE_GETVM() ((factor_vm*)myvm)
743   #define PRIMITIVE_OVERFLOW_GETVM() ASSERTVM(); myvm
744   #define VM_PTR myvm
745   #define SIGNAL_VM_PTR() tls_vm()
746 #endif
747
748 #ifdef FACTOR_REENTRANT_TLS
749 /* uses thread local storage to obtain vm ptr */
750   #define PRIMITIVE_GETVM() tls_vm()
751   #define PRIMITIVE_OVERFLOW_GETVM() tls_vm()
752   #define VM_PTR tls_vm()
753   #define ASSERTVM() 
754   #define SIGNAL_VM_PTR() tls_vm()
755 #endif
756
757 #ifdef FACTOR_REENTRANT
758   #define PRIMITIVE_GETVM() ((factor_vm*)myvm)
759   #define PRIMITIVE_OVERFLOW_GETVM() ((factor_vm*)myvm)
760   #define VM_PTR myvm
761   #define ASSERTVM() 
762   #define SIGNAL_VM_PTR() tls_vm()
763 #endif
764
765 }