]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
code_alloc_field to allocate entityfield data, ir now has ir_builder_create_field...
[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 /*
26  * The macros below expand to a typesafe vector implementation, which
27  * can be viewed in gmqcc.h
28  *
29  * code_statements_data      -- raw prog_section_statement array
30  * code_statements_elements  -- number of elements
31  * code_statements_allocated -- size of the array allocated
32  * code_statements_add(T)    -- add element (returns -1 on error)
33  *
34  * code_vars_data            -- raw prog_section_var array
35  * code_vars_elements        -- number of elements
36  * code_vars_allocated       -- size of the array allocated
37  * code_vars_add(T)          -- add element (returns -1 on error)
38  *
39  * code_fields_data          -- raw prog_section_field array
40  * code_fields_elements      -- number of elements
41  * code_fields_allocated     -- size of the array allocated
42  * code_fields_add(T)        -- add element (returns -1 on error)
43  *
44  * code_functions_data       -- raw prog_section_function array
45  * code_functions_elements   -- number of elements
46  * code_functions_allocated  -- size of the array allocated
47  * code_functions_add(T)     -- add element (returns -1 on error)
48  *
49  * code_globals_data         -- raw prog_section_def array
50  * code_globals_elements     -- number of elements
51  * code_globals_allocated    -- size of the array allocated
52  * code_globals_add(T)       -- add element (returns -1 on error)
53  *
54  * code_chars_data           -- raw char* array
55  * code_chars_elements       -- number of elements
56  * code_chars_allocated      -- size of the array allocated
57  * code_chars_add(T)         -- add element (returns -1 on error)
58  */
59 VECTOR_MAKE(prog_section_statement, code_statements);
60 VECTOR_MAKE(prog_section_def,       code_defs      );
61 VECTOR_MAKE(prog_section_field,     code_fields    );
62 VECTOR_MAKE(prog_section_function,  code_functions );
63 VECTOR_MAKE(int,                    code_globals   );
64 VECTOR_MAKE(char,                   code_chars     );
65
66 uint32_t                            code_entfields;
67
68 void code_init() {
69     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
70     prog_section_statement empty_statement = {0,{0},{0},{0}};
71     int                    i               = 0;
72
73     /* omit creation of null code */
74     if (OPTS_FLAG(OMIT_NULL_BYTES))
75         return;
76
77     /*
78      * The way progs.dat is suppose to work is odd, there needs to be
79      * some null (empty) statements, functions, and 28 globals
80      */
81     for(; i < 28; i++)
82         code_globals_add(0);
83
84     code_chars_add     ('\0');
85     code_functions_add (empty_function);
86     code_statements_add(empty_statement);
87
88     code_entfields = 0;
89 }
90
91 uint32_t code_genstring(const char *str)
92 {
93     uint32_t off = code_chars_elements;
94     while (*str) {
95         code_chars_add(*str);
96         ++str;
97     }
98     code_chars_add(0);
99     return off;
100 }
101
102 uint32_t code_cachedstring(const char *str)
103 {
104     size_t s = 0;
105     /* We could implement knuth-morris-pratt or something
106      * and also take substrings, but I'm uncomfortable with
107      * pointing to subparts of strings for the sake of clarity...
108      */
109     while (s < code_chars_elements) {
110         if (!strcmp(str, code_chars_data + s))
111             return s;
112         while (code_chars_data[s]) ++s;
113         ++s;
114     }
115     return code_genstring(str);
116 }
117
118 void code_test() {
119     prog_section_def       d1 = { TYPE_VOID,     28, 1 };
120     prog_section_def       d2 = { TYPE_FUNCTION, 29, 8 };
121     prog_section_def       d3 = { TYPE_STRING,   30, 14};
122     prog_section_function  f1 = { 1, 0, 0, 0, 1,            0,0, {0}};
123     prog_section_function  f2 = {-4, 0, 0, 0, 8,            0,0, {0}};
124     prog_section_function  f3 = { 0, 0, 0, 0, 14+13,        0,0, {0}};
125     prog_section_function  f4 = { 0, 0, 0, 0, 14+13+10,     0,0, {0}};
126     prog_section_function  f5 = { 0, 0, 0, 0, 14+13+10+7,   0,0, {0}};
127     prog_section_function  f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
128     prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
129     prog_section_statement s2 = { INSTR_CALL1,   {29}, {0},         {0}};
130     prog_section_statement s3 = { INSTR_RETURN,  {0},  {0},         {0}};
131
132     code_chars_put("m_init",        0x6);
133     code_chars_put("print",         0x5);
134     code_chars_put("hello world\n", 0xC);
135     code_chars_put("m_keydown",     0x9);
136     code_chars_put("m_draw",        0x6);
137     code_chars_put("m_toggle",      0x8);
138     code_chars_put("m_shutdown",    0xA);
139
140     code_globals_add(1);  /* m_init */
141     code_globals_add(2);  /* print  */
142     code_globals_add(14); /* hello world in string table */
143
144     /* now the defs */
145     code_defs_add      (d1); /* m_init    */
146     code_defs_add      (d2); /* print     */
147     code_defs_add      (d3); /*hello_world*/
148     code_functions_add (f1); /* m_init    */
149     code_functions_add (f2); /* print     */
150     code_functions_add (f3); /* m_keydown */
151     code_functions_add (f4);
152     code_functions_add (f5);
153     code_functions_add (f6);
154     code_statements_add(s1);
155     code_statements_add(s2);
156     code_statements_add(s3);
157 }
158
159 qcint code_alloc_field (size_t qcsize)
160 {
161     qcint pos = (qcint)code_entfields;
162     code_entfields += qcsize;
163     return pos;
164 }
165
166 bool code_write(const char *filename) {
167     prog_header  code_header;
168     FILE        *fp           = NULL;
169     size_t       it           = 2;
170
171     /* see proposal.txt */
172     if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
173     code_header.statements.offset = sizeof(prog_header);
174     code_header.statements.length = code_statements_elements;
175     code_header.defs.offset       = code_header.statements.offset + (sizeof(prog_section_statement) * code_statements_elements);
176     code_header.defs.length       = code_defs_elements;
177     code_header.fields.offset     = code_header.defs.offset       + (sizeof(prog_section_def)       * code_defs_elements);
178     code_header.fields.length     = code_fields_elements;
179     code_header.functions.offset  = code_header.fields.offset     + (sizeof(prog_section_field)     * code_fields_elements);
180     code_header.functions.length  = code_functions_elements;
181     code_header.globals.offset    = code_header.functions.offset  + (sizeof(prog_section_function)  * code_functions_elements);
182     code_header.globals.length    = code_globals_elements;
183     code_header.strings.offset    = code_header.globals.offset    + (sizeof(int32_t)                * code_globals_elements);
184     code_header.strings.length    = code_chars_elements;
185     code_header.version           = 6;
186     code_header.crc16             = 0; /* TODO: */
187     code_header.entfield          = code_entfields;
188
189     if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
190         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
191
192         /* >= + P */
193         code_chars_add('\0'); /* > */
194         code_chars_add('\0'); /* = */
195         code_chars_add('\0'); /* P */
196     }
197
198     /* ensure all data is in LE format */
199     util_endianswap(&code_header,          1,                       sizeof(prog_header));
200     util_endianswap(code_statements_data, code_statements_elements, sizeof(prog_section_statement));
201     util_endianswap(code_defs_data,       code_defs_elements,       sizeof(prog_section_def));
202     util_endianswap(code_fields_data,     code_fields_elements,     sizeof(prog_section_field));
203     util_endianswap(code_functions_data,  code_functions_elements,  sizeof(prog_section_function));
204     util_endianswap(code_globals_data,    code_globals_elements,    sizeof(int32_t));
205
206     fp = fopen(filename, "wb");
207     if (!fp)
208         return false;
209
210     if (1 != fwrite(&code_header,         sizeof(prog_header), 1, fp) ||
211         code_statements_elements != fwrite(code_statements_data, sizeof(prog_section_statement), code_statements_elements, fp) ||
212         code_defs_elements       != fwrite(code_defs_data,       sizeof(prog_section_def)      , code_defs_elements      , fp) ||
213         code_fields_elements     != fwrite(code_fields_data,     sizeof(prog_section_field)    , code_fields_elements    , fp) ||
214         code_functions_elements  != fwrite(code_functions_data,  sizeof(prog_section_function) , code_functions_elements , fp) ||
215         code_globals_elements    != fwrite(code_globals_data,    sizeof(int32_t)               , code_globals_elements   , fp) ||
216         code_chars_elements      != fwrite(code_chars_data,      1                             , code_chars_elements     , fp))
217     {
218         fclose(fp);
219         return false;
220     }
221
222     util_debug("GEN","HEADER:\n");
223     util_debug("GEN","    version:    = %d\n", code_header.version );
224     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
225     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
226     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
227     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
228     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
229     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
230     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
231     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
232
233     /* FUNCTIONS */
234     util_debug("GEN", "FUNCTIONS:\n");
235     for (; it < code_functions_elements; it++) {
236         size_t j = code_functions_data[it].entry;
237         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",
238             code_functions_data[it].entry,
239             code_functions_data[it].firstlocal,
240             code_functions_data[it].locals,
241             code_functions_data[it].profile,
242             code_functions_data[it].name,
243             code_functions_data[it].file,
244             code_functions_data[it].nargs,
245             code_functions_data[it].argsize[0],
246             code_functions_data[it].argsize[1],
247             code_functions_data[it].argsize[2],
248             code_functions_data[it].argsize[3],
249             code_functions_data[it].argsize[4],
250             code_functions_data[it].argsize[5],
251             code_functions_data[it].argsize[6],
252             code_functions_data[it].argsize[7]
253
254         );
255         util_debug("GEN", "    NAME: %s\n", &code_chars_data[code_functions_data[it].name]);
256         /* Internal functions have no code */
257         if (code_functions_data[it].entry >= 0) {
258             util_debug("GEN", "    CODE:\n");
259             for (;;) {
260                 if (code_statements_data[j].opcode != AINSTR_END)
261                     util_debug("GEN", "        %s {0x%05x,0x%05x,0x%05x}\n",
262                         asm_instr[code_statements_data[j].opcode].m,
263                         code_statements_data[j].o1.s1,
264                         code_statements_data[j].o2.s1,
265                         code_statements_data[j].o3.s1
266                     );
267                 else {
268                     util_debug("GEN", "        DONE  {0x00000,0x00000,0x00000}\n");
269                     break;
270                 }
271                 j++;
272             }
273         }
274     }
275
276     mem_d(code_statements_data);
277     mem_d(code_defs_data);
278     mem_d(code_fields_data);
279     mem_d(code_functions_data);
280     mem_d(code_globals_data);
281     mem_d(code_chars_data);
282     fclose(fp);
283     return true;
284 }