view python/zcc.py @ 266:649884d9dc61 Working chain 1

Test
author Windel Bouwman
date Fri, 09 Aug 2013 16:49:29 +0200
parents 04c19282a5aa
children e64bae57cda8
line wrap: on
line source

#!/usr/bin/python

import sys, argparse
import c3, ppci, codegen
import codegenarm
from optimize import optimize
import outstream
import hexfile
import logging

def logLevel(s):
    numeric_level = getattr(logging, s.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError('Invalid log level: {}'.format(s))
    return numeric_level

# 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('--dumpir', action='store_true', help="Dump IR-code")
parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
parser.add_argument('--optimize', action='store_true', help="Optimize")
parser.add_argument('-o', '--output', help='Output file', metavar='filename')
parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)

def zcc(src, outs, diag, dumpir=False, do_optimize=False):
    logging.info('Zcc started')
    # Front end:
    c3b = c3.Builder(diag)
    ircode = c3b.build(src)
    if not ircode:
        return

    # Optimization passes:
    if do_optimize:
        optimize(ircode)

    if dumpir:
        ircode.dump()

    # Code generation:
    cg = codegenarm.ArmCodeGenerator(outs)
    obj = cg.generate(ircode)
    return True

logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'

def main(args):
    logging.basicConfig(format=logformat, level=args.log)
    src = args.source.read()
    args.source.close()
    diag = ppci.DiagnosticsManager()
    outs = outstream.TextOutputStream()

    # Invoke compiler:
    res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize)
    if not res:
        diag.printErrors(src)
        sys.exit(1)

    if args.dumpasm:
        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 args.hexfile:
        logging.info('Creating hexfile')
        hf = hexfile.HexFile()
        hf.addRegion(0x08000000, code_bytes)
        hf.save(args.hexfile)

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