{"id":13648364,"url":"https://github.com/cslarsen/minijit","last_synced_at":"2025-08-21T20:32:22.027Z","repository":{"id":137136193,"uuid":"107056170","full_name":"cslarsen/minijit","owner":"cslarsen","description":"A basic x86-64 JIT compiler written from scratch in stock Python ","archived":false,"fork":false,"pushed_at":"2018-04-21T18:02:58.000Z","size":32,"stargazers_count":227,"open_issues_count":0,"forks_count":13,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-12-08T08:31:29.810Z","etag":null,"topics":["assembly","compiler","jit","jit-compiler","machine-code","python","x86-64"],"latest_commit_sha":null,"homepage":"https://csl.name/post/python-jit/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cslarsen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-10-15T23:17:54.000Z","updated_at":"2024-11-30T11:55:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"ca6fb6a1-97e2-4c0e-bd46-3e6334a29558","html_url":"https://github.com/cslarsen/minijit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Fminijit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Fminijit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Fminijit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Fminijit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cslarsen","download_url":"https://codeload.github.com/cslarsen/minijit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230532443,"owners_count":18240792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly","compiler","jit","jit-compiler","machine-code","python","x86-64"],"created_at":"2024-08-02T01:04:10.827Z","updated_at":"2024-12-20T04:07:28.342Z","avatar_url":"https://github.com/cslarsen.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"MiniJIT\n=======\n\nContains code for the posts\n\n  * Writing a basic x86-64 JIT compiler from scratch in stock Python\n    https://csl.name/post/python-jit/\n\n  * JIT compiling a tiny subset of Python to x86-64 from scratch — in Python\n    https://csl.name/post/python-compiler/\n\nYou need a UNIX/POSIX system and an x86-64 compatible CPU. I've tested this on\nLinux and macOS, using Python 2.7 and 3+\n\nThe ~500 lines of code relies only on standard Python libraries and contains a\nPython bytecode converter, peephole optimizer and x86-64 machine code\nassembler. The code is meant to be simple to understand and pedagogical.\n\nFinally, there is a decorator that automatically swaps out Python functions\nwith native code:\n\n    \u003e\u003e\u003e from jitcompiler import jit, disassemble\n    \u003e\u003e\u003e @jit\n    ... def foo(a, b): return a + b\n    ... \n    --- Installing JIT for \u003cfunction foo at 0x10bc48c08\u003e\n    \u003e\u003e\u003e foo(10, -2)\n    --- JIT-compiling \u003cfunction foo at 0x10bc48c08\u003e\n    8\n    \u003e\u003e\u003e print(disassemble(foo))\n    0x10bb3c000 48 89 fb       mov rbx, rdi\n    0x10bb3c003 48 89 f0       mov rax, rsi\n    0x10bb3c006 48 01 d8       add rax, rbx\n    0x10bb3c009 c3             ret \n\nHow to run tests\n----------------\n\nThe first one patches up some machine code and runs it at runtime\n\n    $ python mj.py\n\nThe second one JIT compiles Python bytecode to machine code at runtime\n\n    $ python tests.py\n\nIf you have the `capstone` module installed, it will display an in-memory\ndisassembly as well.\n\nYou can also run the decorator test. It defines a function like this\n\n    import jitcompiler\n\n    #...\n\n    @jitcompiler.jit\n    def foo(a, b):\n        return a*a - b*b\n\nOn the first *call* to `foo`, it will be compiled to native code and swap out\nthe original Python function. It treats all arguments as signed 64-bit\nintegers. If you have the Capstone module installed, it will also print a\ndisassembly. To run:\n\n    $ python test-decorator.py\n    Definition point of foo\n\n    Installing JIT for \u003cfunction foo at 0x1f855f0\u003e\n\n    Calling foo\n\n    JIT-compiling \u003cfunction foo at 0x1f855f0\u003e\n    Installed native code for \u003cfunction foo at 0x1f855f0\u003e\n    Calling function \u003cCFunctionType object at 0x7f867642b600\u003e\n    foo(1, 2) =\u003e -3\n    Calling function \u003cCFunctionType object at 0x7f867642b600\u003e\n    foo(2, 3) =\u003e -5\n\n    Disassembly of foo\n\n    0x7f86765f9000 48 89 fb       mov rbx, rdi\n    0x7f86765f9003 48 89 f8       mov rax, rdi\n    0x7f86765f9006 48 0f af c3    imul rax, rbx\n    0x7f86765f900a 50             push rax\n    0x7f86765f900b 48 89 f3       mov rbx, rsi\n    0x7f86765f900e 48 89 f0       mov rax, rsi\n    0x7f86765f9011 48 0f af c3    imul rax, rbx\n    0x7f86765f9015 48 89 c3       mov rbx, rax\n    0x7f86765f9018 58             pop rax\n    0x7f86765f9019 48 29 d8       sub rax, rbx\n    0x7f86765f901c c3             ret\n\nIf you want to get serious about this\n-------------------------------------\n\n  * Check out a full-blown assembler library for Python:\n    https://github.com/Maratyszcza/PeachPy\n\nReferences\n----------\n\n  * Intel assembly manuals:\n    https://software.intel.com/en-us/articles/intel-sdm\n\n  * x86 Reference: http://ref.x86asm.net/\n\n  * Capstone disassembler:\n    http://www.capstone-engine.org/lang_python.html\n\nLicense\n-------\n\nPut in the public domain in 2017 by the author Christian Stigen Larsen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcslarsen%2Fminijit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcslarsen%2Fminijit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcslarsen%2Fminijit/lists"}