104
|
1 #!/usr/bin/python
|
|
2
|
213
|
3 import sys, argparse
|
205
|
4 import c3, ppci, codegen
|
|
5 import codegenarm
|
252
|
6 import transform
|
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:
|
252
|
26 transform.optimize(ircode)
|
219
|
27
|
249
|
28 if dumpir:
|
207
|
29 ircode.dump()
|
249
|
30
|
207
|
31 # Code generation:
|
|
32 cg = codegenarm.ArmCodeGenerator(outs)
|
|
33 obj = cg.generate(ircode)
|
249
|
34 return True
|
|
35
|
|
36 def main(args):
|
|
37 src = args.source.read()
|
|
38 args.source.close()
|
|
39 diag = ppci.DiagnosticsManager()
|
|
40 outs = outstream.TextOutputStream()
|
|
41
|
|
42 # Invoke compiler:
|
|
43 res = zcc(src, outs, diag, dumpir=args.dumpir)
|
|
44 if not res:
|
|
45 diag.printErrors(src)
|
|
46 sys.exit(1)
|
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'
|
249
|
57
|
237
|
58 with open(output_filename, 'wb') as f:
|
|
59 f.write(code_bytes)
|
104
|
60
|
246
|
61 if args.hexfile:
|
|
62 hf = hexfile.HexFile()
|
|
63 hf.addRegion(0x08000000, code_bytes)
|
|
64 hf.save(args.hexfile)
|
|
65
|
207
|
66 if __name__ == '__main__':
|
213
|
67 arguments = parser.parse_args()
|
|
68 main(arguments)
|
205
|
69
|