]> gitweb.factorcode.org Git - factor.git/blob - vm/vm.hpp
VM: prettier and more detailed output when dumping generations
[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 divide_by_zero_error();
219   void primitive_unimplemented();
220   void memory_signal_handler_impl();
221   void synchronous_signal_handler_impl();
222   void fp_signal_handler_impl();
223
224   // bignum
225   int bignum_equal_p(bignum* x, bignum* y);
226   enum bignum_comparison bignum_compare(bignum* x, bignum* y);
227   bignum* bignum_add(bignum* x, bignum* y);
228   bignum* bignum_subtract(bignum* x, bignum* y);
229   bignum* bignum_square(bignum* x_);
230   bignum* bignum_multiply(bignum* x, bignum* y);
231   void bignum_divide(bignum* numerator, bignum* denominator, bignum** quotient,
232                      bignum** remainder);
233   bignum* bignum_quotient(bignum* numerator, bignum* denominator);
234   bignum* bignum_remainder(bignum* numerator, bignum* denominator);
235   fixnum bignum_to_fixnum_strict(bignum* bn);
236   bignum* double_to_bignum(double x);
237   int bignum_equal_p_unsigned(bignum* x, bignum* y);
238   enum bignum_comparison bignum_compare_unsigned(bignum* x, bignum* y);
239   bignum* bignum_add_unsigned(bignum* x_, bignum* y_, int negative_p);
240   bignum* bignum_subtract_unsigned(bignum* x_, bignum* y_);
241   bignum* bignum_multiply_unsigned(bignum* x_, bignum* y_, int negative_p);
242   bignum* bignum_multiply_unsigned_small_factor(bignum* x, bignum_digit_type y,
243                                                 int negative_p);
244   void bignum_destructive_add(bignum* bn, bignum_digit_type n);
245   void bignum_destructive_scale_up(bignum* bn, bignum_digit_type factor);
246   void bignum_divide_unsigned_large_denominator(
247       bignum* numerator_, bignum* denominator_, bignum** quotient,
248       bignum** remainder, int q_negative_p, int r_negative_p);
249   void bignum_divide_unsigned_normalized(bignum* u, bignum* v, bignum* q);
250   bignum_digit_type bignum_divide_subtract(bignum_digit_type* v_start,
251                                            bignum_digit_type* v_end,
252                                            bignum_digit_type guess,
253                                            bignum_digit_type* u_start);
254   void bignum_divide_unsigned_medium_denominator(
255       bignum* numerator_, bignum_digit_type denominator, bignum** quotient,
256       bignum** remainder, int q_negative_p, int r_negative_p);
257   void bignum_destructive_normalization(bignum* source, bignum* target,
258                                         int shift_left);
259   void bignum_destructive_unnormalization(bignum* bn, int shift_right);
260   bignum_digit_type bignum_digit_divide(
261       bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v,
262       bignum_digit_type* q) /* return value */;
263   bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1,
264                                                  bignum_digit_type v2,
265                                                  bignum_digit_type guess,
266                                                  bignum_digit_type* u);
267   void bignum_divide_unsigned_small_denominator(
268       bignum* numerator_, bignum_digit_type denominator, bignum** quotient,
269       bignum** remainder, int q_negative_p, int r_negative_p);
270   bignum_digit_type bignum_destructive_scale_down(
271       bignum* bn, bignum_digit_type denominator);
272   bignum* bignum_remainder_unsigned_small_denominator(bignum* n,
273                                                       bignum_digit_type d,
274                                                       int negative_p);
275   bignum* bignum_digit_to_bignum(bignum_digit_type digit, int negative_p);
276   bignum* allot_bignum(bignum_length_type length, int negative_p);
277   bignum* allot_bignum_zeroed(bignum_length_type length, int negative_p);
278   bignum* bignum_shorten_length(bignum* bn, bignum_length_type length);
279   bignum* bignum_trim(bignum* bn);
280   bignum* bignum_new_sign(bignum* x_, int negative_p);
281   bignum* bignum_maybe_new_sign(bignum* x_, int negative_p);
282   void bignum_destructive_copy(bignum* source, bignum* target);
283   bignum* bignum_bitwise_not(bignum* x_);
284   bignum* bignum_arithmetic_shift(bignum* arg1, fixnum n);
285   bignum* bignum_bitwise_and(bignum* arg1, bignum* arg2);
286   bignum* bignum_bitwise_ior(bignum* arg1, bignum* arg2);
287   bignum* bignum_bitwise_xor(bignum* arg1, bignum* arg2);
288   bignum* bignum_magnitude_ash(bignum* arg1_, fixnum n);
289   bignum* bignum_pospos_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
290   bignum* bignum_posneg_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
291   bignum* bignum_negneg_bitwise_op(int op, bignum* arg1_, bignum* arg2_);
292   void bignum_negate_magnitude(bignum* arg);
293   bignum* bignum_integer_length(bignum* x_);
294   int bignum_logbitp(int shift, bignum* arg);
295   int bignum_unsigned_logbitp(int shift, bignum* bn);
296   bignum* bignum_gcd(bignum* a_, bignum* b_);
297
298   //data heap
299   void init_card_decks();
300   void set_data_heap(data_heap* data_);
301   void init_data_heap(cell young_size, cell aging_size, cell tenured_size);
302   void primitive_size();
303   data_heap_room data_room();
304   void primitive_data_room();
305   void begin_scan();
306   void end_scan();
307   cell instances(cell type);
308   void primitive_all_instances();
309
310   template <typename Generation, typename Iterator>
311   inline void each_object(Generation* gen, Iterator& iterator) {
312     cell obj = gen->first_object();
313     while (obj) {
314       iterator((object*)obj);
315       obj = gen->next_object_after(obj);
316     }
317   }
318
319   template <typename Iterator> inline void each_object(Iterator& iterator) {
320
321     /* The nursery can't be iterated because there may be gaps between
322        the objects (see factor_vm::reallot_array) so we require it to
323        be empty first. */
324     FACTOR_ASSERT(data->nursery->occupied_space() == 0);
325
326     gc_off = true;
327     each_object(data->tenured, iterator);
328     each_object(data->aging, iterator);
329     gc_off = false;
330   }
331
332   template <typename Iterator>
333   inline void each_object_each_slot(Iterator& iterator) {
334     auto each_object_func = [&](object* obj) {
335       auto each_slot_func = [&](cell* slot) {
336         iterator(obj, slot);
337       };
338       obj->each_slot(each_slot_func);
339     };
340     each_object(each_object_func);
341   }
342
343   /* the write barrier must be called any time we are potentially storing a
344      pointer from an older generation to a younger one */
345   inline void write_barrier(cell* slot_ptr) {
346     *(char*)(cards_offset + ((cell)slot_ptr >> card_bits)) = card_mark_mask;
347     *(char*)(decks_offset + ((cell)slot_ptr >> deck_bits)) = card_mark_mask;
348   }
349
350   inline void write_barrier(object* obj, cell size) {
351     cell start = (cell)obj & (~card_size + 1);
352     cell end = ((cell)obj + size + card_size - 1) & (~card_size + 1);
353
354     for (cell offset = start; offset < end; offset += card_size)
355       write_barrier((cell*)offset);
356   }
357
358   // data heap checker
359   void check_data_heap();
360
361   // gc
362   void end_gc();
363   void set_current_gc_op(gc_op op);
364   void start_gc_again();
365   void collect_nursery();
366   void collect_aging();
367   void collect_to_tenured();
368   void update_code_roots_for_sweep();
369   void update_code_roots_for_compaction();
370   void collect_mark_impl();
371   void collect_sweep_impl();
372   void collect_full();
373   void collect_compact_impl();
374   void collect_compact();
375   void collect_growing_heap(cell requested_size);
376   void gc(gc_op op, cell requested_size);
377   void primitive_minor_gc();
378   void primitive_full_gc();
379   void primitive_compact_gc();
380   void primitive_enable_gc_events();
381   void primitive_disable_gc_events();
382   object* allot_object(cell type, cell size);
383   object* allot_large_object(cell type, cell size);
384
385   /* Allocates memory */
386   template <typename Type> Type* allot(cell size) {
387     return (Type*)allot_object(Type::type_number, size);
388   }
389
390   // generic arrays
391   template <typename Array> Array* allot_uninitialized_array(cell capacity);
392   template <typename Array>
393   bool reallot_array_in_place_p(Array* array, cell capacity);
394   template <typename Array> Array* reallot_array(Array* array_, cell capacity);
395
396   // debug
397   void print_chars(ostream& out, string* str);
398   void print_word(ostream& out, word* word, cell nesting);
399   void print_factor_string(ostream& out, string* str);
400   void print_array(ostream& out, array* array, cell nesting);
401   void print_byte_array(ostream& out, byte_array* array, cell nesting);
402   void print_tuple(ostream& out, tuple* tuple, cell nesting);
403   void print_alien(ostream& out, alien* alien, cell nesting);
404   void print_nested_obj(ostream& out, cell obj, fixnum nesting);
405   void print_obj(ostream& out, cell obj);
406   void print_objects(ostream& out, cell* start, cell* end);
407   void print_datastack(ostream& out);
408   void print_retainstack(ostream& out);
409   void print_callstack(ostream& out);
410   void print_callstack_object(ostream& out, callstack* obj);
411   void dump_cell(ostream& out, cell x);
412   void dump_memory(ostream& out, cell from, cell to);
413   void dump_memory_layout(ostream& out);
414   void dump_objects(ostream& out, cell type);
415   void dump_edges(ostream& out);
416   void find_data_references(ostream& out, cell look_for_);
417   void dump_code_heap(ostream& out);
418   void factorbug_usage(bool advanced_p);
419   void factorbug();
420   void primitive_die();
421
422   // arrays
423   inline void set_array_nth(array* array, cell slot, cell value);
424   array* allot_array(cell capacity, cell fill_);
425   void primitive_array();
426   cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_);
427   void primitive_resize_array();
428   cell std_vector_to_array(std::vector<cell>& elements);
429
430   // strings
431   string* allot_string_internal(cell capacity);
432   void fill_string(string* str_, cell start, cell capacity, cell fill);
433   string* allot_string(cell capacity, cell fill);
434   void primitive_string();
435   bool reallot_string_in_place_p(string* str, cell capacity);
436   string* reallot_string(string* str_, cell capacity);
437   void primitive_resize_string();
438   void primitive_set_string_nth_fast();
439
440   // booleans
441   cell tag_boolean(cell untagged) {
442     return (untagged ? true_object : false_object);
443   }
444
445   // byte arrays
446   byte_array* allot_byte_array(cell size);
447   void primitive_byte_array();
448   void primitive_uninitialized_byte_array();
449   void primitive_resize_byte_array();
450
451   template <typename Type> byte_array* byte_array_from_value(Type* value);
452
453   // tuples
454   void primitive_tuple();
455   void primitive_tuple_boa();
456
457   // words
458   word* allot_word(cell name_, cell vocab_, cell hashcode_);
459   void primitive_word();
460   void primitive_word_code();
461   void primitive_word_optimized_p();
462   void primitive_wrapper();
463   void jit_compile_word(cell word_, cell def_, bool relocating);
464   cell find_all_words();
465   void compile_all_words();
466
467   // math
468   void primitive_bignum_to_fixnum();
469   void primitive_bignum_to_fixnum_strict();
470   void primitive_float_to_fixnum();
471   void primitive_fixnum_divint();
472   void primitive_fixnum_divmod();
473   bignum* fixnum_to_bignum(fixnum);
474   bignum* cell_to_bignum(cell);
475   bignum* long_long_to_bignum(int64_t n);
476   bignum* ulong_long_to_bignum(uint64_t n);
477   inline fixnum sign_mask(fixnum x);
478   inline fixnum branchless_max(fixnum x, fixnum y);
479   inline fixnum branchless_abs(fixnum x);
480   void primitive_fixnum_shift();
481   void primitive_fixnum_to_bignum();
482   void primitive_float_to_bignum();
483   void primitive_bignum_eq();
484   void primitive_bignum_add();
485   void primitive_bignum_subtract();
486   void primitive_bignum_multiply();
487   void primitive_bignum_divint();
488   void primitive_bignum_divmod();
489   void primitive_bignum_mod();
490   void primitive_bignum_gcd();
491   void primitive_bignum_and();
492   void primitive_bignum_or();
493   void primitive_bignum_xor();
494   void primitive_bignum_shift();
495   void primitive_bignum_less();
496   void primitive_bignum_lesseq();
497   void primitive_bignum_greater();
498   void primitive_bignum_greatereq();
499   void primitive_bignum_not();
500   void primitive_bignum_bitp();
501   void primitive_bignum_log2();
502   inline cell unbox_array_size();
503   void primitive_fixnum_to_float();
504   void primitive_format_float();
505   void primitive_float_eq();
506   void primitive_float_add();
507   void primitive_float_subtract();
508   void primitive_float_multiply();
509   void primitive_float_divfloat();
510   void primitive_float_less();
511   void primitive_float_lesseq();
512   void primitive_float_greater();
513   void primitive_float_greatereq();
514   void primitive_float_bits();
515   void primitive_bits_float();
516   void primitive_double_bits();
517   void primitive_bits_double();
518   fixnum to_fixnum(cell tagged);
519   fixnum to_fixnum_strict(cell tagged);
520   cell to_cell(cell tagged);
521   cell from_signed_8(int64_t n);
522   int64_t to_signed_8(cell obj);
523   cell from_unsigned_8(uint64_t n);
524   uint64_t to_unsigned_8(cell obj);
525   float to_float(cell value);
526   double to_double(cell value);
527   inline void overflow_fixnum_add(fixnum x, fixnum y);
528   inline void overflow_fixnum_subtract(fixnum x, fixnum y);
529   inline void overflow_fixnum_multiply(fixnum x, fixnum y);
530   inline cell from_signed_cell(fixnum x);
531   inline cell from_unsigned_cell(cell x);
532   inline cell allot_float(double n);
533   inline bignum* float_to_bignum(cell tagged);
534   inline double untag_float(cell tagged);
535   inline double untag_float_check(cell tagged);
536   inline fixnum float_to_fixnum(cell tagged);
537   inline double fixnum_to_float(cell tagged);
538
539   // tagged
540   template <typename Type> Type* untag_check(cell value);
541
542   // io
543   void init_c_io();
544   void io_error_if_not_EINTR();
545   FILE* safe_fopen(char* filename, const char* mode);
546   int safe_fgetc(FILE* stream);
547   size_t safe_fread(void* ptr, size_t size, size_t nitems, FILE* stream);
548   void safe_fputc(int c, FILE* stream);
549   size_t safe_fwrite(void* ptr, size_t size, size_t nitems, FILE* stream);
550   int safe_ftell(FILE* stream);
551   void safe_fseek(FILE* stream, off_t offset, int whence);
552   void safe_fflush(FILE* stream);
553   void primitive_fopen();
554   FILE* pop_file_handle();
555   FILE* peek_file_handle();
556   void primitive_fgetc();
557   void primitive_fread();
558   void primitive_fputc();
559   void primitive_fwrite();
560   void primitive_ftell();
561   void primitive_fseek();
562   void primitive_fflush();
563   void primitive_fclose();
564
565   // code_block
566   cell compute_entry_point_address(cell obj);
567   cell compute_entry_point_pic_address(word* w, cell tagged_quot);
568   cell compute_entry_point_pic_address(cell w_);
569   cell compute_entry_point_pic_tail_address(cell w_);
570   cell compute_external_address(instruction_operand op);
571
572   cell code_block_owner(code_block* compiled);
573   void update_word_references(code_block* compiled, bool reset_inline_caches);
574   void undefined_symbol();
575   cell compute_dlsym_address(array* literals, cell index, bool toc);
576   cell compute_vm_address(cell arg);
577   cell lookup_external_address(relocation_type rel_type,
578                                code_block* compiled,
579                                array* parameters,
580                                cell index);
581   cell compute_here_address(cell arg, cell offset, code_block* compiled);
582   void initialize_code_block(code_block* compiled, cell literals);
583   void initialize_code_block(code_block* compiled);
584   void fixup_labels(array* labels, code_block* compiled);
585   code_block* allot_code_block(cell size, code_block_type type);
586   code_block* add_code_block(code_block_type type, cell code_, cell labels_,
587                              cell owner_, cell relocation_, cell parameters_,
588                              cell literals_, cell frame_size_untagged);
589
590   //code heap
591   template <typename Iterator> void each_code_block(Iterator& iter) {
592     code->allocator->iterate(iter);
593   }
594
595   void update_code_heap_words(bool reset_inline_caches);
596   void initialize_code_blocks();
597   void primitive_modify_code_heap();
598   void primitive_code_room();
599   void primitive_strip_stack_traces();
600   cell code_blocks();
601   void primitive_code_blocks();
602
603   // callbacks
604   void init_callbacks(cell size);
605   void primitive_free_callback();
606   void primitive_callback();
607   void primitive_callback_room();
608
609   // image
610   void init_objects(image_header* h);
611   void load_data_heap(FILE* file, image_header* h, vm_parameters* p);
612   void load_code_heap(FILE* file, image_header* h, vm_parameters* p);
613   bool save_image(const vm_char* saving_filename, const vm_char* filename);
614   void primitive_save_image();
615   void fixup_data(cell data_offset, cell code_offset);
616   void fixup_code(cell data_offset, cell code_offset);
617   FILE* open_image(vm_parameters* p);
618   void load_image(vm_parameters* p);
619   bool read_embedded_image_footer(FILE* file, embedded_image_footer* footer);
620   bool embedded_image_p();
621
622   template <typename Iterator, typename Fixup>
623   void iterate_callstack_object(callstack* stack_, Iterator& iterator,
624                                 Fixup& fixup);
625   template <typename Iterator>
626   void iterate_callstack_object(callstack* stack_, Iterator& iterator);
627
628   callstack* allot_callstack(cell size);
629   cell second_from_top_stack_frame(context* ctx);
630   cell capture_callstack(context* ctx);
631   void primitive_callstack_for();
632   void primitive_callstack_to_array();
633   void primitive_innermost_stack_frame_executing();
634   void primitive_innermost_stack_frame_scan();
635   void primitive_set_innermost_stack_frame_quotation();
636   void primitive_callstack_bounds();
637
638   template <typename Iterator, typename Fixup>
639   void iterate_callstack(context* ctx, Iterator& iterator, Fixup& fixup);
640   template <typename Iterator>
641   void iterate_callstack(context* ctx, Iterator& iterator);
642
643   // cpu-*
644   void dispatch_signal_handler(cell* sp, cell* pc, cell newpc);
645 #if defined(FACTOR_X86) || defined(FACTOR_64)
646   void dispatch_non_resumable_signal(cell* sp, cell* pc,
647                                      cell handler,
648                                      cell limit);
649   void dispatch_resumable_signal(cell* sp, cell* pc, cell handler);
650 #endif
651
652   // alien
653   char* pinned_alien_offset(cell obj);
654   cell allot_alien(cell delegate_, cell displacement);
655   cell allot_alien(cell address);
656   void primitive_displaced_alien();
657   void primitive_alien_address();
658   void* alien_pointer();
659   void primitive_dlopen();
660   void primitive_dlsym();
661   void primitive_dlsym_raw();
662   void primitive_dlclose();
663   void primitive_dll_validp();
664   char* alien_offset(cell obj);
665
666   // quotations
667   void primitive_jit_compile();
668   cell lazy_jit_compile_entry_point();
669   void primitive_array_to_quotation();
670   void primitive_quotation_code();
671   code_block* jit_compile_quotation(cell owner_, cell quot_, bool relocating);
672   void jit_compile_quotation(cell quot_, bool relocating);
673   fixnum quot_code_offset_to_scan(cell quot_, cell offset);
674   cell lazy_jit_compile(cell quot);
675   bool quotation_compiled_p(quotation* quot);
676   void primitive_quotation_compiled_p();
677   void initialize_all_quotations();
678
679   // dispatch
680   cell search_lookup_alist(cell table, cell klass);
681   cell search_lookup_hash(cell table, cell klass, cell hashcode);
682   cell nth_superclass(tuple_layout* layout, fixnum echelon);
683   cell nth_hashcode(tuple_layout* layout, fixnum echelon);
684   cell lookup_tuple_method(cell obj, cell methods);
685   cell lookup_method(cell obj, cell methods);
686   void primitive_lookup_method();
687   cell object_class(cell obj);
688   cell method_cache_hashcode(cell klass, array* array);
689   void update_method_cache(cell cache, cell klass, cell method);
690   void primitive_mega_cache_miss();
691   void primitive_reset_dispatch_stats();
692   void primitive_dispatch_stats();
693
694   // inline cache
695   void init_inline_caching(int max_size);
696   void deallocate_inline_cache(cell return_address);
697   cell determine_inline_cache_type(array* cache_entries);
698   void update_pic_count(cell type);
699   code_block* compile_inline_cache(fixnum index, cell generic_word_,
700                                    cell methods_, cell cache_entries_,
701                                    bool tail_call_p);
702   cell inline_cache_size(cell cache_entries);
703   cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
704   void update_pic_transitions(cell pic_size);
705   cell inline_cache_miss(cell return_address);
706
707   // entry points
708   void c_to_factor(cell quot);
709   void unwind_native_frames(cell quot, cell to);
710   cell get_fpu_state();
711   void set_fpu_state(cell state);
712
713   // factor
714   void default_parameters(vm_parameters* p);
715   bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
716   void init_parameters_from_args(vm_parameters* p, int argc, vm_char** argv);
717   void prepare_boot_image();
718   void init_factor(vm_parameters* p);
719   void pass_args_to_factor(int argc, vm_char** argv);
720   void start_factor(vm_parameters* p);
721   void stop_factor();
722   void start_embedded_factor(vm_parameters* p);
723   void start_standalone_factor(int argc, vm_char** argv);
724   char* factor_eval_string(char* string);
725   void factor_eval_free(char* result);
726   void factor_yield();
727   void factor_sleep(long us);
728
729   // os-*
730   void primitive_existsp();
731   void init_ffi();
732   void ffi_dlopen(dll* dll);
733   cell ffi_dlsym(dll* dll, symbol_char* symbol);
734   cell ffi_dlsym_raw(dll* dll, symbol_char* symbol);
735   void ffi_dlclose(dll* dll);
736   void c_to_factor_toplevel(cell quot);
737   void init_signals();
738   void start_sampling_profiler_timer();
739   void end_sampling_profiler_timer();
740
741 // os-windows
742 #if defined(WINDOWS)
743   HANDLE sampler_thread;
744   void sampler_thread_loop();
745
746   const vm_char* vm_executable_path();
747   const vm_char* default_image_path();
748   BOOL windows_stat(vm_char* path);
749
750   LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
751                          void* dispatch);
752
753 #else  // UNIX
754   void dispatch_signal(void* uap, void(handler)());
755   void unix_init_signals();
756 #endif
757
758 #ifdef __APPLE__
759   void call_fault_handler(exception_type_t exception,
760                           exception_data_type_t code,
761                           MACH_EXC_STATE_TYPE* exc_state,
762                           MACH_THREAD_STATE_TYPE* thread_state,
763                           MACH_FLOAT_STATE_TYPE* float_state);
764 #endif
765
766   factor_vm(THREADHANDLE thread_id);
767   ~factor_vm();
768 };
769
770 }