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
33
 
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:-
42
 
43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
45
 
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;
49
 
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;
53
 
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
$Author: release $
63
$Date: 1998/01/17 15:55:47 $
64
$Revision: 1.1.1.1 $
65
$Log: label_ops.c,v $
66
 * Revision 1.1.1.1  1998/01/17  15:55:47  release
67
 * First version to be checked into rolling release.
68
 *
69
 * Revision 1.5  1997/08/06  10:58:32  currie
70
 * Catch overflowed constants, PlumHall requirement
71
 *
72
 * Revision 1.4  1997/04/18  10:58:47  currie
73
 * another pathelogical case
74
 *
75
 * Revision 1.3  1996/05/21  12:53:54  currie
76
 * Pathological gotos
77
 *
78
 * Revision 1.2  1995/10/11  17:10:07  currie
79
 * avs errors
80
 *
81
 * Revision 1.1  1995/04/06  10:44:05  currie
82
 * Initial revision
83
 *
84
***********************************************************************/
85
 
86
 
87
 
88
#include "config.h"
89
#include "common_types.h"
90
#include "expmacs.h"
91
#include "tags.h"
92
#include "label_ops.h"
93
#include "check.h"
94
#include "externs.h"
95
#include "exp.h"
96
#include "installglob.h"
97
#include "shapemacs.h"
98
 
99
 
100
/* label_is_next returns 1 if lab is a labst which immediately
101
   follows e, otherwise 0. e will not be nilexp
102
*/
7 7u83 103
int
104
label_is_next(exp lab, exp e)
2 7u83 105
{
7 7u83 106
	if (!indirect_jumps) {
107
		return 0;
108
	}
109
	while (last(e)) {
110
		e = bro(e);
111
		if (e == nilexp || name(e) >= ass_tag || name(sh(e)) != bothd) {
112
			return 0;
113
		}
114
	}
115
	if (name(bro(e)) == labst_tag) {
116
		return bro(e) == lab;
117
	}
118
	return 0;
2 7u83 119
}
120
 
7 7u83 121
 
122
static exp
123
is_jumper(exp e)
2 7u83 124
{
7 7u83 125
	if (name(e) == test_tag || name(e) == goto_tag ||
126
	    name(e) == testbit_tag || name(e) == res_tag) {
127
		return e;
128
	}
129
	return nilexp;
2 7u83 130
}
131
 
7 7u83 132
 
133
static exp
134
down(exp e)
2 7u83 135
{
7 7u83 136
	if (name(e) == seq_tag) {
137
		return down(son(son(e)));
138
	}
139
	if (name(e) == cond_tag) {
140
		return down(son(e));
141
	}
142
	return e;
2 7u83 143
}
144
 
7 7u83 145
 
2 7u83 146
/* next_jump delivers a goto, res, test or testbit exp if this
147
   is certain to be the next thing obeyed after e. nilexp
148
   otherwise.
149
*/
7 7u83 150
static exp
151
next_jump(exp e)
2 7u83 152
{
7 7u83 153
	if (!indirect_jumps) {
154
		return nilexp;
155
	}
2 7u83 156
 
7 7u83 157
	do {
158
		while (last(e)) {
159
			e = bro(e);
160
			if (e == nilexp || name(e) >= goto_tag) {
161
				return nilexp;
162
			}
163
		}
164
		e = bro(e);
165
	} while (name(e) == labst_tag && (e = father(e), name(e) !=rep_tag));
2 7u83 166
 
7 7u83 167
	if (is_jumper(e)) {
168
		return e;
169
	}
170
	if (name(e) == seq_tag || name(e) == cond_tag) {
171
		return is_jumper(down(e));
172
	}
173
	if (name(e) == top_tag) {
174
		return next_jump(e);
175
	}
176
	return nilexp;
2 7u83 177
}
178
 
7 7u83 179
 
2 7u83 180
/* next_jump delivers a goto, res, test or testbit exp if this
181
   is certain to be the next thing obeyed after e and there is no
182
   other route to the goto (etc.). nilexp otherwise.
183
*/
7 7u83 184
exp
185
short_next_jump(exp e)
2 7u83 186
{
7 7u83 187
	if (!indirect_jumps) {
188
		return nilexp;
189
	}
2 7u83 190
 
7 7u83 191
	while (last(e)) {
192
		e = bro(e);
193
		if (e == nilexp || name(e) >= cond_tag) {
194
			return nilexp;
195
		}
196
	}
197
	e = bro(e);
2 7u83 198
 
7 7u83 199
	if (is_jumper(e)) {
200
		return e;
201
	}
202
	if (name(e) == seq_tag || name(e) == cond_tag) {
203
		return is_jumper(down(e));
204
	}
205
	if (name(e) == top_tag) {
206
		return short_next_jump(e);
207
	}
208
	return nilexp;
2 7u83 209
}
210
 
7 7u83 211
 
212
exp
213
jump_dest(exp lab)
2 7u83 214
{
7 7u83 215
	return next_jump(son(lab));
2 7u83 216
}
217
 
7 7u83 218
 
219
exp
220
final_dest(exp lab)
2 7u83 221
{
7 7u83 222
	exp final = lab;
223
	exp temp, ll;
224
	while (name(final) == labst_tag) {
225
		temp = jump_dest(final);
226
		if (temp != nilexp && name(temp) == goto_tag &&
227
		    pt(temp) != final) {
228
			ll = lab;
229
			while (ll != final) {
230
				if (pt(temp) == ll) {
231
					/* pathological loop */
232
					return final;
233
				}
234
				ll = pt(jump_dest(ll));
235
			}
236
			final = pt(temp);
237
		} else {
238
			break;
239
		}
240
	}
241
	return final;
2 7u83 242
}
243
 
7 7u83 244
 
2 7u83 245
/* delivers 1 iff when a jumps, b also jumps
7 7u83 246
   a and b will be test or testbit */
247
static int
248
subsumes(exp a, exp b)
2 7u83 249
{
7 7u83 250
	if (name(a) == name(b) && test_number(a) == test_number(b) &&
251
	    eq_exp(son(a), son(b)) && eq_exp(bro(son(a)), bro(son(b)))) {
252
		return 1;
253
	}
254
	return 0;
2 7u83 255
}
256
 
7 7u83 257
 
258
exp
259
final_dest_test(exp lab, exp e)
2 7u83 260
{
7 7u83 261
	exp final = lab;
262
	exp temp, ll;
263
	while (name(final) == labst_tag) {
264
		temp = jump_dest(final);
265
		if (temp == nilexp || final == pt(temp)) {
266
			return final;
267
		}
268
		if (name(temp) == goto_tag ||
269
		    (name(temp) == name(e) && subsumes(e, temp))) {
270
			ll = lab;
271
			while (ll != final) {
272
				if (pt(temp) == ll) {
273
					/* pathological loop */
274
					return final;
275
				}
276
				ll = pt(jump_dest(ll));
277
			}
278
			final = pt(temp);
279
		} else {
280
			break;
281
		}
282
	}
283
	return final;
2 7u83 284
}