Pull in some of the lpt_port_test fixes from lpt.c.
[unix-history] / usr.bin / f2c / init.c
CommitLineData
f1525c23
WH
1/****************************************************************
2Copyright 1990, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
3
4Permission to use, copy, modify, and distribute this software
5and its documentation for any purpose and without fee is hereby
6granted, provided that the above copyright notice appear in all
7copies and that both that the copyright notice and this
8permission notice and warranty disclaimer appear in supporting
9documentation, and that the names of AT&T Bell Laboratories or
10Bellcore or any of their entities not be used in advertising or
11publicity pertaining to distribution of the software without
12specific, written prior permission.
13
14AT&T and Bellcore disclaim all warranties with regard to this
15software, including all implied warranties of merchantability
16and fitness. In no event shall AT&T or Bellcore be liable for
17any special, indirect or consequential damages or any damages
18whatsoever resulting from loss of use, data or profits, whether
19in an action of contract, negligence or other tortious action,
20arising out of or in connection with the use or performance of
21this software.
22****************************************************************/
23
24#include "defs.h"
25#include "output.h"
26#include "iob.h"
27
28/* State required for the C output */
29char *fl_fmt_string; /* Float format string */
30char *db_fmt_string; /* Double format string */
31char *cm_fmt_string; /* Complex format string */
32char *dcm_fmt_string; /* Double complex format string */
33
34chainp new_vars = CHNULL; /* List of newly created locals in this
35 function. These may have identifiers
36 which have underscores and more than VL
37 characters */
38chainp used_builtins = CHNULL; /* List of builtins used by this function.
39 These are all Addrps with UNAM_EXTERN
40 */
41chainp assigned_fmts = CHNULL; /* assigned formats */
42chainp allargs; /* union of args in all entry points */
43chainp earlylabs; /* labels seen before enddcl() */
44char main_alias[52]; /* PROGRAM name, if any is given */
45int tab_size = 4;
46
47
48FILEP infile;
49FILEP diagfile;
50
51FILEP c_file;
52FILEP pass1_file;
53FILEP initfile;
54FILEP blkdfile;
55
56
57char token[MAXTOKENLEN+2];
58int toklen;
59long lineno; /* Current line in the input file, NOT the
60 Fortran statement label number */
61char *infname;
62int needkwd;
63struct Labelblock *thislabel = NULL;
64int nerr;
65int nwarn;
66
67flag saveall;
68flag substars;
69int parstate = OUTSIDE;
70flag headerdone = NO;
71int blklevel;
72int doin_setbound;
73int impltype[26];
74ftnint implleng[26];
75int implstg[26];
76
77int tyint = TYLONG ;
78int tylogical = TYLONG;
79int tylog = TYLOGICAL;
80int typesize[NTYPES] = {
81 1, SZADDR, 1, SZSHORT, SZLONG,
82#ifdef TYQUAD
83 2*SZLONG,
84#endif
85 SZLONG, 2*SZLONG,
86 2*SZLONG, 4*SZLONG, 1, SZSHORT, SZLONG, 1, 1, 0,
87 4*SZLONG + SZADDR, /* sizeof(cilist) */
88 4*SZLONG + 2*SZADDR, /* sizeof(icilist) */
89 4*SZLONG + 5*SZADDR, /* sizeof(olist) */
90 2*SZLONG + SZADDR, /* sizeof(cllist) */
91 2*SZLONG, /* sizeof(alist) */
92 11*SZLONG + 15*SZADDR /* sizeof(inlist) */
93 };
94
95int typealign[NTYPES] = {
96 1, ALIADDR, 1, ALISHORT, ALILONG,
97#ifdef TYQUAD
98 ALIDOUBLE,
99#endif
100 ALILONG, ALIDOUBLE,
101 ALILONG, ALIDOUBLE, 1, ALISHORT, ALILONG, 1, 1, 1,
102 ALILONG, ALILONG, ALILONG, ALILONG, ALILONG, ALILONG};
103
104int type_choice[4] = { TYDREAL, TYSHORT, TYLONG, TYSHORT };
105
106char *typename[] = {
107 "<<unknown>>",
108 "address",
109 "integer1",
110 "shortint",
111 "integer",
112#ifdef TYQUAD
113 "longint",
114#endif
115 "real",
116 "doublereal",
117 "complex",
118 "doublecomplex",
119 "logical1",
120 "shortlogical",
121 "logical",
122 "char" /* character */
123 };
124
125int type_pref[NTYPES] = { 0, 0, 3, 5, 7,
126#ifdef TYQUAD
127 10,
128#endif
129 8, 11, 9, 12, 1, 4, 6, 2 };
130
131char *protorettypes[] = {
132 "?", "??", "integer1", "shortint", "integer",
133#ifdef TYQUAD
134 "longint",
135#endif
136 "real", "doublereal",
137 "C_f", "Z_f", "logical1", "shortlogical", "logical", "H_f", "int"
138 };
139
140char *casttypes[TYSUBR+1] = {
141 "U_fp", "??bug??", "I1_fp",
142 "J_fp", "I_fp",
143#ifdef TYQUAD
144 "Q_fp",
145#endif
146 "R_fp", "D_fp", "C_fp", "Z_fp",
147 "L1_fp", "L2_fp", "L_fp", "H_fp", "S_fp"
148 };
149char *usedcasts[TYSUBR+1];
150
151char *dfltarg[] = {
152 0, 0, "(integer1 *)0",
153 "(shortint *)0", "(integer *)0",
154#ifdef TYQUAD
155 "(longint *)0",
156#endif
157 "(real *)0",
158 "(doublereal *)0", "(complex *)0", "(doublecomplex *)0",
159 "(logical1 *)0","(shortlogical *)0)", "(logical *)0", "(char *)0"
160 };
161
162static char *dflt0proc[] = {
163 0, 0, "(integer1 (*)())0",
164 "(shortint (*)())0", "(integer (*)())0",
165#ifdef TYQUAD
166 "(longint (*)())0",
167#endif
168 "(real (*)())0",
169 "(doublereal (*)())0", "(complex (*)())0", "(doublecomplex (*)())0",
170 "(logical1 (*)())0", "(shortlogical (*)())0",
171 "(logical (*)())0", "(char (*)())0", "(int (*)())0"
172 };
173
174char *dflt1proc[] = { "(U_fp)0", "(??bug??)0", "(I1_fp)0",
175 "(J_fp)0", "(I_fp)0",
176#ifdef TYQUAD
177 "(Q_fp)0",
178#endif
179 "(R_fp)0", "(D_fp)0", "(C_fp)0", "(Z_fp)0",
180 "(L1_fp)0","(L2_fp)0",
181 "(L_fp)0", "(H_fp)0", "(S_fp)0"
182 };
183
184char **dfltproc = dflt0proc;
185
186static char Bug[] = "bug";
187
188char *ftn_types[] = { "external", "??", "integer*1",
189 "integer*2", "integer",
190#ifdef TYQUAD
191 "integer*8",
192#endif
193 "real",
194 "double precision", "complex", "double complex",
195 "logical*1", "logical*2",
196 "logical", "character", "subroutine",
197 Bug,Bug,Bug,Bug,Bug,Bug,Bug,Bug,Bug, "ftnlen"
198 };
199
200int init_ac[TYSUBR+1] = { 0,0,0,0,0,0,0,
201#ifdef TYQUAD
202 0,
203#endif
204 1, 1, 0, 0, 0, 2};
205
206int proctype = TYUNKNOWN;
207char *procname;
208int rtvlabel[NTYPES0];
209Addrp retslot; /* Holds automatic variable which was
210 allocated the function return value
211 */
212Addrp xretslot[NTYPES0]; /* for multiple entry points */
213int cxslot = -1;
214int chslot = -1;
215int chlgslot = -1;
216int procclass = CLUNKNOWN;
217int nentry;
218int nallargs;
219int nallchargs;
220flag multitype;
221ftnint procleng;
222long lastiolabno;
223int lastlabno;
224int lastvarno;
225int lastargslot;
226int autonum[TYVOID];
227char *av_pfix[TYVOID] = {"??TYUNKNOWN??", "a","i1","s","i",
228#ifdef TYQUAD
229 "i8",
230#endif
231 "r","d","q","z","L1","L2","L","ch",
232 "??TYSUBR??", "??TYERROR??","ci", "ici",
233 "o", "cl", "al", "ioin" };
234
235extern int maxctl;
236struct Ctlframe *ctls;
237struct Ctlframe *ctlstack;
238struct Ctlframe *lastctl;
239
240Namep regnamep[MAXREGVAR];
241int highregvar;
242int nregvar;
243
244extern int maxext;
245Extsym *extsymtab;
246Extsym *nextext;
247Extsym *lastext;
248
249extern int maxequiv;
250struct Equivblock *eqvclass;
251
252extern int maxhash;
253struct Hashentry *hashtab;
254struct Hashentry *lasthash;
255
256extern int maxstno; /* Maximum number of statement labels */
257struct Labelblock *labeltab;
258struct Labelblock *labtabend;
259struct Labelblock *highlabtab;
260
261int maxdim = MAXDIM;
262struct Rplblock *rpllist = NULL;
263struct Chain *curdtp = NULL;
264flag toomanyinit;
265ftnint curdtelt;
266chainp templist[TYVOID];
267chainp holdtemps;
268int dorange = 0;
269struct Entrypoint *entries = NULL;
270
271chainp chains = NULL;
272
273flag inioctl;
274int iostmt;
275int nioctl;
276int nequiv = 0;
277int eqvstart = 0;
278int nintnames = 0;
279extern int maxlablist;
280struct Labelblock **labarray;
281
282struct Literal *litpool;
283int nliterals;
284
285char dflttype[26];
286char hextoi_tab[Table_size], Letters[Table_size];
287char *ei_first, *ei_next, *ei_last;
288char *wh_first, *wh_next, *wh_last;
289
290#define ALLOCN(n,x) (struct x *) ckalloc((n)*sizeof(struct x))
291
292fileinit()
293{
294 register char *s;
295 register int i, j;
296 extern void fmt_init(), mem_init(), np_init();
297
298 lastiolabno = 100000;
299 lastlabno = 0;
300 lastvarno = 0;
301 nliterals = 0;
302 nerr = 0;
303
304 infile = stdin;
305
306 memset(dflttype, tyreal, 26);
307 memset(dflttype + 'i' - 'a', tyint, 6);
308 memset(hextoi_tab, 16, sizeof(hextoi_tab));
309 for(i = 0, s = "0123456789abcdef"; *s; i++, s++)
310 hextoi(*s) = i;
311 for(i = 10, s = "ABCDEF"; *s; i++, s++)
312 hextoi(*s) = i;
313 for(j = 0, s = "abcdefghijklmnopqrstuvwxyz"; i = *s++; j++)
314 Letters[i] = Letters[i+'A'-'a'] = j;
315
316 ctls = ALLOCN(maxctl+1, Ctlframe);
317 extsymtab = ALLOCN(maxext, Extsym);
318 eqvclass = ALLOCN(maxequiv, Equivblock);
319 hashtab = ALLOCN(maxhash, Hashentry);
320 labeltab = ALLOCN(maxstno, Labelblock);
321 litpool = ALLOCN(maxliterals, Literal);
322 labarray = (struct Labelblock **)ckalloc(maxlablist*
323 sizeof(struct Labelblock *));
324 fmt_init();
325 mem_init();
326 np_init();
327
328 ctlstack = ctls++;
329 lastctl = ctls + maxctl;
330 nextext = extsymtab;
331 lastext = extsymtab + maxext;
332 lasthash = hashtab + maxhash;
333 labtabend = labeltab + maxstno;
334 highlabtab = labeltab;
335 main_alias[0] = '\0';
336 if (forcedouble)
337 dfltproc[TYREAL] = dfltproc[TYDREAL];
338
339/* Initialize the routines for providing C output */
340
341 out_init ();
342}
343
344hashclear() /* clear hash table */
345{
346 register struct Hashentry *hp;
347 register Namep p;
348 register struct Dimblock *q;
349 register int i;
350
351 for(hp = hashtab ; hp < lasthash ; ++hp)
352 if(p = hp->varp)
353 {
354 frexpr(p->vleng);
355 if(q = p->vdim)
356 {
357 for(i = 0 ; i < q->ndim ; ++i)
358 {
359 frexpr(q->dims[i].dimsize);
360 frexpr(q->dims[i].dimexpr);
361 }
362 frexpr(q->nelt);
363 frexpr(q->baseoffset);
364 frexpr(q->basexpr);
365 free( (charptr) q);
366 }
367 if(p->vclass == CLNAMELIST)
368 frchain( &(p->varxptr.namelist) );
369 free( (charptr) p);
370 hp->varp = NULL;
371 }
372 }
373
374procinit()
375{
376 register struct Labelblock *lp;
377 struct Chain *cp;
378 int i;
379 struct memblock;
380 extern struct memblock *curmemblock, *firstmemblock;
381 extern char *mem_first, *mem_next, *mem_last, *mem0_last;
382 extern void frexchain();
383
384 curmemblock = firstmemblock;
385 mem_next = mem_first;
386 mem_last = mem0_last;
387 ei_next = ei_first = ei_last = 0;
388 wh_next = wh_first = wh_last = 0;
389 iob_list = 0;
390 for(i = 0; i < 9; i++)
391 io_structs[i] = 0;
392
393 parstate = OUTSIDE;
394 headerdone = NO;
395 blklevel = 1;
396 saveall = NO;
397 substars = NO;
398 nwarn = 0;
399 thislabel = NULL;
400 needkwd = 0;
401
402 proctype = TYUNKNOWN;
403 procname = "MAIN_";
404 procclass = CLUNKNOWN;
405 nentry = 0;
406 nallargs = nallchargs = 0;
407 multitype = NO;
408 retslot = NULL;
409 for(i = 0; i < NTYPES0; i++) {
410 frexpr((expptr)xretslot[i]);
411 xretslot[i] = 0;
412 }
413 cxslot = -1;
414 chslot = -1;
415 chlgslot = -1;
416 procleng = 0;
417 blklevel = 1;
418 lastargslot = 0;
419
420 for(lp = labeltab ; lp < labtabend ; ++lp)
421 lp->stateno = 0;
422
423 hashclear();
424
425/* Clear the list of newly generated identifiers from the previous
426 function */
427
428 frexchain(&new_vars);
429 frexchain(&used_builtins);
430 frchain(&assigned_fmts);
431 frchain(&allargs);
432 frchain(&earlylabs);
433
434 nintnames = 0;
435 highlabtab = labeltab;
436
437 ctlstack = ctls - 1;
438 for(i = TYADDR; i < TYVOID; i++) {
439 for(cp = templist[i]; cp ; cp = cp->nextp)
440 free( (charptr) (cp->datap) );
441 frchain(templist + i);
442 autonum[i] = 0;
443 }
444 holdtemps = NULL;
445 dorange = 0;
446 nregvar = 0;
447 highregvar = 0;
448 entries = NULL;
449 rpllist = NULL;
450 inioctl = NO;
451 eqvstart += nequiv;
452 nequiv = 0;
453 dcomplex_seen = 0;
454
455 for(i = 0 ; i<NTYPES0 ; ++i)
456 rtvlabel[i] = 0;
457
458 if(undeftype)
459 setimpl(TYUNKNOWN, (ftnint) 0, 'a', 'z');
460 else
461 {
462 setimpl(tyreal, (ftnint) 0, 'a', 'z');
463 setimpl(tyint, (ftnint) 0, 'i', 'n');
464 }
465 setimpl(-STGBSS, (ftnint) 0, 'a', 'z'); /* set class */
466 setlog();
467}
468
469
470
471
472setimpl(type, length, c1, c2)
473int type;
474ftnint length;
475int c1, c2;
476{
477 int i;
478 char buff[100];
479
480 if(c1==0 || c2==0)
481 return;
482
483 if(c1 > c2) {
484 sprintf(buff, "characters out of order in implicit:%c-%c", c1, c2);
485 err(buff);
486 }
487 else {
488 c1 = letter(c1);
489 c2 = letter(c2);
490 if(type < 0)
491 for(i = c1 ; i<=c2 ; ++i)
492 implstg[i] = - type;
493 else {
494 type = lengtype(type, length);
495 if(type == TYCHAR) {
496 if (length < 0) {
497 err("length (*) in implicit");
498 length = 1;
499 }
500 }
501 else if (type != TYLONG)
502 length = 0;
503 for(i = c1 ; i<=c2 ; ++i) {
504 impltype[i] = type;
505 implleng[i] = length;
506 }
507 }
508 }
509 }