Merge remote-tracking branch 'origin/nasm-2.13.xx'
[nasm.git] / include / nasm.h
blobf640cd63c7efb34dce7a045d1a6649e320c540da
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * nasm.h main header file for the Netwide Assembler: inter-module interface
38 #ifndef NASM_NASM_H
39 #define NASM_NASM_H
41 #include "compiler.h"
43 #include <stdio.h>
44 #include <time.h>
46 #include "nasmlib.h"
47 #include "strlist.h"
48 #include "preproc.h"
49 #include "insnsi.h" /* For enum opcode */
50 #include "directiv.h" /* For enum directive */
51 #include "opflags.h"
52 #include "regs.h"
54 /* Time stamp for the official start of compilation */
55 struct compile_time {
56 time_t t;
57 bool have_local, have_gm, have_posix;
58 int64_t posix;
59 struct tm local;
60 struct tm gm;
62 extern struct compile_time official_compile_time;
64 #define NO_SEG -1L /* null segment value */
65 #define SEG_ABS 0x40000000L /* mask for far-absolute segments */
67 #ifndef FILENAME_MAX
68 #define FILENAME_MAX 256
69 #endif
71 #ifndef PREFIX_MAX
72 #define PREFIX_MAX 10
73 #endif
75 #ifndef POSTFIX_MAX
76 #define POSTFIX_MAX 10
77 #endif
79 #define IDLEN_MAX 4096
80 #define DECOLEN_MAX 32
83 * Name pollution problems: <time.h> on Digital UNIX pulls in some
84 * strange hardware header file which sees fit to define R_SP. We
85 * undefine it here so as not to break the enum below.
87 #ifdef R_SP
88 #undef R_SP
89 #endif
92 * We must declare the existence of this structure type up here,
93 * since we have to reference it before we define it...
95 struct ofmt;
98 * Values for the `type' parameter to an output function.
100 enum out_type {
101 OUT_RAWDATA, /* Plain bytes */
102 OUT_RESERVE, /* Reserved bytes (RESB et al) */
103 OUT_ZERODATA, /* Initialized data, but all zero */
104 OUT_ADDRESS, /* An address (symbol value) */
105 OUT_RELADDR, /* A relative address */
106 OUT_SEGMENT, /* A segment number */
109 * These values are used by the legacy backend interface only;
110 * see output/legacy.c for more information. These should never
111 * be used otherwise. Once all backends have been migrated to the
112 * new interface they should be removed.
114 OUT_REL1ADR,
115 OUT_REL2ADR,
116 OUT_REL4ADR,
117 OUT_REL8ADR
120 enum out_sign {
121 OUT_WRAP, /* Undefined signedness (wraps) */
122 OUT_SIGNED, /* Value is signed */
123 OUT_UNSIGNED /* Value is unsigned */
127 * The data we send down to the backend.
128 * XXX: We still want to push down the base address symbol if
129 * available, and replace the segment numbers with a structure.
131 struct out_data {
132 int64_t offset; /* Offset within segment */
133 int32_t segment; /* Segment written to */
134 enum out_type type; /* See above */
135 enum out_sign sign; /* See above */
136 int inslen; /* Length of instruction */
137 int insoffs; /* Offset inside instruction */
138 int bits; /* Bits mode of compilation */
139 uint64_t size; /* Size of output */
140 const struct itemplate *itemp; /* Instruction template */
141 const void *data; /* Data for OUT_RAWDATA */
142 uint64_t toffset; /* Target address offset for relocation */
143 int32_t tsegment; /* Target segment for relocation */
144 int32_t twrt; /* Relocation with respect to */
145 int64_t relbase; /* Relative base for OUT_RELADDR */
149 * A label-lookup function.
151 typedef bool (*lfunc)(char *label, int32_t *segment, int64_t *offset);
154 * And a label-definition function. The boolean parameter
155 * `is_norm' states whether the label is a `normal' label (which
156 * should affect the local-label system), or something odder like
157 * an EQU or a segment-base symbol, which shouldn't.
159 typedef void (*ldfunc)(char *label, int32_t segment, int64_t offset,
160 char *special, bool is_norm, bool isextrn);
162 void define_label(char *label, int32_t segment, int64_t offset,
163 char *special, bool is_norm, bool isextrn);
166 * Token types returned by the scanner, in addition to ordinary
167 * ASCII character values, and zero for end-of-string.
169 enum token_type { /* token types, other than chars */
170 TOKEN_INVALID = -1, /* a placeholder value */
171 TOKEN_EOS = 0, /* end of string */
172 TOKEN_EQ = '=',
173 TOKEN_GT = '>',
174 TOKEN_LT = '<', /* aliases */
175 TOKEN_ID = 256, /* identifier */
176 TOKEN_NUM, /* numeric constant */
177 TOKEN_ERRNUM, /* malformed numeric constant */
178 TOKEN_STR, /* string constant */
179 TOKEN_ERRSTR, /* unterminated string constant */
180 TOKEN_FLOAT, /* floating-point constant */
181 TOKEN_REG, /* register name */
182 TOKEN_INSN, /* instruction name */
183 TOKEN_HERE, /* $ */
184 TOKEN_BASE, /* $$ */
185 TOKEN_SPECIAL, /* BYTE, WORD, DWORD, QWORD, FAR, NEAR, etc */
186 TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */
187 TOKEN_SHL, /* << */
188 TOKEN_SHR, /* >> */
189 TOKEN_SDIV, /* // */
190 TOKEN_SMOD, /* %% */
191 TOKEN_GE, /* >= */
192 TOKEN_LE, /* <= */
193 TOKEN_NE, /* <> (!= is same as <>) */
194 TOKEN_DBL_AND, /* && */
195 TOKEN_DBL_OR, /* || */
196 TOKEN_DBL_XOR, /* ^^ */
197 TOKEN_SEG, /* SEG */
198 TOKEN_WRT, /* WRT */
199 TOKEN_FLOATIZE, /* __floatX__ */
200 TOKEN_STRFUNC, /* __utf16*__, __utf32*__ */
201 TOKEN_IFUNC, /* __ilog2*__ */
202 TOKEN_DECORATOR, /* decorators such as {...} */
203 TOKEN_OPMASK /* translated token for opmask registers */
206 enum floatize {
207 FLOAT_8,
208 FLOAT_16,
209 FLOAT_32,
210 FLOAT_64,
211 FLOAT_80M,
212 FLOAT_80E,
213 FLOAT_128L,
214 FLOAT_128H
217 /* Must match the list in string_transform(), in strfunc.c */
218 enum strfunc {
219 STRFUNC_UTF16,
220 STRFUNC_UTF16LE,
221 STRFUNC_UTF16BE,
222 STRFUNC_UTF32,
223 STRFUNC_UTF32LE,
224 STRFUNC_UTF32BE
227 enum ifunc {
228 IFUNC_ILOG2E,
229 IFUNC_ILOG2W,
230 IFUNC_ILOG2F,
231 IFUNC_ILOG2C
234 size_t string_transform(char *, size_t, char **, enum strfunc);
237 * The expression evaluator must be passed a scanner function; a
238 * standard scanner is provided as part of nasmlib.c. The
239 * preprocessor will use a different one. Scanners, and the
240 * token-value structures they return, look like this.
242 * The return value from the scanner is always a copy of the
243 * `t_type' field in the structure.
245 struct tokenval {
246 char *t_charptr;
247 int64_t t_integer;
248 int64_t t_inttwo;
249 enum token_type t_type;
250 int8_t t_flag;
252 typedef int (*scanner)(void *private_data, struct tokenval *tv);
254 struct location {
255 int64_t offset;
256 int32_t segment;
257 int known;
259 extern struct location location;
262 * Expression-evaluator datatype. Expressions, within the
263 * evaluator, are stored as an array of these beasts, terminated by
264 * a record with type==0. Mostly, it's a vector type: each type
265 * denotes some kind of a component, and the value denotes the
266 * multiple of that component present in the expression. The
267 * exception is the WRT type, whose `value' field denotes the
268 * segment to which the expression is relative. These segments will
269 * be segment-base types, i.e. either odd segment values or SEG_ABS
270 * types. So it is still valid to assume that anything with a
271 * `value' field of zero is insignificant.
273 typedef struct {
274 int32_t type; /* a register, or EXPR_xxx */
275 int64_t value; /* must be >= 32 bits */
276 } expr;
279 * Library routines to manipulate expression data types.
281 bool is_reloc(const expr *vect);
282 bool is_simple(const expr *vect);
283 bool is_really_simple(const expr *vect);
284 bool is_unknown(const expr *vect);
285 bool is_just_unknown(const expr *vect);
286 int64_t reloc_value(const expr *vect);
287 int32_t reloc_seg(const expr *vect);
288 int32_t reloc_wrt(const expr *vect);
289 bool is_self_relative(const expr *vect);
290 void dump_expr(const expr *vect);
293 * The evaluator can also return hints about which of two registers
294 * used in an expression should be the base register. See also the
295 * `operand' structure.
297 struct eval_hints {
298 int64_t base;
299 int type;
303 * The actual expression evaluator function looks like this. When
304 * called, it expects the first token of its expression to already
305 * be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
306 * it will start by calling the scanner.
308 * If a forward reference happens during evaluation, the evaluator
309 * must set `*fwref' to true if `fwref' is non-NULL.
311 * `critical' is non-zero if the expression may not contain forward
312 * references. The evaluator will report its own error if this
313 * occurs; if `critical' is 1, the error will be "symbol not
314 * defined before use", whereas if `critical' is 2, the error will
315 * be "symbol undefined".
317 * If `critical' has bit 8 set (in addition to its main value: 0x101
318 * and 0x102 correspond to 1 and 2) then an extended expression
319 * syntax is recognised, in which relational operators such as =, <
320 * and >= are accepted, as well as low-precedence logical operators
321 * &&, ^^ and ||.
323 * If `hints' is non-NULL, it gets filled in with some hints as to
324 * the base register in complex effective addresses.
326 #define CRITICAL 0x100
327 typedef expr *(*evalfunc)(scanner sc, void *scprivate,
328 struct tokenval *tv, int *fwref, int critical,
329 struct eval_hints *hints);
332 * Special values for expr->type.
333 * These come after EXPR_REG_END as defined in regs.h.
334 * Expr types : 0 ~ EXPR_REG_END, EXPR_UNKNOWN, EXPR_...., EXPR_RDSAE,
335 * EXPR_SEGBASE ~ EXPR_SEGBASE + SEG_ABS, ...
337 #define EXPR_UNKNOWN (EXPR_REG_END+1) /* forward references */
338 #define EXPR_SIMPLE (EXPR_REG_END+2)
339 #define EXPR_WRT (EXPR_REG_END+3)
340 #define EXPR_RDSAE (EXPR_REG_END+4)
341 #define EXPR_SEGBASE (EXPR_REG_END+5)
344 * preprocessors ought to look like this:
346 struct preproc_ops {
348 * Called once at the very start of assembly.
350 void (*init)(void);
353 * Called at the start of a pass; given a file name, the number
354 * of the pass, an error reporting function, an evaluator
355 * function, and a listing generator to talk to.
357 void (*reset)(char *file, int pass, StrList **deplist);
360 * Called to fetch a line of preprocessed source. The line
361 * returned has been malloc'ed, and so should be freed after
362 * use.
364 char *(*getline)(void);
366 /* Called at the end of a pass */
367 void (*cleanup)(int pass);
369 /* Additional macros specific to output format */
370 void (*extra_stdmac)(macros_t *macros);
372 /* Early definitions and undefinitions for macros */
373 void (*pre_define)(char *definition);
374 void (*pre_undefine)(char *definition);
376 /* Include file from command line */
377 void (*pre_include)(char *fname);
379 /* Include path from command line */
380 void (*include_path)(char *path);
382 /* Unwind the macro stack when printing an error message */
383 void (*error_list_macros)(int severity);
386 extern const struct preproc_ops nasmpp;
387 extern const struct preproc_ops preproc_nop;
389 /* List of dependency files */
390 extern StrList *depend_list;
393 * Some lexical properties of the NASM source language, included
394 * here because they are shared between the parser and preprocessor.
398 * isidstart matches any character that may start an identifier, and isidchar
399 * matches any character that may appear at places other than the start of an
400 * identifier. E.g. a period may only appear at the start of an identifier
401 * (for local labels), whereas a number may appear anywhere *but* at the
402 * start.
403 * isbrcchar matches any character that may placed inside curly braces as a
404 * decorator. E.g. {rn-sae}, {1to8}, {k1}{z}
407 #define isidstart(c) (nasm_isalpha(c) || \
408 (c) == '_' || \
409 (c) == '.' || \
410 (c) == '?' || \
411 (c) == '@')
413 #define isidchar(c) (isidstart(c) || \
414 nasm_isdigit(c) || \
415 (c) == '$' || \
416 (c) == '#' || \
417 (c) == '~')
419 #define isbrcchar(c) (isidchar(c) || \
420 (c) == '-')
422 /* Ditto for numeric constants. */
424 #define isnumstart(c) (nasm_isdigit(c) || (c) == '$')
425 #define isnumchar(c) (nasm_isalnum(c) || (c) == '_')
428 * inline function to skip past an identifier; returns the first character past
429 * the identifier if valid, otherwise NULL.
431 static inline char *nasm_skip_identifier(const char *str)
433 const char *p = str;
435 if (!isidstart(*p++)) {
436 p = NULL;
437 } else {
438 while (isidchar(*p++))
441 return (char *)p;
445 * Data-type flags that get passed to listing-file routines.
447 enum {
448 LIST_READ,
449 LIST_MACRO,
450 LIST_MACRO_NOLIST,
451 LIST_INCLUDE,
452 LIST_INCBIN,
453 LIST_TIMES
457 * -----------------------------------------------------------
458 * Format of the `insn' structure returned from `parser.c' and
459 * passed into `assemble.c'
460 * -----------------------------------------------------------
463 /* Verify value to be a valid register */
464 static inline bool is_register(int reg)
466 return reg >= EXPR_REG_START && reg < REG_ENUM_LIMIT;
469 enum ccode { /* condition code names */
470 C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
471 C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
472 C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z,
473 C_none = -1
477 * token flags
479 #define TFLAG_BRC (1 << 0) /* valid only with braces. {1to8}, {rd-sae}, ...*/
480 #define TFLAG_BRC_OPT (1 << 1) /* may or may not have braces. opmasks {k1} */
481 #define TFLAG_BRC_ANY (TFLAG_BRC | TFLAG_BRC_OPT)
482 #define TFLAG_BRDCAST (1 << 2) /* broadcasting decorator */
483 #define TFLAG_WARN (1 << 3) /* warning only, treat as ID */
485 static inline uint8_t get_cond_opcode(enum ccode c)
487 static const uint8_t ccode_opcodes[] = {
488 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xf, 0xd, 0xc, 0xe, 0x6, 0x2,
489 0x3, 0x7, 0x3, 0x5, 0xe, 0xc, 0xd, 0xf, 0x1, 0xb, 0x9, 0x5,
490 0x0, 0xa, 0xa, 0xb, 0x8, 0x4
493 return ccode_opcodes[(int)c];
497 * REX flags
499 #define REX_MASK 0x4f /* Actual REX prefix bits */
500 #define REX_B 0x01 /* ModRM r/m extension */
501 #define REX_X 0x02 /* SIB index extension */
502 #define REX_R 0x04 /* ModRM reg extension */
503 #define REX_W 0x08 /* 64-bit operand size */
504 #define REX_L 0x20 /* Use LOCK prefix instead of REX.R */
505 #define REX_P 0x40 /* REX prefix present/required */
506 #define REX_H 0x80 /* High register present, REX forbidden */
507 #define REX_V 0x0100 /* Instruction uses VEX/XOP instead of REX */
508 #define REX_NH 0x0200 /* Instruction which doesn't use high regs */
509 #define REX_EV 0x0400 /* Instruction uses EVEX instead of REX */
512 * EVEX bit field
514 #define EVEX_P0MM 0x0f /* EVEX P[3:0] : Opcode map */
515 #define EVEX_P0RP 0x10 /* EVEX P[4] : High-16 reg */
516 #define EVEX_P0X 0x40 /* EVEX P[6] : High-16 rm */
517 #define EVEX_P1PP 0x03 /* EVEX P[9:8] : Legacy prefix */
518 #define EVEX_P1VVVV 0x78 /* EVEX P[14:11] : NDS register */
519 #define EVEX_P1W 0x80 /* EVEX P[15] : Osize extension */
520 #define EVEX_P2AAA 0x07 /* EVEX P[18:16] : Embedded opmask */
521 #define EVEX_P2VP 0x08 /* EVEX P[19] : High-16 NDS reg */
522 #define EVEX_P2B 0x10 /* EVEX P[20] : Broadcast / RC / SAE */
523 #define EVEX_P2LL 0x60 /* EVEX P[22:21] : Vector length */
524 #define EVEX_P2RC EVEX_P2LL /* EVEX P[22:21] : Rounding control */
525 #define EVEX_P2Z 0x80 /* EVEX P[23] : Zeroing/Merging */
528 * REX_V "classes" (prefixes which behave like VEX)
530 enum vex_class {
531 RV_VEX = 0, /* C4/C5 */
532 RV_XOP = 1, /* 8F */
533 RV_EVEX = 2 /* 62 */
537 * Note that because segment registers may be used as instruction
538 * prefixes, we must ensure the enumerations for prefixes and
539 * register names do not overlap.
541 enum prefixes { /* instruction prefixes */
542 P_none = 0,
543 PREFIX_ENUM_START = REG_ENUM_LIMIT,
544 P_A16 = PREFIX_ENUM_START,
545 P_A32,
546 P_A64,
547 P_ASP,
548 P_LOCK,
549 P_O16,
550 P_O32,
551 P_O64,
552 P_OSP,
553 P_REP,
554 P_REPE,
555 P_REPNE,
556 P_REPNZ,
557 P_REPZ,
558 P_TIMES,
559 P_WAIT,
560 P_XACQUIRE,
561 P_XRELEASE,
562 P_BND,
563 P_NOBND,
564 P_EVEX,
565 P_VEX3,
566 P_VEX2,
567 PREFIX_ENUM_LIMIT
570 enum extop_type { /* extended operand types */
571 EOT_NOTHING,
572 EOT_DB_STRING, /* Byte string */
573 EOT_DB_STRING_FREE, /* Byte string which should be nasm_free'd*/
574 EOT_DB_NUMBER /* Integer */
577 enum ea_flags { /* special EA flags */
578 EAF_BYTEOFFS = 1, /* force offset part to byte size */
579 EAF_WORDOFFS = 2, /* force offset part to [d]word size */
580 EAF_TIMESTWO = 4, /* really do EAX*2 not EAX+EAX */
581 EAF_REL = 8, /* IP-relative addressing */
582 EAF_ABS = 16, /* non-IP-relative addressing */
583 EAF_FSGS = 32, /* fs/gs segment override present */
584 EAF_MIB = 64 /* mib operand */
587 enum eval_hint { /* values for `hinttype' */
588 EAH_NOHINT = 0, /* no hint at all - our discretion */
589 EAH_MAKEBASE = 1, /* try to make given reg the base */
590 EAH_NOTBASE = 2, /* try _not_ to make reg the base */
591 EAH_SUMMED = 3 /* base and index are summed into index */
594 typedef struct operand { /* operand to an instruction */
595 opflags_t type; /* type of operand */
596 int disp_size; /* 0 means default; 16; 32; 64 */
597 enum reg_enum basereg;
598 enum reg_enum indexreg; /* address registers */
599 int scale; /* index scale */
600 int hintbase;
601 enum eval_hint hinttype; /* hint as to real base register */
602 int32_t segment; /* immediate segment, if needed */
603 int64_t offset; /* any immediate number */
604 int32_t wrt; /* segment base it's relative to */
605 int eaflags; /* special EA flags */
606 int opflags; /* see OPFLAG_* defines below */
607 decoflags_t decoflags; /* decorator flags such as {...} */
608 } operand;
610 #define OPFLAG_FORWARD 1 /* operand is a forward reference */
611 #define OPFLAG_EXTERN 2 /* operand is an external reference */
612 #define OPFLAG_UNKNOWN 4 /* operand is an unknown reference
613 (always a forward reference also) */
614 #define OPFLAG_RELATIVE 8 /* operand is self-relative, e.g. [foo - $]
615 where foo is not in the current segment */
617 typedef struct extop { /* extended operand */
618 struct extop *next; /* linked list */
619 char *stringval; /* if it's a string, then here it is */
620 size_t stringlen; /* ... and here's how long it is */
621 int64_t offset; /* ... it's given here ... */
622 int32_t segment; /* if it's a number/address, then... */
623 int32_t wrt; /* ... and here */
624 bool relative; /* self-relative expression */
625 enum extop_type type; /* defined above */
626 } extop;
628 enum ea_type {
629 EA_INVALID, /* Not a valid EA at all */
630 EA_SCALAR, /* Scalar EA */
631 EA_XMMVSIB, /* XMM vector EA */
632 EA_YMMVSIB, /* YMM vector EA */
633 EA_ZMMVSIB /* ZMM vector EA */
637 * Prefix positions: each type of prefix goes in a specific slot.
638 * This affects the final ordering of the assembled output, which
639 * shouldn't matter to the processor, but if you have stylistic
640 * preferences, you can change this. REX prefixes are handled
641 * differently for the time being.
643 * LOCK and REP used to be one slot; this is no longer the case since
644 * the introduction of HLE.
646 enum prefix_pos {
647 PPS_WAIT, /* WAIT (technically not a prefix!) */
648 PPS_REP, /* REP/HLE prefix */
649 PPS_LOCK, /* LOCK prefix */
650 PPS_SEG, /* Segment override prefix */
651 PPS_OSIZE, /* Operand size prefix */
652 PPS_ASIZE, /* Address size prefix */
653 PPS_VEX, /* VEX type */
654 MAXPREFIX /* Total number of prefix slots */
658 * Tuple types that are used when determining Disp8*N eligibility
659 * The order must match with a hash %tuple_codes in insns.pl
661 enum ttypes {
662 FV = 001,
663 HV = 002,
664 FVM = 003,
665 T1S8 = 004,
666 T1S16 = 005,
667 T1S = 006,
668 T1F32 = 007,
669 T1F64 = 010,
670 T2 = 011,
671 T4 = 012,
672 T8 = 013,
673 HVM = 014,
674 QVM = 015,
675 OVM = 016,
676 M128 = 017,
677 DUP = 020
680 /* EVEX.L'L : Vector length on vector insns */
681 enum vectlens {
682 VL128 = 0,
683 VL256 = 1,
684 VL512 = 2,
685 VLMAX = 3
688 /* If you need to change this, also change it in insns.pl */
689 #define MAX_OPERANDS 5
691 typedef struct insn { /* an instruction itself */
692 char *label; /* the label defined, or NULL */
693 int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
694 enum opcode opcode; /* the opcode - not just the string */
695 enum ccode condition; /* the condition code, if Jcc/SETcc */
696 int operands; /* how many operands? 0-3 (more if db et al) */
697 int addr_size; /* address size */
698 operand oprs[MAX_OPERANDS]; /* the operands, defined as above */
699 extop *eops; /* extended operands */
700 int eops_float; /* true if DD and floating */
701 int32_t times; /* repeat count (TIMES prefix) */
702 bool forw_ref; /* is there a forward reference? */
703 bool rex_done; /* REX prefix emitted? */
704 int rex; /* Special REX Prefix */
705 int vexreg; /* Register encoded in VEX prefix */
706 int vex_cm; /* Class and M field for VEX prefix */
707 int vex_wlp; /* W, P and L information for VEX prefix */
708 uint8_t evex_p[3]; /* EVEX.P0: [RXB,R',00,mm], P1: [W,vvvv,1,pp] */
709 /* EVEX.P2: [z,L'L,b,V',aaa] */
710 enum ttypes evex_tuple; /* Tuple type for compressed Disp8*N */
711 int evex_rm; /* static rounding mode for AVX512 (EVEX) */
712 int8_t evex_brerop; /* BR/ER/SAE operand position */
713 } insn;
715 /* Instruction flags type: IF_* flags are defined in insns.h */
716 typedef uint64_t iflags_t;
719 * What to return from a directive- or pragma-handling function.
720 * Currently DIRR_OK and DIRR_ERROR are treated the same way;
721 * in both cases the backend is expected to produce the appropriate
722 * error message on its own.
724 * DIRR_BADPARAM causes a generic error message to be printed. Note
725 * that it is an error, not a warning, even in the case of pragmas;
726 * don't use it where forward compatiblity would be compromised
727 * (instead consider adding a DIRR_WARNPARAM.)
729 enum directive_result {
730 DIRR_UNKNOWN, /* Directive not handled by backend */
731 DIRR_OK, /* Directive processed */
732 DIRR_ERROR, /* Directive processed unsuccessfully */
733 DIRR_BADPARAM /* Print bad argument error message */
737 * A pragma facility: this structure is used to request passing a
738 * parsed pragma directive for a specific facility. If the handler is
739 * NULL then this pragma facility is recognized but ignored; pragma
740 * processing stops at that point.
742 * Note that the handler is passed a pointer to the facility structure
743 * as part of the struct pragma.
745 struct pragma;
747 struct pragma_facility {
748 const char *name;
749 enum directive_result (*handler)(const struct pragma *);
753 * This structure defines how a pragma directive is passed to a
754 * facility. This structure may be augmented in the future.
756 * Any facility MAY, but is not required to, add its operations
757 * keywords or a subset thereof into asm/directiv.dat, in which case
758 * the "opcode" field will be set to the corresponding D_ constant
759 * from directiv.h; otherwise it will be D_unknown.
761 struct pragma {
762 const struct pragma_facility *facility;
763 const char *facility_name; /* Facility name exactly as entered by user */
764 const char *opname; /* First word after the facility name */
765 const char *tail; /* Anything after the operation */
766 enum directive opcode; /* Operation as a D_ directives constant */
770 * The data structure defining an output format driver, and the
771 * interfaces to the functions therein.
773 struct ofmt {
775 * This is a short (one-liner) description of the type of
776 * output generated by the driver.
778 const char *fullname;
781 * This is a single keyword used to select the driver.
783 const char *shortname;
786 * Output format flags.
788 #define OFMT_TEXT 1 /* Text file format */
789 unsigned int flags;
791 int maxbits; /* Maximum segment bits supported */
794 * this is a pointer to the first element of the debug information
796 const struct dfmt * const *debug_formats;
799 * the default debugging format if -F is not specified
801 const struct dfmt *default_dfmt;
804 * This, if non-NULL, is a NULL-terminated list of `char *'s
805 * pointing to extra standard macros supplied by the object
806 * format (e.g. a sensible initial default value of __SECT__,
807 * and user-level equivalents for any format-specific
808 * directives).
810 macros_t *stdmac;
813 * This procedure is called at the start of an output session to set
814 * up internal parameters.
816 void (*init)(void);
819 * This is the modern output function, which gets passed
820 * a struct out_data with much more information. See the
821 * definition of struct out_data.
823 void (*output)(const struct out_data *data);
826 * This procedure is called by assemble() to write actual
827 * generated code or data to the object file. Typically it
828 * doesn't have to actually _write_ it, just store it for
829 * later.
831 * The `type' argument specifies the type of output data, and
832 * usually the size as well: its contents are described below.
834 * This is used for backends which have not yet been ported to
835 * the new interface, and should be NULL on ported backends.
836 * To use this entry point, set the output pointer to
837 * nasm_do_legacy_output.
839 void (*legacy_output)(int32_t segto, const void *data,
840 enum out_type type, uint64_t size,
841 int32_t segment, int32_t wrt);
844 * This procedure is called once for every symbol defined in
845 * the module being assembled. It gives the name and value of
846 * the symbol, in NASM's terms, and indicates whether it has
847 * been declared to be global. Note that the parameter "name",
848 * when passed, will point to a piece of static storage
849 * allocated inside the label manager - it's safe to keep using
850 * that pointer, because the label manager doesn't clean up
851 * until after the output driver has.
853 * Values of `is_global' are: 0 means the symbol is local; 1
854 * means the symbol is global; 2 means the symbol is common (in
855 * which case `offset' holds the _size_ of the variable).
856 * Anything else is available for the output driver to use
857 * internally.
859 * This routine explicitly _is_ allowed to call the label
860 * manager to define further symbols, if it wants to, even
861 * though it's been called _from_ the label manager. That much
862 * re-entrancy is guaranteed in the label manager. However, the
863 * label manager will in turn call this routine, so it should
864 * be prepared to be re-entrant itself.
866 * The `special' parameter contains special information passed
867 * through from the command that defined the label: it may have
868 * been an EXTERN, a COMMON or a GLOBAL. The distinction should
869 * be obvious to the output format from the other parameters.
871 void (*symdef)(char *name, int32_t segment, int64_t offset,
872 int is_global, char *special);
875 * This procedure is called when the source code requests a
876 * segment change. It should return the corresponding segment
877 * _number_ for the name, or NO_SEG if the name is not a valid
878 * segment name.
880 * It may also be called with NULL, in which case it is to
881 * return the _default_ section number for starting assembly in.
883 * It is allowed to modify the string it is given a pointer to.
885 * It is also allowed to specify a default instruction size for
886 * the segment, by setting `*bits' to 16 or 32. Or, if it
887 * doesn't wish to define a default, it can leave `bits' alone.
889 int32_t (*section)(char *name, int pass, int *bits);
892 * This procedure is called to modify section alignment,
893 * note there is a trick, the alignment can only increase
895 void (*sectalign)(int32_t seg, unsigned int value);
898 * This procedure is called to modify the segment base values
899 * returned from the SEG operator. It is given a segment base
900 * value (i.e. a segment value with the low bit set), and is
901 * required to produce in return a segment value which may be
902 * different. It can map segment bases to absolute numbers by
903 * means of returning SEG_ABS types.
905 * It should return NO_SEG if the segment base cannot be
906 * determined; the evaluator (which calls this routine) is
907 * responsible for throwing an error condition if that occurs
908 * in pass two or in a critical expression.
910 int32_t (*segbase)(int32_t segment);
913 * This procedure is called to allow the output driver to
914 * process its own specific directives. When called, it has the
915 * directive word in `directive' and the parameter string in
916 * `value'. It is called in both assembly passes, and `pass'
917 * will be either 1 or 2.
919 * The following values are (currently) possible for
920 * directive_result:
922 * 0 - DIRR_UNKNOWN - directive not recognized by backend
923 * 1 - DIRR_OK - directive processed ok
924 * 2 - DIRR_ERROR - backend printed its own error message
925 * 3 - DIRR_BADPARAM - print the generic message
926 * "invalid parameter to [*] directive"
928 enum directive_result
929 (*directive)(enum directive directive, char *value, int pass);
932 * This procedure is called before anything else - even before
933 * the "init" routine - and is passed the name of the input
934 * file from which this output file is being generated. It
935 * should return its preferred name for the output file in
936 * `outname', if outname[0] is not '\0', and do nothing to
937 * `outname' otherwise. Since it is called before the driver is
938 * properly initialized, it has to be passed its error handler
939 * separately.
941 * This procedure may also take its own copy of the input file
942 * name for use in writing the output file: it is _guaranteed_
943 * that it will be called before the "init" routine.
945 * The parameter `outname' points to an area of storage
946 * guaranteed to be at least FILENAME_MAX in size.
948 void (*filename)(char *inname, char *outname);
951 * This procedure is called after assembly finishes, to allow
952 * the output driver to clean itself up and free its memory.
953 * Typically, it will also be the point at which the object
954 * file actually gets _written_.
956 * One thing the cleanup routine should always do is to close
957 * the output file pointer.
959 void (*cleanup)(void);
962 * List of pragma facility names that apply to this backend.
964 const struct pragma_facility *pragmas;
968 * Output format driver alias
970 struct ofmt_alias {
971 const char *shortname;
972 const char *fullname;
973 const struct ofmt *ofmt;
976 extern const struct ofmt *ofmt;
977 extern FILE *ofile;
980 * ------------------------------------------------------------
981 * The data structure defining a debug format driver, and the
982 * interfaces to the functions therein.
983 * ------------------------------------------------------------
986 struct dfmt {
988 * This is a short (one-liner) description of the type of
989 * output generated by the driver.
991 const char *fullname;
994 * This is a single keyword used to select the driver.
996 const char *shortname;
999 * init - called initially to set up local pointer to object format.
1001 void (*init)(void);
1004 * linenum - called any time there is output with a change of
1005 * line number or file.
1007 void (*linenum)(const char *filename, int32_t linenumber, int32_t segto);
1010 * debug_deflabel - called whenever a label is defined. Parameters
1011 * are the same as to 'symdef()' in the output format. This function
1012 * is called after the output format version.
1015 void (*debug_deflabel)(char *name, int32_t segment, int64_t offset,
1016 int is_global, char *special);
1018 * debug_directive - called whenever a DEBUG directive other than 'LINE'
1019 * is encountered. 'directive' contains the first parameter to the
1020 * DEBUG directive, and params contains the rest. For example,
1021 * 'DEBUG VAR _somevar:int' would translate to a call to this
1022 * function with 'directive' equal to "VAR" and 'params' equal to
1023 * "_somevar:int".
1025 void (*debug_directive)(const char *directive, const char *params);
1028 * typevalue - called whenever the assembler wishes to register a type
1029 * for the last defined label. This routine MUST detect if a type was
1030 * already registered and not re-register it.
1032 void (*debug_typevalue)(int32_t type);
1035 * debug_output - called whenever output is required
1036 * 'type' is the type of info required, and this is format-specific
1038 void (*debug_output)(int type, void *param);
1041 * cleanup - called after processing of file is complete
1043 void (*cleanup)(void);
1046 * List of pragma facility names that apply to this backend.
1048 const struct pragma_facility *pragmas;
1051 extern const struct dfmt *dfmt;
1054 * The type definition macros
1055 * for debugging
1057 * low 3 bits: reserved
1058 * next 5 bits: type
1059 * next 24 bits: number of elements for arrays (0 for labels)
1062 #define TY_UNKNOWN 0x00
1063 #define TY_LABEL 0x08
1064 #define TY_BYTE 0x10
1065 #define TY_WORD 0x18
1066 #define TY_DWORD 0x20
1067 #define TY_FLOAT 0x28
1068 #define TY_QWORD 0x30
1069 #define TY_TBYTE 0x38
1070 #define TY_OWORD 0x40
1071 #define TY_YWORD 0x48
1072 #define TY_ZWORD 0x50
1073 #define TY_COMMON 0xE0
1074 #define TY_SEG 0xE8
1075 #define TY_EXTERN 0xF0
1076 #define TY_EQU 0xF8
1078 #define TYM_TYPE(x) ((x) & 0xF8)
1079 #define TYM_ELEMENTS(x) (((x) & 0xFFFFFF00) >> 8)
1081 #define TYS_ELEMENTS(x) ((x) << 8)
1083 enum special_tokens {
1084 SPECIAL_ENUM_START = PREFIX_ENUM_LIMIT,
1085 S_ABS = SPECIAL_ENUM_START,
1086 S_BYTE,
1087 S_DWORD,
1088 S_FAR,
1089 S_LONG,
1090 S_NEAR,
1091 S_NOSPLIT,
1092 S_OWORD,
1093 S_QWORD,
1094 S_REL,
1095 S_SHORT,
1096 S_STRICT,
1097 S_TO,
1098 S_TWORD,
1099 S_WORD,
1100 S_YWORD,
1101 S_ZWORD,
1102 SPECIAL_ENUM_LIMIT
1105 enum decorator_tokens {
1106 DECORATOR_ENUM_START = SPECIAL_ENUM_LIMIT,
1107 BRC_1TO2 = DECORATOR_ENUM_START,
1108 BRC_1TO4,
1109 BRC_1TO8,
1110 BRC_1TO16,
1111 BRC_RN,
1112 BRC_RD,
1113 BRC_RU,
1114 BRC_RZ,
1115 BRC_SAE,
1116 BRC_Z,
1117 DECORATOR_ENUM_LIMIT
1121 * AVX512 Decorator (decoflags_t) bits distribution (counted from 0)
1122 * 3 2 1
1123 * 10987654321098765432109876543210
1125 * | word boundary
1126 * ............................1111 opmask
1127 * ...........................1.... zeroing / merging
1128 * ..........................1..... broadcast
1129 * .........................1...... static rounding
1130 * ........................1....... SAE
1131 * ......................11........ broadcast element size
1132 * ....................11.......... number of broadcast elements
1134 #define OP_GENVAL(val, bits, shift) (((val) & ((UINT64_C(1) << (bits)) - 1)) << (shift))
1137 * Opmask register number
1138 * identical to EVEX.aaa
1140 * Bits: 0 - 3
1142 #define OPMASK_SHIFT (0)
1143 #define OPMASK_BITS (4)
1144 #define OPMASK_MASK OP_GENMASK(OPMASK_BITS, OPMASK_SHIFT)
1145 #define GEN_OPMASK(bit) OP_GENBIT(bit, OPMASK_SHIFT)
1146 #define VAL_OPMASK(val) OP_GENVAL(val, OPMASK_BITS, OPMASK_SHIFT)
1149 * zeroing / merging control available
1150 * matching to EVEX.z
1152 * Bits: 4
1154 #define Z_SHIFT (4)
1155 #define Z_BITS (1)
1156 #define Z_MASK OP_GENMASK(Z_BITS, Z_SHIFT)
1157 #define GEN_Z(bit) OP_GENBIT(bit, Z_SHIFT)
1160 * broadcast - Whether this operand can be broadcasted
1162 * Bits: 5
1164 #define BRDCAST_SHIFT (5)
1165 #define BRDCAST_BITS (1)
1166 #define BRDCAST_MASK OP_GENMASK(BRDCAST_BITS, BRDCAST_SHIFT)
1167 #define GEN_BRDCAST(bit) OP_GENBIT(bit, BRDCAST_SHIFT)
1170 * Whether this instruction can have a static rounding mode.
1171 * It goes with the last simd operand because the static rounding mode
1172 * decorator is located between the last simd operand and imm8 (if any).
1174 * Bits: 6
1176 #define STATICRND_SHIFT (6)
1177 #define STATICRND_BITS (1)
1178 #define STATICRND_MASK OP_GENMASK(STATICRND_BITS, STATICRND_SHIFT)
1179 #define GEN_STATICRND(bit) OP_GENBIT(bit, STATICRND_SHIFT)
1182 * SAE(Suppress all exception) available
1184 * Bits: 7
1186 #define SAE_SHIFT (7)
1187 #define SAE_BITS (1)
1188 #define SAE_MASK OP_GENMASK(SAE_BITS, SAE_SHIFT)
1189 #define GEN_SAE(bit) OP_GENBIT(bit, SAE_SHIFT)
1192 * Broadcasting element size.
1194 * Bits: 8 - 9
1196 #define BRSIZE_SHIFT (8)
1197 #define BRSIZE_BITS (2)
1198 #define BRSIZE_MASK OP_GENMASK(BRSIZE_BITS, BRSIZE_SHIFT)
1199 #define GEN_BRSIZE(bit) OP_GENBIT(bit, BRSIZE_SHIFT)
1201 #define BR_BITS32 GEN_BRSIZE(0)
1202 #define BR_BITS64 GEN_BRSIZE(1)
1205 * Number of broadcasting elements
1207 * Bits: 10 - 11
1209 #define BRNUM_SHIFT (10)
1210 #define BRNUM_BITS (2)
1211 #define BRNUM_MASK OP_GENMASK(BRNUM_BITS, BRNUM_SHIFT)
1212 #define VAL_BRNUM(val) OP_GENVAL(val, BRNUM_BITS, BRNUM_SHIFT)
1214 #define BR_1TO2 VAL_BRNUM(0)
1215 #define BR_1TO4 VAL_BRNUM(1)
1216 #define BR_1TO8 VAL_BRNUM(2)
1217 #define BR_1TO16 VAL_BRNUM(3)
1219 #define MASK OPMASK_MASK /* Opmask (k1 ~ 7) can be used */
1220 #define Z Z_MASK
1221 #define B32 (BRDCAST_MASK|BR_BITS32) /* {1to16} : broadcast 32b * 16 to zmm(512b) */
1222 #define B64 (BRDCAST_MASK|BR_BITS64) /* {1to8} : broadcast 64b * 8 to zmm(512b) */
1223 #define ER STATICRND_MASK /* ER(Embedded Rounding) == Static rounding mode */
1224 #define SAE SAE_MASK /* SAE(Suppress All Exception) */
1227 * Global modes
1231 * This declaration passes the "pass" number to all other modules
1232 * "pass0" assumes the values: 0, 0, ..., 0, 1, 2
1233 * where 0 = optimizing pass
1234 * 1 = pass 1
1235 * 2 = pass 2
1238 extern int pass0;
1239 extern int passn; /* Actual pass number */
1241 extern bool tasm_compatible_mode;
1242 extern int optimizing;
1243 extern int globalbits; /* 16, 32 or 64-bit mode */
1244 extern int globalrel; /* default to relative addressing? */
1245 extern int globalbnd; /* default to using bnd prefix? */
1247 #endif