Update llvm-mc / MCAsmStreamer to print the instruction using the actual target
specific printer (this only works on x86, for now).
- This makes it possible to do some correctness checking of the parsing and
matching, since we can compare the results of 'as' on the original input, to
those of 'as' on the output from llvm-mc.
- In theory, we could now have an easy ATT -> Intel syntax converter. :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78986 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp
index 465c312..7c57f35 100644
--- a/tools/llvm-mc/llvm-mc.cpp
+++ b/tools/llvm-mc/llvm-mc.cpp
@@ -16,7 +16,9 @@
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/ADT/OwningPtr.h"
+#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
@@ -145,28 +147,21 @@
return Error;
}
-static TargetAsmParser *GetTargetAsmParser(const char *ProgName,
- MCAsmParser &Parser) {
+static const Target *GetTarget(const char *ProgName) {
// Get the target specific parser.
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;
- }
+ if (TheTarget)
+ return TheTarget;
- if (TargetAsmParser *TAP = TheTarget->createAsmParser(Parser))
- return TAP;
-
- errs() << ProgName
- << ": error: this target does not support assembly parsing.\n";
+ errs() << ProgName << ": error: unable to get target for '" << TripleName
+ << "', see --version and --triple.\n";
return 0;
}
-static raw_ostream *GetOutputStream() {
+static formatted_raw_ostream *GetOutputStream() {
if (OutputFilename == "" || OutputFilename == "-")
- return &outs();
+ return &fouts();
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT
@@ -183,10 +178,14 @@
return 0;
}
- return Out;
+ 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) {
@@ -208,10 +207,25 @@
SrcMgr.setIncludeDirs(IncludeDirs);
MCContext Ctx;
- raw_ostream *Out = GetOutputStream();
+ formatted_raw_ostream *Out = GetOutputStream();
if (!Out)
return 1;
- OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, *Out));
+
+ // See if we can get an asm printer.
+ OwningPtr<AsmPrinter> AP(0);
+
+ // FIXME: We shouldn't need to do this (and link in codegen).
+ OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName, ""));
+ const TargetAsmInfo *TAI = 0;
+
+ if (TM) {
+ TAI = TheTarget->createAsmInfo(TripleName);
+ assert(TAI && "Unable to create target asm info!");
+
+ AP.reset(TheTarget->createAsmPrinter(*Out, *TM, TAI, true));
+ }
+
+ OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, *Out, AP.get()));
// FIXME: Target hook & command line option for initial section.
Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT","__text",
@@ -220,13 +234,17 @@
Ctx));
AsmParser Parser(SrcMgr, Ctx, *Str.get());
- OwningPtr<TargetAsmParser> TAP(GetTargetAsmParser(ProgName, Parser));
- if (!TAP)
+ 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();
- if (Out != &outs())
+ if (Out != &fouts())
delete Out;
return Res;
@@ -239,8 +257,11 @@
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
- // Initialize targets and assembly parsers.
+ // Initialize targets and assembly printers/parsers.
llvm::InitializeAllTargetInfos();
+ // FIXME: We shouldn't need to initialize the Target(Machine)s.
+ llvm::InitializeAllTargets();
+ llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");