factor/vm/callstack.cpp

143 lines
4.5 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2013-05-11 21:47:34 -04:00
namespace factor {
/* Allocates memory (allot) */
2013-05-11 21:47:34 -04:00
callstack* factor_vm::allot_callstack(cell size) {
callstack* stack = allot<callstack>(callstack_object_size(size));
stack->length = tag_fixnum(size);
return stack;
2009-05-02 05:04:19 -04:00
}
/* We ignore the two topmost frames, the 'callstack' primitive
frame itself, and the frame calling the 'callstack' primitive,
2009-05-02 05:04:19 -04:00
so that set-callstack doesn't get stuck in an infinite loop.
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. */
cell factor_vm::second_from_top_stack_frame(context* ctx) {
cell frame_top = ctx->callstack_top;
2013-05-11 21:47:34 -04:00
for (cell i = 0; i < 2; ++i) {
cell pred = frame_predecessor(frame_top);
2013-05-11 21:47:34 -04:00
if (pred >= ctx->callstack_bottom)
return frame_top;
frame_top = pred;
}
return frame_top;
2009-05-02 05:04:19 -04:00
}
/* Allocates memory (allot_callstack) */
2013-05-11 21:47:34 -04:00
cell factor_vm::capture_callstack(context* ctx) {
cell top = second_from_top_stack_frame(ctx);
cell bottom = ctx->callstack_bottom;
2009-05-02 05:04:19 -04:00
fixnum size = std::max((cell)0, bottom - top);
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
callstack* stack = allot_callstack(size);
memcpy(stack->top(), (void *)top, size);
2013-05-11 21:47:34 -04:00
return tag<callstack>(stack);
}
/* Allocates memory (capture_callstack) */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_callstack() { ctx->push(capture_callstack(ctx)); }
/* Allocates memory (capture_callstack) */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_callstack_for() {
context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
ctx->replace(capture_callstack(other_ctx));
2009-05-02 05:04:19 -04:00
}
cell factor_vm::frame_predecessor(cell frame_top) {
cell addr = *(cell*)frame_top;
2013-05-11 21:47:34 -04:00
FACTOR_ASSERT(addr != 0);
code_block* owner = code->code_block_for_address(addr);
cell frame_size = owner->stack_frame_size_for_address(addr);
return frame_top + frame_size;
2009-05-02 05:04:19 -04:00
}
struct stack_frame_accumulator {
2013-05-11 21:47:34 -04:00
factor_vm* parent;
growable_array frames;
/* Allocates memory (frames is a growable_array, constructor allocates) */
explicit stack_frame_accumulator(factor_vm* parent)
: parent(parent), frames(parent) {}
2013-05-11 21:47:34 -04:00
/* Allocates memory (frames.add()) */
void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
2013-05-11 21:47:34 -04:00
data_root<object> executing_quot(owner->owner_quot(), parent);
data_root<object> executing(owner->owner, parent);
data_root<object> scan(owner->scan(parent, addr), parent);
frames.add(executing.value());
frames.add(executing_quot.value());
frames.add(scan.value());
}
};
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
struct stack_frame_in_array {
cell cells[3];
};
/* Allocates memory (frames.trim()), iterate_callstack_object() */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_callstack_to_array() {
data_root<callstack> callstack(ctx->peek(), this);
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
stack_frame_accumulator accum(this);
iterate_callstack_object(callstack.untagged(), accum);
2013-05-11 21:47:34 -04:00
/* 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));
2013-05-11 21:47:34 -04:00
accum.frames.trim();
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
ctx->replace(accum.frames.elements.value());
2009-05-02 05:04:19 -04:00
}
/* Some primitives implementing a limited form of callstack mutation.
Used by the single stepper. */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_innermost_stack_frame_executing() {
callstack* stack = untag_check<callstack>(ctx->peek());
void* frame = stack->top();
cell addr = *(cell*)frame;
ctx->replace(code->code_block_for_address(addr)->owner_quot());
2009-05-02 05:04:19 -04:00
}
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_innermost_stack_frame_scan() {
callstack* stack = untag_check<callstack>(ctx->peek());
void* frame = stack->top();
cell addr = *(cell*)frame;
ctx->replace(code->code_block_for_address(addr)->scan(this, addr));
2009-05-02 05:04:19 -04:00
}
/* Allocates memory (jit_compile_quot) */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_set_innermost_stack_frame_quot() {
data_root<callstack> stack(ctx->pop(), this);
data_root<quotation> quot(ctx->pop(), this);
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
stack.untag_check(this);
quot.untag_check(this);
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
jit_compile_quot(quot.value(), true);
2009-05-02 05:04:19 -04:00
2013-05-11 21:47:34 -04:00
void* inner = stack->top();
cell addr = *(cell*)inner;
code_block* block = code->code_block_for_address(addr);
2013-05-11 21:47:34 -04:00
cell offset = block->offset(addr);
*(cell*)inner = quot->entry_point + offset;
2009-05-02 05:04:19 -04:00
}
/* Allocates memory (allot_alien) */
2013-05-11 21:47:34 -04:00
void factor_vm::primitive_callstack_bounds() {
ctx->push(allot_alien((void*)ctx->callstack_seg->start));
ctx->push(allot_alien((void*)ctx->callstack_seg->end));
}
2009-05-04 02:46:13 -04:00
}