home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / f2c / src / formatdata.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  26KB  |  1,151 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  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.
  13.  
  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
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "output.h"
  26. #include "names.h"
  27. #include "format.h"
  28.  
  29. #define MAX_INIT_LINE 100
  30. #define NAME_MAX 64
  31.  
  32. static int memno2info Argdcl((int, Namep*));
  33.  
  34.  extern char *initbname;
  35.  
  36.  void
  37. #ifdef KR_headers
  38. list_init_data(Infile, Inname, outfile)
  39.     FILE **Infile;
  40.     char *Inname;
  41.     FILE *outfile;
  42. #else
  43. list_init_data(FILE **Infile, char *Inname, FILE *outfile)
  44. #endif
  45. {
  46.     FILE *sortfp;
  47.     int status;
  48.  
  49.     fclose(*Infile);
  50.     *Infile = 0;
  51.  
  52.     if (status = dsort(Inname, sortfname))
  53.     fatali ("sort failed, status %d", status);
  54.  
  55.     scrub(Inname); /* optionally unlink Inname */
  56.  
  57.     if ((sortfp = fopen(sortfname, textread)) == NULL)
  58.     Fatal("Couldn't open sorted initialization data");
  59.  
  60.     do_init_data(outfile, sortfp);
  61.     fclose(sortfp);
  62.     scrub(sortfname);
  63.  
  64. /* Insert a blank line after any initialized data */
  65.  
  66.     nice_printf (outfile, "\n");
  67.  
  68.     if (debugflag && infname)
  69.      /* don't back block data file up -- it won't be overwritten */
  70.     backup(initfname, initbname);
  71. } /* list_init_data */
  72.  
  73.  
  74.  
  75. /* do_init_data -- returns YES when at least one declaration has been
  76.    written */
  77.  
  78.  int
  79. #ifdef KR_headers
  80. do_init_data(outfile, infile)
  81.     FILE *outfile;
  82.     FILE *infile;
  83. #else
  84. do_init_data(FILE *outfile, FILE *infile)
  85. #endif
  86. {
  87.     char varname[NAME_MAX], ovarname[NAME_MAX];
  88.     ftnint offset;
  89.     ftnint type;
  90.     int vargroup;    /* 0 --> init, 1 --> equiv, 2 --> common */
  91.     int did_one = 0;        /* True when one has been output */
  92.     chainp values = CHNULL;    /* Actual data values */
  93.     int keepit = 0;
  94.     Namep np;
  95.  
  96.     ovarname[0] = '\0';
  97.  
  98.     while (rdname (infile, &vargroup, varname) && rdlong (infile, &offset)
  99.         && rdlong (infile, &type)) {
  100.     if (strcmp (varname, ovarname)) {
  101.  
  102.     /* If this is a new variable name, the old initialization has been
  103.        completed */
  104.  
  105.         wr_one_init(outfile, ovarname, &values, keepit);
  106.  
  107.         strcpy (ovarname, varname);
  108.         values = CHNULL;
  109.         if (vargroup == 0) {
  110.             if (memno2info(atoi(varname+2), &np)) {
  111.                 if (((Addrp)np)->uname_tag != UNAM_NAME) {
  112.                     err("do_init_data: expected NAME");
  113.                     goto Keep;
  114.                     }
  115.                 np = ((Addrp)np)->user.name;
  116.                 }
  117.             if (!(keepit = np->visused) && !np->vimpldovar)
  118.                 warn1("local variable %s never used",
  119.                     np->fvarname);
  120.             }
  121.         else {
  122.  Keep:
  123.             keepit = 1;
  124.             }
  125.         if (keepit && !did_one) {
  126.             nice_printf (outfile, "/* Initialized data */\n\n");
  127.             did_one = YES;
  128.             }
  129.     } /* if strcmp */
  130.  
  131.     values = mkchain((char *)data_value(infile, offset, (int)type), values);
  132.     } /* while */
  133.  
  134. /* Write out the last declaration */
  135.  
  136.     wr_one_init (outfile, ovarname, &values, keepit);
  137.  
  138.     return did_one;
  139. } /* do_init_data */
  140.  
  141.  
  142.  ftnint
  143. #ifdef KR_headers
  144. wr_char_len(outfile, dimp, n, extra1)
  145.     FILE *outfile;
  146.     struct Dimblock *dimp;
  147.     int n;
  148.     int extra1;
  149. #else
  150. wr_char_len(FILE *outfile, struct Dimblock *dimp, int n, int extra1)
  151. #endif
  152. {
  153.     int i, nd;
  154.     expptr e;
  155.     ftnint rv;
  156.  
  157.     if (!dimp) {
  158.         nice_printf (outfile, extra1 ? "[%d+1]" : "[%d]", n);
  159.         return n + extra1;
  160.         }
  161.     nice_printf(outfile, "[%d", n);
  162.     nd = dimp->ndim;
  163.     rv = n;
  164.     for(i = 0; i < nd; i++) {
  165.         e = dimp->dims[i].dimsize;
  166.         if (!ISICON (e))
  167.             err ("wr_char_len:  nonconstant array size");
  168.         else {
  169.             nice_printf(outfile, "*%ld", e->constblock.Const.ci);
  170.             rv *= e->constblock.Const.ci;
  171.             }
  172.         }
  173.     /* extra1 allows for stupid C compilers that complain about
  174.      * too many initializers in
  175.      *    char x[2] = "ab";
  176.      */
  177.     nice_printf(outfile, extra1 ? "+1]" : "]");
  178.     return extra1 ? rv+1 : rv;
  179.     }
  180.  
  181.  static int ch_ar_dim = -1; /* length of each element of char string array */
  182.  static int eqvmemno;    /* kludge */
  183.  
  184.  static void
  185. #ifdef KR_headers
  186. write_char_init(outfile, Values, namep)
  187.     FILE *outfile;
  188.     chainp *Values;
  189.     Namep namep;
  190. #else
  191. write_char_init(FILE *outfile, chainp *Values, Namep namep)
  192. #endif
  193. {
  194.     struct Equivblock *eqv;
  195.     long size;
  196.     struct Dimblock *dimp;
  197.     int i, nd, type;
  198.     expptr ds;
  199.  
  200.     if (!namep)
  201.         return;
  202.     if(nequiv >= maxequiv)
  203.         many("equivalences", 'q', maxequiv);
  204.     eqv = &eqvclass[nequiv];
  205.     eqv->eqvbottom = 0;
  206.     type = namep->vtype;
  207.     size = type == TYCHAR
  208.         ? namep->vleng->constblock.Const.ci
  209.         : typesize[type];
  210.     if (dimp = namep->vdim)
  211.         for(i = 0, nd = dimp->ndim; i < nd; i++) {
  212.             ds = dimp->dims[i].dimsize;
  213.             if (!ISICON(ds))
  214.                 err("write_char_values: nonconstant array size");
  215.             else
  216.                 size *= ds->constblock.Const.ci;
  217.             }
  218.     *Values = revchain(*Values);
  219.     eqv->eqvtop = size;
  220.     eqvmemno = ++lastvarno;
  221.     eqv->eqvtype = type;
  222.     wr_equiv_init(outfile, nequiv, Values, 0);
  223.     def_start(outfile, namep->cvarname, CNULL, "");
  224.     if (type == TYCHAR)
  225.         margin_printf(outfile, "((char *)&equiv_%d)\n\n", eqvmemno);
  226.     else
  227.         margin_printf(outfile, dimp
  228.             ? "((%s *)&equiv_%d)\n\n" : "(*(%s *)&equiv_%d)\n\n",
  229.             c_type_decl(type,0), eqvmemno);
  230.     }
  231.  
  232. /* wr_one_init -- outputs the initialization of the variable pointed to
  233.    by   info.   When   is_addr   is true,   info   is an Addrp; otherwise,
  234.    treat it as a Namep */
  235.  
  236.  void
  237. #ifdef KR_headers
  238. wr_one_init(outfile, varname, Values, keepit)
  239.     FILE *outfile;
  240.     char *varname;
  241.     chainp *Values;
  242.     int keepit;
  243. #else
  244. wr_one_init(FILE *outfile, char *varname, chainp *Values, int keepit)
  245. #endif
  246. {
  247.     static int memno;
  248.     static union {
  249.     Namep name;
  250.     Addrp addr;
  251.     } info;
  252.     Namep namep;
  253.     int is_addr, size, type;
  254.     ftnint last, loc;
  255.     int is_scalar = 0;
  256.     char *array_comment = NULL, *name;
  257.     chainp cp, values;
  258.     extern char datachar[];
  259.     static int e1[3] = {1, 0, 1};
  260.     ftnint x;
  261.     extern int hsize;
  262.  
  263.     if (!keepit)
  264.     goto done;
  265.     if (varname == NULL || varname[1] != '.')
  266.     goto badvar;
  267.  
  268. /* Get back to a meaningful representation; find the given   memno in one
  269.    of the appropriate tables (user-generated variables in the hash table,
  270.    system-generated variables in a separate list */
  271.  
  272.     memno = atoi(varname + 2);
  273.     switch(varname[0]) {
  274.     case 'q':
  275.         /* Must subtract eqvstart when the source file
  276.          * contains more than one procedure.
  277.          */
  278.         wr_equiv_init(outfile, eqvmemno = memno - eqvstart, Values, 0);
  279.         goto done;
  280.     case 'Q':
  281.         /* COMMON initialization (BLOCK DATA) */
  282.         wr_equiv_init(outfile, memno, Values, 1);
  283.         goto done;
  284.     case 'v':
  285.         break;
  286.     default:
  287.  badvar:
  288.         errstr("wr_one_init:  unknown variable name '%s'", varname);
  289.         goto done;
  290.     }
  291.  
  292.     is_addr = memno2info (memno, &info.name);
  293.     if (info.name == (Namep) NULL) {
  294.     err ("wr_one_init -- unknown variable");
  295.     return;
  296.     }
  297.     if (is_addr) {
  298.     if (info.addr -> uname_tag != UNAM_NAME) {
  299.         erri ("wr_one_init -- couldn't get name pointer; tag is %d",
  300.             info.addr -> uname_tag);
  301.         namep = (Namep) NULL;
  302.         nice_printf (outfile, " /* bad init data */");
  303.     } else
  304.         namep = info.addr -> user.name;
  305.     } else
  306.     namep = info.name;
  307.  
  308.     /* check for character initialization */
  309.  
  310.     *Values = values = revchain(*Values);
  311.     type = info.name->vtype;
  312.     if (type == TYCHAR) {
  313.     for(last = 0; values; values = values->nextp) {
  314.         cp = (chainp)values->datap;
  315.         loc = (ftnint)cp->datap;
  316.         if (loc > last) {
  317.             write_char_init(outfile, Values, namep);
  318.             goto done;
  319.             }
  320.         last = (int)cp->nextp->datap == TYBLANK
  321.             ? loc + (int)cp->nextp->nextp->datap
  322.             : loc + 1;
  323.         }
  324.     if (halign && info.name->tag == TNAME) {
  325.         nice_printf(outfile, "static struct { %s fill; char val",
  326.             halign);
  327.         x = wr_char_len(outfile, namep->vdim, ch_ar_dim =
  328.             info.name -> vleng -> constblock.Const.ci, 1);
  329.         if (x %= hsize)
  330.             nice_printf(outfile, "; char fill2[%ld]", hsize - x);
  331.         name = info.name->cvarname;
  332.         nice_printf(outfile, "; } %s_st = { 0,", name);
  333.         wr_output_values(outfile, namep, *Values);
  334.         nice_printf(outfile, " };\n");
  335.         ch_ar_dim = -1;
  336.         def_start(outfile, name, CNULL, name);
  337.         margin_printf(outfile, "_st.val\n");
  338.         goto done;
  339.         }
  340.     }
  341.     else {
  342.     size = typesize[type];
  343.     loc = 0;
  344.     for(; values; values = values->nextp) {
  345.         if ((int)((chainp)values->datap)->nextp->datap == TYCHAR) {
  346.             write_char_init(outfile, Values, namep);
  347.             goto done;
  348.             }
  349.         last = ((long) ((chainp) values->datap)->datap) / size;
  350.         if (last - loc > 4) {
  351.             write_char_init(outfile, Values, namep);
  352.             goto done;
  353.             }
  354.         loc = last;
  355.         }
  356.     }
  357.     values = *Values;
  358.  
  359.     nice_printf (outfile, "static %s ", c_type_decl (type, 0));
  360.  
  361.     if (is_addr)
  362.     write_nv_ident (outfile, info.addr);
  363.     else
  364.     out_name (outfile, info.name);
  365.  
  366.     if (namep)
  367.     is_scalar = namep -> vdim == (struct Dimblock *) NULL;
  368.  
  369.     if (namep && !is_scalar)
  370.     array_comment = type == TYCHAR
  371.         ? 0 : wr_ardecls(outfile, namep->vdim, 1L);
  372.  
  373.     if (type == TYCHAR)
  374.     if (ISICON (info.name -> vleng))
  375.  
  376. /* We'll make single strings one character longer, so that we can use the
  377.    standard C initialization.  All this does is pad an extra zero onto the
  378.    end of the string */
  379.         wr_char_len(outfile, namep->vdim, ch_ar_dim =
  380.             info.name -> vleng -> constblock.Const.ci, e1[Ansi]);
  381.     else
  382.         err ("variable length character initialization");
  383.  
  384.     if (array_comment)
  385.     nice_printf (outfile, "%s", array_comment);
  386.  
  387.     nice_printf (outfile, " = ");
  388.     wr_output_values (outfile, namep, values);
  389.     ch_ar_dim = -1;
  390.     nice_printf (outfile, ";\n");
  391.  done:
  392.     frchain(Values);
  393. } /* wr_one_init */
  394.  
  395.  
  396.  
  397.  
  398.  chainp
  399. #ifdef KR_headers
  400. data_value(infile, offset, type)
  401.     FILE *infile;
  402.     ftnint offset;
  403.     int type;
  404. #else
  405. data_value(FILE *infile, ftnint offset, int type)
  406. #endif
  407. {
  408.     char line[MAX_INIT_LINE + 1], *pointer;
  409.     chainp vals, prev_val;
  410.     char *newval;
  411.  
  412.     if (fgets (line, MAX_INIT_LINE, infile) == NULL) {
  413.     err ("data_value:  error reading from intermediate file");
  414.     return CHNULL;
  415.     } /* if fgets */
  416.  
  417. /* Get rid of the trailing newline */
  418.  
  419.     if (line[0])
  420.     line[strlen (line) - 1] = '\0';
  421.  
  422. #define iswhite(x) (isspace (x) || (x) == ',')
  423.  
  424.     pointer = line;
  425.     prev_val = vals = CHNULL;
  426.  
  427.     while (*pointer) {
  428.     register char *end_ptr, old_val;
  429.  
  430. /* Move   pointer   to the start of the next word */
  431.  
  432.     while (*pointer && iswhite (*pointer))
  433.         pointer++;
  434.     if (*pointer == '\0')
  435.         break;
  436.  
  437. /* Move   end_ptr   to the end of the current word */
  438.  
  439.     for (end_ptr = pointer + 1; *end_ptr && !iswhite (*end_ptr);
  440.         end_ptr++)
  441.         ;
  442.  
  443.     old_val = *end_ptr;
  444.     *end_ptr = '\0';
  445.  
  446. /* Add this value to the end of the list */
  447.  
  448.     if (ONEOF(type, MSKREAL|MSKCOMPLEX))
  449.         newval = cpstring(pointer);
  450.     else
  451.         newval = (char *)atol(pointer);
  452.     if (vals) {
  453.         prev_val->nextp = mkchain(newval, CHNULL);
  454.         prev_val = prev_val -> nextp;
  455.     } else
  456.         prev_val = vals = mkchain(newval, CHNULL);
  457.     *end_ptr = old_val;
  458.     pointer = end_ptr;
  459.     } /* while *pointer */
  460.  
  461.     return mkchain((char *)offset, mkchain((char *)LONG_CAST type, vals));
  462. } /* data_value */
  463.  
  464.  static void
  465. overlapping(Void)
  466. {
  467.     extern char *filename0;
  468.     static int warned = 0;
  469.  
  470.     if (warned)
  471.         return;
  472.     warned = 1;
  473.  
  474.     fprintf(stderr, "Error");
  475.     if (filename0)
  476.         fprintf(stderr, " in file %s", filename0);
  477.     fprintf(stderr, ": overlapping initializations\n");
  478.     nerr++;
  479.     }
  480.  
  481.  static void make_one_const Argdcl((int, union Constant*, chainp));
  482.  static long charlen;
  483.  
  484.  void
  485. #ifdef KR_headers
  486. wr_output_values(outfile, namep, values)
  487.     FILE *outfile;
  488.     Namep namep;
  489.     chainp values;
  490. #else
  491. wr_output_values(FILE *outfile, Namep namep, chainp values)
  492. #endif
  493. {
  494.     int type = TYUNKNOWN;
  495.     struct Constblock Const;
  496.     static expptr Vlen;
  497.  
  498.     if (namep)
  499.         type = namep -> vtype;
  500.  
  501. /* Handle array initializations away from scalars */
  502.  
  503.     if (namep && namep -> vdim)
  504.         wr_array_init (outfile, namep -> vtype, values);
  505.  
  506.     else if (values->nextp && type != TYCHAR)
  507.         overlapping();
  508.  
  509.     else {
  510.         make_one_const(type, &Const.Const, values);
  511.         Const.vtype = type;
  512.         Const.vstg = ONEOF(type, MSKREAL|MSKCOMPLEX) != 0;
  513.         if (type== TYCHAR) {
  514.             if (!Vlen)
  515.                 Vlen = ICON(0);
  516.             Const.vleng = Vlen;
  517.             Vlen->constblock.Const.ci = charlen;
  518.             out_const (outfile, &Const);
  519.             free (Const.Const.ccp);
  520.             }
  521.         else
  522.             out_const (outfile, &Const);
  523.         }
  524.     }
  525.  
  526.  
  527.  void
  528. #ifdef KR_headers
  529. wr_array_init(outfile, type, values)
  530.     FILE *outfile;
  531.     int type;
  532.     chainp values;
  533. #else
  534. wr_array_init(FILE *outfile, int type, chainp values)
  535. #endif
  536. {
  537.     int size = typesize[type];
  538.     long index, main_index = 0;
  539.     int k;
  540.  
  541.     if (type == TYCHAR) {
  542.     nice_printf(outfile, "\"");
  543.     k = 0;
  544.     if (Ansi != 1)
  545.         ch_ar_dim = -1;
  546.     }
  547.     else
  548.     nice_printf (outfile, "{ ");
  549.     while (values) {
  550.     struct Constblock Const;
  551.  
  552.     index = ((long) ((chainp) values->datap)->datap) / size;
  553.     while (index > main_index) {
  554.  
  555. /* Fill with zeros.  The structure shorthand works because the compiler
  556.    will expand the "0" in braces to fill the size of the entire structure
  557.    */
  558.  
  559.         switch (type) {
  560.             case TYREAL:
  561.         case TYDREAL:
  562.             nice_printf (outfile, "0.0,");
  563.             break;
  564.         case TYCOMPLEX:
  565.         case TYDCOMPLEX:
  566.             nice_printf (outfile, "{0},");
  567.             break;
  568.         case TYCHAR:
  569.             nice_printf(outfile, " ");
  570.             break;
  571.         default:
  572.             nice_printf (outfile, "0,");
  573.             break;
  574.         } /* switch */
  575.         main_index++;
  576.     } /* while index > main_index */
  577.  
  578.     if (index < main_index)
  579.         overlapping();
  580.     else switch (type) {
  581.         case TYCHAR:
  582.         { int this_char;
  583.  
  584.         if (k == ch_ar_dim) {
  585.             nice_printf(outfile, "\" \"");
  586.             k = 0;
  587.             }
  588.         this_char = (int) ((chainp) values->datap)->
  589.                 nextp->nextp->datap;
  590.         if ((int)((chainp)values->datap)->nextp->datap == TYBLANK) {
  591.             main_index += this_char;
  592.             k += this_char;
  593.             while(--this_char >= 0)
  594.                 nice_printf(outfile, " ");
  595.             values = values -> nextp;
  596.             continue;
  597.             }
  598.         nice_printf(outfile, str_fmt[this_char], this_char);
  599.         k++;
  600.         } /* case TYCHAR */
  601.             break;
  602.  
  603.         case TYINT1:
  604.         case TYSHORT:
  605.         case TYLONG:
  606. #ifdef TYQUAD
  607.         case TYQUAD:
  608. #endif
  609.         case TYREAL:
  610.         case TYDREAL:
  611.         case TYLOGICAL:
  612.         case TYLOGICAL1:
  613.         case TYLOGICAL2:
  614.         case TYCOMPLEX:
  615.         case TYDCOMPLEX:
  616.         make_one_const(type, &Const.Const, values);
  617.         Const.vtype = type;
  618.         Const.vstg = ONEOF(type, MSKREAL|MSKCOMPLEX) != 0;
  619.         out_const(outfile, &Const);
  620.             break;
  621.         default:
  622.             erri("wr_array_init: bad type '%d'", type);
  623.             break;
  624.     } /* switch */
  625.     values = values->nextp;
  626.  
  627.     main_index++;
  628.     if (values && type != TYCHAR)
  629.         nice_printf (outfile, ",");
  630.     } /* while values */
  631.  
  632.     if (type == TYCHAR) {
  633.     nice_printf(outfile, "\"");
  634.     }
  635.     else
  636.     nice_printf (outfile, " }");
  637. } /* wr_array_init */
  638.  
  639.  
  640.  static void
  641. #ifdef KR_headers
  642. make_one_const(type, storage, values)
  643.     int type;
  644.     union Constant *storage;
  645.     chainp values;
  646. #else
  647. make_one_const(int type, union Constant *storage, chainp values)
  648. #endif
  649. {
  650.     union Constant *Const;
  651.     register char **L;
  652.  
  653.     if (type == TYCHAR) {
  654.     char *str, *str_ptr;
  655.     chainp v, prev;
  656.     int b = 0, k, main_index = 0;
  657.  
  658. /* Find the max length of init string, by finding the highest offset
  659.    value stored in the list of initial values */
  660.  
  661.     for(k = 1, prev = CHNULL, v = values; v; prev = v, v = v->nextp)
  662.         ;
  663.     if (prev != CHNULL)
  664.         k = ((int) (((chainp) prev->datap)->datap)) + 2;
  665.         /* + 2 above for null char at end */
  666.     str = Alloc (k);
  667.     for (str_ptr = str; values; str_ptr++) {
  668.         int index = (int) (((chainp) values->datap)->datap);
  669.  
  670.         if (index < main_index)
  671.         overlapping();
  672.         while (index > main_index++)
  673.         *str_ptr++ = ' ';
  674.  
  675.         k = (int) (((chainp) values->datap)->nextp->nextp->datap);
  676.         if ((int)((chainp)values->datap)->nextp->datap == TYBLANK) {
  677.             b = k;
  678.             break;
  679.             }
  680.         *str_ptr = k;
  681.         values = values -> nextp;
  682.     } /* for str_ptr */
  683.     *str_ptr = '\0';
  684.     Const = storage;
  685.     Const -> ccp = str;
  686.     Const -> ccp1.blanks = b;
  687.     charlen = str_ptr - str;
  688.     } else {
  689.     int i = 0;
  690.     chainp vals;
  691.  
  692.     vals = ((chainp)values->datap)->nextp->nextp;
  693.     if (vals) {
  694.         L = (char **)storage;
  695.         do L[i++] = vals->datap;
  696.             while(vals = vals->nextp);
  697.         }
  698.  
  699.     } /* else */
  700.  
  701. } /* make_one_const */
  702.  
  703.  
  704.  int
  705. #ifdef KR_headers
  706. rdname(infile, vargroupp, name)
  707.     FILE *infile;
  708.     int *vargroupp;
  709.     char *name;
  710. #else
  711. rdname(FILE *infile, int *vargroupp, char *name)
  712. #endif
  713. {
  714.     register int i, c;
  715.  
  716.     c = getc (infile);
  717.  
  718.     if (feof (infile))
  719.     return NO;
  720.  
  721.     *vargroupp = c - '0';
  722.     for (i = 1;; i++) {
  723.     if (i >= NAME_MAX)
  724.         Fatal("rdname: oversize name");
  725.     c = getc (infile);
  726.     if (feof (infile))
  727.         return NO;
  728.     if (c == '\t')
  729.         break;
  730.     *name++ = c;
  731.     }
  732.     *name = 0;
  733.     return YES;
  734. } /* rdname */
  735.  
  736.  int
  737. #ifdef KR_headers
  738. rdlong(infile, n)
  739.     FILE *infile;
  740.     ftnint *n;
  741. #else
  742. rdlong(FILE *infile, ftnint *n)
  743. #endif
  744. {
  745.     register int c;
  746.  
  747.     for (c = getc (infile); !feof (infile) && isspace (c); c = getc (infile))
  748.     ;
  749.  
  750.     if (feof (infile))
  751.     return NO;
  752.  
  753.     for (*n = 0; isdigit (c); c = getc (infile))
  754.     *n = 10 * (*n) + c - '0';
  755.     return YES;
  756. } /* rdlong */
  757.  
  758.  
  759.  static int
  760. #ifdef KR_headers
  761. memno2info(memno, info)
  762.     int memno;
  763.     Namep *info;
  764. #else
  765. memno2info(int memno, Namep *info)
  766. #endif
  767. {
  768.     chainp this_var;
  769.     extern chainp new_vars;
  770.     extern struct Hashentry *hashtab, *lasthash;
  771.     struct Hashentry *entry;
  772.  
  773.     for (this_var = new_vars; this_var; this_var = this_var -> nextp) {
  774.     Addrp var = (Addrp) this_var->datap;
  775.  
  776.     if (var == (Addrp) NULL)
  777.         Fatal("memno2info:  null variable");
  778.     else if (var -> tag != TADDR)
  779.         Fatal("memno2info:  bad tag");
  780.     if (memno == var -> memno) {
  781.         *info = (Namep) var;
  782.         return 1;
  783.     } /* if memno == var -> memno */
  784.     } /* for this_var = new_vars */
  785.  
  786.     for (entry = hashtab; entry < lasthash; ++entry) {
  787.     Namep var = entry -> varp;
  788.  
  789.     if (var && var -> vardesc.varno == memno && var -> vstg == STGINIT) {
  790.         *info = (Namep) var;
  791.         return 0;
  792.     } /* if entry -> vardesc.varno == memno */
  793.     } /* for entry = hashtab */
  794.  
  795.     Fatal("memno2info:  couldn't find memno");
  796.     return 0;
  797. } /* memno2info */
  798.  
  799.  static chainp
  800. #ifdef KR_headers
  801. do_string(outfile, v, nloc)
  802.     FILE *outfile;
  803.     register chainp v;
  804.     ftnint *nloc;
  805. #else
  806. do_string(FILE *outfile, register chainp v, ftnint *nloc)
  807. #endif
  808. {
  809.     register chainp cp, v0;
  810.     ftnint dloc, k, loc;
  811.     unsigned long uk;
  812.     char buf[8], *comma;
  813.  
  814.     nice_printf(outfile, "{");
  815.     cp = (chainp)v->datap;
  816.     loc = (ftnint)cp->datap;
  817.     comma = "";
  818.     for(v0 = v;;) {
  819.         switch((int)cp->nextp->datap) {
  820.             case TYBLANK:
  821.                 k = (ftnint)cp->nextp->nextp->datap;
  822.                 loc += k;
  823.                 while(--k >= 0) {
  824.                     nice_printf(outfile, "%s' '", comma);
  825.                     comma = ", ";
  826.                     }
  827.                 break;
  828.             case TYCHAR:
  829.                 uk = (ftnint)cp->nextp->nextp->datap;
  830.                 sprintf(buf, chr_fmt[uk], uk);
  831.                 nice_printf(outfile, "%s'%s'", comma, buf);
  832.                 comma = ", ";
  833.                 loc++;
  834.                 break;
  835.             default:
  836.                 goto done;
  837.             }
  838.         v0 = v;
  839.         if (!(v = v->nextp))
  840.             break;
  841.         cp = (chainp)v->datap;
  842.         dloc = (ftnint)cp->datap;
  843.         if (loc != dloc)
  844.             break;
  845.         }
  846.  done:
  847.     nice_printf(outfile, "}");
  848.     *nloc = loc;
  849.     return v0;
  850.     }
  851.  
  852.  static chainp
  853. #ifdef KR_headers
  854. Ado_string(outfile, v, nloc)
  855.     FILE *outfile;
  856.     register chainp v;
  857.     ftnint *nloc;
  858. #else
  859. Ado_string(FILE *outfile, register chainp v, ftnint *nloc)
  860. #endif
  861. {
  862.     register chainp cp, v0;
  863.     ftnint dloc, k, loc;
  864.  
  865.     nice_printf(outfile, "\"");
  866.     cp = (chainp)v->datap;
  867.     loc = (ftnint)cp->datap;
  868.     for(v0 = v;;) {
  869.         switch((int)cp->nextp->datap) {
  870.             case TYBLANK:
  871.                 k = (ftnint)cp->nextp->nextp->datap;
  872.                 loc += k;
  873.                 while(--k >= 0)
  874.                     nice_printf(outfile, " ");
  875.                 break;
  876.             case TYCHAR:
  877.                 k = (ftnint)cp->nextp->nextp->datap;
  878.                 nice_printf(outfile, str_fmt[k], k);
  879.                 loc++;
  880.                 break;
  881.             default:
  882.                 goto done;
  883.             }
  884.         v0 = v;
  885.         if (!(v = v->nextp))
  886.             break;
  887.         cp = (chainp)v->datap;
  888.         dloc = (ftnint)cp->datap;
  889.         if (loc != dloc)
  890.             break;
  891.         }
  892.  done:
  893.     nice_printf(outfile, "\"");
  894.     *nloc = loc;
  895.     return v0;
  896.     }
  897.  
  898.  static char *
  899. #ifdef KR_headers
  900. Len(L, type)
  901.     long L;
  902.     int type;
  903. #else
  904. Len(long L, int type)
  905. #endif
  906. {
  907.     static char buf[24];
  908.     if (L == 1 && type != TYCHAR)
  909.         return "";
  910.     sprintf(buf, "[%ld]", L);
  911.     return buf;
  912.     }
  913.  
  914.  void
  915. #ifdef KR_headers
  916. wr_equiv_init(outfile, memno, Values, iscomm)
  917.     FILE *outfile;
  918.     int memno;
  919.     chainp *Values;
  920.     int iscomm;
  921. #else
  922. wr_equiv_init(FILE *outfile, int memno, chainp *Values, int iscomm)
  923. #endif
  924. {
  925.     struct Equivblock *eqv;
  926.     int btype, curtype, dtype, filltype, filltype1, j, k, wasblank, xtype;
  927.     static char Blank[] = "";
  928.     register char *comma = Blank;
  929.     register chainp cp, v;
  930.     chainp sentinel, values, v1, vlast;
  931.     ftnint L, L1, dL, dloc, loc, loc0;
  932.     union Constant Const;
  933.     char imag_buf[50], real_buf[50];
  934.     int szshort = typesize[TYSHORT];
  935.     static char typepref[] = {0, 0, TYINT1, TYSHORT, TYLONG,
  936. #ifdef TYQUAD
  937.                   TYQUAD,
  938. #endif
  939.                   TYREAL, TYDREAL, TYREAL, TYDREAL,
  940.                   TYLOGICAL1, TYLOGICAL2,
  941.                   TYLOGICAL, TYCHAR};
  942.     static char basetype[] = {0, 0, TYCHAR, TYSHORT, TYLONG,
  943. #ifdef TYQUAD
  944.                   TYDREAL,
  945. #endif
  946.                   TYLONG, TYDREAL, TYLONG, TYDREAL,
  947.                   TYCHAR, TYSHORT,
  948.                   TYLONG, TYCHAR};
  949.     extern int htype;
  950.     char *z;
  951.  
  952.     /* add sentinel */
  953.     if (iscomm) {
  954.         L = extsymtab[memno].maxleng;
  955.         xtype = extsymtab[memno].extype;
  956.         }
  957.     else {
  958.         eqv = &eqvclass[memno];
  959.         L = eqv->eqvtop - eqv->eqvbottom;
  960.         xtype = eqv->eqvtype;
  961.         }
  962.  
  963.     if (halign && typealign[typepref[xtype]] < typealign[htype])
  964.         xtype = htype;
  965.     *Values = values = revchain(vlast = *Values);
  966.  
  967.     if (xtype != TYCHAR) {
  968.  
  969.         /* unless the data include a value of the appropriate
  970.          * type, we add an extra element in an attempt
  971.          * to force correct alignment */
  972.  
  973.         btype = basetype[xtype];
  974.         loc = 0;
  975.         for(v = *Values;;v = v->nextp) {
  976.             if (!v) {
  977.                 dtype = typepref[xtype];
  978.                 z = ISREAL(dtype) ? cpstring("0.") : (char *)0;
  979.                 k = typesize[dtype];
  980.                 if (j = L % k)
  981.                     L += k - j;
  982.                 v = mkchain((char *)L,
  983.                     mkchain((char *)LONG_CAST dtype,
  984.                         mkchain(z, CHNULL)));
  985.                 vlast = vlast->nextp =
  986.                     mkchain((char *)v, CHNULL);
  987.                 L += k;
  988.                 break;
  989.                 }
  990.             cp = (chainp)v->datap;
  991.             if (basetype[(int)cp->nextp->datap] == btype)
  992.                 break;
  993.             dloc = (ftnint)cp->datap;
  994.             L1 = dloc - loc;
  995.             if (L1 > 0
  996.              && !(L1 % szshort)
  997.              && !(loc % szshort)
  998.              && btype <= type_choice[L1/szshort % 4]
  999.              && btype <= type_choice[loc/szshort % 4])
  1000.                 break;
  1001.             dtype = (int)cp->nextp->datap;
  1002.             loc = dloc + dtype == TYBLANK
  1003.                     ? (ftnint)cp->nextp->nextp->datap
  1004.                     : typesize[dtype];
  1005.             }
  1006.         }
  1007.     sentinel = mkchain((char *)L, mkchain((char *)TYERROR,CHNULL));
  1008.     vlast->nextp = mkchain((char *)sentinel, CHNULL);
  1009.  
  1010.     /* use doublereal fillers only if there are doublereal values */
  1011.  
  1012.     k = TYLONG;
  1013.     for(v = values; v; v = v->nextp)
  1014.         if (ONEOF((int)((chainp)v->datap)->nextp->datap,
  1015.                 M(TYDREAL)|M(TYDCOMPLEX))) {
  1016.             k = TYDREAL;
  1017.             break;
  1018.             }
  1019.     type_choice[0] = k;
  1020.  
  1021.     nice_printf(outfile, "%sstruct {\n", iscomm ? "" : "static ");
  1022.     next_tab(outfile);
  1023.     loc = loc0 = k = 0;
  1024.     curtype = -1;
  1025.     for(v = values; v; v = v->nextp) {
  1026.         cp = (chainp)v->datap;
  1027.         dloc = (ftnint)cp->datap;
  1028.         L = dloc - loc;
  1029.         if (L < 0) {
  1030.             overlapping();
  1031.             if ((int)cp->nextp->datap != TYERROR) {
  1032.                 v1 = cp;
  1033.                 frchain(&v1);
  1034.                 v->datap = 0;
  1035.                 }
  1036.             continue;
  1037.             }
  1038.         dtype = (int)cp->nextp->datap;
  1039.         if (dtype == TYBLANK) {
  1040.             dtype = TYCHAR;
  1041.             wasblank = 1;
  1042.             }
  1043.         else
  1044.             wasblank = 0;
  1045.         if (curtype != dtype || L > 0) {
  1046.             if (curtype != -1) {
  1047.                 L1 = (loc - loc0)/dL;
  1048.                 nice_printf(outfile, "%s e_%d%s;\n",
  1049.                     typename[curtype], ++k,
  1050.                     Len(L1,curtype));
  1051.                 }
  1052.             curtype = dtype;
  1053.             loc0 = dloc;
  1054.             }
  1055.         if (L > 0) {
  1056.             if (xtype == TYCHAR)
  1057.                 filltype = TYCHAR;
  1058.             else {
  1059.                 filltype = L % szshort ? TYCHAR
  1060.                         : type_choice[L/szshort % 4];
  1061.                 filltype1 = loc % szshort ? TYCHAR
  1062.                         : type_choice[loc/szshort % 4];
  1063.                 if (typesize[filltype] > typesize[filltype1])
  1064.                     filltype = filltype1;
  1065.                 }
  1066.             L1 = L / typesize[filltype];
  1067.             nice_printf(outfile, "%s fill_%d[%ld];\n",
  1068.                 typename[filltype], ++k, L1);
  1069.             loc = dloc;
  1070.             }
  1071.         if (wasblank) {
  1072.             loc += (ftnint)cp->nextp->nextp->datap;
  1073.             dL = 1;
  1074.             }
  1075.         else {
  1076.             dL = typesize[dtype];
  1077.             loc += dL;
  1078.             }
  1079.         }
  1080.     nice_printf(outfile, "} %s = { ", iscomm
  1081.         ? extsymtab[memno].cextname
  1082.         : equiv_name(eqvmemno, CNULL));
  1083.     loc = 0;
  1084.     for(v = values; ; v = v->nextp) {
  1085.         cp = (chainp)v->datap;
  1086.         if (!cp)
  1087.             continue;
  1088.         dtype = (int)cp->nextp->datap;
  1089.         if (dtype == TYERROR)
  1090.             break;
  1091.         dloc = (ftnint)cp->datap;
  1092.         if (dloc > loc) {
  1093.             nice_printf(outfile, "%s{0}", comma);
  1094.             comma = ", ";
  1095.             loc = dloc;
  1096.             }
  1097.         if (comma != Blank)
  1098.             nice_printf(outfile, ", ");
  1099.         comma = ", ";
  1100.         if (dtype == TYCHAR || dtype == TYBLANK) {
  1101.             v =  Ansi == 1  ? Ado_string(outfile, v, &loc)
  1102.                     :  do_string(outfile, v, &loc);
  1103.             continue;
  1104.             }
  1105.         make_one_const(dtype, &Const, v);
  1106.         switch(dtype) {
  1107.             case TYLOGICAL:
  1108.             case TYLOGICAL2:
  1109.             case TYLOGICAL1:
  1110.                 if (Const.ci < 0 || Const.ci > 1)
  1111.                     errl(
  1112.               "wr_equiv_init: unexpected logical value %ld",
  1113.                         Const.ci);
  1114.                 nice_printf(outfile,
  1115.                     Const.ci ? "TRUE_" : "FALSE_");
  1116.                 break;
  1117.             case TYINT1:
  1118.             case TYSHORT:
  1119.             case TYLONG:
  1120. #ifdef TYQUAD
  1121.             case TYQUAD:
  1122. #endif
  1123.                 nice_printf(outfile, "%ld", Const.ci);
  1124.                 break;
  1125.             case TYREAL:
  1126.                 nice_printf(outfile, "%s",
  1127.                     flconst(real_buf, Const.cds[0]));
  1128.                 break;
  1129.             case TYDREAL:
  1130.                 nice_printf(outfile, "%s", Const.cds[0]);
  1131.                 break;
  1132.             case TYCOMPLEX:
  1133.                 nice_printf(outfile, "%s, %s",
  1134.                     flconst(real_buf, Const.cds[0]),
  1135.                     flconst(imag_buf, Const.cds[1]));
  1136.                 break;
  1137.             case TYDCOMPLEX:
  1138.                 nice_printf(outfile, "%s, %s",
  1139.                     Const.cds[0], Const.cds[1]);
  1140.                 break;
  1141.             default:
  1142.                 erri("unexpected type %d in wr_equiv_init",
  1143.                     dtype);
  1144.             }
  1145.         loc += typesize[dtype];
  1146.         }
  1147.     nice_printf(outfile, " };\n\n");
  1148.     prev_tab(outfile);
  1149.     frchain(&sentinel);
  1150.     }
  1151.