6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 prog_section_statement *code_statements;
28 prog_section_def *code_defs;
29 prog_section_field *code_fields;
30 prog_section_function *code_functions;
34 uint32_t code_entfields;
36 /* This is outrageous! */
37 #define QCINT_ENTRY void*
38 #define QCINT_TO_HASH_ENTRY(q) ((void*)(uintptr_t)(q))
39 #define HASH_ENTRY_TO_QCINT(h) ((qcint)(uintptr_t)(h))
40 static ht code_string_cache;
41 static qcint code_string_cached_empty;
43 void code_push_statement(prog_section_statement *stmt, int linenum)
45 vec_push(code_statements, *stmt);
46 vec_push(code_linenums, linenum);
49 void code_pop_statement()
51 vec_pop(code_statements);
52 vec_pop(code_linenums);
56 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
57 prog_section_statement empty_statement = {0,{0},{0},{0}};
58 prog_section_def empty_def = {0, 0, 0};
62 code_string_cache = util_htnew(1024);
65 * The way progs.dat is suppose to work is odd, there needs to be
66 * some null (empty) statements, functions, and 28 globals
69 vec_push(code_globals, 0);
71 vec_push(code_chars, '\0');
72 vec_push(code_functions, empty_function);
73 code_push_statement(&empty_statement, 0);
74 vec_push(code_defs, empty_def);
75 vec_push(code_fields, empty_def);
78 uint32_t code_genstring(const char *str)
88 if (!code_string_cached_empty) {
89 code_string_cached_empty = vec_size(code_chars);
90 vec_push(code_chars, 0);
92 return code_string_cached_empty;
95 hash = util_hthash(code_string_cache, str);
96 existing = util_htgeth(code_string_cache, str, hash);
98 return HASH_ENTRY_TO_QCINT(existing);
100 off = vec_size(code_chars);
101 vec_upload(code_chars, str, strlen(str)+1);
103 util_htseth(code_string_cache, str, hash, QCINT_TO_HASH_ENTRY(off));
104 existing = util_htgeth(code_string_cache, str, hash);
108 qcint code_alloc_field (size_t qcsize)
110 qcint pos = (qcint)code_entfields;
111 code_entfields += qcsize;
115 bool code_write(const char *filename, const char *lnofile) {
116 prog_header code_header;
120 code_header.statements.offset = sizeof(prog_header);
121 code_header.statements.length = vec_size(code_statements);
122 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
123 code_header.defs.length = vec_size(code_defs);
124 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
125 code_header.fields.length = vec_size(code_fields);
126 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
127 code_header.functions.length = vec_size(code_functions);
128 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
129 code_header.globals.length = vec_size(code_globals);
130 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
131 code_header.strings.length = vec_size(code_chars);
132 code_header.version = 6;
134 code_header.crc16 = opts.forced_crc;
136 code_header.crc16 = code_crc;
137 code_header.entfield = code_entfields;
139 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
140 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
143 vec_push(code_chars, '\0'); /* > */
144 vec_push(code_chars, '\0'); /* = */
145 vec_push(code_chars, '\0'); /* P */
148 /* ensure all data is in LE format */
149 util_endianswap(&code_header.version, 1, sizeof(code_header.version));
150 util_endianswap(&code_header.crc16, 1, sizeof(code_header.crc16));
151 util_endianswap(&code_header.statements, 2, sizeof(code_header.statements.offset));
152 util_endianswap(&code_header.defs, 2, sizeof(code_header.statements.offset));
153 util_endianswap(&code_header.fields, 2, sizeof(code_header.statements.offset));
154 util_endianswap(&code_header.functions, 2, sizeof(code_header.statements.offset));
155 util_endianswap(&code_header.strings, 2, sizeof(code_header.statements.offset));
156 util_endianswap(&code_header.globals, 2, sizeof(code_header.statements.offset));
157 util_endianswap(&code_header.entfield, 1, sizeof(code_header.entfield));
158 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
159 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
160 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
161 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
162 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
165 uint32_t lnotype = *(unsigned int*)"LNOF";
166 uint32_t version = 1;
168 fp = file_open(lnofile, "wb");
172 util_endianswap(&version, 1, sizeof(version));
173 util_endianswap(code_linenums, vec_size(code_linenums), sizeof(code_linenums[0]));
176 if (file_write(&lnotype, sizeof(lnotype), 1, fp) != 1 ||
177 file_write(&version, sizeof(version), 1, fp) != 1 ||
178 file_write(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
179 file_write(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
180 file_write(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
181 file_write(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
182 file_write(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
184 con_err("failed to write lno file\n");
191 fp = file_open(filename, "wb");
195 if (1 != file_write(&code_header, sizeof(prog_header) , 1 , fp) ||
196 vec_size(code_statements) != file_write(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
197 vec_size(code_defs) != file_write(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
198 vec_size(code_fields) != file_write(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
199 vec_size(code_functions) != file_write(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
200 vec_size(code_globals) != file_write(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
201 vec_size(code_chars) != file_write(code_chars, 1 , vec_size(code_chars) , fp))
207 util_debug("GEN","HEADER:\n");
208 util_debug("GEN"," version: = %d\n", code_header.version );
209 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
210 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
211 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
212 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
213 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
214 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
215 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
216 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
219 util_debug("GEN", "FUNCTIONS:\n");
220 for (; it < vec_size(code_functions); it++) {
221 size_t j = code_functions[it].entry;
222 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",
223 code_functions[it].entry,
224 code_functions[it].firstlocal,
225 code_functions[it].locals,
226 code_functions[it].profile,
227 code_functions[it].name,
228 code_functions[it].file,
229 code_functions[it].nargs,
230 code_functions[it].argsize[0],
231 code_functions[it].argsize[1],
232 code_functions[it].argsize[2],
233 code_functions[it].argsize[3],
234 code_functions[it].argsize[4],
235 code_functions[it].argsize[5],
236 code_functions[it].argsize[6],
237 code_functions[it].argsize[7]
240 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
241 /* Internal functions have no code */
242 if (code_functions[it].entry >= 0) {
243 util_debug("GEN", " CODE:\n");
245 if (code_statements[j].opcode != INSTR_DONE)
246 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
247 asm_instr[code_statements[j].opcode].m,
248 code_statements[j].o1.s1,
249 code_statements[j].o2.s1,
250 code_statements[j].o3.s1
253 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
261 vec_free(code_statements);
262 vec_free(code_linenums);
264 vec_free(code_fields);
265 vec_free(code_functions);
266 vec_free(code_globals);
267 vec_free(code_chars);