|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This is the assembler phase. |
| 4 | +
|
| 5 | +This variant is only invoked during |
| 6 | +the second compilation where we are building bitcode. The compiler |
| 7 | +has already been instructed to generate LLVM IR; the compiler then |
| 8 | +tries to assemble it into an object file. The standard assembler |
| 9 | +doesn't understand LLVM bitcode, so we interpose and use the llvm-as |
| 10 | +command to build a bitcode file. We leave the bitcode in place, but |
| 11 | +record its full absolute path in the corresponding object file |
| 12 | +(which was created in the first compilation phase by the real |
| 13 | +compiler). We'll link this together at a later stage. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | +import sys |
| 18 | +import os |
| 19 | + |
| 20 | +from subprocess import * |
| 21 | + |
| 22 | +from wllvm.utils import * |
| 23 | + |
| 24 | +from wllvm.popenwrapper import Popen |
| 25 | + |
| 26 | +import logging |
| 27 | +logging.basicConfig() |
| 28 | + |
| 29 | +class BCFilter(ArgumentListFilter): |
| 30 | + def __init__(self, arglist): |
| 31 | + self.bcName = None |
| 32 | + self.outFileName = None |
| 33 | + localCallbacks = { '-o' : (1, BCFilter.outFileCallback) } |
| 34 | + super(BCFilter, self).__init__(arglist, exactMatches=localCallbacks) |
| 35 | + |
| 36 | + def outFileCallback(self, flag, name): |
| 37 | + self.outFileName = name |
| 38 | + |
| 39 | +def main(): |
| 40 | + |
| 41 | + argFilter = BCFilter(sys.argv[1:]) |
| 42 | + # Since this is just the assembler, there should only ever be one file |
| 43 | + try: |
| 44 | + [infile] = argFilter.inputFiles |
| 45 | + except ValueError: |
| 46 | + logging.debug('Input file argument not detected, assuming stdin.') |
| 47 | + infile = "-" |
| 48 | + |
| 49 | + # set llvm-as |
| 50 | + llvmAssembler='llvm-as' |
| 51 | + if os.getenv(llvmCompilerPathEnv): |
| 52 | + llvmAssembler = os.path.join(os.getenv(llvmCompilerPathEnv), llvmAssembler) |
| 53 | + |
| 54 | + # Now compile this llvm assembly file into a bitcode file. The output |
| 55 | + # filename is the same as the object with a .bc appended |
| 56 | + try: |
| 57 | + (dirs, filename) = os.path.split(argFilter.outFileName) |
| 58 | + except AttributeError as e: |
| 59 | + logging.error('Output file argument not found.\nException message: ' + str(e)) |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + fakeAssembler = [llvmAssembler, infile, '-o', argFilter.outFileName] |
| 63 | + |
| 64 | + asmProc = Popen(fakeAssembler) |
| 65 | + realRet = asmProc.wait() |
| 66 | + |
| 67 | + if realRet != 0: |
| 68 | + logging.error('llvm-as failed') |
| 69 | + sys.exit(realRet) |
| 70 | + |
| 71 | + sys.exit(realRet) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + sys.exit(main()) |
0 commit comments