Commit Graph

363 Commits

Author SHA1 Message Date
H. Peter Anvin
14f0328aa1 eval: don't try to poke *opflags if opflags is NULL
While changing this code around to not do redundant lookups, dropped
this NULL pointer check. Oops.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-09-12 18:34:14 -07:00
H. Peter Anvin
d626f355f6 preproc: correct handling of %ifdef for aliases
Correctly handling %ifdef when operating on aliases; we had an
infinite loop going...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-09-12 18:31:29 -07:00
H. Peter Anvin
86b2e93081 assemble: fix too aggressive dropping of overflow warnings
Drop down to OUT_WRAP when the size is big enough, as opposed to not
doing any tests at all.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-09-12 18:20:07 -07:00
H. Peter Anvin
7ad25b2e18 Change LBL_NONE to LBL_none
NASM convention is to use all-upper-case for "real" information, and
mixed-case (upper case common prefix, lower case description) for
meta-information. This is a highly useful distinction.

Thus "LBL_NONE" implies an actual label of type "NONE", as opposed to
no label at all.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-09-12 20:26:23 -04:00
H. Peter Anvin
90b1ccff86 Drop unnecessary EXTERN symbols
Currently, NASM always issues as an unknown symbol any symbol declared
EXTERN. This is highly undesirable when using common header files,
as it might cause the linker to pull in a bunch of unnecessary
modules, depending on how smart the linker is.

Add a new REQUIRED directive which behaves like the old EXTERN, for
the use cases which might still need this behavior.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-09-12 20:21:03 -04:00
H. Peter Anvin
a73ccfebcc error: replace nasm_verror() indirection with preproc callback
Since pp_error_list_macros() was introduced, the only need for
pp_verror() is to suppress error messages in certain contexts. Replace
this function with a preprocessor callback,
preproc->pp_suppress_error(), so we can drop the nasm_verror()
function pointer entirely.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-28 19:02:47 -07:00
H. Peter Anvin
6a4353c4c2 errors: be more robust in handling unexpected fatal errors
Introduce a new error level, ERR_CRITICAL, beyond which we will
minimize the amount of code that will be executed before we die; in
particular don't execute any memory allocations, and if we somehow end
up recursing, abort() immediately.

Basically, "less than panic, more than fatal."

At this point this level is used by nasm_alloc_failed().

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-28 18:32:46 -07:00
H. Peter Anvin
2201ceb238 nasm: avoid null pointer reference on VERY early memory allocation failure
If we get a memory allocation failure before preproc is initialized,
we could end up taking a NULL pointer reference while trying to unwind
macros.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-27 17:19:07 -07:00
H. Peter Anvin
d235408c65 preproc: standard macros now C-safe, %aliases off, fix %? recursion
Enough users expect the namespace starting with underscore to be safe
for symbols. Change our private namespace from __foo__ to
__?foo?__. Use %defalias to provide backwards compatiblity (by using
%defalias instead of %define, we handle the case properly where the
user changes the value.)

Add a preprocessor directive:

%aliases off

... to disable all smacro aliases and thereby making the namespace
clean.

