Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/************************************************************************/
62
/*									*/
63
/*		gen_tokens.c	Version 2.0				*/
64
/*									*/
65
/*  This program produces some of the target dependency tokens		*/
66
/*  in 'PL TDF' or 'tnc' format for the target system it is		*/
67
/*  compiled and run on.						*/
68
/*									*/
69
/*  Results are not guaranteed - they should be checked.		*/
70
/*									*/
71
/*  Each token value may be pre-set by externally defining a macro	*/
72
/*  with the appropriate local name and value.				*/
73
/*									*/
74
/*  Output format is PL TDF by default, 'tnc' if the macro TNC_SRC	*/
75
/*  is defined.								*/
76
/*									*/
77
/************************************************************************/
78
 
79
 
80
#include <stdio.h>
81
#include <stddef.h>
82
#include <limits.h>
83
/* Defined in '../../../../utilities/shared' */
84
 
85
 
7 7u83 86
static void
87
out_nat(char *name, int value)
2 7u83 88
{
89
#ifdef TNC_SRC
90
	(void)printf("( make_tokdef .~%s nat %d )\n\n", name, value);
91
#else	/* PL TDF source */
92
	(void)printf("Tokdef .~%s = [] NAT %d;\n\n", name, value);
93
#endif
94
	return;
95
}
96
 
97
 
7 7u83 98
static void
99
out_bool(char *name, int value)
2 7u83 100
{
7 7u83 101
	char *str = (value ? "true" : "false");
2 7u83 102
#ifdef TNC_SRC
103
	(void)printf("( make_tokdef .~%s bool %s )\n\n", name, str);
104
#else	/* PL TDF source */
105
	(void)printf("Tokdef .~%s = [] BOOL %s;\n\n", name, str);
106
#endif
107
	return;
108
}
109
 
110
 
7 7u83 111
static int
112
calc_width(unsigned long s_max)
2 7u83 113
{
114
	int i;
115
	for (i=1;;i++) {
7 7u83 116
		if ((((unsigned long)1) <<i) > s_max) return i+1;
2 7u83 117
	}
118
	/* UNREACHED */
119
}
120
 
121
#define width(c)	calc_width((unsigned long)(c))
122
 
123
 
7 7u83 124
int
125
main(void)
2 7u83 126
{
127
	int c_width, s_width, i_width, l_width, sz_width;
128
	int p_width, al_width, str_ch_width;
129
	int c_sgn, bf_sgn, bdiv, l_end;
130
 
131
#ifdef char_width
132
	c_width = char_width;
133
#else
134
	c_width = width(SCHAR_MAX);
135
#endif
136
	out_nat("char_width", c_width);
137
 
138
#ifdef short_width
139
	s_width = short_width;
140
#else
141
	s_width = width(SHRT_MAX);
142
#endif
143
	out_nat("short_width", s_width);
144
 
145
#ifdef int_width
146
	i_width = int_width;
147
#else
148
	i_width = width(INT_MAX);
149
#endif
150
	out_nat("int_width", i_width);
151
 
152
#ifdef long_width
153
	l_width = long_width;
154
#else
155
	l_width = width(LONG_MAX);
156
#endif
157
	out_nat("long_width", l_width);
158
 
159
#ifdef size_t_width
160
	sz_width = size_t_width;
161
#else
7 7u83 162
	sz_width = sizeof(size_t)* c_width;
2 7u83 163
#endif
164
	out_nat("size_t_width", sz_width);
165
 
166
#ifdef ptr_width
167
	p_width = ptr_width;
168
#else
7 7u83 169
	p_width = sizeof(char*)* c_width;
2 7u83 170
#endif
171
	out_nat("ptr_width", p_width);
172
 
173
#ifdef min_struct_rep
174
	al_width = min_struct_rep;
175
	str_ch_width = min_struct_rep;
176
#else
177
	{
178
		struct t { char c; struct { char c; } s; };
179
		struct c { char c; };
7 7u83 180
		al_width = offsetof(struct t,s)* c_width;
181
		str_ch_width = sizeof(struct c)* c_width;
2 7u83 182
	}
183
#endif
7 7u83 184
	if (al_width == str_ch_width) {
2 7u83 185
		out_nat("min_struct_rep", al_width);
7 7u83 186
	}
2 7u83 187
 
188
#ifdef char_is_signed
189
	c_sgn = char_is_signed;
190
#else
7 7u83 191
	c_sgn = (CHAR_MIN<0)? 1 : 0;
2 7u83 192
#endif
193
	out_nat("char_is_signed", c_sgn);
194
 
195
#ifdef bitfield_is_signed
196
	bf_sgn = bitfield_is_signed;
197
#else
198
	{
199
		struct { int ibits : 2; } s;
200
		s.ibits = -1;
7 7u83 201
		bf_sgn = (s.ibits<0)? 1 : 0;
2 7u83 202
	}
203
#endif
204
	out_nat("bitfield_is_signed", bf_sgn);
205
 
206
#ifdef best_div
207
	bdiv = best_div;
208
#else
7 7u83 209
	bdiv = (4%(-6) >0) +1;
2 7u83 210
#endif
211
	out_nat("best_div", bdiv);
212
 
213
#ifdef little_endian
214
	l_end = little_endian;
215
#else
216
	{
7 7u83 217
		unsigned u;
218
		u = ('a' << 24) | ('b' << 16) | ('c' << 8) | ('d' << 0);
2 7u83 219
		l_end = -1;
7 7u83 220
		if (sizeof(u) ==4 && *(char *) &u == 'd')l_end = 1;
221
		if (sizeof(u) ==4 && *(char *) &u == 'a')l_end = 0;
2 7u83 222
	}
223
#endif
7 7u83 224
	if (l_end >= 0 && l_end <= 1) {
2 7u83 225
		out_bool("little_endian", l_end);
7 7u83 226
	}
2 7u83 227
 
228
	return 0;
229
}