-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
183 lines (169 loc) · 5.59 KB
/
process.py
File metadata and controls
183 lines (169 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright 2025 Yingwei Zheng
# SPDX-License-Identifier: Apache-2.0
import json
import sys
import subprocess
import tqdm
import os
from targets import SUPPORTED_TARGETS
PROPERTIES_LIST = [
"isReturn",
"isBranch",
"isEHScopeReturn",
"isIndirectBranch",
"isCompare",
"isMoveImm",
"isMoveReg",
"isBitcast",
"isSelect",
"isBarrier",
"isCall",
"isAdd",
"isTrap",
"mayLoad",
"mayStore",
"mayRaiseFPException",
"isTerminator",
"hasDelaySlot",
"hasCtrlDep",
"isNotDuplicable",
"isConvergent",
"isAsCheapAsAMove",
"hasSideEffects",
]
def convert_operand_list(args):
res = []
for arg, identifier in args:
res.append(f"{arg['printable']}:{identifier}")
return res
def encode_var_bit(var, msb, lsb):
if msb == lsb:
return var + "[" + str(msb) + "]"
return var + "[" + str(msb) + ":" + str(lsb) + "]"
def encode_inst(encoding):
bit_string = ""
var = None
var_msb = None
var_lsb = None
for bit in reversed(encoding):
if bit is None:
bit_string += "?"
elif isinstance(bit, int):
if var:
bit_string += encode_var_bit(var, var_msb, var_lsb)
var = None
bit_string += str(bit)
elif bit["kind"] == "var":
bit_string += bit["var"] + '[0]'
elif bit["kind"] == "varbit":
new_var = bit["var"]
new_idx = bit["index"]
if var is None:
var = new_var
var_msb = var_lsb = new_idx
elif var == new_var and new_idx + 1 == var_lsb:
var_lsb = new_idx
else:
bit_string += encode_var_bit(var, var_msb, var_lsb)
var = new_var
var_msb = var_lsb = new_idx
else:
assert bit["kind"] == "complex"
return None
return bit_string
def convert_json(target, input_json, output_json):
with open(input_json) as f:
obj = json.load(f)
inst_list = []
for item in tqdm.tqdm(obj.values()):
if not isinstance(item, dict):
continue
if item.get("!anonymous", True):
continue
name = item.get("!name")
if not name:
continue
fields = item.get("!fields", [])
superclasses = item.get("!superclasses", [])
if "Inst" in fields or "Instruction" in superclasses:
if item["isCodeGenOnly"] == 1:
continue
if item["isPseudo"] == 1:
continue
if item["isPreISelOpcode"] == 1:
continue
# print(json.dumps(item, indent=2))
inst_obj = dict()
inst_obj["Name"] = name
inst_obj["AsmString"] = item["AsmString"]
if item["DecoderNamespace"] != "":
inst_obj["DecoderNamespace"] = item["DecoderNamespace"]
if item["Constraints"] != "":
inst_obj["Constraints"] = item["Constraints"]
in_ops = convert_operand_list(item["InOperandList"]["args"])
if len(in_ops) > 0:
inst_obj["Inputs"] = in_ops
out_ops = convert_operand_list(item["OutOperandList"]["args"])
if len(out_ops) > 0:
inst_obj["Outputs"] = out_ops
predicates = set()
for pred in item["Predicates"]:
predicates.add(pred["printable"])
if len(predicates) > 0:
inst_obj["Predicates"] = list(predicates)
if "Inst" in item:
inst_obj["Size"] = item["Size"]
inst_encoding = encode_inst(item["Inst"])
if inst_encoding:
inst_obj["Encoding"] = inst_encoding
elif "X86Inst" in superclasses:
# TODO: handle X86 inst prefixes
pass
properties = []
for key in PROPERTIES_LIST:
if item.get(key, False):
properties.append(key)
if len(properties) > 0:
properties.sort()
inst_obj["Properties"] = properties
inst_list.append(inst_obj)
inst_list.sort(key=lambda x: x["Name"])
obj = {
"Target": target,
"Insts": inst_list,
}
with open(output_json, "w") as f:
json.dump(obj, f)
if __name__ == "__main__":
llvm_src = sys.argv[1]
llvm_tblgen = sys.argv[2]
build_dir = "build"
os.makedirs(build_dir, exist_ok=True)
original_dir = os.path.join(build_dir, "original")
os.makedirs(original_dir, exist_ok=True)
artifact_dir = os.path.join(build_dir, "artifact")
os.makedirs(artifact_dir, exist_ok=True)
include_dir = os.path.join(llvm_src, "llvm/include")
for target in SUPPORTED_TARGETS:
print("Converting", target)
intermediate_json = os.path.join(original_dir, target + ".json")
target_dir = os.path.join(llvm_src, "llvm/lib/Target", target)
target_td = target + ".td"
if target == "PowerPC":
target_td = "PPC.td"
if not os.path.exists(intermediate_json):
subprocess.check_call(
[
llvm_tblgen,
"--dump-json",
os.path.join(target_dir, target_td),
"-o",
intermediate_json,
"-I",
include_dir,
"-I",
target_dir,
]
)
artifact_json = os.path.join(artifact_dir, target + ".json")
convert_json(target, intermediate_json, artifact_json)