Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251
WebSVN – tendra.SVN – Diff – /trunk/src/producers/common/parse/constant.c – Rev 2 and 7

Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
29
 
59
 
30
 
60
 
31
#include "config.h"
61
#include "config.h"
32
#include "c_types.h"
62
#include "c_types.h"
33
#include "etype_ops.h"
63
#include "etype_ops.h"
34
#include "exp_ops.h"
64
#include "exp_ops.h"
Line 62... Line 92...
62
 
92
 
63
    These arrays are used to hold the small integer literals to avoid
93
    These arrays are used to hold the small integer literals to avoid
64
    duplication.
94
    duplication.
65
*/
95
*/
66
 
96
 
67
NAT small_nat [ SMALL_NAT_SIZE ] ;
97
NAT small_nat[SMALL_NAT_SIZE];
68
NAT small_neg_nat [ SMALL_NAT_SIZE ] ;
98
NAT small_neg_nat[SMALL_NAT_SIZE];
69
 
99
 
70
 
100
 
71
/*
101
/*
72
    SMALL NUMBERS
102
    SMALL NUMBERS
73
 
103
 
74
    These strings are used to hold strings representing the small integer
104
    These strings are used to hold strings representing the small integer
75
    literals to avoid duplication.
105
    literals to avoid duplication.
76
*/
106
*/
77
 
107
 
78
string small_number [ SMALL_FLT_SIZE ] ;
108
string small_number[SMALL_FLT_SIZE];
79
 
109
 
80
 
110
 
81
/*
111
/*
82
    CREATE A SMALL NUMBER
112
    CREATE A SMALL NUMBER
83
 
113
 
84
    This routine returns the element of the arrays small_nat or small_neg_nat
114
    This routine returns the element of the arrays small_nat or small_neg_nat
85
    corresponding to the value v, allocating it if necessary.
115
    corresponding to the value v, allocating it if necessary.
86
*/
116
*/
87
 
117
 
88
NAT make_small_nat
118
NAT
89
    PROTO_N ( ( v ) )
-
 
90
    PROTO_T ( int v )
119
make_small_nat(int v)
91
{
120
{
92
    NAT n ;
121
	NAT n;
93
    if ( v >= 0 ) {
122
	if (v >= 0) {
94
	n = small_nat [v] ;
123
		n = small_nat[v];
95
	if ( IS_NULL_nat ( n ) ) {
124
		if (IS_NULL_nat(n)) {
96
	    MAKE_nat_small ( ( unsigned ) v, n ) ;
125
			MAKE_nat_small((unsigned)v, n);
97
	    small_nat [v] = n ;
126
			small_nat[v] = n;
-
 
127
		}
-
 
128
	} else {
-
 
129
		v = -v;
-
 
130
		n = small_neg_nat[v];
-
 
131
		if (IS_NULL_nat(n)) {
-
 
132
			n = make_small_nat(v);
-
 
133
			MAKE_nat_neg(n, n);
-
 
134
			small_neg_nat[v] = n;
-
 
135
		}
98
	}
136
	}
99
    } else {
-
 
100
	v = -v ;
-
 
101
	n = small_neg_nat [v] ;
-
 
102
	if ( IS_NULL_nat ( n ) ) {
-
 
103
	    n = make_small_nat ( v ) ;
-
 
104
	    MAKE_nat_neg ( n, n ) ;
-
 
105
	    small_neg_nat [v] = n ;
-
 
106
	}
-
 
107
    }
-
 
108
    return ( n ) ;
137
	return(n);
109
}
138
}
110
 
139
 
111
 
140
 
112
/*
141
/*
113
    CONSTANT EVALUATION BUFFERS
142
    CONSTANT EVALUATION BUFFERS
114
 
143
 
115
    These lists are used to hold single digit lists in the constant
144
    These lists are used to hold single digit lists in the constant
116
    evaluation routines to allow for uniform handling of both small and
145
    evaluation routines to allow for uniform handling of both small and
117
    large literals.
146
    large literals.
118
*/
147
*/
119
 
148
 
120
static LIST ( unsigned ) small_nat_1 ;
149
static LIST(unsigned)small_nat_1;
121
static LIST ( unsigned ) small_nat_2 ;
150
static LIST(unsigned)small_nat_2;
122
 
151
 
123
 
152
 
124
/*
153
/*
125
    ALLOCATE A DIGIT LIST
154
    ALLOCATE A DIGIT LIST
126
 
155
 
127
    This routine allocates a list of digits of length n.  The digits in the
156
    This routine allocates a list of digits of length n.  The digits in the
128
    list are initialised to zero.
157
    list are initialised to zero.
129
*/
158
*/
130
 
159
 
131
static LIST ( unsigned ) digit_list
160
static LIST(unsigned)
132
    PROTO_N ( ( n ) )
-
 
133
    PROTO_T ( unsigned n )
161
digit_list(unsigned n)
134
{
162
{
135
    LIST ( unsigned ) p = NULL_list ( unsigned ) ;
163
	LIST(unsigned)p = NULL_list(unsigned);
136
    while ( n ) {
164
	while (n) {
137
	CONS_unsigned ( 0, p, p ) ;
165
		CONS_unsigned(0, p, p);
138
	n-- ;
166
		n--;
139
    }
167
	}
140
    return ( p ) ;
168
	return(p);
141
}
169
}
142
 
170
 
143
 
171
 
144
/*
172
/*
145
    MAKE AN EXTENDED VALUE INTO AN INTEGER CONSTANT
173
    MAKE AN EXTENDED VALUE INTO AN INTEGER CONSTANT
146
 
174
 
147
    This routine creates an integer constant from an extended value, v.
175
    This routine creates an integer constant from an extended value, v.
148
*/
176
*/
149
 
177
 
150
NAT make_nat_value
178
NAT
151
    PROTO_N ( ( v ) )
-
 
152
    PROTO_T ( unsigned long v )
179
make_nat_value(unsigned long v)
153
{
180
{
154
    NAT n ;
181
	NAT n;
155
    unsigned lo = LO_HALF ( v ) ;
182
	unsigned lo = LO_HALF(v);
156
    unsigned hi = HI_HALF ( v ) ;
183
	unsigned hi = HI_HALF(v);
157
    if ( hi ) {
184
	if (hi) {
158
	LIST ( unsigned ) p = NULL_list ( unsigned ) ;
185
		LIST(unsigned)p = NULL_list(unsigned);
159
	CONS_unsigned ( hi, p, p ) ;
186
		CONS_unsigned(hi, p, p);
160
	CONS_unsigned ( lo, p, p ) ;
187
		CONS_unsigned(lo, p, p);
161
	MAKE_nat_large ( p, n ) ;
188
		MAKE_nat_large(p, n);
162
    } else if ( lo < SMALL_NAT_SIZE ) {
189
	} else if (lo < SMALL_NAT_SIZE) {
163
	n = small_nat [ lo ] ;
190
		n = small_nat[lo];
164
	if ( IS_NULL_nat ( n ) ) n = make_small_nat ( ( int ) lo ) ;
191
		if (IS_NULL_nat(n))n = make_small_nat((int)lo);
165
    } else {
192
	} else {
166
	MAKE_nat_small ( lo, n ) ;
193
		MAKE_nat_small(lo, n);
167
    }
194
	}
168
    return ( n ) ;
195
	return(n);
169
}
196
}
170
 
197
 
171
 
198
 
172
/*
199
/*
173
    MAKE AN INTEGER CONSTANT INTO AN EXTENDED VALUE
200
    MAKE AN INTEGER CONSTANT INTO AN EXTENDED VALUE
174
 
201
 
175
    This routine finds the extended value corresponding to the integer
202
    This routine finds the extended value corresponding to the integer
176
    constant n.  If n is the null constant or does not fit into an extended
203
    constant n.  If n is the null constant or does not fit into an extended
177
    value then the maximum extended value is returned.
204
    value then the maximum extended value is returned.
178
*/
205
*/
179
 
206
 
180
unsigned long get_nat_value
207
unsigned long
181
    PROTO_N ( ( n ) )
-
 
182
    PROTO_T ( NAT n )
208
get_nat_value(NAT n)
183
{
209
{
184
    if ( !IS_NULL_nat ( n ) ) {
210
	if (!IS_NULL_nat(n)) {
185
	unsigned tag = TAG_nat ( n ) ;
211
		unsigned tag = TAG_nat(n);
186
	if ( tag == nat_small_tag ) {
212
		if (tag == nat_small_tag) {
187
	    unsigned val = DEREF_unsigned ( nat_small_value ( n ) ) ;
213
			unsigned val = DEREF_unsigned(nat_small_value(n));
188
	    return ( EXTEND_VALUE ( val ) ) ;
214
			return(EXTEND_VALUE(val));
189
	} else if ( tag == nat_large_tag ) {
215
		} else if (tag == nat_large_tag) {
190
	    LIST ( unsigned ) p = DEREF_list ( nat_large_values ( n ) ) ;
216
			LIST(unsigned)p = DEREF_list(nat_large_values(n));
191
	    if ( LENGTH_list ( p ) == 2 ) {
217
			if (LENGTH_list(p) == 2) {
192
		unsigned v1, v2 ;
218
				unsigned v1, v2;
193
		v1 = DEREF_unsigned ( HEAD_list ( p ) ) ;
219
				v1 = DEREF_unsigned(HEAD_list(p));
194
		v2 = DEREF_unsigned ( HEAD_list ( TAIL_list ( p ) ) ) ;
220
				v2 = DEREF_unsigned(HEAD_list(TAIL_list(p)));
195
		return ( COMBINE_VALUES ( v1, v2 ) ) ;
221
				return(COMBINE_VALUES(v1, v2));
196
	    }
222
			}
197
	}
223
		}
198
    }
224
	}
199
    return ( EXTENDED_MAX ) ;
225
	return(EXTENDED_MAX);
200
}
226
}
201
 
227
 
202
 
228
 
203
/*
229
/*
204
    MAKE A LIST OF DIGITS INTO AN INTEGER CONSTANT
230
    MAKE A LIST OF DIGITS INTO AN INTEGER CONSTANT
205
 
231
 
206
    This routine creates an integer constant from a list of digits, p.
232
    This routine creates an integer constant from a list of digits, p.
207
    This list may contain initial zero digits, which need to be removed.
233
    This list may contain initial zero digits, which need to be removed.
208
*/
234
*/
209
 
235
 
210
NAT make_large_nat
236
NAT
211
    PROTO_N ( ( p ) )
-
 
212
    PROTO_T ( LIST ( unsigned ) p )
237
make_large_nat(LIST(unsigned)p)
213
{
238
{
214
    NAT n ;
239
	NAT n;
215
    LIST ( unsigned ) q = p ;
240
	LIST(unsigned)q = p;
216
    LIST ( unsigned ) r = p ;
241
	LIST(unsigned)r = p;
217
 
-
 
218
    /* Scan for last nonzero digit */
-
 
219
    while ( !IS_NULL_list ( q ) ) {
-
 
220
	unsigned v = DEREF_unsigned ( HEAD_list ( q ) ) ;
-
 
221
	if ( v != 0 ) r = q ;
-
 
222
	q = TAIL_list ( q ) ;
-
 
223
    }
-
 
224
 
242
 
-
 
243
	/* Scan for last nonzero digit */
-
 
244
	while (!IS_NULL_list(q)) {
-
 
245
		unsigned v = DEREF_unsigned(HEAD_list(q));
-
 
246
		if (v != 0)r = q;
-
 
247
		q = TAIL_list(q);
-
 
248
	}
-
 
249
 
225
    /* Construct result */
250
	/* Construct result */
226
    if ( EQ_list ( r, p ) ) {
251
	if (EQ_list(r, p)) {
227
	/* Small values */
252
		/* Small values */
228
	unsigned v = DEREF_unsigned ( HEAD_list ( p ) ) ;
253
		unsigned v = DEREF_unsigned(HEAD_list(p));
229
	if ( v < SMALL_NAT_SIZE ) {
254
		if (v < SMALL_NAT_SIZE) {
230
	    n = make_small_nat ( ( int ) v ) ;
255
			n = make_small_nat((int)v);
-
 
256
		} else {
-
 
257
			MAKE_nat_small(v, n);
-
 
258
		}
-
 
259
		DESTROY_list(p, SIZE_unsigned);
231
	} else {
260
	} else {
-
 
261
		/* Large values */
-
 
262
		q = TAIL_list(r);
-
 
263
		COPY_list(PTR_TAIL_list(r), NULL_list(unsigned));
-
 
264
		DESTROY_list(q, SIZE_unsigned);
232
	    MAKE_nat_small ( v, n ) ;
265
		MAKE_nat_large(p, n);
233
	}
266
	}
234
	DESTROY_list ( p, SIZE_unsigned ) ;
-
 
235
    } else {
-
 
236
	/* Large values */
-
 
237
	q = TAIL_list ( r ) ;
-
 
238
	COPY_list ( PTR_TAIL_list ( r ), NULL_list ( unsigned ) ) ;
-
 
239
	DESTROY_list ( q, SIZE_unsigned ) ;
-
 
240
	MAKE_nat_large ( p, n ) ;
-
 
241
    }
-
 
242
    return ( n ) ;
267
	return(n);
243
}
268
}
244
 
269
 
245
 
270
 
246
/*
271
/*
247
    BUILD UP AN INTEGER CONSTANT
272
    BUILD UP AN INTEGER CONSTANT
Line 251... Line 276...
251
    the base and d the digit being added.  b will not be zero, and n will
276
    the base and d the digit being added.  b will not be zero, and n will
252
    be a simple constant.  Note that the original value of n is overwritten
277
    be a simple constant.  Note that the original value of n is overwritten
253
    with the return value.
278
    with the return value.
254
*/
279
*/
255
 
280
 
256
NAT make_nat_literal
281
NAT
257
    PROTO_N ( ( n, b, d ) )
-
 
258
    PROTO_T ( NAT n X unsigned b X unsigned d )
