aboutsummaryrefslogtreecommitdiff
path: root/README.org
blob: f1f20e0c5e83dea1f3bdaabbbdca4d78fc632980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#+title: Oreo's Virtual Machine (OVM)
#+author: Aryadev Chavali
#+date: 2023-10-15

A stack based virtual machine in C11, with a dynamic register setup
which acts as variable space.  Deals primarily in bytes, doesn't make
assertions about typing and is very simple to target.
* How to build
Requires =GNU make=, a compliant C11 compiler.  Code base has been
tested against =gcc= and =clang=, but given how the project has been
written without use of GNU'isms it shouldn't be an issue to compile
using something like =tcc= or another compiler (look at
[[file:Makefile::CC=gcc][here]] to change the compiler).

To build everything simply run ~make~.  This will build:
+ [[file:lib/inst.c][instruction bytecode system]] which allows one to
  target the VM by writing and reading instruction bytecode
+ [[file:vm/main.c][VM executable]] which can execute bytecode (your
  =java= executable)
+ [[file:asm/main.c][Assembler executable]] which can assemble
  compliant assembly code to VM bytecode
+ [[file:examples/][Assembly examples]] which provide some source code
  examples on common programs one may write.  Use this to figure out
  how to write compliant assembly.

One may also build each component individually through the
corresponding recipe:
+ ~make lib~
+ ~make vm~
+ ~make asm~
+ ~make examples~
* Instructions to target the virtual machine
You need to link with the object files for
[[file:lib/base.c][base.c]], [[file:lib/darr.c][darr.c]] and
[[file:lib/inst.c][inst.c]] to be able to properly target the OVM.
The basic idea is to create instructions via ~inst_t~ then using the
~inst(s)_write_*~ API to generate bytecode (and write to a file).

Then to execute the program, the virtual machine interpreter ~ovm.out~
is used.

For clarity, one may build ~lib~ (~make lib~) then use the resulting
object files to link and create bytecode for the virtual machine.
* Lines of code
#+begin_src sh :results table :exports results
find -name '*.[ch]' -exec wc -l '{}' ';'
#+end_src

#+RESULTS:
|------+------------------|
|  216 | ./vm/runtime.h   |
|   83 | ./vm/main.c      |
|  718 | ./vm/runtime.c   |
|  452 | ./lib/inst.c     |
|   39 | ./lib/darr.h     |
|  193 | ./lib/inst.h     |
|   90 | ./lib/base.h     |
|   39 | ./lib/base.c     |
|   77 | ./lib/darr.c     |
|  315 | ./asm/parser.c   |
|  140 | ./asm/main.c     |
|   68 | ./asm/lexer.h    |
|   37 | ./asm/parser.h   |
|  439 | ./asm/lexer.c    |
|   80 | ./examples/fib.c |
|------+------------------|
| 2770 | all              |
|------+------------------|
#+TBLFM: @>$1=vsum(@2$1..@>>$1)