1 /*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
2 |*                                                                            *|
3 |*                     The LLVM Compiler Infrastructure                       *|
4 |*                                                                            *|
5 |* This file is distributed under the University of Illinois Open Source      *|
6 |* License. See LICENSE.TXT for details.                                      *|
7 |*                                                                            *|
8 |*===----------------------------------------------------------------------===*|
9 |*                                                                            *|
10 |* This header provides public interface to an abstract link time optimization*|
11 |* library.  LLVM provides an implementation of this interface for use with   *|
12 |* llvm bitcode files.                                                        *|
13 |*                                                                            *|
14 \*===----------------------------------------------------------------------===*/
15 
16 module deimos.llvm.c.lto;
17 
18 import core.stdc.stddef;
19 import core.sys.posix.unistd;
20 
21 extern(C) nothrow:
22 
23 /**
24  * @defgroup LLVMCLTO LTO
25  * @ingroup LLVMC
26  *
27  * @{
28  */
29 
30 enum LTO_API_VERSION = 4;
31 
32 alias int lto_symbol_attributes;
33 enum : lto_symbol_attributes {
34     LTO_SYMBOL_ALIGNMENT_MASK              = 0x0000001F, /* log2 of alignment */
35     LTO_SYMBOL_PERMISSIONS_MASK            = 0x000000E0,
36     LTO_SYMBOL_PERMISSIONS_CODE            = 0x000000A0,
37     LTO_SYMBOL_PERMISSIONS_DATA            = 0x000000C0,
38     LTO_SYMBOL_PERMISSIONS_RODATA          = 0x00000080,
39     LTO_SYMBOL_DEFINITION_MASK             = 0x00000700,
40     LTO_SYMBOL_DEFINITION_REGULAR          = 0x00000100,
41     LTO_SYMBOL_DEFINITION_TENTATIVE        = 0x00000200,
42     LTO_SYMBOL_DEFINITION_WEAK             = 0x00000300,
43     LTO_SYMBOL_DEFINITION_UNDEFINED        = 0x00000400,
44     LTO_SYMBOL_DEFINITION_WEAKUNDEF        = 0x00000500,
45     LTO_SYMBOL_SCOPE_MASK                  = 0x00003800,
46     LTO_SYMBOL_SCOPE_INTERNAL              = 0x00000800,
47     LTO_SYMBOL_SCOPE_HIDDEN                = 0x00001000,
48     LTO_SYMBOL_SCOPE_PROTECTED             = 0x00002000,
49     LTO_SYMBOL_SCOPE_DEFAULT               = 0x00001800,
50     LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800
51 }
52 
53 alias int lto_debug_model;
54 enum : lto_debug_model {
55     LTO_DEBUG_MODEL_NONE         = 0,
56     LTO_DEBUG_MODEL_DWARF        = 1
57 }
58 
59 alias int lto_codegen_model;
60 enum : lto_codegen_model {
61     LTO_CODEGEN_PIC_MODEL_STATIC         = 0,
62     LTO_CODEGEN_PIC_MODEL_DYNAMIC        = 1,
63     LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2
64 }
65 
66 
67 /** opaque reference to a loaded object module */
68 struct __LTOModule {};
69 alias __LTOModule*         lto_module_t;
70 
71 /** opaque reference to a code generator */
72 struct __LTOCodeGenerator  {};
73 alias __LTOCodeGenerator*  lto_code_gen_t;
74 
75 /**
76  * Returns a printable string.
77  */
78 extern const(char)*
79 lto_get_version();
80 
81 
82 /**
83  * Returns the last error string or NULL if last operation was successful.
84  */
85 extern const(char)*
86 lto_get_error_message();
87 
88 /**
89  * Checks if a file is a loadable object file.
90  */
91 extern bool
92 lto_module_is_object_file(const(char)* path);
93 
94 
95 /**
96  * Checks if a file is a loadable object compiled for requested target.
97  */
98 extern bool
99 lto_module_is_object_file_for_target(const(char)* path,
100                                      const(char)* target_triple_prefix);
101 
102 
103 /**
104  * Checks if a buffer is a loadable object file.
105  */
106 extern bool
107 lto_module_is_object_file_in_memory(const(void)* mem, size_t length);
108 
109 
110 /**
111  * Checks if a buffer is a loadable object compiled for requested target.
112  */
113 extern bool
114 lto_module_is_object_file_in_memory_for_target(const(void)* mem, size_t length,
115                                               const(char)* target_triple_prefix);
116 
117 
118 /**
119  * Loads an object file from disk.
120  * Returns NULL on error (check lto_get_error_message() for details).
121  */
122 extern lto_module_t
123 lto_module_create(const(char)* path);
124 
125 
126 /**
127  * Loads an object file from memory.
128  * Returns NULL on error (check lto_get_error_message() for details).
129  */
130 extern lto_module_t
131 lto_module_create_from_memory(const(void)* mem, size_t length);
132 
133 /**
134  * Loads an object file from disk. The seek point of fd is not preserved.
135  * Returns NULL on error (check lto_get_error_message() for details).
136  */
137 extern lto_module_t
138 lto_module_create_from_fd(int fd, const(char) *path, size_t file_size);
139 
140 /**
141  * Loads an object file from disk. The seek point of fd is not preserved.
142  * Returns NULL on error (check lto_get_error_message() for details).
143  */
144 extern lto_module_t
145 lto_module_create_from_fd_at_offset(int fd, const(char) *path, size_t file_size,
146                                     size_t map_size, off_t offset);
147 
148 
149 /**
150  * Frees all memory internally allocated by the module.
151  * Upon return the lto_module_t is no longer valid.
152  */
153 extern void
154 lto_module_dispose(lto_module_t mod);
155 
156 
157 /**
158  * Returns triple string which the object module was compiled under.
159  */
160 extern const(char)*
161 lto_module_get_target_triple(lto_module_t mod);
162 
163 /**
164  * Sets triple string with which the object will be codegened.
165  */
166 extern void
167 lto_module_set_target_triple(lto_module_t mod, const(char) *triple);
168 
169 
170 /**
171  * Returns the number of symbols in the object module.
172  */
173 extern uint
174 lto_module_get_num_symbols(lto_module_t mod);
175 
176 
177 /**
178  * Returns the name of the ith symbol in the object module.
179  */
180 extern const(char)*
181 lto_module_get_symbol_name(lto_module_t mod, uint index);
182 
183 
184 /**
185  * Returns the attributes of the ith symbol in the object module.
186  */
187 extern lto_symbol_attributes
188 lto_module_get_symbol_attribute(lto_module_t mod, uint index);
189 
190 
191 /**
192  * Instantiates a code generator.
193  * Returns NULL on error (check lto_get_error_message() for details).
194  */
195 extern lto_code_gen_t
196 lto_codegen_create();
197 
198 
199 /**
200  * Frees all code generator and all memory it internally allocated.
201  * Upon return the lto_code_gen_t is no longer valid.
202  */
203 extern void
204 lto_codegen_dispose(lto_code_gen_t);
205 
206 
207 
208 /**
209  * Add an object module to the set of modules for which code will be generated.
210  * Returns true on error (check lto_get_error_message() for details).
211  */
212 extern bool
213 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
214 
215 
216 
217 /**
218  * Sets if debug info should be generated.
219  * Returns true on error (check lto_get_error_message() for details).
220  */
221 extern bool
222 lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
223 
224 
225 /**
226  * Sets which PIC code model to generated.
227  * Returns true on error (check lto_get_error_message() for details).
228  */
229 extern bool
230 lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
231 
232 
233 /**
234  * Sets the cpu to generate code for.
235  */
236 extern void
237 lto_codegen_set_cpu(lto_code_gen_t cg, const(char) *cpu);
238 
239 
240 /**
241  * Sets the location of the assembler tool to run. If not set, libLTO
242  * will use gcc to invoke the assembler.
243  */
244 extern void
245 lto_codegen_set_assembler_path(lto_code_gen_t cg, const(char)* path);
246 
247 /**
248  * Sets extra arguments that libLTO should pass to the assembler.
249  */
250 extern void
251 lto_codegen_set_assembler_args(lto_code_gen_t cg, const(char) **args,
252                                int nargs);
253 
254 /**
255  * Adds to a list of all global symbols that must exist in the final
256  * generated code.  If a function is not listed, it might be
257  * inlined into every usage and optimized away.
258  */
259 extern void
260 lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const(char)* symbol);
261 
262 /**
263  * Writes a new object file at the specified path that contains the
264  * merged contents of all modules added so far.
265  * Returns true on error (check lto_get_error_message() for details).
266  */
267 extern bool
268 lto_codegen_write_merged_modules(lto_code_gen_t cg, const(char)* path);
269 
270 /**
271  * Generates code for all added modules into one native object file.
272  * On success returns a pointer to a generated mach-o/ELF buffer and
273  * length set to the buffer size.  The buffer is owned by the
274  * lto_code_gen_t and will be freed when lto_codegen_dispose()
275  * is called, or lto_codegen_compile() is called again.
276  * On failure, returns NULL (check lto_get_error_message() for details).
277  */
278 extern const(void)*
279 lto_codegen_compile(lto_code_gen_t cg, size_t* length);
280 
281 /**
282  * Generates code for all added modules into one native object file.
283  * The name of the file is written to name. Returns true on error.
284  */
285 extern bool
286 lto_codegen_compile_to_file(lto_code_gen_t cg, const(char)** name);
287 
288 
289 /**
290  * Sets options to help debug codegen bugs.
291  */
292 extern void
293 lto_codegen_debug_options(lto_code_gen_t cg, const(char) *);
294 
295 /**
296  * Initializes LLVM disassemblers.
297  * FIXME: This doesn't really belong here.
298  */
299 extern void
300 lto_initialize_disassembler();
301 
302 /**
303  * @}
304  */