def ddump_optimizer(source): import parser import cfg import typecheck import codegen with errors.listen(): parse = parser.make_parser() ast = parse(source) typecheck.typecheck(ast) functions = cfg.ssa_pass(ast) cgen = codegen.LLVMEmitter() blockgen = codegen.BlockEmitter(cgen) for name, retty, argtys, start_block in functions: function = blockgen.generate_function( name, retty, argtys, start_block ) optimizer = codegen.LLVMOptimizer(cgen.module) print 'Optimizer Diff'.center(80, '=') optimizer.diff(function, cgen.module)
def ddump_lex(source): import sys import errors lexer = make_lexer() lexer.input(source) with errors.listen(): for tok in iter(lexer.token,None): sys.stdout.write("%s\n" % tok)
def ddump_parse(source): import sys import lexer import errors from astutils import dump lexer = lexer.make_lexer() parser = yacc.yacc() with errors.listen(): program = parser.parse(source) sys.stdout.write(dump(program)+'\n')
def ddump_parse(source): import sys import lexer import errors from astutils import dump lexer = lexer.make_lexer() parser = yacc.yacc() with errors.listen(): program = parser.parse(source) sys.stdout.write(dump(program) + '\n')
def ddump_tc(source, verbose=False): import sys import lexer import parser import errors import pprint with errors.listen(): parse = parser.make_parser() ast = parse(source) symtab = typecheck(ast, verbose) if errors.occurred(): sys.stdout.write("Not well-typed!\n") else: print 'Locals'.center(80, '=') sys.stdout.write(pprint.pformat(symtab._locals.items()) + '\n') print 'Globals'.center(80, '=') sys.stdout.write(pprint.pformat(symtab._globals) + '\n') sys.stdout.write("Well-typed!\n")
def ddump_blocks(source): import errors import lexer import parser import typecheck parse = parser.make_parser() with errors.listen(): program = parse(source) typecheck.typecheck(program) if not errors.reported(): functions = ssa_pass(program) for funcname, retty, argtys, start_block in functions: fname = (" %s %s %s " % (funcname, retty, argtys)) print fname.center(80, '-') print_block(start_block) print("") else: raise AssertionError
def ddump_optimizer(source): import parser import cfg import typecheck import codegen with errors.listen(): parse = parser.make_parser() ast = parse(source) typecheck.typecheck(ast) functions = cfg.ssa_pass(ast) cgen = codegen.LLVMEmitter() blockgen = codegen.BlockEmitter(cgen) for name, retty, argtys, start_block in functions: function = blockgen.generate_function(name, retty, argtys, start_block) optimizer = codegen.LLVMOptimizer(cgen.module) print 'Optimizer Diff'.center(80, '=') optimizer.diff(function, cgen.module)
def main(): import argparse argp = argparse.ArgumentParser('blirc') argp.add_argument('file', metavar="file", nargs='?', help='Source file') argp.add_argument('-O', metavar="opt", nargs='?', type=int, help='Optimization level', default=2) argp.add_argument('--ddump-parse', action='store_true', help='Dump parse tree') argp.add_argument('--ddump-lex', action='store_true', help='Dump token stream') argp.add_argument('--ddump-blocks', action='store_true', help='Dump the block structure') argp.add_argument('--ddump-tc', action='store_true', help='Dump the type checker state') argp.add_argument('--ddump-optimizer', action='store_true', help='Dump diff of the LLVM optimizer pass') argp.add_argument('--noprelude', action='store_true', help='Don\'t link against the prelude') argp.add_argument('--nooptimize', action='store_true', help='Don\'t run LLVM optimization pass') argp.add_argument('--emit-llvm', action='store_true', help=' Generate output files in LLVM formats ') argp.add_argument('--emit-x86', action='store_true', help=' Generate output files in x86 assembly ') argp.add_argument('--run', action='store_true', help='Execute generated code ') args = argp.parse_args() if args.file: source = open(args.file).read() else: sys.stderr.write('No input\n') sys.exit(1) if args.ddump_lex: lexer.ddump_lex(source) if args.ddump_parse: parser.ddump_parse(source) if args.ddump_blocks: cfg.ddump_blocks(source) if args.ddump_optimizer: codegen.ddump_optimizer(source) if args.ddump_tc: typecheck.ddump_tc(source) try: # ===================================== start = time.time() with errors.listen(): opts = vars(args) ast, env = compile(source, **opts) timing = time.time() - start # ===================================== if args.emit_llvm: print env['lmodule'] elif args.emit_x86: print env['lmodule'].to_native_assembly() elif args.run: ctx = exc.Context(env) exc.execute(ctx, fname='main') else: print 'Compile time %.3fs' % timing except CompileError as e: sys.stderr.write('FAIL: Failure in compiler phase: %s\n' % e.args[0]) sys.exit(1) errors.reset()
def main(): import argparse argp = argparse.ArgumentParser("blirc") argp.add_argument("file", metavar="file", nargs="?", help="Source file") argp.add_argument("-O", metavar="opt", nargs="?", type=int, help="Optimization level", default=2) argp.add_argument("--ddump-parse", action="store_true", help="Dump parse tree") argp.add_argument("--ddump-lex", action="store_true", help="Dump token stream") argp.add_argument("--ddump-blocks", action="store_true", help="Dump the block structure") argp.add_argument("--ddump-tc", action="store_true", help="Dump the type checker state") argp.add_argument("--ddump-optimizer", action="store_true", help="Dump diff of the LLVM optimizer pass") argp.add_argument("--noprelude", action="store_true", help="Don't link against the prelude") argp.add_argument("--nooptimize", action="store_true", help="Don't run LLVM optimization pass") argp.add_argument("--emit-llvm", action="store_true", help=" Generate output files in LLVM formats ") argp.add_argument("--emit-x86", action="store_true", help=" Generate output files in x86 assembly ") argp.add_argument("--run", action="store_true", help="Execute generated code ") args = argp.parse_args() if args.file: source = open(args.file).read() else: sys.stderr.write("No input\n") sys.exit(1) if args.ddump_lex: lexer.ddump_lex(source) if args.ddump_parse: parser.ddump_parse(source) if args.ddump_blocks: cfg.ddump_blocks(source) if args.ddump_optimizer: codegen.ddump_optimizer(source) if args.ddump_tc: typecheck.ddump_tc(source) try: # ===================================== start = time.time() with errors.listen(): opts = vars(args) ast, env = compile(source, **opts) timing = time.time() - start # ===================================== if args.emit_llvm: print env["lmodule"] elif args.emit_x86: print env["lmodule"].to_native_assembly() elif args.run: ctx = exc.Context(env) exc.execute(ctx, fname="main") else: print "Compile time %.3fs" % timing except CompileError as e: sys.stderr.write("FAIL: Failure in compiler phase: %s\n" % e.args[0]) sys.exit(1) errors.reset()