398 Commits

Author SHA1 Message Date
Aryadev Chavali
26dea6ce72 Added todos to rename the constructive macros in runtime.c 2023-11-01 22:09:26 +00:00
Aryadev Chavali
90ee9b492d Implemented MGET_STACK and MSET_STACK in the runtime
Very easy, they just pop a word then defer to their normal versions.
This is probably the best case where a macro works directly so I
didn't even need to write a function form for them first.  One thing
is that then names for the macros are quite bad right now, probably
need renaming.
2023-11-01 22:08:06 +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
44125d7ad9 Implemented OP_MSIZE into lexer/parser of ASM 2023-11-01 21:47:19 +00:00
Aryadev Chavali
ea715c569e Implemented OP_MSIZE in the VM runtime 2023-11-01 21:47:05 +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
7564938113 Implemented lexer and parser for new memory management instructions 2023-11-01 21:40:25 +00:00
Aryadev Chavali
32c1bcb859 Added a print_heap mechanism into vm
Pretty simple implementation, nothing massive.
In VERBOSE mode, heap is tracked for any allocations/deletions,
printing it if so.
2023-11-01 21:39:00 +00:00
Aryadev Chavali
be312cfbbf Implemented instructions in the runtime for memory management
Pretty simple, required some new types of errors.  Obviously there's
space for a macro for the differing instruction implementations, but
these things need to be tested a bit more before I can do that.
2023-11-01 21:38:52 +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
4e64f1fe23 DUP implementation is now part of WORD_ROUTINES
As PUSH_REGISTER and MOV have the same signature of taking a word as
input, DUP may as well be part of it.

This leads to a larger discussion about how signatures of functions
matter: I may need to do a cleanup at some point.
2023-11-01 21:14:04 +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
0f295221ca VM runtime now maintains a heap internally
Now need to create some instructions which manage the heap
2023-11-01 20:08:40 +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
d648344c2c Updated README LOC 2023-11-01 18:56:07 +00:00
Aryadev Chavali
6677424f12 Deleted fib.c as fib.asm replaces it 2023-11-01 18:49:59 +00:00
Aryadev Chavali
f4ae33949b Lines of Code heading for README 2023-11-01 18:48:28 +00:00
Aryadev Chavali
044afc302e Updated README with build instructions 2023-11-01 18:41:01 +00:00
Aryadev Chavali
cb8b3f3b8c Fix off by one issues in register implementations
word register 0 refers to the first 8 bytes of the dynamic array.
Hence the used counter should be at least 8 bytes.  This deals with
those issues.  Also print more useful information in
vm_print_registers (how many byte|hword|word registers are currently
in use, how many are available).
2023-11-01 18:31:11 +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
cc34c9b9c3 Ignore all out files 2023-11-01 18:20:03 +00:00
Aryadev Chavali
bfa0a1f85c Implemented a factorial program in the assembly
Very cool, easy, and reads well
2023-11-01 18:12:55 +00:00
Aryadev Chavali
d521871b98 Removed the index printing in fib.asm 2023-11-01 18:12:27 +00:00
Aryadev Chavali
848c2f75e3 Implement OP_MULT in runtime
Lucky surprise: OP_PLUS follows the same principle rules as the
bitwise operators in that they return the same type as the input.
Therefore I can simply use the same macro to implement it and MULT as
those.  Very nice.
2023-11-01 18:10:43 +00:00
Aryadev Chavali
83678ad29a Add MULT to lexer and parser for assembler 2023-11-01 18:09:00 +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
c244e7c4a4 Use vm_stop and vm_load_registers
By default I initialise the registers with 8 words, though this may
not be necessary for your purposes.
2023-11-01 17:56:22 +00:00
Aryadev Chavali
57e6923279 Fixed bug where comparators wouldn't be parsed correctly
This is because comparators may apply to signed types, so I need to
use the right parsing function.
2023-11-01 17:55:54 +00:00
Aryadev Chavali
809cc1b26a examples/fib.asm now terminates on a very large bound
This is using the comparators and a jump-if
2023-11-01 17:55:34 +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
081661cb6b Fixed bug with comparators where all results were flipped
This is because: say we have {a, b} where a is on top of the stack.  A
comparator C applies in the order C(b, a) i.e. b `C` a.  The previous
version did a `C` b which was wrong.
2023-11-01 17:52:58 +00:00
Aryadev Chavali
90d901345a Added a routine to cleanup resources allocated to the VM
This means the stack should be heap allocated, which makes sense as
beyond 1KB one should really be using the heap rather than the stack.
2023-11-01 17:52:15 +00:00
Aryadev Chavali
7a1129d80f VM registers are now a dynamic array
Stack based machines generally need "variable space".  This may be
quite via a symbol-to-word association a list, a hashmap, or some
other system.  Here I decide to go for the simplest: extending the
register system to a dynamic/infinite number of them.  This means, in
practice, that we may use a theoretically infinite number of indexed
words, hwords and bytes to act as variable space.  This means that the
onus is on those who are targeting this virtual machine to create
their own association system to create syntactic variables: all the
machinery is technically installed within the VM, without the veneer
that causes extra cruft.
2023-11-01 17:49:33 +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
0bab4ec014 Made an example translation of fib.c to the custom assembly (fib.asm) 2023-11-01 16:39:07 +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
2703e5c577 Enable clang-format-mode in dir-locals 2023-11-01 15:25:58 +00:00
Aryadev Chavali
6d35283ef0 Clearer VERBOSE messages 2023-11-01 15:22:47 +00:00
Aryadev Chavali
6a270eda1e Parser now uses updated lexer
Much simpler, uses a switch case which is a much faster method of
doing the parsing.  Though roughly equivalent in terms of LOC, I feel
that this is more extensible
2023-11-01 15:09:56 +00:00
Aryadev Chavali
93d234cd48 Lexer now returns more descriptive tokens
More useful tokens, in particular for each opcode possible.  This
makes parsing a simpler task to reason as now we're just checking
against an enum rather than doing a string check in linear time.

It makes more sense to do this at the tokeniser as the local data from
the buffer will be in the cache most likely as the buffer is
contiguously allocated.  While it will always be slow to do linear
time checks on strings, when doing it at the parser we're having to
check strings that may be allocated in a variety of different places.
This means caching becomes a harder task, but with this approach we're
less likely to have cache misses as long as the buffer stays there.
2023-11-01 15:09:47 +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
90fb9816b4 Added new macro for bitwise comparison construction
This pushes a datum of the same type as the operands, which is why it
cannot use the comparator macro as that always pushes bytes.
2023-10-31 22:30:58 +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
0f0a1c7699 Allow hex literals for numbers
As strto(ul|ll) allow the parsing of hex literals of the form `0x`, we
allow lexing of hex literals which start with `x`.

They're lexed into C hex literals which work for strtol.
2023-10-31 22:27:53 +00:00
Aryadev Chavali
d9aaaf2a53 Use macros to stop duplication of code
I've made a single macro which defines a function through some common
metric, removing code duplication.  Not particularly readable per se,
but using a macro expansion in your IDE allows one to inspect the code.
2023-10-31 21:42:29 +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
5202dfbb26 Remove duplicated code 2023-10-31 21:28:19 +00:00
Aryadev Chavali
f7f566b106 Implemented all the comparators
So much reused code, I definitely need to find a way to make this cleaner.
2023-10-31 21:24:54 +00:00
Aryadev Chavali
7817b5acc9 Use standardised signed version of word type from base.h 2023-10-31 21:24:50 +00:00