Skip to content

Commit

Permalink
merging by hand LRGH pull request + minor typos
Browse files Browse the repository at this point in the history
  • Loading branch information
bdcht committed May 9, 2020
1 parent 117e9f5 commit 0a6ba6a
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 88 deletions.
2 changes: 1 addition & 1 deletion amoco/arch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class Formatter(object):

def __init__(self, formats):
self.formats = formats
self.default = ("{i.mnemonic} ", lambda i: ", ".join(map(str, i.operands)))
self.default = ("{i.mnemonic:<20}", lambda i: ", ".join(map(str, i.operands)))

def getkey(self, i):
if i.mnemonic in self.formats:
Expand Down
18 changes: 9 additions & 9 deletions amoco/arch/mips/r3000/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,46 +145,46 @@ def i_BLTZAL(ins,fmap):

@__npc
def i_BREAK(ins,fmap):
ext("BREAK").call(fmap,ins.code)
ext("BREAK").call(fmap,code=ins.code)

@__npc
def i_CFC(ins, fmap):
rt, rd = ins.operands
if rt is not zero:
fmap[rt] = ext("CFC%d"%(ins.z),size=rt.size).call(fmap,rd)
fmap[rt] = ext("CFC%d"%(ins.z),size=rt.size).call(fmap,rd=rd)

@__npc
def i_MFC(ins, fmap):
rt, rd = ins.operands
if rt is not zero:
fmap[rt] = ext("MFC%d"%(ins.z),size=rt.size).call(fmap,rd)
fmap[rt] = ext("MFC%d"%(ins.z),size=rt.size).call(fmap,rd=rd)

@__npc
def i_COP(ins, fmap):
fun = ins.cofun
ext("COP%d"%(ins.z)).call(fmap,fun)
ext("COP%d"%(ins.z)).call(fmap,cofun=fun)

@__npc
def i_CTC(ins, fmap):
rt, rd = ins.operands
ext("CTC%d"%(ins.z)).call(fmap,rd,rt)
ext("CTC%d"%(ins.z)).call(fmap,rd=rd,rt=rt)

@__npc
def i_MTC(ins, fmap):
rt, rd = ins.operands
ext("MTC%d"%(ins.z)).call(fmap,rd,rt)
ext("MTC%d"%(ins.z)).call(fmap,rd=rd,rt=rt)

@__npc
def i_LWC(ins, fmap):
rt, base, offset = ins.operands
data = mem(base+offset,32)
ext("LWC%d"%(ins.z)).call(fmap,rt,data)
ext("LWC%d"%(ins.z)).call(fmap,rt=rt,data=data)

@__npc
def i_SWC(ins, fmap):
rt, base, offset = ins.operands
addr = fmap(base+offset)
fmap[mem(data,32)] = ext("SWC%d"%(ins.z),size=32).call(fmap,rt)
fmap[mem(data,32)] = ext("SWC%d"%(ins.z),size=32).call(fmap,rt=rt)

@__npc
def i_DIV(ins, fmap):
Expand Down Expand Up @@ -407,4 +407,4 @@ def i_SRLV(ins, fmap):

@__npc
def i_SYSCALL(ins, fmap):
ext("SYSCALL").call(fmap,ins.code)
ext("SYSCALL").call(fmap,code=ins.code)
2 changes: 1 addition & 1 deletion amoco/arch/sparc/cpu_v8.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from amoco.arch.sparc.formats import SPARC_V8_full
from amoco.arch.sparc.formats import SPARC_V8_synthetic

instruction_sparc.set_formatter(SPARC_V8_full)
instruction_sparc.set_formatter(SPARC_V8_synthetic)

# define disassembler:
from amoco.arch.sparc import spec_v8
Expand Down
152 changes: 87 additions & 65 deletions amoco/arch/sparc/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from .env import *
from .utils import *
from amoco.arch.core import Formatter
from amoco.ui.render import Token, TokenListJoin
from amoco.ui.render import highlight, Token, TokenListJoin
from amoco.ui.render import replace_mnemonic_token, replace_opn_token

whitespace = " "

Expand All @@ -26,25 +27,25 @@ def address(a):


def deref(a):
return (
return [
[(Token.Memory, "[")]
+ address(a.base + a.disp)
+ [(Token.Memory, "]%s" % a.seg)]
)
+ [(Token.Memory, "]%s" % (a.seg or ""))]
]


def mnemo_icc(i):
s = i.mnemonic
if i.misc["icc"]:
s += "cc"
return [(Token.Mnemonic, s)]
return [(Token.Mnemonic, "{:<8}".format(s))]


def mnemo_cond(i):
s = CONDxB[i.mnemonic][i.cond]
if i.misc["annul"]:
s += ",a"
return [(Token.Mnemonic, s)]
return [(Token.Mnemonic, "{:<8}".format(s))]


