Home | History | Annotate | Download | only in nasm
      1 /* nasm.h   main header file for the Netwide Assembler: inter-module interface
      2  *
      3  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
      4  * Julian Hall. All rights reserved. The software is
      5  * redistributable under the licence given in the file "Licence"
      6  * distributed in the NASM archive.
      7  *
      8  * initial version: 27/iii/95 by Simon Tatham
      9  */
     10 #ifndef YASM_NASM_H
     11 #define YASM_NASM_H
     12 
     13 #ifndef NULL
     14 #define NULL 0
     15 #endif
     16 
     17 #ifndef FALSE
     18 #define FALSE 0                        /* comes in handy */
     19 #endif
     20 #ifndef TRUE
     21 #define TRUE 1
     22 #endif
     23 
     24 #ifndef FILENAME_MAX
     25 #define FILENAME_MAX 256
     26 #endif
     27 
     28 #ifndef PREFIX_MAX
     29 #define PREFIX_MAX 10
     30 #endif
     31 
     32 #ifndef POSTFIX_MAX
     33 #define POSTFIX_MAX 10
     34 #endif
     35 
     36 #define IDLEN_MAX 4096
     37 
     38 /*
     39  * -------------------------
     40  * Error reporting functions
     41  * -------------------------
     42  */
     43 
     44 /*
     45  * An error reporting function should look like this.
     46  */
     47 typedef void (*efunc) (int severity, const char *fmt, ...);
     48 
     49 /*
     50  * These are the error severity codes which get passed as the first
     51  * argument to an efunc.
     52  */
     53 
     54 #define ERR_DEBUG       0x00000008      /* put out debugging message */
     55 #define ERR_WARNING     0x00000000      /* warn only: no further action */
     56 #define ERR_NONFATAL    0x00000001      /* terminate assembly after phase */
     57 #define ERR_FATAL       0x00000002      /* instantly fatal: exit with error */
     58 #define ERR_PANIC       0x00000003      /* internal error: panic instantly
     59                                         * and dump core for reference */
     60 #define ERR_MASK        0x0000000F      /* mask off the above codes */
     61 #define ERR_NOFILE      0x00000010      /* don't give source file name/line */
     62 #define ERR_USAGE       0x00000020      /* print a usage message */
     63 #define ERR_PASS1       0x00000040      /* only print this error on pass one */
     64 
     65 /*
     66  * These codes define specific types of suppressible warning.
     67  */
     68 
     69 #define ERR_WARN_MASK   0x0000FF00      /* the mask for this feature */
     70 #define ERR_WARN_SHR  8                /* how far to shift right */
     71 
     72 #define ERR_WARN_MNP    0x00000100      /* macro-num-parameters warning */
     73 #define ERR_WARN_MSR    0x00000200      /* macro self-reference */
     74 #define ERR_WARN_OL     0x00000300      /* orphan label (no colon, and
     75                                         * alone on line) */
     76 #define ERR_WARN_NOV    0x00000400      /* numeric overflow */
     77 #define ERR_WARN_GNUELF 0x00000500      /* using GNU ELF extensions */
     78 #define ERR_WARN_MAX    5               /* the highest numbered one */
     79 
     80 /*
     81  * -----------------------
     82  * Other function typedefs
     83  * -----------------------
     84  */
     85 
     86 /*
     87  * List-file generators should look like this:
     88  */
     89 typedef struct {
     90     /*
     91      * Called to initialise the listing file generator. Before this
     92      * is called, the other routines will silently do nothing when
     93      * called. The `char *' parameter is the file name to write the
     94      * listing to.
     95      */
     96     void (*init) (char *, efunc);
     97 
     98     /*
     99      * Called to clear stuff up and close the listing file.
    100      */
    101     void (*cleanup) (void);
    102 
    103     /*
    104      * Called to output binary data. Parameters are: the offset;
    105      * the data; the data type. Data types are similar to the
    106      * output-format interface, only OUT_ADDRESS will _always_ be
    107      * displayed as if it's relocatable, so ensure that any non-
    108      * relocatable address has been converted to OUT_RAWDATA by
    109      * then. Note that OUT_RAWDATA+0 is a valid data type, and is a
    110      * dummy call used to give the listing generator an offset to
    111      * work with when doing things like uplevel(LIST_TIMES) or
    112      * uplevel(LIST_INCBIN).
    113      */
    114     void (*output) (long, const void *, unsigned long);
    115 
    116     /*
    117      * Called to send a text line to the listing generator. The
    118      * `int' parameter is LIST_READ or LIST_MACRO depending on
    119      * whether the line came directly from an input file or is the
    120      * result of a multi-line macro expansion.
    121      */
    122     void (*line) (int, char *);
    123 
    124     /*
    125      * Called to change one of the various levelled mechanisms in
    126      * the listing generator. LIST_INCLUDE and LIST_MACRO can be
    127      * used to increase the nesting level of include files and
    128      * macro expansions; LIST_TIMES and LIST_INCBIN switch on the
    129      * two binary-output-suppression mechanisms for large-scale
    130      * pseudo-instructions.
    131      *
    132      * LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
    133      * it indicates the beginning of the expansion of a `nolist'
    134      * macro, so anything under that level won't be expanded unless
    135      * it includes another file.
    136      */
    137     void (*uplevel) (int);
    138 
    139     /*
    140      * Reverse the effects of uplevel.
    141      */
    142     void (*downlevel) (int);
    143 } ListGen;
    144 
    145 /*
    146  * The expression evaluator must be passed a scanner function; a
    147  * standard scanner is provided as part of nasmlib.c. The
    148  * preprocessor will use a different one. Scanners, and the
    149  * token-value structures they return, look like this.
    150  *
    151  * The return value from the scanner is always a copy of the
    152  * `t_type' field in the structure.
    153  */
    154 struct tokenval {
    155     int t_type;
    156     yasm_intnum *t_integer, *t_inttwo;
    157     char *t_charptr;
    158 };
    159 typedef int (*scanner) (void *private_data, struct tokenval *tv);
    160 
    161 /*
    162  * Token types returned by the scanner, in addition to ordinary
    163  * ASCII character values, and zero for end-of-string.
    164  */
    165 enum {                                 /* token types, other than chars */
    166     TOKEN_INVALID = -1,                /* a placeholder value */
    167     TOKEN_EOS = 0,                     /* end of string */
    168     TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<',   /* aliases */
    169     TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN,  /* major token types */
    170     TOKEN_ERRNUM,                      /* numeric constant with error in */
    171     TOKEN_HERE, TOKEN_BASE,            /* $ and $$ */
    172     TOKEN_SPECIAL,                     /* BYTE, WORD, DWORD, FAR, NEAR, etc */
    173     TOKEN_PREFIX,                      /* A32, O16, LOCK, REPNZ, TIMES, etc */
    174     TOKEN_SHL, TOKEN_SHR,              /* << and >> */
    175     TOKEN_SDIV, TOKEN_SMOD,            /* // and %% */
    176     TOKEN_GE, TOKEN_LE, TOKEN_NE,      /* >=, <= and <> (!= is same as <>) */
    177     TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR,   /* &&, || and ^^ */
    178     TOKEN_SEG, TOKEN_WRT,              /* SEG and WRT */
    179     TOKEN_FLOAT                        /* floating-point constant */
    180 };
    181 
    182 /*
    183  * The actual expression evaluator function looks like this. When
    184  * called, it expects the first token of its expression to already
    185  * be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
    186  * it will start by calling the scanner.
    187  *
    188  * `critical' is non-zero if the expression may not contain forward
    189  * references. The evaluator will report its own error if this
    190  * occurs; if `critical' is 1, the error will be "symbol not
    191  * defined before use", whereas if `critical' is 2, the error will
    192  * be "symbol undefined".
    193  *
    194  * If `critical' has bit 8 set (in addition to its main value: 0x101
    195  * and 0x102 correspond to 1 and 2) then an extended expression
    196  * syntax is recognised, in which relational operators such as =, <
    197  * and >= are accepted, as well as low-precedence logical operators
    198  * &&, ^^ and ||.
    199  */
    200 #define CRITICAL 0x100
    201 typedef yasm_expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv,
    202                            int critical, efunc error);
    203 
    204 /*
    205  * Preprocessors ought to look like this:
    206  */
    207 typedef struct {
    208     /*
    209      * Called at the start of a pass; given a file name, the number
    210      * of the pass, an error reporting function, an evaluator
    211      * function, and a listing generator to talk to.
    212      */
    213     void (*reset) (FILE *, const char *, int, efunc, evalfunc, ListGen *);
    214 
    215     /*
    216      * Called to fetch a line of preprocessed source. The line
    217      * returned has been malloc'ed, and so should be freed after
    218      * use.
    219      */
    220     char *(*getline) (void);
    221 
    222     /*
    223      * Called at the end of a pass.
    224      */
    225     void (*cleanup) (int);
    226 } Preproc;
    227 
    228 /*
    229  * ----------------------------------------------------------------
    230  * Some lexical properties of the NASM source language, included
    231  * here because they are shared between the parser and preprocessor
    232  * ----------------------------------------------------------------
    233  */
    234 
    235 /*
    236  * isidstart matches any character that may start an identifier, and isidchar
    237  * matches any character that may appear at places other than the start of an
    238  * identifier. E.g. a period may only appear at the start of an identifier
    239  * (for local labels), whereas a number may appear anywhere *but* at the
    240  * start.
    241  */
    242 
    243 #define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
    244                                   || (c)=='@' )
    245 #define isidchar(c)  ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
    246                                                   || (c)=='~' )
    247 
    248 /* Ditto for numeric constants. */
    249 
    250 #define isnumstart(c)  ( isdigit(c) || (c)=='$' )
    251 #define isnumchar(c)   ( isalnum(c) )
    252 
    253 /* This returns the numeric value of a given 'digit'. */
    254 
    255 #define numvalue(c)  ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
    256 
    257 /*
    258  * Data-type flags that get passed to listing-file routines.
    259  */
    260 enum {
    261     LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
    262     LIST_INCBIN, LIST_TIMES
    263 };
    264 
    265 /*
    266  * -----
    267  * Other
    268  * -----
    269  */
    270 
    271 /*
    272  * This is a useful #define which I keep meaning to use more often:
    273  * the number of elements of a statically defined array.
    274  */
    275 
    276 #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
    277 
    278 extern int tasm_compatible_mode;
    279 extern int tasm_locals;
    280 extern const char *tasm_segment;
    281 const char *tasm_get_segment_register(const char *segment);
    282 
    283 #endif
    284