{"id":13670596,"url":"https://github.com/vkazanov/bytecode-interpreters-post","last_synced_at":"2025-04-05T16:05:20.522Z","repository":{"id":92275300,"uuid":"151270902","full_name":"vkazanov/bytecode-interpreters-post","owner":"vkazanov","description":"A few basic bytecode interpreters used as example code in a series of articles","archived":false,"fork":false,"pushed_at":"2024-11-18T08:58:38.000Z","size":1079,"stargazers_count":125,"open_issues_count":1,"forks_count":6,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-29T15:04:39.019Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vkazanov.png","metadata":{"files":{"readme":"README.org","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-02T14:43:12.000Z","updated_at":"2025-03-25T14:06:21.000Z","dependencies_parsed_at":"2025-02-12T10:09:32.780Z","dependency_job_id":"93eb7209-134b-410e-9559-1f57ce24473f","html_url":"https://github.com/vkazanov/bytecode-interpreters-post","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkazanov%2Fbytecode-interpreters-post","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkazanov%2Fbytecode-interpreters-post/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkazanov%2Fbytecode-interpreters-post/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkazanov%2Fbytecode-interpreters-post/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vkazanov","download_url":"https://codeload.github.com/vkazanov/bytecode-interpreters-post/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361617,"owners_count":20926642,"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":[],"created_at":"2024-08-02T09:00:45.989Z","updated_at":"2025-04-05T16:05:20.501Z","avatar_url":"https://github.com/vkazanov.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"* What..?\n\nA few basic bytecode interpreters used as example code in a series of articles. Given a recent GCC\nall the interpreters can be compiled in one go using a supplied Makefile:\n\n#+BEGIN_SRC shell\nmake all\nmake test\n#+END_SRC\n\n* Where?\n\nArticles:\n\n1. Home-grown bytecode interpreters ([[file:interpreter-p1.org][source in Russian]], [[https://habr.com/company/badoo/blog/425325/][Russian]], [[https://badootech.badoo.com/home-grown-bytecode-interpreters-51e12d59b25c][English]])\n2. When pigs fly: optimising bytecode interpreters ([[file:interpreter-p2-pigletvm.org][source in Russian]], [[https://habr.com/company/badoo/blog/428878/][Russian]], [[https://badootech.badoo.com/when-pigs-fly-optimising-bytecode-interpreters-f64fb6bfa20f][English]])\n3. Regex bytecode interpreter: looking for needles in session haystacks ([[file:interpreter-p3-matcher.org][source in Russian]], [[https://habr.com/company/badoo/blog/433054/][Russian]], [[https://badootech.badoo.com/regex-bytecode-interpreter-looking-for-needles-in-session-haystacks-9bbff9db09bc][English]])\n\nInterpreter examples:\n\n1. [[file:interpreter-basic-switch.c][A trival switch interpreter]].\n2. Immediate [[file:interpreter-immediate-arg.c][operand instruction example]].\n3. [[file:interpreter-stack-machine.c][A stack vm]].\n4. [[file:interpreter-register-machine.c][A register vm]].\n5. A [[file:interpreter-regexp.c][regular expression matching machine]].\n6. Various main loop implementations for [[file:pigletvm.h][PigletVM]].\n7. [[file:piglet-matcher.h][Regular expression matcher]] defined on event sequences.\n\n* PigletVM, a trivial stack machine\n\nPigletVM is a simple stack machine created for testing various bytecode interpreter main loop\nimplementations.\n\nPigletVM examples in PVM assembly:\n\n1. A trivial [[file:test/sum.pvm][Sum of Numbers]]\n2. Naive implementation of the [[file:test/sieve.pvm][Sieve of Eratosthenes]]\n\nBase techinques implemented:\n\n1. basic switch\n2. basic switch with the switch value range check eliminated\n3. token threaded code\n4. trace interpreter\n\nThanks to [[https://github.com/iliazeus][@iliazeus]] we now have a second set of the same interpreters with stack top cached:\n\n1. basic switch with stack top cache\n2. switch with no range check and stack top cache\n3. token threaded code with a stack cache\n4. trace interpreter with a stack cache\n\nCompiling and running PigletVM assembler examples:\n\n#+BEGIN_EXAMPLE\n\u003e # build all vms\n\u003e make all\n\u003e # Assemble the program and run it\n\u003e ./pigletvm asm test/sieve.pvm test/sieve.bin\n\u003e ./pigletvm run test/sieve.bin \u003e /dev/null                                                                                  07:54:24\nPROFILE: switch code finished took 20ms\nPROFILE: switch code (no range check) finished took 16ms\nPROFILE: threaded code finished took 7ms\nPROFILE: trace code finished took 8ms\nPROFILE: switch code (reg cache) finished took 4ms\nPROFILE: switch code (reg cache) (no range check) finished took 3ms\nPROFILE: threaded code (reg cache) finished took 2ms\nPROFILE: trace code (reg cache) finished took 5ms\n\u003e # Run the assembled program a number of times:\n\u003e ./pigletvm runtimes test/sieve.bin 100 \u003e /dev/null                                                                         07:54:25\nPROFILE: switch code finished took 430ms\nPROFILE: switch code (no range check) finished took 384ms\nPROFILE: threaded code finished took 472ms\nPROFILE: trace code finished took 363ms\nPROFILE: switch code (reg cache) finished took 350ms\nPROFILE: switch code (reg cache) (no range check) finished took 304ms\nPROFILE: threaded code (reg cache) finished took 255ms\nPROFILE: trace code (reg cache) finished took 301ms\n\n#+END_EXAMPLE\n\n* Want a proper language for PigletVM? PigletC to the rescue!\n\nApart from assembler there's a better way to write PigletVM programs. [[https://github.com/true-grue][@true-grue]] somehow managed to\nimplement a proper language compiled into PigletmVM assembler: [[https://github.com/true-grue/PigletC][PigletC]]!\n\n* PigletMatcher, event sequence matcher.\n\nPigletMatcher is a regular expression engine defined for event sequences. It comes in two pieces: the\n[[file:piglet-matcher.h][virtual machine itself]] and [[file:regexp/regexp.py][a parser]] based on the same tool that was used for [[https://github.com/true-grue/PigletC][PigletC]].\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkazanov%2Fbytecode-interpreters-post","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvkazanov%2Fbytecode-interpreters-post","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkazanov%2Fbytecode-interpreters-post/lists"}