Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "calculus.h"
33
#include "code.h"
34
#include "error.h"
35
#include "common.h"
36
#include "lex.h"
37
#include "output.h"
38
#include "suffix.h"
39
#include "token.h"
40
#include "type_ops.h"
41
 
42
 
43
/*
44
    PRINT SIMPLE TOKENS
45
 
46
    This routine prints the simple tokens for a type named nm with short
47
    name ns.  e is true for simply dereferenced types.
48
*/
49
 
50
static void print_simple_tok
51
    PROTO_N ( ( nm, ns, e, lst ) )
52
    PROTO_T ( char *nm X char *ns X int e X int lst )
53
{
54
    /* SIZE token */
55
    output ( "%pt %xc : SIZE ( %e ) : SIZE_%e #\n\n", nm, ns ) ;
56
 
57
    /* Assign token */
58
    output ( "%pt PROC (\\\n" ) ;
59
    output ( "\t%xr : PTR ( %e ) :,\\\n\t%xr : %e :\\\n    ) ", nm, nm ) ;
60
    output ( e ? "%xr : void :" : "STATEMENT" ) ;
61
    output ( " COPY_%e #\n\n", ns ) ;
62
 
63
    /* Dereference token */
64
    output ( "%pt PROC (\\\n" ) ;
65
    output ( "\t%xr : PTR ( %e ) :", nm ) ;
66
    if ( e ) {
67
	output ( "\\\n    ) %xr : %e :", nm ) ;
68
    } else {
69
	output ( ",\\\n\t%xl : %e :\\\n    ) STATEMENT", nm ) ;
70
    }
71
    output ( " DEREF_%e #\n\n", ns ) ;
72
 
73
    if ( lst ) {
74
	/* CONS token */
75
	output ( "%pt PROC (\\\n" ) ;
76
	output ( "\t%xr : %e :,\\\n\t%xr : LIST ( %e ) :,\\\n", nm, nm ) ;
77
	output ( "\t%xl : LIST ( %e ) :\\\n", nm ) ;
78
	output ( "    ) STATEMENT CONS_%e #\n\n", ns ) ;
79
 
80
	/* UN_CONS token */
81
	output ( "%pt PROC (\\\n" ) ;
82
	output ( "\t%xl : %e :,\\\n", nm ) ;
83
	output ( "\t%xl : LIST ( %e ) :,\\\n", nm ) ;
84
	output ( "\t%xr : LIST ( %e ) :\\\n", nm ) ;
85
	output ( "    ) STATEMENT UN_CONS_%e #\n\n", ns ) ;
86
 
87
	/* DESTROY_CONS token */
88
	output ( "%pt PROC (\\\n" ) ;
89
	output ( "\t%xr : DESTROYER :,\\\n\t%xl : %e :,\\\n", nm ) ;
90
	output ( "\t%xl : LIST ( %e ) :,\\\n", nm ) ;
91
	output ( "\t%xr : LIST ( %e ) :\\\n", nm ) ;
92
	output ( "    ) STATEMENT DESTROY_CONS_%e #\n\n", ns ) ;
93
 
94
	if ( allow_stack ) {
95
	    /* PUSH token */
96
	    output ( "%pt PROC (\\\n" ) ;
97
	    output ( "\t%xr : %e :,\\\n", nm ) ;
98
	    output ( "\t%xl : STACK ( %e ) :\\\n", nm ) ;
99
	    output ( "    ) STATEMENT PUSH_%e #\n\n", ns ) ;
100
 
101
	    /* POP token */
102
	    output ( "%pt PROC (\\\n" ) ;
103
	    output ( "\t%xl : %e :,\\\n", nm ) ;
104
	    output ( "\t%xl : STACK ( %e ) :\\\n", nm ) ;
105
	    output ( "    ) STATEMENT POP_%e #\n\n", ns ) ;
106
	}
107
    }
108
 
109
    /* Interface commands */
110
    output ( "%pi SIZE_%e COPY_%e DEREF_%e\n", ns, ns, ns ) ;
111
    if ( lst ) {
112
	output ( "%pi CONS_%e UN_CONS_%e DESTROY_CONS_%e\n", ns, ns, ns ) ;
113
	if ( allow_stack ) output ( "%pi PUSH_%e POP_%e\n", ns, ns ) ;
114
    }
115
    output ( "\n\n" ) ;
116
    return ;
117
}
118
 
119
 
120
/*
121
    PRINT SIMPLE TOKENS FOR TYPE OPERATIONS
122
 
123
    This routine prints the simple tokens for the type operation op.
124
    The tokens are named using nm.  e is true for simply dereferenced types.
125
*/
126
 
127
static void print_type_ops_tok
128
    PROTO_N ( ( op, nm, e ) )
129
    PROTO_T ( char *op X char *nm X int e )
