1 /****************************************************************
2 Copyright 1990, 1991 by AT&T Bell Laboratories, Bellcore.
4 Permission to use, copy, modify, and distribute this software
5 and its documentation for any purpose and without fee is hereby
6 granted, provided that the above copyright notice appear in all
7 copies and that both that the copyright notice and this
8 permission notice and warranty disclaimer appear in supporting
9 documentation, and that the names of AT&T Bell Laboratories or
10 Bellcore or any of their entities not be used in advertising or
11 publicity pertaining to distribution of the software without
12 specific, written prior permission.
14 AT&T and Bellcore disclaim all warranties with regard to this
15 software, including all implied warranties of merchantability
16 and fitness. In no event shall AT&T or Bellcore be liable for
17 any special, indirect or consequential damages or any damages
18 whatsoever resulting from loss of use, data or profits, whether
19 in an action of contract, negligence or other tortious action,
20 arising out of or in connection with the use or performance of
22 ****************************************************************/
31 #define MAXINCLUDES 10
32 #define MAXLITERALS 200 /* Max number of constants in the literal
34 #define MAXTOKENLEN 302 /* length of longest token */
40 #define MAXLABLIST 125 /* Max number of labels in an alternate
43 /* These are the primary pointer types used in the compiler */
45 typedef union Expression *expptr, *tagptr;
46 typedef struct Chain *chainp;
47 typedef struct Addrblock *Addrp;
48 typedef struct Constblock *Constp;
49 typedef struct Exprblock *Exprp;
50 typedef struct Nameblock *Namep;
54 extern FILEP diagfile;
55 extern FILEP textfile;
57 extern FILEP c_file; /* output file for all functions; extern
58 declarations will have to be prepended */
59 extern FILEP pass1_file; /* Temp file to hold the function bodies
61 extern FILEP expr_file; /* Debugging file */
62 extern FILEP initfile; /* Intermediate data file pointer */
63 extern FILEP blkdfile; /* BLOCK DATA file */
65 extern int current_ftn_file;
67 extern char *blkdfname, *initfname, *sortfname;
68 extern long int headoffset; /* Since the header block requires data we
69 don't know about until AFTER each
70 function has been processed, we keep a
71 pointer to the current (dummy) header
72 block (at the top of the assembly file)
75 extern char main_alias[]; /* name given to PROGRAM psuedo-op */
76 extern char token [ ];
81 extern struct Labelblock *thislabel;
83 /* Used to allow runtime expansion of internal tables. In particular,
84 these values can exceed their associated constants */
92 extern flag nowarnflag;
93 extern flag ftn66flag; /* Generate warnings when weird f77
94 features are used (undeclared dummy
95 procedure, non-char initialized with
96 string, 1-dim subscript in EQUIV) */
97 extern flag no66flag; /* Generate an error when a generic
98 function (f77 feature) is used */
99 extern flag noextflag; /* Generate an error when an extension to
100 Fortran 77 is used (hex/oct/bin
101 constants, automatic, static, double
103 extern flag zflag; /* enable double complex intrinsics */
104 extern flag shiftcase;
105 extern flag undeftype;
106 extern flag shortsubs; /* Use short subscripts on arrays? */
107 extern flag onetripflag; /* if true, always execute DO loop body */
108 extern flag checksubs;
109 extern flag debugflag;
114 extern flag headerdone; /* True iff the current procedure's header
115 data has been written */
118 extern flag substars; /* True iff some formal parameter is an
120 extern int impltype[ ];
121 extern ftnint implleng[ ];
122 extern int implstg[ ];
124 extern int tycomplex, tyint, tyioint, tyreal;
125 extern int tylogical; /* TY____ of the implementation of logical.
126 This will be LONG unless '-2' is given
127 on the command line */
128 extern int type_choice[];
129 extern char *typename[];
131 extern int typesize[]; /* size (in bytes) of an object of each
132 type. Indexed by TY___ macros */
133 extern int typealign[];
134 extern int proctype; /* Type of return value in this procedure */
135 extern char * procname; /* External name of the procedure, or last ENTRY name */
136 extern int rtvlabel[ ]; /* Return value labels, indexed by TY___ macros */
137 extern Addrp retslot;
138 extern Addrp xretslot[];
139 extern int cxslot; /* Complex return argument slot (frame pointer offset)*/
140 extern int chslot; /* Character return argument slot (fp offset) */
141 extern int chlgslot; /* Argument slot for length of character buffer */
142 extern int procclass; /* Class of the current procedure: either CLPROC,
143 CLMAIN, CLBLOCK or CLUNKNOWN */
144 extern ftnint procleng; /* Length of function return value (e.g. char
145 string length). If this is -1, then the length is
146 not known at compile time */
147 extern int nentry; /* Number of entry points (other than the original
148 function call) into this procedure */
149 extern flag multitype; /* YES iff there is more than one return value
152 extern long lastiolabno;
153 extern int lastlabno;
154 extern int lastvarno;
155 extern int lastargslot; /* integer offset pointing to the next free
156 location for an argument to the current routine */
158 extern int autonum[]; /* for numbering
159 automatic variables, e.g. temporaries */
161 extern int ret0label;
162 extern int dorange; /* Number of the label which terminates
163 the innermost DO loop */
164 extern int regnum[ ]; /* Numbers of DO indicies named in
166 extern Namep regnamep[ ]; /* List of DO indicies in registers */
167 extern int maxregvar; /* number of elts in regnamep */
168 extern int highregvar; /* keeps track of the highest register
169 number used by DO index allocator */
170 extern int nregvar; /* count of DO indicies in registers */
172 extern chainp templist[];
174 extern chainp earlylabs;
175 extern chainp holdtemps;
176 extern struct Entrypoint *entries;
177 extern struct Rplblock *rpllist;
178 extern struct Chain *curdtp;
179 extern ftnint curdtelt;
180 extern chainp allargs; /* union of args in entries */
181 extern int nallargs; /* total number of args */
182 extern int nallchargs; /* total number of character args */
183 extern flag toomanyinit; /* True iff too many initializers in a
191 extern int eqvstart; /* offset to eqv number to guarantee uniqueness
192 and prevent <something> from going negative */
193 extern int nintnames;
195 /* Chain of tagged blocks */
200 char * datap; /* Tagged block */
203 extern chainp chains;
205 /* Recall that field is intended to hold four-bit characters */
207 /* This structure exists only to defeat the type checking */
215 expptr vleng; /* Expression for length of char string -
216 this may be a constant, or an argument
217 generated by mkarg() */
220 /* Control construct info (for do loops, else, etc) */
225 unsigned dostepsign:8; /* 0 - variable, 1 - pos, 2 - neg */
227 int ctlabels[4]; /* Control labels, defined below */
228 int dolabel; /* label marking end of this DO loop */
229 Namep donamep; /* DO index variable */
230 expptr domax; /* constant or temp variable holding MAX
231 loop value; or expr of while(expr) */
232 expptr dostep; /* expression */
235 #define endlabel ctlabels[0]
236 #define elselabel ctlabels[1]
237 #define dobodylabel ctlabels[1]
238 #define doposlabel ctlabels[2]
239 #define doneglabel ctlabels[3]
240 extern struct Ctlframe *ctls; /* Keeps info on DO and BLOCK IF
241 structures - this is the stack
243 extern struct Ctlframe *ctlstack; /* Pointer to current nesting
245 extern struct Ctlframe *lastctl; /* Point to end of
246 dynamically-allocated array */
258 /* External Symbols */
262 char *fextname; /* Fortran version of external name */
263 char *cextname; /* C version of external name */
264 field extstg; /* STG -- should be COMMON, UNKNOWN or EXT
266 unsigned extype:4; /* for transmitting type to output routines */
267 unsigned used_here:1; /* Boolean - true on the second pass
268 through a function if the block has
270 unsigned exused:1; /* Has been used (for help with error msgs
271 about externals typed differently in
272 different modules) */
273 unsigned exproto:1; /* type specified in a .P file */
274 unsigned extinit:1; /* Procedure has been defined,
275 or COMMON has DATA */
276 unsigned extseen:1; /* True if previously referenced */
277 chainp extp; /* List of identifiers in the common
278 block for this function, stored as
279 Namep (hash table pointers) */
280 chainp allextp; /* List of lists of identifiers; we keep one
281 list for each layout of this common block */
282 int curno; /* current number for this common block,
283 used for constructing appending _nnn
284 to the common block name */
285 int maxno; /* highest curno value for this common block */
290 typedef struct Extsym Extsym;
292 extern Extsym *extsymtab; /* External symbol table */
293 extern Extsym *nextext;
294 extern Extsym *lastext;
295 extern int complex_seen, dcomplex_seen;
297 /* Statement labels */
301 int labelno; /* Internal label */
302 unsigned blklevel:8; /* level of nesting , for branch-in-loop
305 unsigned fmtlabused:1;
306 unsigned labinacc:1; /* inaccessible? (i.e. has its scope
308 unsigned labdefined:1; /* YES or NO */
309 unsigned labtype:2; /* LAB{FORMAT,EXEC,etc} */
310 ftnint stateno; /* Original label */
311 char *fmtstring; /* format string */
314 extern struct Labelblock *labeltab; /* Label table - keeps track of
315 all labels, including undefined */
316 extern struct Labelblock *labtabend;
317 extern struct Labelblock *highlabtab;
319 /* Entry point list */
323 struct Entrypoint *entnextp;
324 Extsym *entryname; /* Name of this ENTRY */
326 int typelabel; /* Label for function exit; this
327 will return the proper type of
329 Namep enamep; /* External name */
332 /* Primitive block, or Primary block. This is a general template returned
333 by the parser, which will be interpreted in context. It is a template
334 for an identifier (variable name, function name), parenthesized
335 arguments (array subscripts, function parameters) and substring
342 Namep namep; /* Pointer to structure Nameblock */
343 struct Listblock *argsp;
344 expptr fcharp; /* first-char-index-pointer (in
346 expptr lcharp; /* last-char-index-pointer (in
356 extern struct Hashentry *hashtab; /* Hash table */
357 extern struct Hashentry *lasthash;
359 struct Intrpacked /* bits for intrinsic function description */
373 expptr vleng; /* length of character string, if applicable */
374 char *fvarname; /* name in the Fortran source */
375 char *cvarname; /* name in the resulting C */
376 chainp vlastdim; /* datap points to new_vars entry for the */
377 /* system variable, if any, storing the final */
378 /* dimension; we zero the datap if this */
379 /* variable is needed */
380 unsigned vprocclass:3; /* P____ macros - selects the varxptr
382 unsigned vdovar:1; /* "is it a DO variable?" for register
383 and multi-level loop checking */
384 unsigned vdcldone:1; /* "do I think I'm done?" - set when the
385 context is sufficient to determine its
387 unsigned vadjdim:1; /* "adjustable dimension?" - needed for
388 information about copies */
390 unsigned vimpldovar:1; /* used to prevent erroneous error messages
391 for variables used only in DATA stmt
393 unsigned vis_assigned:1;/* True if this variable has had some
394 label ASSIGNED to it; hence
395 varxptr.assigned_values is valid */
396 unsigned vimplstg:1; /* True if storage type is assigned implicitly;
397 this allows a COMMON variable to participate
398 in a DIMENSION before the COMMON declaration.
400 unsigned vcommequiv:1; /* True if EQUIVALENCEd onto STGCOMMON */
401 unsigned vfmt_asg:1; /* True if char *var_fmt needed */
402 unsigned vpassed:1; /* True if passed as a character-variable arg */
403 unsigned vknownarg:1; /* True if seen in a previous entry point */
404 unsigned visused:1; /* True if variable is referenced -- so we */
405 /* can omit variables that only appear in DATA */
406 unsigned vnamelist:1; /* Appears in a NAMELIST */
407 unsigned vimpltype:1; /* True if implicitly typed and not
408 invoked as a function or subroutine
409 (so we can consistently type procedures
410 declared external and passed as args
413 unsigned vtypewarned:1; /* so we complain just once about
414 changed types of external procedures */
415 unsigned vinftype:1; /* so we can restore implicit type to a
416 procedure if it is invoked as a function
417 after being given a different type by -it */
418 unsigned vinfproc:1; /* True if -it infers this to be a procedure */
419 unsigned vcalled:1; /* has been invoked */
420 unsigned vdimfinish:1; /* need to invoke dim_finish() */
422 /* The vardesc union below is used to store the number of an intrinsic
423 function (when vstg == STGINTR and vprocclass == PINTRINSIC), or to
424 store the index of this external symbol in extsymtab (when vstg ==
425 STGEXT and vprocclass == PEXTERNAL) */
428 int varno; /* Return variable for a function.
429 This is used when a function is
430 assigned a return value. Also
431 used to point to the COMMON
432 block, when this is a field of
433 that block. Also points to
434 EQUIV block when STGEQUIV */
435 struct Intrpacked intrdesc; /* bits for intrinsic function*/
437 struct Dimblock *vdim; /* points to the dimensions if they exist */
438 ftnint voffset; /* offset in a storage block (the variable
439 name will be "v.%d", voffset in a
440 common blck on the vax). Also holds
441 pointers for automatic variables. When
442 STGEQUIV, this is -(offset from array
445 chainp namelist; /* points to names in the NAMELIST,
446 if this is a NAMELIST name */
447 chainp vstfdesc; /* points to (formals, expr) pair */
448 chainp assigned_values; /* list of integers, each being a
449 statement label assigned to
450 this variable in the current function */
452 int argno; /* for multiple entries */
457 /* PARAMETER statements */
472 /* Expression block */
480 expptr vleng; /* in the case of a character expression, this
481 value is inherited from the children */
494 ftnint ci; /* Constant long integer */
498 #define ccp ccp1.ccp0
505 field vstg; /* vstg = 1 when using Const.cds */
507 union Constant Const;
520 /* Address block - this is the FINAL form of identifiers before being
521 sent to pass 2. We'll want to add the original identifier here so that it can
522 be preserved in the translation.
524 An example identifier is q.7. The "q" refers to the storage class
525 (field vstg), the 7 to the variable number (int memno). */
534 /* put union...user here so the beginning of an Addrblock
535 * is the same as a Constblock.
538 Namep name; /* contains a pointer into the hash table */
539 char ident[IDENT_LEN + 1]; /* C string form of identifier */
541 union Constant Const; /* Constant value */
545 } kludge; /* so we can distinguish string vs binary
546 * floating-point constants */
548 long memno; /* when vstg == STGCONST, this is the
549 numeric part of the assembler label
550 where the constant value is stored */
551 expptr memoffset; /* used in subscript computations, usually */
552 unsigned istemp:1; /* used in stack management of temporary
554 unsigned isarray:1; /* used to show that memoffset is
555 meaningful, even if zero */
556 unsigned ntempelt:10; /* for representing temporary arrays, as
558 unsigned dbl_builtin:1; /* builtin to be declared double */
559 unsigned charleng:1; /* so saveargtypes can get i/o calls right */
560 ftnint varleng; /* holds a copy of a constant length which
561 is stored in the vleng field (e.g.
562 a double is 8 bytes) */
563 int uname_tag; /* Tag describing which of the unions()
565 char *Field; /* field name when dereferencing a struct */
566 }; /* struct Addrblock */
569 /* Errorbock - placeholder for errors, to allow the compilation to
579 /* Implicit DO block, especially related to DATA statements. This block
580 keeps track of the compiler's location in the implicit DO while it's
581 running. In particular, the isactive and isbusy flags tell where
597 struct Chain *datalist;
601 /* Each of these components has a first field called tag. This union
602 exists just for allocation simplicity */
607 struct Addrblock addrblock;
608 struct Constblock constblock;
609 struct Errorblock errorblock;
610 struct Exprblock exprblock;
611 struct Headblock headblock;
612 struct Impldoblock impldoblock;
613 struct Listblock listblock;
614 struct Nameblock nameblock;
615 struct Paramblock paramblock;
616 struct Primblock primblock;
624 expptr nelt; /* This is NULL if the array is unbounded */
625 expptr baseoffset; /* a constant or local variable holding
626 the offset in this procedure */
627 expptr basexpr; /* expression for comuting the offset, if
628 it's not constant. If this is
629 non-null, the register named in
630 baseoffset will get initialized to this
631 value in the procedure's prolog */
634 expptr dimsize; /* constant or register holding the size
636 expptr dimexpr; /* as above in basexpr, this is an
637 expression for computing a variable
639 } dims[1]; /* Dimblocks are allocated with enough
640 space for this to become dims[ndim] */
644 /* Statement function identifier stack - this holds the name and value of
645 the parameters in a statement function invocation. For example,
652 generates a stack of depth 3, with <x 1>, <y 2>, <z 3> AT THE INVOCATION, NOT
655 struct Rplblock /* name replacement block */
657 struct Rplblock *rplnextp;
658 Namep rplnp; /* Name of the formal parameter */
659 expptr rplvp; /* Value of the actual parameter */
660 expptr rplxp; /* Initialization of temporary variable,
661 if required; else null */
662 int rpltag; /* Tag on the value of the actual param */
667 /* Equivalence block */
671 struct Eqvchain *equivs; /* List (Eqvchain) of primblocks
672 holding variable identifiers */
678 #define eqvleng eqvtop
680 extern struct Equivblock *eqvclass;
685 struct Eqvchain *eqvnextp;
688 struct Primblock *eqvlhs;
696 /* For allocation purposes only, and to keep lint quiet. In particular,
697 don't count on the tag being able to tell you which structure is used */
700 /* There is a tradition in Fortran that the compiler not generate the same
701 bit pattern more than is necessary. This structure is used to do just
702 that; if two integer constants have the same bit pattern, just generate
703 it once. This could be expanded to optimize without regard to type, by
704 removing the type check in putconst() */
709 short litnum; /* numeric part of the assembler
710 label for this constant value */
711 int lituse; /* usage count */
715 ftnint litival2[2]; /* length, nblanks for strings */
720 extern struct Literal *litpool;
721 extern int maxliterals, nliterals;
722 extern char Letters[];
723 #define letter(x) Letters[x]
725 struct Dims { expptr lb, ub; };
728 /* popular functions with non integer return values */
732 char *varstr(), *nounder(), *addunder();
733 char *copyn(), *copys();
734 chainp hookup(), mkchain(), revchain();
740 struct Labelblock *mklabel(), *execlab();
741 Extsym *mkext(), *newentry();
742 expptr addrof(), call1(), call2(), call3(), call4();
743 Addrp builtin(), mktmp(), mktmp0(), mktmpn(), autovar();
744 Addrp mkplace(), mkaddr(), putconst(), memversion();
745 expptr mkprim(), mklhs(), mkexpr(), mkconv(), mkfunct(), fixexpr(), fixtype();
746 expptr errnode(), mkaddcon(), mkintcon(), putcxop();
748 ftnint lmin(), lmax(), iarrlen();
749 char *dbconst(), *flconst();
751 void puteq (), putex1 ();
752 expptr putx (), putsteq (), putassign ();
754 extern int forcedouble; /* force real functions to double */
755 extern int doin_setbound; /* special handling for array bounds */
757 extern char *cds(), *cpstring(), *dtos(), *string_num();
758 extern char *c_type_decl();
759 extern char hextoi_tab[];
760 #define hextoi(x) hextoi_tab[(x) & 0xff]
761 extern char *casttypes[], *ftn_types[], *protorettypes[], *usedcasts[];
762 extern int Castargs, infertypes;
763 extern FILE *protofile;
764 extern void exit(), inferdcl(), protowrite(), save_argtypes();
765 extern char binread[], binwrite[], textread[], textwrite[];
766 extern char *ei_first, *ei_last, *ei_next;
767 extern char *wh_first, *wh_last, *wh_next;
768 extern void putwhile();