Mercurial > lcfOS
view python/zcc.py @ 314:38f5f298ce0e
Add log for interference graph
author | Windel Bouwman |
---|---|
date | Wed, 18 Dec 2013 20:22:20 +0100 |
parents | 04cf4d26a3bc |
children | 084cccaa5deb |
line wrap: on
line source
#!/usr/bin/env python import sys import argparse import logging from ppci.c3 import Builder, AstPrinter import ppci from ppci.irutils import Verifier, Writer from ppci.codegen import CodeGenerator import outstream from utils import HexFile import target from ppci import irutils import io logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' def logLevel(s): """ Converts a string to a valid logging level """ numeric_level = getattr(logging, s.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: {}'.format(s)) return numeric_level class RstFormatter(logging.Formatter): """ Formatter that tries to create an rst document """ def __init__(self): super().__init__(fmt=logformat) def format(self, record): s = super().format(record) s += '\n' if hasattr(record, 'c3_ast'): f = io.StringIO() print('', file=f) print('', file=f) print('.. code::', file=f) print('', file=f) AstPrinter().printAst(record.c3_ast, f) #Writer(' ').write(record.c3_ast, f) print('', file=f) s += '\n' + f.getvalue() if hasattr(record, 'ircode'): f = io.StringIO() print('', file=f) print('', file=f) print('.. code::', file=f) print('', file=f) Writer(' ').write(record.ircode, f) print('', file=f) s += '\n' + f.getvalue() if hasattr(record, 'irfunc'): f = io.StringIO() print('', file=f) print('', file=f) print('.. code::', file=f) print('', file=f) Writer(' ').write_function(record.irfunc, f) print('', file=f) s += '\n' + f.getvalue() if hasattr(record, 'ppci_frame'): f = io.StringIO() frame = record.ppci_frame print('', file=f) print('.. code::', file=f) print('', file=f) print(' {}'.format(frame.name), file=f) for i in frame.instructions: print(' {}'.format(i),file=f) print('', file=f) s += '\n' + f.getvalue() if hasattr(record, 'ra_cfg'): f = io.StringIO() print('', file=f) print('', file=f) print('.. graphviz::', file=f) print('', file=f) print(' digraph G {', file=f) cfg = record.ra_cfg cfg.to_dot(f) print(' }', file=f) print('', file=f) s += '\n' + f.getvalue() if hasattr(record, 'ra_ig'): f = io.StringIO() print('', file=f) print('', file=f) print('.. graphviz::', file=f) print('', file=f) print(' digraph G {', file=f) ig = record.ra_ig ig.to_dot(f) print(' }', file=f) print('', file=f) s += '\n' + f.getvalue() return s target_list = [target.armtarget] targets = {t.name: t for t in target_list} targetnames = list(targets.keys()) # Parse arguments: parser = argparse.ArgumentParser(description='lcfos Compiler') # Input: parser.add_argument('source', type=argparse.FileType('r'), \ help='the source file to build', nargs="+") parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \ help='Possible import module', action='append', default=[]) parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") parser.add_argument('--optimize', action='store_true', help="Optimize") parser.add_argument('--target', help="Backend selection", choices=targetnames, required=True) 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(srcs, imps, tg, outs, diag, dumpir=False): """ Compile sources into output stream. Sources is an iterable of open files. """ logging.info('Zcc started') # Front end: c3b = Builder(diag, tg) cg = CodeGenerator(tg) # TODO: remove this arm specifics: outs.getSection('code').address = 0x08000000 outs.getSection('data').address = 0x20000000 # Emit some custom start code: tg.startCode(outs) for ircode in c3b.build(srcs, imps): if not ircode: return d = {'ircode':ircode} logging.info('Verifying code {}'.format(ircode), extra=d) # Optimization passes, TODO Verifier().verify(ircode) # Code generation: d = {'ircode':ircode} logging.info('Starting code generation for {}'.format(ircode), extra=d) cg.generate(ircode, outs) # TODO: fixup references, do this in another way? outs.backpatch() outs.backpatch() # Why two times? return c3b.ok def main(args): #logging.getLogger().setLevel(logging.DEBUG) #logging.getLogger().addHandler(RstLogHandler()) #fh = logging.FileHandler('log.rst', mode='w') #fh.setFormatter(RstFormatter()) #logging.getLogger().addHandler(fh) tg = targets[args.target] diag = ppci.DiagnosticsManager() outs = outstream.TextOutputStream() res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) if not res: diag.printErrors() return 1 logging.info('Assembly created', extra={'zcc_outs':outs}) code_bytes = outs.sections['code'].to_bytes() if args.output: output_filename = args.output with open(output_filename, 'wb') as f: f.write(code_bytes) if args.hexfile: logging.info('Creating hexfile') hf = HexFile() hf.addRegion(0x08000000, code_bytes) hf.save(args.hexfile) return 0 if __name__ == '__main__': arguments = parser.parse_args() sys.exit(main(arguments))