annotate python/zcc.py @ 249:e41e4109addd

Added current position arrow
author Windel Bouwman
date Fri, 26 Jul 2013 20:26:05 +0200
parents f254b87258e6
children c4370696ccc7
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 #!/usr/bin/python
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
3 import sys, argparse
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
4 import c3, ppci, codegen
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
5 import codegenarm
239
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
6 from transform import CleanPass, SameImmLoadDeletePass
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
7 import outstream
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
8 import hexfile
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
9
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
10 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
11 parser = argparse.ArgumentParser(description='lcfos Compiler')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
12 parser.add_argument('source', type=argparse.FileType('r'), \
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
13 help='the source file to build')
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
14 parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code")
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
15 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
16 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
17
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
18 def zcc(src, outs, diag, dumpir=False):
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
19 # Front end:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
20 c3b = c3.Builder(diag)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
21 ircode = c3b.build(src)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
22 if not ircode:
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
23 return
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
24
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
25 # Optimization passes:
239
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
26 ircode.check()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
27 cp = CleanPass()
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
28 cp.run(ircode)
239
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
29 ircode.check()
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
30 sidp = SameImmLoadDeletePass()
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
31 sidp.run(ircode)
63bb40758066 added check
Windel Bouwman
parents: 238
diff changeset
32 ircode.check()
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
33
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
34 if dumpir:
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
35 ircode.dump()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
36
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
37 # Code generation:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
38 cg = codegenarm.ArmCodeGenerator(outs)
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
39 obj = cg.generate(ircode)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
40 return True
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
41
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
42 def main(args):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
43 src = args.source.read()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
44 args.source.close()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
45 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
46 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
47
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
48 # Invoke compiler:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
49 res = zcc(src, outs, diag, dumpir=args.dumpir)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
50 if not res:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
51 diag.printErrors(src)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
52 sys.exit(1)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
53
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
54 if args.dumpir:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
55 outs.dump()
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
56
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
57 code_bytes = outs.sections['code'].to_bytes()
238
90637d1bbfad Added test sequence 2
Windel Bouwman
parents: 237
diff changeset
58 #print('bytes:', code_bytes)
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
59 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
60 output_filename = args.output
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
61 else:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
62 output_filename = 'b.output'
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
63
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
64 with open(output_filename, 'wb') as f:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
65 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
66
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
67 if args.hexfile:
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
68 hf = hexfile.HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
69 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
70 hf.save(args.hexfile)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
71
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
72 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
73 arguments = parser.parse_args()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
74 main(arguments)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
75