blob: ea7537dd9b46aaa4f47840a48f953ae8c945b560 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +03002 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07003 * 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.
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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 */
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030053static char *stdscan_bufptr = NULL;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054static char **stdscan_tempstorage = NULL;
55static int stdscan_tempsize = 0, stdscan_templen = 0;
56#define STDSCAN_TEMP_DELTA 256
57
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030058void stdscan_set(char *str)
59{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030060 stdscan_bufptr = str;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030061}
62
63char *stdscan_get(void)
64{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030065 return stdscan_bufptr;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030066}
67
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000068static void stdscan_pop(void)
69{
70 nasm_free(stdscan_tempstorage[--stdscan_templen]);
71}
72
73void stdscan_reset(void)
74{
75 while (stdscan_templen > 0)
76 stdscan_pop();
77}
78
79/*
80 * Unimportant cleanup is done to avoid confusing people who are trying
81 * to debug real memory leaks
82 */
83void stdscan_cleanup(void)
84{
85 stdscan_reset();
86 nasm_free(stdscan_tempstorage);
87}
88
89static char *stdscan_copy(char *p, int len)
90{
91 char *text;
92
93 text = nasm_malloc(len + 1);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -070094 memcpy(text, p, len);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000095 text[len] = '\0';
96
97 if (stdscan_templen >= stdscan_tempsize) {
98 stdscan_tempsize += STDSCAN_TEMP_DELTA;
99 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
100 stdscan_tempsize *
101 sizeof(char *));
102 }
103 stdscan_tempstorage[stdscan_templen++] = text;
104
105 return text;
106}
107
Jin Kyu Song72018a22013-08-05 20:46:18 -0700108/*
109 * a token is enclosed with braces. proper token type will be assigned
110 * accordingly with the token flag.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700111 */
112static int stdscan_handle_brace(struct tokenval *tv)
113{
114 if (!(tv->t_flag & TFLAG_BRC_ANY)) {
115 /* invalid token is put inside braces */
116 nasm_error(ERR_NONFATAL,
117 "%s is not a valid decorator with braces", tv->t_charptr);
118 tv->t_type = TOKEN_INVALID;
119 } else if (tv->t_flag & TFLAG_BRC_OPT) {
120 if (is_reg_class(OPMASKREG, tv->t_integer)) {
121 /* within braces, opmask register is now used as a mask */
122 tv->t_type = TOKEN_OPMASK;
123 }
124 }
125
Jin Kyu Song72018a22013-08-05 20:46:18 -0700126 return tv->t_type;
127}
128
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000129int stdscan(void *private_data, struct tokenval *tv)
130{
131 char ourcopy[MAX_KEYWORD + 1], *r, *s;
132
133 (void)private_data; /* Don't warn that this parameter is unused */
134
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300135 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
Jin Kyu Song487f3522013-11-27 14:10:40 -0800136 if (!*stdscan_bufptr)
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300137 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000138
139 /* we have a token; either an id, a number or a char */
140 if (isidstart(*stdscan_bufptr) ||
Jin Kyu Song487f3522013-11-27 14:10:40 -0800141 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000142 /* now we've got an identifier */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700143 bool is_sym = false;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700144 int token_type;
145
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000146 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700147 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000148 stdscan_bufptr++;
149 }
150
151 r = stdscan_bufptr++;
152 /* read the entire buffer to advance the buffer pointer but... */
Jin Kyu Song487f3522013-11-27 14:10:40 -0800153 while (isidchar(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000154 stdscan_bufptr++;
155
156 /* ... copy only up to IDLEN_MAX-1 characters */
157 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
158 stdscan_bufptr - r : IDLEN_MAX - 1);
159
160 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
161 return tv->t_type = TOKEN_ID; /* bypass all other checks */
162
163 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700164 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000165 *r = '\0';
166 /* right, so we have an identifier sitting in temp storage. now,
167 * is it actually a register or instruction name, or what? */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700168 token_type = nasm_token_hash(ourcopy, tv);
169
Jin Kyu Song487f3522013-11-27 14:10:40 -0800170 if (likely(!(tv->t_flag & TFLAG_BRC))) {
171 /* most of the tokens fall into this case */
172 return token_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700173 } else {
Jin Kyu Song487f3522013-11-27 14:10:40 -0800174 return tv->t_type = TOKEN_ID;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700175 }
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000176 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
177 /*
178 * It's a $ sign with no following hex number; this must
179 * mean it's a Here token ($), evaluating to the current
180 * assembly location, or a Base token ($$), evaluating to
181 * the base of the current segment.
182 */
183 stdscan_bufptr++;
184 if (*stdscan_bufptr == '$') {
185 stdscan_bufptr++;
186 return tv->t_type = TOKEN_BASE;
187 }
188 return tv->t_type = TOKEN_HERE;
189 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700190 bool rn_error;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300191 bool is_hex = false;
192 bool is_float = false;
193 bool has_e = false;
194 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000195
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700196 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000197
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300198 if (*stdscan_bufptr == '$') {
199 stdscan_bufptr++;
200 is_hex = true;
201 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700202
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300203 for (;;) {
204 c = *stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700205
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300206 if (!is_hex && (c == 'e' || c == 'E')) {
207 has_e = true;
208 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
209 /*
210 * e can only be followed by +/- if it is either a
211 * prefixed hex number or a floating-point number
212 */
213 is_float = true;
214 stdscan_bufptr++;
215 }
216 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
217 is_hex = true;
218 } else if (c == 'P' || c == 'p') {
219 is_float = true;
220 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
221 stdscan_bufptr++;
222 } else if (isnumchar(c) || c == '_')
223 ; /* just advance */
224 else if (c == '.')
225 is_float = true;
226 else
227 break;
228 }
229 stdscan_bufptr--; /* Point to first character beyond number */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700230
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300231 if (has_e && !is_hex) {
232 /* 1e13 is floating-point, but 1e13h is not */
233 is_float = true;
234 }
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700235
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300236 if (is_float) {
237 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
238 return tv->t_type = TOKEN_FLOAT;
239 } else {
240 r = stdscan_copy(r, stdscan_bufptr - r);
241 tv->t_integer = readnum(r, &rn_error);
242 stdscan_pop();
243 if (rn_error) {
244 /* some malformation occurred */
245 return tv->t_type = TOKEN_ERRNUM;
246 }
247 tv->t_charptr = NULL;
248 return tv->t_type = TOKEN_NUM;
249 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700250 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300251 *stdscan_bufptr == '`') {
252 /* a quoted string */
253 char start_quote = *stdscan_bufptr;
254 tv->t_charptr = stdscan_bufptr;
255 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
256 if (*stdscan_bufptr != start_quote)
257 return tv->t_type = TOKEN_ERRSTR;
258 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700259 return tv->t_type = TOKEN_STR;
Jin Kyu Song487f3522013-11-27 14:10:40 -0800260 } else if (*stdscan_bufptr == '{') {
261 /* now we've got a decorator */
262 int token_len;
263
264 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
265
266 r = ++stdscan_bufptr;
267 /*
268 * read the entire buffer to advance the buffer pointer
269 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
270 */
271 while (isbrcchar(*stdscan_bufptr))
272 stdscan_bufptr++;
273
274 token_len = stdscan_bufptr - r;
275
276 /* ... copy only up to DECOLEN_MAX-1 characters */
277 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
278 token_len : DECOLEN_MAX - 1);
279
280 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
281 /* if brace is not closed properly or token is too long */
282 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
283 nasm_error(ERR_NONFATAL,
284 "invalid decorator token inside braces");
285 return tv->t_type = TOKEN_INVALID;
286 }
287
288 stdscan_bufptr++; /* skip closing brace */
289
290 for (s = tv->t_charptr, r = ourcopy; *s; s++)
291 *r++ = nasm_tolower(*s);
292 *r = '\0';
293
294 /* right, so we have a decorator sitting in temp storage. */
295 nasm_token_hash(ourcopy, tv);
296
297 /* handle tokens inside braces */
298 return stdscan_handle_brace(tv);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700299 } else if (*stdscan_bufptr == ';') {
300 /* a comment has happened - stay */
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300301 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000302 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
303 stdscan_bufptr += 2;
304 return tv->t_type = TOKEN_SHR;
305 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
306 stdscan_bufptr += 2;
307 return tv->t_type = TOKEN_SHL;
308 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
309 stdscan_bufptr += 2;
310 return tv->t_type = TOKEN_SDIV;
311 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
312 stdscan_bufptr += 2;
313 return tv->t_type = TOKEN_SMOD;
314 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
315 stdscan_bufptr += 2;
316 return tv->t_type = TOKEN_EQ;
317 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
318 stdscan_bufptr += 2;
319 return tv->t_type = TOKEN_NE;
320 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
321 stdscan_bufptr += 2;
322 return tv->t_type = TOKEN_NE;
323 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
324 stdscan_bufptr += 2;
325 return tv->t_type = TOKEN_LE;
326 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
327 stdscan_bufptr += 2;
328 return tv->t_type = TOKEN_GE;
329 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
330 stdscan_bufptr += 2;
331 return tv->t_type = TOKEN_DBL_AND;
332 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
333 stdscan_bufptr += 2;
334 return tv->t_type = TOKEN_DBL_XOR;
335 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
336 stdscan_bufptr += 2;
337 return tv->t_type = TOKEN_DBL_OR;
338 } else /* just an ordinary char */
339 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
340}