104
|
1 #!/usr/bin/python
|
|
2
|
213
|
3 import sys, argparse
|
205
|
4 import c3, ppci, codegen
|
|
5 import codegenarm
|
239
|
6 from transform import CleanPass, SameImmLoadDeletePass
|
205
|
7 import outstream
|
246
|
8 import hexfile
|
105
|
9
|
205
|
10 # Parse arguments:
|
204
|
11 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
213
|
12 parser.add_argument('source', type=argparse.FileType('r'), \
|
|
13 help='the source file to build')
|
205
|
14 parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code")
|
|
15 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
246
|
16 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
|
104
|
17
|
249
|
18 def zcc(src, outs, diag, dumpir=False):
|
207
|
19 # Front end:
|
|
20 c3b = c3.Builder(diag)
|
|
21 ircode = c3b.build(src)
|
|
22 if not ircode:
|
249
|
23 return
|
104
|
24
|
219
|
25 # Optimization passes:
|
239
|
26 ircode.check()
|
219
|
27 cp = CleanPass()
|
|
28 cp.run(ircode)
|
239
|
29 ircode.check()
|
|
30 sidp = SameImmLoadDeletePass()
|
|
31 sidp.run(ircode)
|
|
32 ircode.check()
|
219
|
33
|
249
|
34 if dumpir:
|
207
|
35 ircode.dump()
|
249
|
36
|
207
|
37 # Code generation:
|
|
38 cg = codegenarm.ArmCodeGenerator(outs)
|
|
39 obj = cg.generate(ircode)
|
249
|
40 return True
|
|
41
|
|
42 def main(args):
|
|
43 src = args.source.read()
|
|
44 args.source.close()
|
|
45 diag = ppci.DiagnosticsManager()
|
|
46 outs = outstream.TextOutputStream()
|
|
47
|
|
48 # Invoke compiler:
|
|
49 res = zcc(src, outs, diag, dumpir=args.dumpir)
|
|
50 if not res:
|
|
51 diag.printErrors(src)
|
|
52 sys.exit(1)
|
205
|
53
|
207
|
54 if args.dumpir:
|
|
55 outs.dump()
|
105
|
56
|
237
|
57 code_bytes = outs.sections['code'].to_bytes()
|
238
|
58 #print('bytes:', code_bytes)
|
207
|
59 if args.output:
|
|
60 output_filename = args.output
|
|
61 else:
|
237
|
62 output_filename = 'b.output'
|
249
|
63
|
237
|
64 with open(output_filename, 'wb') as f:
|
|
65 f.write(code_bytes)
|
104
|
66
|
246
|
67 if args.hexfile:
|
|
68 hf = hexfile.HexFile()
|
|
69 hf.addRegion(0x08000000, code_bytes)
|
|
70 hf.save(args.hexfile)
|
|
71
|
207
|
72 if __name__ == '__main__':
|
213
|
73 arguments = parser.parse_args()
|
|
74 main(arguments)
|
205
|
75
|