Example #1
0
def main():
    target = sys.argv[1]
    with open(target) as f:
        source = f.read()
    tree = ast.parse(source, target)

    ctx = Context()
    with open("libpyc.c") as f:
        ctx.body_write(f.read() + "\n")

    for builtin, fn in BUILTINS.items():
        ctx.register_global(builtin, fn)

    generate(ctx, tree)

    # Create and move to working directory
    outdir = "bin"
    shutil.rmtree(outdir, ignore_errors=True)
    os.mkdir(outdir)
    os.chdir(outdir)

    with open("main.c", "w") as f:
        f.write(ctx.body.content)

        main = ctx.namings.get("main")["name"]
        f.write(f"""int main(int argc, char *argv[]) {{
  Py_Initialize();

  // Initialize globals, if any.
{ctx.initializations.content}
  PyObject* r = {main}();
  return PyLong_AsLong(r);
}}""")

    subprocess.run(["clang-format", "-i", "main.c"])

    cflags_raw = subprocess.check_output(["python3-config", "--cflags"])
    cflags = [f.strip() for f in cflags_raw.decode().split(" ") if f.strip()]
    cmd = ["gcc", "-c", "-o", "main.o"] + cflags + ["main.c"]
    subprocess.run(cmd)

    ldflags_raw = subprocess.check_output(["python3-config", "--ldflags"])
    ldflags = [f.strip() for f in ldflags_raw.decode().split(" ") if f.strip()]
    cmd = ["gcc"] + ldflags + ["main.o"] + ["-lpython3.8"]
    subprocess.run(cmd)
Example #2
0
def assemble_file(paths, offset, size, entry_label):
    nodes = parser.parse(paths)
    buf, entry_point, code_size = codegen.generate(nodes, offset, entry_label)

    rom, rom_size = create_rom_buffer(buf, code_size, size, entry_point)

    with open("a.out", 'wb') as f:
        for b in rom:
            f.write(b)
Example #3
0
def assemble_vhdl(paths, vhdl_path, offset, size, entry_label):
    nodes = parser.parse(paths)
    buf, entry_point, code_size = codegen.generate(nodes, offset, entry_label)

    rom, rom_size = create_rom_buffer(buf, code_size, size, entry_point)

    with open("a.out", 'wb') as f:
        for b in rom:
            f.write(b)

    vhdl_template = None
    with open(vhdl_path, 'r') as vhdl_file:
        vhdl_template = vhdl_file.read()

    required_bits = int(math.log(rom_size, 2))

    vhdl_template = vhdl_template.replace("<MAX_INDEX>", str(rom_size - 1))
    vhdl_template = vhdl_template.replace("<MAX_BIT>", str(required_bits - 1))

    sp = vhdl_template.split("<BUFFER>")

    print(sp[0])

    print("(")
    with open("a.out", "rb") as f:
        byte = f.read(1)
        first = True
        while byte != "":
            if first:
                print(' x"' + "%0.2x" % ord(byte) + '"')
            else:
                print(',x"' + "%0.2x" % ord(byte) + '"')

            byte = f.read(1)
            first = False
    print(");")

    print(sp[1])
Example #4
0
 def generate(self):
     generate(self.get_state())
Example #5
0
import parser
import codegen
import sys

if len(sys.argv) == 1:
    sys.stderr.write("No input files given.\n")
    sys.exit(1)
else:
    if sys.argv[1] == "--read-ast":
        sys.argv = sys.argv[1:]
        for fname in sys.argv[1:]:
            codegen.generate(parser.read(fname))
    else:
        for fname in sys.argv[1:]:
            codegen.generate(parser.parse(fname))
