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
|
207
|
18 def main(args):
|
|
19 # Front end:
|
|
20 src = args.source.read()
|
|
21 args.source.close()
|
|
22 diag = ppci.DiagnosticsManager()
|
|
23 c3b = c3.Builder(diag)
|
204
|
24
|
207
|
25 ircode = c3b.build(src)
|
|
26 if not ircode:
|
|
27 diag.printErrors(src)
|
|
28 sys.exit(1)
|
104
|
29
|
219
|
30 # Optimization passes:
|
239
|
31 ircode.check()
|
219
|
32 cp = CleanPass()
|
|
33 cp.run(ircode)
|
239
|
34 ircode.check()
|
|
35 sidp = SameImmLoadDeletePass()
|
|
36 sidp.run(ircode)
|
|
37 ircode.check()
|
219
|
38
|
207
|
39 if args.dumpir:
|
|
40 ircode.dump()
|
|
41 # Code generation:
|
205
|
42
|
207
|
43 #cg = codegen.CodeGenerator(arm_cm3.armtarget)
|
|
44 outs = outstream.TextOutputStream()
|
|
45 cg = codegenarm.ArmCodeGenerator(outs)
|
|
46 obj = cg.generate(ircode)
|
205
|
47
|
207
|
48 if args.dumpir:
|
|
49 outs.dump()
|
105
|
50
|
237
|
51 code_bytes = outs.sections['code'].to_bytes()
|
238
|
52 #print('bytes:', code_bytes)
|
207
|
53 if args.output:
|
|
54 output_filename = args.output
|
|
55 else:
|
237
|
56 output_filename = 'b.output'
|
|
57 with open(output_filename, 'wb') as f:
|
|
58 f.write(code_bytes)
|
104
|
59
|
246
|
60 if args.hexfile:
|
|
61 hf = hexfile.HexFile()
|
|
62 hf.addRegion(0x08000000, code_bytes)
|
|
63 hf.save(args.hexfile)
|
|
64
|
207
|
65 if __name__ == '__main__':
|
213
|
66 arguments = parser.parse_args()
|
|
67 main(arguments)
|
205
|
68
|