| 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 ***** */ | |
| 40 | ||
| 41 | /* | |
| 42 | * Generate CPU-specific bit-size and similar #defines. | |
| 43 | */ | |
| 44 | #include <stdio.h> | |
| 45 | #include <stdlib.h> | |
| 46 | ||
| 47 | #ifdef CROSS_COMPILE | |
| 48 | #include <prtypes.h> | |
| 49 | #define INT64 PRInt64 | |
| 50 | #else | |
| 51 | ||
| 52 | #ifdef __MWERKS__ | |
| 53 | #define XP_MAC 1 | |
| 54 | #endif | |
| 55 | ||
| 56 | /************************************************************************/ | |
| 57 | ||
| 58 | /* Generate cpucfg.h */ | |
| 59 | #ifdef XP_MAC | |
| 60 | #include <Types.h> | |
| 61 | #define INT64 UnsignedWide | |
| 62 | #else | |
| 63 | #if defined(XP_WIN) || defined(XP_OS2) | |
| 64 | #ifdef WIN32 | |
| 65 | #if defined(__GNUC__) | |
| 66 | #define INT64 long long | |
| 67 | #else | |
| 68 | #define INT64 _int64 | |
| 69 | #endif /* __GNUC__ */ | |
| 70 | #else | |
| 71 | #define INT64 long | |
| 72 | #endif | |
| 73 | #else | |
| 74 | #if defined(HPUX) || defined(__QNX__) || defined(_SCO_DS) || defined(UNIXWARE) | |
| 75 | #define INT64 long | |
| 76 | #else | |
| 77 | #define INT64 long long | |
| 78 | #endif | |
| 79 | #endif | |
| 80 | #endif | |
| 81 | ||
| 82 | #endif /* CROSS_COMPILE */ | |
| 83 | ||
| 84 | typedef void *prword; | |
| 85 | ||
| 86 | struct align_short { | |
| 87 | char c; | |
| 88 | short a; | |
| 89 | }; | |
| 90 | struct align_int { | |
| 91 | char c; | |
| 92 | int a; | |
| 93 | }; | |
| 94 | struct align_long { | |
| 95 | char c; | |
| 96 | long a; | |
| 97 | }; | |
| 98 | struct align_int64 { | |
| 99 | char c; | |
| 100 | INT64 a; | |
| 101 | }; | |
| 102 | struct align_fakelonglong { | |
| 103 | char c; | |
| 104 | struct { | |
| 105 | long hi, lo; | |
| 106 | } a; | |
| 107 | }; | |
| 108 | struct align_float { | |
| 109 | char c; | |
| 110 | float a; | |
| 111 | }; | |
| 112 | struct align_double { | |
| 113 | char c; | |
| 114 | double a; | |
| 115 | }; | |
| 116 | struct align_pointer { | |
| 117 | char c; | |
| 118 | void *a; | |
| 119 | }; | |
| 120 | struct align_prword { | |
| 121 | char c; | |
| 122 | prword a; | |
| 123 | }; | |
| 124 | ||
| 125 | #define ALIGN_OF(type) \ | |
| 126 | (((char*)&(((struct align_##type *)0)->a)) - ((char*)0)) | |
| 127 | ||
| 128 | unsigned int bpb; | |
| 129 | ||
| 130 | static int Log2(unsigned int n) | |
| 131 | 0 | { |
| 132 | 0 | int log2 = 0; |
| 133 | ||
| 134 | 0 | if (n & (n-1)) |
| 135 | 0 | log2++; |
| 136 | 0 | if (n >> 16) |
| 137 | 0 | log2 += 16, n >>= 16; |
| 138 | 0 | if (n >> 8) |
| 139 | 0 | log2 += 8, n >>= 8; |
| 140 | 0 | if (n >> 4) |
| 141 | 0 | log2 += 4, n >>= 4; |
| 142 | 0 | if (n >> 2) |
| 143 | 0 | log2 += 2, n >>= 2; |
| 144 | 0 | if (n >> 1) |
| 145 | 0 | log2++; |
| 146 | 0 | return log2; |
| 147 | } | |
| 148 | ||
| 149 | /* | |
| 150 | * Conceivably this could actually be used, but there is lots of code out | |
| 151 | * there with ands and shifts in it that assumes a byte is exactly 8 bits, | |
| 152 | * so forget about porting THIS code to all those non 8 bit byte machines. | |
| 153 | */ | |
| 154 | static void BitsPerByte(void) | |
| 155 | 0 | { |
| 156 | 0 | bpb = 8; |
| 157 | } | |
| 158 | ||
| 159 | static int StackGrowthDirection(int *dummy1addr) | |
| 160 | 0 | { |
| 161 | 0 | int dummy2; |
| 162 | ||
| 163 | 0 | return (&dummy2 < dummy1addr) ? -1 : 1; |
| 164 | } | |
| 165 | ||
| 166 | int main(int argc, char **argv) | |
| 167 | 0 | { |
| 168 | 0 | int sizeof_char, sizeof_short, sizeof_int, sizeof_int64, sizeof_long, |
| 169 | 0 | sizeof_float, sizeof_double, sizeof_word, sizeof_dword; |
| 170 | 0 | int bits_per_int64_log2, align_of_short, align_of_int, align_of_long, |
| 171 | 0 | align_of_int64, align_of_float, align_of_double, align_of_pointer, |
| 172 | 0 | align_of_word; |
| 173 | 0 | int dummy1; |
| 174 | ||
| 175 | 0 | BitsPerByte(); |
| 176 | ||
| 177 | 0 | printf("#ifndef js_cpucfg___\n"); |
| 178 | 0 | printf("#define js_cpucfg___\n\n"); |
| 179 | ||
| 180 | 0 | printf("/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n"); |
| 181 | ||
| 182 | #ifdef CROSS_COMPILE | |
| 183 | #if defined(IS_LITTLE_ENDIAN) | |
| 184 | printf("#define IS_LITTLE_ENDIAN 1\n"); | |
| 185 | printf("#undef IS_BIG_ENDIAN\n\n"); | |
| 186 | #elif defined(IS_BIG_ENDIAN) | |
| 187 | printf("#undef IS_LITTLE_ENDIAN\n"); | |
| 188 | printf("#define IS_BIG_ENDIAN 1\n\n"); | |
| 189 | #else | |
| 190 | #error "Endianess not defined." | |
| 191 | #endif | |
| 192 | ||
| 193 | sizeof_char = PR_BYTES_PER_BYTE; | |
| 194 | sizeof_short = PR_BYTES_PER_SHORT; | |
| 195 | sizeof_int = PR_BYTES_PER_INT; | |
| 196 | sizeof_int64 = PR_BYTES_PER_INT64; | |
| 197 | sizeof_long = PR_BYTES_PER_LONG; | |
| 198 | sizeof_float = PR_BYTES_PER_FLOAT; | |
| 199 | sizeof_double = PR_BYTES_PER_DOUBLE; | |
| 200 | sizeof_word = PR_BYTES_PER_WORD; | |
| 201 | sizeof_dword = PR_BYTES_PER_DWORD; | |
| 202 | ||
| 203 | bits_per_int64_log2 = PR_BITS_PER_INT64_LOG2; | |
| 204 | ||
| 205 | align_of_short = PR_ALIGN_OF_SHORT; | |
| 206 | align_of_int = PR_ALIGN_OF_INT; | |
| 207 | align_of_long = PR_ALIGN_OF_LONG; | |
| 208 | align_of_int64 = PR_ALIGN_OF_INT64; | |
| 209 | align_of_float = PR_ALIGN_OF_FLOAT; | |
| 210 | align_of_double = PR_ALIGN_OF_DOUBLE; | |
| 211 | align_of_pointer = PR_ALIGN_OF_POINTER; | |
| 212 | align_of_word = PR_ALIGN_OF_WORD; | |
| 213 | ||
| 214 | #else /* !CROSS_COMPILE */ | |
| 215 | ||
| 216 | /* | |
| 217 | * We don't handle PDP-endian or similar orders: if a short is big-endian, | |
| 218 | * so must int and long be big-endian for us to generate the IS_BIG_ENDIAN | |
| 219 | * #define and the IS_LITTLE_ENDIAN #undef. | |
| 220 | */ | |
| 221 | { | |
| 222 | 0 | int big_endian = 0, little_endian = 0, ntests = 0; |
| 223 | ||
| 224 | 0 | if (sizeof(short) == 2) { |
| 225 | /* force |volatile| here to get rid of any compiler optimisations | |
| 226 | * (var in register etc.) which may be appiled to |auto| vars - | |
| 227 | * even those in |union|s... | |
| 228 | * (|static| is used to get the same functionality for compilers | |
| 229 | * which do not honor |volatile|...). | |
| 230 | */ | |
| 231 | volatile static union { | |
| 232 | short i; | |
| 233 | char c[2]; | |
| 234 | 0 | } u; |
| 235 | ||
| 236 | 0 | u.i = 0x0102; |
| 237 | 0 | big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02); |
| 238 | 0 | little_endian += (u.c[0] == 0x02 && u.c[1] == 0x01); |
| 239 | 0 | ntests++; |
| 240 | } | |
| 241 | ||
| 242 | 0 | if (sizeof(int) == 4) { |
| 243 | /* force |volatile| here ... */ | |
| 244 | volatile static union { | |
| 245 | int i; | |
| 246 | char c[4]; | |
| 247 | 0 | } u; |
| 248 | ||
| 249 | 0 | u.i = 0x01020304; |
| 250 | 0 | big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 && |
| 251 | u.c[2] == 0x03 && u.c[3] == 0x04); | |
| 252 | 0 | little_endian += (u.c[0] == 0x04 && u.c[1] == 0x03 && |
| 253 | u.c[2] == 0x02 && u.c[3] == 0x01); | |
| 254 | 0 | ntests++; |
| 255 | } | |
| 256 | ||
| 257 | 0 | if (sizeof(long) == 8) { |
| 258 | /* force |volatile| here ... */ | |
| 259 | volatile static union { | |
| 260 | long i; | |
| 261 | char c[8]; | |
| 262 | 0 | } u; |
| 263 | ||
| 264 | /* | |
| 265 | * Write this as portably as possible: avoid 0x0102030405060708L | |
| 266 | * and <<= 32. | |
| 267 | */ | |
| 268 | 0 | u.i = 0x01020304; |
| 269 | 0 | u.i <<= 16, u.i <<= 16; |
| 270 | 0 | u.i |= 0x05060708; |
| 271 | 0 | big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 && |
| 272 | u.c[2] == 0x03 && u.c[3] == 0x04 && | |
| 273 | u.c[4] == 0x05 && u.c[5] == 0x06 && | |
| 274 | u.c[6] == 0x07 && u.c[7] == 0x08); | |
| 275 | 0 | little_endian += (u.c[0] == 0x08 && u.c[1] == 0x07 && |
| 276 | u.c[2] == 0x06 && u.c[3] == 0x05 && | |
| 277 | u.c[4] == 0x04 && u.c[5] == 0x03 && | |
| 278 | u.c[6] == 0x02 && u.c[7] == 0x01); | |
| 279 | 0 | ntests++; |
| 280 | } | |
| 281 | ||
| 282 | 0 | if (big_endian && big_endian == ntests) { |
| 283 | 0 | printf("#undef IS_LITTLE_ENDIAN\n"); |
| 284 | 0 | printf("#define IS_BIG_ENDIAN 1\n\n"); |
| 285 | 0 | } else if (little_endian && little_endian == ntests) { |
| 286 | 0 | printf("#define IS_LITTLE_ENDIAN 1\n"); |
| 287 | 0 | printf("#undef IS_BIG_ENDIAN\n\n"); |
| 288 | } else { | |
| 289 | 0 | fprintf(stderr, "%s: unknown byte order" |
| 290 | "(big_endian=%d, little_endian=%d, ntests=%d)!\n", | |
| 291 | argv[0], big_endian, little_endian, ntests); | |
| 292 | 0 | return EXIT_FAILURE; |
| 293 | } | |
| 294 | } | |
| 295 | ||
| 296 | 0 | sizeof_char = sizeof(char); |
| 297 | 0 | sizeof_short = sizeof(short); |
| 298 | 0 | sizeof_int = sizeof(int); |
| 299 | 0 | sizeof_int64 = 8; |
| 300 | 0 | sizeof_long = sizeof(long); |
| 301 | 0 | sizeof_float = sizeof(float); |
| 302 | 0 | sizeof_double = sizeof(double); |
| 303 | 0 | sizeof_word = sizeof(prword); |
| 304 | 0 | sizeof_dword = 8; |
| 305 | ||
| 306 | 0 | bits_per_int64_log2 = 6; |
| 307 | ||
| 308 | 0 | align_of_short = ALIGN_OF(short); |
| 309 | 0 | align_of_int = ALIGN_OF(int); |
| 310 | 0 | align_of_long = ALIGN_OF(long); |
| 311 | 0 | if (sizeof(INT64) < 8) { |
| 312 | /* this machine doesn't actually support int64's */ | |
| 313 | 0 | align_of_int64 = ALIGN_OF(fakelonglong); |
| 314 | } else { | |
| 315 | 0 | align_of_int64 = ALIGN_OF(int64); |
| 316 | } | |
| 317 | 0 | align_of_float = ALIGN_OF(float); |
| 318 | 0 | align_of_double = ALIGN_OF(double); |
| 319 | 0 | align_of_pointer = ALIGN_OF(pointer); |
| 320 | 0 | align_of_word = ALIGN_OF(prword); |
| 321 | ||
| 322 | #endif /* CROSS_COMPILE */ | |
| 323 | ||
| 324 | 0 | printf("#define JS_BYTES_PER_BYTE %dL\n", sizeof_char); |
| 325 | 0 | printf("#define JS_BYTES_PER_SHORT %dL\n", sizeof_short); |
| 326 | 0 | printf("#define JS_BYTES_PER_INT %dL\n", sizeof_int); |
| 327 | 0 | printf("#define JS_BYTES_PER_INT64 %dL\n", sizeof_int64); |
| 328 | 0 | printf("#define JS_BYTES_PER_LONG %dL\n", sizeof_long); |
| 329 | 0 | printf("#define JS_BYTES_PER_FLOAT %dL\n", sizeof_float); |
| 330 | 0 | printf("#define JS_BYTES_PER_DOUBLE %dL\n", sizeof_double); |
| 331 | 0 | printf("#define JS_BYTES_PER_WORD %dL\n", sizeof_word); |
| 332 | 0 | printf("#define JS_BYTES_PER_DWORD %dL\n", sizeof_dword); |
| 333 | 0 | printf("\n"); |
| 334 | ||
| 335 | 0 | printf("#define JS_BITS_PER_BYTE %dL\n", bpb); |
| 336 | 0 | printf("#define JS_BITS_PER_SHORT %dL\n", bpb * sizeof_short); |
| 337 | 0 | printf("#define JS_BITS_PER_INT %dL\n", bpb * sizeof_int); |
| 338 | 0 | printf("#define JS_BITS_PER_INT64 %dL\n", bpb * sizeof_int64); |
| 339 | 0 | printf("#define JS_BITS_PER_LONG %dL\n", bpb * sizeof_long); |
| 340 | 0 | printf("#define JS_BITS_PER_FLOAT %dL\n", bpb * sizeof_float); |
| 341 | 0 | printf("#define JS_BITS_PER_DOUBLE %dL\n", bpb * sizeof_double); |
| 342 | 0 | printf("#define JS_BITS_PER_WORD %dL\n", bpb * sizeof_word); |
| 343 | 0 | printf("\n"); |
| 344 | ||
| 345 | 0 | printf("#define JS_BITS_PER_BYTE_LOG2 %dL\n", Log2(bpb)); |
| 346 | 0 | printf("#define JS_BITS_PER_SHORT_LOG2 %dL\n", Log2(bpb * sizeof_short)); |
| 347 | 0 | printf("#define JS_BITS_PER_INT_LOG2 %dL\n", Log2(bpb * sizeof_int)); |
| 348 | 0 | printf("#define JS_BITS_PER_INT64_LOG2 %dL\n", bits_per_int64_log2); |
| 349 | 0 | printf("#define JS_BITS_PER_LONG_LOG2 %dL\n", Log2(bpb * sizeof_long)); |
| 350 | 0 | printf("#define JS_BITS_PER_FLOAT_LOG2 %dL\n", Log2(bpb * sizeof_float)); |
| 351 | 0 | printf("#define JS_BITS_PER_DOUBLE_LOG2 %dL\n", Log2(bpb * sizeof_double)); |
| 352 | 0 | printf("#define JS_BITS_PER_WORD_LOG2 %dL\n", Log2(bpb * sizeof_word)); |
| 353 | 0 | printf("\n"); |
| 354 | ||
| 355 | 0 | printf("#define JS_ALIGN_OF_SHORT %dL\n", align_of_short); |
| 356 | 0 | printf("#define JS_ALIGN_OF_INT %dL\n", align_of_int); |
| 357 | 0 | printf("#define JS_ALIGN_OF_LONG %dL\n", align_of_long); |
| 358 | 0 | printf("#define JS_ALIGN_OF_INT64 %dL\n", align_of_int64); |
| 359 | 0 | printf("#define JS_ALIGN_OF_FLOAT %dL\n", align_of_float); |
| 360 | 0 | printf("#define JS_ALIGN_OF_DOUBLE %dL\n", align_of_double); |
| 361 | 0 | printf("#define JS_ALIGN_OF_POINTER %dL\n", align_of_pointer); |
| 362 | 0 | printf("#define JS_ALIGN_OF_WORD %dL\n", align_of_word); |
| 363 | 0 | printf("\n"); |
| 364 | ||
| 365 | 0 | printf("#define JS_BYTES_PER_WORD_LOG2 %dL\n", Log2(sizeof_word)); |
| 366 | 0 | printf("#define JS_BYTES_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword)); |
| 367 | 0 | printf("#define JS_WORDS_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword/sizeof_word)); |
| 368 | 0 | printf("\n"); |
| 369 | ||
| 370 | 0 | printf("#define JS_STACK_GROWTH_DIRECTION (%d)\n", StackGrowthDirection(&dummy1)); |
| 371 | 0 | printf("\n"); |
| 372 | ||
| 373 | 0 | printf("#endif /* js_cpucfg___ */\n"); |
| 374 | ||
| 375 | 0 | return EXIT_SUCCESS; |