| 1 |
/* modules.cc -- D module initialization and termination. |
| 2 |
Copyright (C) 2013-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/declaration.h" |
| 23 |
#include "dmd/identifier.h" |
| 24 |
#include "dmd/module.h" |
| 25 |
|
| 26 |
#include "tree.h" |
| 27 |
#include "diagnostic.h" |
| 28 |
#include "fold-const.h" |
| 29 |
#include "tm.h" |
| 30 |
#include "function.h" |
| 31 |
#include "cgraph.h" |
| 32 |
#include "stor-layout.h" |
| 33 |
#include "debug.h" |
| 34 |
#include "toplev.h" |
| 35 |
#include "target.h" |
| 36 |
#include "common/common-target.h" |
| 37 |
#include "stringpool.h" |
| 38 |
|
| 39 |
#include "d-tree.h" |
| 40 |
#include "d-target.h" |
| 41 |
|
| 42 |
|
| 43 |
/* D generates module information to inform the runtime library which modules |
| 44 |
need some kind of special handling. All `static this()', `static ~this()', |
| 45 |
and `unittest' functions for a given module are aggregated into a single |
| 46 |
function - one for each kind - and a pointer to that function is inserted |
| 47 |
into the ModuleInfo instance for that module. |
| 48 |
|
| 49 |
Module information for a particular module is indicated with an ABI defined |
| 50 |
structure derived from ModuleInfo. ModuleInfo is a variably sized struct |
| 51 |
with two fixed base fields. The first field `flags' determines what |
| 52 |
information is packed immediately after the record type. |
| 53 |
|
| 54 |
Like TypeInfo, the runtime library provides the definitions of the ModuleInfo |
| 55 |
structure, as well as accessors for the variadic fields. So we only define |
| 56 |
layout compatible POD_structs for ModuleInfo. */ |
| 57 |
|
| 58 |
/* The internally represented ModuleInfo and CompilerDSO types. */ |
| 59 |
static tree moduleinfo_type; |
| 60 |
static tree compiler_dso_type; |
| 61 |
static tree dso_registry_fn; |
| 62 |
|
| 63 |
/* The DSO slot for use by the druntime implementation. */ |
| 64 |
static tree dso_slot_node; |
| 65 |
|
| 66 |
/* For registering and deregistering DSOs with druntime, we have one global |
| 67 |
constructor and destructor per object that calls _d_dso_registry with the |
| 68 |
respective DSO record. To ensure that this is only done once, a |
| 69 |
`dso_initialized' variable is introduced to guard repeated calls. */ |
| 70 |
static tree dso_initialized_node; |
| 71 |
|
| 72 |
/* The beginning and end of the `minfo' section. */ |
| 73 |
static tree start_minfo_node; |
| 74 |
static tree stop_minfo_node; |
| 75 |
|
| 76 |
/* Record information about module initialization, termination, |
| 77 |
unit testing, and thread local storage in the compilation. */ |
| 78 |
|
| 79 |
struct module_info |
| 80 |
{ |
| 81 |
vec <tree, va_gc> *ctors; |
| 82 |
vec <tree, va_gc> *dtors; |
| 83 |
vec <tree, va_gc> *ctorgates; |
| 84 |
|
| 85 |
vec <tree, va_gc> *sharedctors; |
| 86 |
vec <tree, va_gc> *shareddtors; |
| 87 |
vec <tree, va_gc> *sharedctorgates; |
| 88 |
vec <tree, va_gc> *standalonectors; |
| 89 |
|
| 90 |
vec <tree, va_gc> *unitTests; |
| 91 |
}; |
| 92 |
|
| 93 |
/* These must match the values in libdruntime/object_.d. */ |
| 94 |
|
| 95 |
enum module_info_flags |
| 96 |
{ |
| 97 |
MIctorstart = 0x1, |
| 98 |
MIctordone = 0x2, |
| 99 |
MIstandalone = 0x4, |
| 100 |
MItlsctor = 0x8, |
| 101 |
MItlsdtor = 0x10, |
| 102 |
MIctor = 0x20, |
| 103 |
MIdtor = 0x40, |
| 104 |
MIxgetMembers = 0x80, |
| 105 |
MIictor = 0x100, |
| 106 |
MIunitTest = 0x200, |
| 107 |
MIimportedModules = 0x400, |
| 108 |
MIlocalClasses = 0x800, |
| 109 |
MIname = 0x1000 |
| 110 |
}; |
| 111 |
|
| 112 |
/* The ModuleInfo information structure for the module currently being compiled. |
| 113 |
Assuming that only ever process one at a time. */ |
| 114 |
|
| 115 |
static module_info *current_moduleinfo; |
| 116 |
|
| 117 |
/* When compiling with -fbuilding-libphobos-tests, this contains information |
| 118 |
about the module that gets compiled in only when unittests are enabled. */ |
| 119 |
|
| 120 |
static module_info *current_testing_module; |
| 121 |
|
| 122 |
/* The declaration of the current module being compiled. */ |
| 123 |
|
| 124 |
static Module *current_module_decl; |
| 125 |
|
| 126 |
/* Any inline symbols that were deferred during codegen. */ |
| 127 |
vec<Declaration *> *deferred_inline_declarations; |
| 128 |
|
| 129 |
/* Returns an internal function identified by IDENT. This is used |
| 130 |
by both module initialization and dso handlers. */ |
| 131 |
|
| 132 |
static FuncDeclaration * |
| 133 |
get_internal_fn (tree ident, const Visibility &visibility) |
| 134 |
{ |
| 135 |
Module *mod = current_module_decl; |
| 136 |
const char *name = IDENTIFIER_POINTER (ident); |
| 137 |
|
| 138 |
if (!mod) |
| 139 |
mod = Module::rootModule; |
| 140 |
|
| 141 |
if (name[0] == '*') |
| 142 |
{ |
| 143 |
tree s = mangle_internal_decl (mod, name + 1, "FZv"); |
| 144 |
name = IDENTIFIER_POINTER (s); |
| 145 |
} |
| 146 |
|
| 147 |
FuncDeclaration *fd = dmd::genCfunc (NULL, Type::tvoid, |
| 148 |
Identifier::idPool (name)); |
| 149 |
fd->isGenerated (true); |
| 150 |
fd->loc = Loc::singleFilename (mod->srcfile.toChars ()); |
| 151 |
fd->parent = mod; |
| 152 |
fd->visibility = visibility; |
| 153 |
fd->semanticRun (PASS::semantic3done); |
| 154 |
|
| 155 |
return fd; |
| 156 |
} |
| 157 |
|
| 158 |
/* Generate an internal function identified by IDENT. |
| 159 |
The function body to add is in EXPR. */ |
| 160 |
|
| 161 |
static tree |
| 162 |
build_internal_fn (tree ident, tree expr) |
| 163 |
{ |
| 164 |
Visibility visibility; |
| 165 |
visibility.kind = Visibility::private_; |
| 166 |
FuncDeclaration *fd = get_internal_fn (ident, visibility); |
| 167 |
tree decl = get_symbol_decl (fd); |
| 168 |
|
| 169 |
tree old_context = start_function (fd); |
| 170 |
rest_of_decl_compilation (decl, 1, 0); |
| 171 |
add_stmt (expr); |
| 172 |
finish_function (old_context); |
| 173 |
|
| 174 |
/* D static ctors, static dtors, unittests, and the ModuleInfo |
| 175 |
chain function are always private. */ |
| 176 |
TREE_PUBLIC (decl) = 0; |
| 177 |
TREE_USED (decl) = 1; |
| 178 |
DECL_ARTIFICIAL (decl) = 1; |
| 179 |
|
| 180 |
return decl; |
| 181 |
} |
| 182 |
|
| 183 |
/* Build and emit a function identified by IDENT that increments (in order) |
| 184 |
all variables in GATES, then calls the list of functions in FUNCTIONS. */ |
| 185 |
|
| 186 |
static tree |
| 187 |
build_funcs_gates_fn (tree ident, vec <tree, va_gc> *functions, |
| 188 |
vec <tree, va_gc> *gates) |
| 189 |
{ |
| 190 |
tree expr_list = NULL_TREE; |
| 191 |
|
| 192 |
/* Increment gates first. */ |
| 193 |
for (size_t i = 0; i < vec_safe_length (gates); i++) |
| 194 |
{ |
| 195 |
tree decl = (*gates)[i]; |
| 196 |
tree value = build2 (PLUS_EXPR, TREE_TYPE (decl), |
| 197 |
decl, integer_one_node); |
| 198 |
tree var_expr = modify_expr (decl, value); |
| 199 |
expr_list = compound_expr (expr_list, var_expr); |
| 200 |
} |
| 201 |
|
| 202 |
/* Call Functions. */ |
| 203 |
for (size_t i = 0; i < vec_safe_length (functions); i++) |
| 204 |
{ |
| 205 |
tree decl = (*functions)[i]; |
| 206 |
tree call_expr = build_call_expr (decl, 0); |
| 207 |
expr_list = compound_expr (expr_list, call_expr); |
| 208 |
} |
| 209 |
|
| 210 |
if (expr_list) |
| 211 |
return build_internal_fn (ident, expr_list); |
| 212 |
|
| 213 |
return NULL_TREE; |
| 214 |
} |
| 215 |
|
| 216 |
/* Return the type for ModuleInfo, create it if it doesn't already exist. */ |
| 217 |
|
| 218 |
static tree |
| 219 |
get_moduleinfo_type (void) |
| 220 |
{ |
| 221 |
if (moduleinfo_type) |
| 222 |
return moduleinfo_type; |
| 223 |
|
| 224 |
/* Layout of ModuleInfo is: |
| 225 |
uint flags; |
| 226 |
uint index; */ |
| 227 |
tree fields = create_field_decl (d_uint_type, NULL, 1, 1); |
| 228 |
DECL_CHAIN (fields) = create_field_decl (d_uint_type, NULL, 1, 1); |
| 229 |
|
| 230 |
moduleinfo_type = make_node (RECORD_TYPE); |
| 231 |
finish_builtin_struct (moduleinfo_type, "ModuleInfo", fields, NULL_TREE); |
| 232 |
|
| 233 |
return moduleinfo_type; |
| 234 |
} |
| 235 |
|
| 236 |
/* Get the VAR_DECL of the ModuleInfo for DECL. If this does not yet exist, |
| 237 |
create it. The ModuleInfo decl is used to keep track of constructors, |
| 238 |
destructors, unittests, members, classes, and imports for the given module. |
| 239 |
This is used by the D runtime for module initialization and termination. */ |
| 240 |
|
| 241 |
static tree |
| 242 |
get_moduleinfo_decl (Module *decl) |
| 243 |
{ |
| 244 |
if (decl->csym) |
| 245 |
return decl->csym; |
| 246 |
|
| 247 |
tree ident = mangle_internal_decl (decl, "__ModuleInfo", "Z"); |
| 248 |
tree type = get_moduleinfo_type (); |
| 249 |
|
| 250 |
decl->csym = declare_extern_var (ident, type); |
| 251 |
DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (NULL); |
| 252 |
|
| 253 |
DECL_CONTEXT (decl->csym) = build_import_decl (decl); |
| 254 |
/* Not readonly, moduleinit depends on this. */ |
| 255 |
TREE_READONLY (decl->csym) = 0; |
| 256 |
|
| 257 |
return decl->csym; |
| 258 |
} |
| 259 |
|
| 260 |
/* Return the type for CompilerDSOData, create it if it doesn't exist. */ |
| 261 |
|
| 262 |
static tree |
| 263 |
get_compiler_dso_type (void) |
| 264 |
{ |
| 265 |
if (compiler_dso_type) |
| 266 |
return compiler_dso_type; |
| 267 |
|
| 268 |
/* Layout of CompilerDSOData is: |
| 269 |
size_t version; |
| 270 |
void** slot; |
| 271 |
ModuleInfo** _minfo_beg; |
| 272 |
ModuleInfo** _minfo_end; |
| 273 |
FuncTable* _deh_beg; |
| 274 |
FuncTable* _deh_end; |
| 275 |
|
| 276 |
Note, finish_builtin_struct() expects these fields in reverse order. */ |
| 277 |
tree fields = create_field_decl (ptr_type_node, NULL, 1, 1); |
| 278 |
tree field = create_field_decl (ptr_type_node, NULL, 1, 1); |
| 279 |
DECL_CHAIN (field) = fields; |
| 280 |
fields = field; |
| 281 |
|
| 282 |
tree moduleinfo_ptr_ptr_type = |
| 283 |
build_pointer_type (build_pointer_type (get_moduleinfo_type ())); |
| 284 |
|
| 285 |
field = create_field_decl (moduleinfo_ptr_ptr_type, NULL, 1, 1); |
| 286 |
DECL_CHAIN (field) = fields; |
| 287 |
fields = field; |
| 288 |
field = create_field_decl (moduleinfo_ptr_ptr_type, NULL, 1, 1); |
| 289 |
DECL_CHAIN (field) = fields; |
| 290 |
fields = field; |
| 291 |
|
| 292 |
field = create_field_decl (build_pointer_type (ptr_type_node), NULL, 1, 1); |
| 293 |
DECL_CHAIN (field) = fields; |
| 294 |
fields = field; |
| 295 |
|
| 296 |
field = create_field_decl (size_type_node, NULL, 1, 1); |
| 297 |
DECL_CHAIN (field) = fields; |
| 298 |
fields = field; |
| 299 |
|
| 300 |
compiler_dso_type = make_node (RECORD_TYPE); |
| 301 |
finish_builtin_struct (compiler_dso_type, "CompilerDSOData", |
| 302 |
fields, NULL_TREE); |
| 303 |
|
| 304 |
return compiler_dso_type; |
| 305 |
} |
| 306 |
|
| 307 |
/* Returns the _d_dso_registry FUNCTION_DECL. */ |
| 308 |
|
| 309 |
static tree |
| 310 |
get_dso_registry_fn (void) |
| 311 |
{ |
| 312 |
if (dso_registry_fn) |
| 313 |
return dso_registry_fn; |
| 314 |
|
| 315 |
tree dso_type = get_compiler_dso_type (); |
| 316 |
tree fntype = build_function_type_list (void_type_node, |
| 317 |
build_pointer_type (dso_type), |
| 318 |
NULL_TREE); |
| 319 |
dso_registry_fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, |
| 320 |
get_identifier ("_d_dso_registry"), fntype); |
| 321 |
TREE_PUBLIC (dso_registry_fn) = 1; |
| 322 |
DECL_EXTERNAL (dso_registry_fn) = 1; |
| 323 |
|
| 324 |
return dso_registry_fn; |
| 325 |
} |
| 326 |
|
| 327 |
/* Depending on CTOR_P, builds and emits eiter a constructor or destructor |
| 328 |
calling _d_dso_registry if `dso_initialized' is `false' in a constructor |
| 329 |
or `true' in a destructor. */ |
| 330 |
|
| 331 |
static tree |
| 332 |
build_dso_cdtor_fn (bool ctor_p) |
| 333 |
{ |
| 334 |
const char *name = ctor_p ? GDC_PREFIX ("dso_ctor") : GDC_PREFIX ("dso_dtor"); |
| 335 |
tree condition = ctor_p ? d_bool_true_node : d_bool_false_node; |
| 336 |
|
| 337 |
/* Declaration of dso_ctor/dso_dtor is: |
| 338 |
|
| 339 |
extern(C) void dso_{c,d}tor (void) |
| 340 |
{ |
| 341 |
if (dso_initialized != condition) |
| 342 |
{ |
| 343 |
dso_initialized = condition; |
| 344 |
CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo}; |
| 345 |
_d_dso_registry (&dso); |
| 346 |
} |
| 347 |
} |
| 348 |
*/ |
| 349 |
Visibility visibility; |
| 350 |
visibility.kind = Visibility::public_; |
| 351 |
FuncDeclaration *fd = get_internal_fn (get_identifier (name), visibility); |
| 352 |
tree decl = get_symbol_decl (fd); |
| 353 |
|
| 354 |
TREE_PUBLIC (decl) = 1; |
| 355 |
DECL_ARTIFICIAL (decl) = 1; |
| 356 |
DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; |
| 357 |
DECL_VISIBILITY_SPECIFIED (decl) = 1; |
| 358 |
|
| 359 |
/* Start laying out the body. */ |
| 360 |
tree old_context = start_function (fd); |
| 361 |
rest_of_decl_compilation (decl, 1, 0); |
| 362 |
|
| 363 |
/* if (dso_initialized != condition). */ |
| 364 |
tree if_cond = build_boolop (NE_EXPR, dso_initialized_node, condition); |
| 365 |
|
| 366 |
/* dso_initialized = condition; */ |
| 367 |
tree expr_list = modify_expr (dso_initialized_node, condition); |
| 368 |
|
| 369 |
/* CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo}; */ |
| 370 |
tree dso_type = get_compiler_dso_type (); |
| 371 |
tree dso = build_local_temp (dso_type); |
| 372 |
|
| 373 |
vec <constructor_elt, va_gc> *ve = NULL; |
| 374 |
CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_integer_cst (1, size_type_node)); |
| 375 |
CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (dso_slot_node)); |
| 376 |
CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (start_minfo_node)); |
| 377 |
CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (stop_minfo_node)); |
| 378 |
|
| 379 |
tree assign_expr = modify_expr (dso, build_struct_literal (dso_type, ve)); |
| 380 |
expr_list = compound_expr (expr_list, assign_expr); |
| 381 |
|
| 382 |
/* _d_dso_registry (&dso); */ |
| 383 |
tree call_expr = build_call_expr (get_dso_registry_fn (), 1, |
| 384 |
build_address (dso)); |
| 385 |
expr_list = compound_expr (expr_list, call_expr); |
| 386 |
|
| 387 |
add_stmt (build_vcondition (if_cond, expr_list, void_node)); |
| 388 |
finish_function (old_context); |
| 389 |
|
| 390 |
return decl; |
| 391 |
} |
| 392 |
|
| 393 |
/* Build a variable used in the dso_registry code identified by NAME, |
| 394 |
and data type TYPE. The variable always has VISIBILITY_HIDDEN and |
| 395 |
TREE_PUBLIC flags set. */ |
| 396 |
|
| 397 |
static tree |
| 398 |
build_dso_registry_var (const char * name, tree type) |
| 399 |
{ |
| 400 |
tree var = declare_extern_var (get_identifier (name), type); |
| 401 |
DECL_VISIBILITY (var) = VISIBILITY_HIDDEN; |
| 402 |
DECL_VISIBILITY_SPECIFIED (var) = 1; |
| 403 |
return var; |
| 404 |
} |
| 405 |
|
| 406 |
/* Place a reference to the ModuleInfo symbol MINFO for DECL into the |
| 407 |
`minfo' section. Then create the global ctors/dtors to call the |
| 408 |
_d_dso_registry function if necessary. */ |
| 409 |
|
| 410 |
static void |
| 411 |
register_moduleinfo (Module *decl, tree minfo) |
| 412 |
{ |
| 413 |
/* No defined minfo section for target. */ |
| 414 |
if (targetdm.d_minfo_section == NULL) |
| 415 |
return; |
| 416 |
|
| 417 |
if (!targetm_common.have_named_sections) |
| 418 |
sorry ("%<-fmoduleinfo%> is not supported on this target"); |
| 419 |
|
| 420 |
/* Build the ModuleInfo reference, this is done once for every Module. */ |
| 421 |
tree ident = mangle_internal_decl (decl, "__moduleRef", "Z"); |
| 422 |
tree mref = declare_extern_var (ident, ptr_type_node); |
| 423 |
|
| 424 |
/* Build the initializer and emit. Do not start section with a `.' character |
| 425 |
so that the linker will provide a __start_ and __stop_ symbol to indicate |
| 426 |
the start and end address of the section respectively. |
| 427 |
https://sourceware.org/binutils/docs-2.26/ld/Orphan-Sections.html. */ |
| 428 |
DECL_INITIAL (mref) = build_address (minfo); |
| 429 |
DECL_EXTERNAL (mref) = 0; |
| 430 |
DECL_PRESERVE_P (mref) = 1; |
| 431 |
|
| 432 |
set_decl_section_name (mref, targetdm.d_minfo_section); |
| 433 |
symtab_node::get (mref)->implicit_section = true; |
| 434 |
d_pushdecl (mref); |
| 435 |
rest_of_decl_compilation (mref, 1, 0); |
| 436 |
|
| 437 |
/* Only for the first D module being emitted do we need to generate a static |
| 438 |
constructor and destructor for. These are only required once per shared |
| 439 |
library, so it's safe to emit them only once per object file. */ |
| 440 |
static bool first_module = true; |
| 441 |
if (!first_module) |
| 442 |
return; |
| 443 |
|
| 444 |
start_minfo_node = build_dso_registry_var (targetdm.d_minfo_section_start, |
| 445 |
ptr_type_node); |
| 446 |
rest_of_decl_compilation (start_minfo_node, 1, 0); |
| 447 |
|
| 448 |
stop_minfo_node = build_dso_registry_var (targetdm.d_minfo_section_end, |
| 449 |
ptr_type_node); |
| 450 |
rest_of_decl_compilation (stop_minfo_node, 1, 0); |
| 451 |
|
| 452 |
/* Declare dso_slot and dso_initialized. */ |
| 453 |
dso_slot_node = build_dso_registry_var (GDC_PREFIX ("dso_slot"), |
| 454 |
ptr_type_node); |
| 455 |
d_finish_decl (dso_slot_node); |
| 456 |
|
| 457 |
dso_initialized_node = build_dso_registry_var (GDC_PREFIX ("dso_initialized"), |
| 458 |
d_bool_type); |
| 459 |
d_finish_decl (dso_initialized_node); |
| 460 |
|
| 461 |
/* Declare dso_ctor() and dso_dtor(). */ |
| 462 |
tree dso_ctor = build_dso_cdtor_fn (true); |
| 463 |
DECL_STATIC_CONSTRUCTOR (dso_ctor) = 1; |
| 464 |
decl_init_priority_insert (dso_ctor, DEFAULT_INIT_PRIORITY); |
| 465 |
|
| 466 |
tree dso_dtor = build_dso_cdtor_fn (false); |
| 467 |
DECL_STATIC_DESTRUCTOR (dso_dtor) = 1; |
| 468 |
decl_fini_priority_insert (dso_dtor, DEFAULT_INIT_PRIORITY); |
| 469 |
|
| 470 |
first_module = false; |
| 471 |
} |
| 472 |
|
| 473 |
/* Convenience function for layout_moduleinfo_fields. Adds a field of TYPE to |
| 474 |
the moduleinfo record at OFFSET, incrementing the offset to the next field |
| 475 |
position. No alignment is taken into account, all fields are packed. */ |
| 476 |
|
| 477 |
static void |
| 478 |
layout_moduleinfo_field (tree type, tree rec_type, HOST_WIDE_INT &offset) |
| 479 |
{ |
| 480 |
tree field = create_field_decl (type, NULL, 1, 1); |
| 481 |
insert_aggregate_field (rec_type, field, offset); |
| 482 |
offset += int_size_in_bytes (type); |
| 483 |
} |
| 484 |
|
| 485 |
/* Layout fields that immediately come after the moduleinfo TYPE for DECL. |
| 486 |
Data relating to the module is packed into the type on an as-needed |
| 487 |
basis, this is done to keep its size to a minimum. */ |
| 488 |
|
| 489 |
static tree |
| 490 |
layout_moduleinfo_fields (Module *decl, tree type) |
| 491 |
{ |
| 492 |
HOST_WIDE_INT offset = int_size_in_bytes (type); |
| 493 |
type = copy_aggregate_type (type); |
| 494 |
|
| 495 |
/* First fields added are all the function pointers. */ |
| 496 |
if (decl->sctor) |
| 497 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 498 |
|
| 499 |
if (decl->sdtor) |
| 500 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 501 |
|
| 502 |
if (decl->ssharedctor) |
| 503 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 504 |
|
| 505 |
if (decl->sshareddtor) |
| 506 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 507 |
|
| 508 |
if (dmd::findGetMembers (decl)) |
| 509 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 510 |
|
| 511 |
if (decl->sictor) |
| 512 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 513 |
|
| 514 |
if (decl->stest) |
| 515 |
layout_moduleinfo_field (ptr_type_node, type, offset); |
| 516 |
|
| 517 |
/* Array of module imports is laid out as a length field, followed by |
| 518 |
a static array of ModuleInfo pointers. */ |
| 519 |
size_t aimports_dim = decl->aimports.length; |
| 520 |
for (size_t i = 0; i < decl->aimports.length; i++) |
| 521 |
{ |
| 522 |
Module *mi = decl->aimports[i]; |
| 523 |
if (!mi->needmoduleinfo) |
| 524 |
aimports_dim--; |
| 525 |
} |
| 526 |
|
| 527 |
if (aimports_dim) |
| 528 |
{ |
| 529 |
layout_moduleinfo_field (size_type_node, type, offset); |
| 530 |
layout_moduleinfo_field (make_array_type (Type::tvoidptr, aimports_dim), |
| 531 |
type, offset); |
| 532 |
} |
| 533 |
|
| 534 |
/* Array of local ClassInfo decls are laid out in the same way. */ |
| 535 |
ClassDeclarations aclasses; |
| 536 |
dmd::getLocalClasses (decl, aclasses); |
| 537 |
|
| 538 |
if (aclasses.length) |
| 539 |
{ |
| 540 |
layout_moduleinfo_field (size_type_node, type, offset); |
| 541 |
layout_moduleinfo_field (make_array_type (Type::tvoidptr, |
| 542 |
aclasses.length), |
| 543 |
type, offset); |
| 544 |
} |
| 545 |
|
| 546 |
/* Lastly, the name of the module is a static char array. */ |
| 547 |
size_t namelen = strlen (decl->toPrettyChars ()) + 1; |
| 548 |
layout_moduleinfo_field (make_array_type (Type::tchar, namelen), |
| 549 |
type, offset); |
| 550 |
|
| 551 |
size_t alignsize = MAX (TYPE_ALIGN_UNIT (type), |
| 552 |
TYPE_ALIGN_UNIT (ptr_type_node)); |
| 553 |
finish_aggregate_type (offset, alignsize, type); |
| 554 |
|
| 555 |
return type; |
| 556 |
} |
| 557 |
|
| 558 |
/* Output the ModuleInfo for module DECL and register it with druntime. */ |
| 559 |
|
| 560 |
static void |
| 561 |
layout_moduleinfo (Module *decl) |
| 562 |
{ |
| 563 |
ClassDeclarations aclasses; |
| 564 |
FuncDeclaration *sgetmembers; |
| 565 |
|
| 566 |
dmd::getLocalClasses (decl, aclasses); |
| 567 |
|
| 568 |
size_t aimports_dim = decl->aimports.length; |
| 569 |
for (size_t i = 0; i < decl->aimports.length; i++) |
| 570 |
{ |
| 571 |
Module *mi = decl->aimports[i]; |
| 572 |
if (!mi->needmoduleinfo) |
| 573 |
aimports_dim--; |
| 574 |
} |
| 575 |
|
| 576 |
sgetmembers = dmd::findGetMembers (decl); |
| 577 |
|
| 578 |
size_t flags = 0; |
| 579 |
if (decl->sctor) |
| 580 |
flags |= MItlsctor; |
| 581 |
if (decl->sdtor) |
| 582 |
flags |= MItlsdtor; |
| 583 |
if (decl->ssharedctor) |
| 584 |
flags |= MIctor; |
| 585 |
if (decl->sshareddtor) |
| 586 |
flags |= MIdtor; |
| 587 |
if (sgetmembers) |
| 588 |
flags |= MIxgetMembers; |
| 589 |
if (decl->sictor) |
| 590 |
flags |= MIictor; |
| 591 |
if (decl->stest) |
| 592 |
flags |= MIunitTest; |
| 593 |
if (aimports_dim) |
| 594 |
flags |= MIimportedModules; |
| 595 |
if (aclasses.length) |
| 596 |
flags |= MIlocalClasses; |
| 597 |
if (!decl->needmoduleinfo) |
| 598 |
flags |= MIstandalone; |
| 599 |
|
| 600 |
flags |= MIname; |
| 601 |
|
| 602 |
tree minfo = get_moduleinfo_decl (decl); |
| 603 |
tree type = layout_moduleinfo_fields (decl, TREE_TYPE (minfo)); |
| 604 |
|
| 605 |
/* Put out the two named fields in a ModuleInfo decl: |
| 606 |
uint flags; |
| 607 |
uint index; */ |
| 608 |
vec <constructor_elt, va_gc> *minit = NULL; |
| 609 |
|
| 610 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 611 |
build_integer_cst (flags, d_uint_type)); |
| 612 |
|
| 613 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 614 |
build_integer_cst (0, d_uint_type)); |
| 615 |
|
| 616 |
/* Order of appearance, depending on flags: |
| 617 |
void function() tlsctor; |
| 618 |
void function() tlsdtor; |
| 619 |
void* function() xgetMembers; |
| 620 |
void function() ctor; |
| 621 |
void function() dtor; |
| 622 |
void function() ictor; |
| 623 |
void function() unitTest; |
| 624 |
ModuleInfo*[] importedModules; |
| 625 |
TypeInfo_Class[] localClasses; |
| 626 |
char[N] name; |
| 627 |
*/ |
| 628 |
if (flags & MItlsctor) |
| 629 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sctor)); |
| 630 |
|
| 631 |
if (flags & MItlsdtor) |
| 632 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sdtor)); |
| 633 |
|
| 634 |
if (flags & MIctor) |
| 635 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 636 |
build_address (decl->ssharedctor)); |
| 637 |
|
| 638 |
if (flags & MIdtor) |
| 639 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 640 |
build_address (decl->sshareddtor)); |
| 641 |
|
| 642 |
if (flags & MIxgetMembers) |
| 643 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 644 |
build_address (get_symbol_decl (sgetmembers))); |
| 645 |
|
| 646 |
if (flags & MIictor) |
| 647 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sictor)); |
| 648 |
|
| 649 |
if (flags & MIunitTest) |
| 650 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->stest)); |
| 651 |
|
| 652 |
if (flags & MIimportedModules) |
| 653 |
{ |
| 654 |
vec <constructor_elt, va_gc> *elms = NULL; |
| 655 |
tree satype = make_array_type (Type::tvoidptr, aimports_dim); |
| 656 |
size_t idx = 0; |
| 657 |
|
| 658 |
for (size_t i = 0; i < decl->aimports.length; i++) |
| 659 |
{ |
| 660 |
Module *mi = decl->aimports[i]; |
| 661 |
if (mi->needmoduleinfo) |
| 662 |
{ |
| 663 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (idx), |
| 664 |
build_address (get_moduleinfo_decl (mi))); |
| 665 |
idx++; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aimports_dim)); |
| 670 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 671 |
build_padded_constructor (satype, elms)); |
| 672 |
} |
| 673 |
|
| 674 |
if (flags & MIlocalClasses) |
| 675 |
{ |
| 676 |
vec <constructor_elt, va_gc> *elms = NULL; |
| 677 |
tree satype = make_array_type (Type::tvoidptr, aclasses.length); |
| 678 |
|
| 679 |
for (size_t i = 0; i < aclasses.length; i++) |
| 680 |
{ |
| 681 |
ClassDeclaration *cd = aclasses[i]; |
| 682 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), |
| 683 |
build_address (get_classinfo_decl (cd))); |
| 684 |
} |
| 685 |
|
| 686 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aclasses.length)); |
| 687 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, |
| 688 |
build_padded_constructor (satype, elms)); |
| 689 |
} |
| 690 |
|
| 691 |
if (flags & MIname) |
| 692 |
{ |
| 693 |
/* Put out module name as a 0-terminated C-string, to save bytes. */ |
| 694 |
const char *name = decl->toPrettyChars (); |
| 695 |
size_t namelen = strlen (name) + 1; |
| 696 |
tree strtree = build_string (namelen, name); |
| 697 |
TREE_TYPE (strtree) = make_array_type (Type::tchar, namelen); |
| 698 |
CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, strtree); |
| 699 |
} |
| 700 |
|
| 701 |
TREE_TYPE (minfo) = type; |
| 702 |
DECL_INITIAL (minfo) = build_struct_literal (type, minit); |
| 703 |
d_finish_decl (minfo); |
| 704 |
|
| 705 |
/* Register the module against druntime. */ |
| 706 |
register_moduleinfo (decl, minfo); |
| 707 |
} |
| 708 |
|
| 709 |
/* Send the Module AST class DECL to GCC back-end. */ |
| 710 |
|
| 711 |
void |
| 712 |
build_module_tree (Module *decl) |
| 713 |
{ |
| 714 |
/* There may be more than one module per object file, but should only |
| 715 |
ever compile them one at a time. */ |
| 716 |
assert (!current_moduleinfo && !current_module_decl); |
| 717 |
|
| 718 |
module_info mi = module_info (); |
| 719 |
module_info mitest = module_info (); |
| 720 |
|
| 721 |
current_moduleinfo = &mi; |
| 722 |
current_testing_module = &mitest; |
| 723 |
current_module_decl = decl; |
| 724 |
|
| 725 |
vec<Declaration *> deferred_decls = vNULL; |
| 726 |
deferred_inline_declarations = &deferred_decls; |
| 727 |
|
| 728 |
/* Layout module members. */ |
| 729 |
if (decl->members) |
| 730 |
{ |
| 731 |
for (size_t i = 0; i < decl->members->length; i++) |
| 732 |
{ |
| 733 |
Dsymbol *s = (*decl->members)[i]; |
| 734 |
build_decl_tree (s); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
/* For libphobos-internal use only. Generate a separate module info symbol |
| 739 |
that references all compiled in unittests, this allows compiling library |
| 740 |
modules and linking to libphobos without having run-time conflicts because |
| 741 |
of two ModuleInfo records with the same name being present in two DSOs. */ |
| 742 |
if (flag_building_libphobos_tests) |
| 743 |
{ |
| 744 |
/* Associate the module info symbol with a mock module. */ |
| 745 |
const char *name = concat (GDC_PREFIX ("modtest__"), |
| 746 |
decl->ident->toChars (), NULL); |
| 747 |
Module *tm = Module::create (decl->arg.ptr, Identifier::idPool (name), |
| 748 |
0, 0); |
| 749 |
Dsymbols members; |
| 750 |
|
| 751 |
/* Setting parent puts module in the same package as the current, to |
| 752 |
avoid any symbol conflicts. */ |
| 753 |
tm->parent = decl->parent; |
| 754 |
tm->needmoduleinfo = decl->needmoduleinfo; |
| 755 |
tm->members = &members; |
| 756 |
/* Register the current module as being imported by the mock module. |
| 757 |
This informs run-time that there is a dependency between the two. */ |
| 758 |
tm->aimports.push (decl); |
| 759 |
|
| 760 |
if (mitest.ctors || mitest.ctorgates) |
| 761 |
tm->sctor = build_funcs_gates_fn (get_identifier ("*__modtestctor"), |
| 762 |
mitest.ctors, mitest.ctorgates); |
| 763 |
|
| 764 |
if (mitest.dtors) |
| 765 |
tm->sdtor = build_funcs_gates_fn (get_identifier ("*__modtestdtor"), |
| 766 |
mitest.dtors, NULL); |
| 767 |
|
| 768 |
if (mi.standalonectors) |
| 769 |
tm->sictor |
| 770 |
= build_funcs_gates_fn (get_identifier ("*__modtestsharedictor"), |
| 771 |
mi.standalonectors, NULL); |
| 772 |
|
| 773 |
if (mitest.sharedctors || mitest.sharedctorgates) |
| 774 |
tm->ssharedctor |
| 775 |
= build_funcs_gates_fn (get_identifier ("*__modtestsharedctor"), |
| 776 |
mitest.sharedctors, mitest.sharedctorgates); |
| 777 |
|
| 778 |
if (mitest.shareddtors) |
| 779 |
tm->sshareddtor |
| 780 |
= build_funcs_gates_fn (get_identifier ("*__modtestshareddtor"), |
| 781 |
mitest.shareddtors, NULL); |
| 782 |
|
| 783 |
if (mi.unitTests) |
| 784 |
tm->stest = build_funcs_gates_fn (get_identifier ("*__modtest"), |
| 785 |
mi.unitTests, NULL); |
| 786 |
|
| 787 |
mi.unitTests = NULL; |
| 788 |
layout_moduleinfo (tm); |
| 789 |
} |
| 790 |
|
| 791 |
/* Default behavior is to always generate module info because of templates. |
| 792 |
Can be switched off for not compiling against runtime library. */ |
| 793 |
if (global.params.useModuleInfo && Module::moduleinfo != NULL) |
| 794 |
{ |
| 795 |
if (mi.ctors || mi.ctorgates) |
| 796 |
decl->sctor = build_funcs_gates_fn (get_identifier ("*__modctor"), |
| 797 |
mi.ctors, mi.ctorgates); |
| 798 |
|
| 799 |
if (mi.dtors) |
| 800 |
decl->sdtor = build_funcs_gates_fn (get_identifier ("*__moddtor"), |
| 801 |
mi.dtors, NULL); |
| 802 |
|
| 803 |
if (mi.standalonectors) |
| 804 |
decl->sictor |
| 805 |
= build_funcs_gates_fn (get_identifier ("*__modsharedictor"), |
| 806 |
mi.standalonectors, NULL); |
| 807 |
|
| 808 |
if (mi.sharedctors || mi.sharedctorgates) |
| 809 |
decl->ssharedctor |
| 810 |
= build_funcs_gates_fn (get_identifier ("*__modsharedctor"), |
| 811 |
mi.sharedctors, mi.sharedctorgates); |
| 812 |
|
| 813 |
if (mi.shareddtors) |
| 814 |
decl->sshareddtor |
| 815 |
= build_funcs_gates_fn (get_identifier ("*__modshareddtor"), |
| 816 |
mi.shareddtors, NULL); |
| 817 |
|
| 818 |
if (mi.unitTests) |
| 819 |
decl->stest = build_funcs_gates_fn (get_identifier ("*__modtest"), |
| 820 |
mi.unitTests, NULL); |
| 821 |
|
| 822 |
layout_moduleinfo (decl); |
| 823 |
} |
| 824 |
|
| 825 |
/* Process all deferred functions after finishing module. */ |
| 826 |
for (size_t i = 0; i < deferred_decls.length (); ++i) |
| 827 |
build_decl_tree (deferred_decls[i]); |
| 828 |
|
| 829 |
current_moduleinfo = NULL; |
| 830 |
current_testing_module = NULL; |
| 831 |
current_module_decl = NULL; |
| 832 |
deferred_inline_declarations = NULL; |
| 833 |
} |
| 834 |
|
| 835 |
/* Returns the current function or module context for the purpose |
| 836 |
of imported_module_or_decl. */ |
| 837 |
|
| 838 |
tree |
| 839 |
d_module_context (void) |
| 840 |
{ |
| 841 |
if (cfun != NULL) |
| 842 |
return current_function_decl; |
| 843 |
|
| 844 |
gcc_assert (current_module_decl != NULL); |
| 845 |
return build_import_decl (current_module_decl); |
| 846 |
} |
| 847 |
|
| 848 |
/* Maybe record declaration D against our module information structure. */ |
| 849 |
|
| 850 |
void |
| 851 |
register_module_decl (Declaration *d) |
| 852 |
{ |
| 853 |
FuncDeclaration *fd = d->isFuncDeclaration (); |
| 854 |
if (fd != NULL) |
| 855 |
{ |
| 856 |
tree decl = get_symbol_decl (fd); |
| 857 |
|
| 858 |
/* Any module constructors or destructors that are only present when |
| 859 |
compiling in unittests are kept track of separately so they are |
| 860 |
not omitted when compiling with -fbuilding-libphobos-tests. */ |
| 861 |
module_info *minfo; |
| 862 |
if (flag_building_libphobos_tests && !fd->isUnitTestDeclaration () |
| 863 |
&& DECL_IN_UNITTEST_CONDITION_P (decl)) |
| 864 |
minfo = current_testing_module; |
| 865 |
else |
| 866 |
minfo = current_moduleinfo; |
| 867 |
|
| 868 |
gcc_assert (minfo != NULL); |
| 869 |
|
| 870 |
/* If a static constructor, push into the current ModuleInfo. |
| 871 |
Checks for `shared' first because it derives from the non-shared |
| 872 |
constructor type in the front-end. */ |
| 873 |
if (SharedStaticCtorDeclaration *sctor |
| 874 |
= fd->isSharedStaticCtorDeclaration ()) |
| 875 |
{ |
| 876 |
/* The `shared' static constructor was marked `@standalone'. */ |
| 877 |
if (sctor->standalone) |
| 878 |
vec_safe_push (minfo->standalonectors, decl); |
| 879 |
else |
| 880 |
vec_safe_push (minfo->sharedctors, decl); |
| 881 |
} |
| 882 |
else if (fd->isStaticCtorDeclaration ()) |
| 883 |
vec_safe_push (minfo->ctors, decl); |
| 884 |
|
| 885 |
/* If a static destructor, do same as with constructors, but also |
| 886 |
increment the destructor's vgate at construction time. */ |
| 887 |
if (fd->isSharedStaticDtorDeclaration ()) |
| 888 |
{ |
| 889 |
VarDeclaration *vgate = ((SharedStaticDtorDeclaration *) fd)->vgate; |
| 890 |
if (vgate != NULL) |
| 891 |
{ |
| 892 |
tree gate = get_symbol_decl (vgate); |
| 893 |
vec_safe_push (minfo->sharedctorgates, gate); |
| 894 |
} |
| 895 |
vec_safe_insert (minfo->shareddtors, 0, decl); |
| 896 |
} |
| 897 |
else if (fd->isStaticDtorDeclaration ()) |
| 898 |
{ |
| 899 |
VarDeclaration *vgate = ((StaticDtorDeclaration *) fd)->vgate; |
| 900 |
if (vgate != NULL) |
| 901 |
{ |
| 902 |
tree gate = get_symbol_decl (vgate); |
| 903 |
vec_safe_push (minfo->ctorgates, gate); |
| 904 |
} |
| 905 |
vec_safe_insert (minfo->dtors, 0, decl); |
| 906 |
} |
| 907 |
|
| 908 |
/* If a unittest function. */ |
| 909 |
if (fd->isUnitTestDeclaration ()) |
| 910 |
vec_safe_push (minfo->unitTests, decl); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
/* Add DECL as a declaration to emit at the end of the current module. */ |
| 915 |
|
| 916 |
void |
| 917 |
d_defer_declaration (Declaration *decl) |
| 918 |
{ |
| 919 |
gcc_assert (deferred_inline_declarations != NULL); |
| 920 |
deferred_inline_declarations->safe_push (decl); |
| 921 |
} |
| 922 |
|
| 923 |
/* Wrapup all global declarations and start the final compilation. */ |
| 924 |
|
| 925 |
void |
| 926 |
d_finish_compilation (tree *vec, int len) |
| 927 |
{ |
| 928 |
/* Complete all generated thunks. */ |
| 929 |
symtab->process_same_body_aliases (); |
| 930 |
|
| 931 |
/* Output debug information for all type declarations in this unit. */ |
| 932 |
for (int i = 0; i < len; i++) |
| 933 |
{ |
| 934 |
tree decl = vec[i]; |
| 935 |
if (TREE_CODE (decl) == TYPE_DECL) |
| 936 |
debug_hooks->type_decl (decl, false); |
| 937 |
} |
| 938 |
|
| 939 |
/* Process all file scopes in this compilation, and the external_scope, |
| 940 |
through wrapup_global_declarations. */ |
| 941 |
for (int i = 0; i < len; i++) |
| 942 |
{ |
| 943 |
tree decl = vec[i]; |
| 944 |
wrapup_global_declarations (&decl, 1); |
| 945 |
} |
| 946 |
} |