8cbba3d862d41828d56bcdcb6f8138634b83b20d
[bertos.git] / bertos / cpu / types.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific type definitions.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_TYPES_H
42 #define CPU_TYPES_H
43
44 #include "detect.h"
45 #include "attr.h"
46 #include <cfg/compiler.h> /* for uintXX_t */
47
48 #if CPU_I196
49
50         typedef uint16_t cpuflags_t; // FIXME
51         typedef unsigned int cpustack_t;
52         #warning Verify following constant
53         #define SIZEOF_CPUSTACK_T 2
54
55 #elif CPU_X86
56
57         /* Get cpuflags_t definition from the hosting environment. */
58         #include <cfg/os.h>
59         #if OS_EMBEDDED
60                 typedef uint32_t cpuflags_t; // FIXME
61         #endif /* OS_EMBEDDED */
62
63         #if CPU_X86_64
64                 typedef uint64_t cpustack_t;
65                 #define SIZEOF_CPUSTACK_T 8
66         #else
67                 typedef uint32_t cpustack_t;
68                 #define SIZEOF_CPUSTACK_T 4
69         #endif
70
71 #elif CPU_ARM
72
73         typedef uint32_t cpuflags_t;
74         typedef uint32_t cpustack_t;
75         #define SIZEOF_CPUSTACK_T 4
76
77 #elif CPU_PPC
78
79         /* Get cpuflags_t definition from the hosting environment. */
80         #include <cfg/os.h>
81         #if OS_EMBEDDED
82                 typedef uint32_t cpuflags_t; // FIXME
83         #endif
84
85         typedef uint32_t cpustack_t; // FIXME
86         #define SIZEOF_CPUSTACK_T 4
87
88 #elif CPU_DSP56K
89
90         typedef uint16_t cpuflags_t;
91         typedef unsigned int cpustack_t;
92         #warning Verify following costant
93         #define SIZEOF_CPUSTACK_T 2
94
95 #elif CPU_AVR
96
97         typedef uint8_t cpuflags_t;
98         typedef uint8_t cpustack_t;
99         #define SIZEOF_CPUSTACK_T 1
100
101 #else
102         #error No CPU_... defined.
103 #endif
104
105 /**
106  * \name Default type sizes.
107  *
108  * These defaults are reasonable for most 16/32bit machines.
109  * Some of these macros may be overridden by CPU-specific code above.
110  *
111  * ANSI C requires that the following equations be true:
112  * \code
113  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
114  *   sizeof(float) <= sizeof(double)
115  *   CPU_BITS_PER_CHAR  >= 8
116  *   CPU_BITS_PER_SHORT >= 8
117  *   CPU_BITS_PER_INT   >= 16
118  *   CPU_BITS_PER_LONG  >= 32
119  * \endcode
120  * \{
121  */
122 #ifndef SIZEOF_CHAR
123 #define SIZEOF_CHAR  1
124 #endif
125
126 #ifndef SIZEOF_SHORT
127 #define SIZEOF_SHORT  2
128 #endif
129
130 #ifndef SIZEOF_INT
131 #if CPU_REG_BITS < 32
132         #define SIZEOF_INT  2
133 #else
134         #define SIZEOF_INT  4
135 #endif
136 #endif /* !SIZEOF_INT */
137
138 #ifndef SIZEOF_LONG
139 #if CPU_REG_BITS > 32
140         #define SIZEOF_LONG  8
141 #else
142         #define SIZEOF_LONG  4
143 #endif
144 #endif
145
146 #ifndef SIZEOF_PTR
147 #if CPU_REG_BITS < 32
148         #define SIZEOF_PTR   2
149 #elif CPU_REG_BITS == 32
150         #define SIZEOF_PTR   4
151 #else /* CPU_REG_BITS > 32 */
152         #define SIZEOF_PTR   8
153 #endif
154 #endif
155
156 #ifndef CPU_BITS_PER_CHAR
157 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
158 #endif
159
160 #ifndef CPU_BITS_PER_SHORT
161 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
162 #endif
163
164 #ifndef CPU_BITS_PER_INT
165 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
166 #endif
167
168 #ifndef CPU_BITS_PER_LONG
169 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
170 #endif
171
172 #ifndef CPU_BITS_PER_PTR
173 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
174 #endif
175
176
177 /*\}*/
178
179 /* Sanity checks for the above definitions */
180 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
181 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
182 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
183 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
184 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
185 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
186 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
187 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
188 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
189 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
190 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
191 #ifdef __HAS_INT64_T__
192 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
193 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
194 #endif
195 STATIC_ASSERT(sizeof(cpustack_t) == SIZEOF_CPUSTACK_T);
196
197
198 #endif /* CPU_TYPES_H */