Changes to let cons handle bignums like general expressions.
[external/binutils.git] / gas / read.c
1 /* read.c - read a source file -
2    Copyright (C) 1986, 1987, 1990, 1991, 1993 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 2, or (at your option)
9 any later version.
10
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.
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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #if 0
21 #define MASK_CHAR (0xFF)        /* If your chars aren't 8 bits, you will
22                                    change this a bit.  But then, GNU isn't
23                                    spozed to run on your machine anyway.
24                                    (RMS is so shortsighted sometimes.)
25                                    */
26 #else
27 #define MASK_CHAR ((int)(unsigned char)-1)
28 #endif
29
30
31 /* This is the largest known floating point format (for now). It will
32    grow when we do 4361 style flonums. */
33
34 #define MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT (16)
35
36 /* Routines that read assembler source text to build spagetti in memory.
37    Another group of these functions is in the expr.c module.  */
38
39 /* for isdigit() */
40 #include <ctype.h>
41
42 #include "as.h"
43 #include "subsegs.h"
44
45 #include "obstack.h"
46 #include "listing.h"
47
48 /* We need this, despite the apparent object format dependency, since
49    it defines stab types, which all object formats can use now. */
50
51 #include "aout/stab_gnu.h"
52
53 #ifndef TC_START_LABEL
54 #define TC_START_LABEL(x,y) (x==':')
55 #endif
56
57 /* The NOP_OPCODE is for the alignment fill value.
58  * fill it a nop instruction so that the disassembler does not choke
59  * on it
60  */
61 #ifndef NOP_OPCODE
62 #define NOP_OPCODE 0x00
63 #endif
64
65 char *input_line_pointer;       /*->next char of source file to parse. */
66
67 #if BITS_PER_CHAR != 8
68 /*  The following table is indexed by[(char)] and will break if
69     a char does not have exactly 256 states (hopefully 0:255!)!  */
70 die horribly;
71 #endif
72
73 #ifndef LEX_AT
74 /* The m88k unfortunately uses @ as a label beginner.  */
75 #define LEX_AT 0
76 #endif
77
78 /* used by is_... macros. our ctype[] */
79 const char lex_type[256] =
80 {
81   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* @ABCDEFGHIJKLMNO */
82   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* PQRSTUVWXYZ[\]^_ */
83   0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,       /* _!"#$%&'()*+,-./ */
84   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,       /* 0123456789:;<=>? */
85   LEX_AT, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,  /* @ABCDEFGHIJKLMNO */
86   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3,       /* PQRSTUVWXYZ[\]^_ */
87   0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,       /* `abcdefghijklmno */
88   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0,       /* pqrstuvwxyz{|}~. */
89   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 };
97
98
99 /*
100  * In: a character.
101  * Out: 1 if this character ends a line.
102  */
103 #define _ (0)
104 char is_end_of_line[256] =
105 {
106 #ifdef CR_EOL
107   _, _, _, _, _, _, _, _, _, _, 99, _, _, 99, _, _,     /* @abcdefghijklmno */
108 #else
109   _, _, _, _, _, _, _, _, _, _, 99, _, _, _, _, _,      /* @abcdefghijklmno */
110 #endif
111   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
112 #ifdef TC_HPPA
113   _,99, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* _!"#$%&'()*+,-./ */
114   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* 0123456789:;<=>? */
115 #else
116   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
117   _, _, _, _, _, _, _, _, _, _, _, 99, _, _, _, _,      /* 0123456789:;<=>? */
118 #endif
119   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
120   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
121   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
122   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
123   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
124   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
125   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
126   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
127   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,       /* */
128 };
129 #undef _
130
131 /* Functions private to this file. */
132
133 static char *buffer;    /* 1st char of each buffer of lines is here. */
134 static char *buffer_limit;      /*->1 + last char in buffer. */
135
136 int target_big_endian;
137
138 static char *old_buffer;        /* JF a hack */
139 static char *old_input;
140 static char *old_limit;
141
142 /* Variables for handling include file directory list. */
143
144 char **include_dirs;    /* List of pointers to directories to
145                            search for .include's */
146 int include_dir_count;  /* How many are in the list */
147 int include_dir_maxlen = 1;/* Length of longest in list */
148
149 #ifndef WORKING_DOT_WORD
150 struct broken_word *broken_words;
151 int new_broken_words;
152 #endif
153
154 static char *demand_copy_string PARAMS ((int *lenP));
155 int is_it_end_of_statement PARAMS ((void));
156 static segT get_segmented_expression PARAMS ((expressionS *expP));
157 static segT get_known_segmented_expression PARAMS ((expressionS * expP));
158 static void pobegin PARAMS ((void));
159 \f
160
161 void
162 read_begin ()
163 {
164   const char *p;
165
166   pobegin ();
167   obj_read_begin_hook ();
168
169   /* Something close -- but not too close -- to a multiple of 1024.
170      The debugging malloc I'm using has 24 bytes of overhead.  */
171   obstack_begin (&notes, 5090);
172   obstack_begin (&cond_obstack, 990);
173
174   /* Use machine dependent syntax */
175   for (p = line_separator_chars; *p; p++)
176     is_end_of_line[(unsigned char) *p] = 1;
177   /* Use more.  FIXME-SOMEDAY. */
178 }
179 \f
180 /* set up pseudo-op tables */
181
182 struct hash_control *po_hash;
183
184 static const pseudo_typeS potable[] =
185 {
186   {"abort", s_abort, 0},
187   {"align", s_align_ptwo, 0},
188   {"ascii", stringer, 0},
189   {"asciz", stringer, 1},
190 /* block */
191   {"byte", cons, 1},
192   {"comm", s_comm, 0},
193   {"data", s_data, 0},
194 #ifdef S_SET_DESC
195   {"desc", s_desc, 0},
196 #endif
197 /* dim */
198   {"double", float_cons, 'd'},
199 /* dsect */
200   {"eject", listing_eject, 0},  /* Formfeed listing */
201   {"else", s_else, 0},
202   {"end", s_end, 0},
203   {"endif", s_endif, 0},
204 /* endef */
205   {"equ", s_set, 0},
206 /* err */
207 /* extend */
208   {"extern", s_ignore, 0},      /* We treat all undef as ext */
209   {"appfile", s_app_file, 1},
210   {"appline", s_app_line, 0},
211   {"file", s_app_file, 0},
212   {"fill", s_fill, 0},
213   {"float", float_cons, 'f'},
214   {"global", s_globl, 0},
215   {"globl", s_globl, 0},
216   {"hword", cons, 2},
217   {"if", s_if, 0},
218   {"ifdef", s_ifdef, 0},
219   {"ifeqs", s_ifeqs, 0},
220   {"ifndef", s_ifdef, 1},
221   {"ifnes", s_ifeqs, 1},
222   {"ifnotdef", s_ifdef, 1},
223   {"include", s_include, 0},
224   {"int", cons, 4},
225   {"lcomm", s_lcomm, 0},
226   {"lflags", listing_flags, 0}, /* Listing flags */
227   {"list", listing_list, 1},    /* Turn listing on */
228   {"long", cons, 4},
229   {"lsym", s_lsym, 0},
230   {"nolist", listing_list, 0},  /* Turn listing off */
231   {"octa", cons, 16},
232   {"org", s_org, 0},
233   {"psize", listing_psize, 0},  /* set paper size */
234 /* print */
235   {"quad", cons, 8},
236   {"sbttl", listing_title, 1},  /* Subtitle of listing */
237 /* scl */
238 /* sect */
239   {"set", s_set, 0},
240   {"short", cons, 2},
241   {"single", float_cons, 'f'},
242 /* size */
243   {"space", s_space, 0},
244   {"stabd", s_stab, 'd'},
245   {"stabn", s_stab, 'n'},
246   {"stabs", s_stab, 's'},
247   {"string", stringer, 1},
248 /* tag */
249   {"text", s_text, 0},
250   {"title", listing_title, 0},  /* Listing title */
251 /* type */
252 /* use */
253 /* val */
254   {"xstabs", s_xstab, 's'},
255   {"word", cons, 2},
256   {NULL}                        /* end sentinel */
257 };
258
259 static void 
260 pobegin ()
261 {
262   const char *errtxt;                   /* error text */
263   const pseudo_typeS *pop;
264
265   po_hash = hash_new ();
266
267   /* Do the target-specific pseudo ops. */
268   for (pop = md_pseudo_table; pop->poc_name; pop++)
269     {
270       errtxt = hash_insert (po_hash, pop->poc_name, (char *) pop);
271       if (errtxt)
272         {
273           as_fatal ("error constructing md pseudo-op table");
274         }                       /* on error */
275     }                           /* for each op */
276
277   /* Now object specific.  Skip any that were in the target table. */
278   for (pop = obj_pseudo_table; pop->poc_name; pop++)
279     {
280       errtxt = hash_insert (po_hash, pop->poc_name, (char *) pop);
281       if (errtxt)
282         {
283           if (!strcmp (errtxt, "exists"))
284             {
285 #ifdef DIE_ON_OVERRIDES
286               as_fatal ("pseudo op \".%s\" overridden.\n", pop->poc_name);
287 #endif /* DIE_ON_OVERRIDES */
288               continue;         /* OK if target table overrides. */
289             }
290           else
291             {
292               as_fatal ("error constructing obj pseudo-op table");
293             }                   /* if overridden */
294         }                       /* on error */
295     }                           /* for each op */
296
297   /* Now portable ones.  Skip any that we've seen already. */
298   for (pop = potable; pop->poc_name; pop++)
299     {
300       errtxt = hash_insert (po_hash, pop->poc_name, (char *) pop);
301       if (errtxt)
302         {
303           if (!strcmp (errtxt, "exists"))
304             {
305 #ifdef DIE_ON_OVERRIDES
306               as_fatal ("pseudo op \".%s\" overridden.\n", pop->poc_name);
307 #endif /* DIE_ON_OVERRIDES */
308               continue;         /* OK if target table overrides. */
309             }
310           else
311             {
312               as_fatal ("error constructing obj pseudo-op table");
313             }                   /* if overridden */
314         }                       /* on error */
315     }                           /* for each op */
316
317   return;
318 }                               /* pobegin() */
319 \f
320 #define HANDLE_CONDITIONAL_ASSEMBLY()                                   \
321   if (ignore_input ())                                                  \
322     {                                                                   \
323       while (! is_end_of_line[(unsigned char) *input_line_pointer++])   \
324         if (input_line_pointer == buffer_limit)                         \
325           break;                                                        \
326       continue;                                                         \
327     }
328
329
330 /*      read_a_source_file()
331  *
332  * We read the file, putting things into a web that
333  * represents what we have been reading.
334  */
335 void 
336 read_a_source_file (name)
337      char *name;
338 {
339   register char c;
340   register char *s;             /* string of symbol, '\0' appended */
341   register int temp;
342   pseudo_typeS *pop;
343
344   buffer = input_scrub_new_file (name);
345
346   listing_file (name);
347   listing_newline ("");
348
349   while ((buffer_limit = input_scrub_next_buffer (&input_line_pointer)) != 0)
350     {                           /* We have another line to parse. */
351       know (buffer_limit[-1] == '\n');  /* Must have a sentinel. */
352     contin:                     /* JF this goto is my fault I admit it.
353                                    Someone brave please re-write the whole
354                                    input section here?  Pleeze???  */
355       while (input_line_pointer < buffer_limit)
356         {
357           /* We have more of this buffer to parse. */
358
359           /*
360            * We now have input_line_pointer->1st char of next line.
361            * If input_line_pointer [-1] == '\n' then we just
362            * scanned another line: so bump line counters.
363            */
364           if (input_line_pointer[-1] == '\n')
365             {
366               bump_line_counters ();
367
368 #ifdef MRI
369               /* Text at the start of a line must be a label, we run down and stick a colon in */
370               if (is_name_beginner (*input_line_pointer))
371                 {
372                   char *line_start = input_line_pointer;
373                   char c = get_symbol_end ();
374                   colon (line_start);
375                   *input_line_pointer = c;
376                   if (c == ':')
377                     input_line_pointer++;
378
379                 }
380 #endif
381             }
382
383
384           /*
385            * We are at the begining of a line, or similar place.
386            * We expect a well-formed assembler statement.
387            * A "symbol-name:" is a statement.
388            *
389            * Depending on what compiler is used, the order of these tests
390            * may vary to catch most common case 1st.
391            * Each test is independent of all other tests at the (top) level.
392            * PLEASE make a compiler that doesn't use this assembler.
393            * It is crufty to waste a compiler's time encoding things for this
394            * assembler, which then wastes more time decoding it.
395            * (And communicating via (linear) files is silly!
396            * If you must pass stuff, please pass a tree!)
397            */
398           if ((c = *input_line_pointer++) == '\t'
399               || c == ' '
400               || c == '\f'
401               || c == 0)
402             {
403               c = *input_line_pointer++;
404             }
405           know (c != ' ');      /* No further leading whitespace. */
406           LISTING_NEWLINE ();
407           /*
408            * C is the 1st significant character.
409            * Input_line_pointer points after that character.
410            */
411           if (is_name_beginner (c))
412             {                   /* want user-defined label or pseudo/opcode */
413               HANDLE_CONDITIONAL_ASSEMBLY ();
414
415               s = --input_line_pointer;
416               c = get_symbol_end ();    /* name's delimiter */
417               /*
418                * C is character after symbol.
419                * That character's place in the input line is now '\0'.
420                * S points to the beginning of the symbol.
421                *   [In case of pseudo-op, s->'.'.]
422                * Input_line_pointer->'\0' where c was.
423                */
424               if (TC_START_LABEL(c, input_line_pointer))
425                 {
426                   colon (s);    /* user-defined label */
427                   *input_line_pointer++ = ':';  /* Put ':' back for error messages' sake. */
428                   /* Input_line_pointer->after ':'. */
429                   SKIP_WHITESPACE ();
430
431
432                 }
433               else if (c == '='
434                        || (input_line_pointer[1] == '='
435 #ifdef TC_EQUAL_IN_INSN
436                            && ! TC_EQUAL_IN_INSN (c, input_line_pointer)
437 #endif
438                            ))
439                 {
440                   equals (s);
441                   demand_empty_rest_of_line ();
442                 }
443               else
444                 {               /* expect pseudo-op or machine instruction */
445 #ifdef MRI
446                   if (!done_pseudo (s))
447
448 #else
449
450                   pop = NULL;
451
452 #ifdef NO_PSEUDO_DOT
453                   /* The m88k uses pseudo-ops without a period.  */
454                   pop = (pseudo_typeS *) hash_find (po_hash, s);
455                   if (pop != NULL && pop->poc_handler == NULL)
456                     pop = NULL;
457 #endif
458
459                   if (pop != NULL || *s == '.')
460                     {
461                       /*
462                        * PSEUDO - OP.
463                        *
464                        * WARNING: c has next char, which may be end-of-line.
465                        * We lookup the pseudo-op table with s+1 because we
466                        * already know that the pseudo-op begins with a '.'.
467                        */
468
469                       if (pop == NULL)
470                         pop = (pseudo_typeS *) hash_find (po_hash, s + 1);
471
472                       /* Print the error msg now, while we still can */
473                       if (pop == NULL)
474                         {
475                           as_bad ("Unknown pseudo-op:  `%s'", s);
476                           *input_line_pointer = c;
477                           s_ignore (0);
478                           break;
479                         }
480
481                       /* Put it back for error messages etc. */
482                       *input_line_pointer = c;
483                       /* The following skip of whitespace is compulsory.
484                          A well shaped space is sometimes all that separates
485                          keyword from operands. */
486                       if (c == ' ' || c == '\t')
487                         {
488                           input_line_pointer++;
489                         }       /* Skip seperator after keyword. */
490                       /*
491                        * Input_line is restored.
492                        * Input_line_pointer->1st non-blank char
493                        * after pseudo-operation.
494                        */
495                       if (!pop)
496                         {
497                           ignore_rest_of_line ();
498                           break;
499                         }
500                       else
501                         {
502                           (*pop->poc_handler) (pop->poc_val);
503                         }       /* if we have one */
504                     }
505                   else
506 #endif
507                     {           /* machine instruction */
508                       /* WARNING: c has char, which may be end-of-line. */
509                       /* Also: input_line_pointer->`\0` where c was. */
510                       *input_line_pointer = c;
511                       while (!is_end_of_line[(unsigned char) *input_line_pointer]
512 #ifdef TC_EOL_IN_INSN
513                              || TC_EOL_IN_INSN (input_line_pointer)
514 #endif
515                              )
516                         {
517                           input_line_pointer++;
518                         }
519
520                       c = *input_line_pointer;
521                       *input_line_pointer = '\0';
522
523                       md_assemble (s);  /* Assemble 1 instruction. */
524
525                       *input_line_pointer++ = c;
526
527                       /* We resume loop AFTER the end-of-line from this instruction */
528                     }           /* if (*s=='.') */
529
530                 }               /* if c==':' */
531               continue;
532             }                   /* if (is_name_beginner(c) */
533
534
535           if (is_end_of_line[(unsigned char) c])
536             {
537               continue;
538             }                   /* empty statement */
539
540
541 #if defined(LOCAL_LABELS_DOLLAR) || defined(LOCAL_LABELS_FB)
542           if (isdigit (c))
543             {                   /* local label  ("4:") */
544               char *backup = input_line_pointer;
545
546               HANDLE_CONDITIONAL_ASSEMBLY ();
547
548               temp = c - '0';
549
550               while (isdigit (*input_line_pointer))
551                 {
552                   temp = (temp * 10) + *input_line_pointer - '0';
553                   ++input_line_pointer;
554                 }               /* read the whole number */
555
556 #ifdef LOCAL_LABELS_DOLLAR
557               if (*input_line_pointer == '$'
558                   && *(input_line_pointer + 1) == ':')
559                 {
560                   input_line_pointer += 2;
561
562                   if (dollar_label_defined (temp))
563                     {
564                       as_fatal ("label \"%d$\" redefined", temp);
565                     }
566
567                   define_dollar_label (temp);
568                   colon (dollar_label_name (temp, 0));
569                   continue;
570                 }
571 #endif /* LOCAL_LABELS_DOLLAR */
572
573 #ifdef LOCAL_LABELS_FB
574               if (*input_line_pointer++ == ':')
575                 {
576                   fb_label_instance_inc (temp);
577                   colon (fb_label_name (temp, 0));
578                   continue;
579                 }
580 #endif /* LOCAL_LABELS_FB */
581
582               input_line_pointer = backup;
583             }                   /* local label  ("4:") */
584 #endif /* LOCAL_LABELS_DOLLAR or LOCAL_LABELS_FB */
585
586           if (c && strchr (line_comment_chars, c))
587             {                   /* Its a comment.  Better say APP or NO_APP */
588               char *ends;
589               char *new_buf;
590               char *new_tmp;
591               unsigned int new_length;
592               char *tmp_buf = 0;
593               extern char *scrub_string, *scrub_last_string;
594
595               bump_line_counters ();
596               s = input_line_pointer;
597               if (strncmp (s, "APP\n", 4))
598                 continue;       /* We ignore it */
599               s += 4;
600
601               ends = strstr (s, "#NO_APP\n");
602
603               if (!ends)
604                 {
605                   unsigned int tmp_len;
606                   unsigned int num;
607
608                   /* The end of the #APP wasn't in this buffer.  We
609                      keep reading in buffers until we find the #NO_APP
610                      that goes with this #APP  There is one.  The specs
611                      guarentee it. . . */
612                   tmp_len = buffer_limit - s;
613                   tmp_buf = xmalloc (tmp_len + 1);
614                   memcpy (tmp_buf, s, tmp_len);
615                   do
616                     {
617                       new_tmp = input_scrub_next_buffer (&buffer);
618                       if (!new_tmp)
619                         break;
620                       else
621                         buffer_limit = new_tmp;
622                       input_line_pointer = buffer;
623                       ends = strstr (buffer, "#NO_APP\n");
624                       if (ends)
625                         num = ends - buffer;
626                       else
627                         num = buffer_limit - buffer;
628
629                       tmp_buf = xrealloc (tmp_buf, tmp_len + num);
630                       memcpy (tmp_buf, buffer + tmp_len, num);
631                       tmp_len += num;
632                     }
633                   while (!ends);
634
635                   input_line_pointer = ends ? ends + 8 : NULL;
636
637                   s = tmp_buf;
638                   ends = s + tmp_len;
639
640                 }
641               else
642                 {
643                   input_line_pointer = ends + 8;
644                 }
645               new_buf = xmalloc (100);
646               new_length = 100;
647               new_tmp = new_buf;
648
649               scrub_string = s;
650               scrub_last_string = ends;
651               for (;;)
652                 {
653                   int ch;
654
655                   ch = do_scrub_next_char (scrub_from_string, scrub_to_string);
656                   if (ch == EOF)
657                     break;
658                   *new_tmp++ = ch;
659                   if (new_tmp == new_buf + new_length)
660                     {
661                       new_buf = xrealloc (new_buf, new_length + 100);
662                       new_tmp = new_buf + new_length;
663                       new_length += 100;
664                     }
665                 }
666
667               if (tmp_buf)
668                 free (tmp_buf);
669               old_buffer = buffer;
670               old_input = input_line_pointer;
671               old_limit = buffer_limit;
672               buffer = new_buf;
673               input_line_pointer = new_buf;
674               buffer_limit = new_tmp;
675               continue;
676             }
677
678           HANDLE_CONDITIONAL_ASSEMBLY ();
679
680           /* as_warn("Junk character %d.",c);  Now done by ignore_rest */
681           input_line_pointer--; /* Report unknown char as ignored. */
682           ignore_rest_of_line ();
683         }                       /* while (input_line_pointer<buffer_limit) */
684       if (old_buffer)
685         {
686           bump_line_counters ();
687           if (old_input != 0)
688             {
689               buffer = old_buffer;
690               input_line_pointer = old_input;
691               buffer_limit = old_limit;
692               old_buffer = 0;
693               goto contin;
694             }
695         }
696     }                           /* while (more buffers to scan) */
697   input_scrub_close ();         /* Close the input file */
698
699 }                               /* read_a_source_file() */
700
701 void 
702 s_abort (ignore)
703      int ignore;
704 {
705   as_fatal (".abort detected.  Abandoning ship.");
706 }                               /* s_abort() */
707
708 /* For machines where ".align 4" means align to a 4 byte boundary. */
709 void 
710 s_align_bytes (arg)
711      int arg;
712 {
713   register unsigned int temp;
714   register long temp_fill;
715   unsigned int i = 0;
716   unsigned long max_alignment = 1 << 15;
717
718   if (is_end_of_line[(unsigned char) *input_line_pointer])
719     temp = arg;                 /* Default value from pseudo-op table */
720   else
721     temp = get_absolute_expression ();
722
723   if (temp > max_alignment)
724     {
725       as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
726     }
727
728   /*
729      * For the sparc, `.align (1<<n)' actually means `.align n'
730      * so we have to convert it.
731      */
732   if (temp != 0)
733     {
734       for (i = 0; (temp & 1) == 0; temp >>= 1, ++i)
735         ;
736     }
737   if (temp != 1)
738     as_bad ("Alignment not a power of 2");
739
740   temp = i;
741   if (*input_line_pointer == ',')
742     {
743       input_line_pointer++;
744       temp_fill = get_absolute_expression ();
745     }
746   else if (now_seg != data_section && now_seg != bss_section)
747     temp_fill = NOP_OPCODE;
748   else
749     temp_fill = 0;
750   /* Only make a frag if we HAVE to. . . */
751   if (temp && !need_pass_2)
752     frag_align ((int) temp, (int) temp_fill);
753
754   record_alignment (now_seg, (int) temp);
755
756   demand_empty_rest_of_line ();
757 }                               /* s_align_bytes() */
758
759 /* For machines where ".align 4" means align to 2**4 boundary. */
760 void 
761 s_align_ptwo (ignore)
762      int ignore;
763 {
764   register int temp;
765   register long temp_fill;
766   long max_alignment = 15;
767
768   temp = get_absolute_expression ();
769   if (temp > max_alignment)
770     as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
771   else if (temp < 0)
772     {
773       as_bad ("Alignment negative. 0 assumed.");
774       temp = 0;
775     }
776   if (*input_line_pointer == ',')
777     {
778       input_line_pointer++;
779       temp_fill = get_absolute_expression ();
780     }
781   /* @@ Fix this right for BFD!  */
782   else if (now_seg != data_section && now_seg != bss_section)
783     temp_fill = NOP_OPCODE;
784   else
785     temp_fill = 0;
786   /* Only make a frag if we HAVE to. . . */
787   if (temp && !need_pass_2)
788     frag_align (temp, (int) temp_fill);
789
790   record_alignment (now_seg, temp);
791
792   demand_empty_rest_of_line ();
793 }                               /* s_align_ptwo() */
794
795 void 
796 s_comm (ignore)
797      int ignore;
798 {
799   register char *name;
800   register char c;
801   register char *p;
802   offsetT temp;
803   register symbolS *symbolP;
804
805   name = input_line_pointer;
806   c = get_symbol_end ();
807   /* just after name is now '\0' */
808   p = input_line_pointer;
809   *p = c;
810   SKIP_WHITESPACE ();
811   if (*input_line_pointer != ',')
812     {
813       as_bad ("Expected comma after symbol-name: rest of line ignored.");
814       ignore_rest_of_line ();
815       return;
816     }
817   input_line_pointer++;         /* skip ',' */
818   if ((temp = get_absolute_expression ()) < 0)
819     {
820       as_warn (".COMMon length (%ld.) <0! Ignored.", (long) temp);
821       ignore_rest_of_line ();
822       return;
823     }
824   *p = 0;
825   symbolP = symbol_find_or_make (name);
826   *p = c;
827   if (S_IS_DEFINED (symbolP))
828     {
829       as_bad ("Ignoring attempt to re-define symbol");
830       ignore_rest_of_line ();
831       return;
832     }
833   if (S_GET_VALUE (symbolP))
834     {
835       if (S_GET_VALUE (symbolP) != (valueT) temp)
836         as_bad ("Length of .comm \"%s\" is already %ld. Not changed to %ld.",
837                 S_GET_NAME (symbolP),
838                 (long) S_GET_VALUE (symbolP),
839                 (long) temp);
840     }
841   else
842     {
843       S_SET_VALUE (symbolP, (valueT) temp);
844       S_SET_EXTERNAL (symbolP);
845     }
846 #ifdef OBJ_VMS
847   if ( (!temp) || !flagseen['1'])
848     S_GET_OTHER(symbolP) = const_flag;
849 #endif /* not OBJ_VMS */
850   know (symbolP->sy_frag == &zero_address_frag);
851   demand_empty_rest_of_line ();
852 }                               /* s_comm() */
853
854 void
855 s_data (ignore)
856      int ignore;
857 {
858   segT section;
859   register int temp;
860
861   temp = get_absolute_expression ();
862   if (flagseen['R'])
863     {
864       section = text_section;
865       temp += 1000;
866     }
867   else
868     section = data_section;
869
870   subseg_set (section, (subsegT) temp);
871
872 #ifdef OBJ_VMS
873   const_flag = 0;
874 #endif
875   demand_empty_rest_of_line ();
876 }
877
878 /* Handle the .appfile pseudo-op.  This is automatically generated by
879    do_scrub_next_char when a preprocessor # line comment is seen with
880    a file name.  This default definition may be overridden by the
881    object or CPU specific pseudo-ops.  This function is also the
882    default definition for .file; the APPFILE argument is 1 for
883    .appfile, 0 for .file.  */
884
885 void 
886 s_app_file (appfile)
887      int appfile;
888 {
889   register char *s;
890   int length;
891
892   /* Some assemblers tolerate immediately following '"' */
893   if ((s = demand_copy_string (&length)) != 0)
894     {
895       /* If this is a fake .appfile, a fake newline was inserted into
896          the buffer.  Passing -2 to new_logical_line tells it to
897          account for it.  */
898       new_logical_line (s, appfile ? -2 : -1);
899       demand_empty_rest_of_line ();
900 #ifdef LISTING
901       if (listing)
902         listing_source_file (s);
903 #endif
904     }
905 #ifdef OBJ_COFF
906   c_dot_file_symbol (s);
907 #endif /* OBJ_COFF */
908 #ifdef OBJ_ELF
909   elf_file_symbol (s);
910 #endif
911 }
912
913 /* Handle the .appline pseudo-op.  This is automatically generated by
914    do_scrub_next_char when a preprocessor # line comment is seen.
915    This default definition may be overridden by the object or CPU
916    specific pseudo-ops.  */
917
918 void
919 s_app_line (ignore)
920      int ignore;
921 {
922   int l;
923
924   /* The given number is that of the next line.  */
925   l = get_absolute_expression () - 1;
926   new_logical_line ((char *) NULL, l);
927 #ifdef LISTING
928   if (listing)
929     listing_source_line (l);
930 #endif
931   demand_empty_rest_of_line ();
932 }
933
934 void 
935 s_fill (ignore)
936      int ignore;
937 {
938   long temp_repeat = 0;
939   long temp_size = 1;
940   register long temp_fill = 0;
941   char *p;
942
943
944   temp_repeat = get_absolute_expression ();
945   if (*input_line_pointer == ',')
946     {
947       input_line_pointer++;
948       temp_size = get_absolute_expression ();
949       if (*input_line_pointer == ',')
950         {
951           input_line_pointer++;
952           temp_fill = get_absolute_expression ();
953         }
954     }
955   /* This is to be compatible with BSD 4.2 AS, not for any rational reason.  */
956 #define BSD_FILL_SIZE_CROCK_8 (8)
957   if (temp_size > BSD_FILL_SIZE_CROCK_8)
958     {
959       as_warn (".fill size clamped to %d.", BSD_FILL_SIZE_CROCK_8);
960       temp_size = BSD_FILL_SIZE_CROCK_8;
961     }
962   if (temp_size < 0)
963     {
964       as_warn ("Size negative: .fill ignored.");
965       temp_size = 0;
966     }
967   else if (temp_repeat <= 0)
968     {
969       as_warn ("Repeat < 0, .fill ignored");
970       temp_size = 0;
971     }
972
973   if (temp_size && !need_pass_2)
974     {
975       p = frag_var (rs_fill, (int) temp_size, (int) temp_size, (relax_substateT) 0, (symbolS *) 0, temp_repeat, (char *) 0);
976       memset (p, 0, (unsigned int) temp_size);
977       /* The magic number BSD_FILL_SIZE_CROCK_4 is from BSD 4.2 VAX
978        * flavoured AS.  The following bizzare behaviour is to be
979        * compatible with above.  I guess they tried to take up to 8
980        * bytes from a 4-byte expression and they forgot to sign
981        * extend. Un*x Sux. */
982 #define BSD_FILL_SIZE_CROCK_4 (4)
983       md_number_to_chars (p, (valueT) temp_fill,
984                           (temp_size > BSD_FILL_SIZE_CROCK_4
985                            ? BSD_FILL_SIZE_CROCK_4
986                            : (int) temp_size));
987       /* Note: .fill (),0 emits no frag (since we are asked to .fill 0 bytes)
988        * but emits no error message because it seems a legal thing to do.
989        * It is a degenerate case of .fill but could be emitted by a compiler.
990        */
991     }
992   demand_empty_rest_of_line ();
993 }
994
995 void 
996 s_globl (ignore)
997      int ignore;
998 {
999   char *name;
1000   int c;
1001   symbolS *symbolP;
1002
1003   do
1004     {
1005       name = input_line_pointer;
1006       c = get_symbol_end ();
1007       symbolP = symbol_find_or_make (name);
1008       *input_line_pointer = c;
1009       SKIP_WHITESPACE ();
1010       S_SET_EXTERNAL (symbolP);
1011       if (c == ',')
1012         {
1013           input_line_pointer++;
1014           SKIP_WHITESPACE ();
1015           if (*input_line_pointer == '\n')
1016             c = '\n';
1017         }
1018     }
1019   while (c == ',');
1020   demand_empty_rest_of_line ();
1021 }
1022
1023 void 
1024 s_lcomm (needs_align)
1025      /* 1 if this was a ".bss" directive, which may require a 3rd argument
1026         (alignment); 0 if it was an ".lcomm" (2 args only)  */
1027      int needs_align;
1028 {
1029   register char *name;
1030   register char c;
1031   register char *p;
1032   register int temp;
1033   register symbolS *symbolP;
1034   segT current_seg = now_seg;
1035   subsegT current_subseg = now_subseg;
1036   const int max_alignment = 15;
1037   int align = 0;
1038   segT bss_seg = bss_section;
1039
1040   name = input_line_pointer;
1041   c = get_symbol_end ();
1042   p = input_line_pointer;
1043   *p = c;
1044   SKIP_WHITESPACE ();
1045   if (*input_line_pointer != ',')
1046     {
1047       as_bad ("Expected comma after name");
1048       ignore_rest_of_line ();
1049       return;
1050     }
1051
1052   ++input_line_pointer;
1053
1054   if (*input_line_pointer == '\n')
1055     {
1056       as_bad ("Missing size expression");
1057       return;
1058     }
1059
1060   if ((temp = get_absolute_expression ()) < 0)
1061     {
1062       as_warn ("BSS length (%d.) <0! Ignored.", temp);
1063       ignore_rest_of_line ();
1064       return;
1065     }
1066
1067 #ifdef TC_MIPS
1068 #ifdef OBJ_ECOFF
1069   /* For MIPS ECOFF, small objects are put in .sbss.  */
1070   if (temp <= bfd_get_gp_size (stdoutput))
1071     bss_seg = subseg_new (".sbss", 1);
1072 #endif
1073 #endif
1074
1075   if (needs_align)
1076     {
1077       align = 0;
1078       SKIP_WHITESPACE ();
1079       if (*input_line_pointer != ',')
1080         {
1081           as_bad ("Expected comma after size");
1082           ignore_rest_of_line ();
1083           return;
1084         }
1085       input_line_pointer++;
1086       SKIP_WHITESPACE ();
1087       if (*input_line_pointer == '\n')
1088         {
1089           as_bad ("Missing alignment");
1090           return;
1091         }
1092       align = get_absolute_expression ();
1093       if (align > max_alignment)
1094         {
1095           align = max_alignment;
1096           as_warn ("Alignment too large: %d. assumed.", align);
1097         }
1098       else if (align < 0)
1099         {
1100           align = 0;
1101           as_warn ("Alignment negative. 0 assumed.");
1102         }
1103       record_alignment (bss_seg, align);
1104     }                           /* if needs align */
1105
1106   *p = 0;
1107   symbolP = symbol_find_or_make (name);
1108   *p = c;
1109
1110   if (
1111 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
1112        S_GET_OTHER (symbolP) == 0 &&
1113        S_GET_DESC (symbolP) == 0 &&
1114 #endif /* OBJ_AOUT or OBJ_BOUT */
1115        (S_GET_SEGMENT (symbolP) == bss_seg
1116         || (!S_IS_DEFINED (symbolP) && S_GET_VALUE (symbolP) == 0)))
1117     {
1118       char *pfrag;
1119
1120       subseg_set (bss_seg, 1);
1121
1122       if (align)
1123         frag_align (align, 0);
1124                                         /* detach from old frag */
1125       if (S_GET_SEGMENT (symbolP) == bss_seg)
1126         symbolP->sy_frag->fr_symbol = NULL;
1127
1128       symbolP->sy_frag = frag_now;
1129       pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, symbolP,
1130                         temp, (char *)0);
1131       *pfrag = 0;
1132
1133       S_SET_SEGMENT (symbolP, bss_seg);
1134
1135 #ifdef OBJ_COFF
1136       /* The symbol may already have been created with a preceding
1137          ".globl" directive -- be careful not to step on storage class
1138          in that case.  Otherwise, set it to static. */
1139       if (S_GET_STORAGE_CLASS (symbolP) != C_EXT)
1140         {
1141           S_SET_STORAGE_CLASS (symbolP, C_STAT);
1142         }
1143 #endif /* OBJ_COFF */
1144     }
1145   else
1146     {
1147       as_bad ("Ignoring attempt to re-define symbol %s.", name);
1148     }
1149
1150   subseg_set (current_seg, current_subseg);
1151
1152   demand_empty_rest_of_line ();
1153 }                               /* s_lcomm() */
1154
1155 void 
1156 s_lsym (ignore)
1157      int ignore;
1158 {
1159   register char *name;
1160   register char c;
1161   register char *p;
1162   expressionS exp;
1163   register symbolS *symbolP;
1164
1165   /* we permit ANY defined expression: BSD4.2 demands constants */
1166   name = input_line_pointer;
1167   c = get_symbol_end ();
1168   p = input_line_pointer;
1169   *p = c;
1170   SKIP_WHITESPACE ();
1171   if (*input_line_pointer != ',')
1172     {
1173       *p = 0;
1174       as_bad ("Expected comma after name \"%s\"", name);
1175       *p = c;
1176       ignore_rest_of_line ();
1177       return;
1178     }
1179   input_line_pointer++;
1180   expression (&exp);
1181   if (exp.X_op != O_constant
1182       && exp.X_op != O_register)
1183     {
1184       as_bad ("bad expression");
1185       ignore_rest_of_line ();
1186       return;
1187     }
1188   *p = 0;
1189   symbolP = symbol_find_or_make (name);
1190
1191   /* FIXME-SOON I pulled a (&& symbolP->sy_other == 0 &&
1192      symbolP->sy_desc == 0) out of this test because coff doesn't have
1193      those fields, and I can't see when they'd ever be tripped.  I
1194      don't think I understand why they were here so I may have
1195      introduced a bug. As recently as 1.37 didn't have this test
1196      anyway.  xoxorich. */
1197
1198   if (S_GET_SEGMENT (symbolP) == undefined_section
1199       && S_GET_VALUE (symbolP) == 0)
1200     {
1201       /* The name might be an undefined .global symbol; be sure to
1202          keep the "external" bit. */
1203       S_SET_SEGMENT (symbolP,
1204                      (exp.X_op == O_constant
1205                       ? absolute_section
1206                       : reg_section));
1207       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
1208     }
1209   else
1210     {
1211       as_bad ("Symbol %s already defined", name);
1212     }
1213   *p = c;
1214   demand_empty_rest_of_line ();
1215 }                               /* s_lsym() */
1216
1217 void 
1218 s_org (ignore)
1219      int ignore;
1220 {
1221   register segT segment;
1222   expressionS exp;
1223   register long temp_fill;
1224   register char *p;
1225   /* Don't believe the documentation of BSD 4.2 AS.  There is no such
1226      thing as a sub-segment-relative origin.  Any absolute origin is
1227      given a warning, then assumed to be segment-relative.  Any
1228      segmented origin expression ("foo+42") had better be in the right
1229      segment or the .org is ignored.
1230
1231      BSD 4.2 AS warns if you try to .org backwards. We cannot because
1232      we never know sub-segment sizes when we are reading code.  BSD
1233      will crash trying to emit negative numbers of filler bytes in
1234      certain .orgs. We don't crash, but see as-write for that code.
1235
1236      Don't make frag if need_pass_2==1.  */
1237   segment = get_known_segmented_expression (&exp);
1238   if (*input_line_pointer == ',')
1239     {
1240       input_line_pointer++;
1241       temp_fill = get_absolute_expression ();
1242     }
1243   else
1244     temp_fill = 0;
1245   if (!need_pass_2)
1246     {
1247       if (segment != now_seg && segment != absolute_section)
1248         as_bad ("Invalid segment \"%s\". Segment \"%s\" assumed.",
1249                 segment_name (segment), segment_name (now_seg));
1250       p = frag_var (rs_org, 1, 1, (relax_substateT) 0, exp.X_add_symbol,
1251                     exp.X_add_number, (char *) 0);
1252       *p = temp_fill;
1253     }                           /* if (ok to make frag) */
1254   demand_empty_rest_of_line ();
1255 }                               /* s_org() */
1256
1257 void 
1258 s_set (ignore)
1259      int ignore;
1260 {
1261   register char *name;
1262   register char delim;
1263   register char *end_name;
1264   register symbolS *symbolP;
1265
1266   /*
1267    * Especial apologies for the random logic:
1268    * this just grew, and could be parsed much more simply!
1269    * Dean in haste.
1270    */
1271   name = input_line_pointer;
1272   delim = get_symbol_end ();
1273   end_name = input_line_pointer;
1274   *end_name = delim;
1275   SKIP_WHITESPACE ();
1276
1277   if (*input_line_pointer != ',')
1278     {
1279       *end_name = 0;
1280       as_bad ("Expected comma after name \"%s\"", name);
1281       *end_name = delim;
1282       ignore_rest_of_line ();
1283       return;
1284     }
1285
1286   input_line_pointer++;
1287   *end_name = 0;
1288
1289   if (name[0] == '.' && name[1] == '\0')
1290     {
1291       /* Turn '. = mumble' into a .org mumble */
1292       register segT segment;
1293       expressionS exp;
1294       register char *ptr;
1295
1296       segment = get_known_segmented_expression (&exp);
1297
1298       if (!need_pass_2)
1299         {
1300           if (segment != now_seg && segment != absolute_section)
1301             as_bad ("Invalid segment \"%s\". Segment \"%s\" assumed.",
1302                     segment_name (segment),
1303                     segment_name (now_seg));
1304           ptr = frag_var (rs_org, 1, 1, (relax_substateT) 0, exp.X_add_symbol,
1305                           exp.X_add_number, (char *) 0);
1306           *ptr = 0;
1307         }                       /* if (ok to make frag) */
1308
1309       *end_name = delim;
1310       return;
1311     }
1312
1313   if ((symbolP = symbol_find (name)) == NULL
1314       && (symbolP = md_undefined_symbol (name)) == NULL)
1315     {
1316       symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
1317 #ifdef OBJ_COFF
1318       /* "set" symbols are local unless otherwise specified. */
1319       SF_SET_LOCAL (symbolP);
1320 #endif /* OBJ_COFF */
1321
1322     }                           /* make a new symbol */
1323
1324   symbol_table_insert (symbolP);
1325
1326   *end_name = delim;
1327   pseudo_set (symbolP);
1328   demand_empty_rest_of_line ();
1329 }                               /* s_set() */
1330
1331 void 
1332 s_space (mult)
1333      int mult;
1334 {
1335   long temp_repeat;
1336   register long temp_fill;
1337   register char *p;
1338
1339   /* Just like .fill, but temp_size = 1 */
1340   if (get_absolute_expression_and_terminator (&temp_repeat) == ',')
1341     {
1342       temp_fill = get_absolute_expression ();
1343     }
1344   else
1345     {
1346       input_line_pointer--;     /* Backup over what was not a ','. */
1347       temp_fill = 0;
1348     }
1349   if (mult)
1350     {
1351       temp_repeat *= mult;
1352     }
1353   if (temp_repeat <= 0)
1354     {
1355       as_warn ("Repeat < 0, .space ignored");
1356       ignore_rest_of_line ();
1357       return;
1358     }
1359   if (!need_pass_2)
1360     {
1361       p = frag_var (rs_fill, 1, 1, (relax_substateT) 0, (symbolS *) 0,
1362                     temp_repeat, (char *) 0);
1363       *p = temp_fill;
1364     }
1365   demand_empty_rest_of_line ();
1366 }                               /* s_space() */
1367
1368 void
1369 s_text (ignore)
1370      int ignore;
1371 {
1372   register int temp;
1373
1374   temp = get_absolute_expression ();
1375   subseg_set (text_section, (subsegT) temp);
1376   demand_empty_rest_of_line ();
1377 }                               /* s_text() */
1378 \f
1379
1380 void 
1381 demand_empty_rest_of_line ()
1382 {
1383   SKIP_WHITESPACE ();
1384   if (is_end_of_line[(unsigned char) *input_line_pointer])
1385     {
1386       input_line_pointer++;
1387     }
1388   else
1389     {
1390       ignore_rest_of_line ();
1391     }
1392   /* Return having already swallowed end-of-line. */
1393 }                               /* Return pointing just after end-of-line. */
1394
1395 void
1396 ignore_rest_of_line ()          /* For suspect lines: gives warning. */
1397 {
1398   if (!is_end_of_line[(unsigned char) *input_line_pointer])
1399     {
1400       if (isprint (*input_line_pointer))
1401         as_bad ("Rest of line ignored. First ignored character is `%c'.",
1402                 *input_line_pointer);
1403       else
1404         as_bad ("Rest of line ignored. First ignored character valued 0x%x.",
1405                 *input_line_pointer);
1406       while (input_line_pointer < buffer_limit
1407              && !is_end_of_line[(unsigned char) *input_line_pointer])
1408         {
1409           input_line_pointer++;
1410         }
1411     }
1412   input_line_pointer++;         /* Return pointing just after end-of-line. */
1413   know (is_end_of_line[(unsigned char) input_line_pointer[-1]]);
1414 }
1415
1416 /*
1417  *                      pseudo_set()
1418  *
1419  * In:  Pointer to a symbol.
1420  *      Input_line_pointer->expression.
1421  *
1422  * Out: Input_line_pointer->just after any whitespace after expression.
1423  *      Tried to set symbol to value of expression.
1424  *      Will change symbols type, value, and frag;
1425  */
1426 void
1427 pseudo_set (symbolP)
1428      symbolS *symbolP;
1429 {
1430   expressionS exp;
1431 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
1432   int ext;
1433 #endif /* OBJ_AOUT or OBJ_BOUT */
1434
1435   know (symbolP);               /* NULL pointer is logic error. */
1436 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
1437   /* @@ Fix this right for BFD.  */
1438   ext = S_IS_EXTERNAL (symbolP);
1439 #endif /* OBJ_AOUT or OBJ_BOUT */
1440
1441   (void) expression (&exp);
1442
1443   if (exp.X_op == O_illegal)
1444     as_bad ("illegal expression; zero assumed");
1445   else if (exp.X_op == O_absent)
1446     as_bad ("missing expression; zero assumed");
1447   else if (exp.X_op == O_big)
1448     as_bad ("%s number invalid; zero assumed",
1449             exp.X_add_number > 0 ? "bignum" : "floating point");
1450   else if (exp.X_op == O_subtract
1451            && (S_GET_SEGMENT (exp.X_add_symbol)
1452                == S_GET_SEGMENT (exp.X_op_symbol))
1453            && SEG_NORMAL (S_GET_SEGMENT (exp.X_add_symbol))
1454            && exp.X_add_symbol->sy_frag == exp.X_op_symbol->sy_frag)
1455     {
1456       exp.X_op = O_constant;
1457       exp.X_add_number = (S_GET_VALUE (exp.X_add_symbol)
1458                           - S_GET_VALUE (exp.X_op_symbol));
1459     }
1460
1461   switch (exp.X_op)
1462     {
1463     case O_illegal:
1464     case O_absent:
1465     case O_big:
1466       exp.X_add_number = 0;
1467       /* Fall through.  */
1468     case O_constant:
1469       S_SET_SEGMENT (symbolP, absolute_section);
1470 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
1471       /* @@ Fix this right for BFD.  */
1472       if (ext)
1473         S_SET_EXTERNAL (symbolP);
1474       else
1475         S_CLEAR_EXTERNAL (symbolP);
1476 #endif /* OBJ_AOUT or OBJ_BOUT */
1477       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
1478       symbolP->sy_frag = &zero_address_frag;
1479       break;
1480
1481     case O_register:
1482       S_SET_SEGMENT (symbolP, reg_section);
1483       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
1484       symbolP->sy_frag = &zero_address_frag;
1485       break;
1486
1487     case O_symbol:
1488       if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
1489         symbolP->sy_value = exp;
1490       else
1491         {
1492           S_SET_SEGMENT (symbolP, S_GET_SEGMENT (exp.X_add_symbol));
1493 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
1494           /* @@ Fix this right for BFD!  */
1495           if (ext)
1496             S_SET_EXTERNAL (symbolP);
1497           else
1498             S_CLEAR_EXTERNAL (symbolP);
1499 #endif /* OBJ_AOUT or OBJ_BOUT */
1500           S_SET_VALUE (symbolP,
1501                        exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
1502           symbolP->sy_frag = exp.X_add_symbol->sy_frag;
1503         }
1504       break;
1505
1506     default:
1507       /* The value is some complex expression.
1508          FIXME: Should we set the segment to anything?  */
1509       symbolP->sy_value = exp;
1510       break;
1511     }
1512 }
1513 \f
1514 /*
1515  *                      cons()
1516  *
1517  * CONStruct more frag of .bytes, or .words etc.
1518  * Should need_pass_2 be 1 then emit no frag(s).
1519  * This understands EXPRESSIONS.
1520  *
1521  * Bug (?)
1522  *
1523  * This has a split personality. We use expression() to read the
1524  * value. We can detect if the value won't fit in a byte or word.
1525  * But we can't detect if expression() discarded significant digits
1526  * in the case of a long. Not worth the crocks required to fix it.
1527  */
1528
1529 /* Select a parser for cons expressions.  */
1530
1531 /* Some targets need to parse the expression in various fancy ways.
1532    You can define TC_PARSE_CONS_EXPRESSION to do whatever you like
1533    (for example, the HPPA does this).  Otherwise, you can define
1534    BITFIELD_CONS_EXPRESSIONS to permit bitfields to be specified, or
1535    REPEAT_CONS_EXPRESSIONS to permit repeat counts.  If none of these
1536    are defined, which is the normal case, then only simple expressions
1537    are permitted.  */
1538
1539 #ifndef TC_PARSE_CONS_EXPRESSION
1540 #ifdef BITFIELD_CONS_EXPRESSIONS
1541 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) parse_bitfield_cons (EXP, NBYTES)
1542 static void 
1543 parse_bitfield_cons PARAMS ((expressionS *exp, unsigned int nbytes));
1544 #endif
1545 #ifdef MRI
1546 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) parse_mri_cons (EXP)
1547 static void
1548 parse_mri_cons PARAMS ((expressionS *exp));
1549 #endif
1550 #ifdef REPEAT_CONS_EXPRESSIONS
1551 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) parse_repeat_cons (EXP, NBYTES)
1552 static void
1553 parse_repeat_cons PARAMS ((expressionS *exp, unsigned int nbytes));
1554 #endif
1555
1556 /* If we haven't gotten one yet, just call expression.  */
1557 #ifndef TC_PARSE_CONS_EXPRESSION
1558 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) expression (EXP)
1559 #endif
1560 #endif
1561
1562 /* worker to do .byte etc statements */
1563 /* clobbers input_line_pointer, checks */
1564 /* end-of-line. */
1565 void 
1566 cons (nbytes)
1567      register int nbytes;       /* 1=.byte, 2=.word, 4=.long */
1568 {
1569   expressionS exp;
1570
1571   if (is_it_end_of_statement ())
1572     {
1573       demand_empty_rest_of_line ();
1574       return;
1575     }
1576
1577   do
1578     {
1579       TC_PARSE_CONS_EXPRESSION (&exp, (unsigned int) nbytes);
1580       emit_expr (&exp, (unsigned int) nbytes);
1581     }
1582   while (*input_line_pointer++ == ',');
1583
1584   input_line_pointer--;         /* Put terminator back into stream. */
1585   demand_empty_rest_of_line ();
1586 }                               /* cons() */
1587
1588 /* Put the contents of expression EXP into the object file using
1589    NBYTES bytes.  If need_pass_2 is 1, this does nothing.  */
1590
1591 void
1592 emit_expr (exp, nbytes)
1593      expressionS *exp;
1594      unsigned int nbytes;
1595 {
1596   operatorT op;
1597   register char *p;
1598   valueT extra_digit = 0;
1599
1600   /* Don't do anything if we are going to make another pass.  */
1601   if (need_pass_2)
1602     return;
1603
1604   op = exp->X_op;
1605
1606   /* Handle a negative bignum.  */
1607   if (op == O_uminus
1608       && exp->X_add_number == 0
1609       && exp->X_add_symbol->sy_value.X_op == O_big
1610       && exp->X_add_symbol->sy_value.X_add_number > 0)
1611     {
1612       int i;
1613       unsigned long carry;
1614
1615       exp = &exp->X_add_symbol->sy_value;
1616
1617       /* Negate the bignum: one's complement each digit and add 1.  */
1618       carry = 1;
1619       for (i = 0; i < exp->X_add_number; i++)
1620         {
1621           unsigned long next;
1622
1623           next = (((~ (generic_bignum[i] & LITTLENUM_MASK))
1624                    & LITTLENUM_MASK)
1625                   + carry);
1626           generic_bignum[i] = next & LITTLENUM_MASK;
1627           carry = next >> LITTLENUM_NUMBER_OF_BITS;
1628         }
1629
1630       /* We can ignore any carry out, because it will be handled by
1631          extra_digit if it is needed.  */
1632
1633       extra_digit = (valueT) -1;
1634       op = O_big;
1635     }
1636
1637   if (op == O_absent || op == O_illegal)
1638     {
1639       as_warn ("zero assumed for missing expression");
1640       exp->X_add_number = 0;
1641       op = O_constant;
1642     }
1643   else if (op == O_big && exp->X_add_number <= 0)
1644     {
1645       as_bad ("floating point number invalid; zero assumed");
1646       exp->X_add_number = 0;
1647       op = O_constant;
1648     }
1649   else if (op == O_register)
1650     {
1651       as_warn ("register value used as expression");
1652       op = O_constant;
1653     }
1654
1655   p = frag_more ((int) nbytes);
1656
1657 #ifndef WORKING_DOT_WORD
1658   /* If we have the difference of two symbols in a word, save it on
1659      the broken_words list.  See the code in write.c.  */
1660   if (op == O_subtract && nbytes == 2)
1661     {
1662       struct broken_word *x;
1663
1664       x = (struct broken_word *) xmalloc (sizeof (struct broken_word));
1665       x->next_broken_word = broken_words;
1666       broken_words = x;
1667       x->frag = frag_now;
1668       x->word_goes_here = p;
1669       x->dispfrag = 0;
1670       x->add = exp->X_add_symbol;
1671       x->sub = exp->X_op_symbol;
1672       x->addnum = exp->X_add_number;
1673       x->added = 0;
1674       new_broken_words++;
1675       return;
1676     }
1677 #endif
1678
1679   /* If we have an integer, but the number of bytes is too large to
1680      pass to md_number_to_chars, handle it as a bignum.  */
1681   if (op == O_constant && nbytes > sizeof (valueT))
1682     {
1683       valueT val;
1684       int gencnt;
1685
1686       if (! exp->X_unsigned && exp->X_add_number < 0)
1687         extra_digit = (valueT) -1;
1688       val = (valueT) exp->X_add_number;
1689       gencnt = 0;
1690       do
1691         {
1692           generic_bignum[gencnt] = val & LITTLENUM_MASK;
1693           val >>= LITTLENUM_NUMBER_OF_BITS;
1694           ++gencnt;
1695         }
1696       while (val != 0);
1697       op = exp->X_op = O_big;
1698       exp->X_add_number = gencnt;
1699     }
1700
1701   if (op == O_constant)
1702     {
1703       register long get;
1704       register long use;
1705       register long mask;
1706       register long unmask;
1707
1708       /* JF << of >= number of bits in the object is undefined.  In
1709          particular SPARC (Sun 4) has problems */
1710       if (nbytes >= sizeof (long))
1711         mask = 0;
1712       else
1713         mask = ~0 << (BITS_PER_CHAR * nbytes);  /* Don't store these bits. */
1714
1715       unmask = ~mask;           /* Do store these bits. */
1716
1717 #ifdef NEVER
1718       "Do this mod if you want every overflow check to assume SIGNED 2's complement data.";
1719       mask = ~(unmask >> 1);    /* Includes sign bit now. */
1720 #endif
1721
1722       get = exp->X_add_number;
1723       use = get & unmask;
1724       if ((get & mask) != 0 && (get & mask) != mask)
1725         {               /* Leading bits contain both 0s & 1s. */
1726           as_warn ("Value 0x%lx truncated to 0x%lx.", get, use);
1727         }
1728       /* put bytes in right order. */
1729       md_number_to_chars (p, (valueT) use, (int) nbytes);
1730     }
1731   else if (op == O_big)
1732     {
1733       int size;
1734       LITTLENUM_TYPE *nums;
1735
1736       know (nbytes % CHARS_PER_LITTLENUM == 0);
1737
1738       size = exp->X_add_number * CHARS_PER_LITTLENUM;
1739       if (nbytes < size)
1740         {
1741           as_warn ("Bignum truncated to %d bytes", nbytes);
1742           size = nbytes;
1743         }
1744
1745       if (target_big_endian)
1746         {
1747           while (nbytes > size)
1748             {
1749               md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
1750               nbytes -= CHARS_PER_LITTLENUM;
1751               p += CHARS_PER_LITTLENUM;
1752             }
1753
1754           nums = generic_bignum + size / CHARS_PER_LITTLENUM;
1755           while (size > 0)
1756             {
1757               --nums;
1758               md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
1759               size -= CHARS_PER_LITTLENUM;
1760               p += CHARS_PER_LITTLENUM;
1761             }
1762         }
1763       else
1764         {
1765           nums = generic_bignum;
1766           while (size > 0)
1767             {
1768               md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
1769               ++nums;
1770               size -= CHARS_PER_LITTLENUM;
1771               p += CHARS_PER_LITTLENUM;
1772               nbytes -= CHARS_PER_LITTLENUM;
1773             }
1774
1775           while (nbytes > 0)
1776             {
1777               md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
1778               nbytes -= CHARS_PER_LITTLENUM;
1779               p += CHARS_PER_LITTLENUM;
1780             }
1781         }
1782     }
1783   else
1784     {
1785       md_number_to_chars (p, (valueT) 0, (int) nbytes);
1786
1787       /* Now we need to generate a fixS to record the symbol value.
1788          This is easy for BFD.  For other targets it can be more
1789          complex.  For very complex cases (currently, the HPPA and
1790          NS32K), you can define TC_CONS_FIX_NEW to do whatever you
1791          want.  For simpler cases, you can define TC_CONS_RELOC to be
1792          the name of the reloc code that should be stored in the fixS.
1793          If neither is defined, the code uses NO_RELOC if it is
1794          defined, and otherwise uses 0.  */
1795
1796 #ifdef BFD_ASSEMBLER
1797 #ifdef TC_CONS_FIX_NEW
1798       TC_CONS_FIX_NEW (frag_now, p - frag_now->fr_literal, nbytes, exp);
1799 #else
1800       fix_new_exp (frag_now, p - frag_now->fr_literal, (int) nbytes, exp, 0,
1801                    /* @@ Should look at CPU word size.  */
1802                    nbytes == 2 ? BFD_RELOC_16
1803                    : nbytes == 8 ? BFD_RELOC_64
1804                    : BFD_RELOC_32);
1805 #endif
1806 #else
1807 #ifdef TC_CONS_FIX_NEW
1808       TC_CONS_FIX_NEW (frag_now, p - frag_now->fr_literal, nbytes, exp);
1809 #else
1810       /* Figure out which reloc number to use.  Use TC_CONS_RELOC if
1811          it is defined, otherwise use NO_RELOC if it is defined,
1812          otherwise use 0.  */
1813 #ifndef TC_CONS_RELOC
1814 #ifdef NO_RELOC
1815 #define TC_CONS_RELOC NO_RELOC
1816 #else
1817 #define TC_CONS_RELOC 0
1818 #endif
1819 #endif
1820       fix_new_exp (frag_now, p - frag_now->fr_literal, (int) nbytes, exp, 0,
1821                    TC_CONS_RELOC);
1822 #endif /* TC_CONS_FIX_NEW */
1823 #endif /* BFD_ASSEMBLER */
1824     }
1825 }
1826 \f
1827 #ifdef BITFIELD_CONS_EXPRESSIONS
1828
1829 /* i960 assemblers, (eg, asm960), allow bitfields after ".byte" as
1830    w:x,y:z, where w and y are bitwidths and x and y are values.  They
1831    then pack them all together. We do a little better in that we allow
1832    them in words, longs, etc. and we'll pack them in target byte order
1833    for you.
1834
1835    The rules are: pack least significat bit first, if a field doesn't
1836    entirely fit, put it in the next unit.  Overflowing the bitfield is
1837    explicitly *not* even a warning.  The bitwidth should be considered
1838    a "mask".
1839
1840    To use this function the tc-XXX.h file should define
1841    BITFIELD_CONS_EXPRESSIONS.  */
1842
1843 static void 
1844 parse_bitfield_cons (exp, nbytes)
1845      expressionS *exp;
1846      unsigned int nbytes;
1847 {
1848   unsigned int bits_available = BITS_PER_CHAR * nbytes;
1849   char *hold = input_line_pointer;
1850
1851   (void) expression (exp);
1852
1853   if (*input_line_pointer == ':')
1854     {                   /* bitfields */
1855       long value = 0;
1856
1857       for (;;)
1858         {
1859           unsigned long width;
1860
1861           if (*input_line_pointer != ':')
1862             {
1863               input_line_pointer = hold;
1864               break;
1865             }                   /* next piece is not a bitfield */
1866
1867           /* In the general case, we can't allow
1868              full expressions with symbol
1869              differences and such.  The relocation
1870              entries for symbols not defined in this
1871              assembly would require arbitrary field
1872              widths, positions, and masks which most
1873              of our current object formats don't
1874              support.
1875              
1876              In the specific case where a symbol
1877              *is* defined in this assembly, we
1878              *could* build fixups and track it, but
1879              this could lead to confusion for the
1880              backends.  I'm lazy. I'll take any
1881              SEG_ABSOLUTE. I think that means that
1882              you can use a previous .set or
1883              .equ type symbol.  xoxorich. */
1884
1885           if (exp->X_op == O_absent)
1886             {
1887               as_warn ("using a bit field width of zero");
1888               exp->X_add_number = 0;
1889               exp->X_op = O_constant;
1890             }                   /* implied zero width bitfield */
1891
1892           if (exp->X_op != O_constant)
1893             {
1894               *input_line_pointer = '\0';
1895               as_bad ("field width \"%s\" too complex for a bitfield", hold);
1896               *input_line_pointer = ':';
1897               demand_empty_rest_of_line ();
1898               return;
1899             }                   /* too complex */
1900
1901           if ((width = exp->X_add_number) > (BITS_PER_CHAR * nbytes))
1902             {
1903               as_warn ("field width %lu too big to fit in %d bytes: truncated to %d bits",
1904                        width, nbytes, (BITS_PER_CHAR * nbytes));
1905               width = BITS_PER_CHAR * nbytes;
1906             }                   /* too big */
1907
1908           if (width > bits_available)
1909             {
1910               /* FIXME-SOMEDAY: backing up and reparsing is wasteful.  */
1911               input_line_pointer = hold;
1912               exp->X_add_number = value;
1913               break;
1914             }                   /* won't fit */
1915
1916           hold = ++input_line_pointer; /* skip ':' */
1917
1918           (void) expression (exp);
1919           if (exp->X_op != O_constant)
1920             {
1921               char cache = *input_line_pointer;
1922
1923               *input_line_pointer = '\0';
1924               as_bad ("field value \"%s\" too complex for a bitfield", hold);
1925               *input_line_pointer = cache;
1926               demand_empty_rest_of_line ();
1927               return;
1928             }                   /* too complex */
1929
1930           value |= ((~(-1 << width) & exp->X_add_number)
1931                     << ((BITS_PER_CHAR * nbytes) - bits_available));
1932
1933           if ((bits_available -= width) == 0
1934               || is_it_end_of_statement ()
1935               || *input_line_pointer != ',')
1936             {
1937               break;
1938             }                   /* all the bitfields we're gonna get */
1939
1940           hold = ++input_line_pointer;
1941           (void) expression (exp);
1942         }                       /* forever loop */
1943
1944       exp->X_add_number = value;
1945       exp->X_op = O_constant;
1946       exp->X_unsigned = 1;
1947     }                           /* if looks like a bitfield */
1948 }                               /* parse_bitfield_cons() */
1949
1950 #endif /* BITFIELD_CONS_EXPRESSIONS */
1951 \f
1952 #ifdef MRI
1953
1954 static void
1955 parse_mri_cons (exp, nbytes)
1956      expressionS *exp;
1957      unsigned int nbytes;
1958 {
1959   if (*input_line_pointer == '\'')
1960     {
1961       /* An MRI style string, cut into as many bytes as will fit into
1962          a nbyte chunk, left justify if necessary, and separate with
1963          commas so we can try again later */
1964       int scan = 0;
1965       unsigned int result = 0;
1966       input_line_pointer++;
1967       for (scan = 0; scan < nbytes; scan++)
1968         {
1969           if (*input_line_pointer == '\'')
1970             {
1971               if (input_line_pointer[1] == '\'')
1972                 {
1973                   input_line_pointer++;
1974                 }
1975               else
1976                 break;
1977             }
1978           result = (result << 8) | (*input_line_pointer++);
1979         }
1980
1981       /* Left justify */
1982       while (scan < nbytes)
1983         {
1984           result <<= 8;
1985           scan++;
1986         }
1987       /* Create correct expression */
1988       exp->X_op = O_constant;
1989       exp->X_add_number = result;
1990       /* Fake it so that we can read the next char too */
1991       if (input_line_pointer[0] != '\'' ||
1992           (input_line_pointer[0] == '\'' && input_line_pointer[1] == '\''))
1993         {
1994           input_line_pointer -= 2;
1995           input_line_pointer[0] = ',';
1996           input_line_pointer[1] = '\'';
1997         }
1998       else
1999         input_line_pointer++;
2000     }
2001   else
2002     expression (&exp);
2003 }
2004
2005 #endif /* MRI */
2006 \f
2007 #ifdef REPEAT_CONS_EXPRESSIONS
2008
2009 /* Parse a repeat expression for cons.  This is used by the MIPS
2010    assembler.  The format is NUMBER:COUNT; NUMBER appears in the
2011    object file COUNT times.
2012
2013    To use this for a target, define REPEAT_CONS_EXPRESSIONS.  */
2014
2015 static void
2016 parse_repeat_cons (exp, nbytes)
2017      expressionS *exp;
2018      unsigned int nbytes;
2019 {
2020   expressionS count;
2021   register int i;
2022
2023   expression (exp);
2024
2025   if (*input_line_pointer != ':')
2026     {
2027       /* No repeat count.  */
2028       return;
2029     }
2030
2031   ++input_line_pointer;
2032   expression (&count);
2033   if (count.X_op != O_constant
2034       || count.X_add_number <= 0)
2035     {
2036       as_warn ("Unresolvable or nonpositive repeat count; using 1");
2037       return;
2038     }
2039
2040   /* The cons function is going to output this expression once.  So we
2041      output it count - 1 times.  */
2042   for (i = count.X_add_number - 1; i > 0; i--)
2043     emit_expr (exp, nbytes);
2044 }
2045
2046 #endif /* REPEAT_CONS_EXPRESSIONS */
2047 \f
2048 /*
2049  *                      float_cons()
2050  *
2051  * CONStruct some more frag chars of .floats .ffloats etc.
2052  * Makes 0 or more new frags.
2053  * If need_pass_2 == 1, no frags are emitted.
2054  * This understands only floating literals, not expressions. Sorry.
2055  *
2056  * A floating constant is defined by atof_generic(), except it is preceded
2057  * by 0d 0f 0g or 0h. After observing the STRANGE way my BSD AS does its
2058  * reading, I decided to be incompatible. This always tries to give you
2059  * rounded bits to the precision of the pseudo-op. Former AS did premature
2060  * truncatation, restored noisy bits instead of trailing 0s AND gave you
2061  * a choice of 2 flavours of noise according to which of 2 floating-point
2062  * scanners you directed AS to use.
2063  *
2064  * In:  input_line_pointer->whitespace before, or '0' of flonum.
2065  *
2066  */
2067
2068 void
2069 float_cons (float_type)
2070      /* Clobbers input_line-pointer, checks end-of-line. */
2071      register int float_type;   /* 'f':.ffloat ... 'F':.float ... */
2072 {
2073   register char *p;
2074   int length;                   /* Number of chars in an object. */
2075   register char *err;           /* Error from scanning floating literal. */
2076   char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT];
2077
2078   if (is_it_end_of_statement ())
2079     {
2080       demand_empty_rest_of_line ();
2081       return;
2082     }
2083
2084   do
2085     {
2086       /* input_line_pointer->1st char of a flonum (we hope!). */
2087       SKIP_WHITESPACE ();
2088
2089       /* Skip any 0{letter} that may be present. Don't even check if the
2090        * letter is legal. Someone may invent a "z" format and this routine
2091        * has no use for such information. Lusers beware: you get
2092        * diagnostics if your input is ill-conditioned.
2093        */
2094       if (input_line_pointer[0] == '0' && isalpha (input_line_pointer[1]))
2095         input_line_pointer += 2;
2096
2097       err = md_atof (float_type, temp, &length);
2098       know (length <= MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT);
2099       know (length > 0);
2100       if (err)
2101         {
2102           as_bad ("Bad floating literal: %s", err);
2103           ignore_rest_of_line ();
2104           return;
2105         }
2106
2107       if (!need_pass_2)
2108         {
2109           int count;
2110
2111           count = 1;
2112
2113 #ifdef REPEAT_CONS_EXPRESSIONS
2114           if (*input_line_pointer == ':')
2115             {
2116               expressionS count_exp;
2117
2118               ++input_line_pointer;
2119               expression (&count_exp);
2120               if (count_exp.X_op != O_constant
2121                   || count_exp.X_add_number <= 0)
2122                 {
2123                   as_warn ("unresolvable or nonpositive repeat count; using 1");
2124                 }
2125               else
2126                 count = count_exp.X_add_number;
2127             }
2128 #endif
2129
2130           while (--count >= 0)
2131             {
2132               p = frag_more (length);
2133               memcpy (p, temp, (unsigned int) length);
2134             }
2135         }
2136       SKIP_WHITESPACE ();
2137     }
2138   while (*input_line_pointer++ == ',');
2139
2140   --input_line_pointer;         /* Put terminator back into stream.  */
2141   demand_empty_rest_of_line ();
2142 }                               /* float_cons() */
2143 \f
2144 /*
2145  *                      stringer()
2146  *
2147  * We read 0 or more ',' seperated, double-quoted strings.
2148  *
2149  * Caller should have checked need_pass_2 is FALSE because we don't check it.
2150  */
2151
2152
2153 void 
2154 stringer (append_zero)          /* Worker to do .ascii etc statements. */
2155      /* Checks end-of-line. */
2156      register int append_zero;  /* 0: don't append '\0', else 1 */
2157 {
2158   register unsigned int c;
2159
2160   /*
2161    * The following awkward logic is to parse ZERO or more strings,
2162    * comma seperated. Recall a string expression includes spaces
2163    * before the opening '\"' and spaces after the closing '\"'.
2164    * We fake a leading ',' if there is (supposed to be)
2165    * a 1st, expression. We keep demanding expressions for each
2166    * ','.
2167    */
2168   if (is_it_end_of_statement ())
2169     {
2170       c = 0;                    /* Skip loop. */
2171       ++input_line_pointer;     /* Compensate for end of loop. */
2172     }
2173   else
2174     {
2175       c = ',';                  /* Do loop. */
2176     }
2177   while (c == ',' || c == '<' || c == '"')
2178     {
2179       SKIP_WHITESPACE ();
2180       switch (*input_line_pointer)
2181         {
2182         case '\"':
2183           ++input_line_pointer; /*->1st char of string. */
2184           while (is_a_char (c = next_char_of_string ()))
2185             {
2186               FRAG_APPEND_1_CHAR (c);
2187             }
2188           if (append_zero)
2189             {
2190               FRAG_APPEND_1_CHAR (0);
2191             }
2192           know (input_line_pointer[-1] == '\"');
2193           break;
2194         case '<':
2195           input_line_pointer++;
2196           c = get_single_number ();
2197           FRAG_APPEND_1_CHAR (c);
2198           if (*input_line_pointer != '>')
2199             {
2200               as_bad ("Expected <nn>");
2201             }
2202           input_line_pointer++;
2203           break;
2204         case ',':
2205           input_line_pointer++;
2206           break;
2207         }
2208       SKIP_WHITESPACE ();
2209       c = *input_line_pointer;
2210     }
2211
2212   demand_empty_rest_of_line ();
2213 }                               /* stringer() */
2214 \f
2215 /* FIXME-SOMEDAY: I had trouble here on characters with the
2216     high bits set.  We'll probably also have trouble with
2217     multibyte chars, wide chars, etc.  Also be careful about
2218     returning values bigger than 1 byte.  xoxorich. */
2219
2220 unsigned int 
2221 next_char_of_string ()
2222 {
2223   register unsigned int c;
2224
2225   c = *input_line_pointer++ & CHAR_MASK;
2226   switch (c)
2227     {
2228     case '\"':
2229       c = NOT_A_CHAR;
2230       break;
2231
2232     case '\\':
2233       switch (c = *input_line_pointer++)
2234         {
2235         case 'b':
2236           c = '\b';
2237           break;
2238
2239         case 'f':
2240           c = '\f';
2241           break;
2242
2243         case 'n':
2244           c = '\n';
2245           break;
2246
2247         case 'r':
2248           c = '\r';
2249           break;
2250
2251         case 't':
2252           c = '\t';
2253           break;
2254
2255 #ifdef BACKSLASH_V
2256         case 'v':
2257           c = '\013';
2258           break;
2259 #endif
2260
2261         case '\\':
2262         case '"':
2263           break;                /* As itself. */
2264
2265         case '0':
2266         case '1':
2267         case '2':
2268         case '3':
2269         case '4':
2270         case '5':
2271         case '6':
2272         case '7':
2273         case '8':
2274         case '9':
2275           {
2276             long number;
2277
2278             for (number = 0; isdigit (c); c = *input_line_pointer++)
2279               {
2280                 number = number * 8 + c - '0';
2281               }
2282             c = number & 0xff;
2283           }
2284           --input_line_pointer;
2285           break;
2286
2287         case '\n':
2288           /* To be compatible with BSD 4.2 as: give the luser a linefeed!! */
2289           as_warn ("Unterminated string: Newline inserted.");
2290           c = '\n';
2291           break;
2292
2293         default:
2294
2295 #ifdef ONLY_STANDARD_ESCAPES
2296           as_bad ("Bad escaped character in string, '?' assumed");
2297           c = '?';
2298 #endif /* ONLY_STANDARD_ESCAPES */
2299
2300           break;
2301         }                       /* switch on escaped char */
2302       break;
2303
2304     default:
2305       break;
2306     }                           /* switch on char */
2307   return (c);
2308 }                               /* next_char_of_string() */
2309 \f
2310 static segT
2311 get_segmented_expression (expP)
2312      register expressionS *expP;
2313 {
2314   register segT retval;
2315
2316   retval = expression (expP);
2317   if (expP->X_op == O_illegal
2318       || expP->X_op == O_absent
2319       || expP->X_op == O_big)
2320     {
2321       as_bad ("expected address expression; zero assumed");
2322       expP->X_op = O_constant;
2323       expP->X_add_number = 0;
2324       retval = absolute_section;
2325     }
2326   return retval;
2327 }
2328
2329 static segT 
2330 get_known_segmented_expression (expP)
2331      register expressionS *expP;
2332 {
2333   register segT retval;
2334
2335   if ((retval = get_segmented_expression (expP)) == undefined_section)
2336     {
2337       /* There is no easy way to extract the undefined symbol from the
2338          expression.  */
2339       if (expP->X_add_symbol != NULL
2340           && S_GET_SEGMENT (expP->X_add_symbol) != expr_section)
2341         as_warn ("symbol \"%s\" undefined; zero assumed",
2342                  S_GET_NAME (expP->X_add_symbol));
2343       else
2344         as_warn ("some symbol undefined; zero assumed");
2345       retval = absolute_section;
2346       expP->X_op = O_constant;
2347       expP->X_add_number = 0;
2348     }
2349   know (retval == absolute_section || SEG_NORMAL (retval));
2350   return (retval);
2351 }                               /* get_known_segmented_expression() */
2352
2353 offsetT
2354 get_absolute_expression ()
2355 {
2356   expressionS exp;
2357
2358   expression (&exp);
2359   if (exp.X_op != O_constant)
2360     {
2361       if (exp.X_op != O_absent)
2362         as_bad ("bad absolute expression; zero assumed");
2363       exp.X_add_number = 0;
2364     }
2365   return exp.X_add_number;
2366 }
2367
2368 char                            /* return terminator */
2369 get_absolute_expression_and_terminator (val_pointer)
2370      long *val_pointer;         /* return value of expression */
2371 {
2372   /* FIXME: val_pointer should probably be offsetT *.  */
2373   *val_pointer = (long) get_absolute_expression ();
2374   return (*input_line_pointer++);
2375 }
2376 \f
2377 /*
2378  *                      demand_copy_C_string()
2379  *
2380  * Like demand_copy_string, but return NULL if the string contains any '\0's.
2381  * Give a warning if that happens.
2382  */
2383 char *
2384 demand_copy_C_string (len_pointer)
2385      int *len_pointer;
2386 {
2387   register char *s;
2388
2389   if ((s = demand_copy_string (len_pointer)) != 0)
2390     {
2391       register int len;
2392
2393       for (len = *len_pointer;
2394            len > 0;
2395            len--)
2396         {
2397           if (*s == 0)
2398             {
2399               s = 0;
2400               len = 1;
2401               *len_pointer = 0;
2402               as_bad ("This string may not contain \'\\0\'");
2403             }
2404         }
2405     }
2406   return (s);
2407 }
2408 \f
2409 /*
2410  *                      demand_copy_string()
2411  *
2412  * Demand string, but return a safe (=private) copy of the string.
2413  * Return NULL if we can't read a string here.
2414  */
2415 static char *
2416 demand_copy_string (lenP)
2417      int *lenP;
2418 {
2419   register unsigned int c;
2420   register int len;
2421   char *retval;
2422
2423   len = 0;
2424   SKIP_WHITESPACE ();
2425   if (*input_line_pointer == '\"')
2426     {
2427       input_line_pointer++;     /* Skip opening quote. */
2428
2429       while (is_a_char (c = next_char_of_string ()))
2430         {
2431           obstack_1grow (&notes, c);
2432           len++;
2433         }
2434       /* JF this next line is so demand_copy_C_string will return a null
2435                    termanated string. */
2436       obstack_1grow (&notes, '\0');
2437       retval = obstack_finish (&notes);
2438     }
2439   else
2440     {
2441       as_warn ("Missing string");
2442       retval = NULL;
2443       ignore_rest_of_line ();
2444     }
2445   *lenP = len;
2446   return (retval);
2447 }                               /* demand_copy_string() */
2448 \f
2449 /*
2450  *              is_it_end_of_statement()
2451  *
2452  * In:  Input_line_pointer->next character.
2453  *
2454  * Do:  Skip input_line_pointer over all whitespace.
2455  *
2456  * Out: 1 if input_line_pointer->end-of-line.
2457 */
2458 int 
2459 is_it_end_of_statement ()
2460 {
2461   SKIP_WHITESPACE ();
2462   return (is_end_of_line[(unsigned char) *input_line_pointer]);
2463 }                               /* is_it_end_of_statement() */
2464
2465 void 
2466 equals (sym_name)
2467      char *sym_name;
2468 {
2469   register symbolS *symbolP;    /* symbol we are working with */
2470
2471   input_line_pointer++;
2472   if (*input_line_pointer == '=')
2473     input_line_pointer++;
2474
2475   while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
2476     input_line_pointer++;
2477
2478   if (sym_name[0] == '.' && sym_name[1] == '\0')
2479     {
2480       /* Turn '. = mumble' into a .org mumble */
2481       register segT segment;
2482       expressionS exp;
2483       register char *p;
2484
2485       segment = get_known_segmented_expression (&exp);
2486       if (!need_pass_2)
2487         {
2488           if (segment != now_seg && segment != absolute_section)
2489             as_warn ("Illegal segment \"%s\". Segment \"%s\" assumed.",
2490                      segment_name (segment),
2491                      segment_name (now_seg));
2492           p = frag_var (rs_org, 1, 1, (relax_substateT) 0, exp.X_add_symbol,
2493                         exp.X_add_number, (char *) 0);
2494           *p = 0;
2495         }                       /* if (ok to make frag) */
2496     }
2497   else
2498     {
2499       symbolP = symbol_find_or_make (sym_name);
2500       pseudo_set (symbolP);
2501     }
2502 }                               /* equals() */
2503
2504 /* .include -- include a file at this point. */
2505
2506 /* ARGSUSED */
2507 void 
2508 s_include (arg)
2509      int arg;
2510 {
2511   char *newbuf;
2512   char *filename;
2513   int i;
2514   FILE *try;
2515   char *path;
2516
2517   filename = demand_copy_string (&i);
2518   demand_empty_rest_of_line ();
2519   path = xmalloc ((unsigned long) i + include_dir_maxlen + 5 /* slop */ );
2520   for (i = 0; i < include_dir_count; i++)
2521     {
2522       strcpy (path, include_dirs[i]);
2523       strcat (path, "/");
2524       strcat (path, filename);
2525       if (0 != (try = fopen (path, "r")))
2526         {
2527           fclose (try);
2528           goto gotit;
2529         }
2530     }
2531   free (path);
2532   path = filename;
2533 gotit:
2534   /* malloc Storage leak when file is found on path.  FIXME-SOMEDAY. */
2535   newbuf = input_scrub_include_file (path, input_line_pointer);
2536   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2537 }                               /* s_include() */
2538
2539 void 
2540 add_include_dir (path)
2541      char *path;
2542 {
2543   int i;
2544
2545   if (include_dir_count == 0)
2546     {
2547       include_dirs = (char **) xmalloc (2 * sizeof (*include_dirs));
2548       include_dirs[0] = ".";    /* Current dir */
2549       include_dir_count = 2;
2550     }
2551   else
2552     {
2553       include_dir_count++;
2554       include_dirs = (char **) realloc (include_dirs,
2555                                 include_dir_count * sizeof (*include_dirs));
2556     }
2557
2558   include_dirs[include_dir_count - 1] = path;   /* New one */
2559
2560   i = strlen (path);
2561   if (i > include_dir_maxlen)
2562     include_dir_maxlen = i;
2563 }                               /* add_include_dir() */
2564
2565 void 
2566 s_ignore (arg)
2567      int arg;
2568 {
2569   while (!is_end_of_line[(unsigned char) *input_line_pointer])
2570     {
2571       ++input_line_pointer;
2572     }
2573   ++input_line_pointer;
2574
2575   return;
2576 }                               /* s_ignore() */
2577 \f
2578 /*
2579  * Handle .stabX directives, which used to be open-coded.
2580  * So much creeping featurism overloaded the semantics that we decided
2581  * to put all .stabX thinking in one place. Here.
2582  *
2583  * We try to make any .stabX directive legal. Other people's AS will often
2584  * do assembly-time consistency checks: eg assigning meaning to n_type bits
2585  * and "protecting" you from setting them to certain values. (They also zero
2586  * certain bits before emitting symbols. Tut tut.)
2587  *
2588  * If an expression is not absolute we either gripe or use the relocation
2589  * information. Other people's assemblers silently forget information they
2590  * don't need and invent information they need that you didn't supply.
2591  */
2592
2593 /*
2594  * Build a string dictionary entry for a .stabX symbol.
2595  * The symbol is added to the .<secname>str section.
2596  */
2597
2598 #ifdef SEPARATE_STAB_SECTIONS
2599
2600 unsigned int
2601 get_stab_string_offset (string, secname)
2602      const char *string;
2603      const char *secname;
2604 {
2605   unsigned int length;
2606   unsigned int retval;
2607
2608   retval = 0;
2609   length = strlen (string);
2610   if (length > 0)
2611     {                           /* Ordinary case. */
2612       segT save_seg;
2613       subsegT save_subseg;
2614       char *newsecname;
2615       segT seg;
2616       int aligned;
2617       char *p;
2618
2619       save_seg = now_seg;
2620       save_subseg = now_subseg;
2621
2622       /* Create the stab string section.  */
2623       newsecname = xmalloc ((unsigned long) (strlen (secname) + 4));
2624       strcpy (newsecname, secname);
2625       strcat (newsecname, "str");
2626
2627       seg = subseg_new (newsecname, 0);
2628
2629       retval = seg_info (seg)->stabu.stab_string_size;
2630       if (retval > 0)
2631         free (newsecname);
2632       else
2633         {
2634           /* Make sure the first string is empty.  */
2635           p = frag_more (1);
2636           *p = 0;
2637           retval = seg_info (seg)->stabu.stab_string_size = 1;
2638 #ifdef BFD_ASSEMBLER
2639           bfd_set_section_flags (stdoutput, seg, SEC_READONLY);
2640 #else
2641           free (newsecname);
2642 #endif
2643         }
2644
2645       p = frag_more (length + 1);
2646       strcpy (p, string);
2647
2648       seg_info (seg)->stabu.stab_string_size += length + 1;
2649
2650       subseg_set (save_seg, save_subseg);
2651     }
2652
2653   return retval;
2654 }
2655
2656 #endif /* SEPARATE_STAB_SECTIONS */
2657
2658 /* This can handle different kinds of stabs (s,n,d) and different
2659    kinds of stab sections. */
2660
2661 static void 
2662 s_stab_generic (what, secname)
2663      int what;
2664      char *secname;
2665 {
2666   long longint;
2667   char *string;
2668   int type;
2669   int other;
2670   int desc;
2671
2672   /* The general format is:
2673      .stabs "STRING",TYPE,OTHER,DESC,VALUE
2674      .stabn TYPE,OTHER,DESC,VALUE
2675      .stabd TYPE,OTHER,DESC
2676      At this point input_line_pointer points after the pseudo-op and
2677      any trailing whitespace.  The argument what is one of 's', 'n' or
2678      'd' indicating which type of .stab this is.  */
2679
2680   if (what != 's')
2681     string = "";
2682   else
2683     {
2684       int length;
2685
2686       string = demand_copy_C_string (&length);
2687       SKIP_WHITESPACE ();
2688       if (*input_line_pointer == ',')
2689         input_line_pointer++;
2690       else
2691         {
2692           as_warn (".stabs: Missing comma");
2693           ignore_rest_of_line ();
2694           return;
2695         }
2696     }
2697
2698   if (get_absolute_expression_and_terminator (&longint) != ',')
2699     {
2700       as_warn (".stab%c: Missing comma", what);
2701       ignore_rest_of_line ();
2702       return;
2703     }
2704   type = longint;
2705
2706   if (get_absolute_expression_and_terminator (&longint) != ',')
2707     {
2708       as_warn (".stab%c: Missing comma", what);
2709       ignore_rest_of_line ();
2710       return;
2711     }
2712   other = longint;
2713
2714   desc = get_absolute_expression ();
2715   if (what == 's' || what == 'n')
2716     {
2717       if (*input_line_pointer != ',')
2718         {
2719           as_warn (".stab%c: Missing comma", what);
2720           ignore_rest_of_line ();
2721           return;
2722         }
2723       input_line_pointer++;
2724       SKIP_WHITESPACE ();
2725     }
2726
2727   /* We have not gathered the type, other, and desc information.  For
2728      .stabs or .stabn, input_line_pointer is now pointing at the
2729      value.  */
2730
2731 #ifdef SEPARATE_STAB_SECTIONS
2732   /* Output the stab information in a separate section.  This is used
2733      at least for COFF and ELF.  */
2734   {
2735     segT saved_seg = now_seg;
2736     subsegT saved_subseg = now_subseg;
2737     fragS *saved_frag = frag_now;
2738     valueT dot;
2739     segT seg;
2740     unsigned int stroff;
2741     char *p;
2742     
2743     dot = frag_now_fix ();
2744
2745     seg = subseg_new (secname, 0);
2746
2747     if (! seg_info (seg)->hadone)
2748       {
2749 #ifdef BFD_ASSEMBLER
2750         bfd_set_section_flags (stdoutput, seg, SEC_READONLY | SEC_RELOC);
2751 #endif
2752 #ifdef INIT_STAB_SECTION
2753         INIT_STAB_SECTION (seg);
2754 #endif
2755         seg_info (seg)->hadone = 1;
2756       }
2757
2758     stroff = get_stab_string_offset (string, secname);
2759
2760     /* At least for now, stabs in a special stab section are always
2761        output as 12 byte blocks of information.  */
2762     p = frag_more (8);
2763     md_number_to_chars (p, (valueT) stroff, 4);
2764     md_number_to_chars (p + 4, (valueT) type, 1);
2765     md_number_to_chars (p + 5, (valueT) other, 1);
2766     md_number_to_chars (p + 6, (valueT) desc, 2);
2767
2768     if (what == 's' || what == 'n')
2769       {
2770         /* Pick up the value from the input line.  */
2771         cons (4);
2772         input_line_pointer--;
2773       }
2774     else
2775       {
2776         const char *fake;
2777         symbolS *symbol;
2778         expressionS exp;
2779
2780         /* Arrange for a value representing the current location.  */
2781 #ifdef DOT_LABEL_PREFIX
2782         fake = ".L0\001";
2783 #else
2784         fake = "L0\001";
2785 #endif
2786         symbol = symbol_new (fake, saved_seg, dot, saved_frag);
2787
2788         exp.X_op = O_symbol;
2789         exp.X_add_symbol = symbol;
2790         exp.X_add_number = 0;
2791
2792         emit_expr (&exp, 4);
2793       }
2794
2795 #ifdef OBJ_PROCESS_STAB
2796     OBJ_PROCESS_STAB (seg, string, stroff, type, other, desc);
2797 #endif
2798
2799     subseg_set (saved_seg, saved_subseg);
2800   }
2801 #else /* ! SEPARATE_STAB_SECTIONS */
2802 #ifdef OBJ_PROCESS_STAB
2803   OBJ_PROCESS_STAB (what, string, type, other, desc);
2804 #else
2805   /* Put the stab information in the symbol table.  */
2806   {
2807     symbolS *symbol;
2808
2809     symbol = symbol_new (string, undefined_section, 0,
2810                          (struct frag *) NULL);
2811     if (what == 's' || what == 'n')
2812       {
2813         /* Pick up the value from the input line.  */
2814         symbol->sy_frag = &zero_address_frag;
2815         pseudo_set (symbol);
2816       }
2817     else
2818       {
2819         /* .stabd sets the name to NULL.  Why?  */
2820         S_SET_NAME (symbol, NULL);
2821         symbol->sy_frag = frag_now;
2822         S_SET_VALUE (symbol, (valueT) frag_now_fix ());
2823       }
2824
2825     S_SET_TYPE (symbol, type);
2826     S_SET_OTHER (symbol, other);
2827     S_SET_DESC (symbol, desc);
2828   }
2829 #endif /* ! OBJ_PROCESS_STAB */
2830 #endif /* ! SEPARATE_STAB_SECTIONS */
2831
2832 #ifndef NO_LISTING
2833   if (listing)
2834     {
2835       switch (type)
2836         {
2837         case N_SLINE:
2838           listing_source_line ((unsigned int) desc);
2839           break;
2840         case N_SO:
2841         case N_SOL:
2842           listing_source_file (string);
2843           break;
2844         }
2845     }
2846 #endif /* ! NO_LISTING */
2847
2848   demand_empty_rest_of_line ();
2849 }
2850
2851 /* Regular stab directive. */
2852
2853 void
2854 s_stab (what)
2855      int what;
2856 {
2857   s_stab_generic (what, ".stab");
2858 }
2859
2860 /* "Extended stabs", used in Solaris only now. */
2861
2862 void
2863 s_xstab (what)
2864      int what;
2865 {
2866   int length;
2867   char *secname;
2868
2869   secname = demand_copy_C_string (&length);
2870   SKIP_WHITESPACE ();
2871   if (*input_line_pointer == ',')
2872     input_line_pointer++;
2873   else
2874     {
2875       as_bad ("comma missing in .xstabs");
2876       ignore_rest_of_line ();
2877       return;
2878     }
2879   s_stab_generic (what, secname);
2880 }
2881
2882 #ifdef S_SET_DESC
2883
2884 /* Frob invented at RMS' request. Set the n_desc of a symbol.  */
2885
2886 void 
2887 s_desc (ignore)
2888      int ignore;
2889 {
2890   char *name;
2891   char c;
2892   char *p;
2893   symbolS *symbolP;
2894   int temp;
2895
2896   name = input_line_pointer;
2897   c = get_symbol_end ();
2898   p = input_line_pointer;
2899   *p = c;
2900   SKIP_WHITESPACE ();
2901   if (*input_line_pointer != ',')
2902     {
2903       *p = 0;
2904       as_bad ("Expected comma after name \"%s\"", name);
2905       *p = c;
2906       ignore_rest_of_line ();
2907     }
2908   else
2909     {
2910       input_line_pointer++;
2911       temp = get_absolute_expression ();
2912       *p = 0;
2913       symbolP = symbol_find_or_make (name);
2914       *p = c;
2915       S_SET_DESC (symbolP, temp);
2916     }
2917   demand_empty_rest_of_line ();
2918 }                               /* s_desc() */
2919
2920 #endif /* defined (S_SET_DESC) */
2921
2922 /* end of read.c */