read-md.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* MD reader definitions.
  2. Copyright (C) 1987-2019 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. /* Describes one instance of an overloaded_name. */
  72. struct overloaded_instance {
  73. /* The next instance in the chain, or null if none. */
  74. overloaded_instance *next;
  75. /* The values that the overloaded_name arguments should have for this
  76. instance to be chosen. Each value is a C token. */
  77. vec<const char *> arg_values;
  78. /* The full (non-overloaded) name of the pattern. */
  79. const char *name;
  80. /* The corresponding define_expand or define_insn. */
  81. rtx insn;
  82. };
  83. /* Describes a define_expand or define_insn whose name was preceded by '@'.
  84. Overloads are uniquely determined by their name and the types of their
  85. arguments; it's possible to have overloads with the same name but
  86. different argument types. */
  87. struct overloaded_name {
  88. /* The next overloaded name in the chain. */
  89. overloaded_name *next;
  90. /* The overloaded name (i.e. the name with "@" character and
  91. "<...>" placeholders removed). */
  92. const char *name;
  93. /* The C types of the iterators that determine the underlying pattern,
  94. in the same order as in the pattern name. E.g. "<mode>" in the
  95. pattern name would give a "machine_mode" argument here. */
  96. vec<const char *> arg_types;
  97. /* The first instance associated with this overloaded_name. */
  98. overloaded_instance *first_instance;
  99. /* Where to chain new overloaded_instances. */
  100. overloaded_instance **next_instance_ptr;
  101. };
  102. struct mapping;
  103. /* A class for reading .md files and RTL dump files.
  104. Implemented in read-md.c.
  105. This class has responsibility for reading chars from input files, and
  106. for certain common top-level directives including the "include"
  107. directive.
  108. It does not handle parsing the hierarchically-nested expressions of
  109. rtl.def; for that see the rtx_reader subclass below (implemented in
  110. read-rtl.c). */
  111. class md_reader
  112. {
  113. public:
  114. md_reader (bool compact);
  115. virtual ~md_reader ();
  116. bool read_md_files (int, const char **, bool (*) (const char *));
  117. bool read_file (const char *filename);
  118. bool read_file_fragment (const char *filename,
  119. int first_line,
  120. int last_line);
  121. /* A hook that handles a single .md-file directive, up to but not
  122. including the closing ')'. It takes two arguments: the file position
  123. at which the directive started, and the name of the directive. The next
  124. unread character is the optional space after the directive name. */
  125. virtual void handle_unknown_directive (file_location, const char *) = 0;
  126. file_location get_current_location () const;
  127. bool is_compact () const { return m_compact; }
  128. /* Defined in read-md.c. */
  129. int read_char (void);
  130. void unread_char (int ch);
  131. file_location read_name (struct md_name *name);
  132. file_location read_name_or_nil (struct md_name *);
  133. void read_escape ();
  134. char *read_quoted_string ();
  135. char *read_braced_string ();
  136. char *read_string (int star_if_braced);
  137. void read_skip_construct (int depth, file_location loc);
  138. void require_char (char expected);
  139. void require_char_ws (char expected);
  140. void require_word_ws (const char *expected);
  141. int peek_char (void);
  142. void set_md_ptr_loc (const void *ptr, const char *filename, int lineno);
  143. const struct ptr_loc *get_md_ptr_loc (const void *ptr);
  144. void copy_md_ptr_loc (const void *new_ptr, const void *old_ptr);
  145. void fprint_md_ptr_loc (FILE *outf, const void *ptr);
  146. void print_md_ptr_loc (const void *ptr);
  147. struct enum_type *lookup_enum_type (const char *name);
  148. void traverse_enum_types (htab_trav callback, void *info);
  149. void handle_constants ();
  150. void traverse_md_constants (htab_trav callback, void *info);
  151. void handle_enum (file_location loc, bool md_p);
  152. const char *join_c_conditions (const char *cond1, const char *cond2);
  153. void fprint_c_condition (FILE *outf, const char *cond);
  154. void print_c_condition (const char *cond);
  155. /* Defined in read-rtl.c. */
  156. const char *apply_iterator_to_string (const char *string);
  157. rtx copy_rtx_for_iterators (rtx original);
  158. void read_conditions ();
  159. void record_potential_iterator_use (struct iterator_group *group,
  160. rtx x, unsigned int index,
  161. const char *name);
  162. struct mapping *read_mapping (struct iterator_group *group, htab_t table);
  163. overloaded_name *handle_overloaded_name (rtx, vec<mapping *> *);
  164. const char *get_top_level_filename () const { return m_toplevel_fname; }
  165. const char *get_filename () const { return m_read_md_filename; }
  166. int get_lineno () const { return m_read_md_lineno; }
  167. int get_colno () const { return m_read_md_colno; }
  168. struct obstack *get_string_obstack () { return &m_string_obstack; }
  169. htab_t get_md_constants () { return m_md_constants; }
  170. overloaded_name *get_overloads () const { return m_first_overload; }
  171. private:
  172. /* A singly-linked list of filenames. */
  173. struct file_name_list {
  174. struct file_name_list *next;
  175. const char *fname;
  176. };
  177. private:
  178. void handle_file ();
  179. void handle_toplevel_file ();
  180. void handle_include (file_location loc);
  181. void add_include_path (const char *arg);
  182. bool read_name_1 (struct md_name *name, file_location *out_loc);
  183. private:
  184. /* Are we reading a compact dump? */
  185. bool m_compact;
  186. /* The name of the toplevel file that indirectly included
  187. m_read_md_file. */
  188. const char *m_toplevel_fname;
  189. /* The directory part of m_toplevel_fname
  190. NULL if m_toplevel_fname is a bare filename. */
  191. char *m_base_dir;
  192. /* The file we are reading. */
  193. FILE *m_read_md_file;
  194. /* The filename of m_read_md_file. */
  195. const char *m_read_md_filename;
  196. /* The current line number in m_read_md_file. */
  197. int m_read_md_lineno;
  198. /* The current column number in m_read_md_file. */
  199. int m_read_md_colno;
  200. /* The column number before the last newline, so that
  201. we can handle unread_char ('\n') at least once whilst
  202. retaining column information. */
  203. int m_last_line_colno;
  204. /* The first directory to search. */
  205. file_name_list *m_first_dir_md_include;
  206. /* A pointer to the null terminator of the md include chain. */
  207. file_name_list **m_last_dir_md_include_ptr;
  208. /* Obstack used for allocating MD strings. */
  209. struct obstack m_string_obstack;
  210. /* A table of ptr_locs, hashed on the PTR field. */
  211. htab_t m_ptr_locs;
  212. /* An obstack for the above. Plain xmalloc is a bit heavyweight for a
  213. small structure like ptr_loc. */
  214. struct obstack m_ptr_loc_obstack;
  215. /* A hash table of triples (A, B, C), where each of A, B and C is a condition
  216. and A is equivalent to "B && C". This is used to keep track of the source
  217. of conditions that are made up of separate MD strings (such as the split
  218. condition of a define_insn_and_split). */
  219. htab_t m_joined_conditions;
  220. /* An obstack for allocating joined_conditions entries. */
  221. struct obstack m_joined_conditions_obstack;
  222. /* A table of md_constant structures, hashed by name. Null if no
  223. constant expansion should occur. */
  224. htab_t m_md_constants;
  225. /* A table of enum_type structures, hashed by name. */
  226. htab_t m_enum_types;
  227. /* If non-zero, filter the input to just this subset of lines. */
  228. int m_first_line;
  229. int m_last_line;
  230. /* The first overloaded_name. */
  231. overloaded_name *m_first_overload;
  232. /* Where to chain further overloaded_names, */
  233. overloaded_name **m_next_overload_ptr;
  234. /* A hash table of overloaded_names, keyed off their name and the types of
  235. their arguments. */
  236. htab_t m_overloads_htab;
  237. };
  238. /* Global singleton; constrast with rtx_reader_ptr below. */
  239. extern md_reader *md_reader_ptr;
  240. /* An md_reader subclass which skips unknown directives, for
  241. the gen* tools that purely use read-md.o. */
  242. class noop_reader : public md_reader
  243. {
  244. public:
  245. noop_reader () : md_reader (false) {}
  246. /* A dummy implementation which skips unknown directives. */
  247. void handle_unknown_directive (file_location, const char *);
  248. };
  249. /* An md_reader subclass that actually handles full hierarchical
  250. rtx expressions.
  251. Implemented in read-rtl.c. */
  252. class rtx_reader : public md_reader
  253. {
  254. public:
  255. rtx_reader (bool compact);
  256. ~rtx_reader ();
  257. bool read_rtx (const char *rtx_name, vec<rtx> *rtxen);
  258. rtx read_rtx_code (const char *code_name);
  259. virtual rtx read_rtx_operand (rtx return_rtx, int idx);
  260. rtx read_nested_rtx ();
  261. rtx read_rtx_variadic (rtx form);
  262. char *read_until (const char *terminator_chars, bool consume_terminator);
  263. virtual void handle_any_trailing_information (rtx) {}
  264. virtual rtx postprocess (rtx x) { return x; }
  265. /* Hook to allow function_reader subclass to put STRINGBUF into gc-managed
  266. memory, rather than within an obstack.
  267. This base class implementation is a no-op. */
  268. virtual const char *finalize_string (char *stringbuf) { return stringbuf; }
  269. protected:
  270. /* Analogous to rtx_writer's m_in_call_function_usage. */
  271. bool m_in_call_function_usage;
  272. /* Support for "reuse_rtx" directives. */
  273. auto_vec<rtx> m_reuse_rtx_by_id;
  274. };
  275. /* Global singleton; constrast with md_reader_ptr above. */
  276. extern rtx_reader *rtx_reader_ptr;
  277. extern void (*include_callback) (const char *);
  278. /* Read the next character from the MD file. */
  279. static inline int
  280. read_char (void)
  281. {
  282. return md_reader_ptr->read_char ();
  283. }
  284. /* Put back CH, which was the last character read from the MD file. */
  285. static inline void
  286. unread_char (int ch)
  287. {
  288. md_reader_ptr->unread_char (ch);
  289. }
  290. extern hashval_t leading_string_hash (const void *);
  291. extern int leading_string_eq_p (const void *, const void *);
  292. extern const char *join_c_conditions (const char *, const char *);
  293. extern void message_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  294. extern void error_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  295. extern void fatal_at (file_location, const char *, ...) ATTRIBUTE_PRINTF_2;
  296. extern void fatal_with_file_and_line (const char *, ...)
  297. ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
  298. extern void fatal_expected_char (int, int) ATTRIBUTE_NORETURN;
  299. extern int read_skip_spaces (void);
  300. extern int n_comma_elts (const char *);
  301. extern const char *scan_comma_elt (const char **);
  302. extern void upcase_string (char *);
  303. extern void traverse_enum_types (htab_trav, void *);
  304. extern struct enum_type *lookup_enum_type (const char *);
  305. #endif /* GCC_READ_MD_H */