1 /** @file
2  * @brief Common includes, types, defines and platform specific stuff.
3  *
4  * This header should be included before other headers.
5  * This header should not contain code.
6  */
7 
8 /*
9  * Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr)
10  * Copyright (c) 2014 Flávio J. Saraiva <flaviojs2005@gmail.com>
11  */
12 
13 #pragma once
14 #ifndef __DYNAMIPS_COMMON_H__
15 #define __DYNAMIPS_COMMON_H__
16 
17 /* Config file - not used at the moment */
18 #if HAVE_DYNAMIPS_CONFIG_H
19 #include "dynamips_config.h"
20 #endif
21 
22 /* By default, Cygwin supports only 64 FDs with select()! */
23 #if defined(__CYGWIN__) && !defined(FD_SETSIZE)
24 #define FD_SETSIZE 1024
25 #endif
26 
27 #define _GNU_SOURCE
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <errno.h>
37 
38 #include <pthread.h>
39 
40 /* True/False definitions */
41 #ifndef FALSE
42 #define FALSE 0
43 #endif
44 
45 #ifndef TRUE
46 #define TRUE  1
47 #endif
48 
49 /* Endianness */
50 #define ARCH_BIG_ENDIAN     0x4321
51 #define ARCH_LITTLE_ENDIAN  0x1234
52 
53 #if defined(PPC) || defined(__powerpc__) || defined(__ppc__)
54 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
55 #elif defined(__sparc) || defined(__sparc__)
56 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
57 #elif defined(__alpha) || defined(__alpha__)
58 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
59 #elif defined(__i386) || defined(__i386__) || defined(i386)
60 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
61 #define ARCH_REGPARM_SUPPORTED  1
62 #elif defined(__x86_64__)
63 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
64 #elif defined(__ia64__)
65 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
66 #elif defined(__arm__) || defined (__aarch64__)
67 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
68 #endif
69 
70 #ifndef ARCH_BYTE_ORDER
71 #error Please define your architecture!
72 #endif
73 
74 /* Useful attributes for functions */
75 #ifdef ARCH_REGPARM_SUPPORTED
76 #define asmlinkage __attribute__((regparm(0)))
77 #define fastcall   __attribute__((regparm(3)))
78 #else
79 #define asmlinkage
80 #define fastcall
81 #endif
82 
83 #ifndef _Unused
84 /* Function that is never used */
85 #define _Unused  __attribute__((unused))
86 #endif
87 
88 #ifndef _maybe_used
89 /* Function that is referenced from excluded code (commented out or depends on preprocessor) */
90 #define _maybe_used  __attribute__((unused))
91 #endif
92 
93 #ifndef UNUSED
94 /* Variable that is never used (name is changed to get an error on use) */
95 #define UNUSED(x)  UNUSED_ ## x __attribute__((unused))
96 #endif
97 
98 #if __GNUC__ > 2
99 #define forced_inline inline __attribute__((always_inline))
100 #define no_inline __attribute__ ((noinline))
101 #else
102 #define forced_inline inline
103 #define no_inline
104 #endif
105 
106 #if __GNUC__ > 2
107 /* http://kerneltrap.org/node/4705 */
108 #define likely(x)    __builtin_expect(!!(x),1)
109 #define unlikely(x)  __builtin_expect((x),0)
110 #else
111 #define likely(x)    (x)
112 #define unlikely(x)  (x)
113 #endif
114 
115 #ifndef _not_aligned
116 #define _not_aligned __attribute__ ((aligned (1)))
117 #endif
118 
119 /* Common types */
120 typedef unsigned char m_uint8_t;
121 typedef signed char m_int8_t;
122 
123 typedef unsigned short m_uint16_t;
124 typedef signed short m_int16_t;
125 
126 typedef unsigned int m_uint32_t;
127 typedef signed int m_int32_t;
128 
129 typedef unsigned long long m_uint64_t;
130 typedef signed long long m_int64_t;
131 
132 typedef unsigned long m_iptr_t;
133 typedef m_uint64_t m_tmcnt_t;
134 
135 /* Max and min macro */
136 #define m_max(a,b) (((a) > (b)) ? (a) : (b))
137 #define m_min(a,b) (((a) < (b)) ? (a) : (b))
138 
139 /* A simple macro for adjusting pointers */
140 #define PTR_ADJUST(type,ptr,size) (type)((char *)(ptr) + (size))
141 
142 /* Size of a field in a structure */
143 #define SIZEOF(st,field) (sizeof(((st *)NULL)->field))
144 
145 /* Compute offset of a field in a structure */
146 #define OFFSET(st,f)     ((long)&((st *)(NULL))->f)
147 
148 /* Stringify a constant */
149 #define XSTRINGIFY(val)  #val
150 #define STRINGIFY(val)   XSTRINGIFY(val)
151 
152 #endif
153