Debian Codegraph @

gcc/d/d-compiler.cc

raw
1 /* d-compiler.cc -- D frontend interface to the gcc back-end.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21
22 #include "dmd/compiler.h"
23 #include "dmd/errors.h"
24 #include "dmd/expression.h"
25 #include "dmd/identifier.h"
26 #include "dmd/module.h"
27 #include "dmd/mtype.h"
28
29 #include "tree.h"
30 #include "fold-const.h"
31
32 #include "d-tree.h"
33
34
35 /* Implements the Compiler interface used by the frontend. */
36
37 /* Perform a reinterpret cast of EXPR to type TYPE for use in CTFE.
38 The front end should have already ensured that EXPR is a constant,
39 so we just lower the value to GCC and return the converted CST. */
40
41 Expression *
42 Compiler::paintAsType (UnionExp *, Expression *expr, Type *type)
43 {
44 /* We support up to 512-bit values. */
45 unsigned char buffer[64];
46 tree cst;
47
48 Type *tb = type->toBasetype ();
49
50 if (expr->type->isIntegral ())
51 cst = build_integer_cst (expr->toInteger (), build_ctype (expr->type));
52 else if (expr->type->isFloating ())
53 cst = build_float_cst (expr->toReal (), expr->type);
54 else if (expr->op == EXP::arrayLiteral)
55 {
56 /* Build array as VECTOR_CST, assumes EXPR is constant. */
57 Expressions *elements = expr->isArrayLiteralExp ()->elements;
58 vec <constructor_elt, va_gc> *elms = NULL;
59
60 vec_safe_reserve (elms, elements->length);
61 for (size_t i = 0; i < elements->length; i++)
62 {
63 Expression *e = (*elements)[i];
64 if (e->type->isIntegral ())
65 {
66 tree value = build_integer_cst (e->toInteger (),
67 build_ctype (e->type));
68 CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
69 }
70 else if (e->type->isFloating ())
71 {
72 tree value = build_float_cst (e->toReal (), e->type);
73 CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
74 }
75 else
76 gcc_unreachable ();
77 }
78
79 /* Build vector type. */
80 int nunits = expr->type->isTypeSArray ()->dim->toUInteger ();
81 Type *telem = expr->type->nextOf ();
82 tree vectype = build_vector_type (build_ctype (telem), nunits);
83
84 cst = build_vector_from_ctor (vectype, elms);
85 }
86 else
87 gcc_unreachable ();
88
89 /* Encode CST to buffer. */
90 int len = native_encode_expr (cst, buffer, sizeof (buffer));
91
92 if (tb->ty == TY::Tsarray)
93 {
94 /* Interpret value as a vector of the same size,
95 then return the array literal. */
96 int nunits = type->isTypeSArray ()->dim->toUInteger ();
97 Type *elem = type->nextOf ();
98 tree vectype = build_vector_type (build_ctype (elem), nunits);
99
100 cst = native_interpret_expr (vectype, buffer, len);
101
102 Expression *e = d_eval_constant_expression (expr->loc, cst);
103 gcc_assert (e != NULL && e->op == EXP::vector);
104
105 return e->isVectorExp ()->e1;
106 }
107 else
108 {
109 /* Normal interpret cast. */
110 cst = native_interpret_expr (build_ctype (type), buffer, len);
111
112 Expression *e = d_eval_constant_expression (expr->loc, cst);
113 gcc_assert (e != NULL);
114
115 return e;
116 }
117 }
118
119 /* Check imported module M for any special processing.
120 Modules we look out for are:
121 - object: For D runtime type information.
122 - gcc.builtins: For all gcc builtins.
123 - all other modules for extern(C) gcc library builtins. */
124
125 void
126 Compiler::onParseModule (Module *m)
127 {
128 ModuleDeclaration *md = m->md;
129
130 if (md && md->id)
131 {
132 if (md->packages.length == 0)
133 {
134 if (!strcmp (md->id->toChars (), "object"))
135 {
136 create_tinfo_types (m);
137 return;
138 }
139 }
140 else if (md->packages.length == 1)
141 {
142 if (!strcmp (md->packages.ptr[0]->toChars (), "gcc")
143 && !strcmp (md->id->toChars (), "builtins"))
144 {
145 d_build_builtins_module (m);
146 return;
147 }
148 }
149 }
150 else if (m->ident)
151 {
152 if (!strcmp (m->ident->toChars (), "object"))
153 {
154 create_tinfo_types (m);
155 return;
156 }
157 }
158
159 if (!flag_no_builtin)
160 d_add_builtin_module (m);
161 }
162
163 /* A callback function that is called once an imported module is parsed.
164 If the callback returns true, then it tells the front-end that the
165 driver intends on compiling the import. */
166
167 bool
168 Compiler::onImport (Module *m)
169 {
170 if (!includeImports)
171 return false;
172
173 if (m->filetype != FileType::d && m->filetype != FileType::c)
174 return false;
175
176 /* All imports modules are included except those in the runtime library. */
177 ModuleDeclaration *md = m->md;
178 if (md && md->id)
179 {
180 if (md->packages.length >= 1)
181 {
182 if (!strcmp (md->packages.ptr[0]->toChars (), "core")
183 || !strcmp (md->packages.ptr[0]->toChars (), "std")
184 || !strcmp (md->packages.ptr[0]->toChars (), "gcc")
185 || !strcmp (md->packages.ptr[0]->toChars (), "etc"))
186 return false;
187 }
188 else if (!strcmp (md->id->toChars (), "object"))
189 return false;
190 }
191 else if (m->ident)
192 {
193 if (!strcmp (m->ident->toChars (), "object"))
194 return false;
195 }
196
197 /* This import will be compiled. */
198 if (global.params.v.verbose)
199 message ("compileimport (%s)", m->srcfile.toChars ());
200
201 compiledImports.push (m);
202 return true;
203 }