1 /* Handling of fnspec attribute specifiers
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Parse string of attribute "fn spec". This is an internal attribute
22 describing side effects of a function as follows:
24 character 0 specifies properties of return values as follows:
25 '1'...'4' specifies number of argument function returns (as in memset)
26 'm' specifies that returned value is noalias (as in malloc)
27 '.' specifies that nothing is known.
28 character 1 specifies additional function properties
29 ' ' specifies that nothing is known
30 'p' or 'P' specifies that function is pure except for described side
32 'c' or 'C' specifies that function is const except for described side
34 The uppercase letter in addition specifies that function clobbers errno.
36 character 2+2i specifies properties of argument number i as follows:
37 'x' or 'X' specifies that parameter is unused.
38 'r' or 'R' specifies that the memory pointed to by the parameter is only
39 read and does not escape
40 'o' or 'O' specifies that the memory pointed to by the parameter is only
41 written and does not escape
42 'w' or 'W' specifies that the memory pointed to by the parameter does not
44 '.' specifies that nothing is known.
45 The uppercase letter in addition specifies that the memory pointed to
46 by the parameter is not dereferenced. For 'r' only read applies
47 transitively to pointers read from the pointed-to memory.
49 character 3+2i specifies additional properties of argument number i
52 't' the size of value written/read corresponds to the size of
53 of the pointed-to type of the argument type
54 '1'...'9' the size of value written/read is given by the specified
64 /* fn spec attribute string. */
66 /* length of the fn spec string. */
68 /* Number of characters specifying return value. */
69 const unsigned int return_desc_size
= 2;
70 /* Number of characters specifying size. */
71 const unsigned int arg_desc_size
= 2;
73 /* Return start of specifier of arg i. */
74 unsigned int arg_idx (int i
)
76 return return_desc_size
+ arg_desc_size
* i
;
80 attr_fnspec (const char *str
, unsigned len
)
81 : str (str
), len (len
)
86 attr_fnspec (const char *str
)
87 : str (str
), len (strlen (str
))
92 attr_fnspec (const_tree identifier
)
93 : str (TREE_STRING_POINTER (identifier
)),
94 len (TREE_STRING_LENGTH (identifier
))
100 : str (NULL
), len (0)
104 /* Return true if fn spec is known. */
111 /* Return true if arg I is specified. */
113 arg_specified_p (unsigned int i
)
115 return len
>= arg_idx (i
+ 1);
118 /* True if the argument is not dereferenced recursively, thus only
119 directly reachable memory is read or written. */
121 arg_direct_p (unsigned int i
)
123 unsigned int idx
= arg_idx (i
);
124 gcc_checking_assert (arg_specified_p (i
));
125 return str
[idx
] == 'R' || str
[idx
] == 'O' || str
[idx
] == 'W';
128 /* True if argument is used. */
130 arg_used_p (unsigned int i
)
132 unsigned int idx
= arg_idx (i
);
133 gcc_checking_assert (arg_specified_p (i
));
134 return str
[idx
] != 'x' && str
[idx
] != 'X';
137 /* True if memory reached by the argument is readonly (not clobbered). */
139 arg_readonly_p (unsigned int i
)
141 unsigned int idx
= arg_idx (i
);
142 gcc_checking_assert (arg_specified_p (i
));
143 return str
[idx
] == 'r' || str
[idx
] == 'R';
146 /* True if memory reached by the argument is read (directly or indirectly) */
148 arg_maybe_read_p (unsigned int i
)
150 unsigned int idx
= arg_idx (i
);
151 gcc_checking_assert (arg_specified_p (i
));
152 return str
[idx
] != 'o' && str
[idx
] != 'O'
153 && str
[idx
] != 'x' && str
[idx
] != 'X';
156 /* True if memory reached by the argument is written.
157 (directly or indirectly) */
159 arg_maybe_written_p (unsigned int i
)
161 unsigned int idx
= arg_idx (i
);
162 gcc_checking_assert (arg_specified_p (i
));
163 return str
[idx
] != 'r' && str
[idx
] != 'R'
164 && str
[idx
] != 'x' && str
[idx
] != 'X';
167 /* Return true if load of memory pointed to by argument I is specified
168 by another argument. In this case set ARG. */
170 arg_max_access_size_given_by_arg_p (unsigned int i
, unsigned int *arg
)
172 unsigned int idx
= arg_idx (i
);
173 gcc_checking_assert (arg_specified_p (i
));
174 if (str
[idx
+ 1] >= '1' && str
[idx
+ 1] <= '9')
176 *arg
= str
[idx
+ 1] - '1';
183 /* Return true if the pointed-to type of the argument correspond to the
184 size of the memory acccess. */
186 arg_access_size_given_by_type_p (unsigned int i
)
188 unsigned int idx
= arg_idx (i
);
189 gcc_checking_assert (arg_specified_p (i
));
190 return str
[idx
+ 1] == 't';
193 /* True if the argument does not escape. */
195 arg_noescape_p (unsigned int i
)
197 unsigned int idx
= arg_idx (i
);
198 gcc_checking_assert (arg_specified_p (i
));
199 return str
[idx
] == 'w' || str
[idx
] == 'W'
200 || str
[idx
] == 'r' || str
[idx
] == 'R'
201 || str
[idx
] == 'o' || str
[idx
] == 'O';
204 /* Return true if function returns value of its parameter. If ARG_NO is
205 non-NULL return initialize it to the argument returned. */
207 returns_arg (unsigned int *arg_no
)
209 if (str
[0] >= '1' && str
[0] <= '4')
212 *arg_no
= str
[0] - '1';
218 /* Nonzero if the return value does not alias with anything. Functions
219 with the malloc attribute have this set on their return value. */
223 return str
[0] == 'm';
226 /* Return true if all memory read by the function is specified by fnspec. */
228 global_memory_read_p ()
230 return str
[1] != 'c' && str
[1] != 'C';
233 /* Return true if all memory written by the function
234 is specified by fnspec. */
236 global_memory_written_p ()
238 return str
[1] != 'c' && str
[1] != 'C' && str
[1] != 'p' && str
[1] != 'P';
242 errno_maybe_written_p ()
244 return str
[1] == 'C' || str
[1] == 'P';
247 /* Check validity of the string. */
251 extern attr_fnspec
gimple_call_fnspec (const gcall
*stmt
);
252 extern attr_fnspec
builtin_fnspec (tree
);
254 #endif /* ATTR_FNSPEC_H */