NASM 0.93
[platform/upstream/nasm.git] / nasm.h
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
11 #ifndef NASM_H
12 #define NASM_H
13
14 #define NASM_MAJOR_VER 0
15 #define NASM_MINOR_VER 93
16 #define NASM_VER "0.93"
17
18 #ifndef NULL
19 #define NULL 0
20 #endif
21
22 #ifndef FALSE
23 #define FALSE 0                        /* comes in handy */
24 #endif
25 #ifndef TRUE
26 #define TRUE 1
27 #endif
28
29 #define NO_SEG -1L                     /* null segment value */
30 #define SEG_ABS 0x40000000L            /* mask for far-absolute segments */
31
32 #ifndef FILENAME_MAX
33 #define FILENAME_MAX 256
34 #endif
35
36 /*
37  * We must declare the existence of this structure type up here,
38  * since we have to reference it before we define it...
39  */
40 struct ofmt;
41
42 /*
43  * -------------------------
44  * Error reporting functions
45  * -------------------------
46  */
47
48 /*
49  * An error reporting function should look like this.
50  */
51 typedef void (*efunc) (int severity, char *fmt, ...);
52
53 /*
54  * These are the error severity codes which get passed as the first
55  * argument to an efunc.
56  */
57
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
67 /*
68  * -----------------------
69  * Other function typedefs
70  * -----------------------
71  */
72
73 /*
74  * A label-lookup function should look like this.
75  */
76 typedef int (*lfunc) (char *label, long *segment, long *offset);
77
78 /*
79  * And a label-definition function like this.
80  */
81 typedef void (*ldfunc) (char *label, long segment, long offset,
82                         struct ofmt *ofmt, efunc error);
83
84 /*
85  * -----------------------------------------------------------
86  * Format of the `insn' structure returned from `parser.c' and
87  * passed into `assemble.c'
88  * -----------------------------------------------------------
89  */
90
91 /*
92  * Here we define the operand types. These are implemented as bit
93  * masks, since some are subsets of others; e.g. AX in a MOV
94  * instruction is a special operand type, whereas AX in other
95  * contexts is just another 16-bit register. (Also, consider CL in
96  * shift instructions, DX in OUT, etc.)
97  */
98
99 /* size, and other attributes, of the operand */
100 #define BITS8     0x00000001L
101 #define BITS16    0x00000002L
102 #define BITS32    0x00000004L
103 #define BITS64    0x00000008L          /* FPU only */
104 #define BITS80    0x00000010L          /* FPU only */
105 #define FAR       0x00000020L          /* grotty: this means 16:16 or */
106                                        /* 16:32, like in CALL/JMP */
107 #define NEAR      0x00000040L
108 #define SHORT     0x00000080L          /* and this means what it says :) */
109
110 #define SIZE_MASK 0x000000FFL          /* all the size attributes */
111 #define NON_SIZE  (~SIZE_MASK)
112
113 #define TO        0x00000100L          /* reverse effect in FADD, FSUB &c */
114 #define COLON     0x00000200L          /* operand is followed by a colon */
115
116 /* type of operand: memory reference, register, etc. */
117 #define MEMORY    0x00204000L
118 #define REGISTER  0x00001000L          /* register number in 'basereg' */
119 #define IMMEDIATE 0x00002000L
120
121 #define REGMEM    0x00200000L          /* for r/m, ie EA, operands */
122 #define REGNORM   0x00201000L          /* 'normal' reg, qualifies as EA */
123 #define REG8      0x00201001L
124 #define REG16     0x00201002L
125 #define REG32     0x00201004L
126 #define FPUREG    0x01000000L          /* floating point stack registers */
127 #define FPU0      0x01000800L          /* FPU stack register zero */
128 #define MMXREG    0x00001008L          /* MMX registers */
129
130 /* special register operands: these may be treated differently */
131 #define REG_SMASK 0x00070000L          /* a mask for the following */
132 #define REG_ACCUM 0x00211000L          /* accumulator: AL, AX or EAX */
133 #define REG_AL    0x00211001L          /* REG_ACCUM | BITSxx */
134 #define REG_AX    0x00211002L          /* ditto */
135 #define REG_EAX   0x00211004L          /* and again */
136 #define REG_COUNT 0x00221000L          /* counter: CL, CX or ECX */
137 #define REG_CL    0x00221001L          /* REG_COUNT | BITSxx */
138 #define REG_CX    0x00221002L          /* ditto */
139 #define REG_ECX   0x00221004L          /* another one */
140 #define REG_DX    0x00241002L
141 #define REG_SREG  0x00081002L          /* any segment register */
142 #define REG_CS    0x01081002L          /* CS */
143 #define REG_DESS  0x02081002L          /* DS, ES, SS (non-CS 86 registers) */
144 #define REG_FSGS  0x04081002L          /* FS, GS (386 extended registers) */
145 #define REG_CDT   0x00101004L          /* CRn, DRn and TRn */
146 #define REG_CREG  0x08101004L          /* CRn */
147 #define REG_CR4   0x08101404L          /* CR4 (Pentium only) */
148 #define REG_DREG  0x10101004L          /* DRn */
149 #define REG_TREG  0x20101004L          /* TRn */
150
151 /* special type of EA */
152 #define MEM_OFFS  0x00604000L          /* simple [address] offset */
153
154 /* special type of immediate operand */
155 #define UNITY     0x00802000L          /* for shift/rotate instructions */
156
157 /*
158  * Next, the codes returned from the parser, for registers and
159  * instructions.
160  */
161
162 enum {                                 /* register names */
163     R_AH = 1, R_AL, R_AX, R_BH, R_BL, R_BP, R_BX, R_CH, R_CL, R_CR0,
164     R_CR2, R_CR3, R_CR4, R_CS, R_CX, R_DH, R_DI, R_DL, R_DR0, R_DR1,
165     R_DR2, R_DR3, R_DR6, R_DR7, R_DS, R_DX, R_EAX, R_EBP, R_EBX,
166     R_ECX, R_EDI, R_EDX, R_ES, R_ESI, R_ESP, R_FS, R_GS, R_MM0,
167     R_MM1, R_MM2, R_MM3, R_MM4, R_MM5, R_MM6, R_MM7, R_SI, R_SP,
168     R_SS, R_ST0, R_ST1, R_ST2, R_ST3, R_ST4, R_ST5, R_ST6, R_ST7,
169     R_TR3, R_TR4, R_TR5, R_TR6, R_TR7, REG_ENUM_LIMIT
170 };
171
172 enum {                                 /* instruction names */
173     I_AAA, I_AAD, I_AAM, I_AAS, I_ADC, I_ADD, I_AND, I_ARPL,
174     I_BOUND, I_BSF, I_BSR, I_BSWAP, I_BT, I_BTC, I_BTR, I_BTS,
175     I_CALL, I_CBW, I_CDQ, I_CLC, I_CLD, I_CLI, I_CLTS, I_CMC, I_CMP,
176     I_CMPSB, I_CMPSD, I_CMPSW, I_CMPXCHG, I_CMPXCHG8B, I_CPUID,
177     I_CWD, I_CWDE, I_DAA, I_DAS, I_DB, I_DD, I_DEC, I_DIV, I_DQ,
178     I_DT, I_DW, I_EMMS, I_ENTER, I_EQU, I_F2XM1, I_FABS, I_FADD,
179     I_FADDP, I_FBLD, I_FBSTP, I_FCHS, I_FCLEX, I_FCMOVB, I_FCMOVBE,
180     I_FCMOVE, I_FCMOVNB, I_FCMOVNBE, I_FCMOVNE, I_FCMOVNU, I_FCMOVU,
181     I_FCOM, I_FCOMI, I_FCOMIP, I_FCOMP, I_FCOMPP, I_FCOS, I_FDECSTP,
182     I_FDISI, I_FDIV, I_FDIVP, I_FDIVR, I_FDIVRP, I_FENI, I_FFREE,
183     I_FIADD, I_FICOM, I_FICOMP, I_FIDIV, I_FIDIVR, I_FILD, I_FIMUL,
184     I_FINCSTP, I_FINIT, I_FIST, I_FISTP, I_FISUB, I_FISUBR, I_FLD,
185     I_FLD1, I_FLDCW, I_FLDENV, I_FLDL2E, I_FLDL2T, I_FLDLG2,
186     I_FLDLN2, I_FLDPI, I_FLDZ, I_FMUL, I_FMULP, I_FNOP, I_FPATAN,
187     I_FPREM, I_FPREM1, I_FPTAN, I_FRNDINT, I_FRSTOR, I_FSAVE,
188     I_FSCALE, I_FSETPM, I_FSIN, I_FSINCOS, I_FSQRT, I_FST, I_FSTCW,
189     I_FSTENV, I_FSTP, I_FSTSW, I_FSUB, I_FSUBP, I_FSUBR, I_FSUBRP,
190     I_FTST, I_FUCOM, I_FUCOMI, I_FUCOMIP, I_FUCOMP, I_FUCOMPP,
191     I_FXAM, I_FXCH, I_FXTRACT, I_FYL2X, I_FYL2XP1, I_HLT, I_ICEBP,
192     I_IDIV, I_IMUL, I_IN, I_INC, I_INSB, I_INSD, I_INSW, I_INT,
193     I_INT1, I_INT01, I_INT3, I_INTO, I_INVD, I_INVLPG, I_IRET,
194     I_IRETD, I_IRETW, I_JCXZ, I_JECXZ, I_JMP, I_LAHF, I_LAR, I_LDS,
195     I_LEA, I_LEAVE, I_LES, I_LFS, I_LGDT, I_LGS, I_LIDT, I_LLDT,
196     I_LMSW, I_LOADALL, I_LODSB, I_LODSD, I_LODSW, I_LOOP, I_LOOPE,
197     I_LOOPNE, I_LOOPNZ, I_LOOPZ, I_LSL, I_LSS, I_LTR, I_MOV, I_MOVD,
198     I_MOVQ, I_MOVSB, I_MOVSD, I_MOVSW, I_MOVSX, I_MOVZX, I_MUL,
199     I_NEG, I_NOP, I_NOT, I_OR, I_OUT, I_OUTSB, I_OUTSD, I_OUTSW,
200     I_PACKSSDW, I_PACKSSWB, I_PACKUSWB, I_PADDB, I_PADDD, I_PADDSB,
201     I_PADDSW, I_PADDUSB, I_PADDUSW, I_PADDW, I_PAND, I_PANDN,
202     I_PCMPEQB, I_PCMPEQD, I_PCMPEQW, I_PCMPGTB, I_PCMPGTD,
203     I_PCMPGTW, I_PMADDWD, I_PMULHW, I_PMULLW, I_POP, I_POPA,
204     I_POPAD, I_POPAW, I_POPF, I_POPFD, I_POPFW, I_POR, I_PSLLD,
205     I_PSLLQ, I_PSLLW, I_PSRAD, I_PSRAW, I_PSRLD, I_PSRLQ, I_PSRLW,
206     I_PSUBB, I_PSUBD, I_PSUBSB, I_PSUBSW, I_PSUBUSB, I_PSUBUSW,
207     I_PSUBW, I_PUNPCKHBW, I_PUNPCKHDQ, I_PUNPCKHWD, I_PUNPCKLBW,
208     I_PUNPCKLDQ, I_PUNPCKLWD, I_PUSH, I_PUSHA, I_PUSHAD, I_PUSHAW,
209     I_PUSHF, I_PUSHFD, I_PUSHFW, I_PXOR, I_RCL, I_RCR, I_RDMSR,
210     I_RDPMC, I_RDTSC, I_RESB, I_RESD, I_RESQ, I_REST, I_RESW, I_RET,
211     I_RETF, I_RETN, I_ROL, I_ROR, I_RSM, I_SAHF, I_SAL, I_SALC,
212     I_SAR, I_SBB, I_SCASB, I_SCASD, I_SCASW, I_SGDT, I_SHL, I_SHLD,
213     I_SHR, I_SHRD, I_SIDT, I_SLDT, I_SMSW, I_STC, I_STD, I_STI,
214     I_STOSB, I_STOSD, I_STOSW, I_STR, I_SUB, I_TEST, I_UMOV, I_VERR,
215     I_VERW, I_WAIT, I_WBINVD, I_WRMSR, I_XADD, I_XCHG, I_XLATB,
216     I_XOR, I_CMOVcc, I_Jcc, I_SETcc
217 };
218
219 enum {                                 /* condition code names */
220     C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
221     C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
222     C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
223 };
224
225 /*
226  * Note that because segment registers may be used as instruction
227  * prefixes, we must ensure the enumerations for prefixes and
228  * register names do not overlap.
229  */
230 enum {                                 /* instruction prefixes */
231     PREFIX_ENUM_START = REG_ENUM_LIMIT,
232     P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
233     P_REPNE, P_REPNZ, P_REPZ, P_TIMES
234 };
235
236 enum {                                 /* extended operand types */
237     EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
238 };
239
240 typedef struct {                       /* operand to an instruction */
241     long type;                         /* type of operand */
242     int addr_size;                     /* 0 means default; 16; 32 */
243     int basereg, indexreg, scale;      /* registers and scale involved */
244     long segment;                      /* immediate segment, if needed */
245     long offset;                       /* any immediate number */
246     long wrt;                          /* segment base it's relative to */
247 } operand;
248
249 typedef struct extop {                 /* extended operand */
250     struct extop *next;                /* linked list */
251     long type;                         /* defined above */
252     char *stringval;                   /* if it's a string, then here it is */
253     int stringlen;                     /* ... and here's how long it is */
254     long segment;                      /* if it's a number/address, then... */
255     long offset;                       /* ... it's given here ... */
256     long wrt;                          /* ... and here */
257 } extop;
258
259 #define MAXPREFIX 4
260
261 typedef struct {                       /* an instruction itself */
262     char *label;                       /* the label defined, or NULL */
263     int prefixes[MAXPREFIX];           /* instruction prefixes, if any */
264     int nprefix;                       /* number of entries in above */
265     int opcode;                        /* the opcode - not just the string */
266     int condition;                     /* the condition code, if Jcc/SETcc */
267     int operands;                      /* how many operands? 0-3 */
268     operand oprs[3];                   /* the operands, defined as above */
269     extop *eops;                       /* extended operands */
270     int times;                         /* repeat count (TIMES prefix) */
271     int forw_ref;                      /* is there a forward reference? */
272 } insn;
273
274 /*
275  * ------------------------------------------------------------
276  * The data structure defining an output format driver, and the
277  * interfaces to the functions therein.
278  * ------------------------------------------------------------
279  */
280
281 struct ofmt {
282     /*
283      * This is a short (one-liner) description of the type of
284      * output generated by the driver.
285      */
286     char *fullname;
287
288     /*
289      * This is a single keyword used to select the driver.
290      */
291     char *shortname;
292
293     /*
294      * This procedure is called at the start of an output session.
295      * It tells the output format what file it will be writing to,
296      * what routine to report errors through, and how to interface
297      * to the label manager if necessary. It also gives it a chance
298      * to do other initialisation.
299      */
300     void (*init) (FILE *fp, efunc error, ldfunc ldef);
301
302     /*
303      * This procedure is called by assemble() to write actual
304      * generated code or data to the object file. Typically it
305      * doesn't have to actually _write_ it, just store it for
306      * later.
307      *
308      * The `type' argument specifies the type of output data, and
309      * usually the size as well: its contents are described below.
310      */
311     void (*output) (long segto, void *data, unsigned long type,
312                     long segment, long wrt);
313
314     /*
315      * This procedure is called once for every symbol defined in
316      * the module being assembled. It gives the name and value of
317      * the symbol, in NASM's terms, and indicates whether it has
318      * been declared to be global. Note that the parameter "name",
319      * when passed, will point to a piece of static storage
320      * allocated inside the label manager - it's safe to keep using
321      * that pointer, because the label manager doesn't clean up
322      * until after the output driver has.
323      *
324      * Values of `is_global' are: 0 means the symbol is local; 1
325      * means the symbol is global; 2 means the symbol is common (in
326      * which case `offset' holds the _size_ of the variable).
327      * Anything else is available for the output driver to use
328      * internally.
329      */
330     void (*symdef) (char *name, long segment, long offset, int is_global);
331
332     /*
333      * This procedure is called when the source code requests a
334      * segment change. It should return the corresponding segment
335      * _number_ for the name, or NO_SEG if the name is not a valid
336      * segment name.
337      *
338      * It may also be called with NULL, in which case it is to
339      * return the _default_ section number for starting assembly in.
340      *
341      * It is allowed to modify the string it is given a pointer to.
342      *
343      * It is also allowed to specify a default instruction size for
344      * the segment, by setting `*bits' to 16 or 32. Or, if it
345      * doesn't wish to define a default, it can leave `bits' alone.
346      */
347     long (*section) (char *name, int pass, int *bits);
348
349     /*
350      * This procedure is called to modify the segment base values
351      * returned from the SEG operator. It is given a segment base
352      * value (i.e. a segment value with the low bit set), and is
353      * required to produce in return a segment value which may be
354      * different. It can map segment bases to absolute numbers by
355      * means of returning SEG_ABS types.
356      */
357     long (*segbase) (long segment);
358
359     /*
360      * This procedure is called to allow the output driver to
361      * process its own specific directives. When called, it has the
362      * directive word in `directive' and the parameter string in
363      * `value'. It is called in both assembly passes, and `pass'
364      * will be either 1 or 2.
365      *
366      * This procedure should return zero if it does not _recognise_
367      * the directive, so that the main program can report an error.
368      * If it recognises the directive but then has its own errors,
369      * it should report them itself and then return non-zero. It
370      * should also return non-zero if it correctly processes the
371      * directive.
372      */
373     int (*directive) (char *directive, char *value, int pass);
374
375     /*
376      * This procedure is called before anything else - even before
377      * the "init" routine - and is passed the name of the input
378      * file from which this output file is being generated. It
379      * should return its preferred name for the output file in
380      * `outfunc'. Since it is called before the driver is properly
381      * initialised, it has to be passed its error handler
382      * separately.
383      *
384      * This procedure may also take its own copy of the input file
385      * name for use in writing the output file: it is _guaranteed_
386      * that it will be called before the "init" routine.
387      *
388      * The parameter `outname' points to an area of storage
389      * guaranteed to be at least FILENAME_MAX in size.
390      */
391     void (*filename) (char *inname, char *outname, efunc error);
392
393     /*
394      * This procedure is called after assembly finishes, to allow
395      * the output driver to clean itself up and free its memory.
396      * Typically, it will also be the point at which the object
397      * file actually gets _written_.
398      *
399      * One thing the cleanup routine should always do is to close
400      * the output file pointer.
401      */
402     void (*cleanup) (void);
403 };
404
405 /*
406  * values for the `type' parameter to an output function. Each one
407  * must have the actual number of _bytes_ added to it.
408  *
409  * Exceptions are OUT_RELxADR, which denote an x-byte relocation
410  * which will be a relative jump. For this we need to know the
411  * distance in bytes from the start of the relocated record until
412  * the end of the containing instruction. _This_ is what is stored
413  * in the size part of the parameter, in this case.
414  *
415  * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
416  * and the contents of the "data" parameter is irrelevant.
417  *
418  * The "data" parameter for the output function points to a "long",
419  * containing the address in question, unless the type is
420  * OUT_RAWDATA, in which case it points to an "unsigned char"
421  * array.
422  */
423 #define OUT_RAWDATA 0x00000000UL
424 #define OUT_ADDRESS 0x10000000UL
425 #define OUT_REL2ADR 0x20000000UL
426 #define OUT_REL4ADR 0x30000000UL
427 #define OUT_RESERVE 0x40000000UL
428 #define OUT_TYPMASK 0xF0000000UL
429 #define OUT_SIZMASK 0x0FFFFFFFUL
430
431 /*
432  * -----
433  * Other
434  * -----
435  */
436
437 /*
438  * This is a useful #define which I keep meaning to use more often:
439  * the number of elements of a statically defined array.
440  */
441
442 #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
443
444 #endif