]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
More parsing stuff (still totally broken)
[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 <stdint.h>
24 #include <stdlib.h>
25 #include "gmqcc.h"
26
27 typedef struct {
28         uint16_t opcode;
29         
30         /* operand 1 */
31         union {
32                 int16_t  s1; /* signed   */
33                 uint16_t u1; /* unsigned */
34         };
35         /* operand 2 */
36         union {
37                 int16_t  s2; /* signed   */
38                 uint16_t u2; /* unsigned */
39         };
40         /* operand 3 */
41         union {
42                 int16_t  s3; /* signed   */
43                 uint16_t u3; /* unsigned */
44         };
45         
46         /*
47          * This is the same as the structure in darkplaces
48          * {
49          *     unsigned short op;
50          *     short          a,b,c;
51          * }
52          * But this one is more sane to work with, and the
53          * type sizes are guranteed.
54          */
55 } prog_section_statement;
56
57 typedef struct {
58         /* The type is (I assume)
59          * 0 = ev_void
60          * 1 = ev_string
61          * 2 = ev_float
62          * 3 = ev_vector
63          * 4 = ev_entity
64          * 5 = ev_field
65          * 6 = ev_function
66          * 7 = ev_pointer
67          * 8 = ev_bad    (is this right for uint16_t type?)
68          */
69         uint16_t type;
70         uint16_t offset;     /* offset in file? (what about length)   */
71         uint32_t name;       /* offset in string table? (confused :() */
72 } prog_section_both;
73
74 /*
75  * var and field use the same structure.  But lets not use the same
76  * name just for safety reasons?  (still castable ...).
77  */
78 typedef prog_section_both prog_section_def;
79 typedef prog_section_both prog_section_field;
80
81 typedef struct {
82         int32_t   entry;      /* in statement table for instructions  */
83         uint32_t  firstlocal; /* First local in local table           */
84         uint32_t  locals;     /* Total ints of params + locals        */
85         uint32_t  profile;    /* Always zero (engine uses this)       */
86         uint32_t  name;       /* name of function in string table     */
87         uint32_t  file;       /* file of the source file              */
88         uint32_t  nargs;      /* number of arguments                  */
89         uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
90 } prog_section_function;
91
92 typedef struct {
93         uint32_t offset;      /* Offset in file of where data begins  */
94         uint32_t length;      /* Length of section (how many of)      */
95 } prog_section;
96
97 typedef struct {
98         uint32_t     version;      /* Program version (6)     */
99         uint32_t     crc16;        /* What is this?           */
100         prog_section statements;   /* prog_section_statement  */
101         prog_section defs;         /* prog_section_def        */
102         prog_section fields;       /* prog_section_field      */
103         prog_section functions;    /* prog_section_function   */
104         prog_section strings;      /* What is this?           */
105         prog_section globals;      /* What is this?           */
106         uint32_t     entfield;     /* Number of entity fields */
107 } prog_header;
108
109 /*
110  * The macros below expand to a typesafe vector implementation, which
111  * can be viewed in gmqcc.h
112  * 
113  * code_statements_data      -- raw prog_section_statement array
114  * code_statements_elements  -- number of elements
115  * code_statements_allocated -- size of the array allocated
116  * code_statements_add(T)    -- add element (returns -1 on error)
117  * 
118  * code_vars_data            -- raw prog_section_var array
119  * code_vars_elements        -- number of elements
120  * code_vars_allocated       -- size of the array allocated
121  * code_vars_add(T)          -- add element (returns -1 on error)
122  * 
123  * code_fields_data          -- raw prog_section_field array
124  * code_fields_elements      -- number of elements
125  * code_fields_allocated     -- size of the array allocated
126  * code_fields_add(T)        -- add element (returns -1 on error)
127  *
128  * code_functions_data       -- raw prog_section_function array
129  * code_functions_elements   -- number of elements
130  * code_functions_allocated  -- size of the array allocated
131  * code_functions_add(T)     -- add element (returns -1 on error)
132  * 
133  * code_globals_data         -- raw prog_section_def array
134  * code_globals_elements     -- number of elements
135  * code_globals_allocated    -- size of the array allocated
136  * code_globals_add(T)       -- add element (returns -1 on error)
137  * 
138  * code_strings_data         -- raw char* array
139  * code_strings_elements     -- number of elements
140  * code_strings_allocated    -- size of the array allocated
141  * code_strings_add(T)       -- add element (returns -1 on error)
142  */
143 VECTOR_MAKE(prog_section_statement, code_statements);
144 VECTOR_MAKE(prog_section_def,       code_defs      );
145 VECTOR_MAKE(prog_section_field,     code_fields    );
146 VECTOR_MAKE(prog_section_function,  code_functions );
147 VECTOR_MAKE(int,                    code_globals   );
148 VECTOR_MAKE(char,                   code_strings   );
149 static uint16_t code_crc16  = 0;
150 prog_header     code_header ={0};
151
152 void code_init() {
153         /*
154          * The way progs.dat is suppose to work is odd, there needs to be
155          * some null (empty) statements, functions, and 28 globals
156          */
157         prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
158         prog_section_statement empty_statement = {0,{0},{0},{0}};
159         int i;
160         for(i = 0; i < 28; i++)
161                 code_globals_add(0);
162                 
163         code_strings_add   ('\0');
164         code_functions_add (empty_function);
165         code_statements_add(empty_statement);
166 }
167
168 void code_test() {
169         const char *X;
170         size_t size = sizeof(X);
171         size_t iter = 0;
172         
173         #define FOO(Y) \
174                 X = Y; \
175                 size = sizeof(Y); \
176                 for (iter=0; iter < size; iter++) { \
177                         code_strings_add(X[iter]);    \
178                 }
179                 
180         FOO("m_init");
181         FOO("print");
182         FOO("hello world\n");
183         FOO("m_keydown");
184         FOO("m_draw");
185         FOO("m_toggle");
186         FOO("m_shutdown");
187         
188         code_globals_add(1);  /* m_init */
189         code_globals_add(2);  /* print  */
190         code_globals_add(14); /* hello world in string table */
191         
192         /* now the defs */
193         code_defs_add((prog_section_def){.type=TYPE_VOID,    .offset=28/*globals[28]*/, .name=1 }); /* m_init */
194         code_defs_add((prog_section_def){.type=TYPE_FUNCTION,.offset=29/*globals[29]*/, .name=8 }); /* print  */
195         code_defs_add((prog_section_def){.type=TYPE_STRING,  .offset=30/*globals[30]*/, .name=14}); /*hello_world*/
196         
197         code_functions_add((prog_section_function){1,  0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
198         code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
199         code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13,        0,0, {0}}); /* m_keydown */
200         code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10,     0,0, {0}});
201         code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7,   0,0, {0}});
202         code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7+9, 0,0, {0}});
203         
204         code_statements_add((prog_section_statement){INSTR_STORE_F, {30}/*30 is hello_world */, {OFS_PARM0}, {0}});
205         code_statements_add((prog_section_statement){INSTR_CALL1,   {29}/*29 is print       */, {0},         {0}});
206         code_statements_add((prog_section_statement){INSTR_RETURN,  {0},                        {0},         {0}});
207         
208         code_header.version    = 6;
209         code_header.crc16      = 0; /* TODO: */
210         code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
211         code_header.defs       = (prog_section){code_header.statements.offset + sizeof(prog_section_statement)*code_statements_elements,  code_defs_elements       };
212         code_header.fields     = (prog_section){code_header.defs.offset       + sizeof(prog_section_def)      *code_defs_elements,        code_fields_elements     };
213         code_header.functions  = (prog_section){code_header.fields.offset     + sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements  };
214         code_header.globals    = (prog_section){code_header.functions.offset  + sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
215         code_header.strings    = (prog_section){code_header.globals.offset    + sizeof(int)                   *code_globals_elements,     code_strings_elements    };
216         code_header.entfield   = 0; /* TODO: */
217 }
218
219 /* program header */
220 void code_write() {
221         code_init();
222         code_test();
223         
224         FILE *fp = fopen("program.dat", "wb");
225         fwrite(&code_header,         1, sizeof(prog_header), fp);
226         fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
227         fwrite(code_defs_data,       1, sizeof(prog_section_def)      *code_defs_elements,       fp);
228         fwrite(code_fields_data,     1, sizeof(prog_section_field)    *code_fields_elements,     fp);
229         fwrite(code_functions_data,  1, sizeof(prog_section_function) *code_functions_elements,  fp);
230         fwrite(code_globals_data,    1, sizeof(int)                   *code_globals_elements,    fp);
231         fwrite(code_strings_data,    1, 1                             *code_strings_elements,    fp);
232         
233         free(code_statements_data);
234         free(code_defs_data);
235         free(code_fields_data);
236         free(code_functions_data);
237         free(code_globals_data);
238         free(code_strings_data);
239         
240         fclose(fp);
241 }