stdscan: Use nasm_error helpers
[nasm.git] / asm / stdscan.c
blob9194a9256a662e3ac4155622cb406b129c924f3b
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
34 #include "compiler.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
41 #include "nasm.h"
42 #include "nasmlib.h"
43 #include "error.h"
44 #include "quote.h"
45 #include "stdscan.h"
46 #include "insns.h"
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.
53 static char *stdscan_bufptr = NULL;
54 static char **stdscan_tempstorage = NULL;
55 static int stdscan_tempsize = 0, stdscan_templen = 0;
56 #define STDSCAN_TEMP_DELTA 256
58 void stdscan_set(char *str)
60 stdscan_bufptr = str;
63 char *stdscan_get(void)
65 return stdscan_bufptr;
68 static void stdscan_pop(void)
70 nasm_free(stdscan_tempstorage[--stdscan_templen]);
73 void stdscan_reset(void)
75 while (stdscan_templen > 0)
76 stdscan_pop();
80 * Unimportant cleanup is done to avoid confusing people who are trying
81 * to debug real memory leaks
83 void stdscan_cleanup(void)
85 stdscan_reset();
86 nasm_free(stdscan_tempstorage);
89 static char *stdscan_copy(char *p, int len)
91 char *text;
93 text = nasm_malloc(len + 1);
94 memcpy(text, p, len);
95 text[len] = '\0';
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 *));
103 stdscan_tempstorage[stdscan_templen++] = text;
105 return text;
109 * a token is enclosed with braces. proper token type will be assigned
110 * accordingly with the token flag.
112 static int stdscan_handle_brace(struct tokenval *tv)
114 if (!(tv->t_flag & TFLAG_BRC_ANY)) {
115 /* invalid token is put inside braces */
116 nasm_nonfatal("`%s' is not a valid decorator with braces", tv->t_charptr);
117 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;
125 return tv->t_type;
128 int stdscan(void *private_data, struct tokenval *tv)
130 char ourcopy[MAX_KEYWORD + 1], *r, *s;
132 (void)private_data; /* Don't warn that this parameter is unused */
134 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
135 if (!*stdscan_bufptr)
136 return tv->t_type = TOKEN_EOS;
138 /* we have a token; either an id, a number or a char */
139 if (nasm_isidstart(*stdscan_bufptr) ||
140 (*stdscan_bufptr == '$' && nasm_isidstart(stdscan_bufptr[1]))) {
141 /* now we've got an identifier */
142 bool is_sym = false;
143 int token_type;
145 if (*stdscan_bufptr == '$') {
146 is_sym = true;
147 stdscan_bufptr++;
150 r = stdscan_bufptr++;
151 /* read the entire buffer to advance the buffer pointer but... */
152 while (nasm_isidchar(*stdscan_bufptr))
153 stdscan_bufptr++;
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);
159 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
160 return tv->t_type = TOKEN_ID; /* bypass all other checks */
162 for (s = tv->t_charptr, r = ourcopy; *s; s++)
163 *r++ = nasm_tolower(*s);
164 *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? */
167 token_type = nasm_token_hash(ourcopy, tv);
169 if (unlikely(tv->t_flag & TFLAG_WARN)) {
170 nasm_warnf(ERR_PASS1|ERR_WARN_PTR, "`%s' is not a NASM keyword",
171 tv->t_charptr);
174 if (likely(!(tv->t_flag & TFLAG_BRC))) {
175 /* most of the tokens fall into this case */
176 return token_type;
177 } else {
178 return tv->t_type = TOKEN_ID;
180 } else if (*stdscan_bufptr == '$' && !nasm_isnumchar(stdscan_bufptr[1])) {
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.
187 stdscan_bufptr++;
188 if (*stdscan_bufptr == '$') {
189 stdscan_bufptr++;
190 return tv->t_type = TOKEN_BASE;
192 return tv->t_type = TOKEN_HERE;
193 } else if (nasm_isnumstart(*stdscan_bufptr)) { /* now we've got a number */
194 bool rn_error;
195 bool is_hex = false;
196 bool is_float = false;
197 bool has_e = false;
198 char c;
200 r = stdscan_bufptr;
202 if (*stdscan_bufptr == '$') {
203 stdscan_bufptr++;
204 is_hex = true;
207 for (;;) {
208 c = *stdscan_bufptr++;
210 if (!is_hex && (c == 'e' || c == 'E')) {
211 has_e = true;
212 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
214 * e can only be followed by +/- if it is either a
215 * prefixed hex number or a floating-point number
217 is_float = true;
218 stdscan_bufptr++;
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++;
226 } else if (nasm_isnumchar(c))
227 ; /* just advance */
228 else if (c == '.')
229 is_float = true;
230 else
231 break;
233 stdscan_bufptr--; /* Point to first character beyond number */
235 if (has_e && !is_hex) {
236 /* 1e13 is floating-point, but 1e13h is not */
237 is_float = true;
240 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;
251 tv->t_charptr = NULL;
252 return tv->t_type = TOKEN_NUM;
254 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
255 *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 */
263 return tv->t_type = TOKEN_STR;
264 } else if (*stdscan_bufptr == '{') {
265 /* now we've got a decorator */
266 int token_len;
268 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
270 r = ++stdscan_bufptr;
272 * read the entire buffer to advance the buffer pointer
273 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
275 while (nasm_isbrcchar(*stdscan_bufptr))
276 stdscan_bufptr++;
278 token_len = stdscan_bufptr - r;
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);
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_nonfatal("invalid decorator token inside braces");
288 return tv->t_type = TOKEN_INVALID;
291 stdscan_bufptr++; /* skip closing brace */
293 for (s = tv->t_charptr, r = ourcopy; *s; s++)
294 *r++ = nasm_tolower(*s);
295 *r = '\0';
297 /* right, so we have a decorator sitting in temp storage. */
298 nasm_token_hash(ourcopy, tv);
300 /* handle tokens inside braces */
301 return stdscan_handle_brace(tv);
302 } else if (*stdscan_bufptr == ';') {
303 /* a comment has happened - stay */
304 return tv->t_type = TOKEN_EOS;
305 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
306 if (stdscan_bufptr[2] == '>') {
307 stdscan_bufptr += 3;
308 return tv->t_type = TOKEN_SAR;
309 } else {
310 stdscan_bufptr += 2;
311 return tv->t_type = TOKEN_SHR;
313 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
314 stdscan_bufptr += stdscan_bufptr[2] == '<' ? 3 : 2;
315 return tv->t_type = TOKEN_SHL;
316 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
317 stdscan_bufptr += 2;
318 return tv->t_type = TOKEN_SDIV;
319 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
320 stdscan_bufptr += 2;
321 return tv->t_type = TOKEN_SMOD;
322 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
323 stdscan_bufptr += 2;
324 return tv->t_type = TOKEN_EQ;
325 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
326 stdscan_bufptr += 2;
327 return tv->t_type = TOKEN_NE;
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_LE;
334 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
335 stdscan_bufptr += 2;
336 return tv->t_type = TOKEN_GE;
337 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
338 stdscan_bufptr += 2;
339 return tv->t_type = TOKEN_DBL_AND;
340 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
341 stdscan_bufptr += 2;
342 return tv->t_type = TOKEN_DBL_XOR;
343 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
344 stdscan_bufptr += 2;
345 return tv->t_type = TOKEN_DBL_OR;
346 } else /* just an ordinary char */
347 return tv->t_type = (uint8_t)(*stdscan_bufptr++);