nasm.h 9.4 KB

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