blob: 64e77987ec67b57df76d90ddff99da0cd51ee4c7 [file] [log] [blame]
H. Peter Anvinfe501952007-10-02 21:53:51 -07001#include "compiler.h"
2
H. Peter Anvin74cc5e52007-08-30 22:35:34 +00003#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 */
19static char **stdscan_tempstorage = NULL;
20static int stdscan_tempsize = 0, stdscan_templen = 0;
21#define STDSCAN_TEMP_DELTA 256
22
23static void stdscan_pop(void)
24{
25 nasm_free(stdscan_tempstorage[--stdscan_templen]);
26}
27
28void 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 */
38void stdscan_cleanup(void)
39{
40 stdscan_reset();
41 nasm_free(stdscan_tempstorage);
42}
43
44static 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
63char *stdscan_bufptr = NULL;
64int 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 Anvin6867acc2007-10-10 14:58:45 -070079 bool is_sym = false;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000080
81 if (*stdscan_bufptr == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -070082 is_sym = true;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000083 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 Anvinbf9a24f2007-09-18 22:54:40 -0700103 return nasm_token_hash(ourcopy, tv);
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000104 } 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 Anvin70055962007-10-11 00:05:31 -0700118 bool rn_error;
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000119
120 r = stdscan_bufptr++;
121 while (isnumchar(*stdscan_bufptr))
122 stdscan_bufptr++;
123
124 if (*stdscan_bufptr == '.') {
125 /*
126 * a floating point constant
127 */
128 stdscan_bufptr++;
129 while (isnumchar(*stdscan_bufptr) ||
130 ((stdscan_bufptr[-1] == 'e'
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700131 || stdscan_bufptr[-1] == 'E'
132 || stdscan_bufptr[-1] == 'p'
133 || stdscan_bufptr[-1] == 'P')
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000134 && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
135 stdscan_bufptr++;
136 }
137 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
138 return tv->t_type = TOKEN_FLOAT;
139 }
140 r = stdscan_copy(r, stdscan_bufptr - r);
141 tv->t_integer = readnum(r, &rn_error);
142 stdscan_pop();
143 if (rn_error)
144 return tv->t_type = TOKEN_ERRNUM; /* some malformation occurred */
145 tv->t_charptr = NULL;
146 return tv->t_type = TOKEN_NUM;
147 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') { /* a char constant */
148 char quote = *stdscan_bufptr++, *r;
149 int rn_warn;
150 r = tv->t_charptr = stdscan_bufptr;
151 while (*stdscan_bufptr && *stdscan_bufptr != quote)
152 stdscan_bufptr++;
153 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
154 if (!*stdscan_bufptr)
155 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
156 stdscan_bufptr++; /* skip over final quote */
157 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
158 /* FIXME: rn_warn is not checked! */
159 return tv->t_type = TOKEN_NUM;
160 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
161 return tv->t_type = 0;
162 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
163 stdscan_bufptr += 2;
164 return tv->t_type = TOKEN_SHR;
165 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
166 stdscan_bufptr += 2;
167 return tv->t_type = TOKEN_SHL;
168 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
169 stdscan_bufptr += 2;
170 return tv->t_type = TOKEN_SDIV;
171 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
172 stdscan_bufptr += 2;
173 return tv->t_type = TOKEN_SMOD;
174 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
175 stdscan_bufptr += 2;
176 return tv->t_type = TOKEN_EQ;
177 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
178 stdscan_bufptr += 2;
179 return tv->t_type = TOKEN_NE;
180 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
181 stdscan_bufptr += 2;
182 return tv->t_type = TOKEN_NE;
183 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
184 stdscan_bufptr += 2;
185 return tv->t_type = TOKEN_LE;
186 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
187 stdscan_bufptr += 2;
188 return tv->t_type = TOKEN_GE;
189 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
190 stdscan_bufptr += 2;
191 return tv->t_type = TOKEN_DBL_AND;
192 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
193 stdscan_bufptr += 2;
194 return tv->t_type = TOKEN_DBL_XOR;
195 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
196 stdscan_bufptr += 2;
197 return tv->t_type = TOKEN_DBL_OR;
198 } else /* just an ordinary char */
199 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
200}
201