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
|
// This file is part of GCC.
// GCC 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 3, or (at your option) any later
// version.
// GCC 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 GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-builtins.h"
namespace Rust {
namespace Compile {
static const int builtin_const = 1 << 0;
static const int builtin_noreturn = 1 << 1;
static const int builtin_novops = 1 << 2;
BuiltinsContext &
BuiltinsContext::get ()
{
static BuiltinsContext instance;
return instance;
}
bool
BuiltinsContext::lookup_simple_builtin (const std::string &name, tree *builtin)
{
auto it = rust_intrinsic_to_gcc_builtin.find (name);
if (it == rust_intrinsic_to_gcc_builtin.end ())
return false;
return lookup_gcc_builtin (it->second, builtin);
}
BuiltinsContext::BuiltinsContext () { setup (); }
void
BuiltinsContext::setup_overflow_fns ()
{
tree overflow_type
= build_varargs_function_type_list (boolean_type_node, NULL_TREE);
define_builtin ("add_overflow", BUILT_IN_ADD_OVERFLOW,
"__builtin_add_overflow", "add_overflow", overflow_type, 0);
define_builtin ("sub_overflow", BUILT_IN_SUB_OVERFLOW,
"__builtin_sub_overflow", "sub_overflow", overflow_type, 0);
define_builtin ("mul_overflow", BUILT_IN_MUL_OVERFLOW,
"__builtin_mul_overflow", "mul_overflow", overflow_type, 0);
}
void
BuiltinsContext::setup_math_fns ()
{
tree math_function_type_f32
= build_function_type_list (float_type_node, float_type_node, NULL_TREE);
define_builtin ("sinf32", BUILT_IN_SINF, "__builtin_sinf", "sinf",
math_function_type_f32, builtin_const);
define_builtin ("sqrtf32", BUILT_IN_SQRTF, "__builtin_sqrtf", "sqrtf",
math_function_type_f32, builtin_const);
}
void
BuiltinsContext::setup ()
{
setup_math_fns ();
setup_overflow_fns ();
define_builtin ("unreachable", BUILT_IN_UNREACHABLE, "__builtin_unreachable",
NULL, build_function_type (void_type_node, void_list_node),
builtin_const | builtin_noreturn);
define_builtin ("abort", BUILT_IN_ABORT, "__builtin_abort", "abort",
build_function_type (void_type_node, void_list_node),
builtin_const | builtin_noreturn);
define_builtin ("breakpoint", BUILT_IN_TRAP, "__builtin_trap", "breakpoint",
build_function_type (void_type_node, void_list_node),
builtin_const | builtin_noreturn);
define_builtin ("memcpy", BUILT_IN_MEMCPY, "__builtin_memcpy", "memcpy",
build_function_type_list (build_pointer_type (void_type_node),
build_pointer_type (void_type_node),
build_pointer_type (void_type_node),
size_type_node, NULL_TREE),
0);
}
static void
handle_flags (tree decl, int flags)
{
if (flags & builtin_const)
TREE_READONLY (decl) = 1;
if (flags & builtin_noreturn)
TREE_READONLY (decl) = 1;
if (flags & builtin_novops)
DECL_IS_NOVOPS (decl) = 1;
}
void
BuiltinsContext::define_builtin (const std::string rust_name,
built_in_function bcode, const char *name,
const char *libname, tree fntype, int flags)
{
tree decl = add_builtin_function (name, fntype, bcode, BUILT_IN_NORMAL,
libname, NULL_TREE);
handle_flags (decl, flags);
set_builtin_decl (bcode, decl, true);
this->builtin_functions_[name] = decl;
if (libname != NULL)
{
decl = add_builtin_function (libname, fntype, bcode, BUILT_IN_NORMAL,
NULL, NULL_TREE);
handle_flags (decl, flags);
this->builtin_functions_[libname] = decl;
}
rust_intrinsic_to_gcc_builtin[rust_name] = name;
}
bool
BuiltinsContext::lookup_gcc_builtin (const std::string &name, tree *builtin)
{
auto it = builtin_functions_.find (name);
if (it == builtin_functions_.end ())
return false;
*builtin = it->second;
return true;
}
} // namespace Compile
} // namespace Rust
|