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