]> gitweb.factorcode.org Git - factor.git/blob - vm/cpu-x86.hpp
bfdcd8afb2ce779bf5e75316087b158eb79597ef
[factor.git] / vm / cpu-x86.hpp
1 #include <assert.h>
2
3 namespace factor
4 {
5
6 #define FRAME_RETURN_ADDRESS(frame,vm) *(void **)(vm->frame_successor(frame) + 1)
7
8 #define CALLSTACK_BOTTOM(ctx) (stack_frame *)(ctx->callstack_seg->end - sizeof(cell))
9
10 inline static void flush_icache(cell start, cell len) {}
11
12 /* In the instruction sequence:
13
14    MOV EBX,...
15    JMP blah
16
17    the offset from the immediate operand to MOV to the instruction after
18    the jump is a cell for the immediate operand, 4 bytes for the JMP
19    destination, and one byte for the JMP opcode. */
20 static const fixnum xt_tail_pic_offset = 4 + 1;
21
22 static const unsigned char call_opcode = 0xe8;
23 static const unsigned char jmp_opcode = 0xe9;
24
25 inline static unsigned char call_site_opcode(cell return_address)
26 {
27         return *(unsigned char *)(return_address - 5);
28 }
29
30 inline static void check_call_site(cell return_address)
31 {
32         unsigned char opcode = call_site_opcode(return_address);
33         assert(opcode == call_opcode || opcode == jmp_opcode);
34 }
35
36 inline static void *get_call_target(cell return_address)
37 {
38         check_call_site(return_address);
39         return (void *)(*(int *)(return_address - 4) + return_address);
40 }
41
42 inline static void set_call_target(cell return_address, void *target)
43 {
44         check_call_site(return_address);
45         *(int *)(return_address - 4) = (u32)((cell)target - return_address);
46 }
47
48 inline static bool tail_call_site_p(cell return_address)
49 {
50         switch(call_site_opcode(return_address))
51         {
52         case jmp_opcode: return true;
53         case call_opcode: return false;
54         default: abort(); return false;
55         }
56 }
57
58 inline static unsigned int fpu_status(unsigned int status)
59 {
60         unsigned int r = 0;
61         
62         if (status & 0x01)
63                 r |= FP_TRAP_INVALID_OPERATION;
64         if (status & 0x04)
65                 r |= FP_TRAP_ZERO_DIVIDE;
66         if (status & 0x08)
67                 r |= FP_TRAP_OVERFLOW;
68         if (status & 0x10)
69                 r |= FP_TRAP_UNDERFLOW;
70         if (status & 0x20)
71                 r |= FP_TRAP_INEXACT;
72
73         return r;
74 }
75
76 }