130
{
131
    /* Size token */
132
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
133
    output ( "    ) %xc : SIZE ( %s ( t ) ) : SIZE_%s #\n\n", op, nm ) ;
134
 
135
    /* Assign token */
136
    output ( "%pt PROC {\\\n" ) ;
137
    output ( "\tTYPE t, %xr : PTR ( %s ( t ) ) : e1,\\\n", op ) ;
138
    output ( "\t%xr : %s ( t ) : e2 |\\\n", op ) ;
139
    output ( "\tEXP e1, EXP e2\\\n    } " ) ;
140
    output ( e ? "%xr : void :" : "STATEMENT" ) ;
141
    output ( " COPY_%s #\n\n", nm ) ;
142
 
143
    /* Dereference token */
144
    output ( "%pt PROC {\\\n" ) ;
145
    output ( "\tTYPE t, %xr : PTR ( %s ( t ) ) : e", op ) ;
146
    if ( e ) {
147
	output ( " |\\\n\tEXP e\\\n" ) ;
148
	output ( "    } %xr : %s ( t ) : ", op ) ;
149
    } else {
150
	output ( "1,\\\n\t%xl : %s ( t ) : e2 |\\\n", op ) ;
151
	output ( "\tEXP e1, EXP e2\\\n" ) ;
152
	output ( "    } STATEMENT " ) ;
153
    }
154
    output ( "DEREF_%s #\n\n", nm ) ;
155
 
156
    /* CONS token */
157
    output ( "%pt PROC {\\\n" ) ;
158
    output ( "\tTYPE t, %xr : %s ( t ) : e2,\\\n", op ) ;
159
    output ( "\t%xr : LIST ( %s ( t ) ) : e3,\\\n", op ) ;
160
    output ( "\t%xl : LIST ( %s ( t ) ) : e4 |\\\n", op ) ;
161
    output ( "\tEXP e2, EXP e3, EXP e4\\\n" ) ;
162
    output ( "    } STATEMENT CONS_%s #\n\n", nm ) ;
163
 
164
    /* UN_CONS token */
165
    output ( "%pt PROC {\\\n" ) ;
166
    output ( "\tTYPE t, %xl : %s ( t ) : e2,\\\n", op ) ;
167
    output ( "\t%xl : LIST ( %s ( t ) ) : e3,\\\n", op ) ;
168
    output ( "\t%xr : LIST ( %s ( t ) ) : e4 |\\\n", op ) ;
169
    output ( "\tEXP e2, EXP e3, EXP e4\\\n" ) ;
170
    output ( "    } STATEMENT UN_CONS_%s #\n\n", nm ) ;
171
 
172
    /* DESTROY_CONS token */
173
    output ( "%pt PROC {\\\n" ) ;
174
    output ( "\tTYPE t, %xr : DESTROYER : e1,\\\n" ) ;
175
    output ( "\t%xl : %s ( t ) : e2,\\\n", op ) ;
176
    output ( "\t%xl : LIST ( %s ( t ) ) : e3,\\\n", op ) ;
177
    output ( "\t%xr : LIST ( %s ( t ) ) : e4 |\\\n", op ) ;
178
    output ( "\tEXP e1, EXP e2, EXP e3, EXP e4\\\n" ) ;
179
    output ( "    } STATEMENT DESTROY_CONS_%s #\n\n", nm ) ;
180
 
181
    if ( allow_stack ) {
182
	/* PUSH token */
183
	output ( "%pt PROC {\\\n" ) ;
184
	output ( "\tTYPE t, %xr : %s ( t ) : e2,\\\n", op ) ;
185
	output ( "\t%xl : STACK ( %s ( t ) ) : e3 |\\\n", op ) ;
186
	output ( "\tEXP e2, EXP e3\\\n" ) ;
187
	output ( "    } STATEMENT PUSH_%s #\n\n", nm ) ;
188
 
189
	/* POP token */
190
	output ( "%pt PROC {\\\n" ) ;
191
	output ( "\tTYPE t, %xl : %s ( t ) : e2,\\\n", op ) ;
192
	output ( "\t%xl : STACK ( %s ( t ) ) : e3 |\\\n", op ) ;
193
	output ( "\tEXP e2, EXP e3\\\n" ) ;
194
	output ( "    } STATEMENT POP_%s #\n\n", nm ) ;
195
    }
196
 
197
    /* Interface commands */
198
    output ( "%pi SIZE_%s COPY_%s DEREF_%s\n", nm, nm, nm ) ;
199
    output ( "%pi CONS_%s UN_CONS_%s DESTROY_CONS_%s\n", nm, nm, nm ) ;
200
    if ( allow_stack ) output ( "%pi PUSH_%s POP_%s\n", nm, nm ) ;
201
    output ( "\n\n" ) ;
202
    return ;
203
}
204
 
205
 
206
/*
207
    PRINT BASIC TYPES (TOKEN VERSION)
208
 
209
    This routine prints the token versions of the basic type definitions.
210
*/
211
 
212
static void print_types_tok
213
    PROTO_Z ()
214
{
215
    comment ( "Primitive types" ) ;
216
    LOOP_PRIMITIVE {
217
	CLASS_ID_P c = DEREF_ptr ( prim_id ( CRT_PRIMITIVE ) ) ;
218
	char *pn = DEREF_string ( cid_name ( c ) ) ;
219
	char *pd = DEREF_string ( prim_defn ( CRT_PRIMITIVE ) ) ;
220
	if ( !streq ( pn, pd ) ) output ( "typedef %PD %PN ;\n" ) ;
221
    }
222
    output ( "\n\n" ) ;
223
 
224
    comment ( "Basic types" ) ;
225
    output ( "#ifndef %X_DESTR_DEFINED\n" ) ;
226
    output ( "#define %X_DESTR_DEFINED\n" ) ;
227
    output ( "typedef void ( *DESTROYER ) () ;\n" ) ;
228
    output ( "#endif\n\n" ) ;
229
    output ( "%pt PROC ( TYPE ) TYPE PTR #\n" ) ;
230
    output ( "%pt PROC ( TYPE ) TYPE LIST #\n" ) ;
231
    if ( allow_stack ) {
232
	output ( "%pt PROC ( TYPE ) TYPE STACK #\n" ) ;
233
    }
234
    if ( allow_vec ) {
235
	output ( "%pt VARIETY %X_dim #\n" ) ;
236
	output ( "%pt PROC ( TYPE ) TYPE VEC #\n" ) ;
237
	output ( "%pt PROC ( TYPE ) TYPE VEC_PTR #\n" ) ;
238
    }
239
    output ( "%pt PROC ( TYPE ) TYPE SIZE #\n\n" ) ;
240
    output ( "%pi PTR LIST " ) ;
241
    if ( allow_stack ) output ( "STACK " ) ;
242
    if ( allow_vec ) output ( "%X_dim VEC VEC_PTR " ) ;
243
    output ( "SIZE\n\n\n" ) ;
244
 
245
    comment ( "Enumeration type definitions" ) ;
246
    LOOP_ENUM {
247
	output ( "%pt VARIETY %EN #\n" ) ;
248
	output ( "%pi %EN\n" ) ;
249
    }
250
    output ( "\n\n" ) ;
251
 
252
    comment ( "Union type definitions" ) ;
253
    LOOP_UNION {
254
	output ( "%pt TYPE %UN #\n" ) ;
255
	output ( "%pi %UN\n" ) ;
256
    }
257
    output ( "\n\n" ) ;
258
 
259
    print_struct_defn () ;
260
 
261
    comment ( "Function declarations" ) ;
262
    output ( "extern void destroy_%X () ;\n" ) ;
263
    output ( "extern void dummy_destroy_%X () ;\n" ) ;
264
    output ( "#ifdef %X_IO_ROUTINES\n" ) ;
265
    output ( "extern unsigned crt_%X_alias ;\n" ) ;
266
    output ( "extern void clear_%X_alias PROTO_S ( ( void ) ) ;\n" ) ;
267
    output ( "#endif\n" ) ;
268
    output ( "\n\n" ) ;
269
    return ;
270
}
271
 
