]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
vm: smaller default callstack size on OpenBSD
[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->image_path = NULL;
14
15         p->datastack_size = 32 * sizeof(cell);
16         p->retainstack_size = 32 * sizeof(cell);
17
18 #ifdef __OpenBSD__
19         p->callstack_size = 32 * sizeof(cell);
20 #else
21         p->callstack_size = 128 * sizeof(cell);
22 #endif
23
24         p->code_size = 8 * sizeof(cell);
25         p->young_size = sizeof(cell) / 4;
26         p->aging_size = sizeof(cell) / 2;
27         p->tenured_size = 24 * sizeof(cell);
28
29         p->max_pic_size = 3;
30
31         p->fep = false;
32         p->signals = true;
33
34 #ifdef WINDOWS
35         p->console = false;
36 #else
37         p->console = true;
38 #endif
39
40         p->callback_size = 256;
41 }
42
43 bool factor_vm::factor_arg(const vm_char* str, const vm_char* arg, cell* value)
44 {
45         int val;
46         if(SSCANF(str,arg,&val) > 0)
47         {
48                 *value = val;
49                 return true;
50         }
51         else
52                 return false;
53 }
54
55 void factor_vm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv)
56 {
57         default_parameters(p);
58         p->executable_path = argv[0];
59
60         int i = 0;
61
62         for(i = 1; i < argc; i++)
63         {
64                 vm_char *arg = argv[i];
65                 if(STRCMP(arg,STRING_LITERAL("--")) == 0) break;
66                 else if(factor_arg(arg,STRING_LITERAL("-datastack=%d"),&p->datastack_size));
67                 else if(factor_arg(arg,STRING_LITERAL("-retainstack=%d"),&p->retainstack_size));
68                 else if(factor_arg(arg,STRING_LITERAL("-callstack=%d"),&p->callstack_size));
69                 else if(factor_arg(arg,STRING_LITERAL("-young=%d"),&p->young_size));
70                 else if(factor_arg(arg,STRING_LITERAL("-aging=%d"),&p->aging_size));
71                 else if(factor_arg(arg,STRING_LITERAL("-tenured=%d"),&p->tenured_size));
72                 else if(factor_arg(arg,STRING_LITERAL("-codeheap=%d"),&p->code_size));
73                 else if(factor_arg(arg,STRING_LITERAL("-pic=%d"),&p->max_pic_size));
74                 else if(factor_arg(arg,STRING_LITERAL("-callbacks=%d"),&p->callback_size));
75                 else if(STRCMP(arg,STRING_LITERAL("-fep")) == 0) p->fep = true;
76                 else if(STRCMP(arg,STRING_LITERAL("-nosignals")) == 0) p->signals = false;
77                 else if(STRNCMP(arg,STRING_LITERAL("-i="),3) == 0) p->image_path = arg + 3;
78                 else if(STRCMP(arg,STRING_LITERAL("-console")) == 0) p->console = true;
79         }
80 }
81
82 /* Compile code in boot image so that we can execute the startup quotation */
83 void factor_vm::prepare_boot_image()
84 {
85         std::cout << "*** Stage 2 early init... ";
86         fflush(stdout);
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\n";
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                 p->image_path = default_image_path();
123
124         srand((unsigned int)system_micros());
125         init_ffi();
126         init_contexts(p->datastack_size,p->retainstack_size,p->callstack_size);
127         init_callbacks(p->callback_size);
128         load_image(p);
129         init_c_io();
130         init_inline_caching(p->max_pic_size);
131         if(p->signals)
132                 init_signals();
133
134         if(p->console)
135                 open_console();
136
137         init_profiler();
138
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
154 /* May allocate memory */
155 void factor_vm::pass_args_to_factor(int argc, vm_char **argv)
156 {
157         growable_array args(this);
158
159         for(fixnum i = 1; i < argc; i++)
160                 args.add(allot_alien(false_object,(cell)argv[i]));
161
162         args.trim();
163         special_objects[OBJ_ARGS] = args.elements.value();
164 }
165
166 void factor_vm::start_factor(vm_parameters *p)
167 {
168         if(p->fep) factorbug();
169
170         c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
171 }
172
173 void factor_vm::stop_factor()
174 {
175         c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
176 }
177
178 char *factor_vm::factor_eval_string(char *string)
179 {
180         char *(*callback)(char *) = (char *(*)(char *))alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
181         return callback(string);
182 }
183
184 void factor_vm::factor_eval_free(char *result)
185 {
186         free(result);
187 }
188
189 void factor_vm::factor_yield()
190 {
191         void (*callback)() = (void (*)())alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
192         callback();
193 }
194
195 void factor_vm::factor_sleep(long us)
196 {
197         void (*callback)(long) = (void (*)(long))alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
198         callback(us);
199 }
200
201 void factor_vm::start_standalone_factor(int argc, vm_char **argv)
202 {
203         vm_parameters p;
204         default_parameters(&p);
205         init_parameters_from_args(&p,argc,argv);
206         init_factor(&p);
207         pass_args_to_factor(argc,argv);
208         start_factor(&p);
209 }
210
211 factor_vm *new_factor_vm()
212 {
213         factor_vm *newvm = new factor_vm();
214         register_vm_with_thread(newvm);
215         thread_vms[thread_id()] = newvm;
216
217         return newvm;
218 }
219
220 VM_C_API void start_standalone_factor(int argc, vm_char **argv)
221 {
222         factor_vm *newvm = new_factor_vm();
223         return newvm->start_standalone_factor(argc,argv);
224 }
225
226 }