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