1 /*===-- llvm-c/TargetMachine.h - Target Machine Library 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 declares the C interface to the Target and TargetMachine       *|
11 |* classes, which can be used to generate assembly or object files.           *|
12 |*                                                                            *|
13 |* Many exotic languages can interoperate with C code but have a harder time  *|
14 |* with C++ due to name mangling. So in addition to C, this interface enables *|
15 |* tools written in such languages.                                           *|
16 |*                                                                            *|
17 \*===----------------------------------------------------------------------===*/
18 
19 #ifndef LLVM_C_TARGETMACHINE_H
20 #define LLVM_C_TARGETMACHINE_H
21 
22 #include "llvm-c/Target.h"
23 #include "llvm-c/Types.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
29 typedef struct LLVMTarget *LLVMTargetRef;
30 
31 typedef enum {
32     LLVMCodeGenLevelNone,
33     LLVMCodeGenLevelLess,
34     LLVMCodeGenLevelDefault,
35     LLVMCodeGenLevelAggressive
36 } LLVMCodeGenOptLevel;
37 
38 typedef enum {
39     LLVMRelocDefault,
40     LLVMRelocStatic,
41     LLVMRelocPIC,
42     LLVMRelocDynamicNoPic
43 } LLVMRelocMode;
44 
45 typedef enum {
46     LLVMCodeModelDefault,
47     LLVMCodeModelJITDefault,
48     LLVMCodeModelTiny,
49     LLVMCodeModelSmall,
50     LLVMCodeModelKernel,
51     LLVMCodeModelMedium,
52     LLVMCodeModelLarge
53 } LLVMCodeModel;
54 
55 typedef enum {
56     LLVMAssemblyFile,
57     LLVMObjectFile
58 } LLVMCodeGenFileType;
59 
60 /** Returns the first llvm::Target in the registered targets list. */
61 LLVMTargetRef LLVMGetFirstTarget(void);
62 /** Returns the next llvm::Target given a previous one (or null if there's none) */
63 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T);
64 
65 /*===-- Target ------------------------------------------------------------===*/
66 /** Finds the target corresponding to the given name and stores it in \p T.
67   Returns 0 on success. */
68 LLVMTargetRef LLVMGetTargetFromName(const char *Name);
69 
70 /** Finds the target corresponding to the given triple and stores it in \p T.
71   Returns 0 on success. Optionally returns any error in ErrorMessage.
72   Use LLVMDisposeMessage to dispose the message. */
73 LLVMBool LLVMGetTargetFromTriple(const char* Triple, LLVMTargetRef *T,
74                                  char **ErrorMessage);
75 
76 /** Returns the name of a target. See llvm::Target::getName */
77 const char *LLVMGetTargetName(LLVMTargetRef T);
78 
79 /** Returns the description  of a target. See llvm::Target::getDescription */
80 const char *LLVMGetTargetDescription(LLVMTargetRef T);
81 
82 /** Returns if the target has a JIT */
83 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T);
84 
85 /** Returns if the target has a TargetMachine associated */
86 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T);
87 
88 /** Returns if the target as an ASM backend (required for emitting output) */
89 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T);
90 
91 /*===-- Target Machine ----------------------------------------------------===*/
92 /** Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine */
93 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
94   const char *Triple, const char *CPU, const char *Features,
95   LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel);
96 
97 /** Dispose the LLVMTargetMachineRef instance generated by
98   LLVMCreateTargetMachine. */
99 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T);
100 
101 /** Returns the Target used in a TargetMachine */
102 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T);
103 
104 /** Returns the triple used creating this target machine. See
105   llvm::TargetMachine::getTriple. The result needs to be disposed with
106   LLVMDisposeMessage. */
107 char *LLVMGetTargetMachineTriple(LLVMTargetMachineRef T);
108 
109 /** Returns the cpu used creating this target machine. See
110   llvm::TargetMachine::getCPU. The result needs to be disposed with
111   LLVMDisposeMessage. */
112 char *LLVMGetTargetMachineCPU(LLVMTargetMachineRef T);
113 
114 /** Returns the feature string used creating this target machine. See
115   llvm::TargetMachine::getFeatureString. The result needs to be disposed with
116   LLVMDisposeMessage. */
117 char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T);
118 
119 /** Create a DataLayout based on the targetMachine. */
120 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
121 
122 /** Set the target machine's ASM verbosity. */
123 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
124                                       LLVMBool VerboseAsm);
125 
126 /** Emits an asm or object file for the given module to the filename. This
127   wraps several c++ only classes (among them a file stream). Returns any
128   error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
129 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
130   char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);
131 
132 /** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
133 LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
134   LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);
135 
136 /*===-- Triple ------------------------------------------------------------===*/
137 /** Get a triple for the host machine as a string. The result needs to be
138   disposed with LLVMDisposeMessage. */
139 char* LLVMGetDefaultTargetTriple(void);
140 
141 /** Normalize a target triple. The result needs to be disposed with
142   LLVMDisposeMessage. */
143 char* LLVMNormalizeTargetTriple(const char* triple);
144 
145 /** Get the host CPU as a string. The result needs to be disposed with
146   LLVMDisposeMessage. */
147 char* LLVMGetHostCPUName(void);
148 
149 /** Get the host CPU's features as a string. The result needs to be disposed
150   with LLVMDisposeMessage. */
151 char* LLVMGetHostCPUFeatures(void);
152 
153 /** Adds the target-specific analysis passes to the pass manager. */
154 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif
161