]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
Whitespace
[xonotic/gmqcc.git] / code.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler, Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24
25 prog_section_statement *code_statements;
26 prog_section_def       *code_defs;
27 prog_section_field     *code_fields;
28 prog_section_function  *code_functions;
29 int                    *code_globals;
30 char                   *code_chars;
31
32 uint16_t                            code_crc;
33 uint32_t                            code_entfields;
34
35 void code_init() {
36     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
37     prog_section_statement empty_statement = {0,{0},{0},{0}};
38     prog_section_def       empty_def       = {0, 0, 0};
39     int                    i               = 0;
40
41     code_entfields = 0;
42
43     /* omit creation of null code */
44     if (OPTS_FLAG(OMIT_NULL_BYTES))
45         return;
46
47     /*
48      * The way progs.dat is suppose to work is odd, there needs to be
49      * some null (empty) statements, functions, and 28 globals
50      */
51     for(; i < 28; i++)
52         vec_push(code_globals, 0);
53
54     vec_push(code_chars, '\0');
55     vec_push(code_functions,  empty_function);
56     vec_push(code_statements, empty_statement);
57     vec_push(code_defs,       empty_def);
58     vec_push(code_fields,     empty_def);
59 }
60
61 uint32_t code_genstring(const char *str)
62 {
63     uint32_t off = vec_size(code_chars);
64     while (*str) {
65         vec_push(code_chars, *str);
66         ++str;
67     }
68     vec_push(code_chars, 0);
69     return off;
70 }
71
72 uint32_t code_cachedstring(const char *str)
73 {
74     size_t s = 0;
75     /* We could implement knuth-morris-pratt or something
76      * and also take substrings, but I'm uncomfortable with
77      * pointing to subparts of strings for the sake of clarity...
78      */
79     while (s < vec_size(code_chars)) {
80         if (!strcmp(str, code_chars + s))
81             return s;
82         while (code_chars[s]) ++s;
83         ++s;
84     }
85     return code_genstring(str);
86 }
87
88 void code_test() {
89     prog_section_def       d1 = { TYPE_VOID,     28, 1 };
90     prog_section_def       d2 = { TYPE_FUNCTION, 29, 8 };
91     prog_section_def       d3 = { TYPE_STRING,   30, 14};
92     prog_section_function  f1 = { 1, 0, 0, 0, 1,            0,0, {0}};
93     prog_section_function  f2 = {-4, 0, 0, 0, 8,            0,0, {0}};
94     prog_section_function  f3 = { 0, 0, 0, 0, 14+13,        0,0, {0}};
95     prog_section_function  f4 = { 0, 0, 0, 0, 14+13+10,     0,0, {0}};
96     prog_section_function  f5 = { 0, 0, 0, 0, 14+13+10+7,   0,0, {0}};
97     prog_section_function  f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
98     prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
99     prog_section_statement s2 = { INSTR_CALL1,   {29}, {0},         {0}};
100     prog_section_statement s3 = { INSTR_RETURN,  {0},  {0},         {0}};
101
102     strcpy(vec_add(code_chars, 0x7), "m_init");
103     strcpy(vec_add(code_chars, 0x6), "print");
104     strcpy(vec_add(code_chars, 0xD), "hello world\n");
105     strcpy(vec_add(code_chars, 0xA), "m_keydown");
106     strcpy(vec_add(code_chars, 0x7), "m_draw");
107     strcpy(vec_add(code_chars, 0x9), "m_toggle");
108     strcpy(vec_add(code_chars, 0xB), "m_shutdown");
109
110     vec_push(code_globals, 1);  /* m_init */
111     vec_push(code_globals, 2);  /* print  */
112     vec_push(code_globals, 14); /* hello world in string table */
113
114     /* now the defs */
115     vec_push(code_defs,       d1); /* m_init    */
116     vec_push(code_defs,       d2); /* print     */
117     vec_push(code_defs,       d3); /*hello_world*/
118     vec_push(code_functions,  f1); /* m_init    */
119     vec_push(code_functions,  f2); /* print     */
120     vec_push(code_functions,  f3); /* m_keydown */
121     vec_push(code_functions,  f4);
122     vec_push(code_functions,  f5);
123     vec_push(code_functions,  f6);
124     vec_push(code_statements, s1);
125     vec_push(code_statements, s2);
126     vec_push(code_statements, s3);
127 }
128
129 qcint code_alloc_field (size_t qcsize)
130 {
131     qcint pos = (qcint)code_entfields;
132     code_entfields += qcsize;
133     return pos;
134 }
135
136 bool code_write(const char *filename) {
137     prog_header  code_header;
138     FILE        *fp           = NULL;
139     size_t       it           = 2;
140
141     /* see proposal.txt */
142     if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
143     code_header.statements.offset = sizeof(prog_header);
144     code_header.statements.length = vec_size(code_statements);
145     code_header.defs.offset       = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
146     code_header.defs.length       = vec_size(code_defs);
147     code_header.fields.offset     = code_header.defs.offset       + (sizeof(prog_section_def)       * vec_size(code_defs));
148     code_header.fields.length     = vec_size(code_fields);
149     code_header.functions.offset  = code_header.fields.offset     + (sizeof(prog_section_field)     * vec_size(code_fields));
150     code_header.functions.length  = vec_size(code_functions);
151     code_header.globals.offset    = code_header.functions.offset  + (sizeof(prog_section_function)  * vec_size(code_functions));
152     code_header.globals.length    = vec_size(code_globals);
153     code_header.strings.offset    = code_header.globals.offset    + (sizeof(int32_t)                * vec_size(code_globals));
154     code_header.strings.length    = vec_size(code_chars);
155     code_header.version           = 6;
156     if (opts_forcecrc)
157         code_header.crc16         = opts_forced_crc;
158     else
159         code_header.crc16         = code_crc;
160     code_header.entfield          = code_entfields;
161
162     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
163         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
164
165         /* >= + P */
166         vec_push(code_chars, '\0'); /* > */
167         vec_push(code_chars, '\0'); /* = */
168         vec_push(code_chars, '\0'); /* P */
169     }
170
171     /* ensure all data is in LE format */
172     util_endianswap(&code_header,    1,                         sizeof(prog_header));
173     util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
174     util_endianswap(code_defs,       vec_size(code_defs),       sizeof(prog_section_def));
175     util_endianswap(code_fields,     vec_size(code_fields),     sizeof(prog_section_field));
176     util_endianswap(code_functions,  vec_size(code_functions),  sizeof(prog_section_function));
177     util_endianswap(code_globals,    vec_size(code_globals),    sizeof(int32_t));
178
179     fp = util_fopen(filename, "wb");
180     if (!fp)
181         return false;
182
183     if (1                         != fwrite(&code_header,    sizeof(prog_header)           , 1                        , fp) ||
184         vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
185         vec_size(code_defs)       != fwrite(code_defs,       sizeof(prog_section_def)      , vec_size(code_defs)      , fp) ||
186         vec_size(code_fields)     != fwrite(code_fields,     sizeof(prog_section_field)    , vec_size(code_fields)    , fp) ||
187         vec_size(code_functions)  != fwrite(code_functions,  sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
188         vec_size(code_globals)    != fwrite(code_globals,    sizeof(int32_t)               , vec_size(code_globals)   , fp) ||
189         vec_size(code_chars)      != fwrite(code_chars,      1                             , vec_size(code_chars)     , fp))
190     {
191         fclose(fp);
192         return false;
193     }
194
195     util_debug("GEN","HEADER:\n");
196     util_debug("GEN","    version:    = %d\n", code_header.version );
197     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
198     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
199     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
200     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
201     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
202     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
203     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
204     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
205
206     /* FUNCTIONS */
207     util_debug("GEN", "FUNCTIONS:\n");
208     for (; it < vec_size(code_functions); it++) {
209         size_t j = code_functions[it].entry;
210         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",
211             code_functions[it].entry,
212             code_functions[it].firstlocal,
213             code_functions[it].locals,
214             code_functions[it].profile,
215             code_functions[it].name,
216             code_functions[it].file,
217             code_functions[it].nargs,
218             code_functions[it].argsize[0],
219             code_functions[it].argsize[1],
220             code_functions[it].argsize[2],
221             code_functions[it].argsize[3],
222             code_functions[it].argsize[4],
223             code_functions[it].argsize[5],
224             code_functions[it].argsize[6],
225             code_functions[it].argsize[7]
226
227         );
228         util_debug("GEN", "    NAME: %s\n", &code_chars[code_functions[it].name]);
229         /* Internal functions have no code */
230         if (code_functions[it].entry >= 0) {
231             util_debug("GEN", "    CODE:\n");
232             for (;;) {
233                 if (code_statements[j].opcode != AINSTR_END)
234                     util_debug("GEN", "        %-12s {% 5i,% 5i,% 5i}\n",
235                         asm_instr[code_statements[j].opcode].m,
236                         code_statements[j].o1.s1,
237                         code_statements[j].o2.s1,
238                         code_statements[j].o3.s1
239                     );
240                 else {
241                     util_debug("GEN", "        DONE  {0x00000,0x00000,0x00000}\n");
242                     break;
243                 }
244                 j++;
245             }
246         }
247     }
248
249     vec_free(code_statements);
250     vec_free(code_defs);
251     vec_free(code_fields);
252     vec_free(code_functions);
253     vec_free(code_globals);
254     vec_free(code_chars);
255     fclose(fp);
256     return true;
257 }