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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
26 uint32_t offset; /* Offset in file of where data begins */
27 uint32_t length; /* Length of section (how many of) */
31 uint32_t version; /* Program version (6) */
32 uint16_t crc16; /* What is this? */
33 uint16_t skip; /* see propsal.txt */
35 prog_section statements; /* prog_section_statement */
36 prog_section defs; /* prog_section_def */
37 prog_section fields; /* prog_section_field */
38 prog_section functions; /* prog_section_function */
39 prog_section strings; /* What is this? */
40 prog_section globals; /* What is this? */
41 uint32_t entfield; /* Number of entity fields */
45 * The macros below expand to a typesafe vector implementation, which
46 * can be viewed in gmqcc.h
48 * code_statements_data -- raw prog_section_statement array
49 * code_statements_elements -- number of elements
50 * code_statements_allocated -- size of the array allocated
51 * code_statements_add(T) -- add element (returns -1 on error)
53 * code_vars_data -- raw prog_section_var array
54 * code_vars_elements -- number of elements
55 * code_vars_allocated -- size of the array allocated
56 * code_vars_add(T) -- add element (returns -1 on error)
58 * code_fields_data -- raw prog_section_field array
59 * code_fields_elements -- number of elements
60 * code_fields_allocated -- size of the array allocated
61 * code_fields_add(T) -- add element (returns -1 on error)
63 * code_functions_data -- raw prog_section_function array
64 * code_functions_elements -- number of elements
65 * code_functions_allocated -- size of the array allocated
66 * code_functions_add(T) -- add element (returns -1 on error)
68 * code_globals_data -- raw prog_section_def array
69 * code_globals_elements -- number of elements
70 * code_globals_allocated -- size of the array allocated
71 * code_globals_add(T) -- add element (returns -1 on error)
73 * code_chars_data -- raw char* array
74 * code_chars_elements -- number of elements
75 * code_chars_allocated -- size of the array allocated
76 * code_chars_add(T) -- add element (returns -1 on error)
78 VECTOR_MAKE(prog_section_statement, code_statements);
79 VECTOR_MAKE(prog_section_def, code_defs );
80 VECTOR_MAKE(prog_section_field, code_fields );
81 VECTOR_MAKE(prog_section_function, code_functions );
82 VECTOR_MAKE(int, code_globals );
83 VECTOR_MAKE(char, code_chars );
86 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
87 prog_section_statement empty_statement = {0,{0},{0},{0}};
90 /* omit creation of null code */
91 if (opts_omit_nullcode)
95 * The way progs.dat is suppose to work is odd, there needs to be
96 * some null (empty) statements, functions, and 28 globals
101 code_chars_add ('\0');
102 code_functions_add (empty_function);
103 code_statements_add(empty_statement);
107 prog_section_def d1 = { TYPE_VOID, 28, 1 };
108 prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
109 prog_section_def d3 = { TYPE_STRING, 30, 14};
110 prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
111 prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
112 prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
113 prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
114 prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
115 prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
116 prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
117 prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
118 prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
120 code_chars_put("m_init", 0x6);
121 code_chars_put("print", 0x5);
122 code_chars_put("hello world\n", 0xC);
123 code_chars_put("m_keydown", 0x9);
124 code_chars_put("m_draw", 0x6);
125 code_chars_put("m_toggle", 0x8);
126 code_chars_put("m_shutdown", 0xA);
128 code_globals_add(1); /* m_init */
129 code_globals_add(2); /* print */
130 code_globals_add(14); /* hello world in string table */
133 code_defs_add (d1); /* m_init */
134 code_defs_add (d2); /* print */
135 code_defs_add (d3); /*hello_world*/
136 code_functions_add (f1); /* m_init */
137 code_functions_add (f2); /* print */
138 code_functions_add (f3); /* m_keydown */
139 code_functions_add (f4);
140 code_functions_add (f5);
141 code_functions_add (f6);
142 code_statements_add(s1);
143 code_statements_add(s2);
144 code_statements_add(s3);
148 prog_header code_header;
152 /* see proposal.txt */
153 if (opts_omit_nullcode) {}
154 code_header.statements.offset = sizeof(prog_header);
155 code_header.statements.length = code_statements_elements;
156 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * code_statements_elements);
157 code_header.defs.length = code_defs_elements;
158 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * code_defs_elements);
159 code_header.fields.length = code_fields_elements;
160 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * code_fields_elements);
161 code_header.functions.length = code_functions_elements;
162 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * code_functions_elements);
163 code_header.globals.length = code_globals_elements;
164 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * code_globals_elements);
165 code_header.strings.length = code_chars_elements;
166 code_header.version = 6;
167 code_header.crc16 = 0; /* TODO: */
168 code_header.entfield = 0; /* TODO: */
170 if (opts_darkplaces_stringtablebug) {
171 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
174 code_chars_add('\0'); /* > */
175 code_chars_add('\0'); /* = */
176 code_chars_add('\0'); /* P */
179 /* ensure all data is in LE format */
180 util_endianswap(&code_header, 1, sizeof(prog_header));
181 util_endianswap(code_statements_data, code_statements_elements, sizeof(prog_section_statement));
182 util_endianswap(code_defs_data, code_defs_elements, sizeof(prog_section_def));
183 util_endianswap(code_fields_data, code_fields_elements, sizeof(prog_section_field));
184 util_endianswap(code_functions_data, code_functions_elements, sizeof(prog_section_function));
185 util_endianswap(code_globals_data, code_globals_elements, sizeof(int32_t));
187 fp = fopen("program.dat", "wb");
188 fwrite(&code_header, 1, sizeof(prog_header), fp);
189 fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
190 fwrite(code_defs_data, 1, sizeof(prog_section_def) *code_defs_elements, fp);
191 fwrite(code_fields_data, 1, sizeof(prog_section_field) *code_fields_elements, fp);
192 fwrite(code_functions_data, 1, sizeof(prog_section_function) *code_functions_elements, fp);
193 fwrite(code_globals_data, 1, sizeof(int32_t) *code_globals_elements, fp);
194 fwrite(code_chars_data, 1, 1 *code_chars_elements, fp);
196 util_debug("GEN","HEADER:\n");
197 util_debug("GEN"," version: = %d\n", code_header.version );
198 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
199 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
200 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
201 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
202 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
203 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
204 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
205 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
208 util_debug("GEN", "FUNCTIONS:\n");
209 for (; it < code_functions_elements; it++) {
210 size_t j = code_functions_data[it].entry;
211 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",
212 code_functions_data[it].entry,
213 code_functions_data[it].firstlocal,
214 code_functions_data[it].locals,
215 code_functions_data[it].profile,
216 code_functions_data[it].name,
217 code_functions_data[it].file,
218 code_functions_data[it].nargs,
219 code_functions_data[it].argsize[0],
220 code_functions_data[it].argsize[1],
221 code_functions_data[it].argsize[2],
222 code_functions_data[it].argsize[3],
223 code_functions_data[it].argsize[4],
224 code_functions_data[it].argsize[5],
225 code_functions_data[it].argsize[6],
226 code_functions_data[it].argsize[7]
229 util_debug("GEN", " NAME: %s\n", &code_chars_data[code_functions_data[it].name]);
230 /* Internal functions have no code */
231 if (code_functions_data[it].entry >= 0) {
232 util_debug("GEN", " CODE:\n");
234 if (code_statements_data[j].opcode != INSTR_DONE &&
235 code_statements_data[j].opcode != INSTR_RETURN)
236 util_debug("GEN", " %s {0x%05d,0x%05d,0x%05d}\n",
237 asm_instr[code_statements_data[j].opcode].m,
238 code_statements_data[j].o1.s1,
239 code_statements_data[j].o2.s1,
240 code_statements_data[j].o3.s1
248 mem_d(code_statements_data);
249 mem_d(code_defs_data);
250 mem_d(code_fields_data);
251 mem_d(code_functions_data);
252 mem_d(code_globals_data);
253 mem_d(code_chars_data);