xref: /original-bsd/usr.bin/pascal/src/0.h (revision d25e1985)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 /* static	char sccsid[] = "@(#)0.h 1.2 09/02/80"; */
4 
5 #define DEBUG
6 #define	CHAR
7 #define	STATIC
8 #define hp21mx 0
9 
10 #include	<stdio.h>
11 #include	<sys/types.h>
12 
13 #define		bool	short
14 #define		TRUE	1
15 #define		FALSE	0
16 
17 /*
18  * Option flags
19  *
20  * The following options are recognized in the text of the program
21  * and also on the command line:
22  *
23  *	b	block buffer the file output
24  *
25  *	i	make a listing of the procedures and functions in
26  *		the following include files
27  *
28  *	l	make a listing of the program
29  *
30  *	n	place each include file on a new page with a header
31  *
32  *	p	disable post mortem and statement limit counting
33  *
34  *	t	disable run-time tests
35  *
36  *	u	card image mode; only first 72 chars of input count
37  *
38  *	w	suppress special diagnostic warnings
39  *
40  *	z	generate counters for an execution profile
41  */
42 #ifdef DEBUG
43 bool	fulltrace, errtrace, testtrace, yyunique;
44 #endif DEBUG
45 
46 /*
47  * Each option has a stack of 17 option values, with opts giving
48  * the current, top value, and optstk the value beneath it.
49  * One refers to option `l' as, e.g., opt('l') in the text for clarity.
50  */
51 char	opts[ 'z' - 'A' + 1];
52 short	optstk[ 'z' - 'A' + 1];
53 
54 #define opt(c) opts[c-'A']
55 
56 /*
57  * Monflg is set when we are generating
58  * a pxp profile.  this is set by the -z command line option.
59  */
60 bool	monflg;
61 
62     /*
63      *	profflag is set when we are generating a prof profile.
64      *	this is set by the -p command line option.
65      */
66 bool	profflag;
67 
68 
69 /*
70  * NOTES ON THE DYNAMIC NATURE OF THE DATA STRUCTURES
71  *
72  * Pi uses expandable tables for
73  * its namelist (symbol table), string table
74  * hash table, and parse tree space.  The following
75  * definitions specify the size of the increments
76  * for these items in fundamental units so that
77  * each uses approximately 1024 bytes.
78  */
79 
80 #define	STRINC	1024		/* string space increment */
81 #define	TRINC	512		/* tree space increment */
82 #define	HASHINC	509		/* hash table size in words, each increment */
83 #define	NLINC	56		/* namelist increment size in nl structs */
84 
85 /*
86  * The initial sizes of the structures.
87  * These should be large enough to compile
88  * an "average" sized program so as to minimize
89  * storage requests.
90  * On a small system or and 11/34 or 11/40
91  * these numbers can be trimmed to make the
92  * compiler smaller.
93  */
94 #define	ITREE	2000
95 #define	INL	200
96 #define	IHASH	509
97 
98 /*
99  * The following limits on hash and tree tables currently
100  * allow approximately 1200 symbols and 20k words of tree
101  * space.  The fundamental limit of 64k total data space
102  * should be exceeded well before these are full.
103  */
104 /*
105  * TABLE_MULTIPLIER is for uniformly increasing the sizes of the tables
106  */
107 #define TABLE_MULTIPLIER	8
108 #define	MAXHASH	(4 * TABLE_MULTIPLIER)
109 #define	MAXNL	(12 * TABLE_MULTIPLIER)
110 #define	MAXTREE	(30 * TABLE_MULTIPLIER)
111 /*
112  * MAXDEPTH is the depth of the parse stack.
113  * STACK_MULTIPLIER is for increasing its size.
114  */
115 #define	STACK_MULTIPLIER	8
116 #define	MAXDEPTH ( 150 * STACK_MULTIPLIER )
117 
118 /*
119  * ERROR RELATED DEFINITIONS
120  */
121 
122 /*
123  * Exit statuses to pexit
124  *
125  * AOK
126  * ERRS		Compilation errors inhibit obj productin
127  * NOSTART	Errors before we ever got started
128  * DIED		We ran out of memory or some such
129  */
130 #define	AOK	0
131 #define	ERRS	1
132 #define	NOSTART	2
133 #define	DIED	3
134 
135 bool	Recovery;
136 
137 #define	eholdnl()	Eholdnl = 1
138 #define	nocascade()	Enocascade = 1
139 
140 bool	Eholdnl, Enocascade;
141 
142 
143 /*
144  * The flag eflg is set whenever we have a hard error.
145  * The character in errpfx will precede the next error message.
146  * When cgenflg is set code generation is suppressed.
147  * This happens whenver we have an error (i.e. if eflg is set)
148  * and when we are walking the tree to determine types only.
149  */
150 bool	eflg;
151 char	errpfx;
152 
153 #define	setpfx(x)	errpfx = x
154 
155 #define	standard()	setpfx('s')
156 #define	warning()	setpfx('w')
157 #define	recovered()	setpfx('e')
158 
159 bool	cgenflg;
160 
161 
162 /*
163  * The flag syneflg is used to suppress the diagnostics of the form
164  *	E 10 a, defined in someprocedure, is neither used nor set
165  * when there were syntax errors in "someprocedure".
166  * In this case, it is likely that these warinings would be spurious.
167  */
168 bool	syneflg;
169 
170 /*
171  * The compiler keeps its error messages in a file.
172  * The variable efil is the unit number on which
173  * this file is open for reading of error message text.
174  * Similarly, the file ofil is the unit of the file
175  * "obj" where we write the interpreter code.
176  */
177 short	efil;
178 short	ofil;
179 short	obuf[518];
180 
181 #define	elineoff()	Enoline++
182 #define	elineon()	Enoline = 0
183 
184 bool	Enoline;
185 
186 /*
187  * SYMBOL TABLE STRUCTURE DEFINITIONS
188  *
189  * The symbol table is henceforth referred to as the "namelist".
190  * It consists of a number of structures of the form "nl" below.
191  * These are contained in a number of segments of the symbol
192  * table which are dynamically allocated as needed.
193  * The major namelist manipulation routines are contained in the
194  * file "nl.c".
195  *
196  * The major components of a namelist entry are the "symbol", giving
197  * a pointer into the string table for the string associated with this
198  * entry and the "class" which tells which of the (currently 19)
199  * possible types of structure this is.
200  *
201  * Many of the classes use the "type" field for a pointer to the type
202  * which the entry has.
203  *
204  * Other pieces of information in more than one class include the block
205  * in which the symbol is defined, flags indicating whether the symbol
206  * has been used and whether it has been assigned to, etc.
207  *
208  * A more complete discussion of the features of the namelist is impossible
209  * here as it would be too voluminous.  Refer to the "PI 1.0 Implementation
210  * Notes" for more details.
211  */
212 
213 /*
214  * The basic namelist structure.
215  * There are also two other variants, defining the real
216  * field as longs or integers given below.
217  *
218  * The array disptab defines the hash header for the symbol table.
219  * Symbols are hashed based on the low 6 bits of their pointer into
220  * the string table; see the routines in the file "lookup.c" and also "fdec.c"
221  * especially "funcend".
222  */
223 #ifdef PTREE
224 #   include	"pTree.h"
225 #endif PTREE
226 struct	nl {
227 	char	*symbol;
228 	char	class, nl_flags;
229 #ifdef PC
230 	char	ext_flags;	/* an extra flag is used for externals */
231 #endif PC
232 	struct	nl *type;
233 	struct	nl *chain, *nl_next;
234 	int	*ptr[4];
235 #ifdef PI
236 	int	entloc;
237 #endif PI
238 #	ifdef PTREE
239 	    pPointer	inTree;
240 #	endif PTREE
241 } *nlp, *disptab[077+1];
242 
243 extern struct nl nl[INL];
244 
245 struct {
246 	char	*symbol;
247 	char	class, nl_flags;
248 #ifdef PC
249 	char	ext_flags;
250 #endif
251 	struct	nl *type;
252 	struct	nl *chain, *nl_next;
253 	double	real;
254 };
255 
256 struct {
257 	char	*symbol;
258 	char	class, nl_block;
259 #ifdef PC
260 	char	ext_flags;
261 #endif
262 	struct	nl *type;
263 	struct	nl *chain, *nl_next;
264 	long	range[2];
265 };
266 
267 struct {
268 	char	*symbol;
269 	char	class, nl_flags;
270 #ifdef PC
271 	char	ext_flags;
272 #endif
273 	struct	nl *type;
274 	struct	nl *chain, *nl_next;
275 	long	value[4];
276 };
277 
278 /*
279  * NL FLAGS BITS
280  *
281  * Definitions of the usage of the bits in
282  * the nl_flags byte. Note that the low 5 bits of the
283  * byte are the "nl_block" and that some classes make use
284  * of this byte as a "width".
285  *
286  * The only non-obvious bit definition here is "NFILES"
287  * which records whether a structure contains any files.
288  * Such structures are not allowed to be dynamically allocated.
289  */
290 #define	NUSED	0100
291 #define	NMOD	0040
292 #define	NFORWD	0200
293 #define	NFILES	0200
294 
295 #ifdef PC
296 #define NEXTERN 0001	/* flag used to mark external funcs and procs */
297 #endif
298 
299 /*
300  * Definition of the commonly used "value" fields.
301  * The most important one is NL_OFFS which gives
302  * the offset of a variable in its stack mark.
303  */
304 #define NL_OFFS	0
305 
306 #define	NL_CNTR	1
307 #define	NL_FVAR	3
308 
309 #define NL_GOLEV 2
310 #define NL_GOLINE 3
311 #define NL_FORV 1
312 
313 #define	NL_FLDSZ 1
314 #define	NL_VARNT 2
315 #define	NL_VTOREC 2
316 #define	NL_TAG	3
317 
318 #define	NL_ELABEL	3
319 
320 /*
321  * For BADUSE nl structures, NL_KINDS is a bit vector
322  * indicating the kinds of illegal usages complained about
323  * so far.  For kind of bad use "kind", "1 << kind" is set.
324  * The low bit is reserved as ISUNDEF to indicate whether
325  * this identifier is totally undefined.
326  */
327 #define	NL_KINDS	0
328 
329 #define	ISUNDEF		1
330 
331 /*
332  * NAMELIST CLASSES
333  *
334  * The following are the namelist classes.
335  * Different classes make use of the value fields
336  * of the namelist in different ways.
337  *
338  * The namelist should be redesigned by providing
339  * a number of structure definitions with one corresponding
340  * to each namelist class, ala a variant record in Pascal.
341  */
342 #define	BADUSE	0
343 #define	CONST	1
344 #define	TYPE	2
345 #define	VAR	3
346 #define	ARRAY	4
347 #define	PTRFILE	5
348 #define	RECORD	6
349 #define	FIELD	7
350 #define	PROC	8
351 #define	FUNC	9
352 #define	FVAR	10
353 #define	REF	11
354 #define	PTR	12
355 #define	FILET	13
356 #define	SET	14
357 #define	RANGE	15
358 #define	LABEL	16
359 #define	WITHPTR 17
360 #define	SCAL	18
361 #define	STR	19
362 #define	PROG	20
363 #define	IMPROPER 21
364 #define	VARNT	22
365 
366 /*
367  * Clnames points to an array of names for the
368  * namelist classes.
369  */
370 char	**clnames;
371 
372 /*
373  * PRE-DEFINED NAMELIST OFFSETS
374  *
375  * The following are the namelist offsets for the
376  * primitive types. The ones which are negative
377  * don't actually exist, but are generated and tested
378  * internally. These definitions are sensitive to the
379  * initializations in nl.c.
380  */
381 #define	TFIRST -7
382 #define	TFILE  -7
383 #define	TREC   -6
384 #define	TARY   -5
385 #define	TSCAL  -4
386 #define	TPTR   -3
387 #define	TSET   -2
388 #define	TSTR   -1
389 #define	NIL	0
390 #define	TBOOL	1
391 #define	TCHAR	2
392 #define	TINT	3
393 #define	TDOUBLE	4
394 #define	TNIL	5
395 #define	T1INT	6
396 #define	T2INT	7
397 #define	T4INT	8
398 #define	T1CHAR	9
399 #define	T1BOOL	10
400 #define	T8REAL	11
401 #define TLAST	11
402 
403 /*
404  * SEMANTIC DEFINITIONS
405  */
406 
407 /*
408  * NOCON and SAWCON are flags in the tree telling whether
409  * a constant set is part of an expression.
410  */
411 #define NOCON	0
412 #define SAWCON	1
413 
414 /*
415  * The variable cbn gives the current block number,
416  * the variable bn is set as a side effect of a call to
417  * lookup, and is the block number of the variable which
418  * was found.
419  */
420 short	bn, cbn;
421 
422 /*
423  * The variable line is the current semantic
424  * line and is set in stat.c from the numbers
425  * embedded in statement type tree nodes.
426  */
427 short	line;
428 
429 /*
430  * The size of the display
431  * which defines the maximum nesting
432  * of procedures and functions allowed.
433  * Because of the flags in the current namelist
434  * this must be no greater than 32.
435  */
436 #define	DSPLYSZ 20
437 
438 /*
439  * The following structure is used
440  * to keep track of the amount of variable
441  * storage required by each block.
442  * "Max" is the high water mark, "off"
443  * the current need. Temporaries for "for"
444  * loops and "with" statements are allocated
445  * in the local variable area and these
446  * numbers are thereby changed if necessary.
447  */
448 struct om {
449 	long	om_off;
450 	long	om_max;
451 } sizes[DSPLYSZ];
452 
453     /*
454      *	the following structure records whether a level declares
455      *	any variables which are (or contain) files.
456      *	this so that the runtime routines for file cleanup can be invoked.
457      */
458 bool	dfiles[ DSPLYSZ ];
459 
460 /*
461  * Structure recording information about a constant
462  * declaration.  It is actually the return value from
463  * the routine "gconst", but since C doesn't support
464  * record valued functions, this is more convenient.
465  */
466 struct {
467 	struct nl	*ctype;
468 	short		cival;
469 	double		crval;
470 	int		*cpval;
471 } con;
472 
473 /*
474  * The set structure records the lower bound
475  * and upper bound with the lower bound normalized
476  * to zero when working with a set. It is set by
477  * the routine setran in var.c.
478  */
479 struct {
480 	short	lwrb, uprbp;
481 } set;
482 
483     /*
484      *	structures of this kind are filled in by precset and used by postcset
485      *	to indicate things about constant sets.
486      */
487 struct csetstr {
488     struct nl	*csettype;
489     long	paircnt;
490     long	singcnt;
491     bool	comptime;
492 };
493 /*
494  * The following flags are passed on calls to lvalue
495  * to indicate how the reference is to affect the usage
496  * information for the variable being referenced.
497  * MOD is used to set the NMOD flag in the namelist
498  * entry for the variable, ASGN permits diagnostics
499  * to be formed when a for variable is assigned to in
500  * the range of the loop.
501  */
502 #define	NOFLAGS	0
503 #define	MOD	01
504 #define	ASGN	02
505 #define	NOUSE	04
506 
507     /*
508      *	the following flags are passed to lvalue and rvalue
509      *	to tell them whether an lvalue or rvalue is required.
510      *	the semantics checking is done according to the function called,
511      *	but for pc, lvalue may put out an rvalue by indirecting afterwards,
512      *	and rvalue may stop short of putting out the indirection.
513      */
514 #define	LREQ	01
515 #define	RREQ	02
516 
517 double	MAXINT;
518 double	MININT;
519 
520 /*
521  * Variables for generation of profile information.
522  * Monflg is set when we want to generate a profile.
523  * Gocnt record the total number of goto's and
524  * cnts records the current counter for generating
525  * COUNT operators.
526  */
527 short	gocnt;
528 short	cnts;
529 
530 /*
531  * Most routines call "incompat" rather than asking "!compat"
532  * for historical reasons.
533  */
534 #define incompat 	!compat
535 
536 /*
537  * Parts records which declaration parts have been seen.
538  * The grammar allows the "label" "const" "type" "var" and routine
539  * parts to be repeated and to be in any order, so that
540  * they can be detected semantically to give better
541  * error diagnostics.
542  */
543 int	parts[ DSPLYSZ ];
544 
545 #define	LPRT	1
546 #define	CPRT	2
547 #define	TPRT	4
548 #define	VPRT	8
549 #define	RPRT	16
550 
551 /*
552  * Flags for the "you used / instead of div" diagnostic
553  */
554 bool	divchk;
555 bool	divflg;
556 
557 short	errcnt[DSPLYSZ];
558 
559 /*
560  * Forechain links those types which are
561  *	^ sometype
562  * so that they can be evaluated later, permitting
563  * circular, recursive list structures to be defined.
564  */
565 struct	nl *forechain;
566 
567 /*
568  * Withlist links all the records which are currently
569  * opened scopes because of with statements.
570  */
571 struct	nl *withlist;
572 
573 struct	nl *intset;
574 struct	nl *input, *output;
575 struct	nl *program;
576 
577 /* progseen flag used by PC to determine if
578  * a routine segment is being compiled (and
579  * therefore no program statement seen)
580  */
581 bool	progseen;
582 
583 
584 /*
585  * STRUCTURED STATEMENT GOTO CHECKING
586  *
587  * The variable level keeps track of the current
588  * "structured statement level" when processing the statement
589  * body of blocks.  This is used in the detection of goto's into
590  * structured statements in a block.
591  *
592  * Each label's namelist entry contains two pieces of information
593  * related to this check. The first `NL_GOLEV' either contains
594  * the level at which the label was declared, `NOTYET' if the label
595  * has not yet been declared, or `DEAD' if the label is dead, i.e.
596  * if we have exited the level in which the label was defined.
597  *
598  * When we discover a "goto" statement, if the label has not
599  * been defined yet, then we record the current level and the current line
600  * for a later error check.  If the label has been already become "DEAD"
601  * then a reference to it is an error.  Now the compiler maintains,
602  * for each block, a linked list of the labels headed by "gotos[bn]".
603  * When we exit a structured level, we perform the routine
604  * ungoto in stat.c. It notices labels whose definition levels have been
605  * exited and makes them be dead. For labels which have not yet been
606  * defined, ungoto will maintain NL_GOLEV as the minimum structured level
607  * since the first usage of the label. It is not hard to see that the label
608  * must eventually be declared at this level or an outer level to this
609  * one or a goto into a structured statement will exist.
610  */
611 short	level;
612 struct	nl *gotos[DSPLYSZ];
613 
614 #define	NOTYET	10000
615 #define	DEAD	10000
616 
617 /*
618  * Noreach is true when the next statement will
619  * be unreachable unless something happens along
620  * (like exiting a looping construct) to save
621  * the day.
622  */
623 bool	noreach;
624 
625 /*
626  * UNDEFINED VARIABLE REFERENCE STRUCTURES
627  */
628 struct	udinfo {
629 	int	ud_line;
630 	struct	udinfo *ud_next;
631 	char	nullch;
632 };
633 
634 /*
635  * CODE GENERATION DEFINITIONS
636  */
637 
638 /*
639  * NSTAND is or'ed onto the abstract machine opcode
640  * for non-standard built-in procedures and functions.
641  */
642 #define	NSTAND	0400
643 
644 #define	codeon()	cgenflg++
645 #define	codeoff()	--cgenflg
646 
647 /*
648  * Codeline is the last lino output in the code generator.
649  * It used to be used to suppress LINO operators but no
650  * more since we now count statements.
651  * Lc is the intepreter code location counter.
652  *
653 short	codeline;
654  */
655 char	*lc;
656 
657 
658 /*
659  * Routines which need types
660  * other than "integer" to be
661  * assumed by the compiler.
662  */
663 double		atof();
664 long		lwidth();
665 long		aryconst();
666 long		a8tol();
667 struct nl 	*lookup();
668 double		atof();
669 int		*tree();
670 int		*hash();
671 char		*alloc();
672 int		*calloc();
673 char		*savestr();
674 struct nl	*lookup1();
675 struct nl	*hdefnl();
676 struct nl	*defnl();
677 struct nl	*enter();
678 struct nl	*nlcopy();
679 struct nl	*tyrecl();
680 struct nl	*tyary();
681 struct nl	*fields();
682 struct nl	*variants();
683 struct nl	*deffld();
684 struct nl	*defvnt();
685 struct nl	*tyrec1();
686 struct nl	*reclook();
687 struct nl	*asgnop1();
688 struct nl	*gtype();
689 struct nl	*call();
690 struct nl	*lvalue();
691 struct nl	*rvalue();
692 struct nl	*cset();
693 
694 /*
695  * type cast NIL to keep lint happy (which is not so bad)
696  */
697 #define		NLNIL	( (struct nl *) NIL )
698 
699 /*
700  * Funny structures to use
701  * pointers in wild and wooly ways
702  */
703 struct {
704 	char	pchar;
705 };
706 struct {
707 	short	pint;
708 	short	pint2;
709 };
710 struct {
711 	long	plong;
712 };
713 struct {
714 	double	pdouble;
715 };
716 
717 #define	OCT	1
718 #define	HEX	2
719 
720 /*
721  * MAIN PROGRAM VARIABLES, MISCELLANY
722  */
723 
724 /*
725  * Variables forming a data base referencing
726  * the command line arguments with the "i" option, e.g.
727  * in "pi -i scanner.i compiler.p".
728  */
729 char	**pflist;
730 short	pflstc;
731 short	pfcnt;
732 
733 char	*filename;		/* current source file name */
734 long	tvec;
735 extern char	*snark;		/* SNARK */
736 extern char	*classes[ ];	/* maps namelist classes to string names */
737 
738 #define	derror error
739 
740 #ifdef	PC
741 
742     /*
743      *	the current function number, for [ lines
744      */
745     int	ftnno;
746 
747     /*
748      *	the pc output stream
749      */
750     FILE *pcstream;
751 
752 #endif PC
753