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