272
 
273
/*
274
    PRINT POINTER CONSTRUCTS (TOKEN VERSION)
275
 
276
    This routine prints the token versions of the pointer constructs.
277
*/
278
 
279
static void print_ptr_tok
280
    PROTO_Z ()
281
{
282
    comment ( "Pointer token specifications" ) ;
283
    output ( "%pt PROC {\\\n" ) ;
284
    output ( "\tTYPE t, %xr : PTR ( t ) : e1, %xr : SIZE ( t ) : e2 |\\\n" ) ;
285
    output ( "\tEXP e1, EXP e2\\\n" ) ;
286
    output ( "    } %xr : PTR ( t ) : STEP_ptr #\n\n" ) ;
287
 
288
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
289
    output ( "    ) %xc : PTR ( t ) : NULL_ptr #\n\n" ) ;
290
 
291
    output ( "%pt PROC {\\\n" ) ;
292
    output ( "\tTYPE t, %xr : PTR ( t ) : e |\\\n" ) ;
293
    output ( "\tEXP e\\\n" ) ;
294
    output ( "    } %xr : int : IS_NULL_ptr #\n\n" ) ;
295
 
296
    output ( "%pt PROC {\\\n" ) ;
297
    output ( "\tTYPE t, %xr : PTR ( t ) : e1, %xr : PTR ( t ) : e2 |\\\n" ) ;
298
    output ( "\tEXP e1, EXP e2\\\n" ) ;
299
    output ( "    } %xr : int : EQ_ptr #\n\n" ) ;
300
 
301
    output ( "%pt PROC {\\\n" ) ;
302
    output ( "\tTYPE t, %xr : SIZE ( t ) : e |\\\n" ) ;
303
    output ( "\tEXP e\\\n" ) ;
304
    output ( "    } %xr : PTR ( t ) : MAKE_ptr #\n\n" ) ;
305
 
306
    output ( "%pt PROC {\\\n" ) ;
307
    output ( "\tTYPE t, %xr : PTR ( t ) : e1, %xr : SIZE ( t ) : e2 |\\\n" ) ;
308
    output ( "\tEXP e1, EXP e2\\\n" ) ;
309
    output ( "    } %xr : void : DESTROY_ptr #\n\n" ) ;
310
 
311
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
312
    output ( "    ) %xr : PTR ( t ) : UNIQ_ptr #\n\n" ) ;
313
 
314
    output ( "%pt PROC {\\\n" ) ;
315
    output ( "\tTYPE t, %xr : PTR ( t ) : e |\\\n" ) ;
316
    output ( "\tEXP e\\\n" ) ;
317
    output ( "    } %xr : void : DESTROY_UNIQ_ptr #\n\n" ) ;
318
 
319
    output ( "%pi STEP_ptr NULL_ptr IS_NULL_ptr EQ_ptr\n" ) ;
320
    output ( "%pi MAKE_ptr DESTROY_ptr\n\n" ) ;
321
 
322
    output ( "#ifdef %X_IO_ROUTINES\n" ) ;
323
    output ( "%pt PROC {\\\n" ) ;
324
    output ( "\tTYPE t, %xr : PTR ( t ) : e |\\\n" ) ;
325
    output ( "\tEXP e\\\n" ) ;
326
    output ( "    } %xr : void * : VOIDSTAR_ptr #\n" ) ;
327
    output ( "%pi VOIDSTAR_ptr\n" ) ;
328
    output ( "#endif\n\n" ) ;
329
 
330
    print_type_ops_tok ( "PTR", "ptr", 1 ) ;
331
    return ;
332
}
333
 
334
 
335
/*
336
    PRINT LIST CONSTRUCTS (TOKEN VERSION)
337
 
338
    This routine prints the token versions of the list constructs.
339
*/
340
 
341
static void print_list_tok
342
    PROTO_Z ()
