void factor_vm::init_factor(vm_parameters *p) { /* Kilobytes */ p->datastack_size = align_page(p->datastack_size << 10); p->retainstack_size = align_page(p->retainstack_size << 10); p->callstack_size = align_page(p->callstack_size << 10); p->callback_size = align_page(p->callback_size << 10); /* 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(); const vm_char *executable_path = vm_executable_path(); if(executable_path) p->executable_path = executable_path; if(p->image_path == NULL) p->image_path = default_image_path(); srand((unsigned int)nano_count()); init_ffi(); init_contexts(p->datastack_size,p->retainstack_size,p->callstack_size); init_callbacks(p->callback_size); load_image(p); init_c_io(); init_inline_caching((int)p->max_pic_size); if(p->signals) init_signals(); if(p->console) open_console(); init_profiler(); 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; special_objects[OBJ_VM_COMPILER] = allot_alien(false_object,(cell)FACTOR_COMPILER_VERSION); /* We can GC now */ gc_off = false; if(!to_boolean(special_objects[OBJ_STAGE2])) prepare_boot_image(); }
void factorvm::init_factor(vm_parameters *p) { /* Kilobytes */ p->ds_size = align_page(p->ds_size << 10); p->rs_size = align_page(p->rs_size << 10); /* 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(); const vm_char *executable_path = vm_executable_path(); if(executable_path) p->executable_path = executable_path; if(p->image_path == NULL) p->image_path = default_image_path(); srand(current_micros()); init_ffi(); init_stacks(p->ds_size,p->rs_size); load_image(p); init_c_io(); init_inline_caching(p->max_pic_size); init_signals(); if(p->console) open_console(); init_profiler(); userenv[CPU_ENV] = allot_alien(F,(cell)FACTOR_CPU_STRING); userenv[OS_ENV] = allot_alien(F,(cell)FACTOR_OS_STRING); userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(cell)); userenv[EXECUTABLE_ENV] = allot_alien(F,(cell)p->executable_path); userenv[ARGS_ENV] = F; userenv[EMBEDDED_ENV] = F; /* We can GC now */ gc_off = false; if(userenv[STAGE2_ENV] == F) { userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces); do_stage1_init(); } }
/* make an alien and push */ void box_alien(void *ptr) { if(ptr == NULL) dpush(F); else dpush(allot_alien(F,(CELL)ptr)); }
/* This function also initializes the data and code heaps */ void factor_vm::load_image(vm_parameters* p) { FILE* file = open_image(p); if (file == NULL) { std::cout << "Cannot open image file: " << p->image_path << std::endl; char *msg = threadsafe_strerror(errno); std::cout << "strerror:2: " << msg << std::endl; free(msg); exit(1); } image_header h; if (raw_fread(&h, sizeof(image_header), 1, file) != 1) fatal_error("Cannot read image header", 0); if (h.magic != image_magic) fatal_error("Bad image: magic number check failed", h.magic); if (h.version != image_version) fatal_error("Bad image: version number check failed", h.version); load_data_heap(file, &h, p); load_code_heap(file, &h, p); raw_fclose(file); /* Certain special objects in the image are known to the runtime */ memcpy(special_objects, h.special_objects, sizeof(special_objects)); cell data_offset = data->tenured->start - h.data_relocation_base; cell code_offset = code->allocator->start - h.code_relocation_base; fixup_heaps(data_offset, code_offset); /* Store image path name */ special_objects[OBJ_IMAGE] = allot_alien(false_object, (cell)p->image_path); }
/* make an alien and push */ void factorvm::box_alien(void *ptr) { if(ptr == NULL) dpush(F); else dpush(allot_alien(F,(cell)ptr)); }
/* This function also initializes the data and code heaps */ void load_image(vm_parameters *p) { FILE *file = OPEN_READ(p->image_path); if(file == NULL) { print_string("Cannot open image file: "); print_native_string(p->image_path); nl(); print_string(strerror(errno)); nl(); exit(1); } image_header h; if(fread(&h,sizeof(image_header),1,file) != 1) fatal_error("Cannot read image header",0); if(h.magic != image_magic) fatal_error("Bad image: magic number check failed",h.magic); if(h.version != image_version) fatal_error("Bad image: version number check failed",h.version); load_data_heap(file,&h,p); load_code_heap(file,&h,p); fclose(file); init_objects(&h); relocate_data(); relocate_code(); /* Store image path name */ userenv[IMAGE_ENV] = allot_alien(F,(cell)p->image_path); }
// look up a symbol in a native library // Allocates memory void factor_vm::primitive_dlsym_raw() { data_root<object> library(ctx->pop(), this); data_root<byte_array> name(ctx->peek(), this); check_tagged(name); symbol_char* sym = name->data<symbol_char>(); if (to_boolean(library.value())) { dll* d = untag_check<dll>(library.value()); if (d->handle == NULL) ctx->replace(false_object); else ctx->replace(allot_alien(ffi_dlsym_raw(d, sym))); } else ctx->replace(allot_alien(ffi_dlsym_raw(NULL, sym))); }
void factor_vm::primitive_callback() { cell return_rewind = to_cell(ctx->pop()); tagged<word> w(ctx->pop()); w.untag_check(this); ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point())); }
/* May allocate memory */ void factor_vm::pass_args_to_factor(int argc, vm_char** argv) { growable_array args(this); for (fixnum i = 1; i < argc; i++) args.add(allot_alien(false_object, (cell)argv[i])); args.trim(); special_objects[OBJ_ARGS] = args.elements.value(); }
/* Allocates memory (add(), allot_alien())*/ void factor_vm::primitive_callback() { cell return_rewind = to_cell(ctx->pop()); tagged<word> w(ctx->pop()); w.untag_check(this); cell func = callbacks->add(w.value(), return_rewind)->entry_point(); CODE_TO_FUNCTION_POINTER_CALLBACK(this, func); ctx->push(allot_alien(func)); }
void factor_vm::primitive_fopen() { data_root<byte_array> mode(ctx->pop(), this); data_root<byte_array> path(ctx->pop(), this); mode.untag_check(this); path.untag_check(this); FILE* file; file = safe_fopen((char*)(path.untagged() + 1), (char*)(mode.untagged() + 1)); ctx->push(allot_alien(file)); }
/* May allocate memory */ void factorvm::pass_args_to_factor(int argc, vm_char **argv) { growable_array args(this); int i; for(i = 1; i < argc; i++){ args.add(allot_alien(F,(cell)argv[i])); } args.trim(); userenv[ARGS_ENV] = args.elements.value(); }
/* make an alien pointing at an offset of another alien */ inline void factorvm::vmprim_displaced_alien() { cell alien = dpop(); cell displacement = to_cell(dpop()); if(alien == F && displacement == 0) dpush(F); else { switch(tagged<object>(alien).type()) { case BYTE_ARRAY_TYPE: case ALIEN_TYPE: case F_TYPE: dpush(allot_alien(alien,displacement)); break; default: type_error(ALIEN_TYPE,alien); break; } } }
/* This function also initializes the data and code heaps */ void factor_vm::load_image(vm_parameters *p) { FILE *file = OPEN_READ(p->image_path); if(file == NULL) { std::cout << "Cannot open image file: " << p->image_path << std::endl; std::cout << strerror(errno) << std::endl; exit(1); } image_header h; if(safe_fread(&h,sizeof(image_header),1,file) != 1) fatal_error("Cannot read image header",0); if(h.magic != image_magic) fatal_error("Bad image: magic number check failed",h.magic); if(h.version != image_version) fatal_error("Bad image: version number check failed",h.version); load_data_heap(file,&h,p); load_code_heap(file,&h,p); safe_fclose(file); init_objects(&h); cell data_offset = data->tenured->start - h.data_relocation_base; cell code_offset = code->seg->start - h.code_relocation_base; fixup_data(data_offset,code_offset); fixup_code(data_offset,code_offset); /* Store image path name */ special_objects[OBJ_IMAGE] = allot_alien(false_object,(cell)p->image_path); }
void init_c_io(void) { userenv[STDIN_ENV] = allot_alien(F,(CELL)stdin); userenv[STDOUT_ENV] = allot_alien(F,(CELL)stdout); userenv[STDERR_ENV] = allot_alien(F,(CELL)stderr); }
void factor_vm::primitive_callstack_bounds() { ctx->push(allot_alien((void*)ctx->callstack_seg->start)); ctx->push(allot_alien((void*)ctx->callstack_seg->end)); }
void factor_vm::init_c_io() { special_objects[OBJ_STDIN] = allot_alien(false_object, (cell)stdin); special_objects[OBJ_STDOUT] = allot_alien(false_object, (cell)stdout); special_objects[OBJ_STDERR] = allot_alien(false_object, (cell)stderr); }
/* Allocates memory */ void factor_vm::init_context(context* ctx) { ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx); }
void factor_vm::init_factor(vm_parameters* p) { // Kilobytes p->datastack_size = align_page(p->datastack_size << 10); p->retainstack_size = align_page(p->retainstack_size << 10); p->callstack_size = align_page(p->callstack_size << 10); p->callback_size = align_page(p->callback_size << 10); // 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(); p->executable_path = vm_executable_path(); if (p->image_path == NULL) { if (embedded_image_p()) { p->embedded_image = true; p->image_path = safe_strdup(p->executable_path); } else p->image_path = default_image_path(); } srand((unsigned int)nano_count()); init_ffi(); datastack_size = p->datastack_size; retainstack_size = p->retainstack_size; callstack_size = p->callstack_size; ctx = NULL; spare_ctx = new_context(); callbacks = new callback_heap(p->callback_size, this); load_image(p); max_pic_size = (int)p->max_pic_size; special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell)); special_objects[OBJ_ARGS] = false_object; special_objects[OBJ_EMBEDDED] = false_object; cell aliens[][2] = { {OBJ_STDIN, (cell)stdin}, {OBJ_STDOUT, (cell)stdout}, {OBJ_STDERR, (cell)stderr}, {OBJ_CPU, (cell)FACTOR_CPU_STRING}, {OBJ_EXECUTABLE, (cell)safe_strdup(p->executable_path)}, {OBJ_IMAGE, (cell)safe_strdup(p->image_path)}, {OBJ_OS, (cell)FACTOR_OS_STRING}, {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME}, {OBJ_VM_COMPILER, (cell)FACTOR_COMPILER_VERSION}, {OBJ_VM_GIT_LABEL, (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)}, {OBJ_VM_VERSION, (cell)FACTOR_STRINGIZE(FACTOR_VERSION)}, #if defined(WINDOWS) {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler} #endif }; int n_items = sizeof(aliens) / sizeof(cell[2]); for (int n = 0; n < n_items; n++) { cell idx = aliens[n][0]; special_objects[idx] = allot_alien(false_object, aliens[n][1]); } // We can GC now gc_off = false; if (!to_boolean(special_objects[OBJ_STAGE2])) prepare_boot_image(); if (p->signals) init_signals(); if (p->console) open_console(); }