annotate python/zcc.py @ 311:ff665880a6b0

Added testcase for kernel and userspace
author Windel Bouwman
date Mon, 16 Dec 2013 12:49:24 +0100
parents b145f8e6050b
children 2c9768114877
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
1 #!/usr/bin/env python
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
3 import sys
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
4 import argparse
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
5 import logging
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
6
300
Windel Bouwman
parents: 296
diff changeset
7 from ppci.c3 import Builder
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
8 import ppci
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
9 from ppci.codegen import CodeGenerator
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
10 import outstream
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
11 from utils import HexFile
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
12 import target
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
13 from ppci import irutils
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
14 import io
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
15
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
16
281
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
17 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
4496cae24d7f Improved logview
Windel Bouwman
parents: 276
diff changeset
18
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
19
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
20 def logLevel(s):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 numeric_level = getattr(logging, s.upper(), None)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
22 if not isinstance(numeric_level, int):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
23 raise ValueError('Invalid log level: {}'.format(s))
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
24 return numeric_level
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
25
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
26
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
27 target_list = [target.armtarget]
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
28 targets = {t.name: t for t in target_list}
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
29 targetnames = list(targets.keys())
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
30
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
31 # Parse arguments:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 107
diff changeset
32 parser = argparse.ArgumentParser(description='lcfos Compiler')
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
33 # Input:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
34 parser.add_argument('source', type=argparse.FileType('r'), \
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
35 help='the source file to build', nargs="+")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
36 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
37 help='Possible import module', action='append', default=[])
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
38
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
39 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
40 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
41 parser.add_argument('--optimize', action='store_true', help="Optimize")
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
42 parser.add_argument('--target', help="Backend selection",
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
43 choices=targetnames, required=True)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
44 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
45 parser.add_argument('--hexfile', help='Output hexfile',
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
46 type=argparse.FileType('w'))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
47 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
48
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
49
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
50 def zcc(srcs, imps, tg, outs, diag, dumpir=False):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
51 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
52 Compile sources into output stream.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
53 Sources is an iterable of open files.
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
54 """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
55 logging.info('Zcc started')
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
56 # Front end:
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
57 c3b = Builder(diag, tg)
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
58 cg = CodeGenerator(tg)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
59
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
60 # TODO: remove this arm specifics:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
61 outs.getSection('code').address = 0x08000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
62 outs.getSection('data').address = 0x20000000
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
63
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
64 # Emit some custom start code:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
65 tg.startCode(outs)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
66 for ircode in c3b.build(srcs, imps):
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
67 if not ircode:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
68 return
290
7b38782ed496 File moves
Windel Bouwman
parents: 289
diff changeset
69 # Optimization passes, TODO
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
70
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
71 if dumpir:
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
72 f = io.StringIO()
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
73 irutils.Writer().write(ircode, f)
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 306
diff changeset
74 print(f.getvalue())
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
75
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
76 # Code generation:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
77 logging.info('Starting code generation for {}'.format(ircode))
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
78 cg.generate(ircode, outs)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
79 # TODO: fixup references, do this in another way?
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
80 outs.backpatch()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
81 outs.backpatch() # Why two times?
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
82 return c3b.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
83
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
84
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
85 def main(args):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 255
diff changeset
86 logging.basicConfig(format=logformat, level=args.log)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
87 tg = targets[args.target]
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
88 diag = ppci.DiagnosticsManager()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
89 outs = outstream.TextOutputStream()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
90
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
91 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 246
diff changeset
92 if not res:
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
93 diag.printErrors()
276
Windel Bouwman
parents: 272
diff changeset
94 return 1
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
95
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
96 if args.dumpasm:
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
97 outs.dump()
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
98
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 219
diff changeset
99 code_bytes = outs.sections['code'].to_bytes()
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
100 if args.output:
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
101 output_filename = args.output
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
102 with open(output_filename, 'wb') as f:
1c7c1e619be8 File movage
Windel Bouwman
parents: 281
diff changeset
103 f.write(code_bytes)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
104
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
105 if args.hexfile:
255
7416c923a02a Added more logging
Windel Bouwman
parents: 253
diff changeset
106 logging.info('Creating hexfile')
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
107 hf = HexFile()
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
108 hf.addRegion(0x08000000, code_bytes)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
109 hf.save(args.hexfile)
276
Windel Bouwman
parents: 272
diff changeset
110 return 0
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 239
diff changeset
111
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
112
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 205
diff changeset
113 if __name__ == '__main__':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 207
diff changeset
114 arguments = parser.parse_args()
276
Windel Bouwman
parents: 272
diff changeset
115 sys.exit(main(arguments))