]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/binutils/gas/read.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / binutils / gas / read.c
1 /* read.c - read a source file -
2    Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* If your chars aren't 8 bits, you will change this a bit (eg. to 0xFF).
24    But then, GNU isn't spozed to run on your machine anyway.
25    (RMS is so shortsighted sometimes.)  */
26 #define MASK_CHAR ((int)(unsigned char) -1)
27
28 /* This is the largest known floating point format (for now). It will
29    grow when we do 4361 style flonums.  */
30 #define MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT (16)
31
32 /* Routines that read assembler source text to build spaghetti in memory.
33    Another group of these functions is in the expr.c module.  */
34
35 #include "as.h"
36 #include "safe-ctype.h"
37 #include "subsegs.h"
38 #include "sb.h"
39 #include "macro.h"
40 #include "obstack.h"
41 #include "ecoff.h"
42 #include "dw2gencfi.h"
43
44 #ifndef TC_START_LABEL
45 #define TC_START_LABEL(x,y) (x == ':')
46 #endif
47
48 /* Set by the object-format or the target.  */
49 #ifndef TC_IMPLICIT_LCOMM_ALIGNMENT
50 #define TC_IMPLICIT_LCOMM_ALIGNMENT(SIZE, P2VAR)                \
51   do                                                            \
52     {                                                           \
53       if ((SIZE) >= 8)                                          \
54         (P2VAR) = 3;                                            \
55       else if ((SIZE) >= 4)                                     \
56         (P2VAR) = 2;                                            \
57       else if ((SIZE) >= 2)                                     \
58         (P2VAR) = 1;                                            \
59       else                                                      \
60         (P2VAR) = 0;                                            \
61     }                                                           \
62   while (0)
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 #define LEX_AT 0
75 #endif
76
77 #ifndef LEX_BR
78 /* The RS/6000 assembler uses {,},[,] as parts of symbol names.  */
79 #define LEX_BR 0
80 #endif
81
82 #ifndef LEX_PCT
83 /* The Delta 68k assembler permits % inside label names.  */
84 #define LEX_PCT 0
85 #endif
86
87 #ifndef LEX_QM
88 /* The PowerPC Windows NT assemblers permits ? inside label names.  */
89 #define LEX_QM 0
90 #endif
91
92 #ifndef LEX_HASH
93 /* The IA-64 assembler uses # as a suffix designating a symbol.  We include
94    it in the symbol and strip it out in tc_canonicalize_symbol_name.  */
95 #define LEX_HASH 0
96 #endif
97
98 #ifndef LEX_DOLLAR
99 #define LEX_DOLLAR 3
100 #endif
101
102 #ifndef LEX_TILDE
103 /* The Delta 68k assembler permits ~ at start of label names.  */
104 #define LEX_TILDE 0
105 #endif
106
107 /* Used by is_... macros. our ctype[].  */
108 char lex_type[256] = {
109   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* @ABCDEFGHIJKLMNO */
110   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* PQRSTUVWXYZ[\]^_ */
111   0, 0, 0, LEX_HASH, LEX_DOLLAR, LEX_PCT, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, /* _!"#$%&'()*+,-./ */
112   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, LEX_QM,  /* 0123456789:;<=>? */
113   LEX_AT, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,  /* @ABCDEFGHIJKLMNO */
114   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, LEX_BR, 0, LEX_BR, 0, 3, /* PQRSTUVWXYZ[\]^_ */
115   0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,       /* `abcdefghijklmno */
116   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, LEX_BR, 0, LEX_BR, LEX_TILDE, 0, /* pqrstuvwxyz{|}~.  */
117   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
118   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
119   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
120   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
121   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
122   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
123   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
124   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
125 };
126
127 /* In: a character.
128    Out: 1 if this character ends a line.  */
129 char is_end_of_line[256] = {
130 #ifdef CR_EOL
131   1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,       /* @abcdefghijklmno */
132 #else
133   1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,       /* @abcdefghijklmno */
134 #endif
135   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
136   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* _!"#$%&'()*+,-./ */
137   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* 0123456789:;<=>? */
138   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
139   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
140   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
141   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
142   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
143   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
144   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
145   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
146   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
147   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
148   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* */
149   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0        /* */
150 };
151
152 #ifndef TC_CASE_SENSITIVE
153 char original_case_string[128];
154 #endif
155
156 /* Functions private to this file.  */
157
158 static char *buffer;    /* 1st char of each buffer of lines is here.  */
159 static char *buffer_limit;      /*->1 + last char in buffer.  */
160
161 /* TARGET_BYTES_BIG_ENDIAN is required to be defined to either 0 or 1
162    in the tc-<CPU>.h file.  See the "Porting GAS" section of the
163    internals manual.  */
164 int target_big_endian = TARGET_BYTES_BIG_ENDIAN;
165
166 /* Variables for handling include file directory table.  */
167
168 /* Table of pointers to directories to search for .include's.  */
169 char **include_dirs;
170
171 /* How many are in the table.  */
172 int include_dir_count;
173
174 /* Length of longest in table.  */
175 int include_dir_maxlen = 1;
176
177 #ifndef WORKING_DOT_WORD
178 struct broken_word *broken_words;
179 int new_broken_words;
180 #endif
181
182 /* The current offset into the absolute section.  We don't try to
183    build frags in the absolute section, since no data can be stored
184    there.  We just keep track of the current offset.  */
185 addressT abs_section_offset;
186
187 /* If this line had an MRI style label, it is stored in this variable.
188    This is used by some of the MRI pseudo-ops.  */
189 symbolS *line_label;
190
191 /* This global variable is used to support MRI common sections.  We
192    translate such sections into a common symbol.  This variable is
193    non-NULL when we are in an MRI common section.  */
194 symbolS *mri_common_symbol;
195
196 /* In MRI mode, after a dc.b pseudo-op with an odd number of bytes, we
197    need to align to an even byte boundary unless the next pseudo-op is
198    dc.b, ds.b, or dcb.b.  This variable is set to 1 if an alignment
199    may be needed.  */
200 static int mri_pending_align;
201
202 #ifndef NO_LISTING
203 #ifdef OBJ_ELF
204 /* This variable is set to be non-zero if the next string we see might
205    be the name of the source file in DWARF debugging information.  See
206    the comment in emit_expr for the format we look for.  */
207 static int dwarf_file_string;
208 #endif
209 #endif
210
211 static void do_s_func (int end_p, const char *default_prefix);
212 static void do_align (int, char *, int, int);
213 static void s_align (int, int);
214 static void s_altmacro (int);
215 static void s_bad_end (int);
216 #ifdef OBJ_ELF
217 static void s_gnu_attribute (int);
218 #endif
219 static void s_reloc (int);
220 static int hex_float (int, char *);
221 static segT get_known_segmented_expression (expressionS * expP);
222 static void pobegin (void);
223 static int get_line_sb (sb *);
224 static void generate_file_debug (void);
225 static char *_find_end_of_line (char *, int, int);
226 \f
227 void
228 read_begin (void)
229 {
230   const char *p;
231
232   pobegin ();
233   obj_read_begin_hook ();
234
235   /* Something close -- but not too close -- to a multiple of 1024.
236      The debugging malloc I'm using has 24 bytes of overhead.  */
237   obstack_begin (&notes, chunksize);
238   obstack_begin (&cond_obstack, chunksize);
239
240   /* Use machine dependent syntax.  */
241   for (p = line_separator_chars; *p; p++)
242     is_end_of_line[(unsigned char) *p] = 1;
243   /* Use more.  FIXME-SOMEDAY.  */
244
245   if (flag_mri)
246     lex_type['?'] = 3;
247 }
248 \f
249 #ifndef TC_ADDRESS_BYTES
250 #define TC_ADDRESS_BYTES address_bytes
251
252 static inline int
253 address_bytes (void)
254 {
255   /* Choose smallest of 1, 2, 4, 8 bytes that is large enough to
256      contain an address.  */
257   int n = (stdoutput->arch_info->bits_per_address - 1) / 8;
258   n |= n >> 1;
259   n |= n >> 2;
260   n += 1;
261   return n;
262 }
263 #endif
264
265 /* Set up pseudo-op tables.  */
266
267 static struct hash_control *po_hash;
268
269 static const pseudo_typeS potable[] = {
270   {"abort", s_abort, 0},
271   {"align", s_align_ptwo, 0},
272   {"altmacro", s_altmacro, 1},
273   {"ascii", stringer, 0},
274   {"asciz", stringer, 1},
275   {"balign", s_align_bytes, 0},
276   {"balignw", s_align_bytes, -2},
277   {"balignl", s_align_bytes, -4},
278 /* block  */
279   {"byte", cons, 1},
280   {"comm", s_comm, 0},
281   {"common", s_mri_common, 0},
282   {"common.s", s_mri_common, 1},
283   {"data", s_data, 0},
284   {"dc", cons, 2},
285 #ifdef TC_ADDRESS_BYTES
286   {"dc.a", cons, 0},
287 #endif
288   {"dc.b", cons, 1},
289   {"dc.d", float_cons, 'd'},
290   {"dc.l", cons, 4},
291   {"dc.s", float_cons, 'f'},
292   {"dc.w", cons, 2},
293   {"dc.x", float_cons, 'x'},
294   {"dcb", s_space, 2},
295   {"dcb.b", s_space, 1},
296   {"dcb.d", s_float_space, 'd'},
297   {"dcb.l", s_space, 4},
298   {"dcb.s", s_float_space, 'f'},
299   {"dcb.w", s_space, 2},
300   {"dcb.x", s_float_space, 'x'},
301   {"ds", s_space, 2},
302   {"ds.b", s_space, 1},
303   {"ds.d", s_space, 8},
304   {"ds.l", s_space, 4},
305   {"ds.p", s_space, 12},
306   {"ds.s", s_space, 4},
307   {"ds.w", s_space, 2},
308   {"ds.x", s_space, 12},
309   {"debug", s_ignore, 0},
310 #ifdef S_SET_DESC
311   {"desc", s_desc, 0},
312 #endif
313 /* dim  */
314   {"double", float_cons, 'd'},
315 /* dsect  */
316   {"eject", listing_eject, 0},  /* Formfeed listing.  */
317   {"else", s_else, 0},
318   {"elsec", s_else, 0},
319   {"elseif", s_elseif, (int) O_ne},
320   {"end", s_end, 0},
321   {"endc", s_endif, 0},
322   {"endfunc", s_func, 1},
323   {"endif", s_endif, 0},
324   {"endm", s_bad_end, 0},
325   {"endr", s_bad_end, 1},
326 /* endef  */
327   {"equ", s_set, 0},
328   {"equiv", s_set, 1},
329   {"eqv", s_set, -1},
330   {"err", s_err, 0},
331   {"error", s_errwarn, 1},
332   {"exitm", s_mexit, 0},
333 /* extend  */
334   {"extern", s_ignore, 0},      /* We treat all undef as ext.  */
335   {"appfile", s_app_file, 1},
336   {"appline", s_app_line, 1},
337   {"fail", s_fail, 0},
338   {"file", s_app_file, 0},
339   {"fill", s_fill, 0},
340   {"float", float_cons, 'f'},
341   {"format", s_ignore, 0},
342   {"func", s_func, 0},
343   {"global", s_globl, 0},
344   {"globl", s_globl, 0},
345 #ifdef OBJ_ELF
346   {"gnu_attribute", s_gnu_attribute, 0},
347 #endif
348   {"hword", cons, 2},
349   {"if", s_if, (int) O_ne},
350   {"ifb", s_ifb, 1},
351   {"ifc", s_ifc, 0},
352   {"ifdef", s_ifdef, 0},
353   {"ifeq", s_if, (int) O_eq},
354   {"ifeqs", s_ifeqs, 0},
355   {"ifge", s_if, (int) O_ge},
356   {"ifgt", s_if, (int) O_gt},
357   {"ifle", s_if, (int) O_le},
358   {"iflt", s_if, (int) O_lt},
359   {"ifnb", s_ifb, 0},
360   {"ifnc", s_ifc, 1},
361   {"ifndef", s_ifdef, 1},
362   {"ifne", s_if, (int) O_ne},
363   {"ifnes", s_ifeqs, 1},
364   {"ifnotdef", s_ifdef, 1},
365   {"incbin", s_incbin, 0},
366   {"include", s_include, 0},
367   {"int", cons, 4},
368   {"irp", s_irp, 0},
369   {"irep", s_irp, 0},
370   {"irpc", s_irp, 1},
371   {"irepc", s_irp, 1},
372   {"lcomm", s_lcomm, 0},
373   {"lflags", listing_flags, 0}, /* Listing flags.  */
374   {"linefile", s_app_line, 0},
375   {"linkonce", s_linkonce, 0},
376   {"list", listing_list, 1},    /* Turn listing on.  */
377   {"llen", listing_psize, 1},
378   {"long", cons, 4},
379   {"lsym", s_lsym, 0},
380   {"macro", s_macro, 0},
381   {"mexit", s_mexit, 0},
382   {"mri", s_mri, 0},
383   {".mri", s_mri, 0},   /* Special case so .mri works in MRI mode.  */
384   {"name", s_ignore, 0},
385   {"noaltmacro", s_altmacro, 0},
386   {"noformat", s_ignore, 0},
387   {"nolist", listing_list, 0},  /* Turn listing off.  */
388   {"nopage", listing_nopage, 0},
389   {"octa", cons, 16},
390   {"offset", s_struct, 0},
391   {"org", s_org, 0},
392   {"p2align", s_align_ptwo, 0},
393   {"p2alignw", s_align_ptwo, -2},
394   {"p2alignl", s_align_ptwo, -4},
395   {"page", listing_eject, 0},
396   {"plen", listing_psize, 0},
397   {"print", s_print, 0},
398   {"psize", listing_psize, 0},  /* Set paper size.  */
399   {"purgem", s_purgem, 0},
400   {"quad", cons, 8},
401   {"reloc", s_reloc, 0},
402   {"rep", s_rept, 0},
403   {"rept", s_rept, 0},
404   {"rva", s_rva, 4},
405   {"sbttl", listing_title, 1},  /* Subtitle of listing.  */
406 /* scl  */
407 /* sect  */
408   {"set", s_set, 0},
409   {"short", cons, 2},
410   {"single", float_cons, 'f'},
411 /* size  */
412   {"space", s_space, 0},
413   {"skip", s_space, 0},
414   {"sleb128", s_leb128, 1},
415   {"spc", s_ignore, 0},
416   {"stabd", s_stab, 'd'},
417   {"stabn", s_stab, 'n'},
418   {"stabs", s_stab, 's'},
419   {"string", stringer, 1},
420   {"struct", s_struct, 0},
421 /* tag  */
422   {"text", s_text, 0},
423
424   /* This is for gcc to use.  It's only just been added (2/94), so gcc
425      won't be able to use it for a while -- probably a year or more.
426      But once this has been released, check with gcc maintainers
427      before deleting it or even changing the spelling.  */
428   {"this_GCC_requires_the_GNU_assembler", s_ignore, 0},
429   /* If we're folding case -- done for some targets, not necessarily
430      all -- the above string in an input file will be converted to
431      this one.  Match it either way...  */
432   {"this_gcc_requires_the_gnu_assembler", s_ignore, 0},
433
434   {"title", listing_title, 0},  /* Listing title.  */
435   {"ttl", listing_title, 0},
436 /* type  */
437   {"uleb128", s_leb128, 0},
438 /* use  */
439 /* val  */
440   {"xcom", s_comm, 0},
441   {"xdef", s_globl, 0},
442   {"xref", s_ignore, 0},
443   {"xstabs", s_xstab, 's'},
444   {"warning", s_errwarn, 0},
445   {"weakref", s_weakref, 0},
446   {"word", cons, 2},
447   {"zero", s_space, 0},
448   {NULL, NULL, 0}                       /* End sentinel.  */
449 };
450
451 static offsetT
452 get_absolute_expr (expressionS *exp)
453 {
454   expression_and_evaluate (exp);
455   if (exp->X_op != O_constant)
456     {
457       if (exp->X_op != O_absent)
458         as_bad (_("bad or irreducible absolute expression"));
459       exp->X_add_number = 0;
460     }
461   return exp->X_add_number;
462 }
463
464 offsetT
465 get_absolute_expression (void)
466 {
467   expressionS exp;
468
469   return get_absolute_expr (&exp);
470 }
471
472 static int pop_override_ok = 0;
473 static const char *pop_table_name;
474
475 void
476 pop_insert (const pseudo_typeS *table)
477 {
478   const char *errtxt;
479   const pseudo_typeS *pop;
480   for (pop = table; pop->poc_name; pop++)
481     {
482       errtxt = hash_insert (po_hash, pop->poc_name, (char *) pop);
483       if (errtxt && (!pop_override_ok || strcmp (errtxt, "exists")))
484         as_fatal (_("error constructing %s pseudo-op table: %s"), pop_table_name,
485                   errtxt);
486     }
487 }
488
489 #ifndef md_pop_insert
490 #define md_pop_insert()         pop_insert(md_pseudo_table)
491 #endif
492
493 #ifndef obj_pop_insert
494 #define obj_pop_insert()        pop_insert(obj_pseudo_table)
495 #endif
496
497 #ifndef cfi_pop_insert
498 #define cfi_pop_insert()        pop_insert(cfi_pseudo_table)
499 #endif
500
501 static void
502 pobegin (void)
503 {
504   po_hash = hash_new ();
505
506   /* Do the target-specific pseudo ops.  */
507   pop_table_name = "md";
508   md_pop_insert ();
509
510   /* Now object specific.  Skip any that were in the target table.  */
511   pop_table_name = "obj";
512   pop_override_ok = 1;
513   obj_pop_insert ();
514
515   /* Now portable ones.  Skip any that we've seen already.  */
516   pop_table_name = "standard";
517   pop_insert (potable);
518
519 #ifdef TARGET_USE_CFIPOP
520   pop_table_name = "cfi";
521   pop_override_ok = 1;
522   cfi_pop_insert ();
523 #endif
524 }
525 \f
526 #define HANDLE_CONDITIONAL_ASSEMBLY()                                   \
527   if (ignore_input ())                                                  \
528     {                                                                   \
529       char *eol = find_end_of_line (input_line_pointer, flag_m68k_mri); \
530       input_line_pointer = (input_line_pointer <= buffer_limit          \
531                             && eol >= buffer_limit)                     \
532                            ? buffer_limit                               \
533                            : eol + 1;                                   \
534       continue;                                                         \
535     }
536
537 /* This function is used when scrubbing the characters between #APP
538    and #NO_APP.  */
539
540 static char *scrub_string;
541 static char *scrub_string_end;
542
543 static int
544 scrub_from_string (char *buf, int buflen)
545 {
546   int copy;
547
548   copy = scrub_string_end - scrub_string;
549   if (copy > buflen)
550     copy = buflen;
551   memcpy (buf, scrub_string, copy);
552   scrub_string += copy;
553   return copy;
554 }
555
556 /* Helper function of read_a_source_file, which tries to expand a macro.  */
557 static int
558 try_macro (char term, const char *line)
559 {
560   sb out;
561   const char *err;
562   macro_entry *macro;
563
564   if (check_macro (line, &out, &err, &macro))
565     {
566       if (err != NULL)
567         as_bad ("%s", err);
568       *input_line_pointer++ = term;
569       input_scrub_include_sb (&out,
570                               input_line_pointer, 1);
571       sb_kill (&out);
572       buffer_limit =
573         input_scrub_next_buffer (&input_line_pointer);
574 #ifdef md_macro_info
575       md_macro_info (macro);
576 #endif
577       return 1;
578     }
579   return 0;
580 }
581
582 /* We read the file, putting things into a web that represents what we
583    have been reading.  */
584 void
585 read_a_source_file (char *name)
586 {
587   register char c;
588   register char *s;             /* String of symbol, '\0' appended.  */
589   register int temp;
590   pseudo_typeS *pop;
591
592 #ifdef WARN_COMMENTS
593   found_comment = 0;
594 #endif
595
596   buffer = input_scrub_new_file (name);
597
598   listing_file (name);
599   listing_newline (NULL);
600   register_dependency (name);
601
602   /* Generate debugging information before we've read anything in to denote
603      this file as the "main" source file and not a subordinate one
604      (e.g. N_SO vs N_SOL in stabs).  */
605   generate_file_debug ();
606
607   while ((buffer_limit = input_scrub_next_buffer (&input_line_pointer)) != 0)
608     {                           /* We have another line to parse.  */
609 #ifndef NO_LISTING
610       /* In order to avoid listing macro expansion lines with labels
611          multiple times, keep track of which line was last issued.  */
612       static char *last_eol;
613
614       last_eol = NULL;
615 #endif
616       while (input_line_pointer < buffer_limit)
617         {
618           /* We have more of this buffer to parse.  */
619
620           /* We now have input_line_pointer->1st char of next line.
621              If input_line_pointer [-1] == '\n' then we just
622              scanned another line: so bump line counters.  */
623           if (is_end_of_line[(unsigned char) input_line_pointer[-1]])
624             {
625 #ifdef md_start_line_hook
626               md_start_line_hook ();
627 #endif
628               if (input_line_pointer[-1] == '\n')
629                 bump_line_counters ();
630
631               line_label = NULL;
632
633               if (LABELS_WITHOUT_COLONS || flag_m68k_mri)
634                 {
635                   /* Text at the start of a line must be a label, we
636                      run down and stick a colon in.  */
637                   if (is_name_beginner (*input_line_pointer))
638                     {
639                       char *line_start = input_line_pointer;
640                       char c;
641                       int mri_line_macro;
642
643                       LISTING_NEWLINE ();
644                       HANDLE_CONDITIONAL_ASSEMBLY ();
645
646                       c = get_symbol_end ();
647
648                       /* In MRI mode, the EQU and MACRO pseudoops must
649                          be handled specially.  */
650                       mri_line_macro = 0;
651                       if (flag_m68k_mri)
652                         {
653                           char *rest = input_line_pointer + 1;
654
655                           if (*rest == ':')
656                             ++rest;
657                           if (*rest == ' ' || *rest == '\t')
658                             ++rest;
659                           if ((strncasecmp (rest, "EQU", 3) == 0
660                                || strncasecmp (rest, "SET", 3) == 0)
661                               && (rest[3] == ' ' || rest[3] == '\t'))
662                             {
663                               input_line_pointer = rest + 3;
664                               equals (line_start,
665                                       strncasecmp (rest, "SET", 3) == 0);
666                               continue;
667                             }
668                           if (strncasecmp (rest, "MACRO", 5) == 0
669                               && (rest[5] == ' '
670                                   || rest[5] == '\t'
671                                   || is_end_of_line[(unsigned char) rest[5]]))
672                             mri_line_macro = 1;
673                         }
674
675                       /* In MRI mode, we need to handle the MACRO
676                          pseudo-op specially: we don't want to put the
677                          symbol in the symbol table.  */
678                       if (!mri_line_macro
679 #ifdef TC_START_LABEL_WITHOUT_COLON
680                           && TC_START_LABEL_WITHOUT_COLON(c,
681                                                           input_line_pointer)
682 #endif
683                           )
684                         line_label = colon (line_start);
685                       else
686                         line_label = symbol_create (line_start,
687                                                     absolute_section,
688                                                     (valueT) 0,
689                                                     &zero_address_frag);
690
691                       *input_line_pointer = c;
692                       if (c == ':')
693                         input_line_pointer++;
694                     }
695                 }
696             }
697
698           /* We are at the beginning of a line, or similar place.
699              We expect a well-formed assembler statement.
700              A "symbol-name:" is a statement.
701
702              Depending on what compiler is used, the order of these tests
703              may vary to catch most common case 1st.
704              Each test is independent of all other tests at the (top)
705              level.  */
706           do
707             c = *input_line_pointer++;
708           while (c == '\t' || c == ' ' || c == '\f');
709
710 #ifndef NO_LISTING
711           /* If listing is on, and we are expanding a macro, then give
712              the listing code the contents of the expanded line.  */
713           if (listing)
714             {
715               if ((listing & LISTING_MACEXP) && macro_nest > 0)
716                 {
717                   char *copy;
718                   int len;
719
720                   /* Find the end of the current expanded macro line.  */
721                   s = find_end_of_line (input_line_pointer - 1, flag_m68k_mri);
722
723                   if (s != last_eol)
724                     {
725                       last_eol = s;
726                       /* Copy it for safe keeping.  Also give an indication of
727                          how much macro nesting is involved at this point.  */
728                       len = s - (input_line_pointer - 1);
729                       copy = (char *) xmalloc (len + macro_nest + 2);
730                       memset (copy, '>', macro_nest);
731                       copy[macro_nest] = ' ';
732                       memcpy (copy + macro_nest + 1, input_line_pointer - 1, len);
733                       copy[macro_nest + 1 + len] = '\0';
734
735                       /* Install the line with the listing facility.  */
736                       listing_newline (copy);
737                     }
738                 }
739               else
740                 listing_newline (NULL);
741             }
742 #endif
743           /* C is the 1st significant character.
744              Input_line_pointer points after that character.  */
745           if (is_name_beginner (c))
746             {
747               /* Want user-defined label or pseudo/opcode.  */
748               HANDLE_CONDITIONAL_ASSEMBLY ();
749
750               s = --input_line_pointer;
751               c = get_symbol_end ();    /* name's delimiter.  */
752
753               /* C is character after symbol.
754                  That character's place in the input line is now '\0'.
755                  S points to the beginning of the symbol.
756                    [In case of pseudo-op, s->'.'.]
757                  Input_line_pointer->'\0' where c was.  */
758               if (TC_START_LABEL (c, input_line_pointer))
759                 {
760                   if (flag_m68k_mri)
761                     {
762                       char *rest = input_line_pointer + 1;
763
764                       /* In MRI mode, \tsym: set 0 is permitted.  */
765                       if (*rest == ':')
766                         ++rest;
767
768                       if (*rest == ' ' || *rest == '\t')
769                         ++rest;
770
771                       if ((strncasecmp (rest, "EQU", 3) == 0
772                            || strncasecmp (rest, "SET", 3) == 0)
773                           && (rest[3] == ' ' || rest[3] == '\t'))
774                         {
775                           input_line_pointer = rest + 3;
776                           equals (s, 1);
777                           continue;
778                         }
779                     }
780
781                   line_label = colon (s);       /* User-defined label.  */
782                   /* Put ':' back for error messages' sake.  */
783                   *input_line_pointer++ = ':';
784 #ifdef tc_check_label
785                   tc_check_label (line_label);
786 #endif
787                   /* Input_line_pointer->after ':'.  */
788                   SKIP_WHITESPACE ();
789                 }
790               else if (input_line_pointer[1] == '='
791                        && (c == '='
792                            || ((c == ' ' || c == '\t')
793                                && input_line_pointer[2] == '=')))
794                 {
795                   equals (s, -1);
796                   demand_empty_rest_of_line ();
797                 }
798               else if ((c == '='
799                        || ((c == ' ' || c == '\t')
800                             && input_line_pointer[1] == '='))
801 #ifdef TC_EQUAL_IN_INSN
802                            && !TC_EQUAL_IN_INSN (c, s)
803 #endif
804                            )
805                 {
806                   equals (s, 1);
807                   demand_empty_rest_of_line ();
808                 }
809               else
810                 {
811                   /* Expect pseudo-op or machine instruction.  */
812                   pop = NULL;
813
814 #ifndef TC_CASE_SENSITIVE
815                   {
816                     char *s2 = s;
817
818                     strncpy (original_case_string, s2, sizeof (original_case_string));
819                     original_case_string[sizeof (original_case_string) - 1] = 0;
820
821                     while (*s2)
822                       {
823                         *s2 = TOLOWER (*s2);
824                         s2++;
825                       }
826                   }
827 #endif
828                   if (NO_PSEUDO_DOT || flag_m68k_mri)
829                     {
830                       /* The MRI assembler uses pseudo-ops without
831                          a period.  */
832                       pop = (pseudo_typeS *) hash_find (po_hash, s);
833                       if (pop != NULL && pop->poc_handler == NULL)
834                         pop = NULL;
835                     }
836
837                   if (pop != NULL
838                       || (!flag_m68k_mri && *s == '.'))
839                     {
840                       /* PSEUDO - OP.
841
842                          WARNING: c has next char, which may be end-of-line.
843                          We lookup the pseudo-op table with s+1 because we
844                          already know that the pseudo-op begins with a '.'.  */
845
846                       if (pop == NULL)
847                         pop = (pseudo_typeS *) hash_find (po_hash, s + 1);
848                       if (pop && !pop->poc_handler)
849                         pop = NULL;
850
851                       /* In MRI mode, we may need to insert an
852                          automatic alignment directive.  What a hack
853                          this is.  */
854                       if (mri_pending_align
855                           && (pop == NULL
856                               || !((pop->poc_handler == cons
857                                     && pop->poc_val == 1)
858                                    || (pop->poc_handler == s_space
859                                        && pop->poc_val == 1)
860 #ifdef tc_conditional_pseudoop
861                                    || tc_conditional_pseudoop (pop)
862 #endif
863                                    || pop->poc_handler == s_if
864                                    || pop->poc_handler == s_ifdef
865                                    || pop->poc_handler == s_ifc
866                                    || pop->poc_handler == s_ifeqs
867                                    || pop->poc_handler == s_else
868                                    || pop->poc_handler == s_endif
869                                    || pop->poc_handler == s_globl
870                                    || pop->poc_handler == s_ignore)))
871                         {
872                           do_align (1, (char *) NULL, 0, 0);
873                           mri_pending_align = 0;
874
875                           if (line_label != NULL)
876                             {
877                               symbol_set_frag (line_label, frag_now);
878                               S_SET_VALUE (line_label, frag_now_fix ());
879                             }
880                         }
881
882                       /* Print the error msg now, while we still can.  */
883                       if (pop == NULL)
884                         {
885                           char *end = input_line_pointer;
886
887                           *input_line_pointer = c;
888                           s_ignore (0);
889                           c = *--input_line_pointer;
890                           *input_line_pointer = '\0';
891                           if (! macro_defined || ! try_macro (c, s))
892                             {
893                               *end = '\0';
894                               as_bad (_("unknown pseudo-op: `%s'"), s);
895                               *input_line_pointer++ = c;
896                             }
897                           continue;
898                         }
899
900                       /* Put it back for error messages etc.  */
901                       *input_line_pointer = c;
902                       /* The following skip of whitespace is compulsory.
903                          A well shaped space is sometimes all that separates
904                          keyword from operands.  */
905                       if (c == ' ' || c == '\t')
906                         input_line_pointer++;
907
908                       /* Input_line is restored.
909                          Input_line_pointer->1st non-blank char
910                          after pseudo-operation.  */
911                       (*pop->poc_handler) (pop->poc_val);
912
913                       /* If that was .end, just get out now.  */
914                       if (pop->poc_handler == s_end)
915                         goto quit;
916                     }
917                   else
918                     {
919                       /* WARNING: c has char, which may be end-of-line.  */
920                       /* Also: input_line_pointer->`\0` where c was.  */
921                       *input_line_pointer = c;
922                       input_line_pointer = _find_end_of_line (input_line_pointer, flag_m68k_mri, 1);
923                       c = *input_line_pointer;
924                       *input_line_pointer = '\0';
925
926                       generate_lineno_debug ();
927
928                       if (macro_defined && try_macro (c, s))
929                         continue;
930
931                       if (mri_pending_align)
932                         {
933                           do_align (1, (char *) NULL, 0, 0);
934                           mri_pending_align = 0;
935                           if (line_label != NULL)
936                             {
937                               symbol_set_frag (line_label, frag_now);
938                               S_SET_VALUE (line_label, frag_now_fix ());
939                             }
940                         }
941
942                       md_assemble (s);  /* Assemble 1 instruction.  */
943
944                       *input_line_pointer++ = c;
945
946                       /* We resume loop AFTER the end-of-line from
947                          this instruction.  */
948                     }
949                 }
950               continue;
951             }
952
953           /* Empty statement?  */
954           if (is_end_of_line[(unsigned char) c])
955             continue;
956
957           if ((LOCAL_LABELS_DOLLAR || LOCAL_LABELS_FB) && ISDIGIT (c))
958             {
959               /* local label  ("4:")  */
960               char *backup = input_line_pointer;
961
962               HANDLE_CONDITIONAL_ASSEMBLY ();
963
964               temp = c - '0';
965
966               /* Read the whole number.  */
967               while (ISDIGIT (*input_line_pointer))
968                 {
969                   temp = (temp * 10) + *input_line_pointer - '0';
970                   ++input_line_pointer;
971                 }
972
973               if (LOCAL_LABELS_DOLLAR
974                   && *input_line_pointer == '$'
975                   && *(input_line_pointer + 1) == ':')
976                 {
977                   input_line_pointer += 2;
978
979                   if (dollar_label_defined (temp))
980                     {
981                       as_fatal (_("label \"%d$\" redefined"), temp);
982                     }
983
984                   define_dollar_label (temp);
985                   colon (dollar_label_name (temp, 0));
986                   continue;
987                 }
988
989               if (LOCAL_LABELS_FB
990                   && *input_line_pointer++ == ':')
991                 {
992                   fb_label_instance_inc (temp);
993                   colon (fb_label_name (temp, 0));
994                   continue;
995                 }
996
997               input_line_pointer = backup;
998             }                   /* local label  ("4:") */
999
1000           if (c && strchr (line_comment_chars, c))
1001             {                   /* Its a comment.  Better say APP or NO_APP.  */
1002               sb sbuf;
1003               char *ends;
1004               char *new_buf;
1005               char *new_tmp;
1006               unsigned int new_length;
1007               char *tmp_buf = 0;
1008
1009               s = input_line_pointer;
1010               if (strncmp (s, "APP\n", 4))
1011                 {
1012                   /* We ignore it.  */
1013                   ignore_rest_of_line ();
1014                   continue;
1015                 }
1016               bump_line_counters ();
1017               s += 4;
1018
1019               sb_new (&sbuf);
1020               ends = strstr (s, "#NO_APP\n");
1021
1022               if (!ends)
1023                 {
1024                   unsigned int tmp_len;
1025                   unsigned int num;
1026
1027                   /* The end of the #APP wasn't in this buffer.  We
1028                      keep reading in buffers until we find the #NO_APP
1029                      that goes with this #APP  There is one.  The specs
1030                      guarantee it...  */
1031                   tmp_len = buffer_limit - s;
1032                   tmp_buf = xmalloc (tmp_len + 1);
1033                   memcpy (tmp_buf, s, tmp_len);
1034                   do
1035                     {
1036                       new_tmp = input_scrub_next_buffer (&buffer);
1037                       if (!new_tmp)
1038                         break;
1039                       else
1040                         buffer_limit = new_tmp;
1041                       input_line_pointer = buffer;
1042                       ends = strstr (buffer, "#NO_APP\n");
1043                       if (ends)
1044                         num = ends - buffer;
1045                       else
1046                         num = buffer_limit - buffer;
1047
1048                       tmp_buf = xrealloc (tmp_buf, tmp_len + num);
1049                       memcpy (tmp_buf + tmp_len, buffer, num);
1050                       tmp_len += num;
1051                     }
1052                   while (!ends);
1053
1054                   input_line_pointer = ends ? ends + 8 : NULL;
1055
1056                   s = tmp_buf;
1057                   ends = s + tmp_len;
1058
1059                 }
1060               else
1061                 {
1062                   input_line_pointer = ends + 8;
1063                 }
1064
1065               scrub_string = s;
1066               scrub_string_end = ends;
1067
1068               new_length = ends - s;
1069               new_buf = (char *) xmalloc (new_length);
1070               new_tmp = new_buf;
1071               for (;;)
1072                 {
1073                   int space;
1074                   int size;
1075
1076                   space = (new_buf + new_length) - new_tmp;
1077                   size = do_scrub_chars (scrub_from_string, new_tmp, space);
1078
1079                   if (size < space)
1080                     {
1081                       new_tmp[size] = 0;
1082                       break;
1083                     }
1084
1085                   new_buf = xrealloc (new_buf, new_length + 100);
1086                   new_tmp = new_buf + new_length;
1087                   new_length += 100;
1088                 }
1089
1090               if (tmp_buf)
1091                 free (tmp_buf);
1092
1093               /* We've "scrubbed" input to the preferred format.  In the
1094                  process we may have consumed the whole of the remaining
1095                  file (and included files).  We handle this formatted
1096                  input similar to that of macro expansion, letting
1097                  actual macro expansion (possibly nested) and other
1098                  input expansion work.  Beware that in messages, line
1099                  numbers and possibly file names will be incorrect.  */
1100               sb_add_string (&sbuf, new_buf);
1101               input_scrub_include_sb (&sbuf, input_line_pointer, 0);
1102               sb_kill (&sbuf);
1103               buffer_limit = input_scrub_next_buffer (&input_line_pointer);
1104               free (new_buf);
1105               continue;
1106             }
1107
1108           HANDLE_CONDITIONAL_ASSEMBLY ();
1109
1110 #ifdef tc_unrecognized_line
1111           if (tc_unrecognized_line (c))
1112             continue;
1113 #endif
1114           input_line_pointer--;
1115           /* Report unknown char as error.  */
1116           demand_empty_rest_of_line ();
1117         }
1118
1119 #ifdef md_after_pass_hook
1120       md_after_pass_hook ();
1121 #endif
1122     }
1123
1124  quit:
1125
1126 #ifdef md_cleanup
1127   md_cleanup ();
1128 #endif
1129   /* Close the input file.  */
1130   input_scrub_close ();
1131 #ifdef WARN_COMMENTS
1132   {
1133     if (warn_comment && found_comment)
1134       as_warn_where (found_comment_file, found_comment,
1135                      "first comment found here");
1136   }
1137 #endif
1138 }
1139
1140 /* Convert O_constant expression EXP into the equivalent O_big representation.
1141    Take the sign of the number from X_unsigned rather than X_add_number.  */
1142
1143 static void
1144 convert_to_bignum (expressionS *exp)
1145 {
1146   valueT value;
1147   unsigned int i;
1148
1149   value = exp->X_add_number;
1150   for (i = 0; i < sizeof (exp->X_add_number) / CHARS_PER_LITTLENUM; i++)
1151     {
1152       generic_bignum[i] = value & LITTLENUM_MASK;
1153       value >>= LITTLENUM_NUMBER_OF_BITS;
1154     }
1155   /* Add a sequence of sign bits if the top bit of X_add_number is not
1156      the sign of the original value.  */
1157   if ((exp->X_add_number < 0) != !exp->X_unsigned)
1158     generic_bignum[i++] = exp->X_unsigned ? 0 : LITTLENUM_MASK;
1159   exp->X_op = O_big;
1160   exp->X_add_number = i;
1161 }
1162
1163 /* For most MRI pseudo-ops, the line actually ends at the first
1164    nonquoted space.  This function looks for that point, stuffs a null
1165    in, and sets *STOPCP to the character that used to be there, and
1166    returns the location.
1167
1168    Until I hear otherwise, I am going to assume that this is only true
1169    for the m68k MRI assembler.  */
1170
1171 char *
1172 mri_comment_field (char *stopcp)
1173 {
1174   char *s;
1175 #ifdef TC_M68K
1176   int inquote = 0;
1177
1178   know (flag_m68k_mri);
1179
1180   for (s = input_line_pointer;
1181        ((!is_end_of_line[(unsigned char) *s] && *s != ' ' && *s != '\t')
1182         || inquote);
1183        s++)
1184     {
1185       if (*s == '\'')
1186         inquote = !inquote;
1187     }
1188 #else
1189   for (s = input_line_pointer;
1190        !is_end_of_line[(unsigned char) *s];
1191        s++)
1192     ;
1193 #endif
1194   *stopcp = *s;
1195   *s = '\0';
1196
1197   return s;
1198 }
1199
1200 /* Skip to the end of an MRI comment field.  */
1201
1202 void
1203 mri_comment_end (char *stop, int stopc)
1204 {
1205   know (flag_mri);
1206
1207   input_line_pointer = stop;
1208   *stop = stopc;
1209   while (!is_end_of_line[(unsigned char) *input_line_pointer])
1210     ++input_line_pointer;
1211 }
1212
1213 void
1214 s_abort (int ignore ATTRIBUTE_UNUSED)
1215 {
1216   as_fatal (_(".abort detected.  Abandoning ship."));
1217 }
1218
1219 /* Guts of .align directive.  N is the power of two to which to align.
1220    FILL may be NULL, or it may point to the bytes of the fill pattern.
1221    LEN is the length of whatever FILL points to, if anything.  MAX is
1222    the maximum number of characters to skip when doing the alignment,
1223    or 0 if there is no maximum.  */
1224
1225 static void
1226 do_align (int n, char *fill, int len, int max)
1227 {
1228   if (now_seg == absolute_section)
1229     {
1230       if (fill != NULL)
1231         while (len-- > 0)
1232           if (*fill++ != '\0')
1233             {
1234               as_warn (_("ignoring fill value in absolute section"));
1235               break;
1236             }
1237       fill = NULL;
1238       len = 0;
1239     }
1240
1241 #ifdef md_flush_pending_output
1242   md_flush_pending_output ();
1243 #endif
1244 #ifdef md_do_align
1245   md_do_align (n, fill, len, max, just_record_alignment);
1246 #endif
1247
1248   /* Only make a frag if we HAVE to...  */
1249   if (n != 0 && !need_pass_2)
1250     {
1251       if (fill == NULL)
1252         {
1253           if (subseg_text_p (now_seg))
1254             frag_align_code (n, max);
1255           else
1256             frag_align (n, 0, max);
1257         }
1258       else if (len <= 1)
1259         frag_align (n, *fill, max);
1260       else
1261         frag_align_pattern (n, fill, len, max);
1262     }
1263
1264 #ifdef md_do_align
1265  just_record_alignment: ATTRIBUTE_UNUSED_LABEL
1266 #endif
1267
1268   record_alignment (now_seg, n - OCTETS_PER_BYTE_POWER);
1269 }
1270
1271 /* Handle the .align pseudo-op.  A positive ARG is a default alignment
1272    (in bytes).  A negative ARG is the negative of the length of the
1273    fill pattern.  BYTES_P is non-zero if the alignment value should be
1274    interpreted as the byte boundary, rather than the power of 2.  */
1275
1276 #define ALIGN_LIMIT (stdoutput->arch_info->bits_per_address - 1)
1277
1278 static void
1279 s_align (int arg, int bytes_p)
1280 {
1281   unsigned int align_limit = ALIGN_LIMIT;
1282   unsigned int align;
1283   char *stop = NULL;
1284   char stopc = 0;
1285   offsetT fill = 0;
1286   int max;
1287   int fill_p;
1288
1289   if (flag_mri)
1290     stop = mri_comment_field (&stopc);
1291
1292   if (is_end_of_line[(unsigned char) *input_line_pointer])
1293     {
1294       if (arg < 0)
1295         align = 0;
1296       else
1297         align = arg;    /* Default value from pseudo-op table.  */
1298     }
1299   else
1300     {
1301       align = get_absolute_expression ();
1302       SKIP_WHITESPACE ();
1303     }
1304
1305   if (bytes_p)
1306     {
1307       /* Convert to a power of 2.  */
1308       if (align != 0)
1309         {
1310           unsigned int i;
1311
1312           for (i = 0; (align & 1) == 0; align >>= 1, ++i)
1313             ;
1314           if (align != 1)
1315             as_bad (_("alignment not a power of 2"));
1316
1317           align = i;
1318         }
1319     }
1320
1321   if (align > align_limit)
1322     {
1323       align = align_limit;
1324       as_warn (_("alignment too large: %u assumed"), align);
1325     }
1326
1327   if (*input_line_pointer != ',')
1328     {
1329       fill_p = 0;
1330       max = 0;
1331     }
1332   else
1333     {
1334       ++input_line_pointer;
1335       if (*input_line_pointer == ',')
1336         fill_p = 0;
1337       else
1338         {
1339           fill = get_absolute_expression ();
1340           SKIP_WHITESPACE ();
1341           fill_p = 1;
1342         }
1343
1344       if (*input_line_pointer != ',')
1345         max = 0;
1346       else
1347         {
1348           ++input_line_pointer;
1349           max = get_absolute_expression ();
1350         }
1351     }
1352
1353   if (!fill_p)
1354     {
1355       if (arg < 0)
1356         as_warn (_("expected fill pattern missing"));
1357       do_align (align, (char *) NULL, 0, max);
1358     }
1359   else
1360     {
1361       int fill_len;
1362
1363       if (arg >= 0)
1364         fill_len = 1;
1365       else
1366         fill_len = -arg;
1367       if (fill_len <= 1)
1368         {
1369           char fill_char;
1370
1371           fill_char = fill;
1372           do_align (align, &fill_char, fill_len, max);
1373         }
1374       else
1375         {
1376           char ab[16];
1377
1378           if ((size_t) fill_len > sizeof ab)
1379             abort ();
1380           md_number_to_chars (ab, fill, fill_len);
1381           do_align (align, ab, fill_len, max);
1382         }
1383     }
1384
1385   demand_empty_rest_of_line ();
1386
1387   if (flag_mri)
1388     mri_comment_end (stop, stopc);
1389 }
1390
1391 /* Handle the .align pseudo-op on machines where ".align 4" means
1392    align to a 4 byte boundary.  */
1393
1394 void
1395 s_align_bytes (int arg)
1396 {
1397   s_align (arg, 1);
1398 }
1399
1400 /* Handle the .align pseudo-op on machines where ".align 4" means align
1401    to a 2**4 boundary.  */
1402
1403 void
1404 s_align_ptwo (int arg)
1405 {
1406   s_align (arg, 0);
1407 }
1408
1409 /* Switch in and out of alternate macro mode.  */
1410
1411 void
1412 s_altmacro (int on)
1413 {
1414   demand_empty_rest_of_line ();
1415   macro_set_alternate (on);
1416 }
1417
1418 symbolS *
1419 s_comm_internal (int param,
1420                  symbolS *(*comm_parse_extra) (int, symbolS *, addressT))
1421 {
1422   char *name;
1423   char c;
1424   char *p;
1425   offsetT temp, size;
1426   symbolS *symbolP = NULL;
1427   char *stop = NULL;
1428   char stopc = 0;
1429   expressionS exp;
1430
1431   if (flag_mri)
1432     stop = mri_comment_field (&stopc);
1433
1434   name = input_line_pointer;
1435   c = get_symbol_end ();
1436   /* Just after name is now '\0'.  */
1437   p = input_line_pointer;
1438   *p = c;
1439
1440   if (name == p)
1441     {
1442       as_bad (_("expected symbol name"));
1443       ignore_rest_of_line ();
1444       goto out;
1445     }
1446
1447   SKIP_WHITESPACE ();
1448
1449   /* Accept an optional comma after the name.  The comma used to be
1450      required, but Irix 5 cc does not generate it for .lcomm.  */
1451   if (*input_line_pointer == ',')
1452     input_line_pointer++;
1453
1454   temp = get_absolute_expr (&exp);
1455   size = temp;
1456   size &= ((offsetT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
1457   if (exp.X_op == O_absent)
1458     {
1459       as_bad (_("missing size expression"));
1460       ignore_rest_of_line ();
1461       goto out;
1462     }
1463   else if (temp != size || !exp.X_unsigned)
1464     {
1465       as_warn (_("size (%ld) out of range, ignored"), (long) temp);
1466       ignore_rest_of_line ();
1467       goto out;
1468     }
1469
1470   *p = 0;
1471   symbolP = symbol_find_or_make (name);
1472   if ((S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
1473       && !S_IS_COMMON (symbolP))
1474     {
1475       if (!S_IS_VOLATILE (symbolP))
1476         {
1477           symbolP = NULL;
1478           as_bad (_("symbol `%s' is already defined"), name);
1479           *p = c;
1480           ignore_rest_of_line ();
1481           goto out;
1482         }
1483       symbolP = symbol_clone (symbolP, 1);
1484       S_SET_SEGMENT (symbolP, undefined_section);
1485       S_SET_VALUE (symbolP, 0);
1486       symbol_set_frag (symbolP, &zero_address_frag);
1487       S_CLEAR_VOLATILE (symbolP);
1488     }
1489
1490   size = S_GET_VALUE (symbolP);
1491   if (size == 0)
1492     size = temp;
1493   else if (size != temp)
1494     as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
1495              name, (long) size, (long) temp);
1496
1497   *p = c;
1498   if (comm_parse_extra != NULL)
1499     symbolP = (*comm_parse_extra) (param, symbolP, size);
1500   else
1501     {
1502       S_SET_VALUE (symbolP, (valueT) size);
1503       S_SET_EXTERNAL (symbolP);
1504       S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
1505 #ifdef OBJ_VMS
1506       {
1507         extern int flag_one;
1508         if (size == 0 || !flag_one)
1509           S_GET_OTHER (symbolP) = const_flag;
1510       }
1511 #endif
1512     }
1513
1514   demand_empty_rest_of_line ();
1515  out:
1516   if (flag_mri)
1517     mri_comment_end (stop, stopc);
1518   return symbolP;
1519 }
1520
1521 void
1522 s_comm (int ignore)
1523 {
1524   s_comm_internal (ignore, NULL);
1525 }
1526
1527 /* The MRI COMMON pseudo-op.  We handle this by creating a common
1528    symbol with the appropriate name.  We make s_space do the right
1529    thing by increasing the size.  */
1530
1531 void
1532 s_mri_common (int small ATTRIBUTE_UNUSED)
1533 {
1534   char *name;
1535   char c;
1536   char *alc = NULL;
1537   symbolS *sym;
1538   offsetT align;
1539   char *stop = NULL;
1540   char stopc = 0;
1541
1542   if (!flag_mri)
1543     {
1544       s_comm (0);
1545       return;
1546     }
1547
1548   stop = mri_comment_field (&stopc);
1549
1550   SKIP_WHITESPACE ();
1551
1552   name = input_line_pointer;
1553   if (!ISDIGIT (*name))
1554     c = get_symbol_end ();
1555   else
1556     {
1557       do
1558         {
1559           ++input_line_pointer;
1560         }
1561       while (ISDIGIT (*input_line_pointer));
1562
1563       c = *input_line_pointer;
1564       *input_line_pointer = '\0';
1565
1566       if (line_label != NULL)
1567         {
1568           alc = (char *) xmalloc (strlen (S_GET_NAME (line_label))
1569                                   + (input_line_pointer - name)
1570                                   + 1);
1571           sprintf (alc, "%s%s", name, S_GET_NAME (line_label));
1572           name = alc;
1573         }
1574     }
1575
1576   sym = symbol_find_or_make (name);
1577   *input_line_pointer = c;
1578   if (alc != NULL)
1579     free (alc);
1580
1581   if (*input_line_pointer != ',')
1582     align = 0;
1583   else
1584     {
1585       ++input_line_pointer;
1586       align = get_absolute_expression ();
1587     }
1588
1589   if (S_IS_DEFINED (sym) && !S_IS_COMMON (sym))
1590     {
1591       as_bad (_("symbol `%s' is already defined"), S_GET_NAME (sym));
1592       ignore_rest_of_line ();
1593       mri_comment_end (stop, stopc);
1594       return;
1595     }
1596
1597   S_SET_EXTERNAL (sym);
1598   S_SET_SEGMENT (sym, bfd_com_section_ptr);
1599   mri_common_symbol = sym;
1600
1601 #ifdef S_SET_ALIGN
1602   if (align != 0)
1603     S_SET_ALIGN (sym, align);
1604 #endif
1605
1606   if (line_label != NULL)
1607     {
1608       expressionS exp;
1609       exp.X_op = O_symbol;
1610       exp.X_add_symbol = sym;
1611       exp.X_add_number = 0;
1612       symbol_set_value_expression (line_label, &exp);
1613       symbol_set_frag (line_label, &zero_address_frag);
1614       S_SET_SEGMENT (line_label, expr_section);
1615     }
1616
1617   /* FIXME: We just ignore the small argument, which distinguishes
1618      COMMON and COMMON.S.  I don't know what we can do about it.  */
1619
1620   /* Ignore the type and hptype.  */
1621   if (*input_line_pointer == ',')
1622     input_line_pointer += 2;
1623   if (*input_line_pointer == ',')
1624     input_line_pointer += 2;
1625
1626   demand_empty_rest_of_line ();
1627
1628   mri_comment_end (stop, stopc);
1629 }
1630
1631 void
1632 s_data (int ignore ATTRIBUTE_UNUSED)
1633 {
1634   segT section;
1635   register int temp;
1636
1637   temp = get_absolute_expression ();
1638   if (flag_readonly_data_in_text)
1639     {
1640       section = text_section;
1641       temp += 1000;
1642     }
1643   else
1644     section = data_section;
1645
1646   subseg_set (section, (subsegT) temp);
1647
1648 #ifdef OBJ_VMS
1649   const_flag = 0;
1650 #endif
1651   demand_empty_rest_of_line ();
1652 }
1653
1654 /* Handle the .appfile pseudo-op.  This is automatically generated by
1655    do_scrub_chars when a preprocessor # line comment is seen with a
1656    file name.  This default definition may be overridden by the object
1657    or CPU specific pseudo-ops.  This function is also the default
1658    definition for .file; the APPFILE argument is 1 for .appfile, 0 for
1659    .file.  */
1660
1661 void
1662 s_app_file_string (char *file, int appfile ATTRIBUTE_UNUSED)
1663 {
1664 #ifdef LISTING
1665   if (listing)
1666     listing_source_file (file);
1667 #endif
1668   register_dependency (file);
1669 #ifdef obj_app_file
1670   obj_app_file (file, appfile);
1671 #endif
1672 }
1673
1674 void
1675 s_app_file (int appfile)
1676 {
1677   register char *s;
1678   int length;
1679
1680   /* Some assemblers tolerate immediately following '"'.  */
1681   if ((s = demand_copy_string (&length)) != 0)
1682     {
1683       int may_omit
1684         = (!new_logical_line_flags (s, -1, 1) && appfile);
1685
1686       /* In MRI mode, the preprocessor may have inserted an extraneous
1687          backquote.  */
1688       if (flag_m68k_mri
1689           && *input_line_pointer == '\''
1690           && is_end_of_line[(unsigned char) input_line_pointer[1]])
1691         ++input_line_pointer;
1692
1693       demand_empty_rest_of_line ();
1694       if (!may_omit)
1695         s_app_file_string (s, appfile);
1696     }
1697 }
1698
1699 static int
1700 get_linefile_number (int *flag)
1701 {
1702   SKIP_WHITESPACE ();
1703
1704   if (*input_line_pointer < '0' || *input_line_pointer > '9')
1705     return 0;
1706
1707   *flag = get_absolute_expression ();
1708
1709   return 1;
1710 }
1711
1712 /* Handle the .appline pseudo-op.  This is automatically generated by
1713    do_scrub_chars when a preprocessor # line comment is seen.  This
1714    default definition may be overridden by the object or CPU specific
1715    pseudo-ops.  */
1716
1717 void
1718 s_app_line (int appline)
1719 {
1720   char *file = NULL;
1721   int l;
1722
1723   /* The given number is that of the next line.  */
1724   if (appline)
1725     l = get_absolute_expression ();
1726   else if (!get_linefile_number (&l))
1727     {
1728       ignore_rest_of_line ();
1729       return;
1730     }
1731
1732   l--;
1733
1734   if (l < -1)
1735     /* Some of the back ends can't deal with non-positive line numbers.
1736        Besides, it's silly.  GCC however will generate a line number of
1737        zero when it is pre-processing builtins for assembler-with-cpp files:
1738
1739           # 0 "<built-in>"
1740
1741        We do not want to barf on this, especially since such files are used
1742        in the GCC and GDB testsuites.  So we check for negative line numbers
1743        rather than non-positive line numbers.  */
1744     as_warn (_("line numbers must be positive; line number %d rejected"),
1745              l + 1);
1746   else
1747     {
1748       int flags = 0;
1749       int length = 0;
1750
1751       if (!appline)
1752         {
1753           SKIP_WHITESPACE ();
1754
1755           if (*input_line_pointer == '"')
1756             file = demand_copy_string (&length);
1757
1758           if (file)
1759             {
1760               int this_flag;
1761
1762               while (get_linefile_number (&this_flag))
1763                 switch (this_flag)
1764                   {
1765                     /* From GCC's cpp documentation:
1766                        1: start of a new file.
1767                        2: returning to a file after having included
1768                           another file.
1769                        3: following text comes from a system header file.
1770                        4: following text should be treated as extern "C".
1771
1772                        4 is nonsensical for the assembler; 3, we don't
1773                        care about, so we ignore it just in case a
1774                        system header file is included while
1775                        preprocessing assembly.  So 1 and 2 are all we
1776                        care about, and they are mutually incompatible.
1777                        new_logical_line_flags() demands this.  */
1778                   case 1:
1779                   case 2:
1780                     if (flags && flags != (1 << this_flag))
1781                       as_warn (_("incompatible flag %i in line directive"),
1782                                this_flag);
1783                     else
1784                       flags |= 1 << this_flag;
1785                     break;
1786
1787                   case 3:
1788                   case 4:
1789                     /* We ignore these.  */
1790                     break;
1791
1792                   default:
1793                     as_warn (_("unsupported flag %i in line directive"),
1794                              this_flag);
1795                     break;
1796                   }
1797
1798               if (!is_end_of_line[(unsigned char)*input_line_pointer])
1799                 file = 0;
1800             }
1801         }
1802
1803       if (appline || file)
1804         {
1805           new_logical_line_flags (file, l, flags);
1806 #ifdef LISTING
1807           if (listing)
1808             listing_source_line (l);
1809 #endif
1810         }
1811     }
1812   if (appline || file)
1813     demand_empty_rest_of_line ();
1814   else
1815     ignore_rest_of_line ();
1816 }
1817
1818 /* Handle the .end pseudo-op.  Actually, the real work is done in
1819    read_a_source_file.  */
1820
1821 void
1822 s_end (int ignore ATTRIBUTE_UNUSED)
1823 {
1824   if (flag_mri)
1825     {
1826       /* The MRI assembler permits the start symbol to follow .end,
1827          but we don't support that.  */
1828       SKIP_WHITESPACE ();
1829       if (!is_end_of_line[(unsigned char) *input_line_pointer]
1830           && *input_line_pointer != '*'
1831           && *input_line_pointer != '!')
1832         as_warn (_("start address not supported"));
1833     }
1834 }
1835
1836 /* Handle the .err pseudo-op.  */
1837
1838 void
1839 s_err (int ignore ATTRIBUTE_UNUSED)
1840 {
1841   as_bad (_(".err encountered"));
1842   demand_empty_rest_of_line ();
1843 }
1844
1845 /* Handle the .error and .warning pseudo-ops.  */
1846
1847 void
1848 s_errwarn (int err)
1849 {
1850   int len;
1851   /* The purpose for the conditional assignment is not to
1852      internationalize the directive itself, but that we need a
1853      self-contained message, one that can be passed like the
1854      demand_copy_C_string return value, and with no assumption on the
1855      location of the name of the directive within the message.  */
1856   char *msg
1857     = (err ? _(".error directive invoked in source file")
1858        : _(".warning directive invoked in source file"));
1859
1860   if (!is_it_end_of_statement ())
1861     {
1862       if (*input_line_pointer != '\"')
1863         {
1864           as_bad (_("%s argument must be a string"),
1865                   err ? ".error" : ".warning");
1866           ignore_rest_of_line ();
1867           return;
1868         }
1869
1870       msg = demand_copy_C_string (&len);
1871       if (msg == NULL)
1872         return;
1873     }
1874
1875   if (err)
1876     as_bad ("%s", msg);
1877   else
1878     as_warn ("%s", msg);
1879   demand_empty_rest_of_line ();
1880 }
1881
1882 /* Handle the MRI fail pseudo-op.  */
1883
1884 void
1885 s_fail (int ignore ATTRIBUTE_UNUSED)
1886 {
1887   offsetT temp;
1888   char *stop = NULL;
1889   char stopc = 0;
1890
1891   if (flag_mri)
1892     stop = mri_comment_field (&stopc);
1893
1894   temp = get_absolute_expression ();
1895   if (temp >= 500)
1896     as_warn (_(".fail %ld encountered"), (long) temp);
1897   else
1898     as_bad (_(".fail %ld encountered"), (long) temp);
1899
1900   demand_empty_rest_of_line ();
1901
1902   if (flag_mri)
1903     mri_comment_end (stop, stopc);
1904 }
1905
1906 void
1907 s_fill (int ignore ATTRIBUTE_UNUSED)
1908 {
1909   expressionS rep_exp;
1910   long size = 1;
1911   register long fill = 0;
1912   char *p;
1913
1914 #ifdef md_flush_pending_output
1915   md_flush_pending_output ();
1916 #endif
1917
1918   get_known_segmented_expression (&rep_exp);
1919   if (*input_line_pointer == ',')
1920     {
1921       input_line_pointer++;
1922       size = get_absolute_expression ();
1923       if (*input_line_pointer == ',')
1924         {
1925           input_line_pointer++;
1926           fill = get_absolute_expression ();
1927         }
1928     }
1929
1930   /* This is to be compatible with BSD 4.2 AS, not for any rational reason.  */
1931 #define BSD_FILL_SIZE_CROCK_8 (8)
1932   if (size > BSD_FILL_SIZE_CROCK_8)
1933     {
1934       as_warn (_(".fill size clamped to %d"), BSD_FILL_SIZE_CROCK_8);
1935       size = BSD_FILL_SIZE_CROCK_8;
1936     }
1937   if (size < 0)
1938     {
1939       as_warn (_("size negative; .fill ignored"));
1940       size = 0;
1941     }
1942   else if (rep_exp.X_op == O_constant && rep_exp.X_add_number <= 0)
1943     {
1944       if (rep_exp.X_add_number < 0)
1945         as_warn (_("repeat < 0; .fill ignored"));
1946       size = 0;
1947     }
1948
1949   if (size && !need_pass_2)
1950     {
1951       if (rep_exp.X_op == O_constant)
1952         {
1953           p = frag_var (rs_fill, (int) size, (int) size,
1954                         (relax_substateT) 0, (symbolS *) 0,
1955                         (offsetT) rep_exp.X_add_number,
1956                         (char *) 0);
1957         }
1958       else
1959         {
1960           /* We don't have a constant repeat count, so we can't use
1961              rs_fill.  We can get the same results out of rs_space,
1962              but its argument is in bytes, so we must multiply the
1963              repeat count by size.  */
1964
1965           symbolS *rep_sym;
1966           rep_sym = make_expr_symbol (&rep_exp);
1967           if (size != 1)
1968             {
1969               expressionS size_exp;
1970               size_exp.X_op = O_constant;
1971               size_exp.X_add_number = size;
1972
1973               rep_exp.X_op = O_multiply;
1974               rep_exp.X_add_symbol = rep_sym;
1975               rep_exp.X_op_symbol = make_expr_symbol (&size_exp);
1976               rep_exp.X_add_number = 0;
1977               rep_sym = make_expr_symbol (&rep_exp);
1978             }
1979
1980           p = frag_var (rs_space, (int) size, (int) size,
1981                         (relax_substateT) 0, rep_sym, (offsetT) 0, (char *) 0);
1982         }
1983
1984       memset (p, 0, (unsigned int) size);
1985
1986       /* The magic number BSD_FILL_SIZE_CROCK_4 is from BSD 4.2 VAX
1987          flavoured AS.  The following bizarre behaviour is to be
1988          compatible with above.  I guess they tried to take up to 8
1989          bytes from a 4-byte expression and they forgot to sign
1990          extend.  */
1991 #define BSD_FILL_SIZE_CROCK_4 (4)
1992       md_number_to_chars (p, (valueT) fill,
1993                           (size > BSD_FILL_SIZE_CROCK_4
1994                            ? BSD_FILL_SIZE_CROCK_4
1995                            : (int) size));
1996       /* Note: .fill (),0 emits no frag (since we are asked to .fill 0 bytes)
1997          but emits no error message because it seems a legal thing to do.
1998          It is a degenerate case of .fill but could be emitted by a
1999          compiler.  */
2000     }
2001   demand_empty_rest_of_line ();
2002 }
2003
2004 void
2005 s_globl (int ignore ATTRIBUTE_UNUSED)
2006 {
2007   char *name;
2008   int c;
2009   symbolS *symbolP;
2010   char *stop = NULL;
2011   char stopc = 0;
2012
2013   if (flag_mri)
2014     stop = mri_comment_field (&stopc);
2015
2016   do
2017     {
2018       name = input_line_pointer;
2019       c = get_symbol_end ();
2020       symbolP = symbol_find_or_make (name);
2021       S_SET_EXTERNAL (symbolP);
2022
2023       *input_line_pointer = c;
2024       SKIP_WHITESPACE ();
2025       c = *input_line_pointer;
2026       if (c == ',')
2027         {
2028           input_line_pointer++;
2029           SKIP_WHITESPACE ();
2030           if (is_end_of_line[(unsigned char) *input_line_pointer])
2031             c = '\n';
2032         }
2033     }
2034   while (c == ',');
2035
2036   demand_empty_rest_of_line ();
2037
2038   if (flag_mri)
2039     mri_comment_end (stop, stopc);
2040 }
2041
2042 #ifdef OBJ_ELF
2043 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
2044
2045 static inline int
2046 skip_past_char (char ** str, char c)
2047 {
2048   if (**str == c)
2049     {
2050       (*str)++;
2051       return 0;
2052     }
2053   else
2054     return -1;
2055 }
2056 #define skip_past_comma(str) skip_past_char (str, ',')
2057
2058 /* Parse an attribute directive for VENDOR.  */
2059 void
2060 s_vendor_attribute (int vendor)
2061 {
2062   expressionS exp;
2063   int type;
2064   int tag;
2065   unsigned int i = 0;
2066   char *s = NULL;
2067   char saved_char;
2068
2069   expression (& exp);
2070   if (exp.X_op != O_constant)
2071     goto bad;
2072
2073   tag = exp.X_add_number;
2074   type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
2075
2076   if (skip_past_comma (&input_line_pointer) == -1)
2077     goto bad;
2078   if (type & 1)
2079     {
2080       expression (& exp);
2081       if (exp.X_op != O_constant)
2082         {
2083           as_bad (_("expected numeric constant"));
2084           ignore_rest_of_line ();
2085           return;
2086         }
2087       i = exp.X_add_number;
2088     }
2089   if (type == 3
2090       && skip_past_comma (&input_line_pointer) == -1)
2091     {
2092       as_bad (_("expected comma"));
2093       ignore_rest_of_line ();
2094       return;
2095     }
2096   if (type & 2)
2097     {
2098       skip_whitespace(input_line_pointer);
2099       if (*input_line_pointer != '"')
2100         goto bad_string;
2101       input_line_pointer++;
2102       s = input_line_pointer;
2103       while (*input_line_pointer && *input_line_pointer != '"')
2104         input_line_pointer++;
2105       if (*input_line_pointer != '"')
2106         goto bad_string;
2107       saved_char = *input_line_pointer;
2108       *input_line_pointer = 0;
2109     }
2110   else
2111     {
2112       s = NULL;
2113       saved_char = 0;
2114     }
2115
2116   switch (type)
2117     {
2118     case 3:
2119       bfd_elf_add_obj_attr_compat (stdoutput, vendor, i, s);
2120       break;
2121     case 2:
2122       bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
2123       break;
2124     case 1:
2125       bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
2126       break;
2127     default:
2128       abort ();
2129     }
2130
2131   if (s)
2132     {
2133       *input_line_pointer = saved_char;
2134       input_line_pointer++;
2135     }
2136   demand_empty_rest_of_line ();
2137   return;
2138 bad_string:
2139   as_bad (_("bad string constant"));
2140   ignore_rest_of_line ();
2141   return;
2142 bad:
2143   as_bad (_("expected <tag> , <value>"));
2144   ignore_rest_of_line ();
2145 }
2146
2147 /* Parse a .gnu_attribute directive.  */
2148
2149 static void
2150 s_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2151 {
2152   s_vendor_attribute (OBJ_ATTR_GNU);
2153 }
2154 #endif /* OBJ_ELF */
2155
2156 /* Handle the MRI IRP and IRPC pseudo-ops.  */
2157
2158 void
2159 s_irp (int irpc)
2160 {
2161   char *file, *eol;
2162   unsigned int line;
2163   sb s;
2164   const char *err;
2165   sb out;
2166
2167   as_where (&file, &line);
2168
2169   sb_new (&s);
2170   eol = find_end_of_line (input_line_pointer, 0);
2171   sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
2172   input_line_pointer = eol;
2173
2174   sb_new (&out);
2175
2176   err = expand_irp (irpc, 0, &s, &out, get_line_sb);
2177   if (err != NULL)
2178     as_bad_where (file, line, "%s", err);
2179
2180   sb_kill (&s);
2181
2182   input_scrub_include_sb (&out, input_line_pointer, 1);
2183   sb_kill (&out);
2184   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2185 }
2186
2187 /* Handle the .linkonce pseudo-op.  This tells the assembler to mark
2188    the section to only be linked once.  However, this is not supported
2189    by most object file formats.  This takes an optional argument,
2190    which is what to do about duplicates.  */
2191
2192 void
2193 s_linkonce (int ignore ATTRIBUTE_UNUSED)
2194 {
2195   enum linkonce_type type;
2196
2197   SKIP_WHITESPACE ();
2198
2199   type = LINKONCE_DISCARD;
2200
2201   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2202     {
2203       char *s;
2204       char c;
2205
2206       s = input_line_pointer;
2207       c = get_symbol_end ();
2208       if (strcasecmp (s, "discard") == 0)
2209         type = LINKONCE_DISCARD;
2210       else if (strcasecmp (s, "one_only") == 0)
2211         type = LINKONCE_ONE_ONLY;
2212       else if (strcasecmp (s, "same_size") == 0)
2213         type = LINKONCE_SAME_SIZE;
2214       else if (strcasecmp (s, "same_contents") == 0)
2215         type = LINKONCE_SAME_CONTENTS;
2216       else
2217         as_warn (_("unrecognized .linkonce type `%s'"), s);
2218
2219       *input_line_pointer = c;
2220     }
2221
2222 #ifdef obj_handle_link_once
2223   obj_handle_link_once (type);
2224 #else /* ! defined (obj_handle_link_once) */
2225   {
2226     flagword flags;
2227
2228     if ((bfd_applicable_section_flags (stdoutput) & SEC_LINK_ONCE) == 0)
2229       as_warn (_(".linkonce is not supported for this object file format"));
2230
2231     flags = bfd_get_section_flags (stdoutput, now_seg);
2232     flags |= SEC_LINK_ONCE;
2233     switch (type)
2234       {
2235       default:
2236         abort ();
2237       case LINKONCE_DISCARD:
2238         flags |= SEC_LINK_DUPLICATES_DISCARD;
2239         break;
2240       case LINKONCE_ONE_ONLY:
2241         flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
2242         break;
2243       case LINKONCE_SAME_SIZE:
2244         flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
2245         break;
2246       case LINKONCE_SAME_CONTENTS:
2247         flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
2248         break;
2249       }
2250     if (!bfd_set_section_flags (stdoutput, now_seg, flags))
2251       as_bad (_("bfd_set_section_flags: %s"),
2252               bfd_errmsg (bfd_get_error ()));
2253   }
2254 #endif /* ! defined (obj_handle_link_once) */
2255
2256   demand_empty_rest_of_line ();
2257 }
2258
2259 void
2260 bss_alloc (symbolS *symbolP, addressT size, int align)
2261 {
2262   char *pfrag;
2263   segT current_seg = now_seg;
2264   subsegT current_subseg = now_subseg;
2265   segT bss_seg = bss_section;
2266
2267 #if defined (TC_MIPS) || defined (TC_ALPHA)
2268   if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
2269       || OUTPUT_FLAVOR == bfd_target_elf_flavour)
2270     {
2271       /* For MIPS and Alpha ECOFF or ELF, small objects are put in .sbss.  */
2272       if (size <= bfd_get_gp_size (stdoutput))
2273         {
2274           bss_seg = subseg_new (".sbss", 1);
2275           seg_info (bss_seg)->bss = 1;
2276           if (!bfd_set_section_flags (stdoutput, bss_seg, SEC_ALLOC))
2277             as_warn (_("error setting flags for \".sbss\": %s"),
2278                      bfd_errmsg (bfd_get_error ()));
2279         }
2280     }
2281 #endif
2282   subseg_set (bss_seg, 1);
2283
2284   if (align)
2285     {
2286       record_alignment (bss_seg, align);
2287       frag_align (align, 0, 0);
2288     }
2289
2290   /* Detach from old frag.  */
2291   if (S_GET_SEGMENT (symbolP) == bss_seg)
2292     symbol_get_frag (symbolP)->fr_symbol = NULL;
2293
2294   symbol_set_frag (symbolP, frag_now);
2295   pfrag = frag_var (rs_org, 1, 1, 0, symbolP, size, NULL);
2296   *pfrag = 0;
2297
2298 #ifdef S_SET_SIZE
2299   S_SET_SIZE (symbolP, size);
2300 #endif
2301   S_SET_SEGMENT (symbolP, bss_seg);
2302
2303 #ifdef OBJ_COFF
2304   /* The symbol may already have been created with a preceding
2305      ".globl" directive -- be careful not to step on storage class
2306      in that case.  Otherwise, set it to static.  */
2307   if (S_GET_STORAGE_CLASS (symbolP) != C_EXT)
2308     S_SET_STORAGE_CLASS (symbolP, C_STAT);
2309 #endif /* OBJ_COFF */
2310
2311   subseg_set (current_seg, current_subseg);
2312 }
2313
2314 offsetT
2315 parse_align (int align_bytes)
2316 {
2317   expressionS exp;
2318   addressT align;
2319
2320   SKIP_WHITESPACE ();
2321   if (*input_line_pointer != ',')
2322     {
2323     no_align:
2324       as_bad (_("expected alignment after size"));
2325       ignore_rest_of_line ();
2326       return -1;
2327     }
2328
2329   input_line_pointer++;
2330   SKIP_WHITESPACE ();
2331
2332   align = get_absolute_expr (&exp);
2333   if (exp.X_op == O_absent)
2334     goto no_align;
2335
2336   if (!exp.X_unsigned)
2337     {
2338       as_warn (_("alignment negative; 0 assumed"));
2339       align = 0;
2340     }
2341
2342   if (align_bytes && align != 0)
2343     {
2344       /* convert to a power of 2 alignment */
2345       unsigned int alignp2 = 0;
2346       while ((align & 1) == 0)
2347         align >>= 1, ++alignp2;
2348       if (align != 1)
2349         {
2350           as_bad (_("alignment not a power of 2"));
2351           ignore_rest_of_line ();
2352           return -1;
2353         }
2354       align = alignp2;
2355     }
2356   return align;
2357 }
2358
2359 /* Called from s_comm_internal after symbol name and size have been
2360    parsed.  NEEDS_ALIGN is 0 if it was an ".lcomm" (2 args only),
2361    1 if this was a ".bss" directive which has a 3rd argument
2362    (alignment as a power of 2), or 2 if this was a ".bss" directive
2363    with alignment in bytes.  */
2364
2365 symbolS *
2366 s_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
2367 {
2368   addressT align = 0;
2369
2370   if (needs_align)
2371     {
2372       align = parse_align (needs_align - 1);
2373       if (align == (addressT) -1)
2374         return NULL;
2375     }
2376   else
2377     /* Assume some objects may require alignment on some systems.  */
2378     TC_IMPLICIT_LCOMM_ALIGNMENT (size, align);
2379
2380   bss_alloc (symbolP, size, align);
2381   return symbolP;
2382 }
2383
2384 void
2385 s_lcomm (int needs_align)
2386 {
2387   s_comm_internal (needs_align, s_lcomm_internal);
2388 }
2389
2390 void
2391 s_lcomm_bytes (int needs_align)
2392 {
2393   s_comm_internal (needs_align * 2, s_lcomm_internal);
2394 }
2395
2396 void
2397 s_lsym (int ignore ATTRIBUTE_UNUSED)
2398 {
2399   register char *name;
2400   register char c;
2401   register char *p;
2402   expressionS exp;
2403   register symbolS *symbolP;
2404
2405   /* We permit ANY defined expression: BSD4.2 demands constants.  */
2406   name = input_line_pointer;
2407   c = get_symbol_end ();
2408   p = input_line_pointer;
2409   *p = c;
2410
2411   if (name == p)
2412     {
2413       as_bad (_("expected symbol name"));
2414       ignore_rest_of_line ();
2415       return;
2416     }
2417
2418   SKIP_WHITESPACE ();
2419
2420   if (*input_line_pointer != ',')
2421     {
2422       *p = 0;
2423       as_bad (_("expected comma after \"%s\""), name);
2424       *p = c;
2425       ignore_rest_of_line ();
2426       return;
2427     }
2428
2429   input_line_pointer++;
2430   expression_and_evaluate (&exp);
2431
2432   if (exp.X_op != O_constant
2433       && exp.X_op != O_register)
2434     {
2435       as_bad (_("bad expression"));
2436       ignore_rest_of_line ();
2437       return;
2438     }
2439
2440   *p = 0;
2441   symbolP = symbol_find_or_make (name);
2442
2443   if (S_GET_SEGMENT (symbolP) == undefined_section)
2444     {
2445       /* The name might be an undefined .global symbol; be sure to
2446          keep the "external" bit.  */
2447       S_SET_SEGMENT (symbolP,
2448                      (exp.X_op == O_constant
2449                       ? absolute_section
2450                       : reg_section));
2451       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
2452     }
2453   else
2454     {
2455       as_bad (_("symbol `%s' is already defined"), name);
2456     }
2457
2458   *p = c;
2459   demand_empty_rest_of_line ();
2460 }
2461
2462 /* Read a line into an sb.  Returns the character that ended the line
2463    or zero if there are no more lines.  */
2464
2465 static int
2466 get_line_sb (sb *line)
2467 {
2468   char *eol;
2469
2470   if (input_line_pointer[-1] == '\n')
2471     bump_line_counters ();
2472
2473   if (input_line_pointer >= buffer_limit)
2474     {
2475       buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2476       if (buffer_limit == 0)
2477         return 0;
2478     }
2479
2480   eol = find_end_of_line (input_line_pointer, flag_m68k_mri);
2481   sb_add_buffer (line, input_line_pointer, eol - input_line_pointer);
2482   input_line_pointer = eol;
2483
2484   /* Don't skip multiple end-of-line characters, because that breaks support
2485      for the IA-64 stop bit (;;) which looks like two consecutive end-of-line
2486      characters but isn't.  Instead just skip one end of line character and
2487      return the character skipped so that the caller can re-insert it if
2488      necessary.   */
2489   return *input_line_pointer++;
2490 }
2491
2492 /* Define a macro.  This is an interface to macro.c.  */
2493
2494 void
2495 s_macro (int ignore ATTRIBUTE_UNUSED)
2496 {
2497   char *file, *eol;
2498   unsigned int line;
2499   sb s;
2500   const char *err;
2501   const char *name;
2502
2503   as_where (&file, &line);
2504
2505   sb_new (&s);
2506   eol = find_end_of_line (input_line_pointer, 0);
2507   sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
2508   input_line_pointer = eol;
2509
2510   if (line_label != NULL)
2511     {
2512       sb label;
2513
2514       sb_new (&label);
2515       sb_add_string (&label, S_GET_NAME (line_label));
2516       err = define_macro (0, &s, &label, get_line_sb, file, line, &name);
2517       sb_kill (&label);
2518     }
2519   else
2520     err = define_macro (0, &s, NULL, get_line_sb, file, line, &name);
2521   if (err != NULL)
2522     as_bad_where (file, line, err, name);
2523   else
2524     {
2525       if (line_label != NULL)
2526         {
2527           S_SET_SEGMENT (line_label, absolute_section);
2528           S_SET_VALUE (line_label, 0);
2529           symbol_set_frag (line_label, &zero_address_frag);
2530         }
2531
2532       if (((NO_PSEUDO_DOT || flag_m68k_mri)
2533            && hash_find (po_hash, name) != NULL)
2534           || (!flag_m68k_mri
2535               && *name == '.'
2536               && hash_find (po_hash, name + 1) != NULL))
2537         as_warn_where (file,
2538                  line,
2539                  _("attempt to redefine pseudo-op `%s' ignored"),
2540                  name);
2541     }
2542
2543   sb_kill (&s);
2544 }
2545
2546 /* Handle the .mexit pseudo-op, which immediately exits a macro
2547    expansion.  */
2548
2549 void
2550 s_mexit (int ignore ATTRIBUTE_UNUSED)
2551 {
2552   cond_exit_macro (macro_nest);
2553   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2554 }
2555
2556 /* Switch in and out of MRI mode.  */
2557
2558 void
2559 s_mri (int ignore ATTRIBUTE_UNUSED)
2560 {
2561   int on, old_flag;
2562
2563   on = get_absolute_expression ();
2564   old_flag = flag_mri;
2565   if (on != 0)
2566     {
2567       flag_mri = 1;
2568 #ifdef TC_M68K
2569       flag_m68k_mri = 1;
2570 #endif
2571       macro_mri_mode (1);
2572     }
2573   else
2574     {
2575       flag_mri = 0;
2576 #ifdef TC_M68K
2577       flag_m68k_mri = 0;
2578 #endif
2579       macro_mri_mode (0);
2580     }
2581
2582   /* Operator precedence changes in m68k MRI mode, so we need to
2583      update the operator rankings.  */
2584   expr_set_precedence ();
2585
2586 #ifdef MRI_MODE_CHANGE
2587   if (on != old_flag)
2588     MRI_MODE_CHANGE (on);
2589 #endif
2590
2591   demand_empty_rest_of_line ();
2592 }
2593
2594 /* Handle changing the location counter.  */
2595
2596 static void
2597 do_org (segT segment, expressionS *exp, int fill)
2598 {
2599   if (segment != now_seg && segment != absolute_section)
2600     as_bad (_("invalid segment \"%s\""), segment_name (segment));
2601
2602   if (now_seg == absolute_section)
2603     {
2604       if (fill != 0)
2605         as_warn (_("ignoring fill value in absolute section"));
2606       if (exp->X_op != O_constant)
2607         {
2608           as_bad (_("only constant offsets supported in absolute section"));
2609           exp->X_add_number = 0;
2610         }
2611       abs_section_offset = exp->X_add_number;
2612     }
2613   else
2614     {
2615       char *p;
2616       symbolS *sym = exp->X_add_symbol;
2617       offsetT off = exp->X_add_number * OCTETS_PER_BYTE;
2618
2619       if (exp->X_op != O_constant && exp->X_op != O_symbol)
2620         {
2621           /* Handle complex expressions.  */
2622           sym = make_expr_symbol (exp);
2623           off = 0;
2624         }
2625
2626       p = frag_var (rs_org, 1, 1, (relax_substateT) 0, sym, off, (char *) 0);
2627       *p = fill;
2628     }
2629 }
2630
2631 void
2632 s_org (int ignore ATTRIBUTE_UNUSED)
2633 {
2634   register segT segment;
2635   expressionS exp;
2636   register long temp_fill;
2637
2638 #ifdef md_flush_pending_output
2639   md_flush_pending_output ();
2640 #endif
2641
2642   /* The m68k MRI assembler has a different meaning for .org.  It
2643      means to create an absolute section at a given address.  We can't
2644      support that--use a linker script instead.  */
2645   if (flag_m68k_mri)
2646     {
2647       as_bad (_("MRI style ORG pseudo-op not supported"));
2648       ignore_rest_of_line ();
2649       return;
2650     }
2651
2652   /* Don't believe the documentation of BSD 4.2 AS.  There is no such
2653      thing as a sub-segment-relative origin.  Any absolute origin is
2654      given a warning, then assumed to be segment-relative.  Any
2655      segmented origin expression ("foo+42") had better be in the right
2656      segment or the .org is ignored.
2657
2658      BSD 4.2 AS warns if you try to .org backwards. We cannot because
2659      we never know sub-segment sizes when we are reading code.  BSD
2660      will crash trying to emit negative numbers of filler bytes in
2661      certain .orgs. We don't crash, but see as-write for that code.
2662
2663      Don't make frag if need_pass_2==1.  */
2664   segment = get_known_segmented_expression (&exp);
2665   if (*input_line_pointer == ',')
2666     {
2667       input_line_pointer++;
2668       temp_fill = get_absolute_expression ();
2669     }
2670   else
2671     temp_fill = 0;
2672
2673   if (!need_pass_2)
2674     do_org (segment, &exp, temp_fill);
2675
2676   demand_empty_rest_of_line ();
2677 }
2678
2679 /* Handle parsing for the MRI SECT/SECTION pseudo-op.  This should be
2680    called by the obj-format routine which handles section changing
2681    when in MRI mode.  It will create a new section, and return it.  It
2682    will set *TYPE to the section type: one of 'C' (code), 'D' (data),
2683    'M' (mixed), or 'R' (romable).  The flags will be set in the section.  */
2684
2685 void
2686 s_mri_sect (char *type ATTRIBUTE_UNUSED)
2687 {
2688 #ifdef TC_M68K
2689
2690   char *name;
2691   char c;
2692   segT seg;
2693
2694   SKIP_WHITESPACE ();
2695
2696   name = input_line_pointer;
2697   if (!ISDIGIT (*name))
2698     c = get_symbol_end ();
2699   else
2700     {
2701       do
2702         {
2703           ++input_line_pointer;
2704         }
2705       while (ISDIGIT (*input_line_pointer));
2706
2707       c = *input_line_pointer;
2708       *input_line_pointer = '\0';
2709     }
2710
2711   name = xstrdup (name);
2712
2713   *input_line_pointer = c;
2714
2715   seg = subseg_new (name, 0);
2716
2717   if (*input_line_pointer == ',')
2718     {
2719       int align;
2720
2721       ++input_line_pointer;
2722       align = get_absolute_expression ();
2723       record_alignment (seg, align);
2724     }
2725
2726   *type = 'C';
2727   if (*input_line_pointer == ',')
2728     {
2729       c = *++input_line_pointer;
2730       c = TOUPPER (c);
2731       if (c == 'C' || c == 'D' || c == 'M' || c == 'R')
2732         *type = c;
2733       else
2734         as_bad (_("unrecognized section type"));
2735       ++input_line_pointer;
2736
2737       {
2738         flagword flags;
2739
2740         flags = SEC_NO_FLAGS;
2741         if (*type == 'C')
2742           flags = SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE;
2743         else if (*type == 'D' || *type == 'M')
2744           flags = SEC_ALLOC | SEC_LOAD | SEC_DATA;
2745         else if (*type == 'R')
2746           flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY | SEC_ROM;
2747         if (flags != SEC_NO_FLAGS)
2748           {
2749             if (!bfd_set_section_flags (stdoutput, seg, flags))
2750               as_warn (_("error setting flags for \"%s\": %s"),
2751                        bfd_section_name (stdoutput, seg),
2752                        bfd_errmsg (bfd_get_error ()));
2753           }
2754       }
2755     }
2756
2757   /* Ignore the HP type.  */
2758   if (*input_line_pointer == ',')
2759     input_line_pointer += 2;
2760
2761   demand_empty_rest_of_line ();
2762
2763 #else /* ! TC_M68K */
2764 #ifdef TC_I960
2765
2766   char *name;
2767   char c;
2768   segT seg;
2769
2770   SKIP_WHITESPACE ();
2771
2772   name = input_line_pointer;
2773   c = get_symbol_end ();
2774
2775   name = xstrdup (name);
2776
2777   *input_line_pointer = c;
2778
2779   seg = subseg_new (name, 0);
2780
2781   if (*input_line_pointer != ',')
2782     *type = 'C';
2783   else
2784     {
2785       char *sectype;
2786
2787       ++input_line_pointer;
2788       SKIP_WHITESPACE ();
2789       sectype = input_line_pointer;
2790       c = get_symbol_end ();
2791       if (*sectype == '\0')
2792         *type = 'C';
2793       else if (strcasecmp (sectype, "text") == 0)
2794         *type = 'C';
2795       else if (strcasecmp (sectype, "data") == 0)
2796         *type = 'D';
2797       else if (strcasecmp (sectype, "romdata") == 0)
2798         *type = 'R';
2799       else
2800         as_warn (_("unrecognized section type `%s'"), sectype);
2801       *input_line_pointer = c;
2802     }
2803
2804   if (*input_line_pointer == ',')
2805     {
2806       char *seccmd;
2807
2808       ++input_line_pointer;
2809       SKIP_WHITESPACE ();
2810       seccmd = input_line_pointer;
2811       c = get_symbol_end ();
2812       if (strcasecmp (seccmd, "absolute") == 0)
2813         {
2814           as_bad (_("absolute sections are not supported"));
2815           *input_line_pointer = c;
2816           ignore_rest_of_line ();
2817           return;
2818         }
2819       else if (strcasecmp (seccmd, "align") == 0)
2820         {
2821           int align;
2822
2823           *input_line_pointer = c;
2824           align = get_absolute_expression ();
2825           record_alignment (seg, align);
2826         }
2827       else
2828         {
2829           as_warn (_("unrecognized section command `%s'"), seccmd);
2830           *input_line_pointer = c;
2831         }
2832     }
2833
2834   demand_empty_rest_of_line ();
2835
2836 #else /* ! TC_I960 */
2837   /* The MRI assembler seems to use different forms of .sect for
2838      different targets.  */
2839   as_bad ("MRI mode not supported for this target");
2840   ignore_rest_of_line ();
2841 #endif /* ! TC_I960 */
2842 #endif /* ! TC_M68K */
2843 }
2844
2845 /* Handle the .print pseudo-op.  */
2846
2847 void
2848 s_print (int ignore ATTRIBUTE_UNUSED)
2849 {
2850   char *s;
2851   int len;
2852
2853   s = demand_copy_C_string (&len);
2854   if (s != NULL)
2855     printf ("%s\n", s);
2856   demand_empty_rest_of_line ();
2857 }
2858
2859 /* Handle the .purgem pseudo-op.  */
2860
2861 void
2862 s_purgem (int ignore ATTRIBUTE_UNUSED)
2863 {
2864   if (is_it_end_of_statement ())
2865     {
2866       demand_empty_rest_of_line ();
2867       return;
2868     }
2869
2870   do
2871     {
2872       char *name;
2873       char c;
2874
2875       SKIP_WHITESPACE ();
2876       name = input_line_pointer;
2877       c = get_symbol_end ();
2878       delete_macro (name);
2879       *input_line_pointer = c;
2880       SKIP_WHITESPACE ();
2881     }
2882   while (*input_line_pointer++ == ',');
2883
2884   --input_line_pointer;
2885   demand_empty_rest_of_line ();
2886 }
2887
2888 /* Handle the .endm/.endr pseudo-ops.  */
2889
2890 static void
2891 s_bad_end (int endr)
2892 {
2893   as_warn (_(".end%c encountered without preceeding %s"),
2894            endr ? 'r' : 'm',
2895            endr ? ".rept, .irp, or .irpc" : ".macro");
2896   demand_empty_rest_of_line ();
2897 }
2898
2899 /* Handle the .rept pseudo-op.  */
2900
2901 void
2902 s_rept (int ignore ATTRIBUTE_UNUSED)
2903 {
2904   int count;
2905
2906   count = get_absolute_expression ();
2907
2908   do_repeat (count, "REPT", "ENDR");
2909 }
2910
2911 /* This function provides a generic repeat block implementation.   It allows
2912    different directives to be used as the start/end keys.  */
2913
2914 void
2915 do_repeat (int count, const char *start, const char *end)
2916 {
2917   sb one;
2918   sb many;
2919
2920   sb_new (&one);
2921   if (!buffer_and_nest (start, end, &one, get_line_sb))
2922     {
2923       as_bad (_("%s without %s"), start, end);
2924       return;
2925     }
2926
2927   sb_new (&many);
2928   while (count-- > 0)
2929     sb_add_sb (&many, &one);
2930
2931   sb_kill (&one);
2932
2933   input_scrub_include_sb (&many, input_line_pointer, 1);
2934   sb_kill (&many);
2935   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2936 }
2937
2938 /* Skip to end of current repeat loop; EXTRA indicates how many additional
2939    input buffers to skip.  Assumes that conditionals preceding the loop end
2940    are properly nested.
2941
2942    This function makes it easier to implement a premature "break" out of the
2943    loop.  The EXTRA arg accounts for other buffers we might have inserted,
2944    such as line substitutions.  */
2945
2946 void
2947 end_repeat (int extra)
2948 {
2949   cond_exit_macro (macro_nest);
2950   while (extra-- >= 0)
2951     buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2952 }
2953
2954 static void
2955 assign_symbol (char *name, int mode)
2956 {
2957   symbolS *symbolP;
2958
2959   if (name[0] == '.' && name[1] == '\0')
2960     {
2961       /* Turn '. = mumble' into a .org mumble.  */
2962       segT segment;
2963       expressionS exp;
2964
2965       segment = get_known_segmented_expression (&exp);
2966
2967       if (!need_pass_2)
2968         do_org (segment, &exp, 0);
2969
2970       return;
2971     }
2972
2973   if ((symbolP = symbol_find (name)) == NULL
2974       && (symbolP = md_undefined_symbol (name)) == NULL)
2975     {
2976       symbolP = symbol_find_or_make (name);
2977 #ifndef NO_LISTING
2978       /* When doing symbol listings, play games with dummy fragments living
2979          outside the normal fragment chain to record the file and line info
2980          for this symbol.  */
2981       if (listing & LISTING_SYMBOLS)
2982         {
2983           extern struct list_info_struct *listing_tail;
2984           fragS *dummy_frag = (fragS *) xcalloc (1, sizeof (fragS));
2985           dummy_frag->line = listing_tail;
2986           dummy_frag->fr_symbol = symbolP;
2987           symbol_set_frag (symbolP, dummy_frag);
2988         }
2989 #endif
2990 #ifdef OBJ_COFF
2991       /* "set" symbols are local unless otherwise specified.  */
2992       SF_SET_LOCAL (symbolP);
2993 #endif
2994     }
2995
2996   if (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
2997     {
2998       /* Permit register names to be redefined.  */
2999       if ((mode != 0 || !S_IS_VOLATILE (symbolP))
3000           && S_GET_SEGMENT (symbolP) != reg_section)
3001         {
3002           as_bad (_("symbol `%s' is already defined"), name);
3003           symbolP = symbol_clone (symbolP, 0);
3004         }
3005       /* If the symbol is volatile, copy the symbol and replace the
3006          original with the copy, so that previous uses of the symbol will
3007          retain the value of the symbol at the point of use.  */
3008       else if (S_IS_VOLATILE (symbolP))
3009         symbolP = symbol_clone (symbolP, 1);
3010     }
3011
3012   if (mode == 0)
3013     S_SET_VOLATILE (symbolP);
3014   else if (mode < 0)
3015     S_SET_FORWARD_REF (symbolP);
3016
3017   pseudo_set (symbolP);
3018 }
3019
3020 /* Handle the .equ, .equiv, .eqv, and .set directives.  If EQUIV is 1,
3021    then this is .equiv, and it is an error if the symbol is already
3022    defined.  If EQUIV is -1, the symbol additionally is a forward
3023    reference.  */
3024
3025 void
3026 s_set (int equiv)
3027 {
3028   char *name;
3029   char delim;
3030   char *end_name;
3031
3032   /* Especial apologies for the random logic:
3033      this just grew, and could be parsed much more simply!
3034      Dean in haste.  */
3035   name = input_line_pointer;
3036   delim = get_symbol_end ();
3037   end_name = input_line_pointer;
3038   *end_name = delim;
3039
3040   if (name == end_name)
3041     {
3042       as_bad (_("expected symbol name"));
3043       ignore_rest_of_line ();
3044       return;
3045     }
3046
3047   SKIP_WHITESPACE ();
3048
3049   if (*input_line_pointer != ',')
3050     {
3051       *end_name = 0;
3052       as_bad (_("expected comma after \"%s\""), name);
3053       *end_name = delim;
3054       ignore_rest_of_line ();
3055       return;
3056     }
3057
3058   input_line_pointer++;
3059   *end_name = 0;
3060
3061   assign_symbol (name, equiv);
3062   *end_name = delim;
3063
3064   demand_empty_rest_of_line ();
3065 }
3066
3067 void
3068 s_space (int mult)
3069 {
3070   expressionS exp;
3071   expressionS val;
3072   char *p = 0;
3073   char *stop = NULL;
3074   char stopc = 0;
3075   int bytes;
3076
3077 #ifdef md_flush_pending_output
3078   md_flush_pending_output ();
3079 #endif
3080
3081   if (flag_mri)
3082     stop = mri_comment_field (&stopc);
3083
3084   /* In m68k MRI mode, we need to align to a word boundary, unless
3085      this is ds.b.  */
3086   if (flag_m68k_mri && mult > 1)
3087     {
3088       if (now_seg == absolute_section)
3089         {
3090           abs_section_offset += abs_section_offset & 1;
3091           if (line_label != NULL)
3092             S_SET_VALUE (line_label, abs_section_offset);
3093         }
3094       else if (mri_common_symbol != NULL)
3095         {
3096           valueT val;
3097
3098           val = S_GET_VALUE (mri_common_symbol);
3099           if ((val & 1) != 0)
3100             {
3101               S_SET_VALUE (mri_common_symbol, val + 1);
3102               if (line_label != NULL)
3103                 {
3104                   expressionS *symexp;
3105
3106                   symexp = symbol_get_value_expression (line_label);
3107                   know (symexp->X_op == O_symbol);
3108                   know (symexp->X_add_symbol == mri_common_symbol);
3109                   symexp->X_add_number += 1;
3110                 }
3111             }
3112         }
3113       else
3114         {
3115           do_align (1, (char *) NULL, 0, 0);
3116           if (line_label != NULL)
3117             {
3118               symbol_set_frag (line_label, frag_now);
3119               S_SET_VALUE (line_label, frag_now_fix ());
3120             }
3121         }
3122     }
3123
3124   bytes = mult;
3125
3126   expression (&exp);
3127
3128   SKIP_WHITESPACE ();
3129   if (*input_line_pointer == ',')
3130     {
3131       ++input_line_pointer;
3132       expression (&val);
3133     }
3134   else
3135     {
3136       val.X_op = O_constant;
3137       val.X_add_number = 0;
3138     }
3139
3140   if (val.X_op != O_constant
3141       || val.X_add_number < - 0x80
3142       || val.X_add_number > 0xff
3143       || (mult != 0 && mult != 1 && val.X_add_number != 0))
3144     {
3145       resolve_expression (&exp);
3146       if (exp.X_op != O_constant)
3147         as_bad (_("unsupported variable size or fill value"));
3148       else
3149         {
3150           offsetT i;
3151
3152           if (mult == 0)
3153             mult = 1;
3154           bytes = mult * exp.X_add_number;
3155           for (i = 0; i < exp.X_add_number; i++)
3156             emit_expr (&val, mult);
3157         }
3158     }
3159   else
3160     {
3161       if (now_seg == absolute_section || mri_common_symbol != NULL)
3162         resolve_expression (&exp);
3163
3164       if (exp.X_op == O_constant)
3165         {
3166           long repeat;
3167
3168           repeat = exp.X_add_number;
3169           if (mult)
3170             repeat *= mult;
3171           bytes = repeat;
3172           if (repeat <= 0)
3173             {
3174               if (!flag_mri)
3175                 as_warn (_(".space repeat count is zero, ignored"));
3176               else if (repeat < 0)
3177                 as_warn (_(".space repeat count is negative, ignored"));
3178               goto getout;
3179             }
3180
3181           /* If we are in the absolute section, just bump the offset.  */
3182           if (now_seg == absolute_section)
3183             {
3184               abs_section_offset += repeat;
3185               goto getout;
3186             }
3187
3188           /* If we are secretly in an MRI common section, then
3189              creating space just increases the size of the common
3190              symbol.  */
3191           if (mri_common_symbol != NULL)
3192             {
3193               S_SET_VALUE (mri_common_symbol,
3194                            S_GET_VALUE (mri_common_symbol) + repeat);
3195               goto getout;
3196             }
3197
3198           if (!need_pass_2)
3199             p = frag_var (rs_fill, 1, 1, (relax_substateT) 0, (symbolS *) 0,
3200                           (offsetT) repeat, (char *) 0);
3201         }
3202       else
3203         {
3204           if (now_seg == absolute_section)
3205             {
3206               as_bad (_("space allocation too complex in absolute section"));
3207               subseg_set (text_section, 0);
3208             }
3209
3210           if (mri_common_symbol != NULL)
3211             {
3212               as_bad (_("space allocation too complex in common section"));
3213               mri_common_symbol = NULL;
3214             }
3215
3216           if (!need_pass_2)
3217             p = frag_var (rs_space, 1, 1, (relax_substateT) 0,
3218                           make_expr_symbol (&exp), (offsetT) 0, (char *) 0);
3219         }
3220
3221       if (p)
3222         *p = val.X_add_number;
3223     }
3224
3225  getout:
3226
3227   /* In MRI mode, after an odd number of bytes, we must align to an
3228      even word boundary, unless the next instruction is a dc.b, ds.b
3229      or dcb.b.  */
3230   if (flag_mri && (bytes & 1) != 0)
3231     mri_pending_align = 1;
3232
3233   demand_empty_rest_of_line ();
3234
3235   if (flag_mri)
3236     mri_comment_end (stop, stopc);
3237 }
3238
3239 /* This is like s_space, but the value is a floating point number with
3240    the given precision.  This is for the MRI dcb.s pseudo-op and
3241    friends.  */
3242
3243 void
3244 s_float_space (int float_type)
3245 {
3246   offsetT count;
3247   int flen;
3248   char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT];
3249   char *stop = NULL;
3250   char stopc = 0;
3251
3252   if (flag_mri)
3253     stop = mri_comment_field (&stopc);
3254
3255   count = get_absolute_expression ();
3256
3257   SKIP_WHITESPACE ();
3258   if (*input_line_pointer != ',')
3259     {
3260       as_bad (_("missing value"));
3261       ignore_rest_of_line ();
3262       if (flag_mri)
3263         mri_comment_end (stop, stopc);
3264       return;
3265     }
3266
3267   ++input_line_pointer;
3268
3269   SKIP_WHITESPACE ();
3270
3271   /* Skip any 0{letter} that may be present.  Don't even check if the
3272    * letter is legal.  */
3273   if (input_line_pointer[0] == '0'
3274       && ISALPHA (input_line_pointer[1]))
3275     input_line_pointer += 2;
3276
3277   /* Accept :xxxx, where the x's are hex digits, for a floating point
3278      with the exact digits specified.  */
3279   if (input_line_pointer[0] == ':')
3280     {
3281       flen = hex_float (float_type, temp);
3282       if (flen < 0)
3283         {
3284           ignore_rest_of_line ();
3285           if (flag_mri)
3286             mri_comment_end (stop, stopc);
3287           return;
3288         }
3289     }
3290   else
3291     {
3292       char *err;
3293
3294       err = md_atof (float_type, temp, &flen);
3295       know (flen <= MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT);
3296       know (flen > 0);
3297       if (err)
3298         {
3299           as_bad (_("bad floating literal: %s"), err);
3300           ignore_rest_of_line ();
3301           if (flag_mri)
3302             mri_comment_end (stop, stopc);
3303           return;
3304         }
3305     }
3306
3307   while (--count >= 0)
3308     {
3309       char *p;
3310
3311       p = frag_more (flen);
3312       memcpy (p, temp, (unsigned int) flen);
3313     }
3314
3315   demand_empty_rest_of_line ();
3316
3317   if (flag_mri)
3318     mri_comment_end (stop, stopc);
3319 }
3320
3321 /* Handle the .struct pseudo-op, as found in MIPS assemblers.  */
3322
3323 void
3324 s_struct (int ignore ATTRIBUTE_UNUSED)
3325 {
3326   char *stop = NULL;
3327   char stopc = 0;
3328
3329   if (flag_mri)
3330     stop = mri_comment_field (&stopc);
3331   abs_section_offset = get_absolute_expression ();
3332 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3333   /* The ELF backend needs to know that we are changing sections, so
3334      that .previous works correctly. */
3335   if (IS_ELF)
3336     obj_elf_section_change_hook ();
3337 #endif
3338   subseg_set (absolute_section, 0);
3339   demand_empty_rest_of_line ();
3340   if (flag_mri)
3341     mri_comment_end (stop, stopc);
3342 }
3343
3344 void
3345 s_text (int ignore ATTRIBUTE_UNUSED)
3346 {
3347   register int temp;
3348
3349   temp = get_absolute_expression ();
3350   subseg_set (text_section, (subsegT) temp);
3351   demand_empty_rest_of_line ();
3352 #ifdef OBJ_VMS
3353   const_flag &= ~IN_DEFAULT_SECTION;
3354 #endif
3355 }
3356
3357 /* .weakref x, y sets x as an alias to y that, as long as y is not
3358    referenced directly, will cause y to become a weak symbol.  */
3359 void
3360 s_weakref (int ignore ATTRIBUTE_UNUSED)
3361 {
3362   char *name;
3363   char delim;
3364   char *end_name;
3365   symbolS *symbolP;
3366   symbolS *symbolP2;
3367   expressionS exp;
3368
3369   name = input_line_pointer;
3370   delim = get_symbol_end ();
3371   end_name = input_line_pointer;
3372
3373   if (name == end_name)
3374     {
3375       as_bad (_("expected symbol name"));
3376       *end_name = delim;
3377       ignore_rest_of_line ();
3378       return;
3379     }
3380
3381   symbolP = symbol_find_or_make (name);
3382
3383   if (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
3384     {
3385       if (!S_IS_VOLATILE (symbolP))
3386         {
3387           as_bad (_("symbol `%s' is already defined"), name);
3388           *end_name = delim;
3389           ignore_rest_of_line ();
3390           return;
3391         }
3392       symbolP = symbol_clone (symbolP, 1);
3393       S_CLEAR_VOLATILE (symbolP);
3394     }
3395
3396   *end_name = delim;
3397
3398   SKIP_WHITESPACE ();
3399
3400   if (*input_line_pointer != ',')
3401     {
3402       *end_name = 0;
3403       as_bad (_("expected comma after \"%s\""), name);
3404       *end_name = delim;
3405       ignore_rest_of_line ();
3406       return;
3407     }
3408
3409   input_line_pointer++;
3410
3411   SKIP_WHITESPACE ();
3412
3413   name = input_line_pointer;
3414   delim = get_symbol_end ();
3415   end_name = input_line_pointer;
3416
3417   if (name == end_name)
3418     {
3419       as_bad (_("expected symbol name"));
3420       ignore_rest_of_line ();
3421       return;
3422     }
3423
3424   if ((symbolP2 = symbol_find_noref (name, 1)) == NULL
3425       && (symbolP2 = md_undefined_symbol (name)) == NULL)
3426     {
3427       symbolP2 = symbol_find_or_make (name);
3428       S_SET_WEAKREFD (symbolP2);
3429     }
3430   else
3431     {
3432       symbolS *symp = symbolP2;
3433
3434       while (S_IS_WEAKREFR (symp) && symp != symbolP)
3435         {
3436           expressionS *expP = symbol_get_value_expression (symp);
3437
3438           assert (expP->X_op == O_symbol
3439                   && expP->X_add_number == 0);
3440           symp = expP->X_add_symbol;
3441         }
3442       if (symp == symbolP)
3443         {
3444           char *loop;
3445
3446           loop = concat (S_GET_NAME (symbolP),
3447                          " => ", S_GET_NAME (symbolP2), NULL);
3448
3449           symp = symbolP2;
3450           while (symp != symbolP)
3451             {
3452               char *old_loop = loop;
3453               symp = symbol_get_value_expression (symp)->X_add_symbol;
3454               loop = concat (loop, " => ", S_GET_NAME (symp), NULL);
3455               free (old_loop);
3456             }
3457
3458           as_bad (_("%s: would close weakref loop: %s"),
3459                   S_GET_NAME (symbolP), loop);
3460
3461           free (loop);
3462
3463           *end_name = delim;
3464           ignore_rest_of_line ();
3465           return;
3466         }
3467
3468       /* Short-circuiting instead of just checking here might speed
3469          things up a tiny little bit, but loop error messages would
3470          miss intermediate links.  */
3471       /* symbolP2 = symp; */
3472     }
3473
3474   *end_name = delim;
3475
3476   memset (&exp, 0, sizeof (exp));
3477   exp.X_op = O_symbol;
3478   exp.X_add_symbol = symbolP2;
3479
3480   S_SET_SEGMENT (symbolP, undefined_section);
3481   symbol_set_value_expression (symbolP, &exp);
3482   symbol_set_frag (symbolP, &zero_address_frag);
3483   S_SET_WEAKREFR (symbolP);
3484
3485   demand_empty_rest_of_line ();
3486 }
3487 \f
3488
3489 /* Verify that we are at the end of a line.  If not, issue an error and
3490    skip to EOL.  */
3491
3492 void
3493 demand_empty_rest_of_line (void)
3494 {
3495   SKIP_WHITESPACE ();
3496   if (is_end_of_line[(unsigned char) *input_line_pointer])
3497     input_line_pointer++;
3498   else
3499     {
3500       if (ISPRINT (*input_line_pointer))
3501         as_bad (_("junk at end of line, first unrecognized character is `%c'"),
3502                  *input_line_pointer);
3503       else
3504         as_bad (_("junk at end of line, first unrecognized character valued 0x%x"),
3505                  *input_line_pointer);
3506       ignore_rest_of_line ();
3507     }
3508   
3509   /* Return pointing just after end-of-line.  */
3510   know (is_end_of_line[(unsigned char) input_line_pointer[-1]]);
3511 }
3512
3513 /* Silently advance to the end of line.  Use this after already having
3514    issued an error about something bad.  */
3515
3516 void
3517 ignore_rest_of_line (void)
3518 {
3519   while (input_line_pointer < buffer_limit
3520          && !is_end_of_line[(unsigned char) *input_line_pointer])
3521     input_line_pointer++;
3522
3523   input_line_pointer++;
3524
3525   /* Return pointing just after end-of-line.  */
3526   know (is_end_of_line[(unsigned char) input_line_pointer[-1]]);
3527 }
3528
3529 /* Sets frag for given symbol to zero_address_frag, except when the
3530    symbol frag is already set to a dummy listing frag.  */
3531
3532 static void
3533 set_zero_frag (symbolS *symbolP)
3534 {
3535   if (symbol_get_frag (symbolP)->fr_type != rs_dummy)
3536     symbol_set_frag (symbolP, &zero_address_frag);
3537 }
3538
3539 /* In:  Pointer to a symbol.
3540         Input_line_pointer->expression.
3541
3542    Out: Input_line_pointer->just after any whitespace after expression.
3543         Tried to set symbol to value of expression.
3544         Will change symbols type, value, and frag;  */
3545
3546 void
3547 pseudo_set (symbolS *symbolP)
3548 {
3549   expressionS exp;
3550   segT seg;
3551
3552   know (symbolP);               /* NULL pointer is logic error.  */
3553
3554   if (!S_IS_FORWARD_REF (symbolP))
3555     (void) expression (&exp);
3556   else
3557     (void) deferred_expression (&exp);
3558
3559   if (exp.X_op == O_illegal)
3560     as_bad (_("illegal expression"));
3561   else if (exp.X_op == O_absent)
3562     as_bad (_("missing expression"));
3563   else if (exp.X_op == O_big)
3564     {
3565       if (exp.X_add_number > 0)
3566         as_bad (_("bignum invalid"));
3567       else
3568         as_bad (_("floating point number invalid"));
3569     }
3570   else if (exp.X_op == O_subtract
3571            && !S_IS_FORWARD_REF (symbolP)
3572            && SEG_NORMAL (S_GET_SEGMENT (exp.X_add_symbol))
3573            && (symbol_get_frag (exp.X_add_symbol)
3574                == symbol_get_frag (exp.X_op_symbol)))
3575     {
3576       exp.X_op = O_constant;
3577       exp.X_add_number = (S_GET_VALUE (exp.X_add_symbol)
3578                           - S_GET_VALUE (exp.X_op_symbol));
3579     }
3580
3581   if (symbol_section_p (symbolP))
3582     {
3583       as_bad ("attempt to set value of section symbol");
3584       return;
3585     }
3586
3587   switch (exp.X_op)
3588     {
3589     case O_illegal:
3590     case O_absent:
3591     case O_big:
3592       exp.X_add_number = 0;
3593       /* Fall through.  */
3594     case O_constant:
3595       S_SET_SEGMENT (symbolP, absolute_section);
3596       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
3597       set_zero_frag (symbolP);
3598       break;
3599
3600     case O_register:
3601       S_SET_SEGMENT (symbolP, reg_section);
3602       S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
3603       set_zero_frag (symbolP);
3604       symbol_get_value_expression (symbolP)->X_op = O_register;
3605       break;
3606
3607     case O_symbol:
3608       seg = S_GET_SEGMENT (exp.X_add_symbol);
3609       /* For x=undef+const, create an expression symbol.
3610          For x=x+const, just update x except when x is an undefined symbol
3611          For x=defined+const, evaluate x.  */
3612       if (symbolP == exp.X_add_symbol
3613           && (seg != undefined_section
3614               || !symbol_constant_p (symbolP)))
3615         {
3616           *symbol_X_add_number (symbolP) += exp.X_add_number;
3617           break;
3618         }
3619       else if (!S_IS_FORWARD_REF (symbolP) && seg != undefined_section)
3620         {
3621           symbolS *s = exp.X_add_symbol;
3622
3623           if (S_IS_COMMON (s))
3624             as_bad (_("`%s' can't be equated to common symbol '%s'"),
3625                     S_GET_NAME (symbolP), S_GET_NAME (s));
3626
3627           S_SET_SEGMENT (symbolP, seg);
3628           S_SET_VALUE (symbolP, exp.X_add_number + S_GET_VALUE (s));
3629           symbol_set_frag (symbolP, symbol_get_frag (s));
3630           copy_symbol_attributes (symbolP, s);
3631           break;
3632         }
3633       S_SET_SEGMENT (symbolP, undefined_section);
3634       symbol_set_value_expression (symbolP, &exp);
3635       set_zero_frag (symbolP);
3636       break;
3637
3638     default:
3639       /* The value is some complex expression.  */
3640       S_SET_SEGMENT (symbolP, expr_section);
3641       symbol_set_value_expression (symbolP, &exp);
3642       set_zero_frag (symbolP);
3643       break;
3644     }
3645 }
3646 \f
3647 /*                      cons()
3648
3649    CONStruct more frag of .bytes, or .words etc.
3650    Should need_pass_2 be 1 then emit no frag(s).
3651    This understands EXPRESSIONS.
3652
3653    Bug (?)
3654
3655    This has a split personality. We use expression() to read the
3656    value. We can detect if the value won't fit in a byte or word.
3657    But we can't detect if expression() discarded significant digits
3658    in the case of a long. Not worth the crocks required to fix it.  */
3659
3660 /* Select a parser for cons expressions.  */
3661
3662 /* Some targets need to parse the expression in various fancy ways.
3663    You can define TC_PARSE_CONS_EXPRESSION to do whatever you like
3664    (for example, the HPPA does this).  Otherwise, you can define
3665    BITFIELD_CONS_EXPRESSIONS to permit bitfields to be specified, or
3666    REPEAT_CONS_EXPRESSIONS to permit repeat counts.  If none of these
3667    are defined, which is the normal case, then only simple expressions
3668    are permitted.  */
3669
3670 #ifdef TC_M68K
3671 static void
3672 parse_mri_cons (expressionS *exp, unsigned int nbytes);
3673 #endif
3674
3675 #ifndef TC_PARSE_CONS_EXPRESSION
3676 #ifdef BITFIELD_CONS_EXPRESSIONS
3677 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) parse_bitfield_cons (EXP, NBYTES)
3678 static void
3679 parse_bitfield_cons (expressionS *exp, unsigned int nbytes);
3680 #endif
3681 #ifdef REPEAT_CONS_EXPRESSIONS
3682 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) parse_repeat_cons (EXP, NBYTES)
3683 static void
3684 parse_repeat_cons (expressionS *exp, unsigned int nbytes);
3685 #endif
3686
3687 /* If we haven't gotten one yet, just call expression.  */
3688 #ifndef TC_PARSE_CONS_EXPRESSION
3689 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) expression (EXP)
3690 #endif
3691 #endif
3692
3693 void
3694 do_parse_cons_expression (expressionS *exp,
3695                           int nbytes ATTRIBUTE_UNUSED)
3696 {
3697   TC_PARSE_CONS_EXPRESSION (exp, nbytes);
3698 }
3699
3700
3701 /* Worker to do .byte etc statements.
3702    Clobbers input_line_pointer and checks end-of-line.  */
3703
3704 static void
3705 cons_worker (register int nbytes,       /* 1=.byte, 2=.word, 4=.long.  */
3706              int rva)
3707 {
3708   int c;
3709   expressionS exp;
3710   char *stop = NULL;
3711   char stopc = 0;
3712
3713 #ifdef md_flush_pending_output
3714   md_flush_pending_output ();
3715 #endif
3716
3717   if (flag_mri)
3718     stop = mri_comment_field (&stopc);
3719
3720   if (is_it_end_of_statement ())
3721     {
3722       demand_empty_rest_of_line ();
3723       if (flag_mri)
3724         mri_comment_end (stop, stopc);
3725       return;
3726     }
3727
3728 #ifdef TC_ADDRESS_BYTES
3729   if (nbytes == 0)
3730     nbytes = TC_ADDRESS_BYTES ();
3731 #endif
3732
3733 #ifdef md_cons_align
3734   md_cons_align (nbytes);
3735 #endif
3736
3737   c = 0;
3738   do
3739     {
3740 #ifdef TC_M68K
3741       if (flag_m68k_mri)
3742         parse_mri_cons (&exp, (unsigned int) nbytes);
3743       else
3744 #endif
3745         TC_PARSE_CONS_EXPRESSION (&exp, (unsigned int) nbytes);
3746
3747       if (rva)
3748         {
3749           if (exp.X_op == O_symbol)
3750             exp.X_op = O_symbol_rva;
3751           else
3752             as_fatal (_("rva without symbol"));
3753         }
3754       emit_expr (&exp, (unsigned int) nbytes);
3755       ++c;
3756     }
3757   while (*input_line_pointer++ == ',');
3758
3759   /* In MRI mode, after an odd number of bytes, we must align to an
3760      even word boundary, unless the next instruction is a dc.b, ds.b
3761      or dcb.b.  */
3762   if (flag_mri && nbytes == 1 && (c & 1) != 0)
3763     mri_pending_align = 1;
3764
3765   input_line_pointer--;         /* Put terminator back into stream.  */
3766
3767   demand_empty_rest_of_line ();
3768
3769   if (flag_mri)
3770     mri_comment_end (stop, stopc);
3771 }
3772
3773 void
3774 cons (int size)
3775 {
3776   cons_worker (size, 0);
3777 }
3778
3779 void
3780 s_rva (int size)
3781 {
3782   cons_worker (size, 1);
3783 }
3784
3785 /* .reloc offset, reloc_name, symbol+addend.  */
3786
3787 void
3788 s_reloc (int ignore ATTRIBUTE_UNUSED)
3789 {
3790   char *stop = NULL;
3791   char stopc = 0;
3792   expressionS exp;
3793   char *r_name;
3794   int c;
3795   struct reloc_list *reloc;
3796
3797   reloc = xmalloc (sizeof (*reloc));
3798
3799   if (flag_mri)
3800     stop = mri_comment_field (&stopc);
3801
3802   expression (&exp);
3803   switch (exp.X_op)
3804     {
3805     case O_illegal:
3806     case O_absent:
3807     case O_big:
3808     case O_register:
3809       as_bad (_("missing or bad offset expression"));
3810       goto err_out;
3811     case O_constant:
3812       exp.X_add_symbol = section_symbol (now_seg);
3813       exp.X_op = O_symbol;
3814       /* Fall thru */
3815     case O_symbol:
3816       if (exp.X_add_number == 0)
3817         {
3818           reloc->u.a.offset_sym = exp.X_add_symbol;
3819           break;
3820         }
3821       /* Fall thru */
3822     default:
3823       reloc->u.a.offset_sym = make_expr_symbol (&exp);
3824       break;
3825     }
3826
3827   SKIP_WHITESPACE ();
3828   if (*input_line_pointer != ',')
3829     {
3830       as_bad (_("missing reloc type"));
3831       goto err_out;
3832     }
3833
3834   ++input_line_pointer;
3835   SKIP_WHITESPACE ();
3836   r_name = input_line_pointer;
3837   c = get_symbol_end ();
3838   reloc->u.a.howto = bfd_reloc_name_lookup (stdoutput, r_name);
3839   *input_line_pointer = c;
3840   if (reloc->u.a.howto == NULL)
3841     {
3842       as_bad (_("unrecognized reloc type"));
3843       goto err_out;
3844     }
3845
3846   exp.X_op = O_absent;
3847   SKIP_WHITESPACE ();
3848   if (*input_line_pointer == ',')
3849     {
3850       ++input_line_pointer;
3851       expression_and_evaluate (&exp);
3852     }
3853   switch (exp.X_op)
3854     {
3855     case O_illegal:
3856     case O_big:
3857     case O_register:
3858       as_bad (_("bad reloc expression"));
3859     err_out:
3860       ignore_rest_of_line ();
3861       free (reloc);
3862       if (flag_mri)
3863         mri_comment_end (stop, stopc);
3864       return;
3865     case O_absent:
3866       reloc->u.a.sym = NULL;
3867       reloc->u.a.addend = 0;
3868       break;
3869     case O_constant:
3870       reloc->u.a.sym = NULL;
3871       reloc->u.a.addend = exp.X_add_number;
3872       break;
3873     case O_symbol:
3874       reloc->u.a.sym = exp.X_add_symbol;
3875       reloc->u.a.addend = exp.X_add_number;
3876       break;
3877     default:
3878       reloc->u.a.sym = make_expr_symbol (&exp);
3879       reloc->u.a.addend = 0;
3880       break;
3881     }
3882
3883   as_where (&reloc->file, &reloc->line);
3884   reloc->next = reloc_list;
3885   reloc_list = reloc;
3886
3887   demand_empty_rest_of_line ();
3888   if (flag_mri)
3889     mri_comment_end (stop, stopc);
3890 }
3891
3892 /* Put the contents of expression EXP into the object file using
3893    NBYTES bytes.  If need_pass_2 is 1, this does nothing.  */
3894
3895 void
3896 emit_expr (expressionS *exp, unsigned int nbytes)
3897 {
3898   operatorT op;
3899   register char *p;
3900   valueT extra_digit = 0;
3901
3902   /* Don't do anything if we are going to make another pass.  */
3903   if (need_pass_2)
3904     return;
3905
3906   dot_value = frag_now_fix ();
3907
3908 #ifndef NO_LISTING
3909 #ifdef OBJ_ELF
3910   /* When gcc emits DWARF 1 debugging pseudo-ops, a line number will
3911      appear as a four byte positive constant in the .line section,
3912      followed by a 2 byte 0xffff.  Look for that case here.  */
3913   {
3914     static int dwarf_line = -1;
3915
3916     if (strcmp (segment_name (now_seg), ".line") != 0)
3917       dwarf_line = -1;
3918     else if (dwarf_line >= 0
3919              && nbytes == 2
3920              && exp->X_op == O_constant
3921              && (exp->X_add_number == -1 || exp->X_add_number == 0xffff))
3922       listing_source_line ((unsigned int) dwarf_line);
3923     else if (nbytes == 4
3924              && exp->X_op == O_constant
3925              && exp->X_add_number >= 0)
3926       dwarf_line = exp->X_add_number;
3927     else
3928       dwarf_line = -1;
3929   }
3930
3931   /* When gcc emits DWARF 1 debugging pseudo-ops, a file name will
3932      appear as a 2 byte TAG_compile_unit (0x11) followed by a 2 byte
3933      AT_sibling (0x12) followed by a four byte address of the sibling
3934      followed by a 2 byte AT_name (0x38) followed by the name of the
3935      file.  We look for that case here.  */
3936   {
3937     static int dwarf_file = 0;
3938
3939     if (strcmp (segment_name (now_seg), ".debug") != 0)
3940       dwarf_file = 0;
3941     else if (dwarf_file == 0
3942              && nbytes == 2
3943              && exp->X_op == O_constant
3944              && exp->X_add_number == 0x11)
3945       dwarf_file = 1;
3946     else if (dwarf_file == 1
3947              && nbytes == 2
3948              && exp->X_op == O_constant
3949              && exp->X_add_number == 0x12)
3950       dwarf_file = 2;
3951     else if (dwarf_file == 2
3952              && nbytes == 4)
3953       dwarf_file = 3;
3954     else if (dwarf_file == 3
3955              && nbytes == 2
3956              && exp->X_op == O_constant
3957              && exp->X_add_number == 0x38)
3958       dwarf_file = 4;
3959     else
3960       dwarf_file = 0;
3961
3962     /* The variable dwarf_file_string tells stringer that the string
3963        may be the name of the source file.  */
3964     if (dwarf_file == 4)
3965       dwarf_file_string = 1;
3966     else
3967       dwarf_file_string = 0;
3968   }
3969 #endif
3970 #endif
3971
3972   if (check_eh_frame (exp, &nbytes))
3973     return;
3974
3975   op = exp->X_op;
3976
3977   /* Allow `.word 0' in the absolute section.  */
3978   if (now_seg == absolute_section)
3979     {
3980       if (op != O_constant || exp->X_add_number != 0)
3981         as_bad (_("attempt to store value in absolute section"));
3982       abs_section_offset += nbytes;
3983       return;
3984     }
3985
3986   /* Handle a negative bignum.  */
3987   if (op == O_uminus
3988       && exp->X_add_number == 0
3989       && symbol_get_value_expression (exp->X_add_symbol)->X_op == O_big
3990       && symbol_get_value_expression (exp->X_add_symbol)->X_add_number > 0)
3991     {
3992       int i;
3993       unsigned long carry;
3994
3995       exp = symbol_get_value_expression (exp->X_add_symbol);
3996
3997       /* Negate the bignum: one's complement each digit and add 1.  */
3998       carry = 1;
3999       for (i = 0; i < exp->X_add_number; i++)
4000         {
4001           unsigned long next;
4002
4003           next = (((~(generic_bignum[i] & LITTLENUM_MASK))
4004                    & LITTLENUM_MASK)
4005                   + carry);
4006           generic_bignum[i] = next & LITTLENUM_MASK;
4007           carry = next >> LITTLENUM_NUMBER_OF_BITS;
4008         }
4009
4010       /* We can ignore any carry out, because it will be handled by
4011          extra_digit if it is needed.  */
4012
4013       extra_digit = (valueT) -1;
4014       op = O_big;
4015     }
4016
4017   if (op == O_absent || op == O_illegal)
4018     {
4019       as_warn (_("zero assumed for missing expression"));
4020       exp->X_add_number = 0;
4021       op = O_constant;
4022     }
4023   else if (op == O_big && exp->X_add_number <= 0)
4024     {
4025       as_bad (_("floating point number invalid"));
4026       exp->X_add_number = 0;
4027       op = O_constant;
4028     }
4029   else if (op == O_register)
4030     {
4031       as_warn (_("register value used as expression"));
4032       op = O_constant;
4033     }
4034
4035   p = frag_more ((int) nbytes);
4036
4037 #ifndef WORKING_DOT_WORD
4038   /* If we have the difference of two symbols in a word, save it on
4039      the broken_words list.  See the code in write.c.  */
4040   if (op == O_subtract && nbytes == 2)
4041     {
4042       struct broken_word *x;
4043
4044       x = (struct broken_word *) xmalloc (sizeof (struct broken_word));
4045       x->next_broken_word = broken_words;
4046       broken_words = x;
4047       x->seg = now_seg;
4048       x->subseg = now_subseg;
4049       x->frag = frag_now;
4050       x->word_goes_here = p;
4051       x->dispfrag = 0;
4052       x->add = exp->X_add_symbol;
4053       x->sub = exp->X_op_symbol;
4054       x->addnum = exp->X_add_number;
4055       x->added = 0;
4056       x->use_jump = 0;
4057       new_broken_words++;
4058       return;
4059     }
4060 #endif
4061
4062   /* If we have an integer, but the number of bytes is too large to
4063      pass to md_number_to_chars, handle it as a bignum.  */
4064   if (op == O_constant && nbytes > sizeof (valueT))
4065     {
4066       extra_digit = exp->X_unsigned ? 0 : -1;
4067       convert_to_bignum (exp);
4068       op = O_big;
4069     }
4070
4071   if (op == O_constant)
4072     {
4073       register valueT get;
4074       register valueT use;
4075       register valueT mask;
4076       valueT hibit;
4077       register valueT unmask;
4078
4079       /* JF << of >= number of bits in the object is undefined.  In
4080          particular SPARC (Sun 4) has problems.  */
4081       if (nbytes >= sizeof (valueT))
4082         {
4083           mask = 0;
4084           if (nbytes > sizeof (valueT))
4085             hibit = 0;
4086           else
4087             hibit = (valueT) 1 << (nbytes * BITS_PER_CHAR - 1);
4088         }
4089       else
4090         {
4091           /* Don't store these bits.  */
4092           mask = ~(valueT) 0 << (BITS_PER_CHAR * nbytes);
4093           hibit = (valueT) 1 << (nbytes * BITS_PER_CHAR - 1);
4094         }
4095
4096       unmask = ~mask;           /* Do store these bits.  */
4097
4098 #ifdef NEVER
4099       "Do this mod if you want every overflow check to assume SIGNED 2's complement data.";
4100       mask = ~(unmask >> 1);    /* Includes sign bit now.  */
4101 #endif
4102
4103       get = exp->X_add_number;
4104       use = get & unmask;
4105       if ((get & mask) != 0
4106           && ((get & mask) != mask
4107               || (get & hibit) == 0))
4108         {               /* Leading bits contain both 0s & 1s.  */
4109           as_warn (_("value 0x%lx truncated to 0x%lx"),
4110                    (unsigned long) get, (unsigned long) use);
4111         }
4112       /* Put bytes in right order.  */
4113       md_number_to_chars (p, use, (int) nbytes);
4114     }
4115   else if (op == O_big)
4116     {
4117       unsigned int size;
4118       LITTLENUM_TYPE *nums;
4119
4120       know (nbytes % CHARS_PER_LITTLENUM == 0);
4121
4122       size = exp->X_add_number * CHARS_PER_LITTLENUM;
4123       if (nbytes < size)
4124         {
4125           as_warn (_("bignum truncated to %d bytes"), nbytes);
4126           size = nbytes;
4127         }
4128
4129       if (target_big_endian)
4130         {
4131           while (nbytes > size)
4132             {
4133               md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
4134               nbytes -= CHARS_PER_LITTLENUM;
4135               p += CHARS_PER_LITTLENUM;
4136             }
4137
4138           nums = generic_bignum + size / CHARS_PER_LITTLENUM;
4139           while (size >= CHARS_PER_LITTLENUM)
4140             {
4141               --nums;
4142               md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
4143               size -= CHARS_PER_LITTLENUM;
4144               p += CHARS_PER_LITTLENUM;
4145             }
4146         }
4147       else
4148         {
4149           nums = generic_bignum;
4150           while (size >= CHARS_PER_LITTLENUM)
4151             {
4152               md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
4153               ++nums;
4154               size -= CHARS_PER_LITTLENUM;
4155               p += CHARS_PER_LITTLENUM;
4156               nbytes -= CHARS_PER_LITTLENUM;
4157             }
4158
4159           while (nbytes >= CHARS_PER_LITTLENUM)
4160             {
4161               md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
4162               nbytes -= CHARS_PER_LITTLENUM;
4163               p += CHARS_PER_LITTLENUM;
4164             }
4165         }
4166     }
4167   else
4168     {
4169       memset (p, 0, nbytes);
4170
4171       /* Now we need to generate a fixS to record the symbol value.  */
4172
4173 #ifdef TC_CONS_FIX_NEW
4174       TC_CONS_FIX_NEW (frag_now, p - frag_now->fr_literal, nbytes, exp);
4175 #else
4176       {
4177         bfd_reloc_code_real_type r;
4178
4179         switch (nbytes)
4180           {
4181           case 1:
4182             r = BFD_RELOC_8;
4183             break;
4184           case 2:
4185             r = BFD_RELOC_16;
4186             break;
4187           case 4:
4188             r = BFD_RELOC_32;
4189             break;
4190           case 8:
4191             r = BFD_RELOC_64;
4192             break;
4193           default:
4194             as_bad (_("unsupported BFD relocation size %u"), nbytes);
4195             r = BFD_RELOC_32;
4196             break;
4197           }
4198         fix_new_exp (frag_now, p - frag_now->fr_literal, (int) nbytes, exp,
4199                      0, r);
4200       }
4201 #endif
4202     }
4203 }
4204 \f
4205 #ifdef BITFIELD_CONS_EXPRESSIONS
4206
4207 /* i960 assemblers, (eg, asm960), allow bitfields after ".byte" as
4208    w:x,y:z, where w and y are bitwidths and x and y are values.  They
4209    then pack them all together. We do a little better in that we allow
4210    them in words, longs, etc. and we'll pack them in target byte order
4211    for you.
4212
4213    The rules are: pack least significant bit first, if a field doesn't
4214    entirely fit, put it in the next unit.  Overflowing the bitfield is
4215    explicitly *not* even a warning.  The bitwidth should be considered
4216    a "mask".
4217
4218    To use this function the tc-XXX.h file should define
4219    BITFIELD_CONS_EXPRESSIONS.  */
4220
4221 static void
4222 parse_bitfield_cons (exp, nbytes)
4223      expressionS *exp;
4224      unsigned int nbytes;
4225 {
4226   unsigned int bits_available = BITS_PER_CHAR * nbytes;
4227   char *hold = input_line_pointer;
4228
4229   (void) expression (exp);
4230
4231   if (*input_line_pointer == ':')
4232     {
4233       /* Bitfields.  */
4234       long value = 0;
4235
4236       for (;;)
4237         {
4238           unsigned long width;
4239
4240           if (*input_line_pointer != ':')
4241             {
4242               input_line_pointer = hold;
4243               break;
4244             }                   /* Next piece is not a bitfield.  */
4245
4246           /* In the general case, we can't allow
4247              full expressions with symbol
4248              differences and such.  The relocation
4249              entries for symbols not defined in this
4250              assembly would require arbitrary field
4251              widths, positions, and masks which most
4252              of our current object formats don't
4253              support.
4254
4255              In the specific case where a symbol
4256              *is* defined in this assembly, we
4257              *could* build fixups and track it, but
4258              this could lead to confusion for the
4259              backends.  I'm lazy. I'll take any
4260              SEG_ABSOLUTE. I think that means that
4261              you can use a previous .set or
4262              .equ type symbol.  xoxorich.  */
4263
4264           if (exp->X_op == O_absent)
4265             {
4266               as_warn (_("using a bit field width of zero"));
4267               exp->X_add_number = 0;
4268               exp->X_op = O_constant;
4269             }                   /* Implied zero width bitfield.  */
4270
4271           if (exp->X_op != O_constant)
4272             {
4273               *input_line_pointer = '\0';
4274               as_bad (_("field width \"%s\" too complex for a bitfield"), hold);
4275               *input_line_pointer = ':';
4276               demand_empty_rest_of_line ();
4277               return;
4278             }                   /* Too complex.  */
4279
4280           if ((width = exp->X_add_number) > (BITS_PER_CHAR * nbytes))
4281             {
4282               as_warn (_("field width %lu too big to fit in %d bytes: truncated to %d bits"),
4283                        width, nbytes, (BITS_PER_CHAR * nbytes));
4284               width = BITS_PER_CHAR * nbytes;
4285             }                   /* Too big.  */
4286
4287           if (width > bits_available)
4288             {
4289               /* FIXME-SOMEDAY: backing up and reparsing is wasteful.  */
4290               input_line_pointer = hold;
4291               exp->X_add_number = value;
4292               break;
4293             }                   /* Won't fit.  */
4294
4295           /* Skip ':'.  */
4296           hold = ++input_line_pointer;
4297
4298           (void) expression (exp);
4299           if (exp->X_op != O_constant)
4300             {
4301               char cache = *input_line_pointer;
4302
4303               *input_line_pointer = '\0';
4304               as_bad (_("field value \"%s\" too complex for a bitfield"), hold);
4305               *input_line_pointer = cache;
4306               demand_empty_rest_of_line ();
4307               return;
4308             }                   /* Too complex.  */
4309
4310           value |= ((~(-1 << width) & exp->X_add_number)
4311                     << ((BITS_PER_CHAR * nbytes) - bits_available));
4312
4313           if ((bits_available -= width) == 0
4314               || is_it_end_of_statement ()
4315               || *input_line_pointer != ',')
4316             {
4317               break;
4318             }                   /* All the bitfields we're gonna get.  */
4319
4320           hold = ++input_line_pointer;
4321           (void) expression (exp);
4322         }
4323
4324       exp->X_add_number = value;
4325       exp->X_op = O_constant;
4326       exp->X_unsigned = 1;
4327     }
4328 }
4329
4330 #endif /* BITFIELD_CONS_EXPRESSIONS */
4331 \f
4332 /* Handle an MRI style string expression.  */
4333
4334 #ifdef TC_M68K
4335 static void
4336 parse_mri_cons (exp, nbytes)
4337      expressionS *exp;
4338      unsigned int nbytes;
4339 {
4340   if (*input_line_pointer != '\''
4341       && (input_line_pointer[1] != '\''
4342           || (*input_line_pointer != 'A'
4343               && *input_line_pointer != 'E')))
4344     TC_PARSE_CONS_EXPRESSION (exp, nbytes);
4345   else
4346     {
4347       unsigned int scan;
4348       unsigned int result = 0;
4349
4350       /* An MRI style string.  Cut into as many bytes as will fit into
4351          a nbyte chunk, left justify if necessary, and separate with
4352          commas so we can try again later.  */
4353       if (*input_line_pointer == 'A')
4354         ++input_line_pointer;
4355       else if (*input_line_pointer == 'E')
4356         {
4357           as_bad (_("EBCDIC constants are not supported"));
4358           ++input_line_pointer;
4359         }
4360
4361       input_line_pointer++;
4362       for (scan = 0; scan < nbytes; scan++)
4363         {
4364           if (*input_line_pointer == '\'')
4365             {
4366               if (input_line_pointer[1] == '\'')
4367                 {
4368                   input_line_pointer++;
4369                 }
4370               else
4371                 break;
4372             }
4373           result = (result << 8) | (*input_line_pointer++);
4374         }
4375
4376       /* Left justify.  */
4377       while (scan < nbytes)
4378         {
4379           result <<= 8;
4380           scan++;
4381         }
4382
4383       /* Create correct expression.  */
4384       exp->X_op = O_constant;
4385       exp->X_add_number = result;
4386
4387       /* Fake it so that we can read the next char too.  */
4388       if (input_line_pointer[0] != '\'' ||
4389           (input_line_pointer[0] == '\'' && input_line_pointer[1] == '\''))
4390         {
4391           input_line_pointer -= 2;
4392           input_line_pointer[0] = ',';
4393           input_line_pointer[1] = '\'';
4394         }
4395       else
4396         input_line_pointer++;
4397     }
4398 }
4399 #endif /* TC_M68K */
4400 \f
4401 #ifdef REPEAT_CONS_EXPRESSIONS
4402
4403 /* Parse a repeat expression for cons.  This is used by the MIPS
4404    assembler.  The format is NUMBER:COUNT; NUMBER appears in the
4405    object file COUNT times.
4406
4407    To use this for a target, define REPEAT_CONS_EXPRESSIONS.  */
4408
4409 static void
4410 parse_repeat_cons (exp, nbytes)
4411      expressionS *exp;
4412      unsigned int nbytes;
4413 {
4414   expressionS count;
4415   register int i;
4416
4417   expression (exp);
4418
4419   if (*input_line_pointer != ':')
4420     {
4421       /* No repeat count.  */
4422       return;
4423     }
4424
4425   ++input_line_pointer;
4426   expression (&count);
4427   if (count.X_op != O_constant
4428       || count.X_add_number <= 0)
4429     {
4430       as_warn (_("unresolvable or nonpositive repeat count; using 1"));
4431       return;
4432     }
4433
4434   /* The cons function is going to output this expression once.  So we
4435      output it count - 1 times.  */
4436   for (i = count.X_add_number - 1; i > 0; i--)
4437     emit_expr (exp, nbytes);
4438 }
4439
4440 #endif /* REPEAT_CONS_EXPRESSIONS */
4441 \f
4442 /* Parse a floating point number represented as a hex constant.  This
4443    permits users to specify the exact bits they want in the floating
4444    point number.  */
4445
4446 static int
4447 hex_float (int float_type, char *bytes)
4448 {
4449   int length;
4450   int i;
4451
4452   switch (float_type)
4453     {
4454     case 'f':
4455     case 'F':
4456     case 's':
4457     case 'S':
4458       length = 4;
4459       break;
4460
4461     case 'd':
4462     case 'D':
4463     case 'r':
4464     case 'R':
4465       length = 8;
4466       break;
4467
4468     case 'x':
4469     case 'X':
4470       length = 12;
4471       break;
4472
4473     case 'p':
4474     case 'P':
4475       length = 12;
4476       break;
4477
4478     default:
4479       as_bad (_("unknown floating type type '%c'"), float_type);
4480       return -1;
4481     }
4482
4483   /* It would be nice if we could go through expression to parse the
4484      hex constant, but if we get a bignum it's a pain to sort it into
4485      the buffer correctly.  */
4486   i = 0;
4487   while (hex_p (*input_line_pointer) || *input_line_pointer == '_')
4488     {
4489       int d;
4490
4491       /* The MRI assembler accepts arbitrary underscores strewn about
4492          through the hex constant, so we ignore them as well.  */
4493       if (*input_line_pointer == '_')
4494         {
4495           ++input_line_pointer;
4496           continue;
4497         }
4498
4499       if (i >= length)
4500         {
4501           as_warn (_("floating point constant too large"));
4502           return -1;
4503         }
4504       d = hex_value (*input_line_pointer) << 4;
4505       ++input_line_pointer;
4506       while (*input_line_pointer == '_')
4507         ++input_line_pointer;
4508       if (hex_p (*input_line_pointer))
4509         {
4510           d += hex_value (*input_line_pointer);
4511           ++input_line_pointer;
4512         }
4513       if (target_big_endian)
4514         bytes[i] = d;
4515       else
4516         bytes[length - i - 1] = d;
4517       ++i;
4518     }
4519
4520   if (i < length)
4521     {
4522       if (target_big_endian)
4523         memset (bytes + i, 0, length - i);
4524       else
4525         memset (bytes, 0, length - i);
4526     }
4527
4528   return length;
4529 }
4530
4531 /*                      float_cons()
4532
4533    CONStruct some more frag chars of .floats .ffloats etc.
4534    Makes 0 or more new frags.
4535    If need_pass_2 == 1, no frags are emitted.
4536    This understands only floating literals, not expressions. Sorry.
4537
4538    A floating constant is defined by atof_generic(), except it is preceded
4539    by 0d 0f 0g or 0h. After observing the STRANGE way my BSD AS does its
4540    reading, I decided to be incompatible. This always tries to give you
4541    rounded bits to the precision of the pseudo-op. Former AS did premature
4542    truncation, restored noisy bits instead of trailing 0s AND gave you
4543    a choice of 2 flavours of noise according to which of 2 floating-point
4544    scanners you directed AS to use.
4545
4546    In:  input_line_pointer->whitespace before, or '0' of flonum.  */
4547
4548 void
4549 float_cons (/* Clobbers input_line-pointer, checks end-of-line.  */
4550             register int float_type     /* 'f':.ffloat ... 'F':.float ...  */)
4551 {
4552   register char *p;
4553   int length;                   /* Number of chars in an object.  */
4554   register char *err;           /* Error from scanning floating literal.  */
4555   char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT];
4556
4557   if (is_it_end_of_statement ())
4558     {
4559       demand_empty_rest_of_line ();
4560       return;
4561     }
4562
4563 #ifdef md_flush_pending_output
4564   md_flush_pending_output ();
4565 #endif
4566
4567   do
4568     {
4569       /* input_line_pointer->1st char of a flonum (we hope!).  */
4570       SKIP_WHITESPACE ();
4571
4572       /* Skip any 0{letter} that may be present. Don't even check if the
4573          letter is legal. Someone may invent a "z" format and this routine
4574          has no use for such information. Lusers beware: you get
4575          diagnostics if your input is ill-conditioned.  */
4576       if (input_line_pointer[0] == '0'
4577           && ISALPHA (input_line_pointer[1]))
4578         input_line_pointer += 2;
4579
4580       /* Accept :xxxx, where the x's are hex digits, for a floating
4581          point with the exact digits specified.  */
4582       if (input_line_pointer[0] == ':')
4583         {
4584           ++input_line_pointer;
4585           length = hex_float (float_type, temp);
4586           if (length < 0)
4587             {
4588               ignore_rest_of_line ();
4589               return;
4590             }
4591         }
4592       else
4593         {
4594           err = md_atof (float_type, temp, &length);
4595           know (length <= MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT);
4596           know (length > 0);
4597           if (err)
4598             {
4599               as_bad (_("bad floating literal: %s"), err);
4600               ignore_rest_of_line ();
4601               return;
4602             }
4603         }
4604
4605       if (!need_pass_2)
4606         {
4607           int count;
4608
4609           count = 1;
4610
4611 #ifdef REPEAT_CONS_EXPRESSIONS
4612           if (*input_line_pointer == ':')
4613             {
4614               expressionS count_exp;
4615
4616               ++input_line_pointer;
4617               expression (&count_exp);
4618
4619               if (count_exp.X_op != O_constant
4620                   || count_exp.X_add_number <= 0)
4621                 as_warn (_("unresolvable or nonpositive repeat count; using 1"));
4622               else
4623                 count = count_exp.X_add_number;
4624             }
4625 #endif
4626
4627           while (--count >= 0)
4628             {
4629               p = frag_more (length);
4630               memcpy (p, temp, (unsigned int) length);
4631             }
4632         }
4633       SKIP_WHITESPACE ();
4634     }
4635   while (*input_line_pointer++ == ',');
4636
4637   /* Put terminator back into stream.  */
4638   --input_line_pointer;
4639   demand_empty_rest_of_line ();
4640 }
4641 \f
4642 /* Return the size of a LEB128 value.  */
4643
4644 static inline int
4645 sizeof_sleb128 (offsetT value)
4646 {
4647   register int size = 0;
4648   register unsigned byte;
4649
4650   do
4651     {
4652       byte = (value & 0x7f);
4653       /* Sadly, we cannot rely on typical arithmetic right shift behaviour.
4654          Fortunately, we can structure things so that the extra work reduces
4655          to a noop on systems that do things "properly".  */
4656       value = (value >> 7) | ~(-(offsetT)1 >> 7);
4657       size += 1;
4658     }
4659   while (!(((value == 0) && ((byte & 0x40) == 0))
4660            || ((value == -1) && ((byte & 0x40) != 0))));
4661
4662   return size;
4663 }
4664
4665 static inline int
4666 sizeof_uleb128 (valueT value)
4667 {
4668   register int size = 0;
4669   register unsigned byte;
4670
4671   do
4672     {
4673       byte = (value & 0x7f);
4674       value >>= 7;
4675       size += 1;
4676     }
4677   while (value != 0);
4678
4679   return size;
4680 }
4681
4682 int
4683 sizeof_leb128 (valueT value, int sign)
4684 {
4685   if (sign)
4686     return sizeof_sleb128 ((offsetT) value);
4687   else
4688     return sizeof_uleb128 (value);
4689 }
4690
4691 /* Output a LEB128 value.  */
4692
4693 static inline int
4694 output_sleb128 (char *p, offsetT value)
4695 {
4696   register char *orig = p;
4697   register int more;
4698
4699   do
4700     {
4701       unsigned byte = (value & 0x7f);
4702
4703       /* Sadly, we cannot rely on typical arithmetic right shift behaviour.
4704          Fortunately, we can structure things so that the extra work reduces
4705          to a noop on systems that do things "properly".  */
4706       value = (value >> 7) | ~(-(offsetT)1 >> 7);
4707
4708       more = !((((value == 0) && ((byte & 0x40) == 0))
4709                 || ((value == -1) && ((byte & 0x40) != 0))));
4710       if (more)
4711         byte |= 0x80;
4712
4713       *p++ = byte;
4714     }
4715   while (more);
4716
4717   return p - orig;
4718 }
4719
4720 static inline int
4721 output_uleb128 (char *p, valueT value)
4722 {
4723   char *orig = p;
4724
4725   do
4726     {
4727       unsigned byte = (value & 0x7f);
4728       value >>= 7;
4729       if (value != 0)
4730         /* More bytes to follow.  */
4731         byte |= 0x80;
4732
4733       *p++ = byte;
4734     }
4735   while (value != 0);
4736
4737   return p - orig;
4738 }
4739
4740 int
4741 output_leb128 (char *p, valueT value, int sign)
4742 {
4743   if (sign)
4744     return output_sleb128 (p, (offsetT) value);
4745   else
4746     return output_uleb128 (p, value);
4747 }
4748
4749 /* Do the same for bignums.  We combine sizeof with output here in that
4750    we don't output for NULL values of P.  It isn't really as critical as
4751    for "normal" values that this be streamlined.  */
4752
4753 static inline int
4754 output_big_sleb128 (char *p, LITTLENUM_TYPE *bignum, int size)
4755 {
4756   char *orig = p;
4757   valueT val = 0;
4758   int loaded = 0;
4759   unsigned byte;
4760
4761   /* Strip leading sign extensions off the bignum.  */
4762   while (size > 1
4763          && bignum[size - 1] == LITTLENUM_MASK
4764          && bignum[size - 2] > LITTLENUM_MASK / 2)
4765     size--;
4766
4767   do
4768     {
4769       /* OR in the next part of the littlenum.  */
4770       val |= (*bignum << loaded);
4771       loaded += LITTLENUM_NUMBER_OF_BITS;
4772       size--;
4773       bignum++;
4774
4775       /* Add bytes until there are less than 7 bits left in VAL
4776          or until every non-sign bit has been written.  */
4777       do
4778         {
4779           byte = val & 0x7f;
4780           loaded -= 7;
4781           val >>= 7;
4782           if (size > 0
4783               || val != ((byte & 0x40) == 0 ? 0 : ((valueT) 1 << loaded) - 1))
4784             byte |= 0x80;
4785
4786           if (orig)
4787             *p = byte;
4788           p++;
4789         }
4790       while ((byte & 0x80) != 0 && loaded >= 7);
4791     }
4792   while (size > 0);
4793
4794   /* Mop up any left-over bits (of which there will be less than 7).  */
4795   if ((byte & 0x80) != 0)
4796     {
4797       /* Sign-extend VAL.  */
4798       if (val & (1 << (loaded - 1)))
4799         val |= ~0 << loaded;
4800       if (orig)
4801         *p = val & 0x7f;
4802       p++;
4803     }
4804
4805   return p - orig;
4806 }
4807
4808 static inline int
4809 output_big_uleb128 (char *p, LITTLENUM_TYPE *bignum, int size)
4810 {
4811   char *orig = p;
4812   valueT val = 0;
4813   int loaded = 0;
4814   unsigned byte;
4815
4816   /* Strip leading zeros off the bignum.  */
4817   /* XXX: Is this needed?  */
4818   while (size > 0 && bignum[size - 1] == 0)
4819     size--;
4820
4821   do
4822     {
4823       if (loaded < 7 && size > 0)
4824         {
4825           val |= (*bignum << loaded);
4826           loaded += 8 * CHARS_PER_LITTLENUM;
4827           size--;
4828           bignum++;
4829         }
4830
4831       byte = val & 0x7f;
4832       loaded -= 7;
4833       val >>= 7;
4834
4835       if (size > 0 || val)
4836         byte |= 0x80;
4837
4838       if (orig)
4839         *p = byte;
4840       p++;
4841     }
4842   while (byte & 0x80);
4843
4844   return p - orig;
4845 }
4846
4847 static int
4848 output_big_leb128 (char *p, LITTLENUM_TYPE *bignum, int size, int sign)
4849 {
4850   if (sign)
4851     return output_big_sleb128 (p, bignum, size);
4852   else
4853     return output_big_uleb128 (p, bignum, size);
4854 }
4855
4856 /* Generate the appropriate fragments for a given expression to emit a
4857    leb128 value.  */
4858
4859 static void
4860 emit_leb128_expr (expressionS *exp, int sign)
4861 {
4862   operatorT op = exp->X_op;
4863   unsigned int nbytes;
4864
4865   if (op == O_absent || op == O_illegal)
4866     {
4867       as_warn (_("zero assumed for missing expression"));
4868       exp->X_add_number = 0;
4869       op = O_constant;
4870     }
4871   else if (op == O_big && exp->X_add_number <= 0)
4872     {
4873       as_bad (_("floating point number invalid"));
4874       exp->X_add_number = 0;
4875       op = O_constant;
4876     }
4877   else if (op == O_register)
4878     {
4879       as_warn (_("register value used as expression"));
4880       op = O_constant;
4881     }
4882   else if (op == O_constant
4883            && sign
4884            && (exp->X_add_number < 0) != !exp->X_unsigned)
4885     {
4886       /* We're outputting a signed leb128 and the sign of X_add_number
4887          doesn't reflect the sign of the original value.  Convert EXP
4888          to a correctly-extended bignum instead.  */
4889       convert_to_bignum (exp);
4890       op = O_big;
4891     }
4892
4893   /* Let check_eh_frame know that data is being emitted.  nbytes == -1 is
4894      a signal that this is leb128 data.  It shouldn't optimize this away.  */
4895   nbytes = (unsigned int) -1;
4896   if (check_eh_frame (exp, &nbytes))
4897     abort ();
4898
4899   /* Let the backend know that subsequent data may be byte aligned.  */
4900 #ifdef md_cons_align
4901   md_cons_align (1);
4902 #endif
4903
4904   if (op == O_constant)
4905     {
4906       /* If we've got a constant, emit the thing directly right now.  */
4907
4908       valueT value = exp->X_add_number;
4909       int size;
4910       char *p;
4911
4912       size = sizeof_leb128 (value, sign);
4913       p = frag_more (size);
4914       output_leb128 (p, value, sign);
4915     }
4916   else if (op == O_big)
4917     {
4918       /* O_big is a different sort of constant.  */
4919
4920       int size;
4921       char *p;
4922
4923       size = output_big_leb128 (NULL, generic_bignum, exp->X_add_number, sign);
4924       p = frag_more (size);
4925       output_big_leb128 (p, generic_bignum, exp->X_add_number, sign);
4926     }
4927   else
4928     {
4929       /* Otherwise, we have to create a variable sized fragment and
4930          resolve things later.  */
4931
4932       frag_var (rs_leb128, sizeof_uleb128 (~(valueT) 0), 0, sign,
4933                 make_expr_symbol (exp), 0, (char *) NULL);
4934     }
4935 }
4936
4937 /* Parse the .sleb128 and .uleb128 pseudos.  */
4938
4939 void
4940 s_leb128 (int sign)
4941 {
4942   expressionS exp;
4943
4944 #ifdef md_flush_pending_output
4945   md_flush_pending_output ();
4946 #endif
4947
4948   do
4949     {
4950       expression (&exp);
4951       emit_leb128_expr (&exp, sign);
4952     }
4953   while (*input_line_pointer++ == ',');
4954
4955   input_line_pointer--;
4956   demand_empty_rest_of_line ();
4957 }
4958 \f
4959 /* We read 0 or more ',' separated, double-quoted strings.
4960    Caller should have checked need_pass_2 is FALSE because we don't
4961    check it.  */
4962
4963 void
4964 stringer (/* Worker to do .ascii etc statements.  */
4965           /* Checks end-of-line.  */
4966           register int append_zero      /* 0: don't append '\0', else 1.  */)
4967 {
4968   register unsigned int c;
4969   char *start;
4970
4971 #ifdef md_flush_pending_output
4972   md_flush_pending_output ();
4973 #endif
4974
4975   /* The following awkward logic is to parse ZERO or more strings,
4976      comma separated. Recall a string expression includes spaces
4977      before the opening '\"' and spaces after the closing '\"'.
4978      We fake a leading ',' if there is (supposed to be)
4979      a 1st, expression. We keep demanding expressions for each ','.  */
4980   if (is_it_end_of_statement ())
4981     {
4982       c = 0;                    /* Skip loop.  */
4983       ++input_line_pointer;     /* Compensate for end of loop.  */
4984     }
4985   else
4986     {
4987       c = ',';                  /* Do loop.  */
4988     }
4989   /* If we have been switched into the abs_section then we
4990      will not have an obstack onto which we can hang strings.  */
4991   if (now_seg == absolute_section)
4992     {
4993       as_bad (_("strings must be placed into a section"));
4994       c = 0;
4995       ignore_rest_of_line ();
4996     }
4997
4998   while (c == ',' || c == '<' || c == '"')
4999     {
5000       SKIP_WHITESPACE ();
5001       switch (*input_line_pointer)
5002         {
5003         case '\"':
5004           ++input_line_pointer; /*->1st char of string.  */
5005           start = input_line_pointer;
5006           while (is_a_char (c = next_char_of_string ()))
5007             {
5008               FRAG_APPEND_1_CHAR (c);
5009             }
5010           if (append_zero)
5011             {
5012               FRAG_APPEND_1_CHAR (0);
5013             }
5014           know (input_line_pointer[-1] == '\"');
5015
5016 #ifndef NO_LISTING
5017 #ifdef OBJ_ELF
5018           /* In ELF, when gcc is emitting DWARF 1 debugging output, it
5019              will emit .string with a filename in the .debug section
5020              after a sequence of constants.  See the comment in
5021              emit_expr for the sequence.  emit_expr will set
5022              dwarf_file_string to non-zero if this string might be a
5023              source file name.  */
5024           if (strcmp (segment_name (now_seg), ".debug") != 0)
5025             dwarf_file_string = 0;
5026           else if (dwarf_file_string)
5027             {
5028               c = input_line_pointer[-1];
5029               input_line_pointer[-1] = '\0';
5030               listing_source_file (start);
5031               input_line_pointer[-1] = c;
5032             }
5033 #endif
5034 #endif
5035
5036           break;
5037         case '<':
5038           input_line_pointer++;
5039           c = get_single_number ();
5040           FRAG_APPEND_1_CHAR (c);
5041           if (*input_line_pointer != '>')
5042             {
5043               as_bad (_("expected <nn>"));
5044             }
5045           input_line_pointer++;
5046           break;
5047         case ',':
5048           input_line_pointer++;
5049           break;
5050         }
5051       SKIP_WHITESPACE ();
5052       c = *input_line_pointer;
5053     }
5054
5055   demand_empty_rest_of_line ();
5056 }                               /* stringer() */
5057 \f
5058 /* FIXME-SOMEDAY: I had trouble here on characters with the
5059     high bits set.  We'll probably also have trouble with
5060     multibyte chars, wide chars, etc.  Also be careful about
5061     returning values bigger than 1 byte.  xoxorich.  */
5062
5063 unsigned int
5064 next_char_of_string (void)
5065 {
5066   register unsigned int c;
5067
5068   c = *input_line_pointer++ & CHAR_MASK;
5069   switch (c)
5070     {
5071     case '\"':
5072       c = NOT_A_CHAR;
5073       break;
5074
5075     case '\n':
5076       as_warn (_("unterminated string; newline inserted"));
5077       bump_line_counters ();
5078       break;
5079
5080 #ifndef NO_STRING_ESCAPES
5081     case '\\':
5082       switch (c = *input_line_pointer++)
5083         {
5084         case 'b':
5085           c = '\b';
5086           break;
5087
5088         case 'f':
5089           c = '\f';
5090           break;
5091
5092         case 'n':
5093           c = '\n';
5094           break;
5095
5096         case 'r':
5097           c = '\r';
5098           break;
5099
5100         case 't':
5101           c = '\t';
5102           break;
5103
5104         case 'v':
5105           c = '\013';
5106           break;
5107
5108         case '\\':
5109         case '"':
5110           break;                /* As itself.  */
5111
5112         case '0':
5113         case '1':
5114         case '2':
5115         case '3':
5116         case '4':
5117         case '5':
5118         case '6':
5119         case '7':
5120         case '8':
5121         case '9':
5122           {
5123             long number;
5124             int i;
5125
5126             for (i = 0, number = 0;
5127                  ISDIGIT (c) && i < 3;
5128                  c = *input_line_pointer++, i++)
5129               {
5130                 number = number * 8 + c - '0';
5131               }
5132
5133             c = number & 0xff;
5134           }
5135           --input_line_pointer;
5136           break;
5137
5138         case 'x':
5139         case 'X':
5140           {
5141             long number;
5142
5143             number = 0;
5144             c = *input_line_pointer++;
5145             while (ISXDIGIT (c))
5146               {
5147                 if (ISDIGIT (c))
5148                   number = number * 16 + c - '0';
5149                 else if (ISUPPER (c))
5150                   number = number * 16 + c - 'A' + 10;
5151                 else
5152                   number = number * 16 + c - 'a' + 10;
5153                 c = *input_line_pointer++;
5154               }
5155             c = number & 0xff;
5156             --input_line_pointer;
5157           }
5158           break;
5159
5160         case '\n':
5161           /* To be compatible with BSD 4.2 as: give the luser a linefeed!!  */
5162           as_warn (_("unterminated string; newline inserted"));
5163           c = '\n';
5164           bump_line_counters ();
5165           break;
5166
5167         default:
5168
5169 #ifdef ONLY_STANDARD_ESCAPES
5170           as_bad (_("bad escaped character in string"));
5171           c = '?';
5172 #endif /* ONLY_STANDARD_ESCAPES */
5173
5174           break;
5175         }
5176       break;
5177 #endif /* ! defined (NO_STRING_ESCAPES) */
5178
5179     default:
5180       break;
5181     }
5182   return (c);
5183 }
5184 \f
5185 static segT
5186 get_segmented_expression (register expressionS *expP)
5187 {
5188   register segT retval;
5189
5190   retval = expression (expP);
5191   if (expP->X_op == O_illegal
5192       || expP->X_op == O_absent
5193       || expP->X_op == O_big)
5194     {
5195       as_bad (_("expected address expression"));
5196       expP->X_op = O_constant;
5197       expP->X_add_number = 0;
5198       retval = absolute_section;
5199     }
5200   return retval;
5201 }
5202
5203 static segT
5204 get_known_segmented_expression (register expressionS *expP)
5205 {
5206   register segT retval;
5207
5208   if ((retval = get_segmented_expression (expP)) == undefined_section)
5209     {
5210       /* There is no easy way to extract the undefined symbol from the
5211          expression.  */
5212       if (expP->X_add_symbol != NULL
5213           && S_GET_SEGMENT (expP->X_add_symbol) != expr_section)
5214         as_warn (_("symbol \"%s\" undefined; zero assumed"),
5215                  S_GET_NAME (expP->X_add_symbol));
5216       else
5217         as_warn (_("some symbol undefined; zero assumed"));
5218       retval = absolute_section;
5219       expP->X_op = O_constant;
5220       expP->X_add_number = 0;
5221     }
5222   know (retval == absolute_section || SEG_NORMAL (retval));
5223   return (retval);
5224 }
5225
5226 char                            /* Return terminator.  */
5227 get_absolute_expression_and_terminator (long *val_pointer /* Return value of expression.  */)
5228 {
5229   /* FIXME: val_pointer should probably be offsetT *.  */
5230   *val_pointer = (long) get_absolute_expression ();
5231   return (*input_line_pointer++);
5232 }
5233 \f
5234 /* Like demand_copy_string, but return NULL if the string contains any '\0's.
5235    Give a warning if that happens.  */
5236
5237 char *
5238 demand_copy_C_string (int *len_pointer)
5239 {
5240   register char *s;
5241
5242   if ((s = demand_copy_string (len_pointer)) != 0)
5243     {
5244       register int len;
5245
5246       for (len = *len_pointer; len > 0; len--)
5247         {
5248           if (*s == 0)
5249             {
5250               s = 0;
5251               len = 1;
5252               *len_pointer = 0;
5253               as_bad (_("this string may not contain \'\\0\'"));
5254             }
5255         }
5256     }
5257
5258   return s;
5259 }
5260 \f
5261 /* Demand string, but return a safe (=private) copy of the string.
5262    Return NULL if we can't read a string here.  */
5263
5264 char *
5265 demand_copy_string (int *lenP)
5266 {
5267   register unsigned int c;
5268   register int len;
5269   char *retval;
5270
5271   len = 0;
5272   SKIP_WHITESPACE ();
5273   if (*input_line_pointer == '\"')
5274     {
5275       input_line_pointer++;     /* Skip opening quote.  */
5276
5277       while (is_a_char (c = next_char_of_string ()))
5278         {
5279           obstack_1grow (&notes, c);
5280           len++;
5281         }
5282       /* JF this next line is so demand_copy_C_string will return a
5283          null terminated string.  */
5284       obstack_1grow (&notes, '\0');
5285       retval = obstack_finish (&notes);
5286     }
5287   else
5288     {
5289       as_bad (_("missing string"));
5290       retval = NULL;
5291       ignore_rest_of_line ();
5292     }
5293   *lenP = len;
5294   return (retval);
5295 }
5296 \f
5297 /* In:  Input_line_pointer->next character.
5298
5299    Do:  Skip input_line_pointer over all whitespace.
5300
5301    Out: 1 if input_line_pointer->end-of-line.  */
5302
5303 int
5304 is_it_end_of_statement (void)
5305 {
5306   SKIP_WHITESPACE ();
5307   return (is_end_of_line[(unsigned char) *input_line_pointer]);
5308 }
5309
5310 void
5311 equals (char *sym_name, int reassign)
5312 {
5313   char *stop = NULL;
5314   char stopc = 0;
5315
5316   input_line_pointer++;
5317   if (*input_line_pointer == '=')
5318     input_line_pointer++;
5319   if (reassign < 0 && *input_line_pointer == '=')
5320     input_line_pointer++;
5321
5322   while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
5323     input_line_pointer++;
5324
5325   if (flag_mri)
5326     stop = mri_comment_field (&stopc);
5327
5328   assign_symbol (sym_name, reassign >= 0 ? !reassign : reassign);
5329
5330   if (flag_mri)
5331     {
5332       demand_empty_rest_of_line ();
5333       mri_comment_end (stop, stopc);
5334     }
5335 }
5336
5337 /* .incbin -- include a file verbatim at the current location.  */
5338
5339 void
5340 s_incbin (int x ATTRIBUTE_UNUSED)
5341 {
5342   FILE * binfile;
5343   char * path;
5344   char * filename;
5345   char * binfrag;
5346   long   skip = 0;
5347   long   count = 0;
5348   long   bytes;
5349   int    len;
5350
5351 #ifdef md_flush_pending_output
5352   md_flush_pending_output ();
5353 #endif
5354
5355   SKIP_WHITESPACE ();
5356   filename = demand_copy_string (& len);
5357   if (filename == NULL)
5358     return;
5359
5360   SKIP_WHITESPACE ();
5361
5362   /* Look for optional skip and count.  */
5363   if (* input_line_pointer == ',')
5364     {
5365       ++ input_line_pointer;
5366       skip = get_absolute_expression ();
5367
5368       SKIP_WHITESPACE ();
5369
5370       if (* input_line_pointer == ',')
5371         {
5372           ++ input_line_pointer;
5373
5374           count = get_absolute_expression ();
5375           if (count == 0)
5376             as_warn (_(".incbin count zero, ignoring `%s'"), filename);
5377
5378           SKIP_WHITESPACE ();
5379         }
5380     }
5381
5382   demand_empty_rest_of_line ();
5383
5384   /* Try opening absolute path first, then try include dirs.  */
5385   binfile = fopen (filename, FOPEN_RB);
5386   if (binfile == NULL)
5387     {
5388       int i;
5389
5390       path = xmalloc ((unsigned long) len + include_dir_maxlen + 5);
5391
5392       for (i = 0; i < include_dir_count; i++)
5393         {
5394           sprintf (path, "%s/%s", include_dirs[i], filename);
5395
5396           binfile = fopen (path, FOPEN_RB);
5397           if (binfile != NULL)
5398             break;
5399         }
5400
5401       if (binfile == NULL)
5402         as_bad (_("file not found: %s"), filename);
5403     }
5404   else
5405     path = xstrdup (filename);
5406
5407   if (binfile)
5408     {
5409       long   file_len;
5410
5411       register_dependency (path);
5412
5413       /* Compute the length of the file.  */
5414       if (fseek (binfile, 0, SEEK_END) != 0)
5415         {
5416           as_bad (_("seek to end of .incbin file failed `%s'"), path);
5417           goto done;
5418         }
5419       file_len = ftell (binfile);
5420
5421       /* If a count was not specified use the remainder of the file.  */
5422       if (count == 0)
5423         count = file_len - skip;
5424
5425       if (skip < 0 || count < 0 || file_len < 0 || skip + count > file_len)
5426         {
5427           as_bad (_("skip (%ld) or count (%ld) invalid for file size (%ld)"),
5428                   skip, count, file_len);
5429           goto done;
5430         }
5431
5432       if (fseek (binfile, skip, SEEK_SET) != 0)
5433         {
5434           as_bad (_("could not skip to %ld in file `%s'"), skip, path);
5435           goto done;
5436         }
5437
5438       /* Allocate frag space and store file contents in it.  */
5439       binfrag = frag_more (count);
5440
5441       bytes = fread (binfrag, 1, count, binfile);
5442       if (bytes < count)
5443         as_warn (_("truncated file `%s', %ld of %ld bytes read"),
5444                  path, bytes, count);
5445     }
5446 done:
5447   if (binfile != NULL)
5448     fclose (binfile);
5449   if (path)
5450     free (path);
5451 }
5452
5453 /* .include -- include a file at this point.  */
5454
5455 void
5456 s_include (int arg ATTRIBUTE_UNUSED)
5457 {
5458   char *filename;
5459   int i;
5460   FILE *try;
5461   char *path;
5462
5463   if (!flag_m68k_mri)
5464     {
5465       filename = demand_copy_string (&i);
5466       if (filename == NULL)
5467         {
5468           /* demand_copy_string has already printed an error and
5469              called ignore_rest_of_line.  */
5470           return;
5471         }
5472     }
5473   else
5474     {
5475       SKIP_WHITESPACE ();
5476       i = 0;
5477       while (!is_end_of_line[(unsigned char) *input_line_pointer]
5478              && *input_line_pointer != ' '
5479              && *input_line_pointer != '\t')
5480         {
5481           obstack_1grow (&notes, *input_line_pointer);
5482           ++input_line_pointer;
5483           ++i;
5484         }
5485
5486       obstack_1grow (&notes, '\0');
5487       filename = obstack_finish (&notes);
5488       while (!is_end_of_line[(unsigned char) *input_line_pointer])
5489         ++input_line_pointer;
5490     }
5491
5492   demand_empty_rest_of_line ();
5493   path = xmalloc ((unsigned long) i + include_dir_maxlen + 5 /* slop */ );
5494
5495   for (i = 0; i < include_dir_count; i++)
5496     {
5497       strcpy (path, include_dirs[i]);
5498       strcat (path, "/");
5499       strcat (path, filename);
5500       if (0 != (try = fopen (path, FOPEN_RT)))
5501         {
5502           fclose (try);
5503           goto gotit;
5504         }
5505     }
5506
5507   free (path);
5508   path = filename;
5509 gotit:
5510   /* malloc Storage leak when file is found on path.  FIXME-SOMEDAY.  */
5511   register_dependency (path);
5512   input_scrub_insert_file (path);
5513 }
5514
5515 void
5516 add_include_dir (char *path)
5517 {
5518   int i;
5519
5520   if (include_dir_count == 0)
5521     {
5522       include_dirs = (char **) xmalloc (2 * sizeof (*include_dirs));
5523       include_dirs[0] = ".";    /* Current dir.  */
5524       include_dir_count = 2;
5525     }
5526   else
5527     {
5528       include_dir_count++;
5529       include_dirs =
5530         (char **) realloc (include_dirs,
5531                            include_dir_count * sizeof (*include_dirs));
5532     }
5533
5534   include_dirs[include_dir_count - 1] = path;   /* New one.  */
5535
5536   i = strlen (path);
5537   if (i > include_dir_maxlen)
5538     include_dir_maxlen = i;
5539 }
5540 \f
5541 /* Output debugging information to denote the source file.  */
5542
5543 static void
5544 generate_file_debug (void)
5545 {
5546   if (debug_type == DEBUG_STABS)
5547     stabs_generate_asm_file ();
5548 }
5549
5550 /* Output line number debugging information for the current source line.  */
5551
5552 void
5553 generate_lineno_debug (void)
5554 {
5555   switch (debug_type)
5556     {
5557     case DEBUG_UNSPECIFIED:
5558     case DEBUG_NONE:
5559     case DEBUG_DWARF:
5560       break;
5561     case DEBUG_STABS:
5562       stabs_generate_asm_lineno ();
5563       break;
5564     case DEBUG_ECOFF:
5565       ecoff_generate_asm_lineno ();
5566       break;
5567     case DEBUG_DWARF2:
5568       /* ??? We could here indicate to dwarf2dbg.c that something
5569          has changed.  However, since there is additional backend
5570          support that is required (calling dwarf2_emit_insn), we
5571          let dwarf2dbg.c call as_where on its own.  */
5572       break;
5573     }
5574 }
5575
5576 /* Output debugging information to mark a function entry point or end point.
5577    END_P is zero for .func, and non-zero for .endfunc.  */
5578
5579 void
5580 s_func (int end_p)
5581 {
5582   do_s_func (end_p, NULL);
5583 }
5584
5585 /* Subroutine of s_func so targets can choose a different default prefix.
5586    If DEFAULT_PREFIX is NULL, use the target's "leading char".  */
5587
5588 static void
5589 do_s_func (int end_p, const char *default_prefix)
5590 {
5591   /* Record the current function so that we can issue an error message for
5592      misplaced .func,.endfunc, and also so that .endfunc needs no
5593      arguments.  */
5594   static char *current_name;
5595   static char *current_label;
5596
5597   if (end_p)
5598     {
5599       if (current_name == NULL)
5600         {
5601           as_bad (_("missing .func"));
5602           ignore_rest_of_line ();
5603           return;
5604         }
5605
5606       if (debug_type == DEBUG_STABS)
5607         stabs_generate_asm_endfunc (current_name, current_label);
5608
5609       current_name = current_label = NULL;
5610     }
5611   else /* ! end_p */
5612     {
5613       char *name, *label;
5614       char delim1, delim2;
5615
5616       if (current_name != NULL)
5617         {
5618           as_bad (_(".endfunc missing for previous .func"));
5619           ignore_rest_of_line ();
5620           return;
5621         }
5622
5623       name = input_line_pointer;
5624       delim1 = get_symbol_end ();
5625       name = xstrdup (name);
5626       *input_line_pointer = delim1;
5627       SKIP_WHITESPACE ();
5628       if (*input_line_pointer != ',')
5629         {
5630           if (default_prefix)
5631             asprintf (&label, "%s%s", default_prefix, name);
5632           else
5633             {
5634               char leading_char = bfd_get_symbol_leading_char (stdoutput);
5635               /* Missing entry point, use function's name with the leading
5636                  char prepended.  */
5637               if (leading_char)
5638                 asprintf (&label, "%c%s", leading_char, name);
5639               else
5640                 label = name;
5641             }
5642         }
5643       else
5644         {
5645           ++input_line_pointer;
5646           SKIP_WHITESPACE ();
5647           label = input_line_pointer;
5648           delim2 = get_symbol_end ();
5649           label = xstrdup (label);
5650           *input_line_pointer = delim2;
5651         }
5652
5653       if (debug_type == DEBUG_STABS)
5654         stabs_generate_asm_func (name, label);
5655
5656       current_name = name;
5657       current_label = label;
5658     }
5659
5660   demand_empty_rest_of_line ();
5661 }
5662 \f
5663 void
5664 s_ignore (int arg ATTRIBUTE_UNUSED)
5665 {
5666   ignore_rest_of_line ();
5667 }
5668
5669 void
5670 read_print_statistics (FILE *file)
5671 {
5672   hash_print_statistics (file, "pseudo-op table", po_hash);
5673 }
5674
5675 /* Inserts the given line into the input stream.
5676
5677    This call avoids macro/conditionals nesting checking, since the contents of
5678    the line are assumed to replace the contents of a line already scanned.
5679
5680    An appropriate use of this function would be substitution of input lines when
5681    called by md_start_line_hook().  The given line is assumed to already be
5682    properly scrubbed.  */
5683
5684 void
5685 input_scrub_insert_line (const char *line)
5686 {
5687   sb newline;
5688   sb_new (&newline);
5689   sb_add_string (&newline, line);
5690   input_scrub_include_sb (&newline, input_line_pointer, 0);
5691   sb_kill (&newline);
5692   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
5693 }
5694
5695 /* Insert a file into the input stream; the path must resolve to an actual
5696    file; no include path searching or dependency registering is performed.  */
5697
5698 void
5699 input_scrub_insert_file (char *path)
5700 {
5701   input_scrub_include_file (path, input_line_pointer);
5702   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
5703 }
5704
5705 /* Find the end of a line, considering quotation and escaping of quotes.  */
5706
5707 #if !defined(TC_SINGLE_QUOTE_STRINGS) && defined(SINGLE_QUOTE_STRINGS)
5708 # define TC_SINGLE_QUOTE_STRINGS 1
5709 #endif
5710
5711 static char *
5712 _find_end_of_line (char *s, int mri_string, int insn ATTRIBUTE_UNUSED)
5713 {
5714   char inquote = '\0';
5715   int inescape = 0;
5716
5717   while (!is_end_of_line[(unsigned char) *s]
5718          || (inquote && !ISCNTRL (*s))
5719          || (inquote == '\'' && flag_mri)
5720 #ifdef TC_EOL_IN_INSN
5721          || (insn && TC_EOL_IN_INSN (s))
5722 #endif
5723         )
5724     {
5725       if (mri_string && *s == '\'')
5726         inquote ^= *s;
5727       else if (inescape)
5728         inescape = 0;
5729       else if (*s == '\\')
5730         inescape = 1;
5731       else if (!inquote
5732                ? *s == '"'
5733 #ifdef TC_SINGLE_QUOTE_STRINGS
5734                  || (TC_SINGLE_QUOTE_STRINGS && *s == '\'')
5735 #endif
5736                : *s == inquote)
5737         inquote ^= *s;
5738       ++s;
5739     }
5740   if (inquote)
5741     as_warn (_("missing closing `%c'"), inquote);
5742   if (inescape)
5743     as_warn (_("stray `\\'"));
5744   return s;
5745 }
5746
5747 char *
5748 find_end_of_line (char *s, int mri_string)
5749 {
5750   return _find_end_of_line (s, mri_string, 0);
5751 }