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