Skip to content

Commit 17b1582

Browse files
author
H. Peter Anvin
committed
Merge branch 'master' of ssh://github.com/netwide-assembler/nasm
2 parents 87d621c + 07245bc commit 17b1582

File tree

16 files changed

+339
-134
lines changed

16 files changed

+339
-134
lines changed

asm/assemble.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,12 @@ int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
934934
nasm_nonfatal("instruction not supported in %d-bit mode", bits);
935935
break;
936936
case MERR_ENCMISMATCH:
937-
nasm_nonfatal("instruction not encodable with %s prefix",
938-
prefix_name(instruction->prefixes[PPS_REX]));
937+
if (!instruction->prefixes[PPS_REX]) {
938+
nasm_nonfatal("instruction not encodable without explicit prefix");
939+
} else {
940+
nasm_nonfatal("instruction not encodable with %s prefix",
941+
prefix_name(instruction->prefixes[PPS_REX]));
942+
}
939943
break;
940944
case MERR_BADBND:
941945
case MERR_BADREPNE:
@@ -2552,6 +2556,17 @@ static enum match_result matches(const struct itemplate *itemp,
25522556
return MERR_ENCMISMATCH;
25532557
break;
25542558
default:
2559+
if (itemp_has(itemp, IF_EVEX)) {
2560+
if (!iflag_test(&cpu, IF_EVEX))
2561+
return MERR_ENCMISMATCH;
2562+
} else if (itemp_has(itemp, IF_VEX)) {
2563+
if (!iflag_test(&cpu, IF_VEX)) {
2564+
return MERR_ENCMISMATCH;
2565+
} else if (itemp_has(itemp, IF_LATEVEX)) {
2566+
if (!iflag_test(&cpu, IF_LATEVEX) && iflag_test(&cpu, IF_EVEX))
2567+
return MERR_ENCMISMATCH;
2568+
}
2569+
}
25552570
break;
25562571
}
25572572

asm/assemble.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
#include "nasm.h"
4242
#include "iflag.h"
4343

44-
extern iflag_t cpu;
44+
extern iflag_t cpu, cmd_cpu;
45+
void set_cpu(const char *cpuspec);
46+
4547
extern bool in_absolute; /* Are we in an absolute segment? */
4648
extern struct location absolute;
4749

asm/directiv.c

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* ----------------------------------------------------------------------- *
22
*
3-
* Copyright 1996-2019 The NASM Authors - All Rights Reserved
3+
* Copyright 1996-2022 The NASM Authors - All Rights Reserved
44
* See the file AUTHORS included with the NASM distribution for
55
* the specific copyright holders.
66
*
@@ -59,11 +59,25 @@ struct cpunames {
5959
/* Eventually a table of features */
6060
};
6161

