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