*/
#include "gmqcc.h"
-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) */
- uint16_t crc16; /* What is this? */
- uint16_t skip; /* see propsal.txt */
-
- 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
VECTOR_MAKE(int, code_globals );
VECTOR_MAKE(char, code_chars );
+uint32_t code_entfields;
+
void code_init() {
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_omit_nullcode)
+ if (OPTS_FLAG(OMIT_NULL_BYTES))
return;
/*
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() {
code_statements_add(s3);
}
+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_omit_nullcode) {}
+ 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.strings.length = code_chars_elements;
code_header.version = 6;
code_header.crc16 = 0; /* TODO: */
- code_header.entfield = 0; /* TODO: */
+ code_header.entfield = code_entfields;
- if (opts_darkplaces_stringtablebug) {
+ if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
/* >= + P */
return false;
if (1 != fwrite(&code_header, sizeof(prog_header), 1, fp) ||
- 1 != fwrite(code_statements_data, sizeof(prog_section_statement)*code_statements_elements, 1, fp) ||
- 1 != fwrite(code_defs_data, sizeof(prog_section_def) *code_defs_elements, 1, fp) ||
- 1 != fwrite(code_fields_data, sizeof(prog_section_field) *code_fields_elements, 1, fp) ||
- 1 != fwrite(code_functions_data, sizeof(prog_section_function) *code_functions_elements, 1, fp) ||
- 1 != fwrite(code_globals_data, sizeof(int32_t) *code_globals_elements, 1, fp) ||
- 1 != fwrite(code_chars_data, 1 *code_chars_elements, 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;