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