Mercurial > lcfOS
comparison python/zcc.py @ 290:7b38782ed496
File moves
author | Windel Bouwman |
---|---|
date | Sun, 24 Nov 2013 11:24:15 +0100 |
parents | bd2593de3ff8 |
children | 534b94b40aa8 |
comparison
equal
deleted
inserted
replaced
289:bd2593de3ff8 | 290:7b38782ed496 |
---|---|
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 codegenarm | |
11 from optimize import optimize | |
12 import outstream | 10 import outstream |
13 import hexfile | 11 import hexfile |
12 import target | |
14 | 13 |
15 | 14 |
16 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' | 15 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' |
17 | 16 |
18 | 17 |
21 if not isinstance(numeric_level, int): | 20 if not isinstance(numeric_level, int): |
22 raise ValueError('Invalid log level: {}'.format(s)) | 21 raise ValueError('Invalid log level: {}'.format(s)) |
23 return numeric_level | 22 return numeric_level |
24 | 23 |
25 | 24 |
25 target_list = [target.armtarget] | |
26 targetnames = {t.name: t for t in target_list} | |
27 | |
26 # Parse arguments: | 28 # Parse arguments: |
27 parser = argparse.ArgumentParser(description='lcfos Compiler') | 29 parser = argparse.ArgumentParser(description='lcfos Compiler') |
28 # Input: | 30 # Input: |
29 parser.add_argument('source', type=argparse.FileType('r'), \ | 31 parser.add_argument('source', type=argparse.FileType('r'), \ |
30 help='the source file to build', nargs="+") | 32 help='the source file to build', nargs="+") |
31 parser.add_argument('-i', '--import', type=argparse.FileType('r'), \ | 33 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \ |
32 help='Possible import module', action='append') | 34 help='Possible import module', action='append') |
33 | 35 |
34 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") | 36 parser.add_argument('--dumpir', action='store_true', help="Dump IR-code") |
35 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") | 37 parser.add_argument('--dumpasm', action='store_true', help="Dump ASM-code") |
36 parser.add_argument('--optimize', action='store_true', help="Optimize") | 38 parser.add_argument('--optimize', action='store_true', help="Optimize") |
37 parser.add_argument('--target', help="Backend selection") | 39 parser.add_argument('--target', help="Backend selection", |
40 choices=targetnames.keys()) | |
38 parser.add_argument('-o', '--output', help='Output file', metavar='filename') | 41 parser.add_argument('-o', '--output', help='Output file', metavar='filename') |
39 parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w')) | 42 parser.add_argument('--hexfile', help='Output hexfile', |
43 type=argparse.FileType('w')) | |
40 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) | 44 parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel) |
41 | 45 |
42 | 46 |
43 def zcc(srcs, imps, outs, diag, dumpir=False): | 47 def zcc(srcs, imps, outs, diag, dumpir=False): |
44 """ | 48 """ |
46 Sources is an iterable of open files. | 50 Sources is an iterable of open files. |
47 """ | 51 """ |
48 logging.info('Zcc started') | 52 logging.info('Zcc started') |
49 # Front end: | 53 # Front end: |
50 c3b = c3.Builder(diag) | 54 c3b = c3.Builder(diag) |
55 tg = target.armtarget.armtarget | |
56 # TODO select target here! | |
57 cg = codegen.CodeGenerator(outs, tg) | |
51 for ircode in c3b.build(srcs, imps): | 58 for ircode in c3b.build(srcs, imps): |
52 if not ircode: | 59 if not ircode: |
53 return | 60 return |
54 | 61 |
55 # Optimization passes: | 62 # Optimization passes, TODO |
56 optimize(ircode) | |
57 | 63 |
58 if dumpir: | 64 if dumpir: |
59 ircode.dump() | 65 ircode.dump() |
60 | 66 |
61 # TODO select target here! | |
62 # Code generation: | 67 # Code generation: |
63 codegenarm.ArmCodeGenerator(outs).generate(ircode) | 68 cg.generate(ircode) |
64 return c3b.ok | 69 return c3b.ok |
65 | 70 |
66 | 71 |
67 def main(args): | 72 def main(args): |
68 logging.basicConfig(format=logformat, level=args.log) | 73 logging.basicConfig(format=logformat, level=args.log) |
69 src = args.source | 74 src = args.source |
70 imps = getattr(args, 'import') | 75 imps = args.imp |
71 diag = ppci.DiagnosticsManager() | 76 diag = ppci.DiagnosticsManager() |
72 outs = outstream.TextOutputStream() | 77 outs = outstream.TextOutputStream() |
73 | 78 |
74 # Invoke compiler: | 79 # Invoke compiler: |
75 res = zcc(src, imps, outs, diag, dumpir=args.dumpir) | 80 res = zcc(src, imps, outs, diag, dumpir=args.dumpir) |