1 /*
2  * ORC - Library of Optimized Inner Loops
3  * Copyright (c) 2007 David A. Schleef <ds@schleef.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _ORC_UTILS_H_
29 #define _ORC_UTILS_H_
30 
31 /* Orc objects */
32 /* typedef struct _OrcVariable OrcVariable; */
33 /* typedef struct _OrcOpcodeSet OrcOpcodeSet; */
34 /* typedef struct _OrcStaticOpcode OrcStaticOpcode; */
35 typedef struct _OrcInstruction OrcInstruction;
36 typedef struct _OrcProgram OrcProgram;
37 typedef struct _OrcCompiler OrcCompiler;
38 typedef struct _OrcConstant OrcConstant;
39 /* typedef struct _OrcFixup OrcFixup; */
40 typedef struct _OrcTarget OrcTarget;
41 typedef struct _OrcCode OrcCode;
42 
43 typedef enum {
44   ORC_COMPILE_RESULT_OK = 0,
45 
46   ORC_COMPILE_RESULT_UNKNOWN_COMPILE = 0x100,
47   ORC_COMPILE_RESULT_MISSING_RULE = 0x101,
48 
49   ORC_COMPILE_RESULT_UNKNOWN_PARSE = 0x200,
50   ORC_COMPILE_RESULT_PARSE = 0x201,
51   ORC_COMPILE_RESULT_VARIABLE = 0x202
52 
53 } OrcCompileResult;
54 
55 #include <stddef.h>
56 
57 #ifndef _ORC_INTEGER_TYPEDEFS_
58 #define _ORC_INTEGER_TYPEDEFS_
59 #if defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
60 #include <stdint.h>
61 typedef int8_t orc_int8;
62 typedef int16_t orc_int16;
63 typedef int32_t orc_int32;
64 typedef int64_t orc_int64;
65 typedef uint8_t orc_uint8;
66 typedef uint16_t orc_uint16;
67 typedef uint32_t orc_uint32;
68 typedef uint64_t orc_uint64;
69 typedef intptr_t orc_intptr;
70 #define ORC_UINT64_C(x) UINT64_C(x)
71 #elif defined(_MSC_VER)
72 typedef signed __int8 orc_int8;
73 typedef signed __int16 orc_int16;
74 typedef signed __int32 orc_int32;
75 typedef signed __int64 orc_int64;
76 typedef unsigned __int8 orc_uint8;
77 typedef unsigned __int16 orc_uint16;
78 typedef unsigned __int32 orc_uint32;
79 typedef unsigned __int64 orc_uint64;
80 #ifdef _WIN64
81 typedef unsigned __int64 orc_intptr;
82 #else
83 typedef unsigned long orc_intptr;
84 #endif
85 #define ORC_UINT64_C(x) (x##Ui64)
86 #else
87 #include <limits.h>
88 typedef signed char orc_int8;
89 typedef short orc_int16;
90 typedef int orc_int32;
91 typedef unsigned char orc_uint8;
92 typedef unsigned short orc_uint16;
93 typedef unsigned int orc_uint32;
94 #if INT_MAX == LONG_MAX
95 typedef long long orc_int64;
96 typedef unsigned long long orc_uint64;
97 #define ORC_UINT64_C(x) (x##ULL)
98 #else
99 typedef long orc_int64;
100 typedef unsigned long orc_uint64;
101 #define ORC_UINT64_C(x) (x##UL)
102 #endif
103 #ifdef _WIN64
104 typedef unsigned __int64 orc_intptr;
105 #else
106 typedef unsigned long orc_intptr;
107 #endif
108 #endif
109 typedef union { orc_int16 i; orc_int8 x2[2]; } orc_union16;
110 typedef union { orc_int32 i; float f; orc_int16 x2[2]; orc_int8 x4[4]; } orc_union32;
111 typedef union { orc_int64 i; double f; orc_int32 x2[2]; float x2f[2]; orc_int16 x4[4]; } orc_union64;
112 #endif
113 
114 #ifndef TRUE
115 #define TRUE 1
116 #endif
117 #ifndef FALSE
118 #define FALSE 0
119 #endif
120 
121 typedef unsigned int orc_bool;
122 
123 #define ORC_PTR_TO_INT(x) ((int)(orc_intptr)(x))
124 #define ORC_PTR_OFFSET(ptr,offset) ((void *)(((unsigned char *)(ptr)) + (offset)))
125 
126 #if (defined(__GNUC__)  && __GNUC__ >= 4) || defined (_MSC_VER)
127 #define ORC_STRUCT_OFFSET(struct_type, member) \
128       ((int) offsetof (struct_type, member))
129 #else
130 #define ORC_STRUCT_OFFSET(struct_type, member)	\
131       ((int) ((unsigned char **) &((struct_type*) 0)->member))
132 #endif
133 
134 #ifdef ORC_ENABLE_UNSTABLE_API
135 
136 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
137 #ifndef MIN
138 #define MIN(a,b) ((a)<(b) ? (a) : (b))
139 #endif
140 #ifndef MAX
141 #define MAX(a,b) ((a)>(b) ? (a) : (b))
142 #endif
143 #ifndef ORC_CLAMP
144 #define ORC_CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))
145 #endif
146 
147 #define ORC_READ_UINT32_LE(ptr) \
148   (((orc_uint32)((orc_uint8 *)(ptr))[0]) | \
149    ((orc_uint32)(((orc_uint8 *)(ptr))[1])<<8) | \
150    ((orc_uint32)(((orc_uint8 *)(ptr))[2])<<16) | \
151    ((orc_uint32)(((orc_uint8 *)(ptr))[3])<<24))
152 
153 #define ORC_WRITE_UINT32_LE(ptr,val) \
154   do { \
155     ((orc_uint8 *)ptr)[0] = ((val)>>0)&0xff; \
156     ((orc_uint8 *)ptr)[1] = ((val)>>8)&0xff; \
157     ((orc_uint8 *)ptr)[2] = ((val)>>16)&0xff; \
158     ((orc_uint8 *)ptr)[3] = ((val)>>24)&0xff; \
159   } while(0)
160 
161 #endif
162 
163 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
164 #define ORC_GNUC_PREREQ(maj, min) \
165   ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
166 #else
167 #define ORC_GNUC_PREREQ(maj, min) 0
168 #endif
169 
170 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
171 #define ORC_LIKELY(expr) (__builtin_expect ((expr), 1))
172 #define ORC_UNLIKELY(expr) (__builtin_expect ((expr), 0))
173 #else
174 #define ORC_LIKELY(expr) (expr)
175 #define ORC_UNLIKELY(expr) (expr)
176 #endif
177 
178 #ifndef ORC_INTERNAL
179 #if defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
180 #define ORC_INTERNAL __attribute__((visibility("hidden")))
181 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
182 #define ORC_INTERNAL __hidden
183 #elif defined (__GNUC__) && ORC_GNUC_PREREQ(3,3) && defined(__ELF__)
184 #define ORC_INTERNAL __attribute__((visibility("hidden")))
185 #else
186 #define ORC_INTERNAL
187 #endif
188 #endif
189 
190 #if ORC_GNUC_PREREQ(3,3) /* guess */
191 #define ORC_GNU_PRINTF(a,b) __attribute__((__format__ (__printf__, a, b)))
192 #else
193 #define ORC_GNU_PRINTF(a,b)
194 #endif
195 
196 #if ORC_GNUC_PREREQ(2,4)
197 #define ORC_GNUC_UNUSED __attribute__((__unused__))
198 #else
199 #define ORC_GNUC_UNUSED
200 #endif
201 
202 #ifdef __cplusplus
203 #define ORC_BEGIN_DECLS extern "C" {
204 #define ORC_END_DECLS }
205 #else
206 #define ORC_BEGIN_DECLS
207 #define ORC_END_DECLS
208 #endif
209 
210 /* FIXME: unused, remove */
211 #define ORC_EXPORT
212 
213 #if (defined(_MSC_VER) || defined(_WIN32)) && !defined(ORC_STATIC_COMPILATION)
214 #define ORC_API_IMPORT __declspec(dllimport) extern
215 #else
216 #define ORC_API_IMPORT extern
217 #endif
218 
219 #ifdef BUILDING_ORC
220 #define ORC_API ORC_API_EXPORT /* defined in config.h */
221 #else
222 #define ORC_API ORC_API_IMPORT
223 #endif
224 
225 ORC_BEGIN_DECLS
226 
227 #ifdef ORC_ENABLE_UNSTABLE_API
228 
229 /* FIXME: remove, these are internal functions that were never exported */
230 #if defined(__arm__) || defined(__mips__)
231 char * get_proc_cpuinfo (void);
232 #endif
233 
234 char * _strndup (const char *s, int n);
235 char ** strsplit (const char *s, char delimiter);
236 char * get_tag_value (char *s, const char *tag);
237 
238 orc_int64 _strtoll (const char *nptr, char **endptr, int base);
239 
240 /* FIXME: why are these exported ? */
241 ORC_API void orc_global_mutex_lock (void);
242 ORC_API void orc_global_mutex_unlock (void);
243 
244 #endif
245 
246 ORC_END_DECLS
247 
248 #endif
249 
250