Commit Graph

445 Commits

Author SHA1 Message Date
Aryadev Chavali
a0857b9090 test WORD_NTH_* and WORD_SAFE_SUB, ~refactored general testing code
The new general testing procedure is making a structure for tests that
include all the samples and expected results, then iterating over them
to run the test.
2024-04-28 19:08:20 +05:30
Aryadev Chavali
26a8fac732 Updated testing library to make writing tests easier
Success is printed by the RUN_TEST_SUITE macro so we don't need to add
that boilerplate.
2024-04-28 19:06:44 +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
cf6aa96539 Wrote tests for lib/base.h's convert_bytes_to_* functions
Just runs a sample of suitably byte arrays with an expected set of
functions.  The samples are in little endian format and the outputs
are what we expect them to be.  This should run regardless of the
endian of your machine.
2024-04-28 17:35:46 +05:30
Aryadev Chavali
ab839efbcd Defined a base testing library header
This defines useful macros, in particular to print information and to
define tests and test suites.

The idea is a suite will be a set of test functions, where a test
function will fail <=> an assert is called.  This allows us to stop
testing immediately upon a test failing and produce viable output.
2024-04-28 17:27:42 +05:30
Aryadev Chavali
f725c2a668 Started developing a unit test module
Folder per module in test/.  Header only tests with one actual main
file to implement a program that runs them.

Makefile runs the test and provides some eye candy to show if the test
succeeded or failed.  The tests, on the other hand, will show success
or failure for each of them.
2024-04-28 15:33:47 +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
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
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
a66f9825f6 Updated README lines of code 2024-04-25 11:13:05 +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
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
81caa577fe Fixed static_assert message in inst_print 2024-04-25 03:01:41 +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
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
Aryadev Chavali
71d423a06b Track dependencies properly 2024-04-16 20:45:02 +06:30
Aryadev Chavali
c833f4946b Propagated changes to prog_t into vm/runtime 2024-04-16 18:30:53 +06:30
Aryadev Chavali
5d6cf212e7 Moved prog_t.count -> prog_t.prog_header_t.count 2024-04-16 18:21:39 +06:30
Aryadev Chavali
38d7c13287 Migrate virtual machine from OVM project and rewrite README 2024-04-16 18:21:05 +06:30
Aryadev Chavali
2a1d006a88 Updated README about change to project 2024-04-16 15:49:26 +06:30
Aryadev Chavali
8f75241bcb Halting work on preprocesser units and rewrite as a whole
I've decided to split the project into 2 repositories: the assembler
and the runtime.  The runtime will contain both the executable and
lib/ while the assembler will have the runtime as a git submodule and
use it to build.  I think this is a clean solution, a lot cleaner than
having them all in one project where the Makefile has to massively
expand.
2024-04-16 15:42:59 +06:30
Aryadev Chavali
d5c43b1c3f Wrote up some notes on how preprocesser language may work
Bit formal and really excessively written but I needed my thoughts
down.
2024-04-16 15:42:34 +06:30
Aryadev Chavali
715facf015 Updated README lines of code 2024-04-16 15:42:22 +06:30
Aryadev Chavali
4ecd184759 lerr_type_t::UNKNOWN_CHAR -> UNKNOWN_LEXEME 2024-04-16 15:41:01 +06:30
Aryadev Chavali
27d6a47320 Clean up error message from preprocesser 2024-04-16 15:40:49 +06:30
Aryadev Chavali
3fc1f08134 Fix bug where CONST table didn't actually store symbol names
Pretty simple fix, stupid error in hindsight.
2024-04-16 15:40:00 +06:30
Aryadev Chavali
4b3e9b3567 Clear vector after deleting all tokens
Ensures that iteration over vec_out by caller doesn't occur (such as
in a loop to free the memory).
2024-04-16 15:39:20 +06:30
Aryadev Chavali
05136fdd25 Fixed examples for changes in lexer
Name assigned to %CONST is the next symbol in stream, not the symbol
attached to it.
2024-04-16 15:38:24 +06:30
Aryadev Chavali
1e7f1bdee9 Changed %const format in preprocesser now
Instead of %const(<name>) ... %end it will now be %const <name>
... %end i.e. the first symbol after %const will be considered the
name of the constant similar to %use.
2024-04-15 18:39:37 +06:30
Aryadev Chavali
ba3525d533 preprocesser publicly exposes only preprocesser function
The preprocess_* functions are now privately contained within the
implementation file to help the preprocesser outer function.

Furthermore I've simplified the API of the preprocess_* functions by
making them only return pp_err_t and store their results in a vector
parameter taken by reference.
2024-04-15 18:37:45 +06:30
Aryadev Chavali
d594c0c531 Annotate some completed todos in todo.org 2024-04-15 16:35:44 +06:30
Aryadev Chavali
e960af2904 Added some VERBOSE checked messages into asm/main 2024-04-15 16:33:22 +06:30
Aryadev Chavali
4a7341e26c Propagate changes to lerr_t into preprocesser 2024-04-15 16:33:02 +06:30
Aryadev Chavali
b83bdd0d45 preprocesser function now only returns a pp_err_t
We leave the parameter tokens alone, considering it constant, while
the parameter vec_out is used to hold the new stream of tokens.  This
allows the caller to have a before and after view on the token stream
and reduces the worry of double frees.
2024-04-15 16:31:45 +06:30