62-
static iflag_t get_cpu(const char *value)
62+
static void iflag_set_cpu(iflag_t *a, unsigned int lvl)
6363
{
64-
iflag_t r;
65-
const struct cpunames *cpu;
64+
a->field[0] = 0; /* Not applicable to the CPU type */
65+
iflag_set_all_features(a); /* All feature masking bits set for now */
66+
if (lvl >= IF_ANY) {
67+
/* This is a hack for now */
68+
iflag_set(a, IF_LATEVEX);
69+
}
70+
a->field[IF_CPU_FIELD] &= ~IF_CPU_LEVEL_MASK;
71+
iflag_set(a, lvl);
72+
}
73+
74+
void set_cpu(const char *value)
75+
{
76+
const char *p;
77+
char modifier;
78+
const struct cpunames *cpuflag;
6679
static const struct cpunames cpunames[] = {
80+
{ "default", IF_DEFAULT }, /* Must be first */
6781
{ "8086", IF_8086 },
6882
{ "186", IF_186 },
6983
{ "286", IF_286 },
@@ -96,22 +110,71 @@ static iflag_t get_cpu(const char *value)
96110
{ "ivybridge", IF_FUTURE },
97111
{ "any", IF_ANY },
98112
{ "all", IF_ANY },
99-
{ "default", IF_PLEVEL },
100-
{ NULL, IF_PLEVEL } /* Error and final default entry */
113+
{ "latevex", IF_LATEVEX },
114+
{ "evex", IF_EVEX },
115+
{ "vex", IF_VEX },
116+
{ NULL, 0 }
101117
};
102118

103-
iflag_clear_all(&r);
104-
105-
for (cpu = cpunames; cpu->name; cpu++) {
106-
if (!nasm_stricmp(value, cpu->name))
107-
break;
119+
if (!value) {
120+
iflag_set_cpu(&cpu, cpunames[0].level);
121+
return;
108122
}
109123

110-
if (!cpu->name)
111-
nasm_nonfatal("unknown 'cpu' type '%s'", value);
124+
p = value;
125+
modifier = '+';
126+
while (*p) {
127+
int len = strcspn(p, " ,");
128+
129+
while (len && (*p == '+' || *p == '-' || *p == '*')) {
130+
modifier = *p++;
131+
len--;
132+
if (!len && modifier == '*')
133+
cpu = cmd_cpu;
134+
}
135+
136+
if (len) {
137+
bool invert_flag = false;
138+
139+
if (len >= 3 && !nasm_memicmp(p, "no", 2)) {
140+
invert_flag = true;
141+
p += 2;
142+
len -= 2;
143+
}
112144

113-
iflag_set_cpu(&r, cpu->level);
114-
return r;
145+
for (cpuflag = cpunames; cpuflag->name; cpuflag++)
146+
if (!nasm_strnicmp(p, cpuflag->name, len))
147+
break;
148+
149+
if (!cpuflag->name) {
150+
nasm_nonfatal("unknown CPU type or flag '%.*s'", len, p);
151+
return;
152+
}
153+
154+
if (cpuflag->level >= IF_CPU_FIRST && cpuflag->level <= IF_ANY) {
155+
iflag_set_cpu(&cpu, cpuflag->level);
156+
} else {
157+
switch (modifier) {
158+
case '-':
159+
invert_flag = !invert_flag;
160+
break;
161+
case '*':
162+
invert_flag ^= iflag_test(&cmd_cpu, cpuflag->level);
163+
break;
164+
default:
165+
break;
166+
}
167+
168+
iflag_set(&cpu, cpuflag->level);
169+
if (invert_flag)
170+
iflag_clear(&cpu, cpuflag->level);
171+
}
172+
}
173+
p += len;
174+
if (!*p)
175+
break;
176+
p++; /* Skip separator */
177+
}
115178
}
116179

117180
static int get_bits(const char *value)
@@ -358,11 +421,11 @@ bool process_directives(char *directive)
358421

359422
if (!declare_label(value, type, special))
360423
break;
361-
424+
362425
if (type == LBL_COMMON || type == LBL_EXTERN || type == LBL_REQUIRED)
363426
define_label(value, 0, size, false);
364427

365-
break;
428+
break;
366429
}
367430

368431
case D_ABSOLUTE: /* [ABSOLUTE address] */
@@ -440,7 +503,7 @@ bool process_directives(char *directive)
440503
break;
441504

442505
case D_CPU: /* [CPU] */
443-
cpu = get_cpu(value);
506+
set_cpu(value);
444507
break;
445508

446509
case D_LIST: /* [LIST {+|-}] */

