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