sprintf_malloc
This commit is contained in:
parent
b8296a45a7
commit
83172df776
14
src/VM/VM.c
14
src/VM/VM.c
@ -6,6 +6,18 @@ void VM_init(VM* vm){
|
|||||||
vm->state = VMState_Initialized;
|
vm->state = VMState_Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VM_setErrorMessage(VM* vm, const char* format, ...){
|
||||||
|
va_list argv;
|
||||||
|
va_start(argv, format);
|
||||||
|
char* NULLABLE(buf) = vsprintf_malloc(256, format, argv);
|
||||||
|
va_end(argv);
|
||||||
|
if(buf == NULL){
|
||||||
|
buf = malloc(16);
|
||||||
|
strcpy(buf, "SPRINTF FAILED");
|
||||||
|
}
|
||||||
|
vm->error_message = buf;
|
||||||
|
}
|
||||||
|
|
||||||
bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
||||||
if(data == NULL){
|
if(data == NULL){
|
||||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
|
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
|
||||||
@ -33,7 +45,7 @@ i32 VM_executeProgram(VM* vm){
|
|||||||
|
|
||||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
||||||
if(instr == NULL){
|
if(instr == NULL){
|
||||||
|
VM_setErrorMessage(vm, "[%p] unknown opcode %02x", (void*)pos, opcode);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/VM/VM.h
10
src/VM/VM.h
@ -45,7 +45,7 @@ typedef struct VM {
|
|||||||
};
|
};
|
||||||
|
|
||||||
VMState state;
|
VMState state;
|
||||||
NULLABLE(char* error_message);
|
char* NULLABLE(error_message);
|
||||||
|
|
||||||
u8* data;
|
u8* data;
|
||||||
size_t data_size;
|
size_t data_size;
|
||||||
@ -62,10 +62,6 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size);
|
|||||||
/// @return program exit code or -1 on error (check vm.error_message)
|
/// @return program exit code or -1 on error (check vm.error_message)
|
||||||
i32 VM_executeProgram(VM* vm);
|
i32 VM_executeProgram(VM* vm);
|
||||||
|
|
||||||
#define VM_setErrorMessage(V, FORMAT, ...) {\
|
|
||||||
char* buf = malloc(256);\
|
|
||||||
sprintf(buf, FORMAT, ##__VA_ARGS__);\
|
|
||||||
vm->error_message = buf;\
|
|
||||||
}
|
|
||||||
|
|
||||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
|
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)));
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
NULLABLE(FILE* fileFromN(VM* vm, size_t pos, u32 file_n)){
|
FILE* NULLABLE(fileFromN)(VM* vm, size_t pos, u32 file_n){
|
||||||
FILE* f = NULL;
|
FILE* f = NULL;
|
||||||
switch(file_n){
|
switch(file_n){
|
||||||
case 0: f = stdin; break;
|
case 0: f = stdin; break;
|
||||||
|
|||||||
@ -16,7 +16,7 @@ const Instruction instructions[] = {
|
|||||||
};
|
};
|
||||||
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
||||||
|
|
||||||
NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode)){
|
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode){
|
||||||
if(opcode >= instructions_count)
|
if(opcode >= instructions_count)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|||||||
@ -18,7 +18,7 @@ typedef struct Instruction {
|
|||||||
/// @brief get instruction info from table
|
/// @brief get instruction info from table
|
||||||
/// @param opcode any byte
|
/// @param opcode any byte
|
||||||
/// @return ptr to struct or NULL
|
/// @return ptr to struct or NULL
|
||||||
NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode));
|
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode);
|
||||||
|
|
||||||
i32 NOP_impl(VM* vm, size_t pos);
|
i32 NOP_impl(VM* vm, size_t pos);
|
||||||
i32 PUSH_impl(VM* vm, size_t pos);
|
i32 PUSH_impl(VM* vm, size_t pos);
|
||||||
|
|||||||
18
src/main.c
18
src/main.c
@ -75,3 +75,21 @@ i32 main(const i32 argc, const char** argv){
|
|||||||
free(buffer);
|
free(buffer);
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...){
|
||||||
|
va_list argv;
|
||||||
|
va_start(argv, format);
|
||||||
|
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
|
||||||
|
va_end(argv);
|
||||||
|
return heap_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv){
|
||||||
|
char* buf = malloc(buffer_size);
|
||||||
|
int r = vsprintf_s(buf, buffer_size, format, argv);
|
||||||
|
if(r < 0){
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|||||||
@ -27,4 +27,7 @@ typedef u8 bool;
|
|||||||
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||||
|
|
||||||
/// @warning pointer can be null
|
/// @warning pointer can be null
|
||||||
#define NULLABLE(DECLARATION) DECLARATION
|
#define NULLABLE(NAME) NAME
|
||||||
|
|
||||||
|
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);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user