- switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) {
- default:
- assert(0 && "Invalid file model!");
- return 1;
- case FileModel::Error:
- std::cerr << argv[0] << ": target does not support generation of this"
- << " file type!\n";
- if (Out != &outs()) delete Out;
- // And the Out file is empty and useless, so remove it now.
- sys::Path(OutputFilename).eraseFromDisk();
- return 1;
- case FileModel::AsmFile:
- break;
- case FileModel::MachOFile:
- MCE = AddMachOWriter(Passes, *Out, Target);
- break;
- case FileModel::ElfFile:
- MCE = AddELFWriter(Passes, *Out, Target);
- break;
+ if (TheTriple.getTriple().empty())
+ TheTriple.setTriple(sys::getDefaultTargetTriple());
+
+ // Get the target specific parser.
+ std::string Error;
+ const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
+ Error);
+ if (!TheTarget) {
+ errs() << argv[0] << ": " << Error;
+ return 1;
+ }
+
+ std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
+
+ CodeGenOpt::Level OLvl = CodeGenOpt::Default;
+ switch (OptLevel) {
+ default:
+ errs() << argv[0] << ": invalid optimization level.\n";
+ return 1;
+ case ' ': break;
+ case '0': OLvl = CodeGenOpt::None; break;
+ case '1': OLvl = CodeGenOpt::Less; break;
+ case '2': OLvl = CodeGenOpt::Default; break;
+ case '3': OLvl = CodeGenOpt::Aggressive; break;
+ }
+
+ TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+ Options.DisableIntegratedAS = NoIntegratedAssembler;
+ Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
+ Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
+ Options.MCOptions.AsmVerbose = AsmVerbose;
+
+ std::unique_ptr<TargetMachine> Target(
+ TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, FeaturesStr,
+ Options, RelocModel, CMModel, OLvl));
+
+ assert(Target && "Could not allocate target machine!");
+
+ // If we don't have a module then just exit now. We do this down
+ // here since the CPU/Feature help is underneath the target machine
+ // creation.
+ if (SkipModule)
+ return 0;
+
+ assert(M && "Should have exited if we didn't have a module!");
+ if (FloatABIForCalls != FloatABI::Default)
+ Options.FloatABIType = FloatABIForCalls;
+
+ // Figure out where we are going to send the output.
+ std::unique_ptr<tool_output_file> Out =
+ GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]);
+ if (!Out) return 1;
+
+ // Build up all of the passes that we want to do to the module.
+ legacy::PassManager PM;
+
+ // Add an appropriate TargetLibraryInfo pass for the module's triple.
+ TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
+
+ // The -disable-simplify-libcalls flag actually disables all builtin optzns.
+ if (DisableSimplifyLibCalls)
+ TLII.disableAllFunctions();
+ PM.add(new TargetLibraryInfoWrapperPass(TLII));
+
+ // Add the target data from the target machine, if it exists, or the module.
+ if (const DataLayout *DL = Target->getDataLayout())
+ M->setDataLayout(*DL);
+
+ // Override function attributes based on CPUStr, FeaturesStr, and command line
+ // flags.
+ setFunctionAttributes(CPUStr, FeaturesStr, *M);
+
+ if (RelaxAll.getNumOccurrences() > 0 &&
+ FileType != TargetMachine::CGFT_ObjectFile)
+ errs() << argv[0]
+ << ": warning: ignoring -mc-relax-all because filetype != obj";
+
+ {
+ raw_pwrite_stream *OS = &Out->os();
+ std::unique_ptr<buffer_ostream> BOS;
+ if (FileType != TargetMachine::CGFT_AssemblyFile &&
+ !Out->os().supportsSeeking()) {
+ BOS = make_unique<buffer_ostream>(*OS);
+ OS = BOS.get();
+ }
+
+ AnalysisID StartBeforeID = nullptr;
+ AnalysisID StartAfterID = nullptr;
+ AnalysisID StopAfterID = nullptr;
+ const PassRegistry *PR = PassRegistry::getPassRegistry();
+ if (!RunPass.empty()) {
+ if (!StartAfter.empty() || !StopAfter.empty()) {
+ errs() << argv[0] << ": start-after and/or stop-after passes are "
+ "redundant when run-pass is specified.\n";
+ return 1;
+ }
+ const PassInfo *PI = PR->getPassInfo(RunPass);
+ if (!PI) {
+ errs() << argv[0] << ": run-pass pass is not registered.\n";
+ return 1;
+ }
+ StopAfterID = StartBeforeID = PI->getTypeInfo();
+ } else {
+ if (!StartAfter.empty()) {
+ const PassInfo *PI = PR->getPassInfo(StartAfter);
+ if (!PI) {
+ errs() << argv[0] << ": start-after pass is not registered.\n";
+ return 1;
+ }
+ StartAfterID = PI->getTypeInfo();
+ }
+ if (!StopAfter.empty()) {
+ const PassInfo *PI = PR->getPassInfo(StopAfter);
+ if (!PI) {
+ errs() << argv[0] << ": stop-after pass is not registered.\n";
+ return 1;
+ }
+ StopAfterID = PI->getTypeInfo();
+ }