37 Commits

Author SHA1 Message Date
Aryadev Chavali
6c3bd47c04 Update Makefile to be more like AVM Makefile
Less colours more information, easier to decode.
2024-07-10 00:42:59 +01:00
Aryadev Chavali
a422c7d1dc A reworked preprocesser with focus on stopping recursive errors
Preprocesser requires one function to use: preprocess.  Takes Tokens
and gives back Units.

A unit is a tree of tokens, where each unit is a node in that tree.  A
unit has a "root" token (value of node) and an "expansion" (children
of node) where the root is some preprocesser token (such as a
reference or USE call) and the expansion is the tokens it yields.  In
the case of a USE call this is the tokens of the file it includes, in
the case of a reference it's the tokens of the constant it refers to.
This means that the leaves of the tree of units are the completely
preprocessed/expanded form of the source code.

The function has many working components, which may need to be
extracted.  In particular, the function ensures we don't include a
source twice through a hash map and that constants are not redefined
in inner include scopes if they're already defined in outer
scopes (i.e. if compiling a.asm which defines constant N, then include
b.asm which defines constant N, then N uses the definition of a.asm
rather than b.asm).

I need to make a spec for this.
2024-07-06 17:38:02 +01:00
Aryadev Chavali
42dbf515f0 Deleted preprocesser
Will be reworking it later
2024-07-03 16:55:19 +01:00
Aryadev Chavali
f060a856d3 Fixed Makefile so it tracks dependencies better
It now tracks main.cpp's dependencies and rebuilds them as needed.
2024-04-16 20:42:51 +06:30
Aryadev Chavali
3b9e573c4a Made AVM a git submodule, updated the Makefile to build assembler
Also updated dir-locals to make include path resolution accurate.
2024-04-16 19:18:32 +06:30
Aryadev Chavali
5319fd815d Dependencies stricter and prerequisites for directories in Makefile
Don't need to make a directory every time I compile some code.
2024-04-15 16:25:35 +06:30
Aryadev Chavali
f661438c93 Moved read_file to a general base library 2024-04-15 04:55:51 +06:30
Aryadev Chavali
72ef40e671 parser -> preprocesser + parser
I've decided to split the module parsing into two modules, one for the
preprocessing stage which only deals with tokens and the parsing stage
which generates bytecode.
2024-04-14 17:25:28 +06:30
Aryadev Chavali
e7a09c0de4 Wrote a new lexer API in C++
Essentially a refactor of the C formed lexer into C++ style.  I can
already see some benefits from doing this, in particular speed of
prototyping.
2024-04-14 16:52:58 +06:30
Aryadev Chavali
0ebbf3ca75 Start writing assembler in C++
Best language to use as it's already compatible with the headers I'm
using and can pretty neatly enter the build system while also using
the functions I've built for converting to and from bytecode!
2024-04-14 02:45:48 +06:30
Aryadev Chavali
4e9eb0a42e fix! loops in preprocess_use_blocks iterate to the wrong bound
A token_stream being constructed on the spot has different
used/available properties to a fully constructed one: a fully
constructed token stream uses available to hold the total number of
tokens and used as an internal iterator, while one that is still being
constructed uses the semantics of a standard darr.

Furthermore, some loops didn't divide by ~sizeof(token_t)~ which lead
to iteration over bound errors.
2024-04-14 02:00:17 +06:30
Aryadev Chavali
1935277716 Makefile now assembles and interprets instruction-test.asm example first 2023-11-08 18:16:53 +00:00
Aryadev Chavali
b2e3e39a39 Added Makefile recipe to interpret all examples 2023-11-03 21:51:46 +00:00
Aryadev Chavali
fc5393b010 Added memory-print.asm to examples list in Makefile 2023-11-03 21:15:35 +00:00
Aryadev Chavali
698a823bf2 Added recipes to assemble or interpret individually 2023-11-02 23:30:17 +00:00
Aryadev Chavali
b6e359f5eb Fixed issue where sometimes vm_print_registers wouldn't work for bytes
Happened because we weren't printing all relevant words due to
naturally flooring the result of division.  Here I ceil the division
to ensure we get the maximal number of words necessary.
2023-11-01 22:54:45 +00:00
Aryadev Chavali
525694bea7 Added an arena allocator
A page is a flexibly allocated structure of bytes, with a count of the
number of bytes already allocated (used) and number of bytes available
overall (available), with a pointer to the next page, if any.

