irbuildernodestoirbuildermethods.py 2.23 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
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
"""
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()