blob: f50dd8574d2fb4fa00d78f6f450e23c03863f180 [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{
60 stdscan_bufptr = str;
61}
62
63char *stdscan_get(void)
64{
65 return stdscan_bufptr;
66}
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? */
H. Peter Anvinbf9a24f2007-09-18 22:54:40 -0700147 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;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700163 bool is_hex = false;
164 bool is_float = false;
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700165 bool has_e = false;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700166 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
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700170 if (*stdscan_bufptr == '$') {
171 stdscan_bufptr++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700172 is_hex = true;
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700173 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700174
175 for (;;) {
176 c = *stdscan_bufptr++;
177
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700178 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++;
185 }
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700186 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
187 is_hex = true;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700188 } else if (c == 'P' || c == 'p') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700189 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;
198 }
199 stdscan_bufptr--; /* Point to first character beyond number */
200
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700201 if (has_e && !is_hex) {
H. Peter Anvin37d88e42007-10-19 14:10:35 -0700202 /* 1e13 is floating-point, but 1e13h is not */
203 is_float = true;
204 }
205
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700206 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();
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700213 if (rn_error) {
214 /* some malformation occurred */
215 return tv->t_type = TOKEN_ERRNUM;
216 }
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700217 tv->t_charptr = NULL;
218 return tv->t_type = TOKEN_NUM;
219 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700220 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
221 *stdscan_bufptr == '`') {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700222 /* a quoted string */
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700223 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)
H. Peter Anvin11627042008-06-09 20:45:19 -0700227 return tv->t_type = TOKEN_ERRSTR;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -0700228 stdscan_bufptr++; /* Skip final quote */
H. Peter Anvin11627042008-06-09 20:45:19 -0700229 return tv->t_type = TOKEN_STR;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700230 } else if (*stdscan_bufptr == ';') {
231 /* a comment has happened - stay */
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000232 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++);
271}