view python/zcc.py @ 107:1544e7a4aa98

Improvements
author Windel Bouwman
date Tue, 01 Jan 2013 17:16:05 +0100
parents f2d980eef509
children de3a68f677a5
line wrap: on
line source

#!/usr/bin/python

import sys, os, argparse
from ppci import core, frontends

parser = argparse.ArgumentParser(description='K# to bitcode compiler')
parser.add_argument('source', type=str, help='the source file to build')
args = parser.parse_args()

try:
   with open(args.source, 'r') as f:
      src = f.read()
except IOError:
   print('Failed to load {0}'.format(args.source))
   sys.exit(1)
print('stage 1: Parsing')
# Create a context and a frontend:
context = core.Context()
frontend = frontends.KsFrontend(context)
try:
   module = frontend.compilesource(src)
except core.CompilerException as e:
   print(e)
   lines = src.split(os.linesep)
   row = e.row
   col = e.col
   line = lines[row - 1]
   print('{0}:{1}'.format(row, line))
   print(' ' * col + '^')
   raise
   #sys.exit(2)

# optionally run passes here:
# TODO

print('stage 3: Code generation')
asmWriter = core.AsmWriter()
asmWriter.printModule(module)

# Generate code:
bitcodeWriter = core.BitcodeWriter()
with open(args.source + '.bc', 'wb') as f:
   bitcodeWriter.WriteModuleToFile(module, f)