irbuildertomuctx.py 3.85 KB
Newer Older
1
"""
Kunshan Wang's avatar
Kunshan Wang committed
2
IRBuilder is no longer part of MuCtx. Don't use this.
3 4 5
"""

import re
Kunshan Wang's avatar
Kunshan Wang committed
6 7 8 9
import sys, os.path, io

import injecttools
from refimpl2injectablefiles import injectable_files
10 11 12 13 14

begin = "SCRIPT: BEGIN HERE"
end = "SCRIPT: END HERE"

replaces = [(re.compile(x), y) for (x,y) in [
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    (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'),
38 39
    ]]

40 41
sig = re.compile(r'^(  def\s+(\w+)\s*\(([^)]*)\):\s+(\w+)\s+=)', re.MULTILINE)
arg = re.compile(r'(\w*):\s*([a-zA-Z0-9\[\].]+)')
42
node_like = re.compile(r'Mu\w+Node')
43
node_seq_like = re.compile(r'Seq\[(Mu\w+Node)\]')
44

Kunshan Wang's avatar
Kunshan Wang committed
45 46 47 48
_my_dir = os.path.dirname(__file__)
_refimpl2_root = os.path.join(_my_dir, "..")
irbuilder_path = os.path.join(_refimpl2_root, "src/main/scala/uvm/ir/irbuilder/IRBuilder.scala")

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
## No longer replace names, but just use MuIRNode[...]

#def rewrite_type(ty):
#    for p, t in replaces:
#        oldty = ty
#        ty = p.sub(t, ty)
#        if oldty != ty:
#            break
#    return ty

container_ty_r = re.compile(r'(Seq|Option)\[(.+)\]')

def rewrite_type(ty):
    m = container_ty_r.match(ty)
    if m is not None:
        t1, t2 = m.groups()
        return "{}[{}]".format(t1, rewrite_type(t2))
    for p, t in replaces:
        if p.match(ty) is not None:
            return "MuIRNode[{}]".format(ty)

    return ty

Kunshan Wang's avatar
Kunshan Wang committed
72 73 74
def main():
    with open(irbuilder_path) as f:
        lines = f.read()
75

Kunshan Wang's avatar
Kunshan Wang committed
76
    text = injecttools.extract_lines(lines, begin, end)
77

78 79
    #for p, t in replaces:
        #text = p.sub(t, text)
80

Kunshan Wang's avatar
Kunshan Wang committed
81
    output = io.StringIO()
82

83
    for whole, name, arglist, retty in sig.findall(text):
Kunshan Wang's avatar
Kunshan Wang committed
84
        argnames = []
85 86 87 88 89
        argtypes_muctx = []
        sanitisers = []

        retty = rewrite_type(retty)

Kunshan Wang's avatar
Kunshan Wang committed
90
        for an,at in arg.findall(arglist):
91 92
            rwt = rewrite_type(at)
            print(an, at, rwt)
Kunshan Wang's avatar
Kunshan Wang committed
93
            argnames.append(an)
94 95 96 97 98 99 100 101 102 103 104 105 106 107
            argtypes_muctx.append(rwt)
            m = node_seq_like.match(rwt)
            if m is not None:
                sanitisers.append('    for((n,i) <- {}.zipWithIndex) require(!n.isNull, "{}[%d] must not be NULL".format(i))'.format(an, an))
            elif node_like.match(rwt) is not None:
                sanitisers.append('    require(!{}.isNull, "{} must not be NULL")'.format(an, an))

        new_arglist = ", ".join("{}: {}".format(n,t) for n,t in zip(argnames, argtypes_muctx))

        print("  def {}({}): {} = {{".format(name, new_arglist, retty), file=output)
        for s in sanitisers:
            print(s, file=output)
        if name.startswith("new") or name in["getNode", "getInstRes"]:
            print('    nodeToHandle(irBuilder.{}({}))'.format(name, ", ".join(argnames)), file=output)
Kunshan Wang's avatar
Kunshan Wang committed
108 109 110 111 112 113 114 115
        else:
            print('    irBuilder.{}({})'.format(name, ", ".join(argnames)), file=output)
        print("  }", file=output)
        print(file=output)
        #print(whole, name, args)
        #for n,a in sig.findall(line):
            #args = arg_name.findall(a)
            #print("    addHandle(irBuilder.{}({}))".format(n, ", ".join(args)))
116

117 118 119 120
    #print(output.getvalue())

    #return

Kunshan Wang's avatar
Kunshan Wang committed
121 122 123 124 125 126
    injectable_files["MuCtxIRBuilderPart.scala"].inject_many({
        "METHODS": output.getvalue(),
        })
            
if __name__=='__main__':
    main()
127