-void code_write() {
- prog_header code_header={0};
- 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_chars_elements };
- code_header.entfield = 0; /* TODO: */
-
+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 < vec_size(code_chars)) {
+ if (!strcmp(str, code_chars + s))
+ return s;
+ while (code_chars[s]) ++s;
+ ++s;
+ }
+ return code_genstring(str);
+}
+
+qcint code_alloc_field (size_t qcsize)
+{
+ qcint pos = (qcint)code_entfields;
+ code_entfields += qcsize;
+ return pos;
+}
+
+bool code_write(const char *filename, const char *lnofile) {
+ prog_header code_header;
+ FILE *fp = NULL;
+ size_t it = 2;
+
+ code_header.statements.offset = sizeof(prog_header);
+ code_header.statements.length = vec_size(code_statements);
+ code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
+ code_header.defs.length = vec_size(code_defs);
+ code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
+ code_header.fields.length = vec_size(code_fields);
+ code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
+ code_header.functions.length = vec_size(code_functions);
+ code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
+ code_header.globals.length = vec_size(code_globals);
+ code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
+ code_header.strings.length = vec_size(code_chars);
+ code_header.version = 6;
+ if (opts_forcecrc)
+ code_header.crc16 = opts_forced_crc;
+ else
+ code_header.crc16 = code_crc;
+ code_header.entfield = code_entfields;
+
+ if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
+ util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
+
+ /* >= + P */
+ vec_push(code_chars, '\0'); /* > */
+ vec_push(code_chars, '\0'); /* = */
+ vec_push(code_chars, '\0'); /* P */
+ }
+
+ if (lnofile) {
+ uint32_t lnotype = *(unsigned int*)"LNOF";
+ uint32_t version = 1;
+
+ fp = util_fopen(lnofile, "wb");
+ if (!fp)
+ return false;
+
+ if (fwrite(&lnotype, sizeof(lnotype), 1, fp) != 1 ||
+ fwrite(&version, sizeof(version), 1, fp) != 1 ||
+ fwrite(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
+ fwrite(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
+ fwrite(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
+ fwrite(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
+ fwrite(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
+ {
+ con_err("failed to write lno file\n");
+ }
+
+ fclose(fp);
+ fp = NULL;
+ }
+