view python/zcc.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents 63bb40758066
children f254b87258e6
line wrap: on
line source

#!/usr/bin/python

import sys, argparse
import c3, ppci, codegen
import codegenarm
from transform import CleanPass, SameImmLoadDeletePass
import outstream

# Parse arguments:
parser = argparse.ArgumentParser(description='lcfos Compiler')
parser.add_argument('source', type=argparse.FileType('r'), \
  help='the source file to build')
parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code")
parser.add_argument('-o', '--output', help='Output file', metavar='filename')

def main(args):
    # Front end:
    src = args.source.read()
    args.source.close()
    diag = ppci.DiagnosticsManager()
    c3b = c3.Builder(diag)

    ircode = c3b.build(src)
    if not ircode:
        diag.printErrors(src)
        sys.exit(1)

    # Optimization passes:
    ircode.check()
    cp = CleanPass()
    cp.run(ircode)
    ircode.check()
    sidp = SameImmLoadDeletePass()
    sidp.run(ircode)
    ircode.check()

    if args.dumpir:
        ircode.dump()
    # Code generation:

    #cg = codegen.CodeGenerator(arm_cm3.armtarget)
    outs = outstream.TextOutputStream()
    cg = codegenarm.ArmCodeGenerator(outs)
    obj = cg.generate(ircode)

    if args.dumpir:
        outs.dump()

    code_bytes = outs.sections['code'].to_bytes()
    #print('bytes:', code_bytes)
    if args.output:
        output_filename = args.output
    else:
        output_filename = 'b.output'
    with open(output_filename, 'wb') as f:
        f.write(code_bytes)

if __name__ == '__main__':
    arguments = parser.parse_args()
    main(arguments)