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