]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
All ir_instrs now get their lex-context, so that we can create an LNO file with ...
[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
34 uint16_t                code_crc;
35 uint32_t                code_entfields;
36
37 void code_push_statement(prog_section_statement *stmt, int linenum)
38 {
39     vec_push(code_statements, *stmt);
40     vec_push(code_linenums,   linenum);
41 }
42
43 void code_init() {
44     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
45     prog_section_statement empty_statement = {0,{0},{0},{0}};
46     prog_section_def       empty_def       = {0, 0, 0};
47     int                    i               = 0;
48
49     code_entfields = 0;
50
51     /* omit creation of null code */
52     if (OPTS_FLAG(OMIT_NULL_BYTES))
53         return;
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     code_push_statement(&empty_statement, 0);
65     vec_push(code_defs,       empty_def);
66     vec_push(code_fields,     empty_def);
67 }
68
69 uint32_t code_genstring(const char *str)
70 {
71     uint32_t off = vec_size(code_chars);
72     while (*str) {
73         vec_push(code_chars, *str);
74         ++str;
75     }
76     vec_push(code_chars, 0);
77     return off;
78 }
79
80 uint32_t code_cachedstring(const char *str)
81 {
82     size_t s = 0;
83     /* We could implement knuth-morris-pratt or something
84      * and also take substrings, but I'm uncomfortable with
85      * pointing to subparts of strings for the sake of clarity...
86      */
87     while (s < vec_size(code_chars)) {
88         if (!strcmp(str, code_chars + s))
89             return s;
90         while (code_chars[s]) ++s;
91         ++s;
92     }
93     return code_genstring(str);
94 }
95
96 void code_test() {
97     prog_section_def       d1 = { TYPE_VOID,     28, 1 };
98     prog_section_def       d2 = { TYPE_FUNCTION, 29, 8 };
99     prog_section_def       d3 = { TYPE_STRING,   30, 14};
100     prog_section_function  f1 = { 1, 0, 0, 0, 1,            0,0, {0}};
101     prog_section_function  f2 = {-4, 0, 0, 0, 8,            0,0, {0}};
102     prog_section_function  f3 = { 0, 0, 0, 0, 14+13,        0,0, {0}};
103     prog_section_function  f4 = { 0, 0, 0, 0, 14+13+10,     0,0, {0}};
104     prog_section_function  f5 = { 0, 0, 0, 0, 14+13+10+7,   0,0, {0}};
105     prog_section_function  f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
106     prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
107     prog_section_statement s2 = { INSTR_CALL1,   {29}, {0},         {0}};
108     prog_section_statement s3 = { INSTR_RETURN,  {0},  {0},         {0}};
109
110     strcpy(vec_add(code_chars, 0x7), "m_init");
111     strcpy(vec_add(code_chars, 0x6), "print");
112     strcpy(vec_add(code_chars, 0xD), "hello world\n");
113     strcpy(vec_add(code_chars, 0xA), "m_keydown");
114     strcpy(vec_add(code_chars, 0x7), "m_draw");
115     strcpy(vec_add(code_chars, 0x9), "m_toggle");
116     strcpy(vec_add(code_chars, 0xB), "m_shutdown");
117
118     vec_push(code_globals, 1);  /* m_init */
119     vec_push(code_globals, 2);  /* print  */
120     vec_push(code_globals, 14); /* hello world in string table */
121
122     /* now the defs */
123     vec_push(code_defs,       d1); /* m_init    */
124     vec_push(code_defs,       d2); /* print     */
125     vec_push(code_defs,       d3); /*hello_world*/
126     vec_push(code_functions,  f1); /* m_init    */
127     vec_push(code_functions,  f2); /* print     */
128     vec_push(code_functions,  f3); /* m_keydown */
129     vec_push(code_functions,  f4);
130     vec_push(code_functions,  f5);
131     vec_push(code_functions,  f6);
132     vec_push(code_statements, s1);
133     vec_push(code_statements, s2);
134     vec_push(code_statements, s3);
135 }
136
137 qcint code_alloc_field (size_t qcsize)
138 {
139     qcint pos = (qcint)code_entfields;
140     code_entfields += qcsize;
141     return pos;
142 }
143
144 bool code_write(const char *filename, const char *lnofile) {
145     prog_header  code_header;
146     FILE        *fp           = NULL;
147     size_t       it           = 2;
148
149     /* see proposal.txt */
150     if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
151     code_header.statements.offset = sizeof(prog_header);
152     code_header.statements.length = vec_size(code_statements);
153     code_header.defs.offset       = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
154     code_header.defs.length       = vec_size(code_defs);
155     code_header.fields.offset     = code_header.defs.offset       + (sizeof(prog_section_def)       * vec_size(code_defs));
156     code_header.fields.length     = vec_size(code_fields);
157     code_header.functions.offset  = code_header.fields.offset     + (sizeof(prog_section_field)     * vec_size(code_fields));
158     code_header.functions.length  = vec_size(code_functions);
159     code_header.globals.offset    = code_header.functions.offset  + (sizeof(prog_section_function)  * vec_size(code_functions));
160     code_header.globals.length    = vec_size(code_globals);
161     code_header.strings.offset    = code_header.globals.offset    + (sizeof(int32_t)                * vec_size(code_globals));
162     code_header.strings.length    = vec_size(code_chars);
163     code_header.version           = 6;
164     if (opts_forcecrc)
165         code_header.crc16         = opts_forced_crc;
166     else
167         code_header.crc16         = code_crc;
168     code_header.entfield          = code_entfields;
169
170     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
171         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
172
173         /* >= + P */
174         vec_push(code_chars, '\0'); /* > */
175         vec_push(code_chars, '\0'); /* = */
176         vec_push(code_chars, '\0'); /* P */
177     }
178
179     if (lnofile) {
180         uint32_t lnotype = *(unsigned int*)"LNOF";
181         uint32_t version = 1;
182
183         fp = util_fopen(lnofile, "wb");
184         if (!fp)
185             return false;
186
187         if (fwrite(&lnotype, sizeof(lnotype), 1, fp) != 1 ||
188             fwrite(&version, sizeof(version), 1, fp) != 1 ||
189             fwrite(&code_header.defs.length,        sizeof(code_header.defs.length),        1, fp) != 1 ||
190             fwrite(&code_header.globals.length,     sizeof(code_header.globals.length),     1, fp) != 1 ||
191             fwrite(&code_header.fields.length,      sizeof(code_header.fields.length),      1, fp) != 1 ||
192             fwrite(&code_header.statements.length,  sizeof(code_header.statements.length),  1, fp) != 1 ||
193             fwrite(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
194         {
195             con_err("failed to write lno file\n");
196         }
197             /*
198                         h = SafeOpenWrite (destfile, 2*1024*1024);
199                         SafeWrite (h, &lnotype, sizeof(int));
200                         SafeWrite (h, &version, sizeof(int));
201                         SafeWrite (h, &numglobaldefs, sizeof(int));
202                         SafeWrite (h, &numpr_globals, sizeof(int));
203                         SafeWrite (h, &numfielddefs, sizeof(int));
204                         SafeWrite (h, &numstatements, sizeof(int));
205                         SafeWrite (h, statement_linenums, numstatements*sizeof(int));
206                         SafeClose (h);
207                         */
208
209         fclose(fp);
210         fp = NULL;
211     }
212
213     /* ensure all data is in LE format */
214     util_endianswap(&code_header,    1,                         sizeof(prog_header));
215     util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
216     util_endianswap(code_defs,       vec_size(code_defs),       sizeof(prog_section_def));
217     util_endianswap(code_fields,     vec_size(code_fields),     sizeof(prog_section_field));
218     util_endianswap(code_functions,  vec_size(code_functions),  sizeof(prog_section_function));
219     util_endianswap(code_globals,    vec_size(code_globals),    sizeof(int32_t));
220
221     fp = util_fopen(filename, "wb");
222     if (!fp)
223         return false;
224
225     if (1                         != fwrite(&code_header,    sizeof(prog_header)           , 1                        , fp) ||
226         vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
227         vec_size(code_defs)       != fwrite(code_defs,       sizeof(prog_section_def)      , vec_size(code_defs)      , fp) ||
228         vec_size(code_fields)     != fwrite(code_fields,     sizeof(prog_section_field)    , vec_size(code_fields)    , fp) ||
229         vec_size(code_functions)  != fwrite(code_functions,  sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
230         vec_size(code_globals)    != fwrite(code_globals,    sizeof(int32_t)               , vec_size(code_globals)   , fp) ||
231         vec_size(code_chars)      != fwrite(code_chars,      1                             , vec_size(code_chars)     , fp))
232     {
233         fclose(fp);
234         return false;
235     }
236
237     util_debug("GEN","HEADER:\n");
238     util_debug("GEN","    version:    = %d\n", code_header.version );
239     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
240     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
241     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
242     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
243     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
244     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
245     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
246     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
247
248     /* FUNCTIONS */
249     util_debug("GEN", "FUNCTIONS:\n");
250     for (; it < vec_size(code_functions); it++) {
251         size_t j = code_functions[it].entry;
252         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",
253             code_functions[it].entry,
254             code_functions[it].firstlocal,
255             code_functions[it].locals,
256             code_functions[it].profile,
257             code_functions[it].name,
258             code_functions[it].file,
259             code_functions[it].nargs,
260             code_functions[it].argsize[0],
261             code_functions[it].argsize[1],
262             code_functions[it].argsize[2],
263             code_functions[it].argsize[3],
264             code_functions[it].argsize[4],
265             code_functions[it].argsize[5],
266             code_functions[it].argsize[6],
267             code_functions[it].argsize[7]
268
269         );
270         util_debug("GEN", "    NAME: %s\n", &code_chars[code_functions[it].name]);
271         /* Internal functions have no code */
272         if (code_functions[it].entry >= 0) {
273             util_debug("GEN", "    CODE:\n");
274             for (;;) {
275                 if (code_statements[j].opcode != AINSTR_END)
276                     util_debug("GEN", "        %-12s {% 5i,% 5i,% 5i}\n",
277                         asm_instr[code_statements[j].opcode].m,
278                         code_statements[j].o1.s1,
279                         code_statements[j].o2.s1,
280                         code_statements[j].o3.s1
281                     );
282                 else {
283                     util_debug("GEN", "        DONE  {0x00000,0x00000,0x00000}\n");
284                     break;
285                 }
286                 j++;
287             }
288         }
289     }
290
291     vec_free(code_statements);
292     vec_free(code_defs);
293     vec_free(code_fields);
294     vec_free(code_functions);
295     vec_free(code_globals);
296     vec_free(code_chars);
297     fclose(fp);
298     return true;
299 }