From bda6fc826cac615d0d3a7ed546ff08d9dbce3bce Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Tue, 10 Apr 2012 05:09:55 -0400 Subject: [PATCH] code.c, info.txt --- code.c | 29 ++++++++++++++++++++ info.txt | 12 +++++++++ typedef.c | 11 +++++--- util.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 code.c create mode 100644 info.txt create mode 100644 util.c diff --git a/code.c b/code.c new file mode 100644 index 0000000..ef33d31 --- /dev/null +++ b/code.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2012 + * Dale Weiler + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "gmqcc.h" +/* + * This file is empty: This is where codegen will take place: Once the + * parser is completed, right now it on;y creates parse trees. Syntax + * checking and other convoluted things on the parse tree need to take + * place much like a parser. + */ diff --git a/info.txt b/info.txt new file mode 100644 index 0000000..8916a97 --- /dev/null +++ b/info.txt @@ -0,0 +1,12 @@ +there are 3 accessible memory zones - +globals: + array of 32bit ints/floats, mixed, LE, +entities: + structure is up to the engine but the fields are a linear array + of mixed ints/floats, there are globals referring to the offsets + of these in the entity struct so there are ADDRESS and STOREP and + LOAD instructions that use globals containing field offsets. +strings: + a static array in the progs.dat, with file parsing creating + additional constants, and some engine fields are mapped by + address as well to unique string offsets diff --git a/typedef.c b/typedef.c index e84300a..dcc4e20 100644 --- a/typedef.c +++ b/typedef.c @@ -25,6 +25,7 @@ #include #include "gmqcc.h" static typedef_node *typedef_table[1024]; + void typedef_init() { int i; for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++) @@ -56,14 +57,18 @@ typedef_node *typedef_find(const char *s) { void typedef_clear() { int i; - for(i = 1024; i > 0; i--) - if(typedef_table[i]) + for(i = 1024; i > 0; i--) { + if(typedef_table[i]) { + mem_d(typedef_table[i]->name); mem_d(typedef_table[i]); + } + } } int typedef_add(const char *from, const char *to) { unsigned int hash = typedef_hash(to); typedef_node *find = typedef_table[hash]; + if (find) return error(ERROR_PARSE, "typedef for %s already exists or conflicts\n", to); @@ -82,7 +87,7 @@ int typedef_add(const char *from, const char *to) { typedef_node *find = typedef_table[typedef_hash(from)]; if (find) { typedef_table[hash] = mem_a(sizeof(typedef_node)); - typedef_table[hash]->name = strdup(find->name); + typedef_table[hash]->name = util_strdup(find->name); return -100; } } diff --git a/util.c b/util.c new file mode 100644 index 0000000..5261063 --- /dev/null +++ b/util.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2012 + * Dale Weiler + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include "gmqcc.h" + +struct memblock_t { + const char *file; + unsigned int line; + unsigned int byte; +}; + +void *util_memory_a(unsigned int byte, unsigned int line, const char *file) { + struct memblock_t *data = malloc(sizeof(struct memblock_t) + byte); + if (!data) return NULL; + data->line = line; + data->byte = byte; + data->file = file; + + printf("[MEM] allocation: %08u (bytes) at %s:%u\n", byte, file, line); + return (void*)((uintptr_t)data+sizeof(struct memblock_t)); +} + +void util_memory_d(void *ptrn, unsigned int line, const char *file) { + if (!ptrn) return; + void *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t)); + struct memblock_t *info = (struct memblock_t*)data; + + printf("[MEM] released: %08u (bytes) at %s:%u\n", info->byte, file, line); + free(data); +} + +#ifndef mem_d +#define mem_d(x) util_memory_d((x), __LINE__, __FILE__) +#endif +#ifndef mem_a +#define mem_a(x) util_memory_a((x), __LINE__, __FILE__) +#endif + +/* + * Some string utility functions, because strdup uses malloc, and we want + * to track all memory (without replacing malloc). + */ +char *util_strdup(const char *s) { + size_t len; + char *ptr; + + if (!s) + return NULL; + + len = strlen(s); + ptr = mem_a (len+1); + + if (ptr && len) { + memcpy(ptr, s, len); + ptr[len] = '\0'; + } + + return ptr; +} -- 2.39.2