Add missing static declaration in rdoff/rdlib.c
[nasm.git] / stdscan.c
blobea7537dd9b46aaa4f47840a48f953ae8c945b560
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>
40 #include <inttypes.h>
42 #include "nasm.h"
43 #include "nasmlib.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_error(ERR_NONFATAL,
117 "%s is not a valid decorator with braces", tv->t_charptr);
118 tv->t_type = TOKEN_INVALID;
119 } else if (tv->t_flag & TFLAG_BRC_OPT) {
120 if (is_reg_class(OPMASKREG, tv->t_integer)) {
121 /* within braces, opmask register is now used as a mask */
122 tv->t_type = TOKEN_OPMASK;
126 return tv->t_type;
129 int stdscan(void *private_data, struct tokenval *tv)
131 char ourcopy[MAX_KEYWORD + 1], *r, *s;
133 (void)private_data; /* Don't warn that this parameter is unused */
135 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
136 if (!*stdscan_bufptr)
137 return tv->t_type = TOKEN_EOS;
139 /* we have a token; either an id, a number or a char */
140 if (isidstart(*stdscan_bufptr) ||
141 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
142 /* now we've got an identifier */
143 bool is_sym = false;
144 int token_type;
146 if (*stdscan_bufptr == '$') {
147 is_sym = true;
148 stdscan_bufptr++;
151 r = stdscan_bufptr++;
152 /* read the entire buffer to advance the buffer pointer but... */
153 while (isidchar(*stdscan_bufptr))
154 stdscan_bufptr++;
156 /* ... copy only up to IDLEN_MAX-1 characters */
157 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
158 stdscan_bufptr - r : IDLEN_MAX - 1);
160 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
161 return tv->t_type = TOKEN_ID; /* bypass all other checks */
163 for (s = tv->t_charptr, r = ourcopy; *s; s++)
164 *r++ = nasm_tolower(*s);
165 *r = '\0';
166 /* right, so we have an identifier sitting in temp storage. now,
167 * is it actually a register or instruction name, or what? */
168 token_type = nasm_token_hash(ourcopy, tv);
170 if (likely(!(tv->t_flag & TFLAG_BRC))) {
171 /* most of the tokens fall into this case */
172 return token_type;
173 } else {
174 return tv->t_type = TOKEN_ID;
176 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
178 * It's a $ sign with no following hex number; this must
179 * mean it's a Here token ($), evaluating to the current
180 * assembly location, or a Base token ($$), evaluating to
181 * the base of the current segment.
183 stdscan_bufptr++;
184 if (*stdscan_bufptr == '$') {
185 stdscan_bufptr++;
186 return tv->t_type = TOKEN_BASE;
188 return tv->t_type = TOKEN_HERE;
189 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
190 bool rn_error;
191 bool is_hex = false;
192 bool is_float = false;
193 bool has_e = false;
194 char c;
196 r = stdscan_bufptr;
198 if (*stdscan_bufptr == '$') {
199 stdscan_bufptr++;
200 is_hex = true;
203 for (;;) {
204 c = *stdscan_bufptr++;
206 if (!is_hex && (c == 'e' || c == 'E')) {
207 has_e = true;
208 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
210 * e can only be followed by +/- if it is either a
211 * prefixed hex number or a floating-point number
213 is_float = true;
214 stdscan_bufptr++;
216 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
217 is_hex = true;
218 } else if (c == 'P' || c == 'p') {
219 is_float = true;
220 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
221 stdscan_bufptr++;
222 } else if (isnumchar(c) || c == '_')
223 ; /* just advance */
224 else if (c == '.')
225 is_float = true;
226 else
227 break;
229 stdscan_bufptr--; /* Point to first character beyond number */
231 if (has_e && !is_hex) {
232 /* 1e13 is floating-point, but 1e13h is not */
233 is_float = true;
236 if (is_float) {
237 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
238 return tv->t_type = TOKEN_FLOAT;
239 } else {
240 r = stdscan_copy(r, stdscan_bufptr - r);
241 tv->t_integer = readnum(r, &rn_error);
242 stdscan_pop();
243 if (rn_error) {
244 /* some malformation occurred */
245 return tv->t_type = TOKEN_ERRNUM;
247 tv->t_charptr = NULL;
248 return tv->t_type = TOKEN_NUM;
250 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
251 *stdscan_bufptr == '`') {
252 /* a quoted string */
253 char start_quote = *stdscan_bufptr;
254 tv->t_charptr = stdscan_bufptr;
255 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
256 if (*stdscan_bufptr != start_quote)
257 return tv->t_type = TOKEN_ERRSTR;
258 stdscan_bufptr++; /* Skip final quote */
259 return tv->t_type = TOKEN_STR;
260 } else if (*stdscan_bufptr == '{') {
261 /* now we've got a decorator */
262 int token_len;
264 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
266 r = ++stdscan_bufptr;
268 * read the entire buffer to advance the buffer pointer
269 * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
271 while (isbrcchar(*stdscan_bufptr))
272 stdscan_bufptr++;
274 token_len = stdscan_bufptr - r;
276 /* ... copy only up to DECOLEN_MAX-1 characters */
277 tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
278 token_len : DECOLEN_MAX - 1);
280 stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
281 /* if brace is not closed properly or token is too long */
282 if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
283 nasm_error(ERR_NONFATAL,
284 "invalid decorator token inside braces");
285 return tv->t_type = TOKEN_INVALID;
288 stdscan_bufptr++; /* skip closing brace */
290 for (s = tv->t_charptr, r = ourcopy; *s; s++)
291 *r++ = nasm_tolower(*s);
292 *r = '\0';
294 /* right, so we have a decorator sitting in temp storage. */
295 nasm_token_hash(ourcopy, tv);
297 /* handle tokens inside braces */
298 return stdscan_handle_brace(tv);
299 } else if (*stdscan_bufptr == ';') {
300 /* a comment has happened - stay */
301 return tv->t_type = TOKEN_EOS;
302 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
303 stdscan_bufptr += 2;
304 return tv->t_type = TOKEN_SHR;
305 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
306 stdscan_bufptr += 2;
307 return tv->t_type = TOKEN_SHL;
308 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
309 stdscan_bufptr += 2;
310 return tv->t_type = TOKEN_SDIV;
311 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
312 stdscan_bufptr += 2;
313 return tv->t_type = TOKEN_SMOD;
314 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
315 stdscan_bufptr += 2;
316 return tv->t_type = TOKEN_EQ;
317 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
318 stdscan_bufptr += 2;
319 return tv->t_type = TOKEN_NE;
320 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
321 stdscan_bufptr += 2;
322 return tv->t_type = TOKEN_NE;
323 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
324 stdscan_bufptr += 2;
325 return tv->t_type = TOKEN_LE;
326 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
327 stdscan_bufptr += 2;
328 return tv->t_type = TOKEN_GE;
329 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
330 stdscan_bufptr += 2;
331 return tv->t_type = TOKEN_DBL_AND;
332 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
333 stdscan_bufptr += 2;
334 return tv->t_type = TOKEN_DBL_XOR;
335 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
336 stdscan_bufptr += 2;
337 return tv->t_type = TOKEN_DBL_OR;
338 } else /* just an ordinary char */
339 return tv->t_type = (uint8_t)(*stdscan_bufptr++);