1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef K_COMPILER_H
6 #define K_COMPILER_H
7 
8 #if defined(__CC_ARM)
9 
10 #define RHINO_INLINE                static __inline
11 
12 /* get the return address of the current function
13    unsigned int __return_address(void) */
14 #define RHINO_GET_RA()              (void *)__return_address()
15 
16 /* get the  the value of the stack pointer
17    unsigned int __current_sp(void) */
18 #define RHINO_GET_SP()              (void *)__current_sp()
19 
20 /* Returns the number of leading 0-bits in x,
21    starting at the most signifi cant bit position. */
22 #define RHINO_BIT_CLZ(x)            __builtin_clz(x)
23 
24 /* Returns the number of trailing 0-bits in x,
25    starting at the least signifi cant bit position. */
26 #define RHINO_BIT_CTZ(x)            __builtin_ctz(x)
27 
28 #ifndef RHINO_WEAK
29 #define RHINO_WEAK                  __weak
30 #endif
31 
32 #ifndef RHINO_ASM
33 #define RHINO_ASM                   __asm
34 #endif
35 
36 /* Instruction Synchronization Barrier */
37 #define OS_ISB()                    __isb(15)   /* Full system  Any-Any */
38 
39 /* Data Memory Barrier */
40 #define OS_DMB()                    __dmb(15)   /* Full system  Any-Any */
41 
42 /* Data Synchronization Barrier */
43 #define OS_DSB()                    __dsb(15)   /* Full system  Any-Any */
44 
45 #elif defined(__ICCARM__)
46 #include "intrinsics.h"
47 
48 #define RHINO_INLINE                static inline
49 
50 /* get the return address of the current function
51    unsigned int __get_LR(void) */
52 #define RHINO_GET_RA()              (void *)__get_LR()
53 
54 /* get the  the value of the stack pointer
55    unsigned int __get_SP(void) */
56 #define RHINO_GET_SP()              (void *)__get_SP()
57 
58 /* Returns the number of leading 0-bits in x,
59    starting at the most signifi cant bit position. */
60 #define RHINO_BIT_CLZ(x)            __CLZ(x)
61 
62 //#define RHINO_BIT_CTZ(x)
63 
64 #ifndef RHINO_WEAK
65 #define RHINO_WEAK                  __weak
66 #endif
67 
68 #ifndef RHINO_ASM
69 #define RHINO_ASM                   asm
70 #endif
71 
72 /* Instruction Synchronization Barrier */
73 #define OS_ISB()                    __isb(15)   /* Full system  Any-Any */
74 
75 /* Data Memory Barrier */
76 #define OS_DMB()                    __dmb(15)   /* Full system  Any-Any */
77 
78 /* Data Synchronization Barrier */
79 #define OS_DSB()                    __dsb(15)   /* Full system  Any-Any */
80 
81 #elif defined(__GNUC__)
82 
83 #define RHINO_INLINE                static inline
84 
85 /* get the return address of the current function
86    void * __builtin_return_address (unsigned int level) */
87 #define RHINO_GET_RA()              __builtin_return_address(0)
88 
89 /* get the return address of the current function */
RHINO_GET_SP(void)90 __attribute__((always_inline)) RHINO_INLINE void *RHINO_GET_SP(void)
91 {
92     void *sp;
93     __asm__ volatile("mov %0, SP\n":"=r"(sp));
94     return sp;
95 }
96 
97 /* Returns the number of leading 0-bits in x,
98    starting at the most signifi cant bit position. */
99 #define RHINO_BIT_CLZ(x)            __builtin_clz(x)
100 
101 /* Returns the number of trailing 0-bits in x,
102    starting at the least signifi cant bit position. */
103 #define RHINO_BIT_CTZ(x)            __builtin_ctz(x)
104 
105 #ifndef RHINO_WEAK
106 #define RHINO_WEAK                  __attribute__((weak))
107 #endif
108 
109 #ifndef RHINO_ASM
110 #define RHINO_ASM                   __asm__
111 #endif
112 
113 /* Instruction Synchronization Barrier */
114 #define OS_ISB()                    __asm volatile ("isb sy":::"memory")
115 
116 /* Data Memory Barrier */
117 #define OS_DMB()                    __asm volatile ("dmb sy":::"memory")
118 
119 /* Data Synchronization Barrier */
120 #define OS_DSB()                    __asm volatile ("dsb sy":::"memory")
121 
122 #else
123 #error "Unsupported compiler"
124 #endif
125 
126 #endif /* K_COMPILER_H */
127 
128