]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
Merge branch 'master' 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 "gmqcc.h"
25
26 prog_section_statement *code_statements;
27 int                    *code_linenums;
28 prog_section_def       *code_defs;
29 prog_section_field     *code_fields;
30 prog_section_function  *code_functions;
31 int                    *code_globals;
32 char                   *code_chars;
33 uint16_t                code_crc;
34 uint32_t                code_entfields;
35
36 /* This is outrageous! */
37 #define QCINT_ENTRY void*
38 #define QCINT_TO_HASH_ENTRY(q) ((void*)(uintptr_t)(q))
39 #define HASH_ENTRY_TO_QCINT(h) ((qcint)(uintptr_t)(h))
40 static ht     code_string_cache;
41 static qcint  code_string_cached_empty;
42
43 void code_push_statement(prog_section_statement *stmt, int linenum)
44 {
45     vec_push(code_statements, *stmt);
46     vec_push(code_linenums,   linenum);
47 }
48
49 void code_pop_statement()
50 {
51     vec_pop(code_statements);
52     vec_pop(code_linenums);
53 }
54
55 void code_init() {
56     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
57     prog_section_statement empty_statement = {0,{0},{0},{0}};
58     prog_section_def       empty_def       = {0, 0, 0};
59     int                    i               = 0;
60
61     code_entfields = 0;
62
63     code_string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024);
64
65     /*
66      * The way progs.dat is suppose to work is odd, there needs to be
67      * some null (empty) statements, functions, and 28 globals
68      */
69     for(; i < 28; i++)
70         vec_push(code_globals, 0);
71
72     vec_push(code_chars, '\0');
73     vec_push(code_functions,  empty_function);
74     code_push_statement(&empty_statement, 0);
75     vec_push(code_defs,       empty_def);
76     vec_push(code_fields,     empty_def);
77 }
78
79 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
80 uint32_t code_genstring(const char *str)
81 {
82     uint32_t off;
83     size_t   hash;
84     QCINT_ENTRY existing;
85
86     if (!str)
87         return 0;
88
89     if (!*str) {
90         if (!code_string_cached_empty) {
91             code_string_cached_empty = vec_size(code_chars);
92             vec_push(code_chars, 0);
93         }
94         return code_string_cached_empty;
95     }
96
97     if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
98         hash = ((unsigned char*)str)[strlen(str)-1];
99         existing = code_util_str_htgeth(code_string_cache, str, hash);
100     } else {
101         hash = util_hthash(code_string_cache, str);
102         existing = util_htgeth(code_string_cache, str, hash);
103     }
104
105     if (existing)
106         return HASH_ENTRY_TO_QCINT(existing);
107
108     off = vec_size(code_chars);
109     vec_upload(code_chars, str, strlen(str)+1);
110
111     util_htseth(code_string_cache, str, hash, QCINT_TO_HASH_ENTRY(off));
112     return off;
113 }
114
115 qcint code_alloc_field (size_t qcsize)
116 {
117     qcint pos = (qcint)code_entfields;
118     code_entfields += qcsize;
119     return pos;
120 }
121
122 bool code_write(const char *filename, const char *lnofile) {
123     prog_header  code_header;
124     FILE        *fp           = NULL;
125     size_t       it           = 2;
126
127     code_header.statements.offset = sizeof(prog_header);
128     code_header.statements.length = vec_size(code_statements);
129     code_header.defs.offset       = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
130     code_header.defs.length       = vec_size(code_defs);
131     code_header.fields.offset     = code_header.defs.offset       + (sizeof(prog_section_def)       * vec_size(code_defs));
132     code_header.fields.length     = vec_size(code_fields);
133     code_header.functions.offset  = code_header.fields.offset     + (sizeof(prog_section_field)     * vec_size(code_fields));
134     code_header.functions.length  = vec_size(code_functions);
135     code_header.globals.offset    = code_header.functions.offset  + (sizeof(prog_section_function)  * vec_size(code_functions));
136     code_header.globals.length    = vec_size(code_globals);
137     code_header.strings.offset    = code_header.globals.offset    + (sizeof(int32_t)                * vec_size(code_globals));
138     code_header.strings.length    = vec_size(code_chars);
139     code_header.version           = 6;
140     if (OPTS_OPTION_BOOL(OPTION_FORCECRC))
141         code_header.crc16         = OPTS_OPTION_U16(OPTION_FORCED_CRC);
142     else
143         code_header.crc16         = code_crc;
144     code_header.entfield          = code_entfields;
145
146     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
147         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
148
149         /* >= + P */
150         vec_push(code_chars, '\0'); /* > */
151         vec_push(code_chars, '\0'); /* = */
152         vec_push(code_chars, '\0'); /* P */
153     }
154
155     /* ensure all data is in LE format */
156     util_endianswap(&code_header.version,    1, sizeof(code_header.version));
157     util_endianswap(&code_header.crc16,      1, sizeof(code_header.crc16));
158     util_endianswap(&code_header.statements, 2, sizeof(code_header.statements.offset));
159     util_endianswap(&code_header.defs,       2, sizeof(code_header.statements.offset));
160     util_endianswap(&code_header.fields,     2, sizeof(code_header.statements.offset));
161     util_endianswap(&code_header.functions,  2, sizeof(code_header.statements.offset));
162     util_endianswap(&code_header.strings,    2, sizeof(code_header.statements.offset));
163     util_endianswap(&code_header.globals,    2, sizeof(code_header.statements.offset));
164     util_endianswap(&code_header.entfield,   1, sizeof(code_header.entfield));
165     util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
166     util_endianswap(code_defs,       vec_size(code_defs),       sizeof(prog_section_def));
167     util_endianswap(code_fields,     vec_size(code_fields),     sizeof(prog_section_field));
168     util_endianswap(code_functions,  vec_size(code_functions),  sizeof(prog_section_function));
169     util_endianswap(code_globals,    vec_size(code_globals),    sizeof(int32_t));
170
171     if (lnofile) {
172         uint32_t version = 1;
173
174         fp = file_open(lnofile, "wb");
175         if (!fp)
176             return false;
177
178         util_endianswap(&version,      1,                       sizeof(version));
179         util_endianswap(code_linenums, vec_size(code_linenums), sizeof(code_linenums[0]));
180
181
182         if (file_write("LNOF",                          4,                                      1,                       fp) != 1 ||
183             file_write(&version,                        sizeof(version),                        1,                       fp) != 1 ||
184             file_write(&code_header.defs.length,        sizeof(code_header.defs.length),        1,                       fp) != 1 ||
185             file_write(&code_header.globals.length,     sizeof(code_header.globals.length),     1,                       fp) != 1 ||
186             file_write(&code_header.fields.length,      sizeof(code_header.fields.length),      1,                       fp) != 1 ||
187             file_write(&code_header.statements.length,  sizeof(code_header.statements.length),  1,                       fp) != 1 ||
188             file_write(code_linenums,                   sizeof(code_linenums[0]),               vec_size(code_linenums), fp) != vec_size(code_linenums))
189         {
190             con_err("failed to write lno file\n");
191         }
192
193         file_close(fp);
194         fp = NULL;
195     }
196
197     fp = file_open(filename, "wb");
198     if (!fp)
199         return false;
200
201     if (1                         != file_write(&code_header,    sizeof(prog_header)           , 1                        , fp) ||
202         vec_size(code_statements) != file_write(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
203         vec_size(code_defs)       != file_write(code_defs,       sizeof(prog_section_def)      , vec_size(code_defs)      , fp) ||
204         vec_size(code_fields)     != file_write(code_fields,     sizeof(prog_section_field)    , vec_size(code_fields)    , fp) ||
205         vec_size(code_functions)  != file_write(code_functions,  sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
206         vec_size(code_globals)    != file_write(code_globals,    sizeof(int32_t)               , vec_size(code_globals)   , fp) ||
207         vec_size(code_chars)      != file_write(code_chars,      1                             , vec_size(code_chars)     , fp))
208     {
209         file_close(fp);
210         return false;
211     }
212
213     util_debug("GEN","HEADER:\n");
214     util_debug("GEN","    version:    = %d\n", code_header.version );
215     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
216     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
217     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
218     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
219     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
220     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
221     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
222     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
223
224     /* FUNCTIONS */
225     util_debug("GEN", "FUNCTIONS:\n");
226     for (; it < vec_size(code_functions); it++) {
227         size_t j = code_functions[it].entry;
228         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",
229             code_functions[it].entry,
230             code_functions[it].firstlocal,
231             code_functions[it].locals,
232             code_functions[it].profile,
233             code_functions[it].name,
234             code_functions[it].file,
235             code_functions[it].nargs,
236             code_functions[it].argsize[0],
237             code_functions[it].argsize[1],
238             code_functions[it].argsize[2],
239             code_functions[it].argsize[3],
240             code_functions[it].argsize[4],
241             code_functions[it].argsize[5],
242             code_functions[it].argsize[6],
243             code_functions[it].argsize[7]
244
245         );
246         util_debug("GEN", "    NAME: %s\n", &code_chars[code_functions[it].name]);
247         /* Internal functions have no code */
248         if (code_functions[it].entry >= 0) {
249             util_debug("GEN", "    CODE:\n");
250             for (;;) {
251                 if (code_statements[j].opcode != INSTR_DONE)
252                     util_debug("GEN", "        %-12s {% 5i,% 5i,% 5i}\n",
253                         asm_instr[code_statements[j].opcode].m,
254                         code_statements[j].o1.s1,
255                         code_statements[j].o2.s1,
256                         code_statements[j].o3.s1
257                     );
258                 else {
259                     util_debug("GEN", "        DONE  {0x00000,0x00000,0x00000}\n");
260                     break;
261                 }
262                 j++;
263             }
264         }
265     }
266
267     vec_free(code_statements);
268     vec_free(code_linenums);
269     vec_free(code_defs);
270     vec_free(code_fields);
271     vec_free(code_functions);
272     vec_free(code_globals);
273     vec_free(code_chars);
274     util_htdel(code_string_cache);
275
276     file_close(fp);
277     return true;
278 }