From: Erik Charlebois Date: Sun, 12 May 2013 01:47:34 +0000 (-0400) Subject: VM: Refactor callstack to Factor style X-Git-Tag: 0.97~1312 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=2e20733adead769a4fb153c14e722f335ffb7337 VM: Refactor callstack to Factor style --- diff --git a/vm/callstack.cpp b/vm/callstack.cpp index 15be713df0..4088ba33bc 100644 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -1,13 +1,11 @@ #include "master.hpp" -namespace factor -{ - -callstack *factor_vm::allot_callstack(cell size) -{ - callstack *stack = allot(callstack_object_size(size)); - stack->length = tag_fixnum(size); - return stack; +namespace factor { + +callstack* factor_vm::allot_callstack(cell size) { + callstack* stack = allot(callstack_object_size(size)); + stack->length = tag_fixnum(size); + return stack; } /* We ignore the two topmost frames, the 'callstack' primitive @@ -18,129 +16,118 @@ 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. */ -void *factor_vm::second_from_top_stack_frame(context *ctx) -{ - void *frame_top = ctx->callstack_top; - for (cell i = 0; i < 2; ++i) - { - void *pred = frame_predecessor(frame_top); - if (pred >= ctx->callstack_bottom) - return frame_top; - frame_top = pred; - } - return frame_top; +void* factor_vm::second_from_top_stack_frame(context* ctx) { + void* frame_top = ctx->callstack_top; + for (cell i = 0; i < 2; ++i) { + void* pred = frame_predecessor(frame_top); + if (pred >= ctx->callstack_bottom) + return frame_top; + frame_top = pred; + } + return frame_top; } -cell factor_vm::capture_callstack(context *ctx) -{ - void *top = second_from_top_stack_frame(ctx); - void *bottom = ctx->callstack_bottom; +cell factor_vm::capture_callstack(context* ctx) { + void* top = second_from_top_stack_frame(ctx); + void* bottom = ctx->callstack_bottom; - fixnum size = std::max((fixnum)0,(fixnum)bottom - (fixnum)top); + fixnum size = std::max((fixnum) 0, (fixnum) bottom - (fixnum) top); - callstack *stack = allot_callstack(size); - memcpy(stack->top(),top,size); - return tag(stack); + callstack* stack = allot_callstack(size); + memcpy(stack->top(), top, size); + return tag(stack); } -void factor_vm::primitive_callstack() -{ - ctx->push(capture_callstack(ctx)); -} +void factor_vm::primitive_callstack() { ctx->push(capture_callstack(ctx)); } -void factor_vm::primitive_callstack_for() -{ - context *other_ctx = (context *)pinned_alien_offset(ctx->peek()); - ctx->replace(capture_callstack(other_ctx)); +void factor_vm::primitive_callstack_for() { + context* other_ctx = (context*)pinned_alien_offset(ctx->peek()); + ctx->replace(capture_callstack(other_ctx)); } -void *factor_vm::frame_predecessor(void *frame_top) -{ - void *addr = frame_return_address((void*)frame_top); - FACTOR_ASSERT(addr != 0); - code_block *owner = code->code_block_for_address((cell)addr); - cell frame_size = owner->stack_frame_size_for_address((cell)addr); - return (void*)((char*)frame_top + frame_size); +void* factor_vm::frame_predecessor(void* frame_top) { + void* addr = frame_return_address((void*)frame_top); + FACTOR_ASSERT(addr != 0); + code_block* owner = code->code_block_for_address((cell) addr); + cell frame_size = owner->stack_frame_size_for_address((cell) addr); + return (void*)((char*)frame_top + frame_size); } struct stack_frame_accumulator { - factor_vm *parent; - growable_array frames; - - explicit stack_frame_accumulator(factor_vm *parent_) - : parent(parent_), frames(parent_) {} - - void operator()(void *frame_top, cell frame_size, code_block *owner, void *addr) - { - data_root executing_quot(owner->owner_quot(),parent); - data_root executing(owner->owner,parent); - data_root scan(owner->scan(parent, addr),parent); - - frames.add(executing.value()); - frames.add(executing_quot.value()); - frames.add(scan.value()); - } + factor_vm* parent; + growable_array frames; + + explicit stack_frame_accumulator(factor_vm* parent_) + : parent(parent_), frames(parent_) {} + + void operator()(void* frame_top, cell frame_size, code_block* owner, + void* addr) { + data_root executing_quot(owner->owner_quot(), parent); + data_root executing(owner->owner, parent); + data_root scan(owner->scan(parent, addr), parent); + + frames.add(executing.value()); + frames.add(executing_quot.value()); + frames.add(scan.value()); + } }; -struct stack_frame_in_array { cell cells[3]; }; +struct stack_frame_in_array { + cell cells[3]; +}; -void factor_vm::primitive_callstack_to_array() -{ - data_root callstack(ctx->peek(),this); +void factor_vm::primitive_callstack_to_array() { + data_root callstack(ctx->peek(), this); - stack_frame_accumulator accum(this); - iterate_callstack_object(callstack.untagged(),accum); + stack_frame_accumulator accum(this); + iterate_callstack_object(callstack.untagged(), accum); - /* The callstack iterator visits frames in reverse order (top to bottom) */ - std::reverse( - (stack_frame_in_array*)accum.frames.elements->data(), - (stack_frame_in_array*)(accum.frames.elements->data() + accum.frames.count)); + /* The callstack iterator visits frames in reverse order (top to bottom) */ + std::reverse((stack_frame_in_array*)accum.frames.elements->data(), + (stack_frame_in_array*)(accum.frames.elements->data() + + accum.frames.count)); - accum.frames.trim(); + accum.frames.trim(); - ctx->replace(accum.frames.elements.value()); + ctx->replace(accum.frames.elements.value()); } /* Some primitives implementing a limited form of callstack mutation. Used by the single stepper. */ -void factor_vm::primitive_innermost_stack_frame_executing() -{ - callstack *stack = untag_check(ctx->peek()); - void *frame = stack->top(); - void *addr = frame_return_address(frame); - ctx->replace(code->code_block_for_address((cell)addr)->owner_quot()); +void factor_vm::primitive_innermost_stack_frame_executing() { + callstack* stack = untag_check(ctx->peek()); + void* frame = stack->top(); + void* addr = frame_return_address(frame); + ctx->replace(code->code_block_for_address((cell) addr)->owner_quot()); } -void factor_vm::primitive_innermost_stack_frame_scan() -{ - callstack *stack = untag_check(ctx->peek()); - void *frame = stack->top(); - void *addr = frame_return_address(frame); - ctx->replace(code->code_block_for_address((cell)addr)->scan(this,addr)); +void factor_vm::primitive_innermost_stack_frame_scan() { + callstack* stack = untag_check(ctx->peek()); + void* frame = stack->top(); + void* addr = frame_return_address(frame); + ctx->replace(code->code_block_for_address((cell) addr)->scan(this, addr)); } -void factor_vm::primitive_set_innermost_stack_frame_quot() -{ - data_root stack(ctx->pop(),this); - data_root quot(ctx->pop(),this); +void factor_vm::primitive_set_innermost_stack_frame_quot() { + data_root stack(ctx->pop(), this); + data_root quot(ctx->pop(), this); - stack.untag_check(this); - quot.untag_check(this); + stack.untag_check(this); + quot.untag_check(this); - jit_compile_quot(quot.value(),true); + jit_compile_quot(quot.value(), true); - void *inner = stack->top(); - void *addr = frame_return_address(inner); - code_block *block = code->code_block_for_address((cell)addr); - cell offset = block->offset(addr); - set_frame_return_address(inner, (char*)quot->entry_point + offset); + void* inner = stack->top(); + void* addr = frame_return_address(inner); + code_block* block = code->code_block_for_address((cell) addr); + cell offset = block->offset(addr); + set_frame_return_address(inner, (char*)quot->entry_point + offset); } -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::primitive_callstack_bounds() { + ctx->push(allot_alien((void*)ctx->callstack_seg->start)); + ctx->push(allot_alien((void*)ctx->callstack_seg->end)); } } diff --git a/vm/callstack.hpp b/vm/callstack.hpp index f4fbac2d91..5fee93a372 100644 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -1,82 +1,75 @@ -namespace factor -{ +namespace factor { -inline static cell callstack_object_size(cell size) -{ - return sizeof(callstack) + size; +inline static cell callstack_object_size(cell size) { + return sizeof(callstack) + size; } /* This is a little tricky. The iterator may allocate memory, so we keep the callstack in a GC root and use relative offsets */ -template -inline void factor_vm::iterate_callstack_object(callstack *stack_, - Iterator &iterator, Fixup &fixup) -{ - data_root stack(stack_,this); - fixnum frame_length = factor::untag_fixnum(stack->length); - fixnum frame_offset = 0; - - while(frame_offset < frame_length) - { - void *frame_top = stack->frame_top_at(frame_offset); - void *addr = frame_return_address(frame_top); - - void *fixed_addr = Fixup::translated_code_block_map - ? (void*)fixup.translate_code((code_block*)addr) - : addr; - code_block *owner = code->code_block_for_address((cell)fixed_addr); - cell frame_size = owner->stack_frame_size_for_address((cell)fixed_addr); - - iterator(frame_top, frame_size, owner, fixed_addr); - frame_offset += frame_size; - } +template +inline void factor_vm::iterate_callstack_object(callstack* stack_, + Iterator& iterator, + Fixup& fixup) { + data_root stack(stack_, this); + fixnum frame_length = factor::untag_fixnum(stack->length); + fixnum frame_offset = 0; + + while (frame_offset < frame_length) { + void* frame_top = stack->frame_top_at(frame_offset); + void* addr = frame_return_address(frame_top); + + void* fixed_addr = Fixup::translated_code_block_map + ? (void*)fixup.translate_code((code_block*)addr) + : addr; + code_block* owner = code->code_block_for_address((cell) fixed_addr); + cell frame_size = owner->stack_frame_size_for_address((cell) fixed_addr); + + iterator(frame_top, frame_size, owner, fixed_addr); + frame_offset += frame_size; + } } -template -inline void factor_vm::iterate_callstack_object(callstack *stack_, Iterator &iterator) -{ - no_fixup none; - iterate_callstack_object(stack_, iterator, none); +template +inline void factor_vm::iterate_callstack_object(callstack* stack_, + Iterator& iterator) { + no_fixup none; + iterate_callstack_object(stack_, iterator, none); } -template -inline void factor_vm::iterate_callstack(context *ctx, Iterator &iterator, Fixup &fixup) -{ - if (ctx->callstack_top == ctx->callstack_bottom) - return; +template +inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator, + Fixup& fixup) { + if (ctx->callstack_top == ctx->callstack_bottom) + return; - char *frame_top = (char*)ctx->callstack_top; + char* frame_top = (char*)ctx->callstack_top; - while (frame_top < (char*)ctx->callstack_bottom) - { - void *addr = frame_return_address((void*)frame_top); - FACTOR_ASSERT(addr != 0); - void *fixed_addr = Fixup::translated_code_block_map - ? (void*)fixup.translate_code((code_block*)addr) - : addr; + while (frame_top < (char*)ctx->callstack_bottom) { + void* addr = frame_return_address((void*)frame_top); + FACTOR_ASSERT(addr != 0); + void* fixed_addr = Fixup::translated_code_block_map + ? (void*)fixup.translate_code((code_block*)addr) + : addr; - code_block *owner = code->code_block_for_address((cell)fixed_addr); - code_block *fixed_owner = Fixup::translated_code_block_map - ? owner - : fixup.translate_code(owner); + code_block* owner = code->code_block_for_address((cell) fixed_addr); + code_block* fixed_owner = + Fixup::translated_code_block_map ? owner : fixup.translate_code(owner); - cell frame_size = fixed_owner->stack_frame_size_for_address((cell)fixed_addr); + cell frame_size = + fixed_owner->stack_frame_size_for_address((cell) fixed_addr); - void *fixed_addr_for_iter = Fixup::translated_code_block_map - ? fixed_addr - : addr; + void* fixed_addr_for_iter = + Fixup::translated_code_block_map ? fixed_addr : addr; - iterator(frame_top, frame_size, owner, fixed_addr_for_iter); - frame_top += frame_size; - } + iterator(frame_top, frame_size, owner, fixed_addr_for_iter); + frame_top += frame_size; + } } -template -inline void factor_vm::iterate_callstack(context *ctx, Iterator &iterator) -{ - no_fixup none; - iterate_callstack(ctx, iterator, none); +template +inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator) { + no_fixup none; + iterate_callstack(ctx, iterator, none); } - }