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