summaryrefslogtreecommitdiff
path: root/pr/include/gencfg.c
blob: b49f548c65703ec384c13f341e31e13e95e5c10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape Portable Runtime (NSPR).
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

#include <stdio.h>

#if defined(sgi)
#ifndef IRIX
	error - IRIX is not defined
#endif
#endif

#if defined(__sun)
#if defined(__svr4) || defined(__svr4__) || defined(__SVR4)
#ifndef SOLARIS
	error - SOLARIS is not defined
#endif
#else
#ifndef SUNOS4
	error - SUNOS4 is not defined
#endif
#endif
#endif

#if defined(__hpux)
#ifndef HPUX
	error - HPUX is not defined
#endif
#endif

#if defined(__alpha) 
#if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux)) && !(defined(__FreeBSD__))
	error - None of OSF1, _WIN32, __linux, or __FreeBSD__ is defined
#endif
#endif

#if defined(_IBMR2)
#ifndef AIX
	error - AIX is not defined
#endif
#endif

#if defined(linux)
#ifndef LINUX
	error - LINUX is not defined
#endif
#endif

#if defined(bsdi)
#ifndef BSDI
	error - BSDI is not defined
#endif
#endif

#if defined(M_UNIX)
#ifndef SCO
      error - SCO is not defined
#endif
#endif
#if !defined(M_UNIX) && defined(_USLC_)
#ifndef UNIXWARE
      error - UNIXWARE is not defined
#endif
#endif

#if defined(__APPLE__)
#ifndef RHAPSODY
      error - RHAPSODY is not defined
#endif
#endif

#if defined(__NeXT__)
#ifndef NEXTSTEP
      error - NEXTSTEP is not defined
#endif
#endif

/************************************************************************/

/* Generate cpucfg.h */

#ifdef XP_PC
#ifdef WIN32
#define INT64	_PRInt64
#else
#define INT64	long
#endif
#else
#if defined(HPUX) || defined(NECSVR4) || defined(SCO) || defined(UNIXWARE) || defined (NCR)
#define INT64	long
#else
#define INT64	long long
#endif
#endif

struct align_short {
    char c;
    short a;
};
struct align_int {
    char c;
    int a;
};
struct align_long {
    char c;
    long a;
};
struct align_PRInt64 {
    char c;
    INT64 a;
};
struct align_fakelonglong {
    char c;
    struct {
	long hi, lo;
    } a;
};
struct align_float {
    char c;
    float a;
};
struct align_double {
    char c;
    double a;
};
struct align_pointer {
    char c;
    void *a;
};

#define ALIGN_OF(type) \
    (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))

int bpb;

/* Used if shell doesn't support redirection. By default, assume it does. */
FILE *stream;

static int Log2(int n)
{
    int log2 = 0;

    if (n & (n-1))
	log2++;
    if (n >> 16)
	log2 += 16, n >>= 16;
    if (n >> 8)
	log2 += 8, n >>= 8;
    if (n >> 4)
	log2 += 4, n >>= 4;
    if (n >> 2)
	log2 += 2, n >>= 2;
    if (n >> 1)
	log2++;
    return log2;
}

/* We assume that int's are 32 bits */
static void do64(void)
{
    union {
	int i;
	char c[4];
    } u;

    u.i = 0x01020304;
    if (u.c[0] == 0x01) {
	fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
	fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
    } else {
	fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
	fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
    }
}

static void do32(void)
{
    union {
	long i;
	char c[4];
    } u;

    u.i = 0x01020304;
    if (u.c[0] == 0x01) {
	fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
	fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
    } else {
	fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
	fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
    }
}

/*
** Concievably this could actually be used; but there is lots of code out
** there with and's and shift's in it that assumes a byte is 8 bits, so
** forget about porting THIS code to those non 8 bit byte machines.
*/
static void BitsPerByte(void)
{
    bpb = 8;
}

int main(int argc, char **argv)
{
    BitsPerByte();

    /* If we got a command line argument, try to use it as the stream. */
    ++argv;
    if(*argv) {
        if(!(stream = fopen ( *argv, "wt" ))) {
            fprintf(stderr, "Could not write to output file %s.\n", *argv);
            return 1;
        }
    } else {
		stream = stdout;
	}

    fprintf(stream, "#ifndef nspr_cpucfg___\n");
    fprintf(stream, "#define nspr_cpucfg___\n\n");

    fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");

    if (sizeof(long) == 8) {
	do64();
    } else {
	do32();
    }
    fprintf(stream, "#define PR_BYTES_PER_BYTE   %d\n", sizeof(char));
    fprintf(stream, "#define PR_BYTES_PER_SHORT  %d\n", sizeof(short));
    fprintf(stream, "#define PR_BYTES_PER_INT    %d\n", sizeof(int));
    fprintf(stream, "#define PR_BYTES_PER_INT64  %d\n", 8);
    fprintf(stream, "#define PR_BYTES_PER_LONG   %d\n", sizeof(long));
    fprintf(stream, "#define PR_BYTES_PER_FLOAT  %d\n", sizeof(float));
    fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double));

    fprintf(stream, "#define PR_BITS_PER_BYTE    %d\n", bpb);
    fprintf(stream, "#define PR_BITS_PER_SHORT   %d\n", bpb * sizeof(short));
    fprintf(stream, "#define PR_BITS_PER_INT     %d\n", bpb * sizeof(int));
    fprintf(stream, "#define PR_BITS_PER_INT64   %d\n", bpb * 8);
    fprintf(stream, "#define PR_BITS_PER_LONG    %d\n", bpb * sizeof(long));
    fprintf(stream, "#define PR_BITS_PER_FLOAT   %d\n", bpb * sizeof(float));
    fprintf(stream, "#define PR_BITS_PER_DOUBLE  %d\n\n", 
            bpb * sizeof(double));

    fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2   %d\n", Log2(bpb));
    fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2  %d\n", 
            Log2(bpb * sizeof(short)));
    fprintf(stream, "#define PR_BITS_PER_INT_LOG2    %d\n", 
            Log2(bpb * sizeof(int)));
    fprintf(stream, "#define PR_BITS_PER_INT64_LOG2  %d\n", 6);
    fprintf(stream, "#define PR_BITS_PER_LONG_LOG2   %d\n", 
            Log2(bpb * sizeof(long)));
    fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2  %d\n", 
            Log2(bpb * sizeof(float)));
    fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 
            Log2(bpb * sizeof(double)));

    fprintf(stream, "#define PR_ALIGN_OF_SHORT   %d\n", ALIGN_OF(short));
    fprintf(stream, "#define PR_ALIGN_OF_INT     %d\n", ALIGN_OF(int));
    fprintf(stream, "#define PR_ALIGN_OF_LONG    %d\n", ALIGN_OF(long));
    if (sizeof(INT64) < 8) {
	/* this machine doesn't actually support PRInt64's */
	fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", 
                ALIGN_OF(fakelonglong));
    } else {
	fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", ALIGN_OF(PRInt64));
    }
    fprintf(stream, "#define PR_ALIGN_OF_FLOAT   %d\n", ALIGN_OF(float));
    fprintf(stream, "#define PR_ALIGN_OF_DOUBLE  %d\n", ALIGN_OF(double));
    fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer));

    fprintf(stream, "#endif /* nspr_cpucfg___ */\n");
    fclose(stream);

    return 0;
}