1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
41 /*
42 * Generate CPU-specific bit-size and similar #defines.
43 */
44 #include <stdio.h>
45 #include <stdlib.h>
47 #ifdef CROSS_COMPILE
48 #include <prtypes.h>
49 #define INT64 PRInt64
50 #else
52 /************************************************************************/
54 /* Generate cpucfg.h */
56 #if defined(XP_WIN) || defined(XP_OS2)
57 #ifdef WIN32
58 #if defined(__GNUC__)
59 #define INT64 long long
60 #else
61 #define INT64 _int64
62 #endif /* __GNUC__ */
63 #else
64 #define INT64 long
65 #endif
66 #else
67 #if defined(HPUX) || defined(__QNX__) || defined(_SCO_DS) || defined(UNIXWARE)
68 #define INT64 long
69 #else
70 #define INT64 long long
71 #endif
72 #endif
74 #endif /* CROSS_COMPILE */
76 #ifdef __GNUC__
77 #define NS_NEVER_INLINE __attribute__((noinline))
78 #else
79 #define NS_NEVER_INLINE
80 #endif
82 typedef void *prword;
84 struct align_short {
85 char c;
86 short a;
87 };
88 struct align_int {
89 char c;
90 int a;
91 };
92 struct align_long {
93 char c;
94 long a;
95 };
96 struct align_int64 {
97 char c;
98 INT64 a;
99 };
100 struct align_fakelonglong {
101 char c;
102 struct {
103 long hi, lo;
104 } a;
105 };
106 struct align_float {
107 char c;
108 float a;
109 };
110 struct align_double {
111 char c;
112 double a;
113 };
114 struct align_pointer {
115 char c;
116 void *a;
117 };
118 struct align_prword {
119 char c;
120 prword a;
121 };
123 #define ALIGN_OF(type) \
124 (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
126 unsigned int bpb;
128 static int Log2(unsigned int n)
129 {
130 int log2 = 0;
132 if (n & (n-1))
133 log2++;
134 if (n >> 16)
135 log2 += 16, n >>= 16;
136 if (n >> 8)
137 log2 += 8, n >>= 8;
138 if (n >> 4)
139 log2 += 4, n >>= 4;
140 if (n >> 2)
141 log2 += 2, n >>= 2;
142 if (n >> 1)
143 log2++;
144 return log2;
145 }
147 /*
148 * Conceivably this could actually be used, but there is lots of code out
149 * there with ands and shifts in it that assumes a byte is exactly 8 bits,
150 * so forget about porting THIS code to all those non 8 bit byte machines.
151 */
152 static void BitsPerByte(void)
153 {
154 bpb = 8;
155 }
157 static int NS_NEVER_INLINE StackGrowthDirection(int *dummy1addr)
158 {
159 int dummy2;
161 return (&dummy2 < dummy1addr) ? -1 : 1;
162 }
164 int main(int argc, char **argv)
165 {
166 int sizeof_char, sizeof_short, sizeof_int, sizeof_int64, sizeof_long,
167 sizeof_float, sizeof_double, sizeof_word, sizeof_dword;
168 int bits_per_int64_log2, align_of_short, align_of_int, align_of_long,
169 align_of_int64, align_of_float, align_of_double, align_of_pointer,
170 align_of_word;
171 int dummy1;
173 BitsPerByte();
175 printf("#ifndef js_cpucfg___\n");
176 printf("#define js_cpucfg___\n\n");
178 printf("/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
180 #ifdef CROSS_COMPILE
181 #if defined(IS_LITTLE_ENDIAN)
182 printf("#define IS_LITTLE_ENDIAN 1\n");
183 printf("#undef IS_BIG_ENDIAN\n\n");
184 #elif defined(IS_BIG_ENDIAN)
185 printf("#undef IS_LITTLE_ENDIAN\n");
186 printf("#define IS_BIG_ENDIAN 1\n\n");
187 #else
188 #error "Endianess not defined."
189 #endif
191 sizeof_char = PR_BYTES_PER_BYTE;
192 sizeof_short = PR_BYTES_PER_SHORT;
193 sizeof_int = PR_BYTES_PER_INT;
194 sizeof_int64 = PR_BYTES_PER_INT64;
195 sizeof_long = PR_BYTES_PER_LONG;
196 sizeof_float = PR_BYTES_PER_FLOAT;
197 sizeof_double = PR_BYTES_PER_DOUBLE;
198 sizeof_word = PR_BYTES_PER_WORD;
199 sizeof_dword = PR_BYTES_PER_DWORD;
201 bits_per_int64_log2 = PR_BITS_PER_INT64_LOG2;
203 align_of_short = PR_ALIGN_OF_SHORT;
204 align_of_int = PR_ALIGN_OF_INT;
205 align_of_long = PR_ALIGN_OF_LONG;
206 align_of_int64 = PR_ALIGN_OF_INT64;
207 align_of_float = PR_ALIGN_OF_FLOAT;
208 align_of_double = PR_ALIGN_OF_DOUBLE;
209 align_of_pointer = PR_ALIGN_OF_POINTER;
210 align_of_word = PR_ALIGN_OF_WORD;
212 #else /* !CROSS_COMPILE */
214 /*
215 * We don't handle PDP-endian or similar orders: if a short is big-endian,
216 * so must int and long be big-endian for us to generate the IS_BIG_ENDIAN
217 * #define and the IS_LITTLE_ENDIAN #undef.
218 */
219 {
220 int big_endian = 0, little_endian = 0, ntests = 0;
222 if (sizeof(short) == 2) {
223 /* force |volatile| here to get rid of any compiler optimisations
224 * (var in register etc.) which may be appiled to |auto| vars -
225 * even those in |union|s...
226 * (|static| is used to get the same functionality for compilers
227 * which do not honor |volatile|...).
228 */
229 volatile static union {
230 short i;
231 char c[2];
232 } u;
234 u.i = 0x0102;
235 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02);
236 little_endian += (u.c[0] == 0x02 && u.c[1] == 0x01);
237 ntests++;
238 }
240 if (sizeof(int) == 4) {
241 /* force |volatile| here ... */
242 volatile static union {
243 int i;
244 char c[4];
245 } u;
247 u.i = 0x01020304;
248 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
249 u.c[2] == 0x03 && u.c[3] == 0x04);
250 little_endian += (u.c[0] == 0x04 && u.c[1] == 0x03 &&
251 u.c[2] == 0x02 && u.c[3] == 0x01);
252 ntests++;
253 }
255 if (sizeof(long) == 8) {
256 /* force |volatile| here ... */
257 volatile static union {
258 long i;
259 char c[8];
260 } u;
262 /*
263 * Write this as portably as possible: avoid 0x0102030405060708L
264 * and <<= 32.
265 */
266 u.i = 0x01020304;
267 u.i <<= 16, u.i <<= 16;
268 u.i |= 0x05060708;
269 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
270 u.c[2] == 0x03 && u.c[3] == 0x04 &&
271 u.c[4] == 0x05 && u.c[5] == 0x06 &&
272 u.c[6] == 0x07 && u.c[7] == 0x08);
273 little_endian += (u.c[0] == 0x08 && u.c[1] == 0x07 &&
274 u.c[2] == 0x06 && u.c[3] == 0x05 &&
275 u.c[4] == 0x04 && u.c[5] == 0x03 &&
276 u.c[6] == 0x02 && u.c[7] == 0x01);
277 ntests++;
278 }
280 if (big_endian && big_endian == ntests) {
281 printf("#undef IS_LITTLE_ENDIAN\n");
282 printf("#define IS_BIG_ENDIAN 1\n\n");
283 } else if (little_endian && little_endian == ntests) {
284 printf("#define IS_LITTLE_ENDIAN 1\n");
285 printf("#undef IS_BIG_ENDIAN\n\n");
286 } else {
287 fprintf(stderr, "%s: unknown byte order"
288 "(big_endian=%d, little_endian=%d, ntests=%d)!\n",
289 argv[0], big_endian, little_endian, ntests);
290 return EXIT_FAILURE;
291 }
292 }
294 sizeof_char = sizeof(char);
295 sizeof_short = sizeof(short);
296 sizeof_int = sizeof(int);
297 sizeof_int64 = 8;
298 sizeof_long = sizeof(long);
299 sizeof_float = sizeof(float);
300 sizeof_double = sizeof(double);
301 sizeof_word = sizeof(prword);
302 sizeof_dword = 8;
304 bits_per_int64_log2 = 6;
306 align_of_short = ALIGN_OF(short);
307 align_of_int = ALIGN_OF(int);
308 align_of_long = ALIGN_OF(long);
309 if (sizeof(INT64) < 8) {
310 /* this machine doesn't actually support int64's */
311 align_of_int64 = ALIGN_OF(fakelonglong);
312 } else {
313 align_of_int64 = ALIGN_OF(int64);
314 }
315 align_of_float = ALIGN_OF(float);
316 align_of_double = ALIGN_OF(double);
317 align_of_pointer = ALIGN_OF(pointer);
318 align_of_word = ALIGN_OF(prword);
320 #endif /* CROSS_COMPILE */
322 printf("#define JS_BYTES_PER_BYTE %dL\n", sizeof_char);
323 printf("#define JS_BYTES_PER_SHORT %dL\n", sizeof_short);
324 printf("#define JS_BYTES_PER_INT %dL\n", sizeof_int);
325 printf("#define JS_BYTES_PER_INT64 %dL\n", sizeof_int64);
326 printf("#define JS_BYTES_PER_LONG %dL\n", sizeof_long);
327 printf("#define JS_BYTES_PER_FLOAT %dL\n", sizeof_float);
328 printf("#define JS_BYTES_PER_DOUBLE %dL\n", sizeof_double);
329 printf("#define JS_BYTES_PER_WORD %dL\n", sizeof_word);
330 printf("#define JS_BYTES_PER_DWORD %dL\n", sizeof_dword);
331 printf("\n");
333 printf("#define JS_BITS_PER_BYTE %dL\n", bpb);
334 printf("#define JS_BITS_PER_SHORT %dL\n", bpb * sizeof_short);
335 printf("#define JS_BITS_PER_INT %dL\n", bpb * sizeof_int);
336 printf("#define JS_BITS_PER_INT64 %dL\n", bpb * sizeof_int64);
337 printf("#define JS_BITS_PER_LONG %dL\n", bpb * sizeof_long);
338 printf("#define JS_BITS_PER_FLOAT %dL\n", bpb * sizeof_float);
339 printf("#define JS_BITS_PER_DOUBLE %dL\n", bpb * sizeof_double);
340 printf("#define JS_BITS_PER_WORD %dL\n", bpb * sizeof_word);
341 printf("\n");
343 printf("#define JS_BITS_PER_BYTE_LOG2 %dL\n", Log2(bpb));
344 printf("#define JS_BITS_PER_SHORT_LOG2 %dL\n", Log2(bpb * sizeof_short));
345 printf("#define JS_BITS_PER_INT_LOG2 %dL\n", Log2(bpb * sizeof_int));
346 printf("#define JS_BITS_PER_INT64_LOG2 %dL\n", bits_per_int64_log2);
347 printf("#define JS_BITS_PER_LONG_LOG2 %dL\n", Log2(bpb * sizeof_long));
348 printf("#define JS_BITS_PER_FLOAT_LOG2 %dL\n", Log2(bpb * sizeof_float));
349 printf("#define JS_BITS_PER_DOUBLE_LOG2 %dL\n", Log2(bpb * sizeof_double));
350 printf("#define JS_BITS_PER_WORD_LOG2 %dL\n", Log2(bpb * sizeof_word));
351 printf("\n");
353 printf("#define JS_ALIGN_OF_SHORT %dL\n", align_of_short);
354 printf("#define JS_ALIGN_OF_INT %dL\n", align_of_int);
355 printf("#define JS_ALIGN_OF_LONG %dL\n", align_of_long);
356 printf("#define JS_ALIGN_OF_INT64 %dL\n", align_of_int64);
357 printf("#define JS_ALIGN_OF_FLOAT %dL\n", align_of_float);
358 printf("#define JS_ALIGN_OF_DOUBLE %dL\n", align_of_double);
359 printf("#define JS_ALIGN_OF_POINTER %dL\n", align_of_pointer);
360 printf("#define JS_ALIGN_OF_WORD %dL\n", align_of_word);
361 printf("\n");
363 printf("#define JS_BYTES_PER_WORD_LOG2 %dL\n", Log2(sizeof_word));
364 printf("#define JS_BYTES_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword));
365 printf("#define JS_WORDS_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword/sizeof_word));
366 printf("\n");
368 printf("#define JS_STACK_GROWTH_DIRECTION (%d)\n", StackGrowthDirection(&dummy1));
369 printf("\n");
371 printf("#endif /* js_cpucfg___ */\n");
373 return EXIT_SUCCESS;
374 }