0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-09-22 10:43:39 -04:00
Commit Graph

48 Commits

Author SHA1 Message Date
H. Peter Anvin
04c21dc0c5 Allow synthesis of ROLX
If the shift amount is known, there is really no reason why we can't
accept "ROLX" as an alias for "RORX" with a modified shift operand.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-13 21:25:02 -07:00
H. Peter Anvin
01cef66b23 insns.dat/preinsns.pl: fix the RORX, SHLX, SHRX, SARX patterns
These are subtle: RORX is only available in immediate shift count
form, whereas SHLX, SHRX and SARX are only available in register shift
count form...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-13 20:44:53 -07:00
H. Peter Anvin
7e4e937f97 Merge remote-tracking branch 'origin/master' 2025-09-05 18:03:44 -07:00
Maciej Wieczor-Retman
1054b5aefc x86: Fix handling of the ko# specifier
KMOV opcodes of 0x90 and 0x91 have similar bits set in all size
implementations. .66 and .W1 for 32bit, and .NP and .W1 for 64 bits.

Correct my previous mistake where I swapped the VEX/EVEX.pp values by
accident.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
2025-09-05 15:17:05 +02:00
H. Peter Anvin
3a5cbc7a09 ndisasm: don't generate REX2 patterns for NOAPX instructions
The wrong flags field was examined for the NOAPX or NOREX flags.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 02:07:35 -07:00
H. Peter Anvin
cb20ce6f01 apx: handle RN_L16 on EAs, automatically generate EVEX forms
Memory references should have RN_L16 set if and only if they are
compatible with instruction patterns requiring register numbers below
16.

Add a "vex+" encoding pattern for VEX-encoded instructions that should
be promoted to EVEX when AVX-encoded.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 23:49:10 -07:00
Maciej Wieczor-Retman
312b89d71f x86: Fix wrong encondig of 32 bit KMOV variants
VEX.pp should be 0 for 32 bit variant of KMOV.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
2025-09-04 23:35:14 +02:00
H. Peter Anvin
f2b6dd6f66 More KMOV pattern fixes
Work even more on KMOV size encodings.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2025-09-02 21:09:20 -07:00
H. Peter Anvin
6f42a3aaf6 (Hopefully) fix kmov and kunpck instructions, disallow "test" for "ktest"
Fix the handling of instruction patterns for KMOV and KUNPCK.
Don't allow K-less versions of KTEST and KORTEST because of
fundmentally different semantics.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-02 18:54:27 -07:00
H. Peter Anvin
acd01496d7 asm: distinguish between VEX.V as an immediate and a prefix; fix WW
If VEX.V is an immediate, it should not be subject to register range
checks.

If the WW flag is set, REX_W needs to be OR'd in, not XOR'd, because
the map might have the W bit set for matching purposes.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-02 15:38:49 -07:00
H. Peter Anvin
e05867ce3d ndisasm: make the assembler (hopefully) work again
- Significantly overhauled the disassembler internals to make
  better use of the information already in the instruction template
  and to reduce the implementation differences with the assembler
- Add APX support to the disassembler
- Fix problem with disassembler truncating addresses of jumps
- Fix generation of invalid EAs in 16-bit mode
- Fix array overrun for types in a few modules
- Fix invalid ND flag on near JMP

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-30 16:16:43 -07:00
H. Peter Anvin
3efdd3cf9a assemble: rex2.w; hinted Jcc in 64-bit mode; UDB
- rex2.w is used as a opcode extension (JMPABS), not rex2.x1 as an
  earlier version of the spec had.
- Segment prefixes used as Jcc hints are valid in 64-bit mode.
- Avoid duplicate warning messages for ignored/invalid prefixes.
  * emit_prefixes() is called twice during code generation.
- Add the UDB #UD opcode in 64-bit mode; SALC is 16/32-bit only.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-29 13:55:30 -07:00
H. Peter Anvin
49a56ea9ee Add optimization for operand narrowing; tidy up selector handling
Add a optimization frameword for operand narrowing (where the operand
size doesn't matter beyond a certain range because only certain bits
are referenced.)

