xref: /original-bsd/old/as.tahoe/asscan1.c (revision a9392a99)
1 /*
2  *	Copyright (c) 1982 Regents of the University of California
3  */
4 #ifndef lint
5 static char sccsid[] = "@(#)asscan1.c 4.6 7/6/83";
6 #endif not lint
7 
8 #include "asscanl.h"
9 
10 inittokfile()
11 {
12 	if (passno == 1){
13 		if (useVM){
14 			bufstart = &tokbuf[0];
15 			buftail = &tokbuf[1];
16 			bufstart->tok_next = buftail;
17 			buftail->tok_next = 0;
18 		}
19 		tokbuf[0].tok_count = -1;
20 		tokbuf[1].tok_count = -1;
21 	}
22 	tok_temp = 0;
23 	tok_free = 0;
24 	bufno = 0;
25 	emptybuf = &tokbuf[bufno];
26 	tokptr = 0;
27 	tokub = 0;
28 }
29 
30 closetokfile()
31 {
32 	if (passno == 1){
33 		if (useVM){
34 			emptybuf->toks[emptybuf->tok_count++] = PARSEEOF;
35 		} else {
36 			/*
37 			 *	Clean up the buffers that haven't been
38 			 *	written out yet
39 			 */
40 			if (tokbuf[bufno ^ 1].tok_count >= 0){
41 				if (writeTEST((char *)&tokbuf[bufno ^ 1], sizeof *emptybuf, 1, tokfile)){
42 				  badwrite:
43 					yyerror("Unexpected end of file writing the interpass tmp file");
44 				exit(2);
45 				}
46 			}
47 			/*
48 			 *	Ensure that we will read an End of file,
49 			 *	if there are more than one file names
50 			 *	in the argument list
51 			 */
52 			tokbuf[bufno].toks[tokbuf[bufno].tok_count++] = PARSEEOF;
53 			if (writeTEST((char *)&tokbuf[bufno], sizeof *emptybuf, 1, tokfile))
54 				goto badwrite;
55 		}
56 	}	/*end of being pass 1*/
57 }
58 
59 inttoktype yylex()
60 {
61 	register	ptrall	bufptr;
62 	register	inttoktype		val;
63 	register	struct	exp	*locxp;
64 	/*
65 	 *	No local variables to be allocated; this saves
66 	 *	one piddling instruction..
67 	 */
68 	static	int	Lastjxxx;
69 
70 	bufptr = tokptr;		/*copy in the global value*/
71    top:
72 	if (bufptr < tokub){
73 		gtoken(val, bufptr);
74 		switch(yylval = val){
75 		case	PARSEEOF:
76 				yylval = val = PARSEEOF;
77 				break;
78 		case	BFINT:
79 		case	INT:
80 				if (xp >= &explist[NEXP])
81 				     yyerror("Too many expressions; try simplyfing");
82 				else
83 				    locxp = xp++;
84 				locxp->e_number = Znumber;
85 				locxp->e_number.num_tag = TYPL;
86 				glong(locxp->e_xvalue, bufptr);
87 			  makevalue:
88 				locxp->e_xtype = XABS;
89 				locxp->e_xloc = 0;
90 				locxp->e_xname = NULL;
91 				yylval = (int)locxp;
92 				break;
93 		case	BIGNUM:
94 				if (xp >= &explist[NEXP])
95 				     yyerror("Too many expressions; try simplyfing");
96 				else
97 				    locxp = xp++;
98 				gnumber(locxp->e_number, bufptr);
99 				goto makevalue;
100 		case	NAME:
101 				gptr(yylval, bufptr);
102 				lastnam = (struct symtab *)yylval;
103 				break;
104 		case	SIZESPEC:
105 		case 	REG:
106 				gchar(yylval, bufptr);
107 				break;
108 		case	INSTn:
109 		case	INST0:
110 				gopcode(yyopcode, bufptr);
111 				break;
112 		case	IJXXX:
113 				gopcode(yyopcode, bufptr);
114 				/* We can't cast Lastjxxx into (int *) here.. */
115 				gptr(Lastjxxx, bufptr);
116 				lastjxxx = (struct symtab *)Lastjxxx;
117 				break;
118 		case	ILINESKIP:
119 				gint(yylval, bufptr);
120 				lineno += yylval;
121 				goto top;
122 		case	SKIP:
123 				eatskiplg(bufptr);
124 				goto top;
125 		case	VOID:
126 				goto top;
127 		case 	STRING:
128 		case 	ISTAB:
129 		case	ISTABSTR:
130 		case	ISTABNONE:
131 		case	ISTABDOT:
132 		case	IALIGN:
133 				gptr(yylval, bufptr);
134 				break;
135 		}
136 #ifdef DEBUG
137 		if (toktrace){
138 		char	*tok_to_name();
139 		printf("P: %d T#: %4d, %s ",
140 			passno, bufptr -  firsttoken, tok_to_name(val));
141 		switch(val){
142 		case 	INT:	printf("val %d",
143 					((struct exp *)yylval)->e_xvalue);
144 				break;
145 		case	BFINT:	printf("val %d",
146 					((struct exp *)yylval)->e_xvalue);
147 				break;
148 		case 	BIGNUM: bignumprint(((struct exp*)yylval)->e_number);
149 				break;
150 		case	NAME:	printf("\"%.8s\"",
151 					FETCHNAME((struct symtab *)yylval));
152 				break;
153 		case	REG:	printf(" r%d",
154 					yylval);
155 				break;
156 		case	IJXXX:
157 		case	INST0:
158 		case	INSTn:	printf("%.8s",
159 						FETCHNAME(ITABFETCH(yyopcode)));
160 				break;
161 		case	STRING:
162 			printf("length %d, seekoffset %d, place 0%o ",
163 				((struct strdesc *)yylval)->sd_strlen,
164 				((struct strdesc *)yylval)->sd_stroff,
165 				((struct strdesc *)yylval)->sd_place
166 				);
167 			if (((struct strdesc *)yylval)->sd_place & STR_CORE)
168 				printf("value\"%*s\"",
169 					((struct strdesc *)yylval)->sd_strlen,
170 					((struct strdesc *)yylval)->sd_string);
171 			break;
172 		}  		/*end of the debug switch*/
173 		printf("\n");
174 		}
175 #endif DEBUG
176 
177 	} else {	/* start a new buffer */
178 	    if (useVM){
179 		if (passno == 2){
180 			tok_temp = emptybuf->tok_next;
181 			emptybuf->tok_next = tok_free;
182 			tok_free = emptybuf;
183 			emptybuf = tok_temp;
184 		} else {
185 			emptybuf = emptybuf->tok_next;
186 		}
187 		bufno += 1;
188 		if (emptybuf == 0){
189 			struct	tokbufdesc *newdallop;
190 			int	i;
191 			if (passno == 2)
192 				goto badread;
193 			emptybuf = newdallop = (struct tokbufdesc *)
194 			  Calloc(TOKDALLOP, sizeof (struct tokbufdesc));
195 			for (i=0; i < TOKDALLOP; i++){
196 				buftail->tok_next = newdallop;
197 				buftail = newdallop;
198 				newdallop += 1;
199 			}
200 			buftail->tok_next = 0;
201 		}	/*end of need to get more buffers*/
202 		(bytetoktype *)bufptr = &(emptybuf->toks[0]);
203 		if (passno == 1)
204 			scan_dot_s(emptybuf);
205 	    } else {	/*don't use VM*/
206 		bufno ^= 1;
207 		emptybuf = &tokbuf[bufno];
208 		((bytetoktype *)bufptr) = &(emptybuf->toks[0]);
209 		if (passno == 1){
210 			/*
211 			 *	First check if there are things to write
212 			 *	out at all
213 			 */
214 			if (emptybuf->tok_count >= 0){
215 			    if (writeTEST((char *)emptybuf, sizeof *emptybuf, 1, tokfile)){
216 				yyerror("Unexpected end of file writing the interpass tmp file");
217 				exit(2);
218 			    }
219 			}
220 			scan_dot_s(emptybuf);
221 		} else {	/*pass 2*/
222 		    if (readTEST((char *)emptybuf, sizeof *emptybuf, 1, tokfile)){
223 			 badread:
224 			     yyerror("Unexpected end of file while reading the interpass tmp file");
225 			     exit(1);
226 		    }
227 		}
228 	    }	/*end of using a real live file*/
229 	    (char *)tokub = (char *)bufptr + emptybuf->tok_count;
230 #ifdef DEBUG
231 	    firsttoken = bufptr;
232 	    if (debug)
233 		printf("created buffernumber %d with %d tokens\n",
234 			bufno, emptybuf->tok_count);
235 #endif DEBUG
236 	    goto top;
237 	}	/*end of reading/creating a new buffer*/
238 	tokptr = bufptr;		/*copy back the global value*/
239 	return(val);
240 }	/*end of yylex*/
241 
242 
243 buildskip(from, to)
244 	register	ptrall	from, to;
245 {
246 	int	diff;
247 	register	struct	tokbufdesc *middlebuf;
248 	/*
249 	 *	check if from and to are in the same buffer
250 	 *	from and to DIFFER BY AT MOST 1 buffer and to is
251 	 *	always ahead of from, with to being in the buffer emptybuf
252 	 *	points to.
253 	 *	The hard part here is accounting for the case where the
254 	 *	skip is to cross a buffer boundary; we must construct
255 	 *	two skips.
256 	 *
257 	 *	Figure out where the buffer boundary between from and to is
258 	 *	It's easy in VM, as buffers increase to high memory, but
259 	 *	w/o VM, we alternate between two buffers, and want
260 	 *	to look at the exact middle of the contiguous buffer region.
261 	 */
262 	middlebuf = useVM ? emptybuf : &tokbuf[1];
263 	if (  ( (bytetoktype *)from > (bytetoktype *)middlebuf)
264 	    ^ ( (bytetoktype *)to > (bytetoktype *)middlebuf)
265 	   ){	/*split across a buffer boundary*/
266 		ptoken(from, SKIP);
267 		/*
268 		 *	Set the skip so it lands someplace beyond
269 		 *	the end of this buffer.
270 		 *	When we pull this skip out in the second pass,
271 		 *	we will temporarily move the current pointer
272 		 *	out beyond the end of the buffer, but immediately
273 		 *	do a compare and fail the compare, and then reset
274 		 *	all the pointers correctly to point into the next buffer.
275 		 */
276 		bskiplg(from,  TOKBUFLG + 1);
277 		/*
278 		 *	Now, force from to be in the same buffer as to
279 		 */
280 		(bytetoktype *)from = (bytetoktype *)&(emptybuf->toks[0]);
281 	}
282 	/*
283 	 *	Now, to and from are in the same buffer
284 	 */
285 	if (from > to)
286 		yyerror("Internal error: bad skip construction");
287 	else {
288 		if ( (diff = (bytetoktype *)to - (bytetoktype *)from) >=
289 			(sizeof(bytetoktype) + sizeof(lgtype) + 1)) {
290 				ptoken(from, SKIP);
291 				bskipfromto(from, to);
292 		} else {
293 			for ( ; diff > 0; --diff)
294 				ptoken(from, VOID);
295 		}
296 	}
297 }
298 
299 movestr(to, from, lg)
300 	register	char	*to;
301 	register	char	*from;
302 	register	int	lg;
303 {
304 	if (lg <= 0)
305 		return;
306 	do
307 		*to++ = *from++;
308 	while (--lg);
309 }
310 
311 new_dot_s(namep)
312 	char	*namep;
313 {
314 	newfflag = 1;
315 	newfname = namep;
316 	dotsname = namep;
317 	lineno = 1;
318 	scanlineno = 1;
319 }
320 
321 min(a, b)
322 {
323 	return(a < b ? a : b);
324 }
325