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