Add a macro *and* matching facility for dealing with segment selectors, which are
typically rm16/r32/r64, but exactly how that is applied varies
depending on if a datum is read or written.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2024-08-23 02:25:21 -07:00
H. Peter Anvin
2b2f1fc98a More macroizing and sorting of instructions into categories
More work on cleaning up instruction patterns, fixing matchig corner
cases, and tidying up the organization of insns.dat.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2024-08-22 23:22:59 -07:00
H. Peter Anvin
253ff4f370 insns: tag pseudo-instructions explicitly; change insnsa.c format
Tag pseudo-instructions explicitly and don't set any CPU level flag
for those.

Change insnsa.c to have (length, pointer) rather than using an ever
increasing in size sentinel at the end of each table. This also means
that empty tables (Dx, INCBIN) can be omitted entirely.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-21 12:50:31 -07:00
H. Peter Anvin
58024b4611 insns: more instruction macroizing/fixups; remote FUTURE tags
Add more instruction macros and fix problems. Adjust some matching
problems.

Remove all FUTURE tags from the instruction list, and add a bunch of
new CPUID tags. Hopefully a small step toward actually getting CPU
feature selection working properly in the future.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-21 11:48:47 -07:00
H. Peter Anvin
75f6f4cdb2 WIP: more matching and template work
Further work on a better matching system. Still a work in progress,
however.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2024-08-20 12:59:07 -07:00
H. Peter Anvin
f114a6276e insns: more macroization and organization
Macroize and update more instruction patterns.

Begin organizing the instructions by functional groups.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-15 22:15:48 -07:00
H. Peter Anvin
8f97af7d71 insns: more macro fun; handle things like RET, RETW, RETD, RETQ
Add macro handling for patterns with a non-suffixed default operand
and alternative suffixed operands.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-15 19:23:33 -07:00
H. Peter Anvin
8ee33d2734 insns: more macroized instructions and fix preprocessor bugs
Add more macroized instructions, and fix some bugs in the
instruction preprocessor.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-14 21:26:23 -07:00
H. Peter Anvin
1b136eb543 insns: use the pre-existing instruction flags handler for preinsns.pl
There is no reason to reinvent the wheel; reusing the existing code
will be cleaner anyway.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-14 16:45:09 -07:00
H. Peter Anvin
3b55b62f02 apx: implement the mechanism for evex.zu
Implement the mechanism needed to handle {zu} suffixes that actually
set ND (IMUL, SETcc).

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-14 15:44:38 -07:00
H. Peter Anvin
cb8ca3bb95 insns.pl: for scc encodings, add null string = true
Allow the null string encoding for "true" for scc instructions.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-12 03:16:09 -07:00
H. Peter Anvin
c9457d42a6 WIP checkpoint: more matching changes, starting to work on patterns
This is a WIP checkpoint; not all tests pass yet.

More matching changes, and hopefully something much closer to what
really is desired now. The number of required patterns is now much
smaller.

However, a lot of *changes* are needed to the patterns.

Since some patterns are repeated all over the place, clean up the
x86/addflags.pl script and make it able to generate macro-based
common patterns; first use being the patterns for the "basic 8"
arithmetic patterns.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-11 21:28:57 -07:00
H. Peter Anvin
bff94fbd39 Major changes to a number of subsystems to improve matching
Work through a number of changes toward making matching a lot saner,
both to reduce the number of patterns to generate for APX but also to
make a number of code patterns simpler.

This replaces a fair number of byte codes.

Improve a number of error messages, especially related to overflows.

Move process_insn() from nasm.c to assemble.c, as it really is the
primary entry point to the assembler module.

Reorder some prefixes. In particular, F2/F3 override 66 when used as a
mandatory prefix, so it makes more sense for them to be closer to the
opcode.

Move a lot more information into struct insn. It is better to have it
in one place; memory consumption is not an issue because struct insn
is transient information.

Get rid of "optimization levels" and replace it with a mask of
flags. That was already halfway done; complete the job.

Replace seg:offset in struct out_data with a struct location. It would
be better to extend this to more places, too.

The ARx and SMx flags are now explicit bitmasks, instead of having a
couple of hard-coded ranges.

Add __func__ to assert or panic messages.

Because of prefix and message changes, a number of travis tests had to
be audited and updated.

Fix a number of instruction patterns which had .128 when they ought to
be .lig. This is no longer a minor issue with the disassembler: for
AVX10, the pattern vector length determines how SAE/RC are encoded,
and there is no valid 128-bit encoding. However, with .lig the 512-bit
encoding can be used.

Separate "o64nw" into two pieces: opsize 64 and "nw" = "REX.w not necessary". The
latter can be included in non-64-bit patterns. "o64" still set REX.W
since that is still the common thing.

New "osz" bytecode: emit an OSP *or* REX.W depending on the current
mode and operand size. Useful for special cases like "nop" where "o64
nop" probably wants to be encoded as "48 90".

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-07 17:13:44 -07:00
H. Peter Anvin
d55f780b96 insns.pl: fix the types for the disassembly tables
Make sure to get the right type for the disassembly tables. This is
now a fixed-depth tree, so there is no reason to use union types or
anything along those lines.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-04 15:57:14 -07:00
H. Peter Anvin
84f2553d7f x86/insns.pl: redo the way tables are generated for disassembly
Change the generation of tables for disassembly to be map-based. This
also makes the code a bit more regular.

This is the first step at catching up with APX support in the
disassembler.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-03 21:24:59 -07:00
H. Peter Anvin
dc76618f5d insns.pl: don't generate NDD ZU patterns if not necessary
If the patterns are inherently ZU, then there is no reason to also
generate NDD forms.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-03 18:18:11 -07:00
H. Peter Anvin
c2eade6379 insns.pl: fix the generation of NDD patterns for {zu}
When specified with {zu}, allow generation of NDD patterns if
applicable.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-03 18:05:51 -07:00
H. Peter Anvin
dda9152b35 apx: smarter determination of REX2 prefix eligibility
REX2 encoding is mostly default, so flag the instruction patters which
do *not* support REX2 instead.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-31 16:18:17 -07:00
H. Peter Anvin
973fe19a1b WIP: apx: now can encode most instructions; handle subcases in EVEX
EVEX encoding is really messy, with the 4th register bit in one of
several places depending on which type of register it is. It seems to
work now.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-30 17:03:58 -07:00
H. Peter Anvin
2e4df506e0 WIP: APX: can now generate code for tested APX instructions
a
Support generating code for APX instruction and add support for the
{nf} prefix.

No disassembler support yet, and only a handful instructions encoded.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-29 20:58:04 -07:00
H. Peter Anvin
318a0b9244 WIP: apx: byte code and byte code compiler changes
Change the byte code format and the byte code compiler to be able to
generate various kinds of APX-format instructions.