282
make_nat_literal(NAT n, unsigned b, unsigned d)
259
{
283
{
260
    NAT res ;
284
	NAT res;
261
    unsigned long lb = EXTEND_VALUE ( b ) ;
285
	unsigned long lb = EXTEND_VALUE(b);
262
 
286
 
263
    if ( IS_NULL_nat ( n ) ) {
287
	if (IS_NULL_nat(n)) {
264
	/* Map null integer to zero */
288
		/* Map null integer to zero */
265
	unsigned long ld = EXTEND_VALUE ( d ) ;
289
		unsigned long ld = EXTEND_VALUE(d);
266
	res = make_nat_value ( ld ) ;
290
		res = make_nat_value(ld);
267
 
291
 
268
    } else if ( IS_nat_small ( n ) ) {
292
	} else if (IS_nat_small(n)) {
269
	/* Small integers */
293
		/* Small integers */
270
	unsigned val = DEREF_unsigned ( nat_small_value ( n ) ) ;
294
		unsigned val = DEREF_unsigned(nat_small_value(n));
271
	unsigned long lv = EXTEND_VALUE ( val ) ;
295
		unsigned long lv = EXTEND_VALUE(val);
272
	unsigned long ld = EXTEND_VALUE ( d ) ;
296
		unsigned long ld = EXTEND_VALUE(d);
273
	unsigned long lr = lv * lb + ld ;
297
		unsigned long lr = lv * lb + ld;
274
	unsigned r1 = LO_HALF ( lr ) ;
298
		unsigned r1 = LO_HALF(lr);
275
	unsigned r2 = HI_HALF ( lr ) ;
299
		unsigned r2 = HI_HALF(lr);
276
 
300
 
277
	if ( r2 == 0 ) {
301
		if (r2 == 0) {
278
	    /* Result remains small */
302
			/* Result remains small */
279
	    if ( r1 < SMALL_NAT_SIZE ) {
303
			if (r1 < SMALL_NAT_SIZE) {
280
		res = small_nat [ r1 ] ;
304
				res = small_nat[r1];
281
		if ( IS_NULL_nat ( res ) ) {
305
				if (IS_NULL_nat(res)) {
282
		    res = make_small_nat ( ( int ) r1 ) ;
306
					res = make_small_nat((int)r1);
-
 
307
				}
-
 
308
			} else if (val < SMALL_NAT_SIZE) {
-
 
309
				MAKE_nat_small(r1, res);
-
 
310
			} else {
-
 
311
				COPY_unsigned(nat_small_value(n), r1);
-
 
312
				res = n;
-
 
313
			}
-
 
314
		} else {
-
 
315
			/* Overflow - create large integer */
-
 
316
			LIST(unsigned)digits = NULL_list(unsigned);
-
 
317
			if (val >= SMALL_NAT_SIZE) {
-
 
318
				unsigned ign;
-
 
319
				DESTROY_nat_small(destroy, ign, n);
-
 
320
				UNUSED(ign);
-
 
321
			}
-
 
322
			CONS_unsigned(r2, digits, digits);
-
 
323
			CONS_unsigned(r1, digits, digits);
-
 
324
			MAKE_nat_large(digits, res);
283
		}
325
		}
284
	    } else if ( val < SMALL_NAT_SIZE ) {
-
 
285
		MAKE_nat_small ( r1, res ) ;
-
 
286
	    } else {
-
 
287
		COPY_unsigned ( nat_small_value ( n ), r1 ) ;
-
 
288
		res = n ;
-
 
289
	   }
326
 
290
	} else {
327
	} else {
291
	    /* Overflow - create large integer */
328
		/* Large integers */
292
	    LIST ( unsigned ) digits = NULL_list ( unsigned ) ;
329
		LIST(unsigned)vals = DEREF_list(nat_large_values(n));
293
	    if ( val >= SMALL_NAT_SIZE ) {
330
		LIST(unsigned)v = vals;
294
		unsigned ign ;
331
		unsigned carry = d;
-
 
332
 
295
		DESTROY_nat_small ( destroy, ign, n ) ;
333
		/* Scan through digits */
296
		UNUSED ( ign ) ;
334
		while (!IS_NULL_list(v)) {
297
	    }
-
 
298
	    CONS_unsigned ( r2, digits, digits ) ;
335
			unsigned val = DEREF_unsigned(HEAD_list(v));
299
	    CONS_unsigned ( r1, digits, digits ) ;
336
			unsigned long lv = EXTEND_VALUE(val);
300
	    MAKE_nat_large ( digits, res ) ;
337
			unsigned long lc = EXTEND_VALUE(carry);
301
	}
-
 
302
 
-
 
303
    } else {
-
 
304
	/* Large integers */
338
			unsigned long lr = lv * lb + lc;
305
	LIST ( unsigned ) vals = DEREF_list ( nat_large_values ( n ) ) ;
339
			COPY_unsigned(HEAD_list(v), LO_HALF(lr));
306
	LIST ( unsigned ) v = vals ;
340
			carry = HI_HALF(lr);
307
	unsigned carry = d ;
341
			v = TAIL_list(v);
-
 
342
		}
308
 
343
 
309
	/* Scan through digits */
-
 
310
	while ( !IS_NULL_list ( v ) ) {
-
 
311
	    unsigned val = DEREF_unsigned ( HEAD_list ( v ) ) ;
-
 
312
	    unsigned long lv = EXTEND_VALUE ( val ) ;
-
 
313
	    unsigned long lc = EXTEND_VALUE ( carry ) ;
-
 
314
	    unsigned long lr = lv * lb + lc ;
-
 
315
	    COPY_unsigned ( HEAD_list ( v ), LO_HALF ( lr ) ) ;
-
 
316
	    carry = HI_HALF ( lr ) ;
-
 
317
	    v = TAIL_list ( v ) ;
-
 
318
	}
-
 
319
 
-
 
320
	if ( carry ) {
344
		if (carry) {
321
	    /* Overflow - add an extra digit */
345
			/* Overflow - add an extra digit */
322
	    CONS_unsigned ( carry, NULL_list ( unsigned ), v ) ;
346
			CONS_unsigned(carry, NULL_list(unsigned), v);
323
	    IGNORE APPEND_list ( vals, v ) ;
347
			IGNORE APPEND_list(vals, v);
-
 
348
		}
-
 
349
		res = n;
324
	}
350
	}
325
	res = n ;
-
 
326
    }
-
 
327
    return ( res ) ;
351
	return(res);
328
}
352
}
329
 
353
 
330
 
354
 
331
/*
355
/*
332
    IS AN INTEGER CONSTANT ZERO?
356
    IS AN INTEGER CONSTANT ZERO?
333
 
357
 
334
    This routine checks whether the integer constant n is zero.
358
    This routine checks whether the integer constant n is zero.
335
*/
359
*/
336
 
360
 
337
int is_zero_nat
361
int
338
    PROTO_N ( ( n ) )
-
 
339
    PROTO_T ( NAT n )
362
is_zero_nat(NAT n)
340
{
363
{
341
    unsigned val ;
364
	unsigned val;
342
    if ( !IS_nat_small ( n ) ) return ( 0 ) ;
365
	if (!IS_nat_small(n)) {
-
 
366
		return(0);
-
 
367
	}
343
    val = DEREF_unsigned ( nat_small_value ( n ) ) ;
368
	val = DEREF_unsigned(nat_small_value(n));
344
    return ( val ? 0 : 1 ) ;
369
	return(val ? 0 : 1);
345
}
370
}
346
 
371
 
347
 
372
 
348
/*
373
/*
349
    IS AN INTEGER CONSTANT NEGATIVE?
374
    IS AN INTEGER CONSTANT NEGATIVE?
350
 
375
 
351
    This routine checks whether the integer constant n is negative.
376
    This routine checks whether the integer constant n is negative.
352
*/
377
*/
353
 
378
 
354
int is_negative_nat
379
int
355
    PROTO_N ( ( n ) )
-
 
356
    PROTO_T ( NAT n )
380
is_negative_nat(NAT n)
357
{
381
{
358
    return ( IS_nat_neg ( n ) ) ;
382
	return(IS_nat_neg(n));
359
}
383
}
360
 
384
 
361
 
385
 
362
/*
386
/*
363
    IS AN INTEGER CONSTANT AN ERROR EXPRESSION?
387
    IS AN INTEGER CONSTANT AN ERROR EXPRESSION?
364
 
388
 
365
    This routine checks whether the integer constant n represents an error
389
    This routine checks whether the integer constant n represents an error
366
    expression.
390
    expression.
367
*/
391
*/
368
 
392
 
369
int is_error_nat
393
int
370
    PROTO_N ( ( n ) )
-
 
371
    PROTO_T ( NAT n )
394
is_error_nat(NAT n)
372
{
395
{
373
    if ( IS_nat_calc ( n ) ) {
396
	if (IS_nat_calc(n)) {
374
	EXP e = DEREF_exp ( nat_calc_value ( n ) ) ;
397
		EXP e = DEREF_exp(nat_calc_value(n));
375
	TYPE t = DEREF_type ( exp_type ( e ) ) ;
398
		TYPE t = DEREF_type(exp_type(e));
376
	return ( IS_type_error ( t ) ) ;
399
		return(IS_type_error(t));
377
    }
400
	}
378
    return ( 0 ) ;
401
	return(0);
379
}
402
}
380
 
403
 
381
 
404
 
382
/*
405
/*
383
    IS AN INTEGER CONSTANT A CALCULATED VALUE?
406
    IS AN INTEGER CONSTANT A CALCULATED VALUE?
384
 
407
 
385
    This routine checks whether the integer constant n is a calculated
408
    This routine checks whether the integer constant n is a calculated
386
    value.
409
    value.
387
*/
410
*/
388
 
411
 
389
int is_calc_nat
412
int
390
    PROTO_N ( ( n ) )
-
 
391
    PROTO_T ( NAT n )
413
is_calc_nat(NAT n)
392
{
414
{
393
    unsigned tag = TAG_nat ( n ) ;
415
	unsigned tag = TAG_nat(n);
394
    if ( tag == nat_neg_tag ) {
416
	if (tag == nat_neg_tag) {
395
	n = DEREF_nat ( nat_neg_arg ( n ) ) ;
417
		n = DEREF_nat(nat_neg_arg(n));
396
	tag = TAG_nat ( n ) ;
418
		tag = TAG_nat(n);
397
    }
419
	}
398
    if ( tag == nat_calc_tag || tag == nat_token_tag ) return ( 1 ) ;
420
	if (tag == nat_calc_tag || tag == nat_token_tag) {
-
 
421
		return(1);
-
 
422
	}
399
    return ( 0 ) ;
423
	return(0);
400
}
424
}
401
 
425
 
402
 
426
 
403
/*
427
/*
404
    FIND THE VALUE OF A CALCULATED CONSTANT
428
    FIND THE VALUE OF A CALCULATED CONSTANT
405
 
429
 
406
    This routine creates an integer constant expression of type t with
430
    This routine creates an integer constant expression of type t with
407
    value n.
431
    value n.
408
*/
432
*/
409
 
433
 
410
EXP calc_nat_value
434
EXP
411
    PROTO_N ( ( n, t ) )
-
 
412
    PROTO_T ( NAT n X TYPE t )
435
calc_nat_value(NAT n, TYPE t)
413
{
436
{
414
    EXP e ;
437
	EXP e;
415
    TYPE s = t ;
438
	TYPE s = t;
416
    int ch = check_nat_range ( s, n ) ;
439
	int ch = check_nat_range(s, n);
417
    if ( ch != 0 ) {
440
	if (ch != 0) {
418
	/* n doesn't fit into t */
441
		/* n doesn't fit into t */
419
	int fit = 0 ;
442
		int fit = 0;
420
	string str = NULL_string ;
443
		string str = NULL_string;
421
	s = find_literal_type ( n, BASE_OCTAL, SUFFIX_NONE, str, &fit ) ;
444
		s = find_literal_type(n, BASE_OCTAL, SUFFIX_NONE, str, &fit);
422
    }
445
	}
423
    MAKE_exp_int_lit ( s, n, exp_token_tag, e ) ;
446
	MAKE_exp_int_lit(s, n, exp_token_tag, e);
424
    if ( !EQ_type ( s, t ) ) {
447
	if (!EQ_type(s, t)) {
425
	e = make_cast_nat ( t, e, KILL_err, CAST_STATIC ) ;
448
		e = make_cast_nat(t, e, KILL_err, CAST_STATIC);
426
    }
449
	}
427
    return ( e ) ;
450
	return(e);
428
}
451
}
429
 
452
 
430
 
453
 
431
/*
454
/*
432
    SIMPLIFY AN INTEGER CONSTANT EXPRESSION
455
    SIMPLIFY AN INTEGER CONSTANT EXPRESSION
Line 434... Line 457...
434
    This routine simplifies the integer constant expression e by replacing
457
    This routine simplifies the integer constant expression e by replacing
435
    it by the value of a calculated constant.  This is avoided when this
458
    it by the value of a calculated constant.  This is avoided when this
436
    constant may be tokenised.
459
    constant may be tokenised.
437
*/
460
*/
438
 
461
 
439
static EXP calc_exp_value
462
static EXP
440
    PROTO_N ( ( e ) )
-
 
441
    PROTO_T ( EXP e )
463
calc_exp_value(EXP e)
442
{
464
{
443
    NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
465
	NAT n = DEREF_nat(exp_int_lit_nat(e));
444
    if ( IS_nat_calc ( n ) ) {
466
	if (IS_nat_calc(n)) {
445
	/* Calculated value */
467
		/* Calculated value */
446
	unsigned etag = DEREF_unsigned ( exp_int_lit_etag ( e ) ) ;
468
		unsigned etag = DEREF_unsigned(exp_int_lit_etag(e));
447
	if ( etag != exp_identifier_tag ) {
469
		if (etag != exp_identifier_tag) {
448
	    /* Preserve enumerators */
470
			/* Preserve enumerators */
449
	    e = DEREF_exp ( nat_calc_value ( n ) ) ;
471
			e = DEREF_exp(nat_calc_value(n));
-
 
472
		}
450
	}
473
	}
451
    }
-
 
452
    return ( e ) ;
474
	return(e);
453
}
475
}
454
 
476
 
455
 
477
 
456
/*
478
/*
457
    NEGATE AN INTEGER CONSTANT
479
    NEGATE AN INTEGER CONSTANT
458
 
480
 
459
    This routine negates the integer constant n.
481
    This routine negates the integer constant n.
460
*/
482
*/
461
 
483
 
462
NAT negate_nat
484
NAT
463
    PROTO_N ( ( n ) )
-
 
464
    PROTO_T ( NAT n )
