]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
VM: the windows_image_path method is unused
[factor.git] / vm / vm.hpp
1 using namespace std;
2
3 namespace factor {
4
5 typedef void (*c_to_factor_func_type)(cell quot);
6 typedef void (*unwind_native_frames_func_type)(cell quot, cell to);
7 typedef cell (*get_fpu_state_func_type)();
8 typedef void (*set_fpu_state_func_type)(cell state);
9
10 struct growable_array;
11 struct code_root;
12
13 struct factor_vm {
14   //
15   // vvvvvv
16   // THESE FIELDS ARE ACCESSED DIRECTLY FROM FACTOR. See:
17   //   basis/vm/vm.factor
18   //   basis/compiler/constants/constants.factor
19
20   /* Current context */
21   context* ctx;
22
23   /* Spare context -- for callbacks */
24   context* spare_ctx;
25
26   /* New objects are allocated here, use the data->nursery reference
27      instead from c++ code. */
28   bump_allocator nursery;
29
30   /* Add this to a shifted address to compute write barrier offsets */
31   cell cards_offset;
32   cell decks_offset;
33
34   /* cdecl signal handler address, used by signal handler subprimitives */
35   cell signal_handler_addr;
36
37   /* are we handling a memory error? used to detect double faults */
38   cell faulting_p;
39
40   /* Various special objects, accessed by special-object and
41         set-special-object primitives */
42   cell special_objects[special_object_count];
43
44   // THESE FIELDS ARE ACCESSED DIRECTLY FROM FACTOR.
45   // ^^^^^^
46   //
47
48   /* Handle to the main thread we run in */
49   THREADHANDLE thread;
50
51   /* Data stack and retain stack sizes */
52   cell datastack_size, retainstack_size, callstack_size;
53
54   /* Stack of callback IDs */
55   std::vector<int> callback_ids;
56
57   /* Next callback ID */
58   int callback_id;
59
60   /* List of callback function descriptors for PPC */
61   std::list<void**> function_descriptors;
62
63   /* Pooling unused contexts to make context allocation cheaper */
64   std::list<context*> unused_contexts;
65
66   /* Active contexts, for tracing by the GC */
67   std::set<context*> active_contexts;
68
69   /* Canonical truth value. In Factor, 't' */
70   cell true_object;
71
72   /* External entry points */
73   c_to_factor_func_type c_to_factor_func;
74
75   /* Is profiling enabled? */
76   volatile cell sampling_profiler_p;
77   fixnum samples_per_second;
78
79   /* Global variables used to pass fault handler state from signal handler
80      to VM */
81   bool signal_resumable;
82   cell signal_number;
83   cell signal_fault_addr;
84   cell signal_fault_pc;
85   unsigned int signal_fpu_status;
86
87   /* Pipe used to notify Factor multiplexer of signals */
88   int signal_pipe_input, signal_pipe_output;
89
90   /* State kept by the sampling profiler */
91   std::vector<profiling_sample> samples;
92   std::vector<cell> sample_callstacks;
93
94   /* GC is off during heap walking */
95   bool gc_off;
96
97   /* Data heap */
98   data_heap* data;
99
100   /* Code heap */
101   code_heap* code;
102
103   /* Pinned callback stubs */
104   callback_heap* callbacks;
105
106   /* Only set if we're performing a GC */
107   gc_state* current_gc;
108   volatile cell current_gc_p;
109
110   /* Set if we're in the jit */
111   volatile fixnum current_jit_count;
112
113   /* Mark stack */
114   std::vector<cell> mark_stack;
115
116   /* If not NULL, we push GC events here */
117   std::vector<gc_event>* gc_events;
118
119   /* If a runtime function needs to call another function which potentially
120      allocates memory, it must wrap any references to the data and code
121      heaps with data_root and code_root smart pointers, which register
122      themselves here. See data_roots.hpp and code_roots.hpp */
123
124   std::vector<cell*> data_roots;
125   std::vector<code_root*> code_roots;
126
127   /* Debugger */
128   bool fep_p;
129   bool fep_help_was_shown;
130   bool fep_disabled;
131   bool full_output;
132
133   /* Canonical bignums */
134   cell bignum_zero;
135   cell bignum_pos_one;
136   cell bignum_neg_one;
137
138   /* Method dispatch statistics */
139   dispatch_statistics dispatch_stats;
140
141   /* Number of entries in a polymorphic inline cache */
142   cell max_pic_size;
143
144   /* Incrementing object counter for identity hashing */
145   cell object_counter;
146
147   /* Sanity check to ensure that monotonic counter doesn't decrease */
148   uint64_t last_nano_count;
149
150   /* Stack for signal handlers, only used on Unix */
151   segment* signal_callstack_seg;
152
153   /* Are we already handling a fault? Used to catch double memory faults */
154   static bool fatal_erroring_p;
155
156   /* Safepoint state */
157   volatile safepoint_state safepoint;
158
159   // contexts
160   context* new_context();
161   void init_context(context* ctx);
162   void delete_context();
163   void init_contexts(cell datastack_size_, cell retainstack_size_,
164                      cell callstack_size_);
165   void delete_contexts();
166   cell begin_callback(cell quot);
167   void end_callback();
168   void primitive_current_callback();
169   void primitive_context_object();
170   void primitive_context_object_for();
171   void primitive_set_context_object();
172   cell stack_to_array(cell bottom, cell top, vm_error_type error);
173   cell datastack_to_array(context* ctx);
174   void primitive_datastack_for();
175   cell retainstack_to_array(context* ctx);
176   void primitive_retainstack_for();
177   cell array_to_stack(array* array, cell bottom);
178   void set_datastack(context* ctx, array* array);
179   void primitive_set_datastack();
180   void set_retainstack(context* ctx, array* array);
181   void primitive_set_retainstack();
182   void primitive_check_datastack();
183   void primitive_load_locals();
184
185   // run
186   void primitive_exit();
187   void primitive_nano_count();
188   void primitive_sleep();
189   void primitive_set_slot();
190
191   // objects
192   void primitive_special_object();
193   void primitive_set_special_object();
194   void primitive_identity_hashcode();
195   void compute_identity_hashcode(object* obj);
196   void primitive_compute_identity_hashcode();
197   cell object_size(cell tagged);
198   cell clone_object(cell obj_);
199   void primitive_clone();
200   void primitive_become();
201
202   // sampling_profiler
203   void clear_samples();
204   void record_sample(bool prolog_p);
205   void record_callstack_sample(cell* begin, cell* end, bool prolog_p);
206   void start_sampling_profiler(fixnum rate);
207   void end_sampling_profiler();
208   void set_sampling_profiler(fixnum rate);
209   void primitive_sampling_profiler();
210   void primitive_get_samples();
211   void primitive_clear_samples();
212
213   // errors
214   void general_error(vm_error_type error, cell arg1, cell arg2);
215   void type_error(cell type, cell tagged);
216   void not_implemented_error();
217   void verify_memory_protection_error(cell addr);
218   void memory_protection_error(cell pc, cell addr);
219   void divide_by_zero_error();
220   void primitive_unimplemented();
221   void memory_signal_handler_impl();
222   void synchronous_signal_handler_impl();
223   void fp_signal_handler_impl();
224
225   // bignum
226   int bignum_equal_p(bignum* x, bignum* y);
227   enum bignum_comparison bignum_compare(bignum* x, bignum* y);
228   bignum* bignum_add(bignum* x, bignum* y);
229   bignum* bignum_subtract(bignum* x, bignum* y);
230   bignum* bignum_square(bignum* x_);
231   bignum* bignum_multiply(bignum* x, bignum* y);
232   void bignum_divide(bignum* numerator, bignum* denominator, bignum** quotient,
233                      bignum** remainder);
234   bignum* bignum_quotient(bignum* numerator, bignum* denominator);
235   bignum* bignum_remainder(bignum* numerator, bignum* denominator);
236   fixnum bignum_to_fixnum_strict(bignum* bn);
237   bignum* double_to_bignum(double x);
238   int bignum_equal_p_unsigned(bignum* x, bignum* y);
239   enum bignum_comparison bignum_compare_unsigned(bignum* x, bignum* y);
240   bignum* bignum_add_unsigned(bignum* x_, bignum* y_, int negative_p);
241   bignum* bignum_subtract_unsigned(bignum* x_, bignum* y_);
242   bignum* bignum_multiply_unsigned(bignum* x_, bignum* y_, int negative_p);
243   bignum* bignum_multiply_unsigned_small_factor(bignum* x, bignum_digit_type y,
244                                                 int negative_p);
245   void bignum_destructive_add(bignum* bn, bignum_digit_type n);
246   void bignum_destructive_scale_up(bignum* bn, bignum_digit_type factor);
247   void bignum_divide_unsigned_large_denominator(
248       bignum* numerator_, bignum* denominator_, bignum** quotient,
249       bignum** remainder, int q_negative_p, int r_negative_p);
250   void bignum_divide_unsigned_normalized(bignum* u, bignum* v, bignum* q);
251   bignum_digit_type bignum_divide_subtract(bignum_digit_type* v_start,
252                                            bignum_digit_type* v_end,
253                                            bignum_digit_type guess,
254                                            bignum_digit_type* u_start);
255   void bignum_divide_unsigned_medium_denominator(
256       bignum* numerator_, bignum_digit_type denominator, bignum** quotient,
257       bignum** remainder, int q_negative_p, int r_negative_p);
258   void bignum_destructive_normalization(bignum* source, bignum* target,
259                                         int shift_left);
260   void bignum_destructive_unnormalization(bignum* bn, int shift_right);
261   bignum_digit_type bignum_digit_divide(
262       bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v,
263       bignum_digit_type* q) /* return value */;
264   bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1,
265                                                  bignum_digit_type v2,
266                                                  bignum_digit_type guess,
267                                                  bignum_digit_type* u);
268   void bignum_divide_unsigned_small_denominator(
269       bignum* numerator_, bignum_digit_type denominator, bignum** quotient,
270       bignum** remainder, int q_negative_p, int r_negative_p);
271   bignum_digit_type bignum_destructive_scale_down(
272       bignum* bn, bignum_digit_type denominator);
273   bignum* bignum_remainder_unsigned_small_denominator(bignum* n,
274                                                       bignum_digit_type d,
275                                                       int negative_p);
276   bignum* bignum_digit_to_bignum(bignum_digit_type digit, int negative_p);
277   bignum* allot_bignum(bignum_length_type length, int negative_p);
278   bignum* allot_bignum_zeroed(bignum_length_type length, int negative_p);
279   bignum* bignum_shorten_length(bignum* bn, bignum_length_type length);
280   bignum* bignum_trim(bignum* bn);
281   bignum* bignum_new_sign(bignum* x_, int negative_p);
282   bignum* bignum_maybe_new_sign(bignum* x_, int negative_p);
283   void bignum_destructive_copy(bignum* source, bignum* target);
284   bignum* bignum_bitwise_not(bignum* x_);
285   bignum* bignum_arithmetic_shift(bignum* arg1, fixnum n);
286   bignum* bignum_bitwise_and(bignum* arg1, bignum* arg2);
287   bignum* bignum_bitwise_ior(bignum* arg1, bignum* arg2);
288   bignum* bignum_bitwise_xor(bignum* arg1, bignum* arg2);
289   bignum* bignum_magnitude_ash(bignum* arg1_, fixnum n);
290   bignum* bignum_pospos_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
291   bignum* bignum_posneg_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
292   bignum* bignum_negneg_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
293   void bignum_negate_magnitude(bignum* arg);
294   bignum* bignum_integer_length(bignum* x_);
295   int bignum_logbitp(int shift, bignum* arg);
296   int bignum_unsigned_logbitp(int shift, bignum* bn);
297   bignum* bignum_gcd(bignum* a_, bignum* b_);
298
299   //data heap
300   void init_card_decks();
301   void set_data_heap(data_heap* data_);
302   void init_data_heap(cell young_size, cell aging_size, cell tenured_size);
303   void primitive_size();
304   data_heap_room data_room();
305   void primitive_data_room();
306   void begin_scan();
307   void end_scan();
308   cell instances(cell type);
309   void primitive_all_instances();
310
311   template <typename Generation, typename Iterator>
312   inline void each_object(Generation* gen, Iterator& iterator) {
313     cell obj = gen->first_object();
314     while (obj) {
315       iterator((object*)obj);
316       obj = gen->next_object_after(obj);
317     }
318   }
319
320   template <typename Iterator> inline void each_object(Iterator& iterator) {
321
322     /* The nursery can't be iterated because there may be gaps between
323        the objects (see factor_vm::reallot_array) so we require it to
324        be empty first. */
325     FACTOR_ASSERT(data->nursery->occupied_space() == 0);
326
327     gc_off = true;
328     each_object(data->tenured, iterator);
329     each_object(data->aging, iterator);
330     gc_off = false;
331   }
332
333   template <typename Iterator>
334   inline void each_object_each_slot(Iterator& iterator) {
335     auto each_object_func = [&](object* obj) {
336       auto each_slot_func = [&](cell* slot) {
337         iterator(obj, slot);
338       };
339       obj->each_slot(each_slot_func);
340     };
341     each_object(each_object_func);
342   }
343
344   /* the write barrier must be called any time we are potentially storing a
345      pointer from an older generation to a younger one */
346   inline void write_barrier(cell* slot_ptr) {
347     *(char*)(cards_offset + ((cell)slot_ptr >> card_bits)) = card_mark_mask;
348     *(char*)(decks_offset + ((cell)slot_ptr >> deck_bits)) = card_mark_mask;
349   }
350
351   inline void write_barrier(object* obj, cell size) {
352     cell start = (cell)obj & (~card_size + 1);
353     cell end = ((cell)obj + size + card_size - 1) & (~card_size + 1);
354
355     for (cell offset = start; offset < end; offset += card_size)
356       write_barrier((cell*)offset);
357   }
358
359   // data heap checker
360   void check_data_heap();
361
362   // gc
363   void end_gc();
364   void set_current_gc_op(gc_op op);
365   void start_gc_again();
366   void collect_nursery();
367   void collect_aging();
368   void collect_to_tenured();
369   void update_code_roots_for_sweep();
370   void update_code_roots_for_compaction();
371   void collect_mark_impl();
372   void collect_sweep_impl();
373   void collect_full();
374   void collect_compact_impl();
375   void collect_compact();
376   void collect_growing_heap(cell requested_size);
377   void gc(gc_op op, cell requested_size);
378   void primitive_minor_gc();
379   void primitive_full_gc();
380   void primitive_compact_gc();
381   void primitive_enable_gc_events();
382   void primitive_disable_gc_events();
383   object* allot_object(cell type, cell size);
384   object* allot_large_object(cell type, cell size);
385
386   /* Allocates memory */
387   template <typename Type> Type* allot(cell size) {
388     return (Type*)allot_object(Type::type_number, size);
389   }
390
391   // generic arrays
392   template <typename Array> Array* allot_uninitialized_array(cell capacity);
393   template <typename Array>
394   bool reallot_array_in_place_p(Array* array, cell capacity);
395   template <typename Array> Array* reallot_array(Array* array_, cell capacity);
396
397   // debug
398   void print_chars(ostream& out, string* str);
399   void print_word(ostream& out, word* word, cell nesting);
400   void print_factor_string(ostream& out, string* str);
401   void print_array(ostream& out, array* array, cell nesting);
402   void print_byte_array(ostream& out, byte_array* array, cell nesting);
403   void print_tuple(ostream& out, tuple* tuple, cell nesting);
404   void print_alien(ostream& out, alien* alien, cell nesting);
405   void print_nested_obj(ostream& out, cell obj, fixnum nesting);
406   void print_obj(ostream& out, cell obj);
407   void print_objects(ostream& out, cell* start, cell* end);
408   void print_datastack(ostream& out);
409   void print_retainstack(ostream& out);
410   void print_callstack(ostream& out);
411   void print_callstack_object(ostream& out, callstack* obj);
412   void dump_cell(ostream& out, cell x);
413   void dump_memory(ostream& out, cell from, cell to);
414   template <typename Generation>
415   void dump_generation(ostream& out, const char* name, Generation* gen);
416   void dump_generations(ostream& out);
417   void dump_objects(ostream& out, cell type);
418   void dump_edges(ostream& out);
419   void find_data_references(ostream& out, cell look_for_);
420   void dump_code_heap(ostream& out);
421   void factorbug_usage(bool advanced_p);
422   void factorbug();
423   void primitive_die();
424
425   // arrays
426   inline void set_array_nth(array* array, cell slot, cell value);
427   array* allot_array(cell capacity, cell fill_);
428   void primitive_array();
429   cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
430   void primitive_resize_array();
431   cell std_vector_to_array(std::vector<cell>& elements);
432
433   // strings
434   string* allot_string_internal(cell capacity);
435   void fill_string(string* str_, cell start, cell capacity, cell fill);
436   string* allot_string(cell capacity, cell fill);
437   void primitive_string();
438   bool reallot_string_in_place_p(string* str, cell capacity);
439   string* reallot_string(string* str_, cell capacity);
440   void primitive_resize_string();
441   void primitive_set_string_nth_fast();
442
443   // booleans
444   cell tag_boolean(cell untagged) {
445     return (untagged ? true_object : false_object);
446   }
447
448   // byte arrays
449   byte_array* allot_byte_array(cell size);
450   void primitive_byte_array();
451   void primitive_uninitialized_byte_array();
452   void primitive_resize_byte_array();
453
454   template <typename Type> byte_array* byte_array_from_value(Type* value);
455
456   // tuples
457   void primitive_tuple();
458   void primitive_tuple_boa();
459
460   // words
461   word* allot_word(cell name_, cell vocab_, cell hashcode_);
462   void primitive_word();
463   void primitive_word_code();
464   void primitive_word_optimized_p();
465   void primitive_wrapper();
466   void jit_compile_word(cell word_, cell def_, bool relocating);
467   cell find_all_words();
468   void compile_all_words();
469
470   // math
471   void primitive_bignum_to_fixnum();
472   void primitive_bignum_to_fixnum_strict();
473   void primitive_float_to_fixnum();
474   void primitive_fixnum_divint();
475   void primitive_fixnum_divmod();
476   bignum* fixnum_to_bignum(fixnum);
477   bignum* cell_to_bignum(cell);
478   bignum* long_long_to_bignum(int64_t n);
479   bignum* ulong_long_to_bignum(uint64_t n);
480   inline fixnum sign_mask(fixnum x);
481   inline fixnum branchless_max(fixnum x, fixnum y);
482   inline fixnum branchless_abs(fixnum x);
483   void primitive_fixnum_shift();
484   void primitive_fixnum_to_bignum();
485   void primitive_float_to_bignum();
486   void primitive_bignum_eq();
487   void primitive_bignum_add();
488   void primitive_bignum_subtract();
489   void primitive_bignum_multiply();
490   void primitive_bignum_divint();
491   void primitive_bignum_divmod();
492   void primitive_bignum_mod();
493   void primitive_bignum_gcd();
494   void primitive_bignum_and();
495   void primitive_bignum_or();
496   void primitive_bignum_xor();
497   void primitive_bignum_shift();
498   void primitive_bignum_less();
499   void primitive_bignum_lesseq();
500   void primitive_bignum_greater();
501   void primitive_bignum_greatereq();
502   void primitive_bignum_not();
503   void primitive_bignum_bitp();
504   void primitive_bignum_log2();
505   inline cell unbox_array_size();
506   void primitive_fixnum_to_float();
507   void primitive_format_float();
508   void primitive_float_eq();
509   void primitive_float_add();
510   void primitive_float_subtract();
511   void primitive_float_multiply();
512   void primitive_float_divfloat();
513   void primitive_float_less();
514   void primitive_float_lesseq();
515   void primitive_float_greater();
516   void primitive_float_greatereq();
517   void primitive_float_bits();
518   void primitive_bits_float();
519   void primitive_double_bits();
520   void primitive_bits_double();
521   fixnum to_fixnum(cell tagged);
522   fixnum to_fixnum_strict(cell tagged);
523   cell to_cell(cell tagged);
524   cell from_signed_8(int64_t n);
525   int64_t to_signed_8(cell obj);
526   cell from_unsigned_8(uint64_t n);
527   uint64_t to_unsigned_8(cell obj);
528   float to_float(cell value);
529   double to_double(cell value);
530   inline void overflow_fixnum_add(fixnum x, fixnum y);
531   inline void overflow_fixnum_subtract(fixnum x, fixnum y);
532   inline void overflow_fixnum_multiply(fixnum x, fixnum y);
533   inline cell from_signed_cell(fixnum x);
534   inline cell from_unsigned_cell(cell x);
535   inline cell allot_float(double n);
536   inline bignum* float_to_bignum(cell tagged);
537   inline double untag_float(cell tagged);
538   inline double untag_float_check(cell tagged);
539   inline fixnum float_to_fixnum(cell tagged);
540   inline double fixnum_to_float(cell tagged);
541
542   // tagged
543   template <typename Type> Type* untag_check(cell value);
544
545   // io
546   void init_c_io();
547   void io_error_if_not_EINTR();
548   FILE* safe_fopen(char* filename, char* mode);
549   int safe_fgetc(FILE* stream);
550   size_t safe_fread(void* ptr, size_t size, size_t nitems, FILE* stream);
551   void safe_fputc(int c, FILE* stream);
552   size_t safe_fwrite(void* ptr, size_t size, size_t nitems, FILE* stream);
553   int safe_ftell(FILE* stream);
554   void safe_fseek(FILE* stream, off_t offset, int whence);
555   void safe_fflush(FILE* stream);
556   void safe_fclose(FILE* stream);
557   void primitive_fopen();
558   FILE* pop_file_handle();
559   FILE* peek_file_handle();
560   void primitive_fgetc();
561   void primitive_fread();
562   void primitive_fputc();
563   void primitive_fwrite();
564   void primitive_ftell();
565   void primitive_fseek();
566   void primitive_fflush();
567   void primitive_fclose();
568
569   // code_block
570   cell compute_entry_point_address(cell obj);
571   cell compute_entry_point_pic_address(word* w, cell tagged_quot);
572   cell compute_entry_point_pic_address(cell w_);
573   cell compute_entry_point_pic_tail_address(cell w_);
574   cell compute_external_address(instruction_operand op);
575
576   cell code_block_owner(code_block* compiled);
577   void update_word_references(code_block* compiled, bool reset_inline_caches);
578   void undefined_symbol();
579   cell compute_dlsym_address(array* literals, cell index, bool toc);
580   cell compute_vm_address(cell arg);
581   cell lookup_external_address(relocation_type rel_type,
582                                code_block* compiled,
583                                array* parameters,
584                                cell index);
585   cell compute_here_address(cell arg, cell offset, code_block* compiled);
586   void initialize_code_block(code_block* compiled, cell literals);
587   void initialize_code_block(code_block* compiled);
588   void fixup_labels(array* labels, code_block* compiled);
589   code_block* allot_code_block(cell size, code_block_type type);
590   code_block* add_code_block(code_block_type type, cell code_, cell labels_,
591                              cell owner_, cell relocation_, cell parameters_,
592                              cell literals_, cell frame_size_untagged);
593
594   //code heap
595   template <typename Iterator> void each_code_block(Iterator& iter) {
596     code->allocator->iterate(iter);
597   }
598
599   void update_code_heap_words(bool reset_inline_caches);
600   void initialize_code_blocks();
601   void primitive_modify_code_heap();
602   void primitive_code_room();
603   void primitive_strip_stack_traces();
604   cell code_blocks();
605   void primitive_code_blocks();
606
607   // callbacks
608   void init_callbacks(cell size);
609   void primitive_free_callback();
610   void primitive_callback();
611   void primitive_callback_room();
612
613   // image
614   void init_objects(image_header* h);
615   void load_data_heap(FILE* file, image_header* h, vm_parameters* p);
616   void load_code_heap(FILE* file, image_header* h, vm_parameters* p);
617   bool save_image(const vm_char* saving_filename, const vm_char* filename);
618   void primitive_save_image();
619   void fixup_data(cell data_offset, cell code_offset);
620   void fixup_code(cell data_offset, cell code_offset);
621   FILE* open_image(vm_parameters* p);
622   void load_image(vm_parameters* p);
623   bool read_embedded_image_footer(FILE* file, embedded_image_footer* footer);
624   bool embedded_image_p();
625
626   template <typename Iterator, typename Fixup>
627   void iterate_callstack_object(callstack* stack_, Iterator& iterator,
628                                 Fixup& fixup);
629   template <typename Iterator>
630   void iterate_callstack_object(callstack* stack_, Iterator& iterator);
631
632   callstack* allot_callstack(cell size);
633   cell second_from_top_stack_frame(context* ctx);
634   cell capture_callstack(context* ctx);
635   void primitive_callstack_for();
636   void primitive_callstack_to_array();
637   void primitive_innermost_stack_frame_executing();
638   void primitive_innermost_stack_frame_scan();
639   void primitive_set_innermost_stack_frame_quotation();
640   void primitive_callstack_bounds();
641
642   template <typename Iterator, typename Fixup>
643   void iterate_callstack(context* ctx, Iterator& iterator, Fixup& fixup);
644   template <typename Iterator>
645   void iterate_callstack(context* ctx, Iterator& iterator);
646
647   // cpu-*
648   void dispatch_signal_handler(cell* sp, cell* pc, cell newpc);
649 #if defined(FACTOR_X86) || defined(FACTOR_64)
650   void dispatch_non_resumable_signal(cell* sp, cell* pc,
651                                      cell handler,
652                                      cell limit);
653   void dispatch_resumable_signal(cell* sp, cell* pc, cell handler);
654 #endif
655
656   // alien
657   char* pinned_alien_offset(cell obj);
658   cell allot_alien(cell delegate_, cell displacement);
659   cell allot_alien(cell address);
660   void primitive_displaced_alien();
661   void primitive_alien_address();
662   void* alien_pointer();
663   void primitive_dlopen();
664   void primitive_dlsym();
665   void primitive_dlsym_raw();
666   void primitive_dlclose();
667   void primitive_dll_validp();
668   char* alien_offset(cell obj);
669
670   // quotations
671   void primitive_jit_compile();
672   cell lazy_jit_compile_entry_point();
673   void primitive_array_to_quotation();
674   void primitive_quotation_code();
675   code_block* jit_compile_quotation(cell owner_, cell quot_, bool relocating);
676   void jit_compile_quotation(cell quot_, bool relocating);
677   fixnum quot_code_offset_to_scan(cell quot_, cell offset);
678   cell lazy_jit_compile(cell quot);
679   bool quotation_compiled_p(quotation* quot);
680   void primitive_quotation_compiled_p();
681   void initialize_all_quotations();
682
683   // dispatch
684   cell search_lookup_alist(cell table, cell klass);
685   cell search_lookup_hash(cell table, cell klass, cell hashcode);
686   cell nth_superclass(tuple_layout* layout, fixnum echelon);
687   cell nth_hashcode(tuple_layout* layout, fixnum echelon);
688   cell lookup_tuple_method(cell obj, cell methods);
689   cell lookup_method(cell obj, cell methods);
690   void primitive_lookup_method();
691   cell object_class(cell obj);
692   cell method_cache_hashcode(cell klass, array* array);
693   void update_method_cache(cell cache, cell klass, cell method);
694   void primitive_mega_cache_miss();
695   void primitive_reset_dispatch_stats();
696   void primitive_dispatch_stats();
697
698   // inline cache
699   void init_inline_caching(int max_size);
700   void deallocate_inline_cache(cell return_address);
701   cell determine_inline_cache_type(array* cache_entries);
702   void update_pic_count(cell type);
703   code_block* compile_inline_cache(fixnum index, cell generic_word_,
704                                    cell methods_, cell cache_entries_,
705                                    bool tail_call_p);
706   cell inline_cache_size(cell cache_entries);
707   cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
708   void update_pic_transitions(cell pic_size);
709   cell inline_cache_miss(cell return_address);
710
711   // entry points
712   void c_to_factor(cell quot);
713   void unwind_native_frames(cell quot, cell to);
714   cell get_fpu_state();
715   void set_fpu_state(cell state);
716
717   // factor
718   void default_parameters(vm_parameters* p);
719   bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
720   void init_parameters_from_args(vm_parameters* p, int argc, vm_char** argv);
721   void prepare_boot_image();
722   void init_factor(vm_parameters* p);
723   void pass_args_to_factor(int argc, vm_char** argv);
724   void start_factor(vm_parameters* p);
725   void stop_factor();
726   void start_embedded_factor(vm_parameters* p);
727   void start_standalone_factor(int argc, vm_char** argv);
728   char* factor_eval_string(char* string);
729   void factor_eval_free(char* result);
730   void factor_yield();
731   void factor_sleep(long us);
732
733   // os-*
734   void primitive_existsp();
735   void move_file(const vm_char* path1, const vm_char* path2);
736   void init_ffi();
737   void ffi_dlopen(dll* dll);
738   cell ffi_dlsym(dll* dll, symbol_char* symbol);
739   cell ffi_dlsym_raw(dll* dll, symbol_char* symbol);
740   void ffi_dlclose(dll* dll);
741   void c_to_factor_toplevel(cell quot);
742   void init_signals();
743   void start_sampling_profiler_timer();
744   void end_sampling_profiler_timer();
745
746 // os-windows
747 #if defined(WINDOWS)
748   HANDLE sampler_thread;
749   void sampler_thread_loop();
750
751   const vm_char* vm_executable_path();
752   const vm_char* default_image_path();
753   BOOL windows_stat(vm_char* path);
754
755   LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
756                          void* dispatch);
757
758 #else  // UNIX
759   void dispatch_signal(void* uap, void(handler)());
760   void unix_init_signals();
761 #endif
762
763 #ifdef __APPLE__
764   void call_fault_handler(exception_type_t exception,
765                           exception_data_type_t code,
766                           MACH_EXC_STATE_TYPE* exc_state,
767                           MACH_THREAD_STATE_TYPE* thread_state,
768                           MACH_FLOAT_STATE_TYPE* float_state);
769 #endif
770
771   factor_vm(THREADHANDLE thread_id);
772   ~factor_vm();
773 };
774
775 }