Commit graph

347 commits

Author SHA1 Message Date
H. Peter Anvin (Intel)
18c778c590 files: formalize storing "mapped" file names in the filenames system
Formalize the mechanism for storing mapped (non-filesystem) filenames
in the filenames array. These are not compared against input or output
files for overwrite checks.

Add the infrastructure for comparing more than one input filename
(future proofing.)

Move the Makefile dependency target name to the filenames system.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-07-06 17:08:01 -07:00
H. Peter Anvin (Intel)
1c3d3dc554 nasmlib: add path name comparison function
Add nasm_compare_paths(). At this point, the only effect is wide
character/case insensitivity canonicalization on Windows, but in the
future it might be doing things like comparing st_dev:st_inode pairs
on Unix or compare nasm_realpath().

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-07-06 16:57:38 -07:00
Joshua Watt
851460a10b Add --debug-prefix-map option
Adds an option to remap file prefixes in output object files. This is
analogous to the "-fdebug-prefix-map" option in GCC, and allows files to
be built in a reproducible manner regardless of the build directory.

[ hpa: this still needs to be documented in doc/running.src. ]

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-07-04 19:22:48 -07:00
H. Peter Anvin (Intel)
dad1d8ca42 outbin: register the mapfile; check for input clobber; remove on fail
- Register the map file with the filename system.
- Check to see if the mapfile is clobbering the input.
- Remove the map file if the output file is also removed.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 18:22:39 -07:00
H. Peter Anvin (Intel)
720a32672c Refactor the handling of primary input/output files
Replace the direct setting of variables for input and output variables
with accessors. This allows for properly tracking the lifetimes of the
data and allows for things like checking of the overwrite of the
primary input file to be centralized.

It isn't possible *in the general case* to check for overwrite of
*any* of the input files, although in the particularly important case
of the assembler proper it ought to be possible to do a bit better:

it should be able to guard for overwrites of non-primary input files
except for the error file or the list file if and only if -Lp is used.

That is, however, a latter project.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 18:22:26 -07:00
H. Peter Anvin (Intel)
9d94cac7ae nasmlib/file: add nasm_remove()
Add nasm_remove() to mangle a filename if necessary before calling
an OS-specific remove() function. This allows calling _wremove() on
Windows.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 13:30:03 -07:00
H. Peter Anvin (Intel)
44245e4684 Add nasm_strdupto() and nasm_strto(), to replace a string variable
It is common enough that one wants to set a string pointer to a newly
allocated string, freeing the old one if it is not NULL.

Add specific helper functions for this.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-26 17:25:23 -07:00
H. Peter Anvin (Intel)
7b4e77dae2 Drop const from the output of filename_set_extension()
filename_set_extension() always returns a newly allocated string
buffer, drop the "const" from the return type.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-26 17:24:17 -07:00
H. Peter Anvin (Intel)
e47a62ed4d nasmlib.h: modify nasm_assert_pointer() to work with void pointers
nasm_assert_pointer() would fail with newer gcc when passed a void *,
because the construct sizeof(*(p)) is invalid for void pointers, as is
sizeof(&*(p)).

Instead, make use of the fact that the ternary operator ? : requires
type compatibility between the two sides, and const volatile void * is
type-compatible with any pointer.

Since some compilers are known to elide this check when one side is
NULL, make it a explicit pointer to an arbitrary
object cast to (const volatile void *). zero_buffer[] seems a logical
choice.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-26 16:59:12 -07:00
H. Peter Anvin (Intel)
fe5235ffc9 limits: allow querying limits, and reset limits for each pass
Allow limits to be queried via a %limit() preprocessor function and a
__?NASM_LIMITS?__ macro.

Reset limits at the top of each pass. Note that the pass number limits
are checked at the *end* of the pass, so a %pragma limit will be
properly honored anyway.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-04-09 17:00:49 -07:00
H. Peter Anvin (Intel)
a8d44ce956 limits: revamp the limit processor, change params limit to dynamic
Revamp the limit processor to allow a limit to have an absolute
maximum other than LIMIT_MAX_VAL.