THE NEW BYTE CODES ARE NOT YET IMPLEMENTED IN THE ASSEMBLER OR
DISASSEMBLER.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-28 21:57:31 -07:00
H. Peter Anvin
1618fa745b apx: support parsing special constants like {dfv=}
{dfv=} is basically a constant (immediate). Treat it as such during
parsing, except that if "naked" (not in an expression), it has special
matching properties and does not need a terminal comma.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-28 16:57:24 -07:00
H. Peter Anvin
49640ed315 x86: move the bytecode defintion into a separate file in x86/
At least three files (asm/assemble.c, disasm/disasm.c, and
x86/insns.pl) depend on the bytecode defintions. It makes a lot more
sense for them to live in an explicit documentation file in the x86/
directory.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-23 12:47:25 -07:00
H. Peter Anvin
a3fd34ab80 x86/insns.pl: sort conditional instructions alphabetically
Makes the build a bit more predictable and debuggable.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-12 12:59:23 -08:00
H. Peter Anvin
a2eabbe1d7 insns: drop special handling of conditional instructions
Instead of handling conditional instructions ad hoc, generate
individual instruction patterns as normal. This simplifies the code
and makes CMPccXADD support simpler (otherwise it would be necessary
to hack in the handling of a condition code in the middle of an
instruction.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-12 12:37:37 -08:00
Iouri Kharon
21d8dbfabb restire: Support of AVX512-FP16 Instructions
Add support for AVX512-FP16 instructions and the associated
handling. Allow "mapN" syntax as well as "mN" syntax to match the
documentation.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:21:23 -08:00
H. Peter Anvin (Intel)
e56c2dc5b7 insns.pl: audit for impossible Sx patterns; fix a few
Break the instruction processing if there are impossible combinations
of Sx flags and operand sizes. If the intent is to always require
explicit sizes, use the SX flag.

The INSERTPS instruction pattern was explicitly wrong, the rest of
these are nuisance fixes.

TODO: fix the disassembler to be able to exclude patterns where these
bits don't matter.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-30 15:16:11 -07:00
H. Peter Anvin
6c1ad43d5e insns.dat: get rid of the X64 marker (= X86_64,LONG)
The X64 marker for "X86_64,LONG" has turned out to be a problem in
that it is easy to mistake for "long mode" when adding new
instructions, which results in duplicate CPU flags. Kill it off; it
isn't like we will legitimately have new instructions with this
pattern ever again.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-17 19:25:19 -07:00
H. Peter Anvin
b31a4c9906 Add support for new instructions from ISE June 2020
Add support for new instructions as defined in the Instruction Set
Extensions manual as of June 2020.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2020-07-16 21:52:15 -07:00
H. Peter Anvin (Intel)
5b39461178 obsolete handing: handle a few more subcases in a useful way
Distinguish instructions which have once been valid (OBSOLETE) from
those that never saw the light of day (NEVER). Futhermore, flag
instructions which devolve to an architectural noop from those with
undefined behavior and possibly recycled opcodes.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 14:52:16 -07:00
H. Peter Anvin (Intel)
177a05d0ce perl files: clean up warnings
Clean up some perl warnings, some of which were legitimate (apparently
undef doesn't actually take a list of arguments, a common enough
mistake that it is mentioned in the man page!, and a list of variables
after "my" can be cantankerous), and some of which were nuisance but
were easy enough to clean up.

Maybe this can resolve the problems with very old version of Perl?

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 13:30:19 -07:00
H. Peter Anvin (Intel)
602e67f932 insns.pl: use less cantankerous string expansion; better error info
The flags massaging in insns.pl could end up with things like double
commas in some pathological cases, which would make insns_flag_index()
very unhappy due to the appearance of an empty argument. Fix this by
processing the flags as a list already in insns.pl.

Be more explicit and consistent in error messages.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 02:41:37 -07:00
H. Peter Anvin
af9fe8f597 Don't sort opcodes; move all pseudo-ops to the beginning
We don't need to sort opcodes anymore, since we are using an O(1) hash
and not binary search.  Instead, sort them in the order they first
appear in insns.dat; this lets us move all the pseudo-ops to a
contiguous range at the start of the file, for more efficient
handling.

Change the functions that process pseudo-ops accordingly.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2017-05-01 21:44:24 -07:00
H. Peter Anvin
526a6c7390 Rename insns-iflags.pl -> insns-iflags.ph, add missing dependency
insns-iflags.ph is included from another Perl script, so rename it .ph
(Perl header).  Add missing dependency to the main Makefile.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2017-04-03 10:33:12 -07:00
H. Peter Anvin
841d904f88 perl: change to the new, safer 3-operand form of open()
The 2-operand form was inherently unsafe.  Use the 3-operand form
instead, which guarantees that arbitrary filenames are supported.

This also means we can remove a few instances of sysopen() which was
used for exactly this reason, however, at least in theory sysopen()
isn't portable.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2017-04-02 19:36:41 -07:00
H. Peter Anvin
e1f985c167 Reorganize the source code into subdirectories
Make the source code easier to understand and keep track of by
organizing it into subdirectories depending on the function.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-05-25 12:06:29 -07:00