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