292
|
1 #!/usr/bin/env python
|
104
|
2
|
287
|
3 import sys
|
|
4 import argparse
|
|
5 import logging
|
|
6
|
329
|
7 from ppci.c3 import AstPrinter
|
|
8 from ppci.buildtasks import Compile
|
|
9 from ppci.tasks import TaskRunner
|
205
|
10 import outstream
|
292
|
11 from utils import HexFile
|
290
|
12 import target
|
323
|
13 from target.target_list import target_list
|
311
|
14 from ppci import irutils
|
|
15 import io
|
253
|
16
|
289
|
17
|
281
|
18 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
|
|
19
|
289
|
20
|
253
|
21 def logLevel(s):
|
312
|
22 """ Converts a string to a valid logging level """
|
253
|
23 numeric_level = getattr(logging, s.upper(), None)
|
|
24 if not isinstance(numeric_level, int):
|
|
25 raise ValueError('Invalid log level: {}'.format(s))
|
|
26 return numeric_level
|
105
|
27
|
289
|
28
|
312
|
29 class RstFormatter(logging.Formatter):
|
|
30 """ Formatter that tries to create an rst document """
|
|
31 def __init__(self):
|
|
32 super().__init__(fmt=logformat)
|
|
33
|
|
34 def format(self, record):
|
|
35 s = super().format(record)
|
314
|
36 s += '\n'
|
313
|
37 if hasattr(record, 'c3_ast'):
|
|
38 f = io.StringIO()
|
|
39 print('', file=f)
|
|
40 print('', file=f)
|
|
41 print('.. code::', file=f)
|
|
42 print('', file=f)
|
|
43 AstPrinter().printAst(record.c3_ast, f)
|
|
44 print('', file=f)
|
|
45 s += '\n' + f.getvalue()
|
312
|
46 if hasattr(record, 'ircode'):
|
|
47 f = io.StringIO()
|
|
48 print('', file=f)
|
|
49 print('', file=f)
|
|
50 print('.. code::', file=f)
|
|
51 print('', file=f)
|
|
52 Writer(' ').write(record.ircode, f)
|
|
53 print('', file=f)
|
|
54 s += '\n' + f.getvalue()
|
|
55 if hasattr(record, 'irfunc'):
|
|
56 f = io.StringIO()
|
|
57 print('', file=f)
|
|
58 print('', file=f)
|
|
59 print('.. code::', file=f)
|
|
60 print('', file=f)
|
|
61 Writer(' ').write_function(record.irfunc, f)
|
|
62 print('', file=f)
|
|
63 s += '\n' + f.getvalue()
|
|
64 if hasattr(record, 'ppci_frame'):
|
|
65 f = io.StringIO()
|
|
66 frame = record.ppci_frame
|
|
67 print('', file=f)
|
|
68 print('.. code::', file=f)
|
|
69 print('', file=f)
|
|
70 print(' {}'.format(frame.name), file=f)
|
|
71 for i in frame.instructions:
|
|
72 print(' {}'.format(i),file=f)
|
|
73 print('', file=f)
|
|
74 s += '\n' + f.getvalue()
|
|
75 if hasattr(record, 'ra_cfg'):
|
|
76 f = io.StringIO()
|
|
77 print('', file=f)
|
|
78 print('', file=f)
|
|
79 print('.. graphviz::', file=f)
|
|
80 print('', file=f)
|
|
81 print(' digraph G {', file=f)
|
316
|
82 print(' size="8,80";', file=f)
|
312
|
83 cfg = record.ra_cfg
|
|
84 cfg.to_dot(f)
|
|
85 print(' }', file=f)
|
|
86 print('', file=f)
|
|
87 s += '\n' + f.getvalue()
|
314
|
88 if hasattr(record, 'ra_ig'):
|
|
89 f = io.StringIO()
|
|
90 print('', file=f)
|
|
91 print('', file=f)
|
|
92 print('.. graphviz::', file=f)
|
|
93 print('', file=f)
|
|
94 print(' digraph G {', file=f)
|
316
|
95 print(' ratio="compress";', file=f)
|
|
96 print(' size="8,80";', file=f)
|
314
|
97 ig = record.ra_ig
|
|
98 ig.to_dot(f)
|
|
99 print(' }', file=f)
|
|
100 print('', file=f)
|
|
101 s += '\n' + f.getvalue()
|
316
|
102 if hasattr(record, 'zcc_outs'):
|
|
103 f = io.StringIO()
|
|
104 print('', file=f)
|
|
105 print('', file=f)
|
|
106 print('.. code::', file=f)
|
|
107 print('', file=f)
|
|
108 outstream.OutputStreamWriter(' ').dump(record.zcc_outs, f)
|
|
109 print('', file=f)
|
|
110 s += '\n' + f.getvalue()
|
312
|
111 return s
|
|
112
|
|
113
|
323
|
114 targets = {t.name: t for t in target_list}
|
292
|
115 targetnames = list(targets.keys())
|
290
|
116
|
205
|
117 # Parse arguments:
|
204
|
118 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
213
|
119 parser.add_argument('source', type=argparse.FileType('r'), \
|
287
|
120 help='the source file to build', nargs="+")
|
290
|
121 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
|
296
|
122 help='Possible import module', action='append', default=[])
|
287
|
123
|
329
|
124 #sub_parsers = parser.add_subparsers()
|
|
125 #recipe = sub_parsers.add_parser('recipe')
|
255
|
126 parser.add_argument('--optimize', action='store_true', help="Optimize")
|
290
|
127 parser.add_argument('--target', help="Backend selection",
|
292
|
128 choices=targetnames, required=True)
|
205
|
129 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
329
|
130 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
|
315
|
131 type=logLevel, default='WARN')
|
316
|
132 parser.add_argument('--report',
|
329
|
133 help='Specify a file to write the compile report to',
|
315
|
134 type=argparse.FileType('w'))
|
104
|
135
|
288
|
136
|
249
|
137 def main(args):
|
315
|
138 # Configure some logging:
|
|
139 logging.getLogger().setLevel(logging.DEBUG)
|
|
140 ch = logging.StreamHandler()
|
|
141 ch.setFormatter(logging.Formatter(logformat))
|
|
142 ch.setLevel(args.log)
|
|
143 logging.getLogger().addHandler(ch)
|
|
144 if args.report:
|
|
145 fh = logging.StreamHandler(stream=args.report)
|
|
146 fh.setFormatter(RstFormatter())
|
|
147 logging.getLogger().addHandler(fh)
|
312
|
148
|
292
|
149 tg = targets[args.target]
|
249
|
150 outs = outstream.TextOutputStream()
|
|
151
|
329
|
152 tr = TaskRunner()
|
|
153 tr.add_task(Compile(args.source, args.imp, tg, outs))
|
|
154
|
|
155 res = tr.run_tasks()
|
|
156 if res > 0:
|
276
|
157 return 1
|
313
|
158 logging.info('Assembly created', extra={'zcc_outs':outs})
|
105
|
159
|
237
|
160 code_bytes = outs.sections['code'].to_bytes()
|
207
|
161 if args.output:
|
|
162 output_filename = args.output
|
287
|
163 with open(output_filename, 'wb') as f:
|
|
164 f.write(code_bytes)
|
104
|
165
|
315
|
166 if args.report:
|
|
167 logging.getLogger().removeHandler(fh)
|
|
168 logging.getLogger().removeHandler(ch)
|
276
|
169 return 0
|
246
|
170
|
288
|
171
|
207
|
172 if __name__ == '__main__':
|
213
|
173 arguments = parser.parse_args()
|
276
|
174 sys.exit(main(arguments))
|