xref: /original-bsd/usr.bin/pascal/src/0.h (revision 6c57d260)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 /* static char sccsid[] = "@(#)0.h 1.12 04/17/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 extern struct	nl *Fp;
231 extern int	pnumcnt;
232 
233 #ifdef PTREE
234 #   include	"pTree.h"
235 #endif PTREE
236 struct	nl {
237 	char	*symbol;
238 	char	class, nl_flags;
239 #ifdef PC
240 	char	ext_flags;	/* an extra flag is used for externals */
241 #endif PC
242 	struct	nl *type;
243 	struct	nl *chain, *nl_next;
244 	int	value[5];
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	*ptr[4];
283 #ifdef PI
284 	int	entloc;
285 #endif PI
286 };
287 
288 /*
289  * NL FLAGS BITS
290  *
291  * Definitions of the usage of the bits in
292  * the nl_flags byte. Note that the low 5 bits of the
293  * byte are the "nl_block" and that some classes make use
294  * of this byte as a "width".
295  *
296  * The only non-obvious bit definition here is "NFILES"
297  * which records whether a structure contains any files.
298  * Such structures are not allowed to be dynamically allocated.
299  */
300 
301 #define	BLOCKNO( flag )	( flag & 037 )
302 #define NLFLAGS( flag ) ( flag &~ 037 )
303 
304 #define	NUSED	0100
305 #define	NMOD	0040
306 #define	NFORWD	0200
307 #define	NFILES	0200
308 #ifdef PC
309 #define NEXTERN 0001	/* flag used to mark external funcs and procs */
310 #endif
311 
312 /*
313  * used to mark value[ NL_FORV ] for loop variables
314  */
315 #define	FORVAR		1
316 
317 /*
318  * Definition of the commonly used "value" fields.
319  * The most important one is NL_OFFS which gives
320  * the offset of a variable in its stack mark.
321  */
322 #define NL_OFFS	0
323 
324 #define	NL_CNTR	1
325 #define NL_NLSTRT 2
326 #define	NL_LINENO 3
327 #define	NL_FVAR	3
328 #define	NL_FCHAIN 4
329 
330 #define NL_GOLEV 2
331 #define NL_GOLINE 3
332 #define NL_FORV 1
333 
334 #define	NL_FLDSZ 1
335 #define	NL_VARNT 2
336 #define	NL_VTOREC 2
337 #define	NL_TAG	3
338 
339 #define	NL_ELABEL	4
340 
341 /*
342  * For BADUSE nl structures, NL_KINDS is a bit vector
343  * indicating the kinds of illegal usages complained about
344  * so far.  For kind of bad use "kind", "1 << kind" is set.
345  * The low bit is reserved as ISUNDEF to indicate whether
346  * this identifier is totally undefined.
347  */
348 #define	NL_KINDS	0
349 
350 #define	ISUNDEF		1
351 
352 /*
353  * Variables may reside on the stack as formals or as locals,
354  * or as register temporaries
355  */
356 #define PARAMVAR	1
357 #define LOCALVAR	2
358 #define REGVAR		3
359 
360 /*
361  * NAMELIST CLASSES
362  *
363  * The following are the namelist classes.
364  * Different classes make use of the value fields
365  * of the namelist in different ways.
366  *
367  * The namelist should be redesigned by providing
368  * a number of structure definitions with one corresponding
369  * to each namelist class, ala a variant record in Pascal.
370  */
371 #define	BADUSE	0
372 #define	CONST	1
373 #define	TYPE	2
374 #define	VAR	3
375 #define	ARRAY	4
376 #define	PTRFILE	5
377 #define	RECORD	6
378 #define	FIELD	7
379 #define	PROC	8
380 #define	FUNC	9
381 #define	FVAR	10
382 #define	REF	11
383 #define	PTR	12
384 #define	FILET	13
385 #define	SET	14
386 #define	RANGE	15
387 #define	LABEL	16
388 #define	WITHPTR 17
389 #define	SCAL	18
390 #define	STR	19
391 #define	PROG	20
392 #define	IMPROPER 21
393 #define	VARNT	22
394 #define	FPROC	23
395 #define	FFUNC	24
396 
397 /*
398  * Clnames points to an array of names for the
399  * namelist classes.
400  */
401 char	**clnames;
402 
403 /*
404  * PRE-DEFINED NAMELIST OFFSETS
405  *
406  * The following are the namelist offsets for the
407  * primitive types. The ones which are negative
408  * don't actually exist, but are generated and tested
409  * internally. These definitions are sensitive to the
410  * initializations in nl.c.
411  */
412 #define	TFIRST -7
413 #define	TFILE  -7
414 #define	TREC   -6
415 #define	TARY   -5
416 #define	TSCAL  -4
417 #define	TPTR   -3
418 #define	TSET   -2
419 #define	TSTR   -1
420 #define	NIL	0
421 #define	TBOOL	1
422 #define	TCHAR	2
423 #define	TINT	3
424 #define	TDOUBLE	4
425 #define	TNIL	5
426 #define	T1INT	6
427 #define	T2INT	7
428 #define	T4INT	8
429 #define	T1CHAR	9
430 #define	T1BOOL	10
431 #define	T8REAL	11
432 #define TLAST	11
433 
434 /*
435  * SEMANTIC DEFINITIONS
436  */
437 
438 /*
439  * NOCON and SAWCON are flags in the tree telling whether
440  * a constant set is part of an expression.
441  *	these are no longer used,
442  *	since we now do constant sets at compile time.
443  */
444 #define NOCON	0
445 #define SAWCON	1
446 
447 /*
448  * The variable cbn gives the current block number,
449  * the variable bn is set as a side effect of a call to
450  * lookup, and is the block number of the variable which
451  * was found.
452  */
453 short	bn, cbn;
454 
455 /*
456  * The variable line is the current semantic
457  * line and is set in stat.c from the numbers
458  * embedded in statement type tree nodes.
459  */
460 short	line;
461 
462 /*
463  * The size of the display
464  * which defines the maximum nesting
465  * of procedures and functions allowed.
466  * Because of the flags in the current namelist
467  * this must be no greater than 32.
468  */
469 #define	DSPLYSZ 20
470 
471     /*
472      *	the display is made up of saved AP's and FP's.
473      *	FP's are used to find locals, and AP's are used to find parameters.
474      *	FP and AP are untyped pointers, but are used throughout as (char *).
475      *	the display is used by adding AP_OFFSET or FP_OFFSET to the
476      *	address of the approriate display entry.
477      */
478 struct dispsave {
479     char	*savedAP;
480     char	*savedFP;
481 } display[ DSPLYSZ ];
482 
483 #define	AP_OFFSET	( 0 )
484 #define FP_OFFSET	( sizeof(char *) )
485 
486     /*
487      *	formal routine structure:
488      */
489 struct formalrtn {
490 	long		(*fentryaddr)();	/* formal entry point */
491 	long		fbn;			/* block number of function */
492 	struct dispsave	fdisp[ DSPLYSZ ];	/* saved at first passing */
493 } frtn;
494 
495 #define	FENTRYOFFSET	0
496 #define FBNOFFSET	( FENTRYOFFSET + sizeof frtn.fentryaddr )
497 #define	FDISPOFFSET	( FBNOFFSET + sizeof frtn.fbn )
498 
499 /*
500  * The following structure is used
501  * to keep track of the amount of variable
502  * storage required by each block.
503  * "Max" is the high water mark, "off"
504  * the current need. Temporaries for "for"
505  * loops and "with" statements are allocated
506  * in the local variable area and these
507  * numbers are thereby changed if necessary.
508  */
509 struct om {
510 	long	om_max;
511 	long	reg_max;
512 	struct tmps {
513 		long	om_off;
514 		long	reg_off;
515 	} curtmps;
516 } sizes[DSPLYSZ];
517 #define NOREG 0
518 #define REGOK 1
519 
520     /*
521      *	the following structure records whether a level declares
522      *	any variables which are (or contain) files.
523      *	this so that the runtime routines for file cleanup can be invoked.
524      */
525 bool	dfiles[ DSPLYSZ ];
526 
527 /*
528  * Structure recording information about a constant
529  * declaration.  It is actually the return value from
530  * the routine "gconst", but since C doesn't support
531  * record valued functions, this is more convenient.
532  */
533 struct {
534 	struct nl	*ctype;
535 	short		cival;
536 	double		crval;
537 	int		*cpval;
538 } con;
539 
540 /*
541  * The set structure records the lower bound
542  * and upper bound with the lower bound normalized
543  * to zero when working with a set. It is set by
544  * the routine setran in var.c.
545  */
546 struct {
547 	short	lwrb, uprbp;
548 } set;
549 
550     /*
551      *	structures of this kind are filled in by precset and used by postcset
552      *	to indicate things about constant sets.
553      */
554 struct csetstr {
555     struct nl	*csettype;
556     long	paircnt;
557     long	singcnt;
558     bool	comptime;
559 };
560 /*
561  * The following flags are passed on calls to lvalue
562  * to indicate how the reference is to affect the usage
563  * information for the variable being referenced.
564  * MOD is used to set the NMOD flag in the namelist
565  * entry for the variable, ASGN permits diagnostics
566  * to be formed when a for variable is assigned to in
567  * the range of the loop.
568  */
569 #define	NOFLAGS	0
570 #define	MOD	01
571 #define	ASGN	02
572 #define	NOUSE	04
573 
574     /*
575      *	the following flags are passed to lvalue and rvalue
576      *	to tell them whether an lvalue or rvalue is required.
577      *	the semantics checking is done according to the function called,
578      *	but for pc, lvalue may put out an rvalue by indirecting afterwards,
579      *	and rvalue may stop short of putting out the indirection.
580      */
581 #define	LREQ	01
582 #define	RREQ	02
583 
584 double	MAXINT;
585 double	MININT;
586 
587 /*
588  * Variables for generation of profile information.
589  * Monflg is set when we want to generate a profile.
590  * Gocnt record the total number of goto's and
591  * cnts records the current counter for generating
592  * COUNT operators.
593  */
594 short	gocnt;
595 short	cnts;
596 
597 /*
598  * Most routines call "incompat" rather than asking "!compat"
599  * for historical reasons.
600  */
601 #define incompat 	!compat
602 
603 /*
604  * Parts records which declaration parts have been seen.
605  * The grammar allows the "label" "const" "type" "var" and routine
606  * parts to be repeated and to be in any order, so that
607  * they can be detected semantically to give better
608  * error diagnostics.
609  */
610 int	parts[ DSPLYSZ ];
611 
612 #define	LPRT	1
613 #define	CPRT	2
614 #define	TPRT	4
615 #define	VPRT	8
616 #define	RPRT	16
617 
618 /*
619  * Flags for the "you used / instead of div" diagnostic
620  */
621 bool	divchk;
622 bool	divflg;
623 
624 bool	errcnt[DSPLYSZ];
625 
626 /*
627  * Forechain links those types which are
628  *	^ sometype
629  * so that they can be evaluated later, permitting
630  * circular, recursive list structures to be defined.
631  */
632 struct	nl *forechain;
633 
634 /*
635  * Withlist links all the records which are currently
636  * opened scopes because of with statements.
637  */
638 struct	nl *withlist;
639 
640 struct	nl *intset;
641 struct	nl *input, *output;
642 struct	nl *program;
643 
644 /* progseen flag used by PC to determine if
645  * a routine segment is being compiled (and
646  * therefore no program statement seen)
647  */
648 bool	progseen;
649 
650 
651 /*
652  * STRUCTURED STATEMENT GOTO CHECKING
653  *
654  * The variable level keeps track of the current
655  * "structured statement level" when processing the statement
656  * body of blocks.  This is used in the detection of goto's into
657  * structured statements in a block.
658  *
659  * Each label's namelist entry contains two pieces of information
660  * related to this check. The first `NL_GOLEV' either contains
661  * the level at which the label was declared, `NOTYET' if the label
662  * has not yet been declared, or `DEAD' if the label is dead, i.e.
663  * if we have exited the level in which the label was defined.
664  *
665  * When we discover a "goto" statement, if the label has not
666  * been defined yet, then we record the current level and the current line
667  * for a later error check.  If the label has been already become "DEAD"
668  * then a reference to it is an error.  Now the compiler maintains,
669  * for each block, a linked list of the labels headed by "gotos[bn]".
670  * When we exit a structured level, we perform the routine
671  * ungoto in stat.c. It notices labels whose definition levels have been
672  * exited and makes them be dead. For labels which have not yet been
673  * defined, ungoto will maintain NL_GOLEV as the minimum structured level
674  * since the first usage of the label. It is not hard to see that the label
675  * must eventually be declared at this level or an outer level to this
676  * one or a goto into a structured statement will exist.
677  */
678 short	level;
679 struct	nl *gotos[DSPLYSZ];
680 
681 #define	NOTYET	10000
682 #define	DEAD	10000
683 
684 /*
685  * Noreach is true when the next statement will
686  * be unreachable unless something happens along
687  * (like exiting a looping construct) to save
688  * the day.
689  */
690 bool	noreach;
691 
692 /*
693  * UNDEFINED VARIABLE REFERENCE STRUCTURES
694  */
695 struct	udinfo {
696 	int	ud_line;
697 	struct	udinfo *ud_next;
698 	char	nullch;
699 };
700 
701 /*
702  * CODE GENERATION DEFINITIONS
703  */
704 
705 /*
706  * NSTAND is or'ed onto the abstract machine opcode
707  * for non-standard built-in procedures and functions.
708  */
709 #define	NSTAND	0400
710 
711 #define	codeon()	cgenflg++
712 #define	codeoff()	--cgenflg
713 #define	CGENNING	( cgenflg >= 0 )
714 
715 /*
716  * Codeline is the last lino output in the code generator.
717  * It used to be used to suppress LINO operators but no
718  * more since we now count statements.
719  * Lc is the intepreter code location counter.
720  *
721 short	codeline;
722  */
723 char	*lc;
724 
725 
726 /*
727  * Routines which need types
728  * other than "integer" to be
729  * assumed by the compiler.
730  */
731 double		atof();
732 long		lwidth();
733 long		leven();
734 long		aryconst();
735 long		a8tol();
736 long		roundup();
737 long		tmpalloc();
738 struct nl 	*lookup();
739 double		atof();
740 int		*tree();
741 int		*hash();
742 char		*alloc();
743 int		*calloc();
744 char		*savestr();
745 char		*parnam();
746 bool		fcompat();
747 struct nl	*lookup1();
748 struct nl	*hdefnl();
749 struct nl	*defnl();
750 struct nl	*enter();
751 struct nl	*nlcopy();
752 struct nl	*tyrecl();
753 struct nl	*tyary();
754 struct nl	*fields();
755 struct nl	*variants();
756 struct nl	*deffld();
757 struct nl	*defvnt();
758 struct nl	*tyrec1();
759 struct nl	*reclook();
760 struct nl	*asgnop1();
761 struct nl	*gtype();
762 struct nl	*call();
763 struct nl	*lvalue();
764 struct nl	*rvalue();
765 struct nl	*cset();
766 
767 /*
768  * type cast NIL to keep lint happy (which is not so bad)
769  */
770 #define		NLNIL	( (struct nl *) NIL )
771 
772 /*
773  * Funny structures to use
774  * pointers in wild and wooly ways
775  */
776 struct {
777 	char	pchar;
778 };
779 struct {
780 	short	pint;
781 	short	pint2;
782 };
783 struct {
784 	long	plong;
785 };
786 struct {
787 	double	pdouble;
788 };
789 
790 #define	OCT	1
791 #define	HEX	2
792 
793 /*
794  * MAIN PROGRAM VARIABLES, MISCELLANY
795  */
796 
797 /*
798  * Variables forming a data base referencing
799  * the command line arguments with the "i" option, e.g.
800  * in "pi -i scanner.i compiler.p".
801  */
802 char	**pflist;
803 short	pflstc;
804 short	pfcnt;
805 
806 char	*filename;		/* current source file name */
807 long	tvec;
808 extern char	*snark;		/* SNARK */
809 extern char	*classes[ ];	/* maps namelist classes to string names */
810 
811 #define	derror error
812 
813 #ifdef	PC
814 
815     /*
816      *	the current function number, for [ lines
817      */
818     int	ftnno;
819 
820     /*
821      *	the pc output stream
822      */
823     FILE *pcstream;
824 
825 #endif PC
826