|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Any, Dict, Iterable, List, Tuple |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | +import ohre |
| 8 | +from ohre.misc import Log, utils |
| 9 | + |
| 10 | + |
| 11 | +class ISA: |
| 12 | + def __init__(self, isa_file_path: str): |
| 13 | + self.ori_d: Dict = utils.read_dict_from_yaml_file(isa_file_path) |
| 14 | + assert self.ori_d is not None |
| 15 | + |
| 16 | + self.prefixes: Dict | None = None |
| 17 | + self.prefixes = self._get_prefixes_dict() |
| 18 | + assert self.prefixes is not None |
| 19 | + Log.info(f"[ISA] self.prefixes {len(self.prefixes)} {self.prefixes}") |
| 20 | + |
| 21 | + self.opstr2infod: Dict[str, Dict] | None = None |
| 22 | + self.opstr2infod = self._get_opstr_dict() |
| 23 | + assert self.opstr2infod is not None |
| 24 | + Log.info(f"[ISA] self.opstr2infod {len(self.opstr2infod)} keys: {self.opstr2infod.keys()}") |
| 25 | + |
| 26 | + def _get_prefixes_dict(self) -> Dict: |
| 27 | + if (self.prefixes is not None): |
| 28 | + return self.prefixes |
| 29 | + ret = {} |
| 30 | + for sub_d in self.ori_d["prefixes"]: |
| 31 | + ret[sub_d["name"]] = {"description": sub_d["description"], "opcode_idx": sub_d["opcode_idx"]} |
| 32 | + return ret |
| 33 | + |
| 34 | + def _get_prefix_opcode(self, prefix: str) -> int: |
| 35 | + if (prefix in self.prefixes.keys()): |
| 36 | + return self.prefixes[prefix]["opcode_idx"] |
| 37 | + return -1 |
| 38 | + |
| 39 | + def _get_opstr_dict(self) -> Dict[str, Dict]: |
| 40 | + ret = dict() |
| 41 | + for group in self.ori_d["groups"]: |
| 42 | + title = group["title"] if "title" in group.keys() else None |
| 43 | + assert len(title) > 0 and isinstance(title, str) |
| 44 | + description: str = group["description"].strip() if "description" in group.keys() else None |
| 45 | + verification: List | None = group["verification"] if "verification" in group.keys() else None |
| 46 | + exceptions: List | None = group["exceptions"] if "exceptions" in group.keys() else None |
| 47 | + properties: List | None = group["properties"] if "properties" in group.keys() else None |
| 48 | + namespace: str = group["namespace"].strip() if "namespace" in group.keys() else None |
| 49 | + pseudo: str = group["pseudo"].strip() if "pseudo" in group.keys() else None |
| 50 | + semantics: str = group["semantics"].strip() if "semantics" in group.keys() else None |
| 51 | + |
| 52 | + assert "instructions" in group.keys() |
| 53 | + for ins in group["instructions"]: |
| 54 | + assert "sig" in ins.keys() and "opcode_idx" in ins.keys() |
| 55 | + opstr = ins["sig"].split(" ")[0].strip() |
| 56 | + opcode_idx = ins["opcode_idx"] |
| 57 | + |
| 58 | + acc = ins["acc"] if "acc" in ins.keys() else None |
| 59 | + format = ins["format"] if "format" in ins.keys() else None |
| 60 | + prefix = ins["prefix"] if "prefix" in ins.keys() else None |
| 61 | + |
| 62 | + if (prefix is not None): # final_opcode = prefix_opcode|op_code # concat, not 'or' |
| 63 | + prefix_opcode = self._get_prefix_opcode(prefix) |
| 64 | + assert prefix_opcode != -1 |
| 65 | + opcode_idx = [(prefix_opcode << 8) + op_code for op_code in opcode_idx] |
| 66 | + |
| 67 | + ret[opstr] = { |
| 68 | + "sig": ins["sig"], |
| 69 | + "acc": acc, "opcode_idx": opcode_idx, "prefix": prefix, "format": format, "title": title, |
| 70 | + "description": description, "verification": verification, "exceptions": exceptions, |
| 71 | + "properties": properties, "namespace": namespace, "pseudo": pseudo, "semantics": semantics} |
| 72 | + return ret |
| 73 | + |
| 74 | + def get_opcodes(self, opstr: str) -> List | None: |
| 75 | + opcode_info_d = self.get_opcode_info_dict(opstr) |
| 76 | + if (opcode_info_d is None): |
| 77 | + return None |
| 78 | + else: |
| 79 | + if ("opcode_idx" in opcode_info_d.keys()): |
| 80 | + return opcode_info_d["opcode_idx"] |
| 81 | + else: |
| 82 | + Log.warn(f"[ISA] opstr {opstr}, opcode_idx not in {opcode_info_d.keys()}") |
| 83 | + return None |
| 84 | + |
| 85 | + def get_opcode_info_dict(self, opstr: str) -> Dict | None: |
| 86 | + if opstr in self.opstr2infod.keys(): |
| 87 | + return self.opstr2infod[opstr] |
| 88 | + else: |
| 89 | + Log.warn(f"[ISA] opstr NOT hit directly, opstr {opstr}, remove prefix and match again", True) |
| 90 | + for key_opstr in self.opstr2infod.keys(): |
| 91 | + opstr_rhs = key_opstr |
| 92 | + tmp = opstr_rhs.split(".") |
| 93 | + if (len(tmp) > 1 and opstr == tmp[1]): |
| 94 | + Log.warn(f"[ISA] opstr change: {opstr} -> {key_opstr}", True) |
| 95 | + return self.opstr2infod[key_opstr] |
| 96 | + return None |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + ohre.set_log_print(True) |
| 101 | + d = utils.read_dict_from_yaml_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "isa.yaml")) |
| 102 | + isa = ISA(os.path.join(os.path.dirname(os.path.abspath(__file__)), "isa.yaml")) |
| 103 | + # print(json.dumps(isa.ori_d["groups"], indent=4)) |
| 104 | + assert isa.get_opcodes("deprecated.getiteratornext") == [0xfc02] |
| 105 | + assert isa.get_opcodes("callruntime.notifyconcurrentresult") == [0xfb00] |
| 106 | + for ins_str in ["mov", "callruntime.definefieldbyindex", "isin"]: |
| 107 | + print(f"{ins_str}: {utils.hexstr(isa.get_opcodes(ins_str))} {isa.get_opcode_info_dict(ins_str)}") |
| 108 | + title_set = set() |
| 109 | + for opstr in isa.opstr2infod.keys(): |
| 110 | + title_set.add(isa.opstr2infod[opstr]["title"]) |
| 111 | + print(f"{len(title_set)} {title_set}") |
0 commit comments