added new MC classes for richer disassembling functionality
This commit is contained in:
parent
78be6a7f5f
commit
4eab936224
3 changed files with 75 additions and 10 deletions
|
|
@ -36,17 +36,41 @@ class BadInstr(Instr):
|
|||
|
||||
class Disassembler:
|
||||
|
||||
def __init__(self, mcdisasm):
|
||||
def __init__(self, mcdisasm, mri, mai, mii, mia, mip):
|
||||
self.mcdisasm = mcdisasm
|
||||
if not self.mcdisasm:
|
||||
raise llvm.LLVMException("null MCDisassembler argument")
|
||||
|
||||
self.mri = mri
|
||||
self.mai = mai
|
||||
self.mii = mii
|
||||
self.mia = mia
|
||||
self.mip = mip
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.mcdisasm)
|
||||
|
||||
@staticmethod
|
||||
def new_from_target(target, subtargetinfo):
|
||||
return Disassembler(target.createMCDisassembler(subtargetinfo))
|
||||
def new_from_target(target, triple, cpu, features):
|
||||
def raise_on_false(name, obj):
|
||||
if not obj:
|
||||
raise llvm.LLVMException("Could not create %s" % name)
|
||||
|
||||
sti = target.createMCSubtargetInfo(triple, cpu, features)
|
||||
raise_on_false("subtarget info", sti)
|
||||
mri = target.createMCRegInfo(triple)
|
||||
raise_on_false("register info", mri)
|
||||
mai = target.createMCAsmInfo(mri, triple)
|
||||
raise_on_false("asm info", mai)
|
||||
mii = target.createMCInstrInfo()
|
||||
raise_on_false("instr info", mii)
|
||||
mia = target.createMCInstrAnalysis(mii)
|
||||
raise_on_false("instr analysis", mia)
|
||||
mip = target.createMCInstPrinter(mai.getAssemblerDialect(),
|
||||
mai, mii, mri, sti)
|
||||
|
||||
return Disassembler(target.createMCDisassembler(sti),
|
||||
mri, mai, mii, mia, mip)
|
||||
|
||||
@staticmethod
|
||||
def new_from_triple(triple='', cpu='', features=''):
|
||||
|
|
@ -61,19 +85,14 @@ class Disassembler:
|
|||
if not target.hasMCDisassembler():
|
||||
raise llvm.LLVMException(target, "No disassembler provided for %s." % triple)
|
||||
|
||||
sti = target.createMCSubtargetInfo(triple, cpu, features)
|
||||
if not sti:
|
||||
raise llvm.LLVMException("Could not create sub target info")
|
||||
|
||||
return Disassembler.new_from_target(target, sti)
|
||||
return Disassembler.new_from_target(target, triple, cpu, features)
|
||||
|
||||
@staticmethod
|
||||
def new_from_name(name, cpu='', features=''):
|
||||
name = name.strip()
|
||||
for target in api.llvm.TargetRegistry.targetsList():
|
||||
if name == target.getName():
|
||||
sti = target.createMCSubtargetInfo(name, cpu, features)
|
||||
return Disassembler.new_from_target(target, sti)
|
||||
return Disassembler.new_from_target(target, name, cpu, features)
|
||||
|
||||
raise llvm.LLVMException("failed to find target with name %s" % name)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ MCInst = llvm.Class()
|
|||
MCOperand = llvm.Class()
|
||||
MCExpr = llvm.Class()
|
||||
MCAsmInfo = llvm.Class()
|
||||
MCRegisterInfo = llvm.Class()
|
||||
MCInstrInfo = llvm.Class()
|
||||
MCInstrAnalysis = llvm.Class()
|
||||
MCInstPrinter = llvm.Class()
|
||||
|
||||
@MCSubtargetInfo
|
||||
class MCSubtargetInfo:
|
||||
|
|
@ -49,6 +53,24 @@ class MCInst:
|
|||
class MCAsmInfo:
|
||||
_include_ = "llvm/MC/MCAsmInfo.h"
|
||||
|
||||
getAssemblerDialect = Method(cast(Unsigned, int))
|
||||
|
||||
@MCRegisterInfo
|
||||
class MCRegisterInfo:
|
||||
_include_ = "llvm/MC/MCRegisterInfo.h"
|
||||
|
||||
@MCInstrInfo
|
||||
class MCInstrInfo:
|
||||
_include_ = "llvm/MC/MCInstrInfo.h"
|
||||
|
||||
@MCInstrAnalysis
|
||||
class MCInstrAnalysis:
|
||||
_include_ = "llvm/MC/MCInstrAnalysis.h"
|
||||
|
||||
@MCInstPrinter
|
||||
class MCInstPrinter:
|
||||
_include_ = "llvm/MC/MCInstPrinter.h"
|
||||
|
||||
@MCDisassembler
|
||||
class MCDisassembler:
|
||||
_include_ = "llvm/MC/MCDisassembler.h"
|
||||
|
|
|
|||
|
|
@ -15,6 +15,11 @@ from src.Support.CodeGen import Reloc, CodeModel, CodeGenOpt
|
|||
if LLVM_VERSION >= (3, 4):
|
||||
from src.MC import MCSubtargetInfo
|
||||
from src.MC import MCDisassembler
|
||||
from src.MC import MCRegisterInfo
|
||||
from src.MC import MCAsmInfo
|
||||
from src.MC import MCInstrInfo
|
||||
from src.MC import MCInstrAnalysis
|
||||
from src.MC import MCInstPrinter
|
||||
|
||||
@Target
|
||||
class Target:
|
||||
|
|
@ -56,7 +61,26 @@ class Target:
|
|||
|
||||
createMCDisassembler = Method(ptr(MCDisassembler), ref(MCSubtargetInfo))
|
||||
|
||||
createMCRegInfo = Method(ptr(MCRegisterInfo),
|
||||
cast(str, StringRef) #Triple
|
||||
)
|
||||
|
||||
createMCAsmInfo = Method(ptr(MCAsmInfo),
|
||||
const(ref(MCRegisterInfo)), #MRI
|
||||
cast(str, StringRef) #Triple
|
||||
)
|
||||
|
||||
createMCInstrInfo = Method(ptr(MCInstrInfo))
|
||||
|
||||
createMCInstrAnalysis = Method(ptr(MCInstrAnalysis), const(ptr(MCInstrInfo)))
|
||||
|
||||
createMCInstPrinter = Method(ptr(MCInstPrinter),
|
||||
cast(int, Unsigned), #SyntaxVariant
|
||||
const(ref(MCAsmInfo)), #MAI
|
||||
const(ref(MCInstrInfo)), #MII
|
||||
const(ref(MCRegisterInfo)), #MRI
|
||||
const(ref(MCSubtargetInfo)) #STI
|
||||
)
|
||||
@TargetRegistry
|
||||
class TargetRegistry:
|
||||
printRegisteredTargetsForVersion = StaticMethod()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue