Commit 812b1403 by David Malcolm Committed by David Malcolm

Introduce class rtx_reader

Bundle up various global variables within gensupport.c into a
class rtx_reader, with a view towards making it easier to run the
code more than once in-process.

gcc/ChangeLog:
	* genconstants.c (main): Introduce noop_reader and convert call
	to read_md_files to a method call.
	* genenums.c (main): Likewise.
	* genmddeps.c (main): Likewise.
	* genpreds.c (write_tm_constrs_h): Replace use of "in_fname" with
	rtx_reader_ptr->get_top_level_filename ().
	(write_tm_preds_h): Likewise.
	(write_insn_preds_c): Likewise.
	* gensupport.c (class gen_reader): New subclass of rtx_reader.
	(rtx_handle_directive): Convert to...
	(gen_reader::handle_unknown_directive): ...this.
	(init_rtx_reader_args_cb): Convert return type from bool to
	rtx_reader *.  Create a gen_reader instance, using it for the
	call to read_md_files.  Return it if no errors occur.
	(init_rtx_reader_args): Convert return type from bool to
	rtx_reader *.
	* gensupport.h (init_rtx_reader_args_cb): Likewise.
	(init_rtx_reader_args_cb): Likewise.
	* read-md.c (struct file_name_list): Move to class rtx_reader.
	(read_md_file): Delete in favor of rtx_reader::m_read_md_file.
	(read_md_filename): Delete in favor of
	rtx_reader::m_read_md_filename.
	(read_md_lineno): Delete in favor of rtx_reader::m_read_md_lineno.
	(in_fname): Delete in favor of rtx_reader::m_toplevel_fname.
	(base_dir): Delete in favor of rtx_reader::m_base_dir.
	(first_dir_md_include): Delete in favor of
	rtx_reader::m_first_dir_md_include.
	(last_dir_md_include_ptr): Delete in favor of
	rtx_reader::m_last_dir_md_include_ptr.
	(max_include_len): Delete.
	(rtx_reader_ptr): New.
	(fatal_with_file_and_line): Use get_filename and get_lineno
	accessors of rtx_reader_ptr.
	(require_char_ws): Likewise.
	(rtx_reader::read_char): New method, based on ::read_char.
	(rtx_reader::unread_char): New method, based on ::unread_char.
	(read_escape): Use get_filename and get_lineno accessors of
	rtx_reader_ptr.
	(read_braced_string): Use get_lineno accessor of rtx_reader_ptr.
	(read_string): Use get_filename and get_lineno accessors of
	rtx_reader_ptr.
	(rtx_reader::rtx_reader): New ctor.
	(rtx_reader::~rtx_reader): New dtor.
	(handle_include): Convert from a function to...
	(rtx_reader::handle_include): ...this method, converting
	handle_directive from a callback to a virtual function.
	(handle_file): Likewise, converting to...
	(rtx_reader::handle_file): ...this method.
	(handle_toplevel_file): Likewise, converting to...
	(rtx_reader::handle_toplevel_file): ...this method.
	(rtx_reader::get_current_location): New method.
	(parse_include): Convert from a function to...
	(rtx_reader::add_include_path): ...this method, dropping redundant
	update to unused max_include_len.
	(read_md_files): Convert from a function to...
	(rtx_reader::read_md_files): ...this method, converting
	handle_directive from a callback to a virtual function.
	(noop_reader::handle_unknown_directive): New method.
	* read-md.h (directive_handler_t): Delete this typedef.
	(in_fname): Delete.
	(read_md_file): Delete.
	(read_md_lineno): Delete.
	(read_md_filename): Delete.
	(class rtx_reader): New class.
	(rtx_reader_ptr): New decl.
	(class noop_reader): New subclass of rtx_reader.
	(read_char): Reimplement in terms of rtx_reader::read_char.
	(unread_char): Reimplement in terms of rtx_reader::unread_char.
	(read_md_files): Delete.
	* read-rtl.c (read_rtx_code): Update for deletion of globals
	read_md_filename and read_md_lineno.

