From 28620619e93465e0b7248e1a498874d519e5699b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH] moved alien functions to vm --- vm/alien.cpp | 112 +++++++++++++++++++++++++++++++++++------ vm/callstack.cpp | 126 ++++++++++++++++++++++++++++++++++++++++------- vm/data_gc.cpp | 2 +- vm/profiler.cpp | 2 +- vm/vm.hpp | 38 ++++++++++++++ 5 files changed, 244 insertions(+), 36 deletions(-) mode change 100644 => 100755 vm/alien.cpp mode change 100644 => 100755 vm/callstack.cpp diff --git a/vm/alien.cpp b/vm/alien.cpp old mode 100644 new mode 100755 index 13764a8e50..3a99d89afb --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -5,7 +5,7 @@ namespace factor /* gets the address of an object representing a C pointer, with the intention of storing the pointer across code which may potentially GC. */ -char *pinned_alien_offset(cell obj) +char *factorvm::pinned_alien_offset(cell obj) { switch(tagged(obj).type()) { @@ -24,8 +24,13 @@ char *pinned_alien_offset(cell obj) } } +char *pinned_alien_offset(cell obj) +{ + return vm->pinned_alien_offset(obj); +} + /* make an alien */ -cell allot_alien(cell delegate_, cell displacement) +cell factorvm::allot_alien(cell delegate_, cell displacement) { gc_root delegate(delegate_); gc_root new_alien(allot(sizeof(alien))); @@ -45,8 +50,13 @@ cell allot_alien(cell delegate_, cell displacement) return new_alien.value(); } +cell allot_alien(cell delegate_, cell displacement) +{ + return vm->allot_alien(delegate_,displacement); +} + /* make an alien pointing at an offset of another alien */ -PRIMITIVE(displaced_alien) +inline void factorvm::vmprim_displaced_alien() { cell alien = dpop(); cell displacement = to_cell(dpop()); @@ -69,20 +79,35 @@ PRIMITIVE(displaced_alien) } } +PRIMITIVE(displaced_alien) +{ + PRIMITIVE_GETVM()->vmprim_displaced_alien(); +} + /* address of an object representing a C pointer. Explicitly throw an error if the object is a byte array, as a sanity check. */ -PRIMITIVE(alien_address) +inline void factorvm::vmprim_alien_address() { box_unsigned_cell((cell)pinned_alien_offset(dpop())); } +PRIMITIVE(alien_address) +{ + PRIMITIVE_GETVM()->vmprim_alien_address(); +} + /* pop ( alien n ) from datastack, return alien's address plus n */ -static void *alien_pointer() +void *factorvm::alien_pointer() { fixnum offset = to_fixnum(dpop()); return unbox_alien() + offset; } +void *alien_pointer() +{ + return vm->alien_pointer(); +} + /* define words to read/write values at an alien address */ #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ @@ -111,7 +136,7 @@ DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double) DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) /* open a native library and push a handle */ -PRIMITIVE(dlopen) +inline void factorvm::vmprim_dlopen() { gc_root path(dpop()); path.untag_check(); @@ -121,8 +146,13 @@ PRIMITIVE(dlopen) dpush(library.value()); } +PRIMITIVE(dlopen) +{ + PRIMITIVE_GETVM()->vmprim_dlopen(); +} + /* look up a symbol in a native library */ -PRIMITIVE(dlsym) +inline void factorvm::vmprim_dlsym() { gc_root library(dpop()); gc_root name(dpop()); @@ -143,15 +173,25 @@ PRIMITIVE(dlsym) } } +PRIMITIVE(dlsym) +{ + PRIMITIVE_GETVM()->vmprim_dlsym(); +} + /* close a native library handle */ -PRIMITIVE(dlclose) +inline void factorvm::vmprim_dlclose() { dll *d = untag_check(dpop()); if(d->dll != NULL) ffi_dlclose(d); } -PRIMITIVE(dll_validp) +PRIMITIVE(dlclose) +{ + PRIMITIVE_GETVM()->vmprim_dlclose(); +} + +inline void factorvm::vmprim_dll_validp() { cell library = dpop(); if(library == F) @@ -160,8 +200,13 @@ PRIMITIVE(dll_validp) dpush(untag_check(library)->dll == NULL ? F : T); } +PRIMITIVE(dll_validp) +{ + PRIMITIVE_GETVM()->vmprim_dll_validp(); +} + /* gets the address of an object representing a C pointer */ -VM_C_API char *alien_offset(cell obj) +char *factorvm::alien_offset(cell obj) { switch(tagged(obj).type()) { @@ -182,14 +227,24 @@ VM_C_API char *alien_offset(cell obj) } } +VM_C_API char *alien_offset(cell obj) +{ + return vm->alien_offset(obj); +} + /* pop an object representing a C pointer */ -VM_C_API char *unbox_alien() +char *factorvm::unbox_alien() { return alien_offset(dpop()); } +VM_C_API char *unbox_alien() +{ + return vm->unbox_alien(); +} + /* make an alien and push */ -VM_C_API void box_alien(void *ptr) +void factorvm::box_alien(void *ptr) { if(ptr == NULL) dpush(F); @@ -197,22 +252,37 @@ VM_C_API void box_alien(void *ptr) dpush(allot_alien(F,(cell)ptr)); } +VM_C_API void box_alien(void *ptr) +{ + return vm->box_alien(ptr); +} + /* for FFI calls passing structs by value */ -VM_C_API void to_value_struct(cell src, void *dest, cell size) +void factorvm::to_value_struct(cell src, void *dest, cell size) { memcpy(dest,alien_offset(src),size); } +VM_C_API void to_value_struct(cell src, void *dest, cell size) +{ + return vm->to_value_struct(src,dest,size); +} + /* for FFI callbacks receiving structs by value */ -VM_C_API void box_value_struct(void *src, cell size) +void factorvm::box_value_struct(void *src, cell size) { byte_array *bytes = allot_byte_array(size); memcpy(bytes->data(),src,size); dpush(tag(bytes)); } +VM_C_API void box_value_struct(void *src, cell size) +{ + return vm->box_value_struct(src,size); +} + /* On some x86 OSes, structs <= 8 bytes are returned in registers. */ -VM_C_API void box_small_struct(cell x, cell y, cell size) +void factorvm::box_small_struct(cell x, cell y, cell size) { cell data[2]; data[0] = x; @@ -220,8 +290,13 @@ VM_C_API void box_small_struct(cell x, cell y, cell size) box_value_struct(data,size); } +VM_C_API void box_small_struct(cell x, cell y, cell size) +{ + return vm->box_small_struct(x,y,size); +} + /* On OS X/PPC, complex numbers are returned in registers. */ -VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) +void factorvm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) { cell data[4]; data[0] = x1; @@ -231,4 +306,9 @@ VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) box_value_struct(data,size); } +VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) +{ + return vm->box_medium_struct(x1, x2, x3, x4, size); +} + } diff --git a/vm/callstack.cpp b/vm/callstack.cpp old mode 100644 new mode 100755 index 39988ae976..e82314b8d2 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -3,7 +3,7 @@ namespace factor { -static void check_frame(stack_frame *frame) +void factorvm::check_frame(stack_frame *frame) { #ifdef FACTOR_DEBUG check_code_pointer((cell)frame->xt); @@ -11,14 +11,24 @@ static void check_frame(stack_frame *frame) #endif } -callstack *allot_callstack(cell size) +void check_frame(stack_frame *frame) +{ + return vm->check_frame(frame); +} + +callstack *factorvm::allot_callstack(cell size) { callstack *stack = allot(callstack_size(size)); stack->length = tag_fixnum(size); return stack; } -stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) +callstack *allot_callstack(cell size) +{ + return vm->allot_callstack(size); +} + +stack_frame *factorvm::fix_callstack_top(stack_frame *top, stack_frame *bottom) { stack_frame *frame = bottom - 1; @@ -28,6 +38,11 @@ stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) return frame + 1; } +stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) +{ + return vm->fix_callstack_top(top,bottom); +} + /* We ignore the topmost frame, the one calling 'callstack', so that set-callstack doesn't get stuck in an infinite loop. @@ -35,7 +50,7 @@ This means that if 'callstack' is called in tail position, we will have popped a necessary frame... however this word is only called by continuation implementation, and user code shouldn't be calling it at all, so we leave it as it is for now. */ -stack_frame *capture_start() +stack_frame *factorvm::capture_start() { stack_frame *frame = stack_chain->callstack_bottom - 1; while(frame >= stack_chain->callstack_top @@ -46,7 +61,12 @@ stack_frame *capture_start() return frame + 1; } -PRIMITIVE(callstack) +stack_frame *capture_start() +{ + return vm->capture_start(); +} + +inline void factorvm::vmprim_callstack() { stack_frame *top = capture_start(); stack_frame *bottom = stack_chain->callstack_bottom; @@ -60,7 +80,12 @@ PRIMITIVE(callstack) dpush(tag(stack)); } -PRIMITIVE(set_callstack) +PRIMITIVE(callstack) +{ + PRIMITIVE_GETVM()->vmprim_callstack(); +} + +inline void factorvm::vmprim_set_callstack() { callstack *stack = untag_check(dpop()); @@ -73,18 +98,33 @@ PRIMITIVE(set_callstack) critical_error("Bug in set_callstack()",0); } -code_block *frame_code(stack_frame *frame) +PRIMITIVE(set_callstack) +{ + PRIMITIVE_GETVM()->vmprim_set_callstack(); +} + +code_block *factorvm::frame_code(stack_frame *frame) { check_frame(frame); return (code_block *)frame->xt - 1; } -cell frame_type(stack_frame *frame) +code_block *frame_code(stack_frame *frame) +{ + return vm->frame_code(frame); +} + +cell factorvm::frame_type(stack_frame *frame) { return frame_code(frame)->type; } -cell frame_executing(stack_frame *frame) +cell frame_type(stack_frame *frame) +{ + return vm->frame_type(frame); +} + +cell factorvm::frame_executing(stack_frame *frame) { code_block *compiled = frame_code(frame); if(compiled->literals == F || !stack_traces_p()) @@ -98,14 +138,24 @@ cell frame_executing(stack_frame *frame) } } -stack_frame *frame_successor(stack_frame *frame) +cell frame_executing(stack_frame *frame) +{ + return vm->frame_executing(frame); +} + +stack_frame *factorvm::frame_successor(stack_frame *frame) { check_frame(frame); return (stack_frame *)((cell)frame - frame->size); } +stack_frame *frame_successor(stack_frame *frame) +{ + return vm->frame_successor(frame); +} + /* Allocates memory */ -cell frame_scan(stack_frame *frame) +cell factorvm::frame_scan(stack_frame *frame) { switch(frame_type(frame)) { @@ -131,6 +181,11 @@ cell frame_scan(stack_frame *frame) } } +cell frame_scan(stack_frame *frame) +{ + return vm->frame_scan(frame); +} + namespace { @@ -149,7 +204,7 @@ struct stack_frame_accumulator { } -PRIMITIVE(callstack_to_array) +inline void factorvm::vmprim_callstack_to_array() { gc_root callstack(dpop()); @@ -160,7 +215,12 @@ PRIMITIVE(callstack_to_array) dpush(accum.frames.elements.value()); } -stack_frame *innermost_stack_frame(callstack *stack) +PRIMITIVE(callstack_to_array) +{ + PRIMITIVE_GETVM()->vmprim_callstack_to_array(); +} + +stack_frame *factorvm::innermost_stack_frame(callstack *stack) { stack_frame *top = stack->top(); stack_frame *bottom = stack->bottom(); @@ -172,26 +232,46 @@ stack_frame *innermost_stack_frame(callstack *stack) return frame; } -stack_frame *innermost_stack_frame_quot(callstack *callstack) +stack_frame *innermost_stack_frame(callstack *stack) +{ + return vm->innermost_stack_frame(stack); +} + +stack_frame *factorvm::innermost_stack_frame_quot(callstack *callstack) { stack_frame *inner = innermost_stack_frame(callstack); tagged(frame_executing(inner)).untag_check(); return inner; } +stack_frame *innermost_stack_frame_quot(callstack *callstack) +{ + return vm->innermost_stack_frame_quot(callstack); +} + /* Some primitives implementing a limited form of callstack mutation. Used by the single stepper. */ -PRIMITIVE(innermost_stack_frame_executing) +inline void factorvm::vmprim_innermost_stack_frame_executing() { dpush(frame_executing(innermost_stack_frame(untag_check(dpop())))); } -PRIMITIVE(innermost_stack_frame_scan) +PRIMITIVE(innermost_stack_frame_executing) +{ + PRIMITIVE_GETVM()->vmprim_innermost_stack_frame_executing(); +} + +inline void factorvm::vmprim_innermost_stack_frame_scan() { dpush(frame_scan(innermost_stack_frame_quot(untag_check(dpop())))); } -PRIMITIVE(set_innermost_stack_frame_quot) +PRIMITIVE(innermost_stack_frame_scan) +{ + PRIMITIVE_GETVM()->vmprim_innermost_stack_frame_scan(); +} + +inline void factorvm::vmprim_set_innermost_stack_frame_quot() { gc_root callstack(dpop()); gc_root quot(dpop()); @@ -207,10 +287,20 @@ PRIMITIVE(set_innermost_stack_frame_quot) FRAME_RETURN_ADDRESS(inner) = (char *)quot->xt + offset; } +PRIMITIVE(set_innermost_stack_frame_quot) +{ + PRIMITIVE_GETVM()->vmprim_set_innermost_stack_frame_quot(); +} + /* called before entry into Factor code. */ -VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom) +void factorvm::save_callstack_bottom(stack_frame *callstack_bottom) { stack_chain->callstack_bottom = callstack_bottom; } +VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom) +{ + return vm->save_callstack_bottom(callstack_bottom); +} + } diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index ea7100fc02..35d6812290 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -686,7 +686,7 @@ void factorvm::garbage_collection(cell gen,bool growing_data_heap_,cell requeste code_heap_scans++; if(collecting_gen == data->tenured()) - free_unmarked(&code,(heap_iterator)update_literal_and_word_references); + free_unmarked(&code,(heap_iterator)factor::update_literal_and_word_references); else copy_code_heap_roots(); diff --git a/vm/profiler.cpp b/vm/profiler.cpp index 7b8f04a3bd..054fe01537 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -56,7 +56,7 @@ void factorvm::set_profiling(bool profiling) } /* Update XTs in code heap */ - iterate_code_heap(relocate_code_block); + iterate_code_heap(factor::relocate_code_block); } void set_profiling(bool profiling) diff --git a/vm/vm.hpp b/vm/vm.hpp index 21effc10c3..063627b9b1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -417,6 +417,44 @@ struct factorvm { void fixup_code_block(code_block *compiled); void relocate_code(); void load_image(vm_parameters *p); + + //callstack + void check_frame(stack_frame *frame); + callstack *allot_callstack(cell size); + stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom); + stack_frame *capture_start(); + inline void vmprim_callstack(); + inline void vmprim_set_callstack(); + code_block *frame_code(stack_frame *frame); + cell frame_type(stack_frame *frame); + cell frame_executing(stack_frame *frame); + stack_frame *frame_successor(stack_frame *frame); + cell frame_scan(stack_frame *frame); + inline void vmprim_callstack_to_array(); + stack_frame *innermost_stack_frame(callstack *stack); + stack_frame *innermost_stack_frame_quot(callstack *callstack); + inline void vmprim_innermost_stack_frame_executing(); + inline void vmprim_innermost_stack_frame_scan(); + inline void vmprim_set_innermost_stack_frame_quot(); + void save_callstack_bottom(stack_frame *callstack_bottom); + + //alien + char *pinned_alien_offset(cell obj); + cell allot_alien(cell delegate_, cell displacement); + inline void vmprim_displaced_alien(); + inline void vmprim_alien_address(); + void *alien_pointer(); + inline void vmprim_dlopen(); + inline void vmprim_dlsym(); + inline void vmprim_dlclose(); + inline void vmprim_dll_validp(); + char *alien_offset(cell obj); + char *unbox_alien(); + void box_alien(void *ptr); + void to_value_struct(cell src, void *dest, cell size); + void box_value_struct(void *src, cell size); + void box_small_struct(cell x, cell y, cell size); + void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size); // next method here: