360 Commits

Author SHA1 Message Date
Aryadev Chavali
6d35283ef0 Clearer VERBOSE messages 2023-11-01 15:22:47 +00:00
Aryadev Chavali
6a270eda1e Parser now uses updated lexer
Much simpler, uses a switch case which is a much faster method of
doing the parsing.  Though roughly equivalent in terms of LOC, I feel
that this is more extensible
2023-11-01 15:09:56 +00:00
Aryadev Chavali
93d234cd48 Lexer now returns more descriptive tokens
More useful tokens, in particular for each opcode possible.  This
makes parsing a simpler task to reason as now we're just checking
against an enum rather than doing a string check in linear time.

It makes more sense to do this at the tokeniser as the local data from
the buffer will be in the cache most likely as the buffer is
contiguously allocated.  While it will always be slow to do linear
time checks on strings, when doing it at the parser we're having to
check strings that may be allocated in a variety of different places.
This means caching becomes a harder task, but with this approach we're
less likely to have cache misses as long as the buffer stays there.
2023-11-01 15:09:47 +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
90fb9816b4 Added new macro for bitwise comparison construction
This pushes a datum of the same type as the operands, which is why it
cannot use the comparator macro as that always pushes bytes.
2023-10-31 22:30:58 +00:00
Aryadev Chavali
5045452d7a Added flag which forces the printing of hexes
Anything other than char (which can just use print.byte to print the
hex) and byte (which prints hexes anyway), all other types may be
forced to print a hex rather than a number if PRINT_HEX is 1.
2023-10-31 22:30:53 +00:00
Aryadev Chavali
0f0a1c7699 Allow hex literals for numbers
As strto(ul|ll) allow the parsing of hex literals of the form `0x`, we
allow lexing of hex literals which start with `x`.

They're lexed into C hex literals which work for strtol.
2023-10-31 22:27:53 +00:00
Aryadev Chavali
d9aaaf2a53 Use macros to stop duplication of code
I've made a single macro which defines a function through some common
metric, removing code duplication.  Not particularly readable per se,
but using a macro expansion in your IDE allows one to inspect the code.
2023-10-31 21:42:29 +00:00
Aryadev Chavali
5127202722 Introduced 3 new union members to data_t
These new members are just signed versions of the previous members.
This makes type punning and usage for signed versions easier than
before (no need for memcpy).
2023-10-31 21:41:53 +00:00
Aryadev Chavali
5202dfbb26 Remove duplicated code 2023-10-31 21:28:19 +00:00
Aryadev Chavali
f7f566b106 Implemented all the comparators
So much reused code, I definitely need to find a way to make this cleaner.
2023-10-31 21:24:54 +00:00
Aryadev Chavali
7817b5acc9 Use standardised signed version of word type from base.h 2023-10-31 21:24:50 +00:00
Aryadev Chavali
095e62b86f Introduced signed versions of common types
For each type T there is the signed version s_T
2023-10-31 21:23:00 +00:00
Aryadev Chavali
92ccdfe95c Fixed README for change in inst module 2023-10-31 21:15:39 +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
Aryadev Chavali
14a3820e74 Implemented new types of EQ, forced all comparators to push bytes
Just need to call their unsigned versions.

All comparators should push bytes as it makes return types uniform.
2023-10-31 21:05:25 +00:00
Aryadev Chavali
036d3dcfba vm_execute_all: Print every cycle on VERBOSE >= 2, just print final state otherwise
Changed VERBOSE checks to ensure a degree of information.
2023-10-31 21:00:13 +00:00
Aryadev Chavali
4d8b855d87 Created routines for new comparator opcodes (not implemented)
Will cause error if used currently, which is fine.
2023-10-31 20:59:01 +00:00
Aryadev Chavali
0975d92493 Introduced new instructions for comparison
Comparing signed and unsigned versions of numbers.  Same for EQ as
well.

