Commit Graph

87 Commits

Author SHA1 Message Date
Aryadev Chavali
a7f14c8f58 Remove indent in MESSAGE 2024-07-07 19:39:03 +01:00
Aryadev Chavali
7a5eee932a Moved logging macros to base.h and use them everywhere
The macros used in the testing library are actually useful everywhere
so may as well use them.
2024-07-07 03:16:42 +01:00
Aryadev Chavali
edf90780af Remove JUMP_STACK and CALL_STACK
Any program that generates addresses at runtime can be easily
expressed through fixed address jumps and calls.  No need for them.
2024-07-07 03:01:41 +01:00
Aryadev Chavali
ebdb1a948d Removed m*_stack_* opcodes, make them default behaviour
Memory operations used operands to encode positional/size arguments,
with stack variants in case the user wanted to programmatically do so.
In most large scale cases I don't see the non-stack variants being
used; many cases require using the stack for additions and
subtractions to create values such as indexes or sizes.  Therefore,
it's better to be stack-first.

One counter point is inline optimisation of code at runtime: if an
compile-time-known object is pushed then immediately used in an
operation, we can instead encode the value directly into an operand
based instruction which will speed up execution time because it's
slower to pop the value off the stack than have it available as part
of the instruction.
2024-07-07 03:01:06 +01:00
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
ecadcb5e36 Remove an include and define BYTE_SIZE 2024-06-20 02:24:23 +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
f2addcef0a Introducing short to data_t and handlers for converting endian 2024-06-19 20:31:16 +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
92bbf1f9a3 Fix errors in GitHub workflows version of GCC 2024-06-19 19:11:32 +01:00
Aryadev Chavali
11f79a84a3 Clean errors in compilation when VERBOSE > 0 2024-06-19 19:07:50 +01:00
Aryadev Chavali
4e513dbf01 Fix stupid issues in prog_write_bytecode
In particular, not writing the full words for the program header and
incrementing bytes prematurely.
2024-06-18 23:49:42 +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
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
d65da8eb0d char8_t -> char_t 2024-05-05 20:26:07 +05:30
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
fcc6bce6f9 Added header file for all INST_ macros that I deleted
This can be a possible method of native compilation, albeit requiring
the source code of the project.  Until I explore that this thing will
stay.
2024-04-29 01:42:08 +05:30
Aryadev Chavali
f64f456dff Finished documenting lib/heap.h 2024-04-29 01:38:43 +05:30
Aryadev Chavali
8ba25b2bf2 Use memset instead of setting properties manually
Does the same thing but it's a bit clearer that I'm not setting
anything.
2024-04-29 01:37:47 +05:30
Aryadev Chavali
3e64d7855a Changed darr_at to return a pointer 2024-04-29 01:06:58 +05:30
Aryadev Chavali
c799eb2706 Adjust DARR_REALLOC_MULT 2024-04-28 23:29:50 +05:30
Aryadev Chavali
2ab1ee34f9 Use byteswap to implement conversion to and from bytes
Cleaner looking implementations
2024-04-28 22:31:41 +05:30
Aryadev Chavali
8aecb7b681 Fixed problem with detecting little endian
Copied the code from stack overflow without thinking about it.  The
first byte in little endian order should always be LSB so I construct
a more contrived example (0xFFFF0000) which should make it easier to
detect what the first byte is considered on the machine.  If it's 0
then the LSB is the first byte hence little endian, otherwise it's big
endian.

On a greater note: Don't never copy no code from stack overflow, bro.
I went up there at 11 o'clock last night trynna get me some code.
Bro, I copied that shit, woke up, my motherfucking LITTLE_ENDIAN
detection don't work.  Explain, bro.
2024-04-28 22:17:51 +05:30
Aryadev Chavali
08c9726333 Fixed bug with converting (h)word to (h)word in terms of endian
While it helped with understanding to use unions as a safe way to
access the underlying bits, this shift based mechanism actually makes
more sense at a glance, particularly by utilising WORD_NTH_BYTE
2024-04-28 20:57:41 +05:30
Aryadev Chavali
8461dcf423 Little endian check for convert_*_to_*
We don't need to perform the for loop if not necessary
2024-04-28 20:53:44 +05:30
Aryadev Chavali
df16aa9b19 Fixed WORD_NTH_HWORD which didn't work correctly before
Thank you testing
2024-04-28 19:09:28 +05:30
Aryadev Chavali
06a883d538 Made lib/base conversion functions const where possible 2024-04-28 17:41:17 +05:30
Aryadev Chavali
cce3259b56 Fixed bugs in base.c
In particular, __LITTLE_ENDIAN__ was not a functioning macro.
Instead, I implemented a version by hand (copied from IBM) that
actually figures out if the machine is little endian or not.

Thank you unit testing!
2024-04-28 17:37:48 +05:30
Aryadev Chavali
b775afee2c heap_free_page -> heap_free 2024-04-28 14:54:01 +05:30
Aryadev Chavali
3085dfeded Added a PAGE_DEFAULT_SIZE so max can be set to 0 2024-04-28 14:34:02 +05:30
Aryadev Chavali
a2c9a06ca6 Documented lib/darr.h 2024-04-28 14:20:45 +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
b9c94d0725 Changed doxygen comment style in base.h
Remove asterisk from start of each line and add a @brief.
2024-04-25 11:50:51 +05:30
Aryadev Chavali
49b4470424 Defined bounds for signed and unsigned base types 2024-04-25 11:10:50 +05:30
Aryadev Chavali
331784281e Refactor s_word -> long_t 2024-04-25 11:08:17 +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
81caa577fe Fixed static_assert message in inst_print 2024-04-25 03:01:41 +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
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
e2f4c5c125 Reordered opcode_t
Moved all opcodes that use unsigned types before the signed types AND
ordered signed types into BYTE, CHAR, HWORD, INT, WORD, LONG.  This is
not only logically consistent but also looks prettier.
2024-04-25 02:52:50 +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