blob: 0f7b3bd730465232245b626156b3add60565a159 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvinfe501952007-10-02 21:53:51 -070034#include "compiler.h"
35
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40#include <inttypes.h>
41
42#include "nasm.h"
43#include "nasmlib.h"
H. Peter Anvin6ecc1592008-06-01 21:34:49 -070044#include "quote.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000045#include "stdscan.h"
46#include "insns.h"
47
48/*
49 * Standard scanner routine used by parser.c and some output
50 * formats. It keeps a succession of temporary-storage strings in
51 * stdscan_tempstorage, which can be cleared using stdscan_reset.
52 */
53static char **stdscan_tempstorage = NULL;
54static int stdscan_tempsize = 0, stdscan_templen = 0;
55#define STDSCAN_TEMP_DELTA 256
56
57static void stdscan_pop(void)
58{
59 nasm_free(stdscan_tempstorage[--stdscan_templen]);
60}
61
62void stdscan_reset(void)
63{
64 while (stdscan_templen > 0)
65 stdscan_pop();
66}
67
68/*
69 * Unimportant cleanup is done to avoid confusing people who are trying
70 * to debug real memory leaks
71 */
72void stdscan_cleanup(void)
73{
74 stdscan_reset();
75 nasm_free(stdscan_tempstorage);
76}
77
78static char *stdscan_copy(char *p, int len)
79{
80 char *text;
81
82 text = nasm_malloc(len + 1);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -070083 memcpy(text, p, len);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000084 text[len] = '\0';
85
86 if (stdscan_templen >= stdscan_tempsize) {
87 stdscan_tempsize += STDSCAN_TEMP_DELTA;
88 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
89 stdscan_tempsize *
90 sizeof(char *));
91 }
92 stdscan_tempstorage[stdscan_templen++] = text;
93
94 return text;
95}
96
97char *stdscan_bufptr = NULL;
98int stdscan(void *private_data, struct tokenval *tv)
99{
100 char ourcopy[MAX_KEYWORD + 1], *r, *s;
101
102 (void)private_data; /* Don't warn that this parameter is unused */
103
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700104 while (nasm_isspace(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000105 stdscan_bufptr++;
106 if (!*stdscan_bufptr)
107 return tv->t_type = 0;
108
109 /* we have a token; either an id, a number or a char */
110 if (isidstart(*stdscan_bufptr) ||
111 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
112 /* now we've got an identifier */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700113 bool is_sym = false;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000114
115 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700116 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000117 stdscan_bufptr++;
118 }
119
120 r = stdscan_bufptr++;
121 /* read the entire buffer to advance the buffer pointer but... */
122 while (isidchar(*stdscan_bufptr))
123 stdscan_bufptr++;
124
125 /* ... copy only up to IDLEN_MAX-1 characters */
126 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
127 stdscan_bufptr - r : IDLEN_MAX - 1);
128
129 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
130 return tv->t_type = TOKEN_ID; /* bypass all other checks */
131
132 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700133 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000134 *r = '\0';
135 /* right, so we have an identifier sitting in temp storage. now,
136 * is it actually a register or instruction name, or what? */
H. Peter Anvinbf9a24f2007-09-18 22:54:40 -0700137 return nasm_token_hash(ourcopy, tv);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000138 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
139 /*
140 * It's a $ sign with no following hex number; this must
141 * mean it's a Here token ($), evaluating to the current
142 * assembly location, or a Base token ($$), evaluating to
143 * the base of the current segment.
144 */
145 stdscan_bufptr++;
146 if (*stdscan_bufptr == '$') {
147 stdscan_bufptr++;
148 return tv->t_type = TOKEN_BASE;
149 }
150 return tv->t_type = TOKEN_HERE;
151 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700152 bool rn_error;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700153 bool is_hex = false;
154 bool is_float = false;
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700155 bool has_e = false;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700156 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000157
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700158 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000159
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700160 if (*stdscan_bufptr == '$') {
161 stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700162 is_hex = true;
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700163 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700164
165 for (;;) {
166 c = *stdscan_bufptr++;
167
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700168 if (!is_hex && (c == 'e' || c == 'E')) {
169 has_e = true;
170 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
171 /* e can only be followed by +/- if it is either a
172 prefixed hex number or a floating-point number */
173 is_float = true;
174 stdscan_bufptr++;
175 }
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700176 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
177 is_hex = true;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700178 } else if (c == 'P' || c == 'p') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700179 is_float = true;
180 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
181 stdscan_bufptr++;
182 } else if (isnumchar(c) || c == '_')
183 ; /* just advance */
184 else if (c == '.')
185 is_float = true;
186 else
187 break;
188 }
189 stdscan_bufptr--; /* Point to first character beyond number */
190
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700191 if (has_e && !is_hex) {
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700192 /* 1e13 is floating-point, but 1e13h is not */
193 is_float = true;
194 }
195
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700196 if (is_float) {
197 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
198 return tv->t_type = TOKEN_FLOAT;
199 } else {
200 r = stdscan_copy(r, stdscan_bufptr - r);
201 tv->t_integer = readnum(r, &rn_error);
202 stdscan_pop();
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700203 if (rn_error) {
204 /* some malformation occurred */
205 return tv->t_type = TOKEN_ERRNUM;
206 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700207 tv->t_charptr = NULL;
208 return tv->t_type = TOKEN_NUM;
209 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700210 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
211 *stdscan_bufptr == '`') {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700212 /* a quoted string */
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700213 char start_quote = *stdscan_bufptr;
214 tv->t_charptr = stdscan_bufptr;
215 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
216 if (*stdscan_bufptr != start_quote)
H. Peter Anvin11627042008-06-09 20:45:19 -0700217 return tv->t_type = TOKEN_ERRSTR;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700218 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700219 return tv->t_type = TOKEN_STR;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700220 } else if (*stdscan_bufptr == ';') {
221 /* a comment has happened - stay */
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000222 return tv->t_type = 0;
223 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
224 stdscan_bufptr += 2;
225 return tv->t_type = TOKEN_SHR;
226 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
227 stdscan_bufptr += 2;
228 return tv->t_type = TOKEN_SHL;
229 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
230 stdscan_bufptr += 2;
231 return tv->t_type = TOKEN_SDIV;
232 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
233 stdscan_bufptr += 2;
234 return tv->t_type = TOKEN_SMOD;
235 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
236 stdscan_bufptr += 2;
237 return tv->t_type = TOKEN_EQ;
238 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
239 stdscan_bufptr += 2;
240 return tv->t_type = TOKEN_NE;
241 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
242 stdscan_bufptr += 2;
243 return tv->t_type = TOKEN_NE;
244 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
245 stdscan_bufptr += 2;
246 return tv->t_type = TOKEN_LE;
247 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
248 stdscan_bufptr += 2;
249 return tv->t_type = TOKEN_GE;
250 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
251 stdscan_bufptr += 2;
252 return tv->t_type = TOKEN_DBL_AND;
253 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
254 stdscan_bufptr += 2;
255 return tv->t_type = TOKEN_DBL_XOR;
256 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
257 stdscan_bufptr += 2;
258 return tv->t_type = TOKEN_DBL_OR;
259 } else /* just an ordinary char */
260 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
261}