blob: 8f6a3c61f546ae713f24390cff835b7bb44ddce9 [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 Anvinc2f3f262018-12-27 12:37:25 -080036#include "nctype.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000037
38#include "nasm.h"
39#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080040#include "error.h"
H. Peter Anvin6ecc1592008-06-01 21:34:49 -070041#include "quote.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000042#include "stdscan.h"
43#include "insns.h"
44
45/*
46 * Standard scanner routine used by parser.c and some output
47 * formats. It keeps a succession of temporary-storage strings in
48 * stdscan_tempstorage, which can be cleared using stdscan_reset.
49 */
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030050static char *stdscan_bufptr = NULL;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000051static char **stdscan_tempstorage = NULL;
52static int stdscan_tempsize = 0, stdscan_templen = 0;
53#define STDSCAN_TEMP_DELTA 256
54
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030055void stdscan_set(char *str)
56{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030057 stdscan_bufptr = str;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030058}
59
60char *stdscan_get(void)
61{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030062 return stdscan_bufptr;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030063}
64
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000065static void stdscan_pop(void)
66{
67 nasm_free(stdscan_tempstorage[--stdscan_templen]);
68}
69
70void stdscan_reset(void)
71{
72 while (stdscan_templen > 0)
73 stdscan_pop();
74}
75
76/*
77 * Unimportant cleanup is done to avoid confusing people who are trying
78 * to debug real memory leaks
79 */
80void stdscan_cleanup(void)
81{
82 stdscan_reset();
83 nasm_free(stdscan_tempstorage);
84}
85
86static char *stdscan_copy(char *p, int len)
87{
88 char *text;
89
90 text = nasm_malloc(len + 1);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -070091 memcpy(text, p, len);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000092 text[len] = '\0';
93
94 if (stdscan_templen >= stdscan_tempsize) {
95 stdscan_tempsize += STDSCAN_TEMP_DELTA;
96 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
97 stdscan_tempsize *
98 sizeof(char *));
99 }
100 stdscan_tempstorage[stdscan_templen++] = text;
101
102 return text;
103}
104
Jin Kyu Song72018a22013-08-05 20:46:18 -0700105/*
106 * a token is enclosed with braces. proper token type will be assigned
107 * accordingly with the token flag.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700108 */
109static int stdscan_handle_brace(struct tokenval *tv)
110{
111 if (!(tv->t_flag & TFLAG_BRC_ANY)) {
112 /* invalid token is put inside braces */
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300113 nasm_nonfatal("`%s' is not a valid decorator with braces", tv->t_charptr);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700114 tv->t_type = TOKEN_INVALID;
115 } else if (tv->t_flag & TFLAG_BRC_OPT) {
116 if (is_reg_class(OPMASKREG, tv->t_integer)) {
117 /* within braces, opmask register is now used as a mask */
118 tv->t_type = TOKEN_OPMASK;
119 }
120 }
121
Jin Kyu Song72018a22013-08-05 20:46:18 -0700122 return tv->t_type;
123}
124
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000125int stdscan(void *private_data, struct tokenval *tv)
126{
127 char ourcopy[MAX_KEYWORD + 1], *r, *s;
128
129 (void)private_data; /* Don't warn that this parameter is unused */
130
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300131 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
Jin Kyu Song487f3522013-11-27 14:10:40 -0800132 if (!*stdscan_bufptr)
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300133 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000134
135 /* we have a token; either an id, a number or a char */
H. Peter Anvin13506202018-11-28 14:55:58 -0800136 if (nasm_isidstart(*stdscan_bufptr) ||
137 (*stdscan_bufptr == '$' && nasm_isidstart(stdscan_bufptr[1]))) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000138 /* now we've got an identifier */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700139 bool is_sym = false;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700140 int token_type;
141
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000142 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700143 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000144 stdscan_bufptr++;
145 }
146
147 r = stdscan_bufptr++;
148 /* read the entire buffer to advance the buffer pointer but... */
H. Peter Anvin13506202018-11-28 14:55:58 -0800149 while (nasm_isidchar(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000150 stdscan_bufptr++;
151
152 /* ... copy only up to IDLEN_MAX-1 characters */
153 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
154 stdscan_bufptr - r : IDLEN_MAX - 1);
155
156 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
157 return tv->t_type = TOKEN_ID; /* bypass all other checks */
158
159 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700160 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000161 *r = '\0';
162 /* right, so we have an identifier sitting in temp storage. now,
163 * is it actually a register or instruction name, or what? */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700164 token_type = nasm_token_hash(ourcopy, tv);
165
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300166 if (unlikely(tv->t_flag & TFLAG_WARN)) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800167 /*!
168 *!ptr [on] non-NASM keyword used in other assemblers
169 *! warns about keywords used in other assemblers that might
170 *! indicate a mistake in the source code. Currently only the MASM
171 *! \c{PTR} keyword is recognized.
172 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800173 nasm_warn(WARN_PTR, "`%s' is not a NASM keyword",
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300174 tv->t_charptr);
175 }
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700176
Jin Kyu Song487f3522013-11-27 14:10:40 -0800177 if (likely(!(tv->t_flag & TFLAG_BRC))) {
178 /* most of the tokens fall into this case */
179 return token_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700180 } else {
Jin Kyu Song487f3522013-11-27 14:10:40 -0800181 return tv->t_type = TOKEN_ID;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700182 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800183 } else if (*stdscan_bufptr == '$' && !nasm_isnumchar(stdscan_bufptr[1])) {
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000184 /*
185 * It's a $ sign with no following hex number; this must
186 * mean it's a Here token ($), evaluating to the current
187 * assembly location, or a Base token ($$), evaluating to
188 * the base of the current segment.
189 */
190 stdscan_bufptr++;
191 if (*stdscan_bufptr == '$') {
192 stdscan_bufptr++;
193 return tv->t_type = TOKEN_BASE;
194 }
195 return tv->t_type = TOKEN_HERE;
H. Peter Anvin13506202018-11-28 14:55:58 -0800196 } else if (nasm_isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700197 bool rn_error;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300198 bool is_hex = false;
199 bool is_float = false;
200 bool has_e = false;
201 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000202
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700203 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000204
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300205 if (*stdscan_bufptr == '$') {
206 stdscan_bufptr++;
207 is_hex = true;
208 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700209
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300210 for (;;) {
211 c = *stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700212
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300213 if (!is_hex && (c == 'e' || c == 'E')) {
214 has_e = true;
215 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
216 /*
217 * e can only be followed by +/- if it is either a
218 * prefixed hex number or a floating-point number
219 */
220 is_float = true;
221 stdscan_bufptr++;
222 }
223 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
224 is_hex = true;
225 } else if (c == 'P' || c == 'p') {
226 is_float = true;
227 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
228 stdscan_bufptr++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800229 } else if (nasm_isnumchar(c))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300230 ; /* just advance */
231 else if (c == '.')
232 is_float = true;
233 else
234 break;
235 }
236 stdscan_bufptr--; /* Point to first character beyond number */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700237
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300238 if (has_e && !is_hex) {
239 /* 1e13 is floating-point, but 1e13h is not */
240 is_float = true;
241 }
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700242
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300243 if (is_float) {
244 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
245 return tv->t_type = TOKEN_FLOAT;
246 } else {
247 r = stdscan_copy(r, stdscan_bufptr - r);
248 tv->t_integer = readnum(r, &rn_error);
249 stdscan_pop();
250 if (rn_error) {
251 /* some malformation occurred */
252 return tv->t_type = TOKEN_ERRNUM;
253 }
254 tv->t_charptr = NULL;
255 return tv->t_type = TOKEN_NUM;
256 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700257 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300258 *stdscan_bufptr == '`') {
259 /* a quoted string */
260 char start_quote = *stdscan_bufptr;
261 tv->t_charptr = stdscan_bufptr;
262 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
263 if (*stdscan_bufptr != start_quote)
264 return tv->t_type = TOKEN_ERRSTR;
265 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700266 return tv->t_type = TOKEN_STR;
Jin Kyu Song487f3522013-11-27 14:10:40 -0800267 } else if (*stdscan_bufptr == '{') {
268 /* now we've got a decorator */
269 int token_len;
270
271 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
272
273 r = ++stdscan_bufptr;
274 /*
275 * read the entire buffer to advance the buffer pointer
276 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
277 */
H. Peter Anvin13506202018-11-28 14:55:58 -0800278 while (nasm_isbrcchar(*stdscan_bufptr))
Jin Kyu Song487f3522013-11-27 14:10:40 -0800279 stdscan_bufptr++;
280
281 token_len = stdscan_bufptr - r;
282
283 /* ... copy only up to DECOLEN_MAX-1 characters */
284 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
285 token_len : DECOLEN_MAX - 1);
286
287 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
288 /* if brace is not closed properly or token is too long */
289 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
Cyrill Gorcunovb449ce42018-12-01 21:02:51 +0300290 nasm_nonfatal("invalid decorator token inside braces");
Jin Kyu Song487f3522013-11-27 14:10:40 -0800291 return tv->t_type = TOKEN_INVALID;
292 }
293
294 stdscan_bufptr++; /* skip closing brace */
295
296 for (s = tv->t_charptr, r = ourcopy; *s; s++)
297 *r++ = nasm_tolower(*s);
298 *r = '\0';
299
300 /* right, so we have a decorator sitting in temp storage. */
301 nasm_token_hash(ourcopy, tv);
302
303 /* handle tokens inside braces */
304 return stdscan_handle_brace(tv);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700305 } else if (*stdscan_bufptr == ';') {
306 /* a comment has happened - stay */
Cyrill Gorcunovbeaef4a2009-10-30 11:29:43 +0300307 return tv->t_type = TOKEN_EOS;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000308 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700309 if (stdscan_bufptr[2] == '>') {
310 stdscan_bufptr += 3;
311 return tv->t_type = TOKEN_SAR;
312 } else {
313 stdscan_bufptr += 2;
314 return tv->t_type = TOKEN_SHR;
315 }
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000316 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700317 stdscan_bufptr += stdscan_bufptr[2] == '<' ? 3 : 2;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000318 return tv->t_type = TOKEN_SHL;
319 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
320 stdscan_bufptr += 2;
321 return tv->t_type = TOKEN_SDIV;
322 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
323 stdscan_bufptr += 2;
324 return tv->t_type = TOKEN_SMOD;
325 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
326 stdscan_bufptr += 2;
327 return tv->t_type = TOKEN_EQ;
328 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
329 stdscan_bufptr += 2;
330 return tv->t_type = TOKEN_NE;
331 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
332 stdscan_bufptr += 2;
333 return tv->t_type = TOKEN_NE;
334 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
335 stdscan_bufptr += 2;
336 return tv->t_type = TOKEN_LE;
337 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
338 stdscan_bufptr += 2;
339 return tv->t_type = TOKEN_GE;
340 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
341 stdscan_bufptr += 2;
342 return tv->t_type = TOKEN_DBL_AND;
343 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
344 stdscan_bufptr += 2;
345 return tv->t_type = TOKEN_DBL_XOR;
346 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
347 stdscan_bufptr += 2;
348 return tv->t_type = TOKEN_DBL_OR;
349 } else /* just an ordinary char */
350 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
351}