nasmlib/file.c included <stringapiset.h> directly (for
MultiByteToWideChar()/CompareStringOrdinal()) without first including
<windows.h>. Windows SDK headers like <stringapiset.h> are only
guaranteed to work when pulled in through the normal <windows.h>
pipeline, which sets up SDK-internal architecture macros (_X86_,
_AMD64_, ...) derived from the compiler's own _M_IX86/_M_X64/etc.
Including them directly skips that setup and can fail with a
'No Target Architecture' #error from <winnt.h> -- which is exactly
what happened building with a real cl.exe/nmake in CI.
<windows.h> is deliberately *not* pulled in globally from compiler.h:
it #defines NEAR and FAR as legacy no-op calling-convention keywords,
which clash with NASM's own NEAR/FAR opflags bits (include/opflags.h).
Keep the inclusion local to file.c, the only file that currently needs
Windows API declarations, guarded by WIN32_LEAN_AND_MEAN to keep the
exposed surface minimal.
Confirmed fixed with a real MSVC (cl.exe/nmake) build in CI.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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>
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>
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>
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>
buflen == 0 is an error, but be paranoid about it; the code already
checks for a valid base so it is better to check for a valid buffer
size as well.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
MultiByteToWideChar() and its constants are defined in
<stringapiset.h>, which is *not* included in <windows.h>.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
In os_mangle_filename() with _WIN32, MultiByteToWideChar() was being
called with five arguments instead of six, missing the actual
string to convert(!)
Fixes: 5dfcfc8cab
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
q is passed in by reference and the value of q is never examined, so
there is absolutely no reason to set q = NULL in nasm_free().
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
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>
filename_set_extension() with extension "" would leave the string not
NUL-terminated, because elen == 0.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
The recently removed "file.h" was #included by mmap.c, but mmap.c just
needed a few standard header files.
Similarly, remove <sys/mman.h> from file.c.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
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>
file.h was only included from file.c; combine the two and declare
functions not used anywhere else as static.
Split out the checking I/O routines into a separate routine fileio.c
as they are (mostly) not host operating system specific, whereas
file.c is mostly host operating system generalization code.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
In the Windows-specific filename translation code, use
MultiByteToWideChar() instead of mbsrtowcs(). As this is
Windows-specific code it makes more sense to use the Windows-specific
API, and it might be more widely supported?
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
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>
For the benefit of those platforms which have to rely on VPATH, avoid
having the same filename in multiple subdirectories.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
The idea of putting the warnings in the source code was a nice one,
really, but it ended up being a nightmare from the perspective of
build dependencies. Disaggregate them, and tweak the documentation for
easier reading.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
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>
Turns out that the hotspots of nasm are mainly on string hashing
when accessing memory. A simple performance improvement is to
prefetch the first cacheline of a string to be hashed. Ran 50 tests
on an i9-12900 building intel-ipsec-mb that heavily uses nasm and
improved wall clock build times from 56.1 seconds to 53.2 seconds or
around 5% speed improvement.
Signed-off-by: Colin Ian King <colin.i.king@intel.com>
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>
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>
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>
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>
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>
Without this, gcc may throw a warning which breaks the --enable-werror
build. It is good practice anyway...
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Tidy up the way path syntax selection is handled, and make it possible
to specify it outside this file (e.g. in a Makefile) if need be.
Haiku, like BeOS, uses Unix syntax.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This differs from a plain old comment in the following ways:
1. It is optionally macro-expanded;
2. It has a dash prefix;
3. It can be used inside .nolist macros.
Suggested-by: <pushbx@ulukai.org>
Resolves: https://bugzilla.nasm.us/show_bug.cgi?id=3392915
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Add the %map() function which can apply arguments to a macro from a
list.
Allow the user to specify the desired radix for an evaluated
parameter. It doesn't make any direct difference, but can be nice for
debugging or turning into strings.
As part of this, split expand_one_smacro() into two parts: parameter
parsing and macro expansion. This is a very straightforward splitting
of two mostly unrelated pieces of functionality.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Add the %abs() function, to produce the absolute value as an
always-positive decimal constant.
Change the order of the arguments for %num().
Refactor the handling of optional arguments, to reduce the amount of
redundant code. This is currently only used for builtin functions, but
might be extended in the future.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
We need the ability to produce consistent output for our own tests,
anyway, so make this a user-accessible feature. This was requested in
BR 3392635.
This obsoletes the NASM_TEST_RUN environment variable; simply use the
normal NASMENV environment variable instead.
The .obj tests in travis needed to be updated in order to remove the
rather pointless suffix " CONST" from the NASM signatures.
Reported-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Simplify the code generators by merging the two hash constant arrays
into one. The hash is effectively the same, although the order of the
constants differ (possibly in a way which makes the indexing easier.)
The main difference is the amount of code is necessary to generate
each of the output C files.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Sometimes we want to search for an exact key only, and reject the case
when tree->key < key. Add rb_search_exact() for this purpose, rather
than forcing the caller to perform the comparison in open code.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Add operations to get the first and last entry in the tree,
respectively. Searching for 0 or ~UINT64_C(0) is not sufficient in the
presence of duplicated keys, and is more inefficient anyway.
rb_first() followed by rb_next() until NULL, or equivalently rb_last()
followed by rb_prev() until NULL, can be used to walk the tree in key
order (ascending or descending), including all duplicate key
entries.
Since this is a *threaded* tree now, this walk can safely free entires
as it goes along, as long as the whole tree is destroyed; once any one
entry has been freed, the tree is no longer valid for anything other
than proceeding with the same tree walk.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
The actual pointer value in offsetin() cancels out, but clang still
miscompiles offsetin() for an uninitialized pointer, considering the
value to be completely undefined. Initialize pointer being passed to
offsetin() to make clang happy; both the gcc and clang optimizers
discover later in the code that the initialization is unused and
removes it from the code.
Although technically undefined behavior, this is in my opinion a
severe quality of implementation bug in clang, and I will file a bug
report accordingly.
Reported-by: Jasper Lievisse Adriaanse <r+nasm@jasper.la>
Reported-by: David Bohman <debohman@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
The name UNUSED is too generic and may conflict with future
macro definitions. This is machine-generated code anyway, so
rename UNUSED to UNUSED_HASH_ENTRY.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>