read-md.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* MD reader definitions.
  2. Copyright (C) 1987-2018 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_READ_MD_H
  16. #define GCC_READ_MD_H
  17. #include "obstack.h"
  18. /* Records a position in the file. */
  19. struct file_location {
  20. file_location () {}
  21. file_location (const char *, int, int);
  22. const char *filename;
  23. int lineno;
  24. int colno;
  25. };
  26. inline file_location::file_location (const char *filename_in, int lineno_in, int colno_in)
  27. : filename (filename_in), lineno (lineno_in), colno (colno_in) {}
  28. /* Holds one symbol or number in the .md file. */
  29. struct md_name {
  30. /* The name as it appeared in the .md file. Names are syntactically
  31. limited to the length of this buffer. */
  32. char buffer[256];
  33. /* The name that should actually be used by the generator programs.
  34. This is an expansion of NAME, after things like constant substitution. */
  35. char *string;
  36. };
  37. /* This structure represents a constant defined by define_constant,
  38. define_enum, or such-like. */
  39. struct md_constant {
  40. /* The name of the constant. */
  41. char *name;
  42. /* The string to which the constants expands. */
  43. char *value;
  44. /* If the constant is associated with a enumeration, this field
  45. points to that enumeration, otherwise it is null. */
  46. struct enum_type *parent_enum;
  47. };
  48. /* This structure represents one value in an enum_type. */
  49. struct enum_value {
  50. /* The next value in the enum, or null if this is the last. */
  51. struct enum_value *next;
  52. /* The name of the value as it appears in the .md file. */
  53. char *name;
  54. /* The definition of the related C value. */
  55. struct md_constant *def;
  56. };
  57. /* This structure represents an enum defined by define_enum or the like. */
  58. struct enum_type {
  59. /* The C name of the enumeration. */
  60. char *name;
  61. /* True if this is an md-style enum (DEFINE_ENUM) rather than
  62. a C-style enum (DEFINE_C_ENUM). */
  63. bool md_p;
  64. /* The values of the enumeration. There is always at least one. */
  65. struct enum_value *values;
  66. /* A pointer to the null terminator in VALUES. */
  67. struct enum_value **tail_ptr;
  68. /* The number of enumeration values. */
  69. unsigned int num_values;
  70. };
  71. /* A class for reading .md files and RTL dump files.
  72. Implemented in read-md.c.
  73. This class has responsibility for reading chars from input files, and
  74. for certain common top-level directives including the "include"
  75. directive.
  76. It does not handle parsing the hierarchically-nested expressions of
  77. rtl.def; for that see the rtx_reader subclass below (implemented in
  78. read-rtl.c). */
  79. class md_reader
  80. {
  81. public:
  82. md_reader (bool compact);
  83. virtual ~md_reader ();
  84. bool read_md_files (int, const char **, bool (*) (const char *));
  85. bool read_file (const char *filename);
  86. bool read_file_fragment (const char *filename,
  87. int first_line,
  88. int last_line);
  89. /* A hook that handles a single .md-file directive, up to but not
  90. including the closing ')'. It takes two arguments: the file position
  91. at which the directive started, and the name of the directive. The next
  92. unread character is the optional space after the directive name. */
  93. virtual void handle_unknown_directive (file_location, const char *) = 0;
  94. file_location get_current_location () const;
  95. bool is_compact () const { return m_compact; }
  96. /* Defined in read-md.c. */
  97. int read_char (void);
  98. void unread_char (int ch);
  99. file_location read_name (struct md_name *name);
  100. file_location read_name_or_nil (struct md_name *);
  101. void read_escape ();
  102. char *read_quoted_string ();
  103. char *read_braced_string ();
  104. char *read_string (int star_if_braced);
  105. void read_skip_construct (int depth, file_location loc);
  106. void require_char (char expected);
  107. void require_char_ws (char expected);
  108. void require_word_ws (const char *expected);
  109. int peek_char (void);
  110. void set_md_ptr_loc (const void *ptr, const char *filename, int lineno);
  111. const struct ptr_loc *get_md_ptr_loc (const void *ptr);
  112. void copy_md_ptr_loc (const void *new_ptr, const void *old_ptr);
  113. void fprint_md_ptr_loc (FILE *outf, const void *ptr);
  114. void print_md_ptr_loc (const void *ptr);
  115. struct enum_type *lookup_enum_type (const char *name);
  116. void traverse_enum_types (htab_trav callback, void *info);
  117. void handle_constants ();
  118. void traverse_md_constants (htab_trav callback, void *info);
  119. void handle_enum (file_location loc, bool md_p);
  120. const char *join_c_conditions (const char *cond1, const char *cond2);
  121. void fprint_c_condition (FILE *outf, const char *cond);
  122. void print_c_condition (const char *cond);
  123. /* Defined in read-rtl.c. */
  124. const char *apply_iterator_to_string (const char *string);
  125. rtx copy_rtx_for_iterators (rtx original);
  126. void read_conditions ();
  127. void record_potential_iterator_use (struct iterator_group *group,
  128. rtx x, unsigned int index,
  129. const char *name);
  130. struct mapping *read_mapping (struct iterator_group *group, htab_t table);
  131. const char *get_top_level_filename () const { return m_toplevel_fname; }
  132. const char *get_filename () const { return m_read_md_filename; }
  133. int get_lineno () const { return m_read_md_lineno; }
  134. int get_colno () const { return m_read_md_colno; }
  135. struct obstack *get_string_obstack () { return &m_string_obstack; }
  136. htab_t get_md_constants () { return m_md_constants; }
  137. private:
  138. /* A singly-linked list of filenames. */
  139. struct file_name_list {
  140. struct file_name_list *next;
  141. const char *fname;
  142. };
  143. private:
  144. void handle_file ();
  145. void handle_toplevel_file ();
  146. void handle_include (file_location loc);
  147. void add_include_path (const char *arg);
  148. bool read_name_1 (struct md_name *name, file_location *out_loc);
  149. private:
  150. /* Are we reading a compact dump? */
  151. bool m_compact;
  152. /* The name of the toplevel file that indirectly included
  153. m_read_md_file. */
  154. const char *m_toplevel_fname;
  155. /* The directory part of m_toplevel_fname
  156. NULL if m_toplevel_fname is a bare filename. */
  157. char *m_base_dir;
  158. /* The file we are reading. */
  159. FILE *m_read_md_file;
  160. /* The filename of m_read_md_file. */
  161. const char *m_read_md_filename;
  162. /* The current line number in m_read_md_file. */
  163. int m_read_md_lineno;
  164. /* The current column number in m_read_md_file. */
  165. int m_read_md_colno;
  166. /* The column number before the last newline, so that
  167. we can handle unread_char ('\n') at least once whilst
  168. retaining column information. */
  169. int m_last_line_colno;
  170. /* The first directory to search. */
  171. file_name_list *m_first_dir_md_include;
  172. /* A pointer to the null terminator of the md include chain. */
  173. file_name_list **m_last_dir_md_include_ptr;
  174. /* Obstack used for allocating MD strings. */
  175. struct obstack m_string_obstack;
  176. /* A table of ptr_locs, hashed on the PTR field. */
  177. htab_t m_ptr_locs;
  178. /* An obstack for the above. Plain xmalloc is a bit heavyweight for a
  179. small structure like ptr_loc. */
  180. struct obstack m_ptr_loc_obstack;
  181. /* A hash table of triples (A, B, C), where each of A, B and C is a condition
  182. and A is equivalent to "B && C". This is used to keep track of the source
  183. of conditions that are made up of separate MD strings (such as the split
  184. condition of a define_insn_and_split). */
  185. htab_t m_joined_conditions;
  186. /* An obstack for allocating joined_conditions entries. */
  187. struct obstack m_joined_conditions_obstack;
  188. /* A table of md_constant structures, hashed by name. Null if no
  189. constant expansion should occur. */
  190. htab_t m_md_constants;
  191. /* A table of enum_type structures, hashed by name. */
  192. htab_t m_enum_types;
  193. /* If non-zero, filter the input to just this subset of lines. */
  194. int m_first_line;
  195. int m_last_line;
  196. };
  197. /* Global singleton; constrast with rtx_reader_ptr below. */
  198. extern md_reader *md_reader_ptr;
  199. /* An md_reader subclass which skips unknown directives, for
  200. the gen* tools that purely use read-md.o. */
  201. class noop_reader : public md_reader
  202. {
  203. public:
  204. noop_reader () : md_reader (false) {}
  205. /* A dummy implementation which skips unknown directives. */
  206. void handle_unknown_directive (file_location, const char *);
  207. };
  208. /* An md_reader subclass that actually handles full hierarchical
  209. rtx expressions.
  210. Implemented in read-rtl.c. */
  211. class rtx_reader : public md_reader
  212. {
  213. public:
  214. rtx_reader (bool compact);
  215. ~rtx_reader ();
  216. bool read_rtx (const char *rtx_name, vec<rtx> *rtxen);
  217. rtx read_rtx_code (const char *code_name);
  218. virtual rtx read_rtx_operand (rtx return_rtx, int idx);
  219. rtx read_nested_rtx ();
  220. rtx read_rtx_variadic (rtx form);
  221. char *read_until (const char *terminator_chars, bool consume_terminator);
  222. virtual void handle_any_trailing_information (rtx) {}
  223. virtual rtx postprocess (rtx x) { return x; }
  224. /* Hook to allow function_reader subclass to put STRINGBUF into gc-managed
  225. memory, rather than within an obstack.
  226. This base class implementation is a no-op. */
  227. virtual const char *finalize_string (char *stringbuf) { return stringbuf; }
  228. protected:
  229. /* Analogous to rtx_writer's m_in_call_function_usage. */
  230. bool m_in_call_function_usage;
  231. /* Support for "reuse_rtx" directives. */
  232. auto_vec<rtx> m_reuse_rtx_by_id;
  233. };
  234. /* Global singleton; constrast with md_reader_ptr above. */
  235. extern rtx_reader *rtx_reader_ptr;
  236. extern void (*include_callback) (const char *);
  237. /* Read the next character from the MD file. */
  238. static inline int
  239. read_char (void)
  240. {
  241. return md_reader_ptr->read_char ();
  242. }
  243. /* Put back CH, which was the last character read from the MD file. */
  244. static inline void
  245. unread_char (int ch)
  246. {
  247. md_reader_ptr->unread_char (ch);
  248. }
  249. extern hashval_t leading_string_hash (const void *);
  250. extern int leading_string_eq_p (const void *, const void *);
  251. extern const char *join_c_conditions (const char *, const char *);
  252. extern void message_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  253. extern void error_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  254. extern void fatal_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  255. extern void fatal_with_file_and_line (const char *, ...)
  256. ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
  257. extern void fatal_expected_char (int, int) ATTRIBUTE_NORETURN;
  258. extern int read_skip_spaces (void);
  259. extern int n_comma_elts (const char *);
  260. extern const char *scan_comma_elt (const char **);
  261. extern void upcase_string (char *);
  262. extern void traverse_enum_types (htab_trav, void *);
  263. extern struct enum_type *lookup_enum_type (const char *);
  264. #endif /* GCC_READ_MD_H */