2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2007-08-31 06:35:34 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.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.
|
|
|
|
*/
|
|
|
|
static char **stdscan_tempstorage = NULL;
|
|
|
|
static int stdscan_tempsize = 0, stdscan_templen = 0;
|
|
|
|
#define STDSCAN_TEMP_DELTA 256
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *stdscan_copy(char *p, int len)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *stdscan_bufptr = NULL;
|
|
|
|
int stdscan(void *private_data, struct tokenval *tv)
|
|
|
|
{
|
|
|
|
char ourcopy[MAX_KEYWORD + 1], *r, *s;
|
|
|
|
|
|
|
|
(void)private_data; /* Don't warn that this parameter is unused */
|
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
while (nasm_isspace(*stdscan_bufptr))
|
2007-08-31 06:35:34 +08:00
|
|
|
stdscan_bufptr++;
|
|
|
|
if (!*stdscan_bufptr)
|
|
|
|
return tv->t_type = 0;
|
|
|
|
|
|
|
|
/* we have a token; either an id, a number or a char */
|
|
|
|
if (isidstart(*stdscan_bufptr) ||
|
|
|
|
(*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
|
|
|
|
/* now we've got an identifier */
|
2007-10-11 05:58:45 +08:00
|
|
|
bool is_sym = false;
|
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... */
|
|
|
|
while (isidchar(*stdscan_bufptr))
|
|
|
|
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 */
|
|
|
|
|
|
|
|
for (s = tv->t_charptr, r = ourcopy; *s; s++)
|
2008-06-12 06:49:41 +08:00
|
|
|
*r++ = nasm_tolower(*s);
|
2007-08-31 06:35:34 +08:00
|
|
|
*r = '\0';
|
|
|
|
/* right, so we have an identifier sitting in temp storage. now,
|
|
|
|
* is it actually a register or instruction name, or what? */
|
2007-09-19 13:54:40 +08:00
|
|
|
return nasm_token_hash(ourcopy, tv);
|
2007-08-31 06:35:34 +08:00
|
|
|
} else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
} else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
|
2007-10-11 15:05:31 +08:00
|
|
|
bool rn_error;
|
2007-10-20 04:10:46 +08:00
|
|
|
bool is_hex = false;
|
|
|
|
bool is_float = false;
|
2007-10-20 05:10:35 +08:00
|
|
|
bool has_e = false;
|
2007-10-20 04:10:46 +08:00
|
|
|
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
|
|
|
|
2007-10-23 07:53:48 +08:00
|
|
|
if (*stdscan_bufptr == '$') {
|
|
|
|
stdscan_bufptr++;
|
2007-10-20 04:10:46 +08:00
|
|
|
is_hex = true;
|
2007-10-23 07:53:48 +08:00
|
|
|
}
|
2007-10-20 04:10:46 +08:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = *stdscan_bufptr++;
|
|
|
|
|
2007-10-20 05:10:35 +08:00
|
|
|
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++;
|
|
|
|
}
|
2007-10-23 07:53:48 +08:00
|
|
|
} else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
|
|
|
|
is_hex = true;
|
2007-10-23 08:34:10 +08:00
|
|
|
} else if (c == 'P' || c == 'p') {
|
2007-10-20 04:10:46 +08:00
|
|
|
is_float = true;
|
|
|
|
if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
|
|
|
|
stdscan_bufptr++;
|
|
|
|
} else if (isnumchar(c) || c == '_')
|
|
|
|
; /* just advance */
|
|
|
|
else if (c == '.')
|
|
|
|
is_float = true;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stdscan_bufptr--; /* Point to first character beyond number */
|
|
|
|
|
2007-10-23 07:53:48 +08:00
|
|
|
if (has_e && !is_hex) {
|
2007-10-20 05:10:35 +08:00
|
|
|
/* 1e13 is floating-point, but 1e13h is not */
|
|
|
|
is_float = true;
|
|
|
|
}
|
|
|
|
|
2007-10-20 04:10:46 +08:00
|
|
|
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();
|
2008-06-02 12:34:49 +08:00
|
|
|
if (rn_error) {
|
|
|
|
/* some malformation occurred */
|
|
|
|
return tv->t_type = TOKEN_ERRNUM;
|
|
|
|
}
|
2007-10-20 04:10:46 +08:00
|
|
|
tv->t_charptr = NULL;
|
|
|
|
return tv->t_type = TOKEN_NUM;
|
|
|
|
}
|
2008-06-02 12:34:49 +08:00
|
|
|
} else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
|
|
|
|
*stdscan_bufptr == '`') {
|
2008-06-05 02:26:59 +08:00
|
|
|
/* 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)
|
2008-06-10 11:45:19 +08:00
|
|
|
return tv->t_type = TOKEN_ERRSTR;
|
2008-06-05 02:26:59 +08:00
|
|
|
stdscan_bufptr++; /* Skip final quote */
|
2008-06-10 11:45:19 +08:00
|
|
|
return tv->t_type = TOKEN_STR;
|
2008-06-02 12:34:49 +08:00
|
|
|
} else if (*stdscan_bufptr == ';') {
|
|
|
|
/* a comment has happened - stay */
|
2007-08-31 06:35:34 +08:00
|
|
|
return tv->t_type = 0;
|
|
|
|
} else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_SHR;
|
|
|
|
} else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
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] == '=') {
|
|
|
|
stdscan_bufptr += 2;
|
|
|
|
return tv->t_type = TOKEN_LE;
|
|
|
|
} 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++);
|
|
|
|
}
|