0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-07-24 10:25:42 -04:00

195 Commits

Author SHA1 Message Date
H. Peter Anvin
7b8cd67d51 preproc: add warning for empty %{} construct
An empty %{} becomes % which is simply the arithmetic
operator. Although that is consistent, it might be surprising for
users, to issue a warning.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:01:00 -08:00
H. Peter Anvin
baae422e61 preproc: an empty %[] construct generates a null token, drop
%[] amounts to an empty token; this needs to be handled specially so
that it gets properly dropped.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392806
Reported-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 11:47:22 -08:00
Oleg Oshmyan
80a4e8e454 preproc: fix misparsing of << as right shift
Regression in commit 20e0d616dc954d567c8bf2c7e11cc5d6c10ac544.

Independently discovered and fixed by C. Masloch:
https://bugzilla.nasm.us/show_bug.cgi?id=3392747

Signed-off-by: Oleg Oshmyan <chortos@inbox.lv>
2021-09-21 23:30:53 +03:00
H. Peter Anvin (Intel)
5368e45794 preproc: fix pasting of TOKEN_HERE, TOKEN_BASE and TOKEN_QMARK
Make the pasting behavior of TOKEN_QMARK, TOKEN_HERE and TOKEN_BASE
match the NASM 2.15 behavior: ? is a keyword and pastes as an ID, $
and $$ are treated as operators (which doesn't seem to make much
sense, but it is the current legacy behavior.)

Reported-by: C. Masloch <pushbx@ulukai.org>
Bugzilla: https://bugzilla.nasm.us/show_bug.cgi?id=3392733
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2021-03-24 10:46:45 -07:00
Cyrill Gorcunov
79ac0d686d Merge branch 'nasm-2.15.xx'
* nasm-2.15.xx:
  preproc: prohibit unmacro while macro expansion
2020-12-31 16:50:25 +03:00
Igor Munkin
f95c7e983c preproc: prohibit unmacro while macro expansion
If macro is undefined while it's being expanded, use after free occurs,
since the MMacro instance is released, but it is still used to proceed
the expansion.

This change forbids macro undefinition: non-fatal error is raised and
the MMacro instance is not released if it is being processed by NASM
preprocessor.

Consider the following example:
| $ cat test.asm
| %macro m 0
| %unmacro m 0
| %endmacro
| m
| $ ./nasm test.asm
| test.asm:4: error: `%unmacro' can't undefine the macro being expanded
| test.asm:2: ... from macro `m' defined here

Fixes BR3392531 and BR3392716.

Signed-off-by: Igor Munkin <imun@cpan.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-12-31 16:42:31 +03:00
Cyrill Gorcunov
5e587fda09 preproc: use explicit addressing while clearing smacro
Better point out explicitly that SMacro::next member
is untouched, thus do not use SMacro::next and an array.

CID 1432925

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-10-05 00:09:18 +03:00
H. Peter Anvin (Intel)
6f01aca07c asm/preproc.c: in tokenize() advance pointer past ? for TOKEN_QMARK
When we process a TOKEN_QMARK we also need to advance p, in order to
get the proper start for the next token.

This fixes travis test br3392707.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-11 18:37:35 -07:00
H. Peter Anvin (Intel)
dab902cbdc preproc: when parsing an smacro template, don't mistake , for )
The operation of the ',' and ')' tokens are very similar, except for:

',' issues a error if the processed parameter is greedy;
')' sets the "done" variable.

The code would incorrectly set "done" for a ',' token. This fixes
travis test br3392711.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 14:35:49 -07:00
H. Peter Anvin (Intel)
ff97eb6f7e preproc: don't loop on a variable that doesn't advance
When pasting and stripping %+ and whitespace tokens, we either need to
set *nextp in the loop, or treat next as a separate variable and
update *nextp after the loop finishes. This implements the second
option.

This fixes travis test "amx".

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 14:09:37 -07:00
H. Peter Anvin (Intel)
a9ed8ced7c preproc: add %eval() function, add back %isn*()
Add the %eval() preprocessor function. It evaluates each of its
arguments like a number and expands to a comma-separated lists of the
evaluated arguments.

