]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
VM: removing factor_vm::init_contexts()
[factor.git] / vm / factor.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 void init_globals() { init_mvm(); }
6
7 // Compile code in boot image so that we can execute the startup quotation
8 // Allocates memory
9 void factor_vm::prepare_boot_image() {
10   std::cout << "*** Stage 2 early init... " << std::flush;
11
12   // Compile all words.
13   data_root<array> words(instances(WORD_TYPE), this);
14
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);
18
19     FACTOR_ASSERT(!word->entry_point);
20     jit_compile_word(word.value(), word->def, false);
21   }
22   update_code_heap_words(true);
23
24   // Initialize all quotations
25   data_root<array> quotations(instances(QUOTATION_TYPE), this);
26
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);
30
31     if (!quot->entry_point)
32       quot->entry_point = lazy_jit_compile_entry_point();
33   }
34
35   special_objects[OBJ_STAGE2] = special_objects[OBJ_CANONICAL_TRUE];
36
37   std::cout << "done" << std::endl;
38 }
39
40 void factor_vm::init_factor(vm_parameters* p) {
41   // Kilobytes
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);
46
47   // Megabytes
48   p->young_size <<= 20;
49   p->aging_size <<= 20;
50   p->tenured_size <<= 20;
51   p->code_size <<= 20;
52
53   // Disable GC during init as a sanity check
54   gc_off = true;
55
56   // OS-specific initialization
57   early_init();
58
59   p->executable_path = vm_executable_path();
60
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);
65     } else
66       p->image_path = default_image_path();
67   }
68
69   srand((unsigned int)nano_count());
70   init_ffi();
71
72   datastack_size = p->datastack_size;
73   retainstack_size = p->retainstack_size;
74   callstack_size = p->callstack_size;
75
76   ctx = NULL;
77   spare_ctx = new_context();
78
79   callbacks = new callback_heap(p->callback_size, this);
80   load_image(p);
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;
85
86   cell aliens[][2] = {
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)},
98 #if defined(WINDOWS)
99     {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler}
100 #endif
101   };
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]);
106   }
107
108   // We can GC now
109   gc_off = false;
110
111   if (!to_boolean(special_objects[OBJ_STAGE2]))
112     prepare_boot_image();
113
114   if (p->signals)
115     init_signals();
116
117   if (p->console)
118     open_console();
119
120 }
121
122 // Allocates memory
123 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
124   growable_array args(this);
125
126   for (fixnum i = 0; i < argc; i++)
127     args.add(allot_alien(false_object, (cell)argv[i]));
128
129   args.trim();
130   special_objects[OBJ_ARGS] = args.elements.value();
131 }
132
133 void factor_vm::stop_factor() {
134   c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
135 }
136
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);
141 }
142
143 void factor_vm::factor_eval_free(char* result) { free(result); }
144
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)();
149 }
150
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);
155 }
156
157 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
158   vm_parameters p;
159   p.init_from_args(argc, argv);
160   init_factor(&p);
161   pass_args_to_factor(argc, argv);
162
163   if (p.fep)
164     factorbug();
165
166   c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
167 }
168
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;
174
175   return newvm;
176 }
177
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);
181   delete newvm;
182 }
183
184 }