* gas/mips/elf-rel6.d, gas/mips/elf-rel6.s: Extend testcase.
[binutils.git] / gas / input-scrub.c
blob7407741b3fa538c398f60e12cc9d3818abafb335
1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 2000, 2001, 2003, 2006
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
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.
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. */
23 #include "as.h"
24 #include "input-file.h"
25 #include "sb.h"
26 #include "listing.h"
29 * O/S independent module to supply buffers of sanitised source code
30 * to rest of assembler. We get sanitised input data of arbitrary length.
31 * We break these buffers on line boundaries, recombine pieces that
32 * were broken across buffers, and return a buffer of full lines to
33 * the caller.
34 * The last partial line begins the next buffer we build and return to caller.
35 * The buffer returned to caller is preceded by BEFORE_STRING and followed
36 * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
37 * is a newline.
38 * Also looks after line numbers, for e.g. error messages.
42 * We don't care how filthy our buffers are, but our callers assume
43 * that the following sanitation has already been done.
45 * No comments, reduce a comment to a space.
46 * Reduce a tab to a space unless it is 1st char of line.
47 * All multiple tabs and spaces collapsed into 1 char. Tab only
48 * legal if 1st char of line.
49 * # line file statements converted to .line x;.file y; statements.
50 * Escaped newlines at end of line: remove them but add as many newlines
51 * to end of statement as you removed in the middle, to synch line numbers.
54 #define BEFORE_STRING ("\n")
55 #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */
56 #define BEFORE_SIZE (1)
57 #define AFTER_SIZE (1)
59 static char *buffer_start; /*->1st char of full buffer area. */
60 static char *partial_where; /*->after last full line in buffer. */
61 static int partial_size; /* >=0. Number of chars in partial line in buffer. */
63 /* Because we need AFTER_STRING just after last full line, it clobbers
64 1st part of partial line. So we preserve 1st part of partial line
65 here. */
66 static char save_source[AFTER_SIZE];
68 /* What is the largest size buffer that input_file_give_next_buffer()
69 could return to us? */
70 static unsigned int buffer_length;
72 /* The index into an sb structure we are reading from. -1 if none. */
73 static int sb_index = -1;
75 /* If we are reading from an sb structure, this is it. */
76 static sb from_sb;
78 /* Should we do a conditional check on from_sb? */
79 static int from_sb_is_expansion = 1;
81 /* The number of nested sb structures we have included. */
82 int macro_nest;
84 /* We can have more than one source file open at once, though the info for all
85 but the latest one are saved off in a struct input_save. These files remain
86 open, so we are limited by the number of open files allowed by the
87 underlying OS. We may also sequentially read more than one source file in an
88 assembly. */
90 /* We must track the physical file and line number for error messages. We also
91 track a "logical" file and line number corresponding to (C?) compiler
92 source line numbers. Whenever we open a file we must fill in
93 physical_input_file. So if it is NULL we have not opened any files yet. */
95 static char *physical_input_file;
96 static char *logical_input_file;
98 typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
99 /* A line ends in '\n' or eof. */
101 static line_numberT physical_input_line;
102 static int logical_input_line;
104 /* Struct used to save the state of the input handler during include files */
105 struct input_save {
106 char * buffer_start;
107 char * partial_where;
108 int partial_size;
109 char save_source[AFTER_SIZE];
110 unsigned int buffer_length;
111 char * physical_input_file;
112 char * logical_input_file;
113 line_numberT physical_input_line;
114 int logical_input_line;
115 int sb_index;
116 sb from_sb;
117 int from_sb_is_expansion; /* Should we do a conditional check? */
118 struct input_save * next_saved_file; /* Chain of input_saves. */
119 char * input_file_save; /* Saved state of input routines. */
120 char * saved_position; /* Caller's saved position in buf. */
123 static struct input_save *input_scrub_push (char *saved_position);
124 static char *input_scrub_pop (struct input_save *arg);
126 /* Saved information about the file that .include'd this one. When we hit EOF,
127 we automatically pop to that file. */
129 static struct input_save *next_saved_file;
131 /* Push the state of input reading and scrubbing so that we can #include.
132 The return value is a 'void *' (fudged for old compilers) to a save
133 area, which can be restored by passing it to input_scrub_pop(). */
135 static struct input_save *
136 input_scrub_push (char *saved_position)
138 register struct input_save *saved;
140 saved = (struct input_save *) xmalloc (sizeof *saved);
142 saved->saved_position = saved_position;
143 saved->buffer_start = buffer_start;
144 saved->partial_where = partial_where;
145 saved->partial_size = partial_size;
146 saved->buffer_length = buffer_length;
147 saved->physical_input_file = physical_input_file;
148 saved->logical_input_file = logical_input_file;
149 saved->physical_input_line = physical_input_line;
150 saved->logical_input_line = logical_input_line;
151 saved->sb_index = sb_index;
152 saved->from_sb = from_sb;
153 saved->from_sb_is_expansion = from_sb_is_expansion;
154 memcpy (saved->save_source, save_source, sizeof (save_source));
155 saved->next_saved_file = next_saved_file;
156 saved->input_file_save = input_file_push ();
158 input_file_begin (); /* Reinitialize! */
159 logical_input_line = -1;
160 logical_input_file = (char *) NULL;
161 buffer_length = input_file_buffer_size ();
162 sb_index = -1;
164 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
165 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
167 return saved;
170 static char *
171 input_scrub_pop (struct input_save *saved)
173 char *saved_position;
175 input_scrub_end (); /* Finish off old buffer */
177 input_file_pop (saved->input_file_save);
178 saved_position = saved->saved_position;
179 buffer_start = saved->buffer_start;
180 buffer_length = saved->buffer_length;
181 physical_input_file = saved->physical_input_file;
182 logical_input_file = saved->logical_input_file;
183 physical_input_line = saved->physical_input_line;
184 logical_input_line = saved->logical_input_line;
185 sb_index = saved->sb_index;
186 from_sb = saved->from_sb;
187 from_sb_is_expansion = saved->from_sb_is_expansion;
188 partial_where = saved->partial_where;
189 partial_size = saved->partial_size;
190 next_saved_file = saved->next_saved_file;
191 memcpy (save_source, saved->save_source, sizeof (save_source));
193 free (saved);
194 return saved_position;
197 void
198 input_scrub_begin (void)
200 know (strlen (BEFORE_STRING) == BEFORE_SIZE);
201 know (strlen (AFTER_STRING) == AFTER_SIZE
202 || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
204 input_file_begin ();
206 buffer_length = input_file_buffer_size ();
208 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
209 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
211 /* Line number things. */
212 logical_input_line = -1;
213 logical_input_file = (char *) NULL;
214 physical_input_file = NULL; /* No file read yet. */
215 next_saved_file = NULL; /* At EOF, don't pop to any other file */
216 do_scrub_begin (flag_m68k_mri);
219 void
220 input_scrub_end (void)
222 if (buffer_start)
224 free (buffer_start);
225 buffer_start = 0;
226 input_file_end ();
230 /* Start reading input from a new file.
231 Return start of caller's part of buffer. */
233 char *
234 input_scrub_new_file (char *filename)
236 input_file_open (filename, !flag_no_comments);
237 physical_input_file = filename[0] ? filename : _("{standard input}");
238 physical_input_line = 0;
240 partial_size = 0;
241 return (buffer_start + BEFORE_SIZE);
244 /* Include a file from the current file. Save our state, cause it to
245 be restored on EOF, and begin handling a new file. Same result as
246 input_scrub_new_file. */
248 char *
249 input_scrub_include_file (char *filename, char *position)
251 next_saved_file = input_scrub_push (position);
252 return input_scrub_new_file (filename);
255 /* Start getting input from an sb structure. This is used when
256 expanding a macro. */
258 void
259 input_scrub_include_sb (sb *from, char *position, int is_expansion)
261 if (macro_nest > max_macro_nest)
262 as_fatal (_("macros nested too deeply"));
263 ++macro_nest;
265 #ifdef md_macro_start
266 if (is_expansion)
268 md_macro_start ();
270 #endif
272 next_saved_file = input_scrub_push (position);
274 sb_new (&from_sb);
275 from_sb_is_expansion = is_expansion;
276 if (from->len >= 1 && from->ptr[0] != '\n')
278 /* Add the sentinel required by read.c. */
279 sb_add_char (&from_sb, '\n');
281 sb_scrub_and_add_sb (&from_sb, from);
282 sb_index = 1;
284 /* These variables are reset by input_scrub_push. Restore them
285 since we are, after all, still at the same point in the file. */
286 logical_input_line = next_saved_file->logical_input_line;
287 logical_input_file = next_saved_file->logical_input_file;
290 void
291 input_scrub_close (void)
293 input_file_close ();
296 char *
297 input_scrub_next_buffer (char **bufp)
299 register char *limit; /*->just after last char of buffer. */
301 if (sb_index >= 0)
303 if (sb_index >= from_sb.len)
305 sb_kill (&from_sb);
306 if (from_sb_is_expansion
309 cond_finish_check (macro_nest);
310 #ifdef md_macro_end
311 /* Allow the target to clean up per-macro expansion
312 data. */
313 md_macro_end ();
314 #endif
316 --macro_nest;
317 partial_where = NULL;
318 if (next_saved_file != NULL)
319 *bufp = input_scrub_pop (next_saved_file);
320 return partial_where;
323 partial_where = from_sb.ptr + from_sb.len;
324 partial_size = 0;
325 *bufp = from_sb.ptr + sb_index;
326 sb_index = from_sb.len;
327 return partial_where;
330 *bufp = buffer_start + BEFORE_SIZE;
332 if (partial_size)
334 memcpy (buffer_start + BEFORE_SIZE, partial_where,
335 (unsigned int) partial_size);
336 memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
338 limit = input_file_give_next_buffer (buffer_start
339 + BEFORE_SIZE
340 + partial_size);
341 if (limit)
343 register char *p; /* Find last newline. */
345 for (p = limit - 1; *p != '\n'; --p)
347 ++p;
349 while (p <= buffer_start + BEFORE_SIZE)
351 int limoff;
353 limoff = limit - buffer_start;
354 buffer_length += input_file_buffer_size ();
355 buffer_start = xrealloc (buffer_start,
356 (BEFORE_SIZE
357 + 2 * buffer_length
358 + AFTER_SIZE));
359 *bufp = buffer_start + BEFORE_SIZE;
360 limit = input_file_give_next_buffer (buffer_start + limoff);
362 if (limit == NULL)
364 as_warn (_("partial line at end of file ignored"));
365 partial_where = NULL;
366 if (next_saved_file)
367 *bufp = input_scrub_pop (next_saved_file);
368 return NULL;
371 for (p = limit - 1; *p != '\n'; --p)
373 ++p;
376 partial_where = p;
377 partial_size = limit - p;
378 memcpy (save_source, partial_where, (int) AFTER_SIZE);
379 memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
381 else
383 partial_where = 0;
384 if (partial_size > 0)
386 as_warn (_("partial line at end of file ignored"));
389 /* Tell the listing we've finished the file. */
390 LISTING_EOF ();
392 /* If we should pop to another file at EOF, do it. */
393 if (next_saved_file)
395 *bufp = input_scrub_pop (next_saved_file); /* Pop state */
396 /* partial_where is now correct to return, since we popped it. */
399 return (partial_where);
402 /* The remaining part of this file deals with line numbers, error
403 messages and so on. Return TRUE if we opened any file. */
406 seen_at_least_1_file (void)
408 return (physical_input_file != NULL);
411 void
412 bump_line_counters (void)
414 if (sb_index < 0)
416 ++physical_input_line;
417 if (logical_input_line >= 0)
418 ++logical_input_line;
422 /* Tells us what the new logical line number and file are.
423 If the line_number is -1, we don't change the current logical line
424 number. If it is -2, we decrement the logical line number (this is
425 to support the .appfile pseudo-op inserted into the stream by
426 do_scrub_chars).
427 If the fname is NULL, we don't change the current logical file name.
428 Returns nonzero if the filename actually changes. */
431 new_logical_line (char *fname, /* DON'T destroy it! We point to it! */
432 int line_number)
434 if (line_number >= 0)
435 logical_input_line = line_number;
436 else if (line_number == -2 && logical_input_line > 0)
437 --logical_input_line;
439 if (fname
440 && (logical_input_file == NULL
441 || strcmp (logical_input_file, fname)))
443 logical_input_file = fname;
444 return 1;
446 else
447 return 0;
450 /* Return the current file name and line number.
451 namep should be char * const *, but there are compilers which screw
452 up declarations like that, and it's easier to avoid it. */
454 void
455 as_where (char **namep, unsigned int *linep)
457 if (logical_input_file != NULL
458 && (linep == NULL || logical_input_line >= 0))
460 *namep = logical_input_file;
461 if (linep != NULL)
462 *linep = logical_input_line;
464 else if (physical_input_file != NULL)
466 *namep = physical_input_file;
467 if (linep != NULL)
468 *linep = physical_input_line;
470 else
472 *namep = 0;
473 if (linep != NULL)
474 *linep = 0;