From 38d4de35bba4bf306476ce872ae62a0a7e0eeeda Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Sun, 26 Aug 2012 21:00:29 -0700 Subject: [PATCH] Make it possible to pass LLVM options from environment variables. These options are parsed like commandline options to llc. --- llvm/_core.cpp | 18 ++++++++++++++++++ llvm/core.py | 4 +++- test/native_asm.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/native_asm.py diff --git a/llvm/_core.cpp b/llvm/_core.cpp index dd86745..77ded24 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -43,6 +43,8 @@ #include "llvm-c/ExecutionEngine.h" #include "llvm-c/Target.h" #include "llvm-c/Transforms/IPO.h" + +#include "llvm/Support/CommandLine.h" #if LLVM_VERSION_MAJOR >= 3 && LLVM_VERSION_MINOR >= 2 #include "llvm-c/Linker.h" #else @@ -1413,6 +1415,20 @@ _wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args) Py_RETURN_NONE; } +static PyObject * +_wLLVMParseEnvOpts(PyObject *self, PyObject *args) +{ + const char * progname; + const char * envname; + if(!PyArg_ParseTuple(args, "ss", &progname, &envname)) { + return NULL; + } + + llvm::cl::ParseEnvironmentOptions(progname, envname); + + Py_RETURN_NONE; +} + //_wrap_obj2obj(LLVMInlineFunction, LLVMValueRef, int) /* Expose the void* inside a PyCObject as a PyLong. This allows us to @@ -2015,6 +2031,8 @@ static PyMethodDef core_methods[] = { /* Misc */ _method( LLVMGetIntrinsic ) _method( LLVMLoadLibraryPermanently ) + _method( LLVMParseEnvOpts ) + //_method( LLVMInlineFunction ) _method( PyCObjectVoidPtrToPyLong ) { NULL } diff --git a/llvm/core.py b/llvm/core.py index b8cbe32..a9c90c8 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -2147,7 +2147,8 @@ def inline_function(call): check_is_value(call) return _core.LLVMInlineFunction(call.ptr) - +def parse_environment_options(progname, envname): + _core.LLVMParseEnvOpts(progname, envname) #===----------------------------------------------------------------------=== # Initialization @@ -2178,3 +2179,4 @@ if True: # use PTX? except AttributeError: pass + diff --git a/test/native_asm.py b/test/native_asm.py new file mode 100644 index 0000000..ca9c87c --- /dev/null +++ b/test/native_asm.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +from llvm import * +from llvm.core import * +import sys, os +import unittest + +class TestNativeAsm(unittest.TestCase): + def test_asm(self): + # create a module + m = Module.new('module1') + + foo = m.add_function(Type.function(Type.int(), [Type.int(), Type.int()]), name="foo") + + bldr = Builder.new(foo.append_basic_block('entry')) + x = bldr.add(foo.args[0], foo.args[1]) + bldr.ret(x) + + att_syntax = m.to_native_assembly() + os.environ["LLVMPY_OPTIONS"] = "-x86-asm-syntax=intel" + parse_environment_options(sys.argv[0], "LLVMPY_OPTIONS") + intel_syntax = m.to_native_assembly() + + print(att_syntax) + print(intel_syntax) + self.assertNotEqual(att_syntax, intel_syntax) + +if __name__ == '__main__': + unittest.main()