To support this, add the concept of "true varadic" macros, which are
only used internally. True varadic macros differ from greedy macros in
that the parameter list is still parsed as individual parameters and
provided to the expansion function. As this isn't meaningful for
user-defined macros, there is no way to specify it from a directive.

Add back the %isnfoo() functions. Although one could just as well write
!%isfoo(), it doesn't cost much to provide them, and might help avoid
programmer confusion.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 13:52:32 -07:00
H. Peter Anvin (Intel)
cc64861a61 Merge tag 'nasm-2.15.05'
NASM 2.15.05

Resolved Conflicts:
	asm/preproc.c
	version

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-31 12:23:36 -07:00
H. Peter Anvin (Intel)
2b4886afdd BR 3392715: fix %ifid with $ and $$
%ifid $ and %ifid $$ has traditionally been false, revert to that
behavior.

Reported-by: Mike Hommey <mh+anfz@glandium.org>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-27 11:43:08 -07:00
H. Peter Anvin (Intel)
cc843efa38 Merge tag 'nasm-2.15.04'
NASM 2.15.04

Conflicts:
	asm/listing.h
	asm/pptok.pl
	asm/preproc.c
	version

This doesn't pass travis test 3392711, which is using an extremely odd
construct of %?? in the middle of an argument sequence for an smacro
while not being in a macro itself, and expecting it to expand to the
macro name. This seems to *really* confuse the master branch.

Resolve this later...
2020-08-25 16:16:09 -07:00
H. Peter Anvin (Intel)
1227a5d7fb Merge remote-tracking branch 'github/master'
Resolved Conflicts:
	asm/preproc.c

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-25 15:56:05 -07:00
H. Peter Anvin (Intel)
16ab7aed21 debug: feed single-line macro definitions and include hierachy to dfmt
At least DWARF can encode C-style macros. In doing so, it wants the
file include hierarchy, so give the debug format backend the option of
receiving that information from the preprocessor.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-25 15:52:05 -07:00
Cyrill Gorcunov
7c88289e22 BR3392711: preproc: fix memory corruption in expand_one_smacro
The mempcpy helper returns *last* byte pointer thus when
we call set_text_free we have to pass a pointer to the
start of the string.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-08-18 11:27:03 +03:00
Cyrill Gorcunov
8806c3ca00 BR3392712: pp_tokline: fix double free
Make sure the data being freed get double
freed after -- the pointers must be zapped
(actually nasm_free and free_tlist support
being called with NULL pointer as an argument).

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-08-17 21:30:14 +03:00
H. Peter Anvin (Intel)
6299a3114c BR 3392708: fix NULL pointer reference for invalid %stacksize
After issuing an error message for a missing %stacksize argument, need
to quit rather than continuing to try to access the pointer.

Fold uses of tok_text() while we are at it.

Reported-by: Suhwan <prada960808@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-30 15:57:57 -07:00
H. Peter Anvin
6263a2a4c2 preproc: add %*? and %*??
The %? and %?? tokens are ambiguous when used inside a multi-line
macro. Add tokens %*? and %*?? that only expand during single-macro
expansion.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-18 13:47:59 -07:00
H. Peter Anvin
d6b8c53b8c Merge tag 'nasm-2.15.03'
NASM 2.15.03

Resolved Conflicts:
	asm/preproc.c

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-17 17:58:38 -07:00
H. Peter Anvin
e830e92b77 preproc: when printing errors, don't descent into unlisted macros
If macros are nolisted, *or* they don't have any filename associated
with them, it is absolutely pointless to try to descend into them for
error messages, so just don't, even if -Lb is provided.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2020-07-16 22:50:33 -07:00
H. Peter Anvin (Intel)
4c0bd9e736 preproc: BR 2292698: fix handling of whitespace around %+
The previous code to fix whitespace around and multiple %+ symbols in
a row (checkin 122c5fb75986adc37dfb147cc2a613e3ebc66e80) had some
seriously broken pointer handling when zapping tokens. This could
cause paste_tokens() to go into an infinite loop because it would
attach %+ to another token and then immediately break them apart
again, over and over.

