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
28 * We could use the old method of casting to uintptr_t then to void*
29 * or qcint_t; however, it's incredibly unsafe for two reasons.
30 * 1) The compilers aliasing optimization can legally make it unstable
31 * (it's undefined behaviour).
33 * 2) The cast itself depends on fresh storage (newly allocated in which
34 * ever function is using the cast macros), the contents of which are
35 * transferred in a way that the obligation to release storage is not
43 /* Some sanity macros */
44 #define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
45 #define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
47 void code_push_statement(code_t *code, prog_section_statement_t *stmt, int linenum)
49 vec_push(code->statements, *stmt);
50 vec_push(code->linenums, linenum);
53 void code_pop_statement(code_t *code)
55 vec_pop(code->statements);
56 vec_pop(code->linenums);
60 static prog_section_function_t empty_function = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
61 static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
62 static prog_section_def_t empty_def = {0, 0, 0};
64 code_t *code = (code_t*)mem_a(sizeof(code_t));
67 memset(code, 0, sizeof(code_t));
69 code->string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
72 * The way progs.dat is suppose to work is odd, there needs to be
73 * some null (empty) statements, functions, and 28 globals
76 vec_push(code->globals, 0);
78 vec_push(code->chars, '\0');
79 vec_push(code->functions, empty_function);
81 code_push_statement(code, &empty_statement, 0);
83 vec_push(code->defs, empty_def);
84 vec_push(code->fields, empty_def);
89 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
91 uint32_t code_genstring(code_t *code, const char *str) {
93 code_hash_entry_t existing;
99 if (!code->string_cached_empty) {
100 code->string_cached_empty = vec_size(code->chars);
101 vec_push(code->chars, 0);
103 return code->string_cached_empty;
106 if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
107 hash = ((unsigned char*)str)[strlen(str)-1];
108 CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash);
110 hash = util_hthash(code->string_cache, str);
111 CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash);
114 if (CODE_HASH_ENTER(existing))
115 return CODE_HASH_LEAVE(existing);
117 CODE_HASH_LEAVE(existing) = vec_size(code->chars);
118 vec_upload(code->chars, str, strlen(str)+1);
120 util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing));
121 return CODE_HASH_LEAVE(existing);
124 qcint_t code_alloc_field (code_t *code, size_t qcsize)
126 qcint_t pos = (qcint_t)code->entfields;
127 code->entfields += qcsize;
131 static void code_create_header(code_t *code, prog_header_t *code_header) {
132 code_header->statements.offset = sizeof(prog_header_t);
133 code_header->statements.length = vec_size(code->statements);
134 code_header->defs.offset = code_header->statements.offset + (sizeof(prog_section_statement_t) * vec_size(code->statements));
135 code_header->defs.length = vec_size(code->defs);
136 code_header->fields.offset = code_header->defs.offset + (sizeof(prog_section_def_t) * vec_size(code->defs));
137 code_header->fields.length = vec_size(code->fields);
138 code_header->functions.offset = code_header->fields.offset + (sizeof(prog_section_field_t) * vec_size(code->fields));
139 code_header->functions.length = vec_size(code->functions);
140 code_header->globals.offset = code_header->functions.offset + (sizeof(prog_section_function_t) * vec_size(code->functions));
141 code_header->globals.length = vec_size(code->globals);
142 code_header->strings.offset = code_header->globals.offset + (sizeof(int32_t) * vec_size(code->globals));
143 code_header->strings.length = vec_size(code->chars);
144 code_header->version = 6;
145 code_header->skip = 0;
147 if (OPTS_OPTION_BOOL(OPTION_FORCECRC))
148 code_header->crc16 = OPTS_OPTION_U16(OPTION_FORCED_CRC);
150 code_header->crc16 = code->crc;
151 code_header->entfield = code->entfields;
153 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
154 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
157 vec_push(code->chars, '\0'); /* > */
158 vec_push(code->chars, '\0'); /* = */
159 vec_push(code->chars, '\0'); /* P */
162 /* ensure all data is in LE format */
163 util_endianswap(&code_header->version, 1, sizeof(code_header->version));
164 util_endianswap(&code_header->crc16, 1, sizeof(code_header->crc16));
165 util_endianswap(&code_header->statements, 2, sizeof(code_header->statements.offset));
166 util_endianswap(&code_header->defs, 2, sizeof(code_header->statements.offset));
167 util_endianswap(&code_header->fields, 2, sizeof(code_header->statements.offset));
168 util_endianswap(&code_header->functions, 2, sizeof(code_header->statements.offset));
169 util_endianswap(&code_header->strings, 2, sizeof(code_header->statements.offset));
170 util_endianswap(&code_header->globals, 2, sizeof(code_header->statements.offset));
171 util_endianswap(&code_header->entfield, 1, sizeof(code_header->entfield));
174 * These are not part of the header but we ensure LE format here to save on duplicated
177 util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement_t));
178 util_endianswap(code->defs, vec_size(code->defs), sizeof(prog_section_def_t));
179 util_endianswap(code->fields, vec_size(code->fields), sizeof(prog_section_field_t));
180 util_endianswap(code->functions, vec_size(code->functions), sizeof(prog_section_function_t));
181 util_endianswap(code->globals, vec_size(code->globals), sizeof(int32_t));
185 * Same principle except this one allocates memory and writes the lno(optional) and the dat file
186 * directly out to allocated memory. Which is actually very useful for the future library support
187 * we're going to add.
189 bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) {
190 prog_header_t code_header;
196 code_create_header(code, &code_header);
198 #define WRITE_CHUNK(C,X,S) \
200 memcpy((void*)(&(*C)[offset]), (const void*)(X), (S)); \
204 /* Calculate size required to store entire file out to memory */
206 uint32_t version = 1;
208 *sizelno += 4; /* LNOF */
209 *sizelno += sizeof(version);
210 *sizelno += sizeof(code_header.defs.length);
211 *sizelno += sizeof(code_header.globals.length);
212 *sizelno += sizeof(code_header.fields.length);
213 *sizelno += sizeof(code_header.statements.length);
214 *sizelno += sizeof(code->linenums[0]) * vec_size(code->linenums);
216 *lnomem = (uint8_t*)mem_a(*sizelno);
218 WRITE_CHUNK(lnomem, "LNOF", 4);
219 WRITE_CHUNK(lnomem, &version, sizeof(version));
220 WRITE_CHUNK(lnomem, &code_header.defs.length, sizeof(code_header.defs.length));
221 WRITE_CHUNK(lnomem, &code_header.globals.length, sizeof(code_header.globals.length));
222 WRITE_CHUNK(lnomem, &code_header.fields.length, sizeof(code_header.fields.length));
223 WRITE_CHUNK(lnomem, &code_header.statements.length, sizeof(code_header.statements.length));
225 /* something went terribly wrong */
226 if (offset != *sizelno) {
234 /* Write out the dat */
235 *sizedat += sizeof(prog_header_t);
236 *sizedat += sizeof(prog_section_statement_t) * vec_size(code->statements);
237 *sizedat += sizeof(prog_section_def_t) * vec_size(code->defs);
238 *sizedat += sizeof(prog_section_field_t) * vec_size(code->fields);
239 *sizedat += sizeof(prog_section_function_t) * vec_size(code->functions);
240 *sizedat += sizeof(int32_t) * vec_size(code->globals);
241 *sizedat += 1 * vec_size(code->chars);
243 *datmem = (uint8_t*)mem_a(*sizedat);
245 WRITE_CHUNK(datmem, &code_header, sizeof(prog_header_t));
246 WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement_t) * vec_size(code->statements));
247 WRITE_CHUNK(datmem, code->defs, sizeof(prog_section_def_t) * vec_size(code->defs));
248 WRITE_CHUNK(datmem, code->fields, sizeof(prog_section_field_t) * vec_size(code->fields));
249 WRITE_CHUNK(datmem, code->functions, sizeof(prog_section_function_t) * vec_size(code->functions));
250 WRITE_CHUNK(datmem, code->globals, sizeof(int32_t) * vec_size(code->globals));
251 WRITE_CHUNK(datmem, code->chars, 1 * vec_size(code->chars));
255 vec_free(code->statements);
256 vec_free(code->linenums);
257 vec_free(code->defs);
258 vec_free(code->fields);
259 vec_free(code->functions);
260 vec_free(code->globals);
261 vec_free(code->chars);
263 util_htdel(code->string_cache);
269 bool code_write(code_t *code, const char *filename, const char *lnofile) {
270 prog_header_t code_header;
274 code_create_header(code, &code_header);
277 uint32_t version = 1;
279 fp = fs_file_open(lnofile, "wb");
283 util_endianswap(&version, 1, sizeof(version));
284 util_endianswap(code->linenums, vec_size(code->linenums), sizeof(code->linenums[0]));
287 if (fs_file_write("LNOF", 4, 1, fp) != 1 ||
288 fs_file_write(&version, sizeof(version), 1, fp) != 1 ||
289 fs_file_write(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
290 fs_file_write(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
291 fs_file_write(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
292 fs_file_write(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
293 fs_file_write(code->linenums, sizeof(code->linenums[0]), vec_size(code->linenums), fp) != vec_size(code->linenums))
295 con_err("failed to write lno file\n");
302 fp = fs_file_open(filename, "wb");
306 if (1 != fs_file_write(&code_header, sizeof(prog_header_t) , 1 , fp) ||
307 vec_size(code->statements) != fs_file_write(code->statements, sizeof(prog_section_statement_t), vec_size(code->statements), fp) ||
308 vec_size(code->defs) != fs_file_write(code->defs, sizeof(prog_section_def_t) , vec_size(code->defs) , fp) ||
309 vec_size(code->fields) != fs_file_write(code->fields, sizeof(prog_section_field_t) , vec_size(code->fields) , fp) ||
310 vec_size(code->functions) != fs_file_write(code->functions, sizeof(prog_section_function_t) , vec_size(code->functions) , fp) ||
311 vec_size(code->globals) != fs_file_write(code->globals, sizeof(int32_t) , vec_size(code->globals) , fp) ||
312 vec_size(code->chars) != fs_file_write(code->chars, 1 , vec_size(code->chars) , fp))
318 util_debug("GEN","HEADER:\n");
319 util_debug("GEN"," version: = %d\n", code_header.version );
320 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
321 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
322 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
323 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
324 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
325 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
326 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
327 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
330 util_debug("GEN", "FUNCTIONS:\n");
331 for (; it < vec_size(code->functions); it++) {
332 size_t j = code->functions[it].entry;
333 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",
334 code->functions[it].entry,
335 code->functions[it].firstlocal,
336 code->functions[it].locals,
337 code->functions[it].profile,
338 code->functions[it].name,
339 code->functions[it].file,
340 code->functions[it].nargs,
341 code->functions[it].argsize[0],
342 code->functions[it].argsize[1],
343 code->functions[it].argsize[2],
344 code->functions[it].argsize[3],
345 code->functions[it].argsize[4],
346 code->functions[it].argsize[5],
347 code->functions[it].argsize[6],
348 code->functions[it].argsize[7]
351 util_debug("GEN", " NAME: %s\n", &code->chars[code->functions[it].name]);
352 /* Internal functions have no code */
353 if (code->functions[it].entry >= 0) {
354 util_debug("GEN", " CODE:\n");
356 if (code->statements[j].opcode != INSTR_DONE)
357 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
358 util_instr_str[code->statements[j].opcode],
359 code->statements[j].o1.s1,
360 code->statements[j].o2.s1,
361 code->statements[j].o3.s1
364 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
376 void code_cleanup(code_t *code) {
377 vec_free(code->statements);
378 vec_free(code->linenums);
379 vec_free(code->defs);
380 vec_free(code->fields);
381 vec_free(code->functions);
382 vec_free(code->globals);
383 vec_free(code->chars);
385 util_htdel(code->string_cache);