Back to index

cell-binutils  2.17cvs20070401
input-scrub.c
Go to the documentation of this file.
00001 /* input_scrub.c - Break up input buffers into whole numbers of lines.
00002    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
00003    2000, 2001, 2003, 2006, 2007
00004    Free Software Foundation, Inc.
00005 
00006    This file is part of GAS, the GNU Assembler.
00007 
00008    GAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GAS; see the file COPYING.  If not, write to the Free
00020    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 #include "as.h"
00024 #include "input-file.h"
00025 #include "sb.h"
00026 #include "listing.h"
00027 
00028 /*
00029  * O/S independent module to supply buffers of sanitised source code
00030  * to rest of assembler.  We get sanitised input data of arbitrary length.
00031  * We break these buffers on line boundaries, recombine pieces that
00032  * were broken across buffers, and return a buffer of full lines to
00033  * the caller.
00034  * The last partial line begins the next buffer we build and return to caller.
00035  * The buffer returned to caller is preceded by BEFORE_STRING and followed
00036  * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
00037  * is a newline.
00038  * Also looks after line numbers, for e.g. error messages.
00039  */
00040 
00041 /*
00042  * We don't care how filthy our buffers are, but our callers assume
00043  * that the following sanitation has already been done.
00044  *
00045  * No comments, reduce a comment to a space.
00046  * Reduce a tab to a space unless it is 1st char of line.
00047  * All multiple tabs and spaces collapsed into 1 char. Tab only
00048  *   legal if 1st char of line.
00049  * # line file statements converted to .line x;.file y; statements.
00050  * Escaped newlines at end of line: remove them but add as many newlines
00051  *   to end of statement as you removed in the middle, to synch line numbers.
00052  */
00053 
00054 #define BEFORE_STRING ("\n")
00055 #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke.  */
00056 #define BEFORE_SIZE (1)
00057 #define AFTER_SIZE  (1)
00058 
00059 #ifndef TC_EOL_IN_INSN
00060 #define TC_EOL_IN_INSN(P) 0
00061 #endif
00062 
00063 static char *buffer_start;  /*->1st char of full buffer area.  */
00064 static char *partial_where; /*->after last full line in buffer.  */
00065 static int partial_size;    /* >=0. Number of chars in partial line in buffer.  */
00066 
00067 /* Because we need AFTER_STRING just after last full line, it clobbers
00068    1st part of partial line. So we preserve 1st part of partial line
00069    here.  */
00070 static char save_source[AFTER_SIZE];
00071 
00072 /* What is the largest size buffer that input_file_give_next_buffer()
00073    could return to us?  */
00074 static unsigned int buffer_length;
00075 
00076 /* The index into an sb structure we are reading from.  -1 if none.  */
00077 static int sb_index = -1;
00078 
00079 /* If we are reading from an sb structure, this is it.  */
00080 static sb from_sb;
00081 
00082 /* Should we do a conditional check on from_sb? */
00083 static int from_sb_is_expansion = 1;
00084 
00085 /* The number of nested sb structures we have included.  */
00086 int macro_nest;
00087 
00088 /* We can have more than one source file open at once, though the info for all
00089    but the latest one are saved off in a struct input_save.  These files remain
00090    open, so we are limited by the number of open files allowed by the
00091    underlying OS. We may also sequentially read more than one source file in an
00092    assembly.  */
00093 
00094 /* We must track the physical file and line number for error messages. We also
00095    track a "logical" file and line number corresponding to (C?)  compiler
00096    source line numbers.  Whenever we open a file we must fill in
00097    physical_input_file. So if it is NULL we have not opened any files yet.  */
00098 
00099 static char *physical_input_file;
00100 static char *logical_input_file;
00101 
00102 typedef unsigned int line_numberT; /* 1-origin line number in a source file.  */
00103 /* A line ends in '\n' or eof.  */
00104 
00105 static line_numberT physical_input_line;
00106 static int logical_input_line;
00107 
00108 /* Struct used to save the state of the input handler during include files */
00109 struct input_save {
00110   char *              buffer_start;
00111   char *              partial_where;
00112   int                 partial_size;
00113   char                save_source[AFTER_SIZE];
00114   unsigned int        buffer_length;
00115   char *              physical_input_file;
00116   char *              logical_input_file;
00117   line_numberT        physical_input_line;
00118   int                 logical_input_line;
00119   int                 sb_index;
00120   sb                  from_sb;
00121   int                 from_sb_is_expansion; /* Should we do a conditional check?  */
00122   struct input_save * next_saved_file;    /* Chain of input_saves.  */
00123   char *              input_file_save;    /* Saved state of input routines.  */
00124   char *              saved_position;     /* Caller's saved position in buf.  */
00125 };
00126 
00127 static struct input_save *input_scrub_push (char *saved_position);
00128 static char *input_scrub_pop (struct input_save *arg);
00129 
00130 /* Saved information about the file that .include'd this one.  When we hit EOF,
00131    we automatically pop to that file.  */
00132 
00133 static struct input_save *next_saved_file;
00134 
00135 /* Push the state of input reading and scrubbing so that we can #include.
00136    The return value is a 'void *' (fudged for old compilers) to a save
00137    area, which can be restored by passing it to input_scrub_pop().  */
00138 
00139 static struct input_save *
00140 input_scrub_push (char *saved_position)
00141 {
00142   register struct input_save *saved;
00143 
00144   saved = (struct input_save *) xmalloc (sizeof *saved);
00145 
00146   saved->saved_position = saved_position;
00147   saved->buffer_start = buffer_start;
00148   saved->partial_where = partial_where;
00149   saved->partial_size = partial_size;
00150   saved->buffer_length = buffer_length;
00151   saved->physical_input_file = physical_input_file;
00152   saved->logical_input_file = logical_input_file;
00153   saved->physical_input_line = physical_input_line;
00154   saved->logical_input_line = logical_input_line;
00155   saved->sb_index = sb_index;
00156   saved->from_sb = from_sb;
00157   saved->from_sb_is_expansion = from_sb_is_expansion;
00158   memcpy (saved->save_source, save_source, sizeof (save_source));
00159   saved->next_saved_file = next_saved_file;
00160   saved->input_file_save = input_file_push ();
00161 
00162   input_file_begin ();             /* Reinitialize! */
00163   logical_input_line = -1;
00164   logical_input_file = (char *) NULL;
00165   buffer_length = input_file_buffer_size ();
00166   sb_index = -1;
00167 
00168   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
00169   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
00170 
00171   return saved;
00172 }
00173 
00174 static char *
00175 input_scrub_pop (struct input_save *saved)
00176 {
00177   char *saved_position;
00178 
00179   input_scrub_end ();              /* Finish off old buffer */
00180 
00181   input_file_pop (saved->input_file_save);
00182   saved_position = saved->saved_position;
00183   buffer_start = saved->buffer_start;
00184   buffer_length = saved->buffer_length;
00185   physical_input_file = saved->physical_input_file;
00186   logical_input_file = saved->logical_input_file;
00187   physical_input_line = saved->physical_input_line;
00188   logical_input_line = saved->logical_input_line;
00189   sb_index = saved->sb_index;
00190   from_sb = saved->from_sb;
00191   from_sb_is_expansion = saved->from_sb_is_expansion;
00192   partial_where = saved->partial_where;
00193   partial_size = saved->partial_size;
00194   next_saved_file = saved->next_saved_file;
00195   memcpy (save_source, saved->save_source, sizeof (save_source));
00196 
00197   free (saved);
00198   return saved_position;
00199 }
00200 
00201 void
00202 input_scrub_begin (void)
00203 {
00204   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
00205   know (strlen (AFTER_STRING) == AFTER_SIZE
00206        || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
00207 
00208   input_file_begin ();
00209 
00210   buffer_length = input_file_buffer_size ();
00211 
00212   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
00213   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
00214 
00215   /* Line number things.  */
00216   logical_input_line = -1;
00217   logical_input_file = (char *) NULL;
00218   physical_input_file = NULL;      /* No file read yet.  */
00219   next_saved_file = NULL;   /* At EOF, don't pop to any other file */
00220   do_scrub_begin (flag_m68k_mri);
00221 }
00222 
00223 void
00224 input_scrub_end (void)
00225 {
00226   if (buffer_start)
00227     {
00228       free (buffer_start);
00229       buffer_start = 0;
00230       input_file_end ();
00231     }
00232 }
00233 
00234 /* Start reading input from a new file.
00235    Return start of caller's part of buffer.  */
00236 
00237 char *
00238 input_scrub_new_file (char *filename)
00239 {
00240   input_file_open (filename, !flag_no_comments);
00241   physical_input_file = filename[0] ? filename : _("{standard input}");
00242   physical_input_line = 0;
00243 
00244   partial_size = 0;
00245   return (buffer_start + BEFORE_SIZE);
00246 }
00247 
00248 /* Include a file from the current file.  Save our state, cause it to
00249    be restored on EOF, and begin handling a new file.  Same result as
00250    input_scrub_new_file.  */
00251 
00252 char *
00253 input_scrub_include_file (char *filename, char *position)
00254 {
00255   next_saved_file = input_scrub_push (position);
00256   return input_scrub_new_file (filename);
00257 }
00258 
00259 /* Start getting input from an sb structure.  This is used when
00260    expanding a macro.  */
00261 
00262 void
00263 input_scrub_include_sb (sb *from, char *position, int is_expansion)
00264 {
00265   if (macro_nest > max_macro_nest)
00266     as_fatal (_("macros nested too deeply"));
00267   ++macro_nest;
00268 
00269 #ifdef md_macro_start
00270   if (is_expansion)
00271     {
00272       md_macro_start ();
00273     }
00274 #endif
00275 
00276   next_saved_file = input_scrub_push (position);
00277 
00278   sb_new (&from_sb);
00279   from_sb_is_expansion = is_expansion;
00280   if (from->len >= 1 && from->ptr[0] != '\n')
00281     {
00282       /* Add the sentinel required by read.c.  */
00283       sb_add_char (&from_sb, '\n');
00284     }
00285   sb_scrub_and_add_sb (&from_sb, from);
00286   sb_index = 1;
00287 
00288   /* These variables are reset by input_scrub_push.  Restore them
00289      since we are, after all, still at the same point in the file.  */
00290   logical_input_line = next_saved_file->logical_input_line;
00291   logical_input_file = next_saved_file->logical_input_file;
00292 }
00293 
00294 void
00295 input_scrub_close (void)
00296 {
00297   input_file_close ();
00298 }
00299 
00300 char *
00301 input_scrub_next_buffer (char **bufp)
00302 {
00303   register char *limit;            /*->just after last char of buffer.  */
00304 
00305   if (sb_index >= 0)
00306     {
00307       if (sb_index >= from_sb.len)
00308        {
00309          sb_kill (&from_sb);
00310          if (from_sb_is_expansion
00311              )
00312            {
00313              cond_finish_check (macro_nest);
00314 #ifdef md_macro_end
00315              /* Allow the target to clean up per-macro expansion
00316                 data.  */
00317              md_macro_end ();
00318 #endif
00319            }
00320          --macro_nest;
00321          partial_where = NULL;
00322          if (next_saved_file != NULL)
00323            *bufp = input_scrub_pop (next_saved_file);
00324          return partial_where;
00325        }
00326 
00327       partial_where = from_sb.ptr + from_sb.len;
00328       partial_size = 0;
00329       *bufp = from_sb.ptr + sb_index;
00330       sb_index = from_sb.len;
00331       return partial_where;
00332     }
00333 
00334   *bufp = buffer_start + BEFORE_SIZE;
00335 
00336   if (partial_size)
00337     {
00338       memcpy (buffer_start + BEFORE_SIZE, partial_where,
00339              (unsigned int) partial_size);
00340       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
00341     }
00342   limit = input_file_give_next_buffer (buffer_start
00343                                    + BEFORE_SIZE
00344                                    + partial_size);
00345   if (limit)
00346     {
00347       register char *p;            /* Find last newline.  */
00348       /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN.  */
00349       *limit = '\0';
00350       for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p)
00351        ;
00352       ++p;
00353 
00354       while (p <= buffer_start + BEFORE_SIZE)
00355        {
00356          int limoff;
00357 
00358          limoff = limit - buffer_start;
00359          buffer_length += input_file_buffer_size ();
00360          buffer_start = xrealloc (buffer_start,
00361                                (BEFORE_SIZE
00362                                 + 2 * buffer_length
00363                                 + AFTER_SIZE));
00364          *bufp = buffer_start + BEFORE_SIZE;
00365          limit = input_file_give_next_buffer (buffer_start + limoff);
00366 
00367          if (limit == NULL)
00368            {
00369              as_warn (_("partial line at end of file ignored"));
00370              partial_where = NULL;
00371              if (next_saved_file)
00372               *bufp = input_scrub_pop (next_saved_file);
00373              return NULL;
00374            }
00375 
00376          /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN.  */
00377          *limit = '\0';
00378          for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p)
00379            ;
00380          ++p;
00381        }
00382 
00383       partial_where = p;
00384       partial_size = limit - p;
00385       memcpy (save_source, partial_where, (int) AFTER_SIZE);
00386       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
00387     }
00388   else
00389     {
00390       partial_where = 0;
00391       if (partial_size > 0)
00392        {
00393          as_warn (_("partial line at end of file ignored"));
00394        }
00395 
00396       /* Tell the listing we've finished the file.  */
00397       LISTING_EOF ();
00398 
00399       /* If we should pop to another file at EOF, do it.  */
00400       if (next_saved_file)
00401        {
00402          *bufp = input_scrub_pop (next_saved_file);     /* Pop state */
00403          /* partial_where is now correct to return, since we popped it.  */
00404        }
00405     }
00406   return (partial_where);
00407 }
00408 
00409 /* The remaining part of this file deals with line numbers, error
00410    messages and so on.  Return TRUE if we opened any file.  */
00411 
00412 int
00413 seen_at_least_1_file (void)
00414 {
00415   return (physical_input_file != NULL);
00416 }
00417 
00418 void
00419 bump_line_counters (void)
00420 {
00421   if (sb_index < 0)
00422     {
00423       ++physical_input_line;
00424       if (logical_input_line >= 0)
00425        ++logical_input_line;
00426     }
00427 }
00428 
00429 /* Tells us what the new logical line number and file are.
00430    If the line_number is -1, we don't change the current logical line
00431    number.  If it is -2, we decrement the logical line number (this is
00432    to support the .appfile pseudo-op inserted into the stream by
00433    do_scrub_chars).
00434    If the fname is NULL, we don't change the current logical file name.
00435    Returns nonzero if the filename actually changes.  */
00436 
00437 int
00438 new_logical_line_flags (char *fname, /* DON'T destroy it!  We point to it!  */
00439                      int line_number,
00440                      int flags)
00441 {
00442   switch (flags)
00443     {
00444     case 0:
00445       break;
00446     case 1:
00447       if (line_number != -1)
00448        abort ();
00449       break;
00450     case 1 << 1:
00451     case 1 << 2:
00452       /* FIXME: we could check that include nesting is correct.  */
00453       break;
00454     default:
00455       abort ();
00456     }
00457 
00458   if (line_number >= 0)
00459     logical_input_line = line_number;
00460   else if (line_number == -1 && fname && !*fname && (flags & (1 << 2)))
00461     {
00462       logical_input_file = physical_input_file;
00463       logical_input_line = physical_input_line;
00464       fname = NULL;
00465     }
00466 
00467   if (fname
00468       && (logical_input_file == NULL
00469          || strcmp (logical_input_file, fname)))
00470     {
00471       logical_input_file = fname;
00472       return 1;
00473     }
00474   else
00475     return 0;
00476 }
00477 
00478 int
00479 new_logical_line (char *fname, int line_number)
00480 {
00481   return new_logical_line_flags (fname, line_number, 0);
00482 }
00483 
00484 
00485 /* Return the current file name and line number.
00486    namep should be char * const *, but there are compilers which screw
00487    up declarations like that, and it's easier to avoid it.  */
00488 
00489 void
00490 as_where (char **namep, unsigned int *linep)
00491 {
00492   if (logical_input_file != NULL
00493       && (linep == NULL || logical_input_line >= 0))
00494     {
00495       *namep = logical_input_file;
00496       if (linep != NULL)
00497        *linep = logical_input_line;
00498     }
00499   else if (physical_input_file != NULL)
00500     {
00501       *namep = physical_input_file;
00502       if (linep != NULL)
00503        *linep = physical_input_line;
00504     }
00505   else
00506     {
00507       *namep = 0;
00508       if (linep != NULL)
00509        *linep = 0;
00510     }
00511 }