blob: 27879259a8f14902b668f16e2729c4749cf984e5 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +03002 *
H. Peter Anvin94adf7d2018-06-15 18:37:32 -07003 * Copyright 1996-2018 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>
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000038#include <ctype.h>
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000039
40#include "nasm.h"
41#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080042#include "error.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 */
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300115 nasm_nonfatal("`%s' is not a valid decorator with braces", tv->t_charptr);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700116 tv->t_type = TOKEN_INVALID;
117 } else if (tv->t_flag & TFLAG_BRC_OPT) {
118 if (is_reg_class(OPMASKREG, tv->t_integer)) {
119 /* within braces, opmask register is now used as a mask */
120 tv->t_type = TOKEN_OPMASK;
121 }
122 }
123
Jin Kyu Song72018a22013-08-05 20:46:18 -0700124 return tv->t_type;
125}
126
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000127int stdscan(void *private_data, struct tokenval *tv)
128{
129 char ourcopy[MAX_KEYWORD + 1], *r, *s;
130
131 (void)private_data; /* Don't warn that this parameter is unused */
132
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300133 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
Jin Kyu Song487f3522013-11-27 14:10:40 -0800134 if (!*stdscan_bufptr)
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300135 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000136
137 /* we have a token; either an id, a number or a char */
H. Peter Anvin13506202018-11-28 14:55:58 -0800138 if (nasm_isidstart(*stdscan_bufptr) ||
139 (*stdscan_bufptr == '$' && nasm_isidstart(stdscan_bufptr[1]))) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000140 /* now we've got an identifier */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700141 bool is_sym = false;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700142 int token_type;
143
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000144 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700145 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000146 stdscan_bufptr++;
147 }
148
149 r = stdscan_bufptr++;
150 /* read the entire buffer to advance the buffer pointer but... */
H. Peter Anvin13506202018-11-28 14:55:58 -0800151 while (nasm_isidchar(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000152 stdscan_bufptr++;
153
154 /* ... copy only up to IDLEN_MAX-1 characters */
155 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
156 stdscan_bufptr - r : IDLEN_MAX - 1);
157
158 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
159 return tv->t_type = TOKEN_ID; /* bypass all other checks */
160
161 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700162 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000163 *r = '\0';
164 /* right, so we have an identifier sitting in temp storage. now,
165 * is it actually a register or instruction name, or what? */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700166 token_type = nasm_token_hash(ourcopy, tv);
167
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300168 if (unlikely(tv->t_flag & TFLAG_WARN)) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800169 /*!
170 *!ptr [on] non-NASM keyword used in other assemblers
171 *! warns about keywords used in other assemblers that might
172 *! indicate a mistake in the source code. Currently only the MASM
173 *! \c{PTR} keyword is recognized.
174 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800175 nasm_warn(WARN_PTR, "`%s' is not a NASM keyword",
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300176 tv->t_charptr);
177 }
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700178
Jin Kyu Song487f3522013-11-27 14:10:40 -0800179 if (likely(!(tv->t_flag & TFLAG_BRC))) {
180 /* most of the tokens fall into this case */
181 return token_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700182 } else {
Jin Kyu Song487f3522013-11-27 14:10:40 -0800183 return tv->t_type = TOKEN_ID;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700184 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800185 } else if (*stdscan_bufptr == '$' && !nasm_isnumchar(stdscan_bufptr[1])) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000186 /*
187 * It's a $ sign with no following hex number; this must
188 * mean it's a Here token ($), evaluating to the current
189 * assembly location, or a Base token ($$), evaluating to
190 * the base of the current segment.
191 */
192 stdscan_bufptr++;
193 if (*stdscan_bufptr == '$') {
194 stdscan_bufptr++;
195 return tv->t_type = TOKEN_BASE;
196 }
197 return tv->t_type = TOKEN_HERE;
H. Peter Anvin13506202018-11-28 14:55:58 -0800198 } else if (nasm_isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700199 bool rn_error;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300200 bool is_hex = false;
201 bool is_float = false;
202 bool has_e = false;
203 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000204
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700205 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000206
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300207 if (*stdscan_bufptr == '$') {
208 stdscan_bufptr++;
209 is_hex = true;
210 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700211
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300212 for (;;) {
213 c = *stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700214
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300215 if (!is_hex && (c == 'e' || c == 'E')) {
216 has_e = true;
217 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
218 /*
219 * e can only be followed by +/- if it is either a
220 * prefixed hex number or a floating-point number
221 */
222 is_float = true;
223 stdscan_bufptr++;
224 }
225 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
226 is_hex = true;
227 } else if (c == 'P' || c == 'p') {
228 is_float = true;
229 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
230 stdscan_bufptr++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800231 } else if (nasm_isnumchar(c))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300232 ; /* just advance */
233 else if (c == '.')
234 is_float = true;
235 else
236 break;
237 }
238 stdscan_bufptr--; /* Point to first character beyond number */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700239
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300240 if (has_e && !is_hex) {
241 /* 1e13 is floating-point, but 1e13h is not */
242 is_float = true;
243 }
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700244
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300245 if (is_float) {
246 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
247 return tv->t_type = TOKEN_FLOAT;
248 } else {
249 r = stdscan_copy(r, stdscan_bufptr - r);
250 tv->t_integer = readnum(r, &rn_error);
251 stdscan_pop();
252 if (rn_error) {
253 /* some malformation occurred */
254 return tv->t_type = TOKEN_ERRNUM;
255 }
256 tv->t_charptr = NULL;
257 return tv->t_type = TOKEN_NUM;
258 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700259 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300260 *stdscan_bufptr == '`') {
261 /* a quoted string */
262 char start_quote = *stdscan_bufptr;
263 tv->t_charptr = stdscan_bufptr;
264 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
265 if (*stdscan_bufptr != start_quote)
266 return tv->t_type = TOKEN_ERRSTR;
267 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700268 return tv->t_type = TOKEN_STR;
Jin Kyu Song487f3522013-11-27 14:10:40 -0800269 } else if (*stdscan_bufptr == '{') {
270 /* now we've got a decorator */
271 int token_len;
272
273 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
274
275 r = ++stdscan_bufptr;
276 /*
277 * read the entire buffer to advance the buffer pointer
278 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
279 */
H. Peter Anvin13506202018-11-28 14:55:58 -0800280 while (nasm_isbrcchar(*stdscan_bufptr))
Jin Kyu Song487f3522013-11-27 14:10:40 -0800281 stdscan_bufptr++;
282
283 token_len = stdscan_bufptr - r;
284
285 /* ... copy only up to DECOLEN_MAX-1 characters */
286 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
287 token_len : DECOLEN_MAX - 1);
288
289 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
290 /* if brace is not closed properly or token is too long */
291 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300292 nasm_nonfatal("invalid decorator token inside braces");
Jin Kyu Song487f3522013-11-27 14:10:40 -0800293 return tv->t_type = TOKEN_INVALID;
294 }
295
296 stdscan_bufptr++; /* skip closing brace */
297
298 for (s = tv->t_charptr, r = ourcopy; *s; s++)
299 *r++ = nasm_tolower(*s);
300 *r = '\0';
301
302 /* right, so we have a decorator sitting in temp storage. */
303 nasm_token_hash(ourcopy, tv);
304
305 /* handle tokens inside braces */
306 return stdscan_handle_brace(tv);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700307 } else if (*stdscan_bufptr == ';') {
308 /* a comment has happened - stay */
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300309 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000310 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700311 if (stdscan_bufptr[2] == '>') {
312 stdscan_bufptr += 3;
313 return tv->t_type = TOKEN_SAR;
314 } else {
315 stdscan_bufptr += 2;
316 return tv->t_type = TOKEN_SHR;
317 }
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000318 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700319 stdscan_bufptr += stdscan_bufptr[2] == '<' ? 3 : 2;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000320 return tv->t_type = TOKEN_SHL;
321 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
322 stdscan_bufptr += 2;
323 return tv->t_type = TOKEN_SDIV;
324 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
325 stdscan_bufptr += 2;
326 return tv->t_type = TOKEN_SMOD;
327 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
328 stdscan_bufptr += 2;
329 return tv->t_type = TOKEN_EQ;
330 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
331 stdscan_bufptr += 2;
332 return tv->t_type = TOKEN_NE;
333 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
334 stdscan_bufptr += 2;
335 return tv->t_type = TOKEN_NE;
336 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
337 stdscan_bufptr += 2;
338 return tv->t_type = TOKEN_LE;
339 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
340 stdscan_bufptr += 2;
341 return tv->t_type = TOKEN_GE;
342 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
343 stdscan_bufptr += 2;
344 return tv->t_type = TOKEN_DBL_AND;
345 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
346 stdscan_bufptr += 2;
347 return tv->t_type = TOKEN_DBL_XOR;
348 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
349 stdscan_bufptr += 2;
350 return tv->t_type = TOKEN_DBL_OR;
351 } else /* just an ordinary char */
352 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
353}