1 /* This is the Assembler Pre-Processor
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Modified by Allen Wirfs-Brock, Instantiations Inc 2/90 */
23 /* App, the assembler pre-processor. This pre-processor strips out excess
24 spaces, turns single-quoted characters into a decimal constant, and turns
25 # <number> <filename> <garbage> into a .line <number>\n.file <filename>
26 pair. This needs better error-handling. */
29 #include "as.h" /* For BAD_CASE() only */
33 #define const /* empty */
37 /* Whether we are scrubbing in m68k MRI mode. This is different from
38 flag_m68k_mri, because the two flags will be affected by the .mri
39 pseudo-op at different times. */
40 static int scrub_m68k_mri;
42 /* The pseudo-op which switches in and out of MRI mode. See the
43 comment in do_scrub_chars. */
44 static const char mri_pseudo[] = ".mri 0";
47 static const char symbol_chars[] =
48 "$._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
50 #define LEX_IS_SYMBOL_COMPONENT 1
51 #define LEX_IS_WHITESPACE 2
52 #define LEX_IS_LINE_SEPARATOR 3
53 #define LEX_IS_COMMENT_START 4
54 #define LEX_IS_LINE_COMMENT_START 5
55 #define LEX_IS_TWOCHAR_COMMENT_1ST 6
56 #define LEX_IS_STRINGQUOTE 8
57 #define LEX_IS_COLON 9
58 #define LEX_IS_NEWLINE 10
59 #define LEX_IS_ONECHAR_QUOTE 11
61 #define LEX_IS_DOUBLEDASH_1ST 12
64 #define LEX_IS_DOUBLEBAR_1ST 13
66 #define IS_SYMBOL_COMPONENT(c) (lex[c] == LEX_IS_SYMBOL_COMPONENT)
67 #define IS_WHITESPACE(c) (lex[c] == LEX_IS_WHITESPACE)
68 #define IS_LINE_SEPARATOR(c) (lex[c] == LEX_IS_LINE_SEPARATOR)
69 #define IS_COMMENT(c) (lex[c] == LEX_IS_COMMENT_START)
70 #define IS_LINE_COMMENT(c) (lex[c] == LEX_IS_LINE_COMMENT_START)
71 #define IS_NEWLINE(c) (lex[c] == LEX_IS_NEWLINE)
73 static int process_escape PARAMS ((int));
75 /* FIXME-soon: The entire lexer/parser thingy should be
76 built statically at compile time rather than dynamically
77 each and every time the assembler is run. xoxorich. */
80 do_scrub_begin (m68k_mri)
85 scrub_m68k_mri = m68k_mri;
87 lex[' '] = LEX_IS_WHITESPACE;
88 lex['\t'] = LEX_IS_WHITESPACE;
89 lex['\r'] = LEX_IS_WHITESPACE;
90 lex['\n'] = LEX_IS_NEWLINE;
91 lex[';'] = LEX_IS_LINE_SEPARATOR;
92 lex[':'] = LEX_IS_COLON;
96 lex['"'] = LEX_IS_STRINGQUOTE;
99 lex['\''] = LEX_IS_ONECHAR_QUOTE;
102 #ifdef SINGLE_QUOTE_STRINGS
103 lex['\''] = LEX_IS_STRINGQUOTE;
107 /* Note: if any other character can be LEX_IS_STRINGQUOTE, the loop
108 in state 5 of do_scrub_chars must be changed. */
110 /* Note that these override the previous defaults, e.g. if ';' is a
111 comment char, then it isn't a line separator. */
112 for (p = symbol_chars; *p; ++p)
114 lex[(unsigned char) *p] = LEX_IS_SYMBOL_COMPONENT;
115 } /* declare symbol characters */
117 /* The m68k backend wants to be able to change comment_chars. */
118 #ifndef tc_comment_chars
119 #define tc_comment_chars comment_chars
121 for (p = tc_comment_chars; *p; p++)
123 lex[(unsigned char) *p] = LEX_IS_COMMENT_START;
124 } /* declare comment chars */
126 for (p = line_comment_chars; *p; p++)
128 lex[(unsigned char) *p] = LEX_IS_LINE_COMMENT_START;
129 } /* declare line comment chars */
131 for (p = line_separator_chars; *p; p++)
133 lex[(unsigned char) *p] = LEX_IS_LINE_SEPARATOR;
134 } /* declare line separators */
136 /* Only allow slash-star comments if slash is not in use.
137 FIXME: This isn't right. We should always permit them. */
140 lex['/'] = LEX_IS_TWOCHAR_COMMENT_1ST;
145 lex['\''] = LEX_IS_STRINGQUOTE;
146 lex[';'] = LEX_IS_COMMENT_START;
147 lex['*'] = LEX_IS_LINE_COMMENT_START;
148 /* The MRI documentation says '!' is LEX_IS_COMMENT_START, but
149 then it can't be used in an expression. */
150 lex['!'] = LEX_IS_LINE_COMMENT_START;
154 lex['-'] = LEX_IS_DOUBLEDASH_1ST;
157 lex['|'] = LEX_IS_DOUBLEBAR_1ST;
159 } /* do_scrub_begin() */
161 /* Saved state of the scrubber */
163 static int old_state;
164 static char *out_string;
165 static char out_buf[20];
166 static int add_newlines;
167 static char *saved_input;
168 static int saved_input_len;
169 static const char *mri_state;
170 static char mri_last_ch;
172 /* Data structure for saving the state of app across #include's. Note that
173 app is called asynchronously to the parsing of the .include's, so our
174 state at the time .include is interpreted is completely unrelated.
175 That's why we have to save it all. */
182 char out_buf[sizeof (out_buf)];
187 const char *mri_state;
194 register struct app_save *saved;
196 saved = (struct app_save *) xmalloc (sizeof (*saved));
197 saved->state = state;
198 saved->old_state = old_state;
199 saved->out_string = out_string;
200 memcpy (saved->out_buf, out_buf, sizeof (out_buf));
201 saved->add_newlines = add_newlines;
202 saved->saved_input = saved_input;
203 saved->saved_input_len = saved_input_len;
204 saved->scrub_m68k_mri = scrub_m68k_mri;
205 saved->mri_state = mri_state;
206 saved->mri_last_ch = mri_last_ch;
208 /* do_scrub_begin() is not useful, just wastes time. */
213 return (char *) saved;
220 register struct app_save *saved = (struct app_save *) arg;
222 /* There is no do_scrub_end (). */
223 state = saved->state;
224 old_state = saved->old_state;
225 out_string = saved->out_string;
226 memcpy (out_buf, saved->out_buf, sizeof (out_buf));
227 add_newlines = saved->add_newlines;
228 saved_input = saved->saved_input;
229 saved_input_len = saved->saved_input_len;
230 scrub_m68k_mri = saved->scrub_m68k_mri;
231 mri_state = saved->mri_state;
232 mri_last_ch = saved->mri_last_ch;
237 /* @@ This assumes that \n &c are the same on host and target. This is not
264 /* This function is called to process input characters. The GET
265 parameter is used to retrieve more input characters. GET should
266 set its parameter to point to a buffer, and return the length of
267 the buffer; it should return 0 at end of file. The scrubbed output
268 characters are put into the buffer starting at TOSTART; the TOSTART
269 buffer is TOLEN bytes in length. The function returns the number
270 of scrubbed characters put into TOSTART. This will be TOLEN unless
271 end of file was seen. This function is arranged as a state
272 machine, and saves its state so that it may return at any point.
273 This is the way the old code used to work. */
276 do_scrub_chars (get, tostart, tolen)
277 int (*get) PARAMS ((char **));
282 char *toend = tostart + tolen;
286 register int ch, ch2 = 0;
288 /*State 0: beginning of normal line
289 1: After first whitespace on line (flush more white)
290 2: After first non-white (opcode) on line (keep 1white)
291 3: after second white on line (into operands) (flush white)
292 4: after putting out a .line, put out digits
293 5: parsing a string, then go to old-state
294 6: putting out \ escape in a "d string.
295 7: After putting out a .appfile, put out string.
296 8: After putting out a .appfile string, flush until newline.
297 9: After seeing symbol char in state 3 (keep 1white after symchar)
298 10: After seeing whitespace in state 9 (keep white before symchar)
299 11: After seeing a symbol character in state 0 (eg a label definition)
300 -1: output string in out_string and go to the state in old_state
301 -2: flush text until a '*' '/' is seen, then go to state old_state
303 12: After seeing a dash, looking for a second dash as a start of comment.
306 13: After seeing a vertical bar, looking for a second vertical bar as a parallel expression seperator.
310 /* I added states 9 and 10 because the MIPS ECOFF assembler uses
311 constructs like ``.loc 1 20''. This was turning into ``.loc
312 120''. States 9 and 10 ensure that a space is never dropped in
313 between characters which could appear in a identifier. Ian
314 Taylor, ian@cygnus.com.
316 I added state 11 so that something like "Lfoo add %r25,%r26,%r27" works
317 correctly on the PA (and any other target where colons are optional).
318 Jeff Law, law@cs.utah.edu.
320 I added state 13 so that something like "cmp r1, r2 || trap #1" does not
321 get squashed into "cmp r1,r2||trap#1", with the all important space
322 between the 'trap' and the '#1' being eliminated. nickc@cygnus.com */
324 /* This macro gets the next input character. */
329 : ((saved_input != NULL \
330 ? (free (saved_input), \
331 saved_input = NULL, \
334 fromlen = (*get) (&from), \
335 fromend = from + fromlen, \
340 /* This macro pushes a character back on the input stream. */
342 #define UNGET(uch) (*--from = (uch))
344 /* This macro puts a character into the output buffer. If this
345 character fills the output buffer, this macro jumps to the label
346 TOFULL. We use this rather ugly approach because we need to
347 handle two different termination conditions: EOF on the input
348 stream, and a full output buffer. It would be simpler if we
349 always read in the entire input stream before processing it, but
350 I don't want to make such a significant change to the assembler's
362 if (saved_input != NULL)
365 fromend = from + saved_input_len;
369 fromlen = (*get) (&from);
372 fromend = from + fromlen;
377 /* The cases in this switch end with continue, in order to
378 branch back to the top of this while loop and generate the
379 next output character in the appropriate state. */
384 if (*out_string == '\0')
401 as_warn ("end of file in comment");
410 while ((ch = GET ()) == '*')
415 as_warn ("end of file in comment");
433 else if (ch >= '0' && ch <= '9')
437 while (ch != EOF && IS_WHITESPACE (ch))
443 out_string = "\n\tappfile ";
445 out_string = "\n\t.appfile ";
452 while (ch != EOF && ch != '\n')
461 /* We are going to copy everything up to a quote character,
462 with special handling for a backslash. We try to
463 optimize the copying in the simple case without using the
464 GET and PUT macros. */
469 for (s = from; s < fromend; s++)
472 /* This condition must be changed if the type of any
473 other character can be LEX_IS_STRINGQUOTE. */
481 if (len > toend - to)
485 memcpy (to, from, len);
494 as_warn ("end of file in string: inserted '\"'");
499 else if (lex[ch] == LEX_IS_STRINGQUOTE)
504 #ifndef NO_STRING_ESCAPES
511 else if (scrub_m68k_mri && ch == '\n')
513 /* Just quietly terminate the string. This permits lines like
514 bne label loop if we haven't reach end yet
531 /* Handle strings broken across lines, by turning '\n' into
558 #if defined(IGNORE_NONSTANDARD_ESCAPES) | defined(ONLY_STANDARD_ESCAPES)
560 as_warn ("Unknown escape '\\%c' in string: Ignored", ch);
562 #else /* ONLY_STANDARD_ESCAPES */
564 /* Accept \x as x for any x */
566 #endif /* ONLY_STANDARD_ESCAPES */
569 as_warn ("End of file in string: '\"' inserted");
588 while (ch != '\n' && ch != EOF);
596 /* OK, we are somewhere in states 0 through 4 or 9 through 11 */
604 /* We want to have pseudo-ops which control whether we are in
605 MRI mode or not. Unfortunately, since m68k MRI mode affects
606 the scrubber, that means that we need a special purpose
608 if (mri_state == NULL)
610 if ((state == 0 || state == 1)
611 && ch == mri_pseudo[0])
612 mri_state = mri_pseudo + 1;
616 /* We advance to the next state if we find the right
617 character, or if we need a space character and we get any
618 whitespace character, or if we need a '0' and we get a
619 '1' (this is so that we only need one state to handle
620 ``.mri 0'' and ``.mri 1''). */
623 || (*mri_state == ' '
624 && lex[ch] == LEX_IS_WHITESPACE)
625 || (*mri_state == '0'
631 else if (*mri_state != '\0'
632 || (lex[ch] != LEX_IS_WHITESPACE
633 && lex[ch] != LEX_IS_NEWLINE))
635 /* We did not get the expected character, or we didn't
636 get a valid terminating character after seeing the
637 entire pseudo-op, so we must go back to the
643 /* We've read the entire pseudo-op. mips_last_ch is
644 either '0' or '1' indicating whether to enter or
646 do_scrub_begin (mri_last_ch == '1');
649 /* We continue handling the character as usual. The
650 main gas reader must also handle the .mri pseudo-op
651 to control expression parsing and the like. */
660 as_warn ("end of file not at end of a line; newline inserted");
669 case LEX_IS_WHITESPACE:
674 while (ch != EOF && IS_WHITESPACE (ch));
680 /* Preserve a single whitespace character at the
681 beginning of a line. */
690 || IS_LINE_SEPARATOR (ch))
694 /* In MRI mode, we keep these spaces. */
702 /* If we're in state 2 or 11, we've seen a non-white
703 character followed by whitespace. If the next character
704 is ':', this is whitespace after a label name which we
705 normally must ignore. In MRI mode, though, spaces are
706 not permitted between the label and the colon. */
707 if ((state == 2 || state == 11)
708 && lex[ch] == LEX_IS_COLON
720 goto recycle; /* Punted leading sp */
722 /* We can arrive here if we leave a leading whitespace
723 character at the beginning of a line. */
729 /* Optimize common case by skipping UNGET/GET. */
730 PUT (' '); /* Sp after opco */
739 /* In MRI mode, we keep these spaces. */
744 goto recycle; /* Sp in operands */
749 /* In MRI mode, we keep these spaces. */
755 state = 10; /* Sp after symbol char */
759 #ifdef LABELS_WITHOUT_COLONS
766 /* We know that ch is not ':', since we tested that
767 case above. Therefore this is not a label, so it
768 must be the opcode, and we've just seen the
769 whitespace after it. */
773 PUT (' '); /* Sp after label definition. */
780 case LEX_IS_TWOCHAR_COMMENT_1ST:
789 if (ch2 != EOF && IS_NEWLINE (ch2))
792 while (ch2 != EOF && ch2 != '*');
797 if (ch2 == EOF || ch2 == '/')
800 /* This UNGET will ensure that we count newlines
806 as_warn ("end of file in multiline comment");
815 if (state == 9 || state == 10)
821 case LEX_IS_STRINGQUOTE:
824 /* Preserve the whitespace in foo "bar" */
829 /* PUT didn't jump out. We could just break, but we
830 know what will happen, so optimize a bit. */
843 case LEX_IS_ONECHAR_QUOTE:
846 /* Preserve the whitespace in foo 'b' */
855 as_warn ("end of file after a one-character quote; \\0 inserted");
863 as_warn ("end of file in escape character");
867 ch = process_escape (ch);
869 sprintf (out_buf, "%d", (int) (unsigned char) ch);
871 /* None of these 'x constants for us. We want 'x'. */
872 if ((ch = GET ()) != '\'')
874 #ifdef REQUIRE_CHAR_CLOSE_QUOTE
875 as_warn ("Missing close quote: (assumed)");
881 if (strlen (out_buf) == 1)
891 out_string = out_buf;
897 if (state == 9 || state == 10)
905 /* Roll out a bunch of newlines from inside comments, etc. */
911 /* fall thru into... */
913 case LEX_IS_LINE_SEPARATOR:
919 case LEX_IS_DOUBLEDASH_1ST:
926 /* read and skip to end of line */
931 while (ch != EOF && ch != '\n');
934 as_warn ("end of file in comment; newline inserted");
941 case LEX_IS_DOUBLEBAR_1ST:
948 /* Reset back to state 1 and pretend that we are parsing a line from
949 just after the first white space. */
955 case LEX_IS_LINE_COMMENT_START:
956 /* FIXME-someday: The two character comment stuff was badly
957 thought out. On i386, we want '/' as line comment start
958 AND we want C style comments. hence this hack. The
959 whole lexical process should be reworked. xoxorich. */
975 if (state == 0 || state == 1) /* Only comment at start of line. */
985 while (ch != EOF && IS_WHITESPACE (ch));
988 as_warn ("end of file in comment; newline inserted");
992 if (ch < '0' || ch > '9' || state != 0 || startch != '#')
994 /* Not a cpp line. */
995 while (ch != EOF && !IS_NEWLINE (ch))
998 as_warn ("EOF in Comment: Newline inserted");
1003 /* Loks like `# 123 "filename"' from cpp. */
1008 out_string = "\tappline ";
1010 out_string = "\t.appline ";
1011 PUT (*out_string++);
1016 /* All insns end in a char for which LEX_IS_SYMBOL_COMPONENT is true.
1017 Trap is the only short insn that has a first operand that is
1018 neither register nor label.
1019 We must prevent exef0f ||trap #1 to degenerate to exef0f ||trap#1 .
1020 We can't make '#' LEX_IS_SYMBOL_COMPONENT because it is already
1021 LEX_IS_LINE_COMMENT_START. However, it is the only character in
1022 line_comment_chars for d10v, hence we can recognize it as such. */
1023 /* An alternative approach would be to reset the state to 1 when
1024 we see '||', '<'- or '->', but that seems to be overkill. */
1025 if (state == 10) PUT (' ');
1027 /* We have a line comment character which is not at the
1028 start of a line. If this is also a normal comment
1029 character, fall through. Otherwise treat it as a default
1031 if (strchr (tc_comment_chars, ch) == NULL
1032 && (! scrub_m68k_mri
1033 || (ch != '!' && ch != '*')))
1036 && (ch == '!' || ch == '*' || ch == '#')
1041 case LEX_IS_COMMENT_START:
1046 while (ch != EOF && !IS_NEWLINE (ch));
1048 as_warn ("end of file in comment; newline inserted");
1053 case LEX_IS_SYMBOL_COMPONENT:
1056 /* This is a symbol character following another symbol
1057 character, with whitespace in between. We skipped
1058 the whitespace earlier, so output it now. */
1068 /* This is a common case. Quickly copy CH and all the
1069 following symbol component or normal characters. */
1070 if (to + 1 < toend && mri_state == NULL)
1075 for (s = from; s < fromend; s++)
1082 && type != LEX_IS_SYMBOL_COMPONENT)
1087 /* Handle the last character normally, for
1092 if (len > (toend - to) - 1)
1093 len = (toend - to) - 1;
1099 memcpy (to, from, len);
1107 case 8: *to++ = *from++;
1108 case 7: *to++ = *from++;
1109 case 6: *to++ = *from++;
1110 case 5: *to++ = *from++;
1111 case 4: *to++ = *from++;
1112 case 3: *to++ = *from++;
1113 case 2: *to++ = *from++;
1114 case 1: *to++ = *from++;
1124 /* Some relatively `normal' character. */
1127 state = 11; /* Now seeing label definition */
1129 else if (state == 1)
1131 state = 2; /* Ditto */
1133 else if (state == 9)
1135 if (lex[ch] != LEX_IS_SYMBOL_COMPONENT)
1138 else if (state == 10)
1150 /* We have reached the end of the input. */
1151 return to - tostart;
1154 /* The output buffer is full. Save any input we have not yet
1160 save = (char *) xmalloc (fromend - from);
1161 memcpy (save, from, fromend - from);
1162 if (saved_input != NULL)
1165 saved_input_len = fromend - from;
1169 if (saved_input != NULL)
1175 return to - tostart;