343
{
344
    comment ( "List token specifications" ) ;
345
    output ( "%pt PROC {\\\n" ) ;
346
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
347
    output ( "\tEXP e\\\n" ) ;
348
    output ( "    } %xr : PTR ( t ) : HEAD_list #\n\n" ) ;
349
 
350
    output ( "%pt PROC {\\\n" ) ;
351
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
352
    output ( "\tEXP e\\\n" ) ;
353
    output ( "    } %xr : PTR ( LIST ( t ) ) : PTR_TAIL_list #\n\n" ) ;
354
 
355
    output ( "%pt PROC {\\\n" ) ;
356
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
357
    output ( "\tEXP e\\\n" ) ;
358
    output ( "    } %xr : LIST ( t ) : TAIL_list #\n\n" ) ;
359
 
360
    output ( "%pt PROC {\\\n" ) ;
361
    output ( "\tTYPE t, %xr : LIST ( t ) : e1 |\\\n" ) ;
362
    output ( "\tEXP e1\\\n" ) ;
363
    output ( "    } %xr : unsigned : LENGTH_list #\n\n" ) ;
364
 
365
    output ( "%pt PROC {\\\n" ) ;
366
    output ( "\tTYPE t, %xr : LIST ( t ) : e1 |\\\n" ) ;
367
    output ( "\tEXP e1\\\n" ) ;
368
    output ( "    } %xr : LIST ( t ) : END_list #\n\n" ) ;
369
 
370
    output ( "%pt PROC {\\\n" ) ;
371
    output ( "\tTYPE t, %xr : LIST ( t ) : e1 |\\\n" ) ;
372
    output ( "\tEXP e1\\\n" ) ;
373
    output ( "    } %xr : LIST ( t ) : REVERSE_list #\n\n" ) ;
374
 
375
    output ( "%pt PROC {\\\n" ) ;
376
    output ( "\tTYPE t, %xr : LIST ( t ) : e1, %xr : LIST ( t ) : e2 |\\\n" ) ;
377
    output ( "\tEXP e1, EXP e2\\\n" ) ;
378
    output ( "    } %xr : LIST ( t ) : APPEND_list #\n\n" ) ;
379
 
380
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
381
    output ( "    ) %xc : LIST ( t ) : NULL_list #\n\n" ) ;
382
 
383
    output ( "%pt PROC {\\\n" ) ;
384
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
385
    output ( "\tEXP e\\\n" ) ;
386
    output ( "    } %xr : int : IS_NULL_list #\n\n" ) ;
387
 
388
    output ( "%pt PROC {\\\n" ) ;
389
    output ( "\tTYPE t, %xr : LIST ( t ) : e1, %xr : LIST ( t ) : e2 |\\\n" ) ;
390
    output ( "\tEXP e1, EXP e2\\\n" ) ;
391
    output ( "    } %xr : int : EQ_list #\n\n" ) ;
392
 
393
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
394
    output ( "    ) %xr : LIST ( t ) : UNIQ_list #\n\n" ) ;
395
 
396
    output ( "%pt PROC {\\\n" ) ;
397
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
398
    output ( "\tEXP e\\\n" ) ;
399
    output ( "    } %xr : void : DESTROY_UNIQ_list #\n\n" ) ;
400
 
401
    output ( "%pt PROC {\\\n" ) ;
402
    output ( "\tTYPE t, %xr : LIST ( t ) : e1, %xr : SIZE ( t ) : e2 |\\\n" ) ;
403
    output ( "\tEXP e1, EXP e2\\\n" ) ;
404
    output ( "    } STATEMENT DESTROY_list #\n\n" ) ;
405
 
406
    output ( "%pi HEAD_list PTR_TAIL_list TAIL_list\n" ) ;
407
    output ( "%pi LENGTH_list END_list REVERSE_list APPEND_list\n" ) ;
408
    output ( "%pi NULL_list IS_NULL_list EQ_list\n" ) ;
409
    output ( "%pi UNIQ_list DESTROY_UNIQ_list DESTROY_list\n\n" ) ;
410
 
411
    output ( "#ifdef %X_IO_ROUTINES\n" ) ;
412
    output ( "%pt PROC {\\\n" ) ;
413
    output ( "\tTYPE t, %xr : LIST ( t ) : e |\\\n" ) ;
414
    output ( "\tEXP e\\\n" ) ;
415
    output ( "    } %xr : void * : VOIDSTAR_list #\n" ) ;
416
    output ( "%pi VOIDSTAR_list\n" ) ;
417
    output ( "#endif\n\n" ) ;
418
 
419
    print_type_ops_tok ( "LIST", "list", 1 ) ;
420
    return ;
421
}
422
 
423
 
424
/*
425
    PRINT STACK CONSTRUCTS (TOKEN VERSION)
426
 
427
    This routine prints the token versions of the stack constructs.
428
*/
429
 
430
static void print_stack_tok
431
    PROTO_Z ()
432
{
433
    comment ( "Stack token specifications" ) ;
434
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
435
    output ( "    ) %xc : STACK ( t ) : NULL_stack #\n\n" ) ;
436
 
437
    output ( "%pt PROC {\\\n" ) ;
438
    output ( "\tTYPE t, %xr : STACK ( t ) : e |\\\n" ) ;
439
    output ( "\tEXP e\\\n" ) ;
440
    output ( "    } %xr : int : IS_NULL_stack #\n\n" ) ;
441
 
442
    output ( "%pt PROC {\\\n" ) ;
443
    output ( "\tTYPE t, %xr : STACK ( t ) : e1 |\\\n" ) ;
444
    output ( "\tEXP e1\\\n" ) ;
445
    output ( "    } %xr : LIST ( t ) : LIST_stack #\n\n" ) ;
446
 
447
    output ( "%pt PROC {\\\n" ) ;
448
    output ( "\tTYPE t, %xr : LIST ( t ) : e1 |\\\n" ) ;
449
    output ( "\tEXP e1\\\n" ) ;
450
    output ( "    } %xr : STACK ( t ) : STACK_list #\n\n" ) ;
451
 
452
    output ( "%pi NULL_stack IS_NULL_stack LIST_stack STACK_list\n\n" ) ;
453
 
454
    print_type_ops_tok ( "STACK", "stack", 1 ) ;
455
    return ;
456
}
457
 
