예제 #1
0
def main():
    if len(sys.argv) == 1:
        print 'Usage: %s [file name]' % sys.argv[0]
        return

    fname = sys.argv[1]
    if fname.split('.')[0] + '.s' == fname:
        print 'cannot output to the same file'
        return

    with open(fname.split('.')[0] + '.s', 'w') as f:
        f.write('# %s\n' % datetime.datetime.now())

    fhead = StringIO()
    fbody = StringIO()

    funcs = irgen.from_file(fname)
    if not funcs:
        print
        print '** compilation failed due to irgen not generating any TAC **'
        return
    old_optim.eliminate_unused_func(funcs)

    for func in funcs.values():
        tograph.find_leaders(func)
        optim.run_all(func)
        #tac.pprint(func)
        mipstrans = Translator(func)
        mipstrans.trans_all()
        for var_name, var in mipstrans.static_vars.iteritems():
            fhead.write('%s: .asciiz %s\n' % (var, var_name))
        map(lambda s: fbody.write('%s\n' % s), mipstrans.out)

    with open(fname.split('.')[0] + '.s', 'a') as f:
        f.write('.data\n')
        f.write('randseed: .word 12345\n')
        f.write(fhead.getvalue())
        f.write('.text\n')
        f.write(fbody.getvalue())
예제 #2
0
def main():
    if len(sys.argv) == 1:
        print 'Usage: %s [file name]' % sys.argv[0]
        return

    fname = sys.argv[1]
    funcs = from_file(fname)
    if not funcs:
        print
        print '** IRgen failed to give TAC, abort **'
        return

    want_to_see = []
    if not want_to_see:  # see all
        for fname, fcode in funcs.iteritems():
            tograph.find_leaders(fcode)  # important
            optim.run_all(fcode)
            tac.pprint(fcode)
    else:
        for fname in want_to_see:
            fcode = funcs[fname]
            tograph.find_leaders(fcode)
            optim.run_all(fcode)
            tac.pprint(fcode)