Extract duplicated disassembler.
Change-Id: I6c607a0ed881dbd808ec43ec65e88f4ef06dea90
diff --git a/lib/CodeGen/CodeEmitter.cpp b/lib/CodeGen/CodeEmitter.cpp
index 1ef83e3..26124ab 100644
--- a/lib/CodeGen/CodeEmitter.cpp
+++ b/lib/CodeGen/CodeEmitter.cpp
@@ -18,6 +18,10 @@
#include "Config.h"
+#if USE_DISASSEMBLER
+#include "Disassembler/Disassembler.h"
+#endif
+
#include "CodeMemoryManager.h"
#include "Runtime.h"
#include "ScriptCompiled.h"
@@ -42,18 +46,9 @@
#include "llvm/ExecutionEngine/GenericValue.h"
-#include "llvm/MC/MCAsmInfo.h"
-#include "llvm/MC/MCDisassembler.h"
-#include "llvm/MC/MCInst.h"
-#include "llvm/MC/MCInstPrinter.h"
-
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
-#if USE_DISASSEMBLER
-#include "llvm/Support/MemoryObject.h"
-#endif
-
#include "llvm/Support/Host.h"
#include "llvm/Target/TargetData.h"
@@ -79,34 +74,6 @@
#include <stddef.h>
-namespace {
-
-#if USE_DISASSEMBLER
-class BufferMemoryObject : public llvm::MemoryObject {
-private:
- const uint8_t *mBytes;
- uint64_t mLength;
-
-public:
- BufferMemoryObject(const uint8_t *Bytes, uint64_t Length)
- : mBytes(Bytes), mLength(Length) {
- }
-
- virtual uint64_t getBase() const { return 0; }
- virtual uint64_t getExtent() const { return mLength; }
-
- virtual int readByte(uint64_t Addr, uint8_t *Byte) const {
- if (Addr > getExtent())
- return -1;
- *Byte = mBytes[Addr];
- return 0;
- }
-};
-#endif
-
-}; // namespace anonymous
-
-
namespace bcc {
// Will take the ownership of @MemMgr
@@ -120,22 +87,12 @@
mpConstantPool(NULL),
mpJumpTable(NULL),
mpMMI(NULL),
-#if USE_DISASSEMBLER
- mpAsmInfo(NULL),
- mpDisassmbler(NULL),
- mpIP(NULL),
-#endif
mpSymbolLookupFn(NULL),
mpSymbolLookupContext(NULL) {
}
CodeEmitter::~CodeEmitter() {
-#if USE_DISASSEMBLER
- delete mpAsmInfo;
- delete mpDisassmbler;
- delete mpIP;
-#endif
}
@@ -999,11 +956,15 @@
// JIT, not the address of the external function.
UpdateGlobalMapping(F, Stub);
- if (!Actual)
+ if (!Actual) {
PendingFunctions.insert(F);
- else
- Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub),
- SL.Size, true);
+ } else {
+#if USE_DISASSEMBLER && DEBUG_OLD_JIT_DISASSEMBLE
+ Disassemble(DEBUG_OLD_JIT_DISASSEMBLER_FILE,
+ mpTarget, mpTargetMachine, F->getName(),
+ (unsigned char const *)Stub, SL.Size);
+#endif
+ }
return Stub;
}
@@ -1166,75 +1127,6 @@
}
-void CodeEmitter::Disassemble(const llvm::StringRef &Name,
- uint8_t *Start, size_t Length, bool IsStub) {
-
-#if USE_DISASSEMBLER && DEBUG_OLD_JIT_DISASSEMBLE
- llvm::raw_ostream *OS;
-
-#if USE_DISASSEMBLER_FILE
- std::string ErrorInfo;
- OS = new llvm::raw_fd_ostream("/data/local/tmp/old-jit.s",
- ErrorInfo,
- llvm::raw_fd_ostream::F_Append);
-
- if (!ErrorInfo.empty()) { // some errors occurred
- // LOGE("Error in creating disassembly file");
- delete OS;
- return;
- }
-#else
- OS = &llvm::outs();
-#endif
-
- *OS << "JIT: Disassembled code: " << Name << ((IsStub) ? " (stub)" : "")
- << "\n";
-
- if (mpAsmInfo == NULL)
- mpAsmInfo = mpTarget->createAsmInfo(Compiler::Triple);
- if (mpDisassmbler == NULL)
- mpDisassmbler = mpTarget->createMCDisassembler();
- if (mpIP == NULL)
- mpIP = mpTarget->createMCInstPrinter(*mpTargetMachine,
- mpAsmInfo->getAssemblerDialect(),
- *mpAsmInfo);
-
- const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Start,
- Length);
- uint64_t Size;
- uint64_t Index;
-
- for (Index = 0; Index < Length; Index += Size) {
- llvm::MCInst Inst;
-
- if (mpDisassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
- /* REMOVED */ llvm::nulls())) {
- (*OS).indent(4)
- .write("0x", 2)
- .write_hex((uint32_t) Start + Index)
- .write(": 0x", 4);
- (*OS).write_hex((uint32_t) *(uint32_t*)(Start+Index));
- mpIP->printInst(&Inst, *OS);
- *OS << "\n";
- } else {
- if (Size == 0)
- Size = 1; // skip illegible bytes
- }
- }
-
- *OS << "\n";
- delete BufferMObj;
-
-#if USE_DISASSEMBLER_FILE
- // If you want the disassemble results write to file, uncomment this.
- ((llvm::raw_fd_ostream*)OS)->close();
- delete OS;
-#endif
-
-#endif // USE_DISASSEMBLER
-}
-
-
void CodeEmitter::setTargetMachine(llvm::TargetMachine &TM) {
mpTargetMachine = &TM;
@@ -1313,15 +1205,6 @@
if (llvm::MachineJumpTableInfo *MJTI = F.getJumpTableInfo())
emitJumpTableInfo(MJTI);
- // FnStart is the start of the text, not the start of the constant pool
- // and other per-function data.
- uint8_t *FnStart =
- reinterpret_cast<uint8_t*>(
- GetPointerToGlobalIfAvailable(F.getFunction()));
-
- // FnEnd is the end of the function's machine code.
- uint8_t *FnEnd = CurBufferPtr;
-
if (!mRelocations.empty()) {
//ptrdiff_t BufferOffset = BufferBegin - mpMemMgr->getCodeMemBase();
@@ -1408,7 +1291,20 @@
// Mark code region readable and executable if it's not so already.
mpMemMgr->setMemoryExecutable();
- Disassemble(F.getFunction()->getName(), FnStart, FnEnd - FnStart, false);
+#if USE_DISASSEMBLER && DEBUG_OLD_JIT_DISASSEMBLE
+ // FnStart is the start of the text, not the start of the constant pool
+ // and other per-function data.
+ uint8_t *FnStart =
+ reinterpret_cast<uint8_t*>(
+ GetPointerToGlobalIfAvailable(F.getFunction()));
+
+ // FnEnd is the end of the function's machine code.
+ uint8_t *FnEnd = CurBufferPtr;
+
+ Disassemble(DEBUG_OLD_JIT_DISASSEMBLER_FILE,
+ mpTarget, mpTargetMachine, F.getFunction()->getName(),
+ (unsigned char const *)FnStart, FnEnd - FnStart);
+#endif
return false;
}
@@ -1532,8 +1428,11 @@
mpTJI->emitFunctionStub(F, Addr, *this);
finishGVStub();
- Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub),
- SL.Size, true);
+#if USE_DISASSEMBLER && DEBUG_OLD_JIT_DISASSEMBLE
+ Disassemble(DEBUG_OLD_JIT_DISASSEMBLER_FILE,
+ mpTarget, mpTargetMachine, F->getName(),
+ (unsigned char const *)Stub, SL.Size);
+#endif
PendingFunctions.erase(I);
}
diff --git a/lib/CodeGen/CodeEmitter.h b/lib/CodeGen/CodeEmitter.h
index 5210901..7862a43 100644
--- a/lib/CodeGen/CodeEmitter.h
+++ b/lib/CodeGen/CodeEmitter.h
@@ -45,11 +45,6 @@
class MachineFunction;
class MachineJumpTableInfo;
class MachineModuleInfo;
-#if USE_DISASSEMBLER
- class MCAsmInfo;
- class MCDisassembler;
- class MCInstPrinter;
-#endif
class MCSymbol;
class Target;
class TargetData;
@@ -142,12 +137,6 @@
std::map<void*, void*> ExternalFnToStubMap;
-#if USE_DISASSEMBLER
- const llvm::MCAsmInfo *mpAsmInfo;
- const llvm::MCDisassembler *mpDisassmbler;
- llvm::MCInstPrinter *mpIP;
-#endif
-
public:
// Resolver to undefined symbol in CodeEmitter
BCCSymbolLookupFn mpSymbolLookupFn;
@@ -158,9 +147,6 @@
virtual ~CodeEmitter();
- void Disassemble(const llvm::StringRef &Name, uint8_t *Start,
- size_t Length, bool IsStub);
-
global_addresses_const_iterator global_address_begin() const {
return mGlobalAddressMap.begin();
}
diff --git a/lib/Disassembler/Disassembler.cpp b/lib/Disassembler/Disassembler.cpp
new file mode 100644
index 0000000..29eecc7
--- /dev/null
+++ b/lib/Disassembler/Disassembler.cpp
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2011, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Disassembler.h"
+
+#include "Config.h"
+
+#include "ExecutionEngine/Compiler.h"
+
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCDisassembler.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCInstPrinter.h"
+
+#include "llvm/Support/MemoryObject.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetOptions.h"
+#include "llvm/Target/TargetRegistry.h"
+#include "llvm/Target/TargetSelect.h"
+
+#include "llvm/LLVMContext.h"
+
+#if USE_DISASSEMBLER
+
+namespace {
+
+class BufferMemoryObject : public llvm::MemoryObject {
+private:
+ const uint8_t *mBytes;
+ uint64_t mLength;
+
+public:
+ BufferMemoryObject(const uint8_t *Bytes, uint64_t Length)
+ : mBytes(Bytes), mLength(Length) {
+ }
+
+ virtual uint64_t getBase() const { return 0; }
+ virtual uint64_t getExtent() const { return mLength; }
+
+ virtual int readByte(uint64_t Addr, uint8_t *Byte) const {
+ if (Addr > getExtent())
+ return -1;
+ *Byte = mBytes[Addr];
+ return 0;
+ }
+};
+
+} // namespace anonymous
+
+namespace bcc {
+
+void InitializeDisassembler() {
+#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
+ LLVMInitializeARMDisassembler();
+ LLVMInitializeARMAsmPrinter();
+#endif
+
+#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
+ LLVMInitializeX86Disassembler();
+ LLVMInitializeX86AsmPrinter();
+#endif
+
+#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
+ LLVMInitializeX86Disassembler();
+ LLVMInitializeX86AsmPrinter();
+#endif
+}
+
+void Disassemble(char const *OutputFileName,
+ llvm::Target const *Target,
+ llvm::TargetMachine *TM,
+ std::string const &Name,
+ unsigned char const *Func,
+ size_t FuncSize) {
+ llvm::raw_ostream *OS;
+
+#if USE_DISASSEMBLER_FILE
+ std::string ErrorInfo;
+ OS = new llvm::raw_fd_ostream(OutputFileName, ErrorInfo,
+ llvm::raw_fd_ostream::F_Append);
+
+ if (!ErrorInfo.empty()) { // some errors occurred
+ // LOGE("Error in creating disassembly file");
+ delete OS;
+ return;
+ }
+#else
+ OS = &llvm::outs();
+#endif
+
+ *OS << "Disassembled code: " << Name << "\n";
+
+ const llvm::MCAsmInfo *AsmInfo;
+ const llvm::MCDisassembler *Disassmbler;
+ llvm::MCInstPrinter *IP;
+
+ AsmInfo = Target->createAsmInfo(Compiler::getTargetTriple());
+ Disassmbler = Target->createMCDisassembler();
+ IP = Target->createMCInstPrinter(*TM,
+ AsmInfo->getAssemblerDialect(),
+ *AsmInfo);
+
+ const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
+
+ uint64_t Size;
+ uint64_t Index;
+
+ for (Index = 0; Index < FuncSize; Index += Size) {
+ llvm::MCInst Inst;
+
+ if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
+ /* REMOVED */ llvm::nulls())) {
+ OS->indent(4);
+ OS->write("0x", 2);
+ OS->write_hex((uint32_t)Func + Index);
+ OS->write(": 0x", 4);
+ OS->write_hex(*(uint32_t *)(Func + Index));
+ IP->printInst(&Inst, *OS);
+ *OS << "\n";
+ } else {
+ if (Size == 0)
+ Size = 1; // skip illegible bytes
+ }
+ }
+
+ *OS << "\n";
+ delete BufferMObj;
+
+ delete AsmInfo;
+ delete Disassmbler;
+ delete IP;
+
+#if USE_DISASSEMBLER_FILE
+ ((llvm::raw_fd_ostream*)OS)->close();
+ delete OS;
+#endif
+}
+
+} // namespace bcc
+
+#endif // USE_DISASSEMBLER
diff --git a/lib/Disassembler/Disassembler.h b/lib/Disassembler/Disassembler.h
new file mode 100644
index 0000000..d0d924c
--- /dev/null
+++ b/lib/Disassembler/Disassembler.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2011, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_DISASSEMBLER_H
+#define BCC_DISASSEMBLER_H
+
+#include "Config.h"
+
+#if USE_DISASSEMBLER
+
+#include <string>
+
+namespace llvm {
+ class Target;
+ class TargetMachine;
+}
+
+namespace bcc {
+
+ void InitializeDisassembler();
+
+ void Disassemble(char const *OutputFileName,
+ llvm::Target const *Target,
+ llvm::TargetMachine *TM,
+ std::string const &Name,
+ unsigned char const *Func,
+ size_t FuncSize);
+
+} // namespace bcc
+
+#endif // USE_DISASSEMBLER
+
+#endif // BCC_DISASSEMBLER_H
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
index 4afbbb4..2336a01 100644
--- a/lib/ExecutionEngine/Compiler.cpp
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -39,6 +39,8 @@
#include "llvm/CodeGen/RegAllocRegistry.h"
#include "llvm/CodeGen/SchedulerRegistry.h"
+#include "llvm/MC/SubtargetFeature.h"
+
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h"
@@ -48,16 +50,6 @@
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetSelect.h"
-#if USE_DISASSEMBLER
-#include "llvm/MC/MCAsmInfo.h"
-#include "llvm/MC/MCDisassembler.h"
-#include "llvm/MC/MCInst.h"
-#include "llvm/MC/MCInstPrinter.h"
-#include "llvm/MC/SubtargetFeature.h"
-#include "llvm/Support/MemoryObject.h"
-#include "llvm/LLVMContext.h"
-#endif
-
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -82,33 +74,6 @@
#include <string>
#include <vector>
-namespace {
-
-#if USE_DISASSEMBLER
-class BufferMemoryObject : public llvm::MemoryObject {
-private:
- const uint8_t *mBytes;
- uint64_t mLength;
-
-public:
- BufferMemoryObject(const uint8_t *Bytes, uint64_t Length)
- : mBytes(Bytes), mLength(Length) {
- }
-
- virtual uint64_t getBase() const { return 0; }
- virtual uint64_t getExtent() const { return mLength; }
-
- virtual int readByte(uint64_t Addr, uint8_t *Byte) const {
- if (Addr > getExtent())
- return -1;
- *Byte = mBytes[Addr];
- return 0;
- }
-};
-#endif
-
-}; // namespace anonymous
-
namespace bcc {
//////////////////////////////////////////////////////////////////////////////
@@ -175,28 +140,20 @@
#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
LLVMInitializeARMTargetInfo();
LLVMInitializeARMTarget();
-#if USE_DISASSEMBLER
- LLVMInitializeARMDisassembler();
- LLVMInitializeARMAsmPrinter();
-#endif
#endif
#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();
-#if USE_DISASSEMBLER
- LLVMInitializeX86Disassembler();
- LLVMInitializeX86AsmPrinter();
-#endif
#endif
#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();
-#if USE_DISASSEMBLER
- LLVMInitializeX86Disassembler();
- LLVMInitializeX86AsmPrinter();
#endif
+
+#if USE_DISASSEMBLER
+ InitializeDisassembler();
#endif
// -O0: llvm::CodeGenOpt::None
@@ -413,6 +370,7 @@
ExportVarMetadata, ExportFuncMetadata) != 0) {
goto on_bcc_compile_error;
}
+
#if USE_DISASSEMBLER && DEBUG_MCJIT_DISASSEMBLE
{
// Get MC codegen emitted function name list
@@ -425,7 +383,8 @@
void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]);
if (func) {
size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]);
- Disassemble(Target, TM, func_list[i], (unsigned char const *)func, size);
+ Disassemble(DEBUG_MCJIT_DISASSEMBLER_FILE,
+ Target, TM, func_list[i], (unsigned char const *)func, size);
}
}
}
@@ -901,77 +860,5 @@
// llvm::llvm_shutdown();
}
-#if USE_MCJIT && USE_DISASSEMBLER
-void Compiler::Disassemble(llvm::Target const *Target,
- llvm::TargetMachine *TM,
- std::string const &Name,
- unsigned char const *Func,
- size_t FuncSize) {
- llvm::raw_ostream *OS;
-
-#if USE_DISASSEMBLER_FILE
- std::string ErrorInfo;
- OS = new llvm::raw_fd_ostream("/data/local/tmp/mcjit-dis.s", ErrorInfo,
- llvm::raw_fd_ostream::F_Append);
-
- if (!ErrorInfo.empty()) { // some errors occurred
- // LOGE("Error in creating disassembly file");
- delete OS;
- return;
- }
-#else
- OS = &llvm::outs();
-#endif
-
- *OS << "MC/ Disassembled code: " << Name << "\n";
-
- const llvm::MCAsmInfo *AsmInfo;
- const llvm::MCDisassembler *Disassmbler;
- llvm::MCInstPrinter *IP;
-
- AsmInfo = Target->createAsmInfo(Compiler::Triple);
- Disassmbler = Target->createMCDisassembler();
- IP = Target->createMCInstPrinter(*TM,
- AsmInfo->getAssemblerDialect(),
- *AsmInfo);
-
- const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
-
- uint64_t Size;
- uint64_t Index;
-
- for (Index = 0; Index < FuncSize; Index += Size) {
- llvm::MCInst Inst;
-
- if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
- /* REMOVED */ llvm::nulls())) {
- OS->indent(4);
- OS->write("0x", 2);
- OS->write_hex((uint32_t)Func + Index);
- OS->write(": 0x", 4);
- OS->write_hex(*(uint32_t *)(Func + Index));
- IP->printInst(&Inst, *OS);
- *OS << "\n";
- } else {
- if (Size == 0)
- Size = 1; // skip illegible bytes
- }
- }
-
- *OS << "\n";
- delete BufferMObj;
-
- delete AsmInfo;
- delete Disassmbler;
- delete IP;
-
-#if USE_DISASSEMBLER_FILE
- // If you want the disassemble results write to file, uncomment this.
- ((llvm::raw_fd_ostream*)OS)->close();
- delete OS;
-#endif
-}
-#endif
-
} // namespace bcc
diff --git a/lib/ExecutionEngine/Compiler.h b/lib/ExecutionEngine/Compiler.h
index 11acaf7..7d4ef42 100644
--- a/lib/ExecutionEngine/Compiler.h
+++ b/lib/ExecutionEngine/Compiler.h
@@ -26,6 +26,10 @@
#include "librsloader.h"
#endif
+#if USE_DISASSEMBLER
+#include "Disassembler/Disassembler.h"
+#endif
+
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallVector.h"
@@ -106,14 +110,6 @@
// Loaded and relocated executable
RSExecRef mRSExecutable;
-
-#if USE_DISASSEMBLER
- void Disassemble(llvm::Target const *Target,
- llvm::TargetMachine *TM,
- std::string const &name,
- unsigned char const *code,
- size_t size);
-#endif
#endif
BCCSymbolLookupFn mpSymbolLookupFn;
@@ -129,6 +125,10 @@
static void GlobalInitialization();
+ static std::string const &getTargetTriple() {
+ return Triple;
+ }
+
void setCachePath(const char *cachePath) {
mCachePath = cachePath;
return;