• Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal stack-based VM

intro

The goal of 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.

setup

liblg requires a C compiler and CMake to build.

$ git clone https://github.com/codr7/liblg.git
$ cd liblg
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./fibrec
148810us

benchmarks

The 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.

$ python3 bench/fibrec.py
353813us
$ go run bench/fibrec.go
14170us
$ build/lgasm --bench 100 bench/fibtail.lga
147us
$ python3 bench/fibtail.py
543us
$ go run bench/fibtail.go
9us
$ build/lgasm --bench 100 bench/fibiter.lga
126us
$ python3 bench/fibiter.py
173us
$ go run bench/fibiter.go
4us

design

The core loop uses computed goto, which means that new instructions must be added in identical order here and here.

Fundamental types are global (as in not tied to a specific VM instance), new types may be added here and initialized here.

Values are represented as tagged unions.

assembler

liblg comes equipped with a simple custom assembler that provides a transparent interface to it's functionality.

add

Pop and add the top of the stack to the previous item.

beq $offset $value $label

Branch to $label if the stack value att $offset is equal to $value.

bgr $offset $value $label

Branch to $label if the stack value att $offset is greater than $value.

ble $offset $value $label

Branch to $label if the stack value att $offset is less than $value.

call $label

Push the program counter on the call stack and jump to $label.

cp $offset

Push a copy of the value at stack $offset.

dec $offset

Decrease the value att stack $offset by one.

drop $offset $count

Drop $count items from stack $offset.

jmp $label

Jump to $label.

push $value

Push $value on the stack.

ret

Return from the current call.

stop

Stop execution.

swap

Swap the top two items on the stack.

license

MIT

More Repositories

1

g-fu

Lisp 2 Go
Go
240
star
2

sharpl

a custom Lisp
C#
82
star
3

alang

A minimal viable programming language on top of liblgpp
C++
77
star
4

whirlog

a minimal versioned log structured relational DB in Common Lisp
Common Lisp
73
star
5

liblgpp

an extensible stack based interpreter toolkit
C++
65
star
6

libcodr7

fundamental tools in the spirit of C
C
59
star
7

forthy2

a Forth (for you) too
C++
57
star
8

cidk

interpreter devkit
C++
24
star
9

whirlisp

A whirlwind Lisp adventure
Common Lisp
19
star
10

gfoo

a Go scripting language
Go
13
star
11

swifties-repl

a custom Lisp REPL using Swifties
Swift
12
star
12

swifties

a custom language construction kit
Swift
12
star
13

gofu

Go scripting language toolkit
Go
11
star
14

nojs

an encrypted communication toolkit
8
star
15

alisp

a custom Lisp in C
C
8
star
16

snabel

a Common Lisp scripting language
Common Lisp
5
star
17

ampl

a C++ scripting language
C++
4
star
18

gfun

Lisp 4 Go
Go
4
star
19

lila

a cleaner language based on Common Lisp
Common Lisp
4
star
20

shedulr

Common Lisp
3
star
21

calcl

programmatic calculator
C++
3
star
22

cl-redb

Relational DB library
Common Lisp
3
star
23

stash

C++
2
star
24

gapl

a Go scripting language/toolkit
Go
2
star
25

pardom

a virtual DOM
Common Lisp
2
star
26

snabl_old

a practical embedded Lisp
C++
2
star
27

lishp

a Common Lispy, text based shell
Common Lisp
2
star
28

emash

an e-mail shell
Common Lisp
2
star
29

gollies

Essential Go utilities
Go
2
star
30

jappkit

Java Application Toolkit
Java
2
star
31

swift-interpreter

Build an interpreter in Swift
Swift
2
star
32

advent2021

https://adventofcode.com/2021
Common Lisp
1
star
33

cl-ps

Common Lisp
1
star
34

mems

C++
1
star
35

scrlpp

C++
1
star
36

qm

C++
1
star
37

snabl

a simple Go scripting language
Go
1
star
38

cl-dom

Common Lisp
1
star
39

libcnabl

C
1
star
40

claes

a custom Lisp in C++
C++
1
star
41

cfoo

minimalistic interpreted extension language
C
1
star
42

fibr

a minimal interpreter
C
1
star
43

nibl

a Forth VM
C++
1
star
44

cliblng

Common Lisp Favorites
Common Lisp
1
star
45

unidys

Common Lisp
1
star
46

aoc2022

Common Lisp
1
star
47

snackis

C++
1
star
48

scrl

Go
1
star
49

clpp

Common Lisp
1
star
50

cl-appfs

in-app directory/shell
Common Lisp
1
star
51

logot

Java
1
star
52

clogbase

C++
1
star
53

fipl

a Forth in Lisp
Common Lisp
1
star
54

cl-timestamp

Common Lisp
1
star
55

liblg-old

C
1
star
56

bookng

a general purpose booking system
1
star
57

bookrweb

Go
1
star
58

libcfxp

Fixed point arithmetics in C
C
1
star
59

gappkit

Go
1
star
60

alp

C++
1
star
61

cl-scrl

a scripting language/VM
Common Lisp
1
star
62

libcbs

C boot strap library
C
1
star
63

gstraps

Go
1
star
64

jalang

an embedded custom Lisp interpreter
HTML
1
star
65

claesp

Common Lisp
1
star
66

unid

a simple, web based reservation system
Go
1
star
67

cl-extashy

Extensible hash tables
Common Lisp
1
star
68

swisql

Simpler PostgreSQL in Swift
Swift
1
star
69

goshd

Go
1
star
70

cl-slog

Structured logging library
Common Lisp
1
star