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

643 Commits

Author SHA1 Message Date
H. Peter Anvin
a0396faf3b Fix control/debug register patterns
The control and debug registers are always using the default operand
size. It is probably easiest to just encode it explicitly for now.

Control registers are particularly weird because of the AMD "lock as
REX.R" hack...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 16:26:35 -07:00
H. Peter Anvin
d2aed9fa9d labels: handle "extern" after definition (BR 3392924)
Using "extern" or "required" after the definition should be
interpreted as "global", just as if "extern" or "required" had been
specified before the definition.

Unfortunately the code did not correctly handle the case of upgrading
from LOCAL to GLOBAL via an EXTERN or REQUIRED directive, only from
EXTERN or REQUIRED to GLOBAL via definition or a GLOBAL or COMMON
directive.

Fix.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 14:22:29 -07:00
Maciej Wieczor-Retman
f0efb28d98 assemble: apx: Add NF forbidden flag and fix SBB and ADC
ADC and SBB don't support using the {nf} prefix. They are the only one
in the arithmetic instructions group that are this way.

Add a flag that will warn when an instructions wants to use {nf} but
doesnt' support it.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
2025-09-19 14:53:04 +02: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
856ac7b7fb preproc: add %chr() and %ord() string <-> byte list functions
It is sometimes just too convenient to be able to convert between
strings and bytes at will. At one point I was considering making
something with the full power of the db (et al) directives, but that
is a much bigger change...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-15 21:48:07 -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
a25bc0a5d4 doc: fix broken section heading for DEFAULT BND
The section heading for DEFAULT BND was broken (missing \); make the
tags saner, too.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-14 23:50:43 -07:00
H. Peter Anvin
31c4d8bcfd doc: describe caveat with old NASM and new %if directives
Older versions of NASM would not try to match unknown %if directives
with a corresponding %endif, resulting in some very odd consequences
when it comes to trying to handle support for multiple NASM versions.

Document the problem.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2025-09-14 04:07:19 -07:00
H. Peter Anvin
1f1f3266d6 preproc: look for what looks to be an unknown conditional
If an unknown preprocessor directive starting with %if or %elif is
encountered, assume it should be treated as a conditional directive
for the purpose of balancing %if...%endif.

This avoids some really ugly problems when dealing with code that is
intended to work across NASM versions that uses %if directives that
don't yet exist.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-14 03:25:26 -07:00
H. Peter Anvin
7276cfde99 preproc: remove the message-generating functions
The message-generating functions appear to get expanded when they
shouldn't, resulting in errors. Disable them for now and remove them
from the documentation.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-14 02:48:58 -07:00
H. Peter Anvin
84115eeca6 preproc: add %find() and %findi() functions
Add the %find() and %findi() functions to look for a string in a
list. This is useful with picking apart the contents of the
__?DEFAULT?__ macro, for example.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-14 01:24:42 -07:00
H. Peter Anvin
4ccdf7e6fe preproc: add __?DEFAULT?__ macro representing the DEFAULT settings
Create a __?DEFAULT?__ magic macro with the settings of the DEFAULT
directive.

The DEFAULT directive is complex enough that this is best done with a
magic macro.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-14 00:38:50 -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
a3291bd8fb preproc: return NULL, not false
In at least one place, returned false instead of NULL, which caused
build failures on some platforms.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 19:04:34 -07:00
H. Peter Anvin
1d63ae2a3b doc: update the documentation, and auto-generate some indexes
There are currently six variations of each conditionals, and there may
be more in the future (e.g. %while). Stop trying to enumerate them all
everywhere.

Add support for index copying in the document processor.

Have pptok.pl auto-generate index metadata for conditional
preprocessor directives.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 17:12:06 -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
3ff46c587a assemble: trivially optimize emissions of REX+map prefixes
Emit rex+map prefixes as a single chunk of data.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 03:04:51 -07:00
H. Peter Anvin
5158467d55 Add missing header file asm/asmutil.h
This file was missing from a previous checkin.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 01:08:25 -07:00
H. Peter Anvin
c5faa4a452 assemble.c: don't warn about segment prefixes in 16/32-bit mode
The warning about ignored segment prefixes were incorrectly issued in
16/32-bit mode as well.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 00:49:49 -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
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
ec48f1a3f8 preproc: unbreak %cond()
The previous patch accidentally broke the %cond() function.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 18:23:55 -07:00
H. Peter Anvin
d5bd136ced preproc: add message-generating and %null() functions
Add the %null(), %note(), %warning(), %error(), and %fatal()
functions. They behave like the corresponding directives, then expands
to nothing.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 17:10:42 -07:00
H. Peter Anvin
e80bc631a2 preproc: identify $$ as TOKEN_BASE when tokenizing
$$ is TOKEN_BASE, not a symbol. If this is done incorrectly, ppscan
chokes on $$ as it ends up being an invalid symbol.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 11:33:08 -07:00
H. Peter Anvin
60594fde66 Fix generation of segment references with offsets
Segment references can come from two places: either an explicit SEG
operator or from a far expression. In the former case, the segment can
have a programmer-provided offset, but in the latter case, it
cannot.

The fix for bug 3392949 fixed the latter case, but broke the
former. This patch hopefully makes both work.

