view python/zcc.py @ 105:6a303f835c6d

Removed compilers directory
author Windel Bouwman
date Mon, 31 Dec 2012 17:35:17 +0100
parents ed230e947dc6
children f2d980eef509
line wrap: on
line source

#!/usr/bin/python

import sys, os, argparse
from ppci import core

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)

# Create a context and a frontend:
context = core.Context()
frontend = core.frontends.ks.KsFrontend(context)
try:
   module = frontend.compilesource(src)
except core.CompilerException as e:
   print(e)
   sys.exit(2)

# optionally run passes here:
# TODO

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