From 4ea2023da0b114cc5d20e7a4a571b73779205f03 Mon Sep 17 00:00:00 2001 From: cozis Date: Sun, 6 Mar 2022 16:43:56 +0100 Subject: [PATCH] removed unused code --- build.sh | 4 - src/buildvalue.c | 294 ----------------------- src/buildvalue.h | 6 - src/debug.c | 566 --------------------------------------------- src/debug.h | 8 - src/eval.c | 97 -------- src/eval.h | 4 - src/heap_reconst.c | 240 ------------------- src/heap_reconst.h | 15 -- src/noja.c | 1 - 10 files changed, 1235 deletions(-) delete mode 100644 src/buildvalue.c delete mode 100644 src/buildvalue.h delete mode 100644 src/debug.c delete mode 100644 src/debug.h delete mode 100644 src/eval.c delete mode 100644 src/eval.h delete mode 100644 src/heap_reconst.c delete mode 100644 src/heap_reconst.h diff --git a/build.sh b/build.sh index d267ea6..58b4f73 100755 --- a/build.sh +++ b/build.sh @@ -73,10 +73,6 @@ gcc tests/src/test-objects.c -o build/test-objects $FLAGS -Lbuild/ -lnoja-object gcc src/main.c \ src/noja.c \ - src/debug.c \ - src/eval.c \ - src/buildvalue.c \ - src/heap_reconst.c \ temp/o_builtins.o \ temp/o_net_builtins.o \ temp/utils/hash.o \ diff --git a/src/buildvalue.c b/src/buildvalue.c deleted file mode 100644 index 289ddde..0000000 --- a/src/buildvalue.c +++ /dev/null @@ -1,294 +0,0 @@ -#include -#include -#include -#include -#include "buildvalue.h" -#include "eval.h" - -static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error); - -static Object *walkMapValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error) -{ - assert(fmt[*i] == '{'); - - *i += 1; // Skip the '{'. - - while(isspace(fmt[*i])) - *i += 1; - - Object *map = Object_NewMap(-1, heap, error); - - if(map == NULL) - return NULL; - - if(fmt[*i] != '}') - while(1) - { - Object *key = walkValue(fmt, i, va, heap, error); - - if(key == NULL) - return NULL; - - while(isspace(fmt[*i])) - *i += 1; - - if(fmt[*i] == '\0') - { - Error_Report(error, 0, "Format ended inside a map expression"); - return NULL; - } - - if(fmt[*i] != ':') - { - Error_Report(error, 0, "Got unexpected '%c' in format where ':' was expected", fmt[*i]); - return NULL; - } - - *i += 1; // Skip the ':'. - - Object *val = walkValue(fmt, i, va, heap, error); - - if(val == NULL) - return NULL; - - if(!Object_Insert(map, key, val, heap, error)) - return NULL; - - while(isspace(fmt[*i])) - *i += 1; - - if(fmt[*i] == '\0') - { - Error_Report(error, 0, "Format ended inside a map expression"); - return NULL; - } - - if(fmt[*i] == '}') - break; - - if(fmt[*i] != ',') - { - Error_Report(error, 0, "Got unexpected '%c' in format where either ',' or '}' were expected", fmt[*i]); - return NULL; - } - - *i += 1; // Skip the ','. - } - - assert(fmt[*i] == '}'); - - *i += 1; // Skip the '}'. - - return map; -} -static Object *walkListValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error) -{ - assert(fmt[*i] == '['); - - *i += 1; // Skip the '['. - - while(isspace(fmt[*i])) - *i += 1; - - Object *maybe[8], **ptrs = maybe; - int ptrs_cap = sizeof(maybe) / sizeof(maybe[0]), - ptrs_num = 0; - - if(fmt[*i] != ']') - while(1) - { - Object *item = walkValue(fmt, i, va, heap, error); - - if(item == NULL) - { - if(ptrs != maybe) free(ptrs); - return NULL; - } - - if(ptrs_cap == ptrs_num) - { - int new_capacity = ptrs_cap * 2; - - Object **temp; - - if(ptrs == maybe) - { - temp = malloc(sizeof(Object*) * new_capacity); - - if(temp != NULL) - memcpy(temp, ptrs, ptrs_num * sizeof(Object*)); - } - else - temp = realloc(ptrs, sizeof(Object*) * new_capacity); - - if(temp == NULL) - { - Error_Report(error, 1, "Insufficient memory"); - if(ptrs != maybe) free(ptrs); - return NULL; - } - - ptrs = temp; - ptrs_cap = new_capacity; - } - - ptrs[ptrs_num++] = item; - - while(isspace(fmt[*i])) - *i += 1; - - if(fmt[*i] == '\0') - { - Error_Report(error, 0, "Format ended inside a list expression"); - if(ptrs != maybe) free(ptrs); - return NULL; - } - - if(fmt[*i] == ']') - break; - - if(fmt[*i] != ',') - { - Error_Report(error, 0, "Got unexpected '%c' in format where either ',' or ']' were expected", fmt[*i]); - if(ptrs != maybe) free(ptrs); - return NULL; - } - - *i += 1; // Skip the ','. - } - - assert(fmt[*i] == ']'); - - *i += 1; // Skip the ']'. - - Object *list = Object_NewList2(ptrs_num, ptrs, heap, error); - - if(ptrs != maybe) - free(ptrs); - - return list; -} - -static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error) -{ - while(isspace(fmt[*i])) - *i += 1; - - if(fmt[*i] == '\0') - { - Error_Report(error, 0, "Empty format"); - return NULL; - } - - if(fmt[*i] == '{') - return walkMapValue(fmt, i, va, heap, error); - - if(fmt[*i] == '[') - return walkListValue(fmt, i, va, heap, error); - - if(fmt[*i] != '$') - { - Error_Report(error, 0, "Unexpected character '%c' in format", fmt[*i]); - return NULL; - } - - assert(fmt[*i] == '$'); - - *i += 1; - - Object *o; - - switch(fmt[*i]) - { - case '\0': - Error_Report(error, 0, "Format ended after '$'"); - return NULL; - - case '{': - { - // Noja expression until the closing '}'. - - *i += 1; // Skip the '{'. - - int start = *i; - - while(fmt[*i] != '\0' && fmt[*i] != '}') // TODO: Escape '}'. - *i += 1; - - if(fmt[*i] == '\0') - { - // ERROR: Format ended inside of ${..}. - Error_Report(error, 0, "Format ended inside ${..}"); - return NULL; - } - - assert(fmt[*i] == '}'); - - int end = *i; - - // NOTE: The last '}' is skipped after the switch. - - const char *src = fmt + start; - int len = end - start; - - o = eval(src, len, NULL, heap, error); - - if(o == NULL) - return NULL; - break; - } - - case 'i': - o = Object_FromInt(va_arg(va, long long int), heap, error); - if(o == NULL) return NULL; - break; - - case 'f': - o = Object_FromFloat(va_arg(va, double), heap, error); - if(o == NULL) return NULL; - break; - - case 'b': - // NOTE: Actually we want a _Bool, not an int. - // But gcc says: - // > warning: ‘_Bool’ is promoted to ‘int’ when passed through ‘...’ - // > [...] - // > (so you should pass ‘int’ not ‘_Bool’ to ‘va_arg’) - // - o = Object_FromBool(va_arg(va, int), heap, error); - if(o == NULL) return NULL; - break; - - case 's': - o = Object_FromString(va_arg(va, char*), -1, heap, error); - if(o == NULL) return NULL; - break; - - case 'o': - o = va_arg(va, Object*); - assert(o != NULL); - break; - - default: - Error_Report(error, 0, "Invalid format specifier '%c'", fmt[*i]); - return NULL; - } - - *i += 1; - return o; -} - -Object *buildValue2(Heap *heap, Error *error, const char *fmt, va_list va) -{ - int i = 0; - return walkValue(fmt, &i, va, heap, error); -} - -Object *buildValue(Heap *heap, Error *error, const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - Object *o = buildValue2(heap, error, fmt, va); - va_end(va); - return o; -} \ No newline at end of file diff --git a/src/buildvalue.h b/src/buildvalue.h deleted file mode 100644 index e8b5461..0000000 --- a/src/buildvalue.h +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include "utils/error.h" -#include "objects/objects.h" - -Object *buildValue2(Heap *heap, Error *error, const char *fmt, va_list va); -Object *buildValue(Heap *heap, Error *error, const char *fmt, ...); \ No newline at end of file diff --git a/src/debug.c b/src/debug.c deleted file mode 100644 index 2b494c7..0000000 --- a/src/debug.c +++ /dev/null @@ -1,566 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "debug.h" -#include "utils/defs.h" -#include "heap_reconst.h" - -#define ANSI_COLOR_RED "\x1b[31m" -#define ANSI_COLOR_GREEN "\x1b[32m" -#define ANSI_COLOR_YELLOW "\x1b[33m" -#define ANSI_COLOR_BLUE "\x1b[34m" -#define ANSI_COLOR_MAGENTA "\x1b[35m" -#define ANSI_COLOR_CYAN "\x1b[36m" -#define ANSI_COLOR_RESET "\x1b[0m" - -typedef enum { - BY_LINE, - BY_INDEX, - BY_OFFSET, -} BreakPointMode; - -typedef struct { - BreakPointMode mode; - char *name; - union { - int line; - int index; - int offset; - }; -} BreakPoint; - -struct xDebug { - _Bool continuing; - BreakPoint bpoints[32]; - int bpoints_count; -}; - -Debug *Debug_New() -{ - Debug *dbg = malloc(sizeof(Debug)); - - if(dbg == NULL) - return NULL; - - dbg->continuing = 0; - dbg->bpoints_count = 0; - - return dbg; -} - -void Debug_Free(Debug *dbg) -{ - if(dbg) - { - for(int i = 0; i < dbg->bpoints_count; i += 1) - free(dbg->bpoints[i].name); - free(dbg); - } -} - -_Bool Debug_Callback(Runtime *runtime, void *userp) -{ - assert(runtime != NULL); - assert(userp != NULL); - - Debug *dbg = (Debug*) userp; - - const char *name = NULL; - int line = -1; - int index = -1; - int offset = -1; - int length = -1; - - { - Executable *exe = Runtime_GetCurrentExecutable(runtime); - assert(exe != NULL); - - index = Runtime_GetCurrentIndex(runtime); - assert(index >= 0); - - Source *src = Executable_GetSource(exe); - - if(src != NULL) - { - // Executable has source information. - offset = Executable_GetInstrOffset(exe, index); - assert(offset >= 0); - - length = Executable_GetInstrLength(exe, index); - assert(length >= 0); - - name = Source_GetName(src); - - const char *body = Source_GetBody(src); - assert(body != NULL); - - line = 1; - - int i = 0; - - while(i < offset) - { - if(body[i] == '\n') - line += 1; - - i += 1; - } - - assert(line > 0); - } - } - - // Check if we reached a breakpoint. - - _Bool hit = 0; - int idx; - - for(int i = 0; i < dbg->bpoints_count && hit == 0; i += 1) - { - if(!strcmp(dbg->bpoints[i].name, name)) - { - switch(dbg->bpoints[i].mode) - { - case BY_LINE: - if(dbg->bpoints[i].line == line) - hit = 1; - break; - - case BY_INDEX: - if(dbg->bpoints[i].index == index) - hit = 1; - break; - - case BY_OFFSET: - if(dbg->bpoints[i].offset >= offset && dbg->bpoints[i].offset < offset + length) - hit = 1; - break; - } - - if(hit) - idx = i; - } - } - - if(hit) - { - fprintf(stderr, "Hit breakpoint %d\n", idx); - dbg->continuing = 0; - } - else - { - if(dbg->continuing) - return 1; - } - - - char buffer[256]; - int buflen; - - char *argv[32]; - int argc; - - do { - - if(name == NULL && line == -1) - fprintf(stderr, "(unnamed)" ANSI_COLOR_GREEN " > " ANSI_COLOR_RESET); - else if(name == NULL && line > -1) - fprintf(stderr, "(unnamed):%d" ANSI_COLOR_GREEN " > " ANSI_COLOR_RESET, line); - else if(name != NULL && line == -1) - fprintf(stderr, "%s" ANSI_COLOR_GREEN " > " ANSI_COLOR_RESET, name); - else if(name != NULL && line > -1) - fprintf(stderr, "%s:%d" ANSI_COLOR_GREEN " > " ANSI_COLOR_RESET, name, line); - - { - buflen = 0; - - char c; - while((c = getc(stdin)) != '\n') - { - if(buflen < (int) sizeof(buffer)-1) - buffer[buflen] = c; - - buflen += 1; - } - - if(buflen > (int) sizeof(buffer)-1) - { - fprintf(stdout, - "Command is too long. The internal buffer can only contain %ld bytes.\n" - "Try inserting another command.\n", - sizeof(buffer)-1); - continue; - } - - buffer[buflen] = '\0'; - } - - // Now split the buffer into words in the (argc, argv) style. - - { - argc = 0; - - _Bool again = 0; - int curs = 0; - while(1) - { - // Skip whitespace. - while(isspace(buffer[curs])) - curs += 1; - - if(buffer[curs] == '\0') - break; - - int offset = curs; - - while(!isspace(buffer[curs]) && buffer[curs] != '\0') - curs += 1; - - int length = curs - offset; - - assert(length > 0); - - { - int max_argc = sizeof(argv) / sizeof(argv[0]); - - if(argc == max_argc) - { - fprintf(stdout, - "Command has too many words. The internal buffer can only contain %d words.\n" - "Try inserting another command.\n", - max_argc); - again = 1; - break; - } - - argv[argc++] = buffer + offset; - } - - if(buffer[curs] == '\0') - break; - - assert(isspace(buffer[curs])); - - buffer[curs] = '\0'; - - curs += 1; // Consume the space that ended the word (not overwritten by a zero byte). - } - - if(again) - continue; - } - - if(argc == 0) - continue; - - { - if(!strcmp(argv[0], "help")) - { - if(argc == 1) - { - fprintf(stderr, - "help .............. Show this message\n" - "help .... Display additional information for a given command\n" - "step .............. Run an instruction\n" - "quit .............. Stop execution\n" - "continue .......... Run until a breakpoint or the end of the code is reached\n" - "breakpoint ........ Add a breakpoint\n" - "stack ............. Show the contents of the stack\n" - "heap .............. Show the contents of the heap\n" - "disassembly ....... Show the current file's bytecode\n"); - } - else if(!strcmp(argv[1], "help")) - { - fprintf(stderr, - "\n" - " Command | help\n" - " | \n" - " Usage | > help []\n" - " | \n" - " Description | List all available commands or show detailed \n" - " | information about a given command.\n" - "\n"); - } - else if(!strcmp(argv[1], "step")) - { - fprintf(stderr, - "\n" - " Command | step\n" - " | \n" - " Usage | > step\n" - " | \n" - " Description | Run a single instruction.\n" - "\n"); - } - else if(!strcmp(argv[1], "quit")) - { - fprintf(stderr, - "\n" - " Command | quit\n" - " | \n" - " Usage | > quit\n" - " | \n" - " Description | Stop the execution.\n" - "\n"); - } - else if(!strcmp(argv[1], "continue")) - { - fprintf(stderr, - "\n" - " Command | continue\n" - " | \n" - " Usage | > continue\n" - " | \n" - " Description | Run until a breakpoint or the end of the code is\n" - " | reached.\n" - "\n"); - } - else if(!strcmp(argv[1], "disassembly")) - { - fprintf(stderr, - "\n" - " Command | disassembly\n" - " | \n" - " Usage | > disassembly\n" - " | \n" - " Description | Show the current file's bytecode.\n" - "\n"); - } - else if(!strcmp(argv[1], "breakpoint")) - { - fprintf(stderr, - "\n" - " Command | breakpoint\n" - " | \n" - " Usage | > breakpoint { line | offset | index } \n" - " | \n" - " Description | Add a breakpoint. The first argument of the command \n" - " | specifies the source containing the breakpoint, while \n" - " | the second one specifies the way the breakpoint is \n" - " | expressed: \n" - " | \n" - " | line | by line number.\n" - " | | \n" - " | offset | by character offset.\n" - " | | \n" - " | index | by instruction index (relative to the \n" - " | | start of the source executable's body).\n" - " | \n" - " | the third argument is, based on the second one, either\n" - " | a line number, a character offset or an instruction index.\n" - "\n"); - } - else if(!strcmp(argv[1], "stack")) - { - fprintf(stderr, - "\n" - " Command | stack\n" - " | \n" - " Usage | > stack\n" - " | \n" - " Description | Show the contents of the stack.\n" - "\n"); - } - else if(!strcmp(argv[1], "heap")) - { - fprintf(stderr, - "\n" - " Command | heap\n" - " | \n" - " Usage | > heap\n" - " | \n" - " Description | Show the contents of the heap.\n" - "\n"); - } - else - { - fprintf(stdout, "Unknown command \"%s\".\n", argv[1]); - } - } - else if(!strcmp(argv[0], "breakpoint")) - { - if(argc < 3) - { - fprintf(stderr, "\"breakpoint\" command expects 2 operands.\n"); - continue; - } - - int max_breakpoints = sizeof(dbg->bpoints) / sizeof(dbg->bpoints[0]); - - if(dbg->bpoints_count == max_breakpoints) - { - fprintf(stderr, "Can't add a breakpoint. The maximum number of breakpoints (%d) was reached.\n", max_breakpoints); - } - else - { - // Handle second argument. - - BreakPointMode mode; - - if(!strcmp(argv[2], "line")) - { - mode = BY_LINE; - } - else if(!strcmp(argv[2], "index")) - { - mode = BY_INDEX; - } - else if(!strcmp(argv[2], "offset")) - { - mode = BY_OFFSET; - } - else - { - fprintf(stderr, "Bad second argument \"%s\". It must be either one of \"line\", \"index\", \"offset\".\n", argv[2]); - continue; - } - - // Handle third argument. - - long long int N = strtoll(argv[3], NULL, 10); - - if(errno == ERANGE) - { - if(N == LLONG_MIN) - { - fprintf(stderr, "Bad third argument. Integer is too big.\n"); - } - else - { - assert(N == LLONG_MAX); - fprintf(stderr, "Bad third argument. Integer is too big.\n"); - } - continue; - } - else if(errno != 0) - { - fprintf(stderr, "Bad third argument. (strtoll says: %s).\n", strerror(errno)); - continue; - } - - // Handle first argument. - - char *name_copy = malloc(strlen(argv[1])+1); - - if(name_copy == NULL) - { - fprintf(stderr, "Uoops! No memory!"); - continue; - } - - strcpy(name_copy, argv[1]); - - dbg->bpoints[dbg->bpoints_count] = (BreakPoint) { - .mode = mode, - .name = name_copy, - .line = N, - }; - - dbg->bpoints_count += 1; - fprintf(stderr, "BreakPoint added.\n"); - } - } - else if(!strcmp(argv[0], "continue")) - { - dbg->continuing = 1; - return 1; - } - else if(!strcmp(argv[0], "disassembly")) - { - Executable_Dump(Runtime_GetCurrentExecutable(runtime)); - return 1; - } - else if(!strcmp(argv[0], "step")) - { - return 1; - } - else if(!strcmp(argv[0], "stack")) - { - Stack *stack = Runtime_GetStack(runtime); - assert(stack != NULL); - - if(Stack_Size(stack) == 0) - fprintf(stderr, "The stack is empty.\n"); - - for(int i = 0; i < (int) Stack_Size(stack); i += 1) - { - Object *obj = Stack_Top(stack, -i); - assert(obj != NULL); - - fprintf(stderr, " %d | ", i); - Object_Print(obj, stderr); - fprintf(stderr, "\n"); - } - } - else if(!strcmp(argv[0], "heap")) - { - ReconstructedHeap *rh = reconstruct_heap(runtime); - - if(rh == NULL) - { - fprintf(stderr, "Something went wrong.\n"); - } - else - { - if(rh->count == 0) - { - printf("The heap is empty.\n"); - } - else - { - _Bool in_pool = 0; - - for(int i = 0; i < rh->count; i += 1) - { - MemoryChunk *chunk = rh->chunks + i; - - if(in_pool == 0 && chunk->in_pool == 1) - { - printf("-- Pool start --\n"); - in_pool = 1; - } - else if(in_pool == 1 && chunk->in_pool == 0) - { - printf("-- Pool end --\n"); - in_pool = 0; - } - - if(chunk->parent == NULL) - printf("%d bytes at %p (%s)\n", - chunk->size, - chunk->addr, - Object_GetName(chunk->addr)); - else - printf("%d bytes at %p, extension of %p (%s)\n", - chunk->size, - chunk->addr, - chunk->parent, - Object_GetName(chunk->parent)); - } - - if(in_pool) - printf("-- Pool end --\n"); - } - - free(rh); - } - } - else if(!strcmp(argv[0], "quit")) - { - return 0; - } - else - { - fprintf(stdout, "Unknown command \"%s\". Type \"help\" is you need help.\n", argv[0]); - } - } - - } while(1); - - UNREACHABLE; - return 0; -} diff --git a/src/debug.h b/src/debug.h deleted file mode 100644 index 0aff082..0000000 --- a/src/debug.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef DEBUG_H -#define DEBUG_H -#include "runtime/runtime.h" -typedef struct xDebug Debug; -Debug *Debug_New(); -void Debug_Free(Debug *dbg); -_Bool Debug_Callback(Runtime *runtime, void *userp); -#endif \ No newline at end of file diff --git a/src/eval.c b/src/eval.c deleted file mode 100644 index f32adf2..0000000 --- a/src/eval.c +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include "compiler/parse.h" -#include "compiler/compile.h" -#include "runtime/runtime.h" -#include "o_builtins.h" -#include "eval.h" - -Object *evalf(Object *closure, Heap *heap, Error *error, const char *fmt, ...) -{ - char buffer[1024]; - - va_list va; - va_start(va, fmt); - int n = vsnprintf(buffer, sizeof(buffer), fmt, va); - va_end(va); - - if(n >= (int) sizeof(buffer)) - { - Error_Report(error, 1, "Static buffer is too small"); - return NULL; - } - - return eval(buffer, n, closure, heap, error); -} - -Object *eval(const char *str, int len, Object *closure, Heap *heap, Error *error) -{ - if(len < 0) - len = strlen(str); - - Source *s = Source_FromString(NULL, str, len, error); - - if(s == NULL) - return NULL; // Propagate. - - BPAlloc *alloc = BPAlloc_Init(-1); - - if(alloc == NULL) - { - Error_Report(error, 1, "Insufficient memory"); - return NULL; - } - - AST *ast = parse(s, alloc, error); - - if(ast == NULL) - { - BPAlloc_Free(alloc); - Source_Free(s); - return NULL; - } - - Executable *exe = compile(ast, alloc, error); - - if(exe == NULL) - { - BPAlloc_Free(alloc); - Source_Free(s); - return NULL; - } - - BPAlloc_Free(alloc); - Source_Free(s); - - Runtime *runt = Runtime_New2(-1, heap, 0, NULL, NULL); - - if(runt == NULL) - { - Error_Report(error, 1, "Insufficient memory"); - Executable_Free(exe); - return NULL; - } - - Object *bins = Object_NewBuiltinsMap(runt, heap, error); - - if(bins == NULL) - { - Runtime_Free(runt); - Executable_Free(exe); - return NULL; - } - - Runtime_SetBuiltins(runt, bins); - - Object *o = run(runt, error, exe, 0, closure, NULL, 0); - - if(o == NULL) - { - Runtime_Free(runt); - Executable_Free(exe); - return NULL; - } - - Runtime_Free(runt); - Executable_Free(exe); - return o; -} \ No newline at end of file diff --git a/src/eval.h b/src/eval.h deleted file mode 100644 index a21fe37..0000000 --- a/src/eval.h +++ /dev/null @@ -1,4 +0,0 @@ -#include "utils/error.h" -#include "objects/objects.h" -Object *eval(const char *str, int len, Object *closure, Heap *heap, Error *error); -Object *evalf(Object *closure, Heap *heap, Error *error, const char *fmt, ...); diff --git a/src/heap_reconst.c b/src/heap_reconst.c deleted file mode 100644 index c95bfd1..0000000 --- a/src/heap_reconst.c +++ /dev/null @@ -1,240 +0,0 @@ -#include -#include -#include -#include "heap_reconst.h" - -typedef struct { - Object *parent; - void *addr; - int size; -} Extension; - -typedef struct { - Object **objs; - Extension *exts; - int ext_size, ext_used; - int obj_size, obj_used; -} ReconstructionState; - -static _Bool append_ext(ReconstructionState *state, Extension ext) -{ - if(state->exts == NULL) - { - int n = 8; - - state->exts = malloc(n * sizeof(Extension)); - - if(state->exts == NULL) - return 0; - - state->ext_size = n; - state->ext_used = 0; - } - else if(state->ext_size == state->ext_used) - { - int factor = 2; - - Extension *temp = realloc(state->exts, factor * state->ext_size * sizeof(Extension)); - - if(temp == NULL) - return 0; - - state->exts = temp; - state->ext_size *= factor; - } - - state->exts[state->ext_used++] = ext; - return 1; -} - -static _Bool contains_ext(ReconstructionState *state, void *addr) -{ - for(int i = 0; i < state->ext_used; i += 1) - if(state->exts[i].addr == addr) - return 1; - return 0; -} - -static void exension_walker(void **addr, unsigned int size, void *userp) -{ - ReconstructionState *state = userp; - - if(contains_ext(state, *addr)) - return; - - if(!append_ext(state, (Extension) { .addr = *addr, .size = size, .parent = NULL })) - { - assert(0); - } -} - -static _Bool append_obj(ReconstructionState *state, Object *obj) -{ - if(state->objs == NULL) - { - int n = 8; - - state->objs = malloc(n * sizeof(Object*)); - - if(state->objs == NULL) - return 0; - - state->obj_size = n; - state->obj_used = 0; - } - else if(state->obj_size == state->obj_used) - { - int factor = 2; - - Object **temp = realloc(state->objs, factor * state->obj_size * sizeof(Extension)); - - if(temp == NULL) - return 0; - - state->objs = temp; - state->obj_size *= factor; - } - - state->objs[state->obj_used++] = obj; - return 1; -} - -static _Bool contains_obj(ReconstructionState *state, Object *obj) -{ - for(int i = 0; i < state->obj_used; i += 1) - if(state->objs[i] == obj) - return 1; - return 0; -} - -static void object_walker(Object **referer, void *userp) -{ - ReconstructionState *state = userp; - Object *obj = *referer; - - if(obj == NULL) - return; - - if(!contains_obj(state, obj)) - { - // Object wasn't already encountered. - // Store it at index `left`. - - if(!append_obj(state, obj)) - { - assert(0); - } - - // Walk over its extensions. - { - Object_WalkExtensions(obj, exension_walker, userp); - - if(state->ext_used > 0) - { - int i = state->ext_used-1; - while(i >= 0 && state->exts[i].parent == NULL) - { - state->exts[i].parent = obj; - i -= 1; - } - } - } - - // Walk over its children. - Object_WalkReferences(obj, object_walker, state); - } - else - { - // Object was already encountered and - // it's pointer is stored at index `index`. - } -} - -static int chunk_order(const void *p1, const void *p2) -{ - const MemoryChunk - *c1 = p1, - *c2 = p2; - - intptr_t l = (intptr_t) c1->addr; - intptr_t r = (intptr_t) c2->addr; - - return (l > r) - (l < r); -} - -ReconstructedHeap *reconstruct_heap(Runtime *runtime) -{ - ReconstructionState state; - state.exts = NULL; - state.ext_used = 0; - state.ext_size = 0; - state.objs = NULL; - state.obj_used = 0; - state.obj_size = 0; - - Object *builtins = Runtime_GetBuiltins(runtime); - if(builtins != NULL) - { - object_walker(&builtins, &state); - Object_WalkReferences(builtins, object_walker, &state); - } - - CallStackScanner *scanner = CallStackScanner_New(runtime); - assert(scanner != NULL); - - Object *locals, *closure; - while(CallStackScanner_Next(&scanner, &locals, &closure, NULL, NULL)) - { - if(locals != NULL) - { - object_walker(&locals, &state); - Object_WalkReferences(locals, object_walker, &state); - } - - if(closure != NULL) - { - object_walker(&closure, &state); - Object_WalkReferences(closure, object_walker, &state); - } - } - - ReconstructedHeap *recheap = malloc(sizeof(ReconstructedHeap) + (state.obj_used + state.ext_used) * sizeof(MemoryChunk)); - if(recheap == NULL) - { - free(state.objs); - free(state.exts); - return NULL; - } - - recheap->count = state.obj_used + state.ext_used; - - Heap *heap = Runtime_GetHeap(runtime); - char *heap_addr = Heap_GetPointer(heap); - unsigned int heap_size = Heap_GetSize(heap); - - for(int i = 0; i < state.obj_used; i += 1) - { - recheap->chunks[i] = (MemoryChunk) { - .in_pool = (heap_addr <= (char*) state.objs[i] && (char*) state.objs[i] < heap_addr + heap_size), - .parent = NULL, - .addr = state.objs[i], - .size = state.objs[i]->type->size, - }; - } - - for(int i = 0; i < state.ext_used; i += 1) - { - recheap->chunks[state.obj_used + i] = (MemoryChunk) { - .in_pool = (heap_addr <= (char*) state.exts[i].addr && (char*) state.exts[i].addr < heap_addr + heap_size), - .parent = state.exts[i].parent, - .addr = state.exts[i].addr, - .size = state.exts[i].size, - }; - } - - qsort(recheap->chunks, recheap->count, sizeof(MemoryChunk), chunk_order); - - free(state.objs); - free(state.exts); - return recheap; -} \ No newline at end of file diff --git a/src/heap_reconst.h b/src/heap_reconst.h deleted file mode 100644 index c9a2256..0000000 --- a/src/heap_reconst.h +++ /dev/null @@ -1,15 +0,0 @@ -#include "runtime/runtime.h" - -typedef struct { - _Bool in_pool; - Object *parent; - void *addr; - int size; -} MemoryChunk; - -typedef struct { - int count; - MemoryChunk chunks[]; -} ReconstructedHeap; - -ReconstructedHeap *reconstruct_heap(Runtime *runtime); diff --git a/src/noja.c b/src/noja.c index 43d9832..651f6c6 100644 --- a/src/noja.c +++ b/src/noja.c @@ -2,7 +2,6 @@ #include #include #include -#include "debug.h" #include "o_builtins.h" #include "compiler/parse.h" #include "compiler/serialize.h"