104
|
1 #!/usr/bin/python
|
|
2
|
213
|
3 import sys, argparse
|
205
|
4 import c3, ppci, codegen
|
|
5 import codegenarm
|
253
|
6 from optimize import optimize
|
205
|
7 import outstream
|
246
|
8 import hexfile
|
253
|
9 import logging
|
|
10
|
281
|
11 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
|
|
12
|
253
|
13 def logLevel(s):
|
|
14 numeric_level = getattr(logging, s.upper(), None)
|
|
15 if not isinstance(numeric_level, int):
|
|
16 raise ValueError('Invalid log level: {}'.format(s))
|
|
17 return numeric_level
|
105
|
18
|
205
|
19 # Parse arguments:
|
204
|
20 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
213
|
21 parser.add_argument('source', type=argparse.FileType('r'), \
|
|
22 help='the source file to build')
|
253
|
23 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
|
255
|
24 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
|
|
25 parser.add_argument('--optimize', action='store_true', help="Optimize")
|
272
|
26 parser.add_argument('--package_dir', help="Look in this directory for packages")
|
205
|
27 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
246
|
28 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
|
253
|
29 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
|
104
|
30
|
272
|
31 def zcc(src, outs, diag, dumpir=False, do_optimize=False, pack_dir=None):
|
255
|
32 logging.info('Zcc started')
|
207
|
33 # Front end:
|
|
34 c3b = c3.Builder(diag)
|
272
|
35 ircode = c3b.build(src, pack_dir=pack_dir)
|
207
|
36 if not ircode:
|
249
|
37 return
|
104
|
38
|
219
|
39 # Optimization passes:
|
255
|
40 if do_optimize:
|
|
41 optimize(ircode)
|
219
|
42
|
249
|
43 if dumpir:
|
207
|
44 ircode.dump()
|
249
|
45
|
207
|
46 # Code generation:
|
|
47 cg = codegenarm.ArmCodeGenerator(outs)
|
|
48 obj = cg.generate(ircode)
|
249
|
49 return True
|
|
50
|
|
51 def main(args):
|
258
|
52 logging.basicConfig(format=logformat, level=args.log)
|
249
|
53 src = args.source.read()
|
|
54 args.source.close()
|
|
55 diag = ppci.DiagnosticsManager()
|
|
56 outs = outstream.TextOutputStream()
|
|
57
|
|
58 # Invoke compiler:
|
272
|
59 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize, pack_dir=args.package_dir)
|
249
|
60 if not res:
|
|
61 diag.printErrors(src)
|
276
|
62 return 1
|
205
|
63
|
255
|
64 if args.dumpasm:
|
|
65 outs.dump()
|
105
|
66
|
237
|
67 code_bytes = outs.sections['code'].to_bytes()
|
238
|
68 #print('bytes:', code_bytes)
|
207
|
69 if args.output:
|
|
70 output_filename = args.output
|
|
71 else:
|
237
|
72 output_filename = 'b.output'
|
249
|
73
|
237
|
74 with open(output_filename, 'wb') as f:
|
|
75 f.write(code_bytes)
|
104
|
76
|
246
|
77 if args.hexfile:
|
255
|
78 logging.info('Creating hexfile')
|
246
|
79 hf = hexfile.HexFile()
|
|
80 hf.addRegion(0x08000000, code_bytes)
|
|
81 hf.save(args.hexfile)
|
276
|
82 return 0
|
246
|
83
|
207
|
84 if __name__ == '__main__':
|
213
|
85 arguments = parser.parse_args()
|
276
|
86 sys.exit(main(arguments))
|
205
|
87
|