Commit de2867ae authored by Kunshan Wang's avatar Kunshan Wang

internal IR builder nodes

parent 3884e361
""" This is for one-time use only. It generates contents of
uvm.ir.irbuilder/irBuilderNode.scala, but that file should be maintained
manually in the long run. """
import muapiparser
from refimpl2injectablefiles import muapi_h_path
from muapitocstubs import toCamelCase
_to_scala_type = {
"int": "Int",
"MuBool": "Boolean",
}
def to_scala_type(rt):
return _to_scala_type.get(rt, rt)
def generate_node(method):
func_name = toCamelCase(method["name"])
node_name = "Node" + func_name[3:]
params = method["params"][1:] # skip MuIRBuilder*
node_params = []
for param in params:
pn = param['name']
pt = param['type']
nn = toCamelCase(pn)
if param.get("is_sz_param", False):
continue
elif 'array_sz_param' in param:
assert pt.endswith("*")
rt = pt[:-1]
st = to_scala_type(rt)
nt = "Seq[{}]".format(pt[:-1])
elif param.get('is_optional', False):
assert pt in ["MuCString", "MuID"] or pt.endswith("*") or any(
pt.endswith(x) for x in ["Clause", "Node"]), pt
if pt.endswith("*"):
rt = pt[:-1]
else:
rt = pt
st = to_scala_type(rt)
nt = "Option[{}]".format(rt)
else:
rt = pt
st = to_scala_type(rt)
nt = st
node_params.append((nn, nt))
return '''case class {}({}) extends IRBuilderNode(id)'''.format(
node_name, ", ".join("{}: {}".format(nn, nt) for (nn, nt) in node_params))
def generate_things(ast):
irb = [s for s in ast["structs"] if s["name"] == "MuIRBuilder"][0]
nodes = []
for meth in irb["methods"]:
if meth["name"].startswith("new_"):
nodes.append(generate_node(meth))
return "\n".join(nodes)
def main():
with open(muapi_h_path) as f:
src_text = f.read()
ast = muapiparser.parse_muapi(src_text)
generated = generate_things(ast)
print(generated)
if __name__=='__main__':
main()
......@@ -35,7 +35,7 @@ def find_line(lines: List[str], substr: Predicate, start: int = 0) -> int:
if pred(lines[i], substr):
return i
raise KeyError("Not found: " + str(substr) + "\n text:" + str(lines) )
raise Exception("Not found: " + str(substr) + "\n text:" + str(lines) )
def extract_lines(parent: str, begin: Predicate, end: Predicate) -> str:
"""
......
"""
Usage: python3 irbuildernodestoirbuildermethods.py
"""
import re
import sys, os.path, io
import injecttools
from refimpl2injectablefiles import injectable_files, irbuilder_nodes_path
replaces = [(re.compile(x), y) for (x,y) in [
(r'TrantientBundle', 'MuBundleNode'),
(r'TB', 'MuBundleNode'),
(r'Bundle', 'MuBundleNode'),
(r'TopLevel', 'MuChildNode'),
(r'ChildNode', 'MuChildNode'),
(r'Type\w*', 'MuTypeNode'),
(r'Abstract\w+Type', 'MuTypeNode'),
(r'FuncSig', 'MuFuncSigNode'),
(r'Const\w+', 'MuConstNode'),
(r'GlobalCell', 'MuGlobalNode'),
(r'Function', 'MuFuncNode'),
(r'ExposedFunc', 'MuExpFuncNode'),
(r'FuncVer', 'MuFuncVerNode'),
(r'BasicBlock', 'MuBBNode'),
(r'BB', 'MuBBNode'),
(r'SSAVariable', 'MuVarNode'),
(r'Var', 'MuVarNode'),
(r'LocalVariable', 'MuLocalVarNode'),
(r'NorParam', 'MuNorParamNode'),
(r'ExcParam', 'MuExcParamNode'),
(r'InstResult', 'MuInstResNode'),
(r'Inst\w+', 'MuInstNode'),
(r'HasKeepaliveClause', 'MuInstNode'),
]]
case_class_r = re.compile(r'^(case\s+class\s+(\w+)\s*\(([^)]*)\))', re.MULTILINE)
arg = re.compile(r'(\w+):\s*([a-zA-Z0-9\[\].]+)')
begin="EXT:BEGIN:IRBUILDER_NODES"
end="EXT:END:IRBUILDER_NODES"
def main():
with open(irbuilder_nodes_path) as f:
lines = f.read()
text = injecttools.extract_lines(lines, begin, end)
output = io.StringIO()
for whole, name, arglist in case_class_r.findall(text):
node_name = name
func_name = "new" + name[4:]
argnames = []
argtypes = []
for an,at in arg.findall(arglist):
argnames.append(an)
argtypes.append(at)
print(" def {}({}): Unit = {{".format(func_name, ", ".join(
"{}: {}".format(an, at) for an, at in zip(argnames, argtypes))),
file=output)
print(" val _node = new {}({})".format(node_name,
", ".join(argnames)), file=output)
print(" onNewNodeCreated(_node)", file=output)
print(" }", file=output)
#print(output.getvalue())
#return
injectable_files["IRBuilder.scala"].inject_many({
"IRBUILDERNODE_CONSTRUCTORS": output.getvalue(),
})
if __name__=='__main__':
main()
"""
USAGE: python3 migrate_scripts/irbuildertomuctx.py
And then paste the result intoUse pbcopy on Mac.
IRBuilder is no longer part of MuCtx. Don't use this.
"""
import re
......
......@@ -429,4 +429,5 @@ def main():
"STUBS": generated,
})
main()
if __name__=='__main__':
main()
......@@ -300,4 +300,5 @@ def main():
"MuCtx": muctx_stubs,
})
main()
if __name__=='__main__':
main()
......@@ -8,7 +8,8 @@ def _make_injectable_file_set(m):
m2 = {os.path.join(_refimpl2_root, k): v for k,v in m.items()}
return InjectableFileSet(m2)
muapi_h_path = os.path.join(_my_dir, "../cbinding/muapi.h")
muapi_h_path = os.path.join(_refimpl2_root, "cbinding/muapi.h")
irbuilder_nodes_path = os.path.join(_refimpl2_root, "src/main/scala/uvm/ir/irbuilder/irBuilderNodes.scala")
injectable_files = injecttools.make_injectable_file_set(_refimpl2_root, [
("cStubs.scala", "src/main/scala/uvm/refimpl/nat/cStubs.scala",
......@@ -25,4 +26,6 @@ injectable_files = injecttools.make_injectable_file_set(_refimpl2_root, [
["IRBUILDER_IMPL"]),
("MuCtxIRBuilderPart.scala", "src/main/scala/uvm/refimpl/MuCtxIRBuilderPart.scala",
["METHODS"]),
("IRBuilder.scala", "src/main/scala/uvm/ir/irbuilder/IRBuilder.scala",
["IRBUILDERNODE_CONSTRUCTORS"]),
])
python3 irbuildertomuctx.py
python3 muapitocstubs.py
python3 muapitoirbuildercomminstsimpl.py
python3 muapitolibmupy.py
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment