15 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
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
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
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
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
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
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
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