Mercurial > lcfOS
comparison python/zcc.py @ 255:7416c923a02a
Added more logging
author | Windel Bouwman |
---|---|
date | Sun, 04 Aug 2013 15:10:10 +0200 |
parents | 74c6a20302d5 |
children | 04c19282a5aa |
comparison
equal
deleted
inserted
replaced
254:bd26dc13f270 | 255:7416c923a02a |
---|---|
17 # Parse arguments: | 17 # Parse arguments: |
18 parser = argparse.ArgumentParser(description='lcfos Compiler') | 18 parser = argparse.ArgumentParser(description='lcfos Compiler') |
19 parser.add_argument('source', type=argparse.FileType('r'), \ | 19 parser.add_argument('source', type=argparse.FileType('r'), \ |
20 help='the source file to build') | 20 help='the source file to build') |
21 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") | 21 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") |
22 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") | |
23 parser.add_argument('--optimize', action='store_true', help="Optimize") | |
22 parser.add_argument('-o', '--output', help='Output file', metavar='filename') | 24 parser.add_argument('-o', '--output', help='Output file', metavar='filename') |
23 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w')) | 25 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w')) |
24 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) | 26 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) |
25 | 27 |
26 def zcc(src, outs, diag, dumpir=False): | 28 def zcc(src, outs, diag, dumpir=False, do_optimize=False): |
29 logging.info('Zcc started') | |
27 # Front end: | 30 # Front end: |
28 c3b = c3.Builder(diag) | 31 c3b = c3.Builder(diag) |
29 ircode = c3b.build(src) | 32 ircode = c3b.build(src) |
30 logging.info('Intermediate code generated') | |
31 if not ircode: | 33 if not ircode: |
32 return | 34 return |
33 | 35 |
34 # Optimization passes: | 36 # Optimization passes: |
35 optimize(ircode) | 37 if do_optimize: |
36 logging.info('IR-code optimized') | 38 optimize(ircode) |
37 | 39 |
38 if dumpir: | 40 if dumpir: |
39 ircode.dump() | 41 ircode.dump() |
40 | 42 |
41 # Code generation: | 43 # Code generation: |
42 cg = codegenarm.ArmCodeGenerator(outs) | 44 cg = codegenarm.ArmCodeGenerator(outs) |
43 obj = cg.generate(ircode) | 45 obj = cg.generate(ircode) |
44 return True | 46 return True |
45 | 47 |
46 def main(args): | 48 def main(args): |
47 logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=args.log) | 49 logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s', level=args.log) |
48 src = args.source.read() | 50 src = args.source.read() |
49 args.source.close() | 51 args.source.close() |
50 logging.info('Source loaded') | |
51 diag = ppci.DiagnosticsManager() | 52 diag = ppci.DiagnosticsManager() |
52 outs = outstream.TextOutputStream() | 53 outs = outstream.TextOutputStream() |
53 | 54 |
54 # Invoke compiler: | 55 # Invoke compiler: |
55 res = zcc(src, outs, diag, dumpir=args.dumpir) | 56 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize) |
56 if not res: | 57 if not res: |
57 diag.printErrors(src) | 58 diag.printErrors(src) |
58 sys.exit(1) | 59 sys.exit(1) |
59 | 60 |
60 #if args.dumpir: | 61 if args.dumpasm: |
61 # outs.dump() | 62 outs.dump() |
62 | 63 |
63 code_bytes = outs.sections['code'].to_bytes() | 64 code_bytes = outs.sections['code'].to_bytes() |
64 #print('bytes:', code_bytes) | 65 #print('bytes:', code_bytes) |
65 if args.output: | 66 if args.output: |
66 output_filename = args.output | 67 output_filename = args.output |
69 | 70 |
70 with open(output_filename, 'wb') as f: | 71 with open(output_filename, 'wb') as f: |
71 f.write(code_bytes) | 72 f.write(code_bytes) |
72 | 73 |
73 if args.hexfile: | 74 if args.hexfile: |
75 logging.info('Creating hexfile') | |
74 hf = hexfile.HexFile() | 76 hf = hexfile.HexFile() |
75 hf.addRegion(0x08000000, code_bytes) | 77 hf.addRegion(0x08000000, code_bytes) |
76 hf.save(args.hexfile) | 78 hf.save(args.hexfile) |
77 logging.info('Hexfile created') | |
78 | 79 |
79 if __name__ == '__main__': | 80 if __name__ == '__main__': |
80 arguments = parser.parse_args() | 81 arguments = parser.parse_args() |
81 main(arguments) | 82 main(arguments) |
82 | 83 |