]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
34e2267a88ceb1056e10d2e575b9f429ce11dcd1
[factor.git] / vm / factor.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 factorvm *vm;
7
8 void init_globals()
9 {
10         init_platform_globals();
11 }
12
13 void factorvm::default_parameters(vm_parameters *p)
14 {
15         p->image_path = NULL;
16
17         /* We make a wild guess here that if we're running on ARM, we don't
18         have a lot of memory. */
19 #ifdef FACTOR_ARM
20         p->ds_size = 8 * sizeof(cell);
21         p->rs_size = 8 * sizeof(cell);
22
23         p->gen_count = 2;
24         p->code_size = 4;
25         p->young_size = 1;
26         p->aging_size = 1;
27         p->tenured_size = 6;
28 #else
29         p->ds_size = 32 * sizeof(cell);
30         p->rs_size = 32 * sizeof(cell);
31
32         p->gen_count = 3;
33         p->code_size = 8 * sizeof(cell);
34         p->young_size = sizeof(cell) / 4;
35         p->aging_size = sizeof(cell) / 2;
36         p->tenured_size = 4 * sizeof(cell);
37 #endif
38
39         p->max_pic_size = 3;
40
41         p->secure_gc = false;
42         p->fep = false;
43
44 #ifdef WINDOWS
45         p->console = false;
46 #else
47         if (this == vm)
48                 p->console = true;
49         else            
50                 p->console = false;
51         
52 #endif
53
54         p->stack_traces = true;
55 }
56
57 bool factorvm::factor_arg(const vm_char* str, const vm_char* arg, cell* value)
58 {
59         int val;
60         if(SSCANF(str,arg,&val) > 0)
61         {
62                 *value = val;
63                 return true;
64         }
65         else
66                 return false;
67 }
68
69 void factorvm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv)
70 {
71         default_parameters(p);
72         p->executable_path = argv[0];
73
74         int i = 0;
75
76         for(i = 1; i < argc; i++)
77         {
78                 if(factor_arg(argv[i],STRING_LITERAL("-datastack=%d"),&p->ds_size));
79                 else if(factor_arg(argv[i],STRING_LITERAL("-retainstack=%d"),&p->rs_size));
80                 else if(factor_arg(argv[i],STRING_LITERAL("-generations=%d"),&p->gen_count));
81                 else if(factor_arg(argv[i],STRING_LITERAL("-young=%d"),&p->young_size));
82                 else if(factor_arg(argv[i],STRING_LITERAL("-aging=%d"),&p->aging_size));
83                 else if(factor_arg(argv[i],STRING_LITERAL("-tenured=%d"),&p->tenured_size));
84                 else if(factor_arg(argv[i],STRING_LITERAL("-codeheap=%d"),&p->code_size));
85                 else if(factor_arg(argv[i],STRING_LITERAL("-pic=%d"),&p->max_pic_size));
86                 else if(STRCMP(argv[i],STRING_LITERAL("-securegc")) == 0) p->secure_gc = true;
87                 else if(STRCMP(argv[i],STRING_LITERAL("-fep")) == 0) p->fep = true;
88                 else if(STRNCMP(argv[i],STRING_LITERAL("-i="),3) == 0) p->image_path = argv[i] + 3;
89                 else if(STRCMP(argv[i],STRING_LITERAL("-console")) == 0) p->console = true;
90                 else if(STRCMP(argv[i],STRING_LITERAL("-no-stack-traces")) == 0) p->stack_traces = false;
91         }
92 }
93
94 /* Do some initialization that we do once only */
95 void factorvm::do_stage1_init()
96 {
97         print_string("*** Stage 2 early init... ");
98         fflush(stdout);
99
100         compile_all_words();
101         userenv[STAGE2_ENV] = T;
102
103         print_string("done\n");
104         fflush(stdout);
105 }
106
107 void factorvm::init_factor(vm_parameters *p)
108 {
109         /* Kilobytes */
110         p->ds_size = align_page(p->ds_size << 10);
111         p->rs_size = align_page(p->rs_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                 p->image_path = default_image_path();
132
133         srand(current_micros());
134         init_ffi();
135         init_stacks(p->ds_size,p->rs_size);
136         load_image(p);
137         init_c_io();
138         init_inline_caching(p->max_pic_size);
139         init_signals();
140
141         if(p->console)
142                 open_console();
143
144         init_profiler();
145
146         userenv[CPU_ENV] = allot_alien(F,(cell)FACTOR_CPU_STRING);
147         userenv[OS_ENV] = allot_alien(F,(cell)FACTOR_OS_STRING);
148         userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(cell));
149         userenv[EXECUTABLE_ENV] = allot_alien(F,(cell)p->executable_path);
150         userenv[ARGS_ENV] = F;
151         userenv[EMBEDDED_ENV] = F;
152
153         /* We can GC now */
154         gc_off = false;
155
156         if(userenv[STAGE2_ENV] == F)
157         {
158                 userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces);
159                 do_stage1_init();
160         }
161 }
162
163 /* May allocate memory */
164 void factorvm::pass_args_to_factor(int argc, vm_char **argv)
165 {
166         growable_array args(this);
167         int i;
168
169         for(i = 1; i < argc; i++){
170                 args.add(allot_alien(F,(cell)argv[i]));
171         }
172
173         args.trim();
174         userenv[ARGS_ENV] = args.elements.value();
175 }
176
177 void factorvm::start_factor(vm_parameters *p)
178 {
179         if(p->fep) factorbug();
180
181         nest_stacks();
182         c_to_factor_toplevel(userenv[BOOT_ENV]);
183         unnest_stacks();
184 }
185
186
187 char *factorvm::factor_eval_string(char *string)
188 {
189         char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]);
190         return callback(string);
191 }
192
193 void factorvm::factor_eval_free(char *result)
194 {
195         free(result);
196 }
197
198 void factorvm::factor_yield()
199 {
200         void (*callback)() = (void (*)())alien_offset(userenv[YIELD_CALLBACK_ENV]);
201         callback();
202 }
203
204 void factorvm::factor_sleep(long us)
205 {
206         void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]);
207         callback(us);
208 }
209
210 void factorvm::start_standalone_factor(int argc, vm_char **argv)
211 {
212         vm_parameters p;
213         default_parameters(&p);
214         init_parameters_from_args(&p,argc,argv);
215         init_factor(&p);
216         pass_args_to_factor(argc,argv);
217         start_factor(&p);
218 }
219
220 struct startargs {
221         int argc;
222         vm_char **argv;
223 };
224
225 void* start_standalone_factor_thread(void *arg) 
226 {
227         factorvm *newvm = new factorvm;
228         register_vm_with_thread(newvm);
229         startargs *args = (startargs*) arg;
230         newvm->start_standalone_factor(args->argc, args->argv);
231         return 0;
232 }
233
234
235 VM_C_API void start_standalone_factor(int argc, vm_char **argv)
236 {
237         factorvm *newvm = new factorvm;
238         newvm->print_vm_data();
239         printf("PHIL YEAH: %d %d %d %d\n",(void*)(newvm),(void*)(newvm+1),sizeof(newvm), sizeof(factorvm));
240         vm = newvm;
241         register_vm_with_thread(newvm);
242         return newvm->start_standalone_factor(argc,argv);
243 }
244
245 VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv)
246 {
247         startargs *args = new startargs;   // leaks startargs structure
248         args->argc = argc; args->argv = argv;
249         return start_thread(start_standalone_factor_thread,args);
250 }
251
252
253 void factorvm::print_vm_data() {
254         printf("PHIL: stack_chain %d\n",&stack_chain);
255         printf("PHIL: nursery %d\n",&nursery);
256         printf("PHIL: cards_offset %d\n",&cards_offset);
257         printf("PHIL: decks_offset %d\n",&decks_offset);
258         printf("PHIL: userenv %d\n",&userenv);
259         printf("PHIL: ds_size %d\n",&ds_size);
260         printf("PHIL: rs_size %d\n",&rs_size);
261         printf("PHIL: unused_contexts %d\n",&unused_contexts);
262         printf("PHIL: T %d\n",&T);
263         printf("PHIL: profiling_p %d\n",&profiling_p);
264         printf("PHIL: signal_number %d\n",&signal_number);
265         printf("PHIL: signal_fault_addr %d\n",&signal_fault_addr);
266         printf("PHIL: signal_callstack_top %d\n",&signal_callstack_top);
267         printf("PHIL: secure_gc %d\n",&secure_gc);
268         printf("PHIL: gc_off %d\n",&gc_off);
269         printf("PHIL: data %d\n",&data);
270         printf("PHIL: heap_scan_ptr %d\n",&heap_scan_ptr);
271         printf("PHIL: allot_markers_offset %d\n",&allot_markers_offset);
272         printf("PHIL: newspace %d\n",&newspace);
273         printf("PHIL: performing_gc %d\n",&performing_gc);
274         printf("PHIL: performing_compaction %d\n",&performing_compaction);
275         printf("PHIL: collecting_gen %d\n",&collecting_gen);
276         printf("PHIL: collecting_aging_again %d\n",&collecting_aging_again);
277         printf("PHIL: gc_jmp %d\n",&gc_jmp);
278         printf("PHIL: stats %d\n",&stats);
279         printf("PHIL: cards_scanned %d\n",&cards_scanned);
280         printf("PHIL: decks_scanned %d\n",&decks_scanned);
281         printf("PHIL: card_scan_time %d\n",&card_scan_time);
282         printf("PHIL: code_heap_scans %d\n",&code_heap_scans);
283         printf("PHIL: last_code_heap_scan %d\n",&last_code_heap_scan);
284         printf("PHIL: growing_data_heap %d\n",&growing_data_heap);
285         printf("PHIL: old_data_heap %d\n",&old_data_heap);
286         printf("PHIL: gc_locals %d\n",&gc_locals);
287         printf("PHIL: gc_bignums %d\n",&gc_bignums);
288         printf("PHIL: fep_disabled %d\n",&fep_disabled);
289         printf("PHIL: full_output %d\n",&full_output);
290         printf("PHIL: look_for %d\n",&look_for);
291         printf("PHIL: obj %d\n",&obj);
292         printf("PHIL: bignum_zero %d\n",&bignum_zero);
293         printf("PHIL: bignum_pos_one %d\n",&bignum_pos_one);
294         printf("PHIL: bignum_neg_one %d\n",&bignum_neg_one);
295         printf("PHIL: code %d\n",&code);
296         printf("PHIL: forwarding %d\n",&forwarding);
297         printf("PHIL: code_relocation_base %d\n",&code_relocation_base);
298         printf("PHIL: data_relocation_base %d\n",&data_relocation_base);
299         printf("PHIL: megamorphic_cache_hits %d\n",&megamorphic_cache_hits);
300         printf("PHIL: megamorphic_cache_misses %d\n",&megamorphic_cache_misses);
301         printf("PHIL: max_pic_size %d\n",&max_pic_size);
302         printf("PHIL: cold_call_to_ic_transitions %d\n",&cold_call_to_ic_transitions);
303         printf("PHIL: ic_to_pic_transitions %d\n",&ic_to_pic_transitions);
304         printf("PHIL: pic_to_mega_transitions %d\n",&pic_to_mega_transitions);
305         printf("PHIL: pic_counts %d\n",&pic_counts);
306 }
307
308         // if you change this struct, also change vm.factor k--------
309         context *stack_chain; 
310         zone nursery; /* new objects are allocated here */
311         cell cards_offset;
312         cell decks_offset;
313         cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */
314
315         // -------------------------------
316
317         // contexts
318         cell ds_size, rs_size;
319         context *unused_contexts;
320
321         // run
322         cell T;  /* Canonical T object. It's just a word */
323
324         // profiler
325         bool profiling_p;
326
327         // errors
328         /* Global variables used to pass fault handler state from signal handler to
329            user-space */
330         cell signal_number;
331         cell signal_fault_addr;
332         unsigned int signal_fpu_status;
333         stack_frame *signal_callstack_top;
334
335         //data_heap
336         bool secure_gc;  /* Set by the -securegc command line argument */
337         bool gc_off; /* GC is off during heap walking */
338         data_heap *data;
339         /* A heap walk allows useful things to be done, like finding all
340            references to an object for debugging purposes. */
341         cell heap_scan_ptr;
342         //write barrier
343         cell allot_markers_offset;
344         //data_gc
345         /* used during garbage collection only */
346         zone *newspace;
347         bool performing_gc;
348         bool performing_compaction;
349         cell collecting_gen;
350         /* if true, we are collecting aging space for the second time, so if it is still
351            full, we go on to collect tenured */
352         bool collecting_aging_again;
353         /* in case a generation fills up in the middle of a gc, we jump back
354            up to try collecting the next generation. */
355         jmp_buf gc_jmp;
356         gc_stats stats[max_gen_count];
357         u64 cards_scanned;
358         u64 decks_scanned;
359         u64 card_scan_time;
360         cell code_heap_scans;
361         /* What generation was being collected when copy_code_heap_roots() was last
362            called? Until the next call to add_code_block(), future
363            collections of younger generations don't have to touch the code
364            heap. */
365         cell last_code_heap_scan;
366         /* sometimes we grow the heap */
367         bool growing_data_heap;
368         data_heap *old_data_heap;
369
370         // local roots
371         /* If a runtime function needs to call another function which potentially
372            allocates memory, it must wrap any local variable references to Factor
373            objects in gc_root instances */
374         std::vector<cell> gc_locals;
375         std::vector<cell> gc_bignums;
376
377         //debug
378         bool fep_disabled;
379         bool full_output;
380         cell look_for;
381         cell obj;
382
383         //math
384         cell bignum_zero;
385         cell bignum_pos_one;
386         cell bignum_neg_one;    
387
388         //code_heap
389         heap code;
390         unordered_map<heap_block *,char *> forwarding;
391
392         //image
393         cell code_relocation_base;
394         cell data_relocation_base;
395
396         //dispatch
397         cell megamorphic_cache_hits;
398         cell megamorphic_cache_misses;
399
400         //inline cache
401         cell max_pic_size;
402         cell cold_call_to_ic_transitions;
403         cell ic_to_pic_transitions;
404         cell pic_to_mega_transitions;
405         cell pic_counts[4];  /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
406 }