104
|
1 #!/usr/bin/python
|
|
2
|
287
|
3 import sys
|
|
4 import argparse
|
|
5 import logging
|
|
6
|
|
7 import c3
|
|
8 import ppci
|
|
9 import codegen
|
205
|
10 import outstream
|
246
|
11 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]
|
|
26 targetnames = {t.name: t for t in target_list}
|
|
27
|
205
|
28 # Parse arguments:
|
204
|
29 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
287
|
30 # Input:
|
213
|
31 parser.add_argument('source', type=argparse.FileType('r'), \
|
287
|
32 help='the source file to build', nargs="+")
|
290
|
33 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
|
287
|
34 help='Possible import module', action='append')
|
|
35
|
253
|
36 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
|
255
|
37 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
|
|
38 parser.add_argument('--optimize', action='store_true', help="Optimize")
|
290
|
39 parser.add_argument('--target', help="Backend selection",
|
|
40 choices=targetnames.keys())
|
205
|
41 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
290
|
42 parser.add_argument('--hexfile', help='Output hexfile',
|
|
43 type=argparse.FileType('w'))
|
253
|
44 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
|
104
|
45
|
288
|
46
|
|
47 def zcc(srcs, imps, outs, diag, dumpir=False):
|
287
|
48 """
|
|
49 Compile sources into output stream.
|
|
50 Sources is an iterable of open files.
|
|
51 """
|
255
|
52 logging.info('Zcc started')
|
207
|
53 # Front end:
|
|
54 c3b = c3.Builder(diag)
|
290
|
55 tg = target.armtarget.armtarget
|
|
56 # TODO select target here!
|
|
57 cg = codegen.CodeGenerator(outs, tg)
|
287
|
58 for ircode in c3b.build(srcs, imps):
|
|
59 if not ircode:
|
|
60 return
|
104
|
61
|
290
|
62 # Optimization passes, TODO
|
289
|
63
|
287
|
64 if dumpir:
|
|
65 ircode.dump()
|
249
|
66
|
287
|
67 # Code generation:
|
290
|
68 cg.generate(ircode)
|
288
|
69 return c3b.ok
|
|
70
|
249
|
71
|
|
72 def main(args):
|
258
|
73 logging.basicConfig(format=logformat, level=args.log)
|
287
|
74 src = args.source
|
290
|
75 imps = args.imp
|
249
|
76 diag = ppci.DiagnosticsManager()
|
|
77 outs = outstream.TextOutputStream()
|
|
78
|
|
79 # Invoke compiler:
|
288
|
80 res = zcc(src, imps, outs, diag, dumpir=args.dumpir)
|
249
|
81 if not res:
|
|
82 diag.printErrors(src)
|
276
|
83 return 1
|
205
|
84
|
255
|
85 if args.dumpasm:
|
|
86 outs.dump()
|
105
|
87
|
237
|
88 code_bytes = outs.sections['code'].to_bytes()
|
207
|
89 if args.output:
|
|
90 output_filename = args.output
|
287
|
91 with open(output_filename, 'wb') as f:
|
|
92 f.write(code_bytes)
|
104
|
93
|
246
|
94 if args.hexfile:
|
255
|
95 logging.info('Creating hexfile')
|
246
|
96 hf = hexfile.HexFile()
|
|
97 hf.addRegion(0x08000000, code_bytes)
|
|
98 hf.save(args.hexfile)
|
276
|
99 return 0
|
246
|
100
|
288
|
101
|
207
|
102 if __name__ == '__main__':
|
213
|
103 arguments = parser.parse_args()
|
276
|
104 sys.exit(main(arguments))
|