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