def reg_or_imm(x, t="%d"):
Expand Down Expand Up @@ -97,9 +98,10 @@ def label(i):
if i.operands[0]._is_reg:
return [(Token.Register, "%" + str(i.operands[0].ref))]
if i.operands[0]._is_cst:
return [(Token.Address, "%s" % i.misc["dst"])]
offset = i.operands[0].signextend(32) * 4
return [(Token.Address, str(_pc + offset))]
offset = i.operands[0].signextend(32) * 4
target = i.misc["dst"] or (_pc+offset)
return [(Token.Address, str(target))]
raise TypeError("operand type not supported")


CONDB = {
Expand Down Expand Up @@ -177,7 +179,7 @@ def label(i):

CONDxB = {"b": CONDB, "fb": CONDFB, "cb": CONDCB}

mnemo = lambda i: [(Token.Mnemonic, "{i.mnemonic}".format(i=i))]
mnemo = lambda i: [(Token.Mnemonic, "{i.mnemonic:<8}".format(i=i))]
format_mn = [mnemo]
format_regs = [mnemo, lambda i: TokenListJoin(", ", regs(i))]
format_ld = [mnemo, lambda i: TokenListJoin(", ", deref(i.operands[0]) + regn(i, 1))]
Expand Down Expand Up @@ -205,7 +207,7 @@ def label(i):
lambda i: address(i.operands[0]) + [(Token.Literal, ", ")] + regn(i, 1),
]
format_addr = [mnemo, lambda i: address(i.operands[0])]
format_t = [lambda i: [(Token.Mnemonic, CONDT[i.cond])] + reg_or_imm(i.operands[0])]
format_t = [lambda i: [(Token.Mnemonic, "{:<8}".format(CONDT[i.cond]))] + reg_or_imm(i.operands[0])]
format_rd = format_regs
format_wr = [
mnemo,
Expand Down Expand Up @@ -248,87 +250,107 @@ def label(i):


def SPARC_V8_synthetic(null, i, toks=False):
s = SPARC_V8_full(i)
return SPARC_Synthetic_renaming(s, i)
s = SPARC_V8_full(i, True)
return SPARC_Synthetic_renaming(s, i, toks)


def SPARC_Synthetic_renaming(s, i):
def SPARC_Synthetic_renaming(s, i, toks=False):
if i.mnemonic == "sethi" and i.operands[0] == cst(0, 22) and i.operands[1] == g0:
return "nop"
if (
i.mnemonic == "or"
and not i.misc["icc"]
and i.operands[0] == i.operands[1] == g0
):
return s.replace("or", "clr").replace("%g0, ", "")
if i.mnemonic == "or" and not i.misc["icc"] and i.operands[0] == g0:
return s.replace("or", "mov").replace("%g0, ", "")
if i.mnemonic == "or" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
return s.replace("or", "bset").replace("%%%s," % i.operands[0], "", 1)
if i.mnemonic == "rd":
replace_mnemonic_token(s,"clr")
replace_opn_token(s,1,None)
replace_opn_token(s,0,None)
elif i.mnemonic == "or" and not i.misc["icc"] and i.operands[0] == g0:
replace_mnemonic_token(s,"mov")
replace_opn_token(s,0,None)
elif i.mnemonic == "or" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
replace_mnemonic_token(s,"bset")
replace_opn_token(s,0,None)
elif i.mnemonic == "rd":
op1 = str(i.operands[0])
if op1.startswith("asr") or op1 in ("y", "psr", "wim", "tbr"):
return s.replace("rd", "mov")
if i.mnemonic == "wr" and i.operands[0] == g0:
return s.replace("wr", "mov").replace("%g0,", "")
if i.mnemonic == "sub" and i.misc["icc"] and i.operands[2] == g0:
return s.replace("subcc", "cmp").replace(", %g0", "")
if i.mnemonic == "jmpl" and i.operands[1] == g0:
replace_mnemonic_token(s,"mov")
elif i.mnemonic == "wr" and i.operands[0] == g0:
replace_mnemonic_token(s,"mov")
replace_opn_token(s,0,None)
elif i.mnemonic == "sub" and i.misc["icc"] and i.operands[2] == g0:
replace_mnemonic_token(s,"cmp")
replace_opn_token(s,2,None)
elif i.mnemonic == "jmpl" and i.operands[1] == g0:
if i.operands[0] == (i7 + cst(8)):
return "ret"
if i.operands[0] == (o7 + cst(8)):
return "retl"
return s.replace("jmpl", "jmp").replace(", %g0", "")
if i.mnemonic == "jmpl" and i.operands[1] == o7:
return s.replace("jmpl", "call").replace(", %o7", "")
if (
s = [(Token.Mnemonic, "ret")]
elif i.operands[0] == (o7 + cst(8)):
s = [(Token.Mnemonic, "retl")]
else:
replace_mnemonic_token(s,"jmp")
replace_opn_token(s,1,None)
elif i.mnemonic == "jmpl" and i.operands[1] == o7:
replace_mnemonic_token(s,"call")
replace_opn_token(s,1,None)
elif (
i.mnemonic == "or"
and i.misc["icc"]
and i.operands[1]._is_reg
and i.operands[0] == i.operands[2] == g0
):
return s.replace("orcc", "tst").replace("%g0,", "").replace(", %g0", "")
if (
replace_mnemonic_token(s,"tst")
replace_opn_token(s,2,None)
replace_opn_token(s,0,None)
elif (
i.mnemonic == "restore"
and i.operands[0] == i.operands[1] == i.operands[2] == g0
):
return "restore"
s = [(Token.Mnemonic, "restore")]
if i.mnemonic == "save" and i.operands[0] == i.operands[1] == i.operands[2] == g0:
return "save"
s = [(Token.Mnemonic, "save")]
if i.mnemonic == "xnor" and i.operands[1] == g0:
s = s.replace("xnor", "not").replace("%g0,", "", 1)
replace_mnemonic_token(s,"not")
replace_opn_token(s,1,None)
if i.operands[0] == i.operands[2]:
return s.rpartition(",")[0]
return s
if i.mnemonic == "sub" and i.operands[0] == g0 and i.operands[1]._is_reg:
s = s.replace("sub", "neg").replace("%g0,", "", 1)
replace_opn_token(s,2,None)
elif i.mnemonic == "sub" and i.operands[0] == g0 and i.operands[1]._is_reg:
replace_mnemonic_token(s,"neg")
replace_opn_token(s,0,None)
if i.operands[1] == i.operands[2]:
return s.rpartition(",")[0]
return s
if i.mnemonic == "add" and i.operands[0] == i.operands[2] and i.operands[1]._is_cst:
replace_opn_token(s,2,None)
elif i.mnemonic == "add" and i.operands[0] == i.operands[2] and i.operands[1]._is_cst:
m = "inccc" if i.misc["icc"] else "inc"
replace_mnemonic_token(s,m)
if i.operands[1] == 1:
return "{}{}%{}".format(m, whitespace, i.operands[0])
replace_opn_token(s,2,None)
replace_opn_token(s,1,None)
else:
return "{}{}{}, %{}".format(m, whitespace, i.operands[1], i.operands[0])
if i.mnemonic == "sub" and i.operands[0] == i.operands[2] and i.operands[1]._is_cst:
replace_opn_token(s,0,None)
elif i.mnemonic == "sub" and i.operands[0] == i.operands[2] and i.operands[1]._is_cst:
m = "deccc" if i.misc["icc"] else "dec"
replace_mnemonic_token(s,m)
if i.operands[1] == 1:
return "{} %{}".format(m, i.operands[0])
replace_opn_token(s,2,None)
replace_opn_token(s,1,None)
else:
return "{}{}{}, %{}".format(m, whitespace, i.operands[1], i.operands[0])
if i.mnemonic == "and" and i.misc["icc"] and i.operands[2] == g0:
s = s.replace("andcc", "btst").replace(", %g0", "")
m = s.split()
return "{}{}{}, {}".format(m[0], whitespace, m[2], m[1].replace(",", ""))
if i.mnemonic == "andn" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
return s.replace("andn", "bclr").replace("%%%s," % i.operands[0], "", 1)
if i.mnemonic == "xor" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
return s.replace("xor", "btog").replace("%%%s," % i.operands[0], "", 1)
if i.mnemonic == "stb" and i.operands[0] == g0:
return s.replace("stb", "clrb").replace("%g0, ", "")
if i.mnemonic == "sth" and i.operands[0] == g0:
return s.replace("sth", "clrh").replace("%g0, ", "")
if i.mnemonic == "st" and i.operands[0] == g0:
return s.replace("st", "clr").replace("%g0, ", "")
return s
replace_opn_token(s,0,None)
elif i.mnemonic == "and" and i.misc["icc"] and i.operands[2] == g0:
replace_mnemonic_token(s,"btst")
replace_opn_token(s,2,None)
s[1],s[3] = s[3],s[1]
elif i.mnemonic == "andn" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
replace_mnemonic_token(s,"bclr")
replace_opn_token(s,0,None)
elif i.mnemonic == "xor" and not i.misc["icc"] and i.operands[0] == i.operands[2]:
replace_mnemonic_token(s,"btog")
replace_opn_token(s,0,None)
elif i.mnemonic == "stb" and i.operands[0] == g0:
replace_mnemonic_token(s,"clrb")
replace_opn_token(s,0,None)
elif i.mnemonic == "sth" and i.operands[0] == g0:
replace_mnemonic_token(s,"clrh")
replace_opn_token(s,0,None)
elif i.mnemonic == "st" and i.operands[0] == g0:
replace_mnemonic_token(s,"clr")
replace_opn_token(s,0,None)
return s if toks else highlight(s)
Loading

0 comments on commit 0a6ba6a

Please sign in to comment.