Skip to content

Commit

Permalink
Merge pull request #12 from ohreteam/pk1
Browse files Browse the repository at this point in the history
update newlexvar, stlexvar, ldlexvar
  • Loading branch information
Penkace authored Jan 19, 2025
2 parents 21d2c49 + e3fca97 commit db94f66
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
52 changes: 50 additions & 2 deletions ohre/abcre/dis/DisFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def __init__(self, value):
self.methods: List[AsmMethod] = list()
self.asmstrs: List[AsmString] = list()
self._debug: List = None
self.lex_env: List = list()
self.cur_lex_level: int = 0

lines: List[str] = list()
if (isinstance(value, str)):
file = open(value, "r", encoding="utf-8", errors="ignore")
for line in file:
lines.append(line)
lines = file.readlines()
file.close()
else:
Log.error(f"DisFile init ERROR: value type NOT supported, {type(value)} {value}")
Expand Down Expand Up @@ -262,3 +263,50 @@ def get_external_module_name(
Log.warn(f"get_external_module_name failed, hit_cnt {hit_cnt} \
file_class_name {file_class_name}", True)
return None

def create_lexical_environment(
self, slots: int, literal_id=None) -> Union[str, None]:
slots_number = slots
lex_env_layer = [None] * slots_number
if literal_id:
print(literal_id)
left_s = literal_id.find('[')
right_s = literal_id.find(']')
literal_content = literal_id[left_s:right_s+1]
literal_content = literal_content.split(',')
cnt = 0
for i in range(slots_number):
literal_value = literal_content[cnt].strip().split(':')
if len(literal_value) == 2:
variable_value = literal_value[1].replace('"', '')
else:
Log.warn(f"newlexenvwithname failed. literal id format is {literal_content[cnt]}")
variable_name = literal_content[cnt+1].strip().split(':')
if len(variable_name)==2:
variable_name = variable_name[1].replace('"','')
else:
Log.warn(f"newlexenvwithname failed. literal id format is {literal_content[cnt+1]}")
# lex_env_layer[i] = f"[variable: {variable_name} value: {variable_value}]"
lex_env_layer[i] = variable_name
cnt += 2
self.lex_env.append(lex_env_layer)
self.cur_lex_level += 1
return self.lex_env[-1]

def get_lex_env(
self, lexenv_layer: int, slot_index: int
):
fetch_lex_env_index = self.cur_lex_level - 1 - lexenv_layer
if fetch_lex_env_index >= 0:
return self.lex_env[fetch_lex_env_index][slot_index]
else:
Log.warn(f"get_lex_env failed, cur_lex {self.cur_lex_level}.\
Wanted fetch level {fetch_lex_env_index}")
return None

def pop_lex_env(self):
if len(self.lex_env) == 0:
Log.warn(f"pop_lex_env failed, self.lex_env is empty")
else:
self.lex_env.pop()
self.cur_lex_level -= 1
22 changes: 22 additions & 0 deletions ohre/abcre/dis/NACtoTAC.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def toTAC(self, nac: NAC, meth: AsmMethod, dis_file: DisFile) -> Union[TAC, List
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.UNDEFINED))
if (nac.op == "asyncfunctionenter"):
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.METHOD_OBJ, name="__asyncfunctionenter"))
if (nac.op == 'poplexenv'):
dis_file.pop_lex_env()
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.LEXENV))
# === inst: constant object loaders # END

# === inst: comparation instructions # START
Expand Down Expand Up @@ -263,6 +266,15 @@ def toTAC(self, nac: NAC, meth: AsmMethod, dis_file: DisFile) -> Union[TAC, List
arg_obj = AsmArg.build_object(kv_dict)
print(f"createobjectwithbuffer-arg_obj {type(arg_obj)} {arg_obj}")
return TAC.tac_assign(AsmArg.ACC(), arg_obj, log="createobjectwithbuffer d3bug")
if (nac.op == "newlexenv"):
slots = int(nac.args[0], base=16)
cur_lex_env = dis_file.create_lexical_environment(slots)
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.LEXENV, value=str(cur_lex_env)))
if (nac.op == "newlexenvwithname"):
slots = int(nac.args[0], base=16)
literal_id = nac.args[1]
cur_lex_env = dis_file.create_lexical_environment(slots,literal_id=literal_id)
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.LEXENV, value=str(cur_lex_env)))
# === inst: object creaters # END

# === inst: object visitors # START
Expand Down Expand Up @@ -296,6 +308,16 @@ def toTAC(self, nac: NAC, meth: AsmMethod, dis_file: DisFile) -> Union[TAC, List
return TAC.tac_call(AsmArg(AsmTypes.IMM, value=2), [AsmArg.build_arg(nac.args[0]), AsmArg.ACC()],
ret_store_to=AsmArg.ACC(),
call_addr=AsmArg(AsmTypes.METHOD, name="__asyncfunctionreject"))
if (nac.op == "stlexvar"):
lexenv_layer = int(nac.args[0], base=16)
slot_index = int(nac.args[1], base=16)
dest = AsmArg(AsmTypes.LEXENV, value=[lexenv_layer, slot_index])
return TAC.tac_assign(dest, AsmArg.ACC())
if (nac.op == 'ldlexvar'):
lexenv_layer = int(nac.args[0], base=16)
slot_index = int(nac.args[1], base=16)
lex_value = dis_file.get_lex_env(lexenv_layer, slot_index)
return TAC.tac_assign(AsmArg.ACC(), AsmArg(AsmTypes.LEXENV, value=lex_value))
# === inst: object visitors # END

# === inst: definition instuctions # START
Expand Down
1 change: 1 addition & 0 deletions ohre/abcre/dis/enum/AsmTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AsmTypes(BaseEnum):
ARRAY = "array" # value is also arg
UNDEFINED = "undefined"
HOLE = "hole"
LEXENV = "lexenv"
UNKNOWN = "unknown" # default value in this proj # maybe same as Any

def __init__(self):
Expand Down

0 comments on commit db94f66

Please sign in to comment.