Tweaked 4-byte alignment function

Fixed #140

Thanks @8051enthusiast
This commit is contained in:
2024-10-14 02:07:24 +11:00
parent a51c5591ee
commit 694e262ee2
3 changed files with 18 additions and 14 deletions

View File

@@ -50,6 +50,7 @@ jobs:
commands: commands:
- { build: make repl, run: out/repl.exe -f '../scripts/example.toy' } - { build: make repl, run: out/repl.exe -f '../scripts/example.toy' }
- { build: make repl, run: out/repl.exe -f '../scripts/example-print.toy' } - { build: make repl, run: out/repl.exe -f '../scripts/example-print.toy' }
- { build: make repl, run: out/repl.exe -f '../scripts/example-variables.toy' }
runs-on: ${{ matrix.platforms.os }} runs-on: ${{ matrix.platforms.os }}
steps: steps:

View File

@@ -29,6 +29,9 @@ print 6 * 7;
//strings can be concatenated with the .. operator //strings can be concatenated with the .. operator
print "Hello" .. "world!"; print "Hello" .. "world!";
//declare a variable
var foobar = 42;
//more examples to be added as the features are implemented //more examples to be added as the features are implemented
``` ```
@@ -53,6 +56,7 @@ This source code is covered by the zlib license (see [LICENSE.md](LICENSE.md)).
For a guide on how you can contribute, see [CONTRIBUTING.md](CONTRIBUTING.md). For a guide on how you can contribute, see [CONTRIBUTING.md](CONTRIBUTING.md).
@8051Enthusiast - `fixAlignment()` trick
@hiperiondev - v1 Disassembler, v1 porting support and feedback @hiperiondev - v1 Disassembler, v1 porting support and feedback
@add00 - v1 Library support @add00 - v1 Library support
@gruelingpine185 - Unofficial v1 MacOS support @gruelingpine185 - Unofficial v1 MacOS support

View File

@@ -15,24 +15,23 @@
vm->routine[vm->routineCounter++] vm->routine[vm->routineCounter++]
#define READ_UNSIGNED_INT(vm) \ #define READ_UNSIGNED_INT(vm) \
*((unsigned int*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) *((unsigned int*)(vm->routine + readPostfixUtil(&(vm->routineCounter), 4)))
#define READ_INT(vm) \ #define READ_INT(vm) \
*((int*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) *((int*)(vm->routine + readPostfixUtil(&(vm->routineCounter), 4)))
#define READ_FLOAT(vm) \ #define READ_FLOAT(vm) \
*((float*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) *((float*)(vm->routine + readPostfixUtil(&(vm->routineCounter), 4)))
static inline int _read_postfix(unsigned int* ptr, int amount) { static inline int readPostfixUtil(unsigned int* ptr, int amount) {
int ret = *ptr; int ret = *ptr;
*ptr += amount; *ptr += amount;
return ret; return ret;
} }
static inline void fix_alignment(Toy_VM* vm) { static inline void fixAlignment(Toy_VM* vm) {
if (vm->routineCounter % 4 != 0) { //NOTE: It's a tilde, not a negative sign
vm->routineCounter += (4 - vm->routineCounter % 4); vm->routineCounter = (vm->routineCounter + 3) & ~0b11;
}
} }
//instruction handlers //instruction handlers
@@ -53,19 +52,19 @@ static void processRead(Toy_VM* vm) {
} }
case TOY_VALUE_INTEGER: { case TOY_VALUE_INTEGER: {
fix_alignment(vm); fixAlignment(vm);
value = TOY_VALUE_FROM_INTEGER(READ_INT(vm)); value = TOY_VALUE_FROM_INTEGER(READ_INT(vm));
break; break;
} }
case TOY_VALUE_FLOAT: { case TOY_VALUE_FLOAT: {
fix_alignment(vm); fixAlignment(vm);
value = TOY_VALUE_FROM_FLOAT(READ_FLOAT(vm)); value = TOY_VALUE_FROM_FLOAT(READ_FLOAT(vm));
break; break;
} }
case TOY_VALUE_STRING: { case TOY_VALUE_STRING: {
fix_alignment(vm); fixAlignment(vm);
//grab the jump as an integer //grab the jump as an integer
unsigned int jump = *(unsigned int*)(vm->routine + vm->jumpsAddr + READ_INT(vm)); unsigned int jump = *(unsigned int*)(vm->routine + vm->jumpsAddr + READ_INT(vm));
@@ -107,13 +106,13 @@ static void processRead(Toy_VM* vm) {
Toy_pushStack(&vm->stack, value); Toy_pushStack(&vm->stack, value);
//leave the counter in a good spot //leave the counter in a good spot
fix_alignment(vm); fixAlignment(vm);
} }
static void processDeclare(Toy_VM* vm) { static void processDeclare(Toy_VM* vm) {
Toy_ValueType type = READ_BYTE(vm); //variable type Toy_ValueType type = READ_BYTE(vm); //variable type
unsigned int len = READ_BYTE(vm); //name length unsigned int len = READ_BYTE(vm); //name length
fix_alignment(vm); //one spare byte fixAlignment(vm); //one spare byte
//grab the jump //grab the jump
unsigned int jump = *(unsigned int*)(vm->routine + vm->jumpsAddr + READ_INT(vm)); unsigned int jump = *(unsigned int*)(vm->routine + vm->jumpsAddr + READ_INT(vm));
@@ -412,7 +411,7 @@ static void process(Toy_VM* vm) {
} }
//prepare for the next instruction //prepare for the next instruction
fix_alignment(vm); fixAlignment(vm);
} }
} }