annotate python/zcc.py @ 329:8f6f3ace4e78

Added build tasks
author Windel Bouwman
date Wed, 05 Feb 2014 21:29:31 +0100
parents e9fe6988497c
children a78b41ff6ad2
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
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
7 from ppci.c3 import AstPrinter
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
8 from ppci.buildtasks import Compile
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
9 from ppci.tasks import TaskRunner
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
10 import outstream
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
11 from utils import HexFile
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
12 import target
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
13 from target.target_list import target_list
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 print('', file=f)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
45 s += '\n' + f.getvalue()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
46 if hasattr(record, 'ircode'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
47 f = io.StringIO()
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('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
50 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
51 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
52 Writer(' ').write(record.ircode, f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
53 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
54 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
55 if hasattr(record, 'irfunc'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
56 f = io.StringIO()
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('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
59 print('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
60 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
61 Writer(' ').write_function(record.irfunc, f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
62 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
63 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
64 if hasattr(record, 'ppci_frame'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
65 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
66 frame = record.ppci_frame
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('.. code::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
69 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
70 print(' {}'.format(frame.name), file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
71 for i in frame.instructions:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
72 print(' {}'.format(i),file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
73 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
74 s += '\n' + f.getvalue()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
75 if hasattr(record, 'ra_cfg'):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
76 f = io.StringIO()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
77 print('', file=f)
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('.. graphviz::', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
80 print('', file=f)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
81 print(' digraph G {', file=f)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
82 print(' size="8,80";', file=f)
312
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)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
95 print(' ratio="compress";', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
96 print(' size="8,80";', file=f)
314
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
97 ig = record.ra_ig
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
98 ig.to_dot(f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
99 print(' }', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
100 print('', file=f)
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
101 s += '\n' + f.getvalue()
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
102 if hasattr(record, 'zcc_outs'):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
103 f = io.StringIO()
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
104 print('', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
105 print('', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
106 print('.. code::', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
107 print('', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
108 outstream.OutputStreamWriter(' ').dump(record.zcc_outs, f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
109 print('', file=f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
110 s += '\n' + f.getvalue()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
111 return s
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
112
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
113
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
114 targets = {t.name: t for t in target_list}
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
115 targetnames = list(targets.keys())
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
116
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
117 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
118 parser = argparse.ArgumentParser(description='lcfos Compiler')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
119 parser.add_argument('source', type=argparse.FileType('r'), \
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
120 help='the source file to build', nargs="+")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
121 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
122 help='Possible import module', action='append', default=[])
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
123
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
124 #sub_parsers = parser.add_subparsers()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
125 #recipe = sub_parsers.add_parser('recipe')
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
126 parser.add_argument('--optimize', action='store_true', help="Optimize")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
127 parser.add_argument('--target', help="Backend selection",
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
128 choices=targetnames, required=True)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
129 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
130 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
131 type=logLevel, default='WARN')
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
132 parser.add_argument('--report',
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
133 help='Specify a file to write the compile report to',
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
134 type=argparse.FileType('w'))
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
135
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
136
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
137 def main(args):
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
138 # Configure some logging:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
139 logging.getLogger().setLevel(logging.DEBUG)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
140 ch = logging.StreamHandler()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
141 ch.setFormatter(logging.Formatter(logformat))
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
142 ch.setLevel(args.log)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
143 logging.getLogger().addHandler(ch)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
144 if args.report:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
145 fh = logging.StreamHandler(stream=args.report)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
146 fh.setFormatter(RstFormatter())
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
147 logging.getLogger().addHandler(fh)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
148
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
149 tg = targets[args.target]
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
150 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
151
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
152 tr = TaskRunner()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
153 tr.add_task(Compile(args.source, args.imp, tg, outs))
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
154
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
155 res = tr.run_tasks()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents: 323
diff changeset
156 if res > 0:
276
Windel Bouwman
parents: 272
diff changeset
157 return 1
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
158 logging.info('Assembly created', extra={'zcc_outs':outs})
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
159
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
160 code_bytes = outs.sections['code'].to_bytes()
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
161 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
162 output_filename = args.output
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
163 with open(output_filename, 'wb') as f:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
164 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
165
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
166 if args.report:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
167 logging.getLogger().removeHandler(fh)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 314
diff changeset
168 logging.getLogger().removeHandler(ch)
276
Windel Bouwman
parents: 272
diff changeset
169 return 0
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
170
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
171
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
172 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
173 arguments = parser.parse_args()
276
Windel Bouwman
parents: 272
diff changeset
174 sys.exit(main(arguments))