| 1 | /* |
| 2 | * Copyright (c) 2015 Andrew Kelley |
| 3 | * |
| 4 | * This file is part of zig, which is MIT licensed. |
| 5 | * See http://opensource.org/licenses/MIT |
| 6 | */ |
| 7 | |
| 8 | #ifndef ZIG_ZIG_LLVM_HPP |
| 9 | #define ZIG_ZIG_LLVM_HPP |
| 10 | |
| 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | #include <llvm-c/Core.h> |
| 14 | #include <llvm-c/Analysis.h> |
| 15 | #include <llvm-c/Target.h> |
| 16 | #include <llvm-c/TargetMachine.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | #define ZIG_EXTERN_C extern "C" |
| 20 | #else |
| 21 | #define ZIG_EXTERN_C |
| 22 | #endif |
| 23 | |
| 24 | // ATTENTION: If you modify this file, be sure to update the corresponding |
| 25 | // extern function declarations in the self-hosted compiler. |
| 26 | |
| 27 | // synchronize with llvm/include/Transforms/Instrumentation.h::SanitizerCoverageOptions::Type |
| 28 | // synchronize with codegen/llvm/bindings.zig::TargetMachine::EmitOptions::Coverage::Type |
| 29 | enum ZigLLVMCoverageType { |
| 30 | ZigLLVMCoverageType_None = 0, |
| 31 | ZigLLVMCoverageType_Function, |
| 32 | ZigLLVMCoverageType_BB, |
| 33 | ZigLLVMCoverageType_Edge |
| 34 | }; |
| 35 | |
| 36 | struct ZigLLVMCoverageOptions { |
| 37 | ZigLLVMCoverageType CoverageType; |
| 38 | bool IndirectCalls; |
| 39 | bool TraceBB; |
| 40 | bool TraceCmp; |
| 41 | bool TraceDiv; |
| 42 | bool TraceGep; |
| 43 | bool Use8bitCounters; |
| 44 | bool TracePC; |
| 45 | bool TracePCGuard; |
| 46 | bool Inline8bitCounters; |
| 47 | bool InlineBoolFlag; |
| 48 | bool PCTable; |
| 49 | bool NoPrune; |
| 50 | bool StackDepth; |
| 51 | bool TraceLoads; |
| 52 | bool TraceStores; |
| 53 | bool CollectControlFlow; |
| 54 | }; |
| 55 | |
| 56 | // synchronize with llvm/include/Pass.h::ThinOrFullLTOPhase |
| 57 | // synchronize with codegen/llvm/bindings.zig::EmitOptions::LtoPhase |
| 58 | enum ZigLLVMThinOrFullLTOPhase { |
| 59 | ZigLLVMThinOrFullLTOPhase_None, |
| 60 | ZigLLVMThinOrFullLTOPhase_ThinPreLink, |
| 61 | ZigLLVMThinOrFullLTOPhase_ThinkPostLink, |
| 62 | ZigLLVMThinOrFullLTOPhase_FullPreLink, |
| 63 | ZigLLVMThinOrFullLTOPhase_FullPostLink, |
| 64 | }; |
| 65 | |
| 66 | struct ZigLLVMEmitOptions { |
| 67 | bool is_debug; |
| 68 | bool is_small; |
| 69 | // If not null, and `ZigLLVMTargetMachineEmitToFile` returns `false` indicating success, this |
| 70 | // `char *` will be populated with a `malloc`-allocated string containing the serialized (as |
| 71 | // JSON) time report data. The caller is responsible for freeing that memory. |
| 72 | char **time_report_out; |
| 73 | bool tsan; |
| 74 | bool sancov; |
| 75 | ZigLLVMThinOrFullLTOPhase lto; |
| 76 | bool allow_fast_isel; |
| 77 | bool allow_machine_outliner; |
| 78 | const char *asm_filename; |
| 79 | const char *bin_filename; |
| 80 | const char *llvm_ir_filename; |
| 81 | const char *bitcode_filename; |
| 82 | ZigLLVMCoverageOptions coverage; |
| 83 | }; |
| 84 | |
| 85 | // synchronize with llvm/include/Object/Archive.h::Object::Archive::Kind |
| 86 | // synchronize with codegen/llvm/bindings.zig::ArchiveKind |
| 87 | enum ZigLLVMArchiveKind { |
| 88 | ZigLLVMArchiveKind_GNU, |
| 89 | ZigLLVMArchiveKind_GNU64, |
| 90 | ZigLLVMArchiveKind_BSD, |
| 91 | ZigLLVMArchiveKind_DARWIN, |
| 92 | ZigLLVMArchiveKind_DARWIN64, |
| 93 | ZigLLVMArchiveKind_COFF, |
| 94 | ZigLLVMArchiveKind_AIXBIG, |
| 95 | }; |
| 96 | |
| 97 | // synchronize with llvm/include/Target/TargetOptions.h::FloatABI::ABIType |
| 98 | // synchronize with codegen/llvm/bindings.zig::TargetMachine::FloatABI |
| 99 | enum ZigLLVMFloatABI { |
| 100 | ZigLLVMFloatABI_Default, // Target-specific (either soft or hard depending on triple, etc). |
| 101 | ZigLLVMFloatABI_Soft, // Soft float. |
| 102 | ZigLLVMFloatABI_Hard // Hard float. |
| 103 | }; |
| 104 | |
| 105 | ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, |
| 106 | char **error_message, const ZigLLVMEmitOptions *options); |
| 107 | |
| 108 | ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, |
| 109 | const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, |
| 110 | LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMFloatABI float_abi, |
| 111 | const char *abi_name, bool emulated_tls); |
| 112 | |
| 113 | ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit); |
| 114 | |
| 115 | ZIG_EXTERN_C void ZigLLVMEnableBrokenDebugInfoCheck(LLVMContextRef context_ref); |
| 116 | ZIG_EXTERN_C bool ZigLLVMGetBrokenDebugInfo(LLVMContextRef context_ref); |
| 117 | |
| 118 | ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv); |
| 119 | |
| 120 | ZIG_EXTERN_C bool ZigLLDLinkCOFF(int argc, const char **argv, bool can_exit_early, bool disable_output); |
| 121 | ZIG_EXTERN_C bool ZigLLDLinkELF(int argc, const char **argv, bool can_exit_early, bool disable_output); |
| 122 | ZIG_EXTERN_C bool ZigLLDLinkWasm(int argc, const char **argv, bool can_exit_early, bool disable_output); |
| 123 | |
| 124 | // On error, populates `*err_file_index_out` and `*err_msg_out` and returns `true`. The caller is |
| 125 | // responsible for freeing `*err_msg_out` using `free`. |
| 126 | // |
| 127 | // If an error occurs reading an input file, `*err_file_index_out` is set to the index of that input |
| 128 | // file in `file_names`. Otherwise, `*err_file_index_out` is set to `file_name_count`. |
| 129 | ZIG_EXTERN_C bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count, |
| 130 | ZigLLVMArchiveKind archive_kind, size_t *err_file_index_out, char **err_msg_out); |
| 131 | |
| 132 | #endif |