Reported-by: <alexfru@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-15 16:16:57 -07:00
H. Peter Anvin (Intel)
64c7c763c2 Change dfmt->debug_macros to dfmt->debug_mmacros
... in preparation for adding dfmt->debug_smacros.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-14 12:05:03 -07:00
H. Peter Anvin (Intel)
ecd4ea94b6 preproc, doc: change a few remaining TOK_ -> TOKEN_ in comments only
A handful of comments still had TOK_ instead of TOKEN_.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-14 11:51:35 -07:00
H. Peter Anvin (Intel)
20e0d616dc preproc: unify token codes between the preprocessor and assembler
The different token codes between the preprocessor and the assembler
is a completely unnecessary headache. Furthermore, lumping all the
operators under TOK_OTHER in the preprocessor causes a whole bunch of
unnecessary headaches.

In combining them, the only tricky part is that PP_CONCAT_MASK() is no
longer usable, as the range of token codes is too large. Replace with
dedicated category masks.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-14 11:50:55 -07:00
H. Peter Anvin (Intel)
1d151a8558 preproc: when parsing a # marker, use C-style string unquoting
To handle escape codes in filename strings after # markers correctly,
we need nasm_unquote() to be aware that it is using C escapes;
otherwise things like "foo`bar" will break.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-13 14:14:28 -07:00
Cyrill Gorcunov
4c3798b7e6 BR 3392696: nasm_quote -- fixup callers
In 41e9682efed7cd1df133b1b4ac806e07723f1486 we've
changed the nasm_quote arguments still not all callers
were converted which could lead to nil dereference.

[hpa: no need to call strlen() for the asm/preproc.c chunk]

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2020-07-12 06:06:02 -07:00
H. Peter Anvin
6a9d1e56c6 Revert "BR 3392696: nasm_quote -- fixup callers"
This reverts commit 6c300aa4882bc1662df899501f23ff5a93a3547f.

