store current_pos in VM for better logging

This commit is contained in:
Timerix 2024-11-16 22:05:20 +05:00
parent 83172df776
commit 9d2a26fae3
12 changed files with 110 additions and 51 deletions

View File

@ -6,11 +6,15 @@ void VM_init(VM* vm){
vm->state = VMState_Initialized;
}
void VM_setErrorMessage(VM* vm, const char* format, ...){
void _VM_setError(VM* vm, const char* context, const char* format, ...){
va_list argv;
char position_str[32];
sprintf(position_str, "[at 0x%x][", (u32)vm->current_pos);
char* real_format = strcat_malloc(position_str, context, "] ", format);
va_start(argv, format);
char* NULLABLE(buf) = vsprintf_malloc(256, format, argv);
char* NULLABLE(buf) = vsprintf_malloc(256, real_format, argv);
va_end(argv);
free(real_format);
if(buf == NULL){
buf = malloc(16);
strcpy(buf, "SPRINTF FAILED");
@ -20,11 +24,11 @@ void VM_setErrorMessage(VM* vm, const char* format, ...){
bool VM_loadProgram(VM* vm, u8* data, size_t size){
if(data == NULL){
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
VM_setError(vm, "data == NULL");
return false;
}
if(size == 0){
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because size == 0");
VM_setError(vm, "size == 0");
return false;
}
@ -35,30 +39,30 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size){
i32 VM_executeProgram(VM* vm){
if(vm->data == NULL){
VM_setErrorMessage(vm, "[VM_executeProgram] data is null");
VM_setError(vm, "data == null");
return -1;
}
size_t pos = 0;
while (pos < vm->data_size){
u8 opcode = vm->data[pos];
vm->current_pos = 0;
while (vm->current_pos < vm->data_size){
u8 opcode = vm->data[vm->current_pos];
const Instruction* instr = Instruction_getFromOpcode(opcode);
if(instr == NULL){
VM_setErrorMessage(vm, "[%p] unknown opcode %02x", (void*)pos, opcode);
VM_setError(vm, "unknown opcode %02X", opcode);
return -1;
}
pos++;
i32 bytes_read = instr->implementation(vm, pos);
vm->current_pos++;
i32 bytes_read = instr->implementation(vm);
if(bytes_read < 0)
return -1;
pos += bytes_read;
vm->current_pos += bytes_read;
}
if(vm->state != VMState_Exited){
VM_setErrorMessage(vm, "[%p] unexpected end of program", (void*)pos);
VM_setError(vm, "unexpected end of the program");
return -1;
}
@ -68,7 +72,8 @@ i32 VM_executeProgram(VM* vm){
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
if(pos + size >= vm->data_size){
VM_setErrorMessage(vm, "[%p] unexpected end of data", (void*)vm->data_size);
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable",
size, (u32)pos, vm->data_size - size);
return false;
}

View File

@ -49,6 +49,7 @@ typedef struct VM {
u8* data;
size_t data_size;
size_t current_pos;
} VM;
void VM_init(VM* vm);
@ -64,4 +65,5 @@ i32 VM_executeProgram(VM* vm);
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
void VM_setErrorMessage(VM* vm, const char* format, ...) __attribute__((__format__ (__printf__, 2, 3)));
#define VM_setError(vm, format, ...) _VM_setError(vm, __func__, format ,##__VA_ARGS__)
void _VM_setError(VM* vm, const char* context, const char* format, ...) __attribute__((__format__(__printf__, 3, 4)));

View File

@ -2,7 +2,7 @@
/// EXIT
/// ax - exit code
i32 EXIT_impl(VM* vm, size_t pos){
i32 EXIT_impl(VM* vm){
vm->state = VMState_Exited;
return 0;
}

View File

@ -1,13 +1,13 @@
#include "impl_macros.h"
/// MOV [dst_register] [src_register]
i32 MOV_impl(VM* vm, size_t pos){
i32 MOV_impl(VM* vm){
u8 dst_register_i = 0;
readRegisterVar(dst_register_i);
u8 src_register_i = 0;
readRegisterVar(src_register_i);
if(dst_register_i == src_register_i){
VM_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, src_register_i);
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);
return -1;
}

View File

@ -1,6 +1,6 @@
#include "impl_macros.h"
/// NOP
i32 NOP_impl(VM* vm, size_t pos){
i32 NOP_impl(VM* vm){
return 0;
}

View File

@ -1,14 +1,14 @@
#include "impl_macros.h"
/// PUSH [dst_register] [value_size] [value]
i32 PUSH_impl(VM* vm, size_t pos){
i32 PUSH_impl(VM* vm){
u8 dst_register_i = 0;
readRegisterVar(dst_register_i);
u8 value_size = 0;
readValueSizeVar(value_size);
vm->registers[dst_register_i].u32v = 0;
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, pos, value_size))
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
return -1;
return sizeof(dst_register_i) + sizeof(value_size) + value_size;

View File

@ -1,13 +1,13 @@
#include "impl_macros.h"
FILE* NULLABLE(fileFromN)(VM* vm, size_t pos, u32 file_n){
FILE* NULLABLE(fileFromN)(VM* vm, u32 file_n){
FILE* f = NULL;
switch(file_n){
case 0: f = stdin; break;
case 1: f = stdout; break;
case 2: f = stderr; break;
default:
VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n);
VM_setError(vm, "invalid file_n (%x) ", file_n);
break;
}
@ -16,7 +16,7 @@ FILE* NULLABLE(fileFromN)(VM* vm, size_t pos, u32 file_n){
/// SYS
/// ax - func code
i32 SYS_impl(VM* vm, size_t pos){
i32 SYS_impl(VM* vm){
u8 func_code = vm->ax.u8v0;
size_t result_code = 0;
switch(func_code){
@ -25,17 +25,17 @@ i32 SYS_impl(VM* vm, size_t pos){
// cx - buffer ptr
// dx - buffer size
case 0:;
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, vm->bx.u32v));
break;
// sys_write
// bx - file n
// cx - buffer ptr
// dx - buffer size
case 1:;
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, vm->bx.u32v));
break;
default:
VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code);
VM_setError(vm, "invalid system call (%x) ", func_code);
return -1;
}

View File

@ -2,14 +2,14 @@
#include "../instructions.h"
#define readVar(VAR) {\
if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
return -1;\
pos += sizeof(VAR);\
vm->current_pos += sizeof(VAR);\
}
#define validateRegisterIndex(VAR) {\
if(VAR > sizeof(vm->registers)){\
VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\
VM_setError(vm, "invalid register index (%x)", VAR);\
return -1;\
}\
}
@ -21,7 +21,7 @@
#define validateValueSize(VAR) {\
if(VAR < 1 || VAR > 4){\
VM_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\
VM_setError(vm, "invalid value_size (%x)", VAR);\
return -1;\
}\
}

View File

@ -22,26 +22,26 @@
}
/// ADD [dst_register] [src_register]
i32 ADD_impl(VM* vm, size_t pos){
i32 ADD_impl(VM* vm){
mathOperatorImpl(+);
}
/// SUB [dst_register] [src_register]
i32 SUB_impl(VM* vm, size_t pos){
i32 SUB_impl(VM* vm){
mathOperatorImpl(-);
}
/// MUL [dst_register] [src_register]
i32 MUL_impl(VM* vm, size_t pos){
i32 MUL_impl(VM* vm){
mathOperatorImpl(*)
}
/// DIV [dst_register] [src_register]
i32 DIV_impl(VM* vm, size_t pos){
i32 DIV_impl(VM* vm){
mathOperatorImpl(/)
}
/// MOD [dst_register] [src_register]
i32 MOD_impl(VM* vm, size_t pos){
i32 MOD_impl(VM* vm){
mathOperatorImpl(%)
}

View File

@ -3,7 +3,7 @@
///@param program_pos position in vm->program next afrer opcode
///@returns number of bytes read
typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos);
typedef i32 (*InstructionImplFunc_t)(VM* vm);
typedef struct Instruction {
const char* name;
@ -20,15 +20,15 @@ typedef struct Instruction {
/// @return ptr to struct or NULL
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode);
i32 NOP_impl(VM* vm, size_t pos);
i32 PUSH_impl(VM* vm, size_t pos);
i32 MOV_impl(VM* vm, size_t pos);
i32 ADD_impl(VM* vm, size_t pos);
i32 SUB_impl(VM* vm, size_t pos);
i32 MUL_impl(VM* vm, size_t pos);
i32 DIV_impl(VM* vm, size_t pos);
i32 MOD_impl(VM* vm, size_t pos);
i32 SYS_impl(VM* vm, size_t pos);
i32 EXIT_impl(VM* vm, size_t pos);
i32 JMP_impl(VM* vm, size_t pos);
i32 CALL_impl(VM* vm, size_t pos);
i32 NOP_impl(VM* vm);
i32 PUSH_impl(VM* vm);
i32 MOV_impl(VM* vm);
i32 ADD_impl(VM* vm);
i32 SUB_impl(VM* vm);
i32 MUL_impl(VM* vm);
i32 DIV_impl(VM* vm);
i32 MOD_impl(VM* vm);
i32 SYS_impl(VM* vm);
i32 EXIT_impl(VM* vm);
i32 JMP_impl(VM* vm);
i32 CALL_impl(VM* vm);

View File

@ -19,7 +19,7 @@ i32 main(const i32 argc, const char** argv){
for(u8 opcode = 0; opcode < 255; opcode++){
const Instruction* instr = Instruction_getFromOpcode(opcode);
if(instr != NULL){
printf("%02x %s\n", opcode, instr->name);
printf("%02X %s\n", opcode, instr->name);
}
}
return 0;
@ -76,6 +76,40 @@ i32 main(const i32 argc, const char** argv){
return exit_code;
}
char* _strcat_malloc(size_t n, const char* str0, ...){
va_list argv;
va_start(argv, str0);
char* heap_ptr = _vstrcat_malloc(n, str0, argv);
va_end(argv);
return heap_ptr;
}
char* _vstrcat_malloc(size_t n, const char* str0, va_list argv){
size_t str0_len = strlen(str0);
size_t total_len = str0_len;
const char** const parts = malloc(sizeof(const char*) * n);
size_t* const part_lengths = malloc(sizeof(size_t) * n);
for(size_t i = 0; i < n; i++){
const char* part = va_arg(argv, const char*);
size_t length = strlen(part);
parts[i] = part;
part_lengths[i] = length;
total_len += length;
}
char* const buf = malloc(total_len + 1);
memcpy(buf, str0, str0_len);
char* walking_ptr = buf + str0_len;
for(size_t i = 0; i < n; i++){
memcpy(walking_ptr, parts[i], part_lengths[i]);
walking_ptr += part_lengths[i];
}
buf[total_len] = '\0';
free(parts);
free(part_lengths);
return buf;
}
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...){
va_list argv;
va_start(argv, format);

View File

@ -24,10 +24,28 @@ typedef u8 bool;
#define true 1
#define false 0
#define __count_args( \
a0, a1, a2, a3, a4, a5, a6, a7 , a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23, a24,a25,a26,a27,a28,a29,a30,a31, \
a32,a33,a34,a35,a36,a37,a38,a39, a40,a41,a42,a43,a44,a45,a46,a47, \
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h)
#define count_args(ARGS...) __count_args(ARGS, \
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
32,31,30,29,28,27,26,25, 24,23,22,21,20,19,18,17, \
16,15,14,13,12,11,10,9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
/// @warning pointer can be null
#define NULLABLE(NAME) NAME
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
char* _strcat_malloc(size_t n, const char* str0, ...);
char* _vstrcat_malloc(size_t n, const char* str0, va_list argv);
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...) __attribute__((__format__(__printf__, 2, 3)));
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv);