1#!/usr/bin/env python
2
3class TimingScriptGenerator:
4    """Used to generate a bash script which will invoke the toy and time it"""
5    def __init__(self, scriptname, outputname):
6        self.shfile = open(scriptname, 'w')
7        self.timeFile = outputname
8        self.shfile.write("echo \"\" > %s\n" % self.timeFile)
9
10    def writeTimingCall(self, irname, callname):
11        """Echo some comments and invoke both versions of toy"""
12        rootname = irname
13        if '.' in irname:
14            rootname = irname[:irname.rfind('.')]
15        self.shfile.write("echo \"%s: Calls %s\" >> %s\n" % (callname, irname, self.timeFile))
16        self.shfile.write("echo \"\" >> %s\n" % self.timeFile)
17        self.shfile.write("echo \"With MCJIT\" >> %s\n" % self.timeFile)
18        self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"")
19        self.shfile.write(" -o %s -a " % self.timeFile)
20        self.shfile.write("./toy-mcjit -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname))
21        self.shfile.write("echo \"\" >> %s\n" % self.timeFile)
22        self.shfile.write("echo \"With MCJIT again\" >> %s\n" % self.timeFile)
23        self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"")
24        self.shfile.write(" -o %s -a " % self.timeFile)
25        self.shfile.write("./toy-mcjit -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname))
26        self.shfile.write("echo \"\" >> %s\n" % self.timeFile)
27        self.shfile.write("echo \"With JIT\" >> %s\n" % self.timeFile)
28        self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"")
29        self.shfile.write(" -o %s -a " % self.timeFile)
30        self.shfile.write("./toy-jit -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname))
31        self.shfile.write("echo \"\" >> %s\n" % self.timeFile)
32        self.shfile.write("echo \"\" >> %s\n" % self.timeFile)
33
34class LibScriptGenerator:
35    """Used to generate a bash script which will convert Kaleidoscope files to IR"""
36    def __init__(self, filename):
37        self.shfile = open(filename, 'w')
38
39    def writeLibGenCall(self, libname, irname):
40        self.shfile.write("./toy-ir-gen < %s 2> %s\n" % (libname, irname))
41
42def splitScript(inputname, libGenScript, timingScript):
43  rootname = inputname[:-2]
44  libname = rootname + "-lib.k"
45  irname = rootname + "-lib.ir"
46  callname = rootname + "-call.k"
47  infile = open(inputname, "r")
48  libfile = open(libname, "w")
49  callfile = open(callname, "w")
50  print "Splitting %s into %s and %s" % (inputname, callname, libname)
51  for line in infile:
52    if not line.startswith("#"):
53      if line.startswith("print"):
54        callfile.write(line)
55      else:
56        libfile.write(line)
57  libGenScript.writeLibGenCall(libname, irname)
58  timingScript.writeTimingCall(irname, callname)
59
60# Execution begins here
61libGenScript = LibScriptGenerator("make-libs.sh")
62timingScript = TimingScriptGenerator("time-lib.sh", "lib-timing.txt")
63
64script_list = ["test-5000-3-50-50.k", "test-5000-10-100-10.k", "test-5000-10-5-10.k", "test-5000-10-1-0.k",
65               "test-1000-3-10-50.k", "test-1000-10-100-10.k", "test-1000-10-5-10.k", "test-1000-10-1-0.k",
66               "test-200-3-2-50.k", "test-200-10-40-10.k", "test-200-10-2-10.k", "test-200-10-1-0.k"]
67
68for script in script_list:
69  splitScript(script, libGenScript, timingScript)
70print "All done!"
71