irbuildertomuctx.py 3.2 KB
Newer Older
1
"""
Kunshan Wang's avatar
Kunshan Wang committed
2
USAGE: python3 migrate_scripts/irbuildertomuctx.py
3

Kunshan Wang's avatar
Kunshan Wang committed
4
And then paste the result intoUse pbcopy on Mac.
5 6 7
"""

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

import injecttools
from refimpl2injectablefiles import injectable_files
12 13 14 15 16 17

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

replaces = [(re.compile(x), y) for (x,y) in [
    (r'BN', 'MuBundleNode'),
Kunshan Wang's avatar
Kunshan Wang committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    (r'(CN|ChildNode)\[(_\s*<:\s*)?Identified\]', 'MuChildNode'),
    (r'(CN|ChildNode)\[(_\s*<:\s*)?IdentifiedSettable\]', 'MuChildNode'),
    (r'(CN|ChildNode)\[Type\w*\]', 'MuTypeNode'),
    (r'(CN|ChildNode)\[Abstract\w+Type\]', 'MuTypeNode'),
    (r'(CN|ChildNode)\[FuncSig\]', 'MuFuncSigNode'),
    (r'(CN|ChildNode)\[Const\w+\]', 'MuConstNode'),
    (r'(CN|ChildNode)\[GlobalCell\]', 'MuGlobalNode'),
    (r'(CN|ChildNode)\[Function\]', 'MuFuncNode'),
    (r'(CN|ChildNode)\[ExposedFunc\]', 'MuExpFuncNode'),
    (r'(CN|ChildNode)\[FuncVer\]', 'MuFuncVerNode'),
    (r'(CN|ChildNode)\[BasicBlock\]', 'MuBBNode'),
    (r'(CN|ChildNode)\[BB\]', 'MuBBNode'),
    (r'(CN|ChildNode)\[SSAVariable\]', 'MuVarNode'),
    (r'(CN|ChildNode)\[Var\]', 'MuVarNode'),
    (r'(CN|ChildNode)\[LocalVariable\]', 'MuLocalVarNode'),
    (r'(CN|ChildNode)\[NorParam\]', 'MuNorParamNode'),
    (r'(CN|ChildNode)\[ExcParam\]', 'MuExcParamNode'),
    (r'(CN|ChildNode)\[InstResult\]', 'MuInstResNode'),
    (r'(CN|ChildNode)\[Inst\w+\]', 'MuInstNode'),
37
    (r'(CN|ChildNode)\[HasKeepaliveClause\]', 'MuInstNode'),
38 39
    ]]

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

Kunshan Wang's avatar
Kunshan Wang committed
45 46 47 48 49 50 51
_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")

def main():
    with open(irbuilder_path) as f:
        lines = f.read()
52

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

Kunshan Wang's avatar
Kunshan Wang committed
55 56
    for p, t in replaces:
        text = p.sub(t, text)
57

Kunshan Wang's avatar
Kunshan Wang committed
58
    output = io.StringIO()
59

Kunshan Wang's avatar
Kunshan Wang committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    for whole, name, arglist in sig.findall(text):
        print(whole, "{", file=output)
        argnames = []
        for an,at in arg.findall(arglist):
            argnames.append(an)
            #print(an, at)
            if node_seq_like.match(at) is not None:
                print('    for((n,i) <- {}.zipWithIndex) require(!n.isNull, "{}[%d] must not be NULL".format(i))'.format(an, an), file=output)
            elif node_like.match(at) is not None:
                print('    require(!{}.isNull, "{} must not be NULL")'.format(an, an), file=output)
        if name.startswith("new"):
            print('    addHandle(irBuilder.{}({}))'.format(name, ", ".join(argnames)), file=output)
        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)))
80

Kunshan Wang's avatar
Kunshan Wang committed
81 82 83 84 85 86
    injectable_files["MuCtxIRBuilderPart.scala"].inject_many({
        "METHODS": output.getvalue(),
        })
            
if __name__=='__main__':
    main()
87