Cthulhu  0.2.10
Cthulhu compiler collection
loader.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "common.h"
4 
5 #include "arena/arena.h"
6 #include "base/panic.h"
7 
8 #include <ctu_config.h>
9 
10 #if CT_BUILD_STATIC
11 # include "enum_modules.h"
12 #endif
13 
14 #include "std/typed/vector.h"
15 
16 static const loader_config_t kConfig = eLoadNone
17 #if CT_BUILD_STATIC
18  | eLoadStatic
19 #endif
20 #if CT_BUILD_SHARED
21  | eLoadDynamic
22 #endif
23 ;
24 
26 {
27  loaded_module_t mod = {
28  .type = eModNone,
29  .error = error,
30  .os = os
31  };
32  return mod;
33 }
34 
36 {
37  return kConfig;
38 }
39 
42 {
43  CTASSERT(arena != NULL);
44 
45  loader_t *loader = ARENA_MALLOC(sizeof(loader_t), "loader", NULL, arena);
46  loader->arena = arena;
47 
48  return loader;
49 }
50 
53 {
54  CTASSERT(loader != NULL);
55 
56 #if CT_BUILD_STATIC
57  static_modules_t mods = get_static_modules();
58 
59  typevec_t *vec = typevec_new(sizeof(loaded_module_t), CT_LANG_COUNT + CT_PLUGIN_COUNT + CT_TARGET_COUNT, loader->arena);
60 
61 #if CT_LANG_COUNT > 0
62  for (size_t i = 0; i < CT_LANG_COUNT; i++)
63  {
64  const language_t *lang = mods.langs[i];
65  loaded_module_t mod = {
66  .type = eModLanguage,
67  .lang = lang
68  };
69  typevec_push(vec, &mod);
70  }
71 #endif
72 
73 #if CT_PLUGIN_COUNT > 0
74  for (size_t i = 0; i < CT_PLUGIN_COUNT; i++)
75  {
76  const plugin_t *plugin = mods.plugins[i];
77  loaded_module_t mod = {
78  .type = eModPlugin,
79  .plugin = plugin
80  };
81  typevec_push(vec, &mod);
82  }
83 #endif
84 
85 #if CT_TARGET_COUNT > 0
86  for (size_t i = 0; i < CT_TARGET_COUNT; i++)
87  {
88  const target_t *target = mods.targets[i];
89  loaded_module_t mod = {
90  .type = eModTarget,
91  .target = target
92  };
93  typevec_push(vec, &mod);
94  }
95 #endif
96 
97  return vec;
98 #else
99  return typevec_new(sizeof(loaded_module_t), 0, loader->arena);
100 #endif
101 }
102 
103 STA_DECL
104 loaded_module_t load_module(loader_t *loader, module_type_t mask, const char *name)
105 {
106  CTASSERT(loader != NULL);
107  CTASSERT(name != NULL);
108 
109  loaded_module_t shared_mod = load_shared_module(loader, mask, name);
110  loaded_module_t static_mod = load_static_module(loader, mask, name);
111 
112  loaded_module_t result = {
113  .type = static_mod.type | shared_mod.type,
114  .error = static_mod.error | shared_mod.error,
115  .os = static_mod.os | shared_mod.os,
116  };
117 
118  // select the shared modules if they are available
119  result.lang = shared_mod.lang ? shared_mod.lang : static_mod.lang;
120  result.plugin = shared_mod.plugin ? shared_mod.plugin : static_mod.plugin;
121  result.target = shared_mod.target ? shared_mod.target : static_mod.target;
122 
123  return result;
124 }
125 
126 static const char *const kErrorStrings[eErrorCount] = {
127 #define LOADER_ERROR(ID, STR) [ID] = (STR),
128 #include "support/loader.inc"
129 };
130 
131 const char *load_error_string(load_error_t error)
132 {
133  CTASSERTF(error < eErrorCount, "invalid error code: %d", error);
134  return kErrorStrings[error];
135 }
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
#define ARENA_MALLOC(size, name, parent, arena)
allocate memory from a custom allocator
Definition: arena.h:392
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
#define CTASSERTF(expr,...)
assert a condition with a message and optional format arguments
Definition: panic.h:116
CT_STD_API void * typevec_push(typevec_t *vec, const void *src)
push a value onto the vector
Definition: vector.c:156
CT_NODISCARD CT_STD_API typevec_t * typevec_new(size_t width, size_t len, arena_t *arena)
create a new typed vector on the heap
Definition: vector.c:77
const char * load_error_string(load_error_t error)
Definition: loader.c:131
loaded_module_t load_error(load_error_t error, os_error_t os)
Definition: loader.c:25
STA_DECL loader_t * loader_new(arena_t *arena)
Definition: loader.c:41
STA_DECL typevec_t * load_default_modules(loader_t *loader)
Definition: loader.c:52
STA_DECL loaded_module_t load_module(loader_t *loader, module_type_t mask, const char *name)
Definition: loader.c:104
loader_config_t loader_config(void)
Definition: loader.c:35
CT_SUPPORT_API loaded_module_t load_shared_module(loader_t *loader, module_type_t mask, const char *name)
Definition: disable_shared.c:8
CT_SUPPORT_API loaded_module_t load_static_module(loader_t *loader, module_type_t mask, const char *name)
Definition: disable_static.c:8
module_type_t
Definition: loader.h:32
loader_config_t
Definition: loader.h:26
load_error_t
Definition: loader.h:40
@ eErrorCount
Definition: loader.h:44
an allocator object
Definition: arena.h:86
a language driver support capabilities
Definition: broker.h:143
load_error_t error
Definition: loader.h:55
const plugin_t * plugin
Definition: loader.h:52
module_type_t type
Definition: loader.h:49
const target_t * target
Definition: loader.h:53
os_error_t os
Definition: loader.h:56
const language_t * lang
Definition: loader.h:51
Definition: common.h:8
arena_t * arena
Definition: common.h:9
plugin support capabilities
Definition: broker.h:202
a codegen target backend
Definition: broker.h:232
A vector with a fixed type size.
Definition: vector.h:24