485
negate_nat(NAT n)
465
{
486
{
466
    if ( !IS_NULL_nat ( n ) ) {
487
	if (!IS_NULL_nat(n)) {
467
	switch ( TAG_nat ( n ) ) {
488
		switch (TAG_nat(n)) {
468
	    case nat_small_tag : {
489
		case nat_small_tag: {
469
		unsigned val = DEREF_unsigned ( nat_small_value ( n ) ) ;
490
			unsigned val = DEREF_unsigned(nat_small_value(n));
470
		if ( val < SMALL_NAT_SIZE ) {
491
			if (val < SMALL_NAT_SIZE) {
471
		    n = small_neg_nat [ val ] ;
492
				n = small_neg_nat[val];
472
		    if ( IS_NULL_nat ( n ) ) {
493
				if (IS_NULL_nat(n)) {
473
			int v = ( int ) val ;
494
					int v = (int)val;
474
			n = make_small_nat ( -v ) ;
495
					n = make_small_nat(-v);
475
		    }
496
				}
476
		    break ;
497
				break;
-
 
498
			}
-
 
499
			goto default_lab;
-
 
500
		}
-
 
501
		case nat_neg_tag: {
-
 
502
			n = DEREF_nat(nat_neg_arg(n));
-
 
503
			break;
-
 
504
		}
-
 
505
		case nat_calc_tag: {
-
 
506
			EXP e = DEREF_exp(nat_calc_value(n));
-
 
507
			e = make_uminus_exp(lex_minus, e);
-
 
508
			MAKE_nat_calc(e, n);
-
 
509
			break;
-
 
510
		}
-
 
511
		default:
-
 
512
default_lab:
-
 
513
			MAKE_nat_neg(n, n);
-
 
514
			break;
477
		}
515
		}
478
		goto default_lab ;
-
 
479
	    }
-
 
480
	    case nat_neg_tag : {
-
 
481
		n = DEREF_nat ( nat_neg_arg ( n ) ) ;
-
 
482
		break ;
-
 
483
	    }
-
 
484
	    case nat_calc_tag : {
-
 
485
		EXP e = DEREF_exp ( nat_calc_value ( n ) ) ;
-
 
486
		e = make_uminus_exp ( lex_minus, e ) ;
-
 
487
		MAKE_nat_calc ( e, n ) ;
-
 
488
		break ;
-
 
489
	    }
-
 
490
	    default :
-
 
491
	    default_lab : {
-
 
492
		MAKE_nat_neg ( n, n ) ;
-
 
493
		break ;
-
 
494
	    }
-
 
495
	}
516
	}
496
    }
-
 
497
    return ( n ) ;
517
	return(n);
498
}
518
}
499
 
519
 
500
 
520
 
501
/*
521
/*
502
    COMPARE TWO INTEGER CONSTANTS
522
    COMPARE TWO INTEGER CONSTANTS
503
 
523
 
504
    This routine compares the integer constants n and m.  It returns 0 if
524
    This routine compares the integer constants n and m.  It returns 0 if
505
    they are equal, 1 if n > m and -1 if n < m.  A value of 2 or -2 is
525
    they are equal, 1 if n > m and -1 if n < m.  A value of 2 or -2 is
506
    returned if the result is target dependent or otherwise indeterminate.
526
    returned if the result is target dependent or otherwise indeterminate.
615
    UNIFY TWO INTEGER LITERALS
646
    UNIFY TWO INTEGER LITERALS
616
 
647
 
617
    This routine unifies the integer literals n and m by defining tokens
648
    This routine unifies the integer literals n and m by defining tokens
618
    if possible.  It returns true if the token is assigned a value.
649
    if possible.  It returns true if the token is assigned a value.
619
*/
650
*/
620
 
651
 
621
static int unify_nat
652
static int
622
    PROTO_N ( ( n, m ) )
-
 
623
    PROTO_T ( NAT n X NAT m )
653
unify_nat(NAT n, NAT m)
624
{
654
{
625
    IDENTIFIER id ;
655
	IDENTIFIER id;
626
    LIST ( TOKEN ) args ;
656
	LIST(TOKEN)args;
627
    switch ( TAG_nat ( n ) ) {
657
	switch (TAG_nat(n)) {
628
	case nat_token_tag : {
658
	case nat_token_tag: {
629
	    id = DEREF_id ( nat_token_tok ( n ) ) ;
659
		id = DEREF_id(nat_token_tok(n));
630
	    args = DEREF_list ( nat_token_args ( n ) ) ;
660
		args = DEREF_list(nat_token_args(n));
631
	    break ;
661
		break;
632
	}
662
	}
633
	case nat_calc_tag : {
663
	case nat_calc_tag: {
634
	    EXP e = DEREF_exp ( nat_calc_value ( n ) ) ;
664
		EXP e = DEREF_exp(nat_calc_value(n));
635
	    if ( !IS_exp_token ( e ) ) return ( 0 ) ;
665
		if (!IS_exp_token(e)) {
-
 
666
			return(0);
-
 
667
		}
636
	    id = DEREF_id ( exp_token_tok ( e ) ) ;
668
		id = DEREF_id(exp_token_tok(e));
637
	    args = DEREF_list ( exp_token_args ( e ) ) ;
669
		args = DEREF_list(exp_token_args(e));
638
	    break ;
670
		break;
639
	}
671
	}
640
	default : {
672
	default: {
641
	    return ( 0 ) ;
673
		return(0);
-
 
674
	}
-
 
675
	}
-
 
676
	if (IS_NULL_list(args) && defining_token(id)) {
-
 
677
		return(define_nat_token(id, m));
642
	}
678
	}
643
    }
-
 
644
    if ( IS_NULL_list ( args ) && defining_token ( id ) ) {
-
 
645
	return ( define_nat_token ( id, m ) ) ;
-
 
646
    }
-
 
647
    return ( 0 ) ;
679
	return(0);
648
}
680
}
649
 
681
 
650
 
682
 
651
/*
683
/*
652
    ARE TWO INTEGER LITERALS EQUAL?
684
    ARE TWO INTEGER LITERALS EQUAL?
653
 
685
 
654
    This routine returns true if the literals n and m are equal.
686
    This routine returns true if the literals n and m are equal.
655
*/
687
*/
656
 
688
 
657
int eq_nat
689
int
658
    PROTO_N ( ( n, m ) )
-
 
659
    PROTO_T ( NAT n X NAT m )
690
eq_nat(NAT n, NAT m)
660
{
691
{
661
    if ( EQ_nat ( n, m ) ) return ( 1 ) ;
692
	if (EQ_nat(n, m)) {
-
 
693
		return(1);
-
 
694
	}
662
    if ( IS_NULL_nat ( n ) || IS_NULL_nat ( m ) ) return ( 0 ) ;
695
	if (IS_NULL_nat(n) || IS_NULL_nat(m)) {
-
 
696
		return(0);
-
 
697
	}
663
    if ( compare_nat ( n, m ) == 0 ) return ( 1 ) ;
698
	if (compare_nat(n, m) == 0) {
-
 
699
		return(1);
-
 
700
	}
664
    if ( force_tokdef || force_template || expand_tokdef ) {
701
	if (force_tokdef || force_template || expand_tokdef) {
665
	if ( unify_nat ( n, m ) ) return ( 1 ) ;
702
		if (unify_nat(n, m)) {
-
 
703
			return(1);
-
 
704
		}
666
	if ( unify_nat ( m, n ) ) return ( 1 ) ;
705
		if (unify_nat(m, n)) {
-
 
706
			return(1);
667
    }
707
		}
-
 
708
	}
668
    return ( 0 ) ;
709
	return(0);
669
}
710
}
670
 
711
 
671
 
712
 
672
/*
713
/*
673
    PERFORM A BINARY INTEGER CONSTANT CALCULATION
714
    PERFORM A BINARY INTEGER CONSTANT CALCULATION
674
 
715
 
675
    This routine is used to evaluate the binary operation indicated by tag
716
    This routine is used to evaluate the binary operation indicated by tag
676
    on the integer constants a and b, which will be simple literals.  The
717
    on the integer constants a and b, which will be simple literals.  The
677
    permitted operations are '+', '-', '*', '/', '%', '<<', '>>', '&', '|',
718
    permitted operations are '+', '-', '*', '/', '%', '<<', '>>', '&', '|',
678
    and '^'.  The null literal is returned for undefined or implementation
719
    and '^'.  The null literal is returned for undefined or implementation
679
    dependent calculations.
720
    dependent calculations.
1150
 
1210
 
1151
 
1211
 
1152
/*
1212
/*
1153
    EVALUATE A CONSTANT EXPRESSION
1213
    EVALUATE A CONSTANT EXPRESSION
1154
 
1214
 
1155
    This routine transforms the integer constant expression e into an
1215
    This routine transforms the integer constant expression e into an
1156
    integer constant.  Any errors arising are added to the position
1216
    integer constant.  Any errors arising are added to the position
1157
    indicated by err.
1217
    indicated by err.
1158
*/
1218
*/
1159
 
1219
 
1160
NAT make_nat_exp
1220
NAT
1161
    PROTO_N ( ( e, err ) )
-
 
1162
    PROTO_T ( EXP e X ERROR *err )
1221
make_nat_exp(EXP e, ERROR *err)
1163
{
1222
{
1164
    NAT n ;
1223
	NAT n;
1165
    TYPE t ;
1224
	TYPE t;
1166
 
1225
 
1167
    /* Remove any parentheses round e */
1226
	/* Remove any parentheses round e */
1168
    unsigned tag = TAG_exp ( e ) ;
1227
	unsigned tag = TAG_exp(e);
1169
    while ( tag == exp_paren_tag ) {
1228
	while (tag == exp_paren_tag) {
1170
	e = DEREF_exp ( exp_paren_arg ( e ) ) ;
1229
		e = DEREF_exp(exp_paren_arg(e));
1171
	tag = TAG_exp ( e ) ;
1230
		tag = TAG_exp(e);
1172
    }
1231
	}
1173
 
1232
 
1174
    /* The result should now be an integer constant */
1233
	/* The result should now be an integer constant */
1175
    if ( tag == exp_int_lit_tag ) {
1234
	if (tag == exp_int_lit_tag) {
1176
	n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
1235
		n = DEREF_nat(exp_int_lit_nat(e));
1177
	return ( n ) ;
1236
		return(n);
1178
    }
1237
	}
1179
 
1238
 
1180
    /* Check expression type */
1239
	/* Check expression type */
1181
    t = DEREF_type ( exp_type ( e ) ) ;
1240
	t = DEREF_type(exp_type(e));
1182
    switch ( TAG_type ( t ) ) {
1241
	switch (TAG_type(t)) {
1183
	case type_integer_tag :
1242
	case type_integer_tag:
1184
	case type_enumerate_tag :
1243
	case type_enumerate_tag:
1185
	case type_bitfield_tag : {
1244
	case type_bitfield_tag: {
1186
	    /* Double check for integer constants */
1245
		/* Double check for integer constants */
1187
	    if ( !is_const_exp ( e, 0 ) ) {
1246
		if (!is_const_exp(e, 0)) {
1188
		add_error ( err, ERR_expr_const_bad () ) ;
1247
			add_error(err, ERR_expr_const_bad());
1189
	    }
1248
		}
1190
	    break ;
1249
		break;
1191
	}
1250
	}
1192
	case type_token_tag : {
1251
	case type_token_tag: {
1193
	    /* Allow template types */
1252
		/* Allow template types */
1194
	    if ( !is_templ_type ( t ) ) goto default_lab ;
1253
		if (!is_templ_type(t)) {
-
 
1254
			goto default_lab;
-
 
1255
		}
1195
	    break ;
1256
		break;
1196
	}
1257
	}
1197
	case type_error_tag : {
1258
	case type_error_tag: {
1198
	    /* Allow for error propagation */
1259
		/* Allow for error propagation */
1199
	    break ;
1260
		break;
1200
	}
1261
	}
1201
	default :
1262
	default :
1202
	default_lab : {
1263
default_lab:
1203
	    /* Otherwise report an error */
1264
		/* Otherwise report an error */
1204
	    add_error ( err, ERR_expr_const_int ( t ) ) ;
1265
		add_error(err, ERR_expr_const_int(t));
1205
	    if ( IS_exp_float_lit ( e ) ) {
1266
		if (IS_exp_float_lit(e)) {
1206
		/* Evaluate floating point literals */
1267
			/* Evaluate floating point literals */
1207
		FLOAT f = DEREF_flt ( exp_float_lit_flt ( e ) ) ;
1268
			FLOAT f = DEREF_flt(exp_float_lit_flt(e));
1208
		n = round_float_lit ( f, crt_round_mode ) ;
1269
			n = round_float_lit(f, crt_round_mode);
1209
		if ( !IS_NULL_nat ( n ) ) return ( n ) ;
1270
			if (!IS_NULL_nat(n)) {
-
 
1271
				return(n);
1210
	    }
1272
			}
-
 
1273
		}
1211
	    e = make_error_exp ( 0 ) ;
1274
		e = make_error_exp(0);
1212
	    break ;
1275
		break;
1213
	}
1276
	}
1214
    }
-
 
1215
    MAKE_nat_calc ( e, n ) ;
1277
	MAKE_nat_calc(e, n);
1216
    return ( n ) ;
1278
	return(n);
1217
}
1279
}
1218
 
1280
 
1219
 
1281
 
1220
/*
1282
/*
1221
    FIND THE NUMBER OF BITS IN AN INTEGER
1283
    FIND THE NUMBER OF BITS IN AN INTEGER
1222
 
1284
 
1223
    This routine returns the number of bits in the integer n from the
1285
    This routine returns the number of bits in the integer n from the
1224
    range [0,0xffff].
1286
    range [0, 0xffff].
1225
*/
1287
*/
1226
 
1288
 
1227
unsigned no_bits
1289
unsigned
1228
    PROTO_N ( ( n ) )
-
 
1229
    PROTO_T ( unsigned n )
1290
no_bits(unsigned n)
1230
{
1291
{
1231
    unsigned bits = 0 ;
1292
	unsigned bits = 0;
1232
    static unsigned char small_bits [16] = {
1293
	static unsigned char small_bits[16] = {
1233
	0, 1, 2, 2, 3, 3, 3, 3,
1294
		0, 1, 2, 2, 3, 3, 3, 3,
1234
	4, 4, 4, 4, 4, 4, 4, 4
1295
		4, 4, 4, 4, 4, 4, 4, 4
1235
    } ;
1296
	};
1236
    if ( n & ( ( unsigned ) 0xfff0 ) ) {
1297
	if (n & ((unsigned)0xfff0)) {
1237
	n >>= 4 ;
1298
		n >>= 4;
1238
	bits += 4 ;
1299
		bits += 4;
1239
	if ( n & 0x0ff0 ) {
1300
		if (n & 0x0ff0) {
1240
	    n >>= 4 ;
1301
			n >>= 4;
1241
	    bits += 4 ;
1302
			bits += 4;
1242
	    if ( n & 0x00f0 ) {
1303
			if (n & 0x00f0) {
1243
		n >>= 4 ;
1304
				n >>= 4;
1244
		bits += 4 ;
1305
				bits += 4;
1245
	    }
1306
			}
1246
	}
1307
		}
1247
    }
1308
	}
1248
    bits += ( unsigned ) small_bits [n] ;
1309
	bits += (unsigned)small_bits[n];
1249
    return ( bits ) ;
1310
	return(bits);
1250
}
1311
}
1251
 
