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