annotate python/zcc.py @ 314:38f5f298ce0e

Add log for interference graph
author Windel Bouwman
date Wed, 18 Dec 2013 20:22:20 +0100
parents 04cf4d26a3bc
children 084cccaa5deb
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
1 #!/usr/bin/env python
104
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
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
7 from ppci.c3 import Builder, AstPrinter
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
8 import ppci
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
9 from ppci.irutils import Verifier, Writer
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
10 from ppci.codegen import CodeGenerator
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
11 import outstream
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
12 from utils import HexFile
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
13 import target
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
14 from ppci import irutils
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
15 import io
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
16
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
17
281
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
18 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
19
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
20
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 def logLevel(s):
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
22 """ Converts a string to a valid logging level """
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
23 numeric_level = getattr(logging, s.upper(), None)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
24 if not isinstance(numeric_level, int):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
25 raise ValueError('Invalid log level: {}'.format(s))
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
26 return numeric_level
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
27
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
28
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
29 class RstFormatter(logging.Formatter):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
30 """ Formatter that tries to create an rst document """
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
31 def __init__(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
32 super().__init__(fmt=logformat)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
33
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
34 def format(self, record):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
35 s = super().format(record)
314
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
36 s += '\n'
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
37 if hasattr(record, 'c3_ast'):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
38 f = io.StringIO()
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
39 print('', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
40 print('', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
41 print('.. code::', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
42 print('', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
43 AstPrinter().printAst(record.c3_ast, f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
44 #Writer(' ').write(record.c3_ast, f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
45 print('', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
46 s += '\n' + f.getvalue()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
47 if hasattr(record, 'ircode'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
48 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
49 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
50 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
51 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
52 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
53 Writer(' ').write(record.ircode, f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
54 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
55 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
56 if hasattr(record, 'irfunc'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
57 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
58 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
59 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
60 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
61 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
62 Writer(' ').write_function(record.irfunc, f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
63 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
64 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
65 if hasattr(record, 'ppci_frame'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
66 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
67 frame = record.ppci_frame
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
68 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
69 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
70 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
71 print(' {}'.format(frame.name), file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
72 for i in frame.instructions:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
73 print(' {}'.format(i),file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
74 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
75 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
76 if hasattr(record, 'ra_cfg'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
77 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
78 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
79 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
80 print('.. graphviz::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
81 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
82 print(' digraph G {', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
83 cfg = record.ra_cfg
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
84 cfg.to_dot(f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
85 print(' }', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
86 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
87 s += '\n' + f.getvalue()
314
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
88 if hasattr(record, 'ra_ig'):
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
89 f = io.StringIO()
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
90 print('', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
91 print('', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
92 print('.. graphviz::', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
93 print('', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
94 print(' digraph G {', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
95 ig = record.ra_ig
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
96 ig.to_dot(f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
97 print(' }', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
98 print('', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
99 s += '\n' + f.getvalue()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
100 return s
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
101
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
102
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
103 target_list = [target.armtarget]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
104 targets = {t.name: t for t in target_list}
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
105 targetnames = list(targets.keys())
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
106
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
107 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
108 parser = argparse.ArgumentParser(description='lcfos Compiler')
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
109 # Input:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
110 parser.add_argument('source', type=argparse.FileType('r'), \
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
111 help='the source file to build', nargs="+")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
112 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
113 help='Possible import module', action='append', default=[])
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
114
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
115 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
116 parser.add_argument('--optimize', action='store_true', help="Optimize")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
117 parser.add_argument('--target', help="Backend selection",
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
118 choices=targetnames, required=True)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
119 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
120 parser.add_argument('--hexfile', help='Output hexfile',
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
121 type=argparse.FileType('w'))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
122 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
123
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
124
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
125 def zcc(srcs, imps, tg, outs, diag, dumpir=False):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
126 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
127 Compile sources into output stream.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
128 Sources is an iterable of open files.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
129 """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
130 logging.info('Zcc started')
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
131 # Front end:
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
132 c3b = Builder(diag, tg)
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
133 cg = CodeGenerator(tg)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
134
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
135 # TODO: remove this arm specifics:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
136 outs.getSection('code').address = 0x08000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
137 outs.getSection('data').address = 0x20000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
138
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
139 # Emit some custom start code:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
140 tg.startCode(outs)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
141 for ircode in c3b.build(srcs, imps):
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
142 if not ircode:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
143 return
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
144
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
145 d = {'ircode':ircode}
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
146 logging.info('Verifying code {}'.format(ircode), extra=d)
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
147 # Optimization passes, TODO
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
148 Verifier().verify(ircode)
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
149
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
150 # Code generation:
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
151 d = {'ircode':ircode}
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
152 logging.info('Starting code generation for {}'.format(ircode), extra=d)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
153 cg.generate(ircode, outs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
154 # TODO: fixup references, do this in another way?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
155 outs.backpatch()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
156 outs.backpatch() # Why two times?
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
157 return c3b.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
158
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
159
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
160 def main(args):
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
161 #logging.getLogger().setLevel(logging.DEBUG)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
162 #logging.getLogger().addHandler(RstLogHandler())
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
163 #fh = logging.FileHandler('log.rst', mode='w')
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
164 #fh.setFormatter(RstFormatter())
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
165 #logging.getLogger().addHandler(fh)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
166
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
167 tg = targets[args.target]
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
168 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
169 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
170
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
171 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
172 if not res:
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
173 diag.printErrors()
276
Windel Bouwman
parents: 272
diff changeset
174 return 1
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
175
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
176 logging.info('Assembly created', extra={'zcc_outs':outs})
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
177
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
178 code_bytes = outs.sections['code'].to_bytes()
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
179 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
180 output_filename = args.output
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
181 with open(output_filename, 'wb') as f:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
182 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
183
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
184 if args.hexfile:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
185 logging.info('Creating hexfile')
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
186 hf = HexFile()
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
187 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
188 hf.save(args.hexfile)
276
Windel Bouwman
parents: 272
diff changeset
189 return 0
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
190
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
191
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
192 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
193 arguments = parser.parse_args()
276
Windel Bouwman
parents: 272
diff changeset
194 sys.exit(main(arguments))