preproc: add %i... variants, evaluated macro parameters, cleanups

All directives which create single-line macros now have %i... variants
to define case-insensitive versions. Case insensitive rather sucks,
but at least this way it is consistent.

Single-line macro parameters can now be evaluated as a number, as done
by %assign. To do so, declare a parameter starting with =, for
example:

%define foo(x,=y) mov [x],macro_array_y

... would evaluate y as a number but leave x as a string.

NOTE: it would arguably be better to have this as a per-instance
basis, but it is easily handled by having a secondary macro called
with the same argument twice.

Finally, add a more consistent method for defining "magic" macros,
which need to be evaluated at runtime. For now, it is only used by the
special macros __FILE__, __LINE__, __BITS__, __PTR__, and __PASS__.

__PTR__ is a new macro which evaluates to word, dword or qword
matching the value of __BITS__.

The magic macro framework, however, provides a natural hook for a
future plug-in infrastructure to hook into a scripting language.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2019-02-26 14:00:54 -08:00
parent a8604c83fa
commit 8b26247442
6 changed files with 407 additions and 244 deletions

View File

@ -1,6 +1,6 @@
## --------------------------------------------------------------------------
##
## Copyright 1996-2016 The NASM Authors - All Rights Reserved
## Copyright 1996-2019 The NASM Authors - All Rights Reserved
## See the file AUTHORS included with the NASM distribution for
## the specific copyright holders.
##
@ -70,8 +70,12 @@
%ideftok
%if*
%imacro
%irmacro
%include
%ipathsearch
%irmacro
%istrcat
%istrlen
%isubstr
%ixdefine
%line
%local

File diff suppressed because it is too large Load Diff

View File

@ -216,6 +216,14 @@ static char *emit_utf8(char *q, int32_t v)
return q;
}
/*
* Quote a C string
*/
char *nasm_quote_cstr(const char *str)
{
return nasm_quote(str, strlen(str));
}
/*
* Do an *in-place* dequoting of the specified string, returning the
* resulting length (which may be containing embedded nulls.)

View File

@ -37,6 +37,7 @@
#include "compiler.h"
char *nasm_quote(const char *str, size_t len);
char *nasm_quote_cstr(const char *str);
size_t nasm_unquote(char *str, char **endptr);
char *nasm_skip_string(char *str);

View File

@ -1,6 +1,6 @@
;; --------------------------------------------------------------------------
;;
;; Copyright 1996-2016 The NASM Authors - All Rights Reserved
;; Copyright 1996-2019 The NASM Authors - All Rights Reserved
;; See the file AUTHORS included with the NASM distribution for
;; the specific copyright holders.
;;
@ -49,12 +49,6 @@ STD: nasm
; here, not all of them are: the user-level form of a format-specific
; directive should be defined in the module for that directive.
; These three need to be defined, though the actual definitions will
; be constantly updated during preprocessing.
%define __FILE__
%define __LINE__
%define __BITS__
%define __SECT__ ; it ought to be defined, even if as nothing
%imacro section 1+.nolist

4
test/evalmacro.asm Normal file
View File

@ -0,0 +1,4 @@
%define tonum(=x) x
dd tonum(1+3)
dd tonum(5*7)