{"id":16571705,"url":"https://github.com/coolbutuseless/rbytecode","last_synced_at":"2025-07-25T09:11:15.412Z","repository":{"id":203290885,"uuid":"709238379","full_name":"coolbutuseless/rbytecode","owner":"coolbutuseless","description":"R bytecode assembler/disassembler","archived":false,"fork":false,"pushed_at":"2024-08-09T04:02:18.000Z","size":723,"stargazers_count":35,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-02T00:41:33.407Z","etag":null,"topics":["bytecode","r"],"latest_commit_sha":null,"homepage":"https://coolbutuseless.github.io/book/rbytecodebook/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coolbutuseless.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","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":"2023-10-24T10:04:53.000Z","updated_at":"2025-03-22T10:17:00.000Z","dependencies_parsed_at":"2024-08-08T23:35:13.580Z","dependency_job_id":"30c03edd-91b0-4bf8-8edd-ceaf8954e1e3","html_url":"https://github.com/coolbutuseless/rbytecode","commit_stats":null,"previous_names":["coolbutuseless/rbytecode"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/coolbutuseless/rbytecode","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Frbytecode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Frbytecode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Frbytecode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Frbytecode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coolbutuseless","download_url":"https://codeload.github.com/coolbutuseless/rbytecode/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Frbytecode/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266983523,"owners_count":24016559,"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","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["bytecode","r"],"created_at":"2024-10-11T21:24:55.038Z","updated_at":"2025-07-25T09:11:15.383Z","avatar_url":"https://github.com/coolbutuseless.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = FALSE,\n  comment = \"\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n\nlibrary(rbytecode)\n```\n\n# rbytecode \u003cimg src=\"man/figures/logo.png\" align=\"right\" width = \"40%\"/\u003e\n\n\u003c!-- badges: start --\u003e\n![](https://img.shields.io/badge/cool-useless-green.svg)\n\u003c!-- badges: end --\u003e\n\nModern R now executes most code in a stack-based virtual machine (VM).\n\nThe R code you write is first parsed to \nan abstract syntax tree, which is then compiled to bytecode,  and this \nbytecode is then executed in *R's bytecode VM*.\n\n`{rbytecode}` provides an assembler and disassembler for R bytecode - allowing\nyou to peek under the hood at what R is doing to execute your code.\n\nThe disassembler is a way of inspecting R code internals by dissecting existing code.\n\nThe assembler is a way of compiling R *bytecode assembly* directly into a *bytecode object* i.e.\nwriting directly in the virtual machine language understood internally by \nR's bytecode VM.\n\nThis work is heavily based around the `{compiler}` package (built-in to R) and\nLuke Tierney's document \n[A Byte Code Compiler for R](https://homepage.cs.uiowa.edu/~luke/R/compiler/compiler.pdf)\n\n## This package has a book!\n\nI have written a book to accompany this package.  The book gives some background on \nR's execution of code in its stack-based virtual machine and a reference for the\ndifferent bytecode instructions understood by this VM.\n\nAvailable from:\n\n* [LeanPub](https://leanpub.com/rbytecode) EPUB available for free - but \n  if you felt like showing support there is an option for a small payment.\n* [Online html](https://coolbutuseless.github.io/book/rbytecodebook/)\n\n\n# Ideas for the Future \n\n* Add support for `SWITCH` instruction\n* Keep track of stack size during assembly to try and catch bad code.\n* Write a small VM - maybe only covering a subset of instructions.\n\n## What's in the box\n\n* `dis()` disassembles R language objects to bytecode assembly\n* `disq()` a helper function where `disq(1 + x)` is equivalent to `dis(quote(1 + x))`\n* `asm()` an assembler for R bytecode.  Takes R bytecode assembly and outputs \n  an executable bytecode object\n\n\n## Installation\n\nYou can install from [GitHub](https://github.com/coolbutuseless/rbytecode) with:\n\n``` r\n# install.package('remotes')\nremotes::install_github('coolbutuseless/rbytecode')\n```\n\n## Disassembler\n\n\n```{r example}\nlibrary(rbytecode)\n\ndisq(x + 1) |\u003e \n  as.character()\n```\n\n\n```{r}\ndisq(function(x, y = 1) {x + y}) |\u003e \n  as.character()\n```\n\n\n\n## Assembler - simple example\n\n```{r}\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# Create bytecode assembly\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ncode \u003c- r\"(\nLDCONST 1\nLDCONST 2\nADD\nRETURN\n)\"\n\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# Assemble the code into a bytecode object\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nbc \u003c- asm(code)\nbc\n\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# Evaluate the bytecode\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\neval(bc)\n```\n\n\n## Bytecode Assembly - Fibonacci\n\nThe following bytecode assembly is a reimplementation of R code for calculating\nthe 11th Fibonacci number.\n\n```{r}\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# R implementation\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ni   \u003c- 0\nfn2 \u003c- 0\nfn1 \u003c- 1\nwhile (i \u003c 10) {\n  fnnew \u003c- fn1 + fn2\n  fn2   \u003c- fn1\n  fn1   \u003c- fnnew\n  i     \u003c- i + 1\n}\nfn1\n```\n\n\n```{r}\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# Bytecode implementation\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ncode \u003c- r\"(\nLDCONST 0L\nSETVAR i\nSETVAR fn2\nLDCONST 1L\nSETVAR fn1\n@start \nGETVAR i\nLDCONST 10L\nLT \nBRIFNOT @end\nGETVAR fn1\nGETVAR fn2\nADD \nSETVAR fnnew\nPOP \nGETVAR fn1\nSETVAR fn2\nPOP \nGETVAR fnnew\nSETVAR fn1\nPOP \nGETVAR i\nLDCONST 1L\nADD \nSETVAR i\nPOP \nGOTO @start\n@end \nGETVAR fn1\nRETURN \n)\"\n\nasm(code) |\u003e eval()\n```\n\n\n\n\n\n\n## Related Software\n\n* The `{compiler}` package. One of the base R packages.\n\n## Acknowledgements\n\n* R Core for developing and maintaining the language.\n* CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining\n  the repository\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Frbytecode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoolbutuseless%2Frbytecode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Frbytecode/lists"}