]> git.xonotic.org Git - xonotic/gmqcc.git/blob - code.c
AUTHORS
[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 int code_strings_add(const char *src) {
87     size_t size = strlen(src);
88     size_t iter = 0;
89     while (iter < size)
90         code_chars_add(src[iter++]);
91     code_chars_add('\0');
92     return code_chars_elements;
93 }
94
95 void code_init() {
96     /* omit creation of null code */
97     if (opts_omit_nullcode)
98         return;
99         
100     /*
101      * The way progs.dat is suppose to work is odd, there needs to be
102      * some null (empty) statements, functions, and 28 globals
103      */
104     prog_section_function  empty_function  = {0,0,0,0,0,0,0,{0}};
105     prog_section_statement empty_statement = {0,{0},{0},{0}};
106     int i;
107     for(i = 0; i < 28; i++)
108         code_globals_add(0);
109         
110     code_chars_add     ('\0');
111     code_functions_add (empty_function);
112     code_statements_add(empty_statement);
113 }
114
115 void code_test() {    
116     code_strings_add("m_init");
117     code_strings_add("print");
118     code_strings_add("hello world\n");
119     code_strings_add("m_keydown");
120     code_strings_add("m_draw");
121     code_strings_add("m_toggle");
122     code_strings_add("m_shutdown");
123     
124     code_globals_add(1);  /* m_init */
125     code_globals_add(2);  /* print  */
126     code_globals_add(14); /* hello world in string table */
127     
128     /* now the defs */
129     code_defs_add((prog_section_def){.type=TYPE_VOID,    .offset=28/*globals[28]*/, .name=1 }); /* m_init */
130     code_defs_add((prog_section_def){.type=TYPE_FUNCTION,.offset=29/*globals[29]*/, .name=8 }); /* print  */
131     code_defs_add((prog_section_def){.type=TYPE_STRING,  .offset=30/*globals[30]*/, .name=14}); /*hello_world*/
132     
133     code_functions_add((prog_section_function){1,  0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
134     code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
135     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13,        0,0, {0}}); /* m_keydown */
136     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10,     0,0, {0}});
137     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7,   0,0, {0}});
138     code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7+9, 0,0, {0}});
139     
140     code_statements_add((prog_section_statement){INSTR_STORE_F, {30}/*30 is hello_world */, {OFS_PARM0}, {0}});
141     code_statements_add((prog_section_statement){INSTR_CALL1,   {29}/*29 is print       */, {0},         {0}});
142     code_statements_add((prog_section_statement){INSTR_RETURN,  {0},                        {0},         {0}});
143 }
144
145 void code_write() {
146     prog_header code_header={0};
147
148     /* see proposal.txt */
149     if (opts_omit_nullcode) {
150         code_header.skip   = 28;
151         code_header.flags  = 1;
152     }
153     
154     code_header.version    = 6;
155     code_header.crc16      = 0; /* TODO: */
156     code_header.statements = (prog_section){sizeof(prog_header), code_statements_elements };
157     code_header.defs       = (prog_section){code_header.statements.offset + sizeof(prog_section_statement)*code_statements_elements,  code_defs_elements      };
158     code_header.fields     = (prog_section){code_header.defs.offset       + sizeof(prog_section_def)      *code_defs_elements,        code_fields_elements    };
159     code_header.functions  = (prog_section){code_header.fields.offset     + sizeof(prog_section_field)    *code_fields_elements,      code_functions_elements };
160     code_header.globals    = (prog_section){code_header.functions.offset  + sizeof(prog_section_function) *code_functions_elements,   code_globals_elements   };
161     code_header.strings    = (prog_section){code_header.globals.offset    + sizeof(int)                   *code_globals_elements,     code_chars_elements     };
162     code_header.entfield   = 0; /* TODO: */
163
164     if (opts_darkplaces_stringtablebug) {
165         util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
166
167         /* >= + P */
168         code_chars_add('\0'); /* > */
169         code_chars_add('\0'); /* = */
170         code_chars_add('\0'); /* P */
171     }
172         
173     /* ensure all data is in LE format */
174     util_endianswap(&code_header,         1,                        sizeof(prog_header));
175     util_endianswap(code_statements_data, code_statements_elements, sizeof(prog_section_statement));
176     util_endianswap(code_defs_data,       code_defs_elements,       sizeof(prog_section_def));
177     util_endianswap(code_fields_data,     code_fields_elements,     sizeof(prog_section_field));
178     util_endianswap(code_functions_data,  code_functions_elements,  sizeof(prog_section_function));
179     util_endianswap(code_globals_data,    code_globals_elements,    sizeof(int));
180     
181     FILE *fp = fopen("program.dat", "wb");
182     fwrite(&code_header,         1, sizeof(prog_header), fp);
183     fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
184     fwrite(code_defs_data,       1, sizeof(prog_section_def)      *code_defs_elements,       fp);
185     fwrite(code_fields_data,     1, sizeof(prog_section_field)    *code_fields_elements,     fp);
186     fwrite(code_functions_data,  1, sizeof(prog_section_function) *code_functions_elements,  fp);
187     fwrite(code_globals_data,    1, sizeof(int)                   *code_globals_elements,    fp);
188     fwrite(code_chars_data,      1, 1                             *code_chars_elements,      fp);
189     
190     util_debug("GEN","HEADER:\n");
191     util_debug("GEN","    version:    = %d\n", code_header.version );
192     util_debug("GEN","    crc16:      = %d\n", code_header.crc16   );
193     util_debug("GEN","    entfield:   = %d\n", code_header.entfield);
194     util_debug("GEN","    statements  = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
195     util_debug("GEN","    defs        = {.offset = % 8d, .length = % 8d}\n", code_header.defs      .offset, code_header.defs      .length);
196     util_debug("GEN","    fields      = {.offset = % 8d, .length = % 8d}\n", code_header.fields    .offset, code_header.fields    .length);
197     util_debug("GEN","    functions   = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
198     util_debug("GEN","    globals     = {.offset = % 8d, .length = % 8d}\n", code_header.globals   .offset, code_header.globals   .length);
199     util_debug("GEN","    strings     = {.offset = % 8d, .length = % 8d}\n", code_header.strings   .offset, code_header.strings   .length);
200     
201     /* FUNCTIONS */
202     util_debug("GEN", "FUNCTIONS:\n");
203     size_t i = 1;
204     for (; i < code_functions_elements; i++) {
205         size_t j = code_functions_data[i].entry;
206         util_debug("GEN", "    {.entry =% 5d, .firstlocal =% 5d, .locals =% 5d, .profile =% 5d, .name =% 5d, .file =% 5d, .nargs =% 5d, .argsize =%0X }\n",
207             code_functions_data[i].entry,
208             code_functions_data[i].firstlocal,
209             code_functions_data[i].locals,
210             code_functions_data[i].profile,
211             code_functions_data[i].name,
212             code_functions_data[i].file,
213             code_functions_data[i].nargs,
214             *((int32_t*)&code_functions_data[i].argsize)
215         );
216         util_debug("GEN", "    NAME: %s\n", &code_chars_data[code_functions_data[i].name]);
217         util_debug("GEN", "    CODE:\n");
218         for (;;) {
219             if (code_statements_data[j].opcode != INSTR_DONE &&
220                 code_statements_data[j].opcode != INSTR_RETURN)
221                 util_debug("GEN", "        %s {0x%05d,0x%05d,0x%05d}\n",
222                     asm_instr[code_statements_data[j].opcode].m,
223                     code_statements_data[j].s1,
224                     code_statements_data[j].s2,
225                     code_statements_data[j].s3
226                 );
227             else break;
228             j++;
229         }
230     }
231     
232     mem_d(code_statements_data);
233     mem_d(code_defs_data);
234     mem_d(code_fields_data);
235     mem_d(code_functions_data);
236     mem_d(code_globals_data);
237     mem_d(code_chars_data);
238     fclose(fp);
239 }