1312
 
1252
 
1313
 
1253
/*
1314
/*
1254
    FIND THE NUMBER OF BITS IN AN INTEGER CONSTANT
1315
    FIND THE NUMBER OF BITS IN AN INTEGER CONSTANT
1255
 
1316
 
1256
    This routine calculates the number of bits in the representation of
1317
    This routine calculates the number of bits in the representation of
1257
    the simple integer constant n.  The flag eq is set to false unless
1318
    the simple integer constant n.  The flag eq is set to false unless
1258
    n is exactly a power of 2.
1319
    n is exactly a power of 2.
1259
*/
1320
*/
1260
 
1321
 
1261
static unsigned get_nat_bits
1322
static unsigned
1262
    PROTO_N ( ( n, eq ) )
-
 
1263
    PROTO_T ( NAT n X int *eq )
1323
get_nat_bits(NAT n, int *eq)
1264
{
1324
{
1265
    unsigned val ;
1325
	unsigned val;
1266
    unsigned bits = 0 ;
1326
	unsigned bits = 0;
1267
    if ( IS_nat_small ( n ) ) {
1327
	if (IS_nat_small(n)) {
1268
	val = DEREF_unsigned ( nat_small_value ( n ) ) ;
1328
		val = DEREF_unsigned(nat_small_value(n));
1269
    } else {
1329
	} else {
1270
	LIST ( unsigned ) vals = DEREF_list ( nat_large_values ( n ) ) ;
1330
		LIST(unsigned)vals = DEREF_list(nat_large_values(n));
1271
	for ( ; ; ) {
1331
		for (;;) {
1272
	    val = DEREF_unsigned ( HEAD_list ( vals ) ) ;
1332
			val = DEREF_unsigned(HEAD_list(vals));
1273
	    vals = TAIL_list ( vals ) ;
1333
			vals = TAIL_list(vals);
1274
	    if ( IS_NULL_list ( vals ) ) break ;
1334
			if (IS_NULL_list(vals))break;
1275
	    if ( val ) *eq = 0 ;
1335
			if (val)*eq = 0;
1276
	    bits += NAT_DIGITS ;
1336
			bits += NAT_DIGITS;
-
 
1337
		}
-
 
1338
	}
-
 
1339
	if (val) {
-
 
1340
		/* Check the most significant digit */
-
 
1341
		if (val & (val - 1))*eq = 0;
-
 
1342
		bits += no_bits(val);
1277
	}
1343
	}
1278
    }
-
 
1279
    if ( val ) {
-
 
1280
	/* Check the most significant digit */
-
 
1281
	if ( val & ( val - 1 ) ) *eq = 0 ;
-
 
1282
	bits += no_bits ( val ) ;
-
 
1283
    }
-
 
1284
    return ( bits ) ;
1344
	return(bits);
1285
}
1345
}
1286
 
1346
 
1287
 
1347
 
1288
/*
1348
/*
1289
    CHECK WHETHER AN INTEGER CONSTANT FITS INTO A TYPE
1349
    CHECK WHETHER AN INTEGER CONSTANT FITS INTO A TYPE
Line 1297... Line 1357...
1297
	2 if n may fit into t and t is unsigned,
1357
	2 if n may fit into t and t is unsigned,
1298
	3 if n definitely does not fit into t and t is not unsigned,
1358
	3 if n definitely does not fit into t and t is not unsigned,
1299
	4 if n definitely does not fit into t and t is unsigned,
1359
	4 if n definitely does not fit into t and t is unsigned,
1300
	5 if n definitely does not fit into any type and t is not unsigned,
1360
	5 if n definitely does not fit into any type and t is not unsigned,
1301
	6 if n definitely does not fit into any type and t is unsigned.
1361
	6 if n definitely does not fit into any type and t is unsigned.
1302
*/
1362
*/
1303
 
1363
 
1304
int check_nat_range
1364
int
1305
    PROTO_N ( ( t, n ) )
-
 
1306
    PROTO_T ( TYPE t X NAT n )
1365
check_nat_range(TYPE t, NAT n)
1307
{
1366
{
1308
    int eq = 1 ;
1367
	int eq = 1;
1309
    int neg = 0 ;
1368
	int neg = 0;
1310
    unsigned msz ;
1369
	unsigned msz;
1311
    unsigned bits ;
1370
	unsigned bits;
1312
    BASE_TYPE sign ;
1371
	BASE_TYPE sign;
1313
 
-
 
1314
    /* Find type information */
-
 
1315
    unsigned sz = find_type_size ( t, &msz, &sign ) ;
-
 
1316
    int u = ( sign == btype_unsigned ? 1 : 0 ) ;
-
 
1317
 
1372
 
-
 
1373
	/* Find type information */
-
 
1374
	unsigned sz = find_type_size(t, &msz, &sign);
-
 
1375
	int u = (sign == btype_unsigned ? 1 : 0);
-
 
1376
 
1318
    /* Deal with complex constants */
1377
	/* Deal with complex constants */
1319
    unsigned tag = TAG_nat ( n ) ;
1378
	unsigned tag = TAG_nat(n);
1320
    if ( tag == nat_neg_tag ) {
1379
	if (tag == nat_neg_tag) {
1321
	n = DEREF_nat ( nat_neg_arg ( n ) ) ;
1380
		n = DEREF_nat(nat_neg_arg(n));
1322
	tag = TAG_nat ( n ) ;
1381
		tag = TAG_nat(n);
1323
	neg = 1 ;
1382
		neg = 1;
1324
    }
1383
	}
1325
    if ( tag == nat_calc_tag || tag == nat_token_tag ) {
1384
	if (tag == nat_calc_tag || tag == nat_token_tag) {
1326
	return ( 1 + u ) ;
1385
		return(1 + u);
-
 
1386
	}
-
 
1387
 
-
 
1388
	/* Find the number of bits in the representation of n */
-
 
1389
	bits = get_nat_bits(n, &eq);
-
 
1390
	if (bits > basetype_info[ntype_ellipsis].max_bits) {
-
 
1391
		return(5 + u);
1327
    }
1392
	}
1328
 
1393
 
1329
    /* Find the number of bits in the representation of n */
-
 
1330
    bits = get_nat_bits ( n, &eq ) ;
-
 
1331
    if ( bits > basetype_info [ ntype_ellipsis ].max_bits ) {
-
 
1332
	return ( 5 + u ) ;
-
 
1333
    }
-
 
1334
 
-
 
1335
    /* Check the type range */
1394
	/* Check the type range */
1336
    if ( sign == btype_unsigned ) {
1395
	if (sign == btype_unsigned) {
1337
	/* Unsigned types (eg [0-255]) */
1396
		/* Unsigned types (eg [0-255]) */
-
 
1397
		if (neg) {
1338
	if ( neg ) return ( 4 ) ;
1398
			return(4);
-
 
1399
		}
1339
	if ( bits <= sz ) return ( 0 ) ;
1400
		if (bits <= sz) {
-
 
1401
			return(0);
-
 
1402
		}
1340
	if ( bits > msz ) return ( 4 ) ;
1403
		if (bits > msz) {
-
 
1404
			return(4);
-
 
1405
		}
1341
    } else if ( sign == btype_signed ) {
1406
	} else if (sign == btype_signed) {
1342
	/* Symmetric signed types (eg [-127,127]) */
1407
		/* Symmetric signed types (eg [-127, 127]) */
1343
	if ( bits < sz ) return ( 0 ) ;
1408
		if (bits < sz) {
-
 
1409
			return(0);
-
 
1410
		}
1344
	if ( bits >= msz ) return ( 3 ) ;
1411
		if (bits >= msz) {
-
 
1412
			return(3);
-
 
1413
		}
1345
    } else if ( sign == ( btype_signed | btype_long ) ) {
1414
	} else if (sign == (btype_signed | btype_long)) {
1346
	/* Asymmetric signed types (eg [-128,127]) */
1415
		/* Asymmetric signed types (eg [-128, 127]) */
1347
	if ( bits < sz ) return ( 0 ) ;
1416
		if (bits < sz) {
-
 
1417
			return(0);
-
 
1418
		}
1348
	if ( bits == sz && neg && eq ) return ( 0 ) ;
1419
		if (bits == sz && neg && eq) {
-
 
1420
			return(0);
-
 
1421
		}
1349
	if ( bits >= msz ) return ( 3 ) ;
1422
		if (bits >= msz) {
-
 
1423
			return(3);
-
 
1424
		}
1350
    } else {
1425
	} else {
1351
	/* Unspecified types */
1426
		/* Unspecified types */
-
 
1427
		if (neg) {
1352
	if ( neg ) return ( 3 ) ;
1428
			return(3);
-
 
1429
		}
1353
	if ( bits < sz ) return ( 0 ) ;
1430
		if (bits < sz) {
-
 
1431
			return(0);
-
 
1432
		}
1354
	if ( bits >= msz ) return ( 3 ) ;
1433
		if (bits >= msz) {
-
 
1434
			return(3);
1355
    }
1435
		}
-
 
1436
	}
1356
    return ( 1 + u ) ;
1437
	return(1 + u);
1357
}
1438
}
1358
 
1439
 
1359
 
1440
 
1360
/*
1441
/*
1361
    CHECK A TYPE SIZE
1442
    CHECK A TYPE SIZE
Line 1365... Line 1446...
1365
    for example, in checking for overlarge shifts and bitfield sizes.
1446
    for example, in checking for overlarge shifts and bitfield sizes.
1366
    It returns -1 if n is less than the minimum number of bits, 0 if it
1447
    It returns -1 if n is less than the minimum number of bits, 0 if it
1367
    is equal, and 1 otherwise.
1448
    is equal, and 1 otherwise.
1368
*/
1449
*/
1369
 
1450
 
1370
int check_type_size
1451
int
1371
    PROTO_N ( ( t, n ) )
-
 
1372
    PROTO_T ( TYPE t X NAT n )
1452
check_type_size(TYPE t, NAT n)
1373
{
1453
{
1374
    unsigned sz ;
1454
	unsigned sz;
1375
    unsigned msz ;
1455
	unsigned msz;
1376
    BASE_TYPE sign ;
1456
	BASE_TYPE sign;
1377
    unsigned long st, sn ;
1457
	unsigned long st, sn;
1378
    switch ( TAG_nat ( n ) ) {
1458
	switch (TAG_nat(n)) {
1379
	case nat_neg_tag :
1459
	case nat_neg_tag:
1380
	case nat_calc_tag :
1460
	case nat_calc_tag:
1381
	case nat_token_tag : {
1461
	case nat_token_tag:
1382
	    /* Negative and calculated values are alright */
1462
		/* Negative and calculated values are alright */
-
 
1463
		return(-1);
-
 
1464
	}
-
 
1465
	sn = get_nat_value(n);
-
 
1466
	if (sn == EXTENDED_MAX) {
-
 
1467
		return(1);
-
 
1468
	}
-
 
1469
	sz = find_type_size(t, &msz, &sign);
-
 
1470
	UNUSED(sign);
-
 
1471
	UNUSED(msz);
-
 
1472
	st = EXTEND_VALUE(sz);
-
 
1473
	if (sn < st) {
1383
	    return ( -1 ) ;
1474
		return(-1);
-
 
1475
	}
-
 
1476
	if (sn == st) {
-
 
1477
		return(0);
1384
	}
1478
	}
1385
    }
-
 
1386
    sn = get_nat_value ( n ) ;
-
 
1387
    if ( sn == EXTENDED_MAX ) return ( 1 ) ;
-
 
1388
    sz = find_type_size ( t, &msz, &sign ) ;
-
 
1389
    UNUSED ( sign ) ;
-
 
1390
    UNUSED ( msz ) ;
-
 
1391
    st = EXTEND_VALUE ( sz ) ;
-
 
1392
    if ( sn < st ) return ( -1 ) ;
-
 
1393
    if ( sn == st ) return ( 0 ) ;
-
 
1394
    return ( 1 ) ;
1479
	return(1);
1395
}
1480
}
1396
 
1481
 
1397
 
1482
 
1398
/*
1483
/*
1399
    FIND THE MAXIMUM VALUE FOR A TYPE
1484
    FIND THE MAXIMUM VALUE FOR A TYPE
Line 1402... Line 1487...
1402
    true) which is guaranteed to fit into the type t.  The null constant
1487
    true) which is guaranteed to fit into the type t.  The null constant
1403
    is returned if the value can't be determined.  If t is the null type
1488
    is returned if the value can't be determined.  If t is the null type
1404
    the maximum value which can fit into any type is returned.
1489
    the maximum value which can fit into any type is returned.
1405
*/
1490
*/
1406
 
1491
 
1407
NAT max_type_value
1492
NAT
1408
    PROTO_N ( ( t, neg ) )
-
 
1409
    PROTO_T ( TYPE t X int neg )
