Commit Graph

332 Commits

Author SHA1 Message Date
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
H. Peter Anvin (Intel)
d66927a677 Diagnostics: make debug more dynamic, note -> info, add listmsg level
Make debug messages more dynamic by making it easy to conditionalize
the messages.

Change ERR_NOTE to ERR_INFO which reflects the usage better.  Other
compilers use note: for additional information.

Don't unwind the macro stack with ERR_HERE; it is only going to give
confusing results as it will unwind the wrong macro stack.

Add ERR_LISTMSG level which is *always* suppressed, but will still
appear in the list file.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 04:28:55 -07:00
H. Peter Anvin (Intel)
524918394d labels.c: don't use ERR_NOTE for additional information
ERR_NOTE just confuses things, especially in the case of a suppressed
warning.

The preprocessor doesn't use it for unwinding macros, either.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 03:55:29 -07:00
H. Peter Anvin (Intel)
b1e15f42fe Add implicitly sized versions of the K instructions
This allows the K instructions to be specified without a size suffix
as long as the operands are sized; this matches the way most other x86
instructions work. As this is not the syntax specified in the SDM,
don't use it for disassembly.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 02:44:46 -07:00
H. Peter Anvin (Intel)
1c21a53e4e preproc: fix parsing of single-line macro arguments, cleanups
The single-line macro argument parsing was completely broken as a
comma would not be recognized as an argument separator.

In the process of fixing this, make a fair bit of code cleanups.

Note: reverse tokens for smacro->expansion doesn't actually make any
sense anymore, might reconsider that.

This checkin also removes the distinction between "magic" and plain
smacros; the only difference is which specific expand method is being
invoked.

Finally, extend the allocating-string functions such that *all* the
allocating string functions support querying the length of the string
a posteori.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-09 02:34:21 -07:00
H. Peter Anvin (Intel)
875eb24b29 preproc.c: fix macro descent
We have to call expand_one_smacro() recursively, otherwise we will not
expand smacros which point to other smacros. We cannot simply do this
by looping after token pasting, because we need to make sure we don't
recursively expand the same smacro.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-07 17:14:35 -07:00
H. Peter Anvin
0d4d431a01 Merge empty reservations from TIMES; add counts in listings
For constructs like TIMES xx RESB yy merge the TIMES and RESB and feed
a single reservation to the backend; this can (obviously) be
dramatically faster.

Add byte count in listings for <incbin> and repeat count to <rept>; to
make them more reasonable in length shorten to <bin ...> and <rep ...>
respectively, and don't require leading zeroes in bin/rep/res count.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-08-07 00:59:24 -07:00
H. Peter Anvin (Intel)
77335213e3 assemble: shuffle a few assignments around
Shuffle around a few assignments which might help the compiler need to
spill less.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 23:22:48 -07:00
H. Peter Anvin (Intel)
41bb8a8114 Warn if trying to assemble obsolete instructions
Print a warning if one tries to assemble an obsolete instruction,
unless there is an exact match for the CPU directive.

For example:

	CPU 386
	POP CS		; Warning - obsolete instruction

	CPU 8086
	POP CS		; No warning

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 22:56:51 -07:00
H. Peter Anvin (Intel)
d13a6f9708 iflag.h: fix IF_CPU_LEVEL_MASK, add missing CPU definitions
Fix the definition of IF_CPU_LEVEL_MASK (which was missing the top
bit, IFM_ANY itself).

Add CPU definitions that we actually have into directiv.c.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 22:33:14 -07:00
H. Peter Anvin (Intel)
ca47c843ed warnings.pl: move comment
Move a comment to where it makes more sense.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 19:30:36 -07:00
H. Peter Anvin (Intel)
65c958d59f warnings.pl: warn on duplicate definition instead of broken output
Have warnings.pl give a warning(!) message if a warning definition is
found to be duplicated, including the location of both
definitions. Much better than silently creating bogus output.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 19:28:57 -07:00
H. Peter Anvin (Intel)
873ceee29f Replace nasm_error(ERR_WARNING|...) with nasm_warn()
Remove a few remaining instances of nasm_error(ERR_WARNING).

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-08-06 19:18:36 -07:00
H. Peter Anvin
959702baa8 asm/assemble.c: stylistic fix to bnd warning
Clean up the style for the bnd warning.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 20:56:50 -07:00
H. Peter Anvin
fdeb3b0d01 Add group aliases for all prefixed warnings.
For example, -w+float will now enable all warnings with names staring
with float-*.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 20:53:17 -07:00
H. Peter Anvin
db6960c3fa quote: improve comment
Explain why 0xfc + vb5 cannot overflow a byte value.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 17:32:44 -07:00
H. Peter Anvin
10d9589f02 quote: emit invalid UTF-8 rather than just dropping a strange value
If an UTF-8 value exceeds 0x7fffffff, there is no legitimate encoding
for it. However, using FE or FF as leading bytes provide at least some
kind of encoding. This is assembly, and the programmer is (almost?)
always right. It might be worthwhile to add a suppressible warning for
invalid UTF-8 strings in general, though, including any character >
0x10ffff, surrogates, or a string that is constructed by hand.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 17:28:51 -07:00
H. Peter Anvin
236f4a832b strfunc: better error messages if a string transform fails
Let the user know what string transform actually failed on them.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 17:17:16 -07:00
H. Peter Anvin
d4b20355d2 asm/quote.c: fix range cutoffs for UTF-8
The various UTF-8 byte cutoffs were off by a factor of 2. Fix.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 17:16:45 -07:00
H. Peter Anvin
4d7bf79ed7 quote.c: let nasm_skip_string() return NULL for a non-string
Returning NULL makes more sense than returning the initial pointer
(the only other sensible alternative would be to return a pointer the
final null character.)

