From 85cf531593039e0d9e4d95114e9a588df45498a6 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Tue, 18 Dec 2012 23:47:28 +0000 Subject: [PATCH] Add to the disassembler C API an option to print the disassembled instructions in the assembly code variant if one exists. The intended use for this is so tools like lldb and darwin's otool(1) can be switched to print Intel-flavored disassembly. I discussed extensively this API with Jim Grosbach and we feel while it may not be fully general, in reality there is only one syntax for each assembly with the exception of X86 which has exactly two for historical reasons. rdar://10989182 llvm-svn: 170477 --- llvm/include/llvm-c/Disassembler.h | 2 ++ llvm/lib/MC/MCDisassembler/Disassembler.cpp | 16 ++++++++++++++++ llvm/lib/MC/MCDisassembler/Disassembler.h | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/llvm/include/llvm-c/Disassembler.h b/llvm/include/llvm-c/Disassembler.h index f0872c1..df65a7b 100644 --- a/llvm/include/llvm-c/Disassembler.h +++ b/llvm/include/llvm-c/Disassembler.h @@ -168,6 +168,8 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options); #define LLVMDisassembler_Option_UseMarkup 1 /* The option to print immediates as hex. */ #define LLVMDisassembler_Option_PrintImmHex 2 +/* The option use the other assembler printer variant */ +#define LLVMDisassembler_Option_AsmPrinterVariant 4 /** * Dispose of a disassembler context. diff --git a/llvm/lib/MC/MCDisassembler/Disassembler.cpp b/llvm/lib/MC/MCDisassembler/Disassembler.cpp index ac583ac..d3fa906 100644 --- a/llvm/lib/MC/MCDisassembler/Disassembler.cpp +++ b/llvm/lib/MC/MCDisassembler/Disassembler.cpp @@ -195,5 +195,21 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ IP->setPrintImmHex(1); Options &= ~LLVMDisassembler_Option_PrintImmHex; } + if (Options & LLVMDisassembler_Option_AsmPrinterVariant){ + LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; + // Try to set up the new instruction printer. + const MCAsmInfo *MAI = DC->getAsmInfo(); + const MCInstrInfo *MII = DC->getInstrInfo(); + const MCRegisterInfo *MRI = DC->getRegisterInfo(); + const MCSubtargetInfo *STI = DC->getSubtargetInfo(); + int AsmPrinterVariant = MAI->getAssemblerDialect(); + AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0; + MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter( + AsmPrinterVariant, *MAI, *MII, *MRI, *STI); + if (IP) { + DC->setIP(IP); + Options &= ~LLVMDisassembler_Option_AsmPrinterVariant; + } + } return (Options == 0); } diff --git a/llvm/lib/MC/MCDisassembler/Disassembler.h b/llvm/lib/MC/MCDisassembler/Disassembler.h index 28cf04b..6eb59d0 100644 --- a/llvm/lib/MC/MCDisassembler/Disassembler.h +++ b/llvm/lib/MC/MCDisassembler/Disassembler.h @@ -109,7 +109,11 @@ public: const Target *getTarget() const { return TheTarget; } const MCDisassembler *getDisAsm() const { return DisAsm.get(); } const MCAsmInfo *getAsmInfo() const { return MAI.get(); } + const MCInstrInfo *getInstrInfo() const { return MII.get(); } + const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); } + const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); } MCInstPrinter *getIP() { return IP.get(); } + void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); } }; } // namespace llvm -- 2.7.4