Example #6
0
def main():
    cmdline = argparse.ArgumentParser(
        description='auto-generate chpl benchmarks')
    cmdline.add_argument('i',
                         metavar='n',
                         type=int,
                         help='number of elements in the Vector chpl')
    cmdline.add_argument('datatype',
                         metavar='t',
                         help='data type for the Vector chpl')
    cmdline.add_argument('expr',
                         metavar='expr',
                         choices=['copy', 'sum'],
                         help='benchmark expression to generate')
    # cmdline.add_argument('babel', metavar='babel',
    #                    help='the Babel executable')
    args = cmdline.parse_args()
    i = args.i
    datatype = args.datatype
    babel = 'babel'  #args.babel
    expr = args.expr
    if expr == 'copy':
        sidl_code = sidl_code_copy
        benchmark_expr = copy_expr
    elif expr == 'sum':
        benchmark_expr = sum_expr
        sidl_code = sidl_code_sum
    else:
        raise Exception()

    print "-------------------------------------------------------------"
    print "generating servers"
    print "-------------------------------------------------------------"
    subprocess.check_call("mkdir -p out", shell=True)
    f = open('out/chpl_%d_%s_%s.sidl' % (i, datatype, expr), "w")
    f.write(codegen.generate("SIDL", sidl_code(i, datatype)))
    f.close()
    languages = ["C", "CXX", "F77", "F90", "F03", "Java", "Python"]
    for lang in languages:
        ext = {
            "C": "c",
            "CXX": "cxx",
            "F77": "f",
            "F90": "F90",
            "F03": "F03",
            "Java": "java",
            "Python": "py"
        }
        prefix = {
            "C": "s_",
            "CXX": "s_",
            "F77": "s_",
            "F90": "s_",
            "F03": "s_",
            "Java": "s/",
            "Python": "s/"
        }

        print "generating", lang, i, datatype, expr, "..."
        cmd = """
          mkdir -p out/{lang}_{i}_{t}_{e} && cd out/{lang}_{i}_{t}_{e} &&
          {babel} -s{lang} --makefile ../chpl_{i}_{t}_{e}.sidl
          """.format(lang=lang, i=i, babel=babel, t=datatype, e=expr)
        #print cmd
        subprocess.check_call(cmd, shell=True)
        impl = ("out/{lang}_{i}_{t}_{e}/{prefix}Benchmark_Impl.{ext}".format(
            lang=lang,
            i=i,
            t=datatype,
            e=expr,
            ext=ext[lang],
            prefix=prefix[lang]))
        if lang == "Python":
            splicer_block = "run"
        else:
            splicer_block = "s.Benchmark.run"
        code = codegen.generate(lang, benchmark_expr(i, datatype))

        if code == None:
            raise Exception('Code generation failed')
        print "splicing", impl
        splicer.replace(impl, splicer_block, code)

    print "-------------------------------------------------------------"
    print "generating client", i, datatype, expr, "..."
    print "-------------------------------------------------------------"
    cmd = """
      mkdir -p out/client_{i}_{t}_{e} && cd out/client_{i}_{t}_{e} &&
      {babel} -cChapel --makefile ../chpl_{i}_{t}_{e}.sidl
      """.format(i=i, babel='braid', t=datatype, e=expr)
    #print cmd
    subprocess.check_call(cmd, shell=True)
    f = open('out/client_%d_%s_%s/main.chpl' % (i, datatype, expr), "w")
    f.write(gen_main_chpl(i, datatype, expr))
    f.close

    print "-------------------------------------------------------------"
    print "generating benchmark script..."
    print "-------------------------------------------------------------"

    def numruns(t):
        if t == 'string':
            return str(100001)
        return str(10000001)

    f = open('out/client_%d_%s_%s/runAll.sh' % (i, datatype, expr), 'w')
    f.write(r"""#!/usr/bin/bash
PYTHONPATH_1="`/usr/bin/python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(prefix='/home/prantl1/work/babel/install',plat_specific=1) + ':' + get_python_lib(prefix='/home/prantl1/work/babel/install')"`:$PYTHONPATH"
LIBDIR=`babel-config --query-var=libdir`
PYTHON_VERSION=`babel-config --query-var=PYTHON_VERSION`
SIDL_VERSION=`babel-config --query-var=VERSION`
SIDL_DLL_PATH_1="$LIBDIR/libsidlstub_java.scl;$LIBDIR/libsidl.scl;$LIBDIR/libsidlx.scl"
export LD_LIBRARY_PATH="$LIBDIR:$LD_LIBRARY_PATH"

echo "runAll($i)"

function count_insns {
   # measure the number of instructions of $1, save output to $2.all
   # perform one run with only one iteration and subtract that from the result
   # to eliminate startup time
   perf stat -- $1 --num_runs=1 2>$2.base || (echo "FAIL" >$2; exit 1)
   base=`grep instructions $2.base | awk '{print $1}'`
   perf stat -- $1 --num_runs=""" + numruns(datatype) +
            r""" 2>$2.perf || (echo "FAIL" >$2; exit 1)
   grep instructions $2.perf | awk "{print \$1-$base}" >> $2.all
}

function medtime {
   # measure the median running user time
   rm -f $2.all
   MAX=10
   for I in `seq $MAX`; do
     echo "measuring $1 ($3@$4,$5) [$I/$MAX]"
     # echo SIDL_DLL_PATH=$SIDL_DLL_PATH
     # echo PYTHONPATH=$PYTHONPATH
     # /usr/bin/time -f %U -a -o $2.all $1 || (echo "FAIL" >$2; exit 1)
     count_insns $1 $2
   done
   cat $2.all \
       | sort \
       | python -c 'import numpy,sys; \
           print numpy.median( \
             map(lambda x: float(x), sys.stdin.readlines()))' \
       >>$2
}
""")
    for lang in languages:
        f.write('''
rm -f out{lang}
export SIDL_DLL_PATH="../{lang}_{i}_{t}_{e}/libimpl.scl;$SIDL_DLL_PATH_1"
export PYTHONPATH="../Python_{i}_{t}_{e}:$PYTHONPATH_1"
export CLASSPATH="../Java_{i}_{t}_{e}:$LIBDIR/sidl-$SIDL_VERSION.jar:$LIBDIR/sidlstub_$SIDL_VERSION.jar"
medtime ./runChapel2{lang} out{lang} {i} {t} {e}
'''.format(lang=lang, i=i, t=datatype, e=expr))

    f.write("echo %d " % i +
            ' '.join(['`cat out%s`' % lang
                      for lang in languages]) + ' >times\n')
    f.close()
