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
|
105
|
8
|
205
|
9 # Parse arguments:
|
204
|
10 parser = argparse.ArgumentParser(description='lcfos Compiler')
|
213
|
11 parser.add_argument('source', type=argparse.FileType('r'), \
|
|
12 help='the source file to build')
|
205
|
13 parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code")
|
|
14 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
|
104
|
15
|
207
|
16 def main(args):
|
|
17 # Front end:
|
|
18 src = args.source.read()
|
|
19 args.source.close()
|
|
20 diag = ppci.DiagnosticsManager()
|
|
21 c3b = c3.Builder(diag)
|
204
|
22
|
207
|
23 ircode = c3b.build(src)
|
|
24 if not ircode:
|
|
25 diag.printErrors(src)
|
|
26 sys.exit(1)
|
104
|
27
|
219
|
28 # Optimization passes:
|
239
|
29 ircode.check()
|
219
|
30 cp = CleanPass()
|
|
31 cp.run(ircode)
|
239
|
32 ircode.check()
|
|
33 sidp = SameImmLoadDeletePass()
|
|
34 sidp.run(ircode)
|
|
35 ircode.check()
|
219
|
36
|
207
|
37 if args.dumpir:
|
|
38 ircode.dump()
|
|
39 # Code generation:
|
205
|
40
|
207
|
41 #cg = codegen.CodeGenerator(arm_cm3.armtarget)
|
|
42 outs = outstream.TextOutputStream()
|
|
43 cg = codegenarm.ArmCodeGenerator(outs)
|
|
44 obj = cg.generate(ircode)
|
205
|
45
|
207
|
46 if args.dumpir:
|
|
47 outs.dump()
|
105
|
48
|
237
|
49 code_bytes = outs.sections['code'].to_bytes()
|
238
|
50 #print('bytes:', code_bytes)
|
207
|
51 if args.output:
|
|
52 output_filename = args.output
|
|
53 else:
|
237
|
54 output_filename = 'b.output'
|
|
55 with open(output_filename, 'wb') as f:
|
|
56 f.write(code_bytes)
|
104
|
57
|
207
|
58 if __name__ == '__main__':
|
213
|
59 arguments = parser.parse_args()
|
|
60 main(arguments)
|
205
|
61
|