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