1 /* nasm.h main header file for the Netwide Assembler: inter-module interface
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.
8 * initial version: 27/iii/95 by Simon Tatham
14 #define NASM_MAJOR_VER 0
15 #define NASM_MINOR_VER 95
16 #define NASM_VER "0.95"
23 #define FALSE 0 /* comes in handy */
29 #define NO_SEG -1L /* null segment value */
30 #define SEG_ABS 0x40000000L /* mask for far-absolute segments */
33 #define FILENAME_MAX 256
37 * We must declare the existence of this structure type up here,
38 * since we have to reference it before we define it...
43 * -------------------------
44 * Error reporting functions
45 * -------------------------
49 * An error reporting function should look like this.
51 typedef void (*efunc) (int severity, char *fmt, ...);
54 * These are the error severity codes which get passed as the first
55 * argument to an efunc.
58 #define ERR_WARNING 0 /* warn only: no further action */
59 #define ERR_NONFATAL 1 /* terminate assembly after phase */
60 #define ERR_FATAL 2 /* instantly fatal: exit with error */
61 #define ERR_PANIC 3 /* internal error: panic instantly
62 * and dump core for reference */
63 #define ERR_MASK 0x0F /* mask off the above codes */
64 #define ERR_NOFILE 0x10 /* don't give source file name/line */
65 #define ERR_USAGE 0x20 /* print a usage message */
66 #define ERR_OFFBY1 0x40 /* report error as being on the line
67 * we're just _about_ to read, not
68 * the one we've just read */
70 * These codes define specific types of suppressible warning.
72 #define ERR_WARN_MNP 0x0100 /* macro-num-parameters warning */
73 #define ERR_WARN_OL 0x0200 /* orphan label (no colon, and
75 #define ERR_WARN_MASK 0xFF00 /* the mask for this feature */
76 #define ERR_WARN_SHR 8 /* how far to shift right */
77 #define ERR_WARN_MAX 2 /* the highest numbered one */
80 * -----------------------
81 * Other function typedefs
82 * -----------------------
86 * A label-lookup function should look like this.
88 typedef int (*lfunc) (char *label, long *segment, long *offset);
91 * And a label-definition function like this.
93 typedef void (*ldfunc) (char *label, long segment, long offset,
94 struct ofmt *ofmt, efunc error);
97 * List-file generators should look like this:
101 * Called to initialise the listing file generator. Before this
102 * is called, the other routines will silently do nothing when
103 * called. The `char *' parameter is the file name to write the
106 void (*init) (char *, efunc);
109 * Called to clear stuff up and close the listing file.
111 void (*cleanup) (void);
114 * Called to output binary data. Parameters are: the offset;
115 * the data; the data type. Data types are similar to the
116 * output-format interface, only OUT_ADDRESS will _always_ be
117 * displayed as if it's relocatable, so ensure that any non-
118 * relocatable address has been converted to OUT_RAWDATA by
119 * then. Note that OUT_RAWDATA+0 is a valid data type, and is a
120 * dummy call used to give the listing generator an offset to
121 * work with when doing things like uplevel(LIST_TIMES) or
122 * uplevel(LIST_INCBIN).
124 void (*output) (long, void *, unsigned long);
127 * Called to send a text line to the listing generator. The
128 * `int' parameter is LIST_READ or LIST_MACRO depending on
129 * whether the line came directly from an input file or is the
130 * result of a multi-line macro expansion.
132 void (*line) (int, char *);
135 * Called to change one of the various levelled mechanisms in
136 * the listing generator. LIST_INCLUDE and LIST_MACRO can be
137 * used to increase the nesting level of include files and
138 * macro expansions; LIST_TIMES and LIST_INCBIN switch on the
139 * two binary-output-suppression mechanisms for large-scale
140 * pseudo-instructions.
142 * LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
143 * it indicates the beginning of the expansion of a `nolist'
144 * macro, so anything under that level won't be expanded unless
145 * it includes another file.
147 void (*uplevel) (int);
150 * Reverse the effects of uplevel.
152 void (*downlevel) (int);
156 * Preprocessors ought to look like this:
160 * Called at the start of a pass; given a file name, an error
161 * reporting function and a listing generator to talk to.
163 void (*reset) (char *, efunc, ListGen *);
166 * Called to fetch a line of preprocessed source. The line
167 * returned has been malloc'ed, and so should be freed after
170 char *(*getline) (void);
173 * Called at the end of a pass.
175 void (*cleanup) (void);
179 * ----------------------------------------------------------------
180 * Some lexical properties of the NASM source language, included
181 * here because they are shared between the parser and preprocessor
182 * ----------------------------------------------------------------
185 /* isidstart matches any character that may start an identifier, and isidchar
186 * matches any character that may appear at places other than the start of an
187 * identifier. E.g. a period may only appear at the start of an identifier
188 * (for local labels), whereas a number may appear anywhere *but* at the
191 #define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
193 #define isidchar(c) ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
196 /* Ditto for numeric constants. */
198 #define isnumstart(c) ( isdigit(c) || (c)=='$' )
199 #define isnumchar(c) ( isalnum(c) )
201 /* This returns the numeric value of a given 'digit'. */
203 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
206 * Data-type flags that get passed to listing-file routines.
209 LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
210 LIST_INCBIN, LIST_TIMES
214 * -----------------------------------------------------------
215 * Format of the `insn' structure returned from `parser.c' and
216 * passed into `assemble.c'
217 * -----------------------------------------------------------
221 * Here we define the operand types. These are implemented as bit
222 * masks, since some are subsets of others; e.g. AX in a MOV
223 * instruction is a special operand type, whereas AX in other
224 * contexts is just another 16-bit register. (Also, consider CL in
225 * shift instructions, DX in OUT, etc.)
228 /* size, and other attributes, of the operand */
229 #define BITS8 0x00000001L
230 #define BITS16 0x00000002L
231 #define BITS32 0x00000004L
232 #define BITS64 0x00000008L /* FPU only */
233 #define BITS80 0x00000010L /* FPU only */
234 #define FAR 0x00000020L /* grotty: this means 16:16 or */
235 /* 16:32, like in CALL/JMP */
236 #define NEAR 0x00000040L
237 #define SHORT 0x00000080L /* and this means what it says :) */
239 #define SIZE_MASK 0x000000FFL /* all the size attributes */
240 #define NON_SIZE (~SIZE_MASK)
242 #define TO 0x00000100L /* reverse effect in FADD, FSUB &c */
243 #define COLON 0x00000200L /* operand is followed by a colon */
245 /* type of operand: memory reference, register, etc. */
246 #define MEMORY 0x00204000L
247 #define REGISTER 0x00001000L /* register number in 'basereg' */
248 #define IMMEDIATE 0x00002000L
250 #define REGMEM 0x00200000L /* for r/m, ie EA, operands */
251 #define REGNORM 0x00201000L /* 'normal' reg, qualifies as EA */
252 #define REG8 0x00201001L
253 #define REG16 0x00201002L
254 #define REG32 0x00201004L
255 #define FPUREG 0x01000000L /* floating point stack registers */
256 #define FPU0 0x01000800L /* FPU stack register zero */
257 #define MMXREG 0x00001008L /* MMX registers */
259 /* special register operands: these may be treated differently */
260 #define REG_SMASK 0x00070000L /* a mask for the following */
261 #define REG_ACCUM 0x00211000L /* accumulator: AL, AX or EAX */
262 #define REG_AL 0x00211001L /* REG_ACCUM | BITSxx */
263 #define REG_AX 0x00211002L /* ditto */
264 #define REG_EAX 0x00211004L /* and again */
265 #define REG_COUNT 0x00221000L /* counter: CL, CX or ECX */
266 #define REG_CL 0x00221001L /* REG_COUNT | BITSxx */
267 #define REG_CX 0x00221002L /* ditto */
268 #define REG_ECX 0x00221004L /* another one */
269 #define REG_DX 0x00241002L
270 #define REG_SREG 0x00081002L /* any segment register */
271 #define REG_CS 0x01081002L /* CS */
272 #define REG_DESS 0x02081002L /* DS, ES, SS (non-CS 86 registers) */
273 #define REG_FSGS 0x04081002L /* FS, GS (386 extended registers) */
274 #define REG_CDT 0x00101004L /* CRn, DRn and TRn */
275 #define REG_CREG 0x08101004L /* CRn */
276 #define REG_CR4 0x08101404L /* CR4 (Pentium only) */
277 #define REG_DREG 0x10101004L /* DRn */
278 #define REG_TREG 0x20101004L /* TRn */
280 /* special type of EA */
281 #define MEM_OFFS 0x00604000L /* simple [address] offset */
283 /* special type of immediate operand */
284 #define ONENESS 0x00800000L /* so UNITY == IMMEDIATE | ONENESS */
285 #define UNITY 0x00802000L /* for shift/rotate instructions */
288 * Next, the codes returned from the parser, for registers and
292 enum { /* register names */
293 R_AH = 1, R_AL, R_AX, R_BH, R_BL, R_BP, R_BX, R_CH, R_CL, R_CR0,
294 R_CR2, R_CR3, R_CR4, R_CS, R_CX, R_DH, R_DI, R_DL, R_DR0, R_DR1,
295 R_DR2, R_DR3, R_DR6, R_DR7, R_DS, R_DX, R_EAX, R_EBP, R_EBX,
296 R_ECX, R_EDI, R_EDX, R_ES, R_ESI, R_ESP, R_FS, R_GS, R_MM0,
297 R_MM1, R_MM2, R_MM3, R_MM4, R_MM5, R_MM6, R_MM7, R_SI, R_SP,
298 R_SS, R_ST0, R_ST1, R_ST2, R_ST3, R_ST4, R_ST5, R_ST6, R_ST7,
299 R_TR3, R_TR4, R_TR5, R_TR6, R_TR7, REG_ENUM_LIMIT
302 enum { /* instruction names */
303 I_AAA, I_AAD, I_AAM, I_AAS, I_ADC, I_ADD, I_AND, I_ARPL,
304 I_BOUND, I_BSF, I_BSR, I_BSWAP, I_BT, I_BTC, I_BTR, I_BTS,
305 I_CALL, I_CBW, I_CDQ, I_CLC, I_CLD, I_CLI, I_CLTS, I_CMC, I_CMP,
306 I_CMPSB, I_CMPSD, I_CMPSW, I_CMPXCHG, I_CMPXCHG486, I_CMPXCHG8B,
307 I_CPUID, I_CWD, I_CWDE, I_DAA, I_DAS, I_DB, I_DD, I_DEC, I_DIV,
308 I_DQ, I_DT, I_DW, I_EMMS, I_ENTER, I_EQU, I_F2XM1, I_FABS,
309 I_FADD, I_FADDP, I_FBLD, I_FBSTP, I_FCHS, I_FCLEX, I_FCMOVB,
310 I_FCMOVBE, I_FCMOVE, I_FCMOVNB, I_FCMOVNBE, I_FCMOVNE,
311 I_FCMOVNU, I_FCMOVU, I_FCOM, I_FCOMI, I_FCOMIP, I_FCOMP,
312 I_FCOMPP, I_FCOS, I_FDECSTP, I_FDISI, I_FDIV, I_FDIVP, I_FDIVR,
313 I_FDIVRP, I_FENI, I_FFREE, I_FIADD, I_FICOM, I_FICOMP, I_FIDIV,
314 I_FIDIVR, I_FILD, I_FIMUL, I_FINCSTP, I_FINIT, I_FIST, I_FISTP,
315 I_FISUB, I_FISUBR, I_FLD, I_FLD1, I_FLDCW, I_FLDENV, I_FLDL2E,
316 I_FLDL2T, I_FLDLG2, I_FLDLN2, I_FLDPI, I_FLDZ, I_FMUL, I_FMULP,
317 I_FNOP, I_FPATAN, I_FPREM, I_FPREM1, I_FPTAN, I_FRNDINT,
318 I_FRSTOR, I_FSAVE, I_FSCALE, I_FSETPM, I_FSIN, I_FSINCOS,
319 I_FSQRT, I_FST, I_FSTCW, I_FSTENV, I_FSTP, I_FSTSW, I_FSUB,
320 I_FSUBP, I_FSUBR, I_FSUBRP, I_FTST, I_FUCOM, I_FUCOMI,
321 I_FUCOMIP, I_FUCOMP, I_FUCOMPP, I_FXAM, I_FXCH, I_FXTRACT,
322 I_FYL2X, I_FYL2XP1, I_HLT, I_IBTS, I_ICEBP, I_IDIV, I_IMUL,
323 I_IN, I_INC, I_INCBIN, I_INSB, I_INSD, I_INSW, I_INT, I_INT1,
324 I_INT01, I_INT3, I_INTO, I_INVD, I_INVLPG, I_IRET, I_IRETD,
325 I_IRETW, I_JCXZ, I_JECXZ, I_JMP, I_LAHF, I_LAR, I_LDS, I_LEA,
326 I_LEAVE, I_LES, I_LFS, I_LGDT, I_LGS, I_LIDT, I_LLDT, I_LMSW,
327 I_LOADALL, I_LOADALL286, I_LODSB, I_LODSD, I_LODSW, I_LOOP,
328 I_LOOPE, I_LOOPNE, I_LOOPNZ, I_LOOPZ, I_LSL, I_LSS, I_LTR,
329 I_MOV, I_MOVD, I_MOVQ, I_MOVSB, I_MOVSD, I_MOVSW, I_MOVSX,
330 I_MOVZX, I_MUL, I_NEG, I_NOP, I_NOT, I_OR, I_OUT, I_OUTSB,
331 I_OUTSD, I_OUTSW, I_PACKSSDW, I_PACKSSWB, I_PACKUSWB, I_PADDB,
332 I_PADDD, I_PADDSB, I_PADDSW, I_PADDUSB, I_PADDUSW, I_PADDW,
333 I_PAND, I_PANDN, I_PCMPEQB, I_PCMPEQD, I_PCMPEQW, I_PCMPGTB,
334 I_PCMPGTD, I_PCMPGTW, I_PMADDWD, I_PMULHW, I_PMULLW, I_POP,
335 I_POPA, I_POPAD, I_POPAW, I_POPF, I_POPFD, I_POPFW, I_POR,
336 I_PSLLD, I_PSLLQ, I_PSLLW, I_PSRAD, I_PSRAW, I_PSRLD, I_PSRLQ,
337 I_PSRLW, I_PSUBB, I_PSUBD, I_PSUBSB, I_PSUBSW, I_PSUBUSB,
338 I_PSUBUSW, I_PSUBW, I_PUNPCKHBW, I_PUNPCKHDQ, I_PUNPCKHWD,
339 I_PUNPCKLBW, I_PUNPCKLDQ, I_PUNPCKLWD, I_PUSH, I_PUSHA,
340 I_PUSHAD, I_PUSHAW, I_PUSHF, I_PUSHFD, I_PUSHFW, I_PXOR, I_RCL,
341 I_RCR, I_RDMSR, I_RDPMC, I_RDTSC, I_RESB, I_RESD, I_RESQ,
342 I_REST, I_RESW, I_RET, I_RETF, I_RETN, I_ROL, I_ROR, I_RSM,
343 I_SAHF, I_SAL, I_SALC, I_SAR, I_SBB, I_SCASB, I_SCASD, I_SCASW,
344 I_SGDT, I_SHL, I_SHLD, I_SHR, I_SHRD, I_SIDT, I_SLDT, I_SMI,
345 I_SMSW, I_STC, I_STD, I_STI, I_STOSB, I_STOSD, I_STOSW, I_STR,
346 I_SUB, I_TEST, I_UMOV, I_VERR, I_VERW, I_WAIT, I_WBINVD,
347 I_WRMSR, I_XADD, I_XBTS, I_XCHG, I_XLATB, I_XOR, I_CMOVcc,
351 enum { /* condition code names */
352 C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
353 C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
354 C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
358 * Note that because segment registers may be used as instruction
359 * prefixes, we must ensure the enumerations for prefixes and
360 * register names do not overlap.
362 enum { /* instruction prefixes */
363 PREFIX_ENUM_START = REG_ENUM_LIMIT,
364 P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
365 P_REPNE, P_REPNZ, P_REPZ, P_TIMES
368 enum { /* extended operand types */
369 EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
372 typedef struct { /* operand to an instruction */
373 long type; /* type of operand */
374 int addr_size; /* 0 means default; 16; 32 */
375 int basereg, indexreg, scale; /* registers and scale involved */
376 long segment; /* immediate segment, if needed */
377 long offset; /* any immediate number */
378 long wrt; /* segment base it's relative to */
381 typedef struct extop { /* extended operand */
382 struct extop *next; /* linked list */
383 long type; /* defined above */
384 char *stringval; /* if it's a string, then here it is */
385 int stringlen; /* ... and here's how long it is */
386 long segment; /* if it's a number/address, then... */
387 long offset; /* ... it's given here ... */
388 long wrt; /* ... and here */
393 typedef struct { /* an instruction itself */
394 char *label; /* the label defined, or NULL */
395 int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
396 int nprefix; /* number of entries in above */
397 int opcode; /* the opcode - not just the string */
398 int condition; /* the condition code, if Jcc/SETcc */
399 int operands; /* how many operands? 0-3 */
400 operand oprs[3]; /* the operands, defined as above */
401 extop *eops; /* extended operands */
402 long times; /* repeat count (TIMES prefix) */
403 int forw_ref; /* is there a forward reference? */
407 * ------------------------------------------------------------
408 * The data structure defining an output format driver, and the
409 * interfaces to the functions therein.
410 * ------------------------------------------------------------
415 * This is a short (one-liner) description of the type of
416 * output generated by the driver.
421 * This is a single keyword used to select the driver.
426 * This procedure is called at the start of an output session.
427 * It tells the output format what file it will be writing to,
428 * what routine to report errors through, and how to interface
429 * to the label manager if necessary. It also gives it a chance
430 * to do other initialisation.
432 void (*init) (FILE *fp, efunc error, ldfunc ldef);
435 * This procedure is called by assemble() to write actual
436 * generated code or data to the object file. Typically it
437 * doesn't have to actually _write_ it, just store it for
440 * The `type' argument specifies the type of output data, and
441 * usually the size as well: its contents are described below.
443 void (*output) (long segto, void *data, unsigned long type,
444 long segment, long wrt);
447 * This procedure is called once for every symbol defined in
448 * the module being assembled. It gives the name and value of
449 * the symbol, in NASM's terms, and indicates whether it has
450 * been declared to be global. Note that the parameter "name",
451 * when passed, will point to a piece of static storage
452 * allocated inside the label manager - it's safe to keep using
453 * that pointer, because the label manager doesn't clean up
454 * until after the output driver has.
456 * Values of `is_global' are: 0 means the symbol is local; 1
457 * means the symbol is global; 2 means the symbol is common (in
458 * which case `offset' holds the _size_ of the variable).
459 * Anything else is available for the output driver to use
462 * This routine explicitly _is_ allowed to call the label
463 * manager to define further symbols, if it wants to, even
464 * though it's been called _from_ the label manager. That much
465 * re-entrancy is guaranteed in the label manager. However, the
466 * label manager will in turn call this routine, so it should
467 * be prepared to be re-entrant itself.
469 void (*symdef) (char *name, long segment, long offset, int is_global);
472 * This procedure is called when the source code requests a
473 * segment change. It should return the corresponding segment
474 * _number_ for the name, or NO_SEG if the name is not a valid
477 * It may also be called with NULL, in which case it is to
478 * return the _default_ section number for starting assembly in.
480 * It is allowed to modify the string it is given a pointer to.
482 * It is also allowed to specify a default instruction size for
483 * the segment, by setting `*bits' to 16 or 32. Or, if it
484 * doesn't wish to define a default, it can leave `bits' alone.
486 long (*section) (char *name, int pass, int *bits);
489 * This procedure is called to modify the segment base values
490 * returned from the SEG operator. It is given a segment base
491 * value (i.e. a segment value with the low bit set), and is
492 * required to produce in return a segment value which may be
493 * different. It can map segment bases to absolute numbers by
494 * means of returning SEG_ABS types.
496 long (*segbase) (long segment);
499 * This procedure is called to allow the output driver to
500 * process its own specific directives. When called, it has the
501 * directive word in `directive' and the parameter string in
502 * `value'. It is called in both assembly passes, and `pass'
503 * will be either 1 or 2.
505 * This procedure should return zero if it does not _recognise_
506 * the directive, so that the main program can report an error.
507 * If it recognises the directive but then has its own errors,
508 * it should report them itself and then return non-zero. It
509 * should also return non-zero if it correctly processes the
512 int (*directive) (char *directive, char *value, int pass);
515 * This procedure is called before anything else - even before
516 * the "init" routine - and is passed the name of the input
517 * file from which this output file is being generated. It
518 * should return its preferred name for the output file in
519 * `outfunc'. Since it is called before the driver is properly
520 * initialised, it has to be passed its error handler
523 * This procedure may also take its own copy of the input file
524 * name for use in writing the output file: it is _guaranteed_
525 * that it will be called before the "init" routine.
527 * The parameter `outname' points to an area of storage
528 * guaranteed to be at least FILENAME_MAX in size.
530 void (*filename) (char *inname, char *outname, efunc error);
533 * This procedure is called after assembly finishes, to allow
534 * the output driver to clean itself up and free its memory.
535 * Typically, it will also be the point at which the object
536 * file actually gets _written_.
538 * One thing the cleanup routine should always do is to close
539 * the output file pointer.
541 void (*cleanup) (void);
545 * values for the `type' parameter to an output function. Each one
546 * must have the actual number of _bytes_ added to it.
548 * Exceptions are OUT_RELxADR, which denote an x-byte relocation
549 * which will be a relative jump. For this we need to know the
550 * distance in bytes from the start of the relocated record until
551 * the end of the containing instruction. _This_ is what is stored
552 * in the size part of the parameter, in this case.
554 * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
555 * and the contents of the "data" parameter is irrelevant.
557 * The "data" parameter for the output function points to a "long",
558 * containing the address in question, unless the type is
559 * OUT_RAWDATA, in which case it points to an "unsigned char"
562 #define OUT_RAWDATA 0x00000000UL
563 #define OUT_ADDRESS 0x10000000UL
564 #define OUT_REL2ADR 0x20000000UL
565 #define OUT_REL4ADR 0x30000000UL
566 #define OUT_RESERVE 0x40000000UL
567 #define OUT_TYPMASK 0xF0000000UL
568 #define OUT_SIZMASK 0x0FFFFFFFUL
577 * This is a useful #define which I keep meaning to use more often:
578 * the number of elements of a statically defined array.
581 #define elements(x) ( sizeof(x) / sizeof(*(x)) )