comparison python/zcc.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents 4496cae24d7f
children a747a45dcd78
comparison
equal deleted inserted replaced
286:d9df72971cbf 287:1c7c1e619be8
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import sys, argparse 3 import sys
4 import c3, ppci, codegen 4 import argparse
5 import logging
6
7 import c3
8 import ppci
9 import codegen
5 import codegenarm 10 import codegenarm
6 from optimize import optimize 11 from optimize import optimize
7 import outstream 12 import outstream
8 import hexfile 13 import hexfile
9 import logging
10 14
11 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' 15 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
12 16
13 def logLevel(s): 17 def logLevel(s):
14 numeric_level = getattr(logging, s.upper(), None) 18 numeric_level = getattr(logging, s.upper(), None)
16 raise ValueError('Invalid log level: {}'.format(s)) 20 raise ValueError('Invalid log level: {}'.format(s))
17 return numeric_level 21 return numeric_level
18 22
19 # Parse arguments: 23 # Parse arguments:
20 parser = argparse.ArgumentParser(description='lcfos Compiler') 24 parser = argparse.ArgumentParser(description='lcfos Compiler')
25 # Input:
21 parser.add_argument('source', type=argparse.FileType('r'), \ 26 parser.add_argument('source', type=argparse.FileType('r'), \
22 help='the source file to build') 27 help='the source file to build', nargs="+")
28 parser.add_argument('-i', '--import', type=argparse.FileType('r'), \
29 help='Possible import module', action='append')
30
23 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") 31 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
24 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") 32 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code")
25 parser.add_argument('--optimize', action='store_true', help="Optimize") 33 parser.add_argument('--optimize', action='store_true', help="Optimize")
26 parser.add_argument('--package_dir', help="Look in this directory for packages") 34 parser.add_argument('--target', help="Backend selection")
27 parser.add_argument('-o', '--output', help='Output file', metavar='filename') 35 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
28 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w')) 36 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
29 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) 37 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
30 38
31 def zcc(src, outs, diag, dumpir=False, do_optimize=False, pack_dir=None): 39 def zcc(srcs, outs, diag, dumpir=False, do_optimize=False):
40 """
41 Compile sources into output stream.
42 Sources is an iterable of open files.
43 """
32 logging.info('Zcc started') 44 logging.info('Zcc started')
33 # Front end: 45 # Front end:
34 c3b = c3.Builder(diag) 46 c3b = c3.Builder(diag)
35 ircode = c3b.build(src, pack_dir=pack_dir) 47 imps = []
36 if not ircode: 48 for ircode in c3b.build(srcs, imps):
37 return 49 print(ircode)
50 if not ircode:
51 return
38 52
39 # Optimization passes: 53 # Optimization passes:
40 if do_optimize: 54 if do_optimize:
41 optimize(ircode) 55 optimize(ircode)
42 56
43 if dumpir: 57 if dumpir:
44 ircode.dump() 58 ircode.dump()
45 59
46 # Code generation: 60 # Code generation:
47 cg = codegenarm.ArmCodeGenerator(outs) 61 cg = codegenarm.ArmCodeGenerator(outs)
48 obj = cg.generate(ircode) 62 obj = cg.generate(ircode)
49 return True 63 return True
50 64
51 def main(args): 65 def main(args):
52 logging.basicConfig(format=logformat, level=args.log) 66 logging.basicConfig(format=logformat, level=args.log)
53 src = args.source.read() 67 src = args.source
54 args.source.close()
55 diag = ppci.DiagnosticsManager() 68 diag = ppci.DiagnosticsManager()
56 outs = outstream.TextOutputStream() 69 outs = outstream.TextOutputStream()
57 70
58 # Invoke compiler: 71 # Invoke compiler:
59 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize, pack_dir=args.package_dir) 72 res = zcc(src, outs, diag, dumpir=args.dumpir, do_optimize=args.optimize)
60 if not res: 73 if not res:
61 diag.printErrors(src) 74 diag.printErrors(src)
62 return 1 75 return 1
63 76
64 if args.dumpasm: 77 if args.dumpasm:
65 outs.dump() 78 outs.dump()
66 79
67 code_bytes = outs.sections['code'].to_bytes() 80 code_bytes = outs.sections['code'].to_bytes()
68 #print('bytes:', code_bytes)
69 if args.output: 81 if args.output:
70 output_filename = args.output 82 output_filename = args.output
71 else: 83 with open(output_filename, 'wb') as f:
72 output_filename = 'b.output' 84 f.write(code_bytes)
73
74 with open(output_filename, 'wb') as f:
75 f.write(code_bytes)
76 85
77 if args.hexfile: 86 if args.hexfile:
78 logging.info('Creating hexfile') 87 logging.info('Creating hexfile')
79 hf = hexfile.HexFile() 88 hf = hexfile.HexFile()
80 hf.addRegion(0x08000000, code_bytes) 89 hf.addRegion(0x08000000, code_bytes)
81 hf.save(args.hexfile) 90 hf.save(args.hexfile)
82 return 0 91 return 0
83 92
84 if __name__ == '__main__': 93 if __name__ == '__main__':
85 arguments = parser.parse_args() 94 arguments = parser.parse_args()
95 print(arguments)
86 sys.exit(main(arguments)) 96 sys.exit(main(arguments))
87 97