comparison python/zcc.py @ 292:534b94b40aa8

Fixup reorganize
author Windel Bouwman
date Wed, 27 Nov 2013 08:06:42 +0100
parents 7b38782ed496
children 6aa721e7b10b
comparison
equal deleted inserted replaced
290:7b38782ed496 292:534b94b40aa8
1 #!/usr/bin/python 1 #!/usr/bin/env python
2 2
3 import sys 3 import sys
4 import argparse 4 import argparse
5 import logging 5 import logging
6 6
7 import c3 7 import c3
8 import ppci 8 import ppci
9 import codegen 9 import codegen
10 import outstream 10 import outstream
11 import hexfile 11 from utils import HexFile
12 import target 12 import target
13 13
14 14
15 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' 15 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
16 16
21 raise ValueError('Invalid log level: {}'.format(s)) 21 raise ValueError('Invalid log level: {}'.format(s))
22 return numeric_level 22 return numeric_level
23 23
24 24
25 target_list = [target.armtarget] 25 target_list = [target.armtarget]
26 targetnames = {t.name: t for t in target_list} 26 targets = {t.name: t for t in target_list}
27 targetnames = list(targets.keys())
27 28
28 # Parse arguments: 29 # Parse arguments:
29 parser = argparse.ArgumentParser(description='lcfos Compiler') 30 parser = argparse.ArgumentParser(description='lcfos Compiler')
30 # Input: 31 # Input:
31 parser.add_argument('source', type=argparse.FileType('r'), \ 32 parser.add_argument('source', type=argparse.FileType('r'), \
35 36
36 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") 37 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
37 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") 38 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
38 parser.add_argument('--optimize', action='store_true', help="Optimize") 39 parser.add_argument('--optimize', action='store_true', help="Optimize")
39 parser.add_argument('--target', help="Backend selection", 40 parser.add_argument('--target', help="Backend selection",
40 choices=targetnames.keys()) 41 choices=targetnames, required=True)
41 parser.add_argument('-o', '--output', help='Output file', metavar='filename') 42 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
42 parser.add_argument('--hexfile', help='Output hexfile', 43 parser.add_argument('--hexfile', help='Output hexfile',
43 type=argparse.FileType('w')) 44 type=argparse.FileType('w'))
44 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) 45 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
45 46
46 47
47 def zcc(srcs, imps, outs, diag, dumpir=False): 48 def zcc(srcs, imps, tg, outs, diag, dumpir=False):
48 """ 49 """
49 Compile sources into output stream. 50 Compile sources into output stream.
50 Sources is an iterable of open files. 51 Sources is an iterable of open files.
51 """ 52 """
52 logging.info('Zcc started') 53 logging.info('Zcc started')
53 # Front end: 54 # Front end:
54 c3b = c3.Builder(diag) 55 c3b = c3.Builder(diag)
55 tg = target.armtarget.armtarget 56 cg = codegen.CodeGenerator(tg)
56 # TODO select target here! 57
57 cg = codegen.CodeGenerator(outs, tg) 58 # TODO: remove this arm specifics:
59 outs.getSection('code').address = 0x08000000
60 outs.getSection('data').address = 0x20000000
61
62 # Emit some custom start code:
63 tg.startCode(outs)
58 for ircode in c3b.build(srcs, imps): 64 for ircode in c3b.build(srcs, imps):
59 if not ircode: 65 if not ircode:
60 return 66 return
61 67
62 # Optimization passes, TODO 68 # Optimization passes, TODO
63 69
64 if dumpir: 70 if dumpir:
65 ircode.dump() 71 ircode.dump()
66 72
67 # Code generation: 73 # Code generation:
68 cg.generate(ircode) 74 cg.generate(ircode, outs)
75 # TODO: fixup references, do this in another way?
76 outs.backpatch()
77 outs.backpatch() # Why two times?
69 return c3b.ok 78 return c3b.ok
70 79
71 80
72 def main(args): 81 def main(args):
73 logging.basicConfig(format=logformat, level=args.log) 82 logging.basicConfig(format=logformat, level=args.log)
74 src = args.source 83 src = args.source
75 imps = args.imp 84 imps = args.imp
85 tg = targets[args.target]
76 diag = ppci.DiagnosticsManager() 86 diag = ppci.DiagnosticsManager()
77 outs = outstream.TextOutputStream() 87 outs = outstream.TextOutputStream()
78 88
79 # Invoke compiler: 89 res = zcc(src, imps, tg, outs, diag, dumpir=args.dumpir)
80 res = zcc(src, imps, outs, diag, dumpir=args.dumpir)
81 if not res: 90 if not res:
82 diag.printErrors(src) 91 diag.printErrors(src)
83 return 1 92 return 1
84 93
85 if args.dumpasm: 94 if args.dumpasm:
91 with open(output_filename, 'wb') as f: 100 with open(output_filename, 'wb') as f:
92 f.write(code_bytes) 101 f.write(code_bytes)
93 102
94 if args.hexfile: 103 if args.hexfile:
95 logging.info('Creating hexfile') 104 logging.info('Creating hexfile')
96 hf = hexfile.HexFile() 105 hf = HexFile()
97 hf.addRegion(0x08000000, code_bytes) 106 hf.addRegion(0x08000000, code_bytes)
98 hf.save(args.hexfile) 107 hf.save(args.hexfile)
99 return 0 108 return 0
100 109
101 110