36 Commits

Author SHA1 Message Date
Aryadev Chavali
3b912495de Created custom functions to convert (h)words to and from bytecode format
Instead of using endian.h that is not portable AND doesn't work with
C++, I'll just write my own using a forced union based type punning
trick.

I've decided to use little endian for the format as well: it seems to
be used by most desktop computers so it should make these functions
faster to run for most CPUs.
2024-04-14 03:54:54 +06:30
Aryadev Chavali
b7a40f4ab0 Documented lib/darr.h 2024-04-14 02:36:30 +06:30
Aryadev Chavali
1cd31a2702 Moved struct definitions lib/inst.h -> lib/prog.h
This means if I write the new assembler in another language I only
need to FFI this header rather than all the functions as well which
may not be as useful.
2024-04-14 02:35:09 +06:30
Aryadev Chavali
e2667eda65 Fix problems with running programs due to mismatched endian
Basically ensure we're converting to big endian when writing bytecode
and converting from big endian when reading bytecode.
2024-04-12 17:34:17 +06:30
Aryadev Chavali
72585772ef Fixing build problems due to endian.h
Have to define _DEFAULT_SOURCE before you can use the endian
conversion functions.  As most standard library headers use
features.h, and _DEFAULT_SOURCE must be defined before features.h is
included, we have to include base.h before other headers.
2024-04-12 17:32:58 +06:30
Aryadev Chavali
33e1d2ab72 Added some TODOs to lib/inst.c to enforce endian 2024-04-09 21:23:30 +06:30
Aryadev Chavali
d256e06f51 Mid-work through documenting darr.h 2024-04-09 21:21:12 +06:30
Aryadev Chavali
84028dab79 Done TODO: Comment coverage > lib > base.h
Pretty simple
2024-04-09 15:15:00 +06:30
Aryadev Chavali
afb48b65b9 Completed TODO: Rigid Endian
Just used the endian.h functions to convert host endian to and from
big endian.
2024-04-09 15:11:42 +06:30
Aryadev Chavali
4ae6c05276 Current work on preprocessor implementation
Lots to refactor and test
2023-11-05 16:21:09 +00:00
Aryadev Chavali
fe7f26256b Defined a program structure
Essentially a "program header", followed by a count, followed by
instructions.

Provides a stronger format for bytecode files and allows for better
bounds checking on instructions.
2023-11-03 19:07:10 +00:00
Aryadev Chavali
c74c36333b Fixed bug where deleting a page meant not being able to allocate another
This was due to the beg or end page being not set correctly (dangling
pointer).
2023-11-02 23:25:57 +00:00
Aryadev Chavali
86977fe3c1 Introduced instructions to engage with a call stack
Essentially you may "call" an absolute program address, which pushes
the current address onto the call stack.  CALL_STACK does the same
thing but the absolute program address is taken from the data stack.
RET pops an address off the call stack then jumps to that address.
2023-11-02 21:01:21 +00:00
Aryadev Chavali
114fb82990 Removed instruction OP_JUMP_REGISTER
Not necessary when you can just push the relevant word onto the stack
then just do OP_JUMP_STACK.
2023-11-02 20:41:36 +00:00
Aryadev Chavali
99b0ebdfa6 Small fixes 2023-11-02 20:35:47 +00:00
Aryadev Chavali
46e5abbac9 Added instructions for MALLOC_STACK and SUB
MALLOC_STACK is a stack based version of MALLOC, SUB does subtraction.
2023-11-01 22:55:41 +00:00
Aryadev Chavali
0649b3f380 Added stack based versions of MSET and MGET
Essentially they use the stack for their one and only operand.  This
allows user level control, in particular it allows for loops to work
correctly while using these operands.
2023-11-01 22:07:08 +00:00
Aryadev Chavali
cb1cfde0c4 Added instruction to get the size of some allocation
This will allow for more library level code to be written.  For
example, say you wanted to write a generic byte level reversal
algorithm for dynamically sized allocations.  Getting the size of the
allocation would be fundamental to this operation.
2023-11-01 21:45:47 +00:00
Aryadev Chavali
eda49755bc Added instructions for allocating, setting, getting and deleting heap memory
One may allocate any number of (bytes|hwords|words), set or get some
index from allocated memory, and delete heap memory.

The idea is that all the relevant datums will be on the stack, so no
register usage.  This means no instructions should use register space
at all (other than POP, which I'm debating about currently).  Register
space is purely for users.
2023-11-01 21:38:52 +00:00
Aryadev Chavali
a5afaee3d0 heap_free_page returns true if page was successfully deleted 2023-11-01 21:13:00 +00:00
Aryadev Chavali
206dce7bab Heap now maintains a new page per allocation
Instead of having each page be an area of memory, where multiple
pointers to differing data may lie, we instead have each page being
one allocation.  This ensures that a deletion algorithm, as provided,
would actually work without destroying older pointers which may have
been allocated.  Great!
2023-11-01 20:44:37 +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
4be04d2518 Introduced a new mathematical operator MULT
Thankfully multiplication, like addition, is the same under 2s
complement as it is for unsigned numbers.  So I just need to implement
those versions to be fine.
2023-11-01 18:08:11 +00:00
Aryadev Chavali
693ec7a9d0 Changed inst bytecode methods for new register system
As registers may be theoretically infinite in number, we should use
the largest size possible when referring to them in bytecode (a word).
2023-11-01 17:54:15 +00:00
Aryadev Chavali
89fd2b0d17 Set any new data allocated to 0 for clarity
This is only new data allocated, so it's a very careful procedure.
2023-11-01 17:25:33 +00:00
Aryadev Chavali
727081f99a Removed OP_EQ signed versions as they're useless
A negative number under 2s complement can never be equal to its
positive as the top bit *must* be on.  If two numbers are equivalent
bit-by-bit then they are equal for both signed and unsigned numbers.
2023-11-01 14:23:48 +00:00
Aryadev Chavali
5045452d7a Added flag which forces the printing of hexes
Anything other than char (which can just use print.byte to print the
hex) and byte (which prints hexes anyway), all other types may be
forced to print a hex rather than a number if PRINT_HEX is 1.
2023-10-31 22:30:53 +00:00
Aryadev Chavali
5127202722 Introduced 3 new union members to data_t
These new members are just signed versions of the previous members.
This makes type punning and usage for signed versions easier than
before (no need for memcpy).
2023-10-31 21:41:53 +00:00
Aryadev Chavali
095e62b86f Introduced signed versions of common types
For each type T there is the signed version s_T
2023-10-31 21:23:00 +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
157c79d53c Added a "usage" message and colours for assembler
Prints useful and pretty messages when verbose being at least 1.
2023-10-29 16:59:31 +00:00
Aryadev Chavali
1177a5a45b Auto-fill copyright notice in examples/fib.c 2023-10-29 16:56:19 +00:00
Aryadev Chavali
32dfcc109c Added macro to do safe subtractions on a word
Default C just lets overflows occur for subtraction, so this macro
will default to 0 if the subtraction causes an overflow.
2023-10-28 18:16:50 +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
3aad3926d2 Removed assertion in darr_read_file
If an empty file is read, we want to deal with it in later user code
rather than just failing immediately.
2023-10-24 18:20:22 +01:00
Aryadev Chavali
ac57e32a02 Added lib folder for general stuff, introduced as target to Makefile 2023-10-23 04:25:48 +01:00