llvm.org GIT mirror llvm / 1a73540
add bitcode reading support. Remove EH cruft. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36839 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 142 addition(s) and 133 deletion(s). Raw diff Collapse all Expand all
88
99 LEVEL = ../..
1010 TOOLNAME = llc
11 REQUIRES_EH := 1
1211
1312 # Include this here so we can get the configuration of the targets
1413 # that have been configured for construction. We have to do this
1514 # early so we can set up LINK_COMPONENTS before including Makefile.rules
1615 include $(LEVEL)/Makefile.config
1716
18 LINK_COMPONENTS := $(TARGETS_TO_BUILD) bcreader
17 LINK_COMPONENTS := $(TARGETS_TO_BUILD) bcreader bitreader
1918
2019 include $(LLVM_SRC_ROOT)/Makefile.rules
2120
1212 //
1313 //===----------------------------------------------------------------------===//
1414
15 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/Bytecode/Reader.h"
1617 #include "llvm/CodeGen/FileWriters.h"
1718 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
2627 #include "llvm/Support/CommandLine.h"
2728 #include "llvm/Support/Compressor.h"
2829 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/MemoryBuffer.h"
2931 #include "llvm/Support/PluginLoader.h"
3032 #include "llvm/Support/FileUtilities.h"
3133 #include "llvm/Analysis/Verifier.h"
3537 #include
3638 #include
3739 #include
38
3940 using namespace llvm;
41
42 cl::opt Bitcode("bitcode");
43
4044
4145 // General options for llc. Other pass-specific options are specified
4246 // within the corresponding llc passes, and target-specific options
171175 //
172176 int main(int argc, char **argv) {
173177 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
174 try {
175 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
176 sys::PrintStackTraceOnErrorSignal();
177
178 // Load the module to be compiled...
179 std::string errmsg;
180 std::auto_ptr M(ParseBytecodeFile(InputFilename,
181 Compressor::decompressToNewBuffer,
182 &errmsg));
183 if (M.get() == 0) {
184 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
185 std::cerr << "Reason: " << errmsg << "\n";
186 return 1;
187 }
188 Module &mod = *M.get();
189
190 // If we are supposed to override the target triple, do so now.
191 if (!TargetTriple.empty())
192 mod.setTargetTriple(TargetTriple);
193
194 // Allocate target machine. First, check whether the user has
195 // explicitly specified an architecture to compile for.
178 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
179 sys::PrintStackTraceOnErrorSignal();
180
181 // Load the module to be compiled...
182 std::string ErrorMessage;
183 std::auto_ptr M;
184
185 if (Bitcode) {
186 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&InputFilename[0],
187 InputFilename.size());
188 if (Buffer == 0)
189 ErrorMessage = "Error reading file '" + InputFilename + "'";
190 else
191 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
192 delete Buffer;
193
194 } else {
195 M.reset(ParseBytecodeFile(InputFilename,
196 Compressor::decompressToNewBuffer,
197 &ErrorMessage));
198 }
199 if (M.get() == 0) {
200 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
201 std::cerr << "Reason: " << ErrorMessage << "\n";
202 return 1;
203 }
204 Module &mod = *M.get();
205
206 // If we are supposed to override the target triple, do so now.
207 if (!TargetTriple.empty())
208 mod.setTargetTriple(TargetTriple);
209
210 // Allocate target machine. First, check whether the user has
211 // explicitly specified an architecture to compile for.
212 if (MArch == 0) {
213 std::string Err;
214 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
196215 if (MArch == 0) {
197 std::string Err;
198 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
199 if (MArch == 0) {
200 std::cerr << argv[0] << ": error auto-selecting target for module '"
201 << Err << "'. Please use the -march option to explicitly "
202 << "pick a target.\n";
203 return 1;
204 }
205 }
206
207 // Package up features to be passed to target/subtarget
208 std::string FeaturesStr;
209 if (MCPU.size() || MAttrs.size()) {
210 SubtargetFeatures Features;
211 Features.setCPU(MCPU);
212 for (unsigned i = 0; i != MAttrs.size(); ++i)
213 Features.AddFeature(MAttrs[i]);
214 FeaturesStr = Features.getString();
215 }
216
217 std::auto_ptr target(MArch->CtorFn(mod, FeaturesStr));
218 assert(target.get() && "Could not allocate target machine!");
219 TargetMachine &Target = *target.get();
220
221 // Figure out where we are going to send the output...
222 std::ostream *Out = GetOutputStream(argv[0]);
223 if (Out == 0) return 1;
224
225 // If this target requires addPassesToEmitWholeFile, do it now. This is
226 // used by strange things like the C backend.
227 if (Target.WantsWholeFile()) {
228 PassManager PM;
229 PM.add(new TargetData(*Target.getTargetData()));
230 if (!NoVerify)
231 PM.add(createVerifierPass());
232
233 // Ask the target to add backend passes as necessary.
234 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
235 std::cerr << argv[0] << ": target does not support generation of this"
236 << " file type!\n";
237 if (Out != &std::cout) delete Out;
238 // And the Out file is empty and useless, so remove it now.
239 sys::Path(OutputFilename).eraseFromDisk();
240 return 1;
241 }
242 PM.run(mod);
243 } else {
244 // Build up all of the passes that we want to do to the module.
245 FunctionPassManager Passes(new ExistingModuleProvider(M.get()));
246 Passes.add(new TargetData(*Target.getTargetData()));
247
216 std::cerr << argv[0] << ": error auto-selecting target for module '"
217 << Err << "'. Please use the -march option to explicitly "
218 << "pick a target.\n";
219 return 1;
220 }
221 }
222
223 // Package up features to be passed to target/subtarget
224 std::string FeaturesStr;
225 if (MCPU.size() || MAttrs.size()) {
226 SubtargetFeatures Features;
227 Features.setCPU(MCPU);
228 for (unsigned i = 0; i != MAttrs.size(); ++i)
229 Features.AddFeature(MAttrs[i]);
230 FeaturesStr = Features.getString();
231 }
232
233 std::auto_ptr target(MArch->CtorFn(mod, FeaturesStr));
234 assert(target.get() && "Could not allocate target machine!");
235 TargetMachine &Target = *target.get();
236
237 // Figure out where we are going to send the output...
238 std::ostream *Out = GetOutputStream(argv[0]);
239 if (Out == 0) return 1;
240
241 // If this target requires addPassesToEmitWholeFile, do it now. This is
242 // used by strange things like the C backend.
243 if (Target.WantsWholeFile()) {
244 PassManager PM;
245 PM.add(new TargetData(*Target.getTargetData()));
246 if (!NoVerify)
247 PM.add(createVerifierPass());
248
249 // Ask the target to add backend passes as necessary.
250 if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) {
251 std::cerr << argv[0] << ": target does not support generation of this"
252 << " file type!\n";
253 if (Out != &std::cout) delete Out;
254 // And the Out file is empty and useless, so remove it now.
255 sys::Path(OutputFilename).eraseFromDisk();
256 return 1;
257 }
258 PM.run(mod);
259 } else {
260 // Build up all of the passes that we want to do to the module.
261 FunctionPassManager Passes(new ExistingModuleProvider(M.get()));
262 Passes.add(new TargetData(*Target.getTargetData()));
263
248264 #ifndef NDEBUG
249 if (!NoVerify)
250 Passes.add(createVerifierPass());
265 if (!NoVerify)
266 Passes.add(createVerifierPass());
251267 #endif
252
253 // Ask the target to add backend passes as necessary.
254 MachineCodeEmitter *MCE = 0;
255
256 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
257 default:
258 assert(0 && "Invalid file model!");
259 return 1;
260 case FileModel::Error:
261 std::cerr << argv[0] << ": target does not support generation of this"
262 << " file type!\n";
263 if (Out != &std::cout) delete Out;
264 // And the Out file is empty and useless, so remove it now.
265 sys::Path(OutputFilename).eraseFromDisk();
266 return 1;
267 case FileModel::AsmFile:
268 break;
269 case FileModel::MachOFile:
270 MCE = AddMachOWriter(Passes, *Out, Target);
271 break;
272 case FileModel::ElfFile:
273 MCE = AddELFWriter(Passes, *Out, Target);
274 break;
275 }
276
277 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
278 std::cerr << argv[0] << ": target does not support generation of this"
279 << " file type!\n";
280 if (Out != &std::cout) delete Out;
281 // And the Out file is empty and useless, so remove it now.
282 sys::Path(OutputFilename).eraseFromDisk();
283 return 1;
284 }
285
286 Passes.doInitialization();
287
288 // Run our queue of passes all at once now, efficiently.
289 // TODO: this could lazily stream functions out of the module.
290 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
291 if (!I->isDeclaration())
292 Passes.run(*I);
293
294 Passes.doFinalization();
295 }
296
297 // Delete the ostream if it's not a stdout stream
298 if (Out != &std::cout) delete Out;
299
300 return 0;
301 } catch (const std::string& msg) {
302 std::cerr << argv[0] << ": " << msg << "\n";
303 } catch (...) {
304 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
305 }
306 return 1;
268
269 // Ask the target to add backend passes as necessary.
270 MachineCodeEmitter *MCE = 0;
271
272 switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
273 default:
274 assert(0 && "Invalid file model!");
275 return 1;
276 case FileModel::Error:
277 std::cerr << argv[0] << ": target does not support generation of this"
278 << " file type!\n";
279 if (Out != &std::cout) delete Out;
280 // And the Out file is empty and useless, so remove it now.
281 sys::Path(OutputFilename).eraseFromDisk();
282 return 1;
283 case FileModel::AsmFile:
284 break;
285 case FileModel::MachOFile:
286 MCE = AddMachOWriter(Passes, *Out, Target);
287 break;
288 case FileModel::ElfFile:
289 MCE = AddELFWriter(Passes, *Out, Target);
290 break;
291 }
292
293 if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) {
294 std::cerr << argv[0] << ": target does not support generation of this"
295 << " file type!\n";
296 if (Out != &std::cout) delete Out;
297 // And the Out file is empty and useless, so remove it now.
298 sys::Path(OutputFilename).eraseFromDisk();
299 return 1;
300 }
301
302 Passes.doInitialization();
303
304 // Run our queue of passes all at once now, efficiently.
305 // TODO: this could lazily stream functions out of the module.
306 for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I)
307 if (!I->isDeclaration())
308 Passes.run(*I);
309
310 Passes.doFinalization();
311 }
312
313 // Delete the ostream if it's not a stdout stream
314 if (Out != &std::cout) delete Out;
315
316 return 0;
307317 }