1493
max_type_value(TYPE t, int neg)
1410
{
1494
{
1411
    NAT n ;
1495
	NAT n;
1412
    unsigned sz ;
1496
	unsigned sz;
1413
    unsigned msz ;
1497
	unsigned msz;
1414
    int zero = 0 ;
1498
	int zero = 0;
1415
    BASE_TYPE sign ;
1499
	BASE_TYPE sign;
1416
    if ( !IS_NULL_type ( t ) ) {
1500
	if (!IS_NULL_type(t)) {
1417
	sz = find_type_size ( t, &msz, &sign ) ;
1501
		sz = find_type_size(t, &msz, &sign);
1418
    } else {
1502
	} else {
1419
	sz = basetype_info [ ntype_ellipsis ].max_bits ;
1503
		sz = basetype_info[ntype_ellipsis].max_bits;
1420
	sign = btype_unsigned ;
1504
		sign = btype_unsigned;
1421
    }
1505
	}
1422
    if ( !( sign & btype_signed ) ) {
1506
	if (!(sign & btype_signed)) {
1423
	zero = neg ;
1507
		zero = neg;
1424
    }
1508
	}
1425
    if ( !( sign & btype_unsigned ) ) {
1509
	if (!(sign & btype_unsigned)) {
1426
	if ( sz == 0 ) zero = 1 ;
1510
		if (sz == 0) {
-
 
1511
			zero = 1;
-
 
1512
		}
1427
	sz-- ;
1513
		sz--;
1428
    }
1514
	}
1429
    if ( zero ) {
1515
	if (zero) {
1430
	n = small_nat [0] ;
1516
		n = small_nat[0];
1431
    } else {
1517
	} else {
1432
	n = make_nat_value ( ( unsigned long ) sz ) ;
1518
		n = make_nat_value((unsigned long)sz);
1433
	n = binary_nat_op ( exp_lshift_tag, small_nat [1], n ) ;
1519
		n = binary_nat_op(exp_lshift_tag, small_nat[1], n);
1434
	if ( !IS_NULL_nat ( n ) ) {
1520
		if (!IS_NULL_nat(n)) {
1435
	    if ( !neg || !( sign & btype_long ) ) {
1521
			if (!neg || !(sign & btype_long)) {
1436
		n = binary_nat_op ( exp_minus_tag, n, small_nat [1] ) ;
1522
				n = binary_nat_op(exp_minus_tag, n,
-
 
1523
						  small_nat[1]);
1437
	    }
1524
			}
1438
	    if ( neg ) n = negate_nat ( n ) ;
1525
			if (neg)n = negate_nat(n);
-
 
1526
		}
1439
	}
1527
	}
1440
    }
-
 
1441
    return ( n ) ;
1528
	return(n);
1442
}
1529
}
1443
 
1530
 
1444
 
1531
 
1445
 
1532
 
1446
 
1533
 
Line 1451... Line 1538...
1451
    the literal n, performing any appropriate bounds checks.  tag indicates
1538
    the literal n, performing any appropriate bounds checks.  tag indicates
1452
    the operation used to form this result.  The null expression is returned
1539
    the operation used to form this result.  The null expression is returned
1453
    to indicate that n may not fit into t.
1540
    to indicate that n may not fit into t.
1454
*/
1541
*/
1455
 
1542
 
1456
EXP make_int_exp
1543
EXP
1457
    PROTO_N ( ( t, tag, n ) )
-
 
1458
    PROTO_T ( TYPE t X unsigned tag X NAT n )
1544
make_int_exp(TYPE t, unsigned tag, NAT n)
1459
{
1545
{
1460
    EXP e ;
1546
	EXP e;
1461
    int ch = check_nat_range ( t, n ) ;
1547
	int ch = check_nat_range(t, n);
1462
    if ( ch == 0 ) {
1548
	if (ch == 0) {
1463
	MAKE_exp_int_lit ( t, n, tag, e ) ;
1549
		MAKE_exp_int_lit(t, n, tag, e);
1464
    } else {
1550
	} else {
1465
	e = NULL_exp ;
1551
		e = NULL_exp;
1466
    }
1552
	}
1467
    return ( e ) ;
1553
	return(e);
1468
}
1554
}
1469
 
1555
 
1470
 
1556
 
1471
/*
1557
/*
1472
    CHECK ARRAY BOUNDS
1558
    CHECK ARRAY BOUNDS
Line 1474... Line 1560...
1474
    This routine checks an array index operation indicated by op (which
1560
    This routine checks an array index operation indicated by op (which
1475
    can be '[]', '+' or '-') for the array type t and the constant integer
1561
    can be '[]', '+' or '-') for the array type t and the constant integer
1476
    index expression a.  Note that a must be less than the array bound for
1562
    index expression a.  Note that a must be less than the array bound for
1477
    '[]', but may be equal to the bound for the other operations (this is
1563
    '[]', but may be equal to the bound for the other operations (this is
1478
    the 'one past the end' rule).
1564
    the 'one past the end' rule).
1479
*/
1565
*/
1480
 
1566
 
1481
void check_bounds
1567
void
1482
    PROTO_N ( ( op, t, a ) )
-
 
1483
    PROTO_T ( int op X TYPE t X EXP a )
1568
check_bounds(int op, TYPE t, EXP a)
1484
{
1569
{
1485
    if ( IS_exp_int_lit ( a ) ) {
1570
	if (IS_exp_int_lit(a)) {
1486
	int ok = 0 ;
1571
		int ok = 0;
1487
	NAT n = DEREF_nat ( type_array_size ( t ) ) ;
1572
		NAT n = DEREF_nat(type_array_size(t));
1488
	NAT m = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1573
		NAT m = DEREF_nat(exp_int_lit_nat(a));
1489
 
1574
 
1490
	/* Unbound arrays do not give an error */
1575
		/* Unbound arrays do not give an error */
1491
	if ( IS_NULL_nat ( n ) ) return ;
1576
		if (IS_NULL_nat(n)) return;
1492
 
1577
 
1493
	/* Calculated indexes are alright */
1578
		/* Calculated indexes are alright */
1494
	if ( is_calc_nat ( m ) ) return ;
1579
		if (is_calc_nat(m)) return;
-
 
1580
 
-
 
1581
		/* Check the bounds */
-
 
1582
		if (op == lex_minus) {
-
 
1583
			m = negate_nat(m);
-
 
1584
		}
-
 
1585
		if (!IS_nat_neg(m)) {
-
 
1586
			if (!is_calc_nat(n)) {
-
 
1587
				int c = compare_nat(m, n);
-
 
1588
				if (c < 0) {
-
 
1589
					ok = 1;
-
 
1590
				}
-
 
1591
				if (c == 0 && op != lex_array_Hop) {
-
 
1592
					ok = 1;
-
 
1593
				}
-
 
1594
			}
-
 
1595
		}
1495
 
1596
 
1496
	/* Check the bounds */
1597
		/* Report the error */
1497
	if ( op == lex_minus ) m = negate_nat ( m ) ;
-
 
1498
	if ( !IS_nat_neg ( m ) ) {
-
 
1499
	    if ( !is_calc_nat ( n ) ) {
-
 
1500
		int c = compare_nat ( m, n ) ;
-
 
1501
		if ( c < 0 ) ok = 1 ;
1598
		if (!ok) {
1502
		if ( c == 0 && op != lex_array_Hop ) ok = 1 ;
1599
			report(crt_loc, ERR_expr_add_array(m, t, op));
1503
	    }
1600
		}
1504
	}
1601
	}
1505
 
-
 
1506
	/* Report the error */
-
 
1507
	if ( !ok ) report ( crt_loc, ERR_expr_add_array ( m, t, op ) ) ;
-
 
1508
    }
-
 
1509
    return ;
1602
	return;
1510
}
1603
}
1511
 
1604
 
1512
 
1605
 
1513
/*
1606
/*
1514
    EVALUATE A CONSTANT CAST OPERATION
1607
    EVALUATE A CONSTANT CAST OPERATION
Line 1517... Line 1610...
1517
    integral, bitfield, or enumeration type t.  The argument cast indicated
1610
    integral, bitfield, or enumeration type t.  The argument cast indicated
1518
    whether the cast used is implicit or explicit (see cast.h).  Any errors
1611
    whether the cast used is implicit or explicit (see cast.h).  Any errors
1519
    arising are added to err.
1612
    arising are added to err.
1520
*/
1613
*/
1521
 
1614
 
1522
EXP make_cast_nat
1615
EXP
1523
    PROTO_N ( ( t, a, err, cast ) )
-
 
1524
    PROTO_T ( TYPE t X EXP a X ERROR *err X unsigned cast )
1616
make_cast_nat(TYPE t, EXP a, ERROR *err, unsigned cast)
1525
{
1617
{
1526
    EXP e ;
1618
	EXP e;
1527
    int ch ;
1619
	int ch;
1528
    unsigned etag = exp_cast_tag ;
1620
	unsigned etag = exp_cast_tag;
1529
    NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1621
	NAT n = DEREF_nat(exp_int_lit_nat(a));
1530
    if ( cast == CAST_IMPLICIT ) {
1622
	if (cast == CAST_IMPLICIT) {
1531
	etag = DEREF_unsigned ( exp_int_lit_etag ( a ) ) ;
1623
		etag = DEREF_unsigned(exp_int_lit_etag(a));
1532
    }
1624
	}
1533
    ch = check_nat_range ( t, n ) ;
1625
	ch = check_nat_range(t, n);
1534
    if ( ch != 0 ) {
1626
	if (ch != 0) {
1535
	/* n may not fit into t */
1627
		/* n may not fit into t */
1536
	a = calc_exp_value ( a ) ;
1628
		a = calc_exp_value(a);
1537
	MAKE_exp_cast ( t, CONV_INT_INT, a, e ) ;
1629
		MAKE_exp_cast(t, CONV_INT_INT, a, e);
1538
	MAKE_nat_calc ( e, n ) ;
1630
		MAKE_nat_calc(e, n);
1539
    }
1631
	}
1540
    MAKE_exp_int_lit ( t, n, etag, e ) ;
1632
	MAKE_exp_int_lit(t, n, etag, e);
1541
    UNUSED ( err ) ;
1633
	UNUSED(err);
1542
    return ( e ) ;
1634
	return(e);
1543
}
1635
}
1544
 
1636
 
1545
 
1637
 
1546
/*
1638
/*
1547
    EVALUATE A CONSTANT UNARY OPERATION
1639
    EVALUATE A CONSTANT UNARY OPERATION
Line 1550... Line 1642...
1550
    on the integer constant expression a.  Any necessary operand conversions
1642
    on the integer constant expression a.  Any necessary operand conversions
1551
    and arithmetic type conversions have already been performed on a.  The
1643
    and arithmetic type conversions have already been performed on a.  The
1552
    permitted operations are '!', '-' and '~'.
1644
    permitted operations are '!', '-' and '~'.
1553
*/
1645
*/
1554
 
1646
 
1555
EXP make_unary_nat
1647
EXP
1556
    PROTO_N ( ( tag, a ) )
-
 
1557
    PROTO_T ( unsigned tag X EXP a )
1648
make_unary_nat(unsigned tag, EXP a)
1558
{
1649
{
1559
    EXP e ;
1650
	EXP e;
1560
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1651
	TYPE t = DEREF_type(exp_type(a));
1561
    NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1652
	NAT n = DEREF_nat(exp_int_lit_nat(a));
1562
 
1653
 
1563
    /* Can only evaluate result if n is not calculated */
1654
	/* Can only evaluate result if n is not calculated */
1564
    if ( !is_calc_nat ( n ) ) {
1655
	if (!is_calc_nat(n)) {
1565
	switch ( tag ) {
1656
		switch (tag) {
1566
	    case exp_not_tag : {
1657
		case exp_not_tag: {
1567
		/* Deal with '!a' */
1658
			/* Deal with '!a' */
1568
		unsigned p = test_bool_exp ( a ) ;
1659
			unsigned p = test_bool_exp(a);
1569
		if ( p == BOOL_UNKNOWN ) break ;
1660
			if (p == BOOL_UNKNOWN) {
-
 
1661
				break;
-
 
1662
			}
1570
		e = make_bool_exp ( BOOL_NEGATE ( p ), tag ) ;
1663
			e = make_bool_exp(BOOL_NEGATE(p), tag);
1571
		return ( e ) ;
1664
			return(e);
1572
	    }
1665
		}
1573
	    case exp_abs_tag : {
1666
		case exp_abs_tag: {
1574
		/* Deal with 'abs ( a )' */
1667
			/* Deal with 'abs ( a )' */
1575
		int c = compare_nat ( n, small_nat [0] ) ;
1668
			int c = compare_nat(n, small_nat[0]);
1576
		if ( c == 0 || c == 1 ) return ( a ) ;
1669
			if (c == 0 || c == 1) {
-
 
1670
				return(a);
-
 
1671
			}
-
 
1672
			if (c == -1) {
1577
		if ( c == -1 ) goto negate_lab ;
1673
				goto negate_lab;
-
 
1674
			}
1578
		break ;
1675
			break;
1579
	    }
1676
		}
1580
	    case exp_negate_tag :
1677
		case exp_negate_tag:
1581
	    negate_lab : {
1678
negate_lab:
1582
		/* Deal with '-a' */
1679
			/* Deal with '-a' */
1583
		n = negate_nat ( n ) ;
1680
			n = negate_nat(n);
1584
		e = make_int_exp ( t, tag, n ) ;
1681
			e = make_int_exp(t, tag, n);
1585
		if ( !IS_NULL_exp ( e ) ) return ( e ) ;
1682
			if (!IS_NULL_exp(e))  {
1586
		break ;
1683
				return(e);
1587
	    }
1684
			}
-
 
1685
			break;
1588
	    case exp_compl_tag : {
1686
		case exp_compl_tag:
1589
		/* Deal with '~a' */
1687
			/* Deal with '~a' */
1590
		/* NOT YET IMPLEMENTED */
1688
			/* NOT YET IMPLEMENTED */
1591
		break ;
1689
			break;
1592
	    }
1690
		}
1593
	}
1691
	}
1594
    }
-
 
1595
 
1692
 
1596
    /* Calculated case */
1693
	/* Calculated case */
1597
    a = calc_exp_value ( a ) ;
1694
	a = calc_exp_value(a);
1598
    MAKE_exp_negate_etc ( tag, t, a, e ) ;
1695
	MAKE_exp_negate_etc(tag, t, a, e);
1599
    MAKE_nat_calc ( e, n ) ;
1696
	MAKE_nat_calc(e, n);
1600
    MAKE_exp_int_lit ( t, n, tag, e ) ;
1697
	MAKE_exp_int_lit(t, n, tag, e);
1601
    return ( e ) ;
1698
	return(e);
1602
}
1699
}
1603
 
1700
 
1604
 
1701
 
1605
/*
1702
/*
1606
    CHECK A CHARACTER LITERAL CONSTANT
1703
    CHECK A CHARACTER LITERAL CONSTANT
1607
 
1704
 
1608
    This routine checks whether the integer constant expression a represents
1705
    This routine checks whether the integer constant expression a represents
1609
    one of the decimal character literals, '0', '1', ..., '9'.  If so it
1706
    one of the decimal character literals, '0', '1', ..., '9'.  If so it
1610
    returns the corresponding value in the range [0,9].  Otherwise it
1707
    returns the corresponding value in the range [0, 9].  Otherwise it
1611
    returns -1.
1708
    returns -1.
1612
*/
1709
*/
1613
 