asm/nasm.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ struct optimization optimizing =
131131
{ MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
132132
static int cmd_sb = 16; /* by default */
133133

134-
iflag_t cpu;
135-
static iflag_t cmd_cpu;
134+
iflag_t cpu, cmd_cpu;
136135

137136
struct location location;
138137
bool in_absolute; /* Flag we are in ABSOLUTE seg */
@@ -525,8 +524,8 @@ int main(int argc, char **argv)
525524

526525
timestamp();
527526

528-
iflag_set_default_cpu(&cpu);
529-
iflag_set_default_cpu(&cmd_cpu);
527+
set_cpu(NULL);
528+
cmd_cpu = cpu;
530529

531530
set_default_limits();
532531

doc/changes.src

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ reservations (e.g. \c{dw ?}.)
6868
\b Allow forcing an instruction in 64-bit mode to have a (possibly
6969
redundant) REX prefix, using the syntax \i\c{\{rex\}} as a prefix.
7070

71+
\b Add a \c{\{vex\}} prefix to enforce VEX (AVX) encoding of an
72+
instruction, either using the 2- or 3-byte VEX prefixes.
73+
74+
\b The \c{CPU} directive has been augmented to allow control of
75+
generation of VEX (AVX) versus EVEX (AVX-512) instruction formats, see
76+
\k{CPU}.
77+
78+
\b Some recent instructions that previously have been only available
79+
using EVEX encodings are now also encodable using VEX (AVX)
80+
encodings. For backwards compatibility these encodings are not enabled
81+
by default, but can be generated either via an explicit \c{\{vex\}}
82+
prefix or by specifying either \c{CPU LATEVEX} or \c{CPU NOEVEX}; see
83+
\k{CPU}.
84+
7185
\b Document the already existing \c{%unimacro} directive. See \k{unmacro}.
7286

7387
\b Fix a code range generation bug in the DWARF debug format
@@ -76,6 +90,10 @@ output formats. This bug happened to cancel out with a bug in older
7690
versions of the GNU binutils linker, but breaks with other linkers and
7791
updated or other linkers that expect the spec to be followed.
7892

93+
\b Fix segment symbols with addends, e.g. \c{jmp _TEXT+10h:0} in
94+
output formats that support segment relocations, e.g. the \c{obj}
95+
format.
96+
7997
\b Fix various crashes and hangs on invalid input.
8098

8199

@@ -767,9 +785,10 @@ options to indicate whether all relevant branches should be getting
767785
\c{BND} prefixes. This is expected to be the normal for use in MPX
768786
code.
769787

770-
\b Add \c{{evex}}, \c{{vex3}} and \c{{vex2}} instruction prefixes to
771-
have NASM encode the corresponding instruction, if possible, with an EVEX,
772-
3-byte VEX, or 2-byte VEX prefix, respectively.
788+
\b Add \c{\{evex\}}, \c{\{vex3\}} and \c{\{vex2\}} instruction
789+
prefixes to have NASM encode the corresponding instruction, if
790+
possible, with an EVEX, 3-byte VEX, or 2-byte VEX prefix,
791+
respectively.
773792

774793
\b Support for section names longer than 8 bytes in Win32/Win64 COFF.
775794

doc/nasmdoc.src

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,47 +5594,87 @@ are excluded from the symbol mangling and also not marked as global.
55945594
\H{CPU} \i\c{CPU}: Defining CPU Dependencies
55955595

55965596
The \i\c{CPU} directive restricts assembly to those instructions which
5597-
are available on the specified CPU.
5597+
are available on the specified CPU. At the moment, it is primarily
5598+
used to enforce unavailable \e{encodings} of instructions, such as
5599+
5-byte jumps on the 8080.
55985600

5599-
Options are:
5601+
(If someone would volunteer to work through the database and add
5602+
proper annotations to each instruction, this could be greatly
5603+
improved. Please contact the developers to volunteer, see \{contact}.)
56005604

5601-
\b\c{CPU 8086} Assemble only 8086 instruction set
5605+
Current CPU keywords are:
56025606

5603-
\b\c{CPU 186} Assemble instructions up to the 80186 instruction set
5607+
\b\c{CPU 8086} - Assemble only 8086 instruction set
56045608

5605-
\b\c{CPU 286} Assemble instructions up to the 286 instruction set
5609+
\b\c{CPU 186} - Assemble instructions up to the 80186 instruction set
56065610

5607-
\b\c{CPU 386} Assemble instructions up to the 386 instruction set
5611+
\b\c{CPU 286} - Assemble instructions up to the 286 instruction set
56085612

5609-
\b\c{CPU 486} 486 instruction set
5613+
\b\c{CPU 386} - Assemble instructions up to the 386 instruction set
56105614

5611-
\b\c{CPU 586} Pentium instruction set
5615+
\b\c{CPU 486} - 486 instruction set
56125616

5613-
\b\c{CPU PENTIUM} Same as 586
5617+
\b\c{CPU 586} - Pentium instruction set
56145618

5615-
\b\c{CPU 686} P6 instruction set
5619+
\b\c{CPU PENTIUM} - Same as 586
56165620

5617-
\b\c{CPU PPRO} Same as 686
5621+
\b\c{CPU 686} - P6 instruction set
56185622

5619-
\b\c{CPU P2} Same as 686
5623+
\b\c{CPU PPRO} - Same as 686
56205624

5621-
\b\c{CPU P3} Pentium III (Katmai) instruction sets
5625+
\b\c{CPU P2} - Same as 686
56225626

5623-
\b\c{CPU KATMAI} Same as P3
5627+
\b\c{CPU P3} - Pentium III (Katmai) instruction sets
56245628

5625-
\b\c{CPU P4} Pentium 4 (Willamette) instruction set
5629+
\b\c{CPU KATMAI} - Same as P3
56265630

5627-
\b\c{CPU WILLAMETTE} Same as P4
5631+
\b\c{CPU P4} - Pentium 4 (Willamette) instruction set
56285632

5629-
\b\c{CPU PRESCOTT} Prescott instruction set
5633+
\b\c{CPU WILLAMETTE} - Same as P4
56305634

5631-
\b\c{CPU X64} x86-64 (x64/AMD64/Intel 64) instruction set
5635+
\b\c{CPU PRESCOTT} - Prescott instruction set
56325636

5633-
\b\c{CPU IA64} IA64 CPU (in x86 mode) instruction set
5637+
\b\c{CPU X64} - x86-64 (x64/AMD64/Intel 64) instruction set
56345638

5635-
All options are case insensitive. All instructions will be selected
5636-
only if they apply to the selected CPU or lower. By default, all
5637-
instructions are available.
5639+
\b\c{CPU IA64} - IA64 CPU (in x86 mode) instruction set
5640+
5641+
\b\c{CPU DEFAULT} - All available instructions
5642+
5643+
\b\c{CPU ALL} - All available instructions \e{and flags}
5644+
5645+
All options are case insensitive.
5646+
5647+
In addition, optional flags can be specified to modify the instruction
5648+
selections. These can be combined with a CPU declaration or specified
5649+
alone. They can be prefixed by \c{+} (add flag, default), \c{-}
5650+
(remove flag) or \c{*} (set flag to default); these prefixes are
5651+
"sticky", so:
5652+
5653+
\c cpu -foo,bar
5654+
5655+
means remove both the \c{foo} and \c{bar} options.
5656+
5657+
If prefixed with \c{no}, it inverts the meaning of the flag, but this
5658+
is not sticky, so:
5659+
5660+
\c cpu nofoo,bar
5661+
5662+
means remove the \c{foo} flag but add the \c{bar} flag.
5663+
5664+
Currently available flags are:
5665+
5666+
\b\c{EVEX} - Enable generation of EVEX (AVX-512) encoded instructions
5667+
without an explicit \c{\{evex\}} prefix. Default on.
5668+
5669+
\b\c\{VEX} - Enable generation of VEX (AVX) or XOP encoded
5670+
instructions without an explict \c{\{vex\}} prefix. Default on.
5671+
5672+
\b\c{LATEVEX} - Enable generation of VEX (AVX) encoding of
5673+
instructions where the VEX instructions forms were introduced
5674+
\e{after} the corresponding EVEX (AVX-512) instruction forms without
5675+
requiring an explicit \c{\{vex\}} prefix. This is implicit if the
5676+
\c{EVEX} flag is disabled and the \c{VEX} flag is enabled. Default
5677+
off.
56385678

56395679

56405680
\H{FLOAT} \i\c{FLOAT}: Handling of \I{floating-point, constants}floating-point constants
@@ -5643,19 +5683,19 @@ By default, floating-point constants are rounded to nearest, and IEEE
56435683
denormals are supported. The following options can be set to alter
56445684
this behaviour:
56455685

5646-
\b\c{FLOAT DAZ} Flush denormals to zero
5686+
\b\c{FLOAT DAZ} - Flush denormals to zero
56475687

5648-
\b\c{FLOAT NODAZ} Do not flush denormals to zero (default)
5688+
\b\c{FLOAT NODAZ} - Do not flush denormals to zero (default)
56495689

5650-
\b\c{FLOAT NEAR} Round to nearest (default)
5690+
\b\c{FLOAT NEAR} - Round to nearest (default)
56515691

5652-
\b\c{FLOAT UP} Round up (toward +Infinity)
5692+
\b\c{FLOAT UP} - Round up (toward +Infinity)
56535693

5654-
\b\c{FLOAT DOWN} Round down (toward -Infinity)
5694+
\b\c{FLOAT DOWN} - Round down (toward -Infinity)
56555695

5656-
\b\c{FLOAT ZERO} Round toward zero
5696+
\b\c{FLOAT ZERO} - Round toward zero
56575697

5658-
\b\c{FLOAT DEFAULT} Restore default settings
5698+
\b\c{FLOAT DEFAULT} - Restore default settings
56595699

56605700
The standard macros \i\c{__?FLOAT_DAZ?__}, \i\c{__?FLOAT_ROUND?__}, and
56615701
\i\c{__?FLOAT?__} contain the current state, as long as the programmer

0 commit comments

Comments
 (0)