heap_t is a linked list of pages.  One may allocate a requested size
off the heap which causes one of two things:
1) Either a page already exists with enough space for the requested
size, in which case that page's pointer is used as the base for the
requested pointer
2) No pages satisfy the requested size, so a new page is allocated
which is the new end of the heap.
2023-11-01 19:08:59 +00:00
Aryadev Chavali
dab1729f61 Makefile now has recipes for example assembly programs
Dependencies are just ASM_OUT binary and the corresponding assembly
program for the bytecode output file.  Actually works very well, with
changes triggering a recompilation.  Also an `exec` recipe is
introduced to do the task of compiling an assembly program and
executing the corresponding bytecode all at once.
2023-11-01 18:28:56 +00:00
Aryadev Chavali
b6daa58cab Makefile now has green colours for binaries and yellow for object files 2023-11-01 15:40:10 +00:00
Aryadev Chavali
5d800d4366 Moved inst module to lib
As it has no dependencies on vm specifically, and it's more necessary
for any vendors who wish to target the virtual machine, it makes more
sense for inst to be a lib module rather than a vm module.
2023-10-31 21:14:14 +00:00
Aryadev Chavali
c534a53b6f Make Verbose a universal flag
All objects target LIB anyway, so VERBOSE is a universal macro for
this code base.
2023-10-29 16:59:37 +00:00
Aryadev Chavali
2fe2af22a9 Implemented a rudimentary parser with support for 4 instruction types 2023-10-26 11:17:55 +01:00
Aryadev Chavali
d1f84a6a2c Makefile now prints dependencies on successful compilation 2023-10-26 10:22:10 +01:00
Aryadev Chavali
74a85268c4 Moved base functions from inst.c to dedicated file
Doesn't make sense for them to be in the VM module when they have a
more general purpose now.
2023-10-26 10:19:10 +01:00
Aryadev Chavali
94abb9e3d4 Separated lexer from main file in asm 2023-10-25 21:43:13 +01:00
Aryadev Chavali
92855ebb3e Starting development on assembly language 2023-10-23 16:53:50 +01:00
Aryadev Chavali
42ac28d44a Make root directory an include path, set #include's properly
Easier to write includes now just using < with the module name, in
comparison to using relative paths.
2023-10-23 04:46:09 +01:00
Aryadev Chavali
0ac634a9be Reintroduced example fib.c via examples/ folder 2023-10-23 04:25:48 +01:00
Aryadev Chavali
ac57e32a02 Added lib folder for general stuff, introduced as target to Makefile 2023-10-23 04:25:48 +01:00
Aryadev Chavali
b44a61be41 src->vm, Makefile is now a bit more abstracted and pretty colours
Changed folder names for sake of clarity (will be introducing a new
build target soon), and Makefile can now easily support more targets.
2023-10-23 03:58:34 +01:00
Aryadev Chavali
919fae2df8 Added an example program fib.c
This is simply a program with an embedded set of instructions which
indefinitely computes and prints fibonacci numbers, computing them in
pairs.

Does it completely through the virtual machine rather than just hard C
instructions.

Also amended the Makefile to compile it.  Required moving the main.c
object file into the dependencies of $(DIST)/$(OUT).

I should track the dependencies for fib.c and main.c as well.
2023-10-23 00:45:23 +01:00
Aryadev Chavali
ed63025927 Added flag to Makefile to set VERBOSE macro in base.h 2023-10-22 18:26:33 +01:00
Aryadev Chavali
cf23a62008 Made a debug and release configuration via flags
Also split out the check for whether $(DIST) exists by making it its
own recipe.  Removes the repeated checks in each object compilation.
2023-10-22 18:07:41 +01:00
Aryadev Chavali
6038363d2f Added functionality to read and write instruction bytecode
Uses some bit hacks to quickly check what data type an opcode may have
by shifting down to units then casting it to a data_type_t.
Not very well tested yet, we'll need to see now.
2023-10-16 01:18:05 +01:00
Aryadev Chavali
c24c1d1607 Implemented a dynamically sized byte array
Pretty simple, want to ensure amortised constant big O so I use a
REALLOC_MULT, which can be redefined if wished.
2023-10-15 21:50:31 +01:00
Aryadev Chavali
639808a092 Moved vm_* code to its own file (runtime.(h|c)) 2023-10-15 21:41:16 +01:00
Aryadev Chavali
16e2275576 First commit!
This is a from the ground rework of an old project of the same name.
I'm hoping to be more concerned with runtime efficiency, bytecode size
and all those things that should actually matter for something that
may host time/space critical code.
2023-10-15 01:25:24 +01:00