458
 
459
/*
460
    PRINT VECTOR CONSTRUCTS (TOKEN VERSION)
461
 
462
    This routine prints the token versions of the vector constructs.
463
*/
464
 
465
static void print_vec_tok
466
    PROTO_Z ()
467
{
468
    comment ( "Vector token specifications" ) ;
469
    output ( "%pt PROC {\\\n" ) ;
470
    output ( "\tTYPE t, %xr : VEC ( t ) : e |\\\n" ) ;
471
    output ( "\tEXP e\\\n" ) ;
472
    output ( "    } %xr : %X_dim : DIM_vec #\n\n" ) ;
473
 
474
    output ( "%pt PROC {\\\n" ) ;
475
    output ( "\tTYPE t, %xr : PTR ( VEC ( t ) ) : e |\\\n" ) ;
476
    output ( "\tEXP e\\\n" ) ;
477
    output ( "    } %xr : PTR ( t ) : PTR_ptr_vec #\n\n" ) ;
478
 
479
    output ( "%pt PROC {\\\n" ) ;
480
    output ( "\tTYPE t, %xr : PTR ( VEC ( t ) ) : e |\\\n" ) ;
481
    output ( "\tEXP e\\\n" ) ;
482
    output ( "    } %xr : %X_dim : DIM_ptr_vec #\n\n" ) ;
483
 
484
    output ( "%pt PROC (\\\n\tTYPE t\\\n" ) ;
485
    output ( "    ) %xr : VEC ( t ) : NULL_vec #\n\n" ) ;
486
 
487
    output ( "%pt PROC {\\\n" ) ;
488
    output ( "\tTYPE t, %xr : SIZE ( t ) : e1,\\\n" ) ;
489
    output ( "\t%xr : %X_dim : e2,\\\n" ) ;
490
    output ( "\t%xl : VEC ( t ) : e3 |\\\n" ) ;
491
    output ( "\tEXP e1, EXP e2, EXP e3\\\n" ) ;
492
    output ( "    } STATEMENT MAKE_vec #\n\n" ) ;
493
 
494
    output ( "%pt PROC {\\\n" ) ;
495
    output ( "\tTYPE t, %xr : VEC ( t ) : e1, %xr : SIZE ( t ) : e2 |\\\n" ) ;
496
    output ( "\tEXP e1, EXP e2\\\n" ) ;
497
    output ( "    } STATEMENT DESTROY_vec #\n\n" ) ;
498
 
499
    output ( "%pt PROC {\\\n" ) ;
500
    output ( "\tTYPE t, %xr : VEC ( t ) : e1,\\\n" ) ;
501
    output ( "\t%xr : SIZE ( t ) : e2, %xr : int : e3,\\\n" ) ;
502
    output ( "\t%xr : int : e4, %xl : VEC ( t ) : e5 |\\\n" ) ;
503
    output ( "\tEXP e1, EXP e2, EXP e3, EXP e4, EXP e5\\\n" ) ;
504
    output ( "    } STATEMENT TRIM_vec #\n\n" ) ;
505
 
506
    output ( "%pi DIM_vec PTR_ptr_vec DIM_ptr_vec\n" ) ;
507
    output ( "%pi NULL_vec MAKE_vec DESTROY_vec TRIM_vec\n\n" ) ;
508
    print_type_ops_tok ( "VEC", "vec", 0 ) ;
509
    return ;
510
}
511
 
512
 
513
/*
514
    PRINT VECTOR POINTER CONSTRUCTS (TOKEN VERSION)
515
 
516
    This routine prints the token versions of the vector pointer
517
    constructs.
518
*/
519
 
520
static void print_vec_ptr_tok
521
    PROTO_Z ()
522
{
523
    comment ( "Vector pointer token specifications" ) ;
524
    output ( "%pt PROC {\\\n" ) ;
525
    output ( "\tTYPE t, %xr : VEC ( t ) : e |\\\n" ) ;
526
    output ( "\tEXP e\\\n" ) ;
527
    output ( "    } %xr : VEC_PTR ( t ) : VEC_PTR_vec #\n\n" ) ;
528
 
529
    output ( "%pt PROC {\\\n" ) ;
530
    output ( "\tTYPE t, %xr : VEC_PTR ( t ) : e |\\\n" ) ;
531
    output ( "\tEXP e\\\n" ) ;
532
    output ( "    } %xr : PTR ( t ) : PTR_vec_ptr #\n\n" ) ;
533
 
534
    output ( "%pi VEC_PTR_vec PTR_vec_ptr\n\n" ) ;
535
    print_type_ops_tok ( "VEC_PTR", "vec_ptr", 0 ) ;
536
    return ;
537
}
538
 
539
 
540
/*
541
    PRINT SIZE CONSTRUCTS (TOKEN VERSION)
542
 
543
    This routine prints the token versions of the size constructs.
544
*/
545
 
546
static void print_size_tok
547
    PROTO_Z ()
548
{
549
    comment ( "Size token specifications" ) ;
550
    output ( "%pt PROC {\\\n" ) ;
551
    output ( "\tTYPE t, VARIETY v,\\\n" ) ;
552
    output ( "\t%xr : SIZE ( t ) : e1, %xr : v : e2 |\\\n" ) ;
553
    output ( "\tEXP e1, EXP e2\\\n" ) ;
554
    output ( "    } %xr : SIZE ( t ) : SCALE #\n\n" ) ;
555
    output ( "%pi SCALE\n\n\n" ) ;
556
    return ;
557
}
558
 
559
 
560
/*
561
    PRINT PRIMITIVE CONSTRUCTS (TOKEN VERSION)
562
 
563
    This routine prints the token versions of the primitive constructs.
564
*/
565
 
566
static void print_prim_tok
567
    PROTO_Z ()
