Mercurial > lcfOS
comparison python/zcc.py @ 313:04cf4d26a3bc
Added constant function
author | Windel Bouwman |
---|---|
date | Wed, 18 Dec 2013 18:02:26 +0100 |
parents | 2c9768114877 |
children | 38f5f298ce0e |
comparison
equal
deleted
inserted
replaced
312:2c9768114877 | 313:04cf4d26a3bc |
---|---|
2 | 2 |
3 import sys | 3 import sys |
4 import argparse | 4 import argparse |
5 import logging | 5 import logging |
6 | 6 |
7 from ppci.c3 import Builder | 7 from ppci.c3 import Builder, AstPrinter |
8 import ppci | 8 import ppci |
9 from ppci.irutils import Verifier, Writer | 9 from ppci.irutils import Verifier, Writer |
10 from ppci.codegen import CodeGenerator | 10 from ppci.codegen import CodeGenerator |
11 import outstream | 11 import outstream |
12 from utils import HexFile | 12 from utils import HexFile |
31 def __init__(self): | 31 def __init__(self): |
32 super().__init__(fmt=logformat) | 32 super().__init__(fmt=logformat) |
33 | 33 |
34 def format(self, record): | 34 def format(self, record): |
35 s = super().format(record) | 35 s = super().format(record) |
36 if hasattr(record, 'c3_ast'): | |
37 f = io.StringIO() | |
38 print('', file=f) | |
39 print('', file=f) | |
40 print('.. code::', file=f) | |
41 print('', file=f) | |
42 AstPrinter().printAst(record.c3_ast, f) | |
43 #Writer(' ').write(record.c3_ast, f) | |
44 print('', file=f) | |
45 s += '\n' + f.getvalue() | |
36 if hasattr(record, 'ircode'): | 46 if hasattr(record, 'ircode'): |
37 f = io.StringIO() | 47 f = io.StringIO() |
38 print('', file=f) | 48 print('', file=f) |
39 print('', file=f) | 49 print('', file=f) |
40 print('.. code::', file=f) | 50 print('.. code::', file=f) |
88 help='the source file to build', nargs="+") | 98 help='the source file to build', nargs="+") |
89 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \ | 99 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \ |
90 help='Possible import module', action='append', default=[]) | 100 help='Possible import module', action='append', default=[]) |
91 | 101 |
92 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") | 102 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") |
93 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") | |
94 parser.add_argument('--optimize', action='store_true', help="Optimize") | 103 parser.add_argument('--optimize', action='store_true', help="Optimize") |
95 parser.add_argument('--target', help="Backend selection", | 104 parser.add_argument('--target', help="Backend selection", |
96 choices=targetnames, required=True) | 105 choices=targetnames, required=True) |
97 parser.add_argument('-o', '--output', help='Output file', metavar='filename') | 106 parser.add_argument('-o', '--output', help='Output file', metavar='filename') |
98 parser.add_argument('--hexfile', help='Output hexfile', | 107 parser.add_argument('--hexfile', help='Output hexfile', |
117 # Emit some custom start code: | 126 # Emit some custom start code: |
118 tg.startCode(outs) | 127 tg.startCode(outs) |
119 for ircode in c3b.build(srcs, imps): | 128 for ircode in c3b.build(srcs, imps): |
120 if not ircode: | 129 if not ircode: |
121 return | 130 return |
131 | |
132 d = {'ircode':ircode} | |
133 logging.info('Verifying code {}'.format(ircode), extra=d) | |
122 # Optimization passes, TODO | 134 # Optimization passes, TODO |
123 Verifier().verify(ircode) | 135 Verifier().verify(ircode) |
124 | |
125 if dumpir: | |
126 f = io.StringIO() | |
127 irutils.Writer().write(ircode, f) | |
128 print(f.getvalue()) | |
129 | 136 |
130 # Code generation: | 137 # Code generation: |
131 d = {'ircode':ircode} | 138 d = {'ircode':ircode} |
132 logging.info('Starting code generation for {}'.format(ircode), extra=d) | 139 logging.info('Starting code generation for {}'.format(ircode), extra=d) |
133 cg.generate(ircode, outs) | 140 cg.generate(ircode, outs) |
136 outs.backpatch() # Why two times? | 143 outs.backpatch() # Why two times? |
137 return c3b.ok | 144 return c3b.ok |
138 | 145 |
139 | 146 |
140 def main(args): | 147 def main(args): |
141 logging.basicConfig(format=logformat, level=args.log) | 148 #logging.getLogger().setLevel(logging.DEBUG) |
142 #logging.getLogger().addHandler(RstLogHandler()) | 149 #logging.getLogger().addHandler(RstLogHandler()) |
143 fh = logging.FileHandler('log.rst', mode='w') | 150 #fh = logging.FileHandler('log.rst', mode='w') |
144 fh.setFormatter(RstFormatter()) | 151 #fh.setFormatter(RstFormatter()) |
145 logging.getLogger().addHandler(fh) | 152 #logging.getLogger().addHandler(fh) |
146 | 153 |
147 tg = targets[args.target] | 154 tg = targets[args.target] |
148 diag = ppci.DiagnosticsManager() | 155 diag = ppci.DiagnosticsManager() |
149 outs = outstream.TextOutputStream() | 156 outs = outstream.TextOutputStream() |
150 | 157 |
151 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) | 158 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) |
152 if not res: | 159 if not res: |
153 diag.printErrors() | 160 diag.printErrors() |
154 return 1 | 161 return 1 |
155 | 162 |
156 if args.dumpasm: | 163 logging.info('Assembly created', extra={'zcc_outs':outs}) |
157 outs.dump() | |
158 | 164 |
159 code_bytes = outs.sections['code'].to_bytes() | 165 code_bytes = outs.sections['code'].to_bytes() |
160 if args.output: | 166 if args.output: |
161 output_filename = args.output | 167 output_filename = args.output |
162 with open(output_filename, 'wb') as f: | 168 with open(output_filename, 'wb') as f: |