H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | beaef4a | 2009-10-30 11:29:43 +0300 | [diff] [blame] | 2 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 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. |
Cyrill Gorcunov | beaef4a | 2009-10-30 11:29:43 +0300 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 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 Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 34 | #include "compiler.h" |
| 35 | |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 36 | #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 Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 44 | #include "quote.h" |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 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 | */ |
Cyrill Gorcunov | 917117f | 2009-10-29 23:09:18 +0300 | [diff] [blame] | 53 | static char *stdscan_bufptr = NULL; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 54 | static char **stdscan_tempstorage = NULL; |
| 55 | static int stdscan_tempsize = 0, stdscan_templen = 0; |
| 56 | #define STDSCAN_TEMP_DELTA 256 |
| 57 | |
Cyrill Gorcunov | 917117f | 2009-10-29 23:09:18 +0300 | [diff] [blame] | 58 | void stdscan_set(char *str) |
| 59 | { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 60 | stdscan_bufptr = str; |
Cyrill Gorcunov | 917117f | 2009-10-29 23:09:18 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | char *stdscan_get(void) |
| 64 | { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 65 | return stdscan_bufptr; |
Cyrill Gorcunov | 917117f | 2009-10-29 23:09:18 +0300 | [diff] [blame] | 66 | } |
| 67 | |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 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); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 94 | memcpy(text, p, len); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 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 | |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 108 | int 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 | |
Cyrill Gorcunov | beaef4a | 2009-10-30 11:29:43 +0300 | [diff] [blame] | 114 | stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 115 | if (!*stdscan_bufptr) |
Cyrill Gorcunov | beaef4a | 2009-10-30 11:29:43 +0300 | [diff] [blame] | 116 | return tv->t_type = TOKEN_EOS; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 117 | |
| 118 | /* we have a token; either an id, a number or a char */ |
| 119 | if (isidstart(*stdscan_bufptr) || |
| 120 | (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) { |
| 121 | /* now we've got an identifier */ |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 122 | bool is_sym = false; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 123 | |
| 124 | if (*stdscan_bufptr == '$') { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 125 | is_sym = true; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 126 | stdscan_bufptr++; |
| 127 | } |
| 128 | |
| 129 | r = stdscan_bufptr++; |
| 130 | /* read the entire buffer to advance the buffer pointer but... */ |
| 131 | while (isidchar(*stdscan_bufptr)) |
| 132 | stdscan_bufptr++; |
| 133 | |
| 134 | /* ... copy only up to IDLEN_MAX-1 characters */ |
| 135 | tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ? |
| 136 | stdscan_bufptr - r : IDLEN_MAX - 1); |
| 137 | |
| 138 | if (is_sym || stdscan_bufptr - r > MAX_KEYWORD) |
| 139 | return tv->t_type = TOKEN_ID; /* bypass all other checks */ |
| 140 | |
| 141 | for (s = tv->t_charptr, r = ourcopy; *s; s++) |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 142 | *r++ = nasm_tolower(*s); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 143 | *r = '\0'; |
| 144 | /* right, so we have an identifier sitting in temp storage. now, |
| 145 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 146 | return nasm_token_hash(ourcopy, tv); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 147 | } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) { |
| 148 | /* |
| 149 | * It's a $ sign with no following hex number; this must |
| 150 | * mean it's a Here token ($), evaluating to the current |
| 151 | * assembly location, or a Base token ($$), evaluating to |
| 152 | * the base of the current segment. |
| 153 | */ |
| 154 | stdscan_bufptr++; |
| 155 | if (*stdscan_bufptr == '$') { |
| 156 | stdscan_bufptr++; |
| 157 | return tv->t_type = TOKEN_BASE; |
| 158 | } |
| 159 | return tv->t_type = TOKEN_HERE; |
| 160 | } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 161 | bool rn_error; |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 162 | bool is_hex = false; |
| 163 | bool is_float = false; |
| 164 | bool has_e = false; |
| 165 | char c; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 166 | |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 167 | r = stdscan_bufptr; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 168 | |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 169 | if (*stdscan_bufptr == '$') { |
| 170 | stdscan_bufptr++; |
| 171 | is_hex = true; |
| 172 | } |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 173 | |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 174 | for (;;) { |
| 175 | c = *stdscan_bufptr++; |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 176 | |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 177 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 178 | has_e = true; |
| 179 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') { |
| 180 | /* |
| 181 | * e can only be followed by +/- if it is either a |
| 182 | * prefixed hex number or a floating-point number |
| 183 | */ |
| 184 | is_float = true; |
| 185 | stdscan_bufptr++; |
| 186 | } |
| 187 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 188 | is_hex = true; |
| 189 | } else if (c == 'P' || c == 'p') { |
| 190 | is_float = true; |
| 191 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') |
| 192 | stdscan_bufptr++; |
| 193 | } else if (isnumchar(c) || c == '_') |
| 194 | ; /* just advance */ |
| 195 | else if (c == '.') |
| 196 | is_float = true; |
| 197 | else |
| 198 | break; |
| 199 | } |
| 200 | stdscan_bufptr--; /* Point to first character beyond number */ |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 201 | |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 202 | if (has_e && !is_hex) { |
| 203 | /* 1e13 is floating-point, but 1e13h is not */ |
| 204 | is_float = true; |
| 205 | } |
H. Peter Anvin | 37d88e4 | 2007-10-19 14:10:35 -0700 | [diff] [blame] | 206 | |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 207 | if (is_float) { |
| 208 | tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r); |
| 209 | return tv->t_type = TOKEN_FLOAT; |
| 210 | } else { |
| 211 | r = stdscan_copy(r, stdscan_bufptr - r); |
| 212 | tv->t_integer = readnum(r, &rn_error); |
| 213 | stdscan_pop(); |
| 214 | if (rn_error) { |
| 215 | /* some malformation occurred */ |
| 216 | return tv->t_type = TOKEN_ERRNUM; |
| 217 | } |
| 218 | tv->t_charptr = NULL; |
| 219 | return tv->t_type = TOKEN_NUM; |
| 220 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 221 | } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' || |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 222 | *stdscan_bufptr == '`') { |
| 223 | /* a quoted string */ |
| 224 | char start_quote = *stdscan_bufptr; |
| 225 | tv->t_charptr = stdscan_bufptr; |
| 226 | tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr); |
| 227 | if (*stdscan_bufptr != start_quote) |
| 228 | return tv->t_type = TOKEN_ERRSTR; |
| 229 | stdscan_bufptr++; /* Skip final quote */ |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 230 | return tv->t_type = TOKEN_STR; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 231 | } else if (*stdscan_bufptr == ';') { |
| 232 | /* a comment has happened - stay */ |
Cyrill Gorcunov | beaef4a | 2009-10-30 11:29:43 +0300 | [diff] [blame] | 233 | return tv->t_type = TOKEN_EOS; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 234 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') { |
| 235 | stdscan_bufptr += 2; |
| 236 | return tv->t_type = TOKEN_SHR; |
| 237 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') { |
| 238 | stdscan_bufptr += 2; |
| 239 | return tv->t_type = TOKEN_SHL; |
| 240 | } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') { |
| 241 | stdscan_bufptr += 2; |
| 242 | return tv->t_type = TOKEN_SDIV; |
| 243 | } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') { |
| 244 | stdscan_bufptr += 2; |
| 245 | return tv->t_type = TOKEN_SMOD; |
| 246 | } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') { |
| 247 | stdscan_bufptr += 2; |
| 248 | return tv->t_type = TOKEN_EQ; |
| 249 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') { |
| 250 | stdscan_bufptr += 2; |
| 251 | return tv->t_type = TOKEN_NE; |
| 252 | } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') { |
| 253 | stdscan_bufptr += 2; |
| 254 | return tv->t_type = TOKEN_NE; |
| 255 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') { |
| 256 | stdscan_bufptr += 2; |
| 257 | return tv->t_type = TOKEN_LE; |
| 258 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') { |
| 259 | stdscan_bufptr += 2; |
| 260 | return tv->t_type = TOKEN_GE; |
| 261 | } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') { |
| 262 | stdscan_bufptr += 2; |
| 263 | return tv->t_type = TOKEN_DBL_AND; |
| 264 | } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') { |
| 265 | stdscan_bufptr += 2; |
| 266 | return tv->t_type = TOKEN_DBL_XOR; |
| 267 | } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') { |
| 268 | stdscan_bufptr += 2; |
| 269 | return tv->t_type = TOKEN_DBL_OR; |
| 270 | } else /* just an ordinary char */ |
| 271 | return tv->t_type = (uint8_t)(*stdscan_bufptr++); |
| 272 | } |