Subversion Repositories Kolibri OS

Rev

Rev 5222 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* input_scrub.c - Break up input buffers into whole numbers of lines.
  2.    Copyright (C) 1987-2015 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. #define BEFORE_STRING ("\n")
  53. #define AFTER_STRING ("\0")     /* memcpy of 0 chars might choke.  */
  54. #define BEFORE_SIZE (1)
  55. #define AFTER_SIZE  (1)
  56.  
  57. #ifndef TC_EOL_IN_INSN
  58. #define TC_EOL_IN_INSN(P) 0
  59. #endif
  60.  
  61. static char *buffer_start;      /*->1st char of full buffer area.  */
  62. static char *partial_where;     /*->after last full line in buffer.  */
  63. static int partial_size;        /* >=0. Number of chars in partial line in buffer.  */
  64.  
  65. /* Because we need AFTER_STRING just after last full line, it clobbers
  66.    1st part of partial line. So we preserve 1st part of partial line
  67.    here.  */
  68. static char save_source[AFTER_SIZE];
  69.  
  70. /* What is the largest size buffer that input_file_give_next_buffer()
  71.    could return to us?  */
  72. static unsigned int buffer_length;
  73.  
  74. /* The index into an sb structure we are reading from.  -1 if none.  */
  75. static size_t sb_index = -1;
  76.  
  77. /* If we are reading from an sb structure, this is it.  */
  78. static sb from_sb;
  79.  
  80. /* Should we do a conditional check on from_sb? */
  81. static int from_sb_is_expansion = 1;
  82.  
  83. /* The number of nested sb structures we have included.  */
  84. int macro_nest;
  85.  
  86. /* We can have more than one source file open at once, though the info for all
  87.    but the latest one are saved off in a struct input_save.  These files remain
  88.    open, so we are limited by the number of open files allowed by the
  89.    underlying OS. We may also sequentially read more than one source file in an
  90.    assembly.  */
  91.  
  92. /* We must track the physical file and line number for error messages. We also
  93.    track a "logical" file and line number corresponding to (C?)  compiler
  94.    source line numbers.  Whenever we open a file we must fill in
  95.    physical_input_file. So if it is NULL we have not opened any files yet.  */
  96.  
  97. static char *physical_input_file;
  98. static char *logical_input_file;
  99.  
  100. /* 1-origin line number in a source file.  */
  101. /* A line ends in '\n' or eof.  */
  102. static unsigned int physical_input_line;
  103. static int logical_input_line;
  104.  
  105. /* Struct used to save the state of the input handler during include files */
  106. struct input_save {
  107.   char *              buffer_start;
  108.   char *              partial_where;
  109.   int                 partial_size;
  110.   char                save_source[AFTER_SIZE];
  111.   size_t              buffer_length;
  112.   char *              physical_input_file;
  113.   char *              logical_input_file;
  114.   unsigned int        physical_input_line;
  115.   int                 logical_input_line;
  116.   size_t              sb_index;
  117.   sb                  from_sb;
  118.   int                 from_sb_is_expansion; /* Should we do a conditional check?  */
  119.   struct input_save * next_saved_file;  /* Chain of input_saves.  */
  120.   char *              input_file_save;  /* Saved state of input routines.  */
  121.   char *              saved_position;   /* Caller's saved position in buf.  */
  122. };
  123.  
  124. static struct input_save *input_scrub_push (char *saved_position);
  125. static char *input_scrub_pop (struct input_save *arg);
  126.  
  127. /* Saved information about the file that .include'd this one.  When we hit EOF,
  128.    we automatically pop to that file.  */
  129.  
  130. static struct input_save *next_saved_file;
  131.  
  132. /* Push the state of input reading and scrubbing so that we can #include.
  133.    The return value is a 'void *' (fudged for old compilers) to a save
  134.    area, which can be restored by passing it to input_scrub_pop().  */
  135.  
  136. static struct input_save *
  137. input_scrub_push (char *saved_position)
  138. {
  139.   struct input_save *saved;
  140.  
  141.   saved = (struct input_save *) xmalloc (sizeof *saved);
  142.  
  143.   saved->saved_position = saved_position;
  144.   saved->buffer_start = buffer_start;
  145.   saved->partial_where = partial_where;
  146.   saved->partial_size = partial_size;
  147.   saved->buffer_length = buffer_length;
  148.   saved->physical_input_file = physical_input_file;
  149.   saved->logical_input_file = logical_input_file;
  150.   saved->physical_input_line = physical_input_line;
  151.   saved->logical_input_line = logical_input_line;
  152.   saved->sb_index = sb_index;
  153.   saved->from_sb = from_sb;
  154.   saved->from_sb_is_expansion = from_sb_is_expansion;
  155.   memcpy (saved->save_source, save_source, sizeof (save_source));
  156.   saved->next_saved_file = next_saved_file;
  157.   saved->input_file_save = input_file_push ();
  158.  
  159.   input_file_begin ();          /* Reinitialize! */
  160.   logical_input_line = -1;
  161.   logical_input_file = (char *) NULL;
  162.   buffer_length = input_file_buffer_size ();
  163.   sb_index = -1;
  164.  
  165.   buffer_start = (char *) xmalloc ((BEFORE_SIZE + buffer_length
  166.                                     + buffer_length + AFTER_SIZE + 1));
  167.   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
  168.  
  169.   return saved;
  170. }
  171.  
  172. static char *
  173. input_scrub_pop (struct input_save *saved)
  174. {
  175.   char *saved_position;
  176.  
  177.   input_scrub_end ();           /* Finish off old buffer */
  178.  
  179.   input_file_pop (saved->input_file_save);
  180.   saved_position = saved->saved_position;
  181.   buffer_start = saved->buffer_start;
  182.   buffer_length = saved->buffer_length;
  183.   physical_input_file = saved->physical_input_file;
  184.   logical_input_file = saved->logical_input_file;
  185.   physical_input_line = saved->physical_input_line;
  186.   logical_input_line = saved->logical_input_line;
  187.   sb_index = saved->sb_index;
  188.   from_sb = saved->from_sb;
  189.   from_sb_is_expansion = saved->from_sb_is_expansion;
  190.   partial_where = saved->partial_where;
  191.   partial_size = saved->partial_size;
  192.   next_saved_file = saved->next_saved_file;
  193.   memcpy (save_source, saved->save_source, sizeof (save_source));
  194.  
  195.   free (saved);
  196.   return saved_position;
  197. }
  198. void
  199. input_scrub_begin (void)
  200. {
  201.   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
  202.   know (strlen (AFTER_STRING) == AFTER_SIZE
  203.         || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
  204.  
  205.   input_file_begin ();
  206.  
  207.   buffer_length = input_file_buffer_size ();
  208.  
  209.   buffer_start = (char *) xmalloc ((BEFORE_SIZE + buffer_length
  210.                                     + buffer_length + AFTER_SIZE + 1));
  211.   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
  212.  
  213.   /* Line number things.  */
  214.   logical_input_line = -1;
  215.   logical_input_file = (char *) NULL;
  216.   physical_input_file = NULL;   /* No file read yet.  */
  217.   next_saved_file = NULL;       /* At EOF, don't pop to any other file */
  218.   do_scrub_begin (flag_m68k_mri);
  219. }
  220.  
  221. void
  222. input_scrub_end (void)
  223. {
  224.   if (buffer_start)
  225.     {
  226.       free (buffer_start);
  227.       buffer_start = 0;
  228.       input_file_end ();
  229.     }
  230. }
  231.  
  232. /* Start reading input from a new file.
  233.    Return start of caller's part of buffer.  */
  234.  
  235. char *
  236. input_scrub_new_file (char *filename)
  237. {
  238.   input_file_open (filename, !flag_no_comments);
  239.   physical_input_file = filename[0] ? filename : _("{standard input}");
  240.   physical_input_line = 0;
  241.  
  242.   partial_size = 0;
  243.   return (buffer_start + BEFORE_SIZE);
  244. }
  245.  
  246. /* Include a file from the current file.  Save our state, cause it to
  247.    be restored on EOF, and begin handling a new file.  Same result as
  248.    input_scrub_new_file.  */
  249.  
  250. char *
  251. input_scrub_include_file (char *filename, char *position)
  252. {
  253.   next_saved_file = input_scrub_push (position);
  254.   return input_scrub_new_file (filename);
  255. }
  256.  
  257. /* Start getting input from an sb structure.  This is used when
  258.    expanding a macro.  */
  259.  
  260. void
  261. input_scrub_include_sb (sb *from, char *position, int is_expansion)
  262. {
  263.   int newline;
  264.  
  265.   if (macro_nest > max_macro_nest)
  266.     as_fatal (_("macros nested too deeply"));
  267.   ++macro_nest;
  268.  
  269. #ifdef md_macro_start
  270.   if (is_expansion)
  271.     {
  272.       md_macro_start ();
  273.     }
  274. #endif
  275.  
  276.   next_saved_file = input_scrub_push (position);
  277.  
  278.   /* Allocate sufficient space: from->len + optional newline.  */
  279.   newline = from->len >= 1 && from->ptr[0] != '\n';
  280.   sb_build (&from_sb, from->len + newline);
  281.   from_sb_is_expansion = is_expansion;
  282.   if (newline)
  283.     {
  284.       /* Add the sentinel required by read.c.  */
  285.       sb_add_char (&from_sb, '\n');
  286.     }
  287.   sb_scrub_and_add_sb (&from_sb, from);
  288.  
  289.   /* Make sure the parser looks at defined contents when it scans for
  290.      e.g. end-of-line at the end of a macro.  */
  291.   sb_terminate (&from_sb);
  292.  
  293.   sb_index = 1;
  294.  
  295.   /* These variables are reset by input_scrub_push.  Restore them
  296.      since we are, after all, still at the same point in the file.  */
  297.   logical_input_line = next_saved_file->logical_input_line;
  298.   logical_input_file = next_saved_file->logical_input_file;
  299. }
  300.  
  301. void
  302. input_scrub_close (void)
  303. {
  304.   input_file_close ();
  305.   physical_input_line = 0;
  306.   logical_input_line = -1;
  307. }
  308.  
  309. char *
  310. input_scrub_next_buffer (char **bufp)
  311. {
  312.   char *limit;          /*->just after last char of buffer.  */
  313.  
  314.   if (sb_index != (size_t) -1)
  315.     {
  316.       if (sb_index >= from_sb.len)
  317.         {
  318.           sb_kill (&from_sb);
  319.           if (from_sb_is_expansion)
  320.             {
  321.               cond_finish_check (macro_nest);
  322. #ifdef md_macro_end
  323.               /* Allow the target to clean up per-macro expansion
  324.                  data.  */
  325.               md_macro_end ();
  326. #endif
  327.             }
  328.           --macro_nest;
  329.           partial_where = NULL;
  330.           partial_size = 0;
  331.           if (next_saved_file != NULL)
  332.             *bufp = input_scrub_pop (next_saved_file);
  333.           return partial_where;
  334.         }
  335.  
  336.       partial_where = from_sb.ptr + from_sb.len;
  337.       partial_size = 0;
  338.       *bufp = from_sb.ptr + sb_index;
  339.       sb_index = from_sb.len;
  340.       return partial_where;
  341.     }
  342.  
  343.   if (partial_size)
  344.     {
  345.       memmove (buffer_start + BEFORE_SIZE, partial_where,
  346.                (unsigned int) partial_size);
  347.       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
  348.     }
  349.  
  350.   while (1)
  351.     {
  352.       char *p;
  353.  
  354.       *bufp = buffer_start + BEFORE_SIZE;
  355.   limit = input_file_give_next_buffer (buffer_start
  356.                                        + BEFORE_SIZE
  357.                                        + partial_size);
  358.       if (!limit)
  359.     {
  360.           if (partial_size == 0)
  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.             ;
  376.           ++p;
  377.         }
  378.  
  379.       if (p != buffer_start + BEFORE_SIZE)
  380.         {
  381.       partial_where = p;
  382.       partial_size = limit - p;
  383.       memcpy (save_source, partial_where, (int) AFTER_SIZE);
  384.       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
  385.           return partial_where;
  386.     }
  387.  
  388.       partial_size = limit - (buffer_start + BEFORE_SIZE);
  389.       buffer_length += input_file_buffer_size ();
  390.       buffer_start = (char *) xrealloc (buffer_start,
  391.                                         (BEFORE_SIZE
  392.                                          + 2 * buffer_length
  393.                                          + AFTER_SIZE + 1));
  394.         }
  395.  
  396.       /* Tell the listing we've finished the file.  */
  397.       LISTING_EOF ();
  398.  
  399.       /* If we should pop to another file at EOF, do it.  */
  400.   partial_where = NULL;
  401.       if (next_saved_file)
  402.     *bufp = input_scrub_pop (next_saved_file);
  403.  
  404.   return partial_where;
  405. }
  406. /* The remaining part of this file deals with line numbers, error
  407.    messages and so on.  Return TRUE if we opened any file.  */
  408.  
  409. int
  410. seen_at_least_1_file (void)
  411. {
  412.   return (physical_input_file != NULL);
  413. }
  414.  
  415. void
  416. bump_line_counters (void)
  417. {
  418.   if (sb_index == (size_t) -1)
  419.     {
  420.       ++physical_input_line;
  421.       if (logical_input_line >= 0)
  422.         ++logical_input_line;
  423.     }
  424. }
  425. /* Tells us what the new logical line number and file are.
  426.    If the line_number is -1, we don't change the current logical line
  427.    number.  If it is -2, we decrement the logical line number (this is
  428.    to support the .appfile pseudo-op inserted into the stream by
  429.    do_scrub_chars).
  430.    If the fname is NULL, we don't change the current logical file name.
  431.    Returns nonzero if the filename actually changes.  */
  432.  
  433. int
  434. new_logical_line_flags (char *fname, /* DON'T destroy it!  We point to it!  */
  435.                         int line_number,
  436.                         int flags)
  437. {
  438.   switch (flags)
  439.     {
  440.     case 0:
  441.       break;
  442.     case 1:
  443.       if (line_number != -1)
  444.         abort ();
  445.       break;
  446.     case 1 << 1:
  447.     case 1 << 2:
  448.       /* FIXME: we could check that include nesting is correct.  */
  449.       break;
  450.     default:
  451.       abort ();
  452.     }
  453.  
  454.   if (line_number >= 0)
  455.     logical_input_line = line_number;
  456.   else if (line_number == -1 && fname && !*fname && (flags & (1 << 2)))
  457.     {
  458.       logical_input_file = physical_input_file;
  459.       logical_input_line = physical_input_line;
  460.       fname = NULL;
  461.     }
  462.  
  463.   if (fname
  464.       && (logical_input_file == NULL
  465.           || filename_cmp (logical_input_file, fname)))
  466.     {
  467.       logical_input_file = fname;
  468.       return 1;
  469.     }
  470.   else
  471.     return 0;
  472. }
  473.  
  474. int
  475. new_logical_line (char *fname, int line_number)
  476. {
  477.   return new_logical_line_flags (fname, line_number, 0);
  478. }
  479.  
  480. /* Return the current file name and line number.
  481.    namep should be char * const *, but there are compilers which screw
  482.    up declarations like that, and it's easier to avoid it.  */
  483.  
  484. void
  485. as_where (char **namep, unsigned int *linep)
  486. {
  487.   if (logical_input_file != NULL
  488.       && (linep == NULL || logical_input_line >= 0))
  489.     {
  490.       *namep = logical_input_file;
  491.       if (linep != NULL)
  492.         *linep = logical_input_line;
  493.     }
  494.   else if (physical_input_file != NULL)
  495.     {
  496.       *namep = physical_input_file;
  497.       if (linep != NULL)
  498.         *linep = physical_input_line;
  499.     }
  500.   else
  501.     {
  502.       *namep = 0;
  503.       if (linep != NULL)
  504.         *linep = 0;
  505.     }
  506. }
  507.