Fixed buffer overflow, bytecode is WIP

This commit is contained in:
2024-09-19 19:06:49 +10:00
parent 083ee950dd
commit e35af3d84b
3 changed files with 11 additions and 5 deletions

View File

@@ -1,13 +1,14 @@
#include "toy_bytecode.h" #include "toy_bytecode.h"
#include "toy_memory.h" #include "toy_memory.h"
#include "toy_routine.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
//utils //utils
static void expand(Toy_Bytecode* bc, int amount) { static void expand(Toy_Bytecode* bc, int amount) {
if (bc->count + amount > bc->capacity) { while (bc->count + amount > bc->capacity) {
int oldCapacity = bc->capacity; int oldCapacity = bc->capacity;
bc->capacity = TOY_GROW_CAPACITY(oldCapacity); bc->capacity = TOY_GROW_CAPACITY(oldCapacity);
@@ -21,7 +22,7 @@ static void emitByte(Toy_Bytecode* bc, unsigned char byte) {
} }
static void writeModule(Toy_Bytecode* bc, Toy_Ast* ast) { static void writeModule(Toy_Bytecode* bc, Toy_Ast* ast) {
// //TODO: routines
} }
//bytecode //bytecode

View File

@@ -6,8 +6,8 @@
#include <string.h> #include <string.h>
//utils //utils
static void expand(void** handle, int* capacity, int* count) { static void expand(void** handle, int* capacity, int* count, int amount) {
if ((*count) +1 > (*capacity)) { while ((*count) + amount > (*capacity)) {
int oldCapacity = (*capacity); int oldCapacity = (*capacity);
(*capacity) = TOY_GROW_CAPACITY(oldCapacity); (*capacity) = TOY_GROW_CAPACITY(oldCapacity);
@@ -16,7 +16,7 @@ static void expand(void** handle, int* capacity, int* count) {
} }
static void emitByte(void** handle, int* capacity, int* count, unsigned char byte) { static void emitByte(void** handle, int* capacity, int* count, unsigned char byte) {
expand(handle, capacity, count); expand(handle, capacity, count, 1);
((unsigned char*)(*handle))[(*count)++] = byte; ((unsigned char*)(*handle))[(*count)++] = byte;
} }

View File

@@ -24,6 +24,9 @@ int test_bytecode_header(Toy_Bucket* bucket) {
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to write the bytecode header correctly:\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: failed to write the bytecode header correctly:\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, bc.ptr[0], bc.ptr[1], bc.ptr[2], (char*)(bc.ptr + 3)); fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, bc.ptr[0], bc.ptr[1], bc.ptr[2], (char*)(bc.ptr + 3));
fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD); fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);
//cleanup and return
Toy_freeBytecode(bc);
return -1; return -1;
} }
@@ -35,6 +38,8 @@ int test_bytecode_header(Toy_Bucket* bucket) {
} }
int main() { int main() {
fprintf(stderr, TOY_CC_WARN "WARNING: Bytecode implementation incomplete\n" TOY_CC_RESET);
//run each test set, returning the total errors given //run each test set, returning the total errors given
int total = 0, res = 0; int total = 0, res = 0;