Rename out_segment() to out_farseg() and add a comment to explain the
logic behind the difference.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392950
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 11:06:16 -07:00
H. Peter Anvin
7e8f1d571e preproc: fix generation of macro invocation debug data
It is possible for m->mstk.mmac to point back to itself. In that case,
don't terminate the macro debug invocation just yet.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 10:34:28 -07:00
H. Peter Anvin
78abbf26df asm/floats.c: micro-optimize set_bit()
Micro-optimize the set_bit() function. Using size_t (in particular,
using an unsigned type) produces better code.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 01:27:30 -07:00
H. Peter Anvin
a84925df12 $ numeric syntax: require a digit to follow
The documentation actually states that $ as a hex prefix is only valid
when followed by a digit, which at least makes the syntax conflict
less complicated. Actually match the documentation.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 01:05:41 -07:00
H. Peter Anvin
54efdde7b1 preproc: warn for leading $ when a number is consumed in preprocessor
To avoid repeated warnings, there is no warning issued for when
tokenizing a number starting with $ in the preprocessor, but issue a
warning if such a number is *consumed* (used in arithmetic) in the
preprocessor.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 00:46:23 -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
9c3d6ff000 stdscan: handle $-escaped symbols starting with $
For a symbol to start with $, it needs to be escaped with a second
dollar sign: $$. This was not handled correctly, instead $$ was seen
as TOKEN_BASE.

Fix this.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392922
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-02 17:55:22 -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
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
83534f1f37 Merge remote-tracking branch 'origin/master' into apx.wip
Resolved Conflicts:
	Makefile.in
	Mkfiles/msvc.mak
	Mkfiles/openwcom.mak
	asm/assemble.c
	asm/nasm.c
	asm/parser.c
	doc/Makefile.in
	include/nasm.h
	include/tables.h
	output/legacy.c
	travis/test/br3392531.stderr
	travis/test/br3392716.stderr
	travis/test/org.stderr
	x86/insns.dat

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-25 21:09:15 -07:00
H. Peter Anvin
9ea92eab6e preproc: reference counting for mmacros, minor cleanups
The late cleanup of macros can cause severe memory hogging with nested
%reps. Instead, implement proper reference counting for mmacros.

Adds some other minor cleanups as well, notably delete_*() are
designed to update or null the pointer that is passed to it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-25 20:11:02 -07:00
H. Peter Anvin
ada267ec8c preproc: let %ifdef test for existence of macro functions
It is good to have a way to test for the existence of macro functions,
and since they are really just a special case of single-line macros,
allow %ifdef to test for them instead of coming up with something
entirely new.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-16 19:15:00 -07:00
H. Peter Anvin
06f7ce2dba preproc: actually use uleb128 format for encoding macros
The format wasn't actually uleb128 because it was accidentally
bigendian (like UTF-8). That is just begging for confusion in the
future, if and when the uleb128 code gets librarized.

Fix it now.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 17:07:15 -07:00
H. Peter Anvin
df4555d68b preproc: compress standard macros with zlib
The very simple compression scheme used for the builtin macro sets no
longer works adequately, and in fact it generates incorrect output
now.

Drop the whole idea of an ad hoc compression scheme and just use
zlib. For the case where there is no system zlib available, include a
(subset of) the zlib distribution. The configure script can be set to
force this included zlib if desired (e.g. for testing.)

Unfortunately this turned out to be a pretty painful can of worms in
terms of complexity. On the other hand having zlib available might be
useful at some point in the future.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 16:51:39 -07:00
H. Peter Anvin
0c533873f0 preproc: add %iffile|%isfile() and %realpath()
Add a function to test for the existence of a file, and a function
query the real operating system path, if available.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 00:32:33 -07:00
H. Peter Anvin
0843052f05 preproc: implement %pathsearch() and %depend() as functions
Implement preprocessor function equivalents of the %pathsearch and
%depend directives.

Simplify the incbin standard macro by using these functions.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-14 21:46:55 -07:00
H. Peter Anvin
9f24de1034 parser: improve the "instruction expected" error messages
When throwing one of the "instruction expected" error messages, print
what was encountered instead.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-14 18:43:02 -07:00
H. Peter Anvin
2726aefb06 output: remove the legacy output entry point
Remove the legacy output entry point. It has proven impossible to find
the time to completely port the backends all at once.

Instead, always generate the legacy output data, but put them into the
out_data structure. Then add a macro to explode these arguments into
separate variables, equivalent to the old function arguments. This
also centralizes the type definitions for these variables.

Most importantly, it means that the entire struct out_data is now
always available, which means that backends that need the additional
information available in that structure, such as the specific
instruction template, can access that information without needing to
revamp the entire backend code all at once.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-13 13:51:25 -07:00
H. Peter Anvin
1da646aeba Merge remote-tracking branch 'origin/nasm-2.16.xx'
Resolved Conflicts:
	asm/preproc.c

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 18:52:20 -07:00
H. Peter Anvin
9e6a4dd028 preproc: inc_fopen(): use the correct path for hashing the fullpath
This code incorrectly would try to use "path" as the hash key instead
of full->path, causing the key in struct hash_insert to diverge from
the one used in hash_add(). Fix that.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 14:42:47 -07:00
H. Peter Anvin
f49e9f1bea preproc: hack around mmacro lifetime problems by deferring free
Under some circumstances, such as:

- Certain uses of %exitrep in syntactically invalid code;
- %unmacro of a *alias* to a macro currently being expanded;

... it is possible for an mmacro to get freed while it is still in
use. Although inefficient, the easiest way to avoid this is to not
free mmacros until the end of pass cleanup, when named mmacros are
also freed.

To support this, use the existing ->next field in the MMacro structure
to keep a list of anonymous or removed MMacros. Don't free ->name at
this point, though, since that is currently used to distinguish
between %rep's and %macro's.  (This needs to be cleaned up to support
constructs such as %while or %for, but that is for later.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 14:02:12 -07:00
H. Peter Anvin
3d686291c0 assemble: make assemble() and insn_size() static
These functions are only used in assemble.c, so make them static.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-03-04 08:38:45 -08:00