{"id":13629200,"url":"https://github.com/codr7/liblg","last_synced_at":"2026-01-17T17:10:58.649Z","repository":{"id":161215092,"uuid":"287972861","full_name":"codr7/liblg","owner":"codr7","description":"A minimal stack-based VM","archived":false,"fork":false,"pushed_at":"2020-09-10T11:45:31.000Z","size":139,"stargazers_count":182,"open_issues_count":1,"forks_count":4,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-08T19:43:27.708Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codr7.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2020-08-16T15:39:00.000Z","updated_at":"2024-10-14T09:27:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"884bf5e3-91f4-4cc8-8e1f-2d73cfbbdfa7","html_url":"https://github.com/codr7/liblg","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/codr7%2Fliblg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codr7%2Fliblg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codr7%2Fliblg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codr7%2Fliblg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codr7","download_url":"https://codeload.github.com/codr7/liblg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249316056,"owners_count":21249885,"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-01T22:01:04.410Z","updated_at":"2026-01-17T17:10:58.621Z","avatar_url":"https://github.com/codr7.png","language":"C","readme":"### intro\nThe goal of [liblg](https://github.com/codr7/liblg) is to create a reasonably fast, minimal stack-based VM in C. It currently implements just enough functionality to generate the Fibonacci sequence using recursion, tail-recursion and iteration. The general idea is that it's potentially useful to have access to stack vm functionality in library form, for implementing custom DSLs on top etc.\n\n### setup\n[liblg](https://github.com/codr7/liblg) requires a C compiler and CMake to build.\n\n```\n$ git clone https://github.com/codr7/liblg.git\n$ cd liblg\n$ mkdir build\n$ cd build\n$ cmake ..\n$ make\n$ ./fibrec\n148810us\n```\n\n### benchmarks\nThe VM is currently significantly faster than Python3 and an order of magnitude slower than Go, ideas on how to improve the performance further without making a mess are most welcome.\n\n\n```\n$ python3 bench/fibrec.py\n353813us\n$ go run bench/fibrec.go\n14170us\n```\n\n```\n$ build/lgasm --bench 100 bench/fibtail.lga\n147us\n$ python3 bench/fibtail.py\n543us\n$ go run bench/fibtail.go\n9us\n```\n\n```\n$ build/lgasm --bench 100 bench/fibiter.lga\n126us\n$ python3 bench/fibiter.py\n173us\n$ go run bench/fibiter.go\n4us\n```\n\n### design\nThe core loop uses computed goto, which means that new instructions must be added in identical order [here](https://github.com/codr7/liblg/blob/master/src/lg/op.h) and [here](https://github.com/codr7/liblg/blob/master/src/lg/vm.c).\n\nFundamental types are global (as in not tied to a specific VM instance), new types may be added [here](https://github.com/codr7/liblg/tree/master/src/lg/types) and initialized [here](https://github.com/codr7/liblg/blob/master/src/lg/init.c).\n\n[Values](https://github.com/codr7/liblg/blob/master/src/lg/val.h) are represented as tagged unions.\n\n### assembler\n[liblg](https://github.com/codr7/liblg) comes equipped with a simple custom [assembler](https://github.com/codr7/liblg/tree/master/bench/fibtail.lga) that provides a transparent interface to it's functionality.\n\n#### add\nPop and add the top of the stack to the previous item.\n\n#### beq $offset $value $label\nBranch to $label if the stack value att $offset is equal to $value.\n\n#### bgr $offset $value $label\nBranch to $label if the stack value att $offset is greater than $value.\n\n#### ble $offset $value $label\nBranch to $label if the stack value att $offset is less than $value.\n\n#### call $label\nPush the program counter on the call stack and jump to $label.\n\n#### cp $offset\nPush a copy of the value at stack $offset.\n\n#### dec $offset\nDecrease the value att stack $offset by one.\n\n#### drop $offset $count\nDrop $count items from stack $offset.\n\n#### jmp $label\nJump to $label.\n\n#### push $value\nPush $value on the stack.\n\n#### ret\nReturn from the current call.\n\n#### stop\nStop execution.\n\n#### swap\nSwap the top two items on the stack.\n\n### license\n[MIT](https://github.com/codr7/liblg/blob/master/LICENSE.txt)","funding_links":[],"categories":["C","Other"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodr7%2Fliblg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodr7%2Fliblg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodr7%2Fliblg/lists"}