568
{
569
    comment ( "Definitions for primitive %PN" ) ;
570
    print_simple_tok ( "%PN", "%PM", 1, 1 ) ;
571
    return ;
572
}
573
 
574
 
575
/*
576
    PRINT ENUMERATION CONSTRUCTS (TOKEN VERSION)
577
 
578
    This routine prints the token versions of the enumeration constructs.
579
*/
580
 
581
static void print_enum_tok
582
    PROTO_Z ()
583
{
584
    int lst = DEREF_int ( en_lists ( CRT_ENUM ) ) ;
585
    comment ( "Definitions for enumeration %EN" ) ;
586
    print_enum_consts () ;
587
    print_simple_tok ( "%EN", "%EM", 1, lst ) ;
588
    return ;
589
}
590
 
591
 
592
/*
593
    PRINT STRUCTURE CONSTRUCTS (TOKEN VERSION)
594
 
595
    This routine prints the token versions of the structure constructs.
596
*/
597
 
598
static void print_struct_tok
599
    PROTO_Z ()
600
{
601
    STRUCTURE_P base = DEREF_ptr ( str_base ( CRT_STRUCTURE ) ) ;
602
 
603
    comment ( "Definitions for structure %SN" ) ;
604
    LOOP_STRUCTURE_COMPONENT {
605
	output ( "%pt PROC ( %xr : PTR ( %SN ) : ) " )  ;
606
	output ( "%xr : PTR ( %CT ) : %SM_%CN #\n" ) ;
607
	output ( "%pi %SM_%CN\n\n" ) ;
608
    }
609
    output ( "%pt PROC (\\\n" ) ;
610
    LOOP_STRUCTURE_COMPONENT {
611
	string v = DEREF_string ( cmp_value ( CRT_COMPONENT ) ) ;
612
	if ( v == NULL ) output ( "\t%xr : %CT :,\\\n" ) ;
613
    }
614
    output ( "\t%xr : PTR ( %SN ) :\\\n" ) ;
615
    output ( "    ) STATEMENT MAKE_%SM #\n" ) ;
616
    output ( "%pi MAKE_%SM\n\n" ) ;
617
 
618
    if ( !IS_NULL_ptr ( base ) ) {
619
	CLASS_ID_P id = DEREF_ptr ( str_id ( base ) ) ;
620
	char *nt = DEREF_string ( cid_name ( id ) ) ;
621
	char *nm = DEREF_string ( cid_name_aux ( id ) ) ;
622
	output ( "%pt PROC (\\\n" ) ;
623
	output ( "\t%xr : PTR ( %SN ) :\\\n" ) ;
624
	output ( "    ) %xr : PTR ( %s ) : CONVERT_%SM_%s #\n", nt, nm ) ;
625
	output ( "%pi CONVERT_%SM_%s\n\n", nm ) ;
626
    }
627
 
628
    print_simple_tok ( "%SN", "%SM", 0, 1 ) ;
629
    return ;
630
}
631
 
632
 
633
/*
634
    PRINT UNION CONSTRUCTS (TOKEN VERSION)
635
 
636
    This routine prints the token versions of the union constructs.
637
*/
638
 
639
static void print_union_tok
640
    PROTO_Z ()
641
{
642
    UNION_P base = DEREF_ptr ( un_base ( CRT_UNION ) ) ;
643
 
644
    comment ( "Definitions for union %UN" ) ;
645
    output ( "#define ORDER_%UM ( ( unsigned ) %UO )\n" ) ;
646
    output ( "%pt %xc : %UN : NULL_%UM #\n" ) ;
647
    output ( "%pt PROC ( %xr : %UN : ) %xr : int : IS_NULL_%UM #\n" ) ;
648
    output ( "%pt PROC ( %xr : %UN :, %xr : %UN : ) " ) ;
649
    output ( "%xr : int : EQ_%UM #\n" ) ;
650
    output ( "%pi NULL_%UM IS_NULL_%UM EQ_%UM\n\n" ) ;
651
 
652
    if ( !IS_NULL_ptr ( base ) ) {
653
	CLASS_ID_P id = DEREF_ptr ( un_id ( base ) ) ;
654
	char *nt = DEREF_string ( cid_name ( id ) ) ;
655
	char *nm = DEREF_string ( cid_name_aux ( id ) ) ;
656
	output ( "%pt PROC ( %xr : %UN : ) %xr : %s : CONVERT_%UM_%s #\n",
657
		 nt, nm ) ;
658
	output ( "%pi CONVERT_%UM_%s\n\n", nm ) ;
659
    }
660
 
661
    print_simple_tok ( "%UN", "%UM", 1, 1 ) ;
662
    return ;
663
}
664
 
665
 
666
/*
667
    PRINT THE MAIN TOKEN OUTPUT FILE
668
 
669
    This routine prints the token specifications for the objects above.
670
*/
671
 
672
static void print_main_tok
673
    PROTO_N ( ( dir ) )
674
    PROTO_T ( char *dir )
