2 * Copyright (C) 2012, 2013
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,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};
63 code_string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
66 * The way progs.dat is suppose to work is odd, there needs to be
67 * some null (empty) statements, functions, and 28 globals
70 vec_push(code_globals, 0);
72 vec_push(code_chars, '\0');
73 vec_push(code_functions, empty_function);
74 code_push_statement(&empty_statement, 0);
75 vec_push(code_defs, empty_def);
76 vec_push(code_fields, empty_def);
79 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
80 uint32_t code_genstring(const char *str)
90 if (!code_string_cached_empty) {
91 code_string_cached_empty = vec_size(code_chars);
92 vec_push(code_chars, 0);
94 return code_string_cached_empty;
97 if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
98 hash = ((unsigned char*)str)[strlen(str)-1];
99 existing = code_util_str_htgeth(code_string_cache, str, hash);
101 hash = util_hthash(code_string_cache, str);
102 existing = util_htgeth(code_string_cache, str, hash);
106 return HASH_ENTRY_TO_QCINT(existing);
108 off = vec_size(code_chars);
109 vec_upload(code_chars, str, strlen(str)+1);
111 util_htseth(code_string_cache, str, hash, QCINT_TO_HASH_ENTRY(off));
115 qcint code_alloc_field (size_t qcsize)
117 qcint pos = (qcint)code_entfields;
118 code_entfields += qcsize;
122 bool code_write(const char *filename, const char *lnofile) {
123 prog_header code_header;
127 code_header.statements.offset = sizeof(prog_header);
128 code_header.statements.length = vec_size(code_statements);
129 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
130 code_header.defs.length = vec_size(code_defs);
131 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
132 code_header.fields.length = vec_size(code_fields);
133 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
134 code_header.functions.length = vec_size(code_functions);
135 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
136 code_header.globals.length = vec_size(code_globals);
137 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
138 code_header.strings.length = vec_size(code_chars);
139 code_header.version = 6;
141 code_header.crc16 = opts.forced_crc;
143 code_header.crc16 = code_crc;
144 code_header.entfield = code_entfields;
146 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
147 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
150 vec_push(code_chars, '\0'); /* > */
151 vec_push(code_chars, '\0'); /* = */
152 vec_push(code_chars, '\0'); /* P */
155 /* ensure all data is in LE format */
156 util_endianswap(&code_header.version, 1, sizeof(code_header.version));
157 util_endianswap(&code_header.crc16, 1, sizeof(code_header.crc16));
158 util_endianswap(&code_header.statements, 2, sizeof(code_header.statements.offset));
159 util_endianswap(&code_header.defs, 2, sizeof(code_header.statements.offset));
160 util_endianswap(&code_header.fields, 2, sizeof(code_header.statements.offset));
161 util_endianswap(&code_header.functions, 2, sizeof(code_header.statements.offset));
162 util_endianswap(&code_header.strings, 2, sizeof(code_header.statements.offset));
163 util_endianswap(&code_header.globals, 2, sizeof(code_header.statements.offset));
164 util_endianswap(&code_header.entfield, 1, sizeof(code_header.entfield));
165 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
166 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
167 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
168 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
169 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
172 uint32_t version = 1;
174 fp = file_open(lnofile, "wb");
178 util_endianswap(&version, 1, sizeof(version));
179 util_endianswap(code_linenums, vec_size(code_linenums), sizeof(code_linenums[0]));
182 if (file_write("LNOF", 4, 1, fp) != 1 ||
183 file_write(&version, sizeof(version), 1, fp) != 1 ||
184 file_write(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
185 file_write(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
186 file_write(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
187 file_write(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
188 file_write(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
190 con_err("failed to write lno file\n");
197 fp = file_open(filename, "wb");
201 if (1 != file_write(&code_header, sizeof(prog_header) , 1 , fp) ||
202 vec_size(code_statements) != file_write(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
203 vec_size(code_defs) != file_write(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
204 vec_size(code_fields) != file_write(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
205 vec_size(code_functions) != file_write(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
206 vec_size(code_globals) != file_write(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
207 vec_size(code_chars) != file_write(code_chars, 1 , vec_size(code_chars) , fp))
213 util_debug("GEN","HEADER:\n");
214 util_debug("GEN"," version: = %d\n", code_header.version );
215 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
216 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
217 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
218 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
219 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
220 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
221 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
222 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
225 util_debug("GEN", "FUNCTIONS:\n");
226 for (; it < vec_size(code_functions); it++) {
227 size_t j = code_functions[it].entry;
228 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",
229 code_functions[it].entry,
230 code_functions[it].firstlocal,
231 code_functions[it].locals,
232 code_functions[it].profile,
233 code_functions[it].name,
234 code_functions[it].file,
235 code_functions[it].nargs,
236 code_functions[it].argsize[0],
237 code_functions[it].argsize[1],
238 code_functions[it].argsize[2],
239 code_functions[it].argsize[3],
240 code_functions[it].argsize[4],
241 code_functions[it].argsize[5],
242 code_functions[it].argsize[6],
243 code_functions[it].argsize[7]
246 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
247 /* Internal functions have no code */
248 if (code_functions[it].entry >= 0) {
249 util_debug("GEN", " CODE:\n");
251 if (code_statements[j].opcode != INSTR_DONE)
252 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
253 asm_instr[code_statements[j].opcode].m,
254 code_statements[j].o1.s1,
255 code_statements[j].o2.s1,
256 code_statements[j].o3.s1
259 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
267 vec_free(code_statements);
268 vec_free(code_linenums);
270 vec_free(code_fields);
271 vec_free(code_functions);
272 vec_free(code_globals);
273 vec_free(code_chars);
274 util_htdel(code_string_cache);