Notice the irregular pattern of BYTE, CHAR, INT, HWORD,LONG,WORD as
OPCODE_IS_TYPE requires the subcodes to be surrounded by BYTE and
WORD.
2023-10-31 20:50:28 +00:00
Aryadev Chavali
7ca8f2c644 asm/main logs are now indented and look prettier 2023-10-31 20:39:49 +00:00
Aryadev Chavali
75dc36cd19 Lexer now returns errors on failure
Currently only for invalid character literals, but still a possible
problem.
2023-10-31 20:39:26 +00:00
Aryadev Chavali
fa640f13e8 parse_word deals with characters now
Just takes the character literally as a number.
2023-10-31 20:38:03 +00:00
Aryadev Chavali
228f548bd9 Changed asm/parser instruction push-reg->push.reg 2023-10-31 20:37:11 +00:00
Aryadev Chavali
c534a53b6f Make Verbose a universal flag
All objects target LIB anyway, so VERBOSE is a universal macro for
this code base.
2023-10-29 16:59:37 +00:00
Aryadev Chavali
157c79d53c Added a "usage" message and colours for assembler
Prints useful and pretty messages when verbose being at least 1.
2023-10-29 16:59:31 +00:00
Aryadev Chavali
597a45aa73 Fixed bug where JUMP_REGISTER couldn't be in bytecode read 2023-10-29 16:58:18 +00:00
Aryadev Chavali
c4cb45f3ad Fixed bug where reading operand bytecode may stop prematurely
This is due to checking for equality instead of just greater than in
darr->used against darr->available.
2023-10-29 16:56:33 +00:00
Aryadev Chavali
7c367f95c9 Added some examples to instruction-test for positive/negative literals
Use the push.* instructions to see this.
2023-10-29 16:56:19 +00:00
Aryadev Chavali
025b26b877 Introduced an example source file for all instructions
Provides calling conventions, ensures parser and lexer are working
correctly.  Will be updated as more instructions are introduced and
supported in the assembler.
2023-10-29 16:56:19 +00:00
Aryadev Chavali
1177a5a45b Auto-fill copyright notice in examples/fib.c 2023-10-29 16:56:19 +00:00
Aryadev Chavali
1c0bd20cba Introduce error reporting in asm/main
Pretty simple implementation, I've stopped printing the tokens cos I
think the lexer is done.
2023-10-28 18:22:18 +01:00
Aryadev Chavali
eac8cbf1da asm/parser supports all opcodes, introduced parse errors
Introduced some functions to parse differing types of opcodes.  Use
the same style of a.b.c... for namespacing or type specification for
certain opcodes.  Bit hacky and not tested, but does work.

Parse errors can be reported with an exact location using the token
column, line.
2023-10-28 18:21:09 +01:00
Aryadev Chavali
191fe5c6b8 Ignore comments (using semicolons) in lexer
Easier to do it here than at the parser.
2023-10-28 18:19:33 +01:00
Aryadev Chavali
d2429aa549 Introduced a column and line for each token
Accurate error reporting can be introduced using this.
2023-10-28 18:19:30 +01:00
Aryadev Chavali
32dfcc109c Added macro to do safe subtractions on a word
Default C just lets overflows occur for subtraction, so this macro
will default to 0 if the subtraction causes an overflow.
2023-10-28 18:16:50 +01:00
Aryadev Chavali
fc81cda96b Plugged in asm/parser to asm/main
Just prints instructions so far.
2023-10-26 11:18:07 +01:00
Aryadev Chavali
2fe2af22a9 Implemented a rudimentary parser with support for 4 instruction types 2023-10-26 11:17:55 +01:00
Aryadev Chavali
dca51106a2 Added support in lexer for negative numbers
Though we deal with unsigned numbers internally, it should be possible
to read and manipulate negative numbers through 2s complement.  Later
on we'll add support for signed operations via 2s complement, so this
should be allowed.
2023-10-26 11:17:01 +01:00
Aryadev Chavali
e9c54e045a Fixed bug where printing hword of an instruction prints number not hex
This is an easy fix.
2023-10-26 11:16:02 +01:00
Aryadev Chavali
c70a9d9879 asm/main now uses TOKEN_STREAM_AT 2023-10-26 10:23:00 +01:00
Aryadev Chavali
b152365561 Lexer forces uppercase for symbols 2023-10-26 10:22:53 +01:00
Aryadev Chavali
3200e97324 Updated README for targeting VM 2023-10-26 10:22:26 +01:00
Aryadev Chavali
d1f84a6a2c Makefile now prints dependencies on successful compilation 2023-10-26 10:22:10 +01:00
Aryadev Chavali
74a85268c4 Moved base functions from inst.c to dedicated file
Doesn't make sense for them to be in the VM module when they have a
more general purpose now.
2023-10-26 10:19:10 +01:00
Aryadev Chavali
c8c5381cf2 vm/main takes a filename as input to execute bytecode
Also prints a usage for incorrect usage.
2023-10-26 10:18:53 +01:00
Aryadev Chavali
a07b571da2 Auto fill licenses 2023-10-26 08:01:26 +01:00
Aryadev Chavali
7bf6b53230 Unified literal for numbers, main program now tokenises 2023-10-26 07:15:52 +01:00
Aryadev Chavali
131d70a9a3 Started working on a parser
No implementations yet
2023-10-25 21:43:51 +01:00
Aryadev Chavali
94abb9e3d4 Separated lexer from main file in asm 2023-10-25 21:43:13 +01:00
Aryadev Chavali
dbbfac1236 Wrote lexer for assembly
Pretty simple tokeniser, doesn't do a lot and needs to error check better.
2023-10-24 18:20:59 +01:00