Use LIMIT_MAX as a final enum parameter == parameter count == final
actual value + 1, which is the more usual construct and doesn't need
manual updating.

Allow setting a limit to the default value by setting it to "0" or
"default".

Accept "max" or "maximum" instead of "unlimited", specifically to
better handle the case of a fixed maximum < LIMIT_MAX_VAL.

Change the mmacro limit parameter to a dynamic limit, in case someone
needs it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-04-09 15:12:19 -07:00
H. Peter Anvin (Intel)
8d4733962e saa: make saa_wleb128[us]() take appropriate types; recode
Make saa_wleb128[us]() take uint64_t and int64_t, respectively, rather
than int.

Notably, if saa_wleb128u() were to receive negative int, it would have
looped forever.

Recode these functions in a style more consistent with NASM code in
general, and possibly a bit simpler.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-04-06 16:43:51 -07:00
H. Peter Anvin (Intel)
c8fa17205b Add support for C2y-style \o and braced escapes, and \d for decimal
C2y adds \o for octal byte escapes, and allows \x, \o, or \u escape
sequences to be enclosed in braces to indicate their termination.

Add a \d sequence as a NASM extension to allow decimal bytes to be
specified.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-03-06 17:59:28 -08:00
H. Peter Anvin (Intel)
b1909cf39b listing: move LIST_* enums to listing.h
The LIST_* enums belongs in listing.h, move them there and actually
use them in listing.c.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-03-04 14:14:02 -08:00
H. Peter Anvin (Intel)
89e1d6203a assemble: for -LX emit specific template number as well
When using -LX to debug template selection, emit the specific template
number (as per insnsa.c) as well as the xda line number.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-03-04 14:12:55 -08:00
H. Peter Anvin (Intel)
fe9586f201 listing: separate "user" and "debug" flag, add matching debug option
Adding information about the NASM internal state to the list file can
be useful for debugging purposes. Separate the list of options that
are "user" options and those that are "debugging" options. The latter
are not implied by -L+ and are intended for NASM developer use.

Introduce -LX, which lists which instruction patterns are matched by
each instruction.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-03-03 14:12:48 -08:00
H. Peter Anvin (Intel)
fbacd59cce struct itemplate: add insns.xda line number to the structure
Add the line number from insns.xda to struct itemplate. NASM itself
doesn't need this information, but it makes debugging matching *much*
easier; it is worth a few more bytes.

In theory it could be conditionalized on --enable-debug, but it is
probably not worth it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-03-03 09:55:27 -08:00
Maciej Wieczor-Retman
89682def9b disp8: Fix issue #185 - EVEX compressed shift for MOVDDUP tuple type
MOVDDUP tuple type uses a compression shift for the disp8 addressing
scheme. The shift depends on the vector lenght and is defined as 8 for
128 bits, 32 for 256 bits and 64 for 512 bits.

Current implementation doesn't work for the 128 bit vector case since
the expression used evaluates to 16 instead of 8. Fix it with a special
if check before the current expression is returned.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
2026-01-27 22:24:25 +01:00
H. Peter Anvin (Intel)
ff7a315acb nasmlib: move filename_set_extension() into path.c
Move filename_set_extension() into path.c, so it has access to the
filesystem-specific character constants. This prevents something like:

	nasm-code.d/foobar

... from getting truncated to ...

	nasm-code.bin

... instead of producing ...

	nasm-code.d/foobar.bin

Make the extension character (normally '.') configurable; this MIGHT
be usable on RISCOS at some point, although it is not entirely clear
that trying to make sense of RISCOS paths actually is meaningful,
because RISCOS compiler chains seem to do all kinds of path
translation magic trying to behave like other operating systems... it
might simply be more trouble than it is worth, especially for a
non-x86 platform.

