2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2009-10-30 16:29:43 +08:00
|
|
|
*
|
2018-06-16 09:37:32 +08:00
|
|
|
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2009-10-30 16:29:43 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2018-12-28 04:37:25 +08:00
|
|
|
#include "nctype.h"
|
2007-08-31 06:35:34 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2008-06-02 12:34:49 +08:00
|
|
|
#include "quote.h"
|
2007-08-31 06:35:34 +08:00
|
|
|
#include "stdscan.h"
|
|
|
|
#include "insns.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Standard scanner routine used by parser.c and some output
|
|
|
|
* formats. It keeps a succession of temporary-storage strings in
|
|
|
|
* stdscan_tempstorage, which can be cleared using stdscan_reset.
|
|
|
|
*/
|
2009-10-30 04:09:18 +08:00
|
|
|
static char *stdscan_bufptr = NULL;
|
2007-08-31 06:35:34 +08:00
|
|
|
static char **stdscan_tempstorage = NULL;
|
|
|
|
static int stdscan_tempsize = 0, stdscan_templen = 0;
|
|
|
|
#define STDSCAN_TEMP_DELTA 256
|
|
|
|
|
2009-10-30 04:09:18 +08:00
|
|
|
void stdscan_set(char *str)
|
|
|
|
{
|
2009-11-01 01:05:32 +08:00
|
|
|
stdscan_bufptr = str;
|
2009-10-30 04:09:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *stdscan_get(void)
|
|
|
|
{
|
2009-11-01 01:05:32 +08:00
|
|
|
return stdscan_bufptr;
|
2009-10-30 04:09:18 +08:00
|
|
|
}
|
|
|
|
|
2007-08-31 06:35:34 +08:00
|
|
|
static void stdscan_pop(void)
|
|
|
|
{
|
|
|
|
nasm_free(stdscan_tempstorage[--stdscan_templen]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stdscan_reset(void)
|
|
|
|
{
|
|
|
|
while (stdscan_templen > 0)
|
|
|
|
stdscan_pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unimportant cleanup is done to avoid confusing people who are trying
|
|
|
|
* to debug real memory leaks
|
|
|
|
*/
|
|
|
|
void stdscan_cleanup(void)
|
|
|
|
{
|
|
|
|
stdscan_reset();
|
|
|
|
nasm_free(stdscan_tempstorage);
|
|
|
|
}
|
|
|
|
|
2019-09-24 07:40:03 +08:00
|
|
|
static char *stdscan_copy(const char *p, int len)
|
2007-08-31 06:35:34 +08:00
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
text = nasm_malloc(len + 1);
|
2008-06-05 02:26:59 +08:00
|
|
|
memcpy(text, p, len);
|
2007-08-31 06:35:34 +08:00
|
|
|
text[len] = '\0';
|
|
|
|
|
|
|
|
if (stdscan_templen >= stdscan_tempsize) {
|
|
|
|
stdscan_tempsize += STDSCAN_TEMP_DELTA;
|
|
|
|
stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
|
|
|
|
stdscan_tempsize *
|
|
|
|
sizeof(char *));
|
|
|
|
}
|
|
|
|
stdscan_tempstorage[stdscan_templen++] = text;
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2013-08-06 11:46:18 +08:00
|
|
|
/*
|
|
|
|
* a token is enclosed with braces. proper token type will be assigned
|
|
|
|
* accordingly with the token flag.
|
|
|
|
*/
|
|
|
|
static int stdscan_handle_brace(struct tokenval *tv)
|
|
|
|
{
|
|
|
|
if (!(tv->t_flag & TFLAG_BRC_ANY)) {
|
|
|
|
/* invalid token is put inside braces */
|
2018-12-02 02:02:51 +08:00
|
|
|
nasm_nonfatal("`%s' is not a valid decorator with braces", tv->t_charptr);
|
2013-08-06 11:46:18 +08:00
|
|
|
tv->t_type = TOKEN_INVALID;
|
|
|
|
} else if (tv->t_flag & TFLAG_BRC_OPT) {
|
|
|
|
if (is_reg_class(OPMASKREG, tv->t_integer)) {
|
|
|
|
/* within braces, opmask register is now used as a mask */
|
|
|
|
tv->t_type = TOKEN_OPMASK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tv->t_type;
|
|
|
|
}
|
|
|
|
|
2007-08-31 06:35:34 +08:00
|
|
|
int stdscan(void *private_data, struct tokenval *tv)
|
|
|
|
{
|
2019-09-24 07:40:03 +08:00
|
|
|
const char *r;
|
2007-08-31 06:35:34 +08:00
|
|
|
|
|
|
|
(void)private_data; /* Don't warn that this parameter is unused */
|
|
|
|
|
2019-10-17 05:29:16 +08:00
|
|
|
nasm_zero(*tv);
|
|
|
|
|
2009-10-30 16:29:43 +08:00
|
|
|
stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
|
2013-11-28 06:10:40 +08:00
|
|
|
if (!*stdscan_bufptr)
|
2009-10-30 16:29:43 +08:00
|
|
|
return tv->t_type = TOKEN_EOS;
|
2007-08-31 06:35:34 +08:00
|
|
|
|
|
|
|
/* we have a token; either an id, a number or a char */
|
2018-11-29 06:55:58 +08:00
|
|
|
if (nasm_isidstart(*stdscan_bufptr) ||
|
|
|
|
(*stdscan_bufptr == '$' && nasm_isidstart(stdscan_bufptr[1]))) {
|
2007-08-31 06:35:34 +08:00
|
|
|
/* now we've got an identifier */
|
2007-10-11 05:58:45 +08:00
|
|
|
bool is_sym = false;
|
2013-08-06 11:46:18 +08:00
|
|
|
int token_type;
|
|
|
|
|
2007-08-31 06:35:34 +08:00
|
|
|
if (*stdscan_bufptr == '$') {
|
2007-10-11 05:58:45 +08:00
|
|
|
is_sym = true;
|
2007-08-31 06:35:34 +08:00
|
|
|
stdscan_bufptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = stdscan_bufptr++;
|
|
|
|
/* read the entire buffer to advance the buffer pointer but... */
|
2018-11-29 06:55:58 +08:00
|
|
|
while (nasm_isidchar(*stdscan_bufptr))
|
2007-08-31 06:35:34 +08:00
|
|
|
stdscan_bufptr++;
|
|
|
|
|
|
|
|
/* ... copy only up to IDLEN_MAX-1 characters */
|
|
|
|
tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
|
|
|
|
stdscan_bufptr - r : IDLEN_MAX - 1);
|
|
|
|
|
|
|
|
if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
|
|
|
|
return tv->t_type = TOKEN_ID; /* bypass all other checks */
|
|
|
|
|
2019-09-24 07:40:03 +08:00
|
|
|
token_type = nasm_token_hash(tv->t_charptr, tv);
|
2018-12-02 02:02:51 +08:00
|
|
|
if (unlikely(tv->t_flag & TFLAG_WARN)) {
|
2018-12-14 13:53:31 +08:00
|
|
|
/*!
|
|
|
|
*!ptr [on] non-NASM keyword used in other assemblers
|
|
|
|
*! warns about keywords used in other assemblers that might
|
|
|
|
*! indicate a mistake in the source code. Currently only the MASM
|
2020-06-28 13:30:50 +08:00
|
|
|
*! \c{PTR} keyword is recognized. See also \k{pkg_masm}.
|
2018-12-14 13:53:31 +08:00
|
|
|
*/
|
2018-12-19 04:25:11 +08:00
|
|
|
nasm_warn(WARN_PTR, "`%s' is not a NASM keyword",
|
2018-12-02 02:02:51 +08:00
|
|
|
tv->t_charptr);
|
|
|
|
}
|
2016-05-10 03:05:56 +08:00
|
|
|
|
2013-11-28 06:10:40 +08:00
|
|
|
if (likely(!(tv->t_flag & TFLAG_BRC))) {
|
|
|
|
/* most of the tokens fall into this case */
|
|
|
|
return token_type;
|
2013-08-06 11:46:18 +08:00
|
|
|
} else {
|
2013-11-28 06:10:40 +08:00
|
|
|
return tv->t_type = TOKEN_ID;
|
2013-08-06 11:46:18 +08:00
|
|
|
}
|
2018-11-29 06:55:58 +08:00
|
|
|
} else if (*stdscan_bufptr == '$' && !nasm_isnumchar(stdscan_bufptr[1])) {
|
2007-08-31 06:35:34 +08:00
|
|
|
/*
|
|
|
|
* It's a $ sign with no following hex number; this must
|
|
|
|
* mean it's a Here token ($), evaluating to the current
|
|
|
|
* assembly location, or a Base token ($$), evaluating to
|
|
|
|
* the base of the current segment.
|
|
|
|
*/
|
|
|
|
stdscan_bufptr++;
|
|
|
|
if (*stdscan_bufptr == '$') {
|
|
|
|
stdscan_bufptr++;
|
|
|
|
return tv->t_type = TOKEN_BASE;
|
|
|
|
}
|
|
|
|
return tv->t_type = TOKEN_HERE;
|
2018-11-29 06:55:58 +08:00
|
|
|
} else if (nasm_isnumstart(*stdscan_bufptr)) { /* now we've got a number */
|
2007-10-11 15:05:31 +08:00
|
|
|
bool rn_error;
|
2009-11-01 01:05:32 +08:00
|
|
|
bool is_hex = false;
|
|
|
|
bool is_float = false;
|
|
|
|
bool has_e = false;
|
|
|
|
char c;
|
2007-08-31 06:35:34 +08:00
|
|
|
|
2007-10-23 07:53:48 +08:00
|
|
|
r = stdscan_bufptr;
|
2007-08-31 06:35:34 +08:00
|
|
|
|
2009-11-01 01:05:32 +08:00
|
|
|
if (*stdscan_bufptr == '$') {
|
|
|
|
stdscan_bufptr++;
|
|
|
|
is_hex = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = *stdscan_bufptr++;
|
|
|
|
|
|
|
|
if (!is_hex && (c == 'e' || c == 'E')) {
|
|
|
|
has_e = true;
|
|
|
|
if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
|
|
|
|
/*
|
|
|
|
* e can only be followed by +/- if it is either a
|
|
|
|
* prefixed hex number or a floating-point number
|
|
|
|
*/
|
|
|
|
is_float = true;
|
|
|
|
stdscan_bufptr++;
|
|
|
|
}
|
|
|
|
} else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
|
|
|
|
is_hex = true;
|
|
|
|
} else if (c == 'P' || c == 'p') {
|
|
|
|
is_float = true;
|
|
|
|
if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
|
|
|
|
stdscan_bufptr++;
|
2018-11-29 06:55:58 +08:00
|
|
|
} else if (nasm_isnumchar(c))
|
2009-11-01 01:05:32 +08:00
|
|
|
; /* just advance */
|
|
|
|
else if (c == '.')
|
|
|
|
is_float = true;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stdscan_bufptr--; /* Point to first character beyond number */
|
|
|
|
|
|
|
|
if (has_e && !is_hex) {
|
|
|
|
/* 1e13 is floating-point, but 1e13h is not */
|
|
|
|
is_float = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_float) {
|
|
|
|
tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
|
|
|
|
return tv->t_type = TOKEN_FLOAT;
|
|
|
|
} else {
|
|
|
|
r = stdscan_copy(r, stdscan_bufptr - r);
|
|
|
|
tv->t_integer = readnum(r, &rn_error);
|
|
|
|
stdscan_pop();
|
|
|
|
if (rn_error) {
|
|
|
|
/* some malformation occurred */
|
|
|
|
return tv->t_type = TOKEN_ERRNUM;
|
|
|
|
}
|
|
|
|
tv->t_charptr = NULL;
|
|
|
|
return tv->t_type = TOKEN_NUM;
|
|
|
|
}
|
2008-06-02 12:34:49 +08:00
|
|
|
} else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
|
2009-11-01 01:05:32 +08:00
|
|
|
*stdscan_bufptr == '`') {
|
|
|
|
/* a quoted string */
|
|
|
|
char start_quote = *stdscan_bufptr;
|
|
|
|
tv->t_charptr = stdscan_bufptr;
|
|
|
|
tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
|
|
|
|
if (*stdscan_bufptr != start_quote)
|
|
|
|
return tv->t_type = TOKEN_ERRSTR;
|
|
|
|
stdscan_bufptr++; /* Skip final quote */
|
2008-06-10 11:45:19 +08:00
|
|
|
return tv->t_type = TOKEN_STR;
|
2013-11-28 06:10:40 +08:00
|
|
|
} else if (*stdscan_bufptr == '{') {
|
|
|
|
/* now we've got a decorator */
|
|
|
|
int token_len;
|
|
|
|
|
|
|
|
stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
|
|
|
|
|
|
|
|
r = ++stdscan_bufptr;
|
|
|
|
/*
|
|
|
|
* read the entire buffer to advance the buffer pointer
|
|
|
|
* {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
|
|
|
|
*/
|
2018-11-29 06:55:58 +08:00
|
|
|
while (nasm_isbrcchar(*stdscan_bufptr))
|
2013-11-28 06:10:40 +08:00
|
|
|
stdscan_bufptr++;
|
|
|
|
|
|
|
|
token_len = stdscan_bufptr - r;
|
|
|
|
|
|
|
|
/* ... copy only up to DECOLEN_MAX-1 characters */
|
|
|
|
tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
|
|
|
|
token_len : DECOLEN_MAX - 1);
|
|
|
|
|
|
|
|
stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
|
|
|
|
/* if brace is not closed properly or token is too long */
|
|
|
|
if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
|
2018-12-02 02:02:51 +08:00
|
|
|
nasm_nonfatal("invalid decorator token inside braces");
|
2013-11-28 06:10:40 +08:00
|
|
|
return tv->t_type = TOKEN_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdscan_bufptr++; /* skip closing brace */
|
|
|
|
|
|
|
|
/* handle tokens inside braces */
|
2019-09-24 07:40:03 +08:00
|
|
|
nasm_token_hash(tv->t_charptr, tv);
|
2013-11-28 06:10:40 +08:00
|
|
|
return stdscan_handle_brace(tv);
|
2008-06-02 12:34:49 +08:00
|
|
|
} else if (*stdscan_bufptr == ';') {
|
|
|
|
/* a comment has happened - stay */
|
2009-10-30 16:29:43 +08:00
|
|
|
return tv->t_type = TOKEN_EOS;
|
2007-08-31 06:35:34 +08:00
|
|
|
} else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
|
2018-06-16 09:37:32 +08:00
|
|
|
if (stdscan_bufptr[2] == '>') {
|
|
|
|
stdscan_bufptr += 3;
|
|
|
|
return tv->t_type = TOKEN_SAR;
|
|
|
|
} else {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_SHR;
|
|
|
|
}
|
2007-08-31 06:35:34 +08:00
|
|
|
} else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
|
2018-06-16 09:37:32 +08:00
|
|
|
stdscan_bufptr += stdscan_bufptr[2] == '<' ? 3 : 2;
|
2007-08-31 06:35:34 +08:00
|
|
|
return tv->t_type = TOKEN_SHL;
|
|
|
|
} else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_SDIV;
|
|
|
|
} else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_SMOD;
|
|
|
|
} else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_EQ;
|
|
|
|
} else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_NE;
|
|
|
|
} else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_NE;
|
|
|
|
} else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
|
2019-09-24 07:40:03 +08:00
|
|
|
if (stdscan_bufptr[2] == '>') {
|
|
|
|
stdscan_bufptr += 3;
|
|
|
|
return tv->t_type = TOKEN_LEG;
|
|
|
|
} else {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_LE;
|
|
|
|
}
|
2007-08-31 06:35:34 +08:00
|
|
|
} else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_GE;
|
|
|
|
} else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_DBL_AND;
|
|
|
|
} else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_DBL_XOR;
|
|
|
|
} else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_DBL_OR;
|
|
|
|
} else /* just an ordinary char */
|
|
|
|
return tv->t_type = (uint8_t)(*stdscan_bufptr++);
|
|
|
|
}
|