comparison python/zcc.py @ 315:084cccaa5deb

Added console and screen
author Windel Bouwman
date Sat, 21 Dec 2013 10:03:01 +0100
parents 38f5f298ce0e
children 56e6ff84f646
comparison
equal deleted inserted replaced
314:38f5f298ce0e 315:084cccaa5deb
39 print('', file=f) 39 print('', file=f)
40 print('', file=f) 40 print('', file=f)
41 print('.. code::', file=f) 41 print('.. code::', file=f)
42 print('', file=f) 42 print('', file=f)
43 AstPrinter().printAst(record.c3_ast, f) 43 AstPrinter().printAst(record.c3_ast, f)
44 #Writer(' ').write(record.c3_ast, f)
45 print('', file=f) 44 print('', file=f)
46 s += '\n' + f.getvalue() 45 s += '\n' + f.getvalue()
47 if hasattr(record, 'ircode'): 46 if hasattr(record, 'ircode'):
48 f = io.StringIO() 47 f = io.StringIO()
49 print('', file=f) 48 print('', file=f)
110 parser.add_argument('source', type=argparse.FileType('r'), \ 109 parser.add_argument('source', type=argparse.FileType('r'), \
111 help='the source file to build', nargs="+") 110 help='the source file to build', nargs="+")
112 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \ 111 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
113 help='Possible import module', action='append', default=[]) 112 help='Possible import module', action='append', default=[])
114 113
115 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
116 parser.add_argument('--optimize', action='store_true', help="Optimize") 114 parser.add_argument('--optimize', action='store_true', help="Optimize")
117 parser.add_argument('--target', help="Backend selection", 115 parser.add_argument('--target', help="Backend selection",
118 choices=targetnames, required=True) 116 choices=targetnames, required=True)
119 parser.add_argument('-o', '--output', help='Output file', metavar='filename') 117 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
120 parser.add_argument('--hexfile', help='Output hexfile', 118 parser.add_argument('--hexfile', help='Output hexfile',
121 type=argparse.FileType('w')) 119 type=argparse.FileType('w'))
122 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) 120 parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])',
123 121 type=logLevel, default='WARN')
124 122 parser.add_argument('--report', help='Specify a file to write the compile report to',
125 def zcc(srcs, imps, tg, outs, diag, dumpir=False): 123 type=argparse.FileType('w'))
124
125
126 def zcc(srcs, imps, tg, outs, diag):
126 """ 127 """
128 Compiler driver
127 Compile sources into output stream. 129 Compile sources into output stream.
128 Sources is an iterable of open files. 130 Sources is an iterable of open files.
129 """ 131 """
130 logging.info('Zcc started') 132 logging.info('Zcc started {}'.format(srcs))
131 # Front end: 133 # Front end:
132 c3b = Builder(diag, tg) 134 c3b = Builder(diag, tg)
133 cg = CodeGenerator(tg) 135 cg = CodeGenerator(tg)
134 136
135 # TODO: remove this arm specifics: 137 # TODO: remove this arm specifics:
156 outs.backpatch() # Why two times? 158 outs.backpatch() # Why two times?
157 return c3b.ok 159 return c3b.ok
158 160
159 161
160 def main(args): 162 def main(args):
161 #logging.getLogger().setLevel(logging.DEBUG) 163 # Configure some logging:
162 #logging.getLogger().addHandler(RstLogHandler()) 164 logging.getLogger().setLevel(logging.DEBUG)
163 #fh = logging.FileHandler('log.rst', mode='w') 165 ch = logging.StreamHandler()
164 #fh.setFormatter(RstFormatter()) 166 ch.setFormatter(logging.Formatter(logformat))
165 #logging.getLogger().addHandler(fh) 167 ch.setLevel(args.log)
168 logging.getLogger().addHandler(ch)
169 if args.report:
170 fh = logging.StreamHandler(stream=args.report)
171 fh.setFormatter(RstFormatter())
172 logging.getLogger().addHandler(fh)
166 173
167 tg = targets[args.target] 174 tg = targets[args.target]
168 diag = ppci.DiagnosticsManager() 175 diag = ppci.DiagnosticsManager()
169 outs = outstream.TextOutputStream() 176 outs = outstream.TextOutputStream()
170 177
171 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) 178 res = zcc(args.source, args.imp, tg, outs, diag)
172 if not res: 179 if not res:
173 diag.printErrors() 180 diag.printErrors()
174 return 1 181 return 1
175 182
176 logging.info('Assembly created', extra={'zcc_outs':outs}) 183 logging.info('Assembly created', extra={'zcc_outs':outs})
184 if args.hexfile: 191 if args.hexfile:
185 logging.info('Creating hexfile') 192 logging.info('Creating hexfile')
186 hf = HexFile() 193 hf = HexFile()
187 hf.addRegion(0x08000000, code_bytes) 194 hf.addRegion(0x08000000, code_bytes)
188 hf.save(args.hexfile) 195 hf.save(args.hexfile)
196
197 if args.report:
198 logging.getLogger().removeHandler(fh)
199 logging.getLogger().removeHandler(ch)
189 return 0 200 return 0
190 201
191 202
192 if __name__ == '__main__': 203 if __name__ == '__main__':
193 arguments = parser.parse_args() 204 arguments = parser.parse_args()