Finally, fix infinite recursion when seeing %? or %?? due to
paste_tokens(). If we don't paste anything, the expansion is done.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-27 16:42:41 -07:00
H. Peter Anvin (Intel)
7eb18213b7 preproc: make sure the mmacro params list is NULL-terminated
If we adjust nparams due to default or greedy arguments, we need to
re-terminate the params[] array.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 16:24:46 -07:00
H. Peter Anvin (Intel)
d4607846a4 preproc: smacro argument lists can't be preceded by space
The smacro argument list cannot be preceded by whitespace, or we
wouldn't be able to define no-argument smacros the expansion of which
starts with (.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 16:19:37 -07:00
H. Peter Anvin (Intel)
ffe89ddaed preproc: fix comment -La -> -Lm
The -Lm option was briefly called -Lm during development, fix.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 16:06:36 -07:00
H. Peter Anvin (Intel)
41d91a9273 preproc: mmacro argument fixes; listing option for mmacro args
Correctly handle empty mmacro arguments that still have preceding
whitespace tokens.

Default mmacro parameters are obtained by count_mmac_params() so they,
too, need to be shifted over by one.

Add an option to list mmacro calls with arguments. Name this -Lm;
remove the old -Lm option to -Ls since it is related to single-line
macros.

Trivially optimize the case where an mmacro is called from within
itself: if all possible mmacros are excluded by loop removal, there is
no need to delve into the mmac processing code.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 16:03:46 -07:00
H. Peter Anvin (Intel)
68075f8fa6 preproc: fix uninitialized variables
Fix uninitialized variables (not just warnings, actual bugs.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 12:28:05 -07:00
H. Peter Anvin (Intel)
a1a844697d preproc: fix varadic macros, add conditional comma operator
Fix the (severely broken handling of) varadic macros.

Add a conditional comma operator "%,". This expands to a comma unless
followed by a null expansion of some sort, which allows suppressing
the comma before an empty argument (usually varadic, but not
necessarily.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 01:32:28 -07:00
H. Peter Anvin (Intel)
62cf4aaef6 preproc: add suppport for stringify, nostrip, greedy smacro arguments
Add a few more types of smacro arguments, and clean stuff up in
process.

If declared with an &, an smacro parameter will be quoted as a string.
If declared with a +, it is a greedy/varadic parameter.
If declared with an !, don't strip whitespace and braces (useful with &).

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-20 00:05:41 -07:00
H. Peter Anvin (Intel)
de7acc3a46 preproc: defer %00, %? and %?? expansion for nested macros, cleanups
BR 3392603: When doing nested macro definitions, we need %00, %? and
%?? expansion to be deferred to actual expansion time, just as the
other parameters.

Do major cleanups to the mmacro expansion code.

Reported-by: Alexandre Audibert <alexandre.audibert@outlook.fr>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-19 18:24:02 -07:00
H. Peter Anvin (Intel)
41e9705054 assemble.c: fix signed/unsigned comparison warning
Ponderance: if data->bits < globalbits, should we actually use
OUT_UNSIGNED rather than OUT_WRAP here?

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-19 15:47:36 -07:00
H. Peter Anvin (Intel)
b83621350c listing: add the -L+ option to enable all listing options
-L+ or %pragma list options ++ will enable all possible listing
 options.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-19 13:11:34 -07:00
H. Peter Anvin (Intel)
2586cee21d BR 3392472: don't complain on wraparound for lower bit modes
If the address we are using is >= the size of the instruction, then
don't complain on overflow as we can wrap around the top and bottom of
the address space just fine.

Alternatively we could downgrade it to OUT_WRAP in that case.

Reported-by: C. Masloch <pushbx@38.de>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-16 01:44:49 -07:00
H. Peter Anvin (Intel)
93d41d8296 BR 3392576: don't segfault on a bad %pragma limit
Don't segfault on a bad %pragma limit. Instead treat a NULL pointer as
an empty string.

Reported-by: Ren Kimura <rkx1209dev@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-16 01:12:54 -07:00
H. Peter Anvin (Intel)
8b6e6bf04f config.h: separate function and function pointer attributes; automate
Separate out function and function pointer attributes, as not all
versions of all compilers support both.

Have macros related to function attributes auto-generated by
autoheader. As a result, rename config.h.in to unconfig.h, to make it
more obvious that it is really intended to be included from some C
programs.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-16 00:08:27 -07:00
H. Peter Anvin (Intel)
9fbd9fb859 preproc: fix mmacro nesting prevention
BR 3392602: mmacros should not nest unless so explicitly specified.

Reported-by: C. Masloch <pushbx@38.de>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-15 19:26:52 -07:00
H. Peter Anvin (Intel)
4b282d0503 macros: can't use the __USE_*__ macro string anymore; fix comment stripping
Since we use 127 not 0 for end of line in stdmac packages now, we
can't simply use the __USE_*__ macro as a string to test for a %use
package. Keep an internal array of state instead.

Fix the stripping of comments from lines in macro files.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-15 11:53:19 -07:00
H. Peter Anvin (Intel)
6d5c77c95f stdmac: handle up to 160 directives, make macros.c more readable
Handle up to 160 directives for stdmac compression.  This is done by
allowing the directive numbers to wrap around (128-255, 0-31), using
127 for end of line, and forcing any whitespace character to be space.

Make macros.c a bit more legible by using #defines for the byte codes;
strictly for the benefit of the human reader.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-15 02:29:40 -07:00
H. Peter Anvin (Intel)
566a0f2187 pptok.pl: don't leak internal codes into pptok.c
If we have internal codes in pptok.c, we may have false matches for
them as tokens, plus, there is no reason for them to exist there. Go
back to putting NULL in those slots.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-15 01:37:48 -07:00
H. Peter Anvin (Intel)
97cbdd34d0 preproc: simplify handling of conditionals and casesense
Simplify the handling of conditionals; remove the PPC_* types.

Automate the generation of case-sensitive versus case-insensitive
directives, and make it so the bulk of the code doesn't have to worry
about it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-15 01:14:23 -07:00
H. Peter Anvin (Intel)
5e3d741b00 preproc: introduce alias smacros, cleanups
Introduce "alias smacros", which are the smacro equivalent of
symlinks; when used with the various smacro-defining and undefining
directives, they affect the macro they are aliased to. Only explicit
%defalias, %idefalias, and %undefalias affect them.

This is intended for being able to rename macros while retaining the
legacy names.

This patch also removes an *astonishing* amount of duplicated
code:

1. Every caller to defined_smacro() and undef_smacro() would call
   get_ctx() to mangle the macro name; push that into those functions.
2. Common code to get an smacro identifier.
3. Every code path that returns DIRECTIVE_FOUND also has to do
   free_tlist(origline); make it do so.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-14 23:50:37 -07:00
H. Peter Anvin (Intel)
8981724f17 masm.mac, parser: VERY limited MASM emulation package
Very limited MASM emulation.

The parser has been extended to emulate the PTR keyword if the
corresponding macro is enabled, and the syntax displacement[index] for
memory operations is now recognized.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-14 15:44:50 -07:00
H. Peter Anvin (Intel)
02b60ddd1c LEA: allow immediate syntax; ignore operand size entirely
The memory operand size of LEA doesn't matter in any way as it isn't
"real memory". Add an ANYSIZE option to ignore sizes entirely.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-14 15:23:00 -07:00
H. Peter Anvin
a635809620 list_option_mask(): return 0 (empty mask) for < '0'
Missing limit check at the bottom of the ASCII range.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 18:16:37 -07:00
H. Peter Anvin
d91519a107 listing: encapsulate the list_options encoding, make more comprehensive
Encapsulate the list_options() encoding in an inline function. We only
ever compute a mask with a non-constant input in two places (command
line and pragma parsing), so a slightly more complex mapping is of no
consequence; thus map a-z, A-Z and 0-9 as being the most likely
characters we may want to use as options. Space is left for two more :)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 18:04:04 -07:00
H. Peter Anvin
59d4ccc2b0 Add %pragma list options
Add a %pragma to set (or clear) listing options. It only takes effect
on the next assembly pass, however!

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 06:45:12 -07:00
H. Peter Anvin
06335873ae preproc: avoid dropping the facility name in %pragma
tline got advanced a token too far, with the obvious results that the
facility name got truncated. Skip whitespace *after* expand_smacro(),
not before.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 06:42:55 -07:00
H. Peter Anvin
f5d7d90148 preproc: fix double free in the handling of %pragma
expand_smacro() consumes its input, so we need to truncate the input
list so we can call free_tline(origline) safely.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 06:21:00 -07:00
H. Peter Anvin
6686de2bab preproc: add listing options to override nolist; some cleanups
Add listing options:

    -Lb    to show builtin macro packages
    -Lf    to override .nolist

Do some cleanups in the process, in particular generalize read_line()
between stdmac and file alternatives.

When processing stdmac, create an istk entry for it. This means stdmac
can be identified by istk->fp == NULL. At some future date there could
even be a function pointer to an appropriate read function.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 05:33:14 -07:00
H. Peter Anvin
3f51082bcd listing: clean up before a restart
With the -Lp option, the listing generator gets invoked multiple times
in the same session. If we already have a list file open, call
list_cleanup() before reinitializing; otherwise we get stray output in
the updated file.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 05:31:51 -07:00
H. Peter Anvin
a2c1c7d0d4 listing: coalesce TIMES in non-final passes, print <len>, clarify hex
Merge TIMES in the nonfinal passes, there is no point in getting <len
...> an arbitrary number of times.

Actually print <len> (OUT_RAWDATA without a data pointer), not <res>
(OUT_RESERVE).

Dropping the zero-fill for the hex format made the listing more
manageable, but it also doesn't immediately look like hex, plus there
is now the -Ld option. Put an h after hex (shorter than leading 0x) to
make it obvious.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 02:45:41 -07:00
H. Peter Anvin
355bfb879d Stylistic improvements to help text
Trivial stylistic improvements to the help text.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 01:55:00 -07:00
H. Peter Anvin
322bee0aac Additional listing options, improve help output, fix macro limits
Additional listing options:

   -Ld to display counts in decimal
   -Lp to output a list file in every pass (to make sure one exists)

Clean up the help output and make it comprehensive. The -hf and -y
options are no longer necessary, although they are supported for
backwards compatiblity.

Fix macro-levels so it actually count descent levels; a new
macro-tokens limit introduced for the actual token limit.

Slightly simplify the limits code.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-10 01:46:58 -07:00
H. Peter Anvin
ab6f831955 listing: when listing lines in macros and rep blocks, show the actual line
When printing lines coming from %rep blocks and macros, show the line
number corresponding to the line actually being printed.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-09 22:31:45 -07:00
H. Peter Anvin (Intel)
ad1f50a506 warnings.pl: remove one more instance of "scalar(%hash)"
scalar(%hash) is broken on old versions of Perl, use scalar(keys
%hash).

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 16:20:40 -07:00
H. Peter Anvin (Intel)
98031bfff4 preproc.c: make sure we have the correct token lengths
It turns out that in tokenize() we would sometimes truncate a token
string by inserting a NUL into the input string, expecting new_Token()
to pick it up using strlen(). With explicit lengths, that no longer
works, but there is a better solution anyway: instead of inserting
NUL characters, keep track of where the token actually ends and feed
the correct length to new_Token().

This triggered a buffer overflow in detoken(), add a debug level 2
assert for this condition. Use a relatively high debug level, because
strlen() is fairly expensive, and this is an extremely
performance-critical path.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 16:11:28 -07:00
H. Peter Anvin (Intel)
5067fde483 asm/nasm.c: make --debug=level actually work
Turns out there was no support for optional arguments at all.
[
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 16:10:17 -07:00
H. Peter Anvin (Intel)
fb118aecc5 obsolete: make the message clearer in the case of NEVER,!NOP
"instruction never implemented and removed from the target CPU"

... doesn't really make sense, so change it to ...

"instruction never implemented and invalid on the target CPU"

(still may seen redundant, but it is to distingush it from "and is a
noop on...")

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 15:01:28 -07:00
H. Peter Anvin (Intel)
5b39461178 obsolete handing: handle a few more subcases in a useful way
Distinguish instructions which have once been valid (OBSOLETE) from
those that never saw the light of day (NEVER). Futhermore, flag
instructions which devolve to an architectural noop from those with
undefined behavior and possibly recycled opcodes.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 14:52:16 -07:00
H. Peter Anvin (Intel)
fb11889040 BR 3392590: add warning for valid but obsolete instructions
Just becase one is compiling for an old CPU doesn't mean one wants to
use obsolete instructions that would not be forward compatible. Rename
the "obsolete" warning to "obsolete-removed" and create a new
"obsolete-valid" warning to go with it (-w[+-]obsolete controls both
options, as usual.)

Suggested-by: C. Masloch <pushbx@38.de>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 14:21:42 -07:00
H. Peter Anvin (Intel)
d73b10abd5 warnings.pl: BR 3392585: don't use scalar(%hash)
The idiom scalar(%hash) seems similar to scalar(@array), and in fact
is in current versions of Perl. However, in older versions of Perl,
the former is totally useless:

       Prior to Perl 5.25 the value returned was a string consisting
       of the number of used buckets and the number of allocated
       buckets, separated by a slash.  This is pretty much useful only
       to find out whether Perl's internal hashing algorithm is
       performing poorly on your data set.  For example, you stick
       10,000 things in a hash, but evaluating %HASH in scalar context
       reveals "1/16", which means only one out of sixteen buckets has
       been touched, and presumably contains all 10,000 of your items.
       This isn't supposed to happen.

       As of Perl 5.25 the return was changed to be the count of keys
       in the hash. If you need access to the old behavior you can use
       "Hash::Util::bucket_ratio()" instead.

Use scalar(keys %hash) instead.

Reported-by: Orkan Sezer <sezeroz@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 13:45:41 -07:00
H. Peter Anvin (Intel)
177a05d0ce perl files: clean up warnings
Clean up some perl warnings, some of which were legitimate (apparently
undef doesn't actually take a list of arguments, a common enough
mistake that it is mentioned in the man page!, and a list of variables
after "my" can be cantankerous), and some of which were nuisance but
were easy enough to clean up.

Maybe this can resolve the problems with very old version of Perl?

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 13:30:19 -07:00
H. Peter Anvin (Intel)
d6e817751e listing: add -L option for additional listing info
Add an -L option for additional listing information. Currently
supported is -Le, which emits each line after processing through the
preprocessor, and -Lm, which displays each single-line macro defined
or undefined.

NASM doesn't preserve the names of unused arguments, nor does it have
any technical reason to do so. Instead of adding complexity to save
them, make unnamed parameters official by specifying an empty string
in the argument list.

This has the additional advantage that () is now simply considered a
single empty argument, which means that NASM should now properly
handle things like:

%define myreg() eax
	mov edx,myreg()

... similar to how the C preprocessor allows an empty macro argument
list which is distinct from a macro with no arguments whatsoever.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 08:39:02 -07:00