5 void init_globals() { init_mvm(); }
7 // Compile code in boot image so that we can execute the startup quotation
9 void factor_vm::prepare_boot_image() {
10 std::cout << "*** Stage 2 early init... " << std::flush;
13 data_root<array> words(instances(WORD_TYPE), this);
15 cell n_words = array_capacity(words.untagged());
16 for (cell i = 0; i < n_words; i++) {
17 data_root<word> word(array_nth(words.untagged(), i), this);
19 FACTOR_ASSERT(!word->entry_point);
20 jit_compile_word(word.value(), word->def, false);
22 update_code_heap_words(true);
24 // Initialize all quotations
25 data_root<array> quotations(instances(QUOTATION_TYPE), this);
27 cell n_quots = array_capacity(quotations.untagged());
28 for (cell i = 0; i < n_quots; i++) {
29 data_root<quotation> quot(array_nth(quotations.untagged(), i), this);
31 if (!quot->entry_point)
32 quot->entry_point = lazy_jit_compile_entry_point();
35 special_objects[OBJ_STAGE2] = special_objects[OBJ_CANONICAL_TRUE];
37 std::cout << "done" << std::endl;
40 void factor_vm::init_factor(vm_parameters* p) {
42 p->datastack_size = align_page(p->datastack_size << 10);
43 p->retainstack_size = align_page(p->retainstack_size << 10);
44 p->callstack_size = align_page(p->callstack_size << 10);
45 p->callback_size = align_page(p->callback_size << 10);
50 p->tenured_size <<= 20;
53 // Disable GC during init as a sanity check
56 // OS-specific initialization
59 p->executable_path = vm_executable_path();
61 if (p->image_path == NULL) {
62 if (embedded_image_p()) {
63 p->embedded_image = true;
64 p->image_path = safe_strdup(p->executable_path);
66 p->image_path = default_image_path();
69 srand((unsigned int)nano_count());
71 init_contexts(p->datastack_size, p->retainstack_size, p->callstack_size);
72 callbacks = new callback_heap(p->callback_size, this);
75 max_pic_size = (int)p->max_pic_size;
76 special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell));
77 special_objects[OBJ_ARGS] = false_object;
78 special_objects[OBJ_EMBEDDED] = false_object;
81 {OBJ_CPU, (cell)FACTOR_CPU_STRING},
82 {OBJ_EXECUTABLE, (cell)safe_strdup(p->executable_path)},
83 {OBJ_IMAGE, (cell)safe_strdup(p->image_path)},
84 {OBJ_OS, (cell)FACTOR_OS_STRING},
85 {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
86 {OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION},
87 {OBJ_VM_GIT_LABEL, (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)},
88 {OBJ_VM_VERSION, (cell)FACTOR_STRINGIZE(FACTOR_VERSION)},
90 {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler}
93 int n_items = sizeof(aliens) / sizeof(cell[2]);
94 for (int n = 0; n < n_items; n++) {
95 cell idx = aliens[n][0];
96 special_objects[idx] = allot_alien(false_object, aliens[n][1]);
102 if (!to_boolean(special_objects[OBJ_STAGE2]))
103 prepare_boot_image();
114 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
115 growable_array args(this);
117 for (fixnum i = 0; i < argc; i++)
118 args.add(allot_alien(false_object, (cell)argv[i]));
121 special_objects[OBJ_ARGS] = args.elements.value();
124 void factor_vm::stop_factor() {
125 c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
128 char* factor_vm::factor_eval_string(char* string) {
129 void* func = alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
130 CODE_TO_FUNCTION_POINTER(func);
131 return ((char * (*)(char*)) func)(string);
134 void factor_vm::factor_eval_free(char* result) { free(result); }
136 void factor_vm::factor_yield() {
137 void* func = alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
138 CODE_TO_FUNCTION_POINTER(func);
139 ((void(*)()) func)();
142 void factor_vm::factor_sleep(long us) {
143 void* func = alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
144 CODE_TO_FUNCTION_POINTER(func);
145 ((void(*)(long)) func)(us);
148 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
150 p.init_from_args(argc, argv);
152 pass_args_to_factor(argc, argv);
157 c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
160 factor_vm* new_factor_vm() {
161 THREADHANDLE thread = thread_id();
162 factor_vm* newvm = new factor_vm(thread);
163 register_vm_with_thread(newvm);
164 thread_vms[thread] = newvm;
169 VM_C_API void start_standalone_factor(int argc, vm_char** argv) {
170 factor_vm* newvm = new_factor_vm();
171 newvm->start_standalone_factor(argc, argv);