675
{
676
    open_file ( dir, MAIN_PREFIX, MAIN_SUFFIX ) ;
677
    if ( extra_headers ) {
678
	output ( "#include \"%s_bscs.h\"\n\n", MAIN_PREFIX ) ;
679
    }
680
    output ( "#ifndef %X_NAME\n" ) ;
681
    output ( "#define %X_NAME%t40\"%X\"\n" ) ;
682
    output ( "#define %X_VERSION%t40\"%V\"\n" ) ;
683
    output ( "#define %X_SPECIFICATION%t40%d\n", 1 ) ;
684
    output ( "#define %X_IMPLEMENTATION%t40%d\n", 0 ) ;
685
    output ( "#endif\n\n\n" ) ;
686
 
687
    print_proto () ;
688
    print_types_tok () ;
689
    print_ptr_tok () ;
690
    print_list_tok () ;
691
    if ( allow_stack ) {
692
	print_stack_tok () ;
693
    }
694
    if ( allow_vec ) {
695
	print_vec_tok () ;
696
	print_vec_ptr_tok () ;
697
    }
698
    print_size_tok () ;
699
 
700
    LOOP_PRIMITIVE print_prim_tok () ;
701
    LOOP_ENUM print_enum_tok () ;
702
    LOOP_STRUCTURE print_struct_tok () ;
703
    LOOP_UNION print_union_tok () ;
704
 
705
    if ( extra_headers ) {
706
	output ( "#include \"%s_term.h\"\n\n", MAIN_PREFIX ) ;
707
    }
708
    close_file () ;
709
    return ;
710
}
711
 
712
 
713
 
714
/*
715
    PRINT ARGUMENTS FOR A TOKENISED UNION CONSTRUCTOR
716
 
717
    This routine prints the list of arguments for a tokenised union
718
    constructor and similar functions.  lv is true if all the arguments
719
    are lvalues.
720
*/
721
 
722
static void print_cons_tok_args
723
    PROTO_N ( ( lv, d ) )
724
    PROTO_T ( int lv X int d )
725
{
726
    char *a = "%xr" ;
727
    char *b = "%xl" ;
728
    if ( lv ) {
729
	char *c = a ;
730
	a = b ;
731
	b = c ;
732
    }
733
    LOOP_UNION_COMPONENT {
734
	string v = DEREF_string ( cmp_value ( CRT_COMPONENT ) ) ;
735
	if ( v == NULL || d == 0 ) output ( "\\\n\t%e : %CT :,", a ) ;
736
    }
737
    LOOP_FIELD_COMPONENT {
738
	string v = DEREF_string ( cmp_value ( CRT_COMPONENT ) ) ;
739
	if ( v == NULL || d == 0 ) output ( "\\\n\t%e : %CT :,", a ) ;
740
    }
741
    output ( "\\\n\t%e : %UN :", b ) ;
742
    return ;
743
}
744
 
745
 
746
/*
747
    PRINT FIELD SELECTOR OPERATIONS
748
 
749
    This routine prints the operations on field selectors (token version).
750
    rng gives the number of elements in the field set (if appropriate).
751
    al is true if the field is aliased.
752
*/
753
 
754
static void print_field_tok
755
    PROTO_N ( ( rng, al ) )
756
    PROTO_T ( int rng X int al )
757
{
758
    char *f = ( rng ? "%FN_etc" : "%FN" ) ;
759
 
760
    LOOP_FIELD_COMPONENT {
761
	output ( "%pt PROC ( %xr : %UN : )\\\n" ) ;
762
	output ( "    %xr : PTR ( %CT ) : " ) ;
763
	output ( "%UM_%e_%CN #\n", f ) ;
764
	output ( "%pi %UM_%e_%CN\n\n", f ) ;
765
    }
766
 
767
    /* Component constructor */
768
    output ( "%pt PROC (" ) ;
769
    if ( rng ) output ( "\\\n\t%xr : unsigned :," ) ;
770
    print_cons_tok_args ( 0, 1 ) ;
771
    output ( "\\\n    ) STATEMENT MAKE_%UM_%e #\n", f ) ;
772
    output ( "%pi MAKE_%UM_%e\n\n", f ) ;
773
 
774
    /* Tag modifier */
775
    if ( rng ) {
776
	output ( "%pt PROC (" ) ;
777
	output ( "\\\n\t%xr : unsigned :," ) ;
778
	output ( "\\\n\t%xr : %UN :" ) ;
779
	output ( "\\\n    ) STATEMENT MODIFY_%UM_%e #\n", f ) ;
780
	output ( "%pi MODIFY_%UM_%e\n\n", f ) ;
781
    }
782
 
783
    /* Component deconstructor */
784
    if ( field_not_empty () ) {
785
	output ( "%pt PROC (" ) ;
786
	print_cons_tok_args ( 1, 0 ) ;
787
	output ( "\\\n    ) STATEMENT DECONS_%UM_%e #\n", f ) ;
788
	output ( "%pi DECONS_%UM_%e\n\n", f ) ;
789
    }
790
 
791
    /* Component destructor */
792
    output ( "%pt PROC (" ) ;
793
    output ( "\\\n\t%xr : DESTROYER :," ) ;
794
    print_cons_tok_args ( 1, 0 ) ;
795
    output ( "\\\n    ) STATEMENT DESTROY_%UM_%e #\n", f ) ;
796
    output ( "%pi DESTROY_%UM_%e\n\n", f ) ;
797
 
798
    /* Aliasing tokens */
799
    if ( al && !rng ) {
800
	output ( "#ifdef %X_IO_ROUTINES\n\n" ) ;
801
	output ( "%pt PROC (\\\n" ) ;
802
	output ( "\t%xl : %UN :,\\\n" ) ;
803
	output ( "\t%xr : unsigned :\\\n" ) ;
804
	output ( "    ) STATEMENT NEW_ALIAS_%UM_%FN #\n" ) ;
805
	output ( "%pi NEW_ALIAS_%UM_%FN\n\n" ) ;
806
 
807
	output ( "%pt PROC ( %xr : %UN : )\\\n    " ) ;
808
	output ( "%xr : unsigned : GET_ALIAS_%UM_%FN #\n" ) ;
809
	output ( "%pt PROC ( %xr : %UN :, %xr : unsigned : )\\\n    " ) ;
810
	output ( "%xr : void : SET_ALIAS_%UM_%FN #\n" ) ;
811
	output ( "%pt PROC ( %xr : unsigned : )\\\n    " ) ;
812
	output ( "%xr : %UN : FIND_ALIAS_%UM_%FN #\n\n" ) ;
813
	output ( "%pi GET_ALIAS_%UM_%FN SET_ALIAS_%UM_%FN " ) ;
814
	output ( "FIND_ALIAS_%UM_%FN\n\n" ) ;
815
	output ( "#endif\n\n" ) ;
816
    }
817
    output ( "\n" ) ;
818
    return ;
819
}
820
 
