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