Moved logging macros to base.h and use them everywhere
The macros used in the testing library are actually useful everywhere so may as well use them.
This commit is contained in:
10
lib/base.h
10
lib/base.h
@@ -27,6 +27,16 @@
|
|||||||
#define TERM_RED "\033[31m"
|
#define TERM_RED "\033[31m"
|
||||||
#define TERM_RESET "\033[0m"
|
#define TERM_RESET "\033[0m"
|
||||||
|
|
||||||
|
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
|
||||||
|
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
||||||
|
|
||||||
|
#define INFO(NAME, FORMAT, ...) \
|
||||||
|
MESSAGE(stdout, TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
|
||||||
|
#define FAIL(NAME, FORMAT, ...) \
|
||||||
|
MESSAGE(stderr, TERM_RED, NAME, FORMAT, __VA_ARGS__)
|
||||||
|
#define SUCCESS(NAME, FORMAT, ...) \
|
||||||
|
MESSAGE(stdout, TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
|
||||||
|
|
||||||
// Flags for program behaviour (usually related to printing)
|
// Flags for program behaviour (usually related to printing)
|
||||||
#ifndef VERBOSE
|
#ifndef VERBOSE
|
||||||
#define VERBOSE 0
|
#define VERBOSE 0
|
||||||
|
|||||||
@@ -24,16 +24,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
|
|
||||||
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define INFO(NAME, FORMAT, ...) \
|
|
||||||
MESSAGE(stdout, TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
|
|
||||||
#define FAIL(NAME, FORMAT, ...) \
|
|
||||||
MESSAGE(stderr, TERM_RED, NAME, FORMAT, __VA_ARGS__)
|
|
||||||
#define SUCCESS(NAME, FORMAT, ...) \
|
|
||||||
MESSAGE(stdout, TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
|
|
||||||
|
|
||||||
typedef void (*test_fn)(void);
|
typedef void (*test_fn)(void);
|
||||||
struct Test
|
struct Test
|
||||||
{
|
{
|
||||||
|
|||||||
22
vm/main.c
22
vm/main.c
@@ -13,6 +13,7 @@
|
|||||||
* Description: Entrypoint to program
|
* Description: Entrypoint to program
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "lib/base.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ int main(int argc, char *argv[])
|
|||||||
const char *filename = argv[1];
|
const char *filename = argv[1];
|
||||||
|
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
printf("[" TERM_YELLOW "INTERPRETER" TERM_RESET "]: `%s`\n", filename);
|
INFO("INTERPRETER", "`%s`\n", filename);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FILE *fp = fopen(filename, "rb");
|
FILE *fp = fopen(filename, "rb");
|
||||||
@@ -52,8 +53,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (!header_read)
|
if (!header_read)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[ERROR]: Could not deserialise program header in `%s`\n",
|
FAIL("ERROR", "Could not deserialise program header in `%s`\n", filename);
|
||||||
filename);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Ensure that we MUST have something to read
|
// Ensure that we MUST have something to read
|
||||||
@@ -69,7 +69,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (bytes_read == 0)
|
if (bytes_read == 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[ERROR]:%s [%lu]:", filename, read_err.index);
|
FAIL("ERROR", "%s [%lu]:", filename, read_err.index);
|
||||||
switch (read_err.type)
|
switch (read_err.type)
|
||||||
{
|
{
|
||||||
case READ_ERR_INVALID_OPCODE:
|
case READ_ERR_INVALID_OPCODE:
|
||||||
@@ -91,8 +91,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
printf("\t[" TERM_GREEN "SETUP" TERM_RESET "]: Read %lu instructions\n",
|
SUCCESS("SETUP", "Read %lu instructions\n", program.count);
|
||||||
program.count);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size_t stack_size = 256;
|
size_t stack_size = 256;
|
||||||
@@ -112,11 +111,8 @@ int main(int argc, char *argv[])
|
|||||||
vm_load_call_stack(&vm, call_stack, call_stack_size);
|
vm_load_call_stack(&vm, call_stack, call_stack_size);
|
||||||
|
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
printf("\t[" TERM_GREEN "SETUP" TERM_RESET
|
SUCCESS("SETUP", "Loaded internals\n%s", "");
|
||||||
"]: Loaded stack and program into VM\n");
|
INFO("INTERPRETER", "Beginning execution\n%s", "");
|
||||||
#endif
|
|
||||||
#if VERBOSE >= 1
|
|
||||||
printf("[" TERM_YELLOW "INTERPRETER" TERM_RESET "]: Beginning execution\n");
|
|
||||||
#endif
|
#endif
|
||||||
err_t err = vm_execute_all(&vm);
|
err_t err = vm_execute_all(&vm);
|
||||||
|
|
||||||
@@ -124,7 +120,7 @@ int main(int argc, char *argv[])
|
|||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
const char *error_str = err_as_cstr(err);
|
const char *error_str = err_as_cstr(err);
|
||||||
fprintf(stderr, "[ERROR]: %s\n", error_str);
|
FAIL("ERROR", "%s\n", error_str);
|
||||||
vm_print_all(&vm, stderr);
|
vm_print_all(&vm, stderr);
|
||||||
ret = 255 - err;
|
ret = 255 - err;
|
||||||
}
|
}
|
||||||
@@ -132,7 +128,7 @@ int main(int argc, char *argv[])
|
|||||||
vm_stop(&vm);
|
vm_stop(&vm);
|
||||||
|
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
printf("[%sINTERPRETER%s]: Finished execution\n", TERM_GREEN, TERM_RESET);
|
SUCCESS("INTEPRETER", "Finished execution\n%s", "");
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ err_t vm_execute_all(vm_t *vm)
|
|||||||
program->data.instructions[program->ptr].opcode != OP_HALT)
|
program->data.instructions[program->ptr].opcode != OP_HALT)
|
||||||
{
|
{
|
||||||
#if VERBOSE >= 2
|
#if VERBOSE >= 2
|
||||||
fprintf(stdout, "[vm_execute_all]: Trace(Cycle %lu)\n", cycles);
|
INFO("vm_execute_all", "Trace(Cycle%lu)\n", cycles);
|
||||||
fputs(
|
fputs(
|
||||||
"----------------------------------------------------------------------"
|
"----------------------------------------------------------------------"
|
||||||
"----------\n",
|
"----------\n",
|
||||||
@@ -319,8 +319,7 @@ err_t vm_execute_all(vm_t *vm)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
fprintf(stdout, "[%svm_execute_all%s]: Final VM state(Cycle %lu)\n",
|
INFO("vm_execute_all", "Final VM State(Cycle %lu)\n", cycles);
|
||||||
TERM_YELLOW, TERM_RESET, cycles);
|
|
||||||
vm_print_all(vm, stdout);
|
vm_print_all(vm, stdout);
|
||||||
#endif
|
#endif
|
||||||
return err;
|
return err;
|
||||||
|
|||||||
24
vm/struct.c
24
vm/struct.c
@@ -52,18 +52,16 @@ void vm_stop(vm_t *vm)
|
|||||||
{
|
{
|
||||||
#if VERBOSE >= 1
|
#if VERBOSE >= 1
|
||||||
bool leaks = false;
|
bool leaks = false;
|
||||||
printf("[" TERM_YELLOW "DATA" TERM_RESET "]: Checking for leaks...\n");
|
INFO("vm_stop", "Checking for leaks...\n%s", "");
|
||||||
if (vm->call_stack.ptr > 0)
|
if (vm->call_stack.ptr > 0)
|
||||||
{
|
{
|
||||||
leaks = true;
|
leaks = true;
|
||||||
printf("\t[" TERM_RED "DATA" TERM_RESET "]: Call stack at %lu\n\t[" TERM_RED
|
FAIL("vm_stop", "Call stack at %lu\n", vm->call_stack.ptr);
|
||||||
"DATA" TERM_RESET "]\n\t[" TERM_RED "DATA" TERM_RESET "]: Call "
|
FAIL("vm_stop", "Call stack trace:%s", "");
|
||||||
"stack trace:",
|
|
||||||
vm->call_stack.ptr);
|
|
||||||
for (size_t i = vm->call_stack.ptr; i > 0; --i)
|
for (size_t i = vm->call_stack.ptr; i > 0; --i)
|
||||||
{
|
{
|
||||||
word_t w = vm->call_stack.address_pointers[i - 1];
|
word_t w = vm->call_stack.address_pointers[i - 1];
|
||||||
printf("\t\t%lu: %lX", vm->call_stack.ptr - i, w);
|
printf("\t[%lu]: %lX", vm->call_stack.ptr - i, w);
|
||||||
if (i != 1)
|
if (i != 1)
|
||||||
printf(", ");
|
printf(", ");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@@ -80,22 +78,20 @@ void vm_stop(vm_t *vm)
|
|||||||
capacities[i] = cur->available;
|
capacities[i] = cur->available;
|
||||||
total_capacity += capacities[i];
|
total_capacity += capacities[i];
|
||||||
}
|
}
|
||||||
printf("\t[" TERM_RED "DATA" TERM_RESET
|
FAIL("vm_stop", "Heap: %luB (over %lu %s) not reclaimed\n", total_capacity,
|
||||||
"]: Heap: %luB (over %lu %s) not reclaimed\n",
|
size_pages, size_pages == 1 ? "page" : "pages");
|
||||||
total_capacity, size_pages, size_pages == 1 ? "page" : "pages");
|
|
||||||
for (size_t i = 0; i < size_pages; i++)
|
for (size_t i = 0; i < size_pages; i++)
|
||||||
printf("\t\t[%lu]: %luB lost\n", i, capacities[i]);
|
printf("\t[%lu]: %luB lost\n", i, capacities[i]);
|
||||||
}
|
}
|
||||||
if (vm->stack.ptr > 0)
|
if (vm->stack.ptr > 0)
|
||||||
{
|
{
|
||||||
leaks = true;
|
leaks = true;
|
||||||
printf("\t[" TERM_RED "DATA" TERM_RESET "]: Stack: %luB not reclaimed\n",
|
FAIL("vm_stop", "Stack: %luB not reclaimed\n", vm->stack.ptr);
|
||||||
vm->stack.ptr);
|
|
||||||
}
|
}
|
||||||
if (leaks)
|
if (leaks)
|
||||||
printf("[" TERM_RED "DATA" TERM_RESET "]: Leaks found\n");
|
FAIL("vm_stop", "Leaks found\n%s", "");
|
||||||
else
|
else
|
||||||
printf("[" TERM_GREEN "DATA" TERM_RESET "]: No leaks found\n");
|
SUCCESS("vm_stop", "No leaks found\n%s", "");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
vm->registers = (struct Registers){0};
|
vm->registers = (struct Registers){0};
|
||||||
|
|||||||
Reference in New Issue
Block a user