1 /* input_scrub.c - Break up input buffers into whole numbers of lines.
2    Copyright (C) 1987-2020 Free Software Foundation, Inc.
3 
4    This file is part of GAS, the GNU Assembler.
5 
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to the Free
18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19    02110-1301, USA.  */
20 
21 #include "as.h"
22 #include "filenames.h"
23 #include "input-file.h"
24 #include "sb.h"
25 #include "listing.h"
26 
27 /*
28  * O/S independent module to supply buffers of sanitised source code
29  * to rest of assembler.  We get sanitised input data of arbitrary length.
30  * We break these buffers on line boundaries, recombine pieces that
31  * were broken across buffers, and return a buffer of full lines to
32  * the caller.
33  * The last partial line begins the next buffer we build and return to caller.
34  * The buffer returned to caller is preceded by BEFORE_STRING and followed
35  * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
36  * is a newline.
37  * Also looks after line numbers, for e.g. error messages.
38  */
39 
40 /*
41  * We don't care how filthy our buffers are, but our callers assume
42  * that the following sanitation has already been done.
43  *
44  * No comments, reduce a comment to a space.
45  * Reduce a tab to a space unless it is 1st char of line.
46  * All multiple tabs and spaces collapsed into 1 char. Tab only
47  *   legal if 1st char of line.
48  * # line file statements converted to .line x;.file y; statements.
49  * Escaped newlines at end of line: remove them but add as many newlines
50  *   to end of statement as you removed in the middle, to synch line numbers.
51  */
52 
53 #define BEFORE_STRING ("\n")
54 #define AFTER_STRING ("\0")	/* memcpy of 0 chars might choke.  */
55 #define BEFORE_SIZE (1)
56 #define AFTER_SIZE  (1)
57 
58 #ifndef TC_EOL_IN_INSN
59 #define TC_EOL_IN_INSN(P) 0
60 #endif
61 
62 static char *buffer_start;	/*->1st char of full buffer area.  */
63 static char *partial_where;	/*->after last full line in buffer.  */
64 static size_t partial_size;	/* >=0. Number of chars in partial line in buffer.  */
65 
66 /* Because we need AFTER_STRING just after last full line, it clobbers
67    1st part of partial line. So we preserve 1st part of partial line
68    here.  */
69 static char save_source[AFTER_SIZE];
70 
71 /* The size of the input buffer we concatenate
72    input_file_give_next_buffer chunks into.  Excludes the BEFORE and
73    AFTER counts.  */
74 static size_t buffer_length;
75 
76 /* The index into an sb structure we are reading from.  -1 if none.  */
77 static size_t sb_index = -1;
78 
79 /* If we are reading from an sb structure, this is it.  */
80 static sb from_sb;
81 
82 /* Should we do a conditional check on from_sb? */
83 static int from_sb_is_expansion = 1;
84 
85 /* The number of nested sb structures we have included.  */
86 int macro_nest;
87 
88 /* We can have more than one source file open at once, though the info for all
89    but the latest one are saved off in a struct input_save.  These files remain
90    open, so we are limited by the number of open files allowed by the
91    underlying OS. We may also sequentially read more than one source file in an
92    assembly.  */
93 
94 /* We must track the physical file and line number for error messages. We also
95    track a "logical" file and line number corresponding to (C?)  compiler
96    source line numbers.  Whenever we open a file we must fill in
97    physical_input_file. So if it is NULL we have not opened any files yet.  */
98 
99 static const char *physical_input_file;
100 static const char *logical_input_file;
101 
102 /* 1-origin line number in a source file.  */
103 /* A line ends in '\n' or eof.  */
104 static unsigned int physical_input_line;
105 static int logical_input_line;
106 
107 /* Struct used to save the state of the input handler during include files */
108 struct input_save {
109   char *              buffer_start;
110   char *              partial_where;
111   size_t              partial_size;
112   char                save_source[AFTER_SIZE];
113   size_t              buffer_length;
114   const char *              physical_input_file;
115   const char *              logical_input_file;
116   unsigned int        physical_input_line;
117   int                 logical_input_line;
118   size_t              sb_index;
119   sb                  from_sb;
120   int                 from_sb_is_expansion; /* Should we do a conditional check?  */
121   struct input_save * next_saved_file;	/* Chain of input_saves.  */
122   char *              input_file_save;	/* Saved state of input routines.  */
123   char *              saved_position;	/* Caller's saved position in buf.  */
124 };
125 
126 static struct input_save *input_scrub_push (char *saved_position);
127 static char *input_scrub_pop (struct input_save *arg);
128 
129 /* Saved information about the file that .include'd this one.  When we hit EOF,
130    we automatically pop to that file.  */
131 
132 static struct input_save *next_saved_file;
133 
134 /* Initialize input buffering.  */
135 
136 static void
137 input_scrub_reinit (void)
138 {
139   input_file_begin ();		/* Reinitialize! */
140   logical_input_line = -1;
141   logical_input_file = NULL;
142 
143   buffer_length = input_file_buffer_size () * 2;
144   buffer_start = XNEWVEC (char, BEFORE_SIZE + AFTER_SIZE + 1 + buffer_length);
145   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
146 }
147 
148 /* Push the state of input reading and scrubbing so that we can #include.
149    The return value is a 'void *' (fudged for old compilers) to a save
150    area, which can be restored by passing it to input_scrub_pop().  */
151 
152 static struct input_save *
153 input_scrub_push (char *saved_position)
154 {
155   struct input_save *saved;
156 
157   saved = XNEW (struct input_save);
158 
159   saved->saved_position = saved_position;
160   saved->buffer_start = buffer_start;
161   saved->partial_where = partial_where;
162   saved->partial_size = partial_size;
163   saved->buffer_length = buffer_length;
164   saved->physical_input_file = physical_input_file;
165   saved->logical_input_file = logical_input_file;
166   saved->physical_input_line = physical_input_line;
167   saved->logical_input_line = logical_input_line;
168   saved->sb_index = sb_index;
169   saved->from_sb = from_sb;
170   saved->from_sb_is_expansion = from_sb_is_expansion;
171   memcpy (saved->save_source, save_source, sizeof (save_source));
172   saved->next_saved_file = next_saved_file;
173   saved->input_file_save = input_file_push ();
174 
175   sb_index = -1;
176 
177   input_scrub_reinit ();
178 
179   return saved;
180 }
181 
182 static char *
183 input_scrub_pop (struct input_save *saved)
184 {
185   char *saved_position;
186 
187   input_scrub_end ();		/* Finish off old buffer */
188 
189   input_file_pop (saved->input_file_save);
190   saved_position = saved->saved_position;
191   buffer_start = saved->buffer_start;
192   buffer_length = saved->buffer_length;
193   physical_input_file = saved->physical_input_file;
194   logical_input_file = saved->logical_input_file;
195   physical_input_line = saved->physical_input_line;
196   logical_input_line = saved->logical_input_line;
197   sb_index = saved->sb_index;
198   from_sb = saved->from_sb;
199   from_sb_is_expansion = saved->from_sb_is_expansion;
200   partial_where = saved->partial_where;
201   partial_size = saved->partial_size;
202   next_saved_file = saved->next_saved_file;
203   memcpy (save_source, saved->save_source, sizeof (save_source));
204 
205   free (saved);
206   return saved_position;
207 }
208 
209 void
210 input_scrub_begin (void)
211 {
212   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
213   know (strlen (AFTER_STRING) == AFTER_SIZE
214 	|| (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
215 
216   physical_input_file = NULL;	/* No file read yet.  */
217   next_saved_file = NULL;	/* At EOF, don't pop to any other file */
218   input_scrub_reinit ();
219   do_scrub_begin (flag_m68k_mri);
220 }
221 
222 void
223 input_scrub_end (void)
224 {
225   if (buffer_start)
226     {
227       free (buffer_start);
228       buffer_start = 0;
229       input_file_end ();
230     }
231 }
232 
233 /* Start reading input from a new file.
234    Return start of caller's part of buffer.  */
235 
236 char *
237 input_scrub_new_file (const char *filename)
238 {
239   input_file_open (filename, !flag_no_comments);
240   physical_input_file = filename[0] ? filename : _("{standard input}");
241   physical_input_line = 0;
242 
243   partial_size = 0;
244   return (buffer_start + BEFORE_SIZE);
245 }
246 
247 /* Include a file from the current file.  Save our state, cause it to
248    be restored on EOF, and begin handling a new file.  Same result as
249    input_scrub_new_file.  */
250 
251 char *
252 input_scrub_include_file (const char *filename, char *position)
253 {
254   next_saved_file = input_scrub_push (position);
255   return input_scrub_new_file (filename);
256 }
257 
258 /* Start getting input from an sb structure.  This is used when
259    expanding a macro.  */
260 
261 void
262 input_scrub_include_sb (sb *from, char *position, int is_expansion)
263 {
264   int newline;
265 
266   if (macro_nest > max_macro_nest)
267     as_fatal (_("macros nested too deeply"));
268   ++macro_nest;
269 
270 #ifdef md_macro_start
271   if (is_expansion)
272     {
273       md_macro_start ();
274     }
275 #endif
276 
277   next_saved_file = input_scrub_push (position);
278 
279   /* Allocate sufficient space: from->len + optional newline.  */
280   newline = from->len >= 1 && from->ptr[0] != '\n';
281   sb_build (&from_sb, from->len + newline);
282   from_sb_is_expansion = is_expansion;
283   if (newline)
284     {
285       /* Add the sentinel required by read.c.  */
286       sb_add_char (&from_sb, '\n');
287     }
288   sb_scrub_and_add_sb (&from_sb, from);
289 
290   /* Make sure the parser looks at defined contents when it scans for
291      e.g. end-of-line at the end of a macro.  */
292   sb_terminate (&from_sb);
293 
294   sb_index = 1;
295 
296   /* These variables are reset by input_scrub_push.  Restore them
297      since we are, after all, still at the same point in the file.  */
298   logical_input_line = next_saved_file->logical_input_line;
299   logical_input_file = next_saved_file->logical_input_file;
300 }
301 
302 void
303 input_scrub_close (void)
304 {
305   input_file_close ();
306   physical_input_line = 0;
307   logical_input_line = -1;
308 }
309 
310 char *
311 input_scrub_next_buffer (char **bufp)
312 {
313   char *limit;		/*->just after last char of buffer.  */
314 
315   if (sb_index != (size_t) -1)
316     {
317       if (sb_index >= from_sb.len)
318 	{
319 	  sb_kill (&from_sb);
320 	  if (from_sb_is_expansion)
321 	    {
322 	      cond_finish_check (macro_nest);
323 #ifdef md_macro_end
324 	      /* Allow the target to clean up per-macro expansion
325 	         data.  */
326 	      md_macro_end ();
327 #endif
328 	    }
329 	  --macro_nest;
330 	  partial_where = NULL;
331 	  partial_size = 0;
332 	  if (next_saved_file != NULL)
333 	    *bufp = input_scrub_pop (next_saved_file);
334 	  return partial_where;
335 	}
336 
337       partial_where = from_sb.ptr + from_sb.len;
338       partial_size = 0;
339       *bufp = from_sb.ptr + sb_index;
340       sb_index = from_sb.len;
341       return partial_where;
342     }
343 
344   if (partial_size)
345     {
346       memmove (buffer_start + BEFORE_SIZE, partial_where, partial_size);
347       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
348     }
349 
350   while (1)
351     {
352       char *p;
353       char *start = buffer_start + BEFORE_SIZE + partial_size;
354 
355       *bufp = buffer_start + BEFORE_SIZE;
356       limit = input_file_give_next_buffer (start);
357       if (!limit)
358 	{
359 	  if (!partial_size)
360 	    /* End of this file.  */
361 	    break;
362 
363 	  as_warn (_("end of file not at end of a line; newline inserted"));
364 	  p = buffer_start + BEFORE_SIZE + partial_size;
365 	  *p++ = '\n';
366 	  limit = p;
367 	}
368       else
369 	{
370 	  /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN.  */
371 	  *limit = '\0';
372 
373 	  /* Find last newline.  */
374 	  for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p)
375 	    if (p < start)
376 	      goto read_more;
377 	  ++p;
378 	}
379 
380       /* We found a newline in the newly read chars.  */
381       partial_where = p;
382       partial_size = limit - p;
383 
384       /* Save the fragment after that last newline.  */
385       memcpy (save_source, partial_where, (int) AFTER_SIZE);
386       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
387       return partial_where;
388 
389     read_more:
390       /* Didn't find a newline.  Read more text.  */
391       partial_size = limit - (buffer_start + BEFORE_SIZE);
392       if (buffer_length - input_file_buffer_size () < partial_size)
393 	{
394 	  /* Increase the buffer when it doesn't have room for the
395 	     next block of input.  */
396 	  buffer_length *= 2;
397 	  buffer_start = XRESIZEVEC (char, buffer_start,
398 				     (buffer_length
399 				      + BEFORE_SIZE + AFTER_SIZE + 1));
400 	}
401     }
402 
403   /* Tell the listing we've finished the file.  */
404   LISTING_EOF ();
405 
406   /* If we should pop to another file at EOF, do it.  */
407   partial_where = NULL;
408   if (next_saved_file)
409     *bufp = input_scrub_pop (next_saved_file);
410 
411   return partial_where;
412 }
413 
414 /* The remaining part of this file deals with line numbers, error
415    messages and so on.  Return TRUE if we opened any file.  */
416 
417 int
418 seen_at_least_1_file (void)
419 {
420   return (physical_input_file != NULL);
421 }
422 
423 void
424 bump_line_counters (void)
425 {
426   if (sb_index == (size_t) -1)
427     {
428       ++physical_input_line;
429       if (logical_input_line >= 0)
430 	++logical_input_line;
431     }
432 }
433 
434 /* Tells us what the new logical line number and file are.
435    If the line_number is -1, we don't change the current logical line
436    number.  If it is -2, we decrement the logical line number (this is
437    to support the .appfile pseudo-op inserted into the stream by
438    do_scrub_chars).
439    If the fname is NULL, we don't change the current logical file name.
440    Returns nonzero if the filename actually changes.  */
441 
442 int
443 new_logical_line_flags (const char *fname, /* DON'T destroy it!  We point to it!  */
444 			int line_number,
445 			int flags)
446 {
447   switch (flags)
448     {
449     case 0:
450       break;
451     case 1:
452       if (line_number != -1)
453 	abort ();
454       break;
455     case 1 << 1:
456     case 1 << 2:
457       /* FIXME: we could check that include nesting is correct.  */
458       break;
459     default:
460       abort ();
461     }
462 
463   if (line_number >= 0)
464     logical_input_line = line_number;
465   else if (line_number == -1 && fname && !*fname && (flags & (1 << 2)))
466     {
467       logical_input_file = physical_input_file;
468       logical_input_line = physical_input_line;
469       fname = NULL;
470     }
471 
472   if (fname
473       && (logical_input_file == NULL
474 	  || filename_cmp (logical_input_file, fname)))
475     {
476       logical_input_file = fname;
477       return 1;
478     }
479   else
480     return 0;
481 }
482 
483 int
484 new_logical_line (const char *fname, int line_number)
485 {
486   return new_logical_line_flags (fname, line_number, 0);
487 }
488 
489 
490 /* Return the current physical input file name and line number, if known  */
491 
492 const char *
493 as_where_physical (unsigned int *linep)
494 {
495   if (physical_input_file != NULL)
496     {
497       if (linep != NULL)
498 	*linep = physical_input_line;
499       return physical_input_file;
500     }
501 
502   if (linep != NULL)
503     *linep = 0;
504   return NULL;
505 }
506 
507 /* Return the current file name and line number.  */
508 
509 const char *
510 as_where (unsigned int *linep)
511 {
512   if (logical_input_file != NULL
513       && (linep == NULL || logical_input_line >= 0))
514     {
515       if (linep != NULL)
516 	*linep = logical_input_line;
517       return logical_input_file;
518     }
519 
520   return as_where_physical (linep);
521 }
522 
523