]> gitweb.factorcode.org Git - factor.git/blob - vm/cpu-x86.cpp
vm: replace remaining stack_frame-based logic
[factor.git] / vm / cpu-x86.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void factor_vm::dispatch_signal_handler(cell *sp, cell *pc, cell handler)
7 {
8         if (!code->seg->in_segment_p(*pc) || *sp < ctx->callstack_seg->start + stack_reserved)
9         {
10                 /* Fault came from foreign code or a callstack overflow, or we don't
11                 have enough callstack room to try the resumable handler. Cut the
12                 callstack down to the shallowest Factor stack frame that leaves room for
13                 the signal handler to do its thing, and launch the handler without going
14                 through the resumable subprimitive. */
15                 signal_resumable = false;
16                 void *frame_top = (void*)ctx->callstack_top;
17
18                 while(frame_top < ctx->callstack_bottom
19                         && (cell)frame_top < ctx->callstack_seg->start + stack_reserved)
20                 {
21                         frame_top = frame_predecessor(frame_top);
22                 }
23
24                 *sp = (cell)frame_top;
25                 ctx->callstack_top = frame_top;
26                 *pc = handler;
27         } else {
28                 signal_resumable = true;
29                 // Fault came from Factor, and we've got a good callstack. Route the signal
30                 // handler through the resumable signal handler subprimitive.
31                 cell offset = *sp % 16;
32
33                 signal_handler_addr = handler;
34                 tagged<word> handler_word = tagged<word>(special_objects[SIGNAL_HANDLER_WORD]);
35
36                 /* True stack frames are always 16-byte aligned. Leaf procedures
37                 that don't create a stack frame will be out of alignment by sizeof(cell)
38                 bytes. */
39                 /* On architectures with a link register we would have to check for leafness
40                 by matching the PC to a word. We should also use FRAME_RETURN_ADDRESS instead
41                 of assuming the stack pointer is the right place to put the resume address. */
42                 if (offset == 0)
43                 {
44                         cell newsp = *sp - sizeof(cell);
45                         *sp = newsp;
46                         *(cell*)newsp = *pc;
47                 }
48                 else if (offset == 16 - sizeof(cell))
49                 {
50                         // Make a fake frame for the leaf procedure
51                         code_block *leaf_block = code->code_block_for_address(*pc);
52                         FACTOR_ASSERT(leaf_block != NULL);
53
54                         cell newsp = *sp - LEAF_FRAME_SIZE;
55                         *(cell*)(newsp + 3*sizeof(cell)) = 4*sizeof(cell);
56                         *(cell*)(newsp + 2*sizeof(cell)) = (cell)leaf_block->entry_point();
57                         *(cell*) newsp                   = *pc;
58                         *sp = newsp;
59                         handler_word = tagged<word>(special_objects[LEAF_SIGNAL_HANDLER_WORD]);
60                 }
61                 else
62                         FACTOR_ASSERT(false);
63
64                 *pc = (cell)handler_word->entry_point;
65         }
66 }
67
68 }