annotate python/zcc.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents 1c7c1e619be8
children bd2593de3ff8
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 #!/usr/bin/python
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
3 import sys
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
4 import argparse
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
5 import logging
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
6
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
7 import c3
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
8 import ppci
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
9 import codegen
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
10 import codegenarm
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
11 from optimize import optimize
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
12 import outstream
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
13 import hexfile
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
14
281
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
15 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
16
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
17 def logLevel(s):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
18 numeric_level = getattr(logging, s.upper(), None)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
19 if not isinstance(numeric_level, int):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
20 raise ValueError('Invalid log level: {}'.format(s))
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 return numeric_level
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
22
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
23 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
24 parser = argparse.ArgumentParser(description='lcfos Compiler')
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
25 # Input:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
26 parser.add_argument('source', type=argparse.FileType('r'), \
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
27 help='the source file to build', nargs="+")
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
28 parser.add_argument('-i', '--import', type=argparse.FileType('r'), \
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
29 help='Possible import module', action='append')
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
30
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
31 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
32 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
33 parser.add_argument('--optimize', action='store_true', help="Optimize")
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
34 parser.add_argument('--target', help="Backend selection")
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
35 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
36 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
37 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
38
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
39
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
40 def zcc(srcs, imps, outs, diag, dumpir=False):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
41 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
42 Compile sources into output stream.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
43 Sources is an iterable of open files.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
44 """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
45 logging.info('Zcc started')
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
46 # Front end:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
47 c3b = c3.Builder(diag)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
48 for ircode in c3b.build(srcs, imps):
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
49 if not ircode:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
50 return
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
51
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
52 # Optimization passes:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
53 optimize(ircode)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
54
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
55 if dumpir:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
56 ircode.dump()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
57
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
58 # TODO select target here!
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
59 # Code generation:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
60 codegenarm.ArmCodeGenerator(outs).generate(ircode)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
61 return c3b.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
62
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
63
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
64 def main(args):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
65 logging.basicConfig(format=logformat, level=args.log)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
66 src = args.source
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
67 imps = getattr(args, 'import')
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
68 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
69 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
70
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
71 # Invoke compiler:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
72 res = zcc(src, imps, outs, diag, dumpir=args.dumpir)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
73 if not res:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
74 diag.printErrors(src)
276
Windel Bouwman
parents: 272
diff changeset
75 return 1
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
76
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
77 if args.dumpasm:
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
78 outs.dump()
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
79
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
80 code_bytes = outs.sections['code'].to_bytes()
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
81 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
82 output_filename = args.output
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
83 with open(output_filename, 'wb') as f:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
84 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
85
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
86 if args.hexfile:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
87 logging.info('Creating hexfile')
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
88 hf = hexfile.HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
89 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
90 hf.save(args.hexfile)
276
Windel Bouwman
parents: 272
diff changeset
91 return 0
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
92
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
93
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
94 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
95 arguments = parser.parse_args()
276
Windel Bouwman
parents: 272
diff changeset
96 sys.exit(main(arguments))