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