PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/d/modules.cc

http://github.com/D-Programming-GDC/GDC
C++ | 854 lines | 529 code | 163 blank | 162 comment | 83 complexity | e1caa2b9ee9f5cd49ae8b3ed3974b374 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /* modules.cc -- D module initialization and termination.
  2. Copyright (C) 2013-2018 Free Software Foundation, Inc.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC; see the file COPYING3. If not see
  13. <http://www.gnu.org/licenses/>. */
  14. #include "config.h"
  15. #include "system.h"
  16. #include "coretypes.h"
  17. #include "dmd/declaration.h"
  18. #include "dmd/identifier.h"
  19. #include "dmd/module.h"
  20. #include "tree.h"
  21. #include "fold-const.h"
  22. #include "tm.h"
  23. #include "function.h"
  24. #include "cgraph.h"
  25. #include "stor-layout.h"
  26. #include "toplev.h"
  27. #include "target.h"
  28. #include "common/common-target.h"
  29. #include "stringpool.h"
  30. #include "d-tree.h"
  31. /* D generates module information to inform the runtime library which modules
  32. need some kind of special handling. All `static this()', `static ~this()',
  33. and `unittest' functions for a given module are aggregated into a single
  34. function - one for each kind - and a pointer to that function is inserted
  35. into the ModuleInfo instance for that module.
  36. Module information for a particular module is indicated with an ABI defined
  37. structure derived from ModuleInfo. ModuleInfo is a variably sized struct
  38. with two fixed base fields. The first field `flags' determines what
  39. information is packed immediately after the record type.
  40. Like TypeInfo, the runtime library provides the definitions of the ModuleInfo
  41. structure, as well as accessors for the variadic fields. So we only define
  42. layout compatible POD_structs for ModuleInfo. */
  43. /* The internally represented ModuleInfo and CompilerDSO types. */
  44. static tree moduleinfo_type;
  45. static tree compiler_dso_type;
  46. static tree dso_registry_fn;
  47. /* The DSO slot for use by the druntime implementation. */
  48. static tree dso_slot_node;
  49. /* For registering and deregistering DSOs with druntime, we have one global
  50. constructor and destructor per object that calls _d_dso_registry with the
  51. respective DSO record. To ensure that this is only done once, a
  52. `dso_initialized' variable is introduced to guard repeated calls. */
  53. static tree dso_initialized_node;
  54. /* The beginning and end of the `minfo' section. */
  55. static tree start_minfo_node;
  56. static tree stop_minfo_node;
  57. /* Record information about module initialization, termination,
  58. unit testing, and thread local storage in the compilation. */
  59. struct GTY(()) module_info
  60. {
  61. vec<tree, va_gc> *ctors;
  62. vec<tree, va_gc> *dtors;
  63. vec<tree, va_gc> *ctorgates;
  64. vec<tree, va_gc> *sharedctors;
  65. vec<tree, va_gc> *shareddtors;
  66. vec<tree, va_gc> *sharedctorgates;
  67. vec<tree, va_gc> *unitTests;
  68. };
  69. /* These must match the values in libdruntime/object_.d. */
  70. enum module_info_flags
  71. {
  72. MIctorstart = 0x1,
  73. MIctordone = 0x2,
  74. MIstandalone = 0x4,
  75. MItlsctor = 0x8,
  76. MItlsdtor = 0x10,
  77. MIctor = 0x20,
  78. MIdtor = 0x40,
  79. MIxgetMembers = 0x80,
  80. MIictor = 0x100,
  81. MIunitTest = 0x200,
  82. MIimportedModules = 0x400,
  83. MIlocalClasses = 0x800,
  84. MIname = 0x1000
  85. };
  86. /* The ModuleInfo information structure for the module currently being compiled.
  87. Assuming that only ever process one at a time. */
  88. static module_info *current_moduleinfo;
  89. /* The declaration of the current module being compiled. */
  90. static Module *current_module_decl;
  91. /* Static constructors and destructors (not D `static this'). */
  92. static GTY(()) vec<tree, va_gc> *static_ctor_list;
  93. static GTY(()) vec<tree, va_gc> *static_dtor_list;
  94. /* Returns an internal function identified by IDENT. This is used
  95. by both module initialization and dso handlers. */
  96. static FuncDeclaration *
  97. get_internal_fn (tree ident)
  98. {
  99. Module *mod = current_module_decl;
  100. const char *name = IDENTIFIER_POINTER (ident);
  101. if (!mod)
  102. mod = Module::rootModule;
  103. if (name[0] == '*')
  104. {
  105. tree s = mangle_internal_decl (mod, name + 1, "FZv");
  106. name = IDENTIFIER_POINTER (s);
  107. }
  108. FuncDeclaration *fd = FuncDeclaration::genCfunc (NULL, Type::tvoid,
  109. Identifier::idPool (name));
  110. fd->loc = Loc (mod->srcfile->toChars (), 1, 0);
  111. fd->parent = mod;
  112. fd->protection.kind = Prot::private_;
  113. fd->semanticRun = PASSsemantic3done;
  114. return fd;
  115. }
  116. /* Generate an internal function identified by IDENT.
  117. The function body to add is in EXPR. */
  118. static tree
  119. build_internal_fn (tree ident, tree expr)
  120. {
  121. FuncDeclaration *fd = get_internal_fn (ident);
  122. tree decl = get_symbol_decl (fd);
  123. tree old_context = start_function (fd);
  124. rest_of_decl_compilation (decl, 1, 0);
  125. add_stmt (expr);
  126. finish_function (old_context);
  127. /* D static ctors, static dtors, unittests, and the ModuleInfo
  128. chain function are always private. */
  129. TREE_PUBLIC (decl) = 0;
  130. TREE_USED (decl) = 1;
  131. DECL_ARTIFICIAL (decl) = 1;
  132. return decl;
  133. }
  134. /* Build and emit a function identified by IDENT that increments (in order)
  135. all variables in GATES, then calls the list of functions in FUNCTIONS. */
  136. static tree
  137. build_funcs_gates_fn (tree ident, vec<tree, va_gc> *functions,
  138. vec<tree, va_gc> *gates)
  139. {
  140. tree expr_list = NULL_TREE;
  141. /* Increment gates first. */
  142. for (size_t i = 0; i < vec_safe_length (gates); i++)
  143. {
  144. tree decl = (*gates)[i];
  145. tree value = build2 (PLUS_EXPR, TREE_TYPE (decl),
  146. decl, integer_one_node);
  147. tree var_expr = modify_expr (decl, value);
  148. expr_list = compound_expr (expr_list, var_expr);
  149. }
  150. /* Call Functions. */
  151. for (size_t i = 0; i < vec_safe_length (functions); i++)
  152. {
  153. tree decl = (*functions)[i];
  154. tree call_expr = build_call_expr (decl, 0);
  155. expr_list = compound_expr (expr_list, call_expr);
  156. }
  157. if (expr_list)
  158. return build_internal_fn (ident, expr_list);
  159. return NULL_TREE;
  160. }
  161. /* Return the type for ModuleInfo, create it if it doesn't already exist. */
  162. static tree
  163. get_moduleinfo_type (void)
  164. {
  165. if (moduleinfo_type)
  166. return moduleinfo_type;
  167. /* Layout of ModuleInfo is:
  168. uint flags;
  169. uint index; */
  170. tree fields = create_field_decl (d_uint_type, NULL, 1, 1);
  171. DECL_CHAIN (fields) = create_field_decl (d_uint_type, NULL, 1, 1);
  172. moduleinfo_type = make_node (RECORD_TYPE);
  173. finish_builtin_struct (moduleinfo_type, "ModuleInfo", fields, NULL_TREE);
  174. return moduleinfo_type;
  175. }
  176. /* Get the VAR_DECL of the ModuleInfo for DECL. If this does not yet exist,
  177. create it. The ModuleInfo decl is used to keep track of constructors,
  178. destructors, unittests, members, classes, and imports for the given module.
  179. This is used by the D runtime for module initialization and termination. */
  180. static tree
  181. get_moduleinfo_decl (Module *decl)
  182. {
  183. if (decl->csym)
  184. return decl->csym;
  185. tree ident = mangle_internal_decl (decl, "__ModuleInfo", "Z");
  186. tree type = get_moduleinfo_type ();
  187. decl->csym = declare_extern_var (ident, type);
  188. DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (NULL);
  189. DECL_CONTEXT (decl->csym) = build_import_decl (decl);
  190. /* Not readonly, moduleinit depends on this. */
  191. TREE_READONLY (decl->csym) = 0;
  192. return decl->csym;
  193. }
  194. /* Return the type for CompilerDSOData, create it if it doesn't exist. */
  195. static tree
  196. get_compiler_dso_type (void)
  197. {
  198. if (compiler_dso_type)
  199. return compiler_dso_type;
  200. /* Layout of CompilerDSOData is:
  201. size_t version;
  202. void** slot;
  203. ModuleInfo** _minfo_beg;
  204. ModuleInfo** _minfo_end;
  205. FuncTable* _deh_beg;
  206. FuncTable* _deh_end;
  207. Note, finish_builtin_struct() expects these fields in reverse order. */
  208. tree fields = create_field_decl (ptr_type_node, NULL, 1, 1);
  209. tree field = create_field_decl (ptr_type_node, NULL, 1, 1);
  210. DECL_CHAIN (field) = fields;
  211. fields = field;
  212. field = create_field_decl (build_pointer_type (get_moduleinfo_type ()),
  213. NULL, 1, 1);
  214. DECL_CHAIN (field) = fields;
  215. fields = field;
  216. field = create_field_decl (build_pointer_type (get_moduleinfo_type ()),
  217. NULL, 1, 1);
  218. DECL_CHAIN (field) = fields;
  219. fields = field;
  220. field = create_field_decl (build_pointer_type (ptr_type_node), NULL, 1, 1);
  221. DECL_CHAIN (field) = fields;
  222. fields = field;
  223. field = create_field_decl (size_type_node, NULL, 1, 1);
  224. DECL_CHAIN (field) = fields;
  225. fields = field;
  226. compiler_dso_type = make_node (RECORD_TYPE);
  227. finish_builtin_struct (compiler_dso_type, "CompilerDSOData",
  228. fields, NULL_TREE);
  229. return compiler_dso_type;
  230. }
  231. /* Returns the _d_dso_registry FUNCTION_DECL. */
  232. static tree
  233. get_dso_registry_fn (void)
  234. {
  235. if (dso_registry_fn)
  236. return dso_registry_fn;
  237. tree dso_type = get_compiler_dso_type ();
  238. tree fntype = build_function_type_list (void_type_node,
  239. build_pointer_type (dso_type),
  240. NULL_TREE);
  241. dso_registry_fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
  242. get_identifier ("_d_dso_registry"), fntype);
  243. TREE_PUBLIC (dso_registry_fn) = 1;
  244. DECL_EXTERNAL (dso_registry_fn) = 1;
  245. return dso_registry_fn;
  246. }
  247. /* Depending on CTOR_P, builds and emits eiter a constructor or destructor
  248. calling _d_dso_registry if `dso_initialized' is `false' in a constructor
  249. or `true' in a destructor. */
  250. static tree
  251. build_dso_cdtor_fn (bool ctor_p)
  252. {
  253. const char *name = ctor_p ? GDC_PREFIX ("dso_ctor") : GDC_PREFIX ("dso_dtor");
  254. tree condition = ctor_p ? boolean_true_node : boolean_false_node;
  255. /* Declaration of dso_ctor/dso_dtor is:
  256. extern(C) void dso_{c,d}tor (void)
  257. {
  258. if (dso_initialized != condition)
  259. {
  260. dso_initialized = condition;
  261. CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo};
  262. _d_dso_registry (&dso);
  263. }
  264. }
  265. */
  266. FuncDeclaration *fd = get_internal_fn (get_identifier (name));
  267. tree decl = get_symbol_decl (fd);
  268. TREE_PUBLIC (decl) = 1;
  269. DECL_ARTIFICIAL (decl) = 1;
  270. DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  271. DECL_VISIBILITY_SPECIFIED (decl) = 1;
  272. d_comdat_linkage (decl);
  273. /* Start laying out the body. */
  274. tree old_context = start_function (fd);
  275. rest_of_decl_compilation (decl, 1, 0);
  276. /* if (dso_initialized != condition). */
  277. tree if_cond = build_boolop (NE_EXPR, dso_initialized_node, condition);
  278. /* dso_initialized = condition; */
  279. tree expr_list = modify_expr (dso_initialized_node, condition);
  280. /* CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo}; */
  281. tree dso_type = get_compiler_dso_type ();
  282. tree dso = build_local_temp (dso_type);
  283. vec<constructor_elt, va_gc> *ve = NULL;
  284. CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_integer_cst (1, size_type_node));
  285. CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (dso_slot_node));
  286. CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (start_minfo_node));
  287. CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (stop_minfo_node));
  288. tree assign_expr = modify_expr (dso, build_struct_literal (dso_type, ve));
  289. expr_list = compound_expr (expr_list, assign_expr);
  290. /* _d_dso_registry (&dso); */
  291. tree call_expr = build_call_expr (get_dso_registry_fn (), 1,
  292. build_address (dso));
  293. expr_list = compound_expr (expr_list, call_expr);
  294. add_stmt (build_vcondition (if_cond, expr_list, void_node));
  295. finish_function (old_context);
  296. return decl;
  297. }
  298. /* Build a variable used in the dso_registry code identified by NAME,
  299. and data type TYPE. The variable always has VISIBILITY_HIDDEN and
  300. TREE_PUBLIC flags set. */
  301. static tree
  302. build_dso_registry_var (const char * name, tree type)
  303. {
  304. tree var = declare_extern_var (get_identifier (name), type);
  305. DECL_VISIBILITY (var) = VISIBILITY_HIDDEN;
  306. DECL_VISIBILITY_SPECIFIED (var) = 1;
  307. return var;
  308. }
  309. /* Place a reference to the ModuleInfo symbol MINFO for DECL into the
  310. `minfo' section. Then create the global ctors/dtors to call the
  311. _d_dso_registry function if necessary. */
  312. static void
  313. register_moduleinfo (Module *decl, tree minfo)
  314. {
  315. gcc_assert (targetm_common.have_named_sections);
  316. /* Build the ModuleInfo reference, this is done once for every Module. */
  317. tree ident = mangle_internal_decl (decl, "__moduleRef", "Z");
  318. tree mref = declare_extern_var (ident, ptr_type_node);
  319. /* Build the initializer and emit. Do not start section with a `.' character
  320. so that the linker will provide a __start_ and __stop_ symbol to indicate
  321. the start and end address of the section respectively.
  322. https://sourceware.org/binutils/docs-2.26/ld/Orphan-Sections.html. */
  323. DECL_INITIAL (mref) = build_address (minfo);
  324. DECL_EXTERNAL (mref) = 0;
  325. DECL_PRESERVE_P (mref) = 1;
  326. set_decl_section_name (mref, "minfo");
  327. d_pushdecl (mref);
  328. rest_of_decl_compilation (mref, 1, 0);
  329. /* Only for the first D module being emitted do we need to generate a static
  330. constructor and destructor for. These are only required once per shared
  331. library, so it's safe to emit them only once per object file. */
  332. static bool first_module = true;
  333. if (!first_module)
  334. return;
  335. start_minfo_node = build_dso_registry_var ("__start_minfo", ptr_type_node);
  336. rest_of_decl_compilation (start_minfo_node, 1, 0);
  337. stop_minfo_node = build_dso_registry_var ("__stop_minfo", ptr_type_node);
  338. rest_of_decl_compilation (stop_minfo_node, 1, 0);
  339. /* Declare dso_slot and dso_initialized. */
  340. dso_slot_node = build_dso_registry_var (GDC_PREFIX ("dso_slot"),
  341. ptr_type_node);
  342. DECL_EXTERNAL (dso_slot_node) = 0;
  343. d_comdat_linkage (dso_slot_node);
  344. rest_of_decl_compilation (dso_slot_node, 1, 0);
  345. dso_initialized_node = build_dso_registry_var (GDC_PREFIX ("dso_initialized"),
  346. boolean_type_node);
  347. DECL_EXTERNAL (dso_initialized_node) = 0;
  348. d_comdat_linkage (dso_initialized_node);
  349. rest_of_decl_compilation (dso_initialized_node, 1, 0);
  350. /* Declare dso_ctor() and dso_dtor(). */
  351. tree dso_ctor = build_dso_cdtor_fn (true);
  352. vec_safe_push (static_ctor_list, dso_ctor);
  353. tree dso_dtor = build_dso_cdtor_fn (false);
  354. vec_safe_push (static_dtor_list, dso_dtor);
  355. first_module = false;
  356. }
  357. /* Convenience function for layout_moduleinfo_fields. Adds a field of TYPE to
  358. the moduleinfo record at OFFSET, incrementing the offset to the next field
  359. position. No alignment is taken into account, all fields are packed. */
  360. static void
  361. layout_moduleinfo_field (tree type, tree rec_type, HOST_WIDE_INT& offset)
  362. {
  363. tree field = create_field_decl (type, NULL, 1, 1);
  364. insert_aggregate_field (rec_type, field, offset);
  365. offset += int_size_in_bytes (type);
  366. }
  367. /* Layout fields that immediately come after the moduleinfo TYPE for DECL.
  368. Data relating to the module is packed into the type on an as-needed
  369. basis, this is done to keep its size to a minimum. */
  370. static tree
  371. layout_moduleinfo_fields (Module *decl, tree type)
  372. {
  373. HOST_WIDE_INT offset = int_size_in_bytes (type);
  374. type = copy_aggregate_type (type);
  375. /* First fields added are all the function pointers. */
  376. if (decl->sctor)
  377. layout_moduleinfo_field (ptr_type_node, type, offset);
  378. if (decl->sdtor)
  379. layout_moduleinfo_field (ptr_type_node, type, offset);
  380. if (decl->ssharedctor)
  381. layout_moduleinfo_field (ptr_type_node, type, offset);
  382. if (decl->sshareddtor)
  383. layout_moduleinfo_field (ptr_type_node, type, offset);
  384. if (decl->findGetMembers ())
  385. layout_moduleinfo_field (ptr_type_node, type, offset);
  386. if (decl->sictor)
  387. layout_moduleinfo_field (ptr_type_node, type, offset);
  388. if (decl->stest)
  389. layout_moduleinfo_field (ptr_type_node, type, offset);
  390. /* Array of module imports is laid out as a length field, followed by
  391. a static array of ModuleInfo pointers. */
  392. size_t aimports_dim = decl->aimports.dim;
  393. for (size_t i = 0; i < decl->aimports.dim; i++)
  394. {
  395. Module *mi = decl->aimports[i];
  396. if (!mi->needmoduleinfo)
  397. aimports_dim--;
  398. }
  399. if (aimports_dim)
  400. {
  401. layout_moduleinfo_field (size_type_node, type, offset);
  402. layout_moduleinfo_field (make_array_type (Type::tvoidptr, aimports_dim),
  403. type, offset);
  404. }
  405. /* Array of local ClassInfo decls are laid out in the same way. */
  406. ClassDeclarations aclasses;
  407. for (size_t i = 0; i < decl->members->dim; i++)
  408. {
  409. Dsymbol *member = (*decl->members)[i];
  410. member->addLocalClass (&aclasses);
  411. }
  412. if (aclasses.dim)
  413. {
  414. layout_moduleinfo_field (size_type_node, type, offset);
  415. layout_moduleinfo_field (make_array_type (Type::tvoidptr, aclasses.dim),
  416. type, offset);
  417. }
  418. /* Lastly, the name of the module is a static char array. */
  419. size_t namelen = strlen (decl->toPrettyChars ()) + 1;
  420. layout_moduleinfo_field (make_array_type (Type::tchar, namelen),
  421. type, offset);
  422. finish_aggregate_type (offset, 1, type, NULL);
  423. return type;
  424. }
  425. /* Output the ModuleInfo for module DECL and register it with druntime. */
  426. static void
  427. layout_moduleinfo (Module *decl)
  428. {
  429. ClassDeclarations aclasses;
  430. FuncDeclaration *sgetmembers;
  431. for (size_t i = 0; i < decl->members->dim; i++)
  432. {
  433. Dsymbol *member = (*decl->members)[i];
  434. member->addLocalClass (&aclasses);
  435. }
  436. size_t aimports_dim = decl->aimports.dim;
  437. for (size_t i = 0; i < decl->aimports.dim; i++)
  438. {
  439. Module *mi = decl->aimports[i];
  440. if (!mi->needmoduleinfo)
  441. aimports_dim--;
  442. }
  443. sgetmembers = decl->findGetMembers ();
  444. size_t flags = 0;
  445. if (decl->sctor)
  446. flags |= MItlsctor;
  447. if (decl->sdtor)
  448. flags |= MItlsdtor;
  449. if (decl->ssharedctor)
  450. flags |= MIctor;
  451. if (decl->sshareddtor)
  452. flags |= MIdtor;
  453. if (sgetmembers)
  454. flags |= MIxgetMembers;
  455. if (decl->sictor)
  456. flags |= MIictor;
  457. if (decl->stest)
  458. flags |= MIunitTest;
  459. if (aimports_dim)
  460. flags |= MIimportedModules;
  461. if (aclasses.dim)
  462. flags |= MIlocalClasses;
  463. if (!decl->needmoduleinfo)
  464. flags |= MIstandalone;
  465. flags |= MIname;
  466. tree minfo = get_moduleinfo_decl (decl);
  467. tree type = layout_moduleinfo_fields (decl, TREE_TYPE (minfo));
  468. /* Put out the two named fields in a ModuleInfo decl:
  469. uint flags;
  470. uint index; */
  471. vec<constructor_elt, va_gc> *minit = NULL;
  472. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  473. build_integer_cst (flags, d_uint_type));
  474. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  475. build_integer_cst (0, d_uint_type));
  476. /* Order of appearance, depending on flags:
  477. void function() tlsctor;
  478. void function() tlsdtor;
  479. void* function() xgetMembers;
  480. void function() ctor;
  481. void function() dtor;
  482. void function() ictor;
  483. void function() unitTest;
  484. ModuleInfo*[] importedModules;
  485. TypeInfo_Class[] localClasses;
  486. char[N] name;
  487. */
  488. if (flags & MItlsctor)
  489. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sctor));
  490. if (flags & MItlsdtor)
  491. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sdtor));
  492. if (flags & MIctor)
  493. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  494. build_address (decl->ssharedctor));
  495. if (flags & MIdtor)
  496. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  497. build_address (decl->sshareddtor));
  498. if (flags & MIxgetMembers)
  499. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  500. build_address (get_symbol_decl (sgetmembers)));
  501. if (flags & MIictor)
  502. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sictor));
  503. if (flags & MIunitTest)
  504. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->stest));
  505. if (flags & MIimportedModules)
  506. {
  507. vec<constructor_elt, va_gc> *elms = NULL;
  508. tree satype = make_array_type (Type::tvoidptr, aimports_dim);
  509. size_t idx = 0;
  510. for (size_t i = 0; i < decl->aimports.dim; i++)
  511. {
  512. Module *mi = decl->aimports[i];
  513. if (mi->needmoduleinfo)
  514. {
  515. CONSTRUCTOR_APPEND_ELT (elms, size_int (idx),
  516. build_address (get_moduleinfo_decl (mi)));
  517. idx++;
  518. }
  519. }
  520. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aimports_dim));
  521. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  522. build_constructor (satype, elms));
  523. }
  524. if (flags & MIlocalClasses)
  525. {
  526. vec<constructor_elt, va_gc> *elms = NULL;
  527. tree satype = make_array_type (Type::tvoidptr, aclasses.dim);
  528. for (size_t i = 0; i < aclasses.dim; i++)
  529. {
  530. ClassDeclaration *cd = aclasses[i];
  531. CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
  532. build_address (get_classinfo_decl (cd)));
  533. }
  534. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aclasses.dim));
  535. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
  536. build_constructor (satype, elms));
  537. }
  538. if (flags & MIname)
  539. {
  540. /* Put out module name as a 0-terminated C-string, to save bytes. */
  541. const char *name = decl->toPrettyChars ();
  542. size_t namelen = strlen (name) + 1;
  543. tree strtree = build_string (namelen, name);
  544. TREE_TYPE (strtree) = make_array_type (Type::tchar, namelen);
  545. CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, strtree);
  546. }
  547. TREE_TYPE (minfo) = type;
  548. DECL_INITIAL (minfo) = build_struct_literal (type, minit);
  549. d_finish_decl (minfo);
  550. /* Register the module against druntime. */
  551. register_moduleinfo (decl, minfo);
  552. }
  553. /* Send the Module AST class DECL to GCC back-end. */
  554. void
  555. build_module_tree (Module *decl)
  556. {
  557. /* There may be more than one module per object file, but should only
  558. ever compile them one at a time. */
  559. assert (!current_moduleinfo && !current_module_decl);
  560. module_info mi = module_info ();
  561. current_moduleinfo = &mi;
  562. current_module_decl = decl;
  563. /* Layout module members. */
  564. if (decl->members)
  565. {
  566. for (size_t i = 0; i < decl->members->dim; i++)
  567. {
  568. Dsymbol *s = (*decl->members)[i];
  569. build_decl_tree (s);
  570. }
  571. }
  572. /* Default behavior is to always generate module info because of templates.
  573. Can be switched off for not compiling against runtime library. */
  574. if (global.params.useModuleInfo
  575. && Module::moduleinfo != NULL
  576. && decl->ident != Identifier::idPool ("__entrypoint"))
  577. {
  578. if (mi.ctors || mi.ctorgates)
  579. decl->sctor = build_funcs_gates_fn (get_identifier ("*__modctor"),
  580. mi.ctors, mi.ctorgates);
  581. if (mi.dtors)
  582. decl->sdtor = build_funcs_gates_fn (get_identifier ("*__moddtor"),
  583. mi.dtors, NULL);
  584. if (mi.sharedctors || mi.sharedctorgates)
  585. decl->ssharedctor
  586. = build_funcs_gates_fn (get_identifier ("*__modsharedctor"),
  587. mi.sharedctors, mi.sharedctorgates);
  588. if (mi.shareddtors)
  589. decl->sshareddtor
  590. = build_funcs_gates_fn (get_identifier ("*__modshareddtor"),
  591. mi.shareddtors, NULL);
  592. if (mi.unitTests)
  593. decl->stest = build_funcs_gates_fn (get_identifier ("*__modtest"),
  594. mi.unitTests, NULL);
  595. layout_moduleinfo (decl);
  596. }
  597. current_moduleinfo = NULL;
  598. current_module_decl = NULL;
  599. }
  600. /* Returns the current function or module context for the purpose
  601. of imported_module_or_decl. */
  602. tree
  603. d_module_context (void)
  604. {
  605. if (cfun != NULL)
  606. return current_function_decl;
  607. gcc_assert (current_module_decl != NULL);
  608. return build_import_decl (current_module_decl);
  609. }
  610. /* Maybe record declaration D against our module information structure. */
  611. void
  612. register_module_decl (Declaration *d)
  613. {
  614. FuncDeclaration *fd = d->isFuncDeclaration ();
  615. if (fd != NULL)
  616. {
  617. tree decl = get_symbol_decl (fd);
  618. /* If a static constructor, push into the current ModuleInfo.
  619. Checks for `shared' first because it derives from the non-shared
  620. constructor type in the front-end. */
  621. if (fd->isSharedStaticCtorDeclaration ())
  622. vec_safe_push (current_moduleinfo->sharedctors, decl);
  623. else if (fd->isStaticCtorDeclaration ())
  624. vec_safe_push (current_moduleinfo->ctors, decl);
  625. /* If a static destructor, do same as with constructors, but also
  626. increment the destructor's vgate at construction time. */
  627. if (fd->isSharedStaticDtorDeclaration ())
  628. {
  629. VarDeclaration *vgate = ((SharedStaticDtorDeclaration *) fd)->vgate;
  630. if (vgate != NULL)
  631. {
  632. tree gate = get_symbol_decl (vgate);
  633. vec_safe_push (current_moduleinfo->sharedctorgates, gate);
  634. }
  635. vec_safe_insert (current_moduleinfo->shareddtors, 0, decl);
  636. }
  637. else if (fd->isStaticDtorDeclaration ())
  638. {
  639. VarDeclaration *vgate = ((StaticDtorDeclaration *) fd)->vgate;
  640. if (vgate != NULL)
  641. {
  642. tree gate = get_symbol_decl (vgate);
  643. vec_safe_push (current_moduleinfo->ctorgates, gate);
  644. }
  645. vec_safe_insert (current_moduleinfo->dtors, 0, decl);
  646. }
  647. /* If a unittest function. */
  648. if (fd->isUnitTestDeclaration ())
  649. vec_safe_push (current_moduleinfo->unitTests, decl);
  650. }
  651. }
  652. /* Wrapup all global declarations and start the final compilation. */
  653. void
  654. d_finish_compilation (tree *vec, int len)
  655. {
  656. /* Complete all generated thunks. */
  657. symtab->process_same_body_aliases ();
  658. /* Process all file scopes in this compilation, and the external_scope,
  659. through wrapup_global_declarations. */
  660. for (int i = 0; i < len; i++)
  661. {
  662. tree decl = vec[i];
  663. wrapup_global_declarations (&decl, 1);
  664. }
  665. /* If the target does not directly support static constructors,
  666. static_ctor_list contains a list of all static constructors defined
  667. so far. This routine will create a function to call all of those
  668. and is picked up by collect2. */
  669. if (static_ctor_list)
  670. {
  671. tree decl = build_funcs_gates_fn (get_file_function_name ("I"),
  672. static_ctor_list, NULL);
  673. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  674. decl_init_priority_insert (decl, DEFAULT_INIT_PRIORITY);
  675. }
  676. if (static_dtor_list)
  677. {
  678. tree decl = build_funcs_gates_fn (get_file_function_name ("D"),
  679. static_dtor_list, NULL);
  680. DECL_STATIC_DESTRUCTOR (decl) = 1;
  681. decl_fini_priority_insert (decl, DEFAULT_INIT_PRIORITY);
  682. }
  683. }
  684. #include "gt-d-modules.h"