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