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 #include <stdio.h>
15 
16 #ifndef NULL
17 #define NULL 0
18 #endif
19 
20 #ifndef FALSE
21 #define FALSE 0                /* comes in handy */
22 #endif
23 #ifndef TRUE
24 #define TRUE 1
25 #endif
26 
27 /*
28  * Name pollution problems: <time.h> on Digital UNIX pulls in some
29  * strange hardware header file which sees fit to define R_SP. We
30  * undefine it here so as not to break the enum below.
31  */
32 #ifdef R_SP
33 #undef R_SP
34 #endif
35 
36 /*
37  * Special values for expr->type. ASSUMPTION MADE HERE: the number
38  * of distinct register names (i.e. possible "type" fields for an
39  * expr structure) does not exceed 124 (EXPR_REG_START through
40  * EXPR_REG_END).
41  */
42 #define EXPR_REG_START 1
43 
44 /*
45  * Here we define the operand types. These are implemented as bit
46  * masks, since some are subsets of others; e.g. AX in a MOV
47  * instruction is a special operand type, whereas AX in other
48  * contexts is just another 16-bit register. (Also, consider CL in
49  * shift instructions, DX in OUT, etc.)
50  */
51 
52 /* size, and other attributes, of the operand */
53 #define BITS8     0x00000001L
54 #define BITS16    0x00000002L
55 #define BITS32    0x00000004L
56 #define BITS64    0x00000008L          /* FPU only */
57 #define BITS80    0x00000010L          /* FPU only */
58 #define FAR       0x00000020L          /* grotty: this means 16:16 or */
59                        /* 16:32, like in CALL/JMP */
60 #define NEAR      0x00000040L
61 #define SHORT     0x00000080L          /* and this means what it says :) */
62 
63 #define SIZE_MASK 0x000000FFL          /* all the size attributes */
64 #define NON_SIZE  (~SIZE_MASK)
65 
66 #define TO        0x00000100L          /* reverse effect in FADD, FSUB &c */
67 #define COLON     0x00000200L          /* operand is followed by a colon */
68 
69 /* type of operand: memory reference, register, etc. */
70 #define MEMORY    0x00204000L
71 #define REGISTER  0x00001000L          /* register number in 'basereg' */
72 #define IMMEDIATE 0x00002000L
73 
74 #define REGMEM    0x00200000L          /* for r/m, ie EA, operands */
75 #define REGNORM   0x00201000L          /* 'normal' reg, qualifies as EA */
76 #define REG8      0x00201001L
77 #define REG16     0x00201002L
78 #define REG32     0x00201004L
79 #define MMXREG    0x00201008L          /* MMX registers */
80 #define XMMREG    0x00201010L          /* XMM Katmai reg */
81 #define FPUREG    0x01000000L          /* floating point stack registers */
82 #define FPU0      0x01000800L          /* FPU stack register zero */
83 
84 /* special register operands: these may be treated differently */
85 #define REG_SMASK 0x00070000L          /* a mask for the following */
86 #define REG_ACCUM 0x00211000L          /* accumulator: AL, AX or EAX */
87 #define REG_AL    0x00211001L          /* REG_ACCUM | BITSxx */
88 #define REG_AX    0x00211002L          /* ditto */
89 #define REG_EAX   0x00211004L          /* and again */
90 #define REG_COUNT 0x00221000L          /* counter: CL, CX or ECX */
91 #define REG_CL    0x00221001L          /* REG_COUNT | BITSxx */
92 #define REG_CX    0x00221002L          /* ditto */
93 #define REG_ECX   0x00221004L          /* another one */
94 #define REG_DL    0x00241001L
95 #define REG_DX    0x00241002L
96 #define REG_EDX   0x00241004L
97 #define REG_SREG  0x00081002L          /* any segment register */
98 #define REG_CS    0x01081002L          /* CS */
99 #define REG_DESS  0x02081002L          /* DS, ES, SS (non-CS 86 registers) */
100 #define REG_FSGS  0x04081002L          /* FS, GS (386 extended registers) */
101 #define REG_SEG67 0x08081002L          /* Non-implemented segment registers */
102 #define REG_CDT   0x00101004L          /* CRn, DRn and TRn */
103 #define REG_CREG  0x08101004L          /* CRn */
104 #define REG_DREG  0x10101004L          /* DRn */
105 #define REG_TREG  0x20101004L          /* TRn */
106 
107 /* special type of EA */
108 #define MEM_OFFS  0x00604000L          /* simple [address] offset */
109 
110 /* special type of immediate operand */
111 #define ONENESS   0x00800000L          /* so UNITY == IMMEDIATE | ONENESS */
112 #define UNITY     0x00802000L          /* for shift/rotate instructions */
113 #define BYTENESS  0x40000000L          /* so SBYTE == IMMEDIATE | BYTENESS */
114 #define SBYTE     0x40002000L          /* for op r16/32,immediate instrs. */
115 
116 /* Register names automatically generated from regs.dat */
117 /* automatically generated from ./regs.dat - do not edit */
118 enum reg_enum {
119     R_AH = EXPR_REG_START,
120     R_AL,
121     R_AX,
122     R_BH,
123     R_BL,
124     R_BP,
125     R_BX,
126     R_CH,
127     R_CL,
128     R_CR0,
129     R_CR1,
130     R_CR2,
131     R_CR3,
132     R_CR4,
133     R_CR5,
134     R_CR6,
135     R_CR7,
136     R_CS,
137     R_CX,
138     R_DH,
139     R_DI,
140     R_DL,
141     R_DR0,
142     R_DR1,
143     R_DR2,
144     R_DR3,
145     R_DR4,
146     R_DR5,
147     R_DR6,
148     R_DR7,
149     R_DS,
150     R_DX,
151     R_EAX,
152     R_EBP,
153     R_EBX,
154     R_ECX,
155     R_EDI,
156     R_EDX,
157     R_ES,
158     R_ESI,
159     R_ESP,
160     R_FS,
161     R_GS,
162     R_MM0,
163     R_MM1,
164     R_MM2,
165     R_MM3,
166     R_MM4,
167     R_MM5,
168     R_MM6,
169     R_MM7,
170     R_SEGR6,
171     R_SEGR7,
172     R_SI,
173     R_SP,
174     R_SS,
175     R_ST0,
176     R_ST1,
177     R_ST2,
178     R_ST3,
179     R_ST4,
180     R_ST5,
181     R_ST6,
182     R_ST7,
183     R_TR0,
184     R_TR1,
185     R_TR2,
186     R_TR3,
187     R_TR4,
188     R_TR5,
189     R_TR6,
190     R_TR7,
191     R_XMM0,
192     R_XMM1,
193     R_XMM2,
194     R_XMM3,
195     R_XMM4,
196     R_XMM5,
197     R_XMM6,
198     R_XMM7,
199     REG_ENUM_LIMIT
200 };
201 
202 enum {                     /* condition code names */
203     C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
204     C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
205     C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
206 };
207 
208 /*
209  * Note that because segment registers may be used as instruction
210  * prefixes, we must ensure the enumerations for prefixes and
211  * register names do not overlap.
212  */
213 enum {                     /* instruction prefixes */
214     PREFIX_ENUM_START = REG_ENUM_LIMIT,
215     P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
216     P_REPNE, P_REPNZ, P_REPZ, P_TIMES
217 };
218 
219 enum {                     /* extended operand types */
220     EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
221 };
222 
223 enum {                          /* special EA flags */
224     EAF_BYTEOFFS = 1,           /* force offset part to byte size */
225     EAF_WORDOFFS = 2,           /* force offset part to [d]word size */
226     EAF_TIMESTWO = 4            /* really do EAX*2 not EAX+EAX */
227 };
228 
229 enum {                          /* values for `hinttype' */
230     EAH_NOHINT = 0,             /* no hint at all - our discretion */
231     EAH_MAKEBASE = 1,           /* try to make given reg the base */
232     EAH_NOTBASE = 2             /* try _not_ to make reg the base */
233 };
234 
235 typedef struct {                /* operand to an instruction */
236     long type;                  /* type of operand */
237     int addr_size;              /* 0 means default; 16; 32 */
238     int basereg, indexreg, scale;   /* registers and scale involved */
239     int hintbase, hinttype;     /* hint as to real base register */
240     long segment;               /* immediate segment, if needed */
241     long offset;                /* any immediate number */
242     long wrt;                   /* segment base it's relative to */
243     int eaflags;                /* special EA flags */
244     int opflags;                /* see OPFLAG_* defines below */
245 } operand;
246 
247 #define OPFLAG_FORWARD      1   /* operand is a forward reference */
248 #define OPFLAG_EXTERN       2   /* operand is an external reference */
249 
250 typedef struct extop {          /* extended operand */
251     struct extop *next;         /* linked list */
252     long type;                  /* defined above */
253     char *stringval;            /* if it's a string, then here it is */
254     int stringlen;              /* ... and here's how long it is */
255     long segment;               /* if it's a number/address, then... */
256     long offset;                /* ... it's given here ... */
257     long wrt;                   /* ... and here */
258 } extop;
259 
260 #define MAXPREFIX 4
261 
262 typedef struct {                /* an instruction itself */
263     char *label;                /* the label defined, or NULL */
264     int prefixes[MAXPREFIX];    /* instruction prefixes, if any */
265     int nprefix;                /* number of entries in above */
266     int opcode;                 /* the opcode - not just the string */
267     int condition;              /* the condition code, if Jcc/SETcc */
268     int operands;               /* how many operands? 0-3
269                                     * (more if db et al) */
270     operand oprs[3];            /* the operands, defined as above */
271     extop *eops;                /* extended operands */
272     int eops_float;             /* true if DD and floating */
273     long times;                 /* repeat count (TIMES prefix) */
274     int forw_ref;               /* is there a forward reference? */
275 } insn;
276 
277 enum geninfo { GI_SWITCH };
278 
279 /*
280  * values for the `type' parameter to an output function. Each one
281  * must have the actual number of _bytes_ added to it.
282  *
283  * Exceptions are OUT_RELxADR, which denote an x-byte relocation
284  * which will be a relative jump. For this we need to know the
285  * distance in bytes from the start of the relocated record until
286  * the end of the containing instruction. _This_ is what is stored
287  * in the size part of the parameter, in this case.
288  *
289  * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
290  * and the contents of the "data" parameter is irrelevant.
291  *
292  * The "data" parameter for the output function points to a "long",
293  * containing the address in question, unless the type is
294  * OUT_RAWDATA, in which case it points to an "unsigned char"
295  * array.
296  */
297 #define OUT_RAWDATA 0x00000000UL
298 #define OUT_ADDRESS 0x10000000UL
299 #define OUT_REL2ADR 0x20000000UL
300 #define OUT_REL4ADR 0x30000000UL
301 #define OUT_RESERVE 0x40000000UL
302 #define OUT_TYPMASK 0xF0000000UL
303 #define OUT_SIZMASK 0x0FFFFFFFUL
304 
305 /*
306  * The type definition macros
307  * for debugging
308  *
309  * low 3 bits: reserved
310  * next 5 bits: type
311  * next 24 bits: number of elements for arrays (0 for labels)
312  */
313 
314 #define TY_UNKNOWN 0x00
315 #define TY_LABEL   0x08
316 #define TY_BYTE    0x10
317 #define TY_WORD    0x18
318 #define TY_DWORD   0x20
319 #define TY_FLOAT   0x28
320 #define TY_QWORD   0x30
321 #define TY_TBYTE   0x38
322 #define TY_COMMON  0xE0
323 #define TY_SEG     0xE8
324 #define TY_EXTERN  0xF0
325 #define TY_EQU     0xF8
326 
327 #define TYM_TYPE(x) ((x) & 0xF8)
328 #define TYM_ELEMENTS(x) (((x) & 0xFFFFFF00) >> 8)
329 
330 #define TYS_ELEMENTS(x)  ((x) << 8)
331 /*
332  * -----
333  * Other
334  * -----
335  */
336 
337 /*
338  * This is a useful #define which I keep meaning to use more often:
339  * the number of elements of a statically defined array.
340  */
341 
342 #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
343 
344 extern int tasm_compatible_mode;
345 
346 /*
347  * This declaration passes the "pass" number to all other modules
348  * "pass0" assumes the values: 0, 0, ..., 0, 1, 2
349  * where 0 = optimizing pass
350  *       1 = pass 1
351  *       2 = pass 2
352  */
353 
354 extern int pass0;   /* this is globally known */
355 extern int optimizing;
356 
357 #endif
358