[![PyPI](https://img.shields.io/pypi/v/llvm_python.svg)](https://pypi.python.org/pypi/llvm_python)
[![Actions status](https://github.com/Papr1ka/llvm_python/actions/workflows/build_wheels.yml/badge.svg?branch=main)](https://github.com/Papr1ka/llvm_python/actions/workflows/build_wheels.yml)
[![release](https://img.shields.io/github/v/release/Papr1ka/llvm_python.svg?label=release)](https://github.com/Papr1ka/llvm_python/releases)
# llvm_python
A fairly large proportion of programs are written in C/C++.
Let's imagine that you need to analyze programs in a given language, to search for vulnerabilities, to detect patterns or for optimization purposes.
To solve such a problem, it is necessary to have the code in a slightly more convenient form than the source code of the program - an intermediate representation.
You might come up with the idea of building your own compiler, which is quite difficult, or you might decide to use intermediate representations of GCC or LLVM, but in that case you have to deal with the C/C++ API, which is something you don't want when you have elegant solutions in Python.
**llvm_python** allows you to analyze C/C++ programs in the LLVM IR representation in Python.
# Usage example
The following example will build a control flow graph for the following function [factorial.c](./test_files/factorial.c), [factorial.ll](./test_files/factorial.ll).
```cpp
int factorial_req(int n)
{
if (n == 1)
{
return 1;
}
return factorial_req(n - 1) * n;
}
```
```python
from llvm_python import ir
from llvm_python import parse_assembly
from graphviz import Digraph
with open("../test_files/factorial.ll") as file:
ll = file.read()
mod: ir.Module = parse_assembly(ll)
g = Digraph()
node_attributes = {
"shape": "record",
"style": "filled"
}
def graph_node(node: ir.Value):
text = str(node).replace("\n", "\l")
return node.name[1:], node.name + ":" + text
for block in mod.get_function("factorial_req").blocks:
color = "#f59c7d70" # Default block color
last_instruction = block.instructions[-1]
if last_instruction.op_code_name == "br":
operands = last_instruction.operands
if len(operands) == 3:
g.edge(block.name[1:], operands[1].name[1:], label="True")
g.edge(block.name[1:], operands[2].name[1:], label="False")
color = "#b70d2870" # switch-type block
else:
g.edge(block.name[1:], operands[0].name[1:])
if len(block.pred_blocks) >= 2:
color = "#b70d2870" # merge-type block
g.node(*graph_node(block), **node_attributes, color=color)
g.save("cfg.dot")
```
# Installation
## Preferred way
`pip install llvm_python`
Supported versions
CPython3.7 - CPython3.12
On Windows 64bit and manylinux x86_64 platforms.
## Manual installation
1. Dependencies
* For correct build you need **CMake >= 3.27**
* C and C++ compilers.
* The system must also have libraries `libffi` и `libtinfo`.
> If the libraries are not found, you can add them manually, example [CMakeLists.txt](./CMakeLists.txt), lines 12-15.
* Preferred build system **Ninja**
2. Cloning llvm_python
`git clone git@github.com:Papr1ka/llvm_python.git`
`cd llvm_python`
3. LLVM setup
You must have the static library **LLVM** version >= 16 installed on the system.
### This can be done via the distribution's package manager.
Example:
`sudo dnf install llvm`
`sudo dnf install llvm-devel`
### Or you can build LLVM manually and specify the path to the cmake folder, line 21
`LLVM_DIR = "/usr/lib/llvm-18/cmake" # Path to cmake folder of llvm library`
### Or you can download compiled libraries
* Deb пакеты https://apt.llvm.org/
* Windows https://packages.msys2.org/package/mingw-w64-x86_64-llvm
4. Run setup.py
in the directory with setup.py:
`python -m pip install .`
# Contact
For any questions related to the library, you can contact us by mail
``
Raw data
{
"_id": null,
"home_page": "https://github.com/Papr1ka/llvm_python",
"name": "llvm-python",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "llvm, analysis, ir, intermediate, presentation, representation, compiler, parser",
"author": "Papr1ka",
"author_email": "kirillpavlov4214@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ac/95/b417537053fc5ebb8a97ea5ff6ddc44a2c0efc44e554529d42d60a8a6d7d/llvm_python-0.0.1b1.tar.gz",
"platform": null,
"description": "[![PyPI](https://img.shields.io/pypi/v/llvm_python.svg)](https://pypi.python.org/pypi/llvm_python)\n[![Actions status](https://github.com/Papr1ka/llvm_python/actions/workflows/build_wheels.yml/badge.svg?branch=main)](https://github.com/Papr1ka/llvm_python/actions/workflows/build_wheels.yml)\n[![release](https://img.shields.io/github/v/release/Papr1ka/llvm_python.svg?label=release)](https://github.com/Papr1ka/llvm_python/releases)\n\n\n# llvm_python\n\nA fairly large proportion of programs are written in C/C++.\n\nLet's imagine that you need to analyze programs in a given language, to search for vulnerabilities, to detect patterns or for optimization purposes.\nTo solve such a problem, it is necessary to have the code in a slightly more convenient form than the source code of the program - an intermediate representation.\n\nYou might come up with the idea of building your own compiler, which is quite difficult, or you might decide to use intermediate representations of GCC or LLVM, but in that case you have to deal with the C/C++ API, which is something you don't want when you have elegant solutions in Python.\n\n**llvm_python** allows you to analyze C/C++ programs in the LLVM IR representation in Python.\n\n# Usage example\n\nThe following example will build a control flow graph for the following function [factorial.c](./test_files/factorial.c), [factorial.ll](./test_files/factorial.ll).\n\n```cpp\nint factorial_req(int n)\n{\n if (n == 1)\n {\n return 1;\n }\n return factorial_req(n - 1) * n;\n}\n```\n\n```python\nfrom llvm_python import ir\nfrom llvm_python import parse_assembly\nfrom graphviz import Digraph\n\n\nwith open(\"../test_files/factorial.ll\") as file:\n ll = file.read()\n\nmod: ir.Module = parse_assembly(ll)\n\ng = Digraph()\n\nnode_attributes = {\n \"shape\": \"record\",\n \"style\": \"filled\"\n}\n\n\ndef graph_node(node: ir.Value):\n text = str(node).replace(\"\\n\", \"\\l\")\n return node.name[1:], node.name + \":\" + text\n\n\nfor block in mod.get_function(\"factorial_req\").blocks:\n color = \"#f59c7d70\" # Default block color\n last_instruction = block.instructions[-1]\n\n if last_instruction.op_code_name == \"br\":\n operands = last_instruction.operands\n if len(operands) == 3:\n g.edge(block.name[1:], operands[1].name[1:], label=\"True\")\n g.edge(block.name[1:], operands[2].name[1:], label=\"False\")\n color = \"#b70d2870\" # switch-type block\n else:\n g.edge(block.name[1:], operands[0].name[1:])\n if len(block.pred_blocks) >= 2:\n color = \"#b70d2870\" # merge-type block\n\n g.node(*graph_node(block), **node_attributes, color=color)\n\ng.save(\"cfg.dot\")\n```\n\n# Installation\n\n## Preferred way\n\n`pip install llvm_python`\n\nSupported versions\n\nCPython3.7 - CPython3.12\n\nOn Windows 64bit and manylinux x86_64 platforms.\n\n## Manual installation\n\n1. Dependencies\n\n* For correct build you need **CMake >= 3.27**\n\n* C and C++ compilers.\n\n* The system must also have libraries `libffi` \u0438 `libtinfo`.\n\n> If the libraries are not found, you can add them manually, example [CMakeLists.txt](./CMakeLists.txt), lines 12-15.\n\n* Preferred build system **Ninja**\n\n2. Cloning llvm_python\n\n`git clone git@github.com:Papr1ka/llvm_python.git`\n\n`cd llvm_python`\n\n3. LLVM setup\n\nYou must have the static library **LLVM** version >= 16 installed on the system.\n\n### This can be done via the distribution's package manager.\n\nExample:\n\n`sudo dnf install llvm`\n\n`sudo dnf install llvm-devel`\n\n### Or you can build LLVM manually and specify the path to the cmake folder, line 21\n\n`LLVM_DIR = \"/usr/lib/llvm-18/cmake\" # Path to cmake folder of llvm library`\n\n### Or you can download compiled libraries\n\n* Deb \u043f\u0430\u043a\u0435\u0442\u044b https://apt.llvm.org/\n* Windows https://packages.msys2.org/package/mingw-w64-x86_64-llvm\n\n4. Run setup.py\n\nin the directory with setup.py:\n\n`python -m pip install .`\n\n\n# Contact\n\nFor any questions related to the library, you can contact us by mail\n\n``\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A library for analyzing LLVM IR in Python",
"version": "0.0.1b1",
"project_urls": {
"Homepage": "https://github.com/Papr1ka/llvm_python"
},
"split_keywords": [
"llvm",
" analysis",
" ir",
" intermediate",
" presentation",
" representation",
" compiler",
" parser"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1aba98713db6a489920f834dc201aa571cb4d9751e7fe24f1423f052e7486c23",
"md5": "3afb7e4585af4b4e9bae3f0e15678031",
"sha256": "b4f5a3f22d30dee481694c5017c8d98c20674b59751e1d919f65f00735001707"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp310-cp310-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "3afb7e4585af4b4e9bae3f0e15678031",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.7",
"size": 2529112,
"upload_time": "2024-04-20T20:33:07",
"upload_time_iso_8601": "2024-04-20T20:33:07.100909Z",
"url": "https://files.pythonhosted.org/packages/1a/ba/98713db6a489920f834dc201aa571cb4d9751e7fe24f1423f052e7486c23/llvm_python-0.0.1b1-cp310-cp310-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "40833ab43785864e6e1bb6e74eccf702d398f9891dd27570834a5be03e71d6e4",
"md5": "5fe155cb9ac24fb7fe56feb3a73bde9d",
"sha256": "857551873ac2fca0da19565cfbaef10a9d47558e1f360b4410e053254143f911"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp310-cp310-win_amd64.whl",
"has_sig": false,
"md5_digest": "5fe155cb9ac24fb7fe56feb3a73bde9d",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.7",
"size": 2257940,
"upload_time": "2024-04-20T20:33:08",
"upload_time_iso_8601": "2024-04-20T20:33:08.591958Z",
"url": "https://files.pythonhosted.org/packages/40/83/3ab43785864e6e1bb6e74eccf702d398f9891dd27570834a5be03e71d6e4/llvm_python-0.0.1b1-cp310-cp310-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1521840c1e1dafdc66188135fa098419e6945a2d8862f1d2b39450c26eb06b91",
"md5": "6573e99463f1cc095b565279722c99e7",
"sha256": "923faa064883a08644c4e4f1ec34a21183d02109cbb77f173c5180ef57dd26fa"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp311-cp311-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "6573e99463f1cc095b565279722c99e7",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.7",
"size": 2529367,
"upload_time": "2024-04-20T20:33:10",
"upload_time_iso_8601": "2024-04-20T20:33:10.454396Z",
"url": "https://files.pythonhosted.org/packages/15/21/840c1e1dafdc66188135fa098419e6945a2d8862f1d2b39450c26eb06b91/llvm_python-0.0.1b1-cp311-cp311-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "80641d777a81949831e1e3fe95a060745c8f92219c6655f867748e95f491da69",
"md5": "cc27367124d1957674b191f7652fe8c5",
"sha256": "dcbec6184492289f39869ce840d015a09f51576fbadd209e007c12a70f5633ab"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp311-cp311-win_amd64.whl",
"has_sig": false,
"md5_digest": "cc27367124d1957674b191f7652fe8c5",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.7",
"size": 2257977,
"upload_time": "2024-04-20T20:33:12",
"upload_time_iso_8601": "2024-04-20T20:33:12.380528Z",
"url": "https://files.pythonhosted.org/packages/80/64/1d777a81949831e1e3fe95a060745c8f92219c6655f867748e95f491da69/llvm_python-0.0.1b1-cp311-cp311-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "efdb1bb7c8840d7163bfbb8764c54aad35fdac8bd685e03ac61d35bf32d31f96",
"md5": "3dde1330dc55825f61302c41735172a2",
"sha256": "faf6da1cb8dc6b85521aeb6a3dc4d73461c3b6837ccb8f20631fd41c814466a1"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp312-cp312-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "3dde1330dc55825f61302c41735172a2",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.7",
"size": 2529799,
"upload_time": "2024-04-20T20:33:13",
"upload_time_iso_8601": "2024-04-20T20:33:13.677770Z",
"url": "https://files.pythonhosted.org/packages/ef/db/1bb7c8840d7163bfbb8764c54aad35fdac8bd685e03ac61d35bf32d31f96/llvm_python-0.0.1b1-cp312-cp312-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "65a991020dd7d846da88135089be50edaa647eb5958f8162852b30ed773cf176",
"md5": "242aea8ceee78e05726fdfe0cb812f68",
"sha256": "e44af6d48f91c30ff9cb59bd3e9f5ff6dde8cf71c2a17370b28a5e1528ba7768"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp312-cp312-win_amd64.whl",
"has_sig": false,
"md5_digest": "242aea8ceee78e05726fdfe0cb812f68",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.7",
"size": 2258343,
"upload_time": "2024-04-20T20:33:15",
"upload_time_iso_8601": "2024-04-20T20:33:15.139251Z",
"url": "https://files.pythonhosted.org/packages/65/a9/91020dd7d846da88135089be50edaa647eb5958f8162852b30ed773cf176/llvm_python-0.0.1b1-cp312-cp312-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "666715e17ca40114bcc75aeedb393fc23a9fdd93a6e8279444edbad49d76b0e2",
"md5": "1ce56d4fda6cd54ab3c3bef59a8e425f",
"sha256": "57e3971fc045b681997ec64e629d11c76fecf22f4a631f2a243e4fb8e83f0c03"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp37-cp37m-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "1ce56d4fda6cd54ab3c3bef59a8e425f",
"packagetype": "bdist_wheel",
"python_version": "cp37",
"requires_python": ">=3.7",
"size": 2532181,
"upload_time": "2024-04-20T20:33:16",
"upload_time_iso_8601": "2024-04-20T20:33:16.739902Z",
"url": "https://files.pythonhosted.org/packages/66/67/15e17ca40114bcc75aeedb393fc23a9fdd93a6e8279444edbad49d76b0e2/llvm_python-0.0.1b1-cp37-cp37m-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "190a40b148e4f01e46e9a032328232b3420aff8fc831d7014eeb38aa84348627",
"md5": "e5f12af36b687bf4a6dbe42c9ddf1d08",
"sha256": "fe70a34912b2ee1486a67118f287af6f3a32a8add1c52e3dd69ba15781a75f51"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp37-cp37m-win_amd64.whl",
"has_sig": false,
"md5_digest": "e5f12af36b687bf4a6dbe42c9ddf1d08",
"packagetype": "bdist_wheel",
"python_version": "cp37",
"requires_python": ">=3.7",
"size": 2261811,
"upload_time": "2024-04-20T20:33:18",
"upload_time_iso_8601": "2024-04-20T20:33:18.639559Z",
"url": "https://files.pythonhosted.org/packages/19/0a/40b148e4f01e46e9a032328232b3420aff8fc831d7014eeb38aa84348627/llvm_python-0.0.1b1-cp37-cp37m-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7c1072e15317cacfa082dd387fffa76b8dc380d2c45a4fadb4b89623204d84c3",
"md5": "b5143611403a912661d3fc73690cb054",
"sha256": "ef95a531ddf60b722f6604cb7c0b3f4660c76d12604a529a1918930065403537"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp38-cp38-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "b5143611403a912661d3fc73690cb054",
"packagetype": "bdist_wheel",
"python_version": "cp38",
"requires_python": ">=3.7",
"size": 2529073,
"upload_time": "2024-04-20T20:33:20",
"upload_time_iso_8601": "2024-04-20T20:33:20.040300Z",
"url": "https://files.pythonhosted.org/packages/7c/10/72e15317cacfa082dd387fffa76b8dc380d2c45a4fadb4b89623204d84c3/llvm_python-0.0.1b1-cp38-cp38-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3f99697840821dcbf63bc1479a1b901656f60cdc5bd450c69ff20d821970b352",
"md5": "fe8b6c613d2c27170607c27269308760",
"sha256": "d7a7ef4bc87d158fd6c90a7acc21759c6e04710fb1198d6c0d9b37c0d313fd9b"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp38-cp38-win_amd64.whl",
"has_sig": false,
"md5_digest": "fe8b6c613d2c27170607c27269308760",
"packagetype": "bdist_wheel",
"python_version": "cp38",
"requires_python": ">=3.7",
"size": 2258005,
"upload_time": "2024-04-20T20:33:22",
"upload_time_iso_8601": "2024-04-20T20:33:22.562628Z",
"url": "https://files.pythonhosted.org/packages/3f/99/697840821dcbf63bc1479a1b901656f60cdc5bd450c69ff20d821970b352/llvm_python-0.0.1b1-cp38-cp38-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ee9eeaffeeaf186e212db1de9d5192f301ddcdfb495508cfd8a0e85970da83b0",
"md5": "f0e2b6e831e33e64f7106928246f8c67",
"sha256": "3f0d15c03bcc9eff2fab04e5f457bef80d77dc0e3dcc4763374a89a1d9f1bacf"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp39-cp39-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "f0e2b6e831e33e64f7106928246f8c67",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.7",
"size": 2529628,
"upload_time": "2024-04-20T20:33:23",
"upload_time_iso_8601": "2024-04-20T20:33:23.947457Z",
"url": "https://files.pythonhosted.org/packages/ee/9e/eaffeeaf186e212db1de9d5192f301ddcdfb495508cfd8a0e85970da83b0/llvm_python-0.0.1b1-cp39-cp39-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7d90076b032b1e648d0ed2080bcf3fecc82ec7be5e7e705492afa1c87c7cf504",
"md5": "ccb9ff43eaefa19339b854bdcdc690f7",
"sha256": "74370c776e89c92d4389d9b402a8da301b0941d48e7526e66fb1fc371f8db6dd"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1-cp39-cp39-win_amd64.whl",
"has_sig": false,
"md5_digest": "ccb9ff43eaefa19339b854bdcdc690f7",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.7",
"size": 2258155,
"upload_time": "2024-04-20T20:33:25",
"upload_time_iso_8601": "2024-04-20T20:33:25.997477Z",
"url": "https://files.pythonhosted.org/packages/7d/90/076b032b1e648d0ed2080bcf3fecc82ec7be5e7e705492afa1c87c7cf504/llvm_python-0.0.1b1-cp39-cp39-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ac95b417537053fc5ebb8a97ea5ff6ddc44a2c0efc44e554529d42d60a8a6d7d",
"md5": "4a9c035909610bab7276a60d0990b079",
"sha256": "d9f070b004a57d0c5e13b3ca16532e143199f7c1cc06849a07ca08f702473335"
},
"downloads": -1,
"filename": "llvm_python-0.0.1b1.tar.gz",
"has_sig": false,
"md5_digest": "4a9c035909610bab7276a60d0990b079",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 17970,
"upload_time": "2024-04-20T20:33:27",
"upload_time_iso_8601": "2024-04-20T20:33:27.279633Z",
"url": "https://files.pythonhosted.org/packages/ac/95/b417537053fc5ebb8a97ea5ff6ddc44a2c0efc44e554529d42d60a8a6d7d/llvm_python-0.0.1b1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-20 20:33:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Papr1ka",
"github_project": "llvm_python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "llvm-python"
}