stdscan: switch to stdscan_get/set routines
[nasm.git] / stdscan.c
blobf50dd8574d2fb4fa00d78f6f450e23c03863f180
1 /* ----------------------------------------------------------------------- *
2 *
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;
108 int stdscan(void *private_data, struct tokenval *tv)
110 char ourcopy[MAX_KEYWORD + 1], *r, *s;
112 (void)private_data; /* Don't warn that this parameter is unused */
114 while (nasm_isspace(*stdscan_bufptr))
115 stdscan_bufptr++;
116 if (!*stdscan_bufptr)
117 return tv->t_type = 0;
119 /* we have a token; either an id, a number or a char */
120 if (isidstart(*stdscan_bufptr) ||
121 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
122 /* now we've got an identifier */
123 bool is_sym = false;
125 if (*stdscan_bufptr == '$') {
126 is_sym = true;
127 stdscan_bufptr++;
130 r = stdscan_bufptr++;
131 /* read the entire buffer to advance the buffer pointer but... */
132 while (isidchar(*stdscan_bufptr))
133 stdscan_bufptr++;
135 /* ... copy only up to IDLEN_MAX-1 characters */
136 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
137 stdscan_bufptr - r : IDLEN_MAX - 1);
139 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
140 return tv->t_type = TOKEN_ID; /* bypass all other checks */
142 for (s = tv->t_charptr, r = ourcopy; *s; s++)
143 *r++ = nasm_tolower(*s);
144 *r = '\0';
145 /* right, so we have an identifier sitting in temp storage. now,
146 * is it actually a register or instruction name, or what? */
147 return nasm_token_hash(ourcopy, tv);
148 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
150 * It's a $ sign with no following hex number; this must
151 * mean it's a Here token ($), evaluating to the current
152 * assembly location, or a Base token ($$), evaluating to
153 * the base of the current segment.
155 stdscan_bufptr++;
156 if (*stdscan_bufptr == '$') {
157 stdscan_bufptr++;
158 return tv->t_type = TOKEN_BASE;
160 return tv->t_type = TOKEN_HERE;
161 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
162 bool rn_error;
163 bool is_hex = false;
164 bool is_float = false;
165 bool has_e = false;
166 char c;
168 r = stdscan_bufptr;
170 if (*stdscan_bufptr == '$') {
171 stdscan_bufptr++;
172 is_hex = true;
175 for (;;) {
176 c = *stdscan_bufptr++;
178 if (!is_hex && (c == 'e' || c == 'E')) {
179 has_e = true;
180 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
181 /* e can only be followed by +/- if it is either a
182 prefixed hex number or a floating-point number */
183 is_float = true;
184 stdscan_bufptr++;
186 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
187 is_hex = true;
188 } else if (c == 'P' || c == 'p') {
189 is_float = true;
190 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
191 stdscan_bufptr++;
192 } else if (isnumchar(c) || c == '_')
193 ; /* just advance */
194 else if (c == '.')
195 is_float = true;
196 else
197 break;
199 stdscan_bufptr--; /* Point to first character beyond number */
201 if (has_e && !is_hex) {
202 /* 1e13 is floating-point, but 1e13h is not */
203 is_float = true;
206 if (is_float) {
207 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
208 return tv->t_type = TOKEN_FLOAT;
209 } else {
210 r = stdscan_copy(r, stdscan_bufptr - r);
211 tv->t_integer = readnum(r, &rn_error);
212 stdscan_pop();
213 if (rn_error) {
214 /* some malformation occurred */
215 return tv->t_type = TOKEN_ERRNUM;
217 tv->t_charptr = NULL;
218 return tv->t_type = TOKEN_NUM;
220 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
221 *stdscan_bufptr == '`') {
222 /* a quoted string */
223 char start_quote = *stdscan_bufptr;
224 tv->t_charptr = stdscan_bufptr;
225 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
226 if (*stdscan_bufptr != start_quote)
227 return tv->t_type = TOKEN_ERRSTR;
228 stdscan_bufptr++; /* Skip final quote */
229 return tv->t_type = TOKEN_STR;
230 } else if (*stdscan_bufptr == ';') {
231 /* a comment has happened - stay */
232 return tv->t_type = 0;
233 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
234 stdscan_bufptr += 2;
235 return tv->t_type = TOKEN_SHR;
236 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
237 stdscan_bufptr += 2;
238 return tv->t_type = TOKEN_SHL;
239 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
240 stdscan_bufptr += 2;
241 return tv->t_type = TOKEN_SDIV;
242 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
243 stdscan_bufptr += 2;
244 return tv->t_type = TOKEN_SMOD;
245 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
246 stdscan_bufptr += 2;
247 return tv->t_type = TOKEN_EQ;
248 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
249 stdscan_bufptr += 2;
250 return tv->t_type = TOKEN_NE;
251 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
252 stdscan_bufptr += 2;
253 return tv->t_type = TOKEN_NE;
254 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
255 stdscan_bufptr += 2;
256 return tv->t_type = TOKEN_LE;
257 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
258 stdscan_bufptr += 2;
259 return tv->t_type = TOKEN_GE;
260 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
261 stdscan_bufptr += 2;
262 return tv->t_type = TOKEN_DBL_AND;
263 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
264 stdscan_bufptr += 2;
265 return tv->t_type = TOKEN_DBL_XOR;
266 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
267 stdscan_bufptr += 2;
268 return tv->t_type = TOKEN_DBL_OR;
269 } else /* just an ordinary char */
270 return tv->t_type = (uint8_t)(*stdscan_bufptr++);