{"id":16822491,"url":"https://github.com/mborgerson/jit","last_synced_at":"2025-04-11T03:11:41.285Z","repository":{"id":151569549,"uuid":"149684771","full_name":"mborgerson/jit","owner":"mborgerson","description":"A little just-in-time compiler thing","archived":false,"fork":false,"pushed_at":"2018-09-28T08:59:07.000Z","size":7,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T03:11:34.761Z","etag":null,"topics":["assembly","c","jit","jit-interpreter","just-in-time","x86","x86-64"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mborgerson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-20T23:57:46.000Z","updated_at":"2024-11-22T15:01:18.000Z","dependencies_parsed_at":"2023-05-29T09:15:19.170Z","dependency_job_id":null,"html_url":"https://github.com/mborgerson/jit","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/mborgerson%2Fjit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mborgerson%2Fjit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mborgerson%2Fjit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mborgerson%2Fjit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mborgerson","download_url":"https://codeload.github.com/mborgerson/jit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248333604,"owners_count":21086200,"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","c","jit","jit-interpreter","just-in-time","x86","x86-64"],"created_at":"2024-10-13T11:04:28.022Z","updated_at":"2025-04-11T03:11:41.280Z","avatar_url":"https://github.com/mborgerson.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Just-In-Time Compiler Toy\n=========================\n\nLately I've been interested in [just-in-time (JIT)\ncompilers](https://en.wikipedia.org/wiki/Just-in-time_compilation), so I\ndecided to try and build a small one for the experience. This was a fun little\nproject to work on, and pretty easy to build. It's still a work in progress,\nbut hopefully this inspires others to build their own JIT!\n\nA very basic IR is defined with a handful of useful operations. There's an\ninterpreter which can be used to run the IR in software, and the main JIT\nportion which translates the IR to native X86-64 code for execution on your\nsystem (provided you have an X86-64 system, of course).\n\nBackground\n----------\nThe basic setup is inspired by QEMU's excellent\n[TCG](https://git.qemu.org/?p=qemu.git;a=blob_plain;f=tcg/README;hb=HEAD). TCG\nfacilitates emulation of a CPU's instructions by first translating (or\n*lifting*) the emulated instructions into an intermediate representation (IR)\nin groups of instructions called *basic blocks*, terminated by a branch. The\nsystem responsible for translation from the emulated architecture to the IR is\ncalled the *front-end*.\n\nThe IR, like other instruction sets, is composed of many basic instructions\nwhich can be combined to emulate the *behavior* of the emulated instruction\nset, while additionally tracking the changes made to the CPU state by the\nrespective instruction. It's often the case that one input instruction\ngenerates several output IR instructions.\n\nNext, QEMU will translate the IR instructions into the native machine code of\nthe host which the user is running on. The system which translates IR to\nanother instruction set architecture is called the *back-end*. TCG\nhistorically has roots for use in a compiler, and a compiler operates much the\nsame way--except of course that the input to the front-end is usually some\nprogramming language (C, for instance).\n\nThis project is a *very* rudimentary version of all this: just the back-end\nportion which generates X86-64 compatible code from an intermediate\nrepresentation.\n\nHow It Works\n------------\nA basic block composed of IR instructions is created. Note: This could be done\nby parsing some scripting language, disassembling CPU code, or something...\nthis project doesn't touch on that part yet and I've hard-coded the IR by hand\nin the source file. Next the IR block is then either (a) interpreted or (b)\ntranslated.\n\nInterpretation works by decoding the IR instruction, then executing some C\ncode which performs the operation. This can be slower or faster depending on\nthe circumstance (what's being emulated vs how long it takes to JIT code,\netc). Generally speaking though the goal here anyway is to translate to our\nCPU's architecture for native execution. For translation, the IR instructions\nare examined, and corresponding machine code instructions are emitted.\nFinally, the machine code can then be executed. The machine code is written to\nan allocated page in memory and then jumped to from our C code.\n\nApplications\n------------\nJITs can (and are) used in many places from emulation, to acceleration of\nscripting languages and beyond--basically, any application that demands\nperformance. I didn't mention it above, but many JITs will perform\noptimization as well--this could be a basic pass over the IR or more advanced\nwith runtime monitoring and re-compiling.\n\nTODO\n----\n- Error handling (valid label/reg numbers, TB overflows, etc)\n- Support more instructions\n- Support more registers\n- Support calling helper functions\n- Example CPU emulator\n- Block chaining\n- Forward jumps in translated code\n- Improve branch condition handling\n- Improve block prologue/epilogue\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmborgerson%2Fjit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmborgerson%2Fjit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmborgerson%2Fjit/lists"}