home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / src / asm.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-11  |  16.2 KB  |  512 lines

  1. /* asm.h - global header file */
  2. #pragma warning(disable:4057)
  3. #pragma warning(disable:4244)
  4.  
  5. #ifndef asmH
  6. #define asmH            /* Don't declare things twice. */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. //#include <assert.h>
  11. #include <time.h>
  12. #ifndef __PTR_TO_INT
  13. #define __PTR_TO_INT(P) ((P) - (char *)0)
  14. #endif
  15.  
  16. #ifndef __INT_TO_PTR
  17. #define __INT_TO_PTR(P) ((P) + (char *)0)
  18. #endif
  19. extern int atoi(char *);
  20. extern void qsort(void *,int,int,int(*compare)(const void *,const void *));
  21. #define index strchr
  22. struct _obstack_chunk        /* Lives at front of each chunk. */
  23. {
  24.   char  *limit;            /* 1 past end of this chunk */
  25.   struct _obstack_chunk *prev;    /* address of prior chunk or NULL */
  26.   char    contents[4];        /* objects begin here */
  27. };
  28.  
  29. struct obstack        /* control current object in current chunk */
  30. {
  31.   long    chunk_size;        /* preferred size to allocate chunks in */
  32.   struct _obstack_chunk* chunk;    /* address of current struct obstack_chunk */
  33.   char    *object_base;        /* address of object we are building */
  34.   char    *next_free;        /* where to add next char to current object */
  35.   char    *chunk_limit;        /* address of char after current chunk */
  36.   int    temp;            /* Temporary for some macros.  */
  37.   int   alignment_mask;        /* Mask of alignment for each object. */
  38.   struct _obstack_chunk *(*chunkfun) (int); /* User's fcn to allocate a chunk.  */
  39.   void (*freefun) (void *);        /* User's function to free a chunk.  */
  40. };
  41.  
  42.  
  43. /* Do the function-declarations after the structs
  44.    but before defining the macros.  */
  45.  
  46. void obstack_init (struct obstack *obstack);
  47.  
  48. void * obstack_alloc (struct obstack *obstack, int size);
  49.  
  50. void * obstack_copy (struct obstack *obstack, void *address, int size);
  51. void * obstack_copy0 (struct obstack *obstack, void *address, int size);
  52.  
  53. static void obstack_free (struct obstack *obstack, void *block);
  54.  
  55. void obstack_blank (struct obstack *obstack, int size);
  56.  
  57. void obstack_grow (struct obstack *obstack, void *data, int size);
  58. void obstack_grow0 (struct obstack *obstack, void *data, int size);
  59.  
  60. void obstack_1grow (struct obstack *obstack, int data_char);
  61. void obstack_ptr_grow (struct obstack *obstack, void *data);
  62. void obstack_int_grow (struct obstack *obstack, int data);
  63.  
  64. void * obstack_finish (struct obstack *obstack);
  65.  
  66. int obstack_object_size (struct obstack *obstack);
  67.  
  68. int obstack_room (struct obstack *obstack);
  69. void obstack_1grow_fast (struct obstack *obstack, int data_char);
  70. void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
  71. void obstack_int_grow_fast (struct obstack *obstack, int data);
  72. void obstack_blank_fast (struct obstack *obstack, int size);
  73.  
  74. void * obstack_base (struct obstack *obstack);
  75. void * obstack_next_free (struct obstack *obstack);
  76. int obstack_alignment_mask (struct obstack *obstack);
  77. int obstack_chunk_size (struct obstack *obstack);
  78.  
  79.  
  80. /* Non-ANSI C cannot really support alternative functions for these macros,
  81.    so we do not declare them.  */
  82.  
  83. /* Pointer to beginning of object being allocated or to be allocated next.
  84.    Note that this might not be the final address of the object
  85.    because a new chunk might be needed to hold the final size.  */
  86.  
  87. #define obstack_base(h) ((h)->object_base)
  88.  
  89. /* Size for allocating ordinary chunks.  */
  90.  
  91. #define obstack_chunk_size(h) ((h)->chunk_size)
  92.  
  93. /* Pointer to next byte not yet allocated in current chunk.  */
  94.  
  95. #define obstack_next_free(h)    ((h)->next_free)
  96.  
  97. /* Mask specifying low bits that should be clear in address of an object.  */
  98.  
  99. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  100.  
  101. #define obstack_init(h) \
  102.   _obstack_begin ((h), 0, 0, \
  103.           (void *(*) (int)) obstack_chunk_alloc, obstack_chunk_free)
  104.  
  105. #define obstack_begin(h, size) \
  106.   _obstack_begin ((h), (size), 0, \
  107.           (void *(*) (int)) obstack_chunk_alloc, obstack_chunk_free)
  108.  
  109. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  110.  
  111. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  112.  
  113. #define obstack_room(h)        \
  114.  (unsigned) ((h)->chunk_limit - (h)->next_free)
  115.  
  116. #define obstack_grow(h,where,length)                    \
  117. ( (h)->temp = (length),                            \
  118.   (((h)->next_free + (h)->temp > (h)->chunk_limit)            \
  119.    ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),            \
  120.   bcopy (where, (h)->next_free, (h)->temp),                \
  121.   (h)->next_free += (h)->temp)
  122.  
  123. #define obstack_1grow(h,datum)                        \
  124. ( (((h)->next_free + 1 > (h)->chunk_limit)                \
  125.    ? (_obstack_newchunk ((h), 1), 0) : 0),                \
  126.   *((h)->next_free)++ = (datum))
  127.  
  128. #define obstack_ptr_grow_fast(h,aptr) (*((char **)(h)->next_free)++ = (char *)aptr)
  129. #define obstack_int_grow_fast(h,aint) (*((int *)(h)->next_free)++ = (int)aint)
  130.  
  131. #define obstack_blank(h,length)                        \
  132. ( (h)->temp = (length),                            \
  133.   (((h)->chunk_limit - (h)->next_free < (h)->temp)            \
  134.    ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),            \
  135.   (h)->next_free += (h)->temp)
  136.  
  137. #define obstack_alloc(h,length)                        \
  138.  (obstack_blank ((h), (length)), obstack_finish ((h)))
  139.  
  140. #define obstack_copy(h,where,length)                    \
  141.  (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  142.  
  143. #define obstack_finish(h)                          \
  144. ( (h)->temp = __PTR_TO_INT ((h)->object_base),                \
  145.   (h)->next_free                            \
  146.     = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask)    \
  147.             & ~ ((h)->alignment_mask)),                \
  148.   (((h)->next_free - (char *)(h)->chunk                    \
  149.     > (h)->chunk_limit - (char *)(h)->chunk)                \
  150.    ? ((h)->next_free = (h)->chunk_limit) : 0),                \
  151.   (h)->object_base = (h)->next_free,                    \
  152.   __INT_TO_PTR ((h)->temp))
  153.  
  154. #define bcopy(from,to,n) memcpy((to),(from),(n))
  155. #define obstack_chunk_alloc    xmalloc
  156. #define obstack_chunk_free    free
  157.  
  158. /* These defines are potentially useful */
  159. #define FALSE    (0)
  160. #define TRUE    (!FALSE)
  161. #define ASSERT    assert
  162.  
  163.  
  164. #ifdef SUSPECT
  165. #define register        /* no registers: helps debugging */
  166. #define know(p) ASSERT(p)    /* know() is less ugly than #ifdef SUSPECT/ */
  167.                 /* assert()/#endif. */
  168. #else
  169. #define know(p)            /* know() checks are no-op.ed */
  170. #endif                /* #ifdef SUSPECT */
  171.  
  172. /* subsegs.c     Sub-segments. Also, segment(=expression type)s.*/
  173. /*
  174.  * This table describes the use of segments as EXPRESSION types.
  175.  *
  176.  *    X_seg    X_add_symbol  X_subtract_symbol    X_add_number
  177.  * SEG_NONE                        no (legal) expression
  178.  * SEG_PASS1                        no (defined) "
  179.  * SEG_BIG                    *    > 32 bits const.
  180.  * SEG_ABSOLUTE                         0
  181.  * SEG_DATA        *                 0
  182.  * SEG_TEXT        *            0
  183.  * SEG_BSS        *            0
  184.  * SEG_UNKNOWN        *            0
  185.  * SEG_DIFFERENCE    0        *    0
  186.  *
  187.  * The blank fields MUST be 0, and are nugatory.
  188.  * The '0' fields MAY be 0. The '*' fields MAY NOT be 0.
  189.  *
  190.  * SEG_BIG: X_add_number is < 0 if the result is in
  191.  *    generic_floating_point_number.  The value is -'c' where c is the
  192.  *    character that introduced the constant.  e.g. "0f6.9" will have  -'f'
  193.  *    as a X_add_number value.
  194.  *    X_add_number > 0 is a count of how many littlenums it took to
  195.  *    represent a bignum.
  196.  * SEG_DIFFERENCE:
  197.  * If segments of both symbols are known, they are the same segment.
  198.  * X_add_symbol != X_sub_symbol (then we just cancel them, => SEG_ABSOLUTE).
  199.  */
  200.  
  201. typedef enum
  202. {
  203.     SEG_ABSOLUTE,
  204.     SEG_TEXT,
  205.     SEG_DATA,
  206.     SEG_BSS,
  207.     SEG_UNKNOWN,
  208.     SEG_NONE,        /* Mythical Segment: NO expression seen. */
  209.     SEG_PASS1,        /* Mythical Segment: Need another pass. */
  210.     SEG_GOOF,        /* Only happens if AS has a logic error. */
  211.                 /* Invented so we don't crash printing */
  212.                 /* error message involving weird segment. */
  213.     SEG_BIG,            /* Bigger than 32 bits constant. */
  214.     SEG_DIFFERENCE        /* Mythical Segment: absolute difference. */
  215. }        segT;
  216. #define SEG_MAXIMUM_ORDINAL (SEG_DIFFERENCE)
  217.  
  218. typedef unsigned char    subsegT;
  219.  
  220. typedef enum
  221. {
  222.     rs_fill,        /* Variable chars to be repeated fr_offset */
  223.                 /* times. Fr_symbol unused. */
  224.                 /* Used with fr_offset == 0 for a constant */
  225.                 /* length frag. */
  226.  
  227.     rs_align,        /* Align: Fr_offset: power of 2. */
  228.                 /* 1 variable char: fill character. */
  229.     rs_org,            /* Org: Fr_offset, fr_symbol: address. */
  230.                 /* 1 variable char: fill character. */
  231.  
  232.     rs_machine_dependent,
  233. }
  234. relax_stateT;
  235.  
  236. typedef unsigned long int relax_substateT;
  237.  
  238. typedef unsigned long int relax_addressT;/* Enough bits for address. */
  239.                 /* Still an integer type. */
  240.  
  241. /* subsegs.h -> subsegs.c
  242.  * For every sub-segment the user mentions in the ASsembler program,
  243.  * we make one struct frchain. Each sub-segment has exactly one struct frchain
  244.  * and vice versa.
  245.  *
  246.  * Struct frchain's are forward chained (in ascending order of sub-segment
  247.  * code number). The chain runs through frch_next of each subsegment.
  248.  * This makes it hard to find a subsegment's frags
  249.  * if programmer uses a lot of them. Most programs only use text0 and
  250.  * data0, so they don't suffer. At least this way:
  251.  * (1)    There are no "arbitrary" restrictions on how many subsegments
  252.  *    can be programmed;
  253.  * (2)    Subsegments' frchain-s are (later) chained together in the order in
  254.  *    which they are emitted for object file viz text then data.
  255.  *
  256.  * From each struct frchain dangles a chain of struct frags. The frags
  257.  * represent code fragments, for that sub-segment, forward chained.
  258.  */
  259.  
  260. struct frchain            /* control building of a frag chain */
  261. {                /* FRCH = FRagment CHain control */
  262.   struct frag *    frch_root;    /* 1st struct frag in chain, or NULL */
  263.   struct frag *    frch_last;    /* last struct frag in chain, or NULL */
  264.   struct frchain * frch_next;    /* next in chain of struct frchain-s */
  265.   segT        frch_seg;    /* SEG_TEXT or SEG_DATA. */
  266.   subsegT    frch_subseg;    /* subsegment number of this chain */
  267. };
  268.  
  269. typedef struct frchain frchainS;
  270. /*
  271.  * A code fragment (frag) is some known number of chars, followed by some
  272.  * unknown number of chars. Typically the unknown number of chars is an
  273.  * instruction address whose size is yet unknown. We always know the greatest
  274.  * possible size the unknown number of chars may become, and reserve that
  275.  * much room at the end of the frag.
  276.  * Once created, frags do not change address during assembly.
  277.  * We chain the frags in (a) forward-linked list(s). The object-file address
  278.  * of the 1st char of a frag is generally not known until after relax().
  279.  * Many things at assembly time describe an address by {object-file-address
  280.  * of a particular frag}+offset.
  281.  
  282.  BUG: it may be smarter to have a single pointer off to various different
  283. notes for different frag kinds. See how code pans out.
  284.  */
  285. struct frag            /* a code fragment */
  286. {
  287.     long unsigned int fr_address; /* Object file address. */
  288.     struct frag *fr_next;    /* Chain forward; ascending address order. */
  289.                 /* Rooted in frch_root. */
  290.  
  291.     long int fr_fix;    /* (Fixed) number of chars we know we have. */
  292.                 /* May be 0. */
  293.     long int fr_var;    /* (Variable) number of chars after above. */
  294.                 /* May be 0. */
  295.     struct AsmSymbol *fr_symbol; /* For variable-length tail. */
  296.     long int fr_offset;    /* For variable-length tail. */
  297.     char    *fr_opcode;    /*->opcode low addr byte,for relax()ation*/
  298.     relax_stateT fr_type;   /* What state is my tail in? */
  299.     relax_substateT    fr_subtype;
  300.     char    fr_literal [1];    /* Chars begin here. */
  301.                 /* One day we will compile fr_literal[0]. */
  302. };
  303. #define SIZEOF_STRUCT_FRAG \
  304.  ((int)zero_address_frag.fr_literal-(int)&zero_address_frag)
  305.                 /* We want to say fr_literal[0] above. */
  306.  
  307. typedef struct frag fragS;
  308.  
  309. #define N_UNDF 0
  310. #define N_ABS 2
  311. #define N_TEXT 4
  312. #define N_DATA 6
  313. #define N_BSS 8
  314. #define N_FN 15
  315.  
  316. #define N_EXT 1
  317. #define N_TYPE 036
  318. #define N_STAB 0340
  319. struct relocation_info
  320. {
  321.   /* Address (within segment) to be relocated.  */
  322.   int r_address;
  323.   unsigned int r_pcrel:1;
  324. };
  325.  
  326. struct AsmSymbol
  327. {
  328.   struct AsmSymbol         *Next;        /* forward chain, or NULL */
  329.   char                    *Name;
  330.   unsigned long         SymbolValue;
  331.   struct frag            *sy_frag;    /* NULL or -> frag this symbol attaches to. */
  332.   struct AsmSymbol         *sy_forward;/* value is really that of this other symbol */
  333.   int                    SectionNumber;
  334.   int                    Type;
  335.   int                    StorageClass;
  336.   int                    NumberOfAuxSymbols;
  337.   int                    OrdinalNumber;
  338.   int                    StartLine;
  339.   int                    EndLine;
  340.   int                    Flags;
  341.   struct tagCoffReloc     *Relocations;
  342.   struct tagCoffLine     *Lines;
  343.   struct tagCoffFn         *FnInfo;
  344.   unsigned char         sy_type;
  345. };
  346.  
  347. typedef struct AsmSymbol symbolS;
  348.  
  349. typedef unsigned valueT;    /* The type of n_value. Helps casting. */
  350.  
  351. typedef struct {
  352.   char *    poc_name;    /* assembler mnemonic, lower case, no '.' */
  353.   void        (*poc_handler)(void);    /* Do the work */
  354. }
  355. pseudo_typeS;
  356.  
  357. typedef struct
  358. {
  359.     long    rlx_forward;    /* Forward  reach. Signed number. > 0. */
  360.     long    rlx_backward;    /* Backward reach. Signed number. < 0. */
  361.     unsigned char rlx_length;    /* Bytes length of this address. */
  362.     relax_substateT rlx_more;    /* Next longer relax-state. */
  363.                 /* 0 means there is no 'next' relax-state. */
  364. }
  365. relax_typeS;
  366.  
  367.  
  368. /*
  369.  * FixSs may be built up in any order.
  370.  */
  371.  
  372. struct fix
  373. {
  374.   struct fix   *fx_next;    /* NULL or -> next fixS. */
  375.   fragS         *fx_frag;    /* Which frag? */
  376.   long int        fx_where;    /* Where is the 1st byte to fix up? */
  377.   symbolS      *fx_addsy; /* NULL or Symbol whose value we add in. */
  378.   symbolS      *fx_subsy; /* NULL or Symbol whose value we subtract. */
  379.   long int        fx_offset;    /* Absolute number we add in. */
  380.   short int        fx_size;    /* How many bytes are involved? */
  381.   char            fx_pcrel;    /* TRUE: pc-relative. */
  382.   struct        tagCoffReloc *CoffReloc;
  383. };
  384.  
  385. typedef struct fix    fixS;
  386.  
  387. struct HASH_LIST {
  388.     struct HASH_LIST *Next;
  389.     char *Name;
  390.     char *Data;
  391.     short len;
  392. };
  393.  
  394. typedef struct tagCoffReloc {
  395.     struct tagCoffReloc *Next;
  396.     int       Offset;
  397.     symbolS *Symbol;
  398.     fixS *pfixS;
  399.     int Flags;
  400.     short int Type;
  401.     int TargetSegment;
  402. } COFF_RELOC;
  403.  
  404. typedef struct tagCoffFn {
  405.     int Tag;
  406.     int Size;
  407.     int NrOfLines;
  408.     symbolS *Start;
  409.     symbolS *End;
  410.     int LineNumbersFileOffset;
  411.     int NextFnIdx;
  412. } COFF_FN;
  413.  
  414. typedef struct tagCoffLine {
  415.     struct tagCoffLine *Next;
  416.     fragS *Frag;
  417.     int Line;
  418.     int fragOffset;
  419. } COFF_LINE;
  420.  
  421. #define IS_PCRELATIVE 1
  422. #define IS_TEXT       2
  423. #define IS_DATA       4
  424. #define IS_BSS        8
  425. #define IS_FUNCTION      16
  426. #define COUNTED          32
  427.  
  428. #define SECTION_TEXT    1
  429. #define SECTION_DATA    2
  430. #define SECTION_BSS        3
  431.  
  432. typedef struct tagString {
  433.     struct tagString *Next;
  434.     int len;
  435.     char *String;
  436. } STRING;
  437.  
  438. typedef struct tagStringTable {
  439.     int Size;
  440.     STRING *Strings;
  441. } STRING_TABLE;
  442.  
  443. /*
  444.  * A macro to speed up appending exactly 1 char
  445.  * to current frag.
  446.  */
  447. #define FRAG_APPEND_1_CHAR(datum)    \
  448. {                    \
  449.     if (obstack_room( &frags ) <= 1) {\
  450.         frag_wane (frag_now);    \
  451.         frag_new (0);        \
  452.     }                \
  453.     obstack_1grow( &frags, datum );    \
  454. }
  455. #define hash_control HASH_LIST
  456.  
  457. #define symbol_table_lookup(name) ((symbolS *)(hash_find (sy_hash,name)))
  458.  
  459. /*#define SKIP_WHITESPACE() ASSERT( * InputPointer != ' ' )*/
  460. #define SKIP_WHITESPACE() while (*InputPointer == ' ' || *InputPointer == '\t') InputPointer++;
  461. #define    LEX_NAME    (1)    /* may continue a name */
  462. #define LEX_BEGIN_NAME    (2)    /* may begin a name */
  463.  
  464. #define is_name_beginner(c)     ( lex_type[c] & LEX_BEGIN_NAME )
  465. #define is_part_of_name(c)      ( lex_type[c] & LEX_NAME       )
  466.  
  467. /*
  468.  * Abbreviations (mnemonics).
  469.  *
  470.  *    O    operator
  471.  *    Q    quantity,  operand
  472.  *    X    eXpression
  473.   * By popular demand, we define a struct to represent an expression.
  474.  * This will no doubt mutate as expressions become baroque.
  475.  *
  476.  * Currently, we support expressions like "foo-bar+42".
  477.  * In other words we permit a (possibly undefined) minuend, a
  478.  * (possibly undefined) subtrahend and an (absolute) augend.
  479.  * RMS says this is so we can have 1-pass assembly for any compiler
  480.  * emmissions, and a 'case' statement might emit 'undefined1 - undefined2'.
  481.  *
  482.  * To simplify table-driven dispatch, we also have a "segment" for the
  483.  * entire expression. That way we don't require complex reasoning about
  484.  * whether particular components are defined; and we can change component
  485.  * semantics without re-working all the dispatch tables in the assembler.
  486.  * In other words the "type" of an expression is its segment.
  487.  */
  488. typedef struct
  489. {
  490.     symbolS *X_add_symbol;        /* foo */
  491.     symbolS *X_subtract_symbol;    /* bar */
  492.     long int X_add_number;        /* 42.    Must be signed. */
  493.     segT    X_seg;            /* What segment (expr type)? */
  494. }
  495. expressionS;
  496.  
  497. typedef struct tagNewSection {
  498.     struct tagNewSection *Next;
  499.     char Name[10];
  500.     int HeaderPosition;
  501.     int Number;
  502.     int DataSize;
  503.     char *data;
  504. } NewSection;
  505.  
  506.                 /* result should be type (expressionS *). */
  507. #define expression(result) AsmExpression(0,result)
  508.  
  509. #endif                /* #ifdef asH */
  510.  
  511.  
  512.