1 /* This is the Assembler Pre-Processor
2 Copyright (C) 1987, 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS 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 GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Modified by Allen Wirfs-Brock, Instantiations Inc 2/90 */
21 /* App, the assembler pre-processor. This pre-processor strips out excess
22 spaces, turns single-quoted characters into a decimal constant, and turns
23 # <number> <filename> <garbage> into a .line <number>\n.file <filename>
24 pair. This needs better error-handling. */
27 #include "as.h" /* For BAD_CASE() only */
31 #define const /* empty */
36 static const char symbol_chars
[] =
37 "$._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
39 #define LEX_IS_SYMBOL_COMPONENT 1
40 #define LEX_IS_WHITESPACE 2
41 #define LEX_IS_LINE_SEPARATOR 3
42 #define LEX_IS_COMMENT_START 4
43 #define LEX_IS_LINE_COMMENT_START 5
44 #define LEX_IS_TWOCHAR_COMMENT_1ST 6
45 #define LEX_IS_TWOCHAR_COMMENT_2ND 7
46 #define LEX_IS_STRINGQUOTE 8
47 #define LEX_IS_COLON 9
48 #define LEX_IS_NEWLINE 10
49 #define LEX_IS_ONECHAR_QUOTE 11
50 #define IS_SYMBOL_COMPONENT(c) (lex[c] == LEX_IS_SYMBOL_COMPONENT)
51 #define IS_WHITESPACE(c) (lex[c] == LEX_IS_WHITESPACE)
52 #define IS_LINE_SEPARATOR(c) (lex[c] == LEX_IS_LINE_SEPARATOR)
53 #define IS_COMMENT(c) (lex[c] == LEX_IS_COMMENT_START)
54 #define IS_LINE_COMMENT(c) (lex[c] == LEX_IS_LINE_COMMENT_START)
55 #define IS_NEWLINE(c) (lex[c] == LEX_IS_NEWLINE)
57 static int process_escape
PARAMS ((int));
59 /* FIXME-soon: The entire lexer/parser thingy should be
60 built statically at compile time rather than dynamically
61 each and every time the assembler is run. xoxorich. */
68 lex
[' '] = LEX_IS_WHITESPACE
;
69 lex
['\t'] = LEX_IS_WHITESPACE
;
70 lex
['\n'] = LEX_IS_NEWLINE
;
71 lex
[';'] = LEX_IS_LINE_SEPARATOR
;
72 lex
[':'] = LEX_IS_COLON
;
76 lex
['"'] = LEX_IS_STRINGQUOTE
;
79 lex
['\''] = LEX_IS_ONECHAR_QUOTE
;
82 #ifdef SINGLE_QUOTE_STRINGS
83 lex
['\''] = LEX_IS_STRINGQUOTE
;
87 /* Note that these override the previous defaults, e.g. if ';' is a
88 comment char, then it isn't a line separator. */
89 for (p
= symbol_chars
; *p
; ++p
)
91 lex
[(unsigned char) *p
] = LEX_IS_SYMBOL_COMPONENT
;
92 } /* declare symbol characters */
94 for (p
= comment_chars
; *p
; p
++)
96 lex
[(unsigned char) *p
] = LEX_IS_COMMENT_START
;
97 } /* declare comment chars */
99 for (p
= line_comment_chars
; *p
; p
++)
101 lex
[(unsigned char) *p
] = LEX_IS_LINE_COMMENT_START
;
102 } /* declare line comment chars */
104 for (p
= line_separator_chars
; *p
; p
++)
106 lex
[(unsigned char) *p
] = LEX_IS_LINE_SEPARATOR
;
107 } /* declare line separators */
109 /* Only allow slash-star comments if slash is not in use */
112 lex
['/'] = LEX_IS_TWOCHAR_COMMENT_1ST
;
114 /* FIXME-soon. This is a bad hack but otherwise, we can't do
115 c-style comments when '/' is a line comment char. xoxorich. */
118 lex
['*'] = LEX_IS_TWOCHAR_COMMENT_2ND
;
123 lex
['\''] = LEX_IS_STRINGQUOTE
;
124 lex
[';'] = LEX_IS_COMMENT_START
;
125 lex
['*'] = LEX_IS_LINE_COMMENT_START
;
126 /* The MRI documentation says '!' is LEX_IS_COMMENT_START, but
127 then it can't be used in an expression. */
128 lex
['!'] = LEX_IS_LINE_COMMENT_START
;
130 } /* do_scrub_begin() */
137 return getc (scrub_file
);
144 ungetc (ch
, scrub_file
);
145 } /* scrub_to_file() */
148 char *scrub_last_string
;
153 return scrub_string
== scrub_last_string
? EOF
: *scrub_string
++;
154 } /* scrub_from_string() */
160 *--scrub_string
= ch
;
161 } /* scrub_to_string() */
163 /* Saved state of the scrubber */
165 static int old_state
;
166 static char *out_string
;
167 static char out_buf
[20];
168 static int add_newlines
= 0;
170 /* Data structure for saving the state of app across #include's. Note that
171 app is called asynchronously to the parsing of the .include's, so our
172 state at the time .include is interpreted is completely unrelated.
173 That's why we have to save it all. */
180 char out_buf
[sizeof (out_buf
)];
183 char *scrub_last_string
;
190 register struct app_save
*saved
;
192 saved
= (struct app_save
*) xmalloc (sizeof (*saved
));
193 saved
->state
= state
;
194 saved
->old_state
= old_state
;
195 saved
->out_string
= out_string
;
196 memcpy (saved
->out_buf
, out_buf
, sizeof (out_buf
));
197 saved
->add_newlines
= add_newlines
;
198 saved
->scrub_string
= scrub_string
;
199 saved
->scrub_last_string
= scrub_last_string
;
200 saved
->scrub_file
= scrub_file
;
202 /* do_scrub_begin() is not useful, just wastes time. */
203 return (char *) saved
;
210 register struct app_save
*saved
= (struct app_save
*) arg
;
212 /* There is no do_scrub_end (). */
213 state
= saved
->state
;
214 old_state
= saved
->old_state
;
215 out_string
= saved
->out_string
;
216 memcpy (out_buf
, saved
->out_buf
, sizeof (out_buf
));
217 add_newlines
= saved
->add_newlines
;
218 scrub_string
= saved
->scrub_string
;
219 scrub_last_string
= saved
->scrub_last_string
;
220 scrub_file
= saved
->scrub_file
;
225 /* @@ This assumes that \n &c are the same on host and target. This is not
252 do_scrub_next_char (get
, unget
)
256 /*State 0: beginning of normal line
257 1: After first whitespace on line (flush more white)
258 2: After first non-white (opcode) on line (keep 1white)
259 3: after second white on line (into operands) (flush white)
260 4: after putting out a .line, put out digits
261 5: parsing a string, then go to old-state
262 6: putting out \ escape in a "d string.
263 7: After putting out a .appfile, put out string.
264 8: After putting out a .appfile string, flush until newline.
265 9: After seeing symbol char in state 3 (keep 1white after symchar)
266 10: After seeing whitespace in state 9 (keep white before symchar)
267 11: After seeing a symbol character in state 0 (eg a label definition)
268 -1: output string in out_string and go to the state in old_state
269 -2: flush text until a '*' '/' is seen, then go to state old_state
272 /* I added states 9 and 10 because the MIPS ECOFF assembler uses
273 constructs like ``.loc 1 20''. This was turning into ``.loc
274 120''. States 9 and 10 ensure that a space is never dropped in
275 between characters which could appear in a identifier. Ian
276 Taylor, ian@cygnus.com.
278 I added state 11 so that something like "Lfoo add %r25,%r26,%r27" works
279 correctly on the PA (and any other target where colons are optional).
280 Jeff Law, law@cs.utah.edu. */
282 /* This is purely an optimization hack, and relies on gcc's inlining
284 #if defined (__GNUC__) && defined (__OPTIMIZE__)
285 #define GET() (get == scrub_from_file ? scrub_from_file () : (*get) ())
287 #define GET() ((*get) ())
290 register int ch
, ch2
= 0;
291 int not_cpp_line
= 0;
297 if (*out_string
== 0)
311 while (ch
!= EOF
&& ch
!= '\n' && ch
!= '*');
312 if (ch
== '\n' || ch
== EOF
)
315 /* At this point, ch must be a '*' */
316 while ((ch
= GET ()) == '*')
320 if (ch
== EOF
|| ch
== '/')
329 if (ch
== EOF
|| (ch
>= '0' && ch
<= '9'))
333 while (ch
!= EOF
&& IS_WHITESPACE (ch
))
338 out_string
= "\n\t.appfile ";
341 return *out_string
++;
345 while (ch
!= EOF
&& ch
!= '\n')
354 if (lex
[ch
] == LEX_IS_STRINGQUOTE
)
359 #ifndef NO_STRING_ESCAPES
368 as_warn ("End of file in string: inserted '\"'");
383 /* Handle strings broken across lines, by turning '\n' into
409 #if defined(IGNORE_NONSTANDARD_ESCAPES) | defined(ONLY_STANDARD_ESCAPES)
411 as_warn ("Unknown escape '\\%c' in string: Ignored", ch
);
413 #else /* ONLY_STANDARD_ESCAPES */
415 /* Accept \x as x for any x */
417 #endif /* ONLY_STANDARD_ESCAPES */
420 as_warn ("End of file in string: '\"' inserted");
439 /* OK, we are somewhere in states 0 through 4 or 9 through 11 */
448 as_warn ("End of file not at end of a line: Newline inserted.");
457 case LEX_IS_WHITESPACE
:
459 /* Preserve a single whitespace character at the beginning of
468 while (ch
!= EOF
&& IS_WHITESPACE (ch
));
473 || (state
== 0 && IS_LINE_COMMENT (ch
))
475 || IS_LINE_SEPARATOR (ch
))
477 /* cpp never outputs a leading space before the #, so try to
478 avoid being confused. */
483 /* If we're in state 2 or 11, we've seen a non-white character
484 followed by whitespace. If the next character is ':', this
485 is whitespace after a label name which we normally must
486 ignore. In MRI mode, though, spaces are not permitted
487 between the label and the colon. */
488 if ((state
== 2 || state
== 11)
489 && lex
[ch
] == LEX_IS_COLON
500 goto recycle
; /* Punted leading sp */
502 /* We can arrive here if we leave a leading whitespace character
503 at the beginning of a line. */
508 return ' '; /* Sp after opco */
510 goto recycle
; /* Sp in operands */
513 state
= 10; /* Sp after symbol char */
518 return ' '; /* Sp after label definition. */
524 case LEX_IS_TWOCHAR_COMMENT_1ST
:
526 if (ch2
!= EOF
&& lex
[ch2
] == LEX_IS_TWOCHAR_COMMENT_2ND
)
533 if (ch2
!= EOF
&& IS_NEWLINE (ch2
))
537 (lex
[ch2
] != LEX_IS_TWOCHAR_COMMENT_2ND
));
540 (lex
[ch2
] == LEX_IS_TWOCHAR_COMMENT_2ND
))
546 || lex
[ch2
] == LEX_IS_TWOCHAR_COMMENT_1ST
)
551 as_warn ("End of file in multiline comment");
560 if (state
== 9 || state
== 10)
566 case LEX_IS_STRINGQUOTE
:
569 /* Preserve the whitespace in foo "bar" */
581 case LEX_IS_ONECHAR_QUOTE
:
584 /* Preserve the whitespace in foo 'b' */
592 as_warn ("End-of-file after a one-character quote; \\000 inserted");
598 ch
= process_escape (ch
);
600 sprintf (out_buf
, "%d", (int) (unsigned char) ch
);
603 /* None of these 'x constants for us. We want 'x'. */
604 if ((ch
= GET ()) != '\'')
606 #ifdef REQUIRE_CHAR_CLOSE_QUOTE
607 as_warn ("Missing close quote: (assumed)");
612 if (strlen (out_buf
) == 1)
621 out_string
= out_buf
;
622 return *out_string
++;
625 if (state
== 9 || state
== 10)
632 /* Roll out a bunch of newlines from inside comments, etc. */
638 /* fall thru into... */
640 case LEX_IS_LINE_SEPARATOR
:
644 case LEX_IS_LINE_COMMENT_START
:
645 if (state
== 0) /* Only comment at start of line. */
647 /* FIXME-someday: The two character comment stuff was badly
648 thought out. On i386, we want '/' as line comment start
649 AND we want C style comments. hence this hack. The
650 whole lexical process should be reworked. xoxorich. */
657 return (do_scrub_next_char (get
, unget
));
670 while (ch
!= EOF
&& IS_WHITESPACE (ch
));
673 as_warn ("EOF in comment: Newline inserted");
676 if (ch
< '0' || ch
> '9' || not_cpp_line
)
678 /* Non-numerics: Eat whole comment line */
679 while (ch
!= EOF
&& !IS_NEWLINE (ch
))
682 as_warn ("EOF in Comment: Newline inserted");
686 /* Numerics begin comment. Perhaps CPP `# 123 "filename"' */
690 out_string
= "\t.appline ";
691 return *out_string
++;
694 /* We have a line comment character which is not at the start of
695 a line. If this is also a normal comment character, fall
696 through. Otherwise treat it as a default character. */
697 if ((flag_mri
&& (ch
== '!' || ch
== '*'))
698 || strchr (comment_chars
, ch
) == NULL
)
701 case LEX_IS_COMMENT_START
:
704 while (ch
!= EOF
&& !IS_NEWLINE (ch
));
706 as_warn ("EOF in comment: Newline inserted");
710 case LEX_IS_SYMBOL_COMPONENT
:
713 /* This is a symbol character following another symbol
714 character, with whitespace in between. We skipped the
715 whitespace earlier, so output it now. */
725 /* Some relatively `normal' character. */
728 state
= 11; /* Now seeing label definition */
733 state
= 2; /* Ditto */
738 if (lex
[ch
] != LEX_IS_SYMBOL_COMPONENT
)
742 else if (state
== 10)
749 return ch
; /* Opcode or operands already */
759 const char comment_chars
[] = "|";
760 const char line_comment_chars
[] = "#";
767 while ((ch
= do_scrub_next_char (stdin
)) != EOF
)