As a side bonus, this removes the only use of strrchrnul(), so that
function can be dropped.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-12-15 09:58:42 -08:00
H. Peter Anvin (Intel)
f4f7d18c06 error: factor out error functions into separate files
Tidy up a *lot* of code by moving error functions into separate source
files. This required breaking out some of the assembler-only files
into a separate library, as it conflicts with stubs in the
disassembler.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-11-07 14:42:33 -08:00
H. Peter Anvin (Intel)
1b939ecf3d errors: add the ability to hold errors, not just warnings
Re-introduce ERR_HOLD, which means that an error is treated like a
warning except for the last pass, but unlike ERR_PASS2 the error
message *will* be issued if another error happens on the same pass, just
like warnings.

This will be used to improve error messages on instruction mismatch.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-15 21:09:05 -07:00
H. Peter Anvin (Intel)
44ec97993a compiler: add and use unreachable() macro
C23 defines unreachable() as a macro in <stddef.h>. For earlier
versions of gcc, __builtin_unreachable() is possible.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-13 17:53:42 -07:00
H. Peter Anvin (Intel)
746e7c9efa compiler.h: the test for "neither C++ nor C23" still wrong
The test needs to test for neither nor; as it was it tested "(not C++)
or C23" which was not at all what was intended...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-12 13:05:55 -07:00
H. Peter Anvin (Intel)
44e89ba9b6 compiler.h: drop the stupid C++-style cast-to-bool hack
The C++-style cast-to-bool hack was broken in concept that it doesn't help the
fundamental problem -- implicit conversions are broken for the
backwards compatibility enum definition -- as well as in
implementation, as it misspelled __STDC_VERSION__ as __STDC_VERSION.

The #ifdef bool test *should* have prevented this problem, but
apparently several compilers do define "bool" in <stdbool.h> even when
it is a keyword, in violation of the C23 spec.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-12 12:48:32 -07:00
H. Peter Anvin (Intel)
ae9335a0b9 labels: make the prefix/suffix options and pragmas consistent
Make the spellings for the label-mangling options and pragmas
consistent, and implement the directive forms which were documented
but never implemented.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 18:46:39 -07:00
Yongjie Sheng
9cb23294a4 fix a displacement offset calculation error for movddup 2025-10-07 14:35:45 +08:00
H. Peter Anvin
f6a17a29c0 nasmlib.h: protect list_last() macro against misuse
The list_last() macro is a statement macro; wrap it in a
do { ... } while(0) block to prevent accidental misuse.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 14:27:30 -07:00
Adam Sampson
a65be7d59b bytesex.h: fix typo in le32toh function name
This fixes the build on 32-bit/64-bit ARM.

Signed-off-by: Adam Sampson <ats@offog.org>
2025-10-04 15:54:49 -07:00
H. Peter Anvin
b4697f09e7 Conditionalize __builtin_prefetch() on it existing!!
You have to check that something that isn't standard C actually exists
before trying to use it...

Cc: Colin Ian King <colin.i.king@intel.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-03 10:47:23 -07:00
H. Peter Anvin
0b3c971f5b portability: "const macros_t" is redundant
Some C compilers don't like that...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-01 13:55:38 -07:00
H. Peter Anvin
23ce05f906 treewide: replace verbose copyright headers with SPDX tags
SPDX is an international standard for documenting software license
requirements. Remove the existing headers and replace with a brief
SPDX preamble.

See: https://spdx.dev/use/specifications/

The script used to convert the files is added to "tools", and the
file header templates in headers/ are updated.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-01 11:45:31 -07:00
H. Peter Anvin
595afc6f4b portability: add missing PRI* constants, signed 1 in bitfield hack
Add missing uses of PRI constants.

Create a PRI constant for size_t, since %z isn't available on all
platforms. Notably, the legacy Windows runtime needs %I instead of %z.
Use that on UCRT as well, since there doesn't seem to be a way to
determine if you are compiling for MSVCRT or UCRT.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-30 09:41:03 -07:00
H. Peter Anvin
df6b966ca9 nasmlib.h: use int for the enum macros, for strict ISO C
ISO C before C23 restricted enum ranges to type "int" (not even
"unsigned int"). This limits the usability of enums for bit fields,
but it still covers our main use cases.

On gcc/clang this just produces a warning, but who knows what other
compilers will do.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-25 08:33:17 -07:00
H. Peter Anvin
c4ed07830a Tidy up the morass of EVEX bitfield constants
The EVEX bitfields are complicated at best. Tidy them up to the
best of our ability.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-20 18:26:27 -07:00
H. Peter Anvin
47c56d122e nasmlib: set of neat macros to create a useful bitfield enum
When dealing with bitmasks/bitfields, definining them in macros tends
to cause a lot of desirable constants out because it is a pain to
create all of them.  C macros can't create other macros, but they
*can* be used to create fields in an enum, so provide some neat
convenience macros for doing so.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-20 18:25:32 -07:00
H. Peter Anvin
861782dc88 compiler.h: has_attribute() is broken on some gcc versions
Apparently the has_attribute() macro triggers false positives on at
least gcc 5.3, which is the version that the build server uses with
djgpp.

Drop using it, so much for trying to be standards-compliant...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-16 15:27:34 -07:00
H. Peter Anvin
55dd65cddc portability: speed and modernize autoconf; compiler.h improvements
- Add some features to autoconf that makes it cleaner and faster
- Modernize some of the autoconf macros
- Update compiler.h with some C23 features

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-16 13:25:39 -07:00
H. Peter Anvin
f6166e571a preproc: implement %hs2b() and %b2hs() functions for compact binary data
Convenience preprocessor functions that allows for efficient packing
of binary data in source code.

Move some functions that has previously been local but are more
generally useful into more accessible places.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-15 23:01:59 -07:00
H. Peter Anvin
dc247c9f99 Speed up autoconf, use POSIX names instead of cpu_to_le*()
The POSIX names for these functions are htole*(). Use those
preferentially.

Speed up autoconf by allowing early-out during alternative function
searches.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-15 18:26:25 -07:00
H. Peter Anvin
c719835357 Officially deprecate implicit DEFAULT ABS, add DEFAULT [FS|GS]:[ABS|REL]
Making DEFAULT ABS the default for 64-bit mode was a real
mistake. Issue a warning so we can eventually change it.

Support making FS: and GS: references also be REL by default.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-13 23:15:47 -07:00
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
ac93d75da3 Sanitize the handling of messsages; improve info and debug
Make the handling of messages saner. In particular, regularize the
handling of info and debug messages, so that nasm_info() and
nasm_debug() actually become useful.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 12:53:20 -07:00
H. Peter Anvin
31a160759d Add %ifdirective preprocessor directive
Along with C and other languages, the current trend is to be able to
probe for features rather than relying on version numbers. This is
motivated in part by the intent of bumping the major version number to
3.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 19:41:12 -07:00
H. Peter Anvin
61b525fbc1 nasmlib.h: add convenience macros for iterating over fixed arrays
Iterating over fixed arrays is a common operation; add macros to
simplify the task.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 17:09:57 -07:00
H. Peter Anvin
178a1b7443 Add directive to disable dollar-hex constants
The use of $ prefixes for hexadecimal numbers conflicts with
the use of $ to escape symbols.  Add a directive to disable
$ for hexadecimal numbers so that those escapes work OK.

As a result, allow escaped symbols to start with a digit.

Add a warning that this syntax is deprecated.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 00:35:41 -07:00
H. Peter Anvin
80225b4722 Add support for the {pt} and {pn} branch hint prefixes
Add support for the {pt} and {pn} branch hint prefixes, now when they
are no longer orphanned...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-02 16:29:46 -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
9f86275c74 Merge remote-tracking branch 'origin/master' into apx.wip
Resolved Conflicts:
	disasm/disasm.c

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-29 01:51:27 -07:00
H. Peter Anvin
7a5502142b bytesex, ilog2: use C23 <stdbit.h> if available; use in disassembler
Merge the macros used in the assembler and disassembler.
Simplify and slightly correct the byte order/unaligned handling macros.
Use <stdbit.h> from C23 if available for bytesex.h and ilog2.h.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-29 01:46:57 -07:00