You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
13KB

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