1710
 
1614
static int eval_char_nat
1711
static int
1615
    PROTO_N ( ( a, k ) )
-
 
1616
    PROTO_T ( EXP a X unsigned *k )
1712
eval_char_nat(EXP a, unsigned *k)
1617
{
1713
{
1618
    unsigned tag = TAG_exp ( a ) ;
1714
	unsigned tag = TAG_exp(a);
1619
    if ( tag == exp_int_lit_tag ) {
1715
	if (tag == exp_int_lit_tag) {
1620
	NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1716
		NAT n = DEREF_nat(exp_int_lit_nat(a));
1621
	if ( IS_nat_calc ( n ) ) {
1717
		if (IS_nat_calc(n)) {
1622
	    a = DEREF_exp ( nat_calc_value ( n ) ) ;
1718
			a = DEREF_exp(nat_calc_value(n));
1623
	    tag = TAG_exp ( a ) ;
1719
			tag = TAG_exp(a);
1624
	}
1720
		}
1625
    }
1721
	}
1626
    if ( tag == exp_char_lit_tag ) {
1722
	if (tag == exp_char_lit_tag) {
1627
	int d = DEREF_int ( exp_char_lit_digit ( a ) ) ;
1723
		int d = DEREF_int(exp_char_lit_digit(a));
1628
	STRING str = DEREF_str ( exp_char_lit_str ( a ) ) ;
1724
		STRING str = DEREF_str(exp_char_lit_str(a));
1629
	*k = DEREF_unsigned ( str_simple_kind ( str ) ) ;
1725
		*k = DEREF_unsigned(str_simple_kind(str));
1630
	return ( d ) ;
1726
		return(d);
1631
    }
1727
	}
1632
    if ( tag == exp_cast_tag ) {
1728
	if (tag == exp_cast_tag) {
1633
	a = DEREF_exp ( exp_cast_arg ( a ) ) ;
1729
		a = DEREF_exp(exp_cast_arg(a));
1634
	return ( eval_char_nat ( a, k ) ) ;
1730
		return(eval_char_nat(a, k));
1635
    }
1731
	}
1636
    return ( -1 ) ;
1732
	return(-1);
1637
}
1733
}
1638
 
1734
 
1639
 
1735
 
1640
/*
1736
/*
1641
    ADD A VALUE TO A CHARACTER LITERAL CONSTANT
1737
    ADD A VALUE TO A CHARACTER LITERAL CONSTANT
1642
 
1738
 
1643
    This routine adds or subtracts (depending on the value of tag) the
1739
    This routine adds or subtracts (depending on the value of tag) the
1644
    value n to the decimal character literal d, casting the result to
1740
    value n to the decimal character literal d, casting the result to
1645
    type t.  The null expression is returned if the result is not a
1741
    type t.  The null expression is returned if the result is not a
1646
    character literal.  For example, this routine is used to evaluate
1742
    character literal.  For example, this routine is used to evaluate
1647
    '4' + 3 as '7' regardless of the underlying character set.  This
1743
    '4' + 3 as '7' regardless of the underlying character set.  This
1648
    wouldn't be terribly important, but certain validation set suites
1744
    wouldn't be terribly important, but certain validation set suites
1649
    use 6 + '0' - '6' as a null pointer constant!
1745
    use 6 + '0' - '6' as a null pointer constant!
1650
*/
1746
*/
1651
 
1747
 
1652
static EXP make_char_nat
1748
static EXP
1653
    PROTO_N ( ( t, tag, d, kind, n ) )
-
 
1654
    PROTO_T ( TYPE t X unsigned tag X int d X unsigned kind X NAT n )
1749
make_char_nat(TYPE t, unsigned tag, int d, unsigned kind, NAT n)
1655
{
1750
{
1656
    int neg = ( tag == exp_minus_tag ? 1 : 0 ) ;
1751
	int neg = (tag == exp_minus_tag ? 1 : 0);
1657
    if ( IS_nat_neg ( n ) ) {
1752
	if (IS_nat_neg(n)) {
1658
	/* Negate if necessary */
1753
		/* Negate if necessary */
1659
	n = DEREF_nat ( nat_neg_arg ( n ) ) ;
1754
		n = DEREF_nat(nat_neg_arg(n));
1660
	neg = !neg ;
1755
		neg = !neg;
1661
    }
1756
	}
1662
    if ( IS_nat_small ( n ) ) {
1757
	if (IS_nat_small(n)) {
1663
	unsigned v = DEREF_unsigned ( nat_small_value ( n ) ) ;
1758
		unsigned v = DEREF_unsigned(nat_small_value(n));
1664
	if ( v < 10 ) {
1759
		if (v < 10) {
1665
	    int m = ( int ) v ;
1760
			int m = (int)v;
1666
	    if ( neg ) m = -m ;
1761
			if (neg) {
-
 
1762
				m = -m;
-
 
1763
			}
1667
	    d += m ;
1764
			d += m;
1668
	    if ( d >= 0 && d < 10 ) {
1765
			if (d >= 0 && d < 10) {
1669
		/* Construct the result */
1766
				/* Construct the result */
1670
		EXP e ;
1767
				EXP e;
1671
		STRING str ;
1768
				STRING str;
1672
		character s [2] ;
1769
				character s[2];
1673
		ERROR err = NULL_err ;
1770
				ERROR err = NULL_err;
1674
		s [0] = ( character ) ( d + char_zero ) ;
1771
				s[0] = (character)(d + char_zero);
1675
		s [1] = 0 ;
1772
				s[1] = 0;
1676
		MAKE_str_simple ( 1, xustrcpy ( s ), kind, str ) ;
1773
				MAKE_str_simple(1, xustrcpy(s), kind, str);
1677
		e = make_string_exp ( str ) ;
1774
				e = make_string_exp(str);
1678
		e = make_cast_nat ( t, e, &err, CAST_STATIC ) ;
1775
				e = make_cast_nat(t, e, &err, CAST_STATIC);
1679
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1776
				if (!IS_NULL_err(err)) {
-
 
1777
					report(crt_loc, err);
-
 
1778
				}
1680
		return ( e ) ;
1779
				return(e);
1681
	    }
1780
			}
1682
	}
1781
		}
1683
    }
1782
	}
1684
    return ( NULL_exp ) ;
1783
	return(NULL_exp);
1685
}
1784
}
1686
 
1785
 
1687
 
1786
 
1688
/*
1787
/*
1689
    EVALUATE A CONSTANT BINARY OPERATION
1788
    EVALUATE A CONSTANT BINARY OPERATION
1690
 
1789
 
1691
    This routine is used to evaluate the binary operation indicated by tag
1790
    This routine is used to evaluate the binary operation indicated by tag
1692
    on the integer constant expressions a and b.  Any necessary operand
1791
    on the integer constant expressions a and b.  Any necessary operand
1693
    conversions and arithmetic type conversions have already been performed
1792
    conversions and arithmetic type conversions have already been performed
1694
    on a and b.  The permitted operations are '+', '-', '*', '/', '%', '<<',
1793
    on a and b.  The permitted operations are '+', '-', '*', '/', '%', '<<',
1695
    '>>', '&', '|', '^', '&&' and '||'.
1794
    '>>', '&', '|', '^', '&&' and '||'.
1804
 
1900
 
1805
    /* Return result if known (either n, m or 0) */
1901
	/* Return result if known (either n, m or 0) */
1806
    if ( !IS_NULL_nat ( res ) ) {
1902
	if (!IS_NULL_nat(res)) {
1807
	MAKE_exp_int_lit ( t, res, tag, e ) ;
1903
		MAKE_exp_int_lit(t, res, tag, e);
1808
	return ( e ) ;
1904
		return(e);
1809
    }
1905
	}
1810
 
1906
 
1811
    /* Can only evaluate result if n and m are not calculated */
1907
	/* Can only evaluate result if n and m are not calculated */
1812
    if ( calc && !is_calc_nat ( n ) && !is_calc_nat ( m ) ) {
1908
	if (calc && !is_calc_nat(n) && !is_calc_nat(m)) {
1813
	res = binary_nat_op ( tag, n, m ) ;
1909
		res = binary_nat_op(tag, n, m);
1814
	if ( !IS_NULL_nat ( res ) ) {
1910
		if (!IS_NULL_nat(res)) {
1815
	    e = make_int_exp ( t, tag, res ) ;
1911
			e = make_int_exp(t, tag, res);
1816
	    if ( !IS_NULL_exp ( e ) ) return ( e ) ;
1912
			if (!IS_NULL_exp(e)) {
-
 
1913
				return(e);
-
 
1914
			}
1817
	}
1915
		}
1818
    }
1916
	}
1819
 
1917
 
1820
    /* Check for digit characters */
1918
	/* Check for digit characters */
1821
    if ( tag == exp_plus_tag || tag == exp_minus_tag ) {
1919
	if (tag == exp_plus_tag || tag == exp_minus_tag) {
1822
	unsigned ka, kb ;
1920
		unsigned ka, kb;
1823
	int da = eval_char_nat ( a, &ka ) ;
1921
		int da = eval_char_nat(a, &ka);
1824
	int db = eval_char_nat ( b, &kb ) ;
1922
		int db = eval_char_nat(b, &kb);
1825
	if ( da >= 0 ) {
1923
		if (da >= 0) {
1826
	    if ( db >= 0 && tag == exp_minus_tag ) {
1924
			if (db >= 0 && tag == exp_minus_tag) {
1827
		/* Difference of two digits */
1925
				/* Difference of two digits */
1828
		res = make_small_nat ( da - db ) ;
1926
				res = make_small_nat(da - db);
1829
		e = make_int_exp ( t, tag, res ) ;
1927
				e = make_int_exp(t, tag, res);
1830
		if ( !IS_NULL_exp ( e ) ) return ( e ) ;
1928
				if (!IS_NULL_exp(e)) {
-
 
1929
					return(e);
-
 
1930
				}
1831
	    } else {
1931
			} else {
1832
		/* Digit plus or minus value */
1932
				/* Digit plus or minus value */
1833
		e = make_char_nat ( t, tag, da, ka, m ) ;
1933
				e = make_char_nat(t, tag, da, ka, m);
1834
		if ( !IS_NULL_exp ( e ) ) return ( e ) ;
1934
				if (!IS_NULL_exp(e)) {
-
 
1935
					return(e);
1835
	    }
1936
				}
-
 
1937
			}
1836
	} else if ( db >= 0 && tag == exp_plus_tag ) {
1938
		} else if (db >= 0 && tag == exp_plus_tag) {
1837
	    /* Digit plus value */
1939
			/* Digit plus value */
1838
	    e = make_char_nat ( t, tag, db, kb, n ) ;
1940
			e = make_char_nat(t, tag, db, kb, n);
1839
	    if ( !IS_NULL_exp ( e ) ) return ( e ) ;
1941
			if (!IS_NULL_exp(e)) {
-
 
1942
				return(e);
-
 
1943
			}
-
 
1944
		}
1840
	}
1945
	}
1841
    }
-
 
1842
 
1946
 
1843
    /* Calculated case */
1947
	/* Calculated case */
1844
    a = calc_exp_value ( a ) ;
1948
	a = calc_exp_value(a);
1845
    b = calc_exp_value ( b ) ;
1949
	b = calc_exp_value(b);
1846
    MAKE_exp_plus_etc ( tag, t, a, b, e ) ;
1950
	MAKE_exp_plus_etc(tag, t, a, b, e);
1847
    MAKE_nat_calc ( e, res ) ;
1951
	MAKE_nat_calc(e, res);
1848
    MAKE_exp_int_lit ( t, res, tag, e ) ;
1952
	MAKE_exp_int_lit(t, res, tag, e);
1849
    return ( e ) ;
1953
	return(e);
1850
}
1954
}
1851
 
1955
 
1852
 
1956
 
1853
/*
1957
/*
1854
    EVALUATE A CONSTANT TEST OPERATION
1958
    EVALUATE A CONSTANT TEST OPERATION
1855
 
1959
 
1856
    This routine is used to convert the integer constant expression a to
1960
    This routine is used to convert the integer constant expression a to
1857
    a boolean.
1961
    a boolean.
1858
*/
1962
*/
1859
 
1963
 
1860
EXP make_test_nat
1964
EXP
1861
    PROTO_N ( ( a ) )
-
 
1862
    PROTO_T ( EXP a )
1965
make_test_nat(EXP a)
1863
{
1966
{
1864
    EXP e ;
1967
	EXP e;
1865
    NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1968
	NAT n = DEREF_nat(exp_int_lit_nat(a));
1866
    if ( !is_calc_nat ( n ) ) {
1969
	if (!is_calc_nat(n)) {
1867
	/* Zero is false, non-zero is true */
1970
		/* Zero is false, non-zero is true */
1868
	unsigned tag = DEREF_unsigned ( exp_int_lit_etag ( a ) ) ;
1971
		unsigned tag = DEREF_unsigned(exp_int_lit_etag(a));
1869
	unsigned b = BOOL_NEGATE ( is_zero_nat ( n ) ) ;
1972
		unsigned b = BOOL_NEGATE(is_zero_nat(n));
1870
	e = make_bool_exp ( b, tag ) ;
1973
		e = make_bool_exp(b, tag);
1871
    } else {
-
 
1872
	/* Calculated case */
-
 
1873
	TYPE t = DEREF_type ( exp_type ( a ) ) ;
-
 
1874
	if ( check_int_type ( t, btype_bool ) ) {
-
 
1875
	    e = a ;
-
 
1876
	} else {
1974
	} else {
-
 
1975
		/* Calculated case */
-
 
1976
		TYPE t = DEREF_type(exp_type(a));
-
 
1977
		if (check_int_type(t, btype_bool)) {
-
 
1978
			e = a;
-
 
1979
		} else {
1877
	    a = calc_exp_value ( a ) ;
1980
			a = calc_exp_value(a);
1878
	    MAKE_exp_test ( type_bool, ntest_not_eq, a, e ) ;
1981
			MAKE_exp_test(type_bool, ntest_not_eq, a, e);
1879
	    MAKE_nat_calc ( e, n ) ;
1982
			MAKE_nat_calc(e, n);
1880
	    MAKE_exp_int_lit ( type_bool, n, exp_test_tag, e ) ;
1983
			MAKE_exp_int_lit(type_bool, n, exp_test_tag, e);
1881
	}
1984
		}
1882
    }
1985
	}
1883
    return ( e ) ;
1986
	return(e);
1884
}
1987
}
1885
 
