LLVM API Documentation

lto.h
Go to the documentation of this file.
00001 /*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
00002 |*                                                                            *|
00003 |*                     The LLVM Compiler Infrastructure                       *|
00004 |*                                                                            *|
00005 |* This file is distributed under the University of Illinois Open Source      *|
00006 |* License. See LICENSE.TXT for details.                                      *|
00007 |*                                                                            *|
00008 |*===----------------------------------------------------------------------===*|
00009 |*                                                                            *|
00010 |* This header provides public interface to an abstract link time optimization*|
00011 |* library.  LLVM provides an implementation of this interface for use with   *|
00012 |* llvm bitcode files.                                                        *|
00013 |*                                                                            *|
00014 \*===----------------------------------------------------------------------===*/
00015 
00016 #ifndef LLVM_C_LTO_H
00017 #define LLVM_C_LTO_H
00018 
00019 #include <stddef.h>
00020 #include <sys/types.h>
00021 
00022 #ifndef __cplusplus
00023 #if !defined(_MSC_VER)
00024 #include <stdbool.h>
00025 typedef bool lto_bool_t;
00026 #else
00027 /* MSVC in particular does not have anything like _Bool or bool in C, but we can
00028    at least make sure the type is the same size.  The implementation side will
00029    use C++ bool. */
00030 typedef unsigned char lto_bool_t;
00031 #endif
00032 #else
00033 typedef bool lto_bool_t;
00034 #endif
00035 
00036 /**
00037  * @defgroup LLVMCLTO LTO
00038  * @ingroup LLVMC
00039  *
00040  * @{
00041  */
00042 
00043 #define LTO_API_VERSION 10
00044 
00045 /**
00046  * \since prior to LTO_API_VERSION=3
00047  */
00048 typedef enum {
00049     LTO_SYMBOL_ALIGNMENT_MASK              = 0x0000001F, /* log2 of alignment */
00050     LTO_SYMBOL_PERMISSIONS_MASK            = 0x000000E0,
00051     LTO_SYMBOL_PERMISSIONS_CODE            = 0x000000A0,
00052     LTO_SYMBOL_PERMISSIONS_DATA            = 0x000000C0,
00053     LTO_SYMBOL_PERMISSIONS_RODATA          = 0x00000080,
00054     LTO_SYMBOL_DEFINITION_MASK             = 0x00000700,
00055     LTO_SYMBOL_DEFINITION_REGULAR          = 0x00000100,
00056     LTO_SYMBOL_DEFINITION_TENTATIVE        = 0x00000200,
00057     LTO_SYMBOL_DEFINITION_WEAK             = 0x00000300,
00058     LTO_SYMBOL_DEFINITION_UNDEFINED        = 0x00000400,
00059     LTO_SYMBOL_DEFINITION_WEAKUNDEF        = 0x00000500,
00060     LTO_SYMBOL_SCOPE_MASK                  = 0x00003800,
00061     LTO_SYMBOL_SCOPE_INTERNAL              = 0x00000800,
00062     LTO_SYMBOL_SCOPE_HIDDEN                = 0x00001000,
00063     LTO_SYMBOL_SCOPE_PROTECTED             = 0x00002000,
00064     LTO_SYMBOL_SCOPE_DEFAULT               = 0x00001800,
00065     LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800
00066 } lto_symbol_attributes;
00067 
00068 /**
00069  * \since prior to LTO_API_VERSION=3
00070  */
00071 typedef enum {
00072     LTO_DEBUG_MODEL_NONE         = 0,
00073     LTO_DEBUG_MODEL_DWARF        = 1
00074 } lto_debug_model;
00075 
00076 /**
00077  * \since prior to LTO_API_VERSION=3
00078  */
00079 typedef enum {
00080     LTO_CODEGEN_PIC_MODEL_STATIC         = 0,
00081     LTO_CODEGEN_PIC_MODEL_DYNAMIC        = 1,
00082     LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
00083     LTO_CODEGEN_PIC_MODEL_DEFAULT        = 3
00084 } lto_codegen_model;
00085 
00086 /** opaque reference to a loaded object module */
00087 typedef struct LLVMOpaqueLTOModule *lto_module_t;
00088 
00089 /** opaque reference to a code generator */
00090 typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t;
00091 
00092 #ifdef __cplusplus
00093 extern "C" {
00094 #endif
00095 
00096 /**
00097  * Returns a printable string.
00098  *
00099  * \since prior to LTO_API_VERSION=3
00100  */
00101 extern const char*
00102 lto_get_version(void);
00103 
00104 
00105 /**
00106  * Returns the last error string or NULL if last operation was successful.
00107  *
00108  * \since prior to LTO_API_VERSION=3
00109  */
00110 extern const char*
00111 lto_get_error_message(void);
00112 
00113 /**
00114  * Checks if a file is a loadable object file.
00115  *
00116  * \since prior to LTO_API_VERSION=3
00117  */
00118 extern lto_bool_t
00119 lto_module_is_object_file(const char* path);
00120 
00121 
00122 /**
00123  * Checks if a file is a loadable object compiled for requested target.
00124  *
00125  * \since prior to LTO_API_VERSION=3
00126  */
00127 extern lto_bool_t
00128 lto_module_is_object_file_for_target(const char* path,
00129                                      const char* target_triple_prefix);
00130 
00131 
00132 /**
00133  * Checks if a buffer is a loadable object file.
00134  *
00135  * \since prior to LTO_API_VERSION=3
00136  */
00137 extern lto_bool_t
00138 lto_module_is_object_file_in_memory(const void* mem, size_t length);
00139 
00140 
00141 /**
00142  * Checks if a buffer is a loadable object compiled for requested target.
00143  *
00144  * \since prior to LTO_API_VERSION=3
00145  */
00146 extern lto_bool_t
00147 lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length,
00148                                               const char* target_triple_prefix);
00149 
00150 
00151 /**
00152  * Loads an object file from disk.
00153  * Returns NULL on error (check lto_get_error_message() for details).
00154  *
00155  * \since prior to LTO_API_VERSION=3
00156  */
00157 extern lto_module_t
00158 lto_module_create(const char* path);
00159 
00160 
00161 /**
00162  * Loads an object file from memory.
00163  * Returns NULL on error (check lto_get_error_message() for details).
00164  *
00165  * \since prior to LTO_API_VERSION=3
00166  */
00167 extern lto_module_t
00168 lto_module_create_from_memory(const void* mem, size_t length);
00169 
00170 /**
00171  * Loads an object file from memory with an extra path argument.
00172  * Returns NULL on error (check lto_get_error_message() for details).
00173  *
00174  * \since prior to LTO_API_VERSION=9
00175  */
00176 extern lto_module_t
00177 lto_module_create_from_memory_with_path(const void* mem, size_t length,
00178                                         const char *path);
00179 
00180 /**
00181  * Loads an object file from disk. The seek point of fd is not preserved.
00182  * Returns NULL on error (check lto_get_error_message() for details).
00183  *
00184  * \since LTO_API_VERSION=5
00185  */
00186 extern lto_module_t
00187 lto_module_create_from_fd(int fd, const char *path, size_t file_size);
00188 
00189 /**
00190  * Loads an object file from disk. The seek point of fd is not preserved.
00191  * Returns NULL on error (check lto_get_error_message() for details).
00192  *
00193  * \since LTO_API_VERSION=5
00194  */
00195 extern lto_module_t
00196 lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size,
00197                                     size_t map_size, off_t offset);
00198 
00199 /**
00200  * Frees all memory internally allocated by the module.
00201  * Upon return the lto_module_t is no longer valid.
00202  *
00203  * \since prior to LTO_API_VERSION=3
00204  */
00205 extern void
00206 lto_module_dispose(lto_module_t mod);
00207 
00208 /**
00209  * Returns triple string which the object module was compiled under.
00210  *
00211  * \since prior to LTO_API_VERSION=3
00212  */
00213 extern const char*
00214 lto_module_get_target_triple(lto_module_t mod);
00215 
00216 /**
00217  * Sets triple string with which the object will be codegened.
00218  *
00219  * \since LTO_API_VERSION=4
00220  */
00221 extern void
00222 lto_module_set_target_triple(lto_module_t mod, const char *triple);
00223 
00224 
00225 /**
00226  * Returns the number of symbols in the object module.
00227  *
00228  * \since prior to LTO_API_VERSION=3
00229  */
00230 extern unsigned int
00231 lto_module_get_num_symbols(lto_module_t mod);
00232 
00233 
00234 /**
00235  * Returns the name of the ith symbol in the object module.
00236  *
00237  * \since prior to LTO_API_VERSION=3
00238  */
00239 extern const char*
00240 lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
00241 
00242 
00243 /**
00244  * Returns the attributes of the ith symbol in the object module.
00245  *
00246  * \since prior to LTO_API_VERSION=3
00247  */
00248 extern lto_symbol_attributes
00249 lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
00250 
00251 
00252 /**
00253  * Returns the number of dependent libraries in the object module.
00254  *
00255  * \since LTO_API_VERSION=8
00256  */
00257 extern unsigned int
00258 lto_module_get_num_deplibs(lto_module_t mod);
00259 
00260 
00261 /**
00262  * Returns the ith dependent library in the module.
00263  *
00264  * \since LTO_API_VERSION=8
00265  */
00266 extern const char*
00267 lto_module_get_deplib(lto_module_t mod, unsigned int index);
00268 
00269 
00270 /**
00271  * Returns the number of linker options in the object module.
00272  *
00273  * \since LTO_API_VERSION=8
00274  */
00275 extern unsigned int
00276 lto_module_get_num_linkeropts(lto_module_t mod);
00277 
00278 
00279 /**
00280  * Returns the ith linker option in the module.
00281  *
00282  * \since LTO_API_VERSION=8
00283  */
00284 extern const char*
00285 lto_module_get_linkeropt(lto_module_t mod, unsigned int index);
00286 
00287 
00288 /**
00289  * Diagnostic severity.
00290  *
00291  * \since LTO_API_VERSION=7
00292  */
00293 typedef enum {
00294   LTO_DS_ERROR = 0,
00295   LTO_DS_WARNING = 1,
00296   LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10.
00297   LTO_DS_NOTE = 2
00298 } lto_codegen_diagnostic_severity_t;
00299 
00300 /**
00301  * Diagnostic handler type.
00302  * \p severity defines the severity.
00303  * \p diag is the actual diagnostic.
00304  * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
00305  * \p ctxt is used to pass the context set with the diagnostic handler.
00306  *
00307  * \since LTO_API_VERSION=7
00308  */
00309 typedef void (*lto_diagnostic_handler_t)(
00310     lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
00311 
00312 /**
00313  * Set a diagnostic handler and the related context (void *).
00314  * This is more general than lto_get_error_message, as the diagnostic handler
00315  * can be called at anytime within lto.
00316  *
00317  * \since LTO_API_VERSION=7
00318  */
00319 extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
00320                                                lto_diagnostic_handler_t,
00321                                                void *);
00322 
00323 /**
00324  * Instantiates a code generator.
00325  * Returns NULL on error (check lto_get_error_message() for details).
00326  *
00327  * \since prior to LTO_API_VERSION=3
00328  */
00329 extern lto_code_gen_t
00330 lto_codegen_create(void);
00331 
00332 /**
00333  * Frees all code generator and all memory it internally allocated.
00334  * Upon return the lto_code_gen_t is no longer valid.
00335  *
00336  * \since prior to LTO_API_VERSION=3
00337  */
00338 extern void
00339 lto_codegen_dispose(lto_code_gen_t);
00340 
00341 /**
00342  * Add an object module to the set of modules for which code will be generated.
00343  * Returns true on error (check lto_get_error_message() for details).
00344  *
00345  * \since prior to LTO_API_VERSION=3
00346  */
00347 extern lto_bool_t
00348 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
00349 
00350 /**
00351  * Sets if debug info should be generated.
00352  * Returns true on error (check lto_get_error_message() for details).
00353  *
00354  * \since prior to LTO_API_VERSION=3
00355  */
00356 extern lto_bool_t
00357 lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
00358 
00359 
00360 /**
00361  * Sets which PIC code model to generated.
00362  * Returns true on error (check lto_get_error_message() for details).
00363  *
00364  * \since prior to LTO_API_VERSION=3
00365  */
00366 extern lto_bool_t
00367 lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
00368 
00369 
00370 /**
00371  * Sets the cpu to generate code for.
00372  *
00373  * \since LTO_API_VERSION=4
00374  */
00375 extern void
00376 lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
00377 
00378 
00379 /**
00380  * Sets the location of the assembler tool to run. If not set, libLTO
00381  * will use gcc to invoke the assembler.
00382  *
00383  * \since LTO_API_VERSION=3
00384  */
00385 extern void
00386 lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path);
00387 
00388 /**
00389  * Sets extra arguments that libLTO should pass to the assembler.
00390  *
00391  * \since LTO_API_VERSION=4
00392  */
00393 extern void
00394 lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
00395                                int nargs);
00396 
00397 /**
00398  * Adds to a list of all global symbols that must exist in the final generated
00399  * code. If a function is not listed there, it might be inlined into every usage
00400  * and optimized away.
00401  *
00402  * \since prior to LTO_API_VERSION=3
00403  */
00404 extern void
00405 lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol);
00406 
00407 /**
00408  * Writes a new object file at the specified path that contains the
00409  * merged contents of all modules added so far.
00410  * Returns true on error (check lto_get_error_message() for details).
00411  *
00412  * \since LTO_API_VERSION=5
00413  */
00414 extern lto_bool_t
00415 lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path);
00416 
00417 /**
00418  * Generates code for all added modules into one native object file.
00419  * On success returns a pointer to a generated mach-o/ELF buffer and
00420  * length set to the buffer size.  The buffer is owned by the
00421  * lto_code_gen_t and will be freed when lto_codegen_dispose()
00422  * is called, or lto_codegen_compile() is called again.
00423  * On failure, returns NULL (check lto_get_error_message() for details).
00424  *
00425  * \since prior to LTO_API_VERSION=3
00426  */
00427 extern const void*
00428 lto_codegen_compile(lto_code_gen_t cg, size_t* length);
00429 
00430 /**
00431  * Generates code for all added modules into one native object file.
00432  * The name of the file is written to name. Returns true on error.
00433  *
00434  * \since LTO_API_VERSION=5
00435  */
00436 extern lto_bool_t
00437 lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
00438 
00439 
00440 /**
00441  * Sets options to help debug codegen bugs.
00442  *
00443  * \since prior to LTO_API_VERSION=3
00444  */
00445 extern void
00446 lto_codegen_debug_options(lto_code_gen_t cg, const char *);
00447 
00448 /**
00449  * Initializes LLVM disassemblers.
00450  * FIXME: This doesn't really belong here.
00451  *
00452  * \since LTO_API_VERSION=5
00453  */
00454 extern void
00455 lto_initialize_disassembler(void);
00456 
00457 #ifdef __cplusplus
00458 }
00459 #endif
00460 
00461 /**
00462  * @}
00463  */
00464 
00465 #endif