292
|
1 #!/usr/bin/env python
|
104
|
2
|
287
|
3 import sys
|
|
4 import argparse
|
|
5 import logging
|
|
6
|
300
|
7 from ppci.c3 import Builder
|
287
|
8 import ppci
|
301
|
9 from ppci.codegen import CodeGenerator
|
205
|
10 import outstream
|
292
|
11 from utils import HexFile
|
290
|
12 import target
|
311
|
13 from ppci import irutils
|
|
14 import io
|
253
|
15
|
289
|
16
|
281
|
17 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
|
|
18
|
289
|
19
|
253
|
20 def logLevel(s):
|
|
21 numeric_level = getattr(logging, s.upper(), None)
|
|
22 if not isinstance(numeric_level, int):
|
|
23 raise ValueError('Invalid log level: {}'.format(s))
|
|
24 return numeric_level
|
105
|
25
|
289
|
26
|
290
|
27 target_list = [target.armtarget]
|
292
|
28 targets = {t.name: t for t in target_list}
|
|
29 targetnames = list(targets.keys())
|
290
|
30
|
205
|
31 # Parse arguments:
|
204
|
32 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
287
|
33 # Input:
|
213
|
34 parser.add_argument('source', type=argparse.FileType('r'), \
|
287
|
35 help='the source file to build', nargs="+")
|
290
|
36 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
|
296
|
37 help='Possible import module', action='append', default=[])
|
287
|
38
|
253
|
39 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
|
255
|
40 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
|
|
41 parser.add_argument('--optimize', action='store_true', help="Optimize")
|
290
|
42 parser.add_argument('--target', help="Backend selection",
|
292
|
43 choices=targetnames, required=True)
|
205
|
44 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
290
|
45 parser.add_argument('--hexfile', help='Output hexfile',
|
|
46 type=argparse.FileType('w'))
|
253
|
47 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
|
104
|
48
|
288
|
49
|
292
|
50 def zcc(srcs, imps, tg, outs, diag, dumpir=False):
|
287
|
51 """
|
|
52 Compile sources into output stream.
|
|
53 Sources is an iterable of open files.
|
|
54 """
|
255
|
55 logging.info('Zcc started')
|
207
|
56 # Front end:
|
301
|
57 c3b = Builder(diag, tg)
|
|
58 cg = CodeGenerator(tg)
|
292
|
59
|
|
60 # TODO: remove this arm specifics:
|
|
61 outs.getSection('code').address = 0x08000000
|
|
62 outs.getSection('data').address = 0x20000000
|
|
63
|
|
64 # Emit some custom start code:
|
|
65 tg.startCode(outs)
|
287
|
66 for ircode in c3b.build(srcs, imps):
|
|
67 if not ircode:
|
|
68 return
|
290
|
69 # Optimization passes, TODO
|
289
|
70
|
287
|
71 if dumpir:
|
311
|
72 f = io.StringIO()
|
|
73 irutils.Writer().write(ircode, f)
|
|
74 print(f.getvalue())
|
249
|
75
|
287
|
76 # Code generation:
|
305
|
77 logging.info('Starting code generation for {}'.format(ircode))
|
292
|
78 cg.generate(ircode, outs)
|
|
79 # TODO: fixup references, do this in another way?
|
|
80 outs.backpatch()
|
|
81 outs.backpatch() # Why two times?
|
288
|
82 return c3b.ok
|
|
83
|
249
|
84
|
|
85 def main(args):
|
258
|
86 logging.basicConfig(format=logformat, level=args.log)
|
292
|
87 tg = targets[args.target]
|
249
|
88 diag = ppci.DiagnosticsManager()
|
|
89 outs = outstream.TextOutputStream()
|
|
90
|
296
|
91 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
|
249
|
92 if not res:
|
293
|
93 diag.printErrors()
|
276
|
94 return 1
|
205
|
95
|
255
|
96 if args.dumpasm:
|
|
97 outs.dump()
|
105
|
98
|
237
|
99 code_bytes = outs.sections['code'].to_bytes()
|
207
|
100 if args.output:
|
|
101 output_filename = args.output
|
287
|
102 with open(output_filename, 'wb') as f:
|
|
103 f.write(code_bytes)
|
104
|
104
|
246
|
105 if args.hexfile:
|
255
|
106 logging.info('Creating hexfile')
|
292
|
107 hf = HexFile()
|
246
|
108 hf.addRegion(0x08000000, code_bytes)
|
|
109 hf.save(args.hexfile)
|
276
|
110 return 0
|
246
|
111
|
288
|
112
|
207
|
113 if __name__ == '__main__':
|
213
|
114 arguments = parser.parse_args()
|
276
|
115 sys.exit(main(arguments))
|