Example #1
0
    def __call__(self, ast, env):

        for ppass in self.passes:
            ast, env = ppass(ast, env)
            if errors.occurred():
                errors.reset()
                raise CompileError, ppass.__name__
        return ast, env
Example #2
0
    def __call__(self, ast, env):

        for ppass in self.passes:
            ast, env = ppass(ast, env)
            if errors.occurred():
                errors.reset()
                raise CompileError, ppass.__name__
        return ast, env
Example #3
0
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()
Example #4
0
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()