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