annotate python/zcc.py @ 195:37ac6c016e0f

Expanded asm subsystem
author Windel Bouwman
date Fri, 31 May 2013 21:06:44 +0200
parents 1544e7a4aa98
children de3a68f677a5
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
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
4 from ppci import core, frontends
105
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)
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
16 print('stage 1: Parsing')
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()
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
19 frontend = frontends.KsFrontend(context)
105
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)
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
24 lines = src.split(os.linesep)
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
25 row = e.row
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
26 col = e.col
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
27 line = lines[row - 1]
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
28 print('{0}:{1}'.format(row, line))
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
29 print(' ' * col + '^')
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
30 raise
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
31 #sys.exit(2)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
32
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
33 # optionally run passes here:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
34 # TODO
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
35
106
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
36 print('stage 3: Code generation')
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
37 asmWriter = core.AsmWriter()
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
38 asmWriter.printModule(module)
f2d980eef509 improved code generation
Windel Bouwman
parents: 105
diff changeset
39
105
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
40 # Generate code:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
41 bitcodeWriter = core.BitcodeWriter()
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
42 with open(args.source + '.bc', 'wb') as f:
6a303f835c6d Removed compilers directory
Windel Bouwman
parents: 104
diff changeset
43 bitcodeWriter.WriteModuleToFile(module, f)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
44