]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
Merge branch 'cooking' of github.com:graphitemaster/gmqcc into cooking
[xonotic/gmqcc.git] / code.c
1 /*
2  * Copyright (C) 2012, 2013
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     if (OPTS_FLAG(SORT_OPERANDS)) {
75         switch (stmt.opcode) {
76 #define SINGLE(a) \
77             case INSTR_##a: \
78                 if (stmt.o1.u1 < stmt.o2.u1) { \
79                     uint16_t x = stmt.o1.u1; stmt.o1.u1 = stmt.o2.u1; stmt.o2.u1 = x; \
80                 } \
81                 break
82 #define PAIR(a,b) \
83             case INSTR_##a: \
84                 if (stmt.o1.u1 < stmt.o2.u1) { \
85                     uint16_t x = stmt.o1.u1; stmt.o1.u1 = stmt.o2.u1; stmt.o2.u1 = x; \
86                     stmt.opcode = INSTR_##b; \
87                 } \
88                 break; \
89             case INSTR_##b: \
90                 if (stmt.o1.u1 < stmt.o2.u1) { \
91                     uint16_t x = stmt.o1.u1; stmt.o1.u1 = stmt.o2.u1; stmt.o2.u1 = x; \
92                     stmt.opcode = INSTR_##a; \
93                 } \
94                 break
95             PAIR(MUL_VF, MUL_FV);
96             PAIR(LT, GT);
97             PAIR(LE, GE);
98             SINGLE(MUL_F);
99             SINGLE(MUL_V);
100             SINGLE(ADD_F);
101             SINGLE(ADD_V);
102             SINGLE(EQ_F);
103             SINGLE(EQ_V);
104             SINGLE(EQ_S);
105             SINGLE(EQ_E);
106             SINGLE(EQ_FNC);
107             SINGLE(NE_F);
108             SINGLE(NE_V);
109             SINGLE(NE_S);
110             SINGLE(NE_E);
111             SINGLE(NE_FNC);
112             SINGLE(AND);
113             SINGLE(OR);
114             SINGLE(BITAND);
115             SINGLE(BITOR);
116 #undef PAIR
117 #undef SINGLE
118         }
119     }
120
121     vec_push(code->statements, stmt);
122     vec_push(code->linenums,   (int)ctx.line);
123     vec_push(code->columnnums, (int)ctx.column);
124 }
125
126 void code_pop_statement(code_t *code)
127 {
128     vec_pop(code->statements);
129     vec_pop(code->linenums);
130     vec_pop(code->columnnums);
131 }
132
133 code_t *code_init() {
134     static lex_ctx_t                empty_ctx       = {0, 0, 0};
135     static prog_section_function_t  empty_function  = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
136     static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
137     static prog_section_def_t       empty_def       = {0, 0, 0};
138
139     code_t *code       = (code_t*)mem_a(sizeof(code_t));
140     int     i          = 0;
141
142     memset(code, 0, sizeof(code_t));
143     code->entfields    = 0;
144     code->string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
145
146     /*
147      * The way progs.dat is suppose to work is odd, there needs to be
148      * some null (empty) statements, functions, and 28 globals
149      */
150     for(; i < 28; i++)
151         vec_push(code->globals, 0);
152
153     vec_push(code->chars, '\0');
154     vec_push(code->functions,  empty_function);
155
156     code_push_statement(code, &empty_statement, empty_ctx);
157
158     vec_push(code->defs,    empty_def);
159     vec_push(code->fields,  empty_def);
160
161     return code;
162 }
163
164 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
165
166 uint32_t code_genstring(code_t *code, const char *str) {
167     size_t            hash;
168     code_hash_entry_t existing;
169
170     if (!str)
171         return 0;
172
173     if (!*str) {
174         if (!code->string_cached_empty) {
175             code->string_cached_empty = vec_size(code->chars);
176             vec_push(code->chars, 0);
177         }
178         return code->string_cached_empty;
179     }
180
181     if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
182         hash                      = ((unsigned char*)str)[strlen(str)-1];
183         CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash);
184     } else {
185         hash                      = util_hthash(code->string_cache, str);
186         CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash);
187     }
188
189     if (CODE_HASH_ENTER(existing))
190         return CODE_HASH_LEAVE(existing);
191
192     CODE_HASH_LEAVE(existing) = vec_size(code->chars);
193     vec_append(code->chars, strlen(str)+1, str);
194
195     util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing));
196     return CODE_HASH_LEAVE(existing);
197 }
198
199 qcint_t code_alloc_field (code_t *code, size_t qcsize)
200 {
201     qcint_t pos = (qcint_t)code->entfields;
202     code->entfields += qcsize;
203     return pos;
204 }
205
206 static size_t code_size_generic(code_t *code, prog_header_t *code_header, bool lno) {
207     size_t size = 0;
208     if (lno) {
209         size += 4;  /* LNOF */
210         size += sizeof(uint32_t); /* version */
211         size += sizeof(code_header->defs.length);
212         size += sizeof(code_header->globals.length);
213         size += sizeof(code_header->fields.length);
214         size += sizeof(code_header->statements.length);
215         size += sizeof(code->linenums[0])   * vec_size(code->linenums);
216         size += sizeof(code->columnnums[0]) * vec_size(code->columnnums);
217     } else {
218         size += sizeof(prog_header_t);
219         size += sizeof(prog_section_statement_t) * vec_size(code->statements);
220         size += sizeof(prog_section_def_t)       * vec_size(code->defs);
221         size += sizeof(prog_section_field_t)     * vec_size(code->fields);
222         size += sizeof(prog_section_function_t)  * vec_size(code->functions);
223         size += sizeof(int32_t)                  * vec_size(code->globals);
224         size += 1                                * vec_size(code->chars);
225     }
226     return size;
227 }
228
229 #define code_size_binary(C, H) code_size_generic((C), (H), false)
230 #define code_size_debug(C, H)  code_size_generic((C), (H), true)
231
232 static void code_create_header(code_t *code, prog_header_t *code_header, const char *filename, const char *lnofile) {
233     size_t i;
234
235     code_header->statements.offset = sizeof(prog_header_t);
236     code_header->statements.length = vec_size(code->statements);
237     code_header->defs.offset       = code_header->statements.offset + (sizeof(prog_section_statement_t) * vec_size(code->statements));
238     code_header->defs.length       = vec_size(code->defs);
239     code_header->fields.offset     = code_header->defs.offset       + (sizeof(prog_section_def_t)       * vec_size(code->defs));
240     code_header->fields.length     = vec_size(code->fields);
241     code_header->functions.offset  = code_header->fields.offset     + (sizeof(prog_section_field_t)     * vec_size(code->fields));
242     code_header->functions.length  = vec_size(code->functions);
243     code_header->globals.offset    = code_header->functions.offset  + (sizeof(prog_section_function_t)  * vec_size(code->functions));
244     code_header->globals.length    = vec_size(code->globals);
245     code_header->strings.offset    = code_header->globals.offset    + (sizeof(int32_t)                  * vec_size(code->globals));
246     code_header->strings.length    = vec_size(code->chars);
247     code_header->version           = 6;
248     code_header->skip              = 0;
249
250     if (OPTS_OPTION_BOOL(OPTION_FORCECRC))
251         code_header->crc16         = OPTS_OPTION_U16(OPTION_FORCED_CRC);
252     else
253         code_header->crc16         = code->crc;
254     code_header->entfield          = code->entfields;
255
256     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
257         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
258
259         /* >= + P */
260         vec_push(code->chars, '\0'); /* > */
261         vec_push(code->chars, '\0'); /* = */
262         vec_push(code->chars, '\0'); /* P */
263     }
264
265     /* ensure all data is in LE format */
266     util_endianswap(&code_header->version,    1, sizeof(code_header->version));
267     util_endianswap(&code_header->crc16,      1, sizeof(code_header->crc16));
268     util_endianswap(&code_header->statements, 2, sizeof(code_header->statements.offset));
269     util_endianswap(&code_header->defs,       2, sizeof(code_header->statements.offset));
270     util_endianswap(&code_header->fields,     2, sizeof(code_header->statements.offset));
271     util_endianswap(&code_header->functions,  2, sizeof(code_header->statements.offset));
272     util_endianswap(&code_header->strings,    2, sizeof(code_header->statements.offset));
273     util_endianswap(&code_header->globals,    2, sizeof(code_header->statements.offset));
274     util_endianswap(&code_header->entfield,   1, sizeof(code_header->entfield));
275
276     /*
277      * These are not part of the header but we ensure LE format here to save on duplicated
278      * code.
279      */
280     util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement_t));
281     util_endianswap(code->defs,       vec_size(code->defs),       sizeof(prog_section_def_t));
282     util_endianswap(code->fields,     vec_size(code->fields),     sizeof(prog_section_field_t));
283     util_endianswap(code->functions,  vec_size(code->functions),  sizeof(prog_section_function_t));
284     util_endianswap(code->globals,    vec_size(code->globals),    sizeof(int32_t));
285
286
287     if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
288         if (lnofile)
289             con_out("writing '%s' and '%s'...\n", filename, lnofile);
290         else
291             con_out("writing '%s'\n", filename);
292     }
293
294     if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
295         !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
296     {
297         char buffer[1024];
298         con_out("\nOptimizations:\n");
299         for (i = 0; i < COUNT_OPTIMIZATIONS; ++i) {
300             if (opts_optimizationcount[i]) {
301                 util_optimizationtostr(opts_opt_list[i].name, buffer, sizeof(buffer));
302                 con_out(
303                     "    %s: %u\n",
304                     buffer,
305                     (unsigned int)opts_optimizationcount[i]
306                 );
307             }
308         }
309     }
310 }
311
312 static void code_stats(const char *filename, const char *lnofile, code_t *code, prog_header_t *code_header) {
313     if (OPTS_OPTION_BOOL(OPTION_QUIET) ||
314         OPTS_OPTION_BOOL(OPTION_PP_ONLY))
315             return;
316
317     con_out("\nFile statistics:\n");
318     con_out("    dat:\n");
319     con_out("        name: %s\n",         filename);
320     con_out("        size: %u (bytes)\n", code_size_binary(code, code_header));
321     con_out("        crc:  0x%04X\n",     code->crc);
322
323     if (lnofile) {
324         con_out("    lno:\n");
325         con_out("        name: %s\n",  lnofile);
326         con_out("        size: %u (bytes)\n",  code_size_debug(code, code_header));
327     }
328
329     con_out("\n");
330 }
331
332 /*
333  * Same principle except this one allocates memory and writes the lno(optional) and the dat file
334  * directly out to allocated memory. Which is actually very useful for the future library support
335  * we're going to add.
336  */
337 #if 0
338 static bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) GMQCC_UNUSED {
339     prog_header_t code_header;
340     uint32_t      offset  = 0;
341
342     if (!datmem)
343         return false;
344
345     code_create_header(code, &code_header, "<<memory>>", "<<memory>>");
346
347     #define WRITE_CHUNK(C,X,S)                                     \
348         do {                                                       \
349             memcpy((void*)(&(*C)[offset]), (const void*)(X), (S)); \
350             offset += (S);                                         \
351         } while (0)
352
353     /* Calculate size required to store entire file out to memory */
354     if (lnomem) {
355         uint32_t version = 1;
356
357         *sizelno = code_size_debug(code, &code_header);
358         *lnomem  = (uint8_t*)mem_a(*sizelno);
359
360         WRITE_CHUNK(lnomem, "LNOF",                         4);
361         WRITE_CHUNK(lnomem, &version,                       sizeof(version));
362         WRITE_CHUNK(lnomem, &code_header.defs.length,       sizeof(code_header.defs.length));
363         WRITE_CHUNK(lnomem, &code_header.globals.length,    sizeof(code_header.globals.length));
364         WRITE_CHUNK(lnomem, &code_header.fields.length,     sizeof(code_header.fields.length));
365         WRITE_CHUNK(lnomem, &code_header.statements.length, sizeof(code_header.statements.length));
366
367         /* something went terribly wrong */
368         if (offset != *sizelno) {
369             mem_d(*lnomem);
370             *sizelno = 0;
371             return false;
372         }
373         offset = 0;
374     }
375
376     /* Write out the dat */
377     *sizedat = code_size_binary(code, &code_header);
378     *datmem  = (uint8_t*)mem_a(*sizedat);
379
380     WRITE_CHUNK(datmem, &code_header,     sizeof(prog_header_t));
381     WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement_t) * vec_size(code->statements));
382     WRITE_CHUNK(datmem, code->defs,       sizeof(prog_section_def_t)       * vec_size(code->defs));
383     WRITE_CHUNK(datmem, code->fields,     sizeof(prog_section_field_t)     * vec_size(code->fields));
384     WRITE_CHUNK(datmem, code->functions,  sizeof(prog_section_function_t)  * vec_size(code->functions));
385     WRITE_CHUNK(datmem, code->globals,    sizeof(int32_t)                  * vec_size(code->globals));
386     WRITE_CHUNK(datmem, code->chars,      1                                * vec_size(code->chars));
387
388     vec_free(code->statements);
389     vec_free(code->linenums);
390     vec_free(code->columnnums);
391     vec_free(code->defs);
392     vec_free(code->fields);
393     vec_free(code->functions);
394     vec_free(code->globals);
395     vec_free(code->chars);
396
397     util_htdel(code->string_cache);
398     mem_d(code);
399     code_stats("<<memory>>", (lnomem) ? "<<memory>>" : NULL, code, &code_header);
400     return true;
401 }
402 #endif /*!#if 0 reenable when ready to be used */
403 #undef WRITE_CHUNK
404
405 bool code_write(code_t *code, const char *filename, const char *lnofile) {
406     prog_header_t  code_header;
407     FILE          *fp           = NULL;
408     size_t         it           = 2;
409
410     code_create_header(code, &code_header, filename, lnofile);
411
412     if (lnofile) {
413         uint32_t version = 1;
414
415         fp = fs_file_open(lnofile, "wb");
416         if (!fp)
417             return false;
418
419         util_endianswap(&version,         1,                          sizeof(version));
420         util_endianswap(code->linenums,   vec_size(code->linenums),   sizeof(code->linenums[0]));
421         util_endianswap(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0]));
422
423         if (fs_file_write("LNOF",                          4,                                      1,                          fp) != 1 ||
424             fs_file_write(&version,                        sizeof(version),                        1,                          fp) != 1 ||
425             fs_file_write(&code_header.defs.length,        sizeof(code_header.defs.length),        1,                          fp) != 1 ||
426             fs_file_write(&code_header.globals.length,     sizeof(code_header.globals.length),     1,                          fp) != 1 ||
427             fs_file_write(&code_header.fields.length,      sizeof(code_header.fields.length),      1,                          fp) != 1 ||
428             fs_file_write(&code_header.statements.length,  sizeof(code_header.statements.length),  1,                          fp) != 1 ||
429             fs_file_write(code->linenums,                  sizeof(code->linenums[0]),              vec_size(code->linenums),   fp) != vec_size(code->linenums) ||
430             fs_file_write(code->columnnums,                sizeof(code->columnnums[0]),            vec_size(code->columnnums), fp) != vec_size(code->columnnums))
431         {
432             con_err("failed to write lno file\n");
433         }
434
435         fs_file_close(fp);
436         fp = NULL;
437     }
438
439     fp = fs_file_open(filename, "wb");
440     if (!fp)
441         return false;
442
443     if (1                          != fs_file_write(&code_header,     sizeof(prog_header_t)           , 1                         , fp) ||
444         vec_size(code->statements) != fs_file_write(code->statements, sizeof(prog_section_statement_t), vec_size(code->statements), fp) ||
445         vec_size(code->defs)       != fs_file_write(code->defs,       sizeof(prog_section_def_t)      , vec_size(code->defs)      , fp) ||
446         vec_size(code->fields)     != fs_file_write(code->fields,     sizeof(prog_section_field_t)    , vec_size(code->fields)    , fp) ||
447         vec_size(code->functions)  != fs_file_write(code->functions,  sizeof(prog_section_function_t) , vec_size(code->functions) , fp) ||
448         vec_size(code->globals)    != fs_file_write(code->globals,    sizeof(int32_t)                 , vec_size(code->globals)   , fp) ||
449         vec_size(code->chars)      != fs_file_write(code->chars,      1                               , vec_size(code->chars)     , fp))
450     {
451         fs_file_close(fp);
452         return false;
453     }
454
455     util_debug("GEN","HEADER:\n");
456     util_debug("GEN","    version:    = %d\n", code_header.version );
457     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
458     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
459     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
460     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
461     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
462     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
463     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
464     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
465
466     /* FUNCTIONS */
467     util_debug("GEN", "FUNCTIONS:\n");
468     for (; it < vec_size(code->functions); it++) {
469         size_t j = code->functions[it].entry;
470         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",
471             code->functions[it].entry,
472             code->functions[it].firstlocal,
473             code->functions[it].locals,
474             code->functions[it].profile,
475             code->functions[it].name,
476             code->functions[it].file,
477             code->functions[it].nargs,
478             code->functions[it].argsize[0],
479             code->functions[it].argsize[1],
480             code->functions[it].argsize[2],
481             code->functions[it].argsize[3],
482             code->functions[it].argsize[4],
483             code->functions[it].argsize[5],
484             code->functions[it].argsize[6],
485             code->functions[it].argsize[7]
486
487         );
488         util_debug("GEN", "    NAME: %s\n", &code->chars[code->functions[it].name]);
489         /* Internal functions have no code */
490         if (code->functions[it].entry >= 0) {
491             util_debug("GEN", "    CODE:\n");
492             for (;;) {
493                 if (code->statements[j].opcode != INSTR_DONE)
494                     util_debug("GEN", "        %-12s {% 5i,% 5i,% 5i}\n",
495                         util_instr_str[code->statements[j].opcode],
496                         code->statements[j].o1.s1,
497                         code->statements[j].o2.s1,
498                         code->statements[j].o3.s1
499                     );
500                 else {
501                     util_debug("GEN", "        DONE  {0x00000,0x00000,0x00000}\n");
502                     break;
503                 }
504                 j++;
505             }
506         }
507     }
508
509     fs_file_close(fp);
510     code_stats(filename, lnofile, code, &code_header);
511     return true;
512 }
513
514 void code_cleanup(code_t *code) {
515     vec_free(code->statements);
516     vec_free(code->linenums);
517     vec_free(code->columnnums);
518     vec_free(code->defs);
519     vec_free(code->fields);
520     vec_free(code->functions);
521     vec_free(code->globals);
522     vec_free(code->chars);
523
524     util_htdel(code->string_cache);
525
526     mem_d(code);
527 }