1 | /* ----------------------------------------------------------------------- * |
2 | * |
3 | * Copyright 1996-2016 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 | |
34 | #include "compiler.h" |
35 | |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | #include <string.h> |
39 | #include <ctype.h> |
40 | |
41 | #include "nasm.h" |
42 | #include "nasmlib.h" |
43 | #include "error.h" |
44 | #include "quote.h" |
45 | #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 | */ |
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 |
57 | |
58 | void stdscan_set(char *str) |
59 | { |
60 | stdscan_bufptr = str; |
61 | } |
62 | |
63 | char *stdscan_get(void) |
64 | { |
65 | return stdscan_bufptr; |
66 | } |
67 | |
68 | static void stdscan_pop(void) |
69 | { |
70 | nasm_free(stdscan_tempstorage[--stdscan_templen]); |
71 | } |
72 | |
73 | void 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 | */ |
83 | void stdscan_cleanup(void) |
84 | { |
85 | stdscan_reset(); |
86 | nasm_free(stdscan_tempstorage); |
87 | } |
88 | |
89 | static char *stdscan_copy(char *p, int len) |
90 | { |
91 | char *text; |
92 | |
93 | text = nasm_malloc(len + 1); |
94 | memcpy(text, p, len); |
95 | 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 | |
108 | /* |
109 | * a token is enclosed with braces. proper token type will be assigned |
110 | * accordingly with the token flag. |
111 | */ |
112 | static int stdscan_handle_brace(struct tokenval *tv) |
113 | { |
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; |
123 | } |
124 | } |
125 | |
126 | return tv->t_type; |
127 | } |
128 | |
129 | int stdscan(void *private_data, struct tokenval *tv) |
130 | { |
131 | char ourcopy[MAX_KEYWORD + 1], *r, *s; |
132 | |
133 | (void)private_data; /* Don't warn that this parameter is unused */ |
134 | |
135 | stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr); |
136 | if (!*stdscan_bufptr) |
137 | return tv->t_type = TOKEN_EOS; |
138 | |
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; |
145 | |
146 | if (*stdscan_bufptr == '$') { |
147 | is_sym = true; |
148 | stdscan_bufptr++; |
149 | } |
150 | |
151 | r = stdscan_bufptr++; |
152 | /* read the entire buffer to advance the buffer pointer but... */ |
153 | while (isidchar(*stdscan_bufptr)) |
154 | stdscan_bufptr++; |
155 | |
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); |
159 | |
160 | if (is_sym || stdscan_bufptr - r > MAX_KEYWORD) |
161 | return tv->t_type = TOKEN_ID; /* bypass all other checks */ |
162 | |
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); |
169 | |
170 | if (unlikely(tv->t_flag & TFLAG_WARN)) { |
171 | nasm_error(ERR_WARNING|ERR_PASS1|WARN_PTR, |
172 | "`%s' is not a NASM keyword" , tv->t_charptr); |
173 | } |
174 | |
175 | if (likely(!(tv->t_flag & TFLAG_BRC))) { |
176 | /* most of the tokens fall into this case */ |
177 | return token_type; |
178 | } else { |
179 | return tv->t_type = TOKEN_ID; |
180 | } |
181 | } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) { |
182 | /* |
183 | * It's a $ sign with no following hex number; this must |
184 | * mean it's a Here token ($), evaluating to the current |
185 | * assembly location, or a Base token ($$), evaluating to |
186 | * the base of the current segment. |
187 | */ |
188 | stdscan_bufptr++; |
189 | if (*stdscan_bufptr == '$') { |
190 | stdscan_bufptr++; |
191 | return tv->t_type = TOKEN_BASE; |
192 | } |
193 | return tv->t_type = TOKEN_HERE; |
194 | } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */ |
195 | bool rn_error; |
196 | bool is_hex = false; |
197 | bool is_float = false; |
198 | bool has_e = false; |
199 | char c; |
200 | |
201 | r = stdscan_bufptr; |
202 | |
203 | if (*stdscan_bufptr == '$') { |
204 | stdscan_bufptr++; |
205 | is_hex = true; |
206 | } |
207 | |
208 | for (;;) { |
209 | c = *stdscan_bufptr++; |
210 | |
211 | if (!is_hex && (c == 'e' || c == 'E')) { |
212 | has_e = true; |
213 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') { |
214 | /* |
215 | * e can only be followed by +/- if it is either a |
216 | * prefixed hex number or a floating-point number |
217 | */ |
218 | is_float = true; |
219 | stdscan_bufptr++; |
220 | } |
221 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
222 | is_hex = true; |
223 | } else if (c == 'P' || c == 'p') { |
224 | is_float = true; |
225 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') |
226 | stdscan_bufptr++; |
227 | } else if (isnumchar(c)) |
228 | ; /* just advance */ |
229 | else if (c == '.') |
230 | is_float = true; |
231 | else |
232 | break; |
233 | } |
234 | stdscan_bufptr--; /* Point to first character beyond number */ |
235 | |
236 | if (has_e && !is_hex) { |
237 | /* 1e13 is floating-point, but 1e13h is not */ |
238 | is_float = true; |
239 | } |
240 | |
241 | if (is_float) { |
242 | tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r); |
243 | return tv->t_type = TOKEN_FLOAT; |
244 | } else { |
245 | r = stdscan_copy(r, stdscan_bufptr - r); |
246 | tv->t_integer = readnum(r, &rn_error); |
247 | stdscan_pop(); |
248 | if (rn_error) { |
249 | /* some malformation occurred */ |
250 | return tv->t_type = TOKEN_ERRNUM; |
251 | } |
252 | tv->t_charptr = NULL; |
253 | return tv->t_type = TOKEN_NUM; |
254 | } |
255 | } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' || |
256 | *stdscan_bufptr == '`') { |
257 | /* a quoted string */ |
258 | char start_quote = *stdscan_bufptr; |
259 | tv->t_charptr = stdscan_bufptr; |
260 | tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr); |
261 | if (*stdscan_bufptr != start_quote) |
262 | return tv->t_type = TOKEN_ERRSTR; |
263 | stdscan_bufptr++; /* Skip final quote */ |
264 | return tv->t_type = TOKEN_STR; |
265 | } else if (*stdscan_bufptr == '{') { |
266 | /* now we've got a decorator */ |
267 | int token_len; |
268 | |
269 | stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr); |
270 | |
271 | r = ++stdscan_bufptr; |
272 | /* |
273 | * read the entire buffer to advance the buffer pointer |
274 | * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens. |
275 | */ |
276 | while (isbrcchar(*stdscan_bufptr)) |
277 | stdscan_bufptr++; |
278 | |
279 | token_len = stdscan_bufptr - r; |
280 | |
281 | /* ... copy only up to DECOLEN_MAX-1 characters */ |
282 | tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ? |
283 | token_len : DECOLEN_MAX - 1); |
284 | |
285 | stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr); |
286 | /* if brace is not closed properly or token is too long */ |
287 | if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) { |
288 | nasm_error(ERR_NONFATAL, |
289 | "invalid decorator token inside braces" ); |
290 | return tv->t_type = TOKEN_INVALID; |
291 | } |
292 | |
293 | stdscan_bufptr++; /* skip closing brace */ |
294 | |
295 | for (s = tv->t_charptr, r = ourcopy; *s; s++) |
296 | *r++ = nasm_tolower(*s); |
297 | *r = '\0'; |
298 | |
299 | /* right, so we have a decorator sitting in temp storage. */ |
300 | nasm_token_hash(ourcopy, tv); |
301 | |
302 | /* handle tokens inside braces */ |
303 | return stdscan_handle_brace(tv); |
304 | } else if (*stdscan_bufptr == ';') { |
305 | /* a comment has happened - stay */ |
306 | return tv->t_type = TOKEN_EOS; |
307 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') { |
308 | stdscan_bufptr += 2; |
309 | return tv->t_type = TOKEN_SHR; |
310 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') { |
311 | stdscan_bufptr += 2; |
312 | return tv->t_type = TOKEN_SHL; |
313 | } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') { |
314 | stdscan_bufptr += 2; |
315 | return tv->t_type = TOKEN_SDIV; |
316 | } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') { |
317 | stdscan_bufptr += 2; |
318 | return tv->t_type = TOKEN_SMOD; |
319 | } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') { |
320 | stdscan_bufptr += 2; |
321 | return tv->t_type = TOKEN_EQ; |
322 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') { |
323 | stdscan_bufptr += 2; |
324 | return tv->t_type = TOKEN_NE; |
325 | } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') { |
326 | stdscan_bufptr += 2; |
327 | return tv->t_type = TOKEN_NE; |
328 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') { |
329 | stdscan_bufptr += 2; |
330 | return tv->t_type = TOKEN_LE; |
331 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') { |
332 | stdscan_bufptr += 2; |
333 | return tv->t_type = TOKEN_GE; |
334 | } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') { |
335 | stdscan_bufptr += 2; |
336 | return tv->t_type = TOKEN_DBL_AND; |
337 | } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') { |
338 | stdscan_bufptr += 2; |
339 | return tv->t_type = TOKEN_DBL_XOR; |
340 | } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') { |
341 | stdscan_bufptr += 2; |
342 | return tv->t_type = TOKEN_DBL_OR; |
343 | } else /* just an ordinary char */ |
344 | return tv->t_type = (uint8_t)(*stdscan_bufptr++); |
345 | } |
346 | |