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