home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / mips-tfile.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  118KB  |  4,273 lines

  1. /* Update the symbol table (the .T file) in a MIPS object to
  2.    contain debugging information specified by the GNU compiler
  3.    in the form of comments (the mips assembler does not support
  4.    assembly access to debug information).
  5.    Contributed by:  Michael Meissner, meissner@osf.org
  6.    Copyright (C) 1991 Free Software Foundation, Inc.
  7.  
  8. This file is part of GNU CC.
  9.  
  10. GNU CC is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. GNU CC is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU CC; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. #include <stdio.h>
  25. #include "gvarargs.h"
  26. #include "config.h"
  27.  
  28. #ifndef __SABER__
  29. #define saber_stop()
  30. #endif
  31.  
  32. #ifndef __LINE__
  33. #define __LINE__ 0
  34. #endif
  35.  
  36. #ifdef __STDC__
  37. typedef void *PTR_T;
  38. typedef const void *CPTR_T;
  39. #define __proto(x) x
  40. #else
  41.  
  42. #ifdef _STDIO_H_        /* Ultrix 4.0 */
  43. typedef void *PTR_T;
  44. typedef void *CPTR_T;
  45.  
  46. #else
  47. typedef char *PTR_T;        /* Ultrix 3.1 */
  48. typedef char *CPTR_T;
  49. #endif
  50.  
  51. #define __proto(x) ()
  52. #define const
  53. #endif
  54.  
  55. /* Do to size_t being defined in sys/types.h and different
  56.    in stddef.h, we have to do this by hand.....  Note, these
  57.    types are correct for MIPS based systems, and may not be
  58.    correct for other systems.  Ultrix 4.0 and Silicon Graphics
  59.    have this fixed, but since the following is correct, and
  60.    the fact that including stddef.h gets you GCC's version
  61.    instead of the standard one it's not worth it to fix it.  */
  62.  
  63. #define Size_t        unsigned int
  64. #define Ptrdiff_t    int
  65.  
  66. /* The following might be called from obstack or malloc,
  67.    so they can't be static.  */
  68.  
  69. extern void    pfatal_with_name
  70.                 __proto((char *));
  71. extern void    fancy_abort    __proto((void));
  72. extern void    botch        __proto((const char *));
  73. extern PTR_T    xmalloc        __proto((Size_t));
  74. extern PTR_T    xcalloc        __proto((Size_t, Size_t));
  75. extern PTR_T    xrealloc    __proto((PTR_T, Size_t));
  76. extern void    xfree        __proto((PTR_T));
  77.  
  78. extern PTR_T    malloc        __proto((Size_t));
  79. extern PTR_T    calloc        __proto((Size_t, Size_t));
  80. extern PTR_T    realloc        __proto((PTR_T, Size_t));
  81. extern void    free        __proto((PTR_T));
  82.  
  83. extern void    fatal();    /* can't use prototypes here */
  84. extern void    error();
  85.  
  86.  
  87. #ifndef MIPS_DEBUGGING_INFO
  88.  
  89. static int     line_number;
  90. static int     cur_line_start;
  91. static int     debug;
  92. static int     had_errors;
  93. static char    *progname;
  94. static char    *input_name;
  95.  
  96. int
  97. main ()
  98. {
  99.   fprintf (stderr, "Mips-tfile should only be run on a MIPS computer!\n");
  100.   exit (1);
  101. }
  102.  
  103. #else                /* MIPS_DEBUGGING defined */
  104.  
  105.  
  106. #include <sys/types.h>
  107. #include <a.out.h>
  108. #include <string.h>
  109. #include <ctype.h>
  110. #include <fcntl.h>
  111. #include <errno.h>
  112. #include <signal.h>
  113. #include <sys/stat.h>
  114.  
  115. #ifndef errno
  116. extern int errno;            /* MIPS errno.h doesn't declare this */
  117. #endif
  118.  
  119. #ifndef MALLOC_CHECK
  120. #ifdef    __SABER__
  121. #define MALLOC_CHECK
  122. #endif
  123. #endif
  124.  
  125. #define IS_ASM_IDENT(ch) \
  126.   (isalnum (ch) || (ch) == '_' || (ch) == '.' || (ch) == '$')
  127.  
  128.  
  129. /* Redefination of of storage classes as an enumeration for better
  130.    debugging.  */
  131.  
  132. typedef enum sc {
  133.   sc_Nil     = scNil,      /* no storage class */
  134.   sc_Text     = scText,      /* text symbol */
  135.   sc_Data     = scData,      /* initialized data symbol */
  136.   sc_Bss     = scBss,      /* un-initialized data symbol */
  137.   sc_Register     = scRegister,      /* value of symbol is register number */
  138.   sc_Abs     = scAbs,      /* value of symbol is absolute */
  139.   sc_Undefined     = scUndefined,      /* who knows? */
  140.   sc_CdbLocal     = scCdbLocal,      /* variable's value is IN se->va.?? */
  141.   sc_Bits     = scBits,      /* this is a bit field */
  142.   sc_CdbSystem     = scCdbSystem,      /* value is IN CDB's address space */
  143.   sc_RegImage     = scRegImage,      /* register value saved on stack */
  144.   sc_Info     = scInfo,      /* symbol contains debugger information */
  145.   sc_UserStruct     = scUserStruct,  /* addr in struct user for current process */
  146.   sc_SData     = scSData,      /* load time only small data */
  147.   sc_SBss     = scSBss,      /* load time only small common */
  148.   sc_RData     = scRData,      /* load time only read only data */
  149.   sc_Var     = scVar,      /* Var parameter (fortran,pascal) */
  150.   sc_Common     = scCommon,      /* common variable */
  151.   sc_SCommon     = scSCommon,      /* small common */
  152.   sc_VarRegister = scVarRegister, /* Var parameter in a register */
  153.   sc_Variant     = scVariant,      /* Variant record */
  154.   sc_SUndefined     = scSUndefined,  /* small undefined(external) data */
  155.   sc_Init     = scInit,      /* .init section symbol */
  156.   sc_Max     = scMax      /* Max storage class+1 */
  157. } sc_t;
  158.  
  159. /* Redefinition of symbol type.  */
  160.  
  161. typedef enum st {
  162.   st_Nil    = stNil,    /* Nuthin' special */
  163.   st_Global    = stGlobal,    /* external symbol */
  164.   st_Static    = stStatic,    /* static */
  165.   st_Param    = stParam,    /* procedure argument */
  166.   st_Local    = stLocal,    /* local variable */
  167.   st_Label    = stLabel,    /* label */
  168.   st_Proc    = stProc,    /*     "      "     Procedure */
  169.   st_Block    = stBlock,    /* beginnning of block */
  170.   st_End    = stEnd,    /* end (of anything) */
  171.   st_Member    = stMember,    /* member (of anything    - struct/union/enum */
  172.   st_Typedef    = stTypedef,    /* type definition */
  173.   st_File    = stFile,    /* file name */
  174.   st_RegReloc    = stRegReloc,    /* register relocation */
  175.   st_Forward    = stForward,    /* forwarding address */
  176.   st_StaticProc    = stStaticProc,    /* load time only static procs */
  177.   st_Constant    = stConstant,    /* const */
  178.   st_Str    = stStr,    /* string */
  179.   st_Number    = stNumber,    /* pure number (ie. 4 NOR 2+2) */
  180.   st_Expr    = stExpr,    /* 2+2 vs. 4 */
  181.   st_Type    = stType,    /* post-coersion SER */
  182.   st_Max    = stMax        /* max type+1 */
  183. } st_t;
  184.  
  185. /* Redefinition of type qualifiers.  */
  186.  
  187. typedef enum tq {
  188.   tq_Nil    = tqNil,    /* bt is what you see */
  189.   tq_Ptr    = tqPtr,    /* pointer */
  190.   tq_Proc    = tqProc,    /* procedure */
  191.   tq_Array    = tqArray,    /* duh */
  192.   tq_Far    = tqFar,    /* longer addressing - 8086/8 land */
  193.   tq_Vol    = tqVol,    /* volatile */
  194.   tq_Max    = tqMax        /* Max type qualifier+1 */
  195. } tq_t;
  196.  
  197. /* Redefinition of basic types.  */
  198.  
  199. typedef enum bt {
  200.   bt_Nil    = btNil,    /* undefined */
  201.   bt_Adr    = btAdr,    /* address - integer same size as pointer */
  202.   bt_Char    = btChar,    /* character */
  203.   bt_UChar    = btUChar,    /* unsigned character */
  204.   bt_Short    = btShort,    /* short */
  205.   bt_UShort    = btUShort,    /* unsigned short */
  206.   bt_Int    = btInt,    /* int */
  207.   bt_UInt    = btUInt,    /* unsigned int */
  208.   bt_Long    = btLong,    /* long */
  209.   bt_ULong    = btULong,    /* unsigned long */
  210.   bt_Float    = btFloat,    /* float (real) */
  211.   bt_Double    = btDouble,    /* Double (real) */
  212.   bt_Struct    = btStruct,    /* Structure (Record) */
  213.   bt_Union    = btUnion,    /* Union (variant) */
  214.   bt_Enum    = btEnum,    /* Enumerated */
  215.   bt_Typedef    = btTypedef,    /* defined via a typedef, isymRef points */
  216.   bt_Range    = btRange,    /* subrange of int */
  217.   bt_Set    = btSet,    /* pascal sets */
  218.   bt_Complex    = btComplex,    /* fortran complex */
  219.   bt_DComplex    = btDComplex,    /* fortran double complex */
  220.   bt_Indirect    = btIndirect,    /* forward or unnamed typedef */
  221.   bt_FixedDec    = btFixedDec,    /* Fixed Decimal */
  222.   bt_FloatDec    = btFloatDec,    /* Float Decimal */
  223.   bt_String    = btString,    /* Varying Length Character String */
  224.   bt_Bit    = btBit,    /* Aligned Bit String */
  225.   bt_Picture    = btPicture,    /* Picture */
  226.  
  227. #ifdef btVoid
  228.   bt_Void    = btVoid,    /* Void */
  229. #else
  230. #define bt_Void    bt_Nil
  231. #endif
  232.  
  233.   bt_Max    = btMax        /* Max basic type+1 */
  234. } bt_t;
  235.  
  236.  
  237.  
  238. /* Basic COFF storage classes.  */
  239. enum coff_storage {
  240.   C_EFCN    = -1,
  241.   C_NULL    = 0,
  242.   C_AUTO    = 1,
  243.   C_EXT        = 2,
  244.   C_STAT    = 3,
  245.   C_REG        = 4,
  246.   C_EXTDEF    = 5,
  247.   C_LABEL    = 6,
  248.   C_ULABEL    = 7,
  249.   C_MOS        = 8,
  250.   C_ARG        = 9,
  251.   C_STRTAG    = 10,
  252.   C_MOU        = 11,
  253.   C_UNTAG    = 12,
  254.   C_TPDEF    = 13,
  255.   C_USTATIC    = 14,
  256.   C_ENTAG    = 15,
  257.   C_MOE        = 16,
  258.   C_REGPARM    = 17,
  259.   C_FIELD    = 18,
  260.   C_BLOCK    = 100,
  261.   C_FCN        = 101,
  262.   C_EOS        = 102,
  263.   C_FILE    = 103,
  264.   C_LINE    = 104,
  265.   C_ALIAS    = 105,
  266.   C_HIDDEN    = 106,
  267.   C_MAX        = 107
  268. } coff_storage_t;
  269.  
  270. /* Regular COFF fundamental type.  */
  271. typedef enum coff_type {
  272.   T_NULL    = 0,
  273.   T_ARG        = 1,
  274.   T_CHAR    = 2,
  275.   T_SHORT    = 3,
  276.   T_INT        = 4,
  277.   T_LONG    = 5,
  278.   T_FLOAT    = 6,
  279.   T_DOUBLE    = 7,
  280.   T_STRUCT    = 8,
  281.   T_UNION    = 9,
  282.   T_ENUM    = 10,
  283.   T_MOE        = 11,
  284.   T_UCHAR    = 12,
  285.   T_USHORT    = 13,
  286.   T_UINT    = 14,
  287.   T_ULONG    = 15,
  288.   T_MAX        = 16
  289. } coff_type_t;
  290.  
  291. /* Regular COFF derived types.  */
  292. typedef enum coff_dt {
  293.   DT_NON    = 0,
  294.   DT_PTR    = 1,
  295.   DT_FCN    = 2,
  296.   DT_ARY    = 3,
  297.   DT_MAX    = 4
  298. } coff_dt_t;
  299.  
  300. #define N_BTMASK    017    /* bitmask to isolate basic type */
  301. #define N_TMASK        003    /* bitmask to isolate derived type */
  302. #define N_BT_SHIFT    4    /* # bits to shift past basic type */
  303. #define N_TQ_SHIFT    2    /* # bits to shift derived types */
  304. #define    N_TQ        6    /* # of type qualifiers */
  305.  
  306. /* States for whether to hash type or not.  */
  307. typedef enum hash_state {
  308.   hash_no    = 0,        /* don't hash type */
  309.   hash_yes    = 1,        /* ok to hash type, or use previous hash */
  310.   hash_record    = 2        /* ok to record hash, but don't use prev. */
  311. } hash_state_t;
  312.  
  313.  
  314. #define WORD_ALIGN(x)  (((x) + 3) & ~3)
  315. #define DWORD_ALIGN(x) (((x) + 7) & ~7)
  316.  
  317.  
  318. /* Structures to provide n-number of virtual arrays, each of which can
  319.    grow linearly, and which are written in the object file as sequential
  320.    pages.  On systems with a BSD malloc that define USE_MALLOC, the
  321.    MAX_CLUSTER_PAGES should be 1 less than a power of two, since malloc
  322.    adds it's overhead, and rounds up to the next power of 2.  Pages are
  323.    linked together via a linked list.
  324.  
  325.    If PAGE_SIZE is > 4096, the string length in the shash_t structure
  326.    can't be represented (assuming there are strings > 4096 bytes).  */
  327.  
  328. #ifndef PAGE_SIZE
  329. #define PAGE_SIZE 4096        /* size of varray pages */
  330. #endif
  331.  
  332. #define PAGE_USIZE ((Size_t)PAGE_SIZE)
  333.  
  334.  
  335. #ifndef MAX_CLUSTER_PAGES    /* # pages to get from system */
  336. #ifndef USE_MALLOC        /* in one memory request */
  337. #define MAX_CLUSTER_PAGES 64
  338. #else
  339. #define MAX_CLUSTER_PAGES 63
  340. #endif
  341. #endif
  342.  
  343.  
  344. /* Linked list connecting separate page allocations.  */
  345. typedef struct vlinks {
  346.   struct vlinks    *prev;        /* previous set of pages */
  347.   struct vlinks *next;        /* next set of pages */
  348.   union  page   *datum;        /* start of page */
  349.   unsigned long     start_index;    /* starting index # of page */
  350. } vlinks_t;
  351.  
  352.  
  353. /* Virtual array header.  */
  354. typedef struct varray {
  355.   vlinks_t    *first;            /* first page link */
  356.   vlinks_t    *last;            /* last page link */
  357.   unsigned long     num_allocated;        /* # objects allocated */
  358.   unsigned short object_size;        /* size in bytes of each object */
  359.   unsigned short objects_per_page;    /* # objects that can fit on a page */
  360.   unsigned short objects_last_page;    /* # objects allocated on last page */
  361. } varray_t;
  362.  
  363. #ifndef MALLOC_CHECK
  364. #define OBJECTS_PER_PAGE(type) (PAGE_SIZE / sizeof (type))
  365. #else
  366. #define OBJECTS_PER_PAGE(type) ((sizeof (type) > 1) ? 1 : PAGE_SIZE)
  367. #endif
  368.  
  369. #define INIT_VARRAY(type) {    /* macro to initialize a varray */    \
  370.   (vlinks_t *)0,        /* first */                \
  371.   (vlinks_t *)0,        /* last */                \
  372.   0,                /* num_allocated */            \
  373.   sizeof (type),        /* object_size */            \
  374.   OBJECTS_PER_PAGE (type),    /* objects_per_page */            \
  375.   OBJECTS_PER_PAGE (type),    /* objects_last_page */            \
  376. }
  377.  
  378. /* Master type for indexes within the symbol table. */
  379. typedef unsigned long symint_t;
  380.  
  381.  
  382. /* Linked list support for nested scopes (file, block, structure, etc.).  */
  383. typedef struct scope {
  384.   struct scope    *prev;        /* previous scope level */
  385.   SYMR        *lsym;        /* pointer to local symbol node */
  386.   symint_t     lnumber;    /* lsym index */
  387.   st_t         type;        /* type of the node */
  388. } scope_t;
  389.  
  390.  
  391. /* Forward reference list for tags referenced, but not yet defined.  */
  392. typedef struct forward {
  393.   struct forward *next;        /* next forward reference */
  394.   AUXU         *ifd_ptr;    /* pointer to store file index */
  395.   AUXU         *index_ptr;    /* pointer to store symbol index */
  396.   AUXU         *type_ptr;    /* pointer to munge type info */
  397. } forward_t;
  398.  
  399.  
  400. /* Linked list support for tags.  The first tag in the list is always
  401.    the current tag for that block.  */
  402. typedef struct tag {
  403.   struct shash     *hash_ptr;    /* pointer to the hash table head */
  404.   struct tag     *same_name;    /* tag with same name in outer scope */
  405.   struct tag     *same_block;    /* next tag defined in the same block.  */
  406.   struct forward *forward_ref;    /* list of forward references */
  407.   bt_t          basic_type;    /* bt_Struct, bt_Union, or bt_Enum */
  408.   symint_t      ifd;        /* file # tag defined in */
  409.   symint_t      index;    /* index within file's local symbols */
  410. } tag_t;
  411.  
  412. /* Head of a block's linked list of tags.  */
  413. typedef struct thead {
  414.   struct thead    *prev;        /* previous block */
  415.   struct tag    *first_tag;    /* first tag in block defined */
  416. } thead_t;
  417.  
  418.  
  419. /* String hash table support.  The size of the hash table must fit
  420.    within a page.  */
  421.  
  422.  
  423. #ifndef SHASH_SIZE
  424. #define SHASH_SIZE 1009
  425. #endif
  426.  
  427. #define HASH_LEN_MAX ((1 << 12) - 1)    /* Max length we can store */
  428.  
  429. typedef struct shash {
  430.   struct shash    *next;        /* next hash value */
  431.   char        *string;    /* string we are hashing */
  432.   symint_t     len;        /* string length */
  433.   symint_t     index;        /* index within string table */
  434.   EXTR        *esym_ptr;    /* global symbol pointer */
  435.   SYMR        *sym_ptr;    /* local symbol pointer */
  436.   tag_t        *tag_ptr;    /* tag pointer */
  437.   PDR        *proc_ptr;    /* procedure descriptor pointer */
  438. } shash_t;
  439.  
  440.  
  441. /* Type hash table support.  The size of the hash table must fit
  442.    within a page with the other extended file descriptor information.
  443.    Because unique types which are hashed are fewer in number than
  444.    strings, we use a smaller hash value.  */
  445.  
  446. #ifndef THASH_SIZE
  447. #define THASH_SIZE 113
  448. #endif
  449.  
  450. typedef struct thash {
  451.   struct thash    *next;        /* next hash value */
  452.   AUXU         type;        /* type we are hashing */
  453.   symint_t     index;        /* index within string table */
  454. } thash_t;
  455.  
  456.  
  457. /* Union of various small structures that are allocated and freed
  458.    from the same heap.  */
  459.  
  460. typedef union word8 {
  461.   union word8  *prev;        /* previous word8 structure */
  462.   scope_t    s;        /* scope structure */
  463.   vlinks_t    v;        /* varray linked list */
  464.   shash_t    sh;        /* string hash linked list */
  465.   thash_t    th;        /* type hash linked list */
  466.   tag_t        t;        /* tag structure */
  467.   forward_t    f;        /* forward tag references */
  468.   thead_t    thead;        /* thead strcture */
  469.   long        words[8];    /* words to zero out when allocating */
  470. } word8_t;
  471.  
  472.  
  473. /* Extended file descriptor that contains all of the support necessary
  474.    to add things to each file separately.  */
  475. typedef struct efdr {
  476.   FDR       fdr;            /* File header to be written out */
  477.   FDR      *orig_fdr;        /* original file header */
  478.   char      *name;        /* filename */
  479.   int       name_len;        /* length of the filename */
  480.   symint_t void_type;        /* aux. pointer to 'void' type */
  481.   symint_t int_type;        /* aux. pointer to 'int' type */
  482.   scope_t *cur_scope;        /* current nested scopes */
  483.   symint_t file_index;        /* current file number */
  484.   int       nested_scopes;    /* # nested scopes */
  485.   varray_t strings;        /* local strings */
  486.   varray_t symbols;        /* local symbols */
  487.   varray_t procs;        /* procedures */
  488.   varray_t aux_syms;        /* auxiliary symbols */
  489.  
  490.   struct efdr *next_file;    /* next file descriptor */
  491.  
  492.                 /* string/type hash tables */
  493.   shash_t **shash_head;        /* string hash table */
  494.   thash_t *thash_head[THASH_SIZE];
  495. } efdr_t;
  496.  
  497. /* Pre-initialized extended file structure.  */
  498. static efdr_t init_file = 
  499. {
  500.   {            /* FDR structure */
  501.     0,            /* adr:        memory address of beginning of file */
  502.     0,            /* rss:        file name (of source, if known) */
  503.     0,            /* issBase:    file's string space */
  504.     0,            /* cbSs:    number of bytes in the ss */
  505.     0,            /* isymBase:    beginning of symbols */
  506.     0,            /* csym:    count file's of symbols */
  507.     0,            /* ilineBase:    file's line symbols */
  508.     0,            /* cline:    count of file's line symbols */
  509.     0,            /* ioptBase:    file's optimization entries */
  510.     0,            /* copt:    count of file's optimization entries */
  511.     0,            /* ipdFirst:    start of procedures for this file */
  512.     0,            /* cpd:        count of procedures for this file */
  513.     0,            /* iauxBase:    file's auxiliary entries */
  514.     0,            /* caux:    count of file's auxiliary entries */
  515.     0,            /* rfdBase:    index into the file indirect table */
  516.     0,            /* crfd:    count file indirect entries */
  517.     langC,        /* lang:    language for this file */
  518.     1,            /* fMerge:    whether this file can be merged */
  519.     0,            /* fReadin:    true if read in (not just created) */
  520. #ifdef BYTES_BIG_ENDIAN
  521.     1,            /* fBigendian:    if 1, compiled on big endian machine */
  522. #else
  523.     0,            /* fBigendian:    if 1, compiled on big endian machine */
  524. #endif
  525.     GLEVEL_2,        /* glevel:    level this file was compiled with */
  526.     0,            /* reserved:    reserved for future use */
  527.     0,            /* cbLineOffset: byte offset from header for this file ln's */
  528.     0,            /* cbLine:    size of lines for this file */
  529.   },
  530.  
  531.   (FDR *)0,        /* orig_fdr:    original file header pointer */
  532.   (char *)0,        /* name:    pointer to filename */
  533.   0,            /* name_len:    length of filename */
  534.   0,            /* void_type:    ptr to aux node for void type */
  535.   0,            /* int_type:    ptr to aux node for int type */
  536.   (scope_t *)0,        /* cur_scope:    current scope being processed */
  537.   0,            /* file_index:    current file # */
  538.   0,            /* nested_scopes: # nested scopes */
  539.   INIT_VARRAY (char),    /* strings:    local string varray */
  540.   INIT_VARRAY (SYMR),    /* symbols:    local symbols varray */
  541.   INIT_VARRAY (PDR),    /* procs:    procedure varray */
  542.   INIT_VARRAY (AUXU),    /* aux_syms:    auxiliary symbols varray */
  543.  
  544.   (struct efdr *)0,    /* next_file:    next file structure */
  545.  
  546.   (shash_t **)0,    /* shash_head:    string hash table */
  547.   { 0 },        /* thash_head:    type hash table */
  548. };
  549.  
  550.  
  551. static efdr_t *first_file;    /* first file descriptor */
  552. static efdr_t *last_file;    /* last  file descriptor */
  553.  
  554.  
  555. /* Union of various things that are held in pages.  */
  556. typedef union page {
  557.   char        byte [ PAGE_SIZE ];
  558.   unsigned char    ubyte[ PAGE_SIZE ];
  559.   efdr_t    file [ PAGE_SIZE / sizeof (efdr_t)  ];
  560.   FDR        ofile[ PAGE_SIZE / sizeof (FDR)     ];
  561.   PDR        proc [ PAGE_SIZE / sizeof (PDR)     ];
  562.   SYMR        sym  [ PAGE_SIZE / sizeof (SYMR)    ];
  563.   EXTR        esym [ PAGE_SIZE / sizeof (EXTR)    ];
  564.   AUXU        aux  [ PAGE_SIZE / sizeof (AUXU)    ];
  565.   DNR        dense[ PAGE_SIZE / sizeof (DNR)     ];
  566.   word8_t    word8[ PAGE_SIZE / sizeof (word8_t) ];
  567. } page_t;
  568.  
  569.  
  570. /* Type information collected together.  */
  571. typedef struct type_info {
  572.   bt_t          basic_type;        /* basic type */
  573.   coff_type_t orig_type;        /* original COFF-based type */
  574.   int          num_tq;            /* # type qualifiers */
  575.   int          num_dims;            /* # dimensions */
  576.   int          num_sizes;        /* # sizes */
  577.   int          extra_sizes;        /* # extra sizes not tied with dims */
  578.   tag_t *     tag_ptr;            /* tag pointer */
  579.   int          bitfield;            /* symbol is a bitfield */
  580.   int          unknown_tag;        /* this is an unknown tag */
  581.   tq_t          type_qualifiers[N_TQ];    /* type qualifiers (ptr, func, array)*/
  582.   symint_t    dimensions     [N_TQ];    /* dimensions for each array */
  583.   symint_t    sizes         [N_TQ+2];    /* sizes of each array slice + size of
  584.                        struct/union/enum + bitfield size */
  585. } type_info_t;
  586.  
  587. /* Pre-initialized type_info struct.  */
  588. static type_info_t type_info_init = {
  589.   bt_Nil,                /* basic type */
  590.   T_NULL,                /* original COFF-based type */
  591.   0,                    /* # type qualifiers */
  592.   0,                    /* # dimensions */
  593.   0,                    /* # sizes */
  594.   0,                    /* sizes not tied with dims */
  595.   NULL,                    /* ptr to tag */
  596.   0,                    /* bitfield */
  597.   0,                    /* unknown tag */
  598.   {                    /* type qualifiers */
  599.     tq_Nil,
  600.     tq_Nil,
  601.     tq_Nil,
  602.     tq_Nil,
  603.     tq_Nil,
  604.     tq_Nil,
  605.   },
  606.   {                    /* dimensions */
  607.     0,
  608.     0,
  609.     0,
  610.     0,
  611.     0,
  612.     0
  613.   },
  614.   {                    /* sizes */
  615.     0,
  616.     0,
  617.     0,
  618.     0,
  619.     0,
  620.     0,
  621.     0,
  622.     0,
  623.   },
  624. };
  625.  
  626.  
  627. /* Global virtual arrays & hash table for external strings as well as
  628.    for the tags table and global tables for file descriptors, and
  629.    dense numbers.  */
  630.  
  631. static varray_t file_desc    = INIT_VARRAY (efdr_t);
  632. static varray_t dense_num    = INIT_VARRAY (DNR);
  633. static varray_t tag_strings    = INIT_VARRAY (char);
  634. static varray_t ext_strings    = INIT_VARRAY (char);
  635. static varray_t ext_symbols    = INIT_VARRAY (EXTR);
  636.  
  637. static shash_t    *orig_str_hash[SHASH_SIZE];
  638. static shash_t    *ext_str_hash [SHASH_SIZE];
  639. static shash_t    *tag_hash     [SHASH_SIZE];
  640.  
  641. /* Static types for int and void.  Also, remember the last function's
  642.    type (which is set up when we encounter the declaration for the
  643.    function, and used when the end block for the function is emitted.  */
  644.  
  645. static type_info_t int_type_info;
  646. static type_info_t void_type_info;
  647. static type_info_t last_func_type_info;
  648. static EXTR      *last_func_eptr;
  649.  
  650.  
  651. /* Convert COFF basic type to ECOFF basic type.  The T_NULL type
  652.    really should use bt_Void, but this causes the current ecoff GDB to
  653.    issue unsupported type messages, and the Ultrix 4.00 dbx (aka MIPS
  654.    2.0) doesn't understand it, even though the compiler generates it.
  655.    Maybe this will be fixed in 2.10 or 2.20 of the MIPS compiler
  656.    suite, but for now go with what works.  */
  657.  
  658. static bt_t map_coff_types[ (int)T_MAX ] = {
  659.   bt_Nil,            /* T_NULL */
  660.   bt_Nil,            /* T_ARG */
  661.   bt_Char,            /* T_CHAR */
  662.   bt_Short,            /* T_SHORT */
  663.   bt_Int,            /* T_INT */
  664.   bt_Long,            /* T_LONG */
  665.   bt_Float,            /* T_FLOAT */
  666.   bt_Double,            /* T_DOUBLE */
  667.   bt_Struct,            /* T_STRUCT */
  668.   bt_Union,            /* T_UNION */
  669.   bt_Enum,            /* T_ENUM */
  670.   bt_Enum,            /* T_MOE */
  671.   bt_UChar,            /* T_UCHAR */
  672.   bt_UShort,            /* T_USHORT */
  673.   bt_UInt,            /* T_UINT */
  674.   bt_ULong            /* T_ULONG */
  675. };
  676.  
  677. /* Convert COFF storage class to ECOFF storage class.  */
  678. static sc_t map_coff_storage[ (int)C_MAX ] = {
  679.   sc_Nil,            /*   0: C_NULL */
  680.   sc_Abs,            /*   1: C_AUTO      auto var */
  681.   sc_Undefined,            /*   2: C_EXT      external */
  682.   sc_Data,            /*   3: C_STAT      static */
  683.   sc_Register,            /*   4: C_REG      register */
  684.   sc_Undefined,            /*   5: C_EXTDEF  ??? */
  685.   sc_Text,            /*   6: C_LABEL      label */
  686.   sc_Text,            /*   7: C_ULABEL  user label */
  687.   sc_Info,            /*   8: C_MOS      member of struct */
  688.   sc_Abs,            /*   9: C_ARG      argument */
  689.   sc_Info,            /*  10: C_STRTAG  struct tag */
  690.   sc_Info,            /*  11: C_MOU      member of union */
  691.   sc_Info,            /*  12: C_UNTAG   union tag */
  692.   sc_Info,            /*  13: C_TPDEF      typedef */
  693.   sc_Data,            /*  14: C_USTATIC ??? */
  694.   sc_Info,            /*  15: C_ENTAG      enum tag */
  695.   sc_Info,            /*  16: C_MOE      member of enum */
  696.   sc_Register,            /*  17: C_REGPARM register parameter */
  697.   sc_Bits,            /*  18; C_FIELD      bitfield */
  698.   sc_Nil,            /*  19 */
  699.   sc_Nil,            /*  20 */
  700.   sc_Nil,            /*  21 */
  701.   sc_Nil,            /*  22 */
  702.   sc_Nil,            /*  23 */
  703.   sc_Nil,            /*  24 */
  704.   sc_Nil,            /*  25 */
  705.   sc_Nil,            /*  26 */
  706.   sc_Nil,            /*  27 */
  707.   sc_Nil,            /*  28 */
  708.   sc_Nil,            /*  29 */
  709.   sc_Nil,            /*  30 */
  710.   sc_Nil,            /*  31 */
  711.   sc_Nil,            /*  32 */
  712.   sc_Nil,            /*  33 */
  713.   sc_Nil,            /*  34 */
  714.   sc_Nil,            /*  35 */
  715.   sc_Nil,            /*  36 */
  716.   sc_Nil,            /*  37 */
  717.   sc_Nil,            /*  38 */
  718.   sc_Nil,            /*  39 */
  719.   sc_Nil,            /*  40 */
  720.   sc_Nil,            /*  41 */
  721.   sc_Nil,            /*  42 */
  722.   sc_Nil,            /*  43 */
  723.   sc_Nil,            /*  44 */
  724.   sc_Nil,            /*  45 */
  725.   sc_Nil,            /*  46 */
  726.   sc_Nil,            /*  47 */
  727.   sc_Nil,            /*  48 */
  728.   sc_Nil,            /*  49 */
  729.   sc_Nil,            /*  50 */
  730.   sc_Nil,            /*  51 */
  731.   sc_Nil,            /*  52 */
  732.   sc_Nil,            /*  53 */
  733.   sc_Nil,            /*  54 */
  734.   sc_Nil,            /*  55 */
  735.   sc_Nil,            /*  56 */
  736.   sc_Nil,            /*  57 */
  737.   sc_Nil,            /*  58 */
  738.   sc_Nil,            /*  59 */
  739.   sc_Nil,            /*  60 */
  740.   sc_Nil,            /*  61 */
  741.   sc_Nil,            /*  62 */
  742.   sc_Nil,            /*  63 */
  743.   sc_Nil,            /*  64 */
  744.   sc_Nil,            /*  65 */
  745.   sc_Nil,            /*  66 */
  746.   sc_Nil,            /*  67 */
  747.   sc_Nil,            /*  68 */
  748.   sc_Nil,            /*  69 */
  749.   sc_Nil,            /*  70 */
  750.   sc_Nil,            /*  71 */
  751.   sc_Nil,            /*  72 */
  752.   sc_Nil,            /*  73 */
  753.   sc_Nil,            /*  74 */
  754.   sc_Nil,            /*  75 */
  755.   sc_Nil,            /*  76 */
  756.   sc_Nil,            /*  77 */
  757.   sc_Nil,            /*  78 */
  758.   sc_Nil,            /*  79 */
  759.   sc_Nil,            /*  80 */
  760.   sc_Nil,            /*  81 */
  761.   sc_Nil,            /*  82 */
  762.   sc_Nil,            /*  83 */
  763.   sc_Nil,            /*  84 */
  764.   sc_Nil,            /*  85 */
  765.   sc_Nil,            /*  86 */
  766.   sc_Nil,            /*  87 */
  767.   sc_Nil,            /*  88 */
  768.   sc_Nil,            /*  89 */
  769.   sc_Nil,            /*  90 */
  770.   sc_Nil,            /*  91 */
  771.   sc_Nil,            /*  92 */
  772.   sc_Nil,            /*  93 */
  773.   sc_Nil,            /*  94 */
  774.   sc_Nil,            /*  95 */
  775.   sc_Nil,            /*  96 */
  776.   sc_Nil,            /*  97 */
  777.   sc_Nil,            /*  98 */
  778.   sc_Nil,            /*  99 */
  779.   sc_Text,            /* 100: C_BLOCK  block start/end */
  780.   sc_Text,            /* 101: C_FCN     function start/end */
  781.   sc_Info,            /* 102: C_EOS     end of struct/union/enum */
  782.   sc_Nil,            /* 103: C_FILE     file start */
  783.   sc_Nil,            /* 104: C_LINE     line number */
  784.   sc_Nil,            /* 105: C_ALIAS     combined type info */
  785.   sc_Nil,            /* 106: C_HIDDEN ??? */
  786. };
  787.  
  788. /* Convert COFF storage class to ECOFF symbol type.  */
  789. static st_t map_coff_sym_type[ (int)C_MAX ] = {
  790.   st_Nil,            /*   0: C_NULL */
  791.   st_Local,            /*   1: C_AUTO      auto var */
  792.   st_Global,            /*   2: C_EXT      external */
  793.   st_Static,            /*   3: C_STAT      static */
  794.   st_Local,            /*   4: C_REG      register */
  795.   st_Global,            /*   5: C_EXTDEF  ??? */
  796.   st_Label,            /*   6: C_LABEL      label */
  797.   st_Label,            /*   7: C_ULABEL  user label */
  798.   st_Member,            /*   8: C_MOS      member of struct */
  799.   st_Param,            /*   9: C_ARG      argument */
  800.   st_Block,            /*  10: C_STRTAG  struct tag */
  801.   st_Member,            /*  11: C_MOU      member of union */
  802.   st_Block,            /*  12: C_UNTAG   union tag */
  803.   st_Typedef,            /*  13: C_TPDEF      typedef */
  804.   st_Static,            /*  14: C_USTATIC ??? */
  805.   st_Block,            /*  15: C_ENTAG      enum tag */
  806.   st_Member,            /*  16: C_MOE      member of enum */
  807.   st_Param,            /*  17: C_REGPARM register parameter */
  808.   st_Member,            /*  18; C_FIELD      bitfield */
  809.   st_Nil,            /*  19 */
  810.   st_Nil,            /*  20 */
  811.   st_Nil,            /*  21 */
  812.   st_Nil,            /*  22 */
  813.   st_Nil,            /*  23 */
  814.   st_Nil,            /*  24 */
  815.   st_Nil,            /*  25 */
  816.   st_Nil,            /*  26 */
  817.   st_Nil,            /*  27 */
  818.   st_Nil,            /*  28 */
  819.   st_Nil,            /*  29 */
  820.   st_Nil,            /*  30 */
  821.   st_Nil,            /*  31 */
  822.   st_Nil,            /*  32 */
  823.   st_Nil,            /*  33 */
  824.   st_Nil,            /*  34 */
  825.   st_Nil,            /*  35 */
  826.   st_Nil,            /*  36 */
  827.   st_Nil,            /*  37 */
  828.   st_Nil,            /*  38 */
  829.   st_Nil,            /*  39 */
  830.   st_Nil,            /*  40 */
  831.   st_Nil,            /*  41 */
  832.   st_Nil,            /*  42 */
  833.   st_Nil,            /*  43 */
  834.   st_Nil,            /*  44 */
  835.   st_Nil,            /*  45 */
  836.   st_Nil,            /*  46 */
  837.   st_Nil,            /*  47 */
  838.   st_Nil,            /*  48 */
  839.   st_Nil,            /*  49 */
  840.   st_Nil,            /*  50 */
  841.   st_Nil,            /*  51 */
  842.   st_Nil,            /*  52 */
  843.   st_Nil,            /*  53 */
  844.   st_Nil,            /*  54 */
  845.   st_Nil,            /*  55 */
  846.   st_Nil,            /*  56 */
  847.   st_Nil,            /*  57 */
  848.   st_Nil,            /*  58 */
  849.   st_Nil,            /*  59 */
  850.   st_Nil,            /*  60 */
  851.   st_Nil,            /*  61 */
  852.   st_Nil,            /*  62 */
  853.   st_Nil,            /*  63 */
  854.   st_Nil,            /*  64 */
  855.   st_Nil,            /*  65 */
  856.   st_Nil,            /*  66 */
  857.   st_Nil,            /*  67 */
  858.   st_Nil,            /*  68 */
  859.   st_Nil,            /*  69 */
  860.   st_Nil,            /*  70 */
  861.   st_Nil,            /*  71 */
  862.   st_Nil,            /*  72 */
  863.   st_Nil,            /*  73 */
  864.   st_Nil,            /*  74 */
  865.   st_Nil,            /*  75 */
  866.   st_Nil,            /*  76 */
  867.   st_Nil,            /*  77 */
  868.   st_Nil,            /*  78 */
  869.   st_Nil,            /*  79 */
  870.   st_Nil,            /*  80 */
  871.   st_Nil,            /*  81 */
  872.   st_Nil,            /*  82 */
  873.   st_Nil,            /*  83 */
  874.   st_Nil,            /*  84 */
  875.   st_Nil,            /*  85 */
  876.   st_Nil,            /*  86 */
  877.   st_Nil,            /*  87 */
  878.   st_Nil,            /*  88 */
  879.   st_Nil,            /*  89 */
  880.   st_Nil,            /*  90 */
  881.   st_Nil,            /*  91 */
  882.   st_Nil,            /*  92 */
  883.   st_Nil,            /*  93 */
  884.   st_Nil,            /*  94 */
  885.   st_Nil,            /*  95 */
  886.   st_Nil,            /*  96 */
  887.   st_Nil,            /*  97 */
  888.   st_Nil,            /*  98 */
  889.   st_Nil,            /*  99 */
  890.   st_Block,            /* 100: C_BLOCK  block start/end */
  891.   st_Proc,            /* 101: C_FCN     function start/end */
  892.   st_End,            /* 102: C_EOS     end of struct/union/enum */
  893.   st_File,            /* 103: C_FILE     file start */
  894.   st_Nil,            /* 104: C_LINE     line number */
  895.   st_Nil,            /* 105: C_ALIAS     combined type info */
  896.   st_Nil,            /* 106: C_HIDDEN ??? */
  897. };
  898.  
  899. /* Map COFF derived types to ECOFF type qualifiers.  */
  900. static tq_t map_coff_derived_type[ (int)DT_MAX ] = {
  901.   tq_Nil,            /* 0: DT_NON    no more qualifiers */
  902.   tq_Ptr,            /* 1: DT_PTR    pointer */
  903.   tq_Proc,            /* 2: DT_FCN    function */
  904.   tq_Array,            /* 3: DT_ARY    array */
  905. };
  906.  
  907.  
  908.  
  909. /* Pointers and such to the original symbol table that is read in.  */
  910. static struct filehdr orig_file_header;        /* global object file header */
  911.  
  912. static HDRR     orig_sym_hdr;            /* symbolic header on input */
  913. static char    *orig_linenum;            /* line numbers */
  914. static DNR    *orig_dense;            /* dense numbers */
  915. static PDR    *orig_procs;            /* procedures */
  916. static SYMR    *orig_local_syms;        /* local symbols */
  917. static OPTR    *orig_opt_syms;            /* optimization symbols */
  918. static AUXU    *orig_aux_syms;            /* auxiliary symbols */
  919. static char    *orig_local_strs;        /* local strings */
  920. static char    *orig_ext_strs;            /* external strings */
  921. static FDR    *orig_files;            /* file descriptors */
  922. static symint_t    *orig_rfds;            /* relative file desc's */
  923. static EXTR    *orig_ext_syms;            /* external symbols */
  924.  
  925. /* Macros to convert an index into a given object within the original
  926.    symbol table.  */
  927. #define CHECK(num,max,str) \
  928.   (((unsigned long)num > (unsigned long)max) ? out_of_bounds (num, max, str, __LINE__) : 0)
  929.  
  930. #define ORIG_LINENUM(index)    (CHECK ((index), orig_sym_hdr.cbLine,    "line#"), (index) + orig_linenum)
  931. #define ORIG_DENSE(index)    (CHECK ((index), orig_sym_hdr.idnMax,    "dense"), (index) + orig_dense)
  932. #define ORIG_PROCS(index)    (CHECK ((index), orig_sym_hdr.ipdMax,    "procs"), (index) + orig_procs)
  933. #define ORIG_FILES(index)    (CHECK ((index), orig_sym_hdr.ifdMax,    "funcs"), (index) + orig_files)
  934. #define ORIG_LSYMS(index)    (CHECK ((index), orig_sym_hdr.isymMax,   "lsyms"), (index) + orig_local_syms)
  935. #define ORIG_LSTRS(index)    (CHECK ((index), orig_sym_hdr.issMax,    "lstrs"), (index) + orig_local_strs)
  936. #define ORIG_ESYMS(index)    (CHECK ((index), orig_sym_hdr.iextMax,   "esyms"), (index) + orig_ext_syms)
  937. #define ORIG_ESTRS(index)    (CHECK ((index), orig_sym_hdr.issExtMax, "estrs"), (index) + orig_ext_strs)
  938. #define ORIG_OPT(index)        (CHECK ((index), orig_sym_hdr.ioptMax,   "opt"),   (index) + orig_opt_syms)
  939. #define ORIG_AUX(index)        (CHECK ((index), orig_sym_hdr.iauxMax,   "aux"),   (index) + orig_aux_syms)
  940. #define ORIG_RFDS(index)    (CHECK ((index), orig_sym_hdr.crfd,      "rfds"),  (index) + orig_rfds)
  941.  
  942. /* Various other statics.  */
  943. static HDRR    symbolic_header;        /* symbolic header */
  944. static efdr_t  *cur_file_ptr    = (efdr_t *) 0;    /* current file desc. header */
  945. static PDR     *cur_proc_ptr    = (PDR *) 0;    /* current procedure header */
  946. static PDR     *cur_oproc_ptr    = (PDR *) 0;    /* current original procedure*/
  947. static thead_t *cur_tag_head    = (thead_t *)0;    /* current tag head */
  948. static long    file_offset    = 0;        /* current file offset */
  949. static long    max_file_offset    = 0;        /* maximum file offset */
  950. static FILE    *object_stream    = (FILE *)0;    /* file desc. to output .o */
  951. static FILE    *obj_in_stream    = (FILE *)0;    /* file desc. to input .o */
  952. static char    *progname    = (char *)0;    /* program name for errors */
  953. static char    *input_name    = "stdin";    /* name of input file */
  954. static char    *object_name    = (char *)0;    /* tmp. name of object file */
  955. static char    *obj_in_name    = (char *)0;    /* name of input object file */
  956. static char    *cur_line_start    = (char *)0;    /* current line read in */
  957. static char    *cur_line_ptr    = (char *)0;    /* ptr within current line */
  958. static unsigned    cur_line_nbytes    = 0;        /* # bytes for current line */
  959. static unsigned    cur_line_alloc    = 0;        /* # bytes total in buffer */
  960. static long    line_number    = 0;        /* current input line number */
  961. static int    debug        = 0;         /* trace functions */
  962. static int    version        = 0;         /* print version # */
  963. static int    had_errors    = 0;        /* != 0 if errors were found */
  964.  
  965.  
  966. /* Forward reference for functions.  See the definition for more details.  */
  967.  
  968. #ifndef STATIC
  969. #define STATIC static
  970. #endif
  971.  
  972. STATIC int    out_of_bounds    __proto((symint_t, symint_t, const char *, int));
  973.  
  974. STATIC shash_t *hash_string    __proto((const char *,
  975.                      Ptrdiff_t,
  976.                      shash_t **,
  977.                      symint_t *));
  978.  
  979. STATIC symint_t    add_string    __proto((varray_t *,
  980.                      shash_t **,
  981.                      const char *,
  982.                      const char *,
  983.                      shash_t **));
  984.  
  985. STATIC symint_t    add_local_symbol
  986.                 __proto((const char *,
  987.                      const char *,
  988.                      st_t,
  989.                      sc_t,
  990.                      symint_t,
  991.                      symint_t));
  992.  
  993. STATIC symint_t    add_ext_symbol    __proto((const char *,
  994.                      const char *,
  995.                      st_t,
  996.                      sc_t,
  997.                      long,
  998.                      symint_t,
  999.                      int));
  1000.  
  1001. STATIC symint_t    add_aux_sym_symint
  1002.                 __proto((symint_t));
  1003.  
  1004. STATIC symint_t    add_aux_sym_rndx
  1005.                 __proto((int, symint_t));
  1006.  
  1007. STATIC symint_t    add_aux_sym_tir    __proto((type_info_t *,
  1008.                      hash_state_t,
  1009.                      thash_t **));
  1010.  
  1011. STATIC tag_t *    get_tag        __proto((const char *,
  1012.                      const char *,
  1013.                      symint_t,
  1014.                      bt_t));
  1015.  
  1016. STATIC void    add_unknown_tag    __proto((tag_t *));
  1017.  
  1018. STATIC void    add_procedure    __proto((const char *,
  1019.                      const char *));
  1020.  
  1021. STATIC void    add_file    __proto((const char *,
  1022.                      const char *));
  1023.  
  1024. STATIC void    add_bytes    __proto((varray_t *,
  1025.                      char *,
  1026.                      Size_t));
  1027.  
  1028. STATIC void    add_varray_page    __proto((varray_t *));
  1029.  
  1030. STATIC void    update_headers    __proto((void));
  1031.  
  1032. STATIC void    write_varray    __proto((varray_t *, off_t, const char *));
  1033. STATIC void    write_object    __proto((void));
  1034. STATIC char    *st_to_string    __proto((st_t));
  1035. STATIC char    *sc_to_string    __proto((sc_t));
  1036. STATIC char    *read_line    __proto((void));
  1037. STATIC void    parse_input    __proto((void));
  1038. STATIC void    parse_begin    __proto((const char *));
  1039. STATIC void    parse_bend    __proto((const char *));
  1040. STATIC void    parse_def    __proto((const char *));
  1041. STATIC void    parse_end    __proto((const char *));
  1042. STATIC void    parse_ent    __proto((const char *));
  1043. STATIC void    parse_file    __proto((const char *));
  1044. STATIC page_t  *read_seek    __proto((Size_t, off_t, const char *));
  1045. STATIC void    copy_object    __proto((void));
  1046.  
  1047. STATIC void    catch_signal    __proto((int));
  1048. STATIC page_t  *allocate_page    __proto((void));
  1049.  
  1050. STATIC page_t  *allocate_multiple_pages
  1051.                 __proto((Size_t));
  1052.  
  1053. #ifndef MALLOC_CHECK
  1054. STATIC page_t  *allocate_cluster
  1055.                 __proto((Size_t));
  1056. #endif
  1057.  
  1058. STATIC word8_t *allocate_word8    __proto((void));
  1059. STATIC void    free_word8    __proto((word8_t *));
  1060.  
  1061.  
  1062. /* Prototypes for library functions used.  */
  1063. #if !defined(NO_LIB_PROTOTYPE) && !defined(_OSF_SOURCE) && !defined(_STDIO_H_)
  1064. extern char  *strchr        __proto((const char *, int));
  1065. extern char  *strrchr        __proto((const char *, int));
  1066. extern int    strcmp        __proto((const char *, const char *));
  1067. extern long   strtol        __proto((const char *, char **, int));
  1068. extern int    memcmp        __proto((CPTR_T, CPTR_T, Size_t));
  1069. extern time_t time        __proto((time_t *));
  1070. extern int    fputc        __proto((int, FILE *));
  1071. extern int    vprintf        __proto((const char *, va_list));
  1072. extern int    vfprintf        __proto((FILE *, const char *, va_list));
  1073. extern int    vsprintf        __proto((char *, const char *, va_list));
  1074. extern int    fclose        __proto((FILE *));
  1075. extern int    fseek        __proto((FILE *, long, int));
  1076. extern long   ftell        __proto((FILE *));
  1077. extern FILE  *fopen        __proto((const char *, const char *));
  1078. extern FILE  *freopen        __proto((const char *, const char *, FILE *));
  1079. extern int    fflush        __proto((FILE *));
  1080. extern void   perror        __proto((const char *));
  1081. extern void   exit        __proto((int));
  1082. extern int    open        __proto((const char *, int, ...));
  1083.  
  1084. #ifndef sgi
  1085. extern int    setvbuf        __proto((FILE *, char *, int, int));
  1086. extern int    fputs        __proto((char *, FILE *));
  1087. #endif
  1088.  
  1089. #ifndef NO_LIB_INTERNALS
  1090. extern int    _filbuf        __proto((FILE *));
  1091. extern int    _flsbuf        __proto((int, FILE *));
  1092. #endif
  1093. #endif
  1094.  
  1095. extern char  *sbrk        __proto((int));
  1096. extern PTR_T  malloc        __proto((Size_t));
  1097. extern PTR_T  calloc        __proto((Size_t, Size_t));
  1098. extern PTR_T  realloc        __proto((PTR_T, Size_t));
  1099. extern void   free        __proto((PTR_T));
  1100. extern int    close        __proto((int));
  1101. extern int    write        __proto((int, CPTR_T, Size_t));
  1102. extern int    read        __proto((int, PTR_T, Size_t));
  1103. extern long   lseek        __proto((int, long, int));
  1104. extern int    ftruncate        __proto((int, long));
  1105. extern int    getopt        __proto((int, char **, const char *));
  1106. extern int    fstat        __proto((int, struct stat *));
  1107. extern char  *mktemp        __proto((char *));
  1108.  
  1109. extern char *optarg;
  1110. extern int   optind;
  1111. extern int   opterr;
  1112.  
  1113. #ifndef SEEK_SET    /* Symbolic constants for the "fseek" function: */
  1114. #define    SEEK_SET 0    /* Set file pointer to offset */
  1115. #define    SEEK_CUR 1    /* Set file pointer to its current value plus offset */
  1116. #define    SEEK_END 2    /* Set file pointer to the size of the file plus offset */
  1117. #endif
  1118.  
  1119.  
  1120. /* List of assembler pseudo ops and beginning sequences that need
  1121.    special actions.  Someday, this should be a hash table, and such,
  1122.    but for now a linear list of names and calls to memcmp will
  1123.    do...... */
  1124.  
  1125. typedef struct _pseudo_ops {
  1126.   const char *name;            /* pseduo-op in ascii */
  1127.   int len;                /* length of name to compare */
  1128.   void (*func) __proto((const char *));    /* function to handle line */
  1129. } pseudo_ops_t;
  1130.  
  1131. static pseudo_ops_t pseudo_ops[] = {
  1132.   { "#.def",    sizeof("#.def")-1,    parse_def },
  1133.   { "#.begin",    sizeof("#.begin")-1,    parse_begin },
  1134.   { "#.bend",    sizeof("#.bend")-1,    parse_bend },
  1135.   { ".end",    sizeof(".end")-1,    parse_end },
  1136.   { ".ent",    sizeof(".ent")-1,    parse_ent },
  1137.   { ".file",    sizeof(".file")-1,    parse_file },
  1138. };
  1139.  
  1140.  
  1141. /* Add a page to a varray object.  */
  1142.  
  1143. STATIC void
  1144. add_varray_page (vp)
  1145.      varray_t *vp;                /* varray to add page to */
  1146. {
  1147.   vlinks_t *new_links = (vlinks_t *) allocate_word8 ();
  1148.  
  1149. #ifdef MALLOC_CHECK
  1150.   if (vp->object_size > 1)
  1151.     new_links->datum = (page_t *) xcalloc (1, vp->object_size);
  1152.   else
  1153. #endif
  1154.     new_links->datum = allocate_page ();
  1155.  
  1156.   new_links->start_index = vp->num_allocated;
  1157.   vp->objects_last_page = 0;
  1158.  
  1159.   if (vp->first == (vlinks_t *)0)        /* first allocation? */
  1160.     vp->first = vp->last = new_links;
  1161.   else
  1162.     {                        /* 2nd or greater allocation */
  1163.       new_links->prev = vp->last;
  1164.       vp->last->next = new_links;
  1165.       vp->last = new_links;
  1166.     }
  1167. }
  1168.  
  1169.  
  1170. /* Compute hash code (from tree.c) */
  1171.  
  1172. #define HASHBITS 30
  1173.  
  1174. STATIC shash_t *
  1175. hash_string (text, hash_len, hash_tbl, ret_hash_index)
  1176.      const char *text;            /* ptr to text to hash */
  1177.      Ptrdiff_t hash_len;        /* length of the text */
  1178.      shash_t **hash_tbl;        /* hash table */
  1179.      symint_t *ret_hash_index;        /* ptr to store hash index */
  1180. {
  1181.   register unsigned long hi;
  1182.   register Ptrdiff_t i;
  1183.   register shash_t *ptr;
  1184.   register int first_ch = *text;
  1185.  
  1186.   hi = hash_len;
  1187.   for (i = 0; i < hash_len; i++)
  1188.     hi = ((hi & 0x003fffff) * 613) + (text[i] & 0xff);
  1189.  
  1190.   hi &= (1 << HASHBITS) - 1;
  1191.   hi %= SHASH_SIZE;
  1192.  
  1193.   if (ret_hash_index != (symint_t *)0)
  1194.     *ret_hash_index = hi;
  1195.  
  1196.   for (ptr = hash_tbl[hi]; ptr != (shash_t *)0; ptr = ptr->next)
  1197.     if (hash_len == ptr->len
  1198.     && first_ch == ptr->string[0]
  1199.     && memcmp ((CPTR_T) text, (CPTR_T) ptr->string, hash_len) == 0)
  1200.       break;
  1201.  
  1202.   return ptr;
  1203. }
  1204.  
  1205.  
  1206. /* Add a string (and null pad) to one of the string tables.  A
  1207.    consequence of hashing strings, is that we don't let strings
  1208.    cross page boundaries.  The extra nulls will be ignored.  */
  1209.  
  1210. STATIC symint_t
  1211. add_string (vp, hash_tbl, start, end_p1, ret_hash)
  1212.      varray_t *vp;            /* string virtual array */
  1213.      shash_t **hash_tbl;        /* ptr to hash table */
  1214.      const char *start;            /* 1st byte in string */
  1215.      const char *end_p1;        /* 1st byte after string */
  1216.      shash_t **ret_hash;        /* return hash pointer */
  1217. {
  1218.   register Ptrdiff_t len = end_p1 - start;
  1219.   register shash_t *hash_ptr;
  1220.   symint_t hi;
  1221.  
  1222.   if (len >= PAGE_USIZE)
  1223.     fatal ("String too big (%ld bytes)", (long) len);
  1224.  
  1225.   hash_ptr = hash_string (start, len, hash_tbl, &hi);
  1226.   if (hash_ptr == (shash_t *)0)
  1227.     {
  1228.       register char *p;
  1229.  
  1230.       if (vp->objects_last_page + len >= PAGE_USIZE)
  1231.     {
  1232.       vp->num_allocated =
  1233.         ((vp->num_allocated + PAGE_USIZE - 1) / PAGE_USIZE) * PAGE_USIZE;
  1234.       add_varray_page (vp);
  1235.     }
  1236.  
  1237.       hash_ptr = (shash_t *) allocate_word8 ();
  1238.       hash_ptr->next = hash_tbl[hi];
  1239.       hash_tbl[hi] = hash_ptr;
  1240.  
  1241.       hash_ptr->len = len;
  1242.       hash_ptr->index = vp->num_allocated;
  1243.       hash_ptr->string = p =
  1244.     & vp->last->datum->byte[ vp->objects_last_page ];
  1245.  
  1246.       vp->objects_last_page += len+1;
  1247.       vp->num_allocated += len+1;
  1248.  
  1249.       while (len-- > 0)
  1250.     *p++ = *start++;
  1251.  
  1252.       *p = '\0';
  1253.     }
  1254.  
  1255.   if (ret_hash != (shash_t **)0)
  1256.     *ret_hash = hash_ptr;
  1257.  
  1258.   return hash_ptr->index;
  1259. }
  1260.  
  1261.  
  1262. /* Add a local symbol.  */
  1263.  
  1264. STATIC symint_t
  1265. add_local_symbol (str_start, str_end_p1, type, storage, value, indx)
  1266.      const char *str_start;        /* first byte in string */
  1267.      const char *str_end_p1;        /* first byte after string */
  1268.      st_t type;                /* symbol type */
  1269.      sc_t storage;            /* storage class */
  1270.      symint_t value;            /* value of symbol */
  1271.      symint_t indx;            /* index to local/aux. syms */
  1272. {
  1273.   register symint_t ret;
  1274.   register SYMR *psym;
  1275.   register scope_t *pscope;
  1276.   register thead_t *ptag_head;
  1277.   register tag_t *ptag;
  1278.   register tag_t *ptag_next;
  1279.   register varray_t *vp = &cur_file_ptr->symbols;
  1280.   register int scope_delta = 0;
  1281.   shash_t *hash_ptr = (shash_t *)0;
  1282.  
  1283.   if (vp->objects_last_page == vp->objects_per_page)
  1284.     add_varray_page (vp);
  1285.  
  1286.   psym = &vp->last->datum->sym[ vp->objects_last_page++ ];
  1287.  
  1288.   psym->value = value;
  1289.   psym->st = (unsigned) type;
  1290.   psym->sc = (unsigned) storage;
  1291.   psym->index = indx;
  1292.   psym->iss = (str_start == (const char *)0)
  1293.         ? 0
  1294.         : add_string (&cur_file_ptr->strings,
  1295.                   &cur_file_ptr->shash_head[0],
  1296.                   str_start,
  1297.                   str_end_p1,
  1298.                   &hash_ptr);
  1299.  
  1300.   ret = vp->num_allocated++;
  1301.  
  1302.   /* Save the symbol within the hash table if this is a static
  1303.      item.  */
  1304.   if (type == st_Global || type == st_Static || type == st_Label
  1305.       || type == st_Proc || type == st_StaticProc)
  1306.     hash_ptr->sym_ptr = psym;
  1307.  
  1308.   /* push or pop a scope if appropriate.  */
  1309.   switch (type)
  1310.     {
  1311.     default:
  1312.       break;
  1313.  
  1314.     case st_File:            /* beginning of file */
  1315.     case st_Proc:            /* procedure */
  1316.     case st_StaticProc:            /* static procedure */
  1317.     case st_Block:            /* begin scope */
  1318.       pscope = (scope_t *) allocate_word8 ();
  1319.       pscope->prev = cur_file_ptr->cur_scope;
  1320.       pscope->lsym = psym;
  1321.       pscope->lnumber = ret;
  1322.       pscope->type = type;
  1323.       cur_file_ptr->cur_scope = pscope;
  1324.  
  1325.       if (type != st_File)
  1326.     scope_delta = 1;
  1327.  
  1328.       /* For every block type except file, struct, union, or
  1329.      enumeration blocks, push a level on the tag stack.  We omit
  1330.      file types, so that tags can span file boundaries.  */
  1331.       if (type != st_File && storage != sc_Info)
  1332.     {
  1333.       ptag_head = (thead_t *) allocate_word8 ();
  1334.       ptag_head->first_tag = 0;
  1335.       ptag_head->prev = cur_tag_head;
  1336.       cur_tag_head = ptag_head;
  1337.     }
  1338.       break;
  1339.  
  1340.     case st_End:
  1341.       pscope = cur_file_ptr->cur_scope;
  1342.       if (pscope == (scope_t *)0)
  1343.     error ("internal error, too many st_End's");
  1344.  
  1345.       else
  1346.     {
  1347.       st_t begin_type = (st_t) pscope->lsym->st;
  1348.  
  1349.       if (begin_type != st_File)
  1350.         scope_delta = -1;
  1351.  
  1352.       /* Except for file, structure, union, or enumeration end
  1353.          blocks remove all tags created within this scope.  */
  1354.       if (begin_type != st_File && storage != sc_Info)
  1355.         {
  1356.           ptag_head = cur_tag_head;
  1357.           cur_tag_head = ptag_head->prev;
  1358.  
  1359.           for (ptag = ptag_head->first_tag;
  1360.            ptag != (tag_t *)0;
  1361.            ptag = ptag_next)
  1362.         {
  1363.           if (ptag->forward_ref != (forward_t *)0)
  1364.             add_unknown_tag (ptag);
  1365.  
  1366.           ptag_next = ptag->same_block;
  1367.           ptag->hash_ptr->tag_ptr = ptag->same_name;
  1368.           free_word8 ((word8_t *) ptag);
  1369.         }
  1370.  
  1371.           free_word8 ((word8_t *) ptag_head);
  1372.         }
  1373.  
  1374.       cur_file_ptr->cur_scope = pscope->prev;
  1375.       psym->index = pscope->lnumber;    /* blk end gets begin sym # */
  1376.  
  1377.       if (storage != sc_Info)
  1378.         psym->iss = pscope->lsym->iss;    /* blk end gets same name */
  1379.  
  1380.       if (begin_type == st_File || begin_type == st_Block)
  1381.         pscope->lsym->index = ret+1; /* block begin gets next sym # */
  1382.  
  1383.       /* Functions push two or more aux words as follows:
  1384.          1st word: index+1 of the end symbol
  1385.          2nd word: type of the function (plus any aux words needed).
  1386.          Also, tie the external pointer back to the function begin symbol.  */
  1387.       else
  1388.         {
  1389.           symint_t type;
  1390.           pscope->lsym->index = add_aux_sym_symint (ret+1);
  1391.           type = add_aux_sym_tir (&last_func_type_info,
  1392.                       hash_no,
  1393.                       &cur_file_ptr->thash_head[0]);
  1394.           if (last_func_eptr)
  1395.         {
  1396.           last_func_eptr->ifd = cur_file_ptr->file_index;
  1397.           last_func_eptr->asym.index = type;
  1398.         }
  1399.         }
  1400.  
  1401.       free_word8 ((word8_t *) pscope);
  1402.     }
  1403.     }
  1404.  
  1405.   cur_file_ptr->nested_scopes += scope_delta;
  1406.  
  1407.   if (debug && type != st_File
  1408.       && (debug > 2 || type == st_Block || type == st_End
  1409.       || type == st_Proc || type == st_StaticProc))
  1410.     {
  1411.       char *sc_str = sc_to_string (storage);
  1412.       char *st_str = st_to_string (type);
  1413.       int depth = cur_file_ptr->nested_scopes + (scope_delta < 0);
  1414.  
  1415.       fprintf (stderr,
  1416.            "\tlsym\tv= %10ld, depth= %2d, sc= %-12s",
  1417.            value, depth, sc_str);
  1418.  
  1419.       if (str_start && str_end_p1 - str_start > 0)
  1420.     fprintf (stderr, " st= %-11s name= %.*s\n", st_str, str_end_p1 - str_start, str_start);
  1421.       else
  1422.     {
  1423.       Size_t len = strlen (st_str);
  1424.       fprintf (stderr, " st= %.*s\n", len-1, st_str);
  1425.     }
  1426.     }
  1427.  
  1428.   return ret;
  1429. }
  1430.  
  1431.  
  1432. /* Add an external symbol.  */
  1433.  
  1434. STATIC symint_t
  1435. add_ext_symbol (str_start, str_end_p1, type, storage, value, indx, ifd)
  1436.      const char *str_start;        /* first byte in string */
  1437.      const char *str_end_p1;        /* first byte after string */
  1438.      st_t type;                /* symbol type */
  1439.      sc_t storage;            /* storage class */
  1440.      long value;            /* value of symbol */
  1441.      symint_t indx;            /* index to local/aux. syms */
  1442.      int ifd;                /* file index */
  1443. {
  1444.   register EXTR *psym;
  1445.   register varray_t *vp = &ext_symbols;
  1446.   shash_t *hash_ptr = (shash_t *)0;
  1447.  
  1448.   if (debug > 1)
  1449.     {
  1450.       char *sc_str = sc_to_string (storage);
  1451.       char *st_str = st_to_string (type);
  1452.  
  1453.       fprintf (stderr,
  1454.            "\tesym\tv= %10ld, ifd= %2d, sc= %-12s",
  1455.            value, ifd, sc_str);
  1456.  
  1457.       if (str_start && str_end_p1 - str_start > 0)
  1458.     fprintf (stderr, " st= %-11s name= %.*s\n", st_str, str_end_p1 - str_start, str_start);
  1459.       else
  1460.     fprintf (stderr, " st= %s\n", st_str);
  1461.     }
  1462.  
  1463.   if (vp->objects_last_page == vp->objects_per_page)
  1464.     add_varray_page (vp);
  1465.  
  1466.   psym = &vp->last->datum->esym[ vp->objects_last_page++ ];
  1467.  
  1468.   psym->ifd = ifd;
  1469.   psym->asym.value = value;
  1470.   psym->asym.st    = (unsigned) type;
  1471.   psym->asym.sc    = (unsigned) storage;
  1472.   psym->asym.index = indx;
  1473.   psym->asym.iss   = (str_start == (const char *)0)
  1474.             ? 0
  1475.             : add_string (&ext_strings,
  1476.                       &ext_str_hash[0],
  1477.                       str_start,
  1478.                       str_end_p1,
  1479.                       &hash_ptr);
  1480.  
  1481.   hash_ptr->esym_ptr = psym;
  1482.   return vp->num_allocated++;
  1483. }
  1484.  
  1485.  
  1486. /* Add an auxiliary symbol (passing a symint).  */
  1487.  
  1488. STATIC symint_t
  1489. add_aux_sym_symint (aux_word)
  1490.      symint_t aux_word;        /* auxilary information word */
  1491. {
  1492.   register AUXU *aux_ptr;
  1493.   register efdr_t *file_ptr = cur_file_ptr;
  1494.   register varray_t *vp = &file_ptr->aux_syms;
  1495.  
  1496.   if (vp->objects_last_page == vp->objects_per_page)
  1497.     add_varray_page (vp);
  1498.  
  1499.   aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
  1500.   aux_ptr->isym = aux_word;
  1501.  
  1502.   return vp->num_allocated++;
  1503. }
  1504.  
  1505.  
  1506. /* Add an auxiliary symbol (passing a file/symbol index combo).  */
  1507.  
  1508. STATIC symint_t
  1509. add_aux_sym_rndx (file_index, sym_index)
  1510.      int file_index;
  1511.      symint_t sym_index;
  1512. {
  1513.   register AUXU *aux_ptr;
  1514.   register efdr_t *file_ptr = cur_file_ptr;
  1515.   register varray_t *vp = &file_ptr->aux_syms;
  1516.  
  1517.   if (vp->objects_last_page == vp->objects_per_page)
  1518.     add_varray_page (vp);
  1519.  
  1520.   aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
  1521.   aux_ptr->rndx.rfd   = file_index;
  1522.   aux_ptr->rndx.index = sym_index;
  1523.  
  1524.   return vp->num_allocated++;
  1525. }
  1526.  
  1527.  
  1528. /* Add an auxiliary symbol (passing the basic type and possibly
  1529.    type qualifiers).  */
  1530.  
  1531. STATIC symint_t
  1532. add_aux_sym_tir (t, state, hash_tbl)
  1533.      type_info_t *t;        /* current type information */
  1534.      hash_state_t state;    /* whether to hash type or not */
  1535.      thash_t **hash_tbl;    /* pointer to hash table to use */
  1536. {
  1537.   register AUXU *aux_ptr;
  1538.   register efdr_t *file_ptr = cur_file_ptr;
  1539.   register varray_t *vp = &file_ptr->aux_syms;
  1540.   static AUXU init_aux;
  1541.   symint_t ret;
  1542.   int i;
  1543.   AUXU aux;
  1544.  
  1545.   aux = init_aux;
  1546.   aux.ti.bt = (int) t->basic_type;
  1547.   aux.ti.continued = 0;
  1548.   aux.ti.fBitfield = t->bitfield;
  1549.  
  1550.   aux.ti.tq0 = (int) t->type_qualifiers[0];
  1551.   aux.ti.tq1 = (int) t->type_qualifiers[1];
  1552.   aux.ti.tq2 = (int) t->type_qualifiers[2];
  1553.   aux.ti.tq3 = (int) t->type_qualifiers[3];
  1554.   aux.ti.tq4 = (int) t->type_qualifiers[4];
  1555.   aux.ti.tq5 = (int) t->type_qualifiers[5];
  1556.  
  1557.  
  1558.   /* For anything that adds additional information, we must not hash,
  1559.      so check here, and reset our state. */
  1560.  
  1561.   if (state != hash_no
  1562.       && (t->type_qualifiers[0] == tq_Array
  1563.       || t->type_qualifiers[1] == tq_Array
  1564.       || t->type_qualifiers[2] == tq_Array
  1565.       || t->type_qualifiers[3] == tq_Array
  1566.       || t->type_qualifiers[4] == tq_Array
  1567.       || t->type_qualifiers[5] == tq_Array
  1568.       || t->basic_type == bt_Struct
  1569.       || t->basic_type == bt_Union
  1570.       || t->basic_type == bt_Enum
  1571.       || t->bitfield
  1572.       || t->num_dims > 0))
  1573.     state = hash_no;
  1574.  
  1575.   /* See if we can hash this type, and save some space, but some types
  1576.      can't be hashed (because they contain arrays or continuations),
  1577.      and others can be put into the hash list, but cannot use existing
  1578.      types because other aux entries precede this one.  */
  1579.  
  1580.   if (state != hash_no)
  1581.     {
  1582.       register thash_t *hash_ptr;
  1583.       register symint_t hi;
  1584.  
  1585.       hi = aux.isym & ((1 << HASHBITS) - 1);
  1586.       hi %= THASH_SIZE;
  1587.  
  1588.       for (hash_ptr = hash_tbl[hi];
  1589.        hash_ptr != (thash_t *)0;
  1590.        hash_ptr = hash_ptr->next)
  1591.     {
  1592.       if (aux.isym == hash_ptr->type.isym)
  1593.         break;
  1594.     }
  1595.  
  1596.       if (hash_ptr != (thash_t *)0 && state == hash_yes)
  1597.     return hash_ptr->index;
  1598.  
  1599.       if (hash_ptr == (thash_t *)0)
  1600.     {
  1601.       hash_ptr = (thash_t *) allocate_word8 ();
  1602.       hash_ptr->next = hash_tbl[hi];
  1603.       hash_ptr->type = aux;
  1604.       hash_ptr->index = vp->num_allocated;
  1605.       hash_tbl[hi] = hash_ptr;
  1606.     }
  1607.     }
  1608.  
  1609.   /* Everything is set up, add the aux symbol. */
  1610.   if (vp->objects_last_page == vp->objects_per_page)
  1611.     add_varray_page (vp);
  1612.  
  1613.   aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
  1614.   *aux_ptr = aux;
  1615.  
  1616.   ret = vp->num_allocated++;
  1617.  
  1618.   /* Add tag information if needed.  Structure, union, and enum
  1619.      references add 2 aux symbols: a [file index, symbol index]
  1620.      pointer to the structure type, and the current file index.  */
  1621.  
  1622.   if (t->basic_type == bt_Struct
  1623.       || t->basic_type == bt_Union
  1624.       || t->basic_type == bt_Enum)
  1625.     {
  1626.       register symint_t file_index = t->tag_ptr->ifd;
  1627.       register symint_t sym_index  = t->tag_ptr->index;
  1628.  
  1629.       if (t->unknown_tag)
  1630.     {
  1631.       (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
  1632.       (void) add_aux_sym_symint ((symint_t)-1);
  1633.     }
  1634.       else if (sym_index != indexNil)
  1635.     {
  1636.       (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
  1637.       (void) add_aux_sym_symint (file_index);
  1638.     }
  1639.       else
  1640.     {
  1641.       register forward_t *forward_ref = (forward_t *) allocate_word8 ();
  1642.  
  1643.       forward_ref->type_ptr = aux_ptr;
  1644.       forward_ref->next = t->tag_ptr->forward_ref;
  1645.       t->tag_ptr->forward_ref = forward_ref;
  1646.  
  1647.       (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
  1648.       forward_ref->index_ptr
  1649.         = &vp->last->datum->aux[ vp->objects_last_page - 1];
  1650.  
  1651.       (void) add_aux_sym_symint (file_index);
  1652.       forward_ref->ifd_ptr
  1653.         = &vp->last->datum->aux[ vp->objects_last_page - 1];
  1654.     }
  1655.     }
  1656.  
  1657.   /* Add bitfield length if it exists.  */
  1658.   if (t->bitfield)
  1659.     (void) add_aux_sym_symint ((symint_t)t->sizes[0]);
  1660.  
  1661.  
  1662.   /* Add information about array bounds if they exist.  */
  1663.   for (i = 0; i < t->num_dims; i++)
  1664.     {
  1665.       (void) add_aux_sym_rndx (ST_RFDESCAPE,
  1666.                    cur_file_ptr->int_type);
  1667.  
  1668.       (void) add_aux_sym_symint (cur_file_ptr->file_index);    /* file index*/
  1669.       (void) add_aux_sym_symint ((symint_t)0);            /* low bound */
  1670.       (void) add_aux_sym_symint (t->dimensions[i] - 1);        /* high bound*/
  1671.       (void) add_aux_sym_symint ((t->dimensions[i] == 0)    /* stride */
  1672.                   ? 0
  1673.                   : (t->sizes[i] * 8) / t->dimensions[i]);
  1674.     };
  1675.  
  1676.   return ret;
  1677. }
  1678.  
  1679.  
  1680. /* Add a tag to the tag table (unless it already exists).  */
  1681.  
  1682. STATIC tag_t *
  1683. get_tag (tag_start, tag_end_p1, index, basic_type)
  1684.      const char *tag_start;        /* 1st byte of tag name */
  1685.      const char *tag_end_p1;        /* 1st byte after tag name */
  1686.      symint_t index;            /* index of tag start block */
  1687.      bt_t basic_type;            /* bt_Struct, bt_Union, or bt_Enum */
  1688. {
  1689.   shash_t *hash_ptr;
  1690.   tag_t *tag_ptr;
  1691.   hash_ptr = hash_string (tag_start,
  1692.               tag_end_p1 - tag_start,
  1693.               &tag_hash[0],
  1694.               (symint_t *)0);
  1695.  
  1696.   if (hash_ptr != (shash_t *)0
  1697.       && hash_ptr->tag_ptr != (tag_t *)0)
  1698.   {
  1699.     tag_ptr = hash_ptr->tag_ptr;
  1700.     if (index != indexNil)
  1701.       {
  1702.     tag_ptr->basic_type = basic_type;
  1703.     tag_ptr->ifd        = cur_file_ptr->file_index;
  1704.     tag_ptr->index        = index;
  1705.       }
  1706.     return tag_ptr;
  1707.   }
  1708.  
  1709.   (void) add_string (&tag_strings,
  1710.              &tag_hash[0],
  1711.              tag_start,
  1712.              tag_end_p1,
  1713.              &hash_ptr);
  1714.  
  1715.   tag_ptr = (tag_t *) allocate_word8 ();
  1716.   tag_ptr->forward_ref    = (forward_t *) 0;
  1717.   tag_ptr->hash_ptr    = hash_ptr;
  1718.   tag_ptr->same_name    = hash_ptr->tag_ptr;
  1719.   tag_ptr->basic_type    = basic_type;
  1720.   tag_ptr->index    = index;
  1721.   tag_ptr->ifd        = (index == indexNil) ? -1 : cur_file_ptr->file_index;
  1722.   tag_ptr->same_block    = cur_tag_head->first_tag;
  1723.  
  1724.   cur_tag_head->first_tag = tag_ptr;
  1725.   hash_ptr->tag_ptr      = tag_ptr;
  1726.  
  1727.   return tag_ptr;
  1728. }
  1729.  
  1730.  
  1731. /* Add an unknown {struct, union, enum} tag.  */
  1732.  
  1733. STATIC void
  1734. add_unknown_tag (ptag)
  1735.      tag_t    *ptag;        /* pointer to tag information */
  1736. {
  1737.   shash_t *hash_ptr    = ptag->hash_ptr;
  1738.   char *name_start    = hash_ptr->string;
  1739.   char *name_end_p1    = name_start + hash_ptr->len;
  1740.   forward_t *f_next    = ptag->forward_ref;
  1741.   forward_t *f_cur;
  1742.   int sym_index;
  1743.   int file_index    = cur_file_ptr->file_index;
  1744.  
  1745.   if (debug > 1)
  1746.     {
  1747.       char *agg_type    = "{unknown aggregate type}";
  1748.       switch (ptag->basic_type)
  1749.     {
  1750.     case bt_Struct:    agg_type = "struct";    break;
  1751.     case bt_Union:    agg_type = "union";    break;
  1752.     case bt_Enum:    agg_type = "enum";    break;
  1753.     default:                break;
  1754.     }
  1755.  
  1756.       fprintf (stderr, "unknown %s %.*s found\n", agg_type,
  1757.            hash_ptr->len, name_start);
  1758.     }
  1759.  
  1760.   sym_index = add_local_symbol (name_start,
  1761.                 name_end_p1,
  1762.                 st_Block,
  1763.                 sc_Info,
  1764.                 (symint_t)0,
  1765.                 (symint_t)0);
  1766.  
  1767.   (void) add_local_symbol (name_start,
  1768.                name_end_p1,
  1769.                st_End,
  1770.                sc_Info,
  1771.                (symint_t)0,
  1772.                (symint_t)0);
  1773.  
  1774.   while (f_next != (forward_t *)0)
  1775.     {
  1776.       f_cur  = f_next;
  1777.       f_next = f_next->next;
  1778.  
  1779.       f_cur->ifd_ptr->isym = file_index;
  1780.       f_cur->index_ptr->rndx.index = sym_index;
  1781.  
  1782.       free_word8 ((word8_t *) f_cur);
  1783.     }
  1784.  
  1785.   return;
  1786. }
  1787.  
  1788.  
  1789. /* Add a procedure to the current file's list of procedures, and record
  1790.    this is the current procedure.  If the assembler created a PDR for
  1791.    this procedure, use that to initialize the current PDR.  */
  1792.  
  1793. STATIC void
  1794. add_procedure (func_start, func_end_p1)
  1795.      const char *func_start;        /* 1st byte of func name */
  1796.      const char *func_end_p1;        /* 1st byte after func name */
  1797. {
  1798.   register PDR *new_proc_ptr;
  1799.   register efdr_t *file_ptr = cur_file_ptr;
  1800.   register varray_t *vp = &file_ptr->procs;
  1801.   register symint_t value = 0;
  1802.   register st_t proc_type = st_Proc;
  1803.   register shash_t *shash_ptr = hash_string (func_start,
  1804.                         func_end_p1 - func_start,
  1805.                         &orig_str_hash[0],
  1806.                         (symint_t *)0);
  1807.  
  1808.   if (debug)
  1809.     fputc ('\n', stderr);
  1810.  
  1811.   if (vp->objects_last_page == vp->objects_per_page)
  1812.     add_varray_page (vp);
  1813.  
  1814.   cur_proc_ptr = new_proc_ptr = &vp->last->datum->proc[ vp->objects_last_page++ ];
  1815.  
  1816.   vp->num_allocated++;
  1817.  
  1818.  
  1819.   /* Did the assembler create this procedure?  If so, get the PDR information.  */
  1820.   cur_oproc_ptr = (PDR *)0;
  1821.   if (shash_ptr != (shash_t *)0)
  1822.     {
  1823.       register PDR *old_proc_ptr = shash_ptr->proc_ptr;
  1824.       register SYMR *sym_ptr = shash_ptr->sym_ptr;
  1825.  
  1826.       if (old_proc_ptr != (PDR *)0
  1827.       && sym_ptr != (SYMR *)0
  1828.       && ((st_t)sym_ptr->st == st_Proc || (st_t)sym_ptr->st == st_StaticProc))
  1829.     {
  1830.       cur_oproc_ptr = old_proc_ptr;
  1831.       value = sym_ptr->value;
  1832.       proc_type = (st_t)sym_ptr->st;
  1833.       *new_proc_ptr = *old_proc_ptr;    /* initialize */
  1834.     }
  1835.     }
  1836.  
  1837.   if (cur_oproc_ptr == (PDR *)0)
  1838.     error ("Did not find a PDR block for %.*s", func_end_p1 - func_start, func_start);
  1839.  
  1840.   /* Determine the start of symbols. */
  1841.   new_proc_ptr->isym = file_ptr->symbols.num_allocated;
  1842.  
  1843.   /* Push the start of the function.  */
  1844.   (void) add_local_symbol (func_start,
  1845.                func_end_p1,
  1846.                proc_type,
  1847.                sc_Text,
  1848.                value,
  1849.                (symint_t)0);
  1850. }
  1851.  
  1852.  
  1853. /* Add a new filename, and set up all of the file relative
  1854.    virtual arrays (strings, symbols, aux syms, etc.).  Record
  1855.    where the current file structure lives.  */
  1856.  
  1857. STATIC void
  1858. add_file (file_start, file_end_p1)
  1859.      const char *file_start;        /* first byte in string */
  1860.      const char *file_end_p1;        /* first byte after string */
  1861. {
  1862.   static char zero_bytes[2] = { '\0', '\0' };
  1863.  
  1864.   register Ptrdiff_t len = file_end_p1 - file_start;
  1865.   register int first_ch = *file_start;
  1866.   register efdr_t *file_ptr;
  1867.  
  1868.   if (debug)
  1869.     fprintf (stderr, "\tfile\t%.*s\n", len, file_start);
  1870.  
  1871.   /* See if the file has already been created.  */
  1872.   for (file_ptr = first_file;
  1873.        file_ptr != (efdr_t *)0;
  1874.        file_ptr = file_ptr->next_file)
  1875.     {
  1876.       if (first_ch == file_ptr->name[0]
  1877.       && file_ptr->name[len] == '\0'
  1878.       && memcmp ((CPTR_T) file_start, (CPTR_T) file_ptr->name, len) == 0)
  1879.     {
  1880.       cur_file_ptr = file_ptr;
  1881.       break;
  1882.     }
  1883.     }
  1884.  
  1885.   /* If this is a new file, create it. */
  1886.   if (file_ptr == (efdr_t *)0)
  1887.     {
  1888.       if (file_desc.objects_last_page == file_desc.objects_per_page)
  1889.     add_varray_page (&file_desc);
  1890.  
  1891.       file_ptr = cur_file_ptr =
  1892.     &file_desc.last->datum->file[ file_desc.objects_last_page++ ];
  1893.       *file_ptr = init_file;
  1894.  
  1895.       file_ptr->file_index = file_desc.num_allocated++;
  1896.  
  1897.       /* Allocate the string hash table.  */
  1898.       file_ptr->shash_head = (shash_t **) allocate_page ();
  1899.  
  1900.       /* Make sure 0 byte in string table is null  */
  1901.       add_string (&file_ptr->strings,
  1902.           &file_ptr->shash_head[0],
  1903.           &zero_bytes[0],
  1904.           &zero_bytes[0],
  1905.           (shash_t **)0);
  1906.  
  1907.       if (file_end_p1 - file_start > PAGE_USIZE-2)
  1908.     fatal ("Filename goes over one page boundary.");
  1909.  
  1910.       /* Push the start of the filename. We assume that the filename
  1911.          will be stored at string offset 1.  */
  1912.       (void) add_local_symbol (file_start, file_end_p1, st_File, sc_Text,
  1913.                    (symint_t)0, (symint_t)0);
  1914.       file_ptr->fdr.rss = 1;
  1915.       file_ptr->name = &file_ptr->strings.last->datum->byte[1];
  1916.       file_ptr->name_len = file_end_p1 - file_start;
  1917.  
  1918.       /* Update the linked list of file descriptors.  */
  1919.       if (first_file == (efdr_t *)0)
  1920.     first_file = file_ptr;
  1921.       else
  1922.     last_file->next_file = file_ptr;
  1923.  
  1924.       last_file = file_ptr;
  1925.  
  1926.       /* Add void & int types to the file (void should be first to catch
  1927.      errant 0's within the index fields).  */
  1928.       file_ptr->void_type = add_aux_sym_tir (&void_type_info,
  1929.                          hash_yes,
  1930.                          &cur_file_ptr->thash_head[0]);
  1931.  
  1932.       file_ptr->int_type = add_aux_sym_tir (&int_type_info,
  1933.                         hash_yes,
  1934.                         &cur_file_ptr->thash_head[0]);
  1935.     }
  1936. }
  1937.  
  1938.  
  1939. /* Add a stream of random bytes to a varray.  */
  1940.  
  1941. STATIC void
  1942. add_bytes (vp, input_ptr, nitems)
  1943.      varray_t *vp;            /* virtual array to add too */
  1944.      char *input_ptr;            /* start of the bytes */
  1945.      Size_t nitems;            /* # items to move */
  1946. {
  1947.   register Size_t move_items;
  1948.   register Size_t move_bytes;
  1949.   register char *ptr;
  1950.  
  1951.   while (nitems > 0)
  1952.     {
  1953.       if (vp->objects_last_page >= vp->objects_per_page)
  1954.     add_varray_page (vp);
  1955.  
  1956.       ptr = &vp->last->datum->byte[ vp->objects_last_page * vp->object_size ];
  1957.       move_items = vp->objects_per_page - vp->objects_last_page;
  1958.       if (move_items > nitems)
  1959.     move_items = nitems;
  1960.  
  1961.       move_bytes = move_items * vp->object_size;
  1962.       nitems -= move_items;
  1963.  
  1964.       if (move_bytes >= 32)
  1965.     {
  1966.       (void) memcpy ((PTR_T) ptr, (CPTR_T) input_ptr, move_bytes);
  1967.       input_ptr += move_bytes;
  1968.     }
  1969.       else
  1970.     {
  1971.       while (move_bytes-- > 0)
  1972.         *ptr++ = *input_ptr++;
  1973.     }
  1974.     }
  1975. }
  1976.  
  1977.  
  1978. /* Convert storage class to string.  */
  1979.  
  1980. STATIC char *
  1981. sc_to_string(storage_class)
  1982.      sc_t storage_class;
  1983. {
  1984.   switch(storage_class)
  1985.     {
  1986.     case sc_Nil:     return "Nil,";
  1987.     case sc_Text:     return "Text,";
  1988.     case sc_Data:     return "Data,";
  1989.     case sc_Bss:     return "Bss,";
  1990.     case sc_Register:     return "Register,";
  1991.     case sc_Abs:     return "Abs,";
  1992.     case sc_Undefined:     return "Undefined,";
  1993.     case sc_CdbLocal:     return "CdbLocal,";
  1994.     case sc_Bits:     return "Bits,";
  1995.     case sc_CdbSystem:     return "CdbSystem,";
  1996.     case sc_RegImage:     return "RegImage,";
  1997.     case sc_Info:     return "Info,";
  1998.     case sc_UserStruct:     return "UserStruct,";
  1999.     case sc_SData:     return "SData,";
  2000.     case sc_SBss:     return "SBss,";
  2001.     case sc_RData:     return "RData,";
  2002.     case sc_Var:     return "Var,";
  2003.     case sc_Common:     return "Common,";
  2004.     case sc_SCommon:     return "SCommon,";
  2005.     case sc_VarRegister: return "VarRegister,";
  2006.     case sc_Variant:     return "Variant,";
  2007.     case sc_SUndefined:     return "SUndefined,";
  2008.     case sc_Init:     return "Init,";
  2009.     case sc_Max:     return "Max,";
  2010.     }
  2011.  
  2012.   return "???,";
  2013. }
  2014.  
  2015.  
  2016. /* Convert symbol type to string.  */
  2017.  
  2018. STATIC char *
  2019. st_to_string(symbol_type)
  2020.      st_t symbol_type;
  2021. {
  2022.   switch(symbol_type)
  2023.     {
  2024.     case st_Nil:    return "Nil,";
  2025.     case st_Global:    return "Global,";
  2026.     case st_Static:    return "Static,";
  2027.     case st_Param:    return "Param,";
  2028.     case st_Local:    return "Local,";
  2029.     case st_Label:    return "Label,";
  2030.     case st_Proc:    return "Proc,";
  2031.     case st_Block:    return "Block,";
  2032.     case st_End:    return "End,";
  2033.     case st_Member:    return "Member,";
  2034.     case st_Typedef:    return "Typedef,";
  2035.     case st_File:    return "File,";
  2036.     case st_RegReloc:    return "RegReloc,";
  2037.     case st_Forward:    return "Forward,";
  2038.     case st_StaticProc:    return "StaticProc,";
  2039.     case st_Constant:    return "Constant,";
  2040.     case st_Str:    return "String,";
  2041.     case st_Number:    return "Number,";
  2042.     case st_Expr:    return "Expr,";
  2043.     case st_Type:    return "Type,";
  2044.     case st_Max:    return "Max,";
  2045.     }
  2046.  
  2047.   return "???,";
  2048. }
  2049.  
  2050.  
  2051. /* Read a line from standard input, and return the start of the
  2052.    buffer (which is grows if the line is too big).  */
  2053.  
  2054. STATIC char *
  2055. read_line __proto((void))
  2056. {
  2057.   register int ch;
  2058.   register char *ptr;
  2059.  
  2060.   if (cur_line_start == (char *)0)
  2061.     {                /* allocate initial page */
  2062.       cur_line_start = (char *) allocate_page ();
  2063.       cur_line_alloc = PAGE_SIZE;
  2064.     }
  2065.  
  2066.   cur_line_nbytes = 0;
  2067.   line_number++;
  2068.  
  2069.   for (ptr = cur_line_start; (ch = getchar ()) != EOF; *ptr++ = ch)
  2070.     {
  2071.       if (++cur_line_nbytes >= cur_line_alloc-1)
  2072.     {
  2073.       register int num_pages = cur_line_alloc / PAGE_SIZE;
  2074.       register char *old_buffer = cur_line_start;
  2075.  
  2076.       cur_line_alloc += PAGE_SIZE;
  2077.       cur_line_start = (char *) allocate_multiple_pages (num_pages+1);
  2078.       memcpy (cur_line_start, old_buffer, num_pages * PAGE_SIZE);
  2079.  
  2080.       ptr = cur_line_start + cur_line_nbytes - 1;
  2081.     }
  2082.  
  2083.       if (ch == '\n')
  2084.     {
  2085.       *ptr++ = '\n';
  2086.       *ptr = '\0';
  2087.       cur_line_ptr = cur_line_start;
  2088.       return cur_line_ptr;
  2089.     }
  2090.     }
  2091.  
  2092.   if (ferror (stdin))
  2093.     pfatal_with_name (input_name);
  2094.  
  2095.   cur_line_ptr = (char *)0;
  2096.   return (char *)0;
  2097. }
  2098.  
  2099.  
  2100. /* Parse #.begin directives which have a label as the first argument
  2101.    which gives the location of the start of the block.  */
  2102.  
  2103. STATIC void
  2104. parse_begin (start)
  2105.      const char *start;            /* start of directive */
  2106. {
  2107.   const char *end_p1;            /* end of label */
  2108.   int ch;
  2109.   shash_t *hash_ptr;            /* hash pointer to lookup label */
  2110.  
  2111.   if (cur_file_ptr == (efdr_t *)0)
  2112.     {
  2113.       error ("#.begin directive without a preceeding .file directive");
  2114.       return;
  2115.     }
  2116.  
  2117.   if (cur_proc_ptr == (PDR *)0)
  2118.     {
  2119.       error ("#.begin directive without a preceeding .ent directive");
  2120.       return;
  2121.     }
  2122.  
  2123.   for (end_p1 = start; (ch = *end_p1) != '\0' && !isspace (ch); end_p1++)
  2124.     ;
  2125.  
  2126.   hash_ptr = hash_string (start,
  2127.               end_p1 - start,
  2128.               &orig_str_hash[0],
  2129.               (symint_t *)0);
  2130.  
  2131.   if (hash_ptr == (shash_t *)0)
  2132.     {
  2133.       error ("Label %.*s not found for #.begin", end_p1 - start, start);
  2134.       return;
  2135.     }
  2136.  
  2137.   (void) add_local_symbol ((const char *)0,
  2138.                (const char *)0,
  2139.                st_Block,
  2140.                sc_Text,
  2141.                (symint_t)hash_ptr->sym_ptr->value,
  2142.                (symint_t)0);
  2143. }
  2144.  
  2145.  
  2146. /* Parse #.bend directives which have a label as the first argument
  2147.    which gives the location of the end of the block.  */
  2148.  
  2149. STATIC void
  2150. parse_bend (start)
  2151.      const char *start;            /* start of directive */
  2152. {
  2153.   const char *end_p1;            /* end of label */
  2154.   int ch;
  2155.   shash_t *hash_ptr;            /* hash pointer to lookup label */
  2156.  
  2157.   if (cur_file_ptr == (efdr_t *)0)
  2158.     {
  2159.       error ("#.begin directive without a preceeding .file directive");
  2160.       return;
  2161.     }
  2162.  
  2163.   if (cur_proc_ptr == (PDR *)0)
  2164.     {
  2165.       error ("#.begin directive without a preceeding .ent directive");
  2166.       return;
  2167.     }
  2168.  
  2169.   for (end_p1 = start; (ch = *end_p1) != '\0' && !isspace (ch); end_p1++)
  2170.     ;
  2171.  
  2172.   hash_ptr = hash_string (start,
  2173.               end_p1 - start,
  2174.               &orig_str_hash[0],
  2175.               (symint_t *)0);
  2176.  
  2177.   if (hash_ptr == (shash_t *)0)
  2178.     {
  2179.       error ("Label %.*s not found for #.begin", end_p1 - start, start);
  2180.       return;
  2181.     }
  2182.  
  2183.   (void) add_local_symbol ((const char *)0,
  2184.                (const char *)0,
  2185.                st_End,
  2186.                sc_Text,
  2187.                (symint_t)hash_ptr->sym_ptr->value,
  2188.                (symint_t)0);
  2189. }
  2190.  
  2191.  
  2192. /* Parse #.def directives, which are contain standard COFF subdirectives
  2193.    to describe the debugging format.  These subdirectives include:
  2194.  
  2195.     .scl    specify storage class
  2196.     .val    specify a value
  2197.     .endef    specify end of COFF directives
  2198.     .type    specify the type
  2199.     .size    specify the size of an array
  2200.     .dim    specify an array dimension
  2201.     .tag    specify a tag for a struct, union, or enum.  */
  2202.  
  2203. STATIC void
  2204. parse_def (name_start)
  2205.      const char *name_start;            /* start of directive */
  2206. {
  2207.   const char *dir_start;            /* start of current directive*/
  2208.   const char *dir_end_p1;            /* end+1 of current directive*/
  2209.   const char *arg_start;            /* start of current argument */
  2210.   const char *arg_end_p1;            /* end+1 of current argument */
  2211.   const char *name_end_p1;            /* end+1 of label */
  2212.   const char *tag_start      = (const char *)0;    /* start of tag name */
  2213.   const char *tag_end_p1  = (const char *)0;    /* end+1 of tag name */
  2214.   sc_t storage_class      = sc_Nil;
  2215.   st_t symbol_type      = st_Nil;
  2216.   type_info_t t;
  2217.   EXTR *eptr          = (EXTR *)0;        /* ext. sym equivalent to def*/
  2218.   int is_function      = 0;            /* != 0 if function */
  2219.   symint_t value      = 0;
  2220.   symint_t index      = cur_file_ptr->void_type;
  2221.   int error_line      = 0;
  2222.   symint_t arg_number;
  2223.   symint_t temp_array[ N_TQ ];
  2224.   int arg_was_number;
  2225.   int ch, i;
  2226.   Ptrdiff_t len;
  2227.  
  2228.   static int inside_enumeration = 0;        /* is this an enumeration? */
  2229.  
  2230.  
  2231.   /* Initialize the type information.  */
  2232.   t = type_info_init;
  2233.  
  2234.  
  2235.   /* Search for the end of the name being defined.  */
  2236.   for (name_end_p1 = name_start; (ch = *name_end_p1) != ';'; name_end_p1++)
  2237.     {
  2238.       if (ch == '\0' || isspace (ch))
  2239.     {
  2240.       error_line = __LINE__;
  2241.       saber_stop ();
  2242.       goto bomb_out;
  2243.     }
  2244.     }
  2245.  
  2246.   /* Parse the remaining subdirectives now.  */
  2247.   dir_start = name_end_p1+1;
  2248.   for (;;)
  2249.     {
  2250.       while ((ch = *dir_start) == ' ' || ch == '\t')
  2251.     ++dir_start;
  2252.  
  2253.       if (ch != '.')
  2254.     {
  2255.       error_line = __LINE__;
  2256.       saber_stop ();
  2257.       goto bomb_out;
  2258.     }
  2259.  
  2260.       /* Are we done? */
  2261.       if (dir_start[1] == 'e'
  2262.       && memcmp (dir_start, ".endef", sizeof (".endef")-1) == 0)
  2263.     break;
  2264.  
  2265.       /* Pick up the subdirective now */
  2266.       for (dir_end_p1 = dir_start+1;
  2267.        (ch = *dir_end_p1) != ' ' && ch != '\t';
  2268.        dir_end_p1++)
  2269.     {
  2270.       if (ch == '\0' || isspace (ch))
  2271.         {
  2272.           error_line = __LINE__;
  2273.           saber_stop ();
  2274.           goto bomb_out;
  2275.         }
  2276.     }
  2277.  
  2278.       /* Pick up the subdirective argument now.  */
  2279.       arg_was_number = arg_number = 0;
  2280.       arg_end_p1 = (const char *)0;
  2281.       arg_start = dir_end_p1+1;
  2282.       ch = *arg_start;
  2283.       while (ch == ' ' || ch == '\t')
  2284.     ch = *++arg_start;
  2285.  
  2286.       if (isdigit (ch) || ch == '-' || ch == '+')
  2287.     {
  2288.       int ch2;
  2289.       arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
  2290.       if (arg_end_p1 != arg_start || (ch2 = *arg_end_p1 != ';') || ch2 != ',')
  2291.         arg_was_number++;
  2292.     }
  2293.  
  2294.       else if (ch == '\0' || isspace (ch))
  2295.     {
  2296.       error_line = __LINE__;
  2297.       saber_stop ();
  2298.       goto bomb_out;
  2299.     }
  2300.  
  2301.       if (!arg_was_number)
  2302.     for (arg_end_p1 = arg_start+1; (ch = *arg_end_p1) != ';'; arg_end_p1++)
  2303.       {
  2304.         if (ch == '\0' || isspace (ch))
  2305.           {
  2306.         error_line = __LINE__;
  2307.         saber_stop ();
  2308.         goto bomb_out;
  2309.           }
  2310.       }
  2311.  
  2312.  
  2313.       /* Classify the directives now.  */
  2314.       len = dir_end_p1 - dir_start;
  2315.       switch (dir_start[1])
  2316.     {
  2317.     default:
  2318.       error_line = __LINE__;
  2319.       saber_stop ();
  2320.       goto bomb_out;
  2321.  
  2322.     case 'd':
  2323.       if (len == sizeof (".dim")-1
  2324.           && memcmp (dir_start, ".dim", sizeof (".dim")-1) == 0
  2325.           && arg_was_number)
  2326.         {
  2327.           symint_t *t_ptr = &temp_array[ N_TQ-1 ];
  2328.  
  2329.           *t_ptr = arg_number;
  2330.           while (*arg_end_p1 == ',' && arg_was_number)
  2331.         {
  2332.           arg_start = arg_end_p1+1;
  2333.           ch = *arg_start;
  2334.           while (ch == ' ' || ch == '\t')
  2335.             ch = *++arg_start;
  2336.  
  2337.           arg_was_number = 0;
  2338.           if (isdigit (ch) || ch == '-' || ch == '+')
  2339.             {
  2340.               int ch2;
  2341.               arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
  2342.               if (arg_end_p1 != arg_start || (ch2 = *arg_end_p1 != ';') || ch2 != ',')
  2343.             arg_was_number++;
  2344.  
  2345.               if (t_ptr == &temp_array[0])
  2346.             {
  2347.               error_line = __LINE__;
  2348.               saber_stop ();
  2349.               goto bomb_out;
  2350.             }
  2351.  
  2352.               *--t_ptr = arg_number;
  2353.             }
  2354.         }
  2355.  
  2356.           /* Reverse order of dimensions.  */
  2357.           while (t_ptr <= &temp_array[ N_TQ-1 ])
  2358.         {
  2359.           if (t.num_dims >= N_TQ-1)
  2360.             {
  2361.               error_line = __LINE__;
  2362.               saber_stop ();
  2363.               goto bomb_out;
  2364.             }
  2365.  
  2366.           t.dimensions[ t.num_dims++ ] = *t_ptr++;
  2367.         }
  2368.           break;
  2369.         }
  2370.       else
  2371.         {
  2372.           error_line = __LINE__;
  2373.           saber_stop ();
  2374.           goto bomb_out;
  2375.         }
  2376.  
  2377.  
  2378.     case 's':
  2379.       if (len == sizeof (".scl")-1
  2380.           && memcmp (dir_start, ".scl", sizeof (".scl")-1) == 0
  2381.           && arg_was_number
  2382.           && arg_number < ((symint_t) C_MAX))
  2383.         {
  2384.           /* If the symbol is a static or external, we have
  2385.          already gotten the appropriate type and class, so
  2386.          make sure we don't override those values.  This is
  2387.          needed because there are some type and classes that
  2388.          are not in COFF, such as short data, etc.  */
  2389.           if (symbol_type == st_Nil)
  2390.         {
  2391.           symbol_type   = map_coff_sym_type[arg_number];
  2392.           storage_class = map_coff_storage [arg_number];
  2393.         }
  2394.           break;
  2395.         }
  2396.  
  2397.       else if (len == sizeof (".size")-1
  2398.            && memcmp (dir_start, ".size", sizeof (".size")-1) == 0
  2399.            && arg_was_number)
  2400.         {
  2401.           symint_t *t_ptr = &temp_array[ N_TQ-1 ];
  2402.  
  2403.           *t_ptr = arg_number;
  2404.           while (*arg_end_p1 == ',' && arg_was_number)
  2405.         {
  2406.           arg_start = arg_end_p1+1;
  2407.           ch = *arg_start;
  2408.           while (ch == ' ' || ch == '\t')
  2409.             ch = *++arg_start;
  2410.  
  2411.           arg_was_number = 0;
  2412.           if (isdigit (ch) || ch == '-' || ch == '+')
  2413.             {
  2414.               int ch2;
  2415.               arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
  2416.               if (arg_end_p1 != arg_start || (ch2 = *arg_end_p1 != ';') || ch2 != ',')
  2417.             arg_was_number++;
  2418.  
  2419.               if (t_ptr == &temp_array[0])
  2420.             {
  2421.               error_line = __LINE__;
  2422.               saber_stop ();
  2423.               goto bomb_out;
  2424.             }
  2425.  
  2426.               *--t_ptr = arg_number;
  2427.             }
  2428.         }
  2429.  
  2430.           /* Reverse order of sizes.  */
  2431.           while (t_ptr <= &temp_array[ N_TQ-1 ])
  2432.         {
  2433.           if (t.num_sizes >= N_TQ-1)
  2434.             {
  2435.               error_line = __LINE__;
  2436.               saber_stop ();
  2437.               goto bomb_out;
  2438.             }
  2439.  
  2440.           t.sizes[ t.num_sizes++ ] = *t_ptr++;
  2441.         }
  2442.           break;
  2443.         }
  2444.  
  2445.       else
  2446.         {
  2447.           error_line = __LINE__;
  2448.           saber_stop ();
  2449.           goto bomb_out;
  2450.         }
  2451.  
  2452.  
  2453.     case 't':
  2454.       if (len == sizeof (".type")-1
  2455.           && memcmp (dir_start, ".type", sizeof (".type")-1) == 0
  2456.           && arg_was_number)
  2457.         {
  2458.           tq_t *tq_ptr = &t.type_qualifiers[0];
  2459.  
  2460.           t.orig_type = (coff_type_t) (arg_number & N_BTMASK);
  2461.           t.basic_type = map_coff_types [(int)t.orig_type];
  2462.           for (i = N_TQ-1; i >= 0; i--)
  2463.         {
  2464.           int dt = (arg_number >> ((i * N_TQ_SHIFT) + N_BT_SHIFT)
  2465.                 & N_TMASK);
  2466.  
  2467.           if (dt != (int)DT_NON)
  2468.             *tq_ptr++ = map_coff_derived_type [dt];
  2469.         }
  2470.  
  2471.           /* If this is a function, ignore it, so that we don't get
  2472.          two entries (one from the .ent, and one for the .def
  2473.          that preceedes it).  Save the type information so that
  2474.          the end block can properly add it after the begin block
  2475.          index.  For MIPS knows what reason, we must strip off
  2476.          the function type at this point.  */
  2477.           if (tq_ptr != &t.type_qualifiers[0] && tq_ptr[-1] == tq_Proc)
  2478.         {
  2479.           is_function = 1;
  2480.           tq_ptr[-1] = tq_Nil;
  2481.         }
  2482.  
  2483.           break;
  2484.         }
  2485.  
  2486.       else if (len == sizeof (".tag")-1
  2487.           && memcmp (dir_start, ".tag", sizeof (".tag")-1) == 0)
  2488.         {
  2489.           tag_start = arg_start;
  2490.           tag_end_p1 = arg_end_p1;
  2491.           break;
  2492.         }
  2493.  
  2494.       else
  2495.         {
  2496.           error_line = __LINE__;
  2497.           saber_stop ();
  2498.           goto bomb_out;
  2499.         }
  2500.  
  2501.  
  2502.     case 'v':
  2503.       if (len == sizeof (".val")-1
  2504.           && memcmp (dir_start, ".val", sizeof (".val")-1) == 0)
  2505.         {
  2506.           if (arg_was_number)
  2507.         value = arg_number;
  2508.  
  2509.           /* If the value is not an integer value, it must be the
  2510.          name of a static or global item.  Look up the name in
  2511.          the orignal symbol table to pick up the storage
  2512.          class, symbol type, etc.  */
  2513.           else
  2514.         {
  2515.           shash_t *orig_hash_ptr;     /* hash within orig sym table*/
  2516.           shash_t *ext_hash_ptr;    /* hash within ext. sym table*/
  2517.  
  2518.           ext_hash_ptr = hash_string (arg_start,
  2519.                           arg_end_p1 - arg_start,
  2520.                           &ext_str_hash[0],
  2521.                           (symint_t *)0);
  2522.  
  2523.           if (ext_hash_ptr != (shash_t *)0
  2524.               && ext_hash_ptr->esym_ptr != (EXTR *)0)
  2525.             eptr = ext_hash_ptr->esym_ptr;
  2526.  
  2527.           orig_hash_ptr = hash_string (arg_start,
  2528.                            arg_end_p1 - arg_start,
  2529.                            &orig_str_hash[0],
  2530.                            (symint_t *)0);
  2531.  
  2532.           if ((orig_hash_ptr == (shash_t *)0
  2533.                || orig_hash_ptr->sym_ptr == (SYMR *)0)
  2534.               && eptr == (EXTR *)0)
  2535.             error ("internal error, %.*s not found in original or external symbol tables",
  2536.                arg_end_p1 - arg_start,
  2537.                arg_start);
  2538.           else
  2539.             {
  2540.               SYMR *ptr = (orig_hash_ptr != (shash_t *)0
  2541.                    && orig_hash_ptr->sym_ptr != (SYMR *)0)
  2542.                     ? orig_hash_ptr->sym_ptr
  2543.                     : &eptr->asym;
  2544.  
  2545.               symbol_type = (st_t) ptr->st;
  2546.               storage_class = (sc_t) ptr->sc;
  2547.               value = ptr->value;
  2548.             }
  2549.         }
  2550.           break;
  2551.         }
  2552.       else
  2553.         {
  2554.           error_line = __LINE__;
  2555.           saber_stop ();
  2556.           goto bomb_out;
  2557.         }
  2558.     }
  2559.  
  2560.       /* Set up to find next directive.  */
  2561.       dir_start = arg_end_p1 + 1;
  2562.     }
  2563.  
  2564.  
  2565.   t.extra_sizes = (tag_start != (char *)0);
  2566.   if (t.num_dims > 0)
  2567.     {
  2568.       int diff = t.num_dims - t.num_sizes;
  2569.       int i = t.num_dims - 1;
  2570.       int j;
  2571.  
  2572.       if (t.num_sizes != 1 || diff < 0)
  2573.     {
  2574.       error_line = __LINE__;
  2575.       saber_stop ();
  2576.       goto bomb_out;
  2577.     }
  2578.  
  2579.       /* If this is an array, make sure the same number of dimensions
  2580.      and sizes were passed, creating extra sizes for multiply
  2581.      dimensioned arrays if not passed.  */
  2582.  
  2583.       t.extra_sizes = 0;
  2584.       if (diff)
  2585.     {
  2586.       for (j = (sizeof (t.sizes) / sizeof (t.sizes[0])) - 1; j >= 0; j--)
  2587.         t.sizes[ j ] = ((j-diff) >= 0) ? t.sizes[ j-diff ] : 0;
  2588.  
  2589.       t.num_sizes = i + 1;
  2590.       for ( i--; i >= 0; i-- )
  2591.         t.sizes[ i ] = t.sizes[ i+1 ] / t.dimensions[ i+1 ];
  2592.     }
  2593.     }
  2594.  
  2595.   else if (symbol_type == st_Member && t.num_sizes - t.extra_sizes == 1)
  2596.     { /* Is this a bitfield?  This is indicated by a structure memeber
  2597.      having a size field that isn't an array.  */
  2598.  
  2599.       t.bitfield = 1;
  2600.     }
  2601.  
  2602.  
  2603.   /* Except for enumeration members & begin/ending of scopes, put the
  2604.      type word in the aux. symbol table.  */
  2605.  
  2606.   if (symbol_type == st_Block || symbol_type == st_End)
  2607.     index = 0;
  2608.  
  2609.   else if (inside_enumeration)
  2610.     index = cur_file_ptr->void_type;
  2611.  
  2612.   else
  2613.     {
  2614.       if (t.basic_type == bt_Struct
  2615.       || t.basic_type == bt_Union
  2616.       || t.basic_type == bt_Enum)
  2617.     {
  2618.       if (tag_start == (char *)0)
  2619.         {
  2620.           error ("No tag specified for %.*s",
  2621.              name_end_p1 - name_start,
  2622.              name_start);
  2623.           return;
  2624.         }
  2625.  
  2626.       t.tag_ptr = get_tag (tag_start, tag_end_p1,  (symint_t)indexNil,
  2627.                    t.basic_type);
  2628.     }
  2629.  
  2630.       if (is_function)
  2631.     {
  2632.       last_func_type_info = t;
  2633.       last_func_eptr = eptr;
  2634.       return;
  2635.     }
  2636.  
  2637.       index = add_aux_sym_tir (&t,
  2638.                    hash_yes,
  2639.                    &cur_file_ptr->thash_head[0]);
  2640.     }
  2641.  
  2642.  
  2643.   /* If this is an external or static symbol, update the appropriate
  2644.      external symbol.  */
  2645.  
  2646.   if (eptr != (EXTR *)0
  2647.       && (eptr->asym.index == indexNil || cur_proc_ptr == (PDR *)0))
  2648.     {
  2649.       eptr->ifd = cur_file_ptr->file_index;
  2650.       eptr->asym.index = index;
  2651.     }
  2652.  
  2653.  
  2654.   /* Do any last minute adjustments that are necessary.  */
  2655.   switch (symbol_type)
  2656.     {
  2657.     default:
  2658.       break;
  2659.  
  2660.  
  2661.       /* For the beginning of structs, unions, and enumerations, the
  2662.      size info needs to be passed in the value field.  */
  2663.  
  2664.     case st_Block:
  2665.       if (t.num_sizes - t.num_dims - t.extra_sizes != 1)
  2666.     {
  2667.       error_line = __LINE__;
  2668.       saber_stop ();
  2669.       goto bomb_out;
  2670.     }
  2671.  
  2672.       else
  2673.     value = t.sizes[0];
  2674.  
  2675.       inside_enumeration = (t.orig_type == T_ENUM);
  2676.       break;
  2677.  
  2678.  
  2679.       /* For the end of structs, unions, and enumerations, omit the
  2680.      name which is always ".eos".  This needs to be done last, so
  2681.      that any error reporting above gives the correct name.  */
  2682.  
  2683.     case st_End:
  2684.       name_start = name_end_p1 = (const char *)0;
  2685.       value = inside_enumeration = 0;
  2686.       break;
  2687.  
  2688.  
  2689.       /* Members of structures and unions that aren't bitfields, need
  2690.      to adjust the value from a byte offset to a bit offset.
  2691.      Members of enumerations do not have the value adjusted, and
  2692.      can be distinquished by index == indexNil.  For enumerations,
  2693.      update the maximum enumeration value.  */
  2694.  
  2695.     case st_Member:
  2696.       if (!t.bitfield && !inside_enumeration)
  2697.     value *= 8;
  2698.  
  2699.       break;
  2700.     }
  2701.  
  2702.  
  2703.   /* Add the symbol, except for global symbols outside of functions,
  2704.      for which the external symbol table is fine enough.  */
  2705.  
  2706.   if (eptr == (EXTR *)0
  2707.       || eptr->asym.st == (int)st_Nil
  2708.       || cur_proc_ptr != (PDR *)0)
  2709.     {
  2710.       symint_t isym = add_local_symbol (name_start,
  2711.                     name_end_p1,
  2712.                     symbol_type,
  2713.                     storage_class,
  2714.                     value,
  2715.                     index);
  2716.  
  2717.       /* deal with struct, union, and enum tags.  */
  2718.       if (symbol_type == st_Block)
  2719.         {
  2720.       /* Create or update the tag information.  */
  2721.       tag_t *tag_ptr = get_tag (name_start,
  2722.                     name_end_p1,
  2723.                     isym,
  2724.                     t.basic_type);
  2725.  
  2726.       /* If there are any forward references, fill in the appropriate
  2727.          file and symbol indexes.  */
  2728.  
  2729.       symint_t file_index  = cur_file_ptr->file_index;
  2730.       forward_t *f_next = tag_ptr->forward_ref;
  2731.       forward_t *f_cur;
  2732.  
  2733.       while (f_next != (forward_t *)0)
  2734.         {
  2735.           f_cur  = f_next;
  2736.           f_next = f_next->next;
  2737.  
  2738.           f_cur->ifd_ptr->isym = file_index;
  2739.           f_cur->index_ptr->rndx.index = isym;
  2740.  
  2741.           free_word8 ((word8_t *) f_cur);
  2742.         }
  2743.  
  2744.       tag_ptr->forward_ref = (forward_t *)0;
  2745.         }
  2746.     }
  2747.  
  2748.   /* Normal return  */
  2749.   return;
  2750.  
  2751.   /* Error return, issue message.  */
  2752. bomb_out:
  2753.   if (error_line)
  2754.     error ("compiler error, badly formed #.def (internal line # = %d)", error_line);
  2755.   else
  2756.     error ("compiler error, badly formed #.def");
  2757.  
  2758.   return;
  2759. }
  2760.  
  2761.  
  2762. /* Parse .end directives.  */
  2763.  
  2764. STATIC void
  2765. parse_end (start)
  2766.      const char *start;            /* start of directive */
  2767. {
  2768.   register const char *start_func, *end_func_p1;
  2769.   register int ch;
  2770.   register symint_t value;
  2771.   register FDR *orig_fdr;
  2772.  
  2773.   if (cur_file_ptr == (efdr_t *)0)
  2774.     {
  2775.       error (".end directive without a preceeding .file directive");
  2776.       return;
  2777.     }
  2778.  
  2779.   if (cur_proc_ptr == (PDR *)0)
  2780.     {
  2781.       error (".end directive without a preceeding .ent directive");
  2782.       return;
  2783.     }
  2784.  
  2785.   /* Get the function name, skipping whitespace.  */
  2786.   for (start_func = start; isspace (*start_func); start_func++)
  2787.     ;
  2788.  
  2789.   ch = *start_func;
  2790.   if (!IS_ASM_IDENT (ch))
  2791.     {
  2792.       error (".end directive has no name");
  2793.       return;
  2794.     }
  2795.  
  2796.   for (end_func_p1 = start_func; IS_ASM_IDENT (ch); ch = *++end_func_p1)
  2797.     ;
  2798.  
  2799.  
  2800.   /* Get the value field for creating the end from the original object
  2801.      file (which we find by locating the procedure start, and using the
  2802.      pointer to the end+1 block and backing up.  The index points to a
  2803.      two word aux. symbol, whose first word is the index of the end
  2804.      symbol, and the second word is the type of the function return
  2805.      value.  */
  2806.  
  2807.   orig_fdr = cur_file_ptr->orig_fdr;
  2808.   value = 0;
  2809.   if (orig_fdr != (FDR *)0 && cur_oproc_ptr != (PDR *)0)
  2810.     {
  2811.       register SYMR *sym_ptr = ORIG_LSYMS (orig_fdr->isymBase + cur_oproc_ptr->isym);
  2812.  
  2813.       if ((st_t)sym_ptr->st == st_Proc
  2814.       || (st_t)sym_ptr->st == st_StaticProc)
  2815.     {
  2816.       AUXU *aptr = ORIG_AUX (orig_fdr->iauxBase + sym_ptr->index);
  2817.       symint_t end_index = aptr->isym;
  2818.       value = (ORIG_LSYMS (orig_fdr->isymBase + end_index - 1))->value;
  2819.     }
  2820.     }
  2821.  
  2822.   (void) add_local_symbol (start_func,
  2823.                end_func_p1,
  2824.                st_End,
  2825.                sc_Text,
  2826.                value,
  2827.                (symint_t)0);
  2828.  
  2829.   cur_proc_ptr = cur_oproc_ptr = (PDR *)0;
  2830. }
  2831.  
  2832.  
  2833. /* Parse .ent directives.  */
  2834.  
  2835. STATIC void
  2836. parse_ent (start)
  2837.      const char *start;            /* start of directive */
  2838. {
  2839.   register const char *start_func, *end_func_p1;
  2840.   register int ch;
  2841.  
  2842.   if (cur_file_ptr == (efdr_t *)0)
  2843.     {
  2844.       error (".ent directive without a preceeding .file directive");
  2845.       return;
  2846.     }
  2847.  
  2848.   if (cur_proc_ptr != (PDR *)0)
  2849.     {
  2850.       error ("second .ent directive found before .end directive");
  2851.       return;
  2852.     }
  2853.  
  2854.   for (start_func = start; isspace (*start_func); start_func++)
  2855.     ;
  2856.  
  2857.   ch = *start_func;
  2858.   if (!IS_ASM_IDENT (ch))
  2859.     {
  2860.       error (".ent directive has no name");
  2861.       return;
  2862.     }
  2863.  
  2864.   for (end_func_p1 = start_func; IS_ASM_IDENT (ch); ch = *++end_func_p1)
  2865.     ;
  2866.  
  2867.   (void) add_procedure (start_func, end_func_p1);
  2868. }
  2869.  
  2870.  
  2871. /* Parse .file directives.  */
  2872.  
  2873. STATIC void
  2874. parse_file (start)
  2875.      const char *start;            /* start of directive */
  2876. {
  2877.   char *p;
  2878.   register char *start_name, *end_name_p1;
  2879.  
  2880.   (void) strtol (start, &p, 0);
  2881.   if (start == p
  2882.       || (start_name = strchr (p, '"')) == (char *)0
  2883.       || (end_name_p1 = strrchr (++start_name, '"')) == (char *)0)
  2884.     {
  2885.       error ("Illegal .file directive");
  2886.       return;
  2887.     }
  2888.  
  2889.   if (cur_proc_ptr != (PDR *)0)
  2890.     {
  2891.       error ("No way to handle .file within .ent/.end section");
  2892.       return;
  2893.     }
  2894.  
  2895.   add_file (start_name, end_name_p1);
  2896. }
  2897.  
  2898.  
  2899. /* Parse the input file, and write the lines to the output file
  2900.    if needed.  */
  2901.  
  2902. STATIC void
  2903. parse_input __proto((void))
  2904. {
  2905.   register char *p;
  2906.   register int i;
  2907.   register thead_t *ptag_head;
  2908.   register tag_t *ptag;
  2909.   register tag_t *ptag_next;
  2910.  
  2911.   if (debug)
  2912.     fprintf (stderr, "\tinput\n");
  2913.  
  2914.   /* Add a dummy scope block around the entire compilation unit for
  2915.      structures defined outside of blocks.  */
  2916.   ptag_head = (thead_t *) allocate_word8 ();
  2917.   ptag_head->first_tag = 0;
  2918.   ptag_head->prev = cur_tag_head;
  2919.   cur_tag_head = ptag_head;
  2920.  
  2921.   while ((p = read_line ()) != (char *)0)
  2922.     {
  2923.       /* Skip leading blanks */
  2924.       while (isspace (*p))
  2925.     p++;
  2926.  
  2927.       /* See if it's a directive we handle.  If so, dispatch handler.  */
  2928.       for (i = 0; i < sizeof (pseudo_ops) / sizeof (pseudo_ops[0]); i++)
  2929.     if (memcmp (p, pseudo_ops[i].name, pseudo_ops[i].len) == 0
  2930.         && isspace (p[pseudo_ops[i].len]))
  2931.       {
  2932.         p += pseudo_ops[i].len;    /* skip to first argument */
  2933.         while (isspace (*p))
  2934.           p++;
  2935.  
  2936.         (*pseudo_ops[i].func)( p );
  2937.         break;
  2938.       }
  2939.     }
  2940.  
  2941.   /* Process any tags at global level.  */
  2942.   ptag_head = cur_tag_head;
  2943.   cur_tag_head = ptag_head->prev;
  2944.  
  2945.   for (ptag = ptag_head->first_tag;
  2946.        ptag != (tag_t *)0;
  2947.        ptag = ptag_next)
  2948.     {
  2949.       if (ptag->forward_ref != (forward_t *)0)
  2950.     add_unknown_tag (ptag);
  2951.  
  2952.       ptag_next = ptag->same_block;
  2953.       ptag->hash_ptr->tag_ptr = ptag->same_name;
  2954.       free_word8 ((word8_t *) ptag);
  2955.     }
  2956.  
  2957.   free_word8 ((word8_t *) ptag_head);
  2958.  
  2959. }
  2960.  
  2961.  
  2962. /* Update the global headers with the final offsets in preparation
  2963.    to write out the .T file.  */
  2964.  
  2965. STATIC void
  2966. update_headers __proto((void))
  2967. {
  2968.   register symint_t i;
  2969.   register efdr_t *file_ptr;
  2970.  
  2971.   /* Set up the symbolic header.  */
  2972.   file_offset = sizeof (symbolic_header) + orig_file_header.f_symptr;
  2973.   symbolic_header.magic = orig_sym_hdr.magic;
  2974.   symbolic_header.vstamp = orig_sym_hdr.vstamp;
  2975.  
  2976.   /* Set up global counts.  */
  2977.   symbolic_header.issExtMax = ext_strings.num_allocated;
  2978.   symbolic_header.idnMax    = dense_num.num_allocated;
  2979.   symbolic_header.ifdMax    = file_desc.num_allocated;
  2980.   symbolic_header.iextMax   = ext_symbols.num_allocated;
  2981.   symbolic_header.ilineMax  = orig_sym_hdr.ilineMax;
  2982.   symbolic_header.ioptMax   = orig_sym_hdr.ioptMax;
  2983.   symbolic_header.cbLine    = orig_sym_hdr.cbLine;
  2984.   symbolic_header.crfd      = orig_sym_hdr.crfd;
  2985.  
  2986.  
  2987.   /* Loop through each file, figuring out how many local syms,
  2988.      line numbers, etc. there are.  Also, put out end symbol
  2989.      for the filename.  */
  2990.  
  2991.   for (file_ptr = first_file;
  2992.        file_ptr != (efdr_t *)0;
  2993.        file_ptr = file_ptr->next_file)
  2994.     {
  2995.       cur_file_ptr = file_ptr;
  2996.       (void) add_local_symbol ((const char *)0,
  2997.                    (const char *)0,
  2998.                    st_End,
  2999.                    sc_Text,
  3000.                    (symint_t)0,
  3001.                    (symint_t)0);
  3002.  
  3003.       file_ptr->fdr.cpd = file_ptr->procs.num_allocated;
  3004.       file_ptr->fdr.ipdFirst = symbolic_header.ipdMax;
  3005.       symbolic_header.ipdMax += file_ptr->fdr.cpd;
  3006.  
  3007.       file_ptr->fdr.csym = file_ptr->symbols.num_allocated;
  3008.       file_ptr->fdr.isymBase = symbolic_header.isymMax;
  3009.       symbolic_header.isymMax += file_ptr->fdr.csym;
  3010.  
  3011.       file_ptr->fdr.caux = file_ptr->aux_syms.num_allocated;
  3012.       file_ptr->fdr.iauxBase = symbolic_header.iauxMax;
  3013.       symbolic_header.iauxMax += file_ptr->fdr.caux;
  3014.  
  3015.       file_ptr->fdr.cbSs = file_ptr->strings.num_allocated;
  3016.       file_ptr->fdr.issBase = symbolic_header.issMax;
  3017.       symbolic_header.issMax += file_ptr->fdr.cbSs;
  3018.     }
  3019.  
  3020.  
  3021.   i = WORD_ALIGN (symbolic_header.cbLine);    /* line numbers */
  3022.   if (i > 0)
  3023.     {
  3024.       symbolic_header.cbLineOffset = file_offset;
  3025.       file_offset += i;
  3026.     }
  3027.  
  3028.   i = symbolic_header.ioptMax;            /* optimization symbols */
  3029.   if (((long) i) > 0)
  3030.     {
  3031.       symbolic_header.cbOptOffset = file_offset;
  3032.       file_offset += i * sizeof (OPTR);
  3033.     }
  3034.  
  3035.   i = symbolic_header.idnMax;            /* dense numbers */
  3036.   if (i > 0)
  3037.     {
  3038.       symbolic_header.cbDnOffset = file_offset;
  3039.       file_offset += i * sizeof (DNR);
  3040.     }
  3041.  
  3042.   i = symbolic_header.ipdMax;            /* procedure tables */
  3043.   if (i > 0)
  3044.     {
  3045.       symbolic_header.cbPdOffset = file_offset;
  3046.       file_offset += i * sizeof (PDR);
  3047.     }
  3048.  
  3049.   i = symbolic_header.isymMax;            /* local symbols */
  3050.   if (i > 0)
  3051.     {
  3052.       symbolic_header.cbSymOffset = file_offset;
  3053.       file_offset += i * sizeof (SYMR);
  3054.     }
  3055.  
  3056.   i = symbolic_header.iauxMax;            /* aux syms. */
  3057.   if (i > 0)
  3058.     {
  3059.       symbolic_header.cbAuxOffset = file_offset;
  3060.       file_offset += i * sizeof (TIR);
  3061.     }
  3062.  
  3063.   i = WORD_ALIGN (symbolic_header.issMax);    /* local strings */
  3064.   if (i > 0)
  3065.     {
  3066.       symbolic_header.cbSsOffset = file_offset;
  3067.       file_offset += i;
  3068.     }
  3069.  
  3070.   i = WORD_ALIGN (symbolic_header.issExtMax);    /* external strings */
  3071.   if (i > 0)
  3072.     {
  3073.       symbolic_header.cbSsExtOffset = file_offset;
  3074.       file_offset += i;
  3075.     }
  3076.  
  3077.   i = symbolic_header.ifdMax;            /* file tables */
  3078.   if (i > 0)
  3079.     {
  3080.       symbolic_header.cbFdOffset = file_offset;
  3081.       file_offset += i * sizeof (FDR);
  3082.     }
  3083.  
  3084.   i = symbolic_header.crfd;            /* relative file descriptors */
  3085.   if (i > 0)
  3086.     {
  3087.       symbolic_header.cbRfdOffset = file_offset;
  3088.       file_offset += i * sizeof (symint_t);
  3089.     }
  3090.  
  3091.   i = symbolic_header.iextMax;            /* external symbols */
  3092.   if (i > 0)
  3093.     {
  3094.       symbolic_header.cbExtOffset = file_offset;
  3095.       file_offset += i * sizeof (EXTR);
  3096.     }
  3097. }
  3098.  
  3099.  
  3100. /* Write out a varray at a given location.  */
  3101.  
  3102. STATIC void
  3103. write_varray (vp, offset, str)
  3104.      varray_t *vp;            /* virtual array */
  3105.      off_t offset;            /* offset to write varray to */
  3106.      const char *str;            /* string to print out when tracing */
  3107. {
  3108.   int num_write, sys_write;
  3109.   vlinks_t *ptr;
  3110.  
  3111.   if (vp->num_allocated == 0)
  3112.     return;
  3113.  
  3114.   if (debug)
  3115.     fprintf (stderr, "\twarray\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3116.          vp, offset, vp->num_allocated * vp->object_size, str);
  3117.  
  3118.   if (file_offset != offset
  3119.       && fseek (object_stream, (long)offset, SEEK_SET) < 0)
  3120.     pfatal_with_name (object_name);
  3121.  
  3122.   for (ptr = vp->first; ptr != (vlinks_t *)0; ptr = ptr->next)
  3123.     {
  3124.       num_write = (ptr->next == (vlinks_t *)0)
  3125.     ? vp->objects_last_page * vp->object_size
  3126.     : vp->objects_per_page  * vp->object_size;
  3127.  
  3128.       sys_write = fwrite ((PTR_T) ptr->datum, 1, num_write, object_stream);
  3129.       if (sys_write <= 0)
  3130.     pfatal_with_name (object_name);
  3131.  
  3132.       else if (sys_write != num_write)
  3133.     fatal ("Wrote %d bytes to %s, system returned %d",
  3134.            num_write,
  3135.            object_name,
  3136.            sys_write);
  3137.  
  3138.       file_offset += num_write;
  3139.     }
  3140. }
  3141.  
  3142.  
  3143. /* Write out the symbol table in the object file.  */
  3144.  
  3145. STATIC void
  3146. write_object __proto((void))
  3147. {
  3148.   int sys_write;
  3149.   efdr_t *file_ptr;
  3150.   off_t offset;
  3151.  
  3152.   if (debug)
  3153.     fprintf (stderr, "\n\twrite\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3154.          (PTR_T *) &symbolic_header, 0, sizeof (symbolic_header),
  3155.          "symbolic header");
  3156.  
  3157.   sys_write = fwrite ((PTR_T) &symbolic_header,
  3158.               1,
  3159.               sizeof (symbolic_header),
  3160.               object_stream);
  3161.  
  3162.   if (sys_write < 0)
  3163.     pfatal_with_name (object_name);
  3164.  
  3165.   else if (sys_write != sizeof (symbolic_header))
  3166.     fatal ("Wrote %d bytes to %s, system returned %d",
  3167.        sizeof (symbolic_header),
  3168.        object_name,
  3169.        sys_write);
  3170.  
  3171.  
  3172.   file_offset = sizeof (symbolic_header) + orig_file_header.f_symptr;
  3173.  
  3174.   if (symbolic_header.cbLine > 0)        /* line numbers */
  3175.     {
  3176.       long sys_write;
  3177.  
  3178.       if (file_offset != symbolic_header.cbLineOffset
  3179.       && fseek (object_stream, symbolic_header.cbLineOffset, SEEK_SET) != 0)
  3180.     pfatal_with_name (object_name);
  3181.  
  3182.       if (debug)
  3183.     fprintf (stderr, "\twrite\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3184.          (PTR_T *) &orig_linenum, symbolic_header.cbLineOffset,
  3185.          symbolic_header.cbLine, "Line numbers");
  3186.  
  3187.       sys_write = fwrite ((PTR_T) orig_linenum,
  3188.               1,
  3189.               symbolic_header.cbLine,
  3190.               object_stream);
  3191.  
  3192.       if (sys_write <= 0)
  3193.     pfatal_with_name (object_name);
  3194.  
  3195.       else if (sys_write != symbolic_header.cbLine)
  3196.     fatal ("Wrote %d bytes to %s, system returned %d",
  3197.            symbolic_header.cbLine,
  3198.            object_name,
  3199.            sys_write);
  3200.  
  3201.       file_offset = symbolic_header.cbLineOffset + symbolic_header.cbLine;
  3202.     }
  3203.  
  3204.   if (symbolic_header.ioptMax > 0)        /* optimization symbols */
  3205.     {
  3206.       long sys_write;
  3207.       long num_write = symbolic_header.ioptMax * sizeof (OPTR);
  3208.  
  3209.       if (file_offset != symbolic_header.cbOptOffset
  3210.       && fseek (object_stream, symbolic_header.cbOptOffset, SEEK_SET) != 0)
  3211.     pfatal_with_name (object_name);
  3212.  
  3213.       if (debug)
  3214.     fprintf (stderr, "\twrite\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3215.          (PTR_T *) &orig_opt_syms, symbolic_header.cbOptOffset,
  3216.          num_write, "Optimizer symbols");
  3217.  
  3218.       sys_write = fwrite ((PTR_T) orig_opt_syms,
  3219.               1,
  3220.               num_write,
  3221.               object_stream);
  3222.  
  3223.       if (sys_write <= 0)
  3224.     pfatal_with_name (object_name);
  3225.  
  3226.       else if (sys_write != num_write)
  3227.     fatal ("Wrote %d bytes to %s, system returned %d",
  3228.            num_write,
  3229.            object_name,
  3230.            sys_write);
  3231.  
  3232.       file_offset = symbolic_header.cbOptOffset + num_write;
  3233.     }
  3234.  
  3235.   if (symbolic_header.idnMax > 0)        /* dense numbers */
  3236.     write_varray (&dense_num, (off_t)symbolic_header.cbDnOffset, "Dense numbers");
  3237.  
  3238.   if (symbolic_header.ipdMax > 0)        /* procedure tables */
  3239.     {
  3240.       offset = symbolic_header.cbPdOffset;
  3241.       for (file_ptr = first_file;
  3242.        file_ptr != (efdr_t *)0;
  3243.        file_ptr = file_ptr->next_file)
  3244.     {
  3245.       write_varray (&file_ptr->procs, offset, "Procedure tables");
  3246.       offset = file_offset;
  3247.     }
  3248.     }
  3249.  
  3250.   if (symbolic_header.isymMax > 0)        /* local symbols */
  3251.     {
  3252.       offset = symbolic_header.cbSymOffset;
  3253.       for (file_ptr = first_file;
  3254.        file_ptr != (efdr_t *)0;
  3255.        file_ptr = file_ptr->next_file)
  3256.     {
  3257.       write_varray (&file_ptr->symbols, offset, "Local symbols");
  3258.       offset = file_offset;
  3259.     }
  3260.     }
  3261.  
  3262.   if (symbolic_header.iauxMax > 0)        /* aux symbols */
  3263.     {
  3264.       offset = symbolic_header.cbAuxOffset;
  3265.       for (file_ptr = first_file;
  3266.        file_ptr != (efdr_t *)0;
  3267.        file_ptr = file_ptr->next_file)
  3268.     {
  3269.       write_varray (&file_ptr->aux_syms, offset, "Aux. symbols");
  3270.       offset = file_offset;
  3271.     }
  3272.     }
  3273.  
  3274.   if (symbolic_header.issMax > 0)        /* local strings */
  3275.     {
  3276.       offset = symbolic_header.cbSsOffset;
  3277.       for (file_ptr = first_file;
  3278.        file_ptr != (efdr_t *)0;
  3279.        file_ptr = file_ptr->next_file)
  3280.     {
  3281.       write_varray (&file_ptr->strings, offset, "Local strings");
  3282.       offset = file_offset;
  3283.     }
  3284.     }
  3285.  
  3286.   if (symbolic_header.issExtMax > 0)        /* external strings */
  3287.     write_varray (&ext_strings, symbolic_header.cbSsExtOffset, "External strings");
  3288.  
  3289.   if (symbolic_header.ifdMax > 0)        /* file tables */
  3290.     {
  3291.       offset = symbolic_header.cbFdOffset;
  3292.       if (file_offset != offset
  3293.       && fseek (object_stream, (long)offset, SEEK_SET) < 0)
  3294.     pfatal_with_name (object_name);
  3295.  
  3296.       file_offset = offset;
  3297.       for (file_ptr = first_file;
  3298.        file_ptr != (efdr_t *)0;
  3299.        file_ptr = file_ptr->next_file)
  3300.     {
  3301.       if (debug)
  3302.         fprintf (stderr, "\twrite\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3303.              (PTR_T *) &file_ptr->fdr, file_offset, sizeof (FDR), "File header");
  3304.  
  3305.       sys_write = fwrite (&file_ptr->fdr,
  3306.                   1,
  3307.                   sizeof (FDR),
  3308.                   object_stream);
  3309.  
  3310.       if (sys_write < 0)
  3311.         pfatal_with_name (object_name);
  3312.  
  3313.       else if (sys_write != sizeof (FDR))
  3314.         fatal ("Wrote %d bytes to %s, system returned %d",
  3315.            sizeof (FDR),
  3316.            object_name,
  3317.            sys_write);
  3318.  
  3319.       file_offset = offset += sizeof (FDR);
  3320.     }
  3321.     }
  3322.  
  3323.   if (symbolic_header.crfd > 0)            /* relative file descriptors */
  3324.     {
  3325.       long sys_write;
  3326.       symint_t num_write = symbolic_header.crfd * sizeof (symint_t);
  3327.  
  3328.       if (file_offset != symbolic_header.cbRfdOffset
  3329.       && fseek (object_stream, symbolic_header.cbRfdOffset, SEEK_SET) != 0)
  3330.     pfatal_with_name (object_name);
  3331.  
  3332.       if (debug)
  3333.     fprintf (stderr, "\twrite\tvp = 0x%.8x, offset = %7u, size = %7u, %s\n",
  3334.          (PTR_T *) &orig_rfds, symbolic_header.cbRfdOffset,
  3335.          num_write, "Relative file descriptors");
  3336.  
  3337.       sys_write = fwrite (orig_rfds,
  3338.               1,
  3339.               num_write,
  3340.               object_stream);
  3341.  
  3342.       if (sys_write <= 0)
  3343.     pfatal_with_name (object_name);
  3344.  
  3345.       else if (sys_write != num_write)
  3346.     fatal ("Wrote %d bytes to %s, system returned %d",
  3347.            num_write,
  3348.            object_name,
  3349.            sys_write);
  3350.  
  3351.       file_offset = symbolic_header.cbRfdOffset + num_write;
  3352.     }
  3353.  
  3354.   if (symbolic_header.issExtMax > 0)        /* external symbols */
  3355.     write_varray (&ext_symbols, (off_t)symbolic_header.cbExtOffset, "External symbols");
  3356.  
  3357.   if (fclose (object_stream) != 0)
  3358.     pfatal_with_name (object_name);
  3359. }
  3360.  
  3361.  
  3362. /* Read some bytes at a specified location, and return a pointer.  */
  3363.  
  3364. STATIC page_t *
  3365. read_seek (size, offset, str)
  3366.      Size_t size;        /* # bytes to read */
  3367.      off_t offset;        /* offset to read at */
  3368.      const char *str;        /* name for tracing */
  3369. {
  3370.   page_t *ptr;
  3371.   long sys_read = 0;
  3372.  
  3373.   if (size == 0)        /* nothing to read */
  3374.     return (page_t *)0;
  3375.  
  3376.   if (debug)
  3377.     fprintf (stderr, "\trseek\tsize = %7u, offset = %7u, currently at %7u, %s\n",
  3378.          size, offset, file_offset, str);
  3379.  
  3380. #ifndef MALLOC_CHECK
  3381.   ptr = allocate_multiple_pages ((size + PAGE_USIZE - 1) / PAGE_USIZE);
  3382. #else
  3383.   ptr = (page_t *) xcalloc (1, size);
  3384. #endif
  3385.  
  3386.   /* If we need to seek, and the distance is nearby, just do some reads,
  3387.      to speed things up.  */
  3388.   if (file_offset != offset)
  3389.     {
  3390.       symint_t difference = offset - file_offset;
  3391.  
  3392.       if (difference < 8)
  3393.     {
  3394.       char small_buffer[8];
  3395.  
  3396.       sys_read = fread (small_buffer, 1, difference, obj_in_stream);
  3397.       if (sys_read <= 0)
  3398.         pfatal_with_name (obj_in_name);
  3399.  
  3400.       if (sys_read != difference)
  3401.         fatal ("Wanted to read %d bytes from %s, system returned %d",
  3402.            size,
  3403.            sys_read,
  3404.            obj_in_name);
  3405.     }
  3406.       else if (fseek (obj_in_stream, offset, SEEK_SET) < 0)
  3407.     pfatal_with_name (obj_in_name);
  3408.     }
  3409.  
  3410.   sys_read = fread ((PTR_T)ptr, 1, size, obj_in_stream);
  3411.   if (sys_read <= 0)
  3412.     pfatal_with_name (obj_in_name);
  3413.  
  3414.   if (sys_read != size)
  3415.     fatal ("Wanted to read %d bytes from %s, system returned %d",
  3416.        size,
  3417.        sys_read,
  3418.        obj_in_name);
  3419.  
  3420.   file_offset = offset + size;
  3421.  
  3422.   if (file_offset > max_file_offset)
  3423.     max_file_offset = file_offset;
  3424.  
  3425.   return ptr;
  3426. }
  3427.  
  3428.  
  3429. /* Read the existing object file (and copy to the output object file
  3430.    if it is different from the input object file), and remove the old
  3431.    symbol table.  */
  3432.  
  3433. STATIC void
  3434. copy_object __proto((void))
  3435. {
  3436.   char buffer[ PAGE_SIZE ];
  3437.   register int sys_read;
  3438.   register int remaining;
  3439.   register int num_write;
  3440.   register int sys_write;
  3441.   register int fd, es;
  3442.   register int delete_ifd = 0;
  3443.   struct stat stat_buf;
  3444.  
  3445.   if (debug)
  3446.     fprintf (stderr, "\tcopy\n");
  3447.  
  3448.   if (fstat (fileno (obj_in_stream), &stat_buf) != 0
  3449.       || fseek (obj_in_stream, 0L, SEEK_SET) != 0)
  3450.     pfatal_with_name (obj_in_name);
  3451.  
  3452.   sys_read = fread ((PTR_T) &orig_file_header,
  3453.             1,
  3454.             sizeof (struct filehdr),
  3455.             obj_in_stream);
  3456.  
  3457.   if (sys_read < 0)
  3458.     pfatal_with_name (obj_in_name);
  3459.  
  3460.   else if (sys_read == 0 && feof (obj_in_stream))
  3461.     return;            /* create a .T file sans file header */
  3462.  
  3463.   else if (sys_read < sizeof (struct filehdr))
  3464.     fatal ("Wanted to read %d bytes from %s, system returned %d",
  3465.        sizeof (struct filehdr),
  3466.        obj_in_name,
  3467.        sys_read);
  3468.  
  3469.  
  3470.   if (orig_file_header.f_flags != 0)
  3471.     fatal ("Non-zero flags encountered in %s filehdr", input_name);
  3472.  
  3473.   if (orig_file_header.f_nsyms != sizeof (HDRR))
  3474.     fatal ("%s symbolic header wrong size (%d bytes, should be %d)",
  3475.        input_name, orig_file_header.f_nsyms, sizeof (HDRR));
  3476.  
  3477.  
  3478.   /* Read in the current symbolic header.  */
  3479.   if (fseek (obj_in_stream, (long) orig_file_header.f_symptr, SEEK_SET) != 0)
  3480.     pfatal_with_name (input_name);
  3481.  
  3482.   sys_read = fread ((PTR_T) &orig_sym_hdr,
  3483.             1,
  3484.             sizeof (orig_sym_hdr),
  3485.             obj_in_stream);
  3486.  
  3487.   if (sys_read < 0)
  3488.     pfatal_with_name (object_name);
  3489.  
  3490.   else if (sys_read < sizeof (struct filehdr))
  3491.     fatal ("Wanted to read %d bytes from %s, system returned %d",
  3492.        sizeof (struct filehdr),
  3493.        obj_in_name,
  3494.        sys_read);
  3495.  
  3496.  
  3497.   /* Read in each of the sections if they exist in the object file.
  3498.      We read things in in the order the mips assembler creates the
  3499.      sections, so in theory no extra seeks are done.
  3500.  
  3501.      For simplicity sake, round each read up to a page boundary,
  3502.      we may want to revisit this later.... */
  3503.  
  3504.   file_offset =  orig_file_header.f_symptr + sizeof (struct filehdr);
  3505.  
  3506.   if (orig_sym_hdr.cbLine > 0)            /* line numbers */
  3507.     orig_linenum = (char *) read_seek ((Size_t)orig_sym_hdr.cbLine,
  3508.                        orig_sym_hdr.cbLineOffset,
  3509.                        "Line numbers");
  3510.  
  3511.   if (orig_sym_hdr.ipdMax > 0)            /* procedure tables */
  3512.     orig_procs = (PDR *) read_seek ((Size_t)orig_sym_hdr.ipdMax * sizeof (PDR),
  3513.                     orig_sym_hdr.cbPdOffset,
  3514.                     "Procedure tables");
  3515.  
  3516.   if (orig_sym_hdr.isymMax > 0)            /* local symbols */
  3517.     orig_local_syms = (SYMR *) read_seek ((Size_t)orig_sym_hdr.isymMax * sizeof (SYMR),
  3518.                       orig_sym_hdr.cbSymOffset,
  3519.                       "Local symbols");
  3520.  
  3521.   if (orig_sym_hdr.iauxMax > 0)            /* aux symbols */
  3522.     orig_aux_syms = (AUXU *) read_seek ((Size_t)orig_sym_hdr.iauxMax * sizeof (AUXU),
  3523.                     orig_sym_hdr.cbAuxOffset,
  3524.                     "Aux. symbols");
  3525.  
  3526.   if (orig_sym_hdr.issMax > 0)            /* local strings */
  3527.     orig_local_strs = (char *) read_seek ((Size_t)orig_sym_hdr.issMax,
  3528.                       orig_sym_hdr.cbSsOffset,
  3529.                       "Local strings");
  3530.  
  3531.   if (orig_sym_hdr.issExtMax > 0)        /* external strings */
  3532.     orig_ext_strs = (char *) read_seek ((Size_t)orig_sym_hdr.issExtMax,
  3533.                     orig_sym_hdr.cbSsExtOffset,
  3534.                     "External strings");
  3535.  
  3536.   if (orig_sym_hdr.ifdMax > 0)            /* file tables */
  3537.     orig_files = (FDR *) read_seek ((Size_t)orig_sym_hdr.ifdMax * sizeof (FDR),
  3538.                     orig_sym_hdr.cbFdOffset,
  3539.                     "File tables");
  3540.  
  3541.   if (orig_sym_hdr.crfd > 0)            /* relative file descriptors */
  3542.     orig_rfds = (symint_t *) read_seek ((Size_t)orig_sym_hdr.crfd * sizeof (symint_t),
  3543.                     orig_sym_hdr.cbRfdOffset,
  3544.                     "Relative file descriptors");
  3545.  
  3546.   if (orig_sym_hdr.issExtMax > 0)        /* external symbols */
  3547.     orig_ext_syms = (EXTR *) read_seek ((Size_t)orig_sym_hdr.iextMax * sizeof (EXTR),
  3548.                     orig_sym_hdr.cbExtOffset,
  3549.                     "External symbols");
  3550.  
  3551.   if (orig_sym_hdr.idnMax > 0)            /* dense numbers */
  3552.     {
  3553.       orig_dense = (DNR *) read_seek ((Size_t)orig_sym_hdr.idnMax * sizeof (DNR),
  3554.                       orig_sym_hdr.cbDnOffset,
  3555.                       "Dense numbers");
  3556.  
  3557.       add_bytes (&dense_num, (char *) orig_dense, (Size_t)orig_sym_hdr.idnMax);
  3558.     }
  3559.  
  3560.   if (orig_sym_hdr.ioptMax > 0)            /* opt symbols */
  3561.     orig_opt_syms = (OPTR *) read_seek ((Size_t)orig_sym_hdr.ioptMax * sizeof (OPTR),
  3562.                     orig_sym_hdr.cbOptOffset,
  3563.                     "Optimizer symbols");
  3564.  
  3565.  
  3566.  
  3567.   /* Abort if the symbol table is not last.  */
  3568.   if (max_file_offset != stat_buf.st_size)
  3569.     fatal ("Symbol table is not last (symbol table ends at %ld, .o ends at %ld",
  3570.        max_file_offset,
  3571.        stat_buf.st_size);
  3572.  
  3573.  
  3574.   /* If the first original file descriptor is a dummy which the assembler
  3575.      put out, but there are no symbols in it, skip it now.  */
  3576.   if (orig_sym_hdr.ifdMax > 1
  3577.       && orig_files->csym == 2
  3578.       && orig_files->caux == 0)
  3579.     {
  3580.       char *filename = orig_local_strs + (orig_files->issBase + orig_files->rss);
  3581.       char *suffix = strrchr (filename, '.');
  3582.  
  3583.       if (suffix != (char *)0 && strcmp (suffix, ".s") == 0)
  3584.     delete_ifd = 1;
  3585.     }
  3586.  
  3587.  
  3588.   /* Loop, adding each of the external symbols.  These must be in
  3589.      order or otherwise we would have to change the relocation
  3590.      entries.  We don't just call add_bytes, because we need to have
  3591.      the names put into the external hash table.  We set the type to
  3592.      'void' for now, and parse_def will fill in the correct type if it
  3593.      is in the symbol table.  */
  3594.  
  3595.   if (debug)
  3596.     fprintf (stderr, "\tehash\n");
  3597.  
  3598.   for (es = 0; es < orig_sym_hdr.iextMax; es++)
  3599.     {
  3600.       register EXTR *eptr = orig_ext_syms + es;
  3601.       register char *ename = ORIG_ESTRS (eptr->asym.iss);
  3602.  
  3603.       (void) add_ext_symbol (ename,
  3604.                  ename + strlen (ename),
  3605.                  (st_t) eptr->asym.st,
  3606.                  (sc_t) eptr->asym.sc,
  3607.                  eptr->asym.value,
  3608.                  (symint_t)((eptr->asym.index == indexNil) ? indexNil : 0),
  3609.                  eptr->ifd - delete_ifd);
  3610.     }
  3611.  
  3612.  
  3613.   /* For each of the files in the object file, copy the symbols, and such
  3614.      into the varrays for the new object file.  */
  3615.  
  3616.   for (fd = delete_ifd; fd < orig_sym_hdr.ifdMax; fd++)
  3617.     {
  3618.       register FDR *fd_ptr = ORIG_FILES (fd);
  3619.       register char *filename = ORIG_LSTRS (fd_ptr->issBase + fd_ptr->rss);
  3620.       register SYMR *sym_start;
  3621.       register SYMR *sym;
  3622.       register SYMR *sym_end_p1;
  3623.       register PDR *proc_start;
  3624.       register PDR *proc;
  3625.       register PDR *proc_end_p1;
  3626.  
  3627.       /* file support itself.  */
  3628.       add_file (filename, filename + strlen (filename));
  3629.       cur_file_ptr->orig_fdr = fd_ptr;
  3630.  
  3631.       /* Copy stuff that's just passed through (such as line #'s) */
  3632.       cur_file_ptr->fdr.adr         = fd_ptr->adr;
  3633.       cur_file_ptr->fdr.ilineBase    = fd_ptr->ilineBase;
  3634.       cur_file_ptr->fdr.cline         = fd_ptr->cline;
  3635.       cur_file_ptr->fdr.rfdBase         = fd_ptr->rfdBase;
  3636.       cur_file_ptr->fdr.crfd         = fd_ptr->crfd;
  3637.       cur_file_ptr->fdr.cbLineOffset = fd_ptr->cbLineOffset;
  3638.       cur_file_ptr->fdr.cbLine         = fd_ptr->cbLine;
  3639.       cur_file_ptr->fdr.fMerge         = fd_ptr->fMerge;
  3640.       cur_file_ptr->fdr.fReadin         = fd_ptr->fReadin;
  3641.       cur_file_ptr->fdr.glevel         = fd_ptr->glevel;
  3642.  
  3643.       if (debug)
  3644.     fprintf (stderr, "\thash\tstart, filename %s\n", filename);
  3645.  
  3646.       /* For each of the static and global symbols defined, add them
  3647.      to the hash table of original symbols, so we can look up
  3648.      their values.  */
  3649.  
  3650.       sym_start = ORIG_LSYMS (fd_ptr->isymBase);
  3651.       sym_end_p1 = sym_start + fd_ptr->csym;
  3652.       for (sym = sym_start; sym < sym_end_p1; sym++)
  3653.     {
  3654.       switch ((st_t) sym->st)
  3655.         {
  3656.         default:
  3657.           break;
  3658.  
  3659.         case st_Global:
  3660.         case st_Static:
  3661.         case st_Label:
  3662.         case st_Proc:
  3663.         case st_StaticProc:
  3664.           {
  3665.         auto symint_t hash_index;
  3666.         register char *str = ORIG_LSTRS (fd_ptr->issBase + sym->iss);
  3667.         register Size_t len = strlen (str);
  3668.         register shash_t *shash_ptr = hash_string (str,
  3669.                                (Ptrdiff_t)len,
  3670.                                &orig_str_hash[0],
  3671.                                &hash_index);
  3672.  
  3673.         if (shash_ptr != (shash_t *)0)
  3674.           error ("internal error, %s is already in original symbol table", str);
  3675.  
  3676.         else
  3677.           {
  3678.             shash_ptr = (shash_t *) allocate_word8 ();
  3679.             shash_ptr->next = orig_str_hash[hash_index];
  3680.             orig_str_hash[hash_index] = shash_ptr;
  3681.  
  3682.             shash_ptr->len = len;
  3683.             shash_ptr->index = indexNil;
  3684.             shash_ptr->string = str;
  3685.             shash_ptr->sym_ptr = sym;
  3686.           }
  3687.           }
  3688.         }
  3689.     }
  3690.  
  3691.       if (debug)
  3692.     {
  3693.       fprintf (stderr, "\thash\tdone,  filename %s\n", filename);
  3694.       fprintf (stderr, "\tproc\tstart, filename %s\n", filename);
  3695.     }
  3696.  
  3697.       /* Go through each of the procedures in this file, and add the
  3698.      procedure pointer to the hash entry for the given name.  */
  3699.  
  3700.       proc_start = ORIG_PROCS (fd_ptr->ipdFirst);
  3701.       proc_end_p1 = proc_start + fd_ptr->cpd;
  3702.       for (proc = proc_start; proc < proc_end_p1; proc++)
  3703.     {
  3704.       register SYMR *proc_sym = ORIG_LSYMS (fd_ptr->isymBase + proc->isym);
  3705.       register char *str = ORIG_LSTRS (fd_ptr->issBase + proc_sym->iss);
  3706.       register Size_t len = strlen (str);
  3707.       register shash_t *shash_ptr = hash_string (str,
  3708.                              (Ptrdiff_t)len,
  3709.                              &orig_str_hash[0],
  3710.                              (symint_t *)0);
  3711.  
  3712.       if (shash_ptr == (shash_t *)0)
  3713.         error ("internal error, function %s is not in original symbol table", str);
  3714.  
  3715.       else
  3716.         shash_ptr->proc_ptr = proc;
  3717.     }
  3718.  
  3719.       if (debug)
  3720.     fprintf (stderr, "\tproc\tdone,  filename %s\n", filename);
  3721.  
  3722.     }
  3723.   cur_file_ptr = first_file;
  3724.  
  3725.  
  3726.   /* Copy all of the object file up to the symbol table.  Originally
  3727.      we were going to use ftruncate, but that doesn't seem to work
  3728.      on Ultrix 3.1.... */
  3729.  
  3730.   if (fseek (obj_in_stream, (long)0, SEEK_SET) != 0)
  3731.     pfatal_with_name (obj_in_name);
  3732.  
  3733.   if (fseek (object_stream, (long)0, SEEK_SET) != 0)
  3734.     pfatal_with_name (object_name);
  3735.  
  3736.   for (remaining = orig_file_header.f_symptr;
  3737.        remaining > 0;
  3738.        remaining -= num_write)
  3739.     {
  3740.       num_write = (remaining <= sizeof (buffer)) ? remaining : sizeof (buffer);
  3741.       sys_read = fread ((PTR_T) buffer, 1, num_write, obj_in_stream);
  3742.       if (sys_read <= 0)
  3743.     pfatal_with_name (obj_in_name);
  3744.  
  3745.       else if (sys_read != num_write)
  3746.     fatal ("Wanted to read %d bytes from %s, system returned %d",
  3747.            num_write,
  3748.            obj_in_name,
  3749.            sys_read);
  3750.  
  3751.       sys_write = fwrite (buffer, 1, num_write, object_stream);
  3752.       if (sys_write <= 0)
  3753.     pfatal_with_name (object_name);
  3754.  
  3755.       else if (sys_write != num_write)
  3756.     fatal ("Wrote %d bytes to %s, system returned %d",
  3757.            num_write,
  3758.            object_name,
  3759.            sys_write);
  3760.     }
  3761. }
  3762.  
  3763.  
  3764. /* Ye olde main program.  */
  3765.  
  3766. int
  3767. main (argc, argv)
  3768.      int argc;
  3769.      char *argv[];
  3770. {
  3771.   int iflag = 0;
  3772.   char *p = strrchr (argv[0], '/');
  3773.   char *num_end;
  3774.   int option;
  3775.  
  3776.   progname = (p != 0) ? p+1 : argv[0];
  3777.  
  3778.   (void) signal (SIGSEGV, catch_signal);
  3779.   (void) signal (SIGBUS,  catch_signal);
  3780.   (void) signal (SIGABRT, catch_signal);
  3781.  
  3782. #if !defined(__SABER__) && !defined(lint)
  3783.   if (sizeof (efdr_t) > PAGE_USIZE)
  3784.     fatal ("Efdr_t has a sizeof %d bytes, when it should be less than %d",
  3785.        sizeof (efdr_t),
  3786.        PAGE_USIZE);
  3787.  
  3788.   if (sizeof (page_t) != PAGE_USIZE)
  3789.     fatal ("Page_t has a sizeof %d bytes, when it should be %d",
  3790.        sizeof (page_t),
  3791.        PAGE_USIZE);
  3792.  
  3793.   if (sizeof (word8_t) != 8 * sizeof (symint_t))
  3794.     fatal ("Word8_t has a sizeof %d bytes, when it should be %d",
  3795.        sizeof (word8_t),
  3796.        8 * sizeof (symint_t));
  3797. #endif
  3798.  
  3799.   int_type_info  = type_info_init;
  3800.   int_type_info.basic_type = bt_Int;
  3801.  
  3802.   void_type_info = type_info_init;
  3803.   void_type_info.basic_type = bt_Void;
  3804.  
  3805.   while ((option = getopt (argc, argv, "d:i:o:v")) != EOF)
  3806.     switch (option)
  3807.       {
  3808.       default:
  3809.     had_errors++;
  3810.     break;
  3811.  
  3812.       case 'd':
  3813.     debug = strtol (optarg, &num_end, 0);
  3814.     if ((unsigned)debug > 4 || num_end == optarg)
  3815.       had_errors++;
  3816.  
  3817.     break;
  3818.  
  3819.       case 'i':
  3820.     if (obj_in_name == (char *)0)
  3821.       {
  3822.         obj_in_name = optarg;
  3823.         iflag++;
  3824.       }
  3825.     else
  3826.       had_errors++;
  3827.     break;
  3828.  
  3829.       case 'o':
  3830.     if (object_name == (char *)0)
  3831.       object_name = optarg;
  3832.     else
  3833.       had_errors++;
  3834.     break;
  3835.  
  3836.       case 'v':
  3837.     version++;
  3838.     break;
  3839.       }
  3840.  
  3841.   if (obj_in_name == (char *)0 && optind <= argc - 2)
  3842.     obj_in_name = argv[--argc];
  3843.  
  3844.   if (object_name == (char *)0 && optind <= argc - 2)
  3845.     object_name = argv[--argc];
  3846.  
  3847.   if (object_name == (char *)0 || obj_in_name == (char *)0
  3848.       || had_errors || optind != argc - 1)
  3849.     {
  3850.       fprintf (stderr, "Calling Sequence:\n");
  3851.       fprintf (stderr, "\tmips-tfile [-d <num>] [-v] -i <o-in-file> -o <o-out-file> <s-file> (or)\n");
  3852.       fprintf (stderr, "\tmips-tfile [-d <num>] [-v] <s-file> <o-in-file> <o-out-file>\n");
  3853.       fprintf (stderr, "\n");
  3854.       fprintf (stderr, "Debug levels are:\n");
  3855.       fprintf (stderr, "    1\tGeneral debug + trace functions/blocks.\n");
  3856.       fprintf (stderr, "    2\tDebug level 1 + trace externals.\n");
  3857.       fprintf (stderr, "    3\tDebug level 2 + trace all symbols.\n");
  3858.       fprintf (stderr, "    4\tDebug level 3 + trace memory allocations.\n");
  3859.       return 1;
  3860.     }
  3861.  
  3862.  
  3863.   if (version)
  3864.     {
  3865.       extern char *version_string;
  3866.       fprintf (stderr, "mips-tfile version %s", version_string);
  3867. #ifdef TARGET_VERSION
  3868.       TARGET_VERSION;
  3869. #endif
  3870.       fputc ('\n', stderr);
  3871.     }
  3872.  
  3873.   if (obj_in_name == (char *)0)
  3874.     obj_in_name = object_name;
  3875.  
  3876.   obj_in_stream = fopen (obj_in_name, "r");
  3877.   if (obj_in_stream == (FILE *)0)
  3878.     pfatal_with_name (obj_in_name);
  3879.  
  3880.   object_stream = fopen (object_name, "w+b");
  3881.   if (object_stream == (FILE *)0)
  3882.     pfatal_with_name (object_name);
  3883.  
  3884.   if (strcmp (argv[optind], "-") != 0)
  3885.     {
  3886.       input_name = argv[optind];
  3887.       if (freopen (argv[optind], "r", stdin) != stdin)
  3888.     pfatal_with_name (argv[optind]);
  3889.     }
  3890.  
  3891.  
  3892. #ifdef _IOFBF
  3893.   /* Try to prevent stdio from malloc'ing memory for the buffers.  At the
  3894.      same time, increase the size of mips' stdio buffers.  */
  3895.  
  3896.   setvbuf (object_stream, (char *) allocate_multiple_pages (2), _IOFBF, 2*PAGE_SIZE);
  3897.   setvbuf (obj_in_stream, (char *) allocate_multiple_pages (2), _IOFBF, 2*PAGE_SIZE);
  3898.   setvbuf (stdin,      (char *) allocate_multiple_pages (2), _IOFBF, 2*PAGE_SIZE);
  3899.   setvbuf (stdout,      (char *) allocate_multiple_pages (2), _IOFBF, 2*PAGE_SIZE);
  3900.   setvbuf (stderr,      (char *) allocate_multiple_pages (2), _IOLBF, 2*PAGE_SIZE);
  3901. #endif
  3902.  
  3903.   copy_object ();            /* scan & copy object file */
  3904.   parse_input ();            /* scan all of input */
  3905.  
  3906.   update_headers ();            /* write out tfile */
  3907.   write_object ();
  3908.  
  3909.   return (had_errors) ? 1 : 0;
  3910. }
  3911.  
  3912.  
  3913. /* Catch a signal and exit without dumping core.  */
  3914.  
  3915. STATIC void
  3916. catch_signal (signum)
  3917.      int signum;
  3918. {
  3919.   extern char *sys_siglist[NSIG + 1];
  3920.  
  3921.   (void) signal (signum, SIG_DFL);    /* just in case... */
  3922.   fatal (sys_siglist[signum]);
  3923. }
  3924.  
  3925. /* Print a fatal error message.  NAME is the text.
  3926.    Also include a system error message based on `errno'.  */
  3927.  
  3928. void
  3929. pfatal_with_name (msg)
  3930.      char *msg;
  3931. {
  3932.   int save_errno = errno;        /* just in case.... */
  3933.   if (line_number > 0)
  3934.     fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
  3935.   else
  3936.     fprintf (stderr, "%s:", progname);
  3937.  
  3938.   errno = save_errno;
  3939.   if (errno == 0)
  3940.     fprintf (stderr, "[errno = 0] %s\n", msg);
  3941.   else
  3942.     perror (msg);
  3943.  
  3944.   exit (1);
  3945. }
  3946.  
  3947.  
  3948. /* Procedure to abort with an out of bounds error message.  It has
  3949.    type int, so it can be used with an ?: expression within the
  3950.    ORIG_xxx macros, but the function never returns.  */
  3951.  
  3952. static int
  3953. out_of_bounds (index, max, str, prog_line)
  3954.      symint_t index;        /* index that is out of bounds */
  3955.      symint_t max;        /* maximum index */
  3956.      const char *str;        /* string to print out */
  3957.      int prog_line;        /* line number within mips-tfile.c */
  3958. {
  3959.   if (index < max)        /* just in case */
  3960.     return 0;
  3961.  
  3962.   fprintf (stderr, "%s, %s:%ld index %u is out of bounds for %s, max is %u, mips-tfile.c line# %d\n",
  3963.        progname, input_name, line_number, index, str, max, prog_line);
  3964.  
  3965.   exit (1);
  3966.   return 0;            /* turn off warning messages */
  3967. }
  3968.  
  3969.  
  3970. /* Allocate a cluster of pages.  USE_MALLOC says that malloc does not
  3971.    like sbrk's behind it's back (or sbrk isn't available).  If we use
  3972.    sbrk, we assume it gives us zeroed pages.  */
  3973.  
  3974. #ifndef MALLOC_CHECK
  3975. #ifdef USE_MALLOC
  3976.  
  3977. STATIC page_t *
  3978. allocate_cluster (npages)
  3979.      Size_t npages;
  3980. {
  3981.   register page_t *value = (page_t *) calloc (npages, PAGE_USIZE);
  3982.  
  3983.   if (value == 0)
  3984.     fatal ("Virtual memory exhausted.");
  3985.  
  3986.   if (debug > 3)
  3987.     fprintf (stderr, "\talloc\tnpages = %d, value = 0x%.8x\n", npages, value);
  3988.  
  3989.   return value;
  3990. }
  3991.  
  3992. #else /* USE_MALLOC */
  3993.  
  3994. STATIC page_t *
  3995. allocate_cluster (npages)
  3996.      Size_t npages;
  3997. {
  3998.   register page_t *ptr = (page_t *) sbrk (0);    /* current sbreak */
  3999.   unsigned long offset = ((unsigned long) ptr) & (PAGE_SIZE - 1);
  4000.  
  4001.   if (offset != 0)            /* align to a page boundary */
  4002.     {
  4003.       if (sbrk (PAGE_USIZE - offset) == (char *)-1)
  4004.     pfatal_with_name ("allocate_cluster");
  4005.  
  4006.       ptr = (page_t *) (((char *)ptr) + PAGE_SIZE - offset);
  4007.     }
  4008.  
  4009.   if (sbrk (npages * PAGE_USIZE) == (char *)-1)
  4010.     pfatal_with_name ("allocate_cluster");
  4011.  
  4012.   if (debug > 3)
  4013.     fprintf (stderr, "\talloc\tnpages = %d, value = 0x%.8x\n", npages, ptr);
  4014.  
  4015.   return ptr;
  4016. }
  4017.  
  4018. #endif /* USE_MALLOC */
  4019.  
  4020.  
  4021. static page_t    *cluster_ptr    = NULL;
  4022. static unsigned     pages_left    = 0;
  4023.  
  4024. #endif /* MALLOC_CHECK */
  4025.  
  4026.  
  4027. /* Allocate some pages (which is initialized to 0).  */
  4028.  
  4029. STATIC page_t *
  4030. allocate_multiple_pages (npages)
  4031.      Size_t npages;
  4032. {
  4033. #ifndef MALLOC_CHECK
  4034.   if (pages_left == 0 && npages < MAX_CLUSTER_PAGES)
  4035.     {
  4036.       pages_left = MAX_CLUSTER_PAGES;
  4037.       cluster_ptr = allocate_cluster (MAX_CLUSTER_PAGES);
  4038.     }
  4039.  
  4040.   if (npages <= pages_left)
  4041.     {
  4042.       page_t *ptr = cluster_ptr;
  4043.       cluster_ptr += npages;
  4044.       pages_left -= npages;
  4045.       return ptr;
  4046.     }
  4047.  
  4048.   return allocate_cluster (npages);
  4049.  
  4050. #else    /* MALLOC_CHECK */
  4051.   return (page_t *) xcalloc (npages, PAGE_SIZE);
  4052.  
  4053. #endif    /* MALLOC_CHECK */
  4054. }
  4055.  
  4056.  
  4057. /* Allocate one page (which is initialized to 0).  */
  4058.  
  4059. STATIC page_t *
  4060. allocate_page __proto((void))
  4061. {
  4062. #ifndef MALLOC_CHECK
  4063.   if (pages_left == 0)
  4064.     {
  4065.       pages_left = MAX_CLUSTER_PAGES;
  4066.       cluster_ptr = allocate_cluster (MAX_CLUSTER_PAGES);
  4067.     }
  4068.  
  4069.   pages_left--;
  4070.   return cluster_ptr++;
  4071.  
  4072. #else    /* MALLOC_CHECK */
  4073.   return (page_t *) xcalloc (1, PAGE_SIZE);
  4074.  
  4075. #endif    /* MALLOC_CHECK */
  4076. }
  4077.  
  4078.  
  4079. /* Allocate and release 4 word quanities.  */
  4080.  
  4081. #ifndef MALLOC_CHECK
  4082. static word8_t *word8_free_list = (word8_t *)0;
  4083. #endif
  4084.  
  4085. STATIC word8_t *
  4086. allocate_word8 __proto((void))
  4087. {
  4088. #ifndef MALLOC_CHECK
  4089.   register word8_t *ptr = word8_free_list;
  4090.   if (ptr != (word8_t *)0)
  4091.     word8_free_list = ptr->prev;
  4092.   else
  4093.     {
  4094.       register int i;
  4095.       register page_t *page_ptr;
  4096.  
  4097.       page_ptr = allocate_page ();
  4098.       ptr = &page_ptr->word8[0];
  4099.       for (i = 0; i < (PAGE_SIZE / sizeof (word8_t)) - 1; i++)
  4100.     {
  4101.       ptr->prev = word8_free_list;
  4102.       word8_free_list = ptr;
  4103.       ptr++;
  4104.     }
  4105.     }
  4106.  
  4107.   ptr->words[0] = 0;
  4108.   ptr->words[1] = 0;
  4109.   ptr->words[2] = 0;
  4110.   ptr->words[3] = 0;
  4111.   ptr->words[4] = 0;
  4112.   ptr->words[5] = 0;
  4113.   ptr->words[6] = 0;
  4114.   ptr->words[7] = 0;
  4115.   return ptr;
  4116.  
  4117. #else    /* MALLOC_CHECK */
  4118.   return (word8_t *) xcalloc (1, sizeof (word8_t));
  4119.  
  4120. #endif    /* MALLOC_CHECK */
  4121. }
  4122.  
  4123. STATIC void
  4124. free_word8 (ptr)
  4125.      word8_t *ptr;
  4126. {
  4127. #ifndef MALLOC_CHECK
  4128.   ptr->prev = word8_free_list;
  4129.   word8_free_list = ptr;
  4130.  
  4131. #else    /* MALLOC_CHECK */
  4132.   xfree ((PTR_T)ptr);
  4133.  
  4134. #endif    /* MALLOC_CHECK */
  4135. }
  4136.  
  4137. #endif                /* MIPS_DEBUGGING defined */
  4138.  
  4139.  
  4140. /* Output an error message and exit */
  4141.  
  4142. /*VARARGS*/
  4143. void
  4144. fatal (va_alist)
  4145.      va_dcl
  4146. {
  4147.   va_list ap;
  4148.   char *format;
  4149.  
  4150.   if (line_number > 0)
  4151.     fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
  4152.   else
  4153.     fprintf (stderr, "%s:", progname);
  4154.  
  4155.   va_start(ap);
  4156.   format = va_arg (ap, char *);
  4157.   vfprintf (stderr, format, ap);
  4158.   va_end (ap);
  4159.   fprintf (stderr, "\n");
  4160.   if (line_number > 0)
  4161.     fprintf (stderr, "line:\t%s\n", cur_line_start);
  4162.  
  4163.   saber_stop ();
  4164.   exit (1);
  4165. }
  4166.  
  4167. /*VARARGS*/
  4168. void
  4169. error (va_alist) 
  4170.      va_dcl
  4171. {
  4172.   va_list ap;
  4173.   char *format;
  4174.  
  4175.   if (line_number > 0)
  4176.     fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
  4177.   else
  4178.     fprintf (stderr, "%s:", progname);
  4179.  
  4180.   va_start(ap);
  4181.   format = va_arg (ap, char *);
  4182.   vfprintf (stderr, format, ap);
  4183.   fprintf (stderr, "\n");
  4184.   if (line_number > 0)
  4185.     fprintf (stderr, "line:\t%s\n", cur_line_start);
  4186.  
  4187.   had_errors++;
  4188.   va_end (ap);
  4189.  
  4190.   saber_stop ();
  4191. }
  4192.  
  4193. /* More 'friendly' abort that prints the line and file.
  4194.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  4195.  
  4196. void
  4197. fancy_abort ()
  4198. {
  4199.   fatal ("Internal abort.");
  4200. }
  4201.  
  4202.  
  4203. /* When `malloc.c' is compiled with `rcheck' defined,
  4204.    it calls this function to report clobberage.  */
  4205.  
  4206. void
  4207. botch (s)
  4208.      const char *s;
  4209. {
  4210.   fatal (s);
  4211. }
  4212.  
  4213. /* Same as `malloc' but report error if no memory available.  */
  4214.  
  4215. PTR_T
  4216. xmalloc (size)
  4217.      Size_t size;
  4218. {
  4219.   register PTR_T value = malloc (size);
  4220.   if (value == 0)
  4221.     fatal ("Virtual memory exhausted.");
  4222.  
  4223.   if (debug > 3)
  4224.     fprintf (stderr, "\tmalloc\tptr = 0x%.8x, size = %10u\n", value, size);
  4225.  
  4226.   return value;
  4227. }
  4228.  
  4229. /* Same as `calloc' but report error if no memory available.  */
  4230.  
  4231. PTR_T
  4232. xcalloc (size1, size2)
  4233.      Size_t size1, size2;
  4234. {
  4235.   register PTR_T value = calloc (size1, size2);
  4236.   if (value == 0)
  4237.     fatal ("Virtual memory exhausted.");
  4238.  
  4239.   if (debug > 3)
  4240.     fprintf (stderr, "\tcalloc\tptr = 0x%.8x, size1 = %10u, size2 = %10u [%u]\n",
  4241.          value, size1, size2, size1+size2);
  4242.  
  4243.   return value;
  4244. }
  4245.  
  4246. /* Same as `realloc' but report error if no memory available.  */
  4247.  
  4248. PTR_T
  4249. xrealloc (ptr, size)
  4250.      PTR_T ptr;
  4251.      Size_t size;
  4252. {
  4253.   register PTR_T result = realloc (ptr, size);
  4254.   if (!result)
  4255.     fatal ("Virtual memory exhausted.");
  4256.  
  4257.   if (debug > 3)
  4258.     fprintf (stderr, "\trealloc\tptr = 0x%.8x, size = %10u, orig = 0x%.8x\n",
  4259.          result, size, ptr);
  4260.  
  4261.   return result;
  4262. }
  4263.  
  4264. void
  4265. xfree (ptr)
  4266.      PTR_T ptr;
  4267. {
  4268.   if (debug > 3)
  4269.     fprintf (stderr, "\tfree\tptr = 0x%.8x\n", ptr);
  4270.  
  4271.   free (ptr);
  4272. }
  4273.