From-SVN: r240333
parent 0d715437
2016-09-21 David Malcolm <dmalcolm@redhat.com>
* genconstants.c (main): Introduce noop_reader and convert call
to read_md_files to a method call.
* genenums.c (main): Likewise.
* genmddeps.c (main): Likewise.
* genpreds.c (write_tm_constrs_h): Replace use of "in_fname" with
rtx_reader_ptr->get_top_level_filename ().
(write_tm_preds_h): Likewise.
(write_insn_preds_c): Likewise.
* gensupport.c (class gen_reader): New subclass of rtx_reader.
(rtx_handle_directive): Convert to...
(gen_reader::handle_unknown_directive): ...this.
(init_rtx_reader_args_cb): Convert return type from bool to
rtx_reader *. Create a gen_reader instance, using it for the
call to read_md_files. Return it if no errors occur.
(init_rtx_reader_args): Convert return type from bool to
rtx_reader *.
* gensupport.h (init_rtx_reader_args_cb): Likewise.
(init_rtx_reader_args_cb): Likewise.
* read-md.c (struct file_name_list): Move to class rtx_reader.
(read_md_file): Delete in favor of rtx_reader::m_read_md_file.
(read_md_filename): Delete in favor of
rtx_reader::m_read_md_filename.
(read_md_lineno): Delete in favor of rtx_reader::m_read_md_lineno.
(in_fname): Delete in favor of rtx_reader::m_toplevel_fname.
(base_dir): Delete in favor of rtx_reader::m_base_dir.
(first_dir_md_include): Delete in favor of
rtx_reader::m_first_dir_md_include.
(last_dir_md_include_ptr): Delete in favor of
rtx_reader::m_last_dir_md_include_ptr.
(max_include_len): Delete.
(rtx_reader_ptr): New.
(fatal_with_file_and_line): Use get_filename and get_lineno
accessors of rtx_reader_ptr.
(require_char_ws): Likewise.
(rtx_reader::read_char): New method, based on ::read_char.
(rtx_reader::unread_char): New method, based on ::unread_char.
(read_escape): Use get_filename and get_lineno accessors of
rtx_reader_ptr.
(read_braced_string): Use get_lineno accessor of rtx_reader_ptr.
(read_string): Use get_filename and get_lineno accessors of
rtx_reader_ptr.
(rtx_reader::rtx_reader): New ctor.
(rtx_reader::~rtx_reader): New dtor.
(handle_include): Convert from a function to...
(rtx_reader::handle_include): ...this method, converting
handle_directive from a callback to a virtual function.
(handle_file): Likewise, converting to...
(rtx_reader::handle_file): ...this method.
(handle_toplevel_file): Likewise, converting to...
(rtx_reader::handle_toplevel_file): ...this method.
(rtx_reader::get_current_location): New method.
(parse_include): Convert from a function to...
(rtx_reader::add_include_path): ...this method, dropping redundant
update to unused max_include_len.
(read_md_files): Convert from a function to...
(rtx_reader::read_md_files): ...this method, converting
handle_directive from a callback to a virtual function.
(noop_reader::handle_unknown_directive): New method.
* read-md.h (directive_handler_t): Delete this typedef.
(in_fname): Delete.
(read_md_file): Delete.
(read_md_lineno): Delete.
(read_md_filename): Delete.
(class rtx_reader): New class.
(rtx_reader_ptr): New decl.
(class noop_reader): New subclass of rtx_reader.
(read_char): Reimplement in terms of rtx_reader::read_char.
(unread_char): Reimplement in terms of rtx_reader::unread_char.
(read_md_files): Delete.
* read-rtl.c (read_rtx_code): Update for deletion of globals
read_md_filename and read_md_lineno.
2016-09-21 Jason Merrill <jason@redhat.com> 2016-09-21 Jason Merrill <jason@redhat.com>
* input.h (from_macro_definition_at): New. * input.h (from_macro_definition_at): New.
......
...@@ -79,7 +79,8 @@ main (int argc, const char **argv) ...@@ -79,7 +79,8 @@ main (int argc, const char **argv)
{ {
progname = "genconstants"; progname = "genconstants";
if (!read_md_files (argc, argv, NULL, NULL)) noop_reader reader;
if (!reader.read_md_files (argc, argv, NULL))
return (FATAL_EXIT_CODE); return (FATAL_EXIT_CODE);
/* Initializing the MD reader has the side effect of loading up /* Initializing the MD reader has the side effect of loading up
......
...@@ -49,7 +49,8 @@ main (int argc, const char **argv) ...@@ -49,7 +49,8 @@ main (int argc, const char **argv)
{ {
progname = "genenums"; progname = "genenums";
if (!read_md_files (argc, argv, NULL, NULL)) noop_reader reader;
if (!reader.read_md_files (argc, argv, NULL))
return (FATAL_EXIT_CODE); return (FATAL_EXIT_CODE);
puts ("/* Generated automatically by the program `genenums'"); puts ("/* Generated automatically by the program `genenums'");
......
...@@ -47,7 +47,8 @@ main (int argc, const char **argv) ...@@ -47,7 +47,8 @@ main (int argc, const char **argv)
progname = "genmddeps"; progname = "genmddeps";
include_callback = add_filedep; include_callback = add_filedep;
if (!read_md_files (argc, argv, NULL, NULL)) noop_reader reader;
if (!reader.read_md_files (argc, argv, NULL))
return FATAL_EXIT_CODE; return FATAL_EXIT_CODE;
*last = NULL; *last = NULL;
......
...@@ -1204,7 +1204,8 @@ write_tm_constrs_h (void) ...@@ -1204,7 +1204,8 @@ write_tm_constrs_h (void)
printf ("\ printf ("\
/* Generated automatically by the program '%s'\n\ /* Generated automatically by the program '%s'\n\
from the machine description file '%s'. */\n\n", progname, in_fname); from the machine description file '%s'. */\n\n", progname,
rtx_reader_ptr->get_top_level_filename ());
puts ("\ puts ("\
#ifndef GCC_TM_CONSTRS_H\n\ #ifndef GCC_TM_CONSTRS_H\n\
...@@ -1403,7 +1404,8 @@ write_tm_preds_h (void) ...@@ -1403,7 +1404,8 @@ write_tm_preds_h (void)
printf ("\ printf ("\
/* Generated automatically by the program '%s'\n\ /* Generated automatically by the program '%s'\n\
from the machine description file '%s'. */\n\n", progname, in_fname); from the machine description file '%s'. */\n\n", progname,
rtx_reader_ptr->get_top_level_filename ());
puts ("\ puts ("\
#ifndef GCC_TM_PREDS_H\n\ #ifndef GCC_TM_PREDS_H\n\
...@@ -1552,7 +1554,8 @@ write_insn_preds_c (void) ...@@ -1552,7 +1554,8 @@ write_insn_preds_c (void)
printf ("\ printf ("\
/* Generated automatically by the program '%s'\n\ /* Generated automatically by the program '%s'\n\
from the machine description file '%s'. */\n\n", progname, in_fname); from the machine description file '%s'. */\n\n", progname,
rtx_reader_ptr->get_top_level_filename ());
puts ("\ puts ("\
#include \"config.h\"\n\ #include \"config.h\"\n\
......
...@@ -2225,10 +2225,18 @@ process_define_subst (void) ...@@ -2225,10 +2225,18 @@ process_define_subst (void)
} }
} }
/* A read_md_files callback for reading an rtx. */ /* A subclass of rtx_reader which reads .md files and calls process_rtx on
the top-level elements. */
static void class gen_reader : public rtx_reader
rtx_handle_directive (file_location loc, const char *rtx_name) {
public:
gen_reader () : rtx_reader () {}
void handle_unknown_directive (file_location, const char *);
};
void
gen_reader::handle_unknown_directive (file_location loc, const char *rtx_name)
{ {
auto_vec<rtx, 32> subrtxs; auto_vec<rtx, 32> subrtxs;
if (!read_rtx (rtx_name, &subrtxs)) if (!read_rtx (rtx_name, &subrtxs))
...@@ -2499,7 +2507,7 @@ check_define_attr_duplicates () ...@@ -2499,7 +2507,7 @@ check_define_attr_duplicates ()
/* The entry point for initializing the reader. */ /* The entry point for initializing the reader. */
bool rtx_reader *
init_rtx_reader_args_cb (int argc, const char **argv, init_rtx_reader_args_cb (int argc, const char **argv,
bool (*parse_opt) (const char *)) bool (*parse_opt) (const char *))
{ {
...@@ -2515,7 +2523,8 @@ init_rtx_reader_args_cb (int argc, const char **argv, ...@@ -2515,7 +2523,8 @@ init_rtx_reader_args_cb (int argc, const char **argv,
split_sequence_num = 1; split_sequence_num = 1;
peephole2_sequence_num = 1; peephole2_sequence_num = 1;
read_md_files (argc, argv, parse_opt, rtx_handle_directive); gen_reader *reader = new gen_reader ();
reader->read_md_files (argc, argv, parse_opt);
if (define_attr_queue != NULL) if (define_attr_queue != NULL)
check_define_attr_duplicates (); check_define_attr_duplicates ();
...@@ -2531,12 +2540,18 @@ init_rtx_reader_args_cb (int argc, const char **argv, ...@@ -2531,12 +2540,18 @@ init_rtx_reader_args_cb (int argc, const char **argv,
if (define_attr_queue != NULL) if (define_attr_queue != NULL)
gen_mnemonic_attr (); gen_mnemonic_attr ();
return !have_error; if (have_error)
{
delete reader;
return NULL;
}
return reader;
} }
/* Programs that don't have their own options can use this entry point /* Programs that don't have their own options can use this entry point
instead. */ instead. */
bool rtx_reader *
init_rtx_reader_args (int argc, const char **argv) init_rtx_reader_args (int argc, const char **argv)
{ {
return init_rtx_reader_args_cb (argc, argv, 0); return init_rtx_reader_args_cb (argc, argv, 0);
......
...@@ -125,9 +125,9 @@ struct optab_pattern ...@@ -125,9 +125,9 @@ struct optab_pattern
}; };
extern rtx add_implicit_parallel (rtvec); extern rtx add_implicit_parallel (rtvec);
extern bool init_rtx_reader_args_cb (int, const char **, extern rtx_reader *init_rtx_reader_args_cb (int, const char **,
bool (*)(const char *)); bool (*)(const char *));
extern bool init_rtx_reader_args (int, const char **); extern rtx_reader *init_rtx_reader_args (int, const char **);
extern bool read_md_rtx (md_rtx_info *); extern bool read_md_rtx (md_rtx_info *);
extern unsigned int get_num_insn_codes (); extern unsigned int get_num_insn_codes ();
......
...@@ -90,16 +90,81 @@ struct enum_type { ...@@ -90,16 +90,81 @@ struct enum_type {
unsigned int num_values; unsigned int num_values;
}; };
/* A callback that handles a single .md-file directive, up to but not class rtx_reader
{
public:
rtx_reader ();
virtual ~rtx_reader ();
bool read_md_files (int, const char **, bool (*) (const char *));
/* A hook that handles a single .md-file directive, up to but not
including the closing ')'. It takes two arguments: the file position including the closing ')'. It takes two arguments: the file position
at which the directive started, and the name of the directive. The next at which the directive started, and the name of the directive. The next
unread character is the optional space after the directive name. */ unread character is the optional space after the directive name. */
typedef void (*directive_handler_t) (file_location, const char *); virtual void handle_unknown_directive (file_location, const char *) = 0;
file_location get_current_location () const;
int read_char (void);
void unread_char (int ch);
const char *get_top_level_filename () const { return m_toplevel_fname; }
const char *get_filename () const { return m_read_md_filename; }
int get_lineno () const { return m_read_md_lineno; }
private:
/* A singly-linked list of filenames. */
struct file_name_list {
struct file_name_list *next;
const char *fname;
};
private:
void handle_file ();
void handle_toplevel_file ();
void handle_include (file_location loc);
void add_include_path (const char *arg);
private:
/* The name of the toplevel file that indirectly included
m_read_md_file. */
const char *m_toplevel_fname;
/* The directory part of m_toplevel_fname
NULL if m_toplevel_fname is a bare filename. */
char *m_base_dir;
/* The file we are reading. */
FILE *m_read_md_file;
/* The filename of m_read_md_file. */
const char *m_read_md_filename;
/* The current line number in m_read_md_file. */
int m_read_md_lineno;
/* The first directory to search. */
file_name_list *m_first_dir_md_include;
/* A pointer to the null terminator of the md include chain. */
file_name_list **m_last_dir_md_include_ptr;
};
/* Global singleton. */
extern rtx_reader *rtx_reader_ptr;
/* An rtx_reader subclass which skips unknown directives. */
class noop_reader : public rtx_reader
{
public:
noop_reader () : rtx_reader () {}
/* A dummy implementation which skips unknown directives. */
void handle_unknown_directive (file_location, const char *);
};
extern const char *in_fname;
extern FILE *read_md_file;
extern int read_md_lineno;
extern const char *read_md_filename;
extern struct obstack string_obstack; extern struct obstack string_obstack;
extern void (*include_callback) (const char *); extern void (*include_callback) (const char *);
...@@ -108,12 +173,7 @@ extern void (*include_callback) (const char *); ...@@ -108,12 +173,7 @@ extern void (*include_callback) (const char *);
static inline int static inline int
read_char (void) read_char (void)
{ {
int ch; return rtx_reader_ptr->read_char ();
ch = getc (read_md_file);
if (ch == '\n')
read_md_lineno++;
return ch;
} }
/* Put back CH, which was the last character read from the MD file. */ /* Put back CH, which was the last character read from the MD file. */
...@@ -121,9 +181,7 @@ read_char (void) ...@@ -121,9 +181,7 @@ read_char (void)
static inline void static inline void
unread_char (int ch) unread_char (int ch)
{ {
if (ch == '\n') rtx_reader_ptr->unread_char (ch);
read_md_lineno--;
ungetc (ch, read_md_file);
} }
extern hashval_t leading_string_hash (const void *); extern hashval_t leading_string_hash (const void *);
...@@ -151,7 +209,5 @@ extern void upcase_string (char *); ...@@ -151,7 +209,5 @@ extern void upcase_string (char *);
extern void traverse_md_constants (htab_trav, void *); extern void traverse_md_constants (htab_trav, void *);
extern void traverse_enum_types (htab_trav, void *); extern void traverse_enum_types (htab_trav, void *);
extern struct enum_type *lookup_enum_type (const char *); extern struct enum_type *lookup_enum_type (const char *);
extern bool read_md_files (int, const char **, bool (*) (const char *),
directive_handler_t);
#endif /* GCC_READ_MD_H */ #endif /* GCC_READ_MD_H */
...@@ -1234,6 +1234,7 @@ read_rtx_code (const char *code_name) ...@@ -1234,6 +1234,7 @@ read_rtx_code (const char *code_name)
|| GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT)) || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
{ {
char line_name[20]; char line_name[20];
const char *read_md_filename = rtx_reader_ptr->get_filename ();
const char *fn = (read_md_filename ? read_md_filename : "rtx"); const char *fn = (read_md_filename ? read_md_filename : "rtx");
const char *slash; const char *slash;
for (slash = fn; *slash; slash ++) for (slash = fn; *slash; slash ++)
...@@ -1241,7 +1242,7 @@ read_rtx_code (const char *code_name) ...@@ -1241,7 +1242,7 @@ read_rtx_code (const char *code_name)
fn = slash + 1; fn = slash + 1;
obstack_1grow (&string_obstack, '*'); obstack_1grow (&string_obstack, '*');
obstack_grow (&string_obstack, fn, strlen (fn)); obstack_grow (&string_obstack, fn, strlen (fn));
sprintf (line_name, ":%d", read_md_lineno); sprintf (line_name, ":%d", rtx_reader_ptr->get_lineno ());
obstack_grow (&string_obstack, line_name, strlen (line_name)+1); obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
stringbuf = XOBFINISH (&string_obstack, char *); stringbuf = XOBFINISH (&string_obstack, char *);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment