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