Add copyright notice to insns.dat
[nasm.git] / stdscan.c
blobb7d4b80b33661bbc2cfa62ded78ca6e2212c6002
1 #include "compiler.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <inttypes.h>
9 #include "nasm.h"
10 #include "nasmlib.h"
11 #include "quote.h"
12 #include "stdscan.h"
13 #include "insns.h"
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.
20 static char **stdscan_tempstorage = NULL;
21 static int stdscan_tempsize = 0, stdscan_templen = 0;
22 #define STDSCAN_TEMP_DELTA 256
24 static void stdscan_pop(void)
26 nasm_free(stdscan_tempstorage[--stdscan_templen]);
29 void stdscan_reset(void)
31 while (stdscan_templen > 0)
32 stdscan_pop();
36 * Unimportant cleanup is done to avoid confusing people who are trying
37 * to debug real memory leaks
39 void stdscan_cleanup(void)
41 stdscan_reset();
42 nasm_free(stdscan_tempstorage);
45 static char *stdscan_copy(char *p, int len)
47 char *text;
49 text = nasm_malloc(len + 1);
50 memcpy(text, p, len);
51 text[len] = '\0';
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 *));
59 stdscan_tempstorage[stdscan_templen++] = text;
61 return text;
64 char *stdscan_bufptr = NULL;
65 int stdscan(void *private_data, struct tokenval *tv)
67 char ourcopy[MAX_KEYWORD + 1], *r, *s;
69 (void)private_data; /* Don't warn that this parameter is unused */
71 while (nasm_isspace(*stdscan_bufptr))
72 stdscan_bufptr++;
73 if (!*stdscan_bufptr)
74 return tv->t_type = 0;
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 */
80 bool is_sym = false;
82 if (*stdscan_bufptr == '$') {
83 is_sym = true;
84 stdscan_bufptr++;
87 r = stdscan_bufptr++;
88 /* read the entire buffer to advance the buffer pointer but... */
89 while (isidchar(*stdscan_bufptr))
90 stdscan_bufptr++;
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);
96 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
97 return tv->t_type = TOKEN_ID; /* bypass all other checks */
99 for (s = tv->t_charptr, r = ourcopy; *s; s++)
100 *r++ = nasm_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? */
104 return nasm_token_hash(ourcopy, tv);
105 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
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.
112 stdscan_bufptr++;
113 if (*stdscan_bufptr == '$') {
114 stdscan_bufptr++;
115 return tv->t_type = TOKEN_BASE;
117 return tv->t_type = TOKEN_HERE;
118 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
119 bool rn_error;
120 bool is_hex = false;
121 bool is_float = false;
122 bool has_e = false;
123 char c;
125 r = stdscan_bufptr;
127 if (*stdscan_bufptr == '$') {
128 stdscan_bufptr++;
129 is_hex = true;
132 for (;;) {
133 c = *stdscan_bufptr++;
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++;
143 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
144 is_hex = true;
145 } else if (c == 'P' || c == 'p') {
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;
156 stdscan_bufptr--; /* Point to first character beyond number */
158 if (has_e && !is_hex) {
159 /* 1e13 is floating-point, but 1e13h is not */
160 is_float = true;
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();
170 if (rn_error) {
171 /* some malformation occurred */
172 return tv->t_type = TOKEN_ERRNUM;
174 tv->t_charptr = NULL;
175 return tv->t_type = TOKEN_NUM;
177 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
178 *stdscan_bufptr == '`') {
179 /* a quoted string */
180 char start_quote = *stdscan_bufptr;
181 tv->t_charptr = stdscan_bufptr;
182 tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
183 if (*stdscan_bufptr != start_quote)
184 return tv->t_type = TOKEN_ERRSTR;
185 stdscan_bufptr++; /* Skip final quote */
186 return tv->t_type = TOKEN_STR;
187 } else if (*stdscan_bufptr == ';') {
188 /* 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++);