diff python/zcc.py @ 253:74c6a20302d5

Added better logging
author Windel Bouwman
date Wed, 31 Jul 2013 17:57:03 +0200
parents c4370696ccc7
children 7416c923a02a
line wrap: on
line diff
--- a/python/zcc.py	Tue Jul 30 17:57:46 2013 +0200
+++ b/python/zcc.py	Wed Jul 31 17:57:03 2013 +0200
@@ -3,27 +3,37 @@
 import sys, argparse
 import c3, ppci, codegen
 import codegenarm
-import transform
+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('-d', '--dumpir', action='store_true', help="Dump IR-code")
+parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
 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):
     # Front end:
     c3b = c3.Builder(diag)
     ircode = c3b.build(src)
+    logging.info('Intermediate code generated')
     if not ircode:
         return
 
     # Optimization passes:
-    transform.optimize(ircode)
+    optimize(ircode)
+    logging.info('IR-code optimized')
 
     if dumpir:
         ircode.dump()
@@ -34,8 +44,10 @@
     return True
 
 def main(args):
+    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=args.log)
     src = args.source.read()
     args.source.close()
+    logging.info('Source loaded')
     diag = ppci.DiagnosticsManager()
     outs = outstream.TextOutputStream()
 
@@ -45,8 +57,8 @@
         diag.printErrors(src)
         sys.exit(1)
 
-    if args.dumpir:
-        outs.dump()
+    #if args.dumpir:
+    #    outs.dump()
 
     code_bytes = outs.sections['code'].to_bytes()
     #print('bytes:', code_bytes)
@@ -62,6 +74,7 @@
         hf = hexfile.HexFile()
         hf.addRegion(0x08000000, code_bytes)
         hf.save(args.hexfile)
+        logging.info('Hexfile created')
 
 if __name__ == '__main__':
     arguments = parser.parse_args()