1988
 
1886
 
1989
 
1887
/*
1990
/*
1888
    EVALUATE A CONSTANT COMPARISON OPERATION
1991
    EVALUATE A CONSTANT COMPARISON OPERATION
Line 1891... Line 1994...
1891
    op on the integer constant expressions a and b.  Any necessary operand
1994
    op on the integer constant expressions a and b.  Any necessary operand
1892
    conversions and arithmetic type conversions have already been performed
1995
    conversions and arithmetic type conversions have already been performed
1893
    on a and b.
1996
    on a and b.
1894
*/
1997
*/
1895
 
1998
 
1896
EXP make_compare_nat
1999
EXP
1897
    PROTO_N ( ( op, a, b ) )
-
 
1898
    PROTO_T ( NTEST op X EXP a X EXP b )
2000
make_compare_nat(NTEST op, EXP a, EXP b)
1899
{
2001
{
1900
    EXP e ;
2002
	EXP e;
1901
    NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
2003
	NAT n = DEREF_nat(exp_int_lit_nat(a));
1902
    NAT m = DEREF_nat ( exp_int_lit_nat ( b ) ) ;
2004
	NAT m = DEREF_nat(exp_int_lit_nat(b));
1903
    int c = compare_nat ( n, m ) ;
2005
	int c = compare_nat(n, m);
1904
    if ( c == 0 ) {
2006
	if (c == 0) {
1905
	/* n and m are definitely equal */
2007
		/* n and m are definitely equal */
1906
	if ( !overflow_exp ( a ) ) {
2008
		if (!overflow_exp(a)) {
1907
	    unsigned cond = BOOL_FALSE ;
2009
			unsigned cond = BOOL_FALSE;
1908
	    switch ( op ) {
2010
			switch (op) {
1909
		case ntest_eq :
2011
			case ntest_eq:
1910
		case ntest_less_eq :
2012
			case ntest_less_eq:
1911
		case ntest_greater_eq : {
2013
			case ntest_greater_eq:
1912
		    cond = BOOL_TRUE ;
2014
				cond = BOOL_TRUE;
1913
		    break ;
2015
				break;
1914
		}
2016
			}
1915
	    }
-
 
1916
	    e = make_bool_exp ( cond, exp_compare_tag ) ;
2017
			e = make_bool_exp(cond, exp_compare_tag);
1917
	    return ( e ) ;
2018
			return(e);
1918
	}
2019
		}
1919
    } else if ( c == 1 ) {
2020
	} else if (c == 1) {
1920
	/* n is definitely greater than m */
2021
		/* n is definitely greater than m */
1921
	if ( !overflow_exp ( a ) && !overflow_exp ( b ) ) {
2022
		if (!overflow_exp(a) && !overflow_exp(b)) {
1922
	    unsigned cond = BOOL_FALSE ;
2023
			unsigned cond = BOOL_FALSE;
1923
	    switch ( op ) {
2024
			switch (op) {
1924
		case ntest_not_eq :
2025
			case ntest_not_eq:
1925
		case ntest_greater :
2026
			case ntest_greater:
1926
		case ntest_greater_eq : {
2027
			case ntest_greater_eq:
1927
		    cond = BOOL_TRUE ;
2028
				cond = BOOL_TRUE;
1928
		    break ;
2029
				break;
1929
		}
2030
			}
1930
	    }
-
 
1931
	    e = make_bool_exp ( cond, exp_compare_tag ) ;
2031
			e = make_bool_exp(cond, exp_compare_tag);
1932
	    return ( e ) ;
2032
			return(e);
1933
	}
2033
		}
1934
    } else if ( c == -1 ) {
2034
	} else if (c == -1) {
1935
	/* n is definitely less than m */
2035
		/* n is definitely less than m */
1936
	if ( !overflow_exp ( a ) && !overflow_exp ( b ) ) {
2036
		if (!overflow_exp(a) && !overflow_exp(b)) {
1937
	    unsigned cond = BOOL_FALSE ;
2037
			unsigned cond = BOOL_FALSE;
1938
	    switch ( op ) {
2038
			switch (op) {
1939
		case ntest_not_eq :
2039
			case ntest_not_eq:
1940
		case ntest_less :
2040
			case ntest_less:
1941
		case ntest_less_eq : {
2041
			case ntest_less_eq:
1942
		    cond = BOOL_TRUE ;
2042
				cond = BOOL_TRUE;
1943
		    break ;
2043
				break;
-
 
2044
			}
-
 
2045
			e = make_bool_exp(cond, exp_compare_tag);
-
 
2046
			return(e);
1944
		}
2047
		}
1945
	    }
-
 
1946
	    e = make_bool_exp ( cond, exp_compare_tag ) ;
-
 
1947
	    return ( e ) ;
-
 
1948
	}
2048
	}
1949
    }
-
 
1950
 
2049
 
1951
    /* Calculated values require further calculation */
2050
	/* Calculated values require further calculation */
1952
    a = calc_exp_value ( a ) ;
2051
	a = calc_exp_value(a);
1953
    b = calc_exp_value ( b ) ;
2052
	b = calc_exp_value(b);
1954
    MAKE_exp_compare ( type_bool, op, a, b, e ) ;
2053
	MAKE_exp_compare(type_bool, op, a, b, e);
1955
    MAKE_nat_calc ( e, n ) ;
2054
	MAKE_nat_calc(e, n);
1956
    MAKE_exp_int_lit ( type_bool, n, exp_compare_tag, e ) ;
2055
	MAKE_exp_int_lit(type_bool, n, exp_compare_tag, e);
1957
    return ( e ) ;
2056
	return(e);
1958
}
2057
}
1959
 
2058
 
1960
 
2059
 
1961
/*
2060
/*
1962
    EVALUATE A CONSTANT CONDITIONAL OPERATION
2061
    EVALUATE A CONSTANT CONDITIONAL OPERATION
1963
 
2062
 
1964
    This routine is used to evaluate the conditional operation 'a ? b : c'
2063
    This routine is used to evaluate the conditional operation 'a ? b : c'
1965
    when a, b and c are all integer constant expressions.  Any necessary
2064
    when a, b and c are all integer constant expressions.  Any necessary
1966
    operand conversions and arithmetic type conversions have already been
2065
    operand conversions and arithmetic type conversions have already been
1967
    performed on a, b and c.
2066
    performed on a, b and c.
1968
*/
2067
*/
1969
 
2068
 
1970
EXP make_cond_nat
2069
EXP
1971
    PROTO_N ( ( a, b, c ) )
-
 
1972
    PROTO_T ( EXP a X EXP b X EXP c )
2070
make_cond_nat(EXP a, EXP b, EXP c)
1973
{
2071
{
1974
    EXP e ;
2072
	EXP e;
1975
    TYPE t = DEREF_type ( exp_type ( b ) ) ;
2073
	TYPE t = DEREF_type(exp_type(b));
1976
    NAT n = DEREF_nat ( exp_int_lit_nat ( b ) ) ;
2074
	NAT n = DEREF_nat(exp_int_lit_nat(b));
1977
    NAT m = DEREF_nat ( exp_int_lit_nat ( c ) ) ;
2075
	NAT m = DEREF_nat(exp_int_lit_nat(c));
1978
    unsigned p = test_bool_exp ( a ) ;
2076
	unsigned p = test_bool_exp(a);
1979
    if ( p == BOOL_TRUE && !overflow_exp ( c ) ) {
2077
	if (p == BOOL_TRUE && !overflow_exp(c)) {
1980
	/* EMPTY */
2078
		/* EMPTY */
1981
    } else if ( p == BOOL_FALSE && !overflow_exp ( b ) ) {
2079
	} else if (p == BOOL_FALSE && !overflow_exp(b)) {
1982
	n = m ;
2080
		n = m;
1983
    } else {
2081
	} else {
1984
	/* Calculated case */
2082
		/* Calculated case */
1985
	b = calc_exp_value ( b ) ;
2083
		b = calc_exp_value(b);
1986
	c = calc_exp_value ( c ) ;
2084
		c = calc_exp_value(c);
1987
	MAKE_exp_if_stmt ( t, a, b, c, NULL_id, e ) ;
2085
		MAKE_exp_if_stmt(t, a, b, c, NULL_id, e);
1988
	MAKE_nat_calc ( e, n ) ;
2086
		MAKE_nat_calc(e, n);
1989
    }
2087
	}
1990
    MAKE_exp_int_lit ( t, n, exp_if_stmt_tag, e ) ;
2088
	MAKE_exp_int_lit(t, n, exp_if_stmt_tag, e);
1991
    return ( e ) ;
2089
	return(e);
1992
}
2090
}
1993
 
2091
 
1994
 
2092
 
1995
/*
2093
/*
1996
    DOES ONE EXPRESSION DIVIDE ANOTHER?
2094
    DOES ONE EXPRESSION DIVIDE ANOTHER?
1997
 
2095
 
1998
    This routine returns true if a and b are both integer constant
2096
    This routine returns true if a and b are both integer constant
1999
    expressions and b divides a.
2097
    expressions and b divides a.
2000
*/
2098
*/
2001
 
2099
 
2002
int divides_nat
2100
int
2003
    PROTO_N ( ( a, b ) )
-
 
2004
    PROTO_T ( EXP a X EXP b )
2101
divides_nat(EXP a, EXP b)
2005
{
2102
{
2006
    if ( IS_exp_int_lit ( a ) && IS_exp_int_lit ( b ) ) {
2103
	if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
2007
	unsigned long vn, vm ;
2104
		unsigned long vn, vm;
2008
	NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
2105
		NAT n = DEREF_nat(exp_int_lit_nat(a));
2009
	NAT m = DEREF_nat ( exp_int_lit_nat ( b ) ) ;
2106
		NAT m = DEREF_nat(exp_int_lit_nat(b));
-
 
2107
		if (IS_nat_neg(n)) {
2010
	if ( IS_nat_neg ( n ) ) n = DEREF_nat ( nat_neg_arg ( n ) ) ;
2108
			n = DEREF_nat(nat_neg_arg(n));
-
 
2109
		}
-
 
2110
		if (IS_nat_neg(m)) {
2011
	if ( IS_nat_neg ( m ) ) m = DEREF_nat ( nat_neg_arg ( m ) ) ;
2111
			m = DEREF_nat(nat_neg_arg(m));
-
 
2112
		}
2012
	vn = get_nat_value ( n ) ;
2113
		vn = get_nat_value(n);
2013
	vm = get_nat_value ( m ) ;
2114
		vm = get_nat_value(m);
2014
	if ( vm == 0 ) return ( 1 ) ;
2115
		if (vm == 0) {
-
 
2116
			return(1);
-
 
2117
		}
2015
	if ( vn == EXTENDED_MAX || vm == EXTENDED_MAX ) return ( 0 ) ;
2118
		if (vn == EXTENDED_MAX || vm == EXTENDED_MAX) {
-
 
2119
			return(0);
-
 
2120
		}
2016
	if ( ( vn % vm ) == 0 ) return ( 1 ) ;
2121
		if ((vn % vm) == 0) {
-
 
2122
			return(1);
2017
    }
2123
		}
-
 
2124
	}
2018
    return ( 0 ) ;
2125
	return(0);
2019
}
2126
}
2020
 
2127
 
2021
 
2128
 
2022
/*
2129
/*
2023
    EVALUATE A CONSTANT CONDITION
2130
    EVALUATE A CONSTANT CONDITION
2024
 
2131
 
2025
    This routine evaluates the boolean expression e, returning BOOL_FALSE,
2132
    This routine evaluates the boolean expression e, returning BOOL_FALSE,
2026
    BOOL_TRUE or BOOL_UNKNOWN depending on whether it is always false,
2133
    BOOL_TRUE or BOOL_UNKNOWN depending on whether it is always false,
2027
    always true, or constant, but indeterminant.  BOOL_INVALID is returned
2134
    always true, or constant, but indeterminant.  BOOL_INVALID is returned
2028
    for non-constant expressions.
2135
    for non-constant expressions.
2029
*/
2136
*/
2030
 
2137
 
2031
unsigned eval_const_cond
2138
unsigned
2032
    PROTO_N ( ( e ) )
-
 
2033
    PROTO_T ( EXP e )
