Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "types.h"
6 7u83 63
#include "capsule.h"
2 7u83 64
#include "basic.h"
65
#include "binding.h"
66
#include "file.h"
67
#include "sort.h"
68
#include "tdf.h"
69
#include "tree.h"
70
#include "unit.h"
71
#include "utility.h"
72
 
73
 
74
/*
75
    EXPANSION MODE
76
 
77
    The flag dumb_mode determines whether the pretty-printer will
78
    attempt to rationalize its input, or whether it will just blindly
79
    output what it reads.  If show_stuff is true information on the
80
    number of tags and tokens etc will be output.
81
*/
82
 
6 7u83 83
int dumb_mode = 0;
84
int show_stuff = 0;
2 7u83 85
 
86
 
87
/*
88
    SKIP PASS FOR TOKEN DEFINITIONS FLAG
89
 
90
    Tokens may be defined recursively without the necessary previous
91
    declarations to make this simple.  Thus the token definitions are
92
    given a preliminary pass to extract the declaration information.
93
    This feature can be switch on and off using the skip_pass flag.
94
    If show_skip is true the information read on the skip pass is
95
    displayed.  skipping is true iff we are in the skip pass.
96
*/
97
 
6 7u83 98
int skip_pass = 1;
99
int show_skip = 0;
100
int skipping = 0;
2 7u83 101
 
102
 
103
/*
104
    ARRAYS OF EQUATION AND VARIABLE NAMES
105
 
106
    These arrays store the names of the types of equations (e.g. tokdefs)
107
    and the types of variables these equations are in (e.g. tags and tokens).
108
    The number of types of variables is given by no_variables.
109
*/
110
 
6 7u83 111
string *eqn_types = null;
112
string *var_types = null;
113
char *var_letters = "";
114
long *var_count = null;
115
long no_variables = 0;
2 7u83 116
 
117
 
118
/*
119
    CURRENT BINDINGS
120
 
121
    The current binding table is given by crt_binding.
122
*/
123
 
6 7u83 124
binding *crt_binding;
2 7u83 125
 
126
 
127
/*
128
    DECODE AN EXTERNAL LINKAGE
129
 
130
    An external name is decoded and associated with a variable of type v.
131
    r gives the order information.
132
*/
133
 
6 7u83 134
static void
135
de_linkextern(long v, long r)
2 7u83 136
{
6 7u83 137
    object *p;
138
    external ext;
139
    long n = tdf_int();
140
    ext = de_extern_name();
141
    p = new_object(v);
142
    set_binding(crt_binding, v, n, p);
143
    p->named = 1;
144
    p->name = ext;
145
    p->order = r;
146
    if (v == var_token && ext.simple) {
2 7u83 147
	/* Look for special tokens */
6 7u83 148
	char *nm = ext.val.str;
149
	if (nm[0] == '~' && diagnostics) {
150
	    if (streq(nm, "~dg_exp")) {
151
		token_sort(p, sort_exp, "xG", n);
152
	    } else if (streq(nm, "~exp_to_source")) {
153
		token_sort(p, sort_exp, "xMM", n);
154
	    } else if (streq(nm, "~diag_id_scope")) {
155
		token_sort(p, sort_exp, "x$xd", n);
156
	    } else if (streq(nm, "~diag_type_scope")) {
157
		token_sort(p, sort_exp, "x$d", n);
158
	    } else if (streq(nm, "~diag_tag_scope")) {
159
		token_sort(p, sort_exp, "x$d", n);
2 7u83 160
	    }
161
	}
162
    }
6 7u83 163
    if (dumb_mode) {
164
	word *w1, *w2;
165
	w1 = new_word(HORIZ_NONE);
166
	out_string(var_types[v]);
167
	w2 = new_word(HORIZ_BRACKETS);
168
	out_int(n);
169
	end_word(w2);
170
	out_string("represents ");
171
	if (ext.simple) {
172
	    out(ext.val.str);
2 7u83 173
	} else {
6 7u83 174
	    out_unique(ext.val.uniq);
2 7u83 175
	}
6 7u83 176
	end_word(w1);
2 7u83 177
    }
6 7u83 178
    return;
2 7u83 179
}
180
 
181
 
182
/*
183
    COUNT OF TOTAL NUMBER OF EQUATIONS OF A PARTICULAR TYPE
184
 
185
    This should be increased appropriately by each unit decoding function.
186
*/
187
 
6 7u83 188
long total = 0;
189
long blank_lines = 0;
2 7u83 190
 
191
 
192
/*
193
    DECODE A SET OF EQUATIONS
194
 
195
    A set of equations is decoded.  f gives the procedure which is to be
196
    used to decode the equation body.
197
*/
198
 
6 7u83 199
static void
200
de_equation(equation_func f)
2 7u83 201
{
6 7u83 202
    long i, n;
203
    long no_var;
204
    int needs_it = 0;
205
    static long unitno = 1;
2 7u83 206
 
207
    /* Record old bindings */
6 7u83 208
    binding *old_binding = crt_binding;
2 7u83 209
 
210
    /* Read the number of each type of variable */
6 7u83 211
    no_var = tdf_int();
212
    if (no_var) {
213
	if (no_var != no_variables) {
214
	    input_error("Number of local variables wrong");
2 7u83 215
	}
6 7u83 216
	crt_binding = new_binding_table();
217
	for (i = 0; i < no_var; i++) {
218
	    long sz = tdf_int();
219
	    set_binding_size(crt_binding, i, sz);
220
	    if (show_stuff) {
221
		out_string(var_types[i]);
222
		out_string(" x ");
223
		out_int(sz);
2 7u83 224
	    }
225
	}
6 7u83 226
	if (show_stuff)blank_line();
2 7u83 227
    }
228
 
229
    /* Read linkage for each type of variable */
6 7u83 230
    n = tdf_int();
231
    if (n != no_var)input_error("Number of linkage units wrong");
232
    if (no_var) {
233
	if (dumb_mode) {
234
	    word *w = new_word(HORIZ_NONE);
235
	    out_string("Bindings for Unit ");
236
	    out_int(unitno);
237
	    out("(inner->outer)");
238
	    end_word(w);
239
	    blank_line();
2 7u83 240
	}
6 7u83 241
	for (i = 0; i < no_var; i++) {
242
	    long j, no_links = tdf_int();
243
	    for (j = 0; j < no_links; j++) {
244
		object *p;
245
		long inner = tdf_int();
246
		long outer = tdf_int();
247
		if (dumb_mode) {
2 7u83 248
		    /* Output the linkage information */
6 7u83 249
		    word *w1, *w2;
250
		    w1 = new_word(HORIZ_NONE);
251
		    out_string(var_types[i]);
252
		    w2 = new_word(HORIZ_BRACKETS);
253
		    out_int(inner);
254
		    end_word(w2);
255
		    out_string("is bound to ");
256
		    out_string(var_types[i]);
257
		    w2 = new_word(HORIZ_BRACKETS);
258
		    out_int(outer);
259
		    end_word(w2);
260
		    end_word(w1);
261
		    needs_it = 1;
2 7u83 262
		}
6 7u83 263
		p = find_binding(old_binding, i, outer);
264
		set_binding(crt_binding, i, inner, p);
2 7u83 265
	    }
266
	}
6 7u83 267
	if (dumb_mode) {
268
	    if (needs_it)blank_line();
269
	    blank_lines = 1;
2 7u83 270
	}
271
 
272
	/* Complete the bindings */
6 7u83 273
	complete_binding(crt_binding);
2 7u83 274
    }
275
 
276
    /* Read the unit body */
6 7u83 277
    n = BYTESIZE * tdf_int();
278
    byte_align();
279
    if (f == null) {
280
	skip_bits(n);
281
	if (dumb_mode) {
282
	    out("(skipped)");
283
	    blank_line();
284
	    blank_lines = 1;
2 7u83 285
	}
6 7u83 286
	total++;
2 7u83 287
    } else {
6 7u83 288
	long end = posn(here) + n;
289
	(*f)();
290
	byte_align();
291
	if (posn(here)!= end)input_error("Unit length wrong");
2 7u83 292
    }
293
 
294
    /* Restore old bindings */
6 7u83 295
    if (no_var) {
296
	free_binding_table(crt_binding);
297
	crt_binding = old_binding;
298
	if (dumb_mode) {
299
	    for (i = blank_lines; i < 2; i++)blank_line();
300
	    out_string("End of Bindings for Unit ");
301
	    out_int(unitno++);
302
	    blank_line();
303
	    blank_line();
304
	    blank_lines = 2;
2 7u83 305
	}
306
    }
6 7u83 307
    return;
2 7u83 308
}
309
 
310
 
311
/*
312
    DECODE A CAPSULE
313
 
314
    A capsule consists of a number of equation types, a number of variable
315
    sorts, a number of external names for variables and a number of
316
    equations of certain types.
317
*/
318
 
6 7u83 319
void
320
de_capsule(void)
2 7u83 321
{
6 7u83 322
    long i, n;
323
    long no_eqn, no_var;
324
    if (dumb_mode)show_stuff = 1;
2 7u83 325
 
326
    /* Read the magic number */
6 7u83 327
    out("MAGIC NUMBER");
328
    blank_line();
329
    de_magic(version_magic);
330
    blank_line();
331
    blank_line();
2 7u83 332
 
333
    /* Read the equation types */
6 7u83 334
    no_eqn = tdf_int();
335
    if (no_eqn) {
336
	if (show_stuff) {
337
	    out("EQUATION TYPES");
338
	    blank_line();
2 7u83 339
	}
6 7u83 340
	eqn_types = alloc_nof(string, no_eqn);
341
	for (i = 0; i < no_eqn; i++) {
342
	    string s = de_tdfstring_align();
343
	    eqn_types[i] = s;
344
	    if (show_stuff)out(s);
2 7u83 345
	}
6 7u83 346
	if (show_stuff) {
347
	    blank_line();
348
	    blank_line();
2 7u83 349
	}
350
    }
351
 
352
    /* Read the variable types and initialize the bindings */
6 7u83 353
    no_var = tdf_int();
354
    no_variables = no_var;
355
    crt_binding = new_binding_table();
356
    if (no_var) {
357
	if (show_stuff) {
358
	    out("VARIABLE TYPES");
359
	    blank_line();
2 7u83 360
	}
6 7u83 361
	var_types = alloc_nof(string, no_var);
362
	var_letters = alloc_nof(char, no_var + 1);
363
	var_count = alloc_nof(long, no_var);
364
	var_letters[no_var] = 0;
2 7u83 365
 
6 7u83 366
	for (i = 0; i < no_var; i++) {
367
	    string sv = de_tdfstring_align();
368
	    long sz = tdf_int();
369
	    var_letters[i] = find_variable(sv, i);
370
	    var_types[i] = sv;
371
	    var_count[i] = 0;
372
	    set_binding_size(crt_binding, i, sz);
373
	    if (show_stuff) {
374
		out_string(sv);
375
		out_string(" x ");
376
		out_int(sz);
2 7u83 377
	    }
378
	}
6 7u83 379
	if (show_stuff) {
380
	    blank_line();
381
	    blank_line();
2 7u83 382
	}
383
    }
384
 
385
    /* Read the external variable names */
6 7u83 386
    n = tdf_int();
387
    if (n != no_var)input_error("Number of variables wrong");
388
    if (no_var) {
389
	if (dumb_mode) {
390
	    out("EXTERNAL NAMES");
391
	    blank_line();
2 7u83 392
	}
6 7u83 393
	for (i = 0; i < no_var; i++) {
394
	    long j, no_links = tdf_int();
395
	    for (j = 0; j < no_links; j++)de_linkextern(i, j);
2 7u83 396
	}
6 7u83 397
	if (dumb_mode) {
398
	    blank_line();
399
	    blank_line();
2 7u83 400
	}
401
    }
402
 
403
    /* Complete the bindings */
6 7u83 404
    complete_binding(crt_binding);
2 7u83 405
 
406
    /* Read the equations */
6 7u83 407
    n = tdf_int();
408
    if (n != no_eqn)input_error("Number of equations wrong");
409
    for (i = 0; i < no_eqn; i++) {
410
	int used = 0;
411
	char *title = null;
412
	long j, no_units = tdf_int();
413
	string se = eqn_types[i];
414
	equation_func f = find_equation(se, &title, &used);
415
	if (!used) {
416
	    title = null;
417
	    f = null;
2 7u83 418
	}
6 7u83 419
	total = 0;
420
	if (f == de_tokdef_props && no_units) {
2 7u83 421
	    /* Skip pass */
6 7u83 422
	    place pl;
423
	    int old_pf = printflag;
424
	    if (!show_skip)printflag = 0;
425
	    skipping = 1;
426
	    pl.byte = here.byte;
427
	    pl.bit = here.bit;
428
	    if (printflag && (dumb_mode || f)) {
429
		if (title && !show_stuff) {
430
		    out_string(title);
2 7u83 431
		} else {
6 7u83 432
		    out_string("EQUATIONS OF TYPE ");
433
		    out_string(se);
2 7u83 434
		}
6 7u83 435
		out(" (SKIP PASS)");
436
		blank_line();
437
		blank_lines = 1;
2 7u83 438
	    }
6 7u83 439
	    for (j = 0; j < no_units; j++)de_equation(f);
440
	    if (printflag && (dumb_mode || f)) {
441
		if (total == 0) {
442
		    out("(none)");
443
		    blank_lines = 0;
2 7u83 444
		}
6 7u83 445
		for (j = blank_lines; j < 2; j++)blank_line();
446
		blank_lines = 2;
2 7u83 447
	    }
6 7u83 448
	    total = 0;
449
	    set_place(&pl);
450
	    skipping = 0;
451
	    printflag = old_pf;
2 7u83 452
	}
453
 
454
	/* Main pass */
6 7u83 455
	if (dumb_mode || f) {
456
	    if (title && !show_stuff) {
457
		out(title);
2 7u83 458
	    } else {
6 7u83 459
		out_string("EQUATIONS OF TYPE ");
460
		out(se);
2 7u83 461
	    }
6 7u83 462
	    blank_line();
463
	    blank_lines = 1;
2 7u83 464
	}
6 7u83 465
	for (j = 0; j < no_units; j++)de_equation(f);
466
	if (dumb_mode || f) {
467
	    if (total == 0) {
468
		out("(none)");
469
		blank_lines = 0;
2 7u83 470
	    }
6 7u83 471
	    for (j = blank_lines; j < 2; j++)blank_line();
472
	    blank_lines = 2;
2 7u83 473
	}
474
    }
6 7u83 475
    return;
2 7u83 476
}