Will apply a modified version from the nasm-2.15.xx branch instead.
2020-07-12 06:04:48 -07:00
H. Peter Anvin
d831915543 preproc: add %is...() function-like macros
Add the first "preprocessor functions". These are simply "magic"
single-line macros with a suitable expansion function. The first
application is functions equal to the %if directives, e.g.
%ifdef blah == %if %isdef(blah) except can be used anywhere (not just
in %if statements like defined() in C.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2020-07-12 06:03:58 -07:00
Cyrill Gorcunov
65c6ba8716 BR 3392696: nasm_quote -- fixup callers
In 41e9682efed7cd1df133b1b4ac806e07723f1486 we've
changed the nasm_quote arguments still not all callers
were converted which could lead to nil dereference.

[hpa: no need to call strlen() for the asm/preproc.c chunk]

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2020-07-12 06:00:51 -07:00
Cyrill Gorcunov
6c300aa488 BR 3392696: nasm_quote -- fixup callers
In 41e9682efed7cd1df133b1b4ac806e07723f1486 we've
changed the nasm_quote arguments still not all callers
were converted which could lead to nil dereference.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-07-11 19:18:30 +03:00
H. Peter Anvin (Intel)
10c9aa37a4 preproc: drop preproc_ops
There is only one preprocessor now, so drop the indirection through
preproc_ops.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 19:26:44 -07:00
H. Peter Anvin (Intel)
d9ea17fb47 preproc: saner handling of cpp-style line directives
NASM now supports a proper superset of cpp line number markers, so
there is no need to hack around them using the
"prepreprocessor". Instead, just put a quick test in do_directive()
treating it just like %line, except convert a "-quoted string into a
`-quoted string.

(This can break if there is a ` or \" sequence in the string... fix
that at some point. This is still much better than what there is now.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 18:44:15 -07:00
H. Peter Anvin (Intel)
fcd3cb8861 preproc: preserve %[...] in listings
When generating list output, preserve %[...] in the output if we list
a TOK_INDIRECT. The tokenization process removes these deliminators,
so we have to explicitly put them back.

This doesn't affect assembly output, which will only ever be generated
after all TOK_INDIRECT tokens have been removed, but it does affect
some of the listing modes.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 17:22:47 -07:00
H. Peter Anvin (Intel)
a79a700208 preproc: add a %null directive for the masm macro package
Instead of %pragma ignore, use a new %null directive which ignores the
rest of the line, without bothering to expand it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 02:55:50 -07:00
H. Peter Anvin (Intel)
23abe9fe88 preproc: correctly handle %00 capturing a label
defining->dstk.mmac should point back to "defining" when the topmost
definition block is a %macro block.

Otherwise %00 will not inhibit label emission.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 01:52:49 -07:00
H. Peter Anvin (Intel)
1593c1dc60 preproc: don't call do_directive recursively
The hacky %arg and %local directives build directives as strings which
they then tokenize and call do_directive() recursively with. Factor
these out and remove the recursion.

It is too bad that %arg and %local didn't include the [] brackets in
the created macros; if so it would have been possible to do something
sane with 64-bit register operands. Sigh.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 01:21:44 -07:00
H. Peter Anvin (Intel)
72a555972f error: get rid of ERR_PASS1
ERR_PASS1 only remains in three places:

a. Unterminated %! string, an error
   - Should be signalled no matter which pass it is encountered in
b. Two cases of map file problems in outbin
   - The buffered warning system take care of that issue

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 01:10:30 -07:00
H. Peter Anvin (Intel)
fcb3d135ec preproc: a negative number is actually two tokens
A negative number is two tokens: a minus sign and a positive
number. For most uses we still want to generate signed numbers; for
specific uses there might be motivation for an unsigned output, but in
most cases it would be confusing.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 01:08:47 -07:00
H. Peter Anvin (Intel)
93dd437ac9 preproc: move reverse_tokens() into define_smacro()
The fact that smac->expansion is stored in reverse order is a detail
of the implementation, and should not be forced on the caller of
define_smacro().

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-10 00:56:03 -07:00
H. Peter Anvin (Intel)
32322a9a93 preproc: get rid of the prepreprocessor and the nop preprocessor
Fold the prepreprocessor and the nop preprocessor into the main
preprocessor. This means handling # cpp-like lines and TASM
compatibility tokens in the preprocessor proper, but that is really
not very hard to do.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-09 23:34:52 -07:00
H. Peter Anvin (Intel)
c36cdf8a55 Merge remote-tracking branch 'github/debug-macros'
Resolved Conflicts:
	version

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-09 21:16:49 -07:00
H. Peter Anvin (Intel)
7501601dbd preproc: pass macro debug information in invocation order
Reverse the invocation lists once each list is complete, so that the
lists passed to the macro backend are in true invocation order.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-08 10:13:46 -07:00
H. Peter Anvin (Intel)
942092e32a macro debug: use rb_search_exact()
We need the exact match in the rbtree for the current section. An
approximate match is not acceptable.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-08 09:53:49 -07:00
H. Peter Anvin (Intel)
50184c26c7 debug: collect macro information for the debug backend
Collect macro call/nesting information for the benefit of the debug
back end. So far, the only backend for which this is provided is the
debug back end, to show what information is present.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-08 09:28:44 -07:00
H. Peter Anvin
63769c588e Merge remote-tracking branch 'github/nasm-2.15.xx' 2020-07-05 15:27:04 -07:00
H. Peter Anvin (Intel)
122c5fb759 preproc: handle %+ pasting after empty expansions
%+ tokens can end up next to each other, or at the beginning or the
end of an expansion if we try to paste the output of empty
macros. This is perhaps particularly likely to happen in %[]
expressions.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-05 03:39:04 -07:00
H. Peter Anvin (Intel)
5b7369d7e0 Make debug info and error messages correctly reflect macros and reps
1. Error messages would issue with the line number of %endrep.
2. Debug line information would ignore both macros and reps.
   This is doubly wrong; macros are semantically equivalent to
   inline functions, and it is expected that debuggers trace
   into these functions.

These changes finishes the last parts of moving all responsibility for
the listing enable/disable into the preprocessor, so remove the
way over-complicated macro inhibit facility from the listing module
entirely.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-07-05 02:16:13 -07:00
H. Peter Anvin (Intel)
1939b69fdf Merge remote-tracking branch 'github/nasm-2.15.xx' 2020-07-02 20:44:57 -07:00