]> git.saurik.com Git - apple/boot.git/blob - i386/nasm/nasm.h
36b3a59bc3de483d8fd0c07d71cae0cb11b8ed67
[apple/boot.git] / i386 / nasm / nasm.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* nasm.h main header file for the Netwide Assembler: inter-module interface
26 *
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
31 *
32 * initial version: 27/iii/95 by Simon Tatham
33 */
34
35 #ifndef NASM_NASM_H
36 #define NASM_NASM_H
37
38 #define NASM_MAJOR_VER 0
39 #define NASM_MINOR_VER 97
40 #define NASM_VER "0.97"
41
42 #ifndef NULL
43 #define NULL 0
44 #endif
45
46 #ifndef FALSE
47 #define FALSE 0 /* comes in handy */
48 #endif
49 #ifndef TRUE
50 #define TRUE 1
51 #endif
52
53 #define NO_SEG -1L /* null segment value */
54 #define SEG_ABS 0x40000000L /* mask for far-absolute segments */
55
56 #ifndef FILENAME_MAX
57 #define FILENAME_MAX 256
58 #endif
59
60 /*
61 * Name pollution problems: <time.h> on Digital UNIX pulls in some
62 * strange hardware header file which sees fit to define R_SP. We
63 * undefine it here so as not to break the enum below.
64 */
65 #ifdef R_SP
66 #undef R_SP
67 #endif
68
69 /*
70 * We must declare the existence of this structure type up here,
71 * since we have to reference it before we define it...
72 */
73 struct ofmt;
74
75 /*
76 * -------------------------
77 * Error reporting functions
78 * -------------------------
79 */
80
81 /*
82 * An error reporting function should look like this.
83 */
84 typedef void (*efunc) (int severity, char *fmt, ...);
85
86 /*
87 * These are the error severity codes which get passed as the first
88 * argument to an efunc.
89 */
90
91 #define ERR_WARNING 0 /* warn only: no further action */
92 #define ERR_NONFATAL 1 /* terminate assembly after phase */
93 #define ERR_FATAL 2 /* instantly fatal: exit with error */
94 #define ERR_PANIC 3 /* internal error: panic instantly
95 * and dump core for reference */
96 #define ERR_MASK 0x0F /* mask off the above codes */
97 #define ERR_NOFILE 0x10 /* don't give source file name/line */
98 #define ERR_USAGE 0x20 /* print a usage message */
99 #define ERR_OFFBY1 0x40 /* report error as being on the line
100 * we're just _about_ to read, not
101 * the one we've just read */
102 #define ERR_PASS1 0x80 /* only print this error on pass one */
103
104 /*
105 * These codes define specific types of suppressible warning.
106 */
107 #define ERR_WARN_MNP 0x0100 /* macro-num-parameters warning */
108 #define ERR_WARN_OL 0x0200 /* orphan label (no colon, and
109 * alone on line) */
110 #define ERR_WARN_NOV 0x0300 /* numeric overflow */
111 #define ERR_WARN_MASK 0xFF00 /* the mask for this feature */
112 #define ERR_WARN_SHR 8 /* how far to shift right */
113 #define ERR_WARN_MAX 3 /* the highest numbered one */
114
115 /*
116 * -----------------------
117 * Other function typedefs
118 * -----------------------
119 */
120
121 /*
122 * A label-lookup function should look like this.
123 */
124 typedef int (*lfunc) (char *label, long *segment, long *offset);
125
126 /*
127 * And a label-definition function like this. The boolean parameter
128 * `is_norm' states whether the label is a `normal' label (which
129 * should affect the local-label system), or something odder like
130 * an EQU or a segment-base symbol, which shouldn't.
131 */
132 typedef void (*ldfunc) (char *label, long segment, long offset, char *special,
133 int is_norm, int isextrn, struct ofmt *ofmt,
134 efunc error);
135
136 /*
137 * List-file generators should look like this:
138 */
139 typedef struct {
140 /*
141 * Called to initialise the listing file generator. Before this
142 * is called, the other routines will silently do nothing when
143 * called. The `char *' parameter is the file name to write the
144 * listing to.
145 */
146 void (*init) (char *, efunc);
147
148 /*
149 * Called to clear stuff up and close the listing file.
150 */
151 void (*cleanup) (void);
152
153 /*
154 * Called to output binary data. Parameters are: the offset;
155 * the data; the data type. Data types are similar to the
156 * output-format interface, only OUT_ADDRESS will _always_ be
157 * displayed as if it's relocatable, so ensure that any non-
158 * relocatable address has been converted to OUT_RAWDATA by
159 * then. Note that OUT_RAWDATA+0 is a valid data type, and is a
160 * dummy call used to give the listing generator an offset to
161 * work with when doing things like uplevel(LIST_TIMES) or
162 * uplevel(LIST_INCBIN).
163 */
164 void (*output) (long, void *, unsigned long);
165
166 /*
167 * Called to send a text line to the listing generator. The
168 * `int' parameter is LIST_READ or LIST_MACRO depending on
169 * whether the line came directly from an input file or is the
170 * result of a multi-line macro expansion.
171 */
172 void (*line) (int, char *);
173
174 /*
175 * Called to change one of the various levelled mechanisms in
176 * the listing generator. LIST_INCLUDE and LIST_MACRO can be
177 * used to increase the nesting level of include files and
178 * macro expansions; LIST_TIMES and LIST_INCBIN switch on the
179 * two binary-output-suppression mechanisms for large-scale
180 * pseudo-instructions.
181 *
182 * LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
183 * it indicates the beginning of the expansion of a `nolist'
184 * macro, so anything under that level won't be expanded unless
185 * it includes another file.
186 */
187 void (*uplevel) (int);
188
189 /*
190 * Reverse the effects of uplevel.
191 */
192 void (*downlevel) (int);
193 } ListGen;
194
195 /*
196 * The expression evaluator must be passed a scanner function; a
197 * standard scanner is provided as part of nasmlib.c. The
198 * preprocessor will use a different one. Scanners, and the
199 * token-value structures they return, look like this.
200 *
201 * The return value from the scanner is always a copy of the
202 * `t_type' field in the structure.
203 */
204 struct tokenval {
205 int t_type;
206 long t_integer, t_inttwo;
207 char *t_charptr;
208 };
209 typedef int (*scanner) (void *private_data, struct tokenval *tv);
210
211 /*
212 * Token types returned by the scanner, in addition to ordinary
213 * ASCII character values, and zero for end-of-string.
214 */
215 enum { /* token types, other than chars */
216 TOKEN_INVALID = -1, /* a placeholder value */
217 TOKEN_EOS = 0, /* end of string */
218 TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<', /* aliases */
219 TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN, /* major token types */
220 TOKEN_ERRNUM, /* numeric constant with error in */
221 TOKEN_HERE, TOKEN_BASE, /* $ and $$ */
222 TOKEN_SPECIAL, /* BYTE, WORD, DWORD, FAR, NEAR, etc */
223 TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */
224 TOKEN_SHL, TOKEN_SHR, /* << and >> */
225 TOKEN_SDIV, TOKEN_SMOD, /* // and %% */
226 TOKEN_GE, TOKEN_LE, TOKEN_NE, /* >=, <= and <> (!= is same as <>) */
227 TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR, /* &&, || and ^^ */
228 TOKEN_SEG, TOKEN_WRT, /* SEG and WRT */
229 TOKEN_FLOAT /* floating-point constant */
230 };
231
232 /*
233 * Expression-evaluator datatype. Expressions, within the
234 * evaluator, are stored as an array of these beasts, terminated by
235 * a record with type==0. Mostly, it's a vector type: each type
236 * denotes some kind of a component, and the value denotes the
237 * multiple of that component present in the expression. The
238 * exception is the WRT type, whose `value' field denotes the
239 * segment to which the expression is relative. These segments will
240 * be segment-base types, i.e. either odd segment values or SEG_ABS
241 * types. So it is still valid to assume that anything with a
242 * `value' field of zero is insignificant.
243 */
244 typedef struct {
245 long type; /* a register, or EXPR_xxx */
246 long value; /* must be >= 32 bits */
247 } expr;
248
249 /*
250 * The evaluator can also return hints about which of two registers
251 * used in an expression should be the base register. See also the
252 * `operand' structure.
253 */
254 struct eval_hints {
255 int base;
256 int type;
257 };
258
259 /*
260 * The actual expression evaluator function looks like this. When
261 * called, it expects the first token of its expression to already
262 * be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
263 * it will start by calling the scanner.
264 *
265 * If a forward reference happens during evaluation, the evaluator
266 * must set `*fwref' to TRUE if `fwref' is non-NULL.
267 *
268 * `critical' is non-zero if the expression may not contain forward
269 * references. The evaluator will report its own error if this
270 * occurs; if `critical' is 1, the error will be "symbol not
271 * defined before use", whereas if `critical' is 2, the error will
272 * be "symbol undefined".
273 *
274 * If `critical' has bit 4 set (in addition to its main value: 0x11
275 * and 0x12 correspond to 1 and 2) then an extended expression
276 * syntax is recognised, in which relational operators such as =, <
277 * and >= are accepted, as well as low-precedence logical operators
278 * &&, ^^ and ||.
279 *
280 * If `hints' is non-NULL, it gets filled in with some hints as to
281 * the base register in complex effective addresses.
282 */
283 typedef expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv,
284 int *fwref, int critical, efunc error,
285 struct eval_hints *hints);
286
287 /*
288 * There's also an auxiliary routine through which the evaluator
289 * needs to hear about the value of $ and the label (if any)
290 * defined on the current line.
291 */
292 typedef void (*evalinfofunc) (char *labelname, long segment, long offset);
293
294 /*
295 * Special values for expr->type. ASSUMPTION MADE HERE: the number
296 * of distinct register names (i.e. possible "type" fields for an
297 * expr structure) does not exceed 124 (EXPR_REG_START through
298 * EXPR_REG_END).
299 */
300 #define EXPR_REG_START 1
301 #define EXPR_REG_END 124
302 #define EXPR_UNKNOWN 125L /* for forward references */
303 #define EXPR_SIMPLE 126L
304 #define EXPR_WRT 127L
305 #define EXPR_SEGBASE 128L
306
307 /*
308 * Preprocessors ought to look like this:
309 */
310 typedef struct {
311 /*
312 * Called at the start of a pass; given a file name, the number
313 * of the pass, an error reporting function, an evaluator
314 * function, and a listing generator to talk to.
315 */
316 void (*reset) (char *, int, efunc, evalfunc, ListGen *);
317
318 /*
319 * Called to fetch a line of preprocessed source. The line
320 * returned has been malloc'ed, and so should be freed after
321 * use.
322 */
323 char *(*getline) (void);
324
325 /*
326 * Called at the end of a pass.
327 */
328 void (*cleanup) (void);
329 } Preproc;
330
331 /*
332 * ----------------------------------------------------------------
333 * Some lexical properties of the NASM source language, included
334 * here because they are shared between the parser and preprocessor
335 * ----------------------------------------------------------------
336 */
337
338 /* isidstart matches any character that may start an identifier, and isidchar
339 * matches any character that may appear at places other than the start of an
340 * identifier. E.g. a period may only appear at the start of an identifier
341 * (for local labels), whereas a number may appear anywhere *but* at the
342 * start. */
343
344 #define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
345 || (c)=='@' )
346 #define isidchar(c) ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
347 || (c)=='~' )
348
349 /* Ditto for numeric constants. */
350
351 #define isnumstart(c) ( isdigit(c) || (c)=='$' )
352 #define isnumchar(c) ( isalnum(c) )
353
354 /* This returns the numeric value of a given 'digit'. */
355
356 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
357
358 /*
359 * Data-type flags that get passed to listing-file routines.
360 */
361 enum {
362 LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
363 LIST_INCBIN, LIST_TIMES
364 };
365
366 /*
367 * -----------------------------------------------------------
368 * Format of the `insn' structure returned from `parser.c' and
369 * passed into `assemble.c'
370 * -----------------------------------------------------------
371 */
372
373 /*
374 * Here we define the operand types. These are implemented as bit
375 * masks, since some are subsets of others; e.g. AX in a MOV
376 * instruction is a special operand type, whereas AX in other
377 * contexts is just another 16-bit register. (Also, consider CL in
378 * shift instructions, DX in OUT, etc.)
379 */
380
381 /* size, and other attributes, of the operand */
382 #define BITS8 0x00000001L
383 #define BITS16 0x00000002L
384 #define BITS32 0x00000004L
385 #define BITS64 0x00000008L /* FPU only */
386 #define BITS80 0x00000010L /* FPU only */
387 #define FAR 0x00000020L /* grotty: this means 16:16 or */
388 /* 16:32, like in CALL/JMP */
389 #define NEAR 0x00000040L
390 #define SHORT 0x00000080L /* and this means what it says :) */
391
392 #define SIZE_MASK 0x000000FFL /* all the size attributes */
393 #define NON_SIZE (~SIZE_MASK)
394
395 #define TO 0x00000100L /* reverse effect in FADD, FSUB &c */
396 #define COLON 0x00000200L /* operand is followed by a colon */
397
398 /* type of operand: memory reference, register, etc. */
399 #define MEMORY 0x00204000L
400 #define REGISTER 0x00001000L /* register number in 'basereg' */
401 #define IMMEDIATE 0x00002000L
402
403 #define REGMEM 0x00200000L /* for r/m, ie EA, operands */
404 #define REGNORM 0x00201000L /* 'normal' reg, qualifies as EA */
405 #define REG8 0x00201001L
406 #define REG16 0x00201002L
407 #define REG32 0x00201004L
408 #define MMXREG 0x00201008L /* MMX registers */
409 #define FPUREG 0x01000000L /* floating point stack registers */
410 #define FPU0 0x01000800L /* FPU stack register zero */
411
412 /* special register operands: these may be treated differently */
413 #define REG_SMASK 0x00070000L /* a mask for the following */
414 #define REG_ACCUM 0x00211000L /* accumulator: AL, AX or EAX */
415 #define REG_AL 0x00211001L /* REG_ACCUM | BITSxx */
416 #define REG_AX 0x00211002L /* ditto */
417 #define REG_EAX 0x00211004L /* and again */
418 #define REG_COUNT 0x00221000L /* counter: CL, CX or ECX */
419 #define REG_CL 0x00221001L /* REG_COUNT | BITSxx */
420 #define REG_CX 0x00221002L /* ditto */
421 #define REG_ECX 0x00221004L /* another one */
422 #define REG_DX 0x00241002L
423 #define REG_SREG 0x00081002L /* any segment register */
424 #define REG_CS 0x01081002L /* CS */
425 #define REG_DESS 0x02081002L /* DS, ES, SS (non-CS 86 registers) */
426 #define REG_FSGS 0x04081002L /* FS, GS (386 extended registers) */
427 #define REG_CDT 0x00101004L /* CRn, DRn and TRn */
428 #define REG_CREG 0x08101004L /* CRn */
429 #define REG_CR4 0x08101404L /* CR4 (Pentium only) */
430 #define REG_DREG 0x10101004L /* DRn */
431 #define REG_TREG 0x20101004L /* TRn */
432
433 /* special type of EA */
434 #define MEM_OFFS 0x00604000L /* simple [address] offset */
435
436 /* special type of immediate operand */
437 #define ONENESS 0x00800000L /* so UNITY == IMMEDIATE | ONENESS */
438 #define UNITY 0x00802000L /* for shift/rotate instructions */
439
440 /*
441 * Next, the codes returned from the parser, for registers and
442 * instructions.
443 */
444
445 enum { /* register names */
446 R_AH = EXPR_REG_START, R_AL, R_AX, R_BH, R_BL, R_BP, R_BX, R_CH,
447 R_CL, R_CR0, R_CR2, R_CR3, R_CR4, R_CS, R_CX, R_DH, R_DI, R_DL,
448 R_DR0, R_DR1, R_DR2, R_DR3, R_DR6, R_DR7, R_DS, R_DX, R_EAX,
449 R_EBP, R_EBX, R_ECX, R_EDI, R_EDX, R_ES, R_ESI, R_ESP, R_FS,
450 R_GS, R_MM0, R_MM1, R_MM2, R_MM3, R_MM4, R_MM5, R_MM6, R_MM7,
451 R_SI, R_SP, R_SS, R_ST0, R_ST1, R_ST2, R_ST3, R_ST4, R_ST5,
452 R_ST6, R_ST7, R_TR3, R_TR4, R_TR5, R_TR6, R_TR7, REG_ENUM_LIMIT
453 };
454
455 enum { /* instruction names */
456 I_AAA, I_AAD, I_AAM, I_AAS, I_ADC, I_ADD, I_AND, I_ARPL,
457 I_BOUND, I_BSF, I_BSR, I_BSWAP, I_BT, I_BTC, I_BTR, I_BTS,
458 I_CALL, I_CBW, I_CDQ, I_CLC, I_CLD, I_CLI, I_CLTS, I_CMC, I_CMP,
459 I_CMPSB, I_CMPSD, I_CMPSW, I_CMPXCHG, I_CMPXCHG486, I_CMPXCHG8B,
460 I_CPUID, I_CWD, I_CWDE, I_DAA, I_DAS, I_DB, I_DD, I_DEC, I_DIV,
461 I_DQ, I_DT, I_DW, I_EMMS, I_ENTER, I_EQU, I_F2XM1, I_FABS,
462 I_FADD, I_FADDP, I_FBLD, I_FBSTP, I_FCHS, I_FCLEX, I_FCMOVB,
463 I_FCMOVBE, I_FCMOVE, I_FCMOVNB, I_FCMOVNBE, I_FCMOVNE,
464 I_FCMOVNU, I_FCMOVU, I_FCOM, I_FCOMI, I_FCOMIP, I_FCOMP,
465 I_FCOMPP, I_FCOS, I_FDECSTP, I_FDISI, I_FDIV, I_FDIVP, I_FDIVR,
466 I_FDIVRP, I_FENI, I_FFREE, I_FIADD, I_FICOM, I_FICOMP, I_FIDIV,
467 I_FIDIVR, I_FILD, I_FIMUL, I_FINCSTP, I_FINIT, I_FIST, I_FISTP,
468 I_FISUB, I_FISUBR, I_FLD, I_FLD1, I_FLDCW, I_FLDENV, I_FLDL2E,
469 I_FLDL2T, I_FLDLG2, I_FLDLN2, I_FLDPI, I_FLDZ, I_FMUL, I_FMULP,
470 I_FNCLEX, I_FNDISI, I_FNENI, I_FNINIT, I_FNOP, I_FNSAVE,
471 I_FNSTCW, I_FNSTENV, I_FNSTSW, I_FPATAN, I_FPREM, I_FPREM1,
472 I_FPTAN, I_FRNDINT, I_FRSTOR, I_FSAVE, I_FSCALE, I_FSETPM,
473 I_FSIN, I_FSINCOS, I_FSQRT, I_FST, I_FSTCW, I_FSTENV, I_FSTP,
474 I_FSTSW, I_FSUB, I_FSUBP, I_FSUBR, I_FSUBRP, I_FTST, I_FUCOM,
475 I_FUCOMI, I_FUCOMIP, I_FUCOMP, I_FUCOMPP, I_FXAM, I_FXCH,
476 I_FXTRACT, I_FYL2X, I_FYL2XP1, I_HLT, I_IBTS, I_ICEBP, I_IDIV,
477 I_IMUL, I_IN, I_INC, I_INCBIN, I_INSB, I_INSD, I_INSW, I_INT,
478 I_INT1, I_INT01, I_INT3, I_INTO, I_INVD, I_INVLPG, I_IRET,
479 I_IRETD, I_IRETW, I_JCXZ, I_JECXZ, I_JMP, I_LAHF, I_LAR, I_LDS,
480 I_LEA, I_LEAVE, I_LES, I_LFS, I_LGDT, I_LGS, I_LIDT, I_LLDT,
481 I_LMSW, I_LOADALL, I_LOADALL286, I_LODSB, I_LODSD, I_LODSW,
482 I_LOOP, I_LOOPE, I_LOOPNE, I_LOOPNZ, I_LOOPZ, I_LSL, I_LSS,
483 I_LTR, I_MOV, I_MOVD, I_MOVQ, I_MOVSB, I_MOVSD, I_MOVSW,
484 I_MOVSX, I_MOVZX, I_MUL, I_NEG, I_NOP, I_NOT, I_OR, I_OUT,
485 I_OUTSB, I_OUTSD, I_OUTSW, I_PACKSSDW, I_PACKSSWB, I_PACKUSWB,
486 I_PADDB, I_PADDD, I_PADDSB, I_PADDSIW, I_PADDSW, I_PADDUSB,
487 I_PADDUSW, I_PADDW, I_PAND, I_PANDN, I_PAVEB, I_PCMPEQB,
488 I_PCMPEQD, I_PCMPEQW, I_PCMPGTB, I_PCMPGTD, I_PCMPGTW,
489 I_PDISTIB, I_PMACHRIW, I_PMADDWD, I_PMAGW, I_PMULHRW,
490 I_PMULHRIW, I_PMULHW, I_PMULLW, I_PMVGEZB, I_PMVLZB, I_PMVNZB,
491 I_PMVZB, I_POP, I_POPA, I_POPAD, I_POPAW, I_POPF, I_POPFD,
492 I_POPFW, I_POR, I_PSLLD, I_PSLLQ, I_PSLLW, I_PSRAD, I_PSRAW,
493 I_PSRLD, I_PSRLQ, I_PSRLW, I_PSUBB, I_PSUBD, I_PSUBSB,
494 I_PSUBSIW, I_PSUBSW, I_PSUBUSB, I_PSUBUSW, I_PSUBW, I_PUNPCKHBW,
495 I_PUNPCKHDQ, I_PUNPCKHWD, I_PUNPCKLBW, I_PUNPCKLDQ, I_PUNPCKLWD,
496 I_PUSH, I_PUSHA, I_PUSHAD, I_PUSHAW, I_PUSHF, I_PUSHFD,
497 I_PUSHFW, I_PXOR, I_RCL, I_RCR, I_RDMSR, I_RDPMC, I_RDTSC,
498 I_RESB, I_RESD, I_RESQ, I_REST, I_RESW, I_RET, I_RETF, I_RETN,
499 I_ROL, I_ROR, I_RSM, I_SAHF, I_SAL, I_SALC, I_SAR, I_SBB,
500 I_SCASB, I_SCASD, I_SCASW, I_SGDT, I_SHL, I_SHLD, I_SHR, I_SHRD,
501 I_SIDT, I_SLDT, I_SMI, I_SMSW, I_STC, I_STD, I_STI, I_STOSB,
502 I_STOSD, I_STOSW, I_STR, I_SUB, I_TEST, I_UMOV, I_VERR, I_VERW,
503 I_WAIT, I_WBINVD, I_WRMSR, I_XADD, I_XBTS, I_XCHG, I_XLATB,
504 I_XOR, I_CMOVcc, I_Jcc, I_SETcc
505 };
506
507 enum { /* condition code names */
508 C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
509 C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
510 C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
511 };
512
513 /*
514 * Note that because segment registers may be used as instruction
515 * prefixes, we must ensure the enumerations for prefixes and
516 * register names do not overlap.
517 */
518 enum { /* instruction prefixes */
519 PREFIX_ENUM_START = REG_ENUM_LIMIT,
520 P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
521 P_REPNE, P_REPNZ, P_REPZ, P_TIMES
522 };
523
524 enum { /* extended operand types */
525 EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
526 };
527
528 enum { /* special EA flags */
529 EAF_BYTEOFFS = 1, /* force offset part to byte size */
530 EAF_WORDOFFS = 2, /* force offset part to [d]word size */
531 EAF_TIMESTWO = 4 /* really do EAX*2 not EAX+EAX */
532 };
533
534 enum { /* values for `hinttype' */
535 EAH_NOHINT = 0, /* no hint at all - our discretion */
536 EAH_MAKEBASE = 1, /* try to make given reg the base */
537 EAH_NOTBASE = 2 /* try _not_ to make reg the base */
538 };
539
540 typedef struct { /* operand to an instruction */
541 long type; /* type of operand */
542 int addr_size; /* 0 means default; 16; 32 */
543 int basereg, indexreg, scale; /* registers and scale involved */
544 int hintbase, hinttype; /* hint as to real base register */
545 long segment; /* immediate segment, if needed */
546 long offset; /* any immediate number */
547 long wrt; /* segment base it's relative to */
548 int eaflags; /* special EA flags */
549 } operand;
550
551 typedef struct extop { /* extended operand */
552 struct extop *next; /* linked list */
553 long type; /* defined above */
554 char *stringval; /* if it's a string, then here it is */
555 int stringlen; /* ... and here's how long it is */
556 long segment; /* if it's a number/address, then... */
557 long offset; /* ... it's given here ... */
558 long wrt; /* ... and here */
559 } extop;
560
561 #define MAXPREFIX 4
562
563 typedef struct { /* an instruction itself */
564 char *label; /* the label defined, or NULL */
565 int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
566 int nprefix; /* number of entries in above */
567 int opcode; /* the opcode - not just the string */
568 int condition; /* the condition code, if Jcc/SETcc */
569 int operands; /* how many operands? 0-3 */
570 operand oprs[3]; /* the operands, defined as above */
571 extop *eops; /* extended operands */
572 long times; /* repeat count (TIMES prefix) */
573 int forw_ref; /* is there a forward reference? */
574 } insn;
575
576 /*
577 * ------------------------------------------------------------
578 * The data structure defining an output format driver, and the
579 * interfaces to the functions therein.
580 * ------------------------------------------------------------
581 */
582
583 struct ofmt {
584 /*
585 * This is a short (one-liner) description of the type of
586 * output generated by the driver.
587 */
588 char *fullname;
589
590 /*
591 * This is a single keyword used to select the driver.
592 */
593 char *shortname;
594
595 /*
596 * This, if non-NULL, is a NULL-terminated list of `char *'s
597 * pointing to extra standard macros supplied by the object
598 * format (e.g. a sensible initial default value of __SECT__,
599 * and user-level equivalents for any format-specific
600 * directives).
601 */
602 char **stdmac;
603
604 /*
605 * This procedure is called at the start of an output session.
606 * It tells the output format what file it will be writing to,
607 * what routine to report errors through, and how to interface
608 * to the label manager and expression evaluator if necessary.
609 * It also gives it a chance to do other initialisation.
610 */
611 void (*init) (FILE *fp, efunc error, ldfunc ldef, evalfunc eval);
612
613 /*
614 * This procedure is called by assemble() to write actual
615 * generated code or data to the object file. Typically it
616 * doesn't have to actually _write_ it, just store it for
617 * later.
618 *
619 * The `type' argument specifies the type of output data, and
620 * usually the size as well: its contents are described below.
621 */
622 void (*output) (long segto, void *data, unsigned long type,
623 long segment, long wrt);
624
625 /*
626 * This procedure is called once for every symbol defined in
627 * the module being assembled. It gives the name and value of
628 * the symbol, in NASM's terms, and indicates whether it has
629 * been declared to be global. Note that the parameter "name",
630 * when passed, will point to a piece of static storage
631 * allocated inside the label manager - it's safe to keep using
632 * that pointer, because the label manager doesn't clean up
633 * until after the output driver has.
634 *
635 * Values of `is_global' are: 0 means the symbol is local; 1
636 * means the symbol is global; 2 means the symbol is common (in
637 * which case `offset' holds the _size_ of the variable).
638 * Anything else is available for the output driver to use
639 * internally.
640 *
641 * This routine explicitly _is_ allowed to call the label
642 * manager to define further symbols, if it wants to, even
643 * though it's been called _from_ the label manager. That much
644 * re-entrancy is guaranteed in the label manager. However, the
645 * label manager will in turn call this routine, so it should
646 * be prepared to be re-entrant itself.
647 *
648 * The `special' parameter contains special information passed
649 * through from the command that defined the label: it may have
650 * been an EXTERN, a COMMON or a GLOBAL. The distinction should
651 * be obvious to the output format from the other parameters.
652 */
653 void (*symdef) (char *name, long segment, long offset, int is_global,
654 char *special);
655
656 /*
657 * This procedure is called when the source code requests a
658 * segment change. It should return the corresponding segment
659 * _number_ for the name, or NO_SEG if the name is not a valid
660 * segment name.
661 *
662 * It may also be called with NULL, in which case it is to
663 * return the _default_ section number for starting assembly in.
664 *
665 * It is allowed to modify the string it is given a pointer to.
666 *
667 * It is also allowed to specify a default instruction size for
668 * the segment, by setting `*bits' to 16 or 32. Or, if it
669 * doesn't wish to define a default, it can leave `bits' alone.
670 */
671 long (*section) (char *name, int pass, int *bits);
672
673 /*
674 * This procedure is called to modify the segment base values
675 * returned from the SEG operator. It is given a segment base
676 * value (i.e. a segment value with the low bit set), and is
677 * required to produce in return a segment value which may be
678 * different. It can map segment bases to absolute numbers by
679 * means of returning SEG_ABS types.
680 *
681 * It should return NO_SEG if the segment base cannot be
682 * determined; the evaluator (which calls this routine) is
683 * responsible for throwing an error condition if that occurs
684 * in pass two or in a critical expression.
685 */
686 long (*segbase) (long segment);
687
688 /*
689 * This procedure is called to allow the output driver to
690 * process its own specific directives. When called, it has the
691 * directive word in `directive' and the parameter string in
692 * `value'. It is called in both assembly passes, and `pass'
693 * will be either 1 or 2.
694 *
695 * This procedure should return zero if it does not _recognise_
696 * the directive, so that the main program can report an error.
697 * If it recognises the directive but then has its own errors,
698 * it should report them itself and then return non-zero. It
699 * should also return non-zero if it correctly processes the
700 * directive.
701 */
702 int (*directive) (char *directive, char *value, int pass);
703
704 /*
705 * This procedure is called before anything else - even before
706 * the "init" routine - and is passed the name of the input
707 * file from which this output file is being generated. It
708 * should return its preferred name for the output file in
709 * `outname', if outname[0] is not '\0', and do nothing to
710 * `outname' otherwise. Since it is called before the driver is
711 * properly initialised, it has to be passed its error handler
712 * separately.
713 *
714 * This procedure may also take its own copy of the input file
715 * name for use in writing the output file: it is _guaranteed_
716 * that it will be called before the "init" routine.
717 *
718 * The parameter `outname' points to an area of storage
719 * guaranteed to be at least FILENAME_MAX in size.
720 */
721 void (*filename) (char *inname, char *outname, efunc error);
722
723 /*
724 * This procedure is called after assembly finishes, to allow
725 * the output driver to clean itself up and free its memory.
726 * Typically, it will also be the point at which the object
727 * file actually gets _written_.
728 *
729 * One thing the cleanup routine should always do is to close
730 * the output file pointer.
731 */
732 void (*cleanup) (void);
733 };
734
735 /*
736 * values for the `type' parameter to an output function. Each one
737 * must have the actual number of _bytes_ added to it.
738 *
739 * Exceptions are OUT_RELxADR, which denote an x-byte relocation
740 * which will be a relative jump. For this we need to know the
741 * distance in bytes from the start of the relocated record until
742 * the end of the containing instruction. _This_ is what is stored
743 * in the size part of the parameter, in this case.
744 *
745 * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
746 * and the contents of the "data" parameter is irrelevant.
747 *
748 * The "data" parameter for the output function points to a "long",
749 * containing the address in question, unless the type is
750 * OUT_RAWDATA, in which case it points to an "unsigned char"
751 * array.
752 */
753 #define OUT_RAWDATA 0x00000000UL
754 #define OUT_ADDRESS 0x10000000UL
755 #define OUT_REL2ADR 0x20000000UL
756 #define OUT_REL4ADR 0x30000000UL
757 #define OUT_RESERVE 0x40000000UL
758 #define OUT_TYPMASK 0xF0000000UL
759 #define OUT_SIZMASK 0x0FFFFFFFUL
760
761 /*
762 * -----
763 * Other
764 * -----
765 */
766
767 /*
768 * This is a useful #define which I keep meaning to use more often:
769 * the number of elements of a statically defined array.
770 */
771
772 #define elements(x) ( sizeof(x) / sizeof(*(x)) )
773
774 #endif