]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
VM: get rid of the odd RT_EXCEPTION_HANDLER relocation
[factor.git] / vm / factor.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 void init_globals() { init_mvm(); }
6
7 void factor_vm::default_parameters(vm_parameters* p) {
8   p->embedded_image = false;
9   p->image_path = NULL;
10
11   p->datastack_size = 32 * sizeof(cell);
12   p->retainstack_size = 32 * sizeof(cell);
13
14 #if defined(FACTOR_PPC)
15   p->callstack_size = 256 * sizeof(cell);
16 #else
17   p->callstack_size = 128 * sizeof(cell);
18 #endif
19
20   p->code_size = 64;
21   p->young_size = sizeof(cell) / 4;
22   p->aging_size = sizeof(cell) / 2;
23   p->tenured_size = 24 * sizeof(cell);
24
25   p->max_pic_size = 3;
26
27   p->fep = false;
28   p->signals = true;
29
30 #ifdef WINDOWS
31   p->console = GetConsoleWindow() != NULL;
32 #else
33   p->console = true;
34 #endif
35
36   p->callback_size = 256;
37 }
38
39 bool factor_vm::factor_arg(const vm_char* str, const vm_char* arg,
40                            cell* value) {
41   int val;
42   if (SSCANF(str, arg, &val) > 0) {
43     *value = val;
44     return true;
45   } else
46     return false;
47 }
48
49 void factor_vm::init_parameters_from_args(vm_parameters* p, int argc,
50                                           vm_char** argv) {
51   default_parameters(p);
52   p->executable_path = argv[0];
53
54   int i = 0;
55
56   for (i = 1; i < argc; i++) {
57     vm_char* arg = argv[i];
58     if (STRCMP(arg, STRING_LITERAL("--")) == 0)
59       break;
60     else if (factor_arg(arg, STRING_LITERAL("-datastack=%d"),
61                         &p->datastack_size))
62       ;
63     else if (factor_arg(arg, STRING_LITERAL("-retainstack=%d"),
64                         &p->retainstack_size))
65       ;
66     else if (factor_arg(arg, STRING_LITERAL("-callstack=%d"),
67                         &p->callstack_size))
68       ;
69     else if (factor_arg(arg, STRING_LITERAL("-young=%d"), &p->young_size))
70       ;
71     else if (factor_arg(arg, STRING_LITERAL("-aging=%d"), &p->aging_size))
72       ;
73     else if (factor_arg(arg, STRING_LITERAL("-tenured=%d"), &p->tenured_size))
74       ;
75     else if (factor_arg(arg, STRING_LITERAL("-codeheap=%d"), &p->code_size))
76       ;
77     else if (factor_arg(arg, STRING_LITERAL("-pic=%d"), &p->max_pic_size))
78       ;
79     else if (factor_arg(arg, STRING_LITERAL("-callbacks=%d"),
80                         &p->callback_size))
81       ;
82     else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0)
83       p->fep = true;
84     else if (STRCMP(arg, STRING_LITERAL("-nosignals")) == 0)
85       p->signals = false;
86     else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0)
87       p->image_path = arg + 3;
88     else if (STRCMP(arg, STRING_LITERAL("-console")) == 0)
89       p->console = true;
90   }
91 }
92
93 /* Compile code in boot image so that we can execute the startup quotation */
94 /* Allocates memory */
95 void factor_vm::prepare_boot_image() {
96   std::cout << "*** Stage 2 early init... " << std::flush;
97
98   compile_all_words();
99   update_code_heap_words(true);
100   initialize_all_quotations();
101   special_objects[OBJ_STAGE2] = true_object;
102
103   std::cout << "done" << std::endl;
104 }
105
106 void factor_vm::init_factor(vm_parameters* p) {
107   /* Kilobytes */
108   p->datastack_size = align_page(p->datastack_size << 10);
109   p->retainstack_size = align_page(p->retainstack_size << 10);
110   p->callstack_size = align_page(p->callstack_size << 10);
111   p->callback_size = align_page(p->callback_size << 10);
112
113   /* Megabytes */
114   p->young_size <<= 20;
115   p->aging_size <<= 20;
116   p->tenured_size <<= 20;
117   p->code_size <<= 20;
118
119   /* Disable GC during init as a sanity check */
120   gc_off = true;
121
122   /* OS-specific initialization */
123   early_init();
124
125   const vm_char* executable_path = vm_executable_path();
126
127   if (executable_path)
128     p->executable_path = executable_path;
129
130   if (p->image_path == NULL) {
131     if (embedded_image_p()) {
132       p->embedded_image = true;
133       p->image_path = p->executable_path;
134     } else
135       p->image_path = default_image_path();
136   }
137
138   srand((unsigned int)nano_count());
139   init_ffi();
140   init_contexts(p->datastack_size, p->retainstack_size, p->callstack_size);
141   init_callbacks(p->callback_size);
142   load_image(p);
143   init_c_io();
144   init_inline_caching((int)p->max_pic_size);
145   special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell));
146   special_objects[OBJ_ARGS] = false_object;
147   special_objects[OBJ_EMBEDDED] = false_object;
148
149   cell aliens[][2] = {
150     {OBJ_CPU,             (cell)FACTOR_CPU_STRING},
151     {OBJ_EXECUTABLE,      (cell)p->executable_path},
152     {OBJ_OS,              (cell)FACTOR_OS_STRING},
153     {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
154     {OBJ_VM_COMPILER,     (cell)FACTOR_COMPILER_VERSION},
155     {OBJ_VM_GIT_LABEL,    (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)},
156     {OBJ_VM_VERSION,      (cell)FACTOR_STRINGIZE(FACTOR_VERSION)},
157 #if defined(WINDOWS)
158     {UNUSED1,              (cell)&factor::exception_handler}
159 #endif
160   };
161   int n_items = sizeof(aliens) / sizeof(cell[2]);
162   for (int n = 0; n < n_items; n++) {
163     cell idx = aliens[n][0];
164     special_objects[idx] = allot_alien(false_object, aliens[n][1]);
165   }
166
167   /* We can GC now */
168   gc_off = false;
169
170   if (!to_boolean(special_objects[OBJ_STAGE2]))
171     prepare_boot_image();
172
173   if (p->signals)
174     init_signals();
175
176   if (p->console)
177     open_console();
178
179 }
180
181 /* Allocates memory */
182 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
183   growable_array args(this);
184
185   for (fixnum i = 0; i < argc; i++)
186     args.add(allot_alien(false_object, (cell)argv[i]));
187
188   args.trim();
189   special_objects[OBJ_ARGS] = args.elements.value();
190 }
191
192 void factor_vm::start_factor(vm_parameters* p) {
193   if (p->fep)
194     factorbug();
195
196   c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
197 }
198
199 void factor_vm::stop_factor() {
200   c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
201 }
202
203 char* factor_vm::factor_eval_string(char* string) {
204   void* func = alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
205   CODE_TO_FUNCTION_POINTER(func);
206   return ((char * (*)(char*)) func)(string);
207 }
208
209 void factor_vm::factor_eval_free(char* result) { free(result); }
210
211 void factor_vm::factor_yield() {
212   void* func = alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
213   CODE_TO_FUNCTION_POINTER(func);
214   ((void(*)()) func)();
215 }
216
217 void factor_vm::factor_sleep(long us) {
218   void* func = alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
219   CODE_TO_FUNCTION_POINTER(func);
220   ((void(*)(long)) func)(us);
221 }
222
223 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
224   vm_parameters p;
225   init_parameters_from_args(&p, argc, argv);
226   init_factor(&p);
227   pass_args_to_factor(argc, argv);
228   start_factor(&p);
229 }
230
231 factor_vm* new_factor_vm() {
232   THREADHANDLE thread = thread_id();
233   factor_vm* newvm = new factor_vm(thread);
234   register_vm_with_thread(newvm);
235   thread_vms[thread] = newvm;
236
237   return newvm;
238 }
239
240 VM_C_API void start_standalone_factor(int argc, vm_char** argv) {
241   factor_vm* newvm = new_factor_vm();
242   return newvm->start_standalone_factor(argc, argv);
243 }
244
245 }