annotate python/zcc.py @ 312:2c9768114877

Added cool logging formatter
author Windel Bouwman
date Mon, 16 Dec 2013 17:58:15 +0100
parents ff665880a6b0
children 04cf4d26a3bc
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
300
Windel Bouwman
parents: 296
diff changeset
7 from ppci.c3 import Builder
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)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
36 if hasattr(record, 'ircode'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
37 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
38 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
39 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
40 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
41 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
42 Writer(' ').write(record.ircode, f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
43 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
44 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
45 if hasattr(record, 'irfunc'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
46 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
47 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
48 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
49 print('.. code::', 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 Writer(' ').write_function(record.irfunc, 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 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
54 if hasattr(record, 'ppci_frame'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
55 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
56 frame = record.ppci_frame
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
57 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
58 print('.. code::', 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(' {}'.format(frame.name), file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
61 for i in frame.instructions:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
62 print(' {}'.format(i),file=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, 'ra_cfg'):
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 print('', file=f)
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('.. graphviz::', 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(' digraph G {', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
72 cfg = record.ra_cfg
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
73 cfg.to_dot(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 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
76 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
77 return s
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
78
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
79
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
80 target_list = [target.armtarget]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
81 targets = {t.name: t for t in target_list}
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
82 targetnames = list(targets.keys())
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
83
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
84 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
85 parser = argparse.ArgumentParser(description='lcfos Compiler')
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
86 # Input:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
87 parser.add_argument('source', type=argparse.FileType('r'), \
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
88 help='the source file to build', nargs="+")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
89 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
90 help='Possible import module', action='append', default=[])
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
91
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
92 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
93 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
94 parser.add_argument('--optimize', action='store_true', help="Optimize")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
95 parser.add_argument('--target', help="Backend selection",
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
96 choices=targetnames, required=True)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
97 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
98 parser.add_argument('--hexfile', help='Output hexfile',
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
99 type=argparse.FileType('w'))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
100 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
101
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
102
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
103 def zcc(srcs, imps, tg, outs, diag, dumpir=False):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
104 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
105 Compile sources into output stream.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
106 Sources is an iterable of open files.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
107 """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
108 logging.info('Zcc started')
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
109 # Front end:
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
110 c3b = Builder(diag, tg)
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
111 cg = CodeGenerator(tg)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
112
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
113 # TODO: remove this arm specifics:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
114 outs.getSection('code').address = 0x08000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
115 outs.getSection('data').address = 0x20000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
116
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
117 # Emit some custom start code:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
118 tg.startCode(outs)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
119 for ircode in c3b.build(srcs, imps):
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
120 if not ircode:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
121 return
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
122 # Optimization passes, TODO
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
123 Verifier().verify(ircode)
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
124
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
125 if dumpir:
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
126 f = io.StringIO()
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
127 irutils.Writer().write(ircode, f)
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
128 print(f.getvalue())
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
129
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
130 # Code generation:
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
131 d = {'ircode':ircode}
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
132 logging.info('Starting code generation for {}'.format(ircode), extra=d)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
133 cg.generate(ircode, outs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
134 # TODO: fixup references, do this in another way?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
135 outs.backpatch()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
136 outs.backpatch() # Why two times?
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
137 return c3b.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
138
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
139
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
140 def main(args):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
141 logging.basicConfig(format=logformat, level=args.log)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
142 #logging.getLogger().addHandler(RstLogHandler())
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
143 fh = logging.FileHandler('log.rst', mode='w')
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
144 fh.setFormatter(RstFormatter())
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
145 logging.getLogger().addHandler(fh)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
146
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
147 tg = targets[args.target]
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
148 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
149 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
150
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
151 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
152 if not res:
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
153 diag.printErrors()
276
Windel Bouwman
parents: 272
diff changeset
154 return 1
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
155
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
156 if args.dumpasm:
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
157 outs.dump()
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
158
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
159 code_bytes = outs.sections['code'].to_bytes()
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
160 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
161 output_filename = args.output
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
162 with open(output_filename, 'wb') as f:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
163 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
164
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
165 if args.hexfile:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
166 logging.info('Creating hexfile')
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
167 hf = HexFile()
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
168 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
169 hf.save(args.hexfile)
276
Windel Bouwman
parents: 272
diff changeset
170 return 0
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
171
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
172
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
173 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
174 arguments = parser.parse_args()
276
Windel Bouwman
parents: 272
diff changeset
175 sys.exit(main(arguments))