aboutsummaryrefslogtreecommitdiff
path: root/gcc/cil/cil-builtins.c
blob: e86a406ccebb47c42aa9d1e7e575b9f898ea45bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
    Copyright (C) 1992-2007 Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2, or (at your option) any
    later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    In other words, you are welcome to use, share and improve this program.
    You are forbidden to forbid anyone else to use, share and improve
    what you give them.   Help stamp out software-hoarding!

    ---------------------------------------------------------------------------

   Authors:
     Andrea C. Ornstein <andrea.ornstein@st.com>
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tree-dump.h"
#include "tree-iterator.h"
#include "tree-gimple.h"
#include "function.h"
#include "flags.h"
#include "output.h"
#include "ggc.h"
#include "toplev.h"
#include "varray.h"
#include "langhooks-def.h"
#include "langhooks.h"
#include "target.h"
#include "opts.h"
#include "cgraph.h"
#include "convert.h"

#include "cil.h"
#include "cil-builtins.h"
#include "cil-tree.h"
#include "bindings.h"

bool flag_no_cbuiltin;

static tree handle_const_attribute (tree *, tree, tree, int, bool *);
static tree handle_pure_attribute (tree *, tree, tree, int, bool *);

const struct attribute_spec cil_common_attribute_table[] =
{
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
  { "packed",                 0, 0, false, false, false, NULL/*handle_packed_attribute*/ },
  { "nocommon",               0, 0,  true, false, false, NULL/*handle_nocommon_attribute*/ },
  { "common",                 0, 0,  true, false, false, NULL/*handle_common_attribute*/ },
  /* FIXME: logically, noreturn attributes should be listed as
     "false, true, true" and apply to function types.  But implementing this
     would require all the places in the compiler that use TREE_THIS_VOLATILE
     on a decl to identify non-returning functions to be located and fixed
     to check the function type instead.  */
  { "noreturn",               0, 0,  true, false, false, NULL/*handle_noreturn_attribute*/ },
  { "volatile",               0, 0,  true, false, false, NULL/*handle_noreturn_attribute*/ },
  { "noinline",               0, 0,  true, false, false, NULL/*handle_noinline_attribute*/ },
  { "always_inline",          0, 0,  true, false, false, NULL/*handle_always_inline_attribute*/ },
  { "flatten",                0, 0,  true, false, false, NULL/*handle_flatten_attribute*/ },
  { "used",                   0, 0,  true, false, false, NULL/*handle_used_attribute*/ },
  { "unused",                 0, 0, false, false, false, NULL/*handle_unused_attribute*/ },
  { "externally_visible",     0, 0,  true, false, false, NULL/*handle_externally_visible_attribute*/ },
  /* The same comments as for noreturn attributes apply to const ones.  */
  { "const",                  0, 0,  true, false, false, handle_const_attribute },
  { "transparent_union",      0, 0, false, false, false, NULL/*handle_transparent_union_attribute*/ },
  { "constructor",            0, 0,  true, false, false, NULL/*handle_constructor_attribute*/ },
  { "destructor",             0, 0,  true, false, false, NULL/*handle_destructor_attribute*/ },
  { "mode",                   1, 1, false,  true, false, NULL/*handle_mode_attribute*/ },
  { "section",                1, 1,  true, false, false, NULL/*handle_section_attribute*/ },
  { "aligned",                0, 1, false, false, false, NULL/*handle_aligned_attribute*/ },
  { "weak",                   0, 0,  true, false, false, NULL/*handle_weak_attribute*/ },
  { "alias",                  1, 1,  true, false, false, NULL/*handle_alias_attribute*/ },
  { "weakref",                0, 1,  true, false, false, NULL/*handle_weakref_attribute*/ },
  { "no_instrument_function", 0, 0,  true, false, false, NULL/*handle_no_instrument_function_attribute*/ },
  { "malloc",                 0, 0,  true, false, false, NULL/*handle_malloc_attribute*/ },
  { "returns_twice",          0, 0,  true, false, false, NULL/*handle_returns_twice_attribute*/ },
  { "no_stack_limit",         0, 0,  true, false, false, NULL/*handle_no_limit_stack_attribute*/ },
  { "pure",                   0, 0,  true, false, false, handle_pure_attribute },
  /* For internal use (marking of builtins) only.  The name contains space
     to prevent its usage in source code.  */
  { "no vops",                0, 0,  true, false, false, NULL/*handle_novops_attribute*/ },
  { "deprecated",             0, 0, false, false, false, NULL/*handle_deprecated_attribute*/ },
  { "vector_size",            1, 1, false,  true, false, NULL/*handle_vector_size_attribute*/ },
  { "visibility",             1, 1, false, false, false, NULL/*handle_visibility_attribute*/ },
  { "tls_model",              1, 1,  true, false, false, NULL/*handle_tls_model_attribute*/ },
  { "nonnull",                0, -1, false, true,  true, NULL/*handle_nonnull_attribute*/ },
  { "nothrow",                0, 0,  true, false, false, NULL/*handle_nothrow_attribute*/ },
  { "may_alias",              0, 0, false,  true, false, NULL },
  { "cleanup",                1, 1,  true, false, false, NULL/*handle_cleanup_attribute*/ },
  { "warn_unused_result",     0, 0, false,  true,  true, NULL/*handle_warn_unused_result_attribute*/ },
  { "sentinel",               0, 1, false,  true,  true, NULL/*handle_sentinel_attribute*/ },
  { NULL,                     0, 0, false, false, false, NULL }
};

