]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/vm.hpp
webapps.wiki: adding search bar
[factor.git] / vm / vm.hpp
index ac9153fb29081cd9451ae6fcd31bf36f3bdf574c..4bacc4c077322eac297a761837c6ae5b6bf421bc 100644 (file)
--- a/vm/vm.hpp
+++ b/vm/vm.hpp
@@ -17,152 +17,144 @@ struct factor_vm {
   //   basis/vm/vm.factor
   //   basis/compiler/constants/constants.factor
 
-  /* Current context */
+  // Current context
   context* ctx;
 
-  /* Spare context -- for callbacks */
+  // Spare context -- for callbacks
   context* spare_ctx;
 
-  /* New objects are allocated here, use the data->nursery reference
-     instead from c++ code. */
+  // New objects are allocated here, use the data->nursery reference
+  // instead from c++ code.
   bump_allocator nursery;
 
-  /* Add this to a shifted address to compute write barrier offsets */
+  // Add this to a shifted address to compute write barrier offsets
   cell cards_offset;
   cell decks_offset;
 
-  /* cdecl signal handler address, used by signal handler subprimitives */
+  // cdecl signal handler address, used by signal handler subprimitives
   cell signal_handler_addr;
 
-  /* are we handling a memory error? used to detect double faults */
-  cell faulting_p;
+  // are we handling a memory error? used to detect double faults
+  bool faulting_p;
 
-  /* Various special objects, accessed by special-object and
-       set-special-object primitives */
+  // Various special objects, accessed by special-object and
+  // set-special-object primitives
   cell special_objects[special_object_count];
 
   // THESE FIELDS ARE ACCESSED DIRECTLY FROM FACTOR.
   // ^^^^^^
   //
 
-  /* Handle to the main thread we run in */
+  // Handle to the main thread we run in
   THREADHANDLE thread;
 
-  /* Data stack and retain stack sizes */
+  // Data stack and retain stack sizes
   cell datastack_size, retainstack_size, callstack_size;
 
-  /* Stack of callback IDs */
+  // Stack of callback IDs
   std::vector<int> callback_ids;
 
-  /* Next callback ID */
+  // Next callback ID
   int callback_id;
 
-  /* List of callback function descriptors for PPC */
+  // List of callback function descriptors for PPC
   std::list<void**> function_descriptors;
 
-  /* Pooling unused contexts to make context allocation cheaper */
+  // Pooling unused contexts to make context allocation cheaper
   std::list<context*> unused_contexts;
 
-  /* Active contexts, for tracing by the GC */
+  // Active contexts, for tracing by the GC
   std::set<context*> active_contexts;
 
-  /* Canonical truth value. In Factor, 't' */
-  cell true_object;
-
-  /* External entry points */
+  // External entry points
   c_to_factor_func_type c_to_factor_func;
 
-  /* Is profiling enabled? */
-  volatile cell sampling_profiler_p;
+  // Is profiling enabled?
+  volatile bool sampling_profiler_p;
   fixnum samples_per_second;
 
-  /* Global variables used to pass fault handler state from signal handler
-     to VM */
+  // Global variables used to pass fault handler state from signal handler
+  // to VM
   bool signal_resumable;
   cell signal_number;
   cell signal_fault_addr;
   cell signal_fault_pc;
   unsigned int signal_fpu_status;
 
-  /* Pipe used to notify Factor multiplexer of signals */
+  // Pipe used to notify Factor multiplexer of signals
   int signal_pipe_input, signal_pipe_output;
 
-  /* State kept by the sampling profiler */
+  // State kept by the sampling profiler
   std::vector<profiling_sample> samples;
-  std::vector<cell> sample_callstacks;
+  volatile profiling_sample current_sample;
 
-  /* GC is off during heap walking */
+  // GC is off during heap walking
   bool gc_off;
 
-  /* Data heap */
+  // Data heap
   data_heap* data;
 
-  /* Code heap */
+  // Code heap
   code_heap* code;
 
-  /* Pinned callback stubs */
+  // Pinned callback stubs
   callback_heap* callbacks;
 
-  /* Only set if we're performing a GC */
+  // Only set if we're performing a GC
   gc_state* current_gc;
-  volatile cell current_gc_p;
+  volatile bool current_gc_p;
 
-  /* Set if we're in the jit */
+  // Set if we're in the jit
   volatile fixnum current_jit_count;
 
-  /* Mark stack */
+  // Mark stack used for mark & sweep GC
   std::vector<cell> mark_stack;
 
-  /* If not NULL, we push GC events here */
+  // If not NULL, we push GC events here
   std::vector<gc_event>* gc_events;
 
-  /* If a runtime function needs to call another function which potentially
-     allocates memory, it must wrap any references to the data and code
-     heaps with data_root and code_root smart pointers, which register
-     themselves here. See data_roots.hpp and code_roots.hpp */
+  // If a runtime function needs to call another function which potentially
+  // allocates memory, it must wrap any references to the data and code
+  // heaps with data_root and code_root smart pointers, which register
+  // themselves here. See data_roots.hpp and code_roots.hpp
 
   std::vector<cell*> data_roots;
   std::vector<code_root*> code_roots;
 
-  /* Debugger */
+  // Debugger
   bool fep_p;
   bool fep_help_was_shown;
   bool fep_disabled;
   bool full_output;
 
-  /* Canonical bignums */
-  cell bignum_zero;
-  cell bignum_pos_one;
-  cell bignum_neg_one;
-
-  /* Method dispatch statistics */
+  // Method dispatch statistics
   dispatch_statistics dispatch_stats;
 
-  /* Number of entries in a polymorphic inline cache */
+  // Number of entries in a polymorphic inline cache
   cell max_pic_size;
 
-  /* Incrementing object counter for identity hashing */
+  // Incrementing object counter for identity hashing
   cell object_counter;
 
-  /* Sanity check to ensure that monotonic counter doesn't decrease */
+  // Sanity check to ensure that monotonic counter doesn't decrease
   uint64_t last_nano_count;
 
-  /* Stack for signal handlers, only used on Unix */
+  // Stack for signal handlers, only used on Unix
   segment* signal_callstack_seg;
 
-  /* Are we already handling a fault? Used to catch double memory faults */
+  // Are we already handling a fault? Used to catch double memory faults
   static bool fatal_erroring_p;
 
-  /* Safepoint state */
-  volatile safepoint_state safepoint;
+  // Two fep_p variants, one might be redundant.
+  volatile bool safepoint_fep_p;
+
+  // Allow Ctrl-Break a busy loop in the Listener, only used on Windows
+  volatile bool stop_on_ctrl_break;
 
   // contexts
   context* new_context();
   void init_context(context* ctx);
   void delete_context();
-  void init_contexts(cell datastack_size_, cell retainstack_size_,
-                     cell callstack_size_);
-  void delete_contexts();
   cell begin_callback(cell quot);
   void end_callback();
   void primitive_current_callback();
@@ -171,15 +163,10 @@ struct factor_vm {
   void primitive_set_context_object();
   cell stack_to_array(cell bottom, cell top, vm_error_type error);
   cell datastack_to_array(context* ctx);
-  void primitive_datastack();
   void primitive_datastack_for();
   cell retainstack_to_array(context* ctx);
-  void primitive_retainstack();
   void primitive_retainstack_for();
-  cell array_to_stack(array* array, cell bottom);
-  void set_datastack(context* ctx, array* array);
   void primitive_set_datastack();
-  void set_retainstack(context* ctx, array* array);
   void primitive_set_retainstack();
   void primitive_check_datastack();
   void primitive_load_locals();
@@ -194,35 +181,25 @@ struct factor_vm {
   void primitive_special_object();
   void primitive_set_special_object();
   void primitive_identity_hashcode();
-  void compute_identity_hashcode(object* obj);
   void primitive_compute_identity_hashcode();
-  cell object_size(cell tagged);
-  cell clone_object(cell obj_);
   void primitive_clone();
   void primitive_become();
 
   // sampling_profiler
-  void clear_samples();
   void record_sample(bool prolog_p);
-  void record_callstack_sample(cell* begin, cell* end, bool prolog_p);
   void start_sampling_profiler(fixnum rate);
   void end_sampling_profiler();
-  void set_sampling_profiler(fixnum rate);
-  void primitive_sampling_profiler();
+  void set_profiling(fixnum rate);
+  void primitive_set_profiling();
   void primitive_get_samples();
-  void primitive_clear_samples();
+  array* allot_growarr();
+  void growarr_add(array *growarr_, cell value);
 
   // errors
   void general_error(vm_error_type error, cell arg1, cell arg2);
   void type_error(cell type, cell tagged);
-  void not_implemented_error();
-  void verify_memory_protection_error(cell addr);
-  void memory_protection_error(cell pc, cell addr);
+  void set_memory_protection_error(cell fault_addr, cell fault_pc);
   void divide_by_zero_error();
-  void primitive_unimplemented();
-  void memory_signal_handler_impl();
-  void synchronous_signal_handler_impl();
-  void fp_signal_handler_impl();
 
   // bignum
   int bignum_equal_p(bignum* x, bignum* y);
@@ -262,7 +239,7 @@ struct factor_vm {
   void bignum_destructive_unnormalization(bignum* bn, int shift_right);
   bignum_digit_type bignum_digit_divide(
       bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v,
-      bignum_digit_type* q) /* return value */;
+      bignum_digit_type* q); // return value
   bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1,
                                                  bignum_digit_type v2,
                                                  bignum_digit_type guess,
@@ -299,14 +276,10 @@ struct factor_vm {
   bignum* bignum_gcd(bignum* a_, bignum* b_);
 
   //data heap
-  void init_card_decks();
   void set_data_heap(data_heap* data_);
-  void init_data_heap(cell young_size, cell aging_size, cell tenured_size);
   void primitive_size();
   data_heap_room data_room();
   void primitive_data_room();
-  void begin_scan();
-  void end_scan();
   cell instances(cell type);
   void primitive_all_instances();
 
@@ -321,9 +294,9 @@ struct factor_vm {
 
   template <typename Iterator> inline void each_object(Iterator& iterator) {
 
-    /* The nursery can't be iterated because there may be gaps between
-       the objects (see factor_vm::reallot_array) so we require it to
-       be empty first. */
+    // The nursery can't be iterated because there may be gaps between
+    // the objects (see factor_vm::reallot_array) so we require it to
+    // be empty first.
     FACTOR_ASSERT(data->nursery->occupied_space() == 0);
 
     gc_off = true;
@@ -332,11 +305,22 @@ struct factor_vm {
     gc_off = false;
   }
 
-  /* the write barrier must be called any time we are potentially storing a
-     pointer from an older generation to a younger one */
+  template <typename Iterator>
+  inline void each_object_each_slot(Iterator& iterator) {
+    auto each_object_func = [&](object* obj) {
+      auto each_slot_func = [&](cell* slot) {
+        iterator(obj, slot);
+      };
+      obj->each_slot(each_slot_func);
+    };
+    each_object(each_object_func);
+  }
+
+  // the write barrier must be called any time we are potentially storing a
+  // pointer from an older generation to a younger one
   inline void write_barrier(cell* slot_ptr) {
-    *(char*)(cards_offset + ((cell)slot_ptr >> card_bits)) = card_mark_mask;
-    *(char*)(decks_offset + ((cell)slot_ptr >> deck_bits)) = card_mark_mask;
+    *(unsigned char*)(cards_offset + ((cell)slot_ptr >> card_bits)) = card_mark_mask;
+    *(unsigned char*)(decks_offset + ((cell)slot_ptr >> deck_bits)) = card_mark_mask;
   }
 
   inline void write_barrier(object* obj, cell size) {
@@ -351,20 +335,18 @@ struct factor_vm {
   void check_data_heap();
 
   // gc
-  void end_gc();
   void set_current_gc_op(gc_op op);
   void start_gc_again();
   void collect_nursery();
   void collect_aging();
   void collect_to_tenured();
-  void update_code_roots_for_sweep();
   void update_code_roots_for_compaction();
   void collect_mark_impl();
   void collect_sweep_impl();
   void collect_full();
   void collect_compact_impl();
   void collect_compact();
-  void collect_growing_heap(cell requested_size);
+  void collect_growing_data_heap(cell requested_size);
   void gc(gc_op op, cell requested_size);
   void primitive_minor_gc();
   void primitive_full_gc();
@@ -374,7 +356,7 @@ struct factor_vm {
   object* allot_object(cell type, cell size);
   object* allot_large_object(cell type, cell size);
 
-  /* Allocates memory */
+  // Allocates memory
   template <typename Type> Type* allot(cell size) {
     return (Type*)allot_object(Type::type_number, size);
   }
@@ -402,9 +384,7 @@ struct factor_vm {
   void print_callstack_object(ostream& out, callstack* obj);
   void dump_cell(ostream& out, cell x);
   void dump_memory(ostream& out, cell from, cell to);
-  template <typename Generation>
-  void dump_generation(ostream& out, const char* name, Generation* gen);
-  void dump_generations(ostream& out);
+  void dump_memory_layout(ostream& out);
   void dump_objects(ostream& out, cell type);
   void dump_edges(ostream& out);
   void find_data_references(ostream& out, cell look_for_);
@@ -412,6 +392,8 @@ struct factor_vm {
   void factorbug_usage(bool advanced_p);
   void factorbug();
   void primitive_die();
+  void primitive_enable_ctrl_break();
+  void primitive_disable_ctrl_break();
 
   // arrays
   inline void set_array_nth(array* array, cell slot, cell value);
@@ -433,7 +415,7 @@ struct factor_vm {
 
   // booleans
   cell tag_boolean(cell untagged) {
-    return (untagged ? true_object : false_object);
+    return untagged ? special_objects[OBJ_CANONICAL_TRUE] : false_object;
   }
 
   // byte arrays
@@ -455,8 +437,6 @@ struct factor_vm {
   void primitive_word_optimized_p();
   void primitive_wrapper();
   void jit_compile_word(cell word_, cell def_, bool relocating);
-  cell find_all_words();
-  void compile_all_words();
 
   // math
   void primitive_bignum_to_fixnum();
@@ -531,20 +511,27 @@ struct factor_vm {
   inline double fixnum_to_float(cell tagged);
 
   // tagged
-  template <typename Type> Type* untag_check(cell value);
+  template <typename Type> void check_tagged(tagged<Type> t) {
+    if (!t.type_p())
+      type_error(Type::type_number, t.value_);
+  }
+
+  template <typename Type> Type* untag_check(cell value) {
+    tagged<Type> t(value);
+    check_tagged(t);
+    return t.untagged();
+  }
 
   // io
-  void init_c_io();
   void io_error_if_not_EINTR();
-  FILE* safe_fopen(char* filename, char* mode);
+  FILE* safe_fopen(char* filename, const char* mode);
   int safe_fgetc(FILE* stream);
   size_t safe_fread(void* ptr, size_t size, size_t nitems, FILE* stream);
   void safe_fputc(int c, FILE* stream);
   size_t safe_fwrite(void* ptr, size_t size, size_t nitems, FILE* stream);
-  int safe_ftell(FILE* stream);
+  off_t safe_ftell(FILE* stream);
   void safe_fseek(FILE* stream, off_t offset, int whence);
   void safe_fflush(FILE* stream);
-  void safe_fclose(FILE* stream);
   void primitive_fopen();
   FILE* pop_file_handle();
   FILE* peek_file_handle();
@@ -558,25 +545,18 @@ struct factor_vm {
   void primitive_fclose();
 
   // code_block
-  cell compute_entry_point_address(cell obj);
   cell compute_entry_point_pic_address(word* w, cell tagged_quot);
   cell compute_entry_point_pic_address(cell w_);
   cell compute_entry_point_pic_tail_address(cell w_);
   cell compute_external_address(instruction_operand op);
 
-  cell code_block_owner(code_block* compiled);
   void update_word_references(code_block* compiled, bool reset_inline_caches);
   void undefined_symbol();
-  cell compute_dlsym_address(array* literals, cell index);
-#ifdef FACTOR_PPC
-  cell compute_dlsym_toc_address(array* literals, cell index);
-#endif
-  cell compute_vm_address(cell arg);
+  cell compute_dlsym_address(array* literals, cell index, bool toc);
   cell lookup_external_address(relocation_type rel_type,
                                code_block* compiled,
                                array* parameters,
                                cell index);
-  cell compute_here_address(cell arg, cell offset, code_block* compiled);
   void initialize_code_block(code_block* compiled, cell literals);
   void initialize_code_block(code_block* compiled);
   void fixup_labels(array* labels, code_block* compiled);
@@ -587,32 +567,26 @@ struct factor_vm {
 
   //code heap
   template <typename Iterator> void each_code_block(Iterator& iter) {
-    code->allocator->iterate(iter);
+    code->allocator->iterate(iter, no_fixup());
   }
 
   void update_code_heap_words(bool reset_inline_caches);
-  void initialize_code_blocks();
   void primitive_modify_code_heap();
   void primitive_code_room();
   void primitive_strip_stack_traces();
-  cell code_blocks();
   void primitive_code_blocks();
 
   // callbacks
-  void init_callbacks(cell size);
   void primitive_free_callback();
   void primitive_callback();
   void primitive_callback_room();
 
   // image
-  void init_objects(image_header* h);
   void load_data_heap(FILE* file, image_header* h, vm_parameters* p);
   void load_code_heap(FILE* file, image_header* h, vm_parameters* p);
   bool save_image(const vm_char* saving_filename, const vm_char* filename);
   void primitive_save_image();
-  void fixup_data(cell data_offset, cell code_offset);
-  void fixup_code(cell data_offset, cell code_offset);
-  FILE* open_image(vm_parameters* p);
+  void fixup_heaps(cell data_offset, cell code_offset);
   void load_image(vm_parameters* p);
   bool read_embedded_image_footer(FILE* file, embedded_image_footer* footer);
   bool embedded_image_p();
@@ -626,9 +600,7 @@ struct factor_vm {
   callstack* allot_callstack(cell size);
   cell second_from_top_stack_frame(context* ctx);
   cell capture_callstack(context* ctx);
-  void primitive_callstack();
   void primitive_callstack_for();
-  cell frame_predecessor(cell frame_top);
   void primitive_callstack_to_array();
   void primitive_innermost_stack_frame_executing();
   void primitive_innermost_stack_frame_scan();
@@ -642,11 +614,17 @@ struct factor_vm {
 
   // cpu-*
   void dispatch_signal_handler(cell* sp, cell* pc, cell newpc);
+#if defined(FACTOR_X86) || defined(FACTOR_64)
+  void dispatch_non_resumable_signal(cell* sp, cell* pc,
+                                     cell handler,
+                                     cell limit);
+  void dispatch_resumable_signal(cell* sp, cell* pc, cell handler);
+#endif
 
   // alien
   char* pinned_alien_offset(cell obj);
   cell allot_alien(cell delegate_, cell displacement);
-  cell allot_alien(void* address);
+  cell allot_alien(cell address);
   void primitive_displaced_alien();
   void primitive_alien_address();
   void* alien_pointer();
@@ -668,32 +646,20 @@ struct factor_vm {
   cell lazy_jit_compile(cell quot);
   bool quotation_compiled_p(quotation* quot);
   void primitive_quotation_compiled_p();
-  void initialize_all_quotations();
 
   // dispatch
-  cell search_lookup_alist(cell table, cell klass);
-  cell search_lookup_hash(cell table, cell klass, cell hashcode);
-  cell nth_superclass(tuple_layout* layout, fixnum echelon);
-  cell nth_hashcode(tuple_layout* layout, fixnum echelon);
   cell lookup_tuple_method(cell obj, cell methods);
   cell lookup_method(cell obj, cell methods);
   void primitive_lookup_method();
   cell object_class(cell obj);
-  cell method_cache_hashcode(cell klass, array* array);
   void update_method_cache(cell cache, cell klass, cell method);
   void primitive_mega_cache_miss();
   void primitive_reset_dispatch_stats();
   void primitive_dispatch_stats();
 
   // inline cache
-  void init_inline_caching(int max_size);
   void deallocate_inline_cache(cell return_address);
-  cell determine_inline_cache_type(array* cache_entries);
   void update_pic_count(cell type);
-  code_block* compile_inline_cache(fixnum index, cell generic_word_,
-                                   cell methods_, cell cache_entries_,
-                                   bool tail_call_p);
-  cell inline_cache_size(cell cache_entries);
   cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_);
   void update_pic_transitions(cell pic_size);
   cell inline_cache_miss(cell return_address);
@@ -704,16 +670,16 @@ struct factor_vm {
   cell get_fpu_state();
   void set_fpu_state(cell state);
 
+  // safepoints
+  void handle_safepoint(cell pc);
+  void enqueue_samples(cell samples, cell pc, bool foreign_thread_p);
+  void enqueue_fep();
+
   // factor
-  void default_parameters(vm_parameters* p);
-  bool factor_arg(const vm_char* str, const vm_char* arg, cell* value);
-  void init_parameters_from_args(vm_parameters* p, int argc, vm_char** argv);
   void prepare_boot_image();
   void init_factor(vm_parameters* p);
   void pass_args_to_factor(int argc, vm_char** argv);
-  void start_factor(vm_parameters* p);
   void stop_factor();
-  void start_embedded_factor(vm_parameters* p);
   void start_standalone_factor(int argc, vm_char** argv);
   char* factor_eval_string(char* string);
   void factor_eval_free(char* result);
@@ -722,14 +688,10 @@ struct factor_vm {
 
   // os-*
   void primitive_existsp();
-  void move_file(const vm_char* path1, const vm_char* path2);
   void init_ffi();
   void ffi_dlopen(dll* dll);
-  void* ffi_dlsym(dll* dll, symbol_char* symbol);
-  void* ffi_dlsym_raw(dll* dll, symbol_char* symbol);
-#ifdef FACTOR_PPC
-  void* ffi_dlsym_toc(dll* dll, symbol_char* symbol);
-#endif
+  cell ffi_dlsym(dll* dll, symbol_char* symbol);
+  cell ffi_dlsym_raw(dll* dll, symbol_char* symbol);
   void ffi_dlclose(dll* dll);
   void c_to_factor_toplevel(cell quot);
   void init_signals();
@@ -738,13 +700,16 @@ struct factor_vm {
 
 // os-windows
 #if defined(WINDOWS)
+  /* Id of the main thread we run in. Used for Ctrl-Break handling. */
+  DWORD thread_id;
+
+  HANDLE ctrl_break_thread;
+
   HANDLE sampler_thread;
   void sampler_thread_loop();
 
   const vm_char* vm_executable_path();
   const vm_char* default_image_path();
-  void windows_image_path(vm_char* full_path, vm_char* temp_path,
-                          unsigned int length);
   BOOL windows_stat(vm_char* path);
 
   LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,