Example #7
0
import sys
import os
import parser
import analyzer
import codegen

input = sys.argv[1]
infile = open(input, 'r')
source_code = infile.read()
infile.close()
ast = parser.parse(source_code)
analyzer.analyze(ast)
print "Analyzed all lines successfully"

code = codegen.generate(ast)
output = sys.argv[1].split('.')[0]
outfile = open(output + ".asm", 'w')
outfile.write(code)
outfile.close()
result = os.system("nasm -f elf -g -F stabs " + output + ".asm")
if result:
    print "Error during assembly, is NASM installed and added to $PATH?"
    sys.exit(1)
linked_library = ""
for function in codegen.builtin_functions:
    result = os.system("nasm -f elf -g -F stabs " + function + ".asm")
    if result:
        print "Error during assembly, is NASM installed and added to $PATH?"
        sys.exit(1)
    linked_library += function + ".o "
result = os.system("ld -m elf_i386 " + output + ".o " + linked_library +
Example #8
0
def compile(path, templates):
	env = jinja2.Environment(loader=jinja2.FileSystemLoader(path))
	templates = templates if templates else env.loader.list_templates()
	print codegen.generate(env, templates)
Example #9
0
def main():
    cmdline = argparse.ArgumentParser(description='auto-generate struct benchmarks')
    cmdline.add_argument('i', metavar='n', type=int,
			 help='number of elements in the Vector struct')
    cmdline.add_argument('datatype', metavar='t', 
			 help='data type for the Vector struct')
    cmdline.add_argument('expr', metavar='expr', choices=['reverse', 'nop', 'bsort'],
			 help='benchmark expression to generate')
    # cmdline.add_argument('babel', metavar='babel',
    #			 help='the Babel executable')
    args = cmdline.parse_args()
    i = args.i
    datatype = args.datatype
    expr = args.expr
    if expr == 'reverse':
        benchmark_expr = reverse_expr
    elif expr == 'nop':
        benchmark_expr = nop_expr
    elif expr == 'bsort':
        benchmark_expr = bsort_expr
    else: raise

    print "-------------------------------------------------------------"
    print "generating servers"
    print "-------------------------------------------------------------"
    subprocess.check_call("mkdir -p out", shell=True)
    f = open('out/struct_%d_%s_%s.sidl'%(i,datatype,expr), "w")
    f.write(codegen.generate("SIDL", sidl_code(i, datatype)))
    f.close()
    languages = ["C", "CXX", "F77", "F90", "F03", "Java", "Python", "Chapel" ]
    for lang in reversed(languages):
        ext = {"C"      : "c", 
               "CXX"    : "cxx",
               "F77"    : "f",
               "F90"    : "F90",
               "F03"    : "F03",
               "Java"   : "java",
               "Python" : "py",
               "Chapel" : "chpl"}
        prefix = {"C"   : "s_", 
               "CXX"    : "s_",
               "F77"    : "s_",
               "F90"    : "s_",
               "F03"    : "s_",
               "Java"   : "s/",
               "Python" : "s/",
               "Chapel" : "s"}
        babel={"C"      : "babel", 
               "CXX"    : "babel",
               "F77"    : "babel",
               "F90"    : "babel",
               "F03"    : "babel",
               "Java"   : "babel",
               "Python" : "babel",
               "Chapel" : "braid"}


        print "generating", lang, i, datatype, expr, "..."

        cmd = """
          mkdir -p out/{lang}_{i}_{t}_{e} && cd out/{lang}_{i}_{t}_{e} &&
          {babel} -s{lang} --makefile ../struct_{i}_{t}_{e}.sidl
          """.format(lang=lang,i=i,babel=babel[lang],t=datatype,e=expr)
        #print cmd
        subprocess.check_call(cmd, shell=True)
        impl = ("out/{lang}_{i}_{t}_{e}/{prefix}{name}.{ext}".
                format(lang=lang, i=i, t=datatype, e=expr,
                       ext=ext[lang], prefix=prefix[lang], 
                       name='_Impl' if lang == 'Chapel' else 'Benchmark_Impl'))
        if lang == "Python":
            splicer_block = "run"
        else: splicer_block = "s.Benchmark.run"
        code = codegen.generate(lang, benchmark_expr(i,datatype))
        if code == None:
            raise Exception('Code generation failed')
        #print "splicing", impl
        #print code
        splicer.replace(impl, splicer_block, code) 

    print "-------------------------------------------------------------"
    print "generating client", i, datatype, expr, "..."
    print "-------------------------------------------------------------"
    cmd = """
      mkdir -p out/client_{i}_{t}_{e} && cd out/client_{i}_{t}_{e} &&
      {babel} -cC --makefile ../struct_{i}_{t}_{e}.sidl
      """.format(i=i,babel=babel["C"],t=datatype,e=expr)
    #print cmd
    subprocess.check_call(cmd, shell=True)
    f = open('out/client_%d_%s_%s/main.c'%(i,datatype,expr), "w")
    f.write(gen_main_c(i,datatype))
    f.close


    print "-------------------------------------------------------------"
    print "adapting client Makefile..."
    print "-------------------------------------------------------------"
    filename = 'out/client_%d_%s_%s/GNUmakefile'%(i,datatype,expr)
    os.rename(filename, filename+'~')
    dest = open(filename, 'w')
    src = open(filename+'~', 'r')

    for line in src:
        m = re.match(r'^(all *:.*)$', line)
        if m:
            dest.write(m.group(1)+
                       ' runC2C runC2CXX runC2F77 runC2F90 runC2F03 '
                       +'runC2Java runC2Python runC2Chapel\n')
            dest.write("CXX=`babel-config --query-var=CXX`\n"+
                       '\n'.join([
"""
runC2{lang}: lib$(LIBNAME).la ../{lang}_{i}_{t}_{e}/libimpl.la main.lo
\tbabel-libtool --mode=link $(CC) -static main.lo lib$(LIBNAME).la \
\t    ../{lang}_{i}_{t}_{e}/libimpl.la -o runC2{lang}
""".format(lang=lang, i=i, t=datatype, e=expr) for lang in languages[:6]+languages[7:]]))
            dest.write("""
runC2Python: lib$(LIBNAME).la ../Python_{i}_{t}_{e}/libimpl1.la main.lo
\tbabel-libtool --mode=link $(CC) -static main.lo lib$(LIBNAME).la \
\t    ../Python_{i}_{t}_{e}/libimpl1.la -o runC2Python
""".format(i=i,t=datatype,e=expr))
        else:
            dest.write(line)
    dest.close()
    src.close()

    print "-------------------------------------------------------------"
    print "generating benchmark script..."
    print "-------------------------------------------------------------"
    def numruns(t):
        if t == 'string' or expr == 'bsort': 
            return str(100001)
        return     str(1000001)

    for lang in languages:
        f = open('out/client_%d_%s_%s/runC2%s.sh'%(i,datatype,expr, lang), 'w')
        f.write(r"""#!/bin/sh
LIBDIR=`babel-config --query-var=libdir`
PYTHON_VERSION=`babel-config --query-var=PYTHON_VERSION`
PYTHONPATH_1=$LIBDIR/python$PYTHON_VERSION/site-packages:$PYTHONPATH
SIDL_VERSION=`babel-config --query-var=VERSION`
SIDL_DLL_PATH_1="$LIBDIR/libsidlstub_java.scl;$LIBDIR/libsidl.scl;$LIBDIR/libsidlx.scl"
export LD_LIBRARY_PATH="$LIBDIR:$LD_LIBRARY_PATH"

# echo "runC2{lang}({i})"

count_insns () {
   # measure the number of instructions of $1, save output to $2.all
   # perform one run with only one iteration and subtract that from the result 
   # to eliminate startup time
   perf stat -- $1 1 2>$2.base || (echo "FAIL1" >$2; echo FAIL1; exit 1)
   base=`grep instructions $2.base | awk '{print $1}'`
   perf stat -- $1 """+numruns(datatype)+r""" 2>$2.perf || (echo "FAILN" >$2; echo FAILN; exit 1)
   grep instructions $2.perf | awk "{print \$1-$base}" >> $2.all
}

medtime () {
   # measure the median running user time
   rm -f $2.all
   MAX=10 #### 10 see also 1:9 below!!!!!
   for I in `seq $MAX`; do
     echo "measuring $1 ($3@$4,$5) [$I/$MAX]"
     count_insns $1 $2
   done
   cat $2.all \
       | sort \
       | python -c 'import numpy,sys; \
           print numpy.mean( \
             sorted(map(lambda x: float(x), sys.stdin.readlines()))[1:9])' \
       >>$2
}
""")
        f.write('''
rm -f out{lang}
export SIDL_DLL_PATH="../{lang}_{i}_{t}_{e}/libimpl.scl;$SIDL_DLL_PATH_1"
export PYTHONPATH="../Python_{i}_{t}_{e}:$PYTHONPATH_1"
export CLASSPATH="../Java_{i}_{t}_{e}:$LIBDIR/sidl-$SIDL_VERSION.jar:$LIBDIR:$LIBDIR/sidlstub_$SIDL_VERSION.jar"
export BABEL_JVM_FLAGS=-XX:CompileThreshold=1
medtime ./runC2{lang} out{lang} {i} {t} {e}
'''.format(lang=lang,i=i,t=datatype,e=expr))
        f.close()
    # end for langs

    f = open('out/client_%d_%s_%s/combine.sh'%(i,datatype,expr), 'w')
    f.write("#!/bin/sh\n") 
    f.write("echo %d "%i+' '.join(['`cat out%s`'%lang 
                                   for lang in languages])+' >times\n')
    f.close()
Example #10
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from codegen import generate
import simplejson
import sys

if __name__ == '__main__':
    filename = 'definitions.json'
    try:
        filename = sys.argv[2]
    except IndexError:
        pass
    with open(filename) as f:
        classes = simplejson.load(f)
    generate(classes)
Example #11
0
     print "Syntax tree"
     for decl in ast.decls:
         print decl
     print
 if opts.debug: print "-TACIFY-"
 tac = tacifier.tacify(ast)
 if opts.debug:
     print "TAC functions:"
     pprint.pprint(tac.functions)
     print
 assert tac.in_func is None, tac.in_func
 assert len(tac.scopes) == 1
 if opts.debug: print "-ALLOC/RTL-"
 allocations = allocator.alloc(tac, opts.warnings, debug=opts.debug)
 if opts.debug: print "-CODE/GEN-"
 gen = codegen.generate(allocations, opts.gen_opts, opts.warnings)
 if opts.debug:
     print "Generated line counts:"
     for name, g in gen.items():
         print name
         g.print_stats()
 bss, data, text = codegen.combine(gen)
 outs = []
 if opts.debug:
     print "==ASSEMBLY OUTPUT BEGINS HERE=="
     outs.append(sys.stdout)
 if not opts.dry_run:
     if opts.output == '-':
         dest = sys.stdout
     else:
         dest = open(opts.output, 'w')
Example #12
0
def main(args):
  
  try:

    # Setup the configuration variables and definitions
    config.init()

    # Setup parser, parse arguments and initialise globals
    argp = setup_argparse()
    a = argp.parse_args(args)
    setup_globals(a)

    # Create a (valid) target system device oject (before anything else)
    device = set_device(target_system, num_cores)
    
    # Read the input from stdin or from a file 
    vhdr(v, 'Front end')
    input_file = util.read_file(infile) if infile else sys.stdin.read()

    # Setup the error object
    errorlog = ErrorLog()
   
    if build_only:
      # Generate code
      vhdr(v, 'Generating code for {}'.format(device))
      generate(ast, sig, child, device, outfile, 
          False, True, compile_only, display_memory, show_calls, v)

    # Parse the input file and produce an AST
    ast = produce_ast(input_file, errorlog)

    # Perform semantic analysis on the AST
    sym = SymbolTable(errorlog)
    sig = SignatureTable(errorlog)
    sem = semantic_analysis(sym, sig, ast, device, errorlog)
    
    # Perform AST transformations for channels and reps
    if not disable_transformations:
      transform_ast(sem, sym, sig, ast, errorlog, device, v)

    # Perform child analysis
    child = child_analysis(sig, ast)

    # Generate code
    vhdr(v, 'Generating code for {}'.format(device))
    generate(ast, sig, child, device, outfile, 
        translate_only, compile_only, 
        display_memory, show_calls, save_temps, v)

  # Handle (expected) system exits
  except SystemExit:
    return 0

  # Handle any specific compilation errors quietly
  except QuietError as e:
    return 1
  
  # Handle any specific compilation errors
  except Error as e:
    #sys.stderr.write('Error: {}\n'.format(e))
    return 1
  
  # Parser attribute error
  #except AttributeError:
  #  sys.stderr.write('Attribute error')
  #  return 1

  # Handle a keyboard interrupt (ctrl+c)
  except KeyboardInterrupt:
    sys.stderr.write('Interrupted')
    return 1
  
  # Anything else we weren't expecting
  except:
    sys.stderr.write("Unexpected error: {}\n".format(sys.exc_info()[0]))
    raise
    return 1
  
  return 0
Example #13
0
 allocations = allocator.alloc(parse_tree, tac)
 if opts.debug:
     print "RTL functions:"
     for name, rtl in allocations.items():
         print rtl.sc, name
         print rtl.stack
         pprint.pprint(rtl.code)
         print
     print "Structs:"
     for tag, struc in allocations[None].structs.items():
         print "  struct", tag
         for off, typ, memb, size in struc.members:
             print "    <+%02x> %r %s (%x)"%(off, typ, memb, size)
     print
 if opts.debug: print "-CODE/GEN-"
 gen = codegen.generate(allocations)
 if opts.debug:
     print "Generated line counts:"
     for name, g in gen.items():
         print name
         g.print_stats()
 bss, data, text = codegen.combine(gen)
 outs = []
 if opts.debug:
     print "==ASSEMBLY OUTPUT BEGINS HERE=="
     outs.append(sys.stdout)
 if not opts.dry_run:
     dest = open(opts.output, 'w')
     outs.append(dest)
 def pr(line):
     for out in outs: