Update copyright years
[deliverable/binutils-gdb.git] / gas / app.c
1 /* This is the Assembler Pre-Processor
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
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 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
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 the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 /* Modified by Allen Wirfs-Brock, Instantiations Inc 2/90. */
22 /* App, the assembler pre-processor. This pre-processor strips out
23 excess spaces, turns single-quoted characters into a decimal
24 constant, and turns the # in # <number> <filename> <garbage> into a
25 .linefile. This needs better error-handling. */
26
27 #include "as.h"
28
29 #if (__STDC__ != 1)
30 #ifndef const
31 #define const /* empty */
32 #endif
33 #endif
34
35 #ifdef H_TICK_HEX
36 int enable_h_tick_hex = 0;
37 #endif
38
39 #ifdef TC_M68K
40 /* Whether we are scrubbing in m68k MRI mode. This is different from
41 flag_m68k_mri, because the two flags will be affected by the .mri
42 pseudo-op at different times. */
43 static int scrub_m68k_mri;
44
45 /* The pseudo-op which switches in and out of MRI mode. See the
46 comment in do_scrub_chars. */
47 static const char mri_pseudo[] = ".mri 0";
48 #else
49 #define scrub_m68k_mri 0
50 #endif
51
52 #if defined TC_ARM && defined OBJ_ELF
53 /* The pseudo-op for which we need to special-case `@' characters.
54 See the comment in do_scrub_chars. */
55 static const char symver_pseudo[] = ".symver";
56 static const char * symver_state;
57 #endif
58
59 static char lex[256];
60 static const char symbol_chars[] =
61 "$._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
62
63 #define LEX_IS_SYMBOL_COMPONENT 1
64 #define LEX_IS_WHITESPACE 2
65 #define LEX_IS_LINE_SEPARATOR 3
66 #define LEX_IS_COMMENT_START 4
67 #define LEX_IS_LINE_COMMENT_START 5
68 #define LEX_IS_TWOCHAR_COMMENT_1ST 6
69 #define LEX_IS_STRINGQUOTE 8
70 #define LEX_IS_COLON 9
71 #define LEX_IS_NEWLINE 10
72 #define LEX_IS_ONECHAR_QUOTE 11
73 #ifdef TC_V850
74 #define LEX_IS_DOUBLEDASH_1ST 12
75 #endif
76 #ifdef TC_M32R
77 #define DOUBLEBAR_PARALLEL
78 #endif
79 #ifdef DOUBLEBAR_PARALLEL
80 #define LEX_IS_DOUBLEBAR_1ST 13
81 #endif
82 #define LEX_IS_PARALLEL_SEPARATOR 14
83 #ifdef H_TICK_HEX
84 #define LEX_IS_H 15
85 #endif
86 #define IS_SYMBOL_COMPONENT(c) (lex[c] == LEX_IS_SYMBOL_COMPONENT)
87 #define IS_WHITESPACE(c) (lex[c] == LEX_IS_WHITESPACE)
88 #define IS_LINE_SEPARATOR(c) (lex[c] == LEX_IS_LINE_SEPARATOR)
89 #define IS_PARALLEL_SEPARATOR(c) (lex[c] == LEX_IS_PARALLEL_SEPARATOR)
90 #define IS_COMMENT(c) (lex[c] == LEX_IS_COMMENT_START)
91 #define IS_LINE_COMMENT(c) (lex[c] == LEX_IS_LINE_COMMENT_START)
92 #define IS_NEWLINE(c) (lex[c] == LEX_IS_NEWLINE)
93
94 static int process_escape (int);
95
96 /* FIXME-soon: The entire lexer/parser thingy should be
97 built statically at compile time rather than dynamically
98 each and every time the assembler is run. xoxorich. */
99
100 void
101 do_scrub_begin (int m68k_mri ATTRIBUTE_UNUSED)
102 {
103 const char *p;
104 int c;
105
106 lex[' '] = LEX_IS_WHITESPACE;
107 lex['\t'] = LEX_IS_WHITESPACE;
108 lex['\r'] = LEX_IS_WHITESPACE;
109 lex['\n'] = LEX_IS_NEWLINE;
110 lex[':'] = LEX_IS_COLON;
111
112 #ifdef TC_M68K
113 scrub_m68k_mri = m68k_mri;
114
115 if (! m68k_mri)
116 #endif
117 {
118 lex['"'] = LEX_IS_STRINGQUOTE;
119
120 #if ! defined (TC_HPPA) && ! defined (TC_I370)
121 /* I370 uses single-quotes to delimit integer, float constants. */
122 lex['\''] = LEX_IS_ONECHAR_QUOTE;
123 #endif
124
125 #ifdef SINGLE_QUOTE_STRINGS
126 lex['\''] = LEX_IS_STRINGQUOTE;
127 #endif
128 }
129
130 /* Note: if any other character can be LEX_IS_STRINGQUOTE, the loop
131 in state 5 of do_scrub_chars must be changed. */
132
133 /* Note that these override the previous defaults, e.g. if ';' is a
134 comment char, then it isn't a line separator. */
135 for (p = symbol_chars; *p; ++p)
136 lex[(unsigned char) *p] = LEX_IS_SYMBOL_COMPONENT;
137
138 for (c = 128; c < 256; ++c)
139 lex[c] = LEX_IS_SYMBOL_COMPONENT;
140
141 #ifdef tc_symbol_chars
142 /* This macro permits the processor to specify all characters which
143 may appears in an operand. This will prevent the scrubber from
144 discarding meaningful whitespace in certain cases. The i386
145 backend uses this to support prefixes, which can confuse the
146 scrubber as to whether it is parsing operands or opcodes. */
147 for (p = tc_symbol_chars; *p; ++p)
148 lex[(unsigned char) *p] = LEX_IS_SYMBOL_COMPONENT;
149 #endif
150
151 /* The m68k backend wants to be able to change comment_chars. */
152 #ifndef tc_comment_chars
153 #define tc_comment_chars comment_chars
154 #endif
155 for (p = tc_comment_chars; *p; p++)
156 lex[(unsigned char) *p] = LEX_IS_COMMENT_START;
157
158 for (p = line_comment_chars; *p; p++)
159 lex[(unsigned char) *p] = LEX_IS_LINE_COMMENT_START;
160
161 for (p = line_separator_chars; *p; p++)
162 lex[(unsigned char) *p] = LEX_IS_LINE_SEPARATOR;
163
164 #ifdef tc_parallel_separator_chars
165 /* This macro permits the processor to specify all characters which
166 separate parallel insns on the same line. */
167 for (p = tc_parallel_separator_chars; *p; p++)
168 lex[(unsigned char) *p] = LEX_IS_PARALLEL_SEPARATOR;
169 #endif
170
171 /* Only allow slash-star comments if slash is not in use.
172 FIXME: This isn't right. We should always permit them. */
173 if (lex['/'] == 0)
174 lex['/'] = LEX_IS_TWOCHAR_COMMENT_1ST;
175
176 #ifdef TC_M68K
177 if (m68k_mri)
178 {
179 lex['\''] = LEX_IS_STRINGQUOTE;
180 lex[';'] = LEX_IS_COMMENT_START;
181 lex['*'] = LEX_IS_LINE_COMMENT_START;
182 /* The MRI documentation says '!' is LEX_IS_COMMENT_START, but
183 then it can't be used in an expression. */
184 lex['!'] = LEX_IS_LINE_COMMENT_START;
185 }
186 #endif
187
188 #ifdef TC_V850
189 lex['-'] = LEX_IS_DOUBLEDASH_1ST;
190 #endif
191 #ifdef DOUBLEBAR_PARALLEL
192 lex['|'] = LEX_IS_DOUBLEBAR_1ST;
193 #endif
194 #ifdef TC_D30V
195 /* Must do this is we want VLIW instruction with "->" or "<-". */
196 lex['-'] = LEX_IS_SYMBOL_COMPONENT;
197 #endif
198
199 #ifdef H_TICK_HEX
200 if (enable_h_tick_hex)
201 {
202 lex['h'] = LEX_IS_H;
203 lex['H'] = LEX_IS_H;
204 }
205 #endif
206 }
207
208 /* Saved state of the scrubber. */
209 static int state;
210 static int old_state;
211 static char *out_string;
212 static char out_buf[20];
213 static int add_newlines;
214 static char *saved_input;
215 static size_t saved_input_len;
216 static char input_buffer[32 * 1024];
217 static const char *mri_state;
218 static char mri_last_ch;
219
220 /* Data structure for saving the state of app across #include's. Note that
221 app is called asynchronously to the parsing of the .include's, so our
222 state at the time .include is interpreted is completely unrelated.
223 That's why we have to save it all. */
224
225 struct app_save
226 {
227 int state;
228 int old_state;
229 char * out_string;
230 char out_buf[sizeof (out_buf)];
231 int add_newlines;
232 char * saved_input;
233 size_t saved_input_len;
234 #ifdef TC_M68K
235 int scrub_m68k_mri;
236 #endif
237 const char * mri_state;
238 char mri_last_ch;
239 #if defined TC_ARM && defined OBJ_ELF
240 const char * symver_state;
241 #endif
242 };
243
244 char *
245 app_push (void)
246 {
247 register struct app_save *saved;
248
249 saved = (struct app_save *) xmalloc (sizeof (*saved));
250 saved->state = state;
251 saved->old_state = old_state;
252 saved->out_string = out_string;
253 memcpy (saved->out_buf, out_buf, sizeof (out_buf));
254 saved->add_newlines = add_newlines;
255 if (saved_input == NULL)
256 saved->saved_input = NULL;
257 else
258 {
259 saved->saved_input = (char *) xmalloc (saved_input_len);
260 memcpy (saved->saved_input, saved_input, saved_input_len);
261 saved->saved_input_len = saved_input_len;
262 }
263 #ifdef TC_M68K
264 saved->scrub_m68k_mri = scrub_m68k_mri;
265 #endif
266 saved->mri_state = mri_state;
267 saved->mri_last_ch = mri_last_ch;
268 #if defined TC_ARM && defined OBJ_ELF
269 saved->symver_state = symver_state;
270 #endif
271
272 /* do_scrub_begin() is not useful, just wastes time. */
273
274 state = 0;
275 saved_input = NULL;
276 add_newlines = 0;
277
278 return (char *) saved;
279 }
280
281 void
282 app_pop (char *arg)
283 {
284 register struct app_save *saved = (struct app_save *) arg;
285
286 /* There is no do_scrub_end (). */
287 state = saved->state;
288 old_state = saved->old_state;
289 out_string = saved->out_string;
290 memcpy (out_buf, saved->out_buf, sizeof (out_buf));
291 add_newlines = saved->add_newlines;
292 if (saved->saved_input == NULL)
293 saved_input = NULL;
294 else
295 {
296 gas_assert (saved->saved_input_len <= sizeof (input_buffer));
297 memcpy (input_buffer, saved->saved_input, saved->saved_input_len);
298 saved_input = input_buffer;
299 saved_input_len = saved->saved_input_len;
300 free (saved->saved_input);
301 }
302 #ifdef TC_M68K
303 scrub_m68k_mri = saved->scrub_m68k_mri;
304 #endif
305 mri_state = saved->mri_state;
306 mri_last_ch = saved->mri_last_ch;
307 #if defined TC_ARM && defined OBJ_ELF
308 symver_state = saved->symver_state;
309 #endif
310
311 free (arg);
312 }
313
314 /* @@ This assumes that \n &c are the same on host and target. This is not
315 necessarily true. */
316
317 static int
318 process_escape (int ch)
319 {
320 switch (ch)
321 {
322 case 'b':
323 return '\b';
324 case 'f':
325 return '\f';
326 case 'n':
327 return '\n';
328 case 'r':
329 return '\r';
330 case 't':
331 return '\t';
332 case '\'':
333 return '\'';
334 case '"':
335 return '\"';
336 default:
337 return ch;
338 }
339 }
340
341 /* This function is called to process input characters. The GET
342 parameter is used to retrieve more input characters. GET should
343 set its parameter to point to a buffer, and return the length of
344 the buffer; it should return 0 at end of file. The scrubbed output
345 characters are put into the buffer starting at TOSTART; the TOSTART
346 buffer is TOLEN bytes in length. The function returns the number
347 of scrubbed characters put into TOSTART. This will be TOLEN unless
348 end of file was seen. This function is arranged as a state
349 machine, and saves its state so that it may return at any point.
350 This is the way the old code used to work. */
351
352 size_t
353 do_scrub_chars (size_t (*get) (char *, size_t), char *tostart, size_t tolen)
354 {
355 char *to = tostart;
356 char *toend = tostart + tolen;
357 char *from;
358 char *fromend;
359 size_t fromlen;
360 register int ch, ch2 = 0;
361 /* Character that started the string we're working on. */
362 static char quotechar;
363
364 /*State 0: beginning of normal line
365 1: After first whitespace on line (flush more white)
366 2: After first non-white (opcode) on line (keep 1white)
367 3: after second white on line (into operands) (flush white)
368 4: after putting out a .linefile, put out digits
369 5: parsing a string, then go to old-state
370 6: putting out \ escape in a "d string.
371 7: no longer used
372 8: no longer used
373 9: After seeing symbol char in state 3 (keep 1white after symchar)
374 10: After seeing whitespace in state 9 (keep white before symchar)
375 11: After seeing a symbol character in state 0 (eg a label definition)
376 -1: output string in out_string and go to the state in old_state
377 -2: flush text until a '*' '/' is seen, then go to state old_state
378 #ifdef TC_V850
379 12: After seeing a dash, looking for a second dash as a start
380 of comment.
381 #endif
382 #ifdef DOUBLEBAR_PARALLEL
383 13: After seeing a vertical bar, looking for a second
384 vertical bar as a parallel expression separator.
385 #endif
386 #ifdef TC_PREDICATE_START_CHAR
387 14: After seeing a predicate start character at state 0, looking
388 for a predicate end character as predicate.
389 15: After seeing a predicate start character at state 1, looking
390 for a predicate end character as predicate.
391 #endif
392 #ifdef TC_Z80
393 16: After seeing an 'a' or an 'A' at the start of a symbol
394 17: After seeing an 'f' or an 'F' in state 16
395 #endif
396 */
397
398 /* I added states 9 and 10 because the MIPS ECOFF assembler uses
399 constructs like ``.loc 1 20''. This was turning into ``.loc
400 120''. States 9 and 10 ensure that a space is never dropped in
401 between characters which could appear in an identifier. Ian
402 Taylor, ian@cygnus.com.
403
404 I added state 11 so that something like "Lfoo add %r25,%r26,%r27" works
405 correctly on the PA (and any other target where colons are optional).
406 Jeff Law, law@cs.utah.edu.
407
408 I added state 13 so that something like "cmp r1, r2 || trap #1" does not
409 get squashed into "cmp r1,r2||trap#1", with the all important space
410 between the 'trap' and the '#1' being eliminated. nickc@cygnus.com */
411
412 /* This macro gets the next input character. */
413
414 #define GET() \
415 (from < fromend \
416 ? * (unsigned char *) (from++) \
417 : (saved_input = NULL, \
418 fromlen = (*get) (input_buffer, sizeof input_buffer), \
419 from = input_buffer, \
420 fromend = from + fromlen, \
421 (fromlen == 0 \
422 ? EOF \
423 : * (unsigned char *) (from++))))
424
425 /* This macro pushes a character back on the input stream. */
426
427 #define UNGET(uch) (*--from = (uch))
428
429 /* This macro puts a character into the output buffer. If this
430 character fills the output buffer, this macro jumps to the label
431 TOFULL. We use this rather ugly approach because we need to
432 handle two different termination conditions: EOF on the input
433 stream, and a full output buffer. It would be simpler if we
434 always read in the entire input stream before processing it, but
435 I don't want to make such a significant change to the assembler's
436 memory usage. */
437
438 #define PUT(pch) \
439 do \
440 { \
441 *to++ = (pch); \
442 if (to >= toend) \
443 goto tofull; \
444 } \
445 while (0)
446
447 if (saved_input != NULL)
448 {
449 from = saved_input;
450 fromend = from + saved_input_len;
451 }
452 else
453 {
454 fromlen = (*get) (input_buffer, sizeof input_buffer);
455 if (fromlen == 0)
456 return 0;
457 from = input_buffer;
458 fromend = from + fromlen;
459 }
460
461 while (1)
462 {
463 /* The cases in this switch end with continue, in order to
464 branch back to the top of this while loop and generate the
465 next output character in the appropriate state. */
466 switch (state)
467 {
468 case -1:
469 ch = *out_string++;
470 if (*out_string == '\0')
471 {
472 state = old_state;
473 old_state = 3;
474 }
475 PUT (ch);
476 continue;
477
478 case -2:
479 for (;;)
480 {
481 do
482 {
483 ch = GET ();
484
485 if (ch == EOF)
486 {
487 as_warn (_("end of file in comment"));
488 goto fromeof;
489 }
490
491 if (ch == '\n')
492 PUT ('\n');
493 }
494 while (ch != '*');
495
496 while ((ch = GET ()) == '*')
497 ;
498
499 if (ch == EOF)
500 {
501 as_warn (_("end of file in comment"));
502 goto fromeof;
503 }
504
505 if (ch == '/')
506 break;
507
508 UNGET (ch);
509 }
510
511 state = old_state;
512 UNGET (' ');
513 continue;
514
515 case 4:
516 ch = GET ();
517 if (ch == EOF)
518 goto fromeof;
519 else if (ch >= '0' && ch <= '9')
520 PUT (ch);
521 else
522 {
523 while (ch != EOF && IS_WHITESPACE (ch))
524 ch = GET ();
525 if (ch == '"')
526 {
527 quotechar = ch;
528 state = 5;
529 old_state = 3;
530 PUT (ch);
531 }
532 else
533 {
534 while (ch != EOF && ch != '\n')
535 ch = GET ();
536 state = 0;
537 PUT (ch);
538 }
539 }
540 continue;
541
542 case 5:
543 /* We are going to copy everything up to a quote character,
544 with special handling for a backslash. We try to
545 optimize the copying in the simple case without using the
546 GET and PUT macros. */
547 {
548 char *s;
549 ptrdiff_t len;
550
551 for (s = from; s < fromend; s++)
552 {
553 ch = *s;
554 if (ch == '\\'
555 || ch == quotechar
556 || ch == '\n')
557 break;
558 }
559 len = s - from;
560 if (len > toend - to)
561 len = toend - to;
562 if (len > 0)
563 {
564 memcpy (to, from, len);
565 to += len;
566 from += len;
567 if (to >= toend)
568 goto tofull;
569 }
570 }
571
572 ch = GET ();
573 if (ch == EOF)
574 {
575 /* This buffer is here specifically so
576 that the UNGET below will work. */
577 static char one_char_buf[1];
578
579 as_warn (_("end of file in string; '%c' inserted"), quotechar);
580 state = old_state;
581 from = fromend = one_char_buf + 1;
582 fromlen = 1;
583 UNGET ('\n');
584 PUT (quotechar);
585 }
586 else if (ch == quotechar)
587 {
588 state = old_state;
589 PUT (ch);
590 }
591 #ifndef NO_STRING_ESCAPES
592 else if (ch == '\\')
593 {
594 state = 6;
595 PUT (ch);
596 }
597 #endif
598 else if (scrub_m68k_mri && ch == '\n')
599 {
600 /* Just quietly terminate the string. This permits lines like
601 bne label loop if we haven't reach end yet. */
602 state = old_state;
603 UNGET (ch);
604 PUT ('\'');
605 }
606 else
607 {
608 PUT (ch);
609 }
610 continue;
611
612 case 6:
613 state = 5;
614 ch = GET ();
615 switch (ch)
616 {
617 /* Handle strings broken across lines, by turning '\n' into
618 '\\' and 'n'. */
619 case '\n':
620 UNGET ('n');
621 add_newlines++;
622 PUT ('\\');
623 continue;
624
625 case EOF:
626 as_warn (_("end of file in string; '%c' inserted"), quotechar);
627 PUT (quotechar);
628 continue;
629
630 case '"':
631 case '\\':
632 case 'b':
633 case 'f':
634 case 'n':
635 case 'r':
636 case 't':
637 case 'v':
638 case 'x':
639 case 'X':
640 case '0':
641 case '1':
642 case '2':
643 case '3':
644 case '4':
645 case '5':
646 case '6':
647 case '7':
648 break;
649
650 default:
651 #ifdef ONLY_STANDARD_ESCAPES
652 as_warn (_("unknown escape '\\%c' in string; ignored"), ch);
653 #endif
654 break;
655 }
656 PUT (ch);
657 continue;
658
659 #ifdef DOUBLEBAR_PARALLEL
660 case 13:
661 ch = GET ();
662 if (ch != '|')
663 abort ();
664
665 /* Reset back to state 1 and pretend that we are parsing a
666 line from just after the first white space. */
667 state = 1;
668 PUT ('|');
669 #ifdef TC_TIC6X
670 /* "||^" is used for SPMASKed instructions. */
671 ch = GET ();
672 if (ch == EOF)
673 goto fromeof;
674 else if (ch == '^')
675 PUT ('^');
676 else
677 UNGET (ch);
678 #endif
679 continue;
680 #endif
681 #ifdef TC_Z80
682 case 16:
683 /* We have seen an 'a' at the start of a symbol, look for an 'f'. */
684 ch = GET ();
685 if (ch == 'f' || ch == 'F')
686 {
687 state = 17;
688 PUT (ch);
689 }
690 else
691 {
692 state = 9;
693 break;
694 }
695 case 17:
696 /* We have seen "af" at the start of a symbol,
697 a ' here is a part of that symbol. */
698 ch = GET ();
699 state = 9;
700 if (ch == '\'')
701 /* Change to avoid warning about unclosed string. */
702 PUT ('`');
703 else if (ch != EOF)
704 UNGET (ch);
705 break;
706 #endif
707 }
708
709 /* OK, we are somewhere in states 0 through 4 or 9 through 11. */
710
711 /* flushchar: */
712 ch = GET ();
713
714 #ifdef TC_PREDICATE_START_CHAR
715 if (ch == TC_PREDICATE_START_CHAR && (state == 0 || state == 1))
716 {
717 state += 14;
718 PUT (ch);
719 continue;
720 }
721 else if (state == 14 || state == 15)
722 {
723 if (ch == TC_PREDICATE_END_CHAR)
724 {
725 state -= 14;
726 PUT (ch);
727 ch = GET ();
728 }
729 else
730 {
731 PUT (ch);
732 continue;
733 }
734 }
735 #endif
736
737 recycle:
738
739 #if defined TC_ARM && defined OBJ_ELF
740 /* We need to watch out for .symver directives. See the comment later
741 in this function. */
742 if (symver_state == NULL)
743 {
744 if ((state == 0 || state == 1) && ch == symver_pseudo[0])
745 symver_state = symver_pseudo + 1;
746 }
747 else
748 {
749 /* We advance to the next state if we find the right
750 character. */
751 if (ch != '\0' && (*symver_state == ch))
752 ++symver_state;
753 else if (*symver_state != '\0')
754 /* We did not get the expected character, or we didn't
755 get a valid terminating character after seeing the
756 entire pseudo-op, so we must go back to the beginning. */
757 symver_state = NULL;
758 else
759 {
760 /* We've read the entire pseudo-op. If this is the end
761 of the line, go back to the beginning. */
762 if (IS_NEWLINE (ch))
763 symver_state = NULL;
764 }
765 }
766 #endif /* TC_ARM && OBJ_ELF */
767
768 #ifdef TC_M68K
769 /* We want to have pseudo-ops which control whether we are in
770 MRI mode or not. Unfortunately, since m68k MRI mode affects
771 the scrubber, that means that we need a special purpose
772 recognizer here. */
773 if (mri_state == NULL)
774 {
775 if ((state == 0 || state == 1)
776 && ch == mri_pseudo[0])
777 mri_state = mri_pseudo + 1;
778 }
779 else
780 {
781 /* We advance to the next state if we find the right
782 character, or if we need a space character and we get any
783 whitespace character, or if we need a '0' and we get a
784 '1' (this is so that we only need one state to handle
785 ``.mri 0'' and ``.mri 1''). */
786 if (ch != '\0'
787 && (*mri_state == ch
788 || (*mri_state == ' '
789 && lex[ch] == LEX_IS_WHITESPACE)
790 || (*mri_state == '0'
791 && ch == '1')))
792 {
793 mri_last_ch = ch;
794 ++mri_state;
795 }
796 else if (*mri_state != '\0'
797 || (lex[ch] != LEX_IS_WHITESPACE
798 && lex[ch] != LEX_IS_NEWLINE))
799 {
800 /* We did not get the expected character, or we didn't
801 get a valid terminating character after seeing the
802 entire pseudo-op, so we must go back to the
803 beginning. */
804 mri_state = NULL;
805 }
806 else
807 {
808 /* We've read the entire pseudo-op. mips_last_ch is
809 either '0' or '1' indicating whether to enter or
810 leave MRI mode. */
811 do_scrub_begin (mri_last_ch == '1');
812 mri_state = NULL;
813
814 /* We continue handling the character as usual. The
815 main gas reader must also handle the .mri pseudo-op
816 to control expression parsing and the like. */
817 }
818 }
819 #endif
820
821 if (ch == EOF)
822 {
823 if (state != 0)
824 {
825 as_warn (_("end of file not at end of a line; newline inserted"));
826 state = 0;
827 PUT ('\n');
828 }
829 goto fromeof;
830 }
831
832 switch (lex[ch])
833 {
834 case LEX_IS_WHITESPACE:
835 do
836 {
837 ch = GET ();
838 }
839 while (ch != EOF && IS_WHITESPACE (ch));
840 if (ch == EOF)
841 goto fromeof;
842
843 if (state == 0)
844 {
845 /* Preserve a single whitespace character at the
846 beginning of a line. */
847 state = 1;
848 UNGET (ch);
849 PUT (' ');
850 break;
851 }
852
853 #ifdef KEEP_WHITE_AROUND_COLON
854 if (lex[ch] == LEX_IS_COLON)
855 {
856 /* Only keep this white if there's no white *after* the
857 colon. */
858 ch2 = GET ();
859 if (ch2 != EOF)
860 UNGET (ch2);
861 if (!IS_WHITESPACE (ch2))
862 {
863 state = 9;
864 UNGET (ch);
865 PUT (' ');
866 break;
867 }
868 }
869 #endif
870 if (IS_COMMENT (ch)
871 || ch == '/'
872 || IS_LINE_SEPARATOR (ch)
873 || IS_PARALLEL_SEPARATOR (ch))
874 {
875 if (scrub_m68k_mri)
876 {
877 /* In MRI mode, we keep these spaces. */
878 UNGET (ch);
879 PUT (' ');
880 break;
881 }
882 goto recycle;
883 }
884
885 /* If we're in state 2 or 11, we've seen a non-white
886 character followed by whitespace. If the next character
887 is ':', this is whitespace after a label name which we
888 normally must ignore. In MRI mode, though, spaces are
889 not permitted between the label and the colon. */
890 if ((state == 2 || state == 11)
891 && lex[ch] == LEX_IS_COLON
892 && ! scrub_m68k_mri)
893 {
894 state = 1;
895 PUT (ch);
896 break;
897 }
898
899 switch (state)
900 {
901 case 1:
902 /* We can arrive here if we leave a leading whitespace
903 character at the beginning of a line. */
904 goto recycle;
905 case 2:
906 state = 3;
907 if (to + 1 < toend)
908 {
909 /* Optimize common case by skipping UNGET/GET. */
910 PUT (' '); /* Sp after opco */
911 goto recycle;
912 }
913 UNGET (ch);
914 PUT (' ');
915 break;
916 case 3:
917 #ifndef TC_KEEP_OPERAND_SPACES
918 /* For TI C6X, we keep these spaces as they may separate
919 functional unit specifiers from operands. */
920 if (scrub_m68k_mri)
921 #endif
922 {
923 /* In MRI mode, we keep these spaces. */
924 UNGET (ch);
925 PUT (' ');
926 break;
927 }
928 goto recycle; /* Sp in operands */
929 case 9:
930 case 10:
931 #ifndef TC_KEEP_OPERAND_SPACES
932 if (scrub_m68k_mri)
933 #endif
934 {
935 /* In MRI mode, we keep these spaces. */
936 state = 3;
937 UNGET (ch);
938 PUT (' ');
939 break;
940 }
941 state = 10; /* Sp after symbol char */
942 goto recycle;
943 case 11:
944 if (LABELS_WITHOUT_COLONS || flag_m68k_mri)
945 state = 1;
946 else
947 {
948 /* We know that ch is not ':', since we tested that
949 case above. Therefore this is not a label, so it
950 must be the opcode, and we've just seen the
951 whitespace after it. */
952 state = 3;
953 }
954 UNGET (ch);
955 PUT (' '); /* Sp after label definition. */
956 break;
957 default:
958 BAD_CASE (state);
959 }
960 break;
961
962 case LEX_IS_TWOCHAR_COMMENT_1ST:
963 ch2 = GET ();
964 if (ch2 == '*')
965 {
966 for (;;)
967 {
968 do
969 {
970 ch2 = GET ();
971 if (ch2 != EOF && IS_NEWLINE (ch2))
972 add_newlines++;
973 }
974 while (ch2 != EOF && ch2 != '*');
975
976 while (ch2 == '*')
977 ch2 = GET ();
978
979 if (ch2 == EOF || ch2 == '/')
980 break;
981
982 /* This UNGET will ensure that we count newlines
983 correctly. */
984 UNGET (ch2);
985 }
986
987 if (ch2 == EOF)
988 as_warn (_("end of file in multiline comment"));
989
990 ch = ' ';
991 goto recycle;
992 }
993 #ifdef DOUBLESLASH_LINE_COMMENTS
994 else if (ch2 == '/')
995 {
996 do
997 {
998 ch = GET ();
999 }
1000 while (ch != EOF && !IS_NEWLINE (ch));
1001 if (ch == EOF)
1002 as_warn ("end of file in comment; newline inserted");
1003 state = 0;
1004 PUT ('\n');
1005 break;
1006 }
1007 #endif
1008 else
1009 {
1010 if (ch2 != EOF)
1011 UNGET (ch2);
1012 if (state == 9 || state == 10)
1013 state = 3;
1014 PUT (ch);
1015 }
1016 break;
1017
1018 case LEX_IS_STRINGQUOTE:
1019 quotechar = ch;
1020 if (state == 10)
1021 {
1022 /* Preserve the whitespace in foo "bar". */
1023 UNGET (ch);
1024 state = 3;
1025 PUT (' ');
1026
1027 /* PUT didn't jump out. We could just break, but we
1028 know what will happen, so optimize a bit. */
1029 ch = GET ();
1030 old_state = 3;
1031 }
1032 else if (state == 9)
1033 old_state = 3;
1034 else
1035 old_state = state;
1036 state = 5;
1037 PUT (ch);
1038 break;
1039
1040 #ifndef IEEE_STYLE
1041 case LEX_IS_ONECHAR_QUOTE:
1042 #ifdef H_TICK_HEX
1043 if (state == 9 && enable_h_tick_hex)
1044 {
1045 char c;
1046
1047 c = GET ();
1048 as_warn ("'%c found after symbol", c);
1049 UNGET (c);
1050 }
1051 #endif
1052 if (state == 10)
1053 {
1054 /* Preserve the whitespace in foo 'b'. */
1055 UNGET (ch);
1056 state = 3;
1057 PUT (' ');
1058 break;
1059 }
1060 ch = GET ();
1061 if (ch == EOF)
1062 {
1063 as_warn (_("end of file after a one-character quote; \\0 inserted"));
1064 ch = 0;
1065 }
1066 if (ch == '\\')
1067 {
1068 ch = GET ();
1069 if (ch == EOF)
1070 {
1071 as_warn (_("end of file in escape character"));
1072 ch = '\\';
1073 }
1074 else
1075 ch = process_escape (ch);
1076 }
1077 sprintf (out_buf, "%d", (int) (unsigned char) ch);
1078
1079 /* None of these 'x constants for us. We want 'x'. */
1080 if ((ch = GET ()) != '\'')
1081 {
1082 #ifdef REQUIRE_CHAR_CLOSE_QUOTE
1083 as_warn (_("missing close quote; (assumed)"));
1084 #else
1085 if (ch != EOF)
1086 UNGET (ch);
1087 #endif
1088 }
1089 if (strlen (out_buf) == 1)
1090 {
1091 PUT (out_buf[0]);
1092 break;
1093 }
1094 if (state == 9)
1095 old_state = 3;
1096 else
1097 old_state = state;
1098 state = -1;
1099 out_string = out_buf;
1100 PUT (*out_string++);
1101 break;
1102 #endif
1103
1104 case LEX_IS_COLON:
1105 #ifdef KEEP_WHITE_AROUND_COLON
1106 state = 9;
1107 #else
1108 if (state == 9 || state == 10)
1109 state = 3;
1110 else if (state != 3)
1111 state = 1;
1112 #endif
1113 PUT (ch);
1114 break;
1115
1116 case LEX_IS_NEWLINE:
1117 /* Roll out a bunch of newlines from inside comments, etc. */
1118 if (add_newlines)
1119 {
1120 --add_newlines;
1121 UNGET (ch);
1122 }
1123 /* Fall through. */
1124
1125 case LEX_IS_LINE_SEPARATOR:
1126 state = 0;
1127 PUT (ch);
1128 break;
1129
1130 case LEX_IS_PARALLEL_SEPARATOR:
1131 state = 1;
1132 PUT (ch);
1133 break;
1134
1135 #ifdef TC_V850
1136 case LEX_IS_DOUBLEDASH_1ST:
1137 ch2 = GET ();
1138 if (ch2 != '-')
1139 {
1140 if (ch2 != EOF)
1141 UNGET (ch2);
1142 goto de_fault;
1143 }
1144 /* Read and skip to end of line. */
1145 do
1146 {
1147 ch = GET ();
1148 }
1149 while (ch != EOF && ch != '\n');
1150
1151 if (ch == EOF)
1152 as_warn (_("end of file in comment; newline inserted"));
1153
1154 state = 0;
1155 PUT ('\n');
1156 break;
1157 #endif
1158 #ifdef DOUBLEBAR_PARALLEL
1159 case LEX_IS_DOUBLEBAR_1ST:
1160 ch2 = GET ();
1161 if (ch2 != EOF)
1162 UNGET (ch2);
1163 if (ch2 != '|')
1164 goto de_fault;
1165
1166 /* Handle '||' in two states as invoking PUT twice might
1167 result in the first one jumping out of this loop. We'd
1168 then lose track of the state and one '|' char. */
1169 state = 13;
1170 PUT ('|');
1171 break;
1172 #endif
1173 case LEX_IS_LINE_COMMENT_START:
1174 /* FIXME-someday: The two character comment stuff was badly
1175 thought out. On i386, we want '/' as line comment start
1176 AND we want C style comments. hence this hack. The
1177 whole lexical process should be reworked. xoxorich. */
1178 if (ch == '/')
1179 {
1180 ch2 = GET ();
1181 if (ch2 == '*')
1182 {
1183 old_state = 3;
1184 state = -2;
1185 break;
1186 }
1187 else
1188 {
1189 UNGET (ch2);
1190 }
1191 }
1192
1193 if (state == 0 || state == 1) /* Only comment at start of line. */
1194 {
1195 int startch;
1196
1197 startch = ch;
1198
1199 do
1200 {
1201 ch = GET ();
1202 }
1203 while (ch != EOF && IS_WHITESPACE (ch));
1204
1205 if (ch == EOF)
1206 {
1207 as_warn (_("end of file in comment; newline inserted"));
1208 PUT ('\n');
1209 break;
1210 }
1211
1212 if (ch < '0' || ch > '9' || state != 0 || startch != '#')
1213 {
1214 /* Not a cpp line. */
1215 while (ch != EOF && !IS_NEWLINE (ch))
1216 ch = GET ();
1217 if (ch == EOF)
1218 {
1219 as_warn (_("end of file in comment; newline inserted"));
1220 PUT ('\n');
1221 }
1222 else /* IS_NEWLINE (ch) */
1223 {
1224 /* To process non-zero add_newlines. */
1225 UNGET (ch);
1226 }
1227 state = 0;
1228 break;
1229 }
1230 /* Looks like `# 123 "filename"' from cpp. */
1231 UNGET (ch);
1232 old_state = 4;
1233 state = -1;
1234 if (scrub_m68k_mri)
1235 out_string = "\tlinefile ";
1236 else
1237 out_string = "\t.linefile ";
1238 PUT (*out_string++);
1239 break;
1240 }
1241
1242 #ifdef TC_D10V
1243 /* All insns end in a char for which LEX_IS_SYMBOL_COMPONENT is true.
1244 Trap is the only short insn that has a first operand that is
1245 neither register nor label.
1246 We must prevent exef0f ||trap #1 to degenerate to exef0f ||trap#1 .
1247 We can't make '#' LEX_IS_SYMBOL_COMPONENT because it is
1248 already LEX_IS_LINE_COMMENT_START. However, it is the
1249 only character in line_comment_chars for d10v, hence we
1250 can recognize it as such. */
1251 /* An alternative approach would be to reset the state to 1 when
1252 we see '||', '<'- or '->', but that seems to be overkill. */
1253 if (state == 10)
1254 PUT (' ');
1255 #endif
1256 /* We have a line comment character which is not at the
1257 start of a line. If this is also a normal comment
1258 character, fall through. Otherwise treat it as a default
1259 character. */
1260 if (strchr (tc_comment_chars, ch) == NULL
1261 && (! scrub_m68k_mri
1262 || (ch != '!' && ch != '*')))
1263 goto de_fault;
1264 if (scrub_m68k_mri
1265 && (ch == '!' || ch == '*' || ch == '#')
1266 && state != 1
1267 && state != 10)
1268 goto de_fault;
1269 /* Fall through. */
1270 case LEX_IS_COMMENT_START:
1271 #if defined TC_ARM && defined OBJ_ELF
1272 /* On the ARM, `@' is the comment character.
1273 Unfortunately this is also a special character in ELF .symver
1274 directives (and .type, though we deal with those another way).
1275 So we check if this line is such a directive, and treat
1276 the character as default if so. This is a hack. */
1277 if ((symver_state != NULL) && (*symver_state == 0))
1278 goto de_fault;
1279 #endif
1280
1281 #ifdef TC_ARM
1282 /* For the ARM, care is needed not to damage occurrences of \@
1283 by stripping the @ onwards. Yuck. */
1284 if (to > tostart && *(to - 1) == '\\')
1285 /* Do not treat the @ as a start-of-comment. */
1286 goto de_fault;
1287 #endif
1288
1289 #ifdef WARN_COMMENTS
1290 if (!found_comment)
1291 as_where (&found_comment_file, &found_comment);
1292 #endif
1293 do
1294 {
1295 ch = GET ();
1296 }
1297 while (ch != EOF && !IS_NEWLINE (ch));
1298 if (ch == EOF)
1299 as_warn (_("end of file in comment; newline inserted"));
1300 state = 0;
1301 PUT ('\n');
1302 break;
1303
1304 #ifdef H_TICK_HEX
1305 case LEX_IS_H:
1306 /* Look for strings like H'[0-9A-Fa-f] and if found, replace
1307 the H' with 0x to make them gas-style hex characters. */
1308 if (enable_h_tick_hex)
1309 {
1310 char quot;
1311
1312 quot = GET ();
1313 if (quot == '\'')
1314 {
1315 UNGET ('x');
1316 ch = '0';
1317 }
1318 else
1319 UNGET (quot);
1320 }
1321 /* FALL THROUGH */
1322 #endif
1323
1324 case LEX_IS_SYMBOL_COMPONENT:
1325 if (state == 10)
1326 {
1327 /* This is a symbol character following another symbol
1328 character, with whitespace in between. We skipped
1329 the whitespace earlier, so output it now. */
1330 UNGET (ch);
1331 state = 3;
1332 PUT (' ');
1333 break;
1334 }
1335
1336 #ifdef TC_Z80
1337 /* "af'" is a symbol containing '\''. */
1338 if (state == 3 && (ch == 'a' || ch == 'A'))
1339 {
1340 state = 16;
1341 PUT (ch);
1342 ch = GET ();
1343 if (ch == 'f' || ch == 'F')
1344 {
1345 state = 17;
1346 PUT (ch);
1347 break;
1348 }
1349 else
1350 {
1351 state = 9;
1352 if (ch == EOF || !IS_SYMBOL_COMPONENT (ch))
1353 {
1354 if (ch != EOF)
1355 UNGET (ch);
1356 break;
1357 }
1358 }
1359 }
1360 #endif
1361 if (state == 3)
1362 state = 9;
1363
1364 /* This is a common case. Quickly copy CH and all the
1365 following symbol component or normal characters. */
1366 if (to + 1 < toend
1367 && mri_state == NULL
1368 #if defined TC_ARM && defined OBJ_ELF
1369 && symver_state == NULL
1370 #endif
1371 )
1372 {
1373 char *s;
1374 ptrdiff_t len;
1375
1376 for (s = from; s < fromend; s++)
1377 {
1378 int type;
1379
1380 ch2 = *(unsigned char *) s;
1381 type = lex[ch2];
1382 if (type != 0
1383 && type != LEX_IS_SYMBOL_COMPONENT)
1384 break;
1385 }
1386
1387 if (s > from)
1388 /* Handle the last character normally, for
1389 simplicity. */
1390 --s;
1391
1392 len = s - from;
1393
1394 if (len > (toend - to) - 1)
1395 len = (toend - to) - 1;
1396
1397 if (len > 0)
1398 {
1399 PUT (ch);
1400 memcpy (to, from, len);
1401 to += len;
1402 from += len;
1403 if (to >= toend)
1404 goto tofull;
1405 ch = GET ();
1406 }
1407 }
1408
1409 /* Fall through. */
1410 default:
1411 de_fault:
1412 /* Some relatively `normal' character. */
1413 if (state == 0)
1414 {
1415 state = 11; /* Now seeing label definition. */
1416 }
1417 else if (state == 1)
1418 {
1419 state = 2; /* Ditto. */
1420 }
1421 else if (state == 9)
1422 {
1423 if (!IS_SYMBOL_COMPONENT (ch))
1424 state = 3;
1425 }
1426 else if (state == 10)
1427 {
1428 if (ch == '\\')
1429 {
1430 /* Special handling for backslash: a backslash may
1431 be the beginning of a formal parameter (of a
1432 macro) following another symbol character, with
1433 whitespace in between. If that is the case, we
1434 output a space before the parameter. Strictly
1435 speaking, correct handling depends upon what the
1436 macro parameter expands into; if the parameter
1437 expands into something which does not start with
1438 an operand character, then we don't want to keep
1439 the space. We don't have enough information to
1440 make the right choice, so here we are making the
1441 choice which is more likely to be correct. */
1442 if (to + 1 >= toend)
1443 {
1444 /* If we're near the end of the buffer, save the
1445 character for the next time round. Otherwise
1446 we'll lose our state. */
1447 UNGET (ch);
1448 goto tofull;
1449 }
1450 *to++ = ' ';
1451 }
1452
1453 state = 3;
1454 }
1455 PUT (ch);
1456 break;
1457 }
1458 }
1459
1460 /*NOTREACHED*/
1461
1462 fromeof:
1463 /* We have reached the end of the input. */
1464 return to - tostart;
1465
1466 tofull:
1467 /* The output buffer is full. Save any input we have not yet
1468 processed. */
1469 if (fromend > from)
1470 {
1471 saved_input = from;
1472 saved_input_len = fromend - from;
1473 }
1474 else
1475 saved_input = NULL;
1476
1477 return to - tostart;
1478 }
This page took 0.058132 seconds and 4 git commands to generate.