]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
inlineimpl.hpp is toast
[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         //write barrier
176         cell allot_markers_offset;
177
178         inline card *addr_to_card(cell a)
179         {
180                 return (card*)(((cell)(a) >> card_bits) + cards_offset);
181         }
182
183         inline cell card_to_addr(card *c)
184         {
185                 return ((cell)c - cards_offset) << card_bits;
186         }
187         
188         inline cell card_offset(card *c)
189         {
190                 return *(c - (cell)data->cards + (cell)data->allot_markers);
191         }
192         
193         inline card_deck *addr_to_deck(cell a)
194         {
195                 return (card_deck *)(((cell)a >> deck_bits) + decks_offset);
196         }
197         
198         inline cell deck_to_addr(card_deck *c)
199         {
200                 return ((cell)c - decks_offset) << deck_bits;
201         }
202         
203         inline card *deck_to_card(card_deck *d)
204         {
205                 return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset);
206         }
207         
208         inline card *addr_to_allot_marker(object *a)
209         {
210                 return (card *)(((cell)a >> card_bits) + allot_markers_offset);
211         }
212
213         /* the write barrier must be called any time we are potentially storing a
214            pointer from an older generation to a younger one */
215         inline void write_barrier(object *obj)
216         {
217                 *addr_to_card((cell)obj) = card_mark_mask;
218                 *addr_to_deck((cell)obj) = card_mark_mask;
219         }
220
221         /* we need to remember the first object allocated in the card */
222         inline void allot_barrier(object *address)
223         {
224                 card *ptr = addr_to_allot_marker(address);
225                 if(*ptr == invalid_allot_marker)
226                         *ptr = ((cell)address & addr_card_mask);
227         }
228
229         // data_gc
230         /* used during garbage collection only */
231         zone *newspace;
232         bool performing_gc;
233         bool performing_compaction;
234         cell collecting_gen;
235         /* if true, we are collecting aging space for the second time, so if it is still
236            full, we go on to collect tenured */
237         bool collecting_aging_again;
238         /* in case a generation fills up in the middle of a gc, we jump back
239            up to try collecting the next generation. */
240         jmp_buf gc_jmp;
241         gc_stats stats[max_gen_count];
242         u64 cards_scanned;
243         u64 decks_scanned;
244         u64 card_scan_time;
245         cell code_heap_scans;
246         /* What generation was being collected when copy_code_heap_roots() was last
247            called? Until the next call to add_code_block(), future
248            collections of younger generations don't have to touch the code
249            heap. */
250         cell last_code_heap_scan;
251         /* sometimes we grow the heap */
252         bool growing_data_heap;
253         data_heap *old_data_heap;
254
255         void init_data_gc();
256         object *copy_untagged_object_impl(object *pointer, cell size);
257         object *copy_object_impl(object *untagged);
258         bool should_copy_p(object *untagged);
259         object *resolve_forwarding(object *untagged);
260         template <typename T> T *copy_untagged_object(T *untagged);
261         cell copy_object(cell pointer);
262         void copy_handle(cell *handle);
263         void copy_card(card *ptr, cell gen, cell here);
264         void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask);
265         void copy_gen_cards(cell gen);
266         void copy_cards();
267         void copy_stack_elements(segment *region, cell top);
268         void copy_registered_locals();
269         void copy_registered_bignums();
270         void copy_roots();
271         cell copy_next_from_nursery(cell scan);
272         cell copy_next_from_aging(cell scan);
273         cell copy_next_from_tenured(cell scan);
274         void copy_reachable_objects(cell scan, cell *end);
275         void begin_gc(cell requested_bytes);
276         void end_gc(cell gc_elapsed);
277         void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes);
278         void gc();
279         void primitive_gc();
280         void primitive_gc_stats();
281         void clear_gc_stats();
282         void primitive_become();
283         void inline_gc(cell *gc_roots_base, cell gc_roots_size);
284         inline object *allot_zone(zone *z, cell a);
285         object *allot_object(header header, cell size);
286         void primitive_clear_gc_stats();
287
288         template<typename TYPE> TYPE *allot(cell size)
289         {
290                 return (TYPE *)allot_object(header(TYPE::type_number),size);
291         }
292
293         inline bool collecting_accumulation_gen_p()
294         {
295                 return ((data->have_aging_p()
296                          && collecting_gen == data->aging()
297                          && !collecting_aging_again)
298                         || collecting_gen == data->tenured());
299         }
300
301         inline void check_data_pointer(object *pointer)
302         {
303         #ifdef FACTOR_DEBUG
304                 if(!growing_data_heap)
305                 {
306                         assert((cell)pointer >= data->seg->start
307                                && (cell)pointer < data->seg->end);
308                 }
309         #endif
310         }
311
312         inline void check_tagged_pointer(cell tagged)
313         {
314         #ifdef FACTOR_DEBUG
315                 if(!immediate_p(tagged))
316                 {
317                         object *obj = untag<object>(tagged);
318                         check_data_pointer(obj);
319                         obj->h.hi_tag();
320                 }
321         #endif
322         }
323
324         // local roots
325         /* If a runtime function needs to call another function which potentially
326            allocates memory, it must wrap any local variable references to Factor
327            objects in gc_root instances */
328         std::vector<cell> gc_locals;
329         std::vector<cell> gc_bignums;
330
331         // generic arrays
332         template <typename T> T *allot_array_internal(cell capacity);
333         template <typename T> bool reallot_array_in_place_p(T *array, cell capacity);
334         template <typename TYPE> TYPE *reallot_array(TYPE *array_, cell capacity);
335
336         //debug
337         bool fep_disabled;
338         bool full_output;
339         cell look_for;
340         cell obj;
341
342         void print_chars(string* str);
343         void print_word(word* word, cell nesting);
344         void print_factor_string(string* str);
345         void print_array(array* array, cell nesting);
346         void print_tuple(tuple *tuple, cell nesting);
347         void print_nested_obj(cell obj, fixnum nesting);
348         void print_obj(cell obj);
349         void print_objects(cell *start, cell *end);
350         void print_datastack();
351         void print_retainstack();
352         void print_stack_frame(stack_frame *frame);
353         void print_callstack();
354         void dump_cell(cell x);
355         void dump_memory(cell from, cell to);
356         void dump_zone(zone *z);
357         void dump_generations();
358         void dump_objects(cell type);
359         void find_data_references_step(cell *scan);
360         void find_data_references(cell look_for_);
361         void dump_code_heap();
362         void factorbug();
363         void primitive_die();
364
365         //arrays
366         array *allot_array(cell capacity, cell fill_);
367         void primitive_array();
368         cell allot_array_1(cell obj_);
369         cell allot_array_2(cell v1_, cell v2_);
370         cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
371         void primitive_resize_array();
372         inline void set_array_nth(array *array, cell slot, cell value);
373
374         //strings
375         cell string_nth(string* str, cell index);
376         void set_string_nth_fast(string *str, cell index, cell ch);
377         void set_string_nth_slow(string *str_, cell index, cell ch);
378         void set_string_nth(string *str, cell index, cell ch);
379         string *allot_string_internal(cell capacity);
380         void fill_string(string *str_, cell start, cell capacity, cell fill);
381         string *allot_string(cell capacity, cell fill);
382         void primitive_string();
383         bool reallot_string_in_place_p(string *str, cell capacity);
384         string* reallot_string(string *str_, cell capacity);
385         void primitive_resize_string();
386         void primitive_string_nth();
387         void primitive_set_string_nth_fast();
388         void primitive_set_string_nth_slow();
389
390         //booleans
391         void box_boolean(bool value);
392         bool to_boolean(cell value);
393         inline cell tag_boolean(cell untagged);
394
395         //byte arrays
396         byte_array *allot_byte_array(cell size);
397         void primitive_byte_array();
398         void primitive_uninitialized_byte_array();
399         void primitive_resize_byte_array();
400
401         //tuples
402         tuple *allot_tuple(cell layout_);
403         void primitive_tuple();
404         void primitive_tuple_boa();
405
406         //words
407         word *allot_word(cell name_, cell vocab_, cell hashcode_);
408         void primitive_word();
409         void primitive_word_xt();
410         void update_word_xt(cell w_);
411         void primitive_optimized_p();
412         void primitive_wrapper();
413
414         //math
415         cell bignum_zero;
416         cell bignum_pos_one;
417         cell bignum_neg_one;    
418
419         void primitive_bignum_to_fixnum();
420         void primitive_float_to_fixnum();
421         void primitive_fixnum_divint();
422         void primitive_fixnum_divmod();
423         bignum *fixnum_to_bignum(fixnum);
424         bignum *cell_to_bignum(cell);
425         bignum *long_long_to_bignum(s64 n);
426         bignum *ulong_long_to_bignum(u64 n);
427         inline fixnum sign_mask(fixnum x);
428         inline fixnum branchless_max(fixnum x, fixnum y);
429         inline fixnum branchless_abs(fixnum x);
430         void primitive_fixnum_shift();
431         void primitive_fixnum_to_bignum();
432         void primitive_float_to_bignum();
433         void primitive_bignum_eq();
434         void primitive_bignum_add();
435         void primitive_bignum_subtract();
436         void primitive_bignum_multiply();
437         void primitive_bignum_divint();
438         void primitive_bignum_divmod();
439         void primitive_bignum_mod();
440         void primitive_bignum_and();
441         void primitive_bignum_or();
442         void primitive_bignum_xor();
443         void primitive_bignum_shift();
444         void primitive_bignum_less();
445         void primitive_bignum_lesseq();
446         void primitive_bignum_greater();
447         void primitive_bignum_greatereq();
448         void primitive_bignum_not();
449         void primitive_bignum_bitp();
450         void primitive_bignum_log2();
451         unsigned int bignum_producer(unsigned int digit);
452         void primitive_byte_array_to_bignum();
453         cell unbox_array_size();
454         void primitive_fixnum_to_float();
455         void primitive_bignum_to_float();
456         void primitive_str_to_float();
457         void primitive_float_to_str();
458         void primitive_float_eq();
459         void primitive_float_add();
460         void primitive_float_subtract();
461         void primitive_float_multiply();
462         void primitive_float_divfloat();
463         void primitive_float_mod();
464         void primitive_float_less();
465         void primitive_float_lesseq();
466         void primitive_float_greater();
467         void primitive_float_greatereq();
468         void primitive_float_bits();
469         void primitive_bits_float();
470         void primitive_double_bits();
471         void primitive_bits_double();
472         fixnum to_fixnum(cell tagged);
473         cell to_cell(cell tagged);
474         void box_signed_1(s8 n);
475         void box_unsigned_1(u8 n);
476         void box_signed_2(s16 n);
477         void box_unsigned_2(u16 n);
478         void box_signed_4(s32 n);
479         void box_unsigned_4(u32 n);
480         void box_signed_cell(fixnum integer);
481         void box_unsigned_cell(cell cell);
482         void box_signed_8(s64 n);
483         s64 to_signed_8(cell obj);
484         void box_unsigned_8(u64 n);
485         u64 to_unsigned_8(cell obj);
486         void box_float(float flo);
487         float to_float(cell value);
488         void box_double(double flo);
489         double to_double(cell value);
490         inline void overflow_fixnum_add(fixnum x, fixnum y);
491         inline void overflow_fixnum_subtract(fixnum x, fixnum y);
492         inline void overflow_fixnum_multiply(fixnum x, fixnum y);
493         inline cell allot_integer(fixnum x);
494         inline cell allot_cell(cell x);
495         inline cell allot_float(double n);
496         inline bignum *float_to_bignum(cell tagged);
497         inline double bignum_to_float(cell tagged);
498         inline double untag_float(cell tagged);
499         inline double untag_float_check(cell tagged);
500         inline fixnum float_to_fixnum(cell tagged);
501         inline double fixnum_to_float(cell tagged);
502         template <typename T> T *untag_check(cell value);
503         template <typename T> T *untag(cell value);
504         
505         //io
506         void init_c_io();
507         void io_error();
508         void primitive_fopen();
509         void primitive_fgetc();
510         void primitive_fread();
511         void primitive_fputc();
512         void primitive_fwrite();
513         void primitive_fseek();
514         void primitive_fflush();
515         void primitive_fclose();
516
517         //code_block
518         typedef void (factor_vm::*relocation_iterator)(relocation_entry rel, cell index, code_block *compiled);
519
520         relocation_type relocation_type_of(relocation_entry r);
521         relocation_class relocation_class_of(relocation_entry r);
522         cell relocation_offset_of(relocation_entry r);
523         void flush_icache_for(code_block *block);
524         int number_of_parameters(relocation_type type);
525         void *object_xt(cell obj);
526         void *xt_pic(word *w, cell tagged_quot);
527         void *word_xt_pic(word *w);
528         void *word_xt_pic_tail(word *w);
529         void undefined_symbol();
530         void *get_rel_symbol(array *literals, cell index);
531         cell compute_relocation(relocation_entry rel, cell index, code_block *compiled);
532         void iterate_relocations(code_block *compiled, relocation_iterator iter);
533         void store_address_2_2(cell *ptr, cell value);
534         void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift);
535         void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value);
536         void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled);
537         void update_literal_references(code_block *compiled);
538         void copy_literal_references(code_block *compiled);
539         void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled);
540         void update_word_references_step(relocation_entry rel, cell index, code_block *compiled);
541         void update_word_references(code_block *compiled);
542         void update_literal_and_word_references(code_block *compiled);
543         void check_code_address(cell address);
544         void mark_code_block(code_block *compiled);
545         void mark_stack_frame_step(stack_frame *frame);
546         void mark_active_blocks(context *stacks);
547         void mark_object_code_block(object *object);
548         void relocate_code_block(code_block *compiled);
549         void fixup_labels(array *labels, code_block *compiled);
550         code_block *allot_code_block(cell size);
551         code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_);
552         inline bool stack_traces_p()
553         {
554                 return userenv[STACK_TRACES_ENV] != F;
555         }
556
557         //code_heap
558         heap *code;
559         unordered_map<heap_block *, char *> forwarding;
560         typedef void (factor_vm::*code_heap_iterator)(code_block *compiled);
561
562         void init_code_heap(cell size);
563         bool in_code_heap_p(cell ptr);
564         void jit_compile_word(cell word_, cell def_, bool relocate);
565         void iterate_code_heap(code_heap_iterator iter);
566         void copy_code_heap_roots();
567         void update_code_heap_words();
568         void primitive_modify_code_heap();
569         void primitive_code_room();
570         code_block *forward_xt(code_block *compiled);
571         void forward_frame_xt(stack_frame *frame);
572         void forward_object_xts();
573         void fixup_object_xts();
574         void compact_code_heap();
575         inline void check_code_pointer(cell ptr);
576
577         //image
578         cell code_relocation_base;
579         cell data_relocation_base;
580
581         void init_objects(image_header *h);
582         void load_data_heap(FILE *file, image_header *h, vm_parameters *p);
583         void load_code_heap(FILE *file, image_header *h, vm_parameters *p);
584         bool save_image(const vm_char *filename);
585         void primitive_save_image();
586         void primitive_save_image_and_exit();
587         void data_fixup(cell *cell);
588         template <typename T> void code_fixup(T **handle);
589         void fixup_word(word *word);
590         void fixup_quotation(quotation *quot);
591         void fixup_alien(alien *d);
592         void fixup_stack_frame(stack_frame *frame);
593         void fixup_callstack_object(callstack *stack);
594         void relocate_object(object *object);
595         void relocate_data();
596         void fixup_code_block(code_block *compiled);
597         void relocate_code();
598         void load_image(vm_parameters *p);
599
600         //callstack
601         template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator);
602         void check_frame(stack_frame *frame);
603         callstack *allot_callstack(cell size);
604         stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom);
605         stack_frame *capture_start();
606         void primitive_callstack();
607         void primitive_set_callstack();
608         code_block *frame_code(stack_frame *frame);
609         cell frame_type(stack_frame *frame);
610         cell frame_executing(stack_frame *frame);
611         stack_frame *frame_successor(stack_frame *frame);
612         cell frame_scan(stack_frame *frame);
613         void primitive_callstack_to_array();
614         stack_frame *innermost_stack_frame(callstack *stack);
615         stack_frame *innermost_stack_frame_quot(callstack *callstack);
616         void primitive_innermost_stack_frame_executing();
617         void primitive_innermost_stack_frame_scan();
618         void primitive_set_innermost_stack_frame_quot();
619         void save_callstack_bottom(stack_frame *callstack_bottom);
620         template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator);
621         inline void do_slots(cell obj, void (* iter)(cell *,factor_vm*));
622
623         //alien
624         char *pinned_alien_offset(cell obj);
625         cell allot_alien(cell delegate_, cell displacement);
626         void primitive_displaced_alien();
627         void primitive_alien_address();
628         void *alien_pointer();
629         void primitive_dlopen();
630         void primitive_dlsym();
631         void primitive_dlclose();
632         void primitive_dll_validp();
633         void primitive_vm_ptr();
634         char *alien_offset(cell obj);
635         char *unbox_alien();
636         void box_alien(void *ptr);
637         void to_value_struct(cell src, void *dest, cell size);
638         void box_value_struct(void *src, cell size);
639         void box_small_struct(cell x, cell y, cell size);
640         void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size);
641
642         //quotations
643         void primitive_jit_compile();
644         void primitive_array_to_quotation();
645         void primitive_quotation_xt();
646         void set_quot_xt(quotation *quot, code_block *code);
647         void jit_compile(cell quot_, bool relocating);
648         void compile_all_words();
649         fixnum quot_code_offset_to_scan(cell quot_, cell offset);
650         cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);
651         void primitive_quot_compiled_p();
652
653         //dispatch
654         cell megamorphic_cache_hits;
655         cell megamorphic_cache_misses;
656
657         cell search_lookup_alist(cell table, cell klass);
658         cell search_lookup_hash(cell table, cell klass, cell hashcode);
659         cell nth_superclass(tuple_layout *layout, fixnum echelon);
660         cell nth_hashcode(tuple_layout *layout, fixnum echelon);
661         cell lookup_tuple_method(cell obj, cell methods);
662         cell lookup_hi_tag_method(cell obj, cell methods);
663         cell lookup_hairy_method(cell obj, cell methods);
664         cell lookup_method(cell obj, cell methods);
665         void primitive_lookup_method();
666         cell object_class(cell obj);
667         cell method_cache_hashcode(cell klass, array *array);
668         void update_method_cache(cell cache, cell klass, cell method);
669         void primitive_mega_cache_miss();
670         void primitive_reset_dispatch_stats();
671         void primitive_dispatch_stats();
672
673         //inline cache
674         cell max_pic_size;
675         cell cold_call_to_ic_transitions;
676         cell ic_to_pic_transitions;
677         cell pic_to_mega_transitions;
678         cell pic_counts[4];  /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
679
680         void init_inline_caching(int max_size);
681         void deallocate_inline_cache(cell return_address);
682         cell determine_inline_cache_type(array *cache_entries);
683         void update_pic_count(cell type);
684         code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p);
685         void *megamorphic_call_stub(cell generic_word);
686         cell inline_cache_size(cell cache_entries);
687         cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
688         void update_pic_transitions(cell pic_size);
689         void *inline_cache_miss(cell return_address);
690         void primitive_reset_inline_cache_stats();
691         void primitive_inline_cache_stats();
692
693         //factor
694         void default_parameters(vm_parameters *p);
695         bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
696         void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv);
697         void do_stage1_init();
698         void init_factor(vm_parameters *p);
699         void pass_args_to_factor(int argc, vm_char **argv);
700         void start_factor(vm_parameters *p);
701         void start_embedded_factor(vm_parameters *p);
702         void start_standalone_factor(int argc, vm_char **argv);
703         char *factor_eval_string(char *string);
704         void factor_eval_free(char *result);
705         void factor_yield();
706         void factor_sleep(long us);
707
708         // os-*
709         void primitive_existsp();
710         void init_ffi();
711         void ffi_dlopen(dll *dll);
712         void *ffi_dlsym(dll *dll, symbol_char *symbol);
713         void ffi_dlclose(dll *dll);
714         void c_to_factor_toplevel(cell quot);
715
716         // os-windows
717   #if defined(WINDOWS)
718         void sleep_micros(u64 usec);
719         const vm_char *vm_executable_path();
720         const vm_char *default_image_path();
721         void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length);
722         bool windows_stat(vm_char *path);
723         
724    #if defined(WINNT)
725         void open_console();
726         LONG exception_handler(PEXCEPTION_POINTERS pe);
727         // next method here:    
728    #endif
729   #else  // UNIX
730         void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap);
731         void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap);
732         void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap);
733         stack_frame *uap_stack_pointer(void *uap);
734
735   #endif
736
737   #ifdef __APPLE__
738         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);
739   #endif
740         
741         factor_vm() 
742                 : profiling_p(false),
743                   secure_gc(false),
744                   gc_off(false),
745                   performing_gc(false),
746                   performing_compaction(false),
747                   collecting_aging_again(false),
748                   growing_data_heap(false),
749                   fep_disabled(false),
750                   full_output(false),
751                   max_pic_size(0)
752         {
753                 memset(this,0,sizeof(this)); // just to make sure
754         }
755
756 };
757
758 #ifndef FACTOR_REENTRANT
759    #define FACTOR_SINGLE_THREADED_SINGLETON
760 #endif
761
762 #ifdef FACTOR_SINGLE_THREADED_SINGLETON
763 /* calls are dispatched using the singleton vm ptr */
764   extern factor_vm *vm;
765   #define PRIMITIVE_GETVM() vm
766   #define PRIMITIVE_OVERFLOW_GETVM() vm
767   #define VM_PTR vm
768   #define ASSERTVM() 
769   #define SIGNAL_VM_PTR() vm
770 #endif
771
772 #ifdef FACTOR_SINGLE_THREADED_TESTING
773 /* calls are dispatched as per multithreaded, but checked against singleton */
774   extern factor_vm *vm;
775   #define ASSERTVM() assert(vm==myvm)
776   #define PRIMITIVE_GETVM() ((factor_vm*)myvm)
777   #define PRIMITIVE_OVERFLOW_GETVM() ASSERTVM(); myvm
778   #define VM_PTR myvm
779   #define SIGNAL_VM_PTR() tls_vm()
780 #endif
781
782 #ifdef FACTOR_REENTRANT_TLS
783 /* uses thread local storage to obtain vm ptr */
784   #define PRIMITIVE_GETVM() tls_vm()
785   #define PRIMITIVE_OVERFLOW_GETVM() tls_vm()
786   #define VM_PTR tls_vm()
787   #define ASSERTVM() 
788   #define SIGNAL_VM_PTR() tls_vm()
789 #endif
790
791 #ifdef FACTOR_REENTRANT
792   #define PRIMITIVE_GETVM() ((factor_vm*)myvm)
793   #define PRIMITIVE_OVERFLOW_GETVM() ((factor_vm*)myvm)
794   #define VM_PTR myvm
795   #define ASSERTVM() 
796   #define SIGNAL_VM_PTR() tls_vm()
797 #endif
798
799 }