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());
72 datastack_size = p->datastack_size;
73 retainstack_size = p->retainstack_size;
74 callstack_size = p->callstack_size;
77 spare_ctx = new_context();
79 callbacks = new callback_heap(p->callback_size, this);
81 max_pic_size = (int)p->max_pic_size;
82 special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell));
83 special_objects[OBJ_ARGS] = false_object;
84 special_objects[OBJ_EMBEDDED] = false_object;
87 {OBJ_STDIN, (cell)stdin},
88 {OBJ_STDOUT, (cell)stdout},
89 {OBJ_STDERR, (cell)stderr},
90 {OBJ_CPU, (cell)FACTOR_CPU_STRING},
91 {OBJ_EXECUTABLE, (cell)safe_strdup(p->executable_path)},
92 {OBJ_IMAGE, (cell)safe_strdup(p->image_path)},
93 {OBJ_OS, (cell)FACTOR_OS_STRING},
94 {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
95 {OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION},
96 {OBJ_VM_GIT_LABEL, (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)},
97 {OBJ_VM_VERSION, (cell)FACTOR_STRINGIZE(FACTOR_VERSION)},
99 {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler}
102 int n_items = sizeof(aliens) / sizeof(cell[2]);
103 for (int n = 0; n < n_items; n++) {
104 cell idx = aliens[n][0];
105 special_objects[idx] = allot_alien(false_object, aliens[n][1]);
111 if (!to_boolean(special_objects[OBJ_STAGE2]))
112 prepare_boot_image();
123 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
124 growable_array args(this);
126 for (fixnum i = 0; i < argc; i++)
127 args.add(allot_alien(false_object, (cell)argv[i]));
130 special_objects[OBJ_ARGS] = args.elements.value();
133 void factor_vm::stop_factor() {
134 c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
137 char* factor_vm::factor_eval_string(char* string) {
138 void* func = alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
139 CODE_TO_FUNCTION_POINTER(func);
140 return ((char * (*)(char*)) func)(string);
143 void factor_vm::factor_eval_free(char* result) { free(result); }
145 void factor_vm::factor_yield() {
146 void* func = alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
147 CODE_TO_FUNCTION_POINTER(func);
148 ((void(*)()) func)();
151 void factor_vm::factor_sleep(long us) {
152 void* func = alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
153 CODE_TO_FUNCTION_POINTER(func);
154 ((void(*)(long)) func)(us);
157 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
159 p.init_from_args(argc, argv);
161 pass_args_to_factor(argc, argv);
166 c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
169 factor_vm* new_factor_vm() {
170 THREADHANDLE thread = thread_id();
171 factor_vm* newvm = new factor_vm(thread);
172 register_vm_with_thread(newvm);
173 thread_vms[thread] = newvm;
178 VM_C_API void start_standalone_factor(int argc, vm_char** argv) {
179 factor_vm* newvm = new_factor_vm();
180 newvm->start_standalone_factor(argc, argv);