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
|
|
11 def logLevel(s):
|
|
12 numeric_level = getattr(logging, s.upper(), None)
|
|
13 if not isinstance(numeric_level, int):
|
|
14 raise ValueError('Invalid log level: {}'.format(s))
|
|
15 return numeric_level
|
105
|
16
|
205
|
17 # Parse arguments:
|
204
|
18 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
213
|
19 parser.add_argument('source', type=argparse.FileType('r'), \
|
|
20 help='the source file to build')
|
253
|
21 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
|
255
|
22 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
|
|
23 parser.add_argument('--optimize', action='store_true', help="Optimize")
|
272
|
24 parser.add_argument('--package_dir', help="Look in this directory for packages")
|
205
|
25 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
246
|
26 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
|
253
|
27 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
|
104
|
28
|
272
|
29 def zcc(src, outs, diag, dumpir=False, do_optimize=False, pack_dir=None):
|
255
|
30 logging.info('Zcc started')
|
207
|
31 # Front end:
|
|
32 c3b = c3.Builder(diag)
|
272
|
33 ircode = c3b.build(src, pack_dir=pack_dir)
|
207
|
34 if not ircode:
|
249
|
35 return
|
104
|
36
|
219
|
37 # Optimization passes:
|
255
|
38 if do_optimize:
|
|
39 optimize(ircode)
|
219
|
40
|
249
|
41 if dumpir:
|
207
|
42 ircode.dump()
|
249
|
43
|
207
|
44 # Code generation:
|
|
45 cg = codegenarm.ArmCodeGenerator(outs)
|
|
46 obj = cg.generate(ircode)
|
249
|
47 return True
|
|
48
|
258
|
49 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
|
|
50
|
249
|
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
|