llvm.org GIT mirror llvm / 8fd3fcd
Cleanup whitespace. Doxygenize comments. And indent to llvm coding standards. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153740 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
1 changed file(s) with 159 addition(s) and 254 deletion(s). Raw diff Collapse all Expand all
33 //
44 // This file is distributed under the University of Illinois Open Source
55 // License. See LICENSE.TXT for details.
6 //
6 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file implements the Link Time Optimization library. This library is
9 // This file implements the Link Time Optimization library. This library is
1010 // intended to be used by linker to optimize code at link time.
1111 //
1212 //===----------------------------------------------------------------------===//
1818 #include "LTOCodeGenerator.h"
1919
2020
21 // holds most recent error string
22 // *** not thread safe ***
21 // Holds most recent error string.
22 // *** Not thread safe ***
2323 static std::string sLastErrorString;
2424
25
26
27 //
28 // returns a printable string
29 //
30 extern const char* lto_get_version()
31 {
32 return LTOCodeGenerator::getVersionString();
33 }
34
35 //
36 // returns the last error string or NULL if last operation was successful
37 //
38 const char* lto_get_error_message()
39 {
40 return sLastErrorString.c_str();
41 }
42
43
44
45 //
46 // validates if a file is a loadable object file
47 //
48 bool lto_module_is_object_file(const char* path)
49 {
50 return LTOModule::isBitcodeFile(path);
51 }
52
53
54 //
55 // validates if a file is a loadable object file compilable for requested target
56 //
57 bool lto_module_is_object_file_for_target(const char* path,
58 const char* target_triplet_prefix)
59 {
60 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
61 }
62
63
64 //
65 // validates if a buffer is a loadable object file
66 //
67 bool lto_module_is_object_file_in_memory(const void* mem, size_t length)
68 {
69 return LTOModule::isBitcodeFile(mem, length);
70 }
71
72
73 //
74 // validates if a buffer is a loadable object file compilable for the target
75 //
76 bool lto_module_is_object_file_in_memory_for_target(const void* mem,
77 size_t length, const char* target_triplet_prefix)
78 {
79 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
80 }
81
82
83
84 //
85 // loads an object file from disk
86 // returns NULL on error (check lto_get_error_message() for details)
87 //
88 lto_module_t lto_module_create(const char* path)
89 {
90 return LTOModule::makeLTOModule(path, sLastErrorString);
91 }
92
93 //
94 // loads an object file from disk
95 // returns NULL on error (check lto_get_error_message() for details)
96 //
97 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size)
98 {
99 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
100 }
101
102 //
103 // loads an object file from disk
104 // returns NULL on error (check lto_get_error_message() for details)
105 //
25 /// lto_get_version - Returns a printable string.
26 extern const char* lto_get_version() {
27 return LTOCodeGenerator::getVersionString();
28 }
29
30 /// lto_get_error_message - Returns the last error string or NULL if last
31 /// operation was successful.
32 const char* lto_get_error_message() {
33 return sLastErrorString.c_str();
34 }
35
36 /// lto_module_is_object_file - Validates if a file is a loadable object file.
37 bool lto_module_is_object_file(const char* path) {
38 return LTOModule::isBitcodeFile(path);
39 }
40
41 /// lto_module_is_object_file_for_target - Validates if a file is a loadable
42 /// object file compilable for requested target.
43 bool lto_module_is_object_file_for_target(const char* path,
44 const char* target_triplet_prefix) {
45 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
46 }
47
48 /// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
49 /// object file.
50 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
51 return LTOModule::isBitcodeFile(mem, length);
52 }
53
54 /// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
55 /// loadable object file compilable for the target.
56 bool
57 lto_module_is_object_file_in_memory_for_target(const void* mem,
58 size_t length,
59 const char* target_triplet_prefix) {
60 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
61 }
62
63 /// lto_module_create - Loads an object file from disk. Returns NULL on error
64 /// (check lto_get_error_message() for details).
65 lto_module_t lto_module_create(const char* path) {
66 return LTOModule::makeLTOModule(path, sLastErrorString);
67 }
68
69 /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
70 /// error (check lto_get_error_message() for details).
71 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
72 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
73 }
74
75 /// lto_module_create_from_fd_at_offset - Loads an object file from disk.
76 /// Returns NULL on error (check lto_get_error_message() for details).
10677 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
10778 size_t file_size,
10879 size_t map_size,
109 off_t offset)
110 {
111 return LTOModule::makeLTOModule(fd, path, file_size, map_size,
112 offset, sLastErrorString);
113 }
114
115 //
116 // loads an object file from memory
117 // returns NULL on error (check lto_get_error_message() for details)
118 //
119 lto_module_t lto_module_create_from_memory(const void* mem, size_t length)
120 {
121 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
122 }
123
124
125 //
126 // frees all memory for a module
127 // upon return the lto_module_t is no longer valid
128 //
129 void lto_module_dispose(lto_module_t mod)
130 {
131 delete mod;
132 }
133
134
135 //
136 // returns triplet string which the object module was compiled under
137 //
138 const char* lto_module_get_target_triple(lto_module_t mod)
139 {
140 return mod->getTargetTriple();
141 }
142
143 //
144 // sets triple string with which the object will be codegened.
145 //
146 void lto_module_set_target_triple(lto_module_t mod, const char *triple)
147 {
148 return mod->setTargetTriple(triple);
149 }
150
151
152 //
153 // returns the number of symbols in the object module
154 //
155 unsigned int lto_module_get_num_symbols(lto_module_t mod)
156 {
157 return mod->getSymbolCount();
158 }
159
160 //
161 // returns the name of the ith symbol in the object module
162 //
163 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index)
164 {
165 return mod->getSymbolName(index);
166 }
167
168
169 //
170 // returns the attributes of the ith symbol in the object module
171 //
172 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
173 unsigned int index)
174 {
175 return mod->getSymbolAttributes(index);
176 }
177
178
179
180
181
182 //
183 // instantiates a code generator
184 // returns NULL if there is an error
185 //
186 lto_code_gen_t lto_codegen_create(void)
187 {
188 return new LTOCodeGenerator();
189 }
190
191
192
193 //
194 // frees all memory for a code generator
195 // upon return the lto_code_gen_t is no longer valid
196 //
197 void lto_codegen_dispose(lto_code_gen_t cg)
198 {
199 delete cg;
200 }
201
202
203
204 //
205 // add an object module to the set of modules for which code will be generated
206 // returns true on error (check lto_get_error_message() for details)
207 //
208 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod)
209 {
210 return cg->addModule(mod, sLastErrorString);
211 }
212
213
214 //
215 // sets what if any format of debug info should be generated
216 // returns true on error (check lto_get_error_message() for details)
217 //
218 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug)
219 {
220 return cg->setDebugInfo(debug, sLastErrorString);
221 }
222
223
224 //
225 // sets what code model to generated
226 // returns true on error (check lto_get_error_message() for details)
227 //
228 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model)
229 {
80 off_t offset) {
81 return LTOModule::makeLTOModule(fd, path, file_size, map_size,
82 offset, sLastErrorString);
83 }
84
85 /// lto_module_create_from_memory - Loads an object file from memory. Returns
86 /// NULL on error (check lto_get_error_message() for details).
87 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
88 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
89 }
90
91 /// lto_module_dispose - Frees all memory for a module. Upon return the
92 /// lto_module_t is no longer valid.
93 void lto_module_dispose(lto_module_t mod) {
94 delete mod;
95 }
96
97 /// lto_module_get_target_triple - Returns triplet string which the object
98 /// module was compiled under.
99 const char* lto_module_get_target_triple(lto_module_t mod) {
100 return mod->getTargetTriple();
101 }
102
103 /// lto_module_set_target_triple - Sets triple string with which the object will
104 /// be codegened.
105 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
106 return mod->setTargetTriple(triple);
107 }
108
109 /// lto_module_get_num_symbols - Returns the number of symbols in the object
110 /// module.
111 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
112 return mod->getSymbolCount();
113 }
114
115 /// lto_module_get_symbol_name - Returns the name of the ith symbol in the
116 /// object module.
117 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
118 return mod->getSymbolName(index);
119 }
120
121 /// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
122 /// in the object module.
123 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
124 unsigned int index) {
125 return mod->getSymbolAttributes(index);
126 }
127
128 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
129 /// is an error.
130 lto_code_gen_t lto_codegen_create(void) {
131 return new LTOCodeGenerator();
132 }
133
134 /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
135 /// lto_code_gen_t is no longer valid.
136 void lto_codegen_dispose(lto_code_gen_t cg) {
137 delete cg;
138 }
139
140 /// lto_codegen_add_module - Add an object module to the set of modules for
141 /// which code will be generated. Returns true on error (check
142 /// lto_get_error_message() for details).
143 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
144 return cg->addModule(mod, sLastErrorString);
145 }
146
147 /// lto_codegen_set_debug_model - Sets what if any format of debug info should
148 /// be generated. Returns true on error (check lto_get_error_message() for
149 /// details).
150 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
151 return cg->setDebugInfo(debug, sLastErrorString);
152 }
153
154 /// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
155 /// on error (check lto_get_error_message() for details).
156 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
230157 return cg->setCodePICModel(model, sLastErrorString);
231158 }
232159
233 //
234 // sets the cpu to generate code for
235 //
236 void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu)
237 {
160 /// lto_codegen_set_cpu - Sets the cpu to generate code for.
161 void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu) {
238162 return cg->setCpu(cpu);
239163 }
240164
241 //
242 // sets the path to the assembler tool
243 //
244 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path)
245 {
165 /// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
166 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
246167 // In here only for backwards compatibility. We use MC now.
247168 }
248169
249
250 //
251 // sets extra arguments that libLTO should pass to the assembler
252 //
170 /// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
171 /// pass to the assembler.
253172 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char** args,
254 int nargs)
255 {
173 int nargs) {
256174 // In here only for backwards compatibility. We use MC now.
257175 }
258176
259 //
260 // adds to a list of all global symbols that must exist in the final
261 // generated code. If a function is not listed there, it might be
262 // inlined into every usage and optimized away.
263 //
264 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol)
265 {
177 /// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
178 /// that must exist in the final generated code. If a function is not listed
179 /// there, it might be inlined into every usage and optimized away.
180 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
181 const char* symbol) {
266182 cg->addMustPreserveSymbol(symbol);
267183 }
268184
269
270 //
271 // writes a new file at the specified path that contains the
272 // merged contents of all modules added so far.
273 // returns true on error (check lto_get_error_message() for details)
274 //
275 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path)
276 {
185 /// lto_codegen_write_merged_modules - Writes a new file at the specified path
186 /// that contains the merged contents of all modules added so far. Returns true
187 /// on error (check lto_get_error_message() for details).
188 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path) {
277189 return cg->writeMergedModules(path, sLastErrorString);
278190 }
279191
280
281 //
282 // Generates code for all added modules into one native object file.
283 // On success returns a pointer to a generated mach-o/ELF buffer and
284 // length set to the buffer size. The buffer is owned by the
285 // lto_code_gen_t and will be freed when lto_codegen_dispose()
286 // is called, or lto_codegen_compile() is called again.
287 // On failure, returns NULL (check lto_get_error_message() for details).
288 //
192 /// lto_codegen_compile - Generates code for all added modules into one native
193 /// object file. On success returns a pointer to a generated mach-o/ELF buffer
194 /// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
195 /// object and will be freed when lto_codegen_dispose() is called, or
196 /// lto_codegen_compile() is called again. On failure, returns NULL (check
197 /// lto_get_error_message() for details).
289198 extern const void*
290 lto_codegen_compile(lto_code_gen_t cg, size_t* length)
291 {
199 lto_codegen_compile(lto_code_gen_t cg, size_t* length) {
292200 return cg->compile(length, sLastErrorString);
293201 }
294202
298206 return cg->compile_to_file(name, sLastErrorString);
299207 }
300208
301
302 //
303 // Used to pass extra options to the code generator
304 //
209 /// lto_codegen_debug_options - Used to pass extra options to the code
210 /// generator.
305211 extern void
306 lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
307 {
212 lto_codegen_debug_options(lto_code_gen_t cg, const char * opt) {
308213 cg->setCodeGenDebugOptions(opt);
309214 }