]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
Big-endian: Byteswap only the field contents when writing progs.dat
[xonotic/gmqcc.git] / code.c
1 /*
2  * Copyright (C) 2012, 2013, 2014
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
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
22  * SOFTWARE.
23  */
24 #include <string.h>
25 #include "gmqcc.h"
26
27 /*
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).
32  *
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
36  *    propagated.
37  */
38 typedef union {
39     void   *enter;
40     qcint_t leave;
41 } code_hash_entry_t;
42
43 /* Some sanity macros */
44 #define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
45 #define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
46
47 void code_push_statement(code_t *code, prog_section_statement_t *stmt_in, lex_ctx_t ctx)
48 {
49     prog_section_statement_t stmt = *stmt_in;
50
51     if (OPTS_FLAG(TYPELESS_STORES)) {
52         switch (stmt.opcode) {
53             case INSTR_LOAD_S:
54             case INSTR_LOAD_ENT:
55             case INSTR_LOAD_FLD:
56             case INSTR_LOAD_FNC:
57                 stmt.opcode = INSTR_LOAD_F;
58                 break;
59             case INSTR_STORE_S:
60             case INSTR_STORE_ENT:
61             case INSTR_STORE_FLD:
62             case INSTR_STORE_FNC:
63                 stmt.opcode = INSTR_STORE_F;
64                 break;
65             case INSTR_STOREP_S:
66             case INSTR_STOREP_ENT:
67             case INSTR_STOREP_FLD:
68             case INSTR_STOREP_FNC:
69                 stmt.opcode = INSTR_STOREP_F;
70                 break;
71         }
72     }
73
74
75     if (OPTS_FLAG(SORT_OPERANDS)) {
76         uint16_t pair;
77
78         switch (stmt.opcode) {
79             case INSTR_MUL_F:
80             case INSTR_MUL_V:
81             case INSTR_ADD_F:
82             case INSTR_EQ_F:
83             case INSTR_EQ_S:
84             case INSTR_EQ_E:
85             case INSTR_EQ_FNC:
86             case INSTR_NE_F:
87             case INSTR_NE_V:
88             case INSTR_NE_S:
89             case INSTR_NE_E:
90             case INSTR_NE_FNC:
91             case INSTR_AND:
92             case INSTR_OR:
93             case INSTR_BITAND:
94             case INSTR_BITOR:
95                 if (stmt.o1.u1 < stmt.o2.u1) {
96                     uint16_t a = stmt.o2.u1;
97                     stmt.o1.u1 = stmt.o2.u1;
98                     stmt.o2.u1 = a;
99                 }
100                 break;
101
102             case INSTR_MUL_VF: pair = INSTR_MUL_FV; goto case_pair_gen;
103             case INSTR_MUL_FV: pair = INSTR_MUL_VF; goto case_pair_gen;
104             case INSTR_LT:     pair = INSTR_GT;     goto case_pair_gen;
105             case INSTR_GT:     pair = INSTR_LT;     goto case_pair_gen;
106             case INSTR_LE:     pair = INSTR_GT;     goto case_pair_gen;
107             case INSTR_GE:     pair = INSTR_LE;
108
109             case_pair_gen:
110                 if (stmt.o1.u1 < stmt.o2.u1) {
111                     uint16_t x  = stmt.o1.u1;
112                     stmt.o1.u1  = stmt.o2.u1;
113                     stmt.o2.u1  = x;
114                     stmt.opcode = pair;
115                 }
116                 break;
117         }
118     }
119
120     vec_push(code->statements, stmt);
121     vec_push(code->linenums,   (int)ctx.line);
122     vec_push(code->columnnums, (int)ctx.column);
123 }
124
125 void code_pop_statement(code_t *code)
126 {
127     vec_pop(code->statements);
128     vec_pop(code->linenums);
129     vec_pop(code->columnnums);
130 }
131
132 code_t *code_init() {
133     static lex_ctx_t                empty_ctx       = {0, 0, 0};
134     static prog_section_function_t  empty_function  = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
135     static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
136     static prog_section_def_t       empty_def       = {0, 0, 0};
137
138     code_t *code       = (code_t*)mem_a(sizeof(code_t));
139     int     i          = 0;
140
141     memset(code, 0, sizeof(code_t));
142     code->entfields    = 0;
143     code->string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
144
145     /*
146      * The way progs.dat is suppose to work is odd, there needs to be
147      * some null (empty) statements, functions, and 28 globals
148      */
149     for(; i < 28; i++)
150         vec_push(code->globals, 0);
151
152     vec_push(code->chars, '\0');
153     vec_push(code->functions,  empty_function);
154
155     code_push_statement(code, &empty_statement, empty_ctx);
156
157     vec_push(code->defs,    empty_def);
158     vec_push(code->fields,  empty_def);
159
160     return code;
161 }
162
163 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
164
165 uint32_t code_genstring(code_t *code, const char *str) {
166     size_t            hash;
167     code_hash_entry_t existing;
168
169     if (!str)
170         return 0;
171
172     if (!*str) {
173         if (!code->string_cached_empty) {
174             code->string_cached_empty = vec_size(code->chars);
175             vec_push(code->chars, 0);
176         }
177         return code->string_cached_empty;
178     }
179
180     if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
181         hash                      = ((unsigned char*)str)[strlen(str)-1];
182         CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash);
183     } else {
184         hash                      = util_hthash(code->string_cache, str);
185         CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash);
186     }
187
188     if (CODE_HASH_ENTER(existing))
189         return CODE_HASH_LEAVE(existing);
190
191     CODE_HASH_LEAVE(existing) = vec_size(code->chars);
192     vec_append(code->chars, strlen(str)+1, str);
193
194     util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing));
195     return CODE_HASH_LEAVE(existing);
196 }
197
198 qcint_t code_alloc_field (code_t *code, size_t qcsize)
199 {
200     qcint_t pos = (qcint_t)code->entfields;
201     code->entfields += qcsize;
202     return pos;
203 }
204
205 static size_t code_size_generic(code_t *code, prog_header_t *code_header, bool lno) {
206     size_t size = 0;
207     if (lno) {
208         size += 4;  /* LNOF */
209         size += sizeof(uint32_t); /* version */
210         size += sizeof(code_header->defs.length);
211         size += sizeof(code_header->globals.length);
212         size += sizeof(code_header->fields.length);
213         size += sizeof(code_header->statements.length);
214         size += sizeof(code->linenums[0])   * vec_size(code->linenums);
215         size += sizeof(code->columnnums[0]) * vec_size(code->columnnums);
216     } else {
217         size += sizeof(prog_header_t);
218         size += sizeof(prog_section_statement_t) * vec_size(code->statements);
219         size += sizeof(prog_section_def_t)       * vec_size(code->defs);
220         size += sizeof(prog_section_field_t)     * vec_size(code->fields);
221         size += sizeof(prog_section_function_t)  * vec_size(code->functions);
222         size += sizeof(int32_t)                  * vec_size(code->globals);
223         size += 1                                * vec_size(code->chars);
224     }
225     return size;
226 }
227
228 #define code_size_binary(C, H) code_size_generic((C), (H), false)
229 #define code_size_debug(C, H)  code_size_generic((C), (H), true)
230
231 static void code_create_header(code_t *code, prog_header_t *code_header, const char *filename, const char *lnofile) {
232     size_t i;
233
234     code_header->statements.offset = sizeof(prog_header_t);
235     code_header->statements.length = vec_size(code->statements);
236     code_header->defs.offset       = code_header->statements.offset + (sizeof(prog_section_statement_t) * vec_size(code->statements));
237     code_header->defs.length       = vec_size(code->defs);
238     code_header->fields.offset     = code_header->defs.offset       + (sizeof(prog_section_def_t)       * vec_size(code->defs));
239     code_header->fields.length     = vec_size(code->fields);
240     code_header->functions.offset  = code_header->fields.offset     + (sizeof(prog_section_field_t)     * vec_size(code->fields));
241     code_header->functions.length  = vec_size(code->functions);
242     code_header->globals.offset    = code_header->functions.offset  + (sizeof(prog_section_function_t)  * vec_size(code->functions));
243     code_header->globals.length    = vec_size(code->globals);
244     code_header->strings.offset    = code_header->globals.offset    + (sizeof(int32_t)                  * vec_size(code->globals));
245     code_header->strings.length    = vec_size(code->chars);
246     code_header->version           = 6;
247     code_header->skip              = 0;
248
249     if (OPTS_OPTION_BOOL(OPTION_FORCECRC))
250         code_header->crc16         = OPTS_OPTION_U16(OPTION_FORCED_CRC);
251     else
252         code_header->crc16         = code->crc;
253     code_header->entfield          = code->entfields;
254
255     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
256         /* >= + P */
257         vec_push(code->chars, '\0'); /* > */
258         vec_push(code->chars, '\0'); /* = */
259         vec_push(code->chars, '\0'); /* P */
260     }
261
262     /* ensure all data is in LE format */
263     util_tolittleendian(&code_header->version,              sizeof(code_header->version));
264     util_tolittleendian(&code_header->crc16,                sizeof(code_header->crc16));
265     util_tolittleendian(&code_header->statements.offset,    sizeof(code_header->statements.offset));
266     util_tolittleendian(&code_header->statements.length,    sizeof(code_header->statements.length));
267     util_tolittleendian(&code_header->defs.offset,          sizeof(code_header->defs.offset));
268     util_tolittleendian(&code_header->defs.length,          sizeof(code_header->defs.length));
269     util_tolittleendian(&code_header->fields.offset,        sizeof(code_header->fields.offset));
270     util_tolittleendian(&code_header->fields.length,        sizeof(code_header->fields.length));
271     util_tolittleendian(&code_header->functions.offset,     sizeof(code_header->functions.offset));
272     util_tolittleendian(&code_header->functions.length,     sizeof(code_header->functions.length));
273     util_tolittleendian(&code_header->strings.offset,       sizeof(code_header->strings.offset));
274     util_tolittleendian(&code_header->strings.length,       sizeof(code_header->strings.length));
275     util_tolittleendian(&code_header->globals.offset,       sizeof(code_header->globals.offset));
276     util_tolittleendian(&code_header->globals.length,       sizeof(code_header->globals.length));
277     util_tolittleendian(&code_header->entfield,             sizeof(code_header->entfield));
278
279     /*
280      * These are not part of the header but we ensure LE format here to save on duplicated
281      * code.
282      */
283
284     for (i = 0; i < vec_size(code->statements); ++i) {
285         util_tolittleendian(& code->statements[i].opcode,   sizeof(code->statements[i].opcode));
286         util_tolittleendian(& code->statements[i].o1,       sizeof(code->statements[i].o1));
287         util_tolittleendian(& code->statements[i].o2,       sizeof(code->statements[i].o2));
288         util_tolittleendian(& code->statements[i].o3,       sizeof(code->statements[i].o3));
289     }
290
291     for (i = 0; i < vec_size(code->defs); ++i) {
292         util_tolittleendian(& code->defs[i].type,   sizeof(code->defs[i].type));
293         util_tolittleendian(& code->defs[i].offset, sizeof(code->defs[i].offset));
294         util_tolittleendian(& code->defs[i].name,   sizeof(code->defs[i].name));
295     }
296
297     for (i = 0; i < vec_size(code->fields); ++i) {
298         util_tolittleendian(& code->fields[i].type,   sizeof(code->fields[i].type));
299         util_tolittleendian(& code->fields[i].offset, sizeof(code->fields[i].offset));
300         util_tolittleendian(& code->fields[i].name,   sizeof(code->fields[i].name));
301     }
302
303     for (i = 0; i < vec_size(code->functions); ++i) {
304         util_tolittleendian(& code->functions[i].entry,         sizeof(code->functions[i].entry));
305         util_tolittleendian(& code->functions[i].firstlocal,    sizeof(code->functions[i].firstlocal));
306         util_tolittleendian(& code->functions[i].locals,        sizeof(code->functions[i].locals));
307         util_tolittleendian(& code->functions[i].profile,       sizeof(code->functions[i].profile));
308         util_tolittleendian(& code->functions[i].name,          sizeof(code->functions[i].name));
309         util_tolittleendian(& code->functions[i].file,          sizeof(code->functions[i].file));
310         util_tolittleendian(& code->functions[i].nargs,         sizeof(code->functions[i].nargs));
311         /* Don't swap argsize[] - it's just a byte array, which Quake uses only as such. */
312     }
313
314     util_tolittleendianarray(code->globals, vec_size(code->globals), sizeof(int32_t));
315
316
317     if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
318         if (lnofile)
319             con_out("writing '%s' and '%s'...\n", filename, lnofile);
320         else
321             con_out("writing '%s'\n", filename);
322     }
323
324     if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
325         !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
326     {
327         char buffer[1024];
328         con_out("\nOptimizations:\n");
329         for (i = 0; i < COUNT_OPTIMIZATIONS; ++i) {
330             if (opts_optimizationcount[i]) {
331                 util_optimizationtostr(opts_opt_list[i].name, buffer, sizeof(buffer));
332                 con_out(
333                     "    %s: %u\n",
334                     buffer,
335                     (unsigned int)opts_optimizationcount[i]
336                 );
337             }
338         }
339     }
340 }
341
342 static void code_stats(const char *filename, const char *lnofile, code_t *code, prog_header_t *code_header) {
343     if (OPTS_OPTION_BOOL(OPTION_QUIET) ||
344         OPTS_OPTION_BOOL(OPTION_PP_ONLY))
345             return;
346
347     con_out("\nFile statistics:\n");
348     con_out("    dat:\n");
349     con_out("        name: %s\n",         filename);
350     con_out("        size: %u (bytes)\n", code_size_binary(code, code_header));
351     con_out("        crc:  0x%04X\n",     code->crc);
352
353     if (lnofile) {
354         con_out("    lno:\n");
355         con_out("        name: %s\n",  lnofile);
356         con_out("        size: %u (bytes)\n",  code_size_debug(code, code_header));
357     }
358
359     con_out("\n");
360 }
361
362 /*
363  * Same principle except this one allocates memory and writes the lno(optional) and the dat file
364  * directly out to allocated memory. Which is actually very useful for the future library support
365  * we're going to add.
366  */
367 #if 0
368 static bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) GMQCC_UNUSED {
369     prog_header_t code_header;
370     uint32_t      offset  = 0;
371
372     if (!datmem)
373         return false;
374
375     code_create_header(code, &code_header, "<<memory>>", "<<memory>>");
376
377     #define WRITE_CHUNK(C,X,S)                                     \
378         do {                                                       \
379             memcpy((void*)(&(*C)[offset]), (const void*)(X), (S)); \
380             offset += (S);                                         \
381         } while (0)
382
383     /* Calculate size required to store entire file out to memory */
384     if (lnomem) {
385         uint32_t version = 1;
386
387         *sizelno = code_size_debug(code, &code_header);
388         *lnomem  = (uint8_t*)mem_a(*sizelno);
389
390         WRITE_CHUNK(lnomem, "LNOF",                         4);
391         WRITE_CHUNK(lnomem, &version,                       sizeof(version));
392         WRITE_CHUNK(lnomem, &code_header.defs.length,       sizeof(code_header.defs.length));
393         WRITE_CHUNK(lnomem, &code_header.globals.length,    sizeof(code_header.globals.length));
394         WRITE_CHUNK(lnomem, &code_header.fields.length,     sizeof(code_header.fields.length));
395         WRITE_CHUNK(lnomem, &code_header.statements.length, sizeof(code_header.statements.length));
396
397         /* something went terribly wrong */
398         if (offset != *sizelno) {
399             mem_d(*lnomem);
400             *sizelno = 0;
401             return false;
402         }
403         offset = 0;
404     }
405
406     /* Write out the dat */
407     *sizedat = code_size_binary(code, &code_header);
408     *datmem  = (uint8_t*)mem_a(*sizedat);
409
410     WRITE_CHUNK(datmem, &code_header,     sizeof(prog_header_t));
411     WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement_t) * vec_size(code->statements));
412     WRITE_CHUNK(datmem, code->defs,       sizeof(prog_section_def_t)       * vec_size(code->defs));
413     WRITE_CHUNK(datmem, code->fields,     sizeof(prog_section_field_t)     * vec_size(code->fields));
414     WRITE_CHUNK(datmem, code->functions,  sizeof(prog_section_function_t)  * vec_size(code->functions));
415     WRITE_CHUNK(datmem, code->globals,    sizeof(int32_t)                  * vec_size(code->globals));
416     WRITE_CHUNK(datmem, code->chars,      1                                * vec_size(code->chars));
417
418     vec_free(code->statements);
419     vec_free(code->linenums);
420     vec_free(code->columnnums);
421     vec_free(code->defs);
422     vec_free(code->fields);
423     vec_free(code->functions);
424     vec_free(code->globals);
425     vec_free(code->chars);
426
427     util_htdel(code->string_cache);
428     mem_d(code);
429     code_stats("<<memory>>", (lnomem) ? "<<memory>>" : NULL, code, &code_header);
430     return true;
431 }
432 #endif /*!#if 0 reenable when ready to be used */
433 #undef WRITE_CHUNK
434
435 bool code_write(code_t *code, const char *filename, const char *lnofile) {
436     prog_header_t  code_header;
437     fs_file_t     *fp = NULL;
438
439     code_create_header(code, &code_header, filename, lnofile);
440
441     if (lnofile) {
442         uint32_t version = 1;
443
444         fp = fs_file_open(lnofile, "wb");
445         if (!fp)
446             return false;
447
448         util_tolittleendian     (&version,                                     sizeof(version));
449         util_tolittleendianarray(code->linenums,   vec_size(code->linenums),   sizeof(code->linenums[0]));
450         util_tolittleendianarray(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0]));
451
452         if (fs_file_write("LNOF",                          4,                                      1,                          fp) != 1 ||
453             fs_file_write(&version,                        sizeof(version),                        1,                          fp) != 1 ||
454             fs_file_write(&code_header.defs.length,        sizeof(code_header.defs.length),        1,                          fp) != 1 ||
455             fs_file_write(&code_header.globals.length,     sizeof(code_header.globals.length),     1,                          fp) != 1 ||
456             fs_file_write(&code_header.fields.length,      sizeof(code_header.fields.length),      1,                          fp) != 1 ||
457             fs_file_write(&code_header.statements.length,  sizeof(code_header.statements.length),  1,                          fp) != 1 ||
458             fs_file_write(code->linenums,                  sizeof(code->linenums[0]),              vec_size(code->linenums),   fp) != vec_size(code->linenums) ||
459             fs_file_write(code->columnnums,                sizeof(code->columnnums[0]),            vec_size(code->columnnums), fp) != vec_size(code->columnnums))
460         {
461             con_err("failed to write lno file\n");
462         }
463
464         fs_file_close(fp);
465         fp = NULL;
466     }
467
468     fp = fs_file_open(filename, "wb");
469     if (!fp)
470         return false;
471
472     if (1                          != fs_file_write(&code_header,     sizeof(prog_header_t)           , 1                         , fp) ||
473         vec_size(code->statements) != fs_file_write(code->statements, sizeof(prog_section_statement_t), vec_size(code->statements), fp) ||
474         vec_size(code->defs)       != fs_file_write(code->defs,       sizeof(prog_section_def_t)      , vec_size(code->defs)      , fp) ||
475         vec_size(code->fields)     != fs_file_write(code->fields,     sizeof(prog_section_field_t)    , vec_size(code->fields)    , fp) ||
476         vec_size(code->functions)  != fs_file_write(code->functions,  sizeof(prog_section_function_t) , vec_size(code->functions) , fp) ||
477         vec_size(code->globals)    != fs_file_write(code->globals,    sizeof(int32_t)                 , vec_size(code->globals)   , fp) ||
478         vec_size(code->chars)      != fs_file_write(code->chars,      1                               , vec_size(code->chars)     , fp))
479     {
480         fs_file_close(fp);
481         return false;
482     }
483
484     fs_file_close(fp);
485     code_stats(filename, lnofile, code, &code_header);
486     return true;
487 }
488
489 void code_cleanup(code_t *code) {
490     vec_free(code->statements);
491     vec_free(code->linenums);
492     vec_free(code->columnnums);
493     vec_free(code->defs);
494     vec_free(code->fields);
495     vec_free(code->functions);
496     vec_free(code->globals);
497     vec_free(code->chars);
498
499     util_htdel(code->string_cache);
500
501     mem_d(code);
502 }