blob: 33ebcf75586bbde81560503e7897748ec4ba5750 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
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.
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.
17 *
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.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvinfe501952007-10-02 21:53:51 -070034#include "compiler.h"
35
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40#include <inttypes.h>
41
42#include "nasm.h"
43#include "nasmlib.h"
H. Peter Anvin6ecc1592008-06-01 21:34:49 -070044#include "quote.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000045#include "stdscan.h"
46#include "insns.h"
47
48/*
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.
52 */
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030053static char *stdscan_bufptr = NULL;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054static char **stdscan_tempstorage = NULL;
55static int stdscan_tempsize = 0, stdscan_templen = 0;
56#define STDSCAN_TEMP_DELTA 256
57
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030058void stdscan_set(char *str)
59{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030060 stdscan_bufptr = str;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030061}
62
63char *stdscan_get(void)
64{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030065 return stdscan_bufptr;
Cyrill Gorcunov917117f2009-10-29 23:09:18 +030066}
67
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000068static void stdscan_pop(void)
69{
70 nasm_free(stdscan_tempstorage[--stdscan_templen]);
71}
72
73void stdscan_reset(void)
74{
75 while (stdscan_templen > 0)
76 stdscan_pop();
77}
78
79/*
80 * Unimportant cleanup is done to avoid confusing people who are trying
81 * to debug real memory leaks
82 */
83void stdscan_cleanup(void)
84{
85 stdscan_reset();
86 nasm_free(stdscan_tempstorage);
87}
88
89static char *stdscan_copy(char *p, int len)
90{
91 char *text;
92
93 text = nasm_malloc(len + 1);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -070094 memcpy(text, p, len);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000095 text[len] = '\0';
96
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 *));
102 }
103 stdscan_tempstorage[stdscan_templen++] = text;
104
105 return text;
106}
107
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000108int stdscan(void *private_data, struct tokenval *tv)
109{
110 char ourcopy[MAX_KEYWORD + 1], *r, *s;
111
112 (void)private_data; /* Don't warn that this parameter is unused */
113
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700114 while (nasm_isspace(*stdscan_bufptr))
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000115 stdscan_bufptr++;
116 if (!*stdscan_bufptr)
117 return tv->t_type = 0;
118
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 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700123 bool is_sym = false;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000124
125 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700126 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000127 stdscan_bufptr++;
128 }
129
130 r = stdscan_bufptr++;
131 /* read the entire buffer to advance the buffer pointer but... */
132 while (isidchar(*stdscan_bufptr))
133 stdscan_bufptr++;
134
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);
138
139 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
140 return tv->t_type = TOKEN_ID; /* bypass all other checks */
141
142 for (s = tv->t_charptr, r = ourcopy; *s; s++)
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700143 *r++ = nasm_tolower(*s);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000144 *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? */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300147 return nasm_token_hash(ourcopy, tv);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000148 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
149 /*
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.
154 */
155 stdscan_bufptr++;
156 if (*stdscan_bufptr == '$') {
157 stdscan_bufptr++;
158 return tv->t_type = TOKEN_BASE;
159 }
160 return tv->t_type = TOKEN_HERE;
161 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
H. Peter Anvin70055962007-10-11 00:05:31 -0700162 bool rn_error;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300163 bool is_hex = false;
164 bool is_float = false;
165 bool has_e = false;
166 char c;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000167
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700168 r = stdscan_bufptr;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000169
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300170 if (*stdscan_bufptr == '$') {
171 stdscan_bufptr++;
172 is_hex = true;
173 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700174
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300175 for (;;) {
176 c = *stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700177
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300178 if (!is_hex && (c == 'e' || c == 'E')) {
179 has_e = true;
180 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
181 /*
182 * e can only be followed by +/- if it is either a
183 * prefixed hex number or a floating-point number
184 */
185 is_float = true;
186 stdscan_bufptr++;
187 }
188 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
189 is_hex = true;
190 } else if (c == 'P' || c == 'p') {
191 is_float = true;
192 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
193 stdscan_bufptr++;
194 } else if (isnumchar(c) || c == '_')
195 ; /* just advance */
196 else if (c == '.')
197 is_float = true;
198 else
199 break;
200 }
201 stdscan_bufptr--; /* Point to first character beyond number */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700202
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300203 if (has_e && !is_hex) {
204 /* 1e13 is floating-point, but 1e13h is not */
205 is_float = true;
206 }
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700207
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300208 if (is_float) {
209 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
210 return tv->t_type = TOKEN_FLOAT;
211 } else {
212 r = stdscan_copy(r, stdscan_bufptr - r);
213 tv->t_integer = readnum(r, &rn_error);
214 stdscan_pop();
215 if (rn_error) {
216 /* some malformation occurred */
217 return tv->t_type = TOKEN_ERRNUM;
218 }
219 tv->t_charptr = NULL;
220 return tv->t_type = TOKEN_NUM;
221 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700222 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300223 *stdscan_bufptr == '`') {
224 /* a quoted string */
225 char start_quote = *stdscan_bufptr;
226 tv->t_charptr = stdscan_bufptr;
227 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
228 if (*stdscan_bufptr != start_quote)
229 return tv->t_type = TOKEN_ERRSTR;
230 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700231 return tv->t_type = TOKEN_STR;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700232 } else if (*stdscan_bufptr == ';') {
233 /* a comment has happened - stay */
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000234 return tv->t_type = 0;
235 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
236 stdscan_bufptr += 2;
237 return tv->t_type = TOKEN_SHR;
238 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
239 stdscan_bufptr += 2;
240 return tv->t_type = TOKEN_SHL;
241 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
242 stdscan_bufptr += 2;
243 return tv->t_type = TOKEN_SDIV;
244 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
245 stdscan_bufptr += 2;
246 return tv->t_type = TOKEN_SMOD;
247 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
248 stdscan_bufptr += 2;
249 return tv->t_type = TOKEN_EQ;
250 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
251 stdscan_bufptr += 2;
252 return tv->t_type = TOKEN_NE;
253 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
254 stdscan_bufptr += 2;
255 return tv->t_type = TOKEN_NE;
256 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
257 stdscan_bufptr += 2;
258 return tv->t_type = TOKEN_LE;
259 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
260 stdscan_bufptr += 2;
261 return tv->t_type = TOKEN_GE;
262 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
263 stdscan_bufptr += 2;
264 return tv->t_type = TOKEN_DBL_AND;
265 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
266 stdscan_bufptr += 2;
267 return tv->t_type = TOKEN_DBL_XOR;
268 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
269 stdscan_bufptr += 2;
270 return tv->t_type = TOKEN_DBL_OR;
271 } else /* just an ordinary char */
272 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
273}