From d04a8d4b33ff316ca4cf961e06c9e312eff8e64f Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Mon, 3 Dec 2012 16:50:05 +0000 Subject: Use the new script to sort the includes of every file under lib. Sooooo many of these had incorrect or strange main module includes. I have manually inspected all of these, and fixed the main module include to be the nearest plausible thing I could find. If you own or care about any of these source files, I encourage you to take some time and check that these edits were sensible. I can't have broken anything (I strictly added headers, and reordered them, never removed), but they may not be the headers you'd really like to identify as containing the API being implemented. Many forward declarations and missing includes were added to a header files to allow them to parse cleanly when included first. The main module rule does in fact have its merits. =] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169131 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/MCDisassembler/Disassembler.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/MC/MCDisassembler/Disassembler.cpp') diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index 490ca75bc8..363518966e 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -9,7 +9,6 @@ #include "Disassembler.h" #include "llvm-c/Disassembler.h" - #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCDisassembler.h" @@ -18,9 +17,9 @@ #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MemoryObject.h" #include "llvm/Support/TargetRegistry.h" -#include "llvm/Support/ErrorHandling.h" namespace llvm { class Target; -- cgit v1.2.3-70-g09d2 From 14ccc9007a932a23201251ced4be4c898a62d6a5 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Wed, 5 Dec 2012 18:13:19 +0000 Subject: Added a option to the disassembler to print immediates as hex. This is for the lldb team so most of but not all of the values are to be printed as hex with this option. Some small values like the scale in an X86 address were requested to printed in decimal without the leading 0x. There may be some tweaks need to places that may still be in decimal that they want in hex. Specially for arm. I made my best guess. Any tweaks from here should be simple. I also did the best I know now with help from the C++ gurus creating the cleanest formatImm() utility function and containing the changes. But if someone has a better idea to make something cleaner I'm all ears and game for changing the implementation. rdar://8109283 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169393 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/Disassembler.h | 2 ++ include/llvm/MC/MCInstPrinter.h | 13 ++++++++++++- lib/MC/MCDisassembler/Disassembler.cpp | 6 ++++++ lib/MC/MCInstPrinter.cpp | 9 +++++++++ lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp | 14 +++++++------- lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp | 8 ++++---- test/MC/Disassembler/ARM/hex-immediates.txt | 5 +++++ test/MC/Disassembler/X86/hex-immediates.txt | 10 ++++++++++ tools/llvm-mc/llvm-mc.cpp | 20 ++++++++++++++++---- 9 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 test/MC/Disassembler/ARM/hex-immediates.txt create mode 100644 test/MC/Disassembler/X86/hex-immediates.txt (limited to 'lib/MC/MCDisassembler/Disassembler.cpp') diff --git a/include/llvm-c/Disassembler.h b/include/llvm-c/Disassembler.h index b8c4ad9ad7..3146332fa8 100644 --- a/include/llvm-c/Disassembler.h +++ b/include/llvm-c/Disassembler.h @@ -153,6 +153,8 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options); /* The option to produce marked up assembly. */ #define LLVMDisassembler_Option_UseMarkup 1 +/* The option to print immediates as hex. */ +#define LLVMDisassembler_Option_PrintImmHex 2 /** * Dispose of a disassembler context. diff --git a/include/llvm/MC/MCInstPrinter.h b/include/llvm/MC/MCInstPrinter.h index 3b9420a403..d20d8d2c83 100644 --- a/include/llvm/MC/MCInstPrinter.h +++ b/include/llvm/MC/MCInstPrinter.h @@ -10,6 +10,8 @@ #ifndef LLVM_MC_MCINSTPRINTER_H #define LLVM_MC_MCINSTPRINTER_H +#include "llvm/Support/Format.h" + namespace llvm { class MCInst; class raw_ostream; @@ -36,13 +38,16 @@ protected: /// True if we are printing marked up assembly. bool UseMarkup; + /// True if we are printing immediates as hex. + bool PrintImmHex; + /// Utility function for printing annotations. void printAnnotation(raw_ostream &OS, StringRef Annot); public: MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri) : CommentStream(0), MAI(mai), MII(mii), MRI(mri), AvailableFeatures(0), - UseMarkup(0) {} + UseMarkup(0), PrintImmHex(0) {} virtual ~MCInstPrinter(); @@ -70,6 +75,12 @@ public: /// Utility functions to make adding mark ups simpler. StringRef markup(StringRef s) const; StringRef markup(StringRef a, StringRef b) const; + + bool getPrintImmHex() const { return PrintImmHex; } + void setPrintImmHex(bool Value) { PrintImmHex = Value; } + + /// Utility function to print immediates in decimal or hex. + format_object1 formatImm(const int64_t Value) const; }; } // namespace llvm diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index 363518966e..d10f543ae0 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -182,5 +182,11 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ IP->setUseMarkup(1); Options &= ~LLVMDisassembler_Option_UseMarkup; } + if (Options & LLVMDisassembler_Option_PrintImmHex){ + LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; + MCInstPrinter *IP = DC->getIP(); + IP->setPrintImmHex(1); + Options &= ~LLVMDisassembler_Option_PrintImmHex; + } return (Options == 0); } diff --git a/lib/MC/MCInstPrinter.cpp b/lib/MC/MCInstPrinter.cpp index 83d0dd3558..7efb3f0ef3 100644 --- a/lib/MC/MCInstPrinter.cpp +++ b/lib/MC/MCInstPrinter.cpp @@ -12,6 +12,7 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -50,3 +51,11 @@ StringRef MCInstPrinter::markup(StringRef a, StringRef b) const { else return b; } + +/// Utility function to print immediates in decimal or hex. +format_object1 MCInstPrinter::formatImm(const int64_t Value) const { + if (getPrintImmHex()) + return format("0x%llx", Value); + else + return format("%lld", Value); +} diff --git a/lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp b/lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp index b9f21dcdda..d48b37edc9 100644 --- a/lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp +++ b/lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp @@ -293,7 +293,7 @@ void ARMInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, printRegName(O, Reg); } else if (Op.isImm()) { O << markup(""); } else { assert(Op.isExpr() && "unknown operand kind in printOperand"); @@ -319,7 +319,7 @@ void ARMInstPrinter::printThumbLdrLabelOperand(const MCInst *MI, unsigned OpNum, O << *MO1.getExpr(); else if (MO1.isImm()) { O << markup("]>", "]"); } else @@ -911,7 +911,7 @@ void ARMInstPrinter::printAdrLabelOperand(const MCInst *MI, unsigned OpNum, void ARMInstPrinter::printThumbS4ImmOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O) { O << markup("getOperand(OpNum).getImm() * 4 + << "#" << formatImm(MI->getOperand(OpNum).getImm() * 4) << markup(">"); } @@ -919,7 +919,7 @@ void ARMInstPrinter::printThumbSRImm(const MCInst *MI, unsigned OpNum, raw_ostream &O) { unsigned Imm = MI->getOperand(OpNum).getImm(); O << markup(""); } @@ -976,7 +976,7 @@ void ARMInstPrinter::printThumbAddrModeImm5SOperand(const MCInst *MI, if (unsigned ImmOffs = MO2.getImm()) { O << ", " << markup(""); } O << "]" << markup(">"); @@ -1127,7 +1127,7 @@ void ARMInstPrinter::printT2AddrModeImm0_1020s4Operand(const MCInst *MI, if (MO2.getImm()) { O << ", " << markup(""); } O << "]" << markup(">"); @@ -1217,7 +1217,7 @@ void ARMInstPrinter::printImmPlusOneOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O) { unsigned Imm = MI->getOperand(OpNum).getImm(); O << markup(""); } diff --git a/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp b/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp index cf15236ccc..e357710b20 100644 --- a/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp +++ b/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp @@ -131,7 +131,7 @@ void X86ATTInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo, raw_ostream &O) { const MCOperand &Op = MI->getOperand(OpNo); if (Op.isImm()) - O << Op.getImm(); + O << formatImm(Op.getImm()); else { assert(Op.isExpr() && "unknown pcrel immediate operand"); // If a symbolic branch target was added as a constant expression then print @@ -157,7 +157,7 @@ void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, } else if (Op.isImm()) { // Print X86 immediates as signed values. O << markup(""); if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256)) @@ -189,7 +189,7 @@ void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op, if (DispSpec.isImm()) { int64_t DispVal = DispSpec.getImm(); if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) - O << DispVal; + O << formatImm(DispVal); } else { assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); O << *DispSpec.getExpr(); @@ -207,7 +207,7 @@ void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op, if (ScaleVal != 1) { O << ',' << markup(""); } } diff --git a/test/MC/Disassembler/ARM/hex-immediates.txt b/test/MC/Disassembler/ARM/hex-immediates.txt new file mode 100644 index 0000000000..2634d7ed33 --- /dev/null +++ b/test/MC/Disassembler/ARM/hex-immediates.txt @@ -0,0 +1,5 @@ +# RUN: llvm-mc -triple=thumbv7-apple-darwin -mcpu=cortex-a8 -hdis < %s | FileCheck %s +# CHECK: ldr r4, [pc, #0x20] +0x08 0x4c +# CHECK: sub sp, #0x84 +0xa1 0xb0 diff --git a/test/MC/Disassembler/X86/hex-immediates.txt b/test/MC/Disassembler/X86/hex-immediates.txt new file mode 100644 index 0000000000..80d24487ee --- /dev/null +++ b/test/MC/Disassembler/X86/hex-immediates.txt @@ -0,0 +1,10 @@ +# RUN: llvm-mc --hdis %s -triple=x86_64-apple-darwin9 2>&1 | FileCheck %s + +# CHECK: movabsq $0x7fffffffffffffff, %rcx +0x48 0xb9 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x7f +# CHECK: leaq 0x3e2(%rip), %rdi +0x48 0x8d 0x3d 0xe2 0x03 0x00 0x00 +# CHECK: subq $0x40, %rsp +0x48 0x83 0xec 0x40 +# CHECK: leal (,%r14,4), %eax +0x42 0x8d 0x04 0xb5 0x00 0x00 0x00 0x00 diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 935a770365..15cacfabeb 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -162,7 +162,8 @@ enum ActionType { AC_Assemble, AC_Disassemble, AC_EDisassemble, - AC_MDisassemble + AC_MDisassemble, + AC_HDisassemble }; static cl::opt @@ -178,6 +179,9 @@ Action(cl::desc("Action to perform:"), "Enhanced disassembly of strings of hex bytes"), clEnumValN(AC_MDisassemble, "mdis", "Marked up disassembly of strings of hex bytes"), + clEnumValN(AC_HDisassemble, "hdis", + "Disassemble strings of hex bytes printing " + "immediates as hex"), clEnumValEnd)); static const Target *GetTarget(const char *ProgName) { @@ -437,6 +441,7 @@ int main(int argc, char **argv) { } int Res = 1; + bool disassemble = false; switch (Action) { case AC_AsLex: Res = AsLexInput(SrcMgr, *MAI, Out.get()); @@ -446,15 +451,22 @@ int main(int argc, char **argv) { break; case AC_MDisassemble: IP->setUseMarkup(1); - // Fall through to do disassembly. + disassemble = true; + break; + case AC_HDisassemble: + IP->setPrintImmHex(1); + disassemble = true; + break; case AC_Disassemble: - Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, - *Buffer, SrcMgr, Out->os()); + disassemble = true; break; case AC_EDisassemble: Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, SrcMgr, Out->os()); break; } + if (disassemble) + Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, + *Buffer, SrcMgr, Out->os()); // Keep output if no errors. if (Res == 0) Out->keep(); -- cgit v1.2.3-70-g09d2 From 68a590df13f47180abd3022aa75f237ae993770f Mon Sep 17 00:00:00 2001 From: Jim Grosbach Date: Fri, 7 Dec 2012 23:53:27 +0000 Subject: Add C API for specifying CPU to the disassembler. It was a nasty oversight that we didn't include this when we added this API in the first place. Blech. rdar://12839439 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169653 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/Disassembler.h | 15 ++++++++++++++- lib/MC/MCDisassembler/Disassembler.cpp | 25 ++++++++++++++++--------- tools/lto/lto.exports | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) (limited to 'lib/MC/MCDisassembler/Disassembler.cpp') diff --git a/include/llvm-c/Disassembler.h b/include/llvm-c/Disassembler.h index 3146332fa8..f0872c1436 100644 --- a/include/llvm-c/Disassembler.h +++ b/include/llvm-c/Disassembler.h @@ -139,12 +139,25 @@ extern "C" { * by passing a block of information in the DisInfo parameter and specifying the * TagType and callback functions as described above. These can all be passed * as NULL. If successful, this returns a disassembler context. If not, it - * returns NULL. + * returns NULL. This function is equivalent to calling LLVMCreateDisasmCPU() + * with an empty CPU name. */ LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, int TagType, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp); +/** + * Create a disassembler for the TripleName and a specific CPU. Symbolic + * disassembly is supported by passing a block of information in the DisInfo + * parameter and specifying the TagType and callback functions as described + * above. These can all be passed * as NULL. If successful, this returns a + * disassembler context. If not, it returns NULL. + */ +LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, + void *DisInfo, int TagType, + LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp); + /** * Set the disassembler's options. Returns 1 if it can set the Options and 0 * otherwise. diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index d10f543ae0..ac583ac127 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -33,29 +33,29 @@ using namespace llvm; // functions can all be passed as NULL. If successful, this returns a // disassembler context. If not, it returns NULL. // -LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, - int TagType, LLVMOpInfoCallback GetOpInfo, - LLVMSymbolLookupCallback SymbolLookUp) { +LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, + void *DisInfo, int TagType, + LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp){ // Get the target. std::string Error; - const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); + const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); assert(TheTarget && "Unable to create target!"); // Get the assembler info needed to setup the MCContext. - const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); + const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(Triple); assert(MAI && "Unable to create target asm info!"); const MCInstrInfo *MII = TheTarget->createMCInstrInfo(); assert(MII && "Unable to create target instruction info!"); - const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); + const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple); assert(MRI && "Unable to create target register info!"); // Package up features to be passed to target/subtarget std::string FeaturesStr; - std::string CPU; - const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU, + const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU, FeaturesStr); assert(STI && "Unable to create subtarget info!"); @@ -74,7 +74,7 @@ LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, *MAI, *MII, *MRI, *STI); assert(IP && "Unable to create instruction printer!"); - LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, + LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, MAI, MRI, STI, MII, Ctx, DisAsm, IP); @@ -83,6 +83,13 @@ LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, return DC; } +LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo, + int TagType, LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp) { + return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo, + SymbolLookUp); +} + // // LLVMDisasmDispose() disposes of the disassembler specified by the context. // diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports index d783d1cb18..46d0d74c82 100644 --- a/tools/lto/lto.exports +++ b/tools/lto/lto.exports @@ -29,6 +29,7 @@ lto_codegen_set_assembler_path lto_codegen_set_cpu lto_codegen_compile_to_file LLVMCreateDisasm +LLVMCreateDisasmCPU LLVMDisasmDispose LLVMDisasmInstruction LLVMSetDisasmOptions -- cgit v1.2.3-70-g09d2