Debian Codegraph @

common/common.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2025 The NASM Authors - All Rights Reserved */
3
4 /*
5 * common.c - code common to nasm and ndisasm
6 */
7
8 #include "compiler.h"
9 #include "nasm.h"
10 #include "nasmlib.h"
11 #include "insns.h"
12
13 /*
14 * Per-pass global (across segments) state
15 */
16 struct globalopt globl;
17
18 /*
19 * Name of a register token, if applicable; otherwise NULL
20 */
21 const char *register_name(int token)
22 {
23 if (is_register(token))
24 return nasm_reg_names[token - EXPR_REG_START];
25 else
26 return NULL;
27 }
28
29 /*
30 * Common list of prefix names; ideally should be auto-generated
31 * from tokens.dat. This MUST match the enum in include/nasm.h.
32 */
33 const char *prefix_name(int token)
34 {
35 static const char * const
36 prefix_names[PREFIX_ENUM_LIMIT - PREFIX_ENUM_START] = {
37 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
38 "rep", "repe", "repne", "repnz", "repz", "wait",
39 "xacquire", "xrelease", "bnd", "nobnd", "{rex}", "{rex2}",
40 "{evex}", "{vex}", "{vex3}", "{vex2}", "{nf}", "{zu}",
41 "{pt}", "{pn}"
42 };
43 const char *name;
44
45 /* A register can also be a prefix */
46 name = register_name(token);
47
48 if (!name) {
49 const unsigned int prefix = token - PREFIX_ENUM_START;
50 if (prefix < ARRAY_SIZE(prefix_names))
51 name = prefix_names[prefix];
52 }
53
54 return name;
55 }
56
57 /*
58 * True for a valid hinting-NOP opcode, after 0F.
59 */
60 bool is_hint_nop(uint64_t op)
61 {
62 if (op >> 16)
63 return false;
64
65 if ((op >> 8) == 0x0f)
66 op = (uint8_t)op;
67 else if (op >> 8)
68 return false;
69
70 return ((op & ~7) == 0x18) || (op == 0x0d);
71 }