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