6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 prog_section_statement *code_statements;
28 prog_section_def *code_defs;
29 prog_section_field *code_fields;
30 prog_section_function *code_functions;
35 uint32_t code_entfields;
37 void code_push_statement(prog_section_statement *stmt, int linenum)
39 vec_push(code_statements, *stmt);
40 vec_push(code_linenums, linenum);
44 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
45 prog_section_statement empty_statement = {0,{0},{0},{0}};
46 prog_section_def empty_def = {0, 0, 0};
52 * The way progs.dat is suppose to work is odd, there needs to be
53 * some null (empty) statements, functions, and 28 globals
56 vec_push(code_globals, 0);
58 vec_push(code_chars, '\0');
59 vec_push(code_functions, empty_function);
60 code_push_statement(&empty_statement, 0);
61 vec_push(code_defs, empty_def);
62 vec_push(code_fields, empty_def);
65 uint32_t code_genstring(const char *str)
67 uint32_t off = vec_size(code_chars);
69 vec_push(code_chars, *str);
72 vec_push(code_chars, 0);
76 uint32_t code_cachedstring(const char *str)
79 /* We could implement knuth-morris-pratt or something
80 * and also take substrings, but I'm uncomfortable with
81 * pointing to subparts of strings for the sake of clarity...
83 while (s < vec_size(code_chars)) {
84 if (!strcmp(str, code_chars + s))
86 while (code_chars[s]) ++s;
89 return code_genstring(str);
93 prog_section_def d1 = { TYPE_VOID, 28, 1 };
94 prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
95 prog_section_def d3 = { TYPE_STRING, 30, 14};
96 prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
97 prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
98 prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
99 prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
100 prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
101 prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
102 prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
103 prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
104 prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
106 strcpy(vec_add(code_chars, 0x7), "m_init");
107 strcpy(vec_add(code_chars, 0x6), "print");
108 strcpy(vec_add(code_chars, 0xD), "hello world\n");
109 strcpy(vec_add(code_chars, 0xA), "m_keydown");
110 strcpy(vec_add(code_chars, 0x7), "m_draw");
111 strcpy(vec_add(code_chars, 0x9), "m_toggle");
112 strcpy(vec_add(code_chars, 0xB), "m_shutdown");
114 vec_push(code_globals, 1); /* m_init */
115 vec_push(code_globals, 2); /* print */
116 vec_push(code_globals, 14); /* hello world in string table */
119 vec_push(code_defs, d1); /* m_init */
120 vec_push(code_defs, d2); /* print */
121 vec_push(code_defs, d3); /*hello_world*/
122 vec_push(code_functions, f1); /* m_init */
123 vec_push(code_functions, f2); /* print */
124 vec_push(code_functions, f3); /* m_keydown */
125 vec_push(code_functions, f4);
126 vec_push(code_functions, f5);
127 vec_push(code_functions, f6);
128 vec_push(code_statements, s1);
129 vec_push(code_statements, s2);
130 vec_push(code_statements, s3);
133 qcint code_alloc_field (size_t qcsize)
135 qcint pos = (qcint)code_entfields;
136 code_entfields += qcsize;
140 bool code_write(const char *filename, const char *lnofile) {
141 prog_header code_header;
145 code_header.statements.offset = sizeof(prog_header);
146 code_header.statements.length = vec_size(code_statements);
147 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
148 code_header.defs.length = vec_size(code_defs);
149 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
150 code_header.fields.length = vec_size(code_fields);
151 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
152 code_header.functions.length = vec_size(code_functions);
153 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
154 code_header.globals.length = vec_size(code_globals);
155 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
156 code_header.strings.length = vec_size(code_chars);
157 code_header.version = 6;
159 code_header.crc16 = opts_forced_crc;
161 code_header.crc16 = code_crc;
162 code_header.entfield = code_entfields;
164 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
165 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
168 vec_push(code_chars, '\0'); /* > */
169 vec_push(code_chars, '\0'); /* = */
170 vec_push(code_chars, '\0'); /* P */
174 uint32_t lnotype = *(unsigned int*)"LNOF";
175 uint32_t version = 1;
177 fp = util_fopen(lnofile, "wb");
181 if (fwrite(&lnotype, sizeof(lnotype), 1, fp) != 1 ||
182 fwrite(&version, sizeof(version), 1, fp) != 1 ||
183 fwrite(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
184 fwrite(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
185 fwrite(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
186 fwrite(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
187 fwrite(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
189 con_err("failed to write lno file\n");
192 h = SafeOpenWrite (destfile, 2*1024*1024);
193 SafeWrite (h, &lnotype, sizeof(int));
194 SafeWrite (h, &version, sizeof(int));
195 SafeWrite (h, &numglobaldefs, sizeof(int));
196 SafeWrite (h, &numpr_globals, sizeof(int));
197 SafeWrite (h, &numfielddefs, sizeof(int));
198 SafeWrite (h, &numstatements, sizeof(int));
199 SafeWrite (h, statement_linenums, numstatements*sizeof(int));
207 /* ensure all data is in LE format */
208 util_endianswap(&code_header, 1, sizeof(prog_header));
209 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
210 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
211 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
212 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
213 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
215 fp = util_fopen(filename, "wb");
219 if (1 != fwrite(&code_header, sizeof(prog_header) , 1 , fp) ||
220 vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
221 vec_size(code_defs) != fwrite(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
222 vec_size(code_fields) != fwrite(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
223 vec_size(code_functions) != fwrite(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
224 vec_size(code_globals) != fwrite(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
225 vec_size(code_chars) != fwrite(code_chars, 1 , vec_size(code_chars) , fp))
231 util_debug("GEN","HEADER:\n");
232 util_debug("GEN"," version: = %d\n", code_header.version );
233 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
234 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
235 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
236 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
237 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
238 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
239 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
240 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
243 util_debug("GEN", "FUNCTIONS:\n");
244 for (; it < vec_size(code_functions); it++) {
245 size_t j = code_functions[it].entry;
246 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",
247 code_functions[it].entry,
248 code_functions[it].firstlocal,
249 code_functions[it].locals,
250 code_functions[it].profile,
251 code_functions[it].name,
252 code_functions[it].file,
253 code_functions[it].nargs,
254 code_functions[it].argsize[0],
255 code_functions[it].argsize[1],
256 code_functions[it].argsize[2],
257 code_functions[it].argsize[3],
258 code_functions[it].argsize[4],
259 code_functions[it].argsize[5],
260 code_functions[it].argsize[6],
261 code_functions[it].argsize[7]
264 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
265 /* Internal functions have no code */
266 if (code_functions[it].entry >= 0) {
267 util_debug("GEN", " CODE:\n");
269 if (code_statements[j].opcode != AINSTR_END)
270 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
271 asm_instr[code_statements[j].opcode].m,
272 code_statements[j].o1.s1,
273 code_statements[j].o2.s1,
274 code_statements[j].o3.s1
277 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
285 vec_free(code_statements);
287 vec_free(code_fields);
288 vec_free(code_functions);
289 vec_free(code_globals);
290 vec_free(code_chars);