blob: aecbd4a752c965ee6cdec3a3e5b6299ea2f9d0a8 [file] [log] [blame]
H. Peter Anvin74cc5e52007-08-30 22:35:34 +00001#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 */
17static char **stdscan_tempstorage = NULL;
18static int stdscan_tempsize = 0, stdscan_templen = 0;
19#define STDSCAN_TEMP_DELTA 256
20
21static void stdscan_pop(void)
22{
23 nasm_free(stdscan_tempstorage[--stdscan_templen]);
24}
25
26void 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 */
36void stdscan_cleanup(void)
37{
38 stdscan_reset();
39 nasm_free(stdscan_tempstorage);
40}
41
42static 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
61char *stdscan_bufptr = NULL;
62int 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 Anvin74cc5e52007-08-30 22:35:34 +000077 int is_sym = FALSE;
78
79 if (*stdscan_bufptr == '$') {
80 is_sym = TRUE;
81 stdscan_bufptr++;
82 }
83
84 r = stdscan_bufptr++;
85 /* read the entire buffer to advance the buffer pointer but... */
86 while (isidchar(*stdscan_bufptr))
87 stdscan_bufptr++;
88
89 /* ... copy only up to IDLEN_MAX-1 characters */
90 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
91 stdscan_bufptr - r : IDLEN_MAX - 1);
92
93 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
94 return tv->t_type = TOKEN_ID; /* bypass all other checks */
95
96 for (s = tv->t_charptr, r = ourcopy; *s; s++)
97 *r++ = tolower(*s);
98 *r = '\0';
99 /* right, so we have an identifier sitting in temp storage. now,
100 * is it actually a register or instruction name, or what? */
H. Peter Anvinbf9a24f2007-09-18 22:54:40 -0700101 return nasm_token_hash(ourcopy, tv);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000102 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
103 /*
104 * It's a $ sign with no following hex number; this must
105 * mean it's a Here token ($), evaluating to the current
106 * assembly location, or a Base token ($$), evaluating to
107 * the base of the current segment.
108 */
109 stdscan_bufptr++;
110 if (*stdscan_bufptr == '$') {
111 stdscan_bufptr++;
112 return tv->t_type = TOKEN_BASE;
113 }
114 return tv->t_type = TOKEN_HERE;
115 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
116 int rn_error;
117
118 r = stdscan_bufptr++;
119 while (isnumchar(*stdscan_bufptr))
120 stdscan_bufptr++;
121
122 if (*stdscan_bufptr == '.') {
123 /*
124 * a floating point constant
125 */
126 stdscan_bufptr++;
127 while (isnumchar(*stdscan_bufptr) ||
128 ((stdscan_bufptr[-1] == 'e'
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700129 || stdscan_bufptr[-1] == 'E'
130 || stdscan_bufptr[-1] == 'p'
131 || stdscan_bufptr[-1] == 'P')
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000132 && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
133 stdscan_bufptr++;
134 }
135 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
136 return tv->t_type = TOKEN_FLOAT;
137 }
138 r = stdscan_copy(r, stdscan_bufptr - r);
139 tv->t_integer = readnum(r, &rn_error);
140 stdscan_pop();
141 if (rn_error)
142 return tv->t_type = TOKEN_ERRNUM; /* some malformation occurred */
143 tv->t_charptr = NULL;
144 return tv->t_type = TOKEN_NUM;
145 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') { /* a char constant */
146 char quote = *stdscan_bufptr++, *r;
147 int rn_warn;
148 r = tv->t_charptr = stdscan_bufptr;
149 while (*stdscan_bufptr && *stdscan_bufptr != quote)
150 stdscan_bufptr++;
151 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
152 if (!*stdscan_bufptr)
153 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
154 stdscan_bufptr++; /* skip over final quote */
155 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
156 /* FIXME: rn_warn is not checked! */
157 return tv->t_type = TOKEN_NUM;
158 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
159 return tv->t_type = 0;
160 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
161 stdscan_bufptr += 2;
162 return tv->t_type = TOKEN_SHR;
163 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
164 stdscan_bufptr += 2;
165 return tv->t_type = TOKEN_SHL;
166 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
167 stdscan_bufptr += 2;
168 return tv->t_type = TOKEN_SDIV;
169 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
170 stdscan_bufptr += 2;
171 return tv->t_type = TOKEN_SMOD;
172 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
173 stdscan_bufptr += 2;
174 return tv->t_type = TOKEN_EQ;
175 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
176 stdscan_bufptr += 2;
177 return tv->t_type = TOKEN_NE;
178 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
179 stdscan_bufptr += 2;
180 return tv->t_type = TOKEN_NE;
181 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
182 stdscan_bufptr += 2;
183 return tv->t_type = TOKEN_LE;
184 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
185 stdscan_bufptr += 2;
186 return tv->t_type = TOKEN_GE;
187 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
188 stdscan_bufptr += 2;
189 return tv->t_type = TOKEN_DBL_AND;
190 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
191 stdscan_bufptr += 2;
192 return tv->t_type = TOKEN_DBL_XOR;
193 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
194 stdscan_bufptr += 2;
195 return tv->t_type = TOKEN_DBL_OR;
196 } else /* just an ordinary char */
197 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
198}
199