/*
- * Copyright (C) 2012
- * Dale Weiler
+ * Copyright (C) 2012
+ * Dale Weiler, Wolfgang Bumiller
*
* 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
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#include <stdint.h>
-#include <stdlib.h>
#include "gmqcc.h"
-typedef struct {
- uint16_t opcode;
-
- /* operand 1 */
- union {
- int16_t s1; /* signed */
- uint16_t u1; /* unsigned */
- };
- /* operand 2 */
- union {
- int16_t s2; /* signed */
- uint16_t u2; /* unsigned */
- };
- /* operand 3 */
- union {
- int16_t s3; /* signed */
- uint16_t u3; /* unsigned */
- };
-
- /*
- * This is the same as the structure in darkplaces
- * {
- * unsigned short op;
- * short a,b,c;
- * }
- * But this one is more sane to work with, and the
- * type sizes are guranteed.
- */
-} prog_section_statement;
-
-typedef struct {
- /* The type is (I assume)
- * 0 = ev_void
- * 1 = ev_string
- * 2 = ev_float
- * 3 = ev_vector
- * 4 = ev_entity
- * 5 = ev_field
- * 6 = ev_function
- * 7 = ev_pointer
- * 8 = ev_bad (is this right for uint16_t type?)
- */
- uint16_t type;
- uint16_t offset; /* offset in file? (what about length) */
- uint32_t name; /* offset in string table? (confused :() */
-} prog_section_both;
-
-/*
- * var and field use the same structure. But lets not use the same
- * name just for safety reasons? (still castable ...).
- */
-typedef prog_section_both prog_section_def;
-typedef prog_section_both prog_section_field;
-
-typedef struct {
- int32_t entry; /* in statement table for instructions */
- uint32_t firstlocal; /* First local in local table */
- uint32_t locals; /* Total ints of params + locals */
- uint32_t profile; /* Always zero (engine uses this) */
- uint32_t name; /* name of function in string table */
- uint32_t file; /* file of the source file */
- uint32_t nargs; /* number of arguments */
- uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
-} prog_section_function;
-
-typedef struct {
- uint32_t offset; /* Offset in file of where data begins */
- uint32_t length; /* Length of section (how many of) */
-} prog_section;
-
-typedef struct {
- uint32_t version; /* Program version (6) */
- uint32_t crc16; /* What is this? */
- prog_section statements; /* prog_section_statement */
- prog_section defs; /* prog_section_def */
- prog_section fields; /* prog_section_field */
- prog_section functions; /* prog_section_function */
- prog_section strings; /* What is this? */
- prog_section globals; /* What is this? */
- uint32_t entfield; /* Number of entity fields */
-} prog_header;
-
/*
* The macros below expand to a typesafe vector implementation, which
* can be viewed in gmqcc.h
- *
+ *
* code_statements_data -- raw prog_section_statement array
* code_statements_elements -- number of elements
* code_statements_allocated -- size of the array allocated
* code_statements_add(T) -- add element (returns -1 on error)
- *
+ *
* code_vars_data -- raw prog_section_var array
* code_vars_elements -- number of elements
* code_vars_allocated -- size of the array allocated
* code_vars_add(T) -- add element (returns -1 on error)
- *
+ *
* code_fields_data -- raw prog_section_field array
* code_fields_elements -- number of elements
* code_fields_allocated -- size of the array allocated
* code_functions_elements -- number of elements
* code_functions_allocated -- size of the array allocated
* code_functions_add(T) -- add element (returns -1 on error)
- *
+ *
* code_globals_data -- raw prog_section_def array
* code_globals_elements -- number of elements
* code_globals_allocated -- size of the array allocated
* code_globals_add(T) -- add element (returns -1 on error)
- *
- * code_strings_data -- raw char* array
- * code_strings_elements -- number of elements
- * code_strings_allocated -- size of the array allocated
- * code_strings_add(T) -- add element (returns -1 on error)
+ *
+ * code_chars_data -- raw char* array
+ * code_chars_elements -- number of elements
+ * code_chars_allocated -- size of the array allocated
+ * code_chars_add(T) -- add element (returns -1 on error)
*/
VECTOR_MAKE(prog_section_statement, code_statements);
VECTOR_MAKE(prog_section_def, code_defs );
VECTOR_MAKE(prog_section_field, code_fields );
VECTOR_MAKE(prog_section_function, code_functions );
VECTOR_MAKE(int, code_globals );
-VECTOR_MAKE(char, code_strings );
-prog_header code_header ={0};
+VECTOR_MAKE(char, code_chars );
+
+uint32_t code_entfields;
void code_init() {
- /*
- * The way progs.dat is suppose to work is odd, there needs to be
- * some null (empty) statements, functions, and 28 globals
- */
- prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
- prog_section_statement empty_statement = {0,{0},{0},{0}};
- int i;
- for(i = 0; i < 28; i++)
- code_globals_add(0);
-
- code_strings_add ('\0');
- code_functions_add (empty_function);
- code_statements_add(empty_statement);
+ prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
+ prog_section_statement empty_statement = {0,{0},{0},{0}};
+ int i = 0;
+
+ /* omit creation of null code */
+ if (OPTS_FLAG(OMIT_NULL_BYTES))
+ return;
+
+ /*
+ * The way progs.dat is suppose to work is odd, there needs to be
+ * some null (empty) statements, functions, and 28 globals
+ */
+ for(; i < 28; i++)
+ code_globals_add(0);
+
+ code_chars_add ('\0');
+ code_functions_add (empty_function);
+ code_statements_add(empty_statement);
+
+ code_entfields = 0;
+}
+
+uint32_t code_genstring(const char *str)
+{
+ uint32_t off = code_chars_elements;
+ while (*str) {
+ code_chars_add(*str);
+ ++str;
+ }
+ code_chars_add(0);
+ return off;
+}
+
+uint32_t code_cachedstring(const char *str)
+{
+ size_t s = 0;
+ /* We could implement knuth-morris-pratt or something
+ * and also take substrings, but I'm uncomfortable with
+ * pointing to subparts of strings for the sake of clarity...
+ */
+ while (s < code_chars_elements) {
+ if (!strcmp(str, code_chars_data + s))
+ return s;
+ while (code_chars_data[s]) ++s;
+ ++s;
+ }
+ return code_genstring(str);
}
void code_test() {
- const char *X;
- size_t size = sizeof(X);
- size_t iter = 0;
-
- #define FOO(Y) \
- X = Y; \
- size = sizeof(Y); \
- for (iter=0; iter < size; iter++) { \
- code_strings_add(X[iter]); \
- }
-
- FOO("m_init");
- FOO("print");
- FOO("hello world\n");
- FOO("m_keydown");
- FOO("m_draw");
- FOO("m_toggle");
- FOO("m_shutdown");
-
- code_globals_add(1); /* m_init */
- code_globals_add(2); /* print */
- code_globals_add(14); /* hello world in string table */
-
- /* now the defs */
- code_defs_add((prog_section_def){.type=TYPE_VOID, .offset=28/*globals[28]*/, .name=1 }); /* m_init */
- code_defs_add((prog_section_def){.type=TYPE_FUNCTION,.offset=29/*globals[29]*/, .name=8 }); /* print */
- code_defs_add((prog_section_def){.type=TYPE_STRING, .offset=30/*globals[30]*/, .name=14}); /*hello_world*/
-
- code_functions_add((prog_section_function){1, 0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
- code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print */
- code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13, 0,0, {0}}); /* m_keydown */
- code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10, 0,0, {0}});
- code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10+7, 0,0, {0}});
- code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10+7+9, 0,0, {0}});
-
- code_statements_add((prog_section_statement){INSTR_STORE_F, {30}/*30 is hello_world */, {OFS_PARM0}, {0}});
- code_statements_add((prog_section_statement){INSTR_CALL1, {29}/*29 is print */, {0}, {0}});
- code_statements_add((prog_section_statement){INSTR_RETURN, {0}, {0}, {0}});
+ prog_section_def d1 = { TYPE_VOID, 28, 1 };
+ prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
+ prog_section_def d3 = { TYPE_STRING, 30, 14};
+ prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
+ prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
+ prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
+ prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
+ prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
+ prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
+ prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
+ prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
+ prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
+
+ code_chars_put("m_init", 0x6);
+ code_chars_put("print", 0x5);
+ code_chars_put("hello world\n", 0xC);
+ code_chars_put("m_keydown", 0x9);
+ code_chars_put("m_draw", 0x6);
+ code_chars_put("m_toggle", 0x8);
+ code_chars_put("m_shutdown", 0xA);
+
+ code_globals_add(1); /* m_init */
+ code_globals_add(2); /* print */
+ code_globals_add(14); /* hello world in string table */
+
+ /* now the defs */
+ code_defs_add (d1); /* m_init */
+ code_defs_add (d2); /* print */
+ code_defs_add (d3); /*hello_world*/
+ code_functions_add (f1); /* m_init */
+ code_functions_add (f2); /* print */
+ code_functions_add (f3); /* m_keydown */
+ code_functions_add (f4);
+ code_functions_add (f5);
+ code_functions_add (f6);
+ code_statements_add(s1);
+ code_statements_add(s2);
+ code_statements_add(s3);
}
-/* program header */
-void code_write() {
- code_init();
- code_test();
-
- code_header.version = 6;
- code_header.crc16 = 0; /* TODO: */
- code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
- code_header.defs = (prog_section){code_header.statements.offset + sizeof(prog_section_statement)*code_statements_elements, code_defs_elements };
- code_header.fields = (prog_section){code_header.defs.offset + sizeof(prog_section_def) *code_defs_elements, code_fields_elements };
- code_header.functions = (prog_section){code_header.fields.offset + sizeof(prog_section_field) *code_fields_elements, code_functions_elements };
- code_header.globals = (prog_section){code_header.functions.offset + sizeof(prog_section_function) *code_functions_elements, code_globals_elements };
- code_header.strings = (prog_section){code_header.globals.offset + sizeof(int) *code_globals_elements, code_strings_elements };
- code_header.entfield = 0; /* TODO: */
-
- FILE *fp = fopen("program.dat", "wb");
- fwrite(&code_header, 1, sizeof(prog_header), fp);
- fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
- fwrite(code_defs_data, 1, sizeof(prog_section_def) *code_defs_elements, fp);
- fwrite(code_fields_data, 1, sizeof(prog_section_field) *code_fields_elements, fp);
- fwrite(code_functions_data, 1, sizeof(prog_section_function) *code_functions_elements, fp);
- fwrite(code_globals_data, 1, sizeof(int) *code_globals_elements, fp);
- fwrite(code_strings_data, 1, 1 *code_strings_elements, fp);
-
- free(code_statements_data);
- free(code_defs_data);
- free(code_fields_data);
- free(code_functions_data);
- free(code_globals_data);
- free(code_strings_data);
-
- fclose(fp);
+qcint code_alloc_field (size_t qcsize)
+{
+ qcint pos = (qcint)code_entfields;
+ code_entfields += qcsize;
+ return pos;
+}
+
+bool code_write(const char *filename) {
+ prog_header code_header;
+ FILE *fp = NULL;
+ size_t it = 2;
+
+ /* see proposal.txt */
+ if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
+ code_header.statements.offset = sizeof(prog_header);
+ code_header.statements.length = code_statements_elements;
+ code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * code_statements_elements);
+ code_header.defs.length = code_defs_elements;
+ code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * code_defs_elements);
+ code_header.fields.length = code_fields_elements;
+ code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * code_fields_elements);
+ code_header.functions.length = code_functions_elements;
+ code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * code_functions_elements);
+ code_header.globals.length = code_globals_elements;
+ code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * code_globals_elements);
+ code_header.strings.length = code_chars_elements;
+ code_header.version = 6;
+ code_header.crc16 = 0; /* TODO: */
+ code_header.entfield = code_entfields;
+
+ if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
+ util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
+
+ /* >= + P */
+ code_chars_add('\0'); /* > */
+ code_chars_add('\0'); /* = */
+ code_chars_add('\0'); /* P */
+ }
+
+ /* ensure all data is in LE format */
+ util_endianswap(&code_header, 1, sizeof(prog_header));
+ util_endianswap(code_statements_data, code_statements_elements, sizeof(prog_section_statement));
+ util_endianswap(code_defs_data, code_defs_elements, sizeof(prog_section_def));
+ util_endianswap(code_fields_data, code_fields_elements, sizeof(prog_section_field));
+ util_endianswap(code_functions_data, code_functions_elements, sizeof(prog_section_function));
+ util_endianswap(code_globals_data, code_globals_elements, sizeof(int32_t));
+
+ fp = fopen(filename, "wb");
+ if (!fp)
+ return false;
+
+ if (1 != fwrite(&code_header, sizeof(prog_header), 1, fp) ||
+ code_statements_elements != fwrite(code_statements_data, sizeof(prog_section_statement), code_statements_elements, fp) ||
+ code_defs_elements != fwrite(code_defs_data, sizeof(prog_section_def) , code_defs_elements , fp) ||
+ code_fields_elements != fwrite(code_fields_data, sizeof(prog_section_field) , code_fields_elements , fp) ||
+ code_functions_elements != fwrite(code_functions_data, sizeof(prog_section_function) , code_functions_elements , fp) ||
+ code_globals_elements != fwrite(code_globals_data, sizeof(int32_t) , code_globals_elements , fp) ||
+ code_chars_elements != fwrite(code_chars_data, 1 , code_chars_elements , fp))
+ {
+ fclose(fp);
+ return false;
+ }
+
+ util_debug("GEN","HEADER:\n");
+ util_debug("GEN"," version: = %d\n", code_header.version );
+ util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
+ util_debug("GEN"," entfield: = %d\n", code_header.entfield);
+ util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
+ util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
+ util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
+ util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
+ util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
+ util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
+
+ /* FUNCTIONS */
+ util_debug("GEN", "FUNCTIONS:\n");
+ for (; it < code_functions_elements; it++) {
+ size_t j = code_functions_data[it].entry;
+ util_debug("GEN", " {.entry =% 5d, .firstlocal =% 5d, .locals =% 5d, .profile =% 5d, .name =% 5d, .file =% 5d, .nargs =% 5d, .argsize ={%d,%d,%d,%d,%d,%d,%d,%d} }\n",
+ code_functions_data[it].entry,
+ code_functions_data[it].firstlocal,
+ code_functions_data[it].locals,
+ code_functions_data[it].profile,
+ code_functions_data[it].name,
+ code_functions_data[it].file,
+ code_functions_data[it].nargs,
+ code_functions_data[it].argsize[0],
+ code_functions_data[it].argsize[1],
+ code_functions_data[it].argsize[2],
+ code_functions_data[it].argsize[3],
+ code_functions_data[it].argsize[4],
+ code_functions_data[it].argsize[5],
+ code_functions_data[it].argsize[6],
+ code_functions_data[it].argsize[7]
+
+ );
+ util_debug("GEN", " NAME: %s\n", &code_chars_data[code_functions_data[it].name]);
+ /* Internal functions have no code */
+ if (code_functions_data[it].entry >= 0) {
+ util_debug("GEN", " CODE:\n");
+ for (;;) {
+ if (code_statements_data[j].opcode != AINSTR_END)
+ util_debug("GEN", " %s {0x%05x,0x%05x,0x%05x}\n",
+ asm_instr[code_statements_data[j].opcode].m,
+ code_statements_data[j].o1.s1,
+ code_statements_data[j].o2.s1,
+ code_statements_data[j].o3.s1
+ );
+ else {
+ util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
+ break;
+ }
+ j++;
+ }
+ }
+ }
+
+ mem_d(code_statements_data);
+ mem_d(code_defs_data);
+ mem_d(code_fields_data);
+ mem_d(code_functions_data);
+ mem_d(code_globals_data);
+ mem_d(code_chars_data);
+ fclose(fp);
+ return true;
}