2139
eval_const_cond(EXP e)
2034
{
2140
{
2035
    if ( !IS_NULL_exp ( e ) ) {
2141
	if (!IS_NULL_exp(e)) {
2036
	switch ( TAG_exp ( e ) ) {
2142
		switch (TAG_exp(e)) {
2037
	    case exp_int_lit_tag : {
2143
		case exp_int_lit_tag: {
2038
		/* Boolean constants */
2144
			/* Boolean constants */
2039
		unsigned b = test_bool_exp ( e ) ;
2145
			unsigned b = test_bool_exp(e);
2040
		return ( b ) ;
2146
			return(b);
2041
	    }
2147
		}
2042
	    case exp_not_tag : {
2148
		case exp_not_tag: {
2043
		/* Logical negation */
2149
			/* Logical negation */
2044
		EXP a = DEREF_exp ( exp_not_arg ( e ) ) ;
2150
			EXP a = DEREF_exp(exp_not_arg(e));
2045
		unsigned b = eval_const_cond ( a ) ;
2151
			unsigned b = eval_const_cond(a);
2046
		if ( b == BOOL_FALSE ) return ( BOOL_TRUE ) ;
2152
			if (b == BOOL_FALSE) {
-
 
2153
				return(BOOL_TRUE);
-
 
2154
			}
2047
		if ( b == BOOL_TRUE ) return ( BOOL_FALSE ) ;
2155
			if (b == BOOL_TRUE) {
-
 
2156
				return(BOOL_FALSE);
-
 
2157
			}
2048
		return ( b ) ;
2158
			return(b);
2049
	    }
2159
		}
2050
	    case exp_log_and_tag : {
2160
		case exp_log_and_tag: {
2051
		/* Logical and */
2161
			/* Logical and */
2052
		EXP a1 = DEREF_exp ( exp_log_and_arg1 ( e ) ) ;
2162
			EXP a1 = DEREF_exp(exp_log_and_arg1(e));
2053
		EXP a2 = DEREF_exp ( exp_log_and_arg2 ( e ) ) ;
2163
			EXP a2 = DEREF_exp(exp_log_and_arg2(e));
2054
		unsigned b1 = eval_const_cond ( a1 ) ;
2164
			unsigned b1 = eval_const_cond(a1);
2055
		unsigned b2 = eval_const_cond ( a2 ) ;
2165
			unsigned b2 = eval_const_cond(a2);
2056
		if ( b1 == BOOL_FALSE || b2 == BOOL_FALSE ) {
2166
			if (b1 == BOOL_FALSE || b2 == BOOL_FALSE) {
2057
		    return ( BOOL_FALSE ) ;
2167
				return(BOOL_FALSE);
2058
		}
2168
			}
2059
		if ( b1 == BOOL_TRUE && b2 == BOOL_TRUE ) {
2169
			if (b1 == BOOL_TRUE && b2 == BOOL_TRUE) {
2060
		    return ( BOOL_TRUE ) ;
2170
				return(BOOL_TRUE);
2061
		}
2171
			}
2062
		if ( b1 == BOOL_INVALID ) return ( BOOL_INVALID ) ;
2172
			if (b1 == BOOL_INVALID) {
-
 
2173
				return(BOOL_INVALID);
-
 
2174
			}
2063
		if ( b2 == BOOL_INVALID ) return ( BOOL_INVALID ) ;
2175
			if (b2 == BOOL_INVALID) {
-
 
2176
				return(BOOL_INVALID);
-
 
2177
			}
2064
		return ( BOOL_UNKNOWN ) ;
2178
			return(BOOL_UNKNOWN);
2065
	    }
2179
		}
2066
	    case exp_log_or_tag : {
2180
		case exp_log_or_tag: {
2067
		/* Logical or */
2181
			/* Logical or */
2068
		EXP a1 = DEREF_exp ( exp_log_or_arg1 ( e ) ) ;
2182
			EXP a1 = DEREF_exp(exp_log_or_arg1(e));
2069
		EXP a2 = DEREF_exp ( exp_log_or_arg2 ( e ) ) ;
2183
			EXP a2 = DEREF_exp(exp_log_or_arg2(e));
2070
		unsigned b1 = eval_const_cond ( a1 ) ;
2184
			unsigned b1 = eval_const_cond(a1);
2071
		unsigned b2 = eval_const_cond ( a2 ) ;
2185
			unsigned b2 = eval_const_cond(a2);
2072
		if ( b1 == BOOL_TRUE || b2 == BOOL_TRUE ) {
2186
			if (b1 == BOOL_TRUE || b2 == BOOL_TRUE) {
2073
		    return ( BOOL_TRUE ) ;
2187
				return(BOOL_TRUE);
2074
		}
2188
			}
2075
		if ( b1 == BOOL_FALSE && b2 == BOOL_FALSE ) {
2189
			if (b1 == BOOL_FALSE && b2 == BOOL_FALSE) {
2076
		    return ( BOOL_FALSE ) ;
2190
				return(BOOL_FALSE);
2077
		}
2191
			}
2078
		if ( b1 == BOOL_INVALID ) return ( BOOL_INVALID ) ;
2192
			if (b1 == BOOL_INVALID) {
-
 
2193
				return(BOOL_INVALID);
-
 
2194
			}
2079
		if ( b2 == BOOL_INVALID ) return ( BOOL_INVALID ) ;
2195
			if (b2 == BOOL_INVALID) {
-
 
2196
				return(BOOL_INVALID);
-
 
2197
			}
2080
		return ( BOOL_UNKNOWN ) ;
2198
			return(BOOL_UNKNOWN);
2081
	    }
2199
		}
2082
	    case exp_test_tag : {
2200
		case exp_test_tag: {
2083
		/* Test against zero */
2201
			/* Test against zero */
2084
		EXP a = DEREF_exp ( exp_test_arg ( e ) ) ;
2202
			EXP a = DEREF_exp(exp_test_arg(e));
2085
		NTEST op = DEREF_ntest ( exp_test_tst ( e ) ) ;
2203
			NTEST op = DEREF_ntest(exp_test_tst(e));
2086
		if ( IS_exp_null ( a ) ) {
2204
			if (IS_exp_null(a)) {
2087
		    /* Null pointers */
2205
				/* Null pointers */
2088
		    if ( op == ntest_eq ) return ( BOOL_TRUE ) ;
2206
				if (op == ntest_eq) {
-
 
2207
					return(BOOL_TRUE);
-
 
2208
				}
2089
		    if ( op == ntest_not_eq ) return ( BOOL_FALSE ) ;
2209
				if (op == ntest_not_eq) {
-
 
2210
					return(BOOL_FALSE);
-
 
2211
				}
2090
		}
2212
			}
2091
		break ;
2213
			break;
2092
	    }
2214
		}
2093
	    case exp_location_tag : {
2215
		case exp_location_tag: {
2094
		/* Conditions can contain locations */
2216
			/* Conditions can contain locations */
2095
		EXP a = DEREF_exp ( exp_location_arg ( e ) ) ;
2217
			EXP a = DEREF_exp(exp_location_arg(e));
2096
		return ( eval_const_cond ( a ) ) ;
2218
			return(eval_const_cond(a));
2097
	    }
2219
		}
2098
	}
2220
		}
2099
	if ( is_const_exp ( e, -1 ) ) return ( BOOL_UNKNOWN ) ;
2221
		if (is_const_exp(e, -1)) {
-
 
2222
			return(BOOL_UNKNOWN);
2100
    }
2223
		}
-
 
2224
	}
2101
    return ( BOOL_INVALID ) ;
2225
	return(BOOL_INVALID);
2102
}
2226
}
2103
 
2227
 
2104
 
2228
 
2105
/*
2229
/*
2106
    IS AN INTEGER CONSTANT EXPRESSION ZERO?
2230
    IS AN INTEGER CONSTANT EXPRESSION ZERO?
2107
 
2231
 
2108
    This routine checks whether the expression a is a zero integer constant.
2232
    This routine checks whether the expression a is a zero integer constant.
2109
    It is used to identify circumstances when zero is actually the null
2233
    It is used to identify circumstances when zero is actually the null
2110
    pointer etc.
2234
    pointer etc.
2111
*/
2235
*/
2112
 
2236
 
2113
int is_zero_exp
2237
int
2114
    PROTO_N ( ( a ) )
-
 
2115
    PROTO_T ( EXP a )
2238
is_zero_exp(EXP a)
2116
{
2239
{
2117
    if ( !IS_NULL_exp ( a ) && IS_exp_int_lit ( a ) ) {
2240
	if (!IS_NULL_exp(a) && IS_exp_int_lit(a)) {
2118
	NAT n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
2241
		NAT n = DEREF_nat(exp_int_lit_nat(a));
2119
	return ( is_zero_nat ( n ) ) ;
2242
		return(is_zero_nat(n));
2120
    }
2243
	}
2121
    return ( 0 ) ;
2244
	return(0);
2122
}
2245
}
2123
 
2246
 
2124
 
2247
 
2125
/*
2248
/*
2126
    IS AN INTEGER CONSTANT A LITERAL?
2249
    IS AN INTEGER CONSTANT A LITERAL?
2127
 
2250
 
2128
    This routine checks whether the integer constant expression a is an
2251
    This routine checks whether the integer constant expression a is an
2129
    integer literal or is the result of a constant evaluation.  This
2252
    integer literal or is the result of a constant evaluation.  This
2130
    information is recorded in the etag field of the expression.  It
2253
    information is recorded in the etag field of the expression.  It
2131
    returns 2 if the literal was precisely '0'.
2254
    returns 2 if the literal was precisely '0'.
2132
*/
2255
*/
2133
 
2256
 
2134
int is_literal
2257
int
2135
    PROTO_N ( ( a ) )
-
 
2136
    PROTO_T ( EXP a )
2258
is_literal(EXP a)
2137
{
2259
{
2138
    if ( IS_exp_int_lit ( a ) ) {
2260
	if (IS_exp_int_lit(a)) {
2139
	unsigned etag = DEREF_unsigned ( exp_int_lit_etag ( a ) ) ;
2261
		unsigned etag = DEREF_unsigned(exp_int_lit_etag(a));
2140
	if ( etag == exp_int_lit_tag ) return ( 1 ) ;
2262
		if (etag == exp_int_lit_tag) {
-
 
2263
			return(1);
-
 
2264
		}
2141
	if ( etag == exp_null_tag ) return ( 2 ) ;
2265
		if (etag == exp_null_tag) {
-
 
2266
			return(2);
-
 
2267
		}
2142
	if ( etag == exp_identifier_tag ) return ( 1 ) ;
2268
		if (etag == exp_identifier_tag) {
-
 
2269
			return(1);
2143
    }
2270
		}
-
 
2271
	}
2144
    return ( 0 ) ;
2272
	return(0);
2145
}
2273
}
2146
 
2274
 
2147
 
2275
 
2148
/*
2276
/*
2149
    FIND A SMALL FLOATING POINT LITERAL
2277
    FIND A SMALL FLOATING POINT LITERAL
2150
 
2278
 
2151
    This routine returns the nth literal associated with the floating point
2279
    This routine returns the nth literal associated with the floating point
2152
    type t.  The null literal is returned if n is too large.
2280
    type t.  The null literal is returned if n is too large.
2153
*/
2281
*/
2154
 
2282
 
2155
FLOAT get_float
2283
FLOAT
2156
    PROTO_N ( ( t, n ) )
-
 
2157
    PROTO_T ( TYPE t X int n )
2284
get_float(TYPE t, int n)
2158
{
2285
{
2159
    FLOAT_TYPE ft = DEREF_ftype ( type_floating_rep ( t ) ) ;
2286
	FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
2160
    LIST ( FLOAT ) fp = DEREF_list ( ftype_small ( ft ) ) ;
2287
	LIST(FLOAT)fp = DEREF_list(ftype_small(ft));
2161
    while ( !IS_NULL_list ( fp ) ) {
2288
	while (!IS_NULL_list(fp)) {
2162
	if ( n == 0 ) {
2289
		if (n == 0) {
2163
	    FLOAT flt = DEREF_flt ( HEAD_list ( fp ) ) ;
2290
			FLOAT flt = DEREF_flt(HEAD_list(fp));
2164
	    return ( flt ) ;
2291
			return(flt);
2165
	}
2292
		}
2166
	n-- ;
2293
		n--;
2167
	fp = TAIL_list ( fp ) ;
2294
		fp = TAIL_list(fp);
2168
    }
2295
	}
2169
    return ( NULL_flt ) ;
2296
	return(NULL_flt);
2170
}
2297
}
2171
 
2298
 
2172
 
2299
 
2173
/*
2300
/*
2174
    INITIALISE A FLOATING POINT TYPE
2301
    INITIALISE A FLOATING POINT TYPE
2175
 
2302
 
2176
    This routine initialises the floating point type ft by creating its
2303
    This routine initialises the floating point type ft by creating its
2177
    list of small literal values.
2304
    list of small literal values.
2178
*/
2305
*/
2179
 
2306
 
2180
void init_float
2307
void
2181
    PROTO_N ( ( ft ) )
-
 
2182
    PROTO_T ( FLOAT_TYPE ft )
2308
init_float(FLOAT_TYPE ft)
2183
{
2309
{
2184
    int n ;
2310
	int n;
2185
    NAT z = small_nat [0] ;
2311
	NAT z = small_nat[0];
2186
    string fp = small_number [0] ;
2312
	string fp = small_number[0];
2187
    LIST ( FLOAT ) p = NULL_list ( FLOAT ) ;
2313
	LIST(FLOAT)p = NULL_list(FLOAT);
2188
    for ( n = SMALL_FLT_SIZE - 1 ; n >= 0 ; n-- ) {
2314
	for (n = SMALL_FLT_SIZE - 1; n >= 0; n--) {
2189
	FLOAT f ;
2315
		FLOAT f;
2190
	string ip = small_number [n] ;
2316
		string ip = small_number[n];
2191
	MAKE_flt_simple ( ip, fp, z, f ) ;
2317
		MAKE_flt_simple(ip, fp, z, f);
2192
	CONS_flt ( f, p, p ) ;
2318
		CONS_flt(f, p, p);
2193
    }
2319
	}
2194
    COPY_list ( ftype_small ( ft ), p ) ;
2320
	COPY_list(ftype_small(ft), p);
2195
    return ;
2321
	return;
2196
}
2322
}
2197
 
2323
 
2198
 
2324
 
2199
/*
2325
/*
2200
    INITIALISE CONSTANT EVALUATION ROUTINES
2326
    INITIALISE CONSTANT EVALUATION ROUTINES
2201
 
2327
 
2202
    This routine initialises the small_nat array and the buffers used in
2328
    This routine initialises the small_nat array and the buffers used in
2203
    the constant evaluation routines.
2329
    the constant evaluation routines.
2204
*/
2330
*/
2205
 
2331
 
2206
void init_constant
2332
void
2207
    PROTO_Z ()
2333
init_constant(void)
2208
{
2334
{
2209
    int n = 0 ;
2335
	int n = 0;
2210
    while ( n < SMALL_NAT_ALLOC ) {
2336
	while (n < SMALL_NAT_ALLOC) {
2211
	IGNORE make_small_nat ( n ) ;
2337
		IGNORE make_small_nat(n);
2212
	IGNORE make_small_nat ( -n ) ;
2338
		IGNORE make_small_nat(-n);
2213
	n++ ;
2339
		n++;
2214
    }
2340
	}
2215
    while ( n < SMALL_NAT_SIZE ) {
2341
	while (n < SMALL_NAT_SIZE) {
2216
	small_nat [n] = NULL_nat ;
2342
		small_nat[n] = NULL_nat;
2217
	small_neg_nat [n] = NULL_nat ;
2343
		small_neg_nat[n] = NULL_nat;
2218
	n++ ;
2344
		n++;
2219
    }
2345
	}
2220
    small_neg_nat [0] = small_nat [0] ;
2346
	small_neg_nat[0] = small_nat[0];
2221
    CONS_unsigned ( 0, NULL_list ( unsigned ), small_nat_1 ) ;
2347
	CONS_unsigned(0, NULL_list(unsigned), small_nat_1);
2222
    CONS_unsigned ( 0, NULL_list ( unsigned ), small_nat_2 ) ;
2348
	CONS_unsigned(0, NULL_list(unsigned), small_nat_2);
2223
    small_number [0] = ustrlit ( "0" ) ;
2349
	small_number[0] = ustrlit("0");
2224
    small_number [1] = ustrlit ( "1" ) ;
2350
	small_number[1] = ustrlit("1");
2225
    return ;
2351
	return;
2226
}
2352
}