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