]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/input-scrub.c
remove some duplicate #include's.
[thirdparty/binutils-gdb.git] / gas / input-scrub.c
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.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "input-file.h"
25 #include "sb.h"
26 #include "listing.h"
27
28 /*
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.
39 */
40
41 /*
42 * We don't care how filthy our buffers are, but our callers assume
43 * that the following sanitation has already been done.
44 *
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.
52 */
53 \f
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)
58
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. */
62
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];
67
68 /* What is the largest size buffer that input_file_give_next_buffer()
69 could return to us? */
70 static unsigned int buffer_length;
71
72 /* The index into an sb structure we are reading from. -1 if none. */
73 static int sb_index = -1;
74
75 /* If we are reading from an sb structure, this is it. */
76 static sb from_sb;
77
78 /* Should we do a conditional check on from_sb? */
79 static int from_sb_is_expansion = 1;
80
81 /* The number of nested sb structures we have included. */
82 int macro_nest;
83
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. */
89
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. */
94
95 static char *physical_input_file;
96 static char *logical_input_file;
97
98 typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
99 /* A line ends in '\n' or eof. */
100
101 static line_numberT physical_input_line;
102 static int logical_input_line;
103
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. */
121 };
122
123 static struct input_save *input_scrub_push (char *saved_position);
124 static char *input_scrub_pop (struct input_save *arg);
125
126 /* Saved information about the file that .include'd this one. When we hit EOF,
127 we automatically pop to that file. */
128
129 static struct input_save *next_saved_file;
130
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(). */
134
135 static struct input_save *
136 input_scrub_push (char *saved_position)
137 {
138 register struct input_save *saved;
139
140 saved = (struct input_save *) xmalloc (sizeof *saved);
141
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 ();
157
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;
163
164 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
165 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
166
167 return saved;
168 }
169
170 static char *
171 input_scrub_pop (struct input_save *saved)
172 {
173 char *saved_position;
174
175 input_scrub_end (); /* Finish off old buffer */
176
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));
192
193 free (saved);
194 return saved_position;
195 }
196 \f
197 void
198 input_scrub_begin (void)
199 {
200 know (strlen (BEFORE_STRING) == BEFORE_SIZE);
201 know (strlen (AFTER_STRING) == AFTER_SIZE
202 || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
203
204 input_file_begin ();
205
206 buffer_length = input_file_buffer_size ();
207
208 buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
209 memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
210
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);
217 }
218
219 void
220 input_scrub_end (void)
221 {
222 if (buffer_start)
223 {
224 free (buffer_start);
225 buffer_start = 0;
226 input_file_end ();
227 }
228 }
229
230 /* Start reading input from a new file.
231 Return start of caller's part of buffer. */
232
233 char *
234 input_scrub_new_file (char *filename)
235 {
236 input_file_open (filename, !flag_no_comments);
237 physical_input_file = filename[0] ? filename : _("{standard input}");
238 physical_input_line = 0;
239
240 partial_size = 0;
241 return (buffer_start + BEFORE_SIZE);
242 }
243
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. */
247
248 char *
249 input_scrub_include_file (char *filename, char *position)
250 {
251 next_saved_file = input_scrub_push (position);
252 return input_scrub_new_file (filename);
253 }
254
255 /* Start getting input from an sb structure. This is used when
256 expanding a macro. */
257
258 void
259 input_scrub_include_sb (sb *from, char *position, int is_expansion)
260 {
261 if (macro_nest > max_macro_nest)
262 as_fatal (_("macros nested too deeply"));
263 ++macro_nest;
264
265 #ifdef md_macro_start
266 if (is_expansion)
267 {
268 md_macro_start ();
269 }
270 #endif
271
272 next_saved_file = input_scrub_push (position);
273
274 sb_new (&from_sb);
275 from_sb_is_expansion = is_expansion;
276 if (from->len >= 1 && from->ptr[0] != '\n')
277 {
278 /* Add the sentinel required by read.c. */
279 sb_add_char (&from_sb, '\n');
280 }
281 sb_scrub_and_add_sb (&from_sb, from);
282 sb_index = 1;
283
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;
288 }
289
290 void
291 input_scrub_close (void)
292 {
293 input_file_close ();
294 }
295
296 char *
297 input_scrub_next_buffer (char **bufp)
298 {
299 register char *limit; /*->just after last char of buffer. */
300
301 if (sb_index >= 0)
302 {
303 if (sb_index >= from_sb.len)
304 {
305 sb_kill (&from_sb);
306 if (from_sb_is_expansion
307 )
308 {
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
315 }
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;
321 }
322
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;
328 }
329
330 *bufp = buffer_start + BEFORE_SIZE;
331
332 if (partial_size)
333 {
334 memcpy (buffer_start + BEFORE_SIZE, partial_where,
335 (unsigned int) partial_size);
336 memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
337 }
338 limit = input_file_give_next_buffer (buffer_start
339 + BEFORE_SIZE
340 + partial_size);
341 if (limit)
342 {
343 register char *p; /* Find last newline. */
344
345 for (p = limit - 1; *p != '\n'; --p)
346 ;
347 ++p;
348
349 while (p <= buffer_start + BEFORE_SIZE)
350 {
351 int limoff;
352
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);
361
362 if (limit == NULL)
363 {
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;
369 }
370
371 for (p = limit - 1; *p != '\n'; --p)
372 ;
373 ++p;
374 }
375
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);
380 }
381 else
382 {
383 partial_where = 0;
384 if (partial_size > 0)
385 {
386 as_warn (_("partial line at end of file ignored"));
387 }
388
389 /* Tell the listing we've finished the file. */
390 LISTING_EOF ();
391
392 /* If we should pop to another file at EOF, do it. */
393 if (next_saved_file)
394 {
395 *bufp = input_scrub_pop (next_saved_file); /* Pop state */
396 /* partial_where is now correct to return, since we popped it. */
397 }
398 }
399 return (partial_where);
400 }
401 \f
402 /* The remaining part of this file deals with line numbers, error
403 messages and so on. Return TRUE if we opened any file. */
404
405 int
406 seen_at_least_1_file (void)
407 {
408 return (physical_input_file != NULL);
409 }
410
411 void
412 bump_line_counters (void)
413 {
414 if (sb_index < 0)
415 {
416 ++physical_input_line;
417 if (logical_input_line >= 0)
418 ++logical_input_line;
419 }
420 }
421 \f
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. */
429
430 int
431 new_logical_line (char *fname, /* DON'T destroy it! We point to it! */
432 int line_number)
433 {
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;
438
439 if (fname
440 && (logical_input_file == NULL
441 || strcmp (logical_input_file, fname)))
442 {
443 logical_input_file = fname;
444 return 1;
445 }
446 else
447 return 0;
448 }
449 \f
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. */
453
454 void
455 as_where (char **namep, unsigned int *linep)
456 {
457 if (logical_input_file != NULL
458 && (linep == NULL || logical_input_line >= 0))
459 {
460 *namep = logical_input_file;
461 if (linep != NULL)
462 *linep = logical_input_line;
463 }
464 else if (physical_input_file != NULL)
465 {
466 *namep = physical_input_file;
467 if (linep != NULL)
468 *linep = physical_input_line;
469 }
470 else
471 {
472 *namep = 0;
473 if (linep != NULL)
474 *linep = 0;
475 }
476 }