Revert "nasmlib/file.c: Windows _chsize_s() *returns* errno"
[nasm.git] / stdscan.c
blob119883ff4bab585822df57cb2c5e0e4b25131a24
1 /* ----------------------------------------------------------------------- *
3 * 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.
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 "quote.h"
44 #include "stdscan.h"
45 #include "insns.h"
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.
52 static char *stdscan_bufptr = NULL;
53 static char **stdscan_tempstorage = NULL;
54 static int stdscan_tempsize = 0, stdscan_templen = 0;
55 #define STDSCAN_TEMP_DELTA 256
57 void stdscan_set(char *str)
59 stdscan_bufptr = str;
62 char *stdscan_get(void)
64 return stdscan_bufptr;
67 static void stdscan_pop(void)
69 nasm_free(stdscan_tempstorage[--stdscan_templen]);
72 void stdscan_reset(void)
74 while (stdscan_templen > 0)
75 stdscan_pop();
79 * Unimportant cleanup is done to avoid confusing people who are trying
80 * to debug real memory leaks
82 void stdscan_cleanup(void)
84 stdscan_reset();
85 nasm_free(stdscan_tempstorage);
88 static char *stdscan_copy(char *p, int len)
90 char *text;
92 text = nasm_malloc(len + 1);
93 memcpy(text, p, len);
94 text[len] = '\0';
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 *));
102 stdscan_tempstorage[stdscan_templen++] = text;
104 return text;
108 * a token is enclosed with braces. proper token type will be assigned
109 * accordingly with the token flag.
111 static int stdscan_handle_brace(struct tokenval *tv)
113 if (!(tv->t_flag & TFLAG_BRC_ANY)) {
114 /* invalid token is put inside braces */
115 nasm_error(ERR_NONFATAL,
116 "%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 (isidstart(*stdscan_bufptr) ||
140 (*stdscan_bufptr == '$' && 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 (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 (likely(!(tv->t_flag & TFLAG_BRC))) {
170 /* most of the tokens fall into this case */
171 return token_type;
172 } else {
173 return tv->t_type = TOKEN_ID;
175 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
177 * It's a $ sign with no following hex number; this must
178 * mean it's a Here token ($), evaluating to the current
179 * assembly location, or a Base token ($$), evaluating to
180 * the base of the current segment.
182 stdscan_bufptr++;
183 if (*stdscan_bufptr == '$') {
184 stdscan_bufptr++;
185 return tv->t_type = TOKEN_BASE;
187 return tv->t_type = TOKEN_HERE;
188 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
189 bool rn_error;
190 bool is_hex = false;
191 bool is_float = false;
192 bool has_e = false;
193 char c;
195 r = stdscan_bufptr;
197 if (*stdscan_bufptr == '$') {
198 stdscan_bufptr++;
199 is_hex = true;
202 for (;;) {
203 c = *stdscan_bufptr++;
205 if (!is_hex && (c == 'e' || c == 'E')) {
206 has_e = true;
207 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
209 * e can only be followed by +/- if it is either a
210 * prefixed hex number or a floating-point number
212 is_float = true;
213 stdscan_bufptr++;
215 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
216 is_hex = true;
217 } else if (c == 'P' || c == 'p') {
218 is_float = true;
219 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
220 stdscan_bufptr++;
221 } else if (isnumchar(c) || c == '_')
222 ; /* just advance */
223 else if (c == '.')
224 is_float = true;
225 else
226 break;
228 stdscan_bufptr--; /* Point to first character beyond number */
230 if (has_e && !is_hex) {
231 /* 1e13 is floating-point, but 1e13h is not */
232 is_float = true;
235 if (is_float) {
236 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
237 return tv->t_type = TOKEN_FLOAT;
238 } else {
239 r = stdscan_copy(r, stdscan_bufptr - r);
240 tv->t_integer = readnum(r, &rn_error);
241 stdscan_pop();
242 if (rn_error) {
243 /* some malformation occurred */
244 return tv->t_type = TOKEN_ERRNUM;
246 tv->t_charptr = NULL;
247 return tv->t_type = TOKEN_NUM;
249 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
250 *stdscan_bufptr == '`') {
251 /* a quoted string */
252 char start_quote = *stdscan_bufptr;
253 tv->t_charptr = stdscan_bufptr;
254 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
255 if (*stdscan_bufptr != start_quote)
256 return tv->t_type = TOKEN_ERRSTR;
257 stdscan_bufptr++; /* Skip final quote */
258 return tv->t_type = TOKEN_STR;
259 } else if (*stdscan_bufptr == '{') {
260 /* now we've got a decorator */
261 int token_len;
263 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
265 r = ++stdscan_bufptr;
267 * read the entire buffer to advance the buffer pointer
268 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
270 while (isbrcchar(*stdscan_bufptr))
271 stdscan_bufptr++;
273 token_len = stdscan_bufptr - r;
275 /* ... copy only up to DECOLEN_MAX-1 characters */
276 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
277 token_len : DECOLEN_MAX - 1);
279 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
280 /* if brace is not closed properly or token is too long */
281 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
282 nasm_error(ERR_NONFATAL,
283 "invalid decorator token inside braces");
284 return tv->t_type = TOKEN_INVALID;
287 stdscan_bufptr++; /* skip closing brace */
289 for (s = tv->t_charptr, r = ourcopy; *s; s++)
290 *r++ = nasm_tolower(*s);
291 *r = '\0';
293 /* right, so we have a decorator sitting in temp storage. */
294 nasm_token_hash(ourcopy, tv);
296 /* handle tokens inside braces */
297 return stdscan_handle_brace(tv);
298 } else if (*stdscan_bufptr == ';') {
299 /* a comment has happened - stay */
300 return tv->t_type = TOKEN_EOS;
301 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
302 stdscan_bufptr += 2;
303 return tv->t_type = TOKEN_SHR;
304 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
305 stdscan_bufptr += 2;
306 return tv->t_type = TOKEN_SHL;
307 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
308 stdscan_bufptr += 2;
309 return tv->t_type = TOKEN_SDIV;
310 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
311 stdscan_bufptr += 2;
312 return tv->t_type = TOKEN_SMOD;
313 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
314 stdscan_bufptr += 2;
315 return tv->t_type = TOKEN_EQ;
316 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
317 stdscan_bufptr += 2;
318 return tv->t_type = TOKEN_NE;
319 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
320 stdscan_bufptr += 2;
321 return tv->t_type = TOKEN_NE;
322 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
323 stdscan_bufptr += 2;
324 return tv->t_type = TOKEN_LE;
325 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
326 stdscan_bufptr += 2;
327 return tv->t_type = TOKEN_GE;
328 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
329 stdscan_bufptr += 2;
330 return tv->t_type = TOKEN_DBL_AND;
331 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
332 stdscan_bufptr += 2;
333 return tv->t_type = TOKEN_DBL_XOR;
334 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
335 stdscan_bufptr += 2;
336 return tv->t_type = TOKEN_DBL_OR;
337 } else /* just an ordinary char */
338 return tv->t_type = (uint8_t)(*stdscan_bufptr++);