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
|
287
|
39 def zcc(srcs, outs, diag, dumpir=False, do_optimize=False):
|
|
40 """
|
|
41 Compile sources into output stream.
|
|
42 Sources is an iterable of open files.
|
|
43 """
|
255
|
44 logging.info('Zcc started')
|
207
|
45 # Front end:
|
|
46 c3b = c3.Builder(diag)
|
287
|
47 imps = []
|
|
48 for ircode in c3b.build(srcs, imps):
|
|
49 print(ircode)
|
|
50 if not ircode:
|
|
51 return
|
104
|
52
|
287
|
53 # Optimization passes:
|
|
54 if do_optimize:
|
|
55 optimize(ircode)
|
219
|
56
|
287
|
57 if dumpir:
|
|
58 ircode.dump()
|
249
|
59
|
287
|
60 # Code generation:
|
|
61 cg = codegenarm.ArmCodeGenerator(outs)
|
|
62 obj = cg.generate(ircode)
|
249
|
63 return True
|
|
64
|
|
65 def main(args):
|
258
|
66 logging.basicConfig(format=logformat, level=args.log)
|
287
|
67 src = args.source
|
249
|
68 diag = ppci.DiagnosticsManager()
|
|
69 outs = outstream.TextOutputStream()
|
|
70
|
|
71 # Invoke compiler:
|
287
|
72 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize)
|
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
|
207
|
93 if __name__ == '__main__':
|
213
|
94 arguments = parser.parse_args()
|
287
|
95 print(arguments)
|
276
|
96 sys.exit(main(arguments))
|
205
|
97
|