const struct attribute_spec cil_common_format_attribute_table[] =
{
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
  { "format",                 3, 3, false,  true,  true, NULL/*handle_format_attribute*/ },
  { "format_arg",             1, 1, false,  true,  true, NULL/*handle_format_arg_attribute*/ },
  { NULL,                     0, 0, false, false, false, NULL }
};

static tree
handle_const_attribute (tree *node, tree ARG_UNUSED (name), tree ARG_UNUSED (args),
                        int ARG_UNUSED (flags), bool *no_add_attrs)
{
  tree type = TREE_TYPE (*node);

  /* See FIXME comment on noreturn in c_common_attribute_table.  */
  if (TREE_CODE (*node) == FUNCTION_DECL)
    TREE_READONLY (*node) = 1;
  else if (TREE_CODE (type) == POINTER_TYPE
           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
    TREE_TYPE (*node)
      = build_pointer_type
        (build_type_variant (TREE_TYPE (type), 1,
                             TREE_THIS_VOLATILE (TREE_TYPE (type))));
  else
    {
      warning (OPT_Wattributes, "const attribute ignored");
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

static tree
handle_pure_attribute (tree *node, tree ARG_UNUSED (name), tree ARG_UNUSED (args),
                       int ARG_UNUSED (flags), bool *no_add_attrs)
{
  if (TREE_CODE (*node) == FUNCTION_DECL)
    DECL_IS_PURE (*node) = 1;
  /* ??? TODO: Support types.  */
  else
    {
      warning (OPT_Wattributes, "pure attribute ignored");
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

int flag_isoc94 = 1;
int flag_isoc99 = 1;

static GTY(()) tree signed_size_type_node;
static GTY(()) tree wint_type_node;
static GTY(()) tree intmax_type_node;
static GTY(()) tree uintmax_type_node;
static GTY(()) tree string_type_node;
static GTY(()) tree const_string_type_node;
static GTY(()) tree va_list_ref_type_node;
static GTY(()) tree va_list_arg_type_node;

/* Handle C builtins.  */

static void init_builtin_attributes (void);

static void init_builtin_types (void);

static void init_builtins (void);


enum built_in_attribute
{
#define DEF_ATTR_NULL_TREE(ENUM, ...) ENUM,
#define DEF_ATTR_INT(ENUM, ...)       ENUM,
#define DEF_ATTR_IDENT(ENUM, ...)     ENUM,
#define DEF_ATTR_TREE_LIST(ENUM, ...) ENUM,
#  include "builtin-attrs.def"
#undef DEF_ATTR_NULL_TREE
#undef DEF_ATTR_INT
#undef DEF_ATTR_IDENT
#undef DEF_ATTR_TREE_LIST
  ATTR_LAST
};

static GTY(()) tree built_in_attributes[(int) ATTR_LAST];

enum c_builtin_type
{
#define DEF_PRIMITIVE_TYPE(NAME, VALUE)    NAME,
#define DEF_FUNCTION_TYPE_0(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_1(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_2(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_3(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_4(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_5(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_6(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_7(NAME, ...)     NAME,
#define DEF_FUNCTION_TYPE_VAR_0(NAME, ...) NAME,
#define DEF_FUNCTION_TYPE_VAR_1(NAME, ...) NAME,
#define DEF_FUNCTION_TYPE_VAR_2(NAME, ...) NAME,
#define DEF_FUNCTION_TYPE_VAR_3(NAME, ...) NAME,
#define DEF_FUNCTION_TYPE_VAR_4(NAME, ...) NAME,
#define DEF_FUNCTION_TYPE_VAR_5(NAME, ...) NAME,
#define DEF_POINTER_TYPE(NAME, TYPE)       NAME,
#  include "builtin-types.def"
#undef DEF_PRIMITIVE_TYPE
#undef DEF_FUNCTION_TYPE_0
#undef DEF_FUNCTION_TYPE_1
#undef DEF_FUNCTION_TYPE_2
#undef DEF_FUNCTION_TYPE_3
#undef DEF_FUNCTION_TYPE_4
#undef DEF_FUNCTION_TYPE_5
#undef DEF_FUNCTION_TYPE_6
#undef DEF_FUNCTION_TYPE_7
#undef DEF_FUNCTION_TYPE_VAR_0
#undef DEF_FUNCTION_TYPE_VAR_1
#undef DEF_FUNCTION_TYPE_VAR_2
#undef DEF_FUNCTION_TYPE_VAR_3
#undef DEF_FUNCTION_TYPE_VAR_4
#undef DEF_FUNCTION_TYPE_VAR_5
#undef DEF_POINTER_TYPE
  BT_LAST
};

typedef enum c_builtin_type builtin_type;

/* A temporary array for c_common_nodes_and_builtins.  Used in communication with def_fn_type.  */
static GTY(()) tree builtin_types[(int) BT_LAST + 1];



static void
init_builtin_attributes (void)
{
#define DEF_ATTR_NULL_TREE(ENUM)     built_in_attributes[(int) ENUM] = NULL_TREE;
#define DEF_ATTR_INT(ENUM, VALUE)    built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
#define DEF_ATTR_IDENT(ENUM, STRING) built_in_attributes[(int) ENUM] = get_identifier (STRING);
#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
  built_in_attributes[(int) ENUM]                       \
    = tree_cons (built_in_attributes[(int) PURPOSE],    \
                 built_in_attributes[(int) VALUE],      \
                 built_in_attributes[(int) CHAIN]);
#  include "builtin-attrs.def"
#undef DEF_ATTR_NULL_TREE
#undef DEF_ATTR_INT
#undef DEF_ATTR_IDENT
#undef DEF_ATTR_TREE_LIST
}

static tree
builtin_type_for_size (int size, bool unsignedp)
{
  tree type = lang_hooks.types.type_for_size (size, unsignedp);
  return type ? type : error_mark_node;
}

static void
def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
{
  tree args = NULL;
  tree t;
  va_list va;
  int i;

  va_start (va, n);
  for (i = 0; i < n; ++i)
    {
      builtin_type a = va_arg (va, builtin_type);
      t = builtin_types[a];
      if (t == error_mark_node)
        goto egress;
      args = tree_cons (NULL_TREE, t, args);
    }
  va_end (va);

  args = nreverse (args);
  if (!var)
    args = chainon (args, void_list_node);

  t = builtin_types[ret];
  if (t == error_mark_node)
    goto egress;
  t = build_function_type (t, args);

 egress:
  builtin_types[def] = t;
}

static void
init_builtin_types (void)
{
#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) builtin_types[ENUM] = VALUE;
#define DEF_FUNCTION_TYPE_0(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 0);
#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 1, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 2, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 3, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 4, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 5, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 6, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 0, 7, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 0);
#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 1, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 2, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 3, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 4, __VA_ARGS__);
#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ...) def_fn_type (ENUM, RETURN, 1, 5, __VA_ARGS__);
#define DEF_POINTER_TYPE(ENUM, TYPE) \
    builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);

#  include "builtin-types.def"

#undef DEF_PRIMITIVE_TYPE
#undef DEF_FUNCTION_TYPE_1
#undef DEF_FUNCTION_TYPE_2
#undef DEF_FUNCTION_TYPE_3
#undef DEF_FUNCTION_TYPE_4
#undef DEF_FUNCTION_TYPE_5
#undef DEF_FUNCTION_TYPE_6
#undef DEF_FUNCTION_TYPE_VAR_0
#undef DEF_FUNCTION_TYPE_VAR_1
#undef DEF_FUNCTION_TYPE_VAR_2
#undef DEF_FUNCTION_TYPE_VAR_3
#undef DEF_FUNCTION_TYPE_VAR_4
#undef DEF_FUNCTION_TYPE_VAR_5
#undef DEF_POINTER_TYPE
  builtin_types[(int) BT_LAST] = NULL_TREE;
}

static void
def_builtin_1 (enum built_in_function fncode,
               const char *name,
               enum built_in_class fnclass,
               tree fntype, tree libtype,
               tree fnattrs, bool implicit_p)
{
  tree decl;
  const char *libname;

  if (fntype == error_mark_node)
    return;

  libname = name + strlen ("__builtin_");

  decl = lang_hooks.builtin_function (libname, libtype, fncode, fnclass, NULL, fnattrs);
  built_in_decls[(int) fncode] = decl;
  if (implicit_p)
    implicit_built_in_decls[(int) fncode] = decl;

}

static void
init_builtins (void)
{
#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, NONANSI_P, ATTRS, IMPLICIT, COND) \
      if (NAME && COND && BOTH_P)                         \
        def_builtin_1 (ENUM, NAME, CLASS,                 \
                       builtin_types[(int) TYPE],         \
                       builtin_types[(int) LIBTYPE],      \
                       built_in_attributes[(int) ATTRS],  \
                       IMPLICIT);
#  include "builtins.def"
#undef DEF_BUILTIN
}

void
cil_build_builtins (void)
{
  signed_size_type_node  = make_signed_type (POINTER_SIZE);
  wint_type_node         = short_unsigned_type_node;
  intmax_type_node       = long_long_integer_type_node;
  uintmax_type_node      = long_long_unsigned_type_node;
  string_type_node       = build_pointer_type (char_type_node);
  const_string_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
  pid_type_node          = long_integer_type_node;
  va_list_arg_type_node  = va_list_type_node;
  va_list_ref_type_node  = build_reference_type (va_list_type_node);

  init_builtin_attributes ();
  init_builtin_types ();
  if (!flag_no_cbuiltin)
      init_builtins ();

  memset (builtin_types, 0, sizeof (builtin_types));
}

#include "gt-cil-cil-builtins.h"