{"id":13694392,"url":"https://github.com/bspaans/jit-compiler","last_synced_at":"2025-08-20T16:31:05.786Z","repository":{"id":40586634,"uuid":"208474002","full_name":"bspaans/jit-compiler","owner":"bspaans","description":"JIT compiler in Go","archived":false,"fork":false,"pushed_at":"2022-04-28T12:20:16.000Z","size":340,"stargazers_count":205,"open_issues_count":8,"forks_count":22,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-02-17T09:36:43.721Z","etag":null,"topics":["assembler","assembler-x86","compiler","go","golang","jit","jit-compiler","x86-64"],"latest_commit_sha":null,"homepage":"","language":"Go","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/bspaans.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-14T17:04:24.000Z","updated_at":"2024-02-13T20:23:33.000Z","dependencies_parsed_at":"2022-09-10T08:12:11.579Z","dependency_job_id":null,"html_url":"https://github.com/bspaans/jit-compiler","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/bspaans%2Fjit-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bspaans%2Fjit-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bspaans%2Fjit-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bspaans%2Fjit-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bspaans","download_url":"https://codeload.github.com/bspaans/jit-compiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230438185,"owners_count":18225870,"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":["assembler","assembler-x86","compiler","go","golang","jit","jit-compiler","x86-64"],"created_at":"2024-08-02T17:01:30.812Z","updated_at":"2024-12-19T13:07:00.994Z","avatar_url":"https://github.com/bspaans.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library","Go"],"sub_categories":["编译器","Translator"],"readme":"# jit-compiler\n\n[![Documentation](https://godoc.org/github.com/bspaans/jit-compiler?status.svg)](https://godoc.org/github.com/bspaans/jit-compiler) \n\n\nThis is a Golang library containing an x86-64 assembler (see 'asm/') and a\nhigher level intermediate representation that compiles down into x86-64 (see\n'ir/').\n\n## Motivation\n\nThe original intent behind this project was to be able to compile complicated\nSequencer and Synthesizer definitions down to machine code (see my [bleep](https://github.com/bspaans/bleep))\nproject), but it's become a little bit more general purpose since, whilst still \nnot achieving its original goal 👍 There's a very, very early prototype [here](https://github.com/bspaans/bleep-jit), \nbut it's not doing much yet.\n\n\nIn `bleep`, as in many other synthesizers, we build complex sounds by combining\nsmaller building blocks (e.g. a sine wave, a delay filter, etc.) into bigger\ninstruments:\n\n```\n          +---- sine wave\n         /\n  delay \u003c\n         \\\n          +---- sqaure wave\n\n```\n\nWe end up with a tree of sub-synthesizers and filters that together form the\nfinal sound...\n\n...This is nice, but can also be computationally expensive. Especially when \nmultiple separate synthesizers are playing at the same time.\n\nOne of the reasons it is expensive is because the code is jumping around from\nblock to block, basically interpreting the tree. Wouldn't it be nice if we\ncould compile it all down into a single function on the fly? Maybe. This is a\nslightly unnecessary experiment to find out, whilst at the same time learning\nsomething about x86-64 and JIT compilation.\n\n## What is JIT compilation?\n\nJust In Time compilation is a method to convert, at runtime,  the execution of\ndatastructures into machine code. This can be a a lot faster than interpreting\nthe datastructures, as you are dealing directly with the processor and can\napply optimisations that aren't usually possible in the source language. It\ndoes however mean you have to have some way to convert everything into binary;\nhence projects like these.\n\n## What's supported\n\n### Assembler\n\nThe following x86-64 instructions are supported in the assembler. For a detailed \noverview see [`asm/x86_64/opcodes/`](https://github.com/bspaans/jit-compiler/tree/master/asm/x86_64/opcodes):\n\n* MOV, MOVQ, MOVSD, MOVSX, MOVZX (moving things in and out of registers and memory)\n* LEA (loading the address of memory locations into a register)\n* PUSH and POP (stack em up)\n* ADD, SUB, MUL, DIV, IMUL, IDIV (arithmetic)\n* ADDSD, SUBSD, MULSD and DIVSD (float arithmetic)\n* INC and DEC\n* SHL and SHR (shift to the left and right)\n* AND, OR and XOR (logic operations)\n* CMP (compare numbers)\n* CBW, CWD, CDQ, CQO (sign extend %al, %ax, %eax and %rax)\n* CVTSI2SD, CVTTSD2SI (convert int to and from float)\n* SETA, SETAE, SETB, SETBE, SETE, SETL, SETLE, SETG, SETGE, SETNE\n* JMP, JA, JAE, JB, JBE, JE, JG, JGE, JL, JLE, JNA, JNAE, JNB, JNBE, JNE, JNG, JNGE, JNL, JNLE (jumps and conditional jumps)\n* CALL and SYSCALL\n* RET \n* PUSHFQ (push RFLAGS to the stack)\n* VPADDB, VPADDD, VPADDW, VPADDQ\n* VPAND, VPOR\n* Immediate values\n* Addressing modes: direct and indirect registers, displaced registers, RIP relative, SIB\n\n### Higher Level Language\n\nThe higher level language is kind of like a very stripped down Go/C like\nlanguage that makes it easier to generate code. It currently supports:\n\n#### Data Types\n\n* Unsigned 8bit, 16bit, 32bit and 64bit integers\n* Signed 8bit, 16bit, 32bit and 64bit integers\n* 64bit floating point numbers\n* Booleans\n* Static size arrays\n* Structs \n\n#### Expressions\n\n* Signed and unsigned integer arithmetic `(+, -, *, /)`\n* Signed and unsigned integer comparisons `(==, !=, \u003c, \u003c=, \u003e, \u003e=)`\n* Float arithmetic `(+, -, *, /)`\n* Logic expressions `(\u0026\u0026, ||, !)`\n* Array indexing\n* Function calls\n* Syscalls\n* Casting types\n* Equality testing\n* Struct field indexing\n\n#### Statements\n\n* Assigning to variables\n* Assigning to arrays\n* If statements\n* While loops\n* Function definitions\n* Return\n\n#### Register allocation\n\nRegister allocation is really simple and works until you run out of registers;\nthere is no allocating on the stack or heap yet; preserving registers across\ncalls and syscalls is supported however.\n\n## Examples\n\n### Creating machine code \n\n```golang\nimport (\n    \"github.com/bspaans/jit-compiler/asm/x86_64\"\n    \"github.com/bspaans/jit-compiler/asm/x86_64/encoding\"\n    \"github.com/bspaans/jit-compiler/lib\"\n)\n\n\n...\n\nresult := lib.Instructions\nresult = result.Add(x86_64.MOV(encoding.Rax, encoding.Rcx))\nmachineCode, err := result.Encode()\nif err != nil {\n    panic(err)\n}\nmachineCode.Execute()\n\n```\n\n### Using the Intermediate Representation\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/bspaans/jit-compiler/ir\"\n\t\"github.com/bspaans/jit-compiler/ir/encoding/x86_64\"\n\t\"github.com/bspaans/jit-compiler/ir/shared\"\n)\n\nfunc main() {\n\tvar code = `prev = 1; current = 1;\nwhile current != 13 {\n  tmp = current\n  current = current + prev\n  prev = tmp\n}\nreturn current\n`\n\n\tdebug := true\n\tstatements := ir.MustParseIR(code)\n\tmachineCode, err := ir.Compile(\u0026x86_64.X86_64{},\n\t\tx86_64.NewABI_AMDSystemV(),\n\t\t[]shared.IR{statements},\n\t\tdebug)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(machineCode.Execute(debug))\n}\n\n```\n\n## Contributing\n\nContributions are always welcome, but if you want to introduce a breaking\nchange please raise an issue first to discuss. For small additions and bug\nfixes feel free to just create a PR.\n\n## License\n\nThis package is licensed under a MIT License:\n\n```\nCopyright 2020, Bart Spaans\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\nof the Software, and to permit persons to whom the Software is furnished to do\nso, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbspaans%2Fjit-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbspaans%2Fjit-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbspaans%2Fjit-compiler/lists"}