37 Commits

Author SHA1 Message Date
Aryadev Chavali
9d4e56c441 Fixed code in vm_pop_hword DWORD -> DHWORD
Though practically this would work, as the storage for the half word is
not limited in any way, nevertheless it isn't syntactically right and
it's better to fix now.
2024-04-09 15:13:51 +06:30
Aryadev Chavali
af142e71ff Fixed issues with getting and setting words for heap pages
Because I was using the hword macros instead of word macros, this
causes truncation of bytes when I didn't want it.
2023-11-29 23:10:32 +00:00
Aryadev Chavali
70c8a03939 Fixed logs in vm/runtime
Just changing some messages and the format of heap printing
2023-11-29 23:10:17 +00:00
Aryadev Chavali
8fa40d8515 Fixed issue where, on error, runtime would show wrong instruction
When an error occurred, because prog->ptr was incremented beforehand
the trace would show the next instruction as the culprit rather than
the actual instruction.  This commit fixes that by incrementing the
program if and only if the command was run successfully.
2023-11-08 18:12:52 +00:00
Aryadev Chavali
96a83db92b Fixed bug where runtime would not start program at the right place
In vm_execute_all set the program pointer to the start address in the
header of the program payload.
2023-11-03 21:21:33 +00:00
Aryadev Chavali
92f4f9011d Refactor vm/main to use refactor to program structure 2023-11-03 21:15:30 +00:00
Aryadev Chavali
a7588ccc61 Use program structure for runtime
Very barebones, essentially a simple refactor.

I need to introduce a feature to append to a program as well, but as
it's a flexible structure it will likely have to be functional.
2023-11-03 19:09:13 +00:00
Aryadev Chavali
f433febb22 Added memory leak dialog in vm_stop
Stack, call stack and heap are evaluated to check for leaks.
2023-11-02 23:25:37 +00:00
Aryadev Chavali
4eee6e1a0a Implemented subroutine instructions in runtime
Very easy overall, printing the call stack not so much.
2023-11-02 23:25:33 +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
ac3270777b Implemented OP_MALLOC_STACK and OP_SUB in the runtime 2023-11-01 22:56:24 +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
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
ea715c569e Implemented OP_MSIZE in the VM runtime 2023-11-01 21:47:05 +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
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
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
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
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
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
6d35283ef0 Clearer VERBOSE messages 2023-11-01 15:22: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
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
Aryadev Chavali
14a3820e74 Implemented new types of EQ, forced all comparators to push bytes
Just need to call their unsigned versions.

All comparators should push bytes as it makes return types uniform.
2023-10-31 21:05:25 +00:00
Aryadev Chavali
036d3dcfba vm_execute_all: Print every cycle on VERBOSE >= 2, just print final state otherwise
Changed VERBOSE checks to ensure a degree of information.
2023-10-31 21:00:13 +00:00
Aryadev Chavali
4d8b855d87 Created routines for new comparator opcodes (not implemented)
Will cause error if used currently, which is fine.
2023-10-31 20:59:01 +00: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