104
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys, os, argparse
|
106
|
4 from ppci import core, frontends
|
105
|
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)
|
106
|
16 print('stage 1: Parsing')
|
105
|
17 # Create a context and a frontend:
|
|
18 context = core.Context()
|
106
|
19 frontend = frontends.KsFrontend(context)
|
105
|
20 try:
|
|
21 module = frontend.compilesource(src)
|
|
22 except core.CompilerException as e:
|
|
23 print(e)
|
106
|
24 lines = src.split(os.linesep)
|
|
25 row = e.row
|
|
26 col = e.col
|
|
27 line = lines[row - 1]
|
|
28 print('{0}:{1}'.format(row, line))
|
|
29 print(' ' * col + '^')
|
|
30 raise
|
|
31 #sys.exit(2)
|
104
|
32
|
105
|
33 # optionally run passes here:
|
|
34 # TODO
|
|
35
|
106
|
36 print('stage 3: Code generation')
|
|
37 asmWriter = core.AsmWriter()
|
|
38 asmWriter.printModule(module)
|
|
39
|
105
|
40 # Generate code:
|
|
41 bitcodeWriter = core.BitcodeWriter()
|
|
42 with open(args.source + '.bc', 'wb') as f:
|
|
43 bitcodeWriter.WriteModuleToFile(module, f)
|
104
|
44
|