821
 
822
/*
823
    PRINT THE UNION OPERATIONS OUTPUT FILE
824
 
825
    For each union in the calculus there is an operations file.
826
*/
827
 
828
static void print_union_ops_tok
829
    PROTO_N ( ( dir, un ) )
830
    PROTO_T ( char *dir X char *un )
831
{
832
    open_file ( dir, un, OPS_SUFFIX ) ;
833
    if ( extra_headers ) {
834
	output ( "#include \"%s%s\"\n", MAIN_PREFIX, MAIN_SUFFIX ) ;
835
	output ( "#include <%s_ops.h>\n\n", MAIN_PREFIX ) ;
836
    }
837
 
838
    comment ( "Operations for union %UN" ) ;
839
    output ( "%pt PROC ( %xr : %UN : ) %xr : unsigned : TAG_%UM #\n" ) ;
840
    output ( "%pi TAG_%UM\n\n\n" ) ;
841
 
842
    /* Operations on common components */
843
    LOOP_UNION_COMPONENT {
844
	comment ( "Operations for component %CN of union %UN" ) ;
845
	output ( "%pt PROC ( %xr : %UN : )\\\n" ) ;
846
	output ( "    %xr : PTR ( %CT ) : %UM_%CN #\n" ) ;
847
	output ( "%pi %UM_%CN\n\n" ) ;
848
    }
849
 
850
    /* Operations on field components */
851
    LOOP_UNION_FIELD {
852
	int rng = DEREF_int ( fld_set ( CRT_FIELD ) ) ;
853
	int hash = DEREF_int ( fld_flag ( CRT_FIELD ) ) ;
854
	int al = ( hash ? 1 : 0 ) ;
855
	if ( rng ) {
856
	    comment ( "Operations for field set %FN_etc of union %UN" ) ;
857
	    output ( "%pt %xc : unsigned : %UM_%FN_etc_tag #\n" ) ;
858
	    output ( "%pt PROC ( %xr : %UN : ) " ) ;
859
	    output ( "%xr : int : IS_%UM_%FN_etc #\n" ) ;
860
	    output ( "%pi %UM_%FN_etc_tag IS_%UM_%FN_etc\n\n" ) ;
861
	    print_field_tok ( rng, al ) ;
862
	}
863
 
864
	comment ( "Operations for field %FN of union %UN" ) ;
865
	output ( "%pt %xc : unsigned : %UM_%FN_tag #\n" ) ;
866
	output ( "%pt PROC ( %xr : %UN : ) %xr : int : IS_%UM_%FN #\n" ) ;
867
	output ( "%pi %UM_%FN_tag IS_%UM_%FN\n\n" ) ;
868
	print_field_tok ( 0, al ) ;
869
    }
870
 
871
    /* Map tables */
872
    LOOP_UNION_MAP {
873
	int hash = DEREF_int ( map_flag ( CRT_MAP ) ) ;
874
	comment ( "Map %MN on union %UN" ) ;
875
	output ( "%pt PROC (\\\n" ) ;
876
	output ( "\t%xr : %UN :" ) ;
877
	if ( hash ) output ( ",\\\n\t%xr : DESTROYER :" ) ;
878
	LOOP_MAP_ARGUMENT output ( ",\\\n\t%xr : %AT :" ) ;
879
	output ( "\\\n    ) %xr : %MR : %MN_%UM #\n " ) ;
880
	output ( "%pi %MN_%UM\n\n\n" ) ;
881
    }
882
 
883
    /* End of file */
884
    close_file () ;
885
    return ;
886
}
887
 
888
 
889
/*
890
    MAIN ACTION (TOKEN VERSION)
891
 
892
    This routine prints all the output files for the calculus (token
893
    version).
894
*/
895
 
896
void main_action_tok
897
    PROTO_N ( ( dir ) )
898
    PROTO_T ( char *dir )
899
{
900
    int ign = 0 ;
901
    output_c_code = 2 ;
902
    print_main_tok ( dir ) ;
903
 
904
    LOOP_UNION {
905
	LIST ( MAP_P ) maps ;
906
	CLASS_ID_P cid = DEREF_ptr ( un_id ( CRT_UNION ) ) ;
907
	char *un = DEREF_string ( cid_name_aux ( cid ) ) ;
908
	print_union_ops_tok ( dir, un ) ;
909
	maps = DEREF_list ( un_map ( CRT_UNION ) ) ;
910
	if ( !IS_NULL_list ( maps ) ) {
911
	    print_union_map_c ( dir, un ) ;
912
	    print_union_hdr_c ( dir, un ) ;
913
	    ign = 1 ;
914
        }
915
    }
916
 
917
    if ( ign ) {
918
	open_file ( dir, IGNORE_PREFIX, DEF_SUFFIX ) ;
919
	comment ( "Map ignore macros" ) ;
920
	LOOP_UNION {
921
	    LOOP_UNION_MAP output ( "#define IGNORE_%MN_%UM%t40%d\n", 1 ) ;
922
	}
923
	output ( "\n" ) ;
924
	close_file () ;
925
    }
926
 
927
    if ( extra_asserts ) {
928
	open_file ( dir, ASSERT_PREFIX, DEF_SUFFIX ) ;
929
	comment ( "Dummy assertion function definitions" ) ;
930
	close_file () ;
931
    }
932
    return ;
933
}