3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 * 28-Oct-97 JLH bug in getst(): sign extend on ~(SPACE|ILL)
21 * causes infinite loop
25 * Extensions: P. Felber, M. Hope
31 #include "dbuf_string.h"
36 * The module aslex.c includes the general lexical
37 * analysis routines for the assembler.
39 * aslex.c contains the following functions:
50 * aslex.c contains no local/static variables
53 /*)Function VOID getid(id,c)
55 * char * id a pointer to a string of
58 * >=0 this is first character to
59 * copy to the string buffer
60 * <0 skip white space, first
61 * character must be a LETTER
63 * The function getid() scans the current assembler-source text line
64 * from the current position copying the next LETTER | DIGIT string
65 * into the external string buffer (id). The string ends when a non
66 * LETTER or DIGIT character is found. The maximum number of
67 * characters copied is NCPS. If the input string is larger than
68 * NCPS characters then the string is truncated, if the input string
69 * is shorter than NCPS characters then the string is NULL filled.
70 * If the mode argument (c) is >=0 then (c) is the first character
71 * copied to the string buffer, if (c) is <0 then intervening white
72 * space (SPACES and TABS) are skipped and the first character found
73 * must be a LETTER else a 'q' error terminates the parse of this
74 * assembler-source text line.
77 * char * p pointer to external string buffer
78 * int c current character value
81 * char ctype[] a character array which defines the
82 * type of character being processed.
83 * This index is the character
88 * char getnb() aslex.c
89 * VOID unget() aslex.c
92 * use of getnb(), get(), and unget() updates the
93 * global pointer ip, the position in the current
94 * assembler-source text line.
106 if ((ctype[c] & LETTER) == 0)
113 } while (ctype[c=get()] & (LETTER|DIGIT));
115 while (p < &id[NCPS])
119 /*)Function VOID getst(id,c)
121 * char * id a pointer to a string of
122 * maximum length NCPS
124 * >=0 this is first character to
125 * copy to the string buffer
126 * <0 skip white space, first
127 * character must be a LETTER
129 * The function getnbid() scans the current assembler-source text line
130 * from the current position copying the next character string into
131 * the external string buffer (id). The string ends when a SPACE or
132 * ILL character is found. The maximum number of
133 * characters copied is NCPS. If the input string is larger than
134 * NCPS characters then the string is truncated, if the input string
135 * is shorter than NCPS characters then the string is NULL filled.
136 * If the mode argument (c) is >=0 then (c) is the first character
137 * copied to the string buffer, if (c) is <0 then intervening white
138 * space (SPACES and TABS) are skipped and the first character found
139 * must be a LETTER else a 'q' error terminates the parse of this
140 * assembler-source text line.
143 * char * p pointer to external string buffer
144 * int c current character value
147 * char ctype[] a character array which defines the
148 * type of character being processed.
149 * This index is the character
154 * char getnb() aslex.c
155 * VOID unget() aslex.c
158 * use of getnb(), get(), and unget() updates the
159 * global pointer ip, the position in the current
160 * assembler-source text line.
172 if ((ctype[c] & LETTER) == 0)
179 } while (ctype[c=get()] & (0xFF - (SPACE|ILL)));
181 while (p < &id[NCPS])
185 /*)Function char getnb()
187 * The function getnb() scans the current assembler-source
188 * text line returning the first character not a SPACE or TAB.
191 * int c current character from
192 * assembler-source text line
201 * use of get() updates the global pointer ip, the position
202 * in the current assembler-source text line
210 while ((c=get()) == ' ' || c == '\t')
215 /*)Function char get()
217 * The function get() returns the next character in the
218 * assembler-source text line, at the end of the line a
219 * NULL character is returned.
222 * int c current character from
223 * assembler-source text line
226 * char * ip pointer into the current
227 * assembler-source text line
233 * updates ip to the next character position in the
234 * assembler-source text line. If ip is at the end of the
235 * line, ip is not updated.
248 /*)Function VOID unget(c)
250 * int c value of last character read from
251 * assembler-source text line
253 * If (c) is not a NULL character then the global pointer ip
254 * is updated to point to the preceeding character in the
255 * assembler-source text line.
257 * NOTE: This function does not push the character (c)
258 * back into the assembler-source text line, only
259 * the pointer ip is changed.
262 * int c last character read from
263 * assembler-source text line
266 * char * ip position into the current
267 * assembler-source text line
273 * ip decremented by 1 character position
284 /*)Function int getmap(d)
286 * int d value to compare with the
287 * assembler-source text line character
289 * The function getmap() converts the 'C' style characters \b, \f,
290 * \n, \r, and \t to their equivalent ascii values and also
291 * converts 'C' style octal constants '\123' to their equivalent
292 * numeric values. If the first character is equivalent to (d) then
293 * a (-1) is returned, if the end of the line is detected then
294 * a 'q' error terminates the parse for this line, or if the first
295 * character is not a \ then the character value is returned.
298 * int c value of character from the
299 * assembler-source text line
300 * int n looping counter
301 * int v current value of numeric conversion
310 * use of get() updates the global pointer ip the position
311 * in the current assembler-source text line
317 register int c, n, v;
319 if ((c=get()) == '\0')
357 while (++n<=3 && c>='0' && c<='7') {
358 v = (v<<3) + c - '0';
369 /*)Function int as_getline()
371 * The function as_getline() reads a line of assembler-source text
372 * from an assembly source text file or an include file.
373 * Lines of text are processed from assembler-source files until
374 * all files have been read. If an include file is opened then
375 * lines of text are read from the include file (or nested
376 * include file) until the end of the include file is found.
377 * The input text line is copied into the global string ib[]
378 * and converted to a NULL terminated string. The function
379 * as_getline() returns a (1) after succesfully reading a line
380 * or a (0) if all files have been read.
383 * int i string length
386 * const char ib[] string buffer containing
387 * assembler-source text line
388 * char ifp[] array of file handles for
390 * int incfil index for ifp[] specifies
391 * active include file
392 * int incline[] array of include file
394 * char sfp[] array of file handles for
395 * assembler source files
396 * int cfile index for sfp[] specifies
398 * int srcline[] array of source file
400 * int inpfil maximum input file index
404 * int dbuf_set_length()
406 * const char * dbuf_c_str()
407 * int fclose() c-library
408 * char * fgets() c-library
409 * int strlen() c-library
412 * include file will be closed at detection of end of file.
413 * the next sequential source file may be selected.
414 * the global file indexes incfil or cfile may be changed.
415 * The respective source line or include line counter
422 static struct dbuf_s dbuf;
423 static char dbufInitialized = 0;
426 if (!dbufInitialized)
428 dbuf_init (&dbuf, 1024);
432 dbuf_set_length (&dbuf, 0);
437 if ((len = dbuf_getline (&dbuf, ifp[incfil])) == 0)
439 fclose (ifp[incfil]);
440 ifp[incfil--] = NULL;
451 if ((len = dbuf_getline (&dbuf, sfp[cfile])) == 0)
453 if (++cfile <= inpfil)
465 ib = dbuf_c_str (&dbuf);
467 /* remove the trailing NL */
468 if (len > 0 && '\n' == ib[len - 1])
471 if (len > 0 && '\r' == ib[len - 1])
473 dbuf_set_length (&dbuf, len);
474 ib = dbuf_c_str (&dbuf);
480 /*)Function int more()
482 * The function more() scans the assembler-source text line
483 * skipping white space (SPACES and TABS) and returns a (0)
484 * if the end of the line or a comment delimeter (;) is found,
485 * or a (1) if their are additional characters in the line.
488 * int c next character from the
489 * assembler-source text line
495 * char getnb() aslex.c
496 * VOID unget() aslex.c
499 * use of getnb() and unget() updates the global pointer ip
500 * the position in the current assembler-source text line
510 return( (c == '\0' || c == ';') ? 0 : 1 );
513 /*)Function char endline()
515 * The function endline() scans the assembler-source text line
516 * skipping white space (SPACES and TABS) and returns the next
517 * character or a (0) if the end of the line is found or a
518 * comment delimiter (;) is found.
521 * int c next character from the
522 * assembler-source text line
528 * char getnb() aslex.c
531 * use of getnb() updates the global pointer ip the
532 * position in the current assembler-source text line
541 return( (c == '\0' || c == ';') ? 0 : c );