summaryrefslogtreecommitdiff
path: root/base/genarch.c
blob: b58efb65e8a5d256c06a998b80e5d149a306fbf8 (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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/*
 * Generate a header file (arch.h) with parameters
 * reflecting the machine architecture and compiler characteristics.
 */

#include "stdpre.h"
#include <ctype.h>
#include <stdio.h>
/*
 * In theory, not all systems provide <string.h> or declare memset
 * there, but at this point we don't think we care about any that don't.
 */
#include <string.h>
#include <time.h>

/* We provide a _SIZEOF_ macro for GX_COLOR_INDEX_TYPE
   fallback to a generic int if no such type is defined.
   This default must be kept in sync with the one in gxcindex.h
   or ARCH_SIZEOF_GX_COLOR_INDEX will be incorrect for such builds. */
#ifndef GX_COLOR_INDEX_TYPE
#define GX_COLOR_INDEX_TYPE ulong
#endif

/* We should write the result on stdout, but the original Turbo C 'make' */
/* can't handle output redirection (sigh). */

static void
section(FILE * f, const char *str)
{
    fprintf(f, "\n\t /* ---------------- %s ---------------- */\n\n", str);
}

static void
define(FILE *f, const char *str)
{
    fprintf(f, "#define %s ", str);
}

static void
define_int(FILE *f, const char *str, int value)
{
    fprintf(f, "#define %s %d\n", str, value);
}

static void
print_ffs(FILE *f, int nbytes)
{
    int i;

    for (i = 0; i < nbytes; ++i)
        fprintf(f, "ff");
}

static int
ilog2(int n)
{
    int i = 0, m = n;

    while (m > 1)
        ++i, m = (m + 1) >> 1;
    return i;
}

static int
copy_existing_file(FILE *f, char *infname)
{
    FILE *in = fopen(infname, "r");
    char buffer[1024];
    size_t l;

    if (in == NULL) {
        fprintf(stderr, "genarch.c: can't open %s for reading\n", infname);
        fclose(f);
        return exit_FAILED;
    }
    while (!feof(in)) {
        l = fread(buffer, 1, 1024, in);
        if (l > 0)
            fwrite(buffer, 1, l, f);
    }
    fclose(in);
    fclose(f);

    return exit_OK;
}

int
main(int argc, char *argv[])
{
    char *fname;
    long one = 1;
    struct {
        char c;
        short s;
    } ss;
    struct {
        char c;
        int i;
    } si;
    struct {
        char c;
        long l;
    } sl;
    struct {
        char c;
        char *p;
    } sp;
    struct {
        char c;
        float f;
    } sf;
    struct {
        char c;
        double d;
    } sd;
    struct {
        char c;
        size_t z;
    } sz;
    long lm1 = -1;
    long lr1 = lm1 >> 1, lr2 = lm1 >> 2;
    int im1 = -1;
    int ir1 = im1 >> 1, ir2 = im1 >> 2;
    union {
        long l;
        char *p;
    } pl0, pl1;
    int ars;
    union {
        float f;
        int i;
        long l;
    } f0, f1, fm1;
    int floats_are_IEEE;
    FILE *f;

    if (argc < 2 || argc > 3) {
        fprintf(stderr, "genarch: Invalid invocation\n"
                "genarch <output-file> [ <existing-config-file> ]\n");
        return exit_FAILED;
    }

    fname = argv[1];
    f = fopen(fname, "w");
    if (f == NULL) {
        fprintf(stderr, "genarch.c: can't open %s for writing\n", fname);
        return exit_FAILED;
    }

    if (argc == 3) {
        return copy_existing_file(f, argv[2]);
    }

    fprintf(f, "/* Parameters derived from machine and compiler architecture. */\n");
    fprintf(f, "/* This file is generated mechanically by genarch.c. */\n");

    /* We have to test the size dynamically here, */
    /* because the preprocessor can't evaluate sizeof. */
    f0.f = 0.0, f1.f = 1.0, fm1.f = -1.0;
    floats_are_IEEE =
        (size_of(float) == size_of(int) ?
         f0.i == 0 && f1.i == (int)0x3f800000 && fm1.i == (int)0xbf800000 :
         f0.l == 0 && f1.l == 0x3f800000L && fm1.l == 0xbf800000L);

    section(f, "Scalar alignments");

#define OFFSET_IN(s, e) (int)((char *)&s.e - (char *)&s)
    define_int(f, "ARCH_ALIGN_SHORT_MOD", OFFSET_IN(ss, s));
    define_int(f, "ARCH_ALIGN_INT_MOD", OFFSET_IN(si, i));
    define_int(f, "ARCH_ALIGN_LONG_MOD", OFFSET_IN(sl, l));
    define_int(f, "ARCH_ALIGN_SIZE_T_MOD", OFFSET_IN(sz, z));

#if defined (GS_MEMPTR_ALIGNMENT) && GS_MEMPTR_ALIGNMENT != 0
    define_int(f, "ARCH_ALIGN_PTR_MOD", GS_MEMPTR_ALIGNMENT);
#else
#if defined (sparc) || defined (__hpux)
# ifndef __BIGGEST_ALIGNMENT__
#  define __BIGGEST_ALIGNMENT__ 8
# endif
    define_int(f, "ARCH_ALIGN_PTR_MOD", __BIGGEST_ALIGNMENT__);
#else
    define_int(f, "ARCH_ALIGN_PTR_MOD", OFFSET_IN(sp, p));
#endif
#endif

    define_int(f, "ARCH_ALIGN_FLOAT_MOD", OFFSET_IN(sf, f));
    define_int(f, "ARCH_ALIGN_DOUBLE_MOD", OFFSET_IN(sd, d));
#undef OFFSET_IN

    /* Some architectures have special alignment requirements for   */
    /* jmp_buf, and we used to provide ALIGN_STRUCT_MOD for that.   */
    /* We've now dropped that in favor of aligning jmp_buf by hand. */
    /* See setjmp_.h for the implementation of this.                */

    section(f, "Scalar sizes");

    define_int(f, "ARCH_LOG2_SIZEOF_CHAR", ilog2(size_of(char)));
    define_int(f, "ARCH_LOG2_SIZEOF_SHORT", ilog2(size_of(short)));
    define_int(f, "ARCH_LOG2_SIZEOF_INT", ilog2(size_of(int)));
    define_int(f, "ARCH_LOG2_SIZEOF_LONG", ilog2(size_of(long)));
    define_int(f, "ARCH_LOG2_SIZEOF_SIZE_T", ilog2(size_of(size_t)));
#if !defined(_MSC_VER) && ! (defined(__BORLANDC__) && defined(__WIN32__))
    /* MSVC does not provide 'long long' but we need this on some archs
       to define a 64 bit type. A corresponding #ifdef in stdint_.h handles
       that case for MSVC. Most other platforms do support long long if
       they have a 64 bit type at all */
    define_int(f, "ARCH_LOG2_SIZEOF_LONG_LONG", ilog2(size_of(long long)));
#endif
    define_int(f, "ARCH_SIZEOF_SIZE_T", size_of(size_t));
    define_int(f, "ARCH_SIZEOF_GX_COLOR_INDEX", sizeof(GX_COLOR_INDEX_TYPE));
    define_int(f, "ARCH_SIZEOF_PTR", size_of(char *));
    define_int(f, "ARCH_SIZEOF_FLOAT", size_of(float));
    define_int(f, "ARCH_SIZEOF_DOUBLE", size_of(double));
    if (floats_are_IEEE) {
        define_int(f, "ARCH_FLOAT_MANTISSA_BITS", 24);
        define_int(f, "ARCH_DOUBLE_MANTISSA_BITS", 53);
    } else {
        /*
         * There isn't any general way to compute the number of mantissa
         * bits accurately, especially if the machine uses hex rather
         * than binary exponents.  Use conservative values, assuming
         * the exponent is stored in a 16-bit word of its own.
         */
        define_int(f, "ARCH_FLOAT_MANTISSA_BITS", sizeof(float) * 8 - 17);
        define_int(f, "ARCH_DOUBLE_MANTISSA_BITS", sizeof(double) * 8 - 17);
    }

    section(f, "Unsigned max values");

    /*
     * We can't use fprintf with a numeric value for PRINT_MAX, because
     * too many compilers produce warnings or do the wrong thing for
     * complementing or widening unsigned types.
     */
#define PRINT_MAX(str, typ, tstr, l)\
  BEGIN\
    define(f, str);\
    fprintf(f, "((%s)0x", tstr);\
    print_ffs(f, sizeof(typ));\
    fprintf(f, "%s + (%s)0)\n", l, tstr);\
  END
    PRINT_MAX("ARCH_MAX_UCHAR", unsigned char, "unsigned char", "");
    PRINT_MAX("ARCH_MAX_USHORT", unsigned short, "unsigned short", "");
    /*
     * For uint and ulong, a different approach is required to keep gcc
     * with -Wtraditional from spewing out pointless warnings.
     */
    define(f, "ARCH_MAX_UINT");
    fprintf(f, "((unsigned int)~0 + (unsigned int)0)\n");
    define(f, "ARCH_MAX_ULONG");
    fprintf(f, "((unsigned long)~0L + (unsigned long)0)\n");
    define(f, "ARCH_MAX_SIZE_T");
    fprintf(f, "((size_t)~0L + (size_t)0)\n");
#undef PRINT_MAX

    section(f, "Miscellaneous");

    define_int(f, "ARCH_IS_BIG_ENDIAN", 1 - *(char *)&one);
    pl0.l = 0;
    pl1.l = -1;
    define_int(f, "ARCH_PTRS_ARE_SIGNED", (pl1.p < pl0.p));
    define_int(f, "ARCH_FLOATS_ARE_IEEE", (floats_are_IEEE ? 1 : 0));

    /*
     * There are three cases for arithmetic right shift:
     * always correct, correct except for right-shifting a long by 1
     * (a bug in some versions of the Turbo C compiler), and
     * never correct.
     */
    ars = (lr2 != -1 || ir1 != -1 || ir2 != -1 ? 0 :
           lr1 != -1 ? 1 :	/* Turbo C problem */
           2);
    define_int(f, "ARCH_ARITH_RSHIFT", ars);
    /*
     * Determine whether dividing a negative integer by a positive one
     * takes the floor or truncates toward zero.
     */
    define_int(f, "ARCH_DIV_NEG_POS_TRUNCATES", im1 / 2 == 0);

/* ---------------- Done. ---------------- */

    fclose(f);
    return exit_OK;
}