+ return Error;
+}
+
+static formatted_raw_ostream *GetOutputStream() {
+ if (OutputFilename == "")
+ OutputFilename = "-";
+
+ // Make sure that the Out file gets unlinked from the disk if we get a
+ // SIGINT.
+ if (OutputFilename != "-")
+ sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
+ std::string Err;
+ raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Err,
+ raw_fd_ostream::F_Binary);
+ if (!Err.empty()) {
+ errs() << Err << '\n';
+ delete Out;
+ return 0;
+ }
+
+ return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
+}
+
+static int AssembleInput(const char *ProgName) {
+ const Target *TheTarget = GetTarget(ProgName);
+ if (!TheTarget)
+ return 1;
+
+ std::string Error;
+ MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, &Error);
+ if (Buffer == 0) {
+ errs() << ProgName << ": ";
+ if (Error.size())
+ errs() << Error << "\n";
+ else
+ errs() << "input file didn't read correctly.\n";
+ return 1;
+ }
+
+ SourceMgr SrcMgr;
+
+ // Tell SrcMgr about this buffer, which is what the parser will pick up.
+ SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+
+ // Record the location of the include directories so that the lexer can find
+ // it later.
+ SrcMgr.setIncludeDirs(IncludeDirs);
+
+
+ const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
+ assert(MAI && "Unable to create target asm info!");
+
+ MCContext Ctx(*MAI);
+ formatted_raw_ostream *Out = GetOutputStream();
+ if (!Out)
+ return 1;
+
+
+ // FIXME: We shouldn't need to do this (and link in codegen).
+ OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName, ""));
+
+ if (!TM) {
+ errs() << ProgName << ": error: could not create target for triple '"
+ << TripleName << "'.\n";
+ return 1;
+ }
+
+ OwningPtr<MCInstPrinter> IP;
+ OwningPtr<MCCodeEmitter> CE;
+ OwningPtr<MCStreamer> Str;
+ OwningPtr<TargetAsmBackend> TAB;
+
+ if (FileType == OFT_AssemblyFile) {
+ IP.reset(TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *Out));
+ if (ShowEncoding)
+ CE.reset(TheTarget->createCodeEmitter(*TM, Ctx));
+ Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(),
+ /*asmverbose*/true, IP.get(), CE.get(),
+ ShowInst));
+ } else {
+ assert(FileType == OFT_ObjectFile && "Invalid file type!");
+ CE.reset(TheTarget->createCodeEmitter(*TM, Ctx));
+ TAB.reset(TheTarget->createAsmBackend(TripleName));
+ Str.reset(createMachOStreamer(Ctx, *TAB, *Out, CE.get()));
+ }
+
+ AsmParser Parser(SrcMgr, Ctx, *Str.get(), *MAI);
+ OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser));
+ if (!TAP) {
+ errs() << ProgName
+ << ": error: this target does not support assembly parsing.\n";
+ return 1;
+ }
+
+ Parser.setTargetParser(*TAP.get());
+
+ int Res = Parser.Run(NoInitialTextSection);
+ if (Out != &fouts())
+ delete Out;
+
+ // Delete output on errors.
+ if (Res && OutputFilename != "-")
+ sys::Path(OutputFilename).eraseFromDisk();
+
+ return Res;
+}
+
+static int DisassembleInput(const char *ProgName) {
+ std::string Error;
+ const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
+ if (TheTarget == 0) {
+ errs() << ProgName << ": error: unable to get target for '" << TripleName
+ << "', see --version and --triple.\n";
+ return 0;
+ }
+
+ std::string ErrorMessage;
+
+ MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
+ &ErrorMessage);
+
+ if (Buffer == 0) {
+ errs() << ProgName << ": ";
+ if (ErrorMessage.size())
+ errs() << ErrorMessage << "\n";
+ else
+ errs() << "input file didn't read correctly.\n";
+ return 1;
+ }
+
+ return Disassembler::disassemble(*TheTarget, TripleName, *Buffer);