1 /* scan.c - lexical analyser for assembler */
2 
3 #include "syshead.h"
4 #include "const.h"
5 #include "type.h"
6 #include "globvar.h"
7 #undef EXTERN
8 #define EXTERN
9 #include "scan.h"
10 
11 PRIVATE int numbase;		/* base for number */
12 
13 PRIVATE char symofchar[256] =	/* table to convert chars to their symbols */
14 {
15     EOLSYM, WHITESPACE, WHITESPACE, WHITESPACE,
16     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
17     WHITESPACE, WHITESPACE, EOLSYM, WHITESPACE,
18     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
19     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
20     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
21     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
22     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
23 
24     WHITESPACE, EOLSYM, STRINGCONST, IMMEDIATE,	/*  !"# */
25     HEXCONST, BINCONST, ANDOP, CHARCONST,	/* $%&' */
26     LPAREN, RPAREN, STAR, ADDOP,		/* ()*+ */
27     COMMA, SUBOP, IDENT, SLASH,			/* ,-./ */
28 
29     INTCONST, INTCONST, INTCONST, INTCONST,	/* 0123 */
30     INTCONST, INTCONST, INTCONST, INTCONST,	/* 4567 */
31     INTCONST, INTCONST, COLON, EOLSYM,		/* 89:; */
32     LESSTHAN, EQOP, GREATERTHAN, MACROARG,	/* <=>? */
33 
34     INDIRECT, IDENT, IDENT, IDENT,		/* @ABC */
35     IDENT, IDENT, IDENT, IDENT,			/* DEFG */
36     IDENT, IDENT, IDENT, IDENT,			/* HIJK */
37     IDENT, IDENT, IDENT, IDENT,			/* LMNO */
38     IDENT, IDENT, IDENT, IDENT,			/* PQRS */
39     IDENT, IDENT, IDENT, IDENT,			/* TUVW */
40     IDENT, IDENT, IDENT, LBRACKET,		/* XYZ[ */
41     OTHERSYM, RBRACKET, OTHERSYM, IDENT,	/* \]^_ */
42 
43     OTHERSYM, IDENT, IDENT, IDENT,		/* `abc */
44     IDENT, IDENT, IDENT, IDENT,			/* defg */
45     IDENT, IDENT, IDENT, IDENT,			/* hijk */
46     IDENT, IDENT, IDENT, IDENT,			/* lmno */
47     IDENT, IDENT, IDENT, IDENT,			/* pqrs */
48     IDENT, IDENT, IDENT, IDENT,			/* tuvw */
49     IDENT, IDENT, IDENT, OTHERSYM,		/* xyz{ */
50     OROP, OTHERSYM, NOTOP, OTHERSYM,		/* |}~  */
51 
52     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
53     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
54     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
55     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
56     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
57     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
58     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
59     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
60 
61     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
62     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
63     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
64     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
65     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
66     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
67     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
68     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
69 
70     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
71     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
72     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
73     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
74     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
75     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
76     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
77     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
78 
79     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
80     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
81     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
82     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
83     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
84     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
85     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE,
86     WHITESPACE, WHITESPACE, WHITESPACE, WHITESPACE
87 };
88 
89 FORWARD void intconst P((void));
90 
context_hexconst()91 PUBLIC void context_hexconst()
92 {
93     numbase = 16;
94     intconst();
95 }
96 
getsym()97 PUBLIC void getsym()
98 {
99     register char *reglineptr;
100 
101     reglineptr = lineptr;
102 advance:
103     symname = reglineptr;
104     switch (sym = symofchar[(unsigned char) *reglineptr++])
105     {
106     case WHITESPACE:
107 	goto advance;
108     case ADDOP:
109 	if (*reglineptr == '+')
110 	{
111 	    sym = POSTINCOP;
112 	    ++reglineptr;
113 	}
114 	break;
115     case BINCONST:
116 	numbase = 2;
117 	lineptr = reglineptr;
118 	intconst();
119 	return;
120     case CHARCONST:
121 	if ((number = *reglineptr) < ' ')
122 	    number = ' ';
123 	if (*reglineptr != EOL)
124 	    ++reglineptr;
125 	sym = INTCONST;
126 	break;
127     case GREATERTHAN:		/* context-sensitive */
128 	if (*reglineptr == '>')
129 	{
130 	    sym = SROP;
131 	    ++reglineptr;
132 	}
133 	break;
134     case HEXCONST:
135 	numbase = 16;
136 	lineptr = reglineptr;
137 	intconst();
138 	return;
139     case IDENT:
140 	/* walk to end of identifier - magic INTCONST is max of INT, IDENT */
141 	while (symofchar[(unsigned char) *reglineptr] <= INTCONST)
142 	    ++reglineptr;
143 	lineptr = reglineptr;
144 	gsymptr = lookup();
145 	return;
146     case INTCONST:
147 	if (*(reglineptr - 1) == '0')
148 	{
149 	    if (*reglineptr != 'x' && *reglineptr != 'X')
150 		numbase = 8;
151 	    else
152 	    {
153 		numbase = 16;
154 		++reglineptr;
155 	    }
156 	}
157 	else
158 	{
159 	    --reglineptr;
160 	    numbase = 10;
161 	}
162 	lineptr = reglineptr;
163 	intconst();
164 	return;
165     case LESSTHAN:		/* context-sensitive */
166 	if (*reglineptr == '<')
167 	{
168 	    sym = SLOP;
169 	    ++reglineptr;
170 	}
171 	break;
172     case SUBOP:
173 	if (*reglineptr == '-')
174 	{
175 	    sym = PREDECOP;
176 	    ++reglineptr;
177 	}
178 	break;
179     }
180     lineptr = reglineptr;
181     return;
182 }
183 
getsym_nolookup()184 PUBLIC void getsym_nolookup()
185 {
186     bool_t old_ifflag;
187 
188     old_ifflag = ifflag;
189     ifflag = FALSE;
190     getsym();
191     ifflag = old_ifflag;
192 }
193 
intconst()194 PRIVATE void intconst()
195 {
196     register char *reglineptr;
197 
198     number = 0;
199     reglineptr = lineptr;
200     for (; *reglineptr >= '0'; ++reglineptr)
201     {
202 	if (*reglineptr > '9')
203 	{
204 	    if (numbase != 16)
205 		break;
206 	    if (*reglineptr >= 'a' && *reglineptr <= 'f')
207 	    {
208 		if (number != 0)
209 		    number = numbase * number + (*reglineptr - 'a' + 10);
210 		else
211 		    number = *reglineptr - 'a' + 10;
212 	    }
213 	    else if (*reglineptr >= 'A' && *reglineptr <= 'F')
214 	    {
215 		if (number != 0)
216 		    number = numbase * number + (*reglineptr - 'A' + 10);
217 		else
218 		    number = *reglineptr - 'A' + 10;
219 	    }
220 	    else
221 		break;
222 	}
223 	else if (number != 0)
224 	    number = numbase * number + (*reglineptr - '0');
225 	else
226 	    number = *reglineptr - '0';
227     }
228     if (*reglineptr == 'L' || *reglineptr == 'l')
229 	++reglineptr;
230     sym = INTCONST;
231     lineptr = reglineptr;
232 }
233 
initscan()234 PUBLIC void initscan()
235 {
236 #ifndef MC6809
237     if (asld_compatible)
238     {
239 	lindirect = LPAREN;
240 	rindexp = RPEXP;
241 	rindirect = RPAREN;
242     }
243     else
244     {
245 #endif
246 	lindirect = LBRACKET;
247 	rindexp = RBEXP;
248 	rindirect = RBRACKET;
249 #ifndef MC6809
250     }
251 #endif
252 }
253