blob: 3ac6113e4bd780c9d0726558051c17c0eb84c3b3 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +03002 *
H. Peter Anvin69550ea2016-05-09 12:05:56 -07003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * 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>
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin6ecc1592008-06-01 21:34:49 -070043#include "quote.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000044#include "stdscan.h"
45#include "insns.h"
46
47/*
48 * Standard scanner routine used by parser.c and some output
49 * formats. It keeps a succession of temporary-storage strings in
50 * stdscan_tempstorage, which can be cleared using stdscan_reset.
51 */
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030052static char *stdscan_bufptr = NULL;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053static char **stdscan_tempstorage = NULL;
54static int stdscan_tempsize = 0, stdscan_templen = 0;
55#define STDSCAN_TEMP_DELTA 256
56
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030057void stdscan_set(char *str)
58{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030059 stdscan_bufptr = str;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030060}
61
62char *stdscan_get(void)
63{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030064 return stdscan_bufptr;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030065}
66
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000067static void stdscan_pop(void)
68{
69 nasm_free(stdscan_tempstorage[--stdscan_templen]);
70}
71
72void stdscan_reset(void)
73{
74 while (stdscan_templen > 0)
75 stdscan_pop();
76}
77
78/*
79 * Unimportant cleanup is done to avoid confusing people who are trying
80 * to debug real memory leaks
81 */
82void stdscan_cleanup(void)
83{
84 stdscan_reset();
85 nasm_free(stdscan_tempstorage);
86}
87
88static char *stdscan_copy(char *p, int len)
89{
90 char *text;
91
92 text = nasm_malloc(len + 1);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -070093 memcpy(text, p, len);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000094 text[len] = '\0';
95
96 if (stdscan_templen >= stdscan_tempsize) {
97 stdscan_tempsize += STDSCAN_TEMP_DELTA;
98 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
99 stdscan_tempsize *
100 sizeof(char *));
101 }
102 stdscan_tempstorage[stdscan_templen++] = text;
103
104 return text;
105}
106
Jin Kyu Song72018a22013-08-05 20:46:18 -0700107/*
108 * a token is enclosed with braces. proper token type will be assigned
109 * accordingly with the token flag.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700110 */
111static int stdscan_handle_brace(struct tokenval *tv)
112{
113 if (!(tv->t_flag & TFLAG_BRC_ANY)) {
114 /* invalid token is put inside braces */
115 nasm_error(ERR_NONFATAL,
H. Peter Anvin02f49f22016-05-10 03:08:09 -0700116 "`%s' is not a valid decorator with braces", tv->t_charptr);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700117 tv->t_type = TOKEN_INVALID;
118 } else if (tv->t_flag & TFLAG_BRC_OPT) {
119 if (is_reg_class(OPMASKREG, tv->t_integer)) {
120 /* within braces, opmask register is now used as a mask */
121 tv->t_type = TOKEN_OPMASK;
122 }
123 }
124
Jin Kyu Song72018a22013-08-05 20:46:18 -0700125 return tv->t_type;
126}
127
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000128int stdscan(void *private_data, struct tokenval *tv)
129{
130 char ourcopy[MAX_KEYWORD + 1], *r, *s;
131
132 (void)private_data; /* Don't warn that this parameter is unused */
133
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300134 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
Jin Kyu Song487f3522013-11-27 14:10:40 -0800135 if (!*stdscan_bufptr)
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300136 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000137
138 /* we have a token; either an id, a number or a char */
139 if (isidstart(*stdscan_bufptr) ||
Jin Kyu Song487f3522013-11-27 14:10:40 -0800140 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000141 /* now we've got an identifier */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700142 bool is_sym = false;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700143 int token_type;
144
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000145 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700146 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000147 stdscan_bufptr++;
148 }
149
150 r = stdscan_bufptr++;
151 /* read the entire buffer to advance the buffer pointer but... */
Jin Kyu Song487f3522013-11-27 14:10:40 -0800152 while (isidchar(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000153 stdscan_bufptr++;
154
155 /* ... copy only up to IDLEN_MAX-1 characters */
156 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
157 stdscan_bufptr - r : IDLEN_MAX - 1);
158
159 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
160 return tv->t_type = TOKEN_ID; /* bypass all other checks */
161
162 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700163 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000164 *r = '\0';
165 /* right, so we have an identifier sitting in temp storage. now,
166 * is it actually a register or instruction name, or what? */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700167 token_type = nasm_token_hash(ourcopy, tv);
168
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700169 if (unlikely(tv->t_flag & TFLAG_WARN)) {
170 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_PTR,
H. Peter Anvin02f49f22016-05-10 03:08:09 -0700171 "`%s' is not a NASM keyword", tv->t_charptr);
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700172 }
173
Jin Kyu Song487f3522013-11-27 14:10:40 -0800174 if (likely(!(tv->t_flag & TFLAG_BRC))) {
175 /* most of the tokens fall into this case */
176 return token_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700177 } else {
Jin Kyu Song487f3522013-11-27 14:10:40 -0800178 return tv->t_type = TOKEN_ID;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700179 }
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000180 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
181 /*
182 * It's a $ sign with no following hex number; this must
183 * mean it's a Here token ($), evaluating to the current
184 * assembly location, or a Base token ($$), evaluating to
185 * the base of the current segment.
186 */
187 stdscan_bufptr++;
188 if (*stdscan_bufptr == '$') {
189 stdscan_bufptr++;
190 return tv->t_type = TOKEN_BASE;
191 }
192 return tv->t_type = TOKEN_HERE;
193 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700194 bool rn_error;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300195 bool is_hex = false;
196 bool is_float = false;
197 bool has_e = false;
198 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000199
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700200 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000201
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300202 if (*stdscan_bufptr == '$') {
203 stdscan_bufptr++;
204 is_hex = true;
205 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700206
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300207 for (;;) {
208 c = *stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700209
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300210 if (!is_hex && (c == 'e' || c == 'E')) {
211 has_e = true;
212 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
213 /*
214 * e can only be followed by +/- if it is either a
215 * prefixed hex number or a floating-point number
216 */
217 is_float = true;
218 stdscan_bufptr++;
219 }
220 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
221 is_hex = true;
222 } else if (c == 'P' || c == 'p') {
223 is_float = true;
224 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
225 stdscan_bufptr++;
Martin Lindhe9b5aa2e2016-11-16 15:36:16 +0100226 } else if (isnumchar(c))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300227 ; /* just advance */
228 else if (c == '.')
229 is_float = true;
230 else
231 break;
232 }
233 stdscan_bufptr--; /* Point to first character beyond number */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700234
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300235 if (has_e && !is_hex) {
236 /* 1e13 is floating-point, but 1e13h is not */
237 is_float = true;
238 }
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700239
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300240 if (is_float) {
241 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
242 return tv->t_type = TOKEN_FLOAT;
243 } else {
244 r = stdscan_copy(r, stdscan_bufptr - r);
245 tv->t_integer = readnum(r, &rn_error);
246 stdscan_pop();
247 if (rn_error) {
248 /* some malformation occurred */
249 return tv->t_type = TOKEN_ERRNUM;
250 }
251 tv->t_charptr = NULL;
252 return tv->t_type = TOKEN_NUM;
253 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700254 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300255 *stdscan_bufptr == '`') {
256 /* a quoted string */
257 char start_quote = *stdscan_bufptr;
258 tv->t_charptr = stdscan_bufptr;
259 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
260 if (*stdscan_bufptr != start_quote)
261 return tv->t_type = TOKEN_ERRSTR;
262 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700263 return tv->t_type = TOKEN_STR;
Jin Kyu Song487f3522013-11-27 14:10:40 -0800264 } else if (*stdscan_bufptr == '{') {
265 /* now we've got a decorator */
266 int token_len;
267
268 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
269
270 r = ++stdscan_bufptr;
271 /*
272 * read the entire buffer to advance the buffer pointer
273 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
274 */
275 while (isbrcchar(*stdscan_bufptr))
276 stdscan_bufptr++;
277
278 token_len = stdscan_bufptr - r;
279
280 /* ... copy only up to DECOLEN_MAX-1 characters */
281 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
282 token_len : DECOLEN_MAX - 1);
283
284 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
285 /* if brace is not closed properly or token is too long */
286 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
287 nasm_error(ERR_NONFATAL,
288 "invalid decorator token inside braces");
289 return tv->t_type = TOKEN_INVALID;
290 }
291
292 stdscan_bufptr++; /* skip closing brace */
293
294 for (s = tv->t_charptr, r = ourcopy; *s; s++)
295 *r++ = nasm_tolower(*s);
296 *r = '\0';
297
298 /* right, so we have a decorator sitting in temp storage. */
299 nasm_token_hash(ourcopy, tv);
300
301 /* handle tokens inside braces */
302 return stdscan_handle_brace(tv);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700303 } else if (*stdscan_bufptr == ';') {
304 /* a comment has happened - stay */
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300305 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000306 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
307 stdscan_bufptr += 2;
308 return tv->t_type = TOKEN_SHR;
309 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
310 stdscan_bufptr += 2;
311 return tv->t_type = TOKEN_SHL;
312 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
313 stdscan_bufptr += 2;
314 return tv->t_type = TOKEN_SDIV;
315 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
316 stdscan_bufptr += 2;
317 return tv->t_type = TOKEN_SMOD;
318 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
319 stdscan_bufptr += 2;
320 return tv->t_type = TOKEN_EQ;
321 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
322 stdscan_bufptr += 2;
323 return tv->t_type = TOKEN_NE;
324 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
325 stdscan_bufptr += 2;
326 return tv->t_type = TOKEN_NE;
327 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
328 stdscan_bufptr += 2;
329 return tv->t_type = TOKEN_LE;
330 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
331 stdscan_bufptr += 2;
332 return tv->t_type = TOKEN_GE;
333 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
334 stdscan_bufptr += 2;
335 return tv->t_type = TOKEN_DBL_AND;
336 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
337 stdscan_bufptr += 2;
338 return tv->t_type = TOKEN_DBL_XOR;
339 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
340 stdscan_bufptr += 2;
341 return tv->t_type = TOKEN_DBL_OR;
342 } else /* just an ordinary char */
343 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
344}