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