H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 1 | #include "compiler.h" |
| 2 | |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <ctype.h> |
| 7 | #include <inttypes.h> |
| 8 | |
| 9 | #include "nasm.h" |
| 10 | #include "nasmlib.h" |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 11 | #include "quote.h" |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 12 | #include "stdscan.h" |
| 13 | #include "insns.h" |
| 14 | |
| 15 | /* |
| 16 | * Standard scanner routine used by parser.c and some output |
| 17 | * formats. It keeps a succession of temporary-storage strings in |
| 18 | * stdscan_tempstorage, which can be cleared using stdscan_reset. |
| 19 | */ |
| 20 | static char **stdscan_tempstorage = NULL; |
| 21 | static int stdscan_tempsize = 0, stdscan_templen = 0; |
| 22 | #define STDSCAN_TEMP_DELTA 256 |
| 23 | |
| 24 | static void stdscan_pop(void) |
| 25 | { |
| 26 | nasm_free(stdscan_tempstorage[--stdscan_templen]); |
| 27 | } |
| 28 | |
| 29 | void stdscan_reset(void) |
| 30 | { |
| 31 | while (stdscan_templen > 0) |
| 32 | stdscan_pop(); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Unimportant cleanup is done to avoid confusing people who are trying |
| 37 | * to debug real memory leaks |
| 38 | */ |
| 39 | void stdscan_cleanup(void) |
| 40 | { |
| 41 | stdscan_reset(); |
| 42 | nasm_free(stdscan_tempstorage); |
| 43 | } |
| 44 | |
| 45 | static char *stdscan_copy(char *p, int len) |
| 46 | { |
| 47 | char *text; |
| 48 | |
| 49 | text = nasm_malloc(len + 1); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame^] | 50 | memcpy(text, p, len); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 51 | text[len] = '\0'; |
| 52 | |
| 53 | if (stdscan_templen >= stdscan_tempsize) { |
| 54 | stdscan_tempsize += STDSCAN_TEMP_DELTA; |
| 55 | stdscan_tempstorage = nasm_realloc(stdscan_tempstorage, |
| 56 | stdscan_tempsize * |
| 57 | sizeof(char *)); |
| 58 | } |
| 59 | stdscan_tempstorage[stdscan_templen++] = text; |
| 60 | |
| 61 | return text; |
| 62 | } |
| 63 | |
| 64 | char *stdscan_bufptr = NULL; |
| 65 | int stdscan(void *private_data, struct tokenval *tv) |
| 66 | { |
| 67 | char ourcopy[MAX_KEYWORD + 1], *r, *s; |
| 68 | |
| 69 | (void)private_data; /* Don't warn that this parameter is unused */ |
| 70 | |
| 71 | while (isspace(*stdscan_bufptr)) |
| 72 | stdscan_bufptr++; |
| 73 | if (!*stdscan_bufptr) |
| 74 | return tv->t_type = 0; |
| 75 | |
| 76 | /* we have a token; either an id, a number or a char */ |
| 77 | if (isidstart(*stdscan_bufptr) || |
| 78 | (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) { |
| 79 | /* now we've got an identifier */ |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 80 | bool is_sym = false; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 81 | |
| 82 | if (*stdscan_bufptr == '$') { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 83 | is_sym = true; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 84 | stdscan_bufptr++; |
| 85 | } |
| 86 | |
| 87 | r = stdscan_bufptr++; |
| 88 | /* read the entire buffer to advance the buffer pointer but... */ |
| 89 | while (isidchar(*stdscan_bufptr)) |
| 90 | stdscan_bufptr++; |
| 91 | |
| 92 | /* ... copy only up to IDLEN_MAX-1 characters */ |
| 93 | tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ? |
| 94 | stdscan_bufptr - r : IDLEN_MAX - 1); |
| 95 | |
| 96 | if (is_sym || stdscan_bufptr - r > MAX_KEYWORD) |
| 97 | return tv->t_type = TOKEN_ID; /* bypass all other checks */ |
| 98 | |
| 99 | for (s = tv->t_charptr, r = ourcopy; *s; s++) |
| 100 | *r++ = tolower(*s); |
| 101 | *r = '\0'; |
| 102 | /* right, so we have an identifier sitting in temp storage. now, |
| 103 | * is it actually a register or instruction name, or what? */ |
H. Peter Anvin | bf9a24f | 2007-09-18 22:54:40 -0700 | [diff] [blame] | 104 | return nasm_token_hash(ourcopy, tv); |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 105 | } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) { |
| 106 | /* |
| 107 | * It's a $ sign with no following hex number; this must |
| 108 | * mean it's a Here token ($), evaluating to the current |
| 109 | * assembly location, or a Base token ($$), evaluating to |
| 110 | * the base of the current segment. |
| 111 | */ |
| 112 | stdscan_bufptr++; |
| 113 | if (*stdscan_bufptr == '$') { |
| 114 | stdscan_bufptr++; |
| 115 | return tv->t_type = TOKEN_BASE; |
| 116 | } |
| 117 | return tv->t_type = TOKEN_HERE; |
| 118 | } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 119 | bool rn_error; |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 120 | bool is_hex = false; |
| 121 | bool is_float = false; |
H. Peter Anvin | 37d88e4 | 2007-10-19 14:10:35 -0700 | [diff] [blame] | 122 | bool has_e = false; |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 123 | char c; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 124 | |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 125 | r = stdscan_bufptr; |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 126 | |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 127 | if (*stdscan_bufptr == '$') { |
| 128 | stdscan_bufptr++; |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 129 | is_hex = true; |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 130 | } |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 131 | |
| 132 | for (;;) { |
| 133 | c = *stdscan_bufptr++; |
| 134 | |
H. Peter Anvin | 37d88e4 | 2007-10-19 14:10:35 -0700 | [diff] [blame] | 135 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 136 | has_e = true; |
| 137 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') { |
| 138 | /* e can only be followed by +/- if it is either a |
| 139 | prefixed hex number or a floating-point number */ |
| 140 | is_float = true; |
| 141 | stdscan_bufptr++; |
| 142 | } |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 143 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 144 | is_hex = true; |
H. Peter Anvin | c65a2f6 | 2007-10-22 17:34:10 -0700 | [diff] [blame] | 145 | } else if (c == 'P' || c == 'p') { |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 146 | is_float = true; |
| 147 | if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') |
| 148 | stdscan_bufptr++; |
| 149 | } else if (isnumchar(c) || c == '_') |
| 150 | ; /* just advance */ |
| 151 | else if (c == '.') |
| 152 | is_float = true; |
| 153 | else |
| 154 | break; |
| 155 | } |
| 156 | stdscan_bufptr--; /* Point to first character beyond number */ |
| 157 | |
H. Peter Anvin | bea0bbb | 2007-10-22 16:53:48 -0700 | [diff] [blame] | 158 | if (has_e && !is_hex) { |
H. Peter Anvin | 37d88e4 | 2007-10-19 14:10:35 -0700 | [diff] [blame] | 159 | /* 1e13 is floating-point, but 1e13h is not */ |
| 160 | is_float = true; |
| 161 | } |
| 162 | |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 163 | if (is_float) { |
| 164 | tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r); |
| 165 | return tv->t_type = TOKEN_FLOAT; |
| 166 | } else { |
| 167 | r = stdscan_copy(r, stdscan_bufptr - r); |
| 168 | tv->t_integer = readnum(r, &rn_error); |
| 169 | stdscan_pop(); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 170 | if (rn_error) { |
| 171 | /* some malformation occurred */ |
| 172 | return tv->t_type = TOKEN_ERRNUM; |
| 173 | } |
H. Peter Anvin | 2ef4aac | 2007-10-19 13:10:46 -0700 | [diff] [blame] | 174 | tv->t_charptr = NULL; |
| 175 | return tv->t_type = TOKEN_NUM; |
| 176 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 177 | } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' || |
| 178 | *stdscan_bufptr == '`') { |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame^] | 179 | /* a quoted string */ |
H. Peter Anvin | 51cbf4a | 2007-10-11 10:12:58 -0700 | [diff] [blame] | 180 | bool rn_warn; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame^] | 181 | char start_quote = *stdscan_bufptr; |
| 182 | tv->t_charptr = stdscan_bufptr; |
| 183 | tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr); |
| 184 | if (*stdscan_bufptr != start_quote) |
| 185 | return tv->t_type = TOKEN_ERRNUM; |
| 186 | stdscan_bufptr++; /* Skip final quote */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 187 | tv->t_integer = readstrnum(tv->t_charptr, tv->t_inttwo, &rn_warn); |
| 188 | /* Issue: can't readily check rn_warn, because we might be in |
| 189 | a db family context... */ |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 190 | return tv->t_type = TOKEN_NUM; |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 191 | } else if (*stdscan_bufptr == ';') { |
| 192 | /* a comment has happened - stay */ |
H. Peter Anvin | 74cc5e5 | 2007-08-30 22:35:34 +0000 | [diff] [blame] | 193 | return tv->t_type = 0; |
| 194 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') { |
| 195 | stdscan_bufptr += 2; |
| 196 | return tv->t_type = TOKEN_SHR; |
| 197 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') { |
| 198 | stdscan_bufptr += 2; |
| 199 | return tv->t_type = TOKEN_SHL; |
| 200 | } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') { |
| 201 | stdscan_bufptr += 2; |
| 202 | return tv->t_type = TOKEN_SDIV; |
| 203 | } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') { |
| 204 | stdscan_bufptr += 2; |
| 205 | return tv->t_type = TOKEN_SMOD; |
| 206 | } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') { |
| 207 | stdscan_bufptr += 2; |
| 208 | return tv->t_type = TOKEN_EQ; |
| 209 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') { |
| 210 | stdscan_bufptr += 2; |
| 211 | return tv->t_type = TOKEN_NE; |
| 212 | } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') { |
| 213 | stdscan_bufptr += 2; |
| 214 | return tv->t_type = TOKEN_NE; |
| 215 | } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') { |
| 216 | stdscan_bufptr += 2; |
| 217 | return tv->t_type = TOKEN_LE; |
| 218 | } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') { |
| 219 | stdscan_bufptr += 2; |
| 220 | return tv->t_type = TOKEN_GE; |
| 221 | } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') { |
| 222 | stdscan_bufptr += 2; |
| 223 | return tv->t_type = TOKEN_DBL_AND; |
| 224 | } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') { |
| 225 | stdscan_bufptr += 2; |
| 226 | return tv->t_type = TOKEN_DBL_XOR; |
| 227 | } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') { |
| 228 | stdscan_bufptr += 2; |
| 229 | return tv->t_type = TOKEN_DBL_OR; |
| 230 | } else /* just an ordinary char */ |
| 231 | return tv->t_type = (uint8_t)(*stdscan_bufptr++); |
| 232 | } |