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