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