]> gitweb.factorcode.org Git - factor.git/blob - vm/cpu-ppc.hpp
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / vm / cpu-ppc.hpp
1 namespace factor
2 {
3
4 #define FACTOR_CPU_STRING "ppc"
5
6 /* In the instruction sequence:
7
8    LOAD32 r3,...
9    B blah
10
11    the offset from the immediate operand to LOAD32 to the instruction after
12    the branch is one instruction. */
13 static const fixnum xt_tail_pic_offset = 4;
14
15 inline static void check_call_site(cell return_address)
16 {
17         cell insn = *(cell *)return_address;
18         /* Check that absolute bit is 0 */
19         assert((insn & 0x2) == 0x0);
20         /* Check that instruction is branch */
21         assert((insn >> 26) == 0x12);
22 }
23
24 static const cell b_mask = 0x3fffffc;
25
26 inline static void *get_call_target(cell return_address)
27 {
28         return_address -= sizeof(cell);
29         check_call_site(return_address);
30
31         cell insn = *(cell *)return_address;
32         cell unsigned_addr = (insn & b_mask);
33         fixnum signed_addr = (fixnum)(unsigned_addr << 6) >> 6;
34         return (void *)(signed_addr + return_address);
35 }
36
37 inline static void set_call_target(cell return_address, void *target)
38 {
39         return_address -= sizeof(cell);
40         check_call_site(return_address);
41
42         cell insn = *(cell *)return_address;
43
44         fixnum relative_address = ((cell)target - return_address);
45         insn = ((insn & ~b_mask) | (relative_address & b_mask));
46         *(cell *)return_address = insn;
47
48         /* Flush the cache line containing the call we just patched */
49         __asm__ __volatile__ ("icbi 0, %0\n" "sync\n"::"r" (return_address):);
50 }
51
52 inline static bool tail_call_site_p(cell return_address)
53 {
54         return_address -= sizeof(cell);
55         cell insn = *(cell *)return_address;
56         return (insn & 0x1) == 0;
57 }
58
59 inline static unsigned int fpu_status(unsigned int status)
60 {
61         unsigned int r = 0;
62
63         if (status & 0x20000000)
64                 r |= FP_TRAP_INVALID_OPERATION;
65         if (status & 0x10000000)
66                 r |= FP_TRAP_OVERFLOW;
67         if (status & 0x08000000)
68                 r |= FP_TRAP_UNDERFLOW;
69         if (status & 0x04000000)
70                 r |= FP_TRAP_ZERO_DIVIDE;
71         if (status & 0x02000000)
72                 r |= FP_TRAP_INEXACT;
73
74         return r;
75 }
76
77 /* Defined in assembly */
78 VM_C_API void flush_icache(cell start, cell len);
79
80 }