float.c: mark read_exponent() static
[nasm.git] / stdscan.c
blobc2f97e80e2f5de538b64dff54b83b222402ac97b
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 "stdscan.h"
12 #include "insns.h"
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.
19 static char **stdscan_tempstorage = NULL;
20 static int stdscan_tempsize = 0, stdscan_templen = 0;
21 #define STDSCAN_TEMP_DELTA 256
23 static void stdscan_pop(void)
25 nasm_free(stdscan_tempstorage[--stdscan_templen]);
28 void stdscan_reset(void)
30 while (stdscan_templen > 0)
31 stdscan_pop();
35 * Unimportant cleanup is done to avoid confusing people who are trying
36 * to debug real memory leaks
38 void stdscan_cleanup(void)
40 stdscan_reset();
41 nasm_free(stdscan_tempstorage);
44 static char *stdscan_copy(char *p, int len)
46 char *text;
48 text = nasm_malloc(len + 1);
49 strncpy(text, p, len);
50 text[len] = '\0';
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 *));
58 stdscan_tempstorage[stdscan_templen++] = text;
60 return text;
63 char *stdscan_bufptr = NULL;
64 int stdscan(void *private_data, struct tokenval *tv)
66 char ourcopy[MAX_KEYWORD + 1], *r, *s;
68 (void)private_data; /* Don't warn that this parameter is unused */
70 while (isspace(*stdscan_bufptr))
71 stdscan_bufptr++;
72 if (!*stdscan_bufptr)
73 return tv->t_type = 0;
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 */
79 bool is_sym = false;
81 if (*stdscan_bufptr == '$') {
82 is_sym = true;
83 stdscan_bufptr++;
86 r = stdscan_bufptr++;
87 /* read the entire buffer to advance the buffer pointer but... */
88 while (isidchar(*stdscan_bufptr))
89 stdscan_bufptr++;
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);
95 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
96 return tv->t_type = TOKEN_ID; /* bypass all other checks */
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? */
103 return nasm_token_hash(ourcopy, tv);
104 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
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.
111 stdscan_bufptr++;
112 if (*stdscan_bufptr == '$') {
113 stdscan_bufptr++;
114 return tv->t_type = TOKEN_BASE;
116 return tv->t_type = TOKEN_HERE;
117 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
118 bool rn_error;
119 bool is_hex = false;
120 bool is_float = false;
121 bool has_e = false;
122 bool has_h = false;
123 char c;
125 r = stdscan_bufptr++;
127 if (r[0] == '$' || (r[0] == '0' || (r[1] == 'x' || r[1] == 'X')))
128 is_hex = true;
130 for (;;) {
131 c = *stdscan_bufptr++;
133 if (!is_hex && (c == 'e' || c == 'E')) {
134 has_e = true;
135 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
136 /* e can only be followed by +/- if it is either a
137 prefixed hex number or a floating-point number */
138 is_float = true;
139 stdscan_bufptr++;
141 } else if (c == 'H' || c == 'h') {
142 has_h = true;
143 } else if (c == 'P' || c == 'p') {
144 is_float = true;
145 if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
146 stdscan_bufptr++;
147 } else if (isnumchar(c) || c == '_')
148 ; /* just advance */
149 else if (c == '.')
150 is_float = true;
151 else
152 break;
154 stdscan_bufptr--; /* Point to first character beyond number */
156 if (has_e && !has_h) {
157 /* 1e13 is floating-point, but 1e13h is not */
158 is_float = true;
161 if (is_float) {
162 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
163 return tv->t_type = TOKEN_FLOAT;
164 } else {
165 r = stdscan_copy(r, stdscan_bufptr - r);
166 tv->t_integer = readnum(r, &rn_error);
167 stdscan_pop();
168 if (rn_error)
169 return tv->t_type = TOKEN_ERRNUM; /* some malformation occurred */
170 tv->t_charptr = NULL;
171 return tv->t_type = TOKEN_NUM;
173 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') {
174 /* a char constant */
175 char quote = *stdscan_bufptr++, *r;
176 bool rn_warn;
177 r = tv->t_charptr = stdscan_bufptr;
178 while (*stdscan_bufptr && *stdscan_bufptr != quote)
179 stdscan_bufptr++;
180 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
181 if (!*stdscan_bufptr)
182 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
183 stdscan_bufptr++; /* skip over final quote */
184 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
185 /* FIXME: rn_warn is not checked! */
186 return tv->t_type = TOKEN_NUM;
187 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
188 return tv->t_type = 0;
189 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
190 stdscan_bufptr += 2;
191 return tv->t_type = TOKEN_SHR;
192 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
193 stdscan_bufptr += 2;
194 return tv->t_type = TOKEN_SHL;
195 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
196 stdscan_bufptr += 2;
197 return tv->t_type = TOKEN_SDIV;
198 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
199 stdscan_bufptr += 2;
200 return tv->t_type = TOKEN_SMOD;
201 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
202 stdscan_bufptr += 2;
203 return tv->t_type = TOKEN_EQ;
204 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
205 stdscan_bufptr += 2;
206 return tv->t_type = TOKEN_NE;
207 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
208 stdscan_bufptr += 2;
209 return tv->t_type = TOKEN_NE;
210 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
211 stdscan_bufptr += 2;
212 return tv->t_type = TOKEN_LE;
213 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
214 stdscan_bufptr += 2;
215 return tv->t_type = TOKEN_GE;
216 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
217 stdscan_bufptr += 2;
218 return tv->t_type = TOKEN_DBL_AND;
219 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
220 stdscan_bufptr += 2;
221 return tv->t_type = TOKEN_DBL_XOR;
222 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
223 stdscan_bufptr += 2;
224 return tv->t_type = TOKEN_DBL_OR;
225 } else /* just an ordinary char */
226 return tv->t_type = (uint8_t)(*stdscan_bufptr++);