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