10 init_platform_globals();
13 void factorvm::default_parameters(vm_parameters *p)
17 /* We make a wild guess here that if we're running on ARM, we don't
18 have a lot of memory. */
20 p->ds_size = 8 * sizeof(cell);
21 p->rs_size = 8 * sizeof(cell);
29 p->ds_size = 32 * sizeof(cell);
30 p->rs_size = 32 * sizeof(cell);
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);
54 p->stack_traces = true;
57 bool factorvm::factor_arg(const vm_char* str, const vm_char* arg, cell* value)
60 if(SSCANF(str,arg,&val) > 0)
69 void factorvm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv)
71 default_parameters(p);
72 p->executable_path = argv[0];
76 for(i = 1; i < argc; i++)
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;
94 /* Do some initialization that we do once only */
95 void factorvm::do_stage1_init()
97 print_string("*** Stage 2 early init... ");
101 userenv[STAGE2_ENV] = T;
103 print_string("done\n");
107 void factorvm::init_factor(vm_parameters *p)
110 p->ds_size = align_page(p->ds_size << 10);
111 p->rs_size = align_page(p->rs_size << 10);
114 p->young_size <<= 20;
115 p->aging_size <<= 20;
116 p->tenured_size <<= 20;
119 /* Disable GC during init as a sanity check */
122 /* OS-specific initialization */
125 const vm_char *executable_path = vm_executable_path();
128 p->executable_path = executable_path;
130 if(p->image_path == NULL)
131 p->image_path = default_image_path();
133 srand(current_micros());
135 init_stacks(p->ds_size,p->rs_size);
138 init_inline_caching(p->max_pic_size);
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;
156 if(userenv[STAGE2_ENV] == F)
158 userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces);
163 /* May allocate memory */
164 void factorvm::pass_args_to_factor(int argc, vm_char **argv)
166 growable_array args(this);
169 for(i = 1; i < argc; i++){
170 args.add(allot_alien(F,(cell)argv[i]));
174 userenv[ARGS_ENV] = args.elements.value();
177 void factorvm::start_factor(vm_parameters *p)
179 if(p->fep) factorbug();
182 c_to_factor_toplevel(userenv[BOOT_ENV]);
187 char *factorvm::factor_eval_string(char *string)
189 char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]);
190 return callback(string);
193 void factorvm::factor_eval_free(char *result)
198 void factorvm::factor_yield()
200 void (*callback)() = (void (*)())alien_offset(userenv[YIELD_CALLBACK_ENV]);
204 void factorvm::factor_sleep(long us)
206 void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]);
210 void factorvm::start_standalone_factor(int argc, vm_char **argv)
213 default_parameters(&p);
214 init_parameters_from_args(&p,argc,argv);
216 pass_args_to_factor(argc,argv);
225 void* start_standalone_factor_thread(void *arg)
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);
235 VM_C_API void start_standalone_factor(int argc, vm_char **argv)
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));
241 register_vm_with_thread(newvm);
242 return newvm->start_standalone_factor(argc,argv);
245 VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv)
247 startargs *args = new startargs; // leaks startargs structure
248 args->argc = argc; args->argv = argv;
249 return start_thread(start_standalone_factor_thread,args);
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);
308 // if you change this struct, also change vm.factor k--------
309 context *stack_chain;
310 zone nursery; /* new objects are allocated here */
313 cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */
315 // -------------------------------
318 cell ds_size, rs_size;
319 context *unused_contexts;
322 cell T; /* Canonical T object. It's just a word */
328 /* Global variables used to pass fault handler state from signal handler to
331 cell signal_fault_addr;
332 unsigned int signal_fpu_status;
333 stack_frame *signal_callstack_top;
336 bool secure_gc; /* Set by the -securegc command line argument */
337 bool gc_off; /* GC is off during heap walking */
339 /* A heap walk allows useful things to be done, like finding all
340 references to an object for debugging purposes. */
343 cell allot_markers_offset;
345 /* used during garbage collection only */
348 bool performing_compaction;
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. */
356 gc_stats stats[max_gen_count];
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
365 cell last_code_heap_scan;
366 /* sometimes we grow the heap */
367 bool growing_data_heap;
368 data_heap *old_data_heap;
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;
390 unordered_map<heap_block *,char *> forwarding;
393 cell code_relocation_base;
394 cell data_relocation_base;
397 cell megamorphic_cache_hits;
398 cell megamorphic_cache_misses;
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 */