Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jscpucfg.cpp
blob028def676f5d5e2f177b0dfc2d5e847f9a212425
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
25 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
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.
39 * ***** END LICENSE BLOCK ***** */
42 * Generate CPU-specific bit-size and similar #defines.
44 #include <stdio.h>
45 #include <stdlib.h>
47 #if defined(CROSS_COMPILE) && !defined(FORCE_BIG_ENDIAN) && !defined(FORCE_LITTLE_ENDIAN)
48 #include <prtypes.h>
49 #endif
51 /************************************************************************/
53 int main(int argc, char **argv)
55 printf("#ifndef js_cpucfg___\n");
56 printf("#define js_cpucfg___\n\n");
58 printf("/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
60 #ifdef CROSS_COMPILE
61 #if defined(__APPLE__) && !defined(FORCE_BIG_ENDIAN) && !defined(FORCE_LITTLE_ENDIAN)
63 * Darwin NSPR uses the same MDCPUCFG (_darwin.cfg) for multiple
64 * processors, and determines which processor to configure for based
65 * on compiler predefined macros. We do the same thing here.
67 printf("#ifdef __LITTLE_ENDIAN__\n");
68 printf("#define IS_LITTLE_ENDIAN 1\n");
69 printf("#undef IS_BIG_ENDIAN\n");
70 printf("#else\n");
71 printf("#undef IS_LITTLE_ENDIAN\n");
72 printf("#define IS_BIG_ENDIAN 1\n");
73 printf("#endif\n\n");
74 #elif defined(IS_LITTLE_ENDIAN) || defined(FORCE_LITTLE_ENDIAN)
75 printf("#define IS_LITTLE_ENDIAN 1\n");
76 printf("#undef IS_BIG_ENDIAN\n\n");
77 #elif defined(IS_BIG_ENDIAN) || defined(FORCE_BIG_ENDIAN)
78 printf("#undef IS_LITTLE_ENDIAN\n");
79 printf("#define IS_BIG_ENDIAN 1\n\n");
80 #else
81 #error "Endianess not defined."
82 #endif
84 #else
87 * We don't handle PDP-endian or similar orders: if a short is big-endian,
88 * so must int and long be big-endian for us to generate the IS_BIG_ENDIAN
89 * #define and the IS_LITTLE_ENDIAN #undef.
92 int big_endian = 0, little_endian = 0, ntests = 0;
94 if (sizeof(short) == 2) {
95 /* force |volatile| here to get rid of any compiler optimisations
96 * (var in register etc.) which may be appiled to |auto| vars -
97 * even those in |union|s...
98 * (|static| is used to get the same functionality for compilers
99 * which do not honor |volatile|...).
101 volatile static union {
102 short i;
103 char c[2];
104 } u;
106 u.i = 0x0102;
107 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02);
108 little_endian += (u.c[0] == 0x02 && u.c[1] == 0x01);
109 ntests++;
112 if (sizeof(int) == 4) {
113 /* force |volatile| here ... */
114 volatile static union {
115 int i;
116 char c[4];
117 } u;
119 u.i = 0x01020304;
120 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
121 u.c[2] == 0x03 && u.c[3] == 0x04);
122 little_endian += (u.c[0] == 0x04 && u.c[1] == 0x03 &&
123 u.c[2] == 0x02 && u.c[3] == 0x01);
124 ntests++;
127 if (sizeof(long) == 8) {
128 /* force |volatile| here ... */
129 volatile static union {
130 long i;
131 char c[8];
132 } u;
135 * Write this as portably as possible: avoid 0x0102030405060708L
136 * and <<= 32.
138 u.i = 0x01020304;
139 u.i <<= 16, u.i <<= 16;
140 u.i |= 0x05060708;
141 big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
142 u.c[2] == 0x03 && u.c[3] == 0x04 &&
143 u.c[4] == 0x05 && u.c[5] == 0x06 &&
144 u.c[6] == 0x07 && u.c[7] == 0x08);
145 little_endian += (u.c[0] == 0x08 && u.c[1] == 0x07 &&
146 u.c[2] == 0x06 && u.c[3] == 0x05 &&
147 u.c[4] == 0x04 && u.c[5] == 0x03 &&
148 u.c[6] == 0x02 && u.c[7] == 0x01);
149 ntests++;
152 if (big_endian && big_endian == ntests) {
153 printf("#undef IS_LITTLE_ENDIAN\n");
154 printf("#define IS_BIG_ENDIAN 1\n\n");
155 } else if (little_endian && little_endian == ntests) {
156 printf("#define IS_LITTLE_ENDIAN 1\n");
157 printf("#undef IS_BIG_ENDIAN\n\n");
158 } else {
159 fprintf(stderr, "%s: unknown byte order"
160 "(big_endian=%d, little_endian=%d, ntests=%d)!\n",
161 argv[0], big_endian, little_endian, ntests);
162 return EXIT_FAILURE;
166 #endif /* CROSS_COMPILE */
168 // PA-RISC is the only platform we try to support on which the stack
169 // grows towards higher addresses. Trying to detect it here has
170 // historically led to portability problems, which aren't worth it
171 // given the near consensus on stack growth direction.
172 printf("#ifdef __hppa\n"
173 "# define JS_STACK_GROWTH_DIRECTION (1)\n"
174 "#else\n"
175 "# define JS_STACK_GROWTH_DIRECTION (-1)\n"
176 "#endif\n");
178 printf("#endif /* js_cpucfg___ */\n");
180 return EXIT_SUCCESS;