Commit Graph

79 Commits

Author SHA1 Message Date
Aryadev Chavali
6014620baa Changed fill-column to 80, so more space for comments. 2024-06-28 16:11:01 +01:00
Aryadev Chavali
3a09beb582 Change license agreement terming to ensure version 2 only. 2024-06-28 16:07:15 +01:00
Aryadev Chavali
a9f81992ab Update copyright notices and top level licenses 2024-06-25 00:48:43 +01:00
Aryadev Chavali
3cd2dbc2ac Registers are now fixed size
todo.org contains an explanation for this
2024-06-24 15:14:11 +01:00
Aryadev Chavali
4ad3d46996 LE ordering in data oriented instructions, make more macros
Little Endian ordering is now ensured for stack based and register
based operations e.g.  PUSH now pushes datums in LE ordering onto the
stack, POP pops data on the stack, converting from LE ordering to host
ordering.

More functions in the runtime are now macro defined, so there's less
code while still maintaining the lookup table.

--------------------------------------------------------------------------------

What LE ordering means for actual source code is that I utilise the
convert_*_to_* functions for PUSH and POP.  All other data oriented
instructions are completely internal to the system and any data
storage must be in Little Endian.  So MOV, PUSH-REGISTER and DUP can
all directly memcpy the data around the system without needing to
consider endian at all.

Macros were a necessity after I felt the redefinition of push for 4
different data types was too much.  Most functions are essentially
copies for each datatype.  Lisp macros would make this so easy :(
2024-06-24 14:29:05 +01:00
Aryadev Chavali
8984c97d33 Clean up runtime source code a little bit 2024-06-20 02:28:15 +01:00
Aryadev Chavali
05235497dc Propagate addition of short based instructions to runtime
Lots of busy work
2024-06-19 21:48:13 +01:00
Aryadev Chavali
213d4afcfd long -> sword, int -> shword, char -> sbyte 2024-06-19 20:31:16 +01:00
Aryadev Chavali
bdc6e15ae9 Updated includes, README and TODO 2024-06-19 20:01:20 +01:00
Aryadev Chavali
11f79a84a3 Clean errors in compilation when VERBOSE > 0 2024-06-19 19:07:50 +01:00
Aryadev Chavali
66153afbde Some light header pruning 2024-06-17 23:11:30 +01:00
Aryadev Chavali
a60108ae1d Made heap a vector of pages
Instead of using a linked list, which is incredibly fragmented, a
vector keeps all pointers together.  Keeps all our stuff together and
in theory we should have less cache misses when deleting pages.

It does introduce the issue of fragmenting, where if we allocate and
then delete many times a lot of the heap vector will be empty so
traversal will be over a ton of useless stuff.
2024-06-17 23:11:20 +01:00
Aryadev Chavali
b3de11d1f7 Making better documentation 2024-06-04 02:57:51 +01:00
Aryadev Chavali
91af3e14ef Clean up code 2024-05-05 20:20:25 +05:30
Aryadev Chavali
a40eaf29b8 OP_HALT is its own opcode, fixed some other minor bugs 2024-05-01 22:36:34 +05:30
Aryadev Chavali
b775afee2c heap_free_page -> heap_free 2024-04-28 14:54:01 +05:30
Aryadev Chavali
4255c44a6a Propagated change in prog_t into vm
main now does error reporting for read errors
2024-04-27 17:43:42 +05:30
Aryadev Chavali
43d14d05cf Refactor type word -> word_t 2024-04-25 11:07:37 +05:30
Aryadev Chavali
9c0125542e Refactored type hword -> hword_t 2024-04-25 10:59:45 +05:30
Aryadev Chavali
1dfdbb3cd0 Refactor type byte -> byte_t
Simpler to read as a type.  This style will allow me to define signed
versions of the base types as simpler names than what they are
currently.
2024-04-25 10:48:51 +05:30
Aryadev Chavali
159501168c Split vm/runtime.c into two files
struct.c is used for the general construction, inspection and deletion
of the virtual machine structure (vm_t).  runtime defines the
execution routines, error enum, lookup tables, etc.
2024-04-25 03:18:23 +05:30
Aryadev Chavali
a8f8a09752 Massively simplify OP_PRINT implementation
Firstly abuse OPCODE_DATA_TYPE along with integer arithmetic to do a
POP_ROUTINE table lookup (no more ugly conditionals).  Then make a
format string table which we can lookup using the same data type.
2024-04-25 03:05:40 +05:30
Aryadev Chavali
c378591e3f Simplify OP_JUMP_IF implementation
Same method as when simplifying OP_POP's implementation: use the
lookup table along with OPCODE_DATA_TYPE abuse.  In this case I made a
lookup table called OP_POP for this method to work, but it wasn't difficult.
2024-04-25 03:05:11 +05:30
Aryadev Chavali
a0479790a2 Fixed order of operations for certain implementations
OP_PUSH: Increment program pointer iff no error from PUSH_ROUTINE call
STACK_ROUTINES: Same as OP_PUSH
RET: Pop off the call stack iff the jump to the address was good.
2024-04-25 02:59:29 +05:30
Aryadev Chavali
aac6e620c6 Moved static_assert outside of vm_execute
Doesn't change much but is a bit more legible.
2024-04-25 02:57:35 +05:30
Aryadev Chavali
327adb17f4 Added clarifying comments for WORD_ROUTINES and STACK_ROUTINES 2024-04-25 02:57:01 +05:30
Aryadev Chavali
a4b057f20a Refactored inst.c and runtime.c for OPCODE_IS_TYPE change
Pretty simple, there are fewer opcode types that use signed types than
unsigned so it was a pretty simple rename.
2024-04-25 02:55:54 +05:30
Aryadev Chavali
122e12e8fd Simplify the OP_POP_* routine by adjusting data_type_t
This simple fix made the routine for OP_POP not require an additional
dispatch step on top of the conditional due to OPCODE_DATA_TYPE,
instead using data_type_t as a map from an opcode's base type to a
specific type.
2024-04-25 01:20:35 +05:30
Aryadev Chavali
71b0b793af prog_t no longer has a header
This "header" is now embedded directly into the struct.  The semantic
of a header never really matters in the actual runtime anyway, it's
only for bytecode (de)serialising.
2024-04-25 01:19:43 +05:30
Aryadev Chavali
c833f4946b Propagated changes to prog_t into vm/runtime 2024-04-16 18:30:53 +06:30
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