fix member binops on entity fields to generate STOREP, this fixes stuff like ent...
[xonotic/gmqcc.git] / code.cpp
1 #include <string.h>
2 #include "gmqcc.h"
3
4 /*
5  * We could use the old method of casting to uintptr_t then to void*
6  * or qcint_t; however, it's incredibly unsafe for two reasons.
7  * 1) The compilers aliasing optimization can legally make it unstable
8  *    (it's undefined behaviour).
9  *
10  * 2) The cast itself depends on fresh storage (newly allocated in which
11  *    ever function is using the cast macros), the contents of which are
12  *    transferred in a way that the obligation to release storage is not
13  *    propagated.
14  */
15 typedef union {
16     void   *enter;
17     qcint_t leave;
18 } code_hash_entry_t;
19
20 /* Some sanity macros */
21 #define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
22 #define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
23
24 void code_push_statement(code_t *code, prog_section_statement_t *stmt_in, lex_ctx_t ctx)
25 {
26     prog_section_statement_t stmt = *stmt_in;
27
28     if (OPTS_FLAG(TYPELESS_STORES)) {
29         switch (stmt.opcode) {
30             case INSTR_LOAD_S:
31             case INSTR_LOAD_ENT:
32             case INSTR_LOAD_FLD:
33             case INSTR_LOAD_FNC:
34                 stmt.opcode = INSTR_LOAD_F;
35                 break;
36             case INSTR_STORE_S:
37             case INSTR_STORE_ENT:
38             case INSTR_STORE_FLD:
39             case INSTR_STORE_FNC:
40                 stmt.opcode = INSTR_STORE_F;
41                 break;
42             case INSTR_STOREP_S:
43             case INSTR_STOREP_ENT:
44             case INSTR_STOREP_FLD:
45             case INSTR_STOREP_FNC:
46                 stmt.opcode = INSTR_STOREP_F;
47                 break;
48         }
49     }
50
51
52     if (OPTS_FLAG(SORT_OPERANDS)) {
53         uint16_t pair;
54
55         switch (stmt.opcode) {
56             case INSTR_MUL_F:
57             case INSTR_MUL_V:
58             case INSTR_ADD_F:
59             case INSTR_EQ_F:
60             case INSTR_EQ_S:
61             case INSTR_EQ_E:
62             case INSTR_EQ_FNC:
63             case INSTR_NE_F:
64             case INSTR_NE_V:
65             case INSTR_NE_S:
66             case INSTR_NE_E:
67             case INSTR_NE_FNC:
68             case INSTR_AND:
69             case INSTR_OR:
70             case INSTR_BITAND:
71             case INSTR_BITOR:
72                 if (stmt.o1.u1 < stmt.o2.u1) {
73                     uint16_t a = stmt.o2.u1;
74                     stmt.o1.u1 = stmt.o2.u1;
75                     stmt.o2.u1 = a;
76                 }
77                 break;
78
79             case INSTR_MUL_VF: pair = INSTR_MUL_FV; goto case_pair_gen;
80             case INSTR_MUL_FV: pair = INSTR_MUL_VF; goto case_pair_gen;
81             case INSTR_LT:     pair = INSTR_GT;     goto case_pair_gen;
82             case INSTR_GT:     pair = INSTR_LT;     goto case_pair_gen;
83             case INSTR_LE:     pair = INSTR_GT;     goto case_pair_gen;
84             case INSTR_GE:     pair = INSTR_LE;
85
86             case_pair_gen:
87                 if (stmt.o1.u1 < stmt.o2.u1) {
88                     uint16_t x  = stmt.o1.u1;
89                     stmt.o1.u1  = stmt.o2.u1;
90                     stmt.o2.u1  = x;
91                     stmt.opcode = pair;
92                 }
93                 break;
94         }
95     }
96
97     code->statements.push_back(stmt);
98     code->linenums.push_back(ctx.line);
99     code->columnnums.push_back(ctx.column);
100 }
101
102 void code_pop_statement(code_t *code)
103 {
104     code->statements.pop_back();
105     code->linenums.pop_back();
106     code->columnnums.pop_back();
107 }
108
109 void *code_t::operator new(std::size_t bytes) {
110   return mem_a(bytes);
111 }
112
113 void code_t::operator delete(void *ptr) {
114   mem_d(ptr);
115 }
116
117 code_t::code_t()
118 {
119     static lex_ctx_t                empty_ctx       = {0, 0, 0};
120     static prog_section_function_t  empty_function  = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
121     static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
122     static prog_section_def_t       empty_def       = {0, 0, 0};
123
124     string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
125
126     // The way progs.dat is suppose to work is odd, there needs to be
127     // some null (empty) statements, functions, and 28 globals
128     globals.insert(globals.begin(), 28, 0);
129
130     chars.push_back('\0');
131     functions.push_back(empty_function);
132
133     code_push_statement(this, &empty_statement, empty_ctx);
134
135     defs.push_back(empty_def);
136     fields.push_back(empty_def);
137 }
138
139 code_t::~code_t()
140 {
141     util_htdel(string_cache);
142 }
143
144 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
145
146 uint32_t code_genstring(code_t *code, const char *str) {
147     size_t            hash;
148     code_hash_entry_t existing;
149
150     if (!str)
151         return 0;
152
153     if (!*str) {
154         if (!code->string_cached_empty) {
155             code->string_cached_empty = code->chars.size();
156             code->chars.push_back(0);
157         }
158         return code->string_cached_empty;
159     }
160
161     if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
162         hash                      = ((unsigned char*)str)[strlen(str)-1];
163         CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash);
164     } else {
165         hash                      = util_hthash(code->string_cache, str);
166         CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash);
167     }
168
169     if (CODE_HASH_ENTER(existing))
170         return CODE_HASH_LEAVE(existing);
171
172     CODE_HASH_LEAVE(existing) = code->chars.size();
173     code->chars.insert(code->chars.end(), str, str + strlen(str) + 1);
174
175     util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing));
176     return CODE_HASH_LEAVE(existing);
177 }
178
179 qcint_t code_alloc_field (code_t *code, size_t qcsize)
180 {
181     qcint_t pos = (qcint_t)code->entfields;
182     code->entfields += qcsize;
183     return pos;
184 }
185
186 static size_t code_size_generic(code_t *code, prog_header_t *code_header, bool lno) {
187     size_t size = 0;
188     if (lno) {
189         size += 4;  /* LNOF */
190         size += sizeof(uint32_t); /* version */
191         size += sizeof(code_header->defs.length);
192         size += sizeof(code_header->globals.length);
193         size += sizeof(code_header->fields.length);
194         size += sizeof(code_header->statements.length);
195         size += sizeof(code->linenums[0])   * code->linenums.size();
196         size += sizeof(code->columnnums[0]) * code->columnnums.size();
197     } else {
198         size += sizeof(prog_header_t);
199         size += sizeof(prog_section_statement_t) * code->statements.size();
200         size += sizeof(prog_section_def_t)       * code->defs.size();
201         size += sizeof(prog_section_field_t)     * code->fields.size();
202         size += sizeof(prog_section_function_t)  * code->functions.size();
203         size += sizeof(int32_t)                  * code->globals.size();
204         size += 1                                * code->chars.size();
205     }
206     return size;
207 }
208
209 #define code_size_binary(C, H) code_size_generic((C), (H), false)
210 #define code_size_debug(C, H)  code_size_generic((C), (H), true)
211
212 static void code_create_header(code_t *code, prog_header_t *code_header, const char *filename, const char *lnofile) {
213     size_t i;
214
215     code_header->statements.offset = sizeof(prog_header_t);
216     code_header->statements.length = code->statements.size();
217     code_header->defs.offset       = code_header->statements.offset + (sizeof(prog_section_statement_t) * code->statements.size());
218     code_header->defs.length       = code->defs.size();
219     code_header->fields.offset     = code_header->defs.offset       + (sizeof(prog_section_def_t)       * code->defs.size());
220     code_header->fields.length     = code->fields.size();
221     code_header->functions.offset  = code_header->fields.offset     + (sizeof(prog_section_field_t)     * code->fields.size());
222     code_header->functions.length  = code->functions.size();
223     code_header->globals.offset    = code_header->functions.offset  + (sizeof(prog_section_function_t)  * code->functions.size());
224     code_header->globals.length    = code->globals.size();
225     code_header->strings.offset    = code_header->globals.offset    + (sizeof(int32_t)                  * code->globals.size());
226     code_header->strings.length    = code->chars.size();
227     code_header->version           = 6;
228     code_header->skip              = 0;
229
230     if (OPTS_OPTION_BOOL(OPTION_FORCECRC))
231         code_header->crc16         = OPTS_OPTION_U16(OPTION_FORCED_CRC);
232     else
233         code_header->crc16         = code->crc;
234     code_header->entfield          = code->entfields;
235
236     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
237         /* >= + P */
238         code->chars.push_back('\0'); /* > */
239         code->chars.push_back('\0'); /* = */
240         code->chars.push_back('\0'); /* P */
241     }
242
243     /* ensure all data is in LE format */
244     util_swap_header(*code_header);
245     util_swap_statements(code->statements);
246     util_swap_defs_fields(code->defs);
247     util_swap_defs_fields(code->fields);
248     util_swap_functions(code->functions);
249     util_swap_globals(code->globals);
250
251     if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
252         if (lnofile)
253             con_out("writing '%s' and '%s'...\n", filename, lnofile);
254         else
255             con_out("writing '%s'\n", filename);
256     }
257
258     if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
259         !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
260     {
261         char buffer[1024];
262         con_out("\nOptimizations:\n");
263         for (i = 0; i < COUNT_OPTIMIZATIONS; ++i) {
264             if (opts_optimizationcount[i]) {
265                 util_optimizationtostr(opts_opt_list[i].name, buffer, sizeof(buffer));
266                 con_out(
267                     "    %s: %u\n",
268                     buffer,
269                     (unsigned int)opts_optimizationcount[i]
270                 );
271             }
272         }
273     }
274 }
275
276 static void code_stats(const char *filename, const char *lnofile, code_t *code, prog_header_t *code_header) {
277     if (OPTS_OPTION_BOOL(OPTION_QUIET) ||
278         OPTS_OPTION_BOOL(OPTION_PP_ONLY))
279             return;
280
281     con_out("\nFile statistics:\n");
282     con_out("    dat:\n");
283     con_out("        name: %s\n",         filename);
284     con_out("        size: %u (bytes)\n", code_size_binary(code, code_header));
285     con_out("        crc:  0x%04X\n",     code->crc);
286
287     if (lnofile) {
288         con_out("    lno:\n");
289         con_out("        name: %s\n",  lnofile);
290         con_out("        size: %u (bytes)\n",  code_size_debug(code, code_header));
291     }
292
293     con_out("\n");
294 }
295
296 bool code_write(code_t *code, const char *filename, const char *lnofile) {
297     prog_header_t code_header;
298     FILE *fp = nullptr;
299
300     code_create_header(code, &code_header, filename, lnofile);
301
302     if (lnofile) {
303         uint32_t version = 1;
304
305         fp = fopen(lnofile, "wb");
306         if (!fp)
307             return false;
308
309         util_endianswap(&version,             1,                       sizeof(version));
310         util_endianswap(&code->linenums[0],   code->linenums.size(),   sizeof(code->linenums[0]));
311         util_endianswap(&code->columnnums[0], code->columnnums.size(), sizeof(code->columnnums[0]));
312
313         if (fwrite("LNOF",                          4,                                      1,                          fp) != 1 ||
314             fwrite(&version,                        sizeof(version),                        1,                          fp) != 1 ||
315             fwrite(&code_header.defs.length,        sizeof(code_header.defs.length),        1,                          fp) != 1 ||
316             fwrite(&code_header.globals.length,     sizeof(code_header.globals.length),     1,                          fp) != 1 ||
317             fwrite(&code_header.fields.length,      sizeof(code_header.fields.length),      1,                          fp) != 1 ||
318             fwrite(&code_header.statements.length,  sizeof(code_header.statements.length),  1,                          fp) != 1 ||
319             fwrite(&code->linenums[0],              sizeof(code->linenums[0]),              code->linenums.size(),      fp) != code->linenums.size() ||
320             fwrite(&code->columnnums[0],            sizeof(code->columnnums[0]),            code->columnnums.size(),    fp) != code->columnnums.size())
321         {
322             con_err("failed to write lno file\n");
323         }
324
325         fclose(fp);
326         fp = nullptr;
327     }
328
329     fp = fopen(filename, "wb");
330     if (!fp)
331         return false;
332
333     if (1                       != fwrite(&code_header,         sizeof(prog_header_t)           , 1                      , fp) ||
334         code->statements.size() != fwrite(&code->statements[0], sizeof(prog_section_statement_t), code->statements.size(), fp) ||
335         code->defs.size()       != fwrite(&code->defs[0],       sizeof(prog_section_def_t)      , code->defs.size()      , fp) ||
336         code->fields.size()     != fwrite(&code->fields[0],     sizeof(prog_section_field_t)    , code->fields.size()    , fp) ||
337         code->functions.size()  != fwrite(&code->functions[0],  sizeof(prog_section_function_t) , code->functions.size() , fp) ||
338         code->globals.size()    != fwrite(&code->globals[0],    sizeof(int32_t)                 , code->globals.size()   , fp) ||
339         code->chars.size()      != fwrite(&code->chars[0],      1                               , code->chars.size()     , fp))
340     {
341         fclose(fp);
342         return false;
343     }
344
345     fclose(fp);
346     code_stats(filename, lnofile, code, &code_header);
347     return true;
348 }