]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
tabulators->four spaces
[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     uint32_t     version;      /* Program version (6)     */
32     uint32_t     crc16;        /* What is this?           */
33     prog_section statements;   /* prog_section_statement  */
34     prog_section defs;         /* prog_section_def        */
35     prog_section fields;       /* prog_section_field      */
36     prog_section functions;    /* prog_section_function   */
37     prog_section strings;      /* What is this?           */
38     prog_section globals;      /* What is this?           */
39     uint32_t     entfield;     /* Number of entity fields */
40 } prog_header;
41
42 /*
43  * The macros below expand to a typesafe vector implementation, which
44  * can be viewed in gmqcc.h
45  * 
46  * code_statements_data      -- raw prog_section_statement array
47  * code_statements_elements  -- number of elements
48  * code_statements_allocated -- size of the array allocated
49  * code_statements_add(T)    -- add element (returns -1 on error)
50  * 
51  * code_vars_data            -- raw prog_section_var array
52  * code_vars_elements        -- number of elements
53  * code_vars_allocated       -- size of the array allocated
54  * code_vars_add(T)          -- add element (returns -1 on error)
55  * 
56  * code_fields_data          -- raw prog_section_field array
57  * code_fields_elements      -- number of elements
58  * code_fields_allocated     -- size of the array allocated
59  * code_fields_add(T)        -- add element (returns -1 on error)
60  *
61  * code_functions_data       -- raw prog_section_function array
62  * code_functions_elements   -- number of elements
63  * code_functions_allocated  -- size of the array allocated
64  * code_functions_add(T)     -- add element (returns -1 on error)
65  * 
66  * code_globals_data         -- raw prog_section_def array
67  * code_globals_elements     -- number of elements
68  * code_globals_allocated    -- size of the array allocated
69  * code_globals_add(T)       -- add element (returns -1 on error)
70  * 
71  * code_strings_data         -- raw char* array
72  * code_strings_elements     -- number of elements
73  * code_strings_allocated    -- size of the array allocated
74  * code_strings_add(T)       -- add element (returns -1 on error)
75  */
76 VECTOR_MAKE(prog_section_statement, code_statements);
77 VECTOR_MAKE(prog_section_def,       code_defs      );
78 VECTOR_MAKE(prog_section_field,     code_fields    );
79 VECTOR_MAKE(prog_section_function,  code_functions );
80 VECTOR_MAKE(int,                    code_globals   );
81 VECTOR_MAKE(char,                   code_strings   );
82
83 void code_init() {
84     /*
85      * The way progs.dat is suppose to work is odd, there needs to be
86      * some null (empty) statements, functions, and 28 globals
87      */
88     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
89     prog_section_statement empty_statement = {0,{0},{0},{0}};
90     int i;
91     for(i = 0; i < 28; i++)
92         code_globals_add(0);
93         
94     code_strings_add   ('\0');
95     code_functions_add (empty_function);
96     code_statements_add(empty_statement);
97 }
98
99 void code_test() {
100     const char *X;
101     size_t size = sizeof(X);
102     size_t iter = 0;
103     
104     #define FOO(Y) \
105         X = Y; \
106         size = sizeof(Y); \
107         for (iter=0; iter < size; iter++) { \
108             code_strings_add(X[iter]);    \
109         }
110         
111     FOO("m_init");
112     FOO("print");
113     FOO("hello world\n");
114     FOO("m_keydown");
115     FOO("m_draw");
116     FOO("m_toggle");
117     FOO("m_shutdown");
118     
119     code_globals_add(1);  /* m_init */
120     code_globals_add(2);  /* print  */
121     code_globals_add(14); /* hello world in string table */
122     
123     /* now the defs */
124     code_defs_add((prog_section_def){.type=TYPE_VOID,    .offset=28/*globals[28]*/, .name=1 }); /* m_init */
125     code_defs_add((prog_section_def){.type=TYPE_FUNCTION,.offset=29/*globals[29]*/, .name=8 }); /* print  */
126     code_defs_add((prog_section_def){.type=TYPE_STRING,  .offset=30/*globals[30]*/, .name=14}); /*hello_world*/
127     
128     code_functions_add((prog_section_function){1,  0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
129     code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
130     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13,        0,0, {0}}); /* m_keydown */
131     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10,     0,0, {0}});
132     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7,   0,0, {0}});
133     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7+9, 0,0, {0}});
134     
135     code_statements_add((prog_section_statement){INSTR_STORE_F, {30}/*30 is hello_world */, {OFS_PARM0}, {0}});
136     code_statements_add((prog_section_statement){INSTR_CALL1,   {29}/*29 is print       */, {0},         {0}});
137     code_statements_add((prog_section_statement){INSTR_RETURN,  {0},                        {0},         {0}});
138 }
139
140 void code_write() {
141     prog_header code_header={0};
142     code_header.version    = 6;
143     code_header.crc16      = 0; /* TODO: */
144     code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
145     code_header.defs       = (prog_section){code_header.statements.offset + sizeof(prog_section_statement)*code_statements_elements,  code_defs_elements       };
146     code_header.fields     = (prog_section){code_header.defs.offset       + sizeof(prog_section_def)      *code_defs_elements,        code_fields_elements     };
147     code_header.functions  = (prog_section){code_header.fields.offset     + sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements  };
148     code_header.globals    = (prog_section){code_header.functions.offset  + sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
149     code_header.strings    = (prog_section){code_header.globals.offset    + sizeof(int)                   *code_globals_elements,     code_strings_elements    };
150     code_header.entfield   = 0; /* TODO: */
151     
152     FILE *fp = fopen("program.dat", "wb");
153     fwrite(&code_header,         1, sizeof(prog_header), fp);
154     fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
155     fwrite(code_defs_data,       1, sizeof(prog_section_def)      *code_defs_elements,       fp);
156     fwrite(code_fields_data,     1, sizeof(prog_section_field)    *code_fields_elements,     fp);
157     fwrite(code_functions_data,  1, sizeof(prog_section_function) *code_functions_elements,  fp);
158     fwrite(code_globals_data,    1, sizeof(int)                   *code_globals_elements,    fp);
159     fwrite(code_strings_data,    1, 1                             *code_strings_elements,    fp);
160     
161     mem_d(code_statements_data);
162     mem_d(code_defs_data);
163     mem_d(code_fields_data);
164     mem_d(code_functions_data);
165     mem_d(code_globals_data);
166     mem_d(code_strings_data);
167     
168     util_debug("GEN","wrote program.dat:\n\
169     version:    = %d\n\
170     crc16:      = %d\n\
171     entfield:   = %d\n\
172     statements {.offset = % 8d, .length = % 8d}\n\
173     defs       {.offset = % 8d, .length = % 8d}\n\
174     fields     {.offset = % 8d, .length = % 8d}\n\
175     functions  {.offset = % 8d, .length = % 8d}\n\
176     globals    {.offset = % 8d, .length = % 8d}\n\
177     strings    {.offset = % 8d, .length = % 8d}\n",
178         code_header.version,
179         code_header.crc16,
180         code_header.entfield,
181         code_header.statements.offset,
182         code_header.statements.length,
183         code_header.defs.offset,
184         code_header.defs.length,
185         code_header.fields.offset,
186         code_header.fields.length,
187         code_header.functions.offset,
188         code_header.functions.length,
189         code_header.strings.offset,
190         code_header.strings.length,
191         code_header.globals.offset,
192         code_header.globals.length
193     );
194     
195     fclose(fp);
196 }