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