[{"class" : Goto}, {"class" : Label}, ] ) def match(self, fn, items): if items[0].target != items[1].name: return False return True # The action here is simple - just remove the instruction def execute(self, fn, items): items[0] = Nop() return items # Keep the label addTemplate(MatchClass()) # goto LABEL #LABEL2: #LABEL: #-> #LABEL2: #LABEL: class MatchClass2(Template): def __init__(self): Template.__init__(self, [{"class" : Goto}, {"class" : Label}, {"class" : Label}, ] )
# aload $Y # const xx # store $N class MatchClass(Template): def __init__(self): Template.__init__(self, [ { "class": Const }, { "class": Istore }, { "class": Aload }, ]) def match(self, fn, items): return True def execute(self, fn, items): tmp = items[0] tmp2 = items[1] items[0] = items[2] items[1] = tmp items[2] = tmp2 return items addTemplate(MatchClass())
def match(self, fn, items): return False # FIXME: Disabled for now - causes problems N = items[0].targetLocal if N == items[1].sourceLocal: return False if N != items[2].sourceLocal: return False for item in items[3:-1]: if not midInstructionValid(N, item): return False return True # Remove the istore/iload def execute(self, fn, items): items[0] = Nop() items[2] = Swap() return items for n in range(0, 1): for m in range(0, 6): addTemplate(MatchStoreLoadStore(n, m)) for i in range(0, 6): addTemplate(MatchStoreStore(i)) addTemplate(MatchStoreLoadIreturn()) addTemplate(MatchStoreLoad()) addTemplate(MatchDupIstoreIreturn()) addTemplate(MatchStorePutstaticLoadIreturn()) addTemplate(MatchSwap()) addTemplate(MatchSwapDup())
def match(self, fn, items): if not items[-2].sourceLocal == "3" or not items[ -3].sourceLocal == "2" or not items[ -4].sourceLocal == "1" or not items[-5].sourceLocal == "0": return False for p in range(1, self.n + 1): if items[p * 2 - 2].sourceLocal and int(items[p * 2 - 2].sourceLocal) <= 3: return False if int(items[p * 2 - 1].targetLocal) > 3: return False return True def execute(self, fn, items): for p in range(1, self.n + 1): reg = int(items[p * 2 - 1].targetLocal) items[-5 + reg] = items[p * 2 - 2] items[p * 2 - 2] = Nop() items[p * 2 - 1] = Nop() return items addTemplate(MatchBase(4)) addTemplate(MatchBase(3)) addTemplate(MatchBase(2)) addTemplate(MatchBase(1)) addTemplate(MatchBase2(3)) addTemplate(MatchBase2(2)) addTemplate(MatchBase2(1))
] ) self.n = n def match(self, fn, items): if not items[-2].sourceLocal=="3" or not items[-3].sourceLocal=="2" or not items[-4].sourceLocal=="1" or not items[-5].sourceLocal=="0": return False for p in range(1,self.n+1): if items[p*2-2].sourceLocal and int(items[p*2-2].sourceLocal)<=3: return False if int(items[p*2-1].targetLocal)>3: return False return True def execute(self, fn, items): for p in range(1,self.n+1): reg = int(items[p*2-1].targetLocal) items[-5 + reg] = items[p*2-2] items[p*2-2] = Nop() items[p*2-1] = Nop() return items addTemplate(MatchBase(4)) addTemplate(MatchBase(3)) addTemplate(MatchBase(2)) addTemplate(MatchBase(1)) addTemplate(MatchBase2(3)) addTemplate(MatchBase2(2)) addTemplate(MatchBase2(1))
def __init__(self): Template.__init__(self, [{"class" : Const}, {"class" : Pop}, ] ) def match(self, fn, items): return True # make the iinc a nop and replace the const def execute(self, fn, items): items[0] = Nop() items[1] = Nop() return items # iload $X # pop #-> class IloadMatchClass(MatchClass): def __init__(self): Template.__init__(self, [{"class" : Iload}, {"class" : Pop}, ] ) addTemplate(MatchClass()) addTemplate(IloadMatchClass())
def match(self, fn, items): return True # make the iinc a nop and replace the const def execute(self, fn, items): items[0] = Nop() items[1] = Nop() return items # getstatic # pop2 #-> # pop class MatchPop2(Template): def __init__(self): Template.__init__(self, [{"class" : Getstatic}, {"class" : Pop2}, ] ) # make the iinc a nop and replace the const def execute(self, fn, items): items[0] = Nop() items[1] = Pop() return items addTemplate(MatchPop()) addTemplate(MatchPop2())
def execute(self, fn, items): items[0] = Nop() items[1] = Nop() return items # getstatic # pop2 #-> # pop class MatchPop2(Template): def __init__(self): Template.__init__(self, [ { "class": Getstatic }, { "class": Pop2 }, ]) # make the iinc a nop and replace the const def execute(self, fn, items): items[0] = Nop() items[1] = Pop() return items addTemplate(MatchPop()) addTemplate(MatchPop2())
{"class" : Istore}, {"class" : Istore}, ] + [{"class" : PushInstruction}, {"class" : PopInstruction}] * n + [{"class" : PushInstruction}] * m + [ {"class" : Invokestatic}, {"class" : Getstatic}, {"class" : Istore}, {"class" : Istore}, ] ) def match(self, fn, items): regs = [items[1].targetLocal, items[2].targetLocal] if not items[-1].targetLocal in regs or not items[-2].targetLocal in regs: return False i = 3 while not isinstance(items[i], Getstatic): if items[i].sourceLocal and items[i].sourceLocal in regs: return False i += 1 return True def execute(self, fn, items): items[0] = Nop() items[1] = Nop() items[2] = Pop() return items for n in range(0, 5): for m in range(3, 6): addTemplate(MatchJalReturn(n, m))
{ "class": Istore }, { "class": Istore }, ]) def match(self, fn, items): regs = [items[1].targetLocal, items[2].targetLocal] if not items[-1].targetLocal in regs or not items[ -2].targetLocal in regs: return False i = 3 while not isinstance(items[i], Getstatic): if items[i].sourceLocal and items[i].sourceLocal in regs: return False i += 1 return True def execute(self, fn, items): items[0] = Nop() items[1] = Nop() items[2] = Pop() return items for n in range(0, 5): for m in range(3, 6): addTemplate(MatchJalReturn(n, m))