test/Makefile: output dependency information files
[nasm/externdefs2.git] / include / nasm.h
blobd28fe38c41ad0741a98589957329ed8b8e994848
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_ADDRESS, /* An address (symbol value) */
104 OUT_RELADDR, /* A relative address */
105 OUT_SEGMENT, /* A segment number */
108 * These values are used by the legacy backend interface only;
109 * see output/legacy.c for more information. These should never
110 * be used otherwise. Once all backends have been migrated to the
111 * new interface they should be removed.
113 OUT_REL1ADR,
114 OUT_REL2ADR,
115 OUT_REL4ADR,
116 OUT_REL8ADR
119 enum out_sign {
120 OUT_WRAP, /* Undefined signedness (wraps) */
121 OUT_SIGNED, /* Value is signed */
122 OUT_UNSIGNED /* Value is unsigned */
126 * The data we send down to the backend.
127 * XXX: We still want to push down the base address symbol if
128 * available, and replace the segment numbers with a structure.
130 struct out_data {
131 int64_t offset; /* Offset within segment */
132 int32_t segment; /* Segment written to */
133 enum out_type type; /* See above */
134 enum out_sign sign; /* See above */
135 int inslen; /* Length of instruction */
136 int insoffs; /* Offset inside instruction */
137 int bits; /* Bits mode of compilation */
138 uint64_t size; /* Size of output */
139 const struct itemplate *itemp; /* Instruction template */
140 const void *data; /* Data for OUT_RAWDATA */
141 uint64_t toffset; /* Target address offset for relocation */
142 int32_t tsegment; /* Target segment for relocation */
143 int32_t twrt; /* Relocation with respect to */
144 int64_t relbase; /* Relative base for OUT_RELADDR */
148 * A label-lookup function.
150 typedef bool (*lfunc)(char *label, int32_t *segment, int64_t *offset);
153 * And a label-definition function. The boolean parameter
154 * `is_norm' states whether the label is a `normal' label (which
155 * should affect the local-label system), or something odder like
156 * an EQU or a segment-base symbol, which shouldn't.
158 typedef void (*ldfunc)(char *label, int32_t segment, int64_t offset,
159 char *special, bool is_norm, bool isextrn);
161 void define_label(char *label, int32_t segment, int64_t offset,
162 char *special, bool is_norm, bool isextrn);
165 * Token types returned by the scanner, in addition to ordinary
166 * ASCII character values, and zero for end-of-string.
168 enum token_type { /* token types, other than chars */
169 TOKEN_INVALID = -1, /* a placeholder value */
170 TOKEN_EOS = 0, /* end of string */
171 TOKEN_EQ = '=',
172 TOKEN_GT = '>',
173 TOKEN_LT = '<', /* aliases */
174 TOKEN_ID = 256, /* identifier */
175 TOKEN_NUM, /* numeric constant */
176 TOKEN_ERRNUM, /* malformed numeric constant */
177 TOKEN_STR, /* string constant */
178 TOKEN_ERRSTR, /* unterminated string constant */
179 TOKEN_FLOAT, /* floating-point constant */
180 TOKEN_REG, /* register name */
181 TOKEN_INSN, /* instruction name */
182 TOKEN_HERE, /* $ */
183 TOKEN_BASE, /* $$ */
184 TOKEN_SPECIAL, /* BYTE, WORD, DWORD, QWORD, FAR, NEAR, etc */
185 TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */
186 TOKEN_SHL, /* << */
187 TOKEN_SHR, /* >> */
188 TOKEN_SDIV, /* // */
189 TOKEN_SMOD, /* %% */
190 TOKEN_GE, /* >= */
191 TOKEN_LE, /* <= */
192 TOKEN_NE, /* <> (!= is same as <>) */
193 TOKEN_DBL_AND, /* && */
194 TOKEN_DBL_OR, /* || */
195 TOKEN_DBL_XOR, /* ^^ */
196 TOKEN_SEG, /* SEG */
197 TOKEN_WRT, /* WRT */
198 TOKEN_FLOATIZE, /* __floatX__ */
199 TOKEN_STRFUNC, /* __utf16*__, __utf32*__ */
200 TOKEN_IFUNC, /* __ilog2*__ */
201 TOKEN_DECORATOR, /* decorators such as {...} */
202 TOKEN_OPMASK /* translated token for opmask registers */
205 enum floatize {
206 FLOAT_8,
207 FLOAT_16,
208 FLOAT_32,
209 FLOAT_64,
210 FLOAT_80M,
211 FLOAT_80E,
212 FLOAT_128L,
213 FLOAT_128H
216 /* Must match the list in string_transform(), in strfunc.c */
217 enum strfunc {
218 STRFUNC_UTF16,
219 STRFUNC_UTF16LE,
220 STRFUNC_UTF16BE,
221 STRFUNC_UTF32,
222 STRFUNC_UTF32LE,
223 STRFUNC_UTF32BE
226 enum ifunc {
227 IFUNC_ILOG2E,
228 IFUNC_ILOG2W,
229 IFUNC_ILOG2F,
230 IFUNC_ILOG2C
233 size_t string_transform(char *, size_t, char **, enum strfunc);
236 * The expression evaluator must be passed a scanner function; a
237 * standard scanner is provided as part of nasmlib.c. The
238 * preprocessor will use a different one. Scanners, and the
239 * token-value structures they return, look like this.
241 * The return value from the scanner is always a copy of the
242 * `t_type' field in the structure.
244 struct tokenval {
245 char *t_charptr;
246 int64_t t_integer;
247 int64_t t_inttwo;
248 enum token_type t_type;
249 int8_t t_flag;
251 typedef int (*scanner)(void *private_data, struct tokenval *tv);
253 struct location {
254 int64_t offset;
255 int32_t segment;
256 int known;
258 extern struct location location;
261 * Expression-evaluator datatype. Expressions, within the
262 * evaluator, are stored as an array of these beasts, terminated by
263 * a record with type==0. Mostly, it's a vector type: each type
264 * denotes some kind of a component, and the value denotes the
265 * multiple of that component present in the expression. The
266 * exception is the WRT type, whose `value' field denotes the
267 * segment to which the expression is relative. These segments will
268 * be segment-base types, i.e. either odd segment values or SEG_ABS
269 * types. So it is still valid to assume that anything with a
270 * `value' field of zero is insignificant.
272 typedef struct {
273 int32_t type; /* a register, or EXPR_xxx */
274 int64_t value; /* must be >= 32 bits */
275 } expr;
278 * Library routines to manipulate expression data types.
280 bool is_reloc(const expr *vect);
281 bool is_simple(const expr *vect);
282 bool is_really_simple(const expr *vect);
283 bool is_unknown(const expr *vect);
284 bool is_just_unknown(const expr *vect);
285 int64_t reloc_value(const expr *vect);
286 int32_t reloc_seg(const expr *vect);
287 int32_t reloc_wrt(const expr *vect);
288 bool is_self_relative(const expr *vect);
289 void dump_expr(const expr *vect);
292 * The evaluator can also return hints about which of two registers
293 * used in an expression should be the base register. See also the
294 * `operand' structure.
296 struct eval_hints {
297 int64_t base;
298 int type;
302 * The actual expression evaluator function looks like this. When
303 * called, it expects the first token of its expression to already
304 * be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
305 * it will start by calling the scanner.
307 * If a forward reference happens during evaluation, the evaluator
308 * must set `*fwref' to true if `fwref' is non-NULL.
310 * `critical' is non-zero if the expression may not contain forward
311 * references. The evaluator will report its own error if this
312 * occurs; if `critical' is 1, the error will be "symbol not
313 * defined before use", whereas if `critical' is 2, the error will
314 * be "symbol undefined".
316 * If `critical' has bit 8 set (in addition to its main value: 0x101
317 * and 0x102 correspond to 1 and 2) then an extended expression
318 * syntax is recognised, in which relational operators such as =, <
319 * and >= are accepted, as well as low-precedence logical operators
320 * &&, ^^ and ||.
322 * If `hints' is non-NULL, it gets filled in with some hints as to
323 * the base register in complex effective addresses.
325 #define CRITICAL 0x100
326 typedef expr *(*evalfunc)(scanner sc, void *scprivate,
327 struct tokenval *tv, int *fwref, int critical,
328 struct eval_hints *hints);
331 * Special values for expr->type.
332 * These come after EXPR_REG_END as defined in regs.h.
333 * Expr types : 0 ~ EXPR_REG_END, EXPR_UNKNOWN, EXPR_...., EXPR_RDSAE,
334 * EXPR_SEGBASE ~ EXPR_SEGBASE + SEG_ABS, ...
336 #define EXPR_UNKNOWN (EXPR_REG_END+1) /* forward references */
337 #define EXPR_SIMPLE (EXPR_REG_END+2)
338 #define EXPR_WRT (EXPR_REG_END+3)
339 #define EXPR_RDSAE (EXPR_REG_END+4)
340 #define EXPR_SEGBASE (EXPR_REG_END+5)
343 * preprocessors ought to look like this:
345 struct preproc_ops {
347 * Called once at the very start of assembly.
349 void (*init)(void);
352 * Called at the start of a pass; given a file name, the number
353 * of the pass, an error reporting function, an evaluator
354 * function, and a listing generator to talk to.
356 void (*reset)(char *file, int pass, StrList **deplist);
359 * Called to fetch a line of preprocessed source. The line
360 * returned has been malloc'ed, and so should be freed after
361 * use.
363 char *(*getline)(void);
365 /* Called at the end of a pass */
366 void (*cleanup)(int pass);
368 /* Additional macros specific to output format */
369 void (*extra_stdmac)(macros_t *macros);
371 /* Early definitions and undefinitions for macros */
372 void (*pre_define)(char *definition);
373 void (*pre_undefine)(char *definition);
375 /* Include file from command line */
376 void (*pre_include)(char *fname);
378 /* Include path from command line */
379 void (*include_path)(char *path);
381 /* Unwind the macro stack when printing an error message */
382 void (*error_list_macros)(int severity);
385 extern const struct preproc_ops nasmpp;
386 extern const struct preproc_ops preproc_nop;
388 /* List of dependency files */
389 extern StrList *depend_list;
392 * Some lexical properties of the NASM source language, included
393 * here because they are shared between the parser and preprocessor.
397 * isidstart matches any character that may start an identifier, and isidchar
398 * matches any character that may appear at places other than the start of an
399 * identifier. E.g. a period may only appear at the start of an identifier
400 * (for local labels), whereas a number may appear anywhere *but* at the
401 * start.
402 * isbrcchar matches any character that may placed inside curly braces as a
403 * decorator. E.g. {rn-sae}, {1to8}, {k1}{z}
406 #define isidstart(c) (nasm_isalpha(c) || \
407 (c) == '_' || \
408 (c) == '.' || \
409 (c) == '?' || \
410 (c) == '@')
412 #define isidchar(c) (isidstart(c) || \
413 nasm_isdigit(c) || \
414 (c) == '$' || \
415 (c) == '#' || \
416 (c) == '~')
418 #define isbrcchar(c) (isidchar(c) || \
419 (c) == '-')
421 /* Ditto for numeric constants. */
423 #define isnumstart(c) (nasm_isdigit(c) || (c) == '$')
424 #define isnumchar(c) (nasm_isalnum(c) || (c) == '_')
427 * inline function to skip past an identifier; returns the first character past
428 * the identifier if valid, otherwise NULL.
430 static inline char *nasm_skip_identifier(const char *str)
432 const char *p = str;
434 if (!isidstart(*p++)) {
435 p = NULL;
436 } else {
437 while (isidchar(*p++))
440 return (char *)p;
444 * Data-type flags that get passed to listing-file routines.
446 enum {
447 LIST_READ,
448 LIST_MACRO,
449 LIST_MACRO_NOLIST,
450 LIST_INCLUDE,
451 LIST_INCBIN,
452 LIST_TIMES
456 * -----------------------------------------------------------
457 * Format of the `insn' structure returned from `parser.c' and
458 * passed into `assemble.c'
459 * -----------------------------------------------------------
462 /* Verify value to be a valid register */
463 static inline bool is_register(int reg)
465 return reg >= EXPR_REG_START && reg < REG_ENUM_LIMIT;
468 enum ccode { /* condition code names */
469 C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
470 C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
471 C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z,
472 C_none = -1
476 * token flags
478 #define TFLAG_BRC (1 << 0) /* valid only with braces. {1to8}, {rd-sae}, ...*/
479 #define TFLAG_BRC_OPT (1 << 1) /* may or may not have braces. opmasks {k1} */
480 #define TFLAG_BRC_ANY (TFLAG_BRC | TFLAG_BRC_OPT)
481 #define TFLAG_BRDCAST (1 << 2) /* broadcasting decorator */
482 #define TFLAG_WARN (1 << 3) /* warning only, treat as ID */
484 static inline uint8_t get_cond_opcode(enum ccode c)
486 static const uint8_t ccode_opcodes[] = {
487 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xf, 0xd, 0xc, 0xe, 0x6, 0x2,
488 0x3, 0x7, 0x3, 0x5, 0xe, 0xc, 0xd, 0xf, 0x1, 0xb, 0x9, 0x5,
489 0x0, 0xa, 0xa, 0xb, 0x8, 0x4
492 return ccode_opcodes[(int)c];
496 * REX flags
498 #define REX_MASK 0x4f /* Actual REX prefix bits */
499 #define REX_B 0x01 /* ModRM r/m extension */
500 #define REX_X 0x02 /* SIB index extension */
501 #define REX_R 0x04 /* ModRM reg extension */
502 #define REX_W 0x08 /* 64-bit operand size */
503 #define REX_L 0x20 /* Use LOCK prefix instead of REX.R */
504 #define REX_P 0x40 /* REX prefix present/required */
505 #define REX_H 0x80 /* High register present, REX forbidden */
506 #define REX_V 0x0100 /* Instruction uses VEX/XOP instead of REX */
507 #define REX_NH 0x0200 /* Instruction which doesn't use high regs */
508 #define REX_EV 0x0400 /* Instruction uses EVEX instead of REX */
511 * EVEX bit field
513 #define EVEX_P0MM 0x0f /* EVEX P[3:0] : Opcode map */
514 #define EVEX_P0RP 0x10 /* EVEX P[4] : High-16 reg */
515 #define EVEX_P0X 0x40 /* EVEX P[6] : High-16 rm */
516 #define EVEX_P1PP 0x03 /* EVEX P[9:8] : Legacy prefix */
517 #define EVEX_P1VVVV 0x78 /* EVEX P[14:11] : NDS register */
518 #define EVEX_P1W 0x80 /* EVEX P[15] : Osize extension */
519 #define EVEX_P2AAA 0x07 /* EVEX P[18:16] : Embedded opmask */
520 #define EVEX_P2VP 0x08 /* EVEX P[19] : High-16 NDS reg */
521 #define EVEX_P2B 0x10 /* EVEX P[20] : Broadcast / RC / SAE */
522 #define EVEX_P2LL 0x60 /* EVEX P[22:21] : Vector length */
523 #define EVEX_P2RC EVEX_P2LL /* EVEX P[22:21] : Rounding control */
524 #define EVEX_P2Z 0x80 /* EVEX P[23] : Zeroing/Merging */
527 * REX_V "classes" (prefixes which behave like VEX)
529 enum vex_class {
530 RV_VEX = 0, /* C4/C5 */
531 RV_XOP = 1, /* 8F */
532 RV_EVEX = 2 /* 62 */
536 * Note that because segment registers may be used as instruction
537 * prefixes, we must ensure the enumerations for prefixes and
538 * register names do not overlap.
540 enum prefixes { /* instruction prefixes */
541 P_none = 0,
542 PREFIX_ENUM_START = REG_ENUM_LIMIT,
543 P_A16 = PREFIX_ENUM_START,
544 P_A32,
545 P_A64,
546 P_ASP,
547 P_LOCK,
548 P_O16,
549 P_O32,
550 P_O64,
551 P_OSP,
552 P_REP,
553 P_REPE,
554 P_REPNE,
555 P_REPNZ,
556 P_REPZ,
557 P_TIMES,
558 P_WAIT,
559 P_XACQUIRE,
560 P_XRELEASE,
561 P_BND,
562 P_NOBND,
563 P_EVEX,
564 P_VEX3,
565 P_VEX2,
566 PREFIX_ENUM_LIMIT
569 enum extop_type { /* extended operand types */
570 EOT_NOTHING,
571 EOT_DB_STRING, /* Byte string */
572 EOT_DB_STRING_FREE, /* Byte string which should be nasm_free'd*/
573 EOT_DB_NUMBER /* Integer */
576 enum ea_flags { /* special EA flags */
577 EAF_BYTEOFFS = 1, /* force offset part to byte size */
578 EAF_WORDOFFS = 2, /* force offset part to [d]word size */
579 EAF_TIMESTWO = 4, /* really do EAX*2 not EAX+EAX */
580 EAF_REL = 8, /* IP-relative addressing */
581 EAF_ABS = 16, /* non-IP-relative addressing */
582 EAF_FSGS = 32, /* fs/gs segment override present */
583 EAF_MIB = 64 /* mib operand */
586 enum eval_hint { /* values for `hinttype' */
587 EAH_NOHINT = 0, /* no hint at all - our discretion */
588 EAH_MAKEBASE = 1, /* try to make given reg the base */
589 EAH_NOTBASE = 2, /* try _not_ to make reg the base */
590 EAH_SUMMED = 3 /* base and index are summed into index */
593 typedef struct operand { /* operand to an instruction */
594 opflags_t type; /* type of operand */
595 int disp_size; /* 0 means default; 16; 32; 64 */
596 enum reg_enum basereg;
597 enum reg_enum indexreg; /* address registers */
598 int scale; /* index scale */
599 int hintbase;
600 enum eval_hint hinttype; /* hint as to real base register */
601 int32_t segment; /* immediate segment, if needed */
602 int64_t offset; /* any immediate number */
603 int32_t wrt; /* segment base it's relative to */
604 int eaflags; /* special EA flags */
605 int opflags; /* see OPFLAG_* defines below */
606 decoflags_t decoflags; /* decorator flags such as {...} */
607 } operand;
609 #define OPFLAG_FORWARD 1 /* operand is a forward reference */
610 #define OPFLAG_EXTERN 2 /* operand is an external reference */
611 #define OPFLAG_UNKNOWN 4 /* operand is an unknown reference
612 (always a forward reference also) */
613 #define OPFLAG_RELATIVE 8 /* operand is self-relative, e.g. [foo - $]
614 where foo is not in the current segment */
616 typedef struct extop { /* extended operand */
617 struct extop *next; /* linked list */
618 char *stringval; /* if it's a string, then here it is */
619 size_t stringlen; /* ... and here's how long it is */
620 int64_t offset; /* ... it's given here ... */
621 int32_t segment; /* if it's a number/address, then... */
622 int32_t wrt; /* ... and here */
623 bool relative; /* self-relative expression */
624 enum extop_type type; /* defined above */
625 } extop;
627 enum ea_type {
628 EA_INVALID, /* Not a valid EA at all */
629 EA_SCALAR, /* Scalar EA */
630 EA_XMMVSIB, /* XMM vector EA */
631 EA_YMMVSIB, /* YMM vector EA */
632 EA_ZMMVSIB /* ZMM vector EA */
636 * Prefix positions: each type of prefix goes in a specific slot.
637 * This affects the final ordering of the assembled output, which
638 * shouldn't matter to the processor, but if you have stylistic
639 * preferences, you can change this. REX prefixes are handled
640 * differently for the time being.
642 * LOCK and REP used to be one slot; this is no longer the case since
643 * the introduction of HLE.
645 enum prefix_pos {
646 PPS_WAIT, /* WAIT (technically not a prefix!) */
647 PPS_REP, /* REP/HLE prefix */
648 PPS_LOCK, /* LOCK prefix */
649 PPS_SEG, /* Segment override prefix */
650 PPS_OSIZE, /* Operand size prefix */
651 PPS_ASIZE, /* Address size prefix */
652 PPS_VEX, /* VEX type */
653 MAXPREFIX /* Total number of prefix slots */
657 * Tuple types that are used when determining Disp8*N eligibility
658 * The order must match with a hash %tuple_codes in insns.pl
660 enum ttypes {
661 FV = 001,
662 HV = 002,
663 FVM = 003,
664 T1S8 = 004,
665 T1S16 = 005,
666 T1S = 006,
667 T1F32 = 007,
668 T1F64 = 010,
669 T2 = 011,
670 T4 = 012,
671 T8 = 013,
672 HVM = 014,
673 QVM = 015,
674 OVM = 016,
675 M128 = 017,
676 DUP = 020
679 /* EVEX.L'L : Vector length on vector insns */
680 enum vectlens {
681 VL128 = 0,
682 VL256 = 1,
683 VL512 = 2,
684 VLMAX = 3
687 /* If you need to change this, also change it in insns.pl */
688 #define MAX_OPERANDS 5
690 typedef struct insn { /* an instruction itself */
691 char *label; /* the label defined, or NULL */
692 int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
693 enum opcode opcode; /* the opcode - not just the string */
694 enum ccode condition; /* the condition code, if Jcc/SETcc */
695 int operands; /* how many operands? 0-3 (more if db et al) */
696 int addr_size; /* address size */
697 operand oprs[MAX_OPERANDS]; /* the operands, defined as above */
698 extop *eops; /* extended operands */
699 int eops_float; /* true if DD and floating */
700 int32_t times; /* repeat count (TIMES prefix) */
701 bool forw_ref; /* is there a forward reference? */
702 bool rex_done; /* REX prefix emitted? */
703 int rex; /* Special REX Prefix */
704 int vexreg; /* Register encoded in VEX prefix */
705 int vex_cm; /* Class and M field for VEX prefix */
706 int vex_wlp; /* W, P and L information for VEX prefix */
707 uint8_t evex_p[3]; /* EVEX.P0: [RXB,R',00,mm], P1: [W,vvvv,1,pp] */
708 /* EVEX.P2: [z,L'L,b,V',aaa] */
709 enum ttypes evex_tuple; /* Tuple type for compressed Disp8*N */
710 int evex_rm; /* static rounding mode for AVX512 (EVEX) */
711 int8_t evex_brerop; /* BR/ER/SAE operand position */
712 } insn;
714 /* Instruction flags type: IF_* flags are defined in insns.h */
715 typedef uint64_t iflags_t;
718 * What to return from a directive- or pragma-handling function.
719 * Currently DIRR_OK and DIRR_ERROR are treated the same way;
720 * in both cases the backend is expected to produce the appropriate
721 * error message on its own.
723 * DIRR_BADPARAM causes a generic error message to be printed. Note
724 * that it is an error, not a warning, even in the case of pragmas;
725 * don't use it where forward compatiblity would be compromised
726 * (instead consider adding a DIRR_WARNPARAM.)
728 enum directive_result {
729 DIRR_UNKNOWN, /* Directive not handled by backend */
730 DIRR_OK, /* Directive processed */
731 DIRR_ERROR, /* Directive processed unsuccessfully */
732 DIRR_BADPARAM /* Print bad argument error message */
736 * A pragma facility: this structure is used to request passing a
737 * parsed pragma directive for a specific facility. If the handler is
738 * NULL then this pragma facility is recognized but ignored; pragma
739 * processing stops at that point.
741 * Note that the handler is passed a pointer to the facility structure
742 * as part of the struct pragma.
744 struct pragma;
746 struct pragma_facility {
747 const char *name;
748 enum directive_result (*handler)(const struct pragma *);
752 * This structure defines how a pragma directive is passed to a
753 * facility. This structure may be augmented in the future.
755 * Any facility MAY, but is not required to, add its operations
756 * keywords or a subset thereof into asm/directiv.dat, in which case
757 * the "opcode" field will be set to the corresponding D_ constant
758 * from directiv.h; otherwise it will be D_unknown.
760 struct pragma {
761 const struct pragma_facility *facility;
762 const char *facility_name; /* Facility name exactly as entered by user */
763 const char *opname; /* First word after the facility name */
764 const char *tail; /* Anything after the operation */
765 enum directive opcode; /* Operation as a D_ directives constant */
769 * The data structure defining an output format driver, and the
770 * interfaces to the functions therein.
772 struct ofmt {
774 * This is a short (one-liner) description of the type of
775 * output generated by the driver.
777 const char *fullname;
780 * This is a single keyword used to select the driver.
782 const char *shortname;
785 * Output format flags.
787 #define OFMT_TEXT 1 /* Text file format */
788 unsigned int flags;
790 int maxbits; /* Maximum segment bits supported */
793 * this is a pointer to the first element of the debug information
795 const struct dfmt * const *debug_formats;
798 * the default debugging format if -F is not specified
800 const struct dfmt *default_dfmt;
803 * This, if non-NULL, is a NULL-terminated list of `char *'s
804 * pointing to extra standard macros supplied by the object
805 * format (e.g. a sensible initial default value of __SECT__,
806 * and user-level equivalents for any format-specific
807 * directives).
809 macros_t *stdmac;
812 * This procedure is called at the start of an output session to set
813 * up internal parameters.
815 void (*init)(void);
818 * This is the modern output function, which gets passed
819 * a struct out_data with much more information. See the
820 * definition of struct out_data.
822 void (*output)(const struct out_data *data);
825 * This procedure is called by assemble() to write actual
826 * generated code or data to the object file. Typically it
827 * doesn't have to actually _write_ it, just store it for
828 * later.
830 * The `type' argument specifies the type of output data, and
831 * usually the size as well: its contents are described below.
833 * This is used for backends which have not yet been ported to
834 * the new interface, and should be NULL on ported backends.
835 * To use this entry point, set the output pointer to
836 * nasm_do_legacy_output.
838 void (*legacy_output)(int32_t segto, const void *data,
839 enum out_type type, uint64_t size,
840 int32_t segment, int32_t wrt);
843 * This procedure is called once for every symbol defined in
844 * the module being assembled. It gives the name and value of
845 * the symbol, in NASM's terms, and indicates whether it has
846 * been declared to be global. Note that the parameter "name",
847 * when passed, will point to a piece of static storage
848 * allocated inside the label manager - it's safe to keep using
849 * that pointer, because the label manager doesn't clean up
850 * until after the output driver has.
852 * Values of `is_global' are: 0 means the symbol is local; 1
853 * means the symbol is global; 2 means the symbol is common (in
854 * which case `offset' holds the _size_ of the variable).
855 * Anything else is available for the output driver to use
856 * internally.
858 * This routine explicitly _is_ allowed to call the label
859 * manager to define further symbols, if it wants to, even
860 * though it's been called _from_ the label manager. That much
861 * re-entrancy is guaranteed in the label manager. However, the
862 * label manager will in turn call this routine, so it should
863 * be prepared to be re-entrant itself.
865 * The `special' parameter contains special information passed
866 * through from the command that defined the label: it may have
867 * been an EXTERN, a COMMON or a GLOBAL. The distinction should
868 * be obvious to the output format from the other parameters.
870 void (*symdef)(char *name, int32_t segment, int64_t offset,
871 int is_global, char *special);
874 * This procedure is called when the source code requests a
875 * segment change. It should return the corresponding segment
876 * _number_ for the name, or NO_SEG if the name is not a valid
877 * segment name.
879 * It may also be called with NULL, in which case it is to
880 * return the _default_ section number for starting assembly in.
882 * It is allowed to modify the string it is given a pointer to.
884 * It is also allowed to specify a default instruction size for
885 * the segment, by setting `*bits' to 16 or 32. Or, if it
886 * doesn't wish to define a default, it can leave `bits' alone.
888 int32_t (*section)(char *name, int pass, int *bits);
891 * This procedure is called to modify section alignment,
892 * note there is a trick, the alignment can only increase
894 void (*sectalign)(int32_t seg, unsigned int value);
897 * This procedure is called to modify the segment base values
898 * returned from the SEG operator. It is given a segment base
899 * value (i.e. a segment value with the low bit set), and is
900 * required to produce in return a segment value which may be
901 * different. It can map segment bases to absolute numbers by
902 * means of returning SEG_ABS types.
904 * It should return NO_SEG if the segment base cannot be
905 * determined; the evaluator (which calls this routine) is
906 * responsible for throwing an error condition if that occurs
907 * in pass two or in a critical expression.
909 int32_t (*segbase)(int32_t segment);
912 * This procedure is called to allow the output driver to
913 * process its own specific directives. When called, it has the
914 * directive word in `directive' and the parameter string in
915 * `value'. It is called in both assembly passes, and `pass'
916 * will be either 1 or 2.
918 * The following values are (currently) possible for
919 * directive_result:
921 * 0 - DIRR_UNKNOWN - directive not recognized by backend
922 * 1 - DIRR_OK - directive processed ok
923 * 2 - DIRR_ERROR - backend printed its own error message
924 * 3 - DIRR_BADPARAM - print the generic message
925 * "invalid parameter to [*] directive"
927 enum directive_result
928 (*directive)(enum directive directive, char *value, int pass);
931 * This procedure is called before anything else - even before
932 * the "init" routine - and is passed the name of the input
933 * file from which this output file is being generated. It
934 * should return its preferred name for the output file in
935 * `outname', if outname[0] is not '\0', and do nothing to
936 * `outname' otherwise. Since it is called before the driver is
937 * properly initialized, it has to be passed its error handler
938 * separately.
940 * This procedure may also take its own copy of the input file
941 * name for use in writing the output file: it is _guaranteed_
942 * that it will be called before the "init" routine.
944 * The parameter `outname' points to an area of storage
945 * guaranteed to be at least FILENAME_MAX in size.
947 void (*filename)(char *inname, char *outname);
950 * This procedure is called after assembly finishes, to allow
951 * the output driver to clean itself up and free its memory.
952 * Typically, it will also be the point at which the object
953 * file actually gets _written_.
955 * One thing the cleanup routine should always do is to close
956 * the output file pointer.
958 void (*cleanup)(void);
961 * List of pragma facility names that apply to this backend.
963 const struct pragma_facility *pragmas;
967 * Output format driver alias
969 struct ofmt_alias {
970 const char *shortname;
971 const char *fullname;
972 const struct ofmt *ofmt;
975 extern const struct ofmt *ofmt;
976 extern FILE *ofile;
979 * ------------------------------------------------------------
980 * The data structure defining a debug format driver, and the
981 * interfaces to the functions therein.
982 * ------------------------------------------------------------
985 struct dfmt {
987 * This is a short (one-liner) description of the type of
988 * output generated by the driver.
990 const char *fullname;
993 * This is a single keyword used to select the driver.
995 const char *shortname;
998 * init - called initially to set up local pointer to object format.
1000 void (*init)(void);
1003 * linenum - called any time there is output with a change of
1004 * line number or file.
1006 void (*linenum)(const char *filename, int32_t linenumber, int32_t segto);
1009 * debug_deflabel - called whenever a label is defined. Parameters
1010 * are the same as to 'symdef()' in the output format. This function
1011 * is called after the output format version.
1014 void (*debug_deflabel)(char *name, int32_t segment, int64_t offset,
1015 int is_global, char *special);
1017 * debug_directive - called whenever a DEBUG directive other than 'LINE'
1018 * is encountered. 'directive' contains the first parameter to the
1019 * DEBUG directive, and params contains the rest. For example,
1020 * 'DEBUG VAR _somevar:int' would translate to a call to this
1021 * function with 'directive' equal to "VAR" and 'params' equal to
1022 * "_somevar:int".
1024 void (*debug_directive)(const char *directive, const char *params);
1027 * typevalue - called whenever the assembler wishes to register a type
1028 * for the last defined label. This routine MUST detect if a type was
1029 * already registered and not re-register it.
1031 void (*debug_typevalue)(int32_t type);
1034 * debug_output - called whenever output is required
1035 * 'type' is the type of info required, and this is format-specific
1037 void (*debug_output)(int type, void *param);
1040 * cleanup - called after processing of file is complete
1042 void (*cleanup)(void);
1045 * List of pragma facility names that apply to this backend.
1047 const struct pragma_facility *pragmas;
1050 extern const struct dfmt *dfmt;
1053 * The type definition macros
1054 * for debugging
1056 * low 3 bits: reserved
1057 * next 5 bits: type
1058 * next 24 bits: number of elements for arrays (0 for labels)
1061 #define TY_UNKNOWN 0x00
1062 #define TY_LABEL 0x08
1063 #define TY_BYTE 0x10
1064 #define TY_WORD 0x18
1065 #define TY_DWORD 0x20
1066 #define TY_FLOAT 0x28
1067 #define TY_QWORD 0x30
1068 #define TY_TBYTE 0x38
1069 #define TY_OWORD 0x40
1070 #define TY_YWORD 0x48
1071 #define TY_ZWORD 0x50
1072 #define TY_COMMON 0xE0
1073 #define TY_SEG 0xE8
1074 #define TY_EXTERN 0xF0
1075 #define TY_EQU 0xF8
1077 #define TYM_TYPE(x) ((x) & 0xF8)
1078 #define TYM_ELEMENTS(x) (((x) & 0xFFFFFF00) >> 8)
1080 #define TYS_ELEMENTS(x) ((x) << 8)
1082 enum special_tokens {
1083 SPECIAL_ENUM_START = PREFIX_ENUM_LIMIT,
1084 S_ABS = SPECIAL_ENUM_START,
1085 S_BYTE,
1086 S_DWORD,
1087 S_FAR,
1088 S_LONG,
1089 S_NEAR,
1090 S_NOSPLIT,
1091 S_OWORD,
1092 S_QWORD,
1093 S_REL,
1094 S_SHORT,
1095 S_STRICT,
1096 S_TO,
1097 S_TWORD,
1098 S_WORD,
1099 S_YWORD,
1100 S_ZWORD,
1101 SPECIAL_ENUM_LIMIT
1104 enum decorator_tokens {
1105 DECORATOR_ENUM_START = SPECIAL_ENUM_LIMIT,
1106 BRC_1TO2 = DECORATOR_ENUM_START,
1107 BRC_1TO4,
1108 BRC_1TO8,
1109 BRC_1TO16,
1110 BRC_RN,
1111 BRC_RD,
1112 BRC_RU,
1113 BRC_RZ,
1114 BRC_SAE,
1115 BRC_Z,
1116 DECORATOR_ENUM_LIMIT
1120 * AVX512 Decorator (decoflags_t) bits distribution (counted from 0)
1121 * 3 2 1
1122 * 10987654321098765432109876543210
1124 * | word boundary
1125 * ............................1111 opmask
1126 * ...........................1.... zeroing / merging
1127 * ..........................1..... broadcast
1128 * .........................1...... static rounding
1129 * ........................1....... SAE
1130 * ......................11........ broadcast element size
1131 * ....................11.......... number of broadcast elements
1133 #define OP_GENVAL(val, bits, shift) (((val) & ((UINT64_C(1) << (bits)) - 1)) << (shift))
1136 * Opmask register number
1137 * identical to EVEX.aaa
1139 * Bits: 0 - 3
1141 #define OPMASK_SHIFT (0)
1142 #define OPMASK_BITS (4)
1143 #define OPMASK_MASK OP_GENMASK(OPMASK_BITS, OPMASK_SHIFT)
1144 #define GEN_OPMASK(bit) OP_GENBIT(bit, OPMASK_SHIFT)
1145 #define VAL_OPMASK(val) OP_GENVAL(val, OPMASK_BITS, OPMASK_SHIFT)
1148 * zeroing / merging control available
1149 * matching to EVEX.z
1151 * Bits: 4
1153 #define Z_SHIFT (4)
1154 #define Z_BITS (1)
1155 #define Z_MASK OP_GENMASK(Z_BITS, Z_SHIFT)
1156 #define GEN_Z(bit) OP_GENBIT(bit, Z_SHIFT)
1159 * broadcast - Whether this operand can be broadcasted
1161 * Bits: 5
1163 #define BRDCAST_SHIFT (5)
1164 #define BRDCAST_BITS (1)
1165 #define BRDCAST_MASK OP_GENMASK(BRDCAST_BITS, BRDCAST_SHIFT)
1166 #define GEN_BRDCAST(bit) OP_GENBIT(bit, BRDCAST_SHIFT)
1169 * Whether this instruction can have a static rounding mode.
1170 * It goes with the last simd operand because the static rounding mode
1171 * decorator is located between the last simd operand and imm8 (if any).
1173 * Bits: 6
1175 #define STATICRND_SHIFT (6)
1176 #define STATICRND_BITS (1)
1177 #define STATICRND_MASK OP_GENMASK(STATICRND_BITS, STATICRND_SHIFT)
1178 #define GEN_STATICRND(bit) OP_GENBIT(bit, STATICRND_SHIFT)
1181 * SAE(Suppress all exception) available
1183 * Bits: 7
1185 #define SAE_SHIFT (7)
1186 #define SAE_BITS (1)
1187 #define SAE_MASK OP_GENMASK(SAE_BITS, SAE_SHIFT)
1188 #define GEN_SAE(bit) OP_GENBIT(bit, SAE_SHIFT)
1191 * Broadcasting element size.
1193 * Bits: 8 - 9
1195 #define BRSIZE_SHIFT (8)
1196 #define BRSIZE_BITS (2)
1197 #define BRSIZE_MASK OP_GENMASK(BRSIZE_BITS, BRSIZE_SHIFT)
1198 #define GEN_BRSIZE(bit) OP_GENBIT(bit, BRSIZE_SHIFT)
1200 #define BR_BITS32 GEN_BRSIZE(0)
1201 #define BR_BITS64 GEN_BRSIZE(1)
1204 * Number of broadcasting elements
1206 * Bits: 10 - 11
1208 #define BRNUM_SHIFT (10)
1209 #define BRNUM_BITS (2)
1210 #define BRNUM_MASK OP_GENMASK(BRNUM_BITS, BRNUM_SHIFT)
1211 #define VAL_BRNUM(val) OP_GENVAL(val, BRNUM_BITS, BRNUM_SHIFT)
1213 #define BR_1TO2 VAL_BRNUM(0)
1214 #define BR_1TO4 VAL_BRNUM(1)
1215 #define BR_1TO8 VAL_BRNUM(2)
1216 #define BR_1TO16 VAL_BRNUM(3)
1218 #define MASK OPMASK_MASK /* Opmask (k1 ~ 7) can be used */
1219 #define Z Z_MASK
1220 #define B32 (BRDCAST_MASK|BR_BITS32) /* {1to16} : broadcast 32b * 16 to zmm(512b) */
1221 #define B64 (BRDCAST_MASK|BR_BITS64) /* {1to8} : broadcast 64b * 8 to zmm(512b) */
1222 #define ER STATICRND_MASK /* ER(Embedded Rounding) == Static rounding mode */
1223 #define SAE SAE_MASK /* SAE(Suppress All Exception) */
1226 * Global modes
1230 * This declaration passes the "pass" number to all other modules
1231 * "pass0" assumes the values: 0, 0, ..., 0, 1, 2
1232 * where 0 = optimizing pass
1233 * 1 = pass 1
1234 * 2 = pass 2
1237 extern int pass0;
1238 extern int passn; /* Actual pass number */
1240 extern bool tasm_compatible_mode;
1241 extern int optimizing;
1242 extern int globalbits; /* 16, 32 or 64-bit mode */
1243 extern int globalrel; /* default to relative addressing? */
1244 extern int globalbnd; /* default to using bnd prefix? */
1246 #endif