comparison python/zcc.py @ 253:74c6a20302d5

Added better logging
author Windel Bouwman
date Wed, 31 Jul 2013 17:57:03 +0200
parents c4370696ccc7
children 7416c923a02a
comparison
equal deleted inserted replaced
252:c4370696ccc7 253:74c6a20302d5
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import sys, argparse 3 import sys, argparse
4 import c3, ppci, codegen 4 import c3, ppci, codegen
5 import codegenarm 5 import codegenarm
6 import transform 6 from optimize import optimize
7 import outstream 7 import outstream
8 import hexfile 8 import hexfile
9 import logging
10
11 def logLevel(s):
12 numeric_level = getattr(logging, s.upper(), None)
13 if not isinstance(numeric_level, int):
14 raise ValueError('Invalid log level: {}'.format(s))
15 return numeric_level
9 16
10 # Parse arguments: 17 # Parse arguments:
11 parser = argparse.ArgumentParser(description='lcfos Compiler') 18 parser = argparse.ArgumentParser(description='lcfos Compiler')
12 parser.add_argument('source', type=argparse.FileType('r'), \ 19 parser.add_argument('source', type=argparse.FileType('r'), \
13 help='the source file to build') 20 help='the source file to build')
14 parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code") 21 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
15 parser.add_argument('-o', '--output', help='Output file', metavar='filename') 22 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
16 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w')) 23 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
24 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
17 25
18 def zcc(src, outs, diag, dumpir=False): 26 def zcc(src, outs, diag, dumpir=False):
19 # Front end: 27 # Front end:
20 c3b = c3.Builder(diag) 28 c3b = c3.Builder(diag)
21 ircode = c3b.build(src) 29 ircode = c3b.build(src)
30 logging.info('Intermediate code generated')
22 if not ircode: 31 if not ircode:
23 return 32 return
24 33
25 # Optimization passes: 34 # Optimization passes:
26 transform.optimize(ircode) 35 optimize(ircode)
36 logging.info('IR-code optimized')
27 37
28 if dumpir: 38 if dumpir:
29 ircode.dump() 39 ircode.dump()
30 40
31 # Code generation: 41 # Code generation:
32 cg = codegenarm.ArmCodeGenerator(outs) 42 cg = codegenarm.ArmCodeGenerator(outs)
33 obj = cg.generate(ircode) 43 obj = cg.generate(ircode)
34 return True 44 return True
35 45
36 def main(args): 46 def main(args):
47 logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=args.log)
37 src = args.source.read() 48 src = args.source.read()
38 args.source.close() 49 args.source.close()
50 logging.info('Source loaded')
39 diag = ppci.DiagnosticsManager() 51 diag = ppci.DiagnosticsManager()
40 outs = outstream.TextOutputStream() 52 outs = outstream.TextOutputStream()
41 53
42 # Invoke compiler: 54 # Invoke compiler:
43 res = zcc(src, outs, diag, dumpir=args.dumpir) 55 res = zcc(src, outs, diag, dumpir=args.dumpir)
44 if not res: 56 if not res:
45 diag.printErrors(src) 57 diag.printErrors(src)
46 sys.exit(1) 58 sys.exit(1)
47 59
48 if args.dumpir: 60 #if args.dumpir:
49 outs.dump() 61 # outs.dump()
50 62
51 code_bytes = outs.sections['code'].to_bytes() 63 code_bytes = outs.sections['code'].to_bytes()
52 #print('bytes:', code_bytes) 64 #print('bytes:', code_bytes)
53 if args.output: 65 if args.output:
54 output_filename = args.output 66 output_filename = args.output
60 72
61 if args.hexfile: 73 if args.hexfile:
62 hf = hexfile.HexFile() 74 hf = hexfile.HexFile()
63 hf.addRegion(0x08000000, code_bytes) 75 hf.addRegion(0x08000000, code_bytes)
64 hf.save(args.hexfile) 76 hf.save(args.hexfile)
77 logging.info('Hexfile created')
65 78
66 if __name__ == '__main__': 79 if __name__ == '__main__':
67 arguments = parser.parse_args() 80 arguments = parser.parse_args()
68 main(arguments) 81 main(arguments)
69 82