This currently can't happen, as all callers to nasm_skip_string()
currently explicitly tests for an initial quote.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 16:29:52 -07:00
H. Peter Anvin
5282cea85b Merge branch 'master' of ssh://repo.or.cz/nasm
Resolved Conflicts:
	asm/preproc.c

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 16:26:22 -07:00
H. Peter Anvin
249c217070 quote: drop merging of adjacent strings; allow some control chars
The merging of adjacent ' or " strings really does nothing but
introduce gratuitous incompatiblities; drop it.

Allow *some* control characters (BEL BS TAB ESC) in
nasm_unquote_cstr().

The ` state machine can be greatly simplified by treating \0 as just
another character and let it terminate the string in appropriate
contexts, just like `. The only difference with ` is when it occurs
in state st_backslash: you can't escape the null character.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-06-06 16:21:01 -07:00
Chang S. Bae
fea22697e2 preproc: Fix the initial enum value in stdmac_ptr()
TOKEN_ID is from enum pp_token_type, but struct Type has enum
token_type. TOK_ID seems to be a matched one.s

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
2019-06-02 23:51:25 +03:00
H. Peter Anvin (Intel)
a7afe276da preproc: factor out getting a line of tokens and detokenizing it
Split the code for getting a line of tokens from the code that sets
verror and detokenizes the resulting string.

While we are at it, merge the handling of EOF and ^Z into the general
loop in read_line().

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-04-26 00:34:04 -07:00
H. Peter Anvin (Intel)
41e9682efe preproc: massive cleanup of smacro expansion
The smacro expansion code was virtually impossible to understand, and
was leading to very strange failures. Clean it up, and do much better
handling of magic macros.  This should also allow for recursive
macros, but recursive macros are extremely tricky in that it is very
hard to keep them from recursing forever, unless there is at least one
argument which is never expanded. They are not currently implemented.

Even so, I believe token pasting makes it possible to create infinite
loops; e.g.:

%define foo foo %+

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-04-25 18:00:32 -07:00
H. Peter Anvin (Intel)
9bb55bd127 Merge branch 'evalmacro'
Resolved Conflicts:
	asm/preproc.c
	output/elf.h
	output/outelf.c
	output/outelf.h
	version

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-04-24 11:14:43 -07:00
H. Peter Anvin
bb42d30737 quote: disallow control characters in C strings; concatendate; cleanups
In nasm_unquote_cstr(), disallow any control character, not just
NUL. This will matter when allowing quoting symbols.

Merge nasm_unquote() and nasm_unquote_cstr().

Strings can now be concatenated, C style: adjacent quoted strings
(including whitespace-separated) are merged into a single string.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-04-22 14:34:22 -07:00
Cyrill Gorcunov
982186a1a3 preproc: Fix nil dereference on error paths
https://bugzilla.nasm.us/show_bug.cgi?id=3392562

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2019-03-16 23:19:12 +03:00
H. Peter Anvin
8b26247442 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>
2019-02-26 14:00:54 -08:00
H. Peter Anvin (Intel)
1df7263ae9 warnings: add [warning push] and [warning pop]
Add [warning push] and [warning pop] directives.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-01-11 13:13:03 -08:00
H. Peter Anvin (Intel)
38ddb19977 Warnings: move zeroing reserved space to a separate warning class
Zeroing reserved space in a progbits section really should be a
separate warning class, so it can be controlled independently.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2019-01-11 12:27:02 -08:00
H. Peter Anvin
ef4f23d76a tokhash.pl: zero all the fields for a not-found token
Make sure we zero all the token fields if we don't find something in
the hash.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-12-29 20:14:50 -08:00