Commit Graph

23 Commits

Author SHA1 Message Date
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
1ae28bdb3b Propagating short addition to data types into instructions
(De)Serialising routines now can deal with shorts.
2024-06-19 21:36:39 +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
99fb0a0e2e Moved OP_HALT to the beginning (stable ABI)
OP_HALT = 1 now.  This commit also adjusts the error checking in
inst_read_bytecode.

The main reasoning behind this is when other platforms or applications
target the AVM: whenever a new opcode may be added, the actual binary
for OP_HALT changes (as a result of how C enums work).

Say your application targets commit alpha of AVM.  OP_HALT is, say,
98.  In commit beta, AVM is updated with a new opcode so OP_HALT is
changed to 99 (due to the new opcode being placed before OP_HALT).  If
your application builds a binary for AVM version alpha and AVM version
beta is used instead, OP_HALT will be interpreted as another
instruction, which can lead to undefined behaviour.

This can be hard to debug, so here I've made the decision to try and
not place new opcodes in between old ones; new ones will always be
placed *before* NUMBER_OF_OPCODES.
2024-06-18 23:49:42 +01:00
Aryadev Chavali
a40eaf29b8 OP_HALT is its own opcode, fixed some other minor bugs 2024-05-01 22:36:34 +05:30
Aryadev Chavali
40907e5113 Reworked (de)serialising routines for instructions
No longer relying on darr_t or anything other than the C runtime and
aliases.  This means it should be *even easier* to target this via FFI
from other languages without having to initialise my custom made
structures!  Furthermore I've removed any form of allocation in the
library so FFI callers don't need to manage memory in any way.
Instead we rely on the caller allocating the correct amount of memory
for the functions to work, with basic error handling if that doesn't
happen.

In the case of inst_read_bytecode, error reporting occurs by making
the return of a function an integer.  If the integer is positive it is
the number of bytes read from the buffer.  If negative it flags a
possible error, which is a member of read_err_t.

prog_read_bytecode has been split into two functions: prog_read_header
and prog_read_instructions.  prog_read_instructions works under the
assumption that the program's header has been filled, e.g. via
prog_read_header.  prog_read_header returns 0 if there's not enough
space in the buffer or if the start_address is greater than the count.
prog_read_instructions returns a custom structure which contains an
byte position as well as an error enum, allowing for finer error
reporting.

In the case of inst_write_bytecode via the assumption that the caller
allocated the correct memory there is no need for error reporting.
For prog_write_bytecode if an error occurs due to

In the case of inst_read_bytecode we return the number
2024-04-27 17:43:06 +05:30
Aryadev Chavali
13790e0cda Split OPCODE_IS_TYPE -> SIGNED|UNSIGNED
Due to reordering I need to have two macros for checking if an opcode
is of a type.  If the type is signed then the upper bound must be
OP_<type>_LONG whereas if it is unsigned then the upper bound must be
OP_<type>_WORD.
2024-04-25 02:53:55 +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
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
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
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
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
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
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