-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from ohre.abcre.dis.AsmTypes import AsmTypes | ||
from ohre.misc import Log, utils | ||
|
||
|
||
class AsmArg: | ||
def __init__(self, arg_type: AsmTypes = AsmTypes.UNKNOWN, name="", value=None, obj_ref=None): | ||
self.type = arg_type | ||
# name: e.g. for v0, type is VAR, name is v0(stored without truncating the prefix v) | ||
self.name = name | ||
# value: may be set in the subsequent analysis | ||
self.value = value | ||
self.obj_ref = obj_ref | ||
|
||
def __str__(self): | ||
return self.debug_short() | ||
|
||
@classmethod | ||
def build_arg(cls, s: str): | ||
assert isinstance(s, str) and len(s) > 0 | ||
if (s.startswith("v")): | ||
return AsmArg(AsmTypes.VAR, s) | ||
if (s.startswith("a")): | ||
return AsmArg(AsmTypes.ARG, s) | ||
Log.error(f"build_arg failed: s={s}") | ||
|
||
def is_value_valid(self) -> bool: # TODO: for some types, value is not valid, judge it | ||
pass | ||
|
||
def debug_short(self): | ||
out = f"{AsmTypes.get_code_name(self.type)}-{self.name}" | ||
if (self.value is not None): | ||
out += f"({self.value})" | ||
if (self.obj_ref is not None): | ||
out += f"//{self.obj_ref}" | ||
return out | ||
|
||
def debug_deep(self): | ||
out = f"{self.debug_short()}" | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from ohre.abcre.dis.NAC import NAC | ||
from ohre.abcre.dis.TAC import TAC | ||
from ohre.abcre.dis.AsmArg import AsmArg | ||
from ohre.abcre.dis.CODE_LV import CODE_LV | ||
from ohre.abcre.dis.CodeBlocks import CodeBlocks | ||
from ohre.abcre.dis.AsmTypes import AsmTypes | ||
from ohre.misc import Log, utils | ||
|
||
|
||
class NativeToTAC: | ||
@classmethod | ||
def toTAC(cls, nac: NAC) -> TAC: | ||
print(f"toTAC: nac: {nac.debug_deep()}") # TODO: more tac builder plz | ||
if (nac.op == "mov"): # mov v1:out:any, v2:in:any # mov v0, a0 | ||
return TAC.tac_assign(AsmArg.build_arg(nac.args[0]), AsmArg.build_arg(nac.args[1])) | ||
if (nac.op == "lda"): # lda v:in:any # lda v8 | ||
return TAC.tac_assign(AsmArg(AsmTypes.ACC), AsmArg.build_arg(nac.args[0])) | ||
if (nac.op == "sta"): # sta v:out:any # sta v6 | ||
return TAC.tac_assign(AsmArg.build_arg(nac.args[0]), AsmArg(AsmTypes.ACC)) | ||
if (nac.op == "ldobjbyname"): # ldobjbyname imm:u16, string_id # ldobjbyname 0x0, "code" | ||
return TAC.tac_assign( | ||
AsmArg(AsmTypes.ACC), | ||
AsmArg(AsmTypes.ACC, "", nac.args[1]), | ||
log=f"arg0: {nac.args[0]} ") | ||
if (nac.op == "isfalse"): # acc = ecma_op(acc, operand_0, ..., operands_n) | ||
return TAC.tac_assign(AsmArg(AsmTypes.ACC), AsmArg(AsmTypes.ACC), AsmArg(AsmTypes.FALSE), rop="==") | ||
if (nac.op == "jnez"): # jnez imm:i32 # a label str in *.dis file | ||
return TAC.tac_assign() | ||
else: | ||
Log.error(f"toTAC failed, not support nac inst: {nac.debug_deep()}") | ||
return None | ||
|
||
@classmethod | ||
def native_code_to_TAC(cls, blocks: CodeBlocks) -> CodeBlocks: | ||
assert blocks.level == CODE_LV.NATIVE_BLOCK_SPLITED | ||
for block in blocks.blocks: | ||
for nac_inst in block.insts: | ||
tac_inst = NativeToTAC.toTAC(nac_inst) | ||
print(f"toTAC: tac: {tac_inst.debug_deep()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
from typing import Any, Dict, Iterable, List, Tuple | ||
|
||
from ohre.abcre.dis.NACTYPE import NACTYPE | ||
from ohre.abcre.dis.AsmArg import AsmArg | ||
from ohre.abcre.dis.TACTYPE import TACTYPE | ||
|
||
|
||
class TAC(): # Three Address Code | ||
|
||
def __init__(self, optype, op_args: List): | ||
def __init__(self, optype=TACTYPE.UNKNOWN, args: List[AsmArg] = None, rop="", log=""): | ||
self.optype = optype | ||
self.args = None | ||
self.args = args | ||
self.rop = rop # rhs op # e.g. acc = a1 + v1 # rop is "+" | ||
self.log = log | ||
|
||
@classmethod | ||
def tac_assign(cls, dst: AsmArg, src0: AsmArg, src1: AsmArg = None, rop="", log: str = ""): | ||
if (src1 is None): | ||
return TAC(TACTYPE.ASSIGN, [dst, src0]) | ||
assert src1 is not None and rop is not None and len(rop) > 0 | ||
return TAC(TACTYPE.ASSIGN_BI, [dst, src0, src1], rop=rop) | ||
|
||
def __str__(self): | ||
return self.debug_short() | ||
|
||
def debug_short(self): | ||
out = f"[{TACTYPE.get_code_name(self.optype)}]\t" | ||
|
||
for i in range(len(self.args)): | ||
out += f"{self.args[i].debug_short()}, " | ||
return out | ||
|
||
def debug_deep(self): | ||
out = f"[{TACTYPE.get_code_name(self.optype)}]\t" | ||
for i in range(len(self.args)): | ||
out += f"{self.args[i].debug_deep()} " | ||
if (i == 1 and self.rop is not None and len(self.rop) > 0): | ||
out += f"({self.rop}) " | ||
if (self.log is not None and len(self.log) > 0): | ||
out += f" // {self.log}" | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
from ohre.abcre.enum.BaseEnum import BaseEnum | ||
|
||
|
||
class TACTYPE(BaseEnum): | ||
def __init__(self): | ||
super().__init__() | ||
ASSIGN = 0 | ||
ASSIGN_BI = 1 | ||
COND_JMP = 10 # 3 arg | ||
UNCN_JMP = 11 # 1 arg # unconditional | ||
RETURN = 20 | ||
UNKNOWN = 99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters