nasmtok.el: include builtin macros and macro functions

Use a really hacky way to run the NASM binary and extract automatic
builtin macros. As this is not something normally supported during the
NASM build process, the output is canned and will need to be run
manually.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2025-10-01 16:06:40 -07:00
parent 094cc54cd3
commit 03391e61ec
3 changed files with 204 additions and 16 deletions

161
misc/builtin.mac Normal file
View file

@ -0,0 +1,161 @@
;;; Automatically generated list of builtin macros
%define __?FILE?__
%define __?LINE?__
%define __?BITS?__
%define __?PTR?__
%define __?DEFAULT?__
%idefine %abs(=)
%idefine %chr(=+)
%idefine %count(+)
%idefine %depend()
%idefine %eval(=+)
%idefine %hs2b(&&+)
%idefine %map(+)
%idefine %null(+)
%idefine %pathsearch()
%idefine %realpath()
%idefine %str(&+)
%idefine %strcat(&&+)
%idefine %strlen(&&)
%idefine %tok(&&)
%idefine %is(+)
%idefine %isctx(+)
%idefine %isdef(+)
%idefine %isdefalias(+)
%idefine %isdifi(+)
%idefine %isdirective(+)
%idefine %isempty(+)
%idefine %isenv(+)
%idefine %isfile(+)
%idefine %isid(+)
%idefine %isidn(+)
%idefine %isidni(+)
%idefine %ismacro(+)
%idefine %isnum(+)
%idefine %isstr(+)
%idefine %istoken(+)
%idefine %isusable(+)
%idefine %isusing(+)
%idefine %isn(+)
%idefine %isnctx(+)
%idefine %isndef(+)
%idefine %isndefalias(+)
%idefine %isndifi(+)
%idefine %isndirective(+)
%idefine %isnempty(+)
%idefine %isnenv(+)
%idefine %isnfile(+)
%idefine %isnid(+)
%idefine %isnidn(+)
%idefine %isnidni(+)
%idefine %isnmacro(+)
%idefine %isnnum(+)
%idefine %isnstr(+)
%idefine %isntoken(+)
%idefine %isnusable(+)
%idefine %isnusing(+)
%idefine %hex(=+/ux)
%idefine %sel(=,+)
%idefine %cond(=,,)
%idefine %num(=,=,=)
%idefine %substr(&&,=,=)
%idefine %ord(&&,=,=)
%idefine %b2hs(&&,&&)
%idefine %findi(&&,+)
%idefine %find(&&,+)
%define __?PASS?__
%define __?SECT?__
%defalias __SECT__
%define __?SECTALIGN_ALIGN_UPDATES_SECTION?__
%defalias __SECTALIGN_ALIGN_UPDATES_SECTION__
%define __?FLOAT_DAZ?__
%define __?FLOAT_ROUND?__
%define __?FLOAT?__
%defalias __FLOAT_DAZ__
%defalias __FLOAT_ROUND__
%defalias __FLOAT__
%defalias __NASM_MAJOR__
%defalias __NASM_MINOR__
%defalias __NASM_SUBMINOR__
%defalias __NASM_PATCHLEVEL__
%defalias __NASM_SNAPSHOT__
%defalias __NASM_VERSION_ID__
%defalias __NASM_VER__
%defalias __OUTPUT_FORMAT__
%defalias __DEBUG_FORMAT__
%defalias __DATE__
%defalias __DATE_NUM__
%defalias __TIME__
%defalias __TIME_NUM__
%defalias __UTC_DATE__
%defalias __UTC_DATE_NUM__
%defalias __UTC_TIME__
%defalias __UTC_TIME_NUM__
%defalias __POSIX_TIME__
%defalias __FILE__
%defalias __LINE__
%defalias __BITS__
%defalias __PTR__
%defalias __PASS__
%idefine __?infinity?__
%idefine __?nan?__
%idefine __?qnan?__
%idefine __?snan?__
%idefine __?float8?__
%idefine __?float16?__
%idefine __?float32?__
%idefine __?float64?__
%idefine __?float80m?__
%idefine __?float80e?__
%idefine __?float128l?__
%idefine __?float128h?__
%idefine __?utf16?__
%idefine __?utf16le?__
%idefine __?utf16be?__
%idefine __?utf32?__
%idefine __?utf32le?__
%idefine __?utf32be?__
%idefine __?ilog2e?__
%idefine __?ilog2w?__
%idefine __?ilog2f?__
%idefine __?ilog2c?__
%idefalias __infinity__
%idefalias __nan__
%idefalias __qnan__
%idefalias __snan__
%idefalias __float8__
%idefalias __float16__
%idefalias __float32__
%idefalias __float64__
%idefalias __float80m__
%idefalias __float80e__
%idefalias __float128l__
%idefalias __float128h__
%idefalias __utf16__
%idefalias __utf16le__
%idefalias __utf16be__
%idefalias __utf32__
%idefalias __utf32le__
%idefalias __utf32be__
%idefalias __ilog2e__
%idefalias __ilog2w__
%idefalias __ilog2f__
%idefalias __ilog2c__
%define __?NASM_HAS_IFDIRECTIVE?__
%define __?NASM_MAJOR?__
%define __?NASM_MINOR?__
%define __?NASM_SUBMINOR?__
%define __?NASM_PATCHLEVEL?__
%define __?NASM_VERSION_ID?__
%define __?NASM_VER?__
%define __?SECT?__
%define __?DATE?__
%define __?DATE_NUM?__
%define __?TIME?__
%define __?TIME_NUM?__
%define __?UTC_DATE?__
%define __?UTC_DATE_NUM?__
%define __?UTC_TIME?__
%define __?UTC_TIME_NUM?__
%define __?POSIX_TIME?__
%define __?OUTPUT_FORMAT?__

20
misc/dumpbuiltin.sh Executable file
View file

@ -0,0 +1,20 @@
#!/bin/sh -
# Copyright 1996-20xx The NASM Authors - All Rights Reserved
# SPDX-License-Identifier: BSD-2-Clause
#
# Run the nasm binary and extract the list of macros that may or may
# not be defined in .mac files.
#
tmp="$(mktemp -d)"
[ -n "$tmp" ] || exit 1
NASM="${NASM:-../nasm}"
: > "$tmp/junk.asm"
"$NASM" -f bin -o "$tmp/junk.bin" -Lsb -l "$tmp/junk.lst" "$tmp/junk.asm"
printf ';;; Automatically generated list of builtin macros\n'
sed -n -E -e 's/^[^;]*;;; *(%i?(define|defalias|macro)) ([^ ]*) .*$/\1 \3/p'\
< "$tmp/junk.lst"
rm -rf "$tmp"

View file

@ -157,6 +157,26 @@ sub read_version($) {
close($v);
}
sub read_macro_file($) {
my($file) = @_;
open(my $fh, '<', $file) or die;
while (defined(my $l = <$fh>)) {
next unless ($l =~ /^\s*\%/);
my @f = split(/\s+/, $l);
next unless (scalar(@f) >= 2);
next if ($f[1] =~ /^[\%\$][^\(]+$/); # Internal use only
$f[1] =~ s/\(.*$//; # Strip argument list if any
$f[1] = lc($f[1]) if ($f[0] =~ /^\%i/);
if ($f[0] =~ /^\%(i)?(assign|defalias|define|defstr|substr|xdefine)\b/) {
addtoken('smacro', $f[1]);
} elsif ($f[0] =~ /^\%i?macro$/) {
addtoken('mmacro', $f[1]);
}
}
close($fh);
}
sub read_macros($$) {
my($srcdir, $objdir) = @_;
my @dirs;
@ -167,24 +187,11 @@ sub read_macros($$) {
opendir(my $dh, $dir) or die;
while (defined(my $fn = readdir($dh))) {
next unless ($fn =~ /\.mac$/);
open(my $fh, '<', File::Spec->catfile($dir, $fn)) or die;
while (defined(my $l = <$fh>)) {
next unless ($l =~ /^\s*\%/);
my @f = split(/\s+/, $l);
next unless (scalar(@f) >= 2);
$f[1] =~ s/\(.*$//; # Strip argument list if any
next if ($f[1] =~ /^[\%\$]/); # Internal use only
$f[1] = lc($f[1]) if ($f[0] =~ /^\%i/);
if ($f[0] =~ /^\%(i)?(assign|defalias|define|defstr|substr|xdefine)\b/) {
addtoken('smacro', $f[1]);
} elsif ($f[0] =~ /^\%i?macro$/) {
addtoken('mmacro', $f[1]);
}
}
close($fh);
read_macro_file(File::Spec->catdir($dir, $fn));
}
}
# Don't read the whole misc directory!
read_macro_file(File::Spec->catdir($srcdir, 'misc/builtin.mac'));
}
sub make_lines($$@) {