| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-28 16:46:47 -04:00
										 |  |  | void init_globals() | 
					
						
							| 
									
										
										
										
											2009-08-24 15:46:34 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-29 02:23:21 -04:00
										 |  |  | 	init_mvm(); | 
					
						
							| 
									
										
										
										
											2009-08-24 15:46:34 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::default_parameters(vm_parameters *p) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	p->image_path = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	p->datastack_size = 32 * sizeof(cell); | 
					
						
							|  |  |  | 	p->retainstack_size = 32 * sizeof(cell); | 
					
						
							| 
									
										
										
										
											2010-04-02 00:36:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-10 20:26:56 -04:00
										 |  |  | #if defined(__OpenBSD__) && defined(FACTOR_X86)
 | 
					
						
							|  |  |  | 	p->callstack_size = 64 * sizeof(cell); | 
					
						
							|  |  |  | #elif defined(FACTOR_PPC)
 | 
					
						
							| 
									
										
										
										
											2010-04-02 14:09:58 -04:00
										 |  |  | 	p->callstack_size = 256 * sizeof(cell); | 
					
						
							| 
									
										
										
										
											2010-04-02 00:36:45 -04:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	p->callstack_size = 128 * sizeof(cell); | 
					
						
							| 
									
										
										
										
											2010-04-02 00:36:45 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	p->code_size = 8 * sizeof(cell); | 
					
						
							|  |  |  | 	p->young_size = sizeof(cell) / 4; | 
					
						
							|  |  |  | 	p->aging_size = sizeof(cell) / 2; | 
					
						
							| 
									
										
										
										
											2009-10-30 03:26:57 -04:00
										 |  |  | 	p->tenured_size = 24 * sizeof(cell); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	p->max_pic_size = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p->fep = false; | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 	p->signals = true; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef WINDOWS
 | 
					
						
							|  |  |  | 	p->console = false; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:43:22 -05:00
										 |  |  | 	p->console = true; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	p->callback_size = 256; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | bool factor_vm::factor_arg(const vm_char* str, const vm_char* arg, cell* value) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	int val; | 
					
						
							|  |  |  | 	if(SSCANF(str,arg,&val) > 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*value = val; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	default_parameters(p); | 
					
						
							|  |  |  | 	p->executable_path = argv[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(i = 1; i < argc; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 		vm_char *arg = argv[i]; | 
					
						
							| 
									
										
										
										
											2009-10-16 13:13:43 -04:00
										 |  |  | 		if(STRCMP(arg,STRING_LITERAL("--")) == 0) break; | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-datastack=%d"),&p->datastack_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-retainstack=%d"),&p->retainstack_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-callstack=%d"),&p->callstack_size)); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-young=%d"),&p->young_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-aging=%d"),&p->aging_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-tenured=%d"),&p->tenured_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-codeheap=%d"),&p->code_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-pic=%d"),&p->max_pic_size)); | 
					
						
							|  |  |  | 		else if(factor_arg(arg,STRING_LITERAL("-callbacks=%d"),&p->callback_size)); | 
					
						
							|  |  |  | 		else if(STRCMP(arg,STRING_LITERAL("-fep")) == 0) p->fep = true; | 
					
						
							|  |  |  | 		else if(STRCMP(arg,STRING_LITERAL("-nosignals")) == 0) p->signals = false; | 
					
						
							|  |  |  | 		else if(STRNCMP(arg,STRING_LITERAL("-i="),3) == 0) p->image_path = arg + 3; | 
					
						
							|  |  |  | 		else if(STRCMP(arg,STRING_LITERAL("-console")) == 0) p->console = true; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-05 23:55:20 -05:00
										 |  |  | /* Compile code in boot image so that we can execute the startup quotation */ | 
					
						
							|  |  |  | void factor_vm::prepare_boot_image() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "*** Stage 2 early init... "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	fflush(stdout); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	compile_all_words(); | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  | 	update_code_heap_words(true); | 
					
						
							| 
									
										
										
										
											2010-01-05 21:47:36 -05:00
										 |  |  | 	initialize_all_quotations(); | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	special_objects[OBJ_STAGE2] = true_object; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "done\n"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::init_factor(vm_parameters *p) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* Kilobytes */ | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	p->datastack_size = align_page(p->datastack_size << 10); | 
					
						
							|  |  |  | 	p->retainstack_size = align_page(p->retainstack_size << 10); | 
					
						
							| 
									
										
										
										
											2010-03-27 03:35:10 -04:00
										 |  |  | 	p->callstack_size = align_page(p->callstack_size << 10); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 	p->callback_size = align_page(p->callback_size << 10); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Megabytes */ | 
					
						
							|  |  |  | 	p->young_size <<= 20; | 
					
						
							|  |  |  | 	p->aging_size <<= 20; | 
					
						
							|  |  |  | 	p->tenured_size <<= 20; | 
					
						
							|  |  |  | 	p->code_size <<= 20; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Disable GC during init as a sanity check */ | 
					
						
							|  |  |  | 	gc_off = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* OS-specific initialization */ | 
					
						
							|  |  |  | 	early_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	const vm_char *executable_path = vm_executable_path(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(executable_path) | 
					
						
							|  |  |  | 		p->executable_path = executable_path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(p->image_path == NULL) | 
					
						
							|  |  |  | 		p->image_path = default_image_path(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:43:22 -05:00
										 |  |  | 	srand((unsigned int)system_micros()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	init_ffi(); | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	init_contexts(p->datastack_size,p->retainstack_size,p->callstack_size); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 	init_callbacks(p->callback_size); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	load_image(p); | 
					
						
							|  |  |  | 	init_c_io(); | 
					
						
							| 
									
										
										
										
											2010-05-06 01:22:13 -04:00
										 |  |  | 	init_inline_caching((int)p->max_pic_size); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 	if(p->signals) | 
					
						
							|  |  |  | 		init_signals(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(p->console) | 
					
						
							|  |  |  | 		open_console(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	init_profiler(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	special_objects[OBJ_CPU] = allot_alien(false_object,(cell)FACTOR_CPU_STRING); | 
					
						
							|  |  |  | 	special_objects[OBJ_OS] = allot_alien(false_object,(cell)FACTOR_OS_STRING); | 
					
						
							|  |  |  | 	special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell)); | 
					
						
							|  |  |  | 	special_objects[OBJ_EXECUTABLE] = allot_alien(false_object,(cell)p->executable_path); | 
					
						
							|  |  |  | 	special_objects[OBJ_ARGS] = false_object; | 
					
						
							|  |  |  | 	special_objects[OBJ_EMBEDDED] = false_object; | 
					
						
							| 
									
										
										
										
											2010-03-01 16:32:07 -05:00
										 |  |  | 	special_objects[OBJ_VM_COMPILER] = allot_alien(false_object,(cell)FACTOR_COMPILER_VERSION); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* We can GC now */ | 
					
						
							|  |  |  | 	gc_off = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	if(!to_boolean(special_objects[OBJ_STAGE2])) | 
					
						
							| 
									
										
										
										
											2010-01-05 23:55:20 -05:00
										 |  |  | 		prepare_boot_image(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* May allocate memory */ | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::pass_args_to_factor(int argc, vm_char **argv) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:09 -04:00
										 |  |  | 	growable_array args(this); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | 	for(fixnum i = 1; i < argc; i++) | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		args.add(allot_alien(false_object,(cell)argv[i])); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 10:19:09 -04:00
										 |  |  | 	args.trim(); | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	special_objects[OBJ_ARGS] = args.elements.value(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::start_factor(vm_parameters *p) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	if(p->fep) factorbug(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 01:55:16 -05:00
										 |  |  | 	c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 00:28:18 -04:00
										 |  |  | void factor_vm::stop_factor() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-20 01:55:16 -05:00
										 |  |  | 	c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]); | 
					
						
							| 
									
										
										
										
											2009-10-20 00:28:18 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | char *factor_vm::factor_eval_string(char *string) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	char *(*callback)(char *) = (char *(*)(char *))alien_offset(special_objects[OBJ_EVAL_CALLBACK]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	return callback(string); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::factor_eval_free(char *result) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	free(result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::factor_yield() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	void (*callback)() = (void (*)())alien_offset(special_objects[OBJ_YIELD_CALLBACK]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	callback(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::factor_sleep(long us) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	void (*callback)(long) = (void (*)(long))alien_offset(special_objects[OBJ_SLEEP_CALLBACK]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	callback(us); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::start_standalone_factor(int argc, vm_char **argv) | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:09 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-24 13:34:30 -04:00
										 |  |  | 	vm_parameters p; | 
					
						
							|  |  |  | 	default_parameters(&p); | 
					
						
							|  |  |  | 	init_parameters_from_args(&p,argc,argv); | 
					
						
							|  |  |  | 	init_factor(&p); | 
					
						
							|  |  |  | 	pass_args_to_factor(argc,argv); | 
					
						
							|  |  |  | 	start_factor(&p); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:09 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | factor_vm *new_factor_vm() | 
					
						
							| 
									
										
										
										
											2009-08-23 09:36:24 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-05 05:31:35 -04:00
										 |  |  | 	factor_vm *newvm = new factor_vm(); | 
					
						
							| 
									
										
										
										
											2009-08-28 16:46:47 -04:00
										 |  |  | 	register_vm_with_thread(newvm); | 
					
						
							| 
									
										
										
										
											2009-10-02 13:00:01 -04:00
										 |  |  | 	thread_vms[thread_id()] = newvm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return newvm; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-24 13:34:30 -04:00
										 |  |  | VM_C_API void start_standalone_factor(int argc, vm_char **argv) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-02 13:00:01 -04:00
										 |  |  | 	factor_vm *newvm = new_factor_vm(); | 
					
						
							| 
									
										
										
										
											2009-08-24 13:34:30 -04:00
										 |  |  | 	return newvm->start_standalone_factor(argc,argv); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |