Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "calculus.h"
63
#include "code.h"
64
#include "common.h"
65
#include "output.h"
66
#include "print.h"
67
#include "suffix.h"
68
#include "type_ops.h"
69
 
70
 
71
/*
7 7u83 72
 * PRINT A PRIMITIVE PRINTING ROUTINE
73
 *
74
 * This routine prints the body of the printing routine for the current
75
 * primitive.
76
 */
2 7u83 77
 
7 7u83 78
static void
79
printer_prim(void)
2 7u83 80
{
7 7u83 81
    output("    print_indent(f_, d_);\n");
82
    output("    (void)fprintf(f_, \"%%s = \", nm_);\n");
83
    output("    OUTPUT_%PM(f_, x_);\n");
84
    output("    (void)fprintf(f_, \" ;\\n\");\n");
85
    return;
2 7u83 86
}
87
 
88
 
89
/*
7 7u83 90
 * PRINT AN ENUMERATION PRINTING ROUTINE
91
 *
92
 * This routine prints the body of the printing routine for the current
93
 * enumeration.
94
 */
2 7u83 95
 
7 7u83 96
static void
97
printer_enum(void)
2 7u83 98
{
7 7u83 99
    output("    print_indent(f_, d_);\n");
100
    output("    (void)fprintf(f_, \"%%s = \", nm_);\n");
101
    output("    switch (x_) {\n");
2 7u83 102
    LOOP_ENUM_CONST {
7 7u83 103
	output("\tcase %EM_%ES: {\n");
104
	output("\t    (void)fprintf(f_, \"%ES ;\\n\");\n");
105
	output("\t    break;\n");
106
	output("\t}\n");
2 7u83 107
    }
7 7u83 108
    output("\tdefault: {\n");
109
    output("\t    (void)fprintf(f_, \"%%lu ;\\n\",");
110
    output(" (unsigned long)x_);\n");
111
    output("\t    break;\n");
112
    output("\t}\n");
113
    output("    }\n");
114
    return;
2 7u83 115
}
116
 
117
 
118
/*
7 7u83 119
 * PRINT A STRUCTURE PRINTING ROUTINE
120
 *
121
 * This routine prints the body of the printing routine for the current
122
 * structure.
123
 */
2 7u83 124
 
7 7u83 125
static void
126
printer_struct(void)
2 7u83 127
{
7 7u83 128
    output("    print_indent(f_, d_);\n");
129
    output("    (void)fprintf(f_, \"%%s = {\\n\", nm_);\n");
2 7u83 130
    LOOP_STRUCTURE_COMPONENT {
7 7u83 131
	TYPE_P t = DEREF_ptr(cmp_type(CRT_COMPONENT));
132
	output("    PRINT_%TI(f_, x_.%CN, \"%CN\", d_ + 1);\n", t);
2 7u83 133
    }
7 7u83 134
    output("    print_indent(f_, d_);\n");
135
    output("    (void)fprintf(f_, \"}\\n\");\n");
136
    return;
2 7u83 137
}
138
 
139
 
140
/*
7 7u83 141
 * PRINT A UNION COMPONENT PRINTING ROUTINE
142
 *
143
 * This routine prints the code for printing a component of a union.
144
 * The argument d gives the token for accessing the component.
145
 */
2 7u83 146
 
7 7u83 147
static void
148
printer_component(char *d)
2 7u83 149
{
7 7u83 150
    TYPE_P t = DEREF_ptr(cmp_type(CRT_COMPONENT));
151
    output("\t\t{\n");
152
    output("\t\t    %TT z_;\n", t);
153
    output("\t\t    ");
154
    print_deref(t, d, "z_");
155
    output("\t\t    PRINT_%TI(f_, z_, \"%CN\", d_ + 1);\n", t);
156
    output("\t\t}\n");
157
    return;
2 7u83 158
}
159
 
160
 
161
/*
7 7u83 162
 * PRINT A UNION PRINTING ROUTINE
163
 *
164
 * This routine prints the body of the printing routine for the current
165
 * primitive.
166
 */
2 7u83 167
 
7 7u83 168
static void
169
printer_union(void)
2 7u83 170
{
7 7u83 171
    output("    print_indent(f_, d_);\n");
172
    output("    if (IS_NULL_%UM(x_)) {\n");
173
    output("\t(void)fprintf(f_, \"%%s = NULL_%UM ;\\n\", nm_);\n");
174
    output("    } else {\n");
175
    output("\tswitch (TAG_%UM(x_)) {\n");
2 7u83 176
    LOOP_UNION_FIELD {
7 7u83 177
	int al = DEREF_int(fld_flag(CRT_FIELD));
178
	output("\t    case %UM_%FN_tag: {\n");
179
	if (al) {
180
	    output("\t\tunsigned alias_ = GET_ALIAS_%UM_%FN(x_);\n");
181
	    output("\t\tif (alias_) {\n");
182
	    output("\t\t    (void)fprintf(f_, \"%%s = ");
183
	    output("[%%u] ;\\n\", nm_, alias_);\n");
184
	    output("\t\t    break;\n");
185
	    output("\t\t}\n");
186
	    output("\t\talias_ = ++crt_%X_alias ;\n");
187
	    output("\t\tSET_ALIAS_%UM_%FN(x_, alias_);\n");
188
	    output("\t\t(void)fprintf(f_, \"%%s = [%%u] = {\\n\", ");
189
	    output("nm_, alias_);\n");
2 7u83 190
	} else {
7 7u83 191
	    output("\t\t(void)fprintf(f_, \"%%s = {\\n\", nm_);\n");
2 7u83 192
	}
7 7u83 193
	output("\t\tprint_indent(f_, d_ + 1);\n");
194
	output("\t\t(void)fprintf(f_, \"(tag) = %FN ;\\n\");\n");
195
	LOOP_UNION_COMPONENT printer_component("%UM_%CN(x_)");
196
	LOOP_FIELD_COMPONENT printer_component("%UM_%FN_%CN(x_)");
197
	output("\t\tprint_indent(f_, d_);\n");
198
	output("\t\t(void)fprintf(f_, \"}\\n\");\n");
199
	output("\t\tbreak;\n");
200
	output("\t    }\n");
2 7u83 201
    }
7 7u83 202
    output("\t    default : {\n");
203
    output("\t\t(void)fprintf(f_, \"%%s = ERROR!\\n\", nm_);\n");
204
    output("\t\tbreak;\n");
205
    output("\t    }\n");
206
    output("\t}\n");
207
    output("    }\n");
208
    return;
2 7u83 209
}
210
 
211
 
212
/*
7 7u83 213
 * PRINT A POINTER PRINTING ROUTINE
214
 *
215
 * This routine prints the body of the printing routine for a pointer
216
 * to s.
217
 */
2 7u83 218
 
7 7u83 219
static void
220
printer_ptr(TYPE_P s, char *ptr, char *nm, char *i)
2 7u83 221
{
7 7u83 222
    output("%s    print_indent(f_, d_);\n", i);
223
    output("%s    if (IS_NULL_ptr(%s)) {\n", i, nm);
224
    output("%s\t(void)fprintf(f_, ", i);
225
    output("\"%%s = NULL_ptr;\\n\", nm_);\n");
226
    output("%s    } else if (d_ < print_ptr_depth) {\n", i);
227
    output("%s\t%TT z_;\n", i, s);
228
    output("%s\t", i);
229
    print_deref(s, nm, "z_");
230
    output("%s\t(void)fprintf(f_, ", i);
231
    output("\"%%s = %s {\\n\", nm_);\n", ptr);
232
    output("%s\tPRINT_%TI(f_, z_, \"(%TI)\", d_ + 1);\n", i, s, s);
233
    output("%s\tprint_indent(f_, d_);\n", i);
234
    output("%s\t(void)fprintf(f_, \"}\\n\");\n", i);
235
    output("%s    } else {\n", i);
236
    output("%s\t(void)fprintf(f_, \"%%s = ", i);
237
    output("0x%%p ;\\n\", nm_, VOIDSTAR_ptr(%s));\n", nm);
238
    output("%s    }\n", i);
239
    return;
2 7u83 240
}
241
 
242
 
243
/*
7 7u83 244
 * PRINT A LIST PRINTING ROUTINE
245
 *
246
 * This routine prints the body of the printing routine for a list
247
 * of s named nm.
248
 */
2 7u83 249
 
7 7u83 250
static void
251
printer_list(TYPE_P s, char *nm)
2 7u83 252
{
7 7u83 253
    output("    print_indent(f_, d_);\n");
254
    output("    (void)fprintf(f_, \"%%s = LIST {\\n\", nm_);\n");
255
    output("    while (!IS_NULL_list(%s)) {\n", nm);
256
    output("\t%TT z_;\n", s);
257
    output("\tUN_CONS_%TM(z_, %s, %s);\n", s, nm, nm);
258
    output("\tPRINT_%TI(f_, z_, \"(%TI)\", d_ + 1);\n", s, s);
259
    output("\tif (!print_list_expand && !IS_NULL_list(%s)) {\n", nm);
260
    output("\t    print_indent(f_, d_ + 1);\n");
261
    output("\t    (void)fprintf(f_, \"(tail) = ");
262
    output("0x%%p ;\\n\", VOIDSTAR_list(%s));\n", nm);
263
    output("\t    break;\n");
264
    output("\t}\n");
265
    output("    }\n");
266
    output("    print_indent(f_, d_);\n");
267
    output("    (void)fprintf(f_, \"}\\n\");\n");
268
    return;
2 7u83 269
}
270
 
271
 
272
/*
7 7u83 273
 * PRINT A VECTOR PRINTING ROUTINE
274
 *
275
 * This routine prints the body of the printing routine for a vector
276
 * of s.
277
 */
2 7u83 278
 
7 7u83 279
static void
280
printer_vec(TYPE_P s)
2 7u83 281
{
7 7u83 282
    output("    print_indent(f_, d_);\n");
283
    output("    {\n");
284
    output("\t%X_dim n = DIM_vec(x_);\n");
285
    output("\tPTR(%TT) y_ = PTR_vec_ptr(VEC_PTR_vec(x_));\n", s);
286
    output("\t(void)fprintf(f_, \"%%s = {\\n\", nm_);\n");
287
    output("\twhile (n--) {\n");
288
    printer_ptr(s, "VEC", "y_", "\t");
289
    output("\t    y_ = STEP_ptr(y_, SIZE_%TM);\n", s);
290
    output("\t}\n");
291
    output("\tprint_indent(f_, d_);\n");
292
    output("\t(void)fprintf(f_, \"}\\n\");\n");
293
    output("    }\n");
294
    return;
2 7u83 295
}
296
 
297
 
298
/*
7 7u83 299
 * PRINT A VECTOR POINTER PRINTING ROUTINE
300
 *
301
 * This routine prints the body of the printing routine for a vector
302
 * pointer to s.
303
 */
2 7u83 304
 
7 7u83 305
static void
306
printer_vec_ptr(TYPE_P s)
2 7u83 307
{
7 7u83 308
    output("    PTR(%TT) y_ = PTR_vec_ptr(x_);\n", s);
309
    printer_ptr(s, "VEC_PTR", "y_", "");
310
    return;
2 7u83 311
}
312
 
313
 
314
/*
7 7u83 315
 * PRINT ALL PRINTING ROUTINES
316
 *
317
 * This routine prints all the printing routines.
318
 */
2 7u83 319
 
7 7u83 320
void
321
print_action(char *dir)
2 7u83 322
{
7 7u83 323
    open_file(dir, PRINT_PREFIX, DEF_SUFFIX);
324
    print_include();
2 7u83 325
 
7 7u83 326
    comment("Printing function declarations");
2 7u83 327
    LOOP_TYPE {
7 7u83 328
	TYPE_P t = CRT_TYPE;
329
	if (is_identity_type(t)) {
330
	    output("#ifndef PRINT_%TI\n", t);
331
	    output("#define PRINT_%TI(A, B, C, D)", t);
332
	    output("PRINT_%TJ((A), (B), (C), (D))\n", t);
333
	    output("#endif\n\n");
2 7u83 334
	} else {
7 7u83 335
	    output("#ifndef PRINT_%TI\n", t);
336
	    output("static void PRINT_%TI ", t);
337
	    output("(FILE *, %TT, char *, int);\n", t);
338
	    output("#endif\n\n");
2 7u83 339
	}
340
    }
7 7u83 341
    output("\n\n");
2 7u83 342
 
7 7u83 343
    comment("Printing variables");
344
    output("static int print_indent_step = 4;\n");
345
    output("static int print_ptr_depth = 1;\n");
346
    output("static int print_list_expand = 0;\n\n\n");
2 7u83 347
 
7 7u83 348
    comment("Printing indentation routine");
349
    output("static void print_indent\n");
350
    output("\n");
351
    output("(FILE *f, int d)\n");
352
    output("{\n");
353
    output("    int i = print_indent_step * d;\n");
354
    output("    while (i--) (void)fputc(' ', f);\n");
355
    output("    return;\n");
356
    output("}\n\n\n");
2 7u83 357
 
358
    /* Function definitions */
359
    LOOP_TYPE {
7 7u83 360
	TYPE_P t = CRT_TYPE;
361
	TYPE t0 = DEREF_type(t);
362
	unsigned tag = TAG_type(t0);
363
	if (!is_identity_type(t)) {
2 7u83 364
	    /* Function header */
7 7u83 365
	    int is_struct = 0;
366
	    output("/* Printing routines for %TT */\n\n", t);
367
	    output("#ifndef PRINT_%TI\n\n", t);
368
	    output("static void\n");
369
	    output("PRINT_%TI(FILE *f_, %TT x_, char *nm_, int d_)\n", t, t);
370
	    output("{\n");
2 7u83 371
 
372
	    /* Function body */
7 7u83 373
	    switch (tag) {
374
		case type_primitive_tag: {
375
		    PRIMITIVE_P p = DEREF_ptr(type_primitive_prim(t0));
2 7u83 376
		    LOOP_PRIMITIVE {
7 7u83 377
			if (EQ_ptr(CRT_PRIMITIVE, p)) {
378
			    printer_prim();
379
			    break;
2 7u83 380
			}
381
		    }
7 7u83 382
		    break;
2 7u83 383
		}
7 7u83 384
		case type_enumeration_tag: {
385
		    ENUM_P p = DEREF_ptr(type_enumeration_en(t0));
2 7u83 386
		    LOOP_ENUM {
7 7u83 387
			if (EQ_ptr(CRT_ENUM, p)) {
388
			    printer_enum();
389
			    break;
2 7u83 390
			}
391
		    }
7 7u83 392
		    break;
2 7u83 393
		}
7 7u83 394
		case type_structure_tag: {
395
		    STRUCTURE_P p = DEREF_ptr(type_structure_struc(t0));
2 7u83 396
		    LOOP_STRUCTURE {
7 7u83 397
			if (EQ_ptr(CRT_STRUCTURE, p)) {
398
			    printer_struct();
399
			    break;
2 7u83 400
			}
401
		    }
7 7u83 402
		    is_struct = 1;
403
		    break;
2 7u83 404
		}
7 7u83 405
		case type_onion_tag: {
406
		    UNION_P p = DEREF_ptr(type_onion_un(t0));
2 7u83 407
		    LOOP_UNION {
7 7u83 408
			if (EQ_ptr(CRT_UNION, p)) {
409
			    printer_union();
410
			    break;
2 7u83 411
			}
412
		    }
7 7u83 413
		    break;
2 7u83 414
		}
7 7u83 415
		case type_ptr_tag: {
416
		    TYPE_P s = DEREF_ptr(type_ptr_sub(t0));
417
		    printer_ptr(s, "PTR", "x_", "");
418
		    break;
2 7u83 419
		}
7 7u83 420
		case type_list_tag: {
421
		    TYPE_P s = DEREF_ptr(type_list_sub(t0));
422
		    printer_list(s, "x_");
423
		    break;
2 7u83 424
		}
7 7u83 425
		case type_stack_tag: {
426
		    TYPE_P s = DEREF_ptr(type_stack_sub(t0));
427
		    output("    LIST(%TT) y_ = LIST_stack(x_);\n", s);
428
		    printer_list(s, "y_");
429
		    break;
2 7u83 430
		}
7 7u83 431
		case type_vec_tag: {
432
		    TYPE_P s = DEREF_ptr(type_vec_sub(t0));
433
		    printer_vec(s);
434
		    is_struct = 1;
435
		    break;
2 7u83 436
		}
7 7u83 437
		case type_vec_ptr_tag: {
438
		    TYPE_P s = DEREF_ptr(type_vec_ptr_sub(t0));
439
		    printer_vec_ptr(s);
440
		    is_struct = 1;
441
		    break;
2 7u83 442
		}
443
	    }
444
 
445
	    /* Function trailer */
7 7u83 446
	    output("    return;\n");
447
	    output("}\n\n");
2 7u83 448
 
449
	    /* Debugging routine */
7 7u83 450
	    if (extra_asserts) {
451
		char *star = (is_struct ? "*" : "");
452
		output("#ifdef DEBUG\n\n");
453
		output("void\n");
454
		output("DEBUG_%TI(%TT %sx_)\n", t, t, star);
455
		output("{\n    ");
456
		if (is_struct) {
457
			output("if (x_)");
458
		}
459
		output("PRINT_%TI(stdout, %sx_, ", t, star);
460
		output("\"%TI\", 0);\n", t);
461
		output("    return;\n");
462
		output("}\n\n");
463
		output("#endif\n\n");
2 7u83 464
	    }
7 7u83 465
	    output("#endif\n\n\n");
2 7u83 466
	}
467
    }
468
 
7 7u83 469
    close_file();
470
    return;
2 7u83 471
}