annotate python/zcc.py @ 105:6a303f835c6d

Removed compilers directory
author Windel Bouwman
date Mon, 31 Dec 2012 17:35:17 +0100
parents ed230e947dc6
children f2d980eef509
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 #!/usr/bin/python
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
3 import sys, os, argparse
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
4 from ppci import core
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
5
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
6 parser = argparse.ArgumentParser(description='K# to bitcode compiler')
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
7 parser.add_argument('source', type=str, help='the source file to build')
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
8 args = parser.parse_args()
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
9
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
10 try:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
11 with open(args.source, 'r') as f:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
12 src = f.read()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
13 except IOError:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
14 print('Failed to load {0}'.format(args.source))
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
15 sys.exit(1)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
16
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
17 # Create a context and a frontend:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
18 context = core.Context()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
19 frontend = core.frontends.ks.KsFrontend(context)
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
20 try:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
21 module = frontend.compilesource(src)
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
22 except core.CompilerException as e:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
23 print(e)
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
24 sys.exit(2)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
25
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
26 # optionally run passes here:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
27 # TODO
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
28
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
29 # Generate code:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
30 bitcodeWriter = core.BitcodeWriter()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
31 with open(args.source + '.bc', 'wb') as f:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
32 bitcodeWriter.WriteModuleToFile(module, f)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
33
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
34
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
35