1 /* This is the Assembler Pre-Processor
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 1996
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Modified by Allen Wirfs-Brock, Instantiations Inc 2/90 */
22 /* App, the assembler pre-processor. This pre-processor strips out excess
23 spaces, turns single-quoted characters into a decimal constant, and turns
24 # <number> <filename> <garbage> into a .line <number>\n.file <filename>
25 pair. This needs better error-handling. */
28 #include "as.h" /* For BAD_CASE() only */
32 #define const /* empty */
37 static const char symbol_chars[] =
38 "$._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
40 #define LEX_IS_SYMBOL_COMPONENT 1
41 #define LEX_IS_WHITESPACE 2
42 #define LEX_IS_LINE_SEPARATOR 3
43 #define LEX_IS_COMMENT_START 4
44 #define LEX_IS_LINE_COMMENT_START 5
45 #define LEX_IS_TWOCHAR_COMMENT_1ST 6
46 #define LEX_IS_TWOCHAR_COMMENT_2ND 7
47 #define LEX_IS_STRINGQUOTE 8
48 #define LEX_IS_COLON 9
49 #define LEX_IS_NEWLINE 10
50 #define LEX_IS_ONECHAR_QUOTE 11
51 #define IS_SYMBOL_COMPONENT(c) (lex[c] == LEX_IS_SYMBOL_COMPONENT)
52 #define IS_WHITESPACE(c) (lex[c] == LEX_IS_WHITESPACE)
53 #define IS_LINE_SEPARATOR(c) (lex[c] == LEX_IS_LINE_SEPARATOR)
54 #define IS_COMMENT(c) (lex[c] == LEX_IS_COMMENT_START)
55 #define IS_LINE_COMMENT(c) (lex[c] == LEX_IS_LINE_COMMENT_START)
56 #define IS_NEWLINE(c) (lex[c] == LEX_IS_NEWLINE)
58 static int process_escape PARAMS ((int));
60 /* FIXME-soon: The entire lexer/parser thingy should be
61 built statically at compile time rather than dynamically
62 each and every time the assembler is run. xoxorich. */
69 lex[' '] = LEX_IS_WHITESPACE;
70 lex['\t'] = LEX_IS_WHITESPACE;
71 lex['\n'] = LEX_IS_NEWLINE;
72 lex[';'] = LEX_IS_LINE_SEPARATOR;
73 lex[':'] = LEX_IS_COLON;
77 lex['"'] = LEX_IS_STRINGQUOTE;
80 lex['\''] = LEX_IS_ONECHAR_QUOTE;
83 #ifdef SINGLE_QUOTE_STRINGS
84 lex['\''] = LEX_IS_STRINGQUOTE;
88 /* Note: if any other character can be LEX_IS_STRINGQUOTE, the loop
89 in state 5 of do_scrub_chars must be changed. */
91 /* Note that these override the previous defaults, e.g. if ';' is a
92 comment char, then it isn't a line separator. */
93 for (p = symbol_chars; *p; ++p)
95 lex[(unsigned char) *p] = LEX_IS_SYMBOL_COMPONENT;
96 } /* declare symbol characters */
98 for (p = comment_chars; *p; p++)
100 lex[(unsigned char) *p] = LEX_IS_COMMENT_START;
101 } /* declare comment chars */
103 for (p = line_comment_chars; *p; p++)
105 lex[(unsigned char) *p] = LEX_IS_LINE_COMMENT_START;
106 } /* declare line comment chars */
108 for (p = line_separator_chars; *p; p++)
110 lex[(unsigned char) *p] = LEX_IS_LINE_SEPARATOR;
111 } /* declare line separators */
113 /* Only allow slash-star comments if slash is not in use */
116 lex['/'] = LEX_IS_TWOCHAR_COMMENT_1ST;
118 /* FIXME-soon. This is a bad hack but otherwise, we can't do
119 c-style comments when '/' is a line comment char. xoxorich. */
122 lex['*'] = LEX_IS_TWOCHAR_COMMENT_2ND;
127 lex['\''] = LEX_IS_STRINGQUOTE;
128 lex[';'] = LEX_IS_COMMENT_START;
129 lex['*'] = LEX_IS_LINE_COMMENT_START;
130 /* The MRI documentation says '!' is LEX_IS_COMMENT_START, but
131 then it can't be used in an expression. */
132 lex['!'] = LEX_IS_LINE_COMMENT_START;
134 } /* do_scrub_begin() */
136 /* Saved state of the scrubber */
138 static int old_state;
139 static char *out_string;
140 static char out_buf[20];
141 static int add_newlines;
142 static char *saved_input;
143 static int saved_input_len;
145 /* Data structure for saving the state of app across #include's. Note that
146 app is called asynchronously to the parsing of the .include's, so our
147 state at the time .include is interpreted is completely unrelated.
148 That's why we have to save it all. */
155 char out_buf[sizeof (out_buf)];
164 register struct app_save *saved;
166 saved = (struct app_save *) xmalloc (sizeof (*saved));
167 saved->state = state;
168 saved->old_state = old_state;
169 saved->out_string = out_string;
170 memcpy (saved->out_buf, out_buf, sizeof (out_buf));
171 saved->add_newlines = add_newlines;
172 saved->saved_input = saved_input;
173 saved->saved_input_len = saved_input_len;
175 /* do_scrub_begin() is not useful, just wastes time. */
180 return (char *) saved;
187 register struct app_save *saved = (struct app_save *) arg;
189 /* There is no do_scrub_end (). */
190 state = saved->state;
191 old_state = saved->old_state;
192 out_string = saved->out_string;
193 memcpy (out_buf, saved->out_buf, sizeof (out_buf));
194 add_newlines = saved->add_newlines;
195 saved_input = saved->saved_input;
196 saved_input_len = saved->saved_input_len;
201 /* @@ This assumes that \n &c are the same on host and target. This is not
228 /* This function is called to process input characters. The GET
229 parameter is used to retrieve more input characters. GET should
230 set its parameter to point to a buffer, and return the length of
231 the buffer; it should return 0 at end of file. The scrubbed output
232 characters are put into the buffer starting at TOSTART; the TOSTART
233 buffer is TOLEN bytes in length. The function returns the number
234 of scrubbed characters put into TOSTART. This will be TOLEN unless
235 end of file was seen. This function is arranged as a state
236 machine, and saves its state so that it may return at any point.
237 This is the way the old code used to work. */
240 do_scrub_chars (get, tostart, tolen)
241 int (*get) PARAMS ((char **));
246 char *toend = tostart + tolen;
250 register int ch, ch2 = 0;
251 int not_cpp_line = 0;
253 /*State 0: beginning of normal line
254 1: After first whitespace on line (flush more white)
255 2: After first non-white (opcode) on line (keep 1white)
256 3: after second white on line (into operands) (flush white)
257 4: after putting out a .line, put out digits
258 5: parsing a string, then go to old-state
259 6: putting out \ escape in a "d string.
260 7: After putting out a .appfile, put out string.
261 8: After putting out a .appfile string, flush until newline.
262 9: After seeing symbol char in state 3 (keep 1white after symchar)
263 10: After seeing whitespace in state 9 (keep white before symchar)
264 11: After seeing a symbol character in state 0 (eg a label definition)
265 -1: output string in out_string and go to the state in old_state
266 -2: flush text until a '*' '/' is seen, then go to state old_state
269 /* I added states 9 and 10 because the MIPS ECOFF assembler uses
270 constructs like ``.loc 1 20''. This was turning into ``.loc
271 120''. States 9 and 10 ensure that a space is never dropped in
272 between characters which could appear in a identifier. Ian
275 I added state 11 so that something like "Lfoo add %r25,%r26,%r27" works
276 correctly on the PA (and any other target where colons are optional).
279 /* This macro gets the next input character. */
284 : ((saved_input != NULL \
285 ? (free (saved_input), \
286 saved_input = NULL, \
289 fromlen = (*get) (&from), \
290 fromend = from + fromlen, \
295 /* This macro pushes a character back on the input stream. */
297 #define UNGET(uch) (*--from = (uch))
299 /* This macro puts a character into the output buffer. If this
300 character fills the output buffer, this macro jumps to the label
301 TOFULL. We use this rather ugly approach because we need to
302 handle two different termination conditions: EOF on the input
303 stream, and a full output buffer. It would be simpler if we
304 always read in the entire input stream before processing it, but
305 I don't want to make such a significant change to the assembler's
317 if (saved_input != NULL)
320 fromend = from + saved_input_len;
324 fromlen = (*get) (&from);
327 fromend = from + fromlen;
332 /* The cases in this switch end with continue, in order to
333 branch back to the top of this while loop and generate the
334 next output character in the appropriate state. */
339 if (*out_string == '\0')
356 as_warn ("end of file in comment");
365 while ((ch = GET ()) == '*')
370 as_warn ("end of file in comment");
388 else if (ch >= '0' && ch <= '9')
392 while (ch != EOF && IS_WHITESPACE (ch))
398 out_string = "\n\tappfile ";
400 out_string = "\n\t.appfile ";
407 while (ch != EOF && ch != '\n')
416 /* We are going to copy everything up to a quote character,
417 with special handling for a backslash. We try to
418 optimize the copying in the simple case without using the
419 GET and PUT macros. */
424 for (s = from; s < fromend; s++)
427 /* This condition must be changed if the type of any
428 other character can be LEX_IS_STRINGQUOTE. */
436 if (len > toend - to)
440 memcpy (to, from, len);
449 as_warn ("end of file in string: inserted '\"'");
454 else if (lex[ch] == LEX_IS_STRINGQUOTE)
459 #ifndef NO_STRING_ESCAPES
466 else if (flag_m68k_mri && ch == '\n')
468 /* Just quietly terminate the string. This permits lines like
469 bne label loop if we haven't reach end yet
486 /* Handle strings broken across lines, by turning '\n' into
513 #if defined(IGNORE_NONSTANDARD_ESCAPES) | defined(ONLY_STANDARD_ESCAPES)
515 as_warn ("Unknown escape '\\%c' in string: Ignored", ch);
517 #else /* ONLY_STANDARD_ESCAPES */
519 /* Accept \x as x for any x */
521 #endif /* ONLY_STANDARD_ESCAPES */
524 as_warn ("End of file in string: '\"' inserted");
543 while (ch != '\n' && ch != EOF);
551 /* OK, we are somewhere in states 0 through 4 or 9 through 11 */
560 as_warn ("end of file not at end of a line; newline inserted");
569 case LEX_IS_WHITESPACE:
574 while (ch != EOF && IS_WHITESPACE (ch));
580 /* Preserve a single whitespace character at the
581 beginning of a line. */
590 || IS_LINE_SEPARATOR (ch))
592 /* cpp never outputs a leading space before the #, so
593 try to avoid being confused. */
597 /* In MRI mode, we keep these spaces. */
605 /* If we're in state 2 or 11, we've seen a non-white
606 character followed by whitespace. If the next character
607 is ':', this is whitespace after a label name which we
608 normally must ignore. In MRI mode, though, spaces are
609 not permitted between the label and the colon. */
610 if ((state == 2 || state == 11)
611 && lex[ch] == LEX_IS_COLON
623 goto recycle; /* Punted leading sp */
625 /* We can arrive here if we leave a leading whitespace
626 character at the beginning of a line. */
632 /* Optimize common case by skipping UNGET/GET. */
633 PUT (' '); /* Sp after opco */
642 /* In MRI mode, we keep these spaces. */
647 goto recycle; /* Sp in operands */
652 /* In MRI mode, we keep these spaces. */
658 state = 10; /* Sp after symbol char */
663 PUT (' '); /* Sp after label definition. */
670 case LEX_IS_TWOCHAR_COMMENT_1ST:
672 if (ch2 != EOF && lex[ch2] == LEX_IS_TWOCHAR_COMMENT_2ND)
679 if (ch2 != EOF && IS_NEWLINE (ch2))
683 (lex[ch2] != LEX_IS_TWOCHAR_COMMENT_2ND));
686 (lex[ch2] == LEX_IS_TWOCHAR_COMMENT_2ND))
692 || lex[ch2] == LEX_IS_TWOCHAR_COMMENT_1ST)
697 as_warn ("end of file in multiline comment");
706 if (state == 9 || state == 10)
712 case LEX_IS_STRINGQUOTE:
715 /* Preserve the whitespace in foo "bar" */
720 /* PUT didn't jump out. We could just break, but we
721 know what will happen, so optimize a bit. */
734 case LEX_IS_ONECHAR_QUOTE:
737 /* Preserve the whitespace in foo 'b' */
746 as_warn ("end of file after a one-character quote; \\0 inserted");
754 as_warn ("end of file in escape character");
758 ch = process_escape (ch);
760 sprintf (out_buf, "%d", (int) (unsigned char) ch);
762 /* None of these 'x constants for us. We want 'x'. */
763 if ((ch = GET ()) != '\'')
765 #ifdef REQUIRE_CHAR_CLOSE_QUOTE
766 as_warn ("Missing close quote: (assumed)");
772 if (strlen (out_buf) == 1)
782 out_string = out_buf;
788 if (state == 9 || state == 10)
796 /* Roll out a bunch of newlines from inside comments, etc. */
802 /* fall thru into... */
804 case LEX_IS_LINE_SEPARATOR:
809 case LEX_IS_LINE_COMMENT_START:
810 if (state == 0) /* Only comment at start of line. */
812 /* FIXME-someday: The two character comment stuff was
813 badly thought out. On i386, we want '/' as line
814 comment start AND we want C style comments. hence
815 this hack. The whole lexical process should be
816 reworked. xoxorich. */
838 while (ch != EOF && IS_WHITESPACE (ch));
841 as_warn ("end of file in comment; newline inserted");
845 if (ch < '0' || ch > '9' || not_cpp_line)
847 /* Non-numerics: Eat whole comment line */
848 while (ch != EOF && !IS_NEWLINE (ch))
851 as_warn ("EOF in Comment: Newline inserted");
856 /* Numerics begin comment. Perhaps CPP `# 123 "filename"' */
861 out_string = "\tappline ";
863 out_string = "\t.appline ";
868 /* We have a line comment character which is not at the
869 start of a line. If this is also a normal comment
870 character, fall through. Otherwise treat it as a default
872 if (strchr (comment_chars, ch) == NULL
874 || (ch != '!' && ch != '*')))
877 && (ch == '!' || ch == '*' || ch == '#')
882 case LEX_IS_COMMENT_START:
887 while (ch != EOF && !IS_NEWLINE (ch));
889 as_warn ("end of file in comment; newline inserted");
894 case LEX_IS_SYMBOL_COMPONENT:
897 /* This is a symbol character following another symbol
898 character, with whitespace in between. We skipped
899 the whitespace earlier, so output it now. */
909 /* This is a common case. Quickly copy CH and all the
910 following symbol component or normal characters. */
916 for (s = from; s < fromend; s++)
923 && type != LEX_IS_SYMBOL_COMPONENT)
928 /* Handle the last character normally, for
933 if (len > (toend - to) - 1)
934 len = (toend - to) - 1;
940 memcpy (to, from, len);
948 case 8: *to++ = *from++;
949 case 7: *to++ = *from++;
950 case 6: *to++ = *from++;
951 case 5: *to++ = *from++;
952 case 4: *to++ = *from++;
953 case 3: *to++ = *from++;
954 case 2: *to++ = *from++;
955 case 1: *to++ = *from++;
965 /* Some relatively `normal' character. */
968 state = 11; /* Now seeing label definition */
972 state = 2; /* Ditto */
976 if (lex[ch] != LEX_IS_SYMBOL_COMPONENT)
979 else if (state == 10)
991 /* We have reached the end of the input. */
995 /* The output buffer is full. Save any input we have not yet
1001 save = (char *) xmalloc (fromend - from);
1002 memcpy (save, from, fromend - from);
1003 if (saved_input != NULL)
1006 saved_input_len = fromend - from;
1010 if (saved_input != NULL)
1016 return to - tostart;