annotate python/zcc.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents 04c19282a5aa
children e64bae57cda8
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 #!/usr/bin/python
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
3 import sys, argparse
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
4 import c3, ppci, codegen
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
5 import codegenarm
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
6 from optimize import optimize
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
7 import outstream
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
8 import hexfile
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
9 import logging
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
10
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
11 def logLevel(s):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
12 numeric_level = getattr(logging, s.upper(), None)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
13 if not isinstance(numeric_level, int):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
14 raise ValueError('Invalid log level: {}'.format(s))
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
15 return numeric_level
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
16
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
17 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
18 parser = argparse.ArgumentParser(description='lcfos Compiler')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
19 parser.add_argument('source', type=argparse.FileType('r'), \
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
20 help='the source file to build')
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
22 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
23 parser.add_argument('--optimize', action='store_true', help="Optimize")
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
24 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
25 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
26 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
27
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
28 def zcc(src, outs, diag, dumpir=False, do_optimize=False):
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
29 logging.info('Zcc started')
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
30 # Front end:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
31 c3b = c3.Builder(diag)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
32 ircode = c3b.build(src)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
33 if not ircode:
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
34 return
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
35
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
36 # Optimization passes:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
37 if do_optimize:
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
38 optimize(ircode)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
39
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
40 if dumpir:
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
41 ircode.dump()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
42
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
43 # Code generation:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
44 cg = codegenarm.ArmCodeGenerator(outs)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
45 obj = cg.generate(ircode)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
46 return True
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
47
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
48 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
49
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
50 def main(args):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
51 logging.basicConfig(format=logformat, level=args.log)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
52 src = args.source.read()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
53 args.source.close()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
54 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
55 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
56
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
57 # Invoke compiler:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
58 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
59 if not res:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
60 diag.printErrors(src)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
61 sys.exit(1)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
62
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
63 if args.dumpasm:
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
64 outs.dump()
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
65
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
66 code_bytes = outs.sections['code'].to_bytes()
238
90637d1bbfad Added test sequence 2
Windel Bouwman
parents: 237
diff changeset
67 #print('bytes:', code_bytes)
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
68 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
69 output_filename = args.output
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
70 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
71 output_filename = 'b.output'
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
72
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
73 with open(output_filename, 'wb') as f:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
74 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
75
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
76 if args.hexfile:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
77 logging.info('Creating hexfile')
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
78 hf = hexfile.HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
79 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
80 hf.save(args.hexfile)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
81
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
82 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
83 arguments = parser.parse_args()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
84 main(arguments)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
85