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;
34 uint32_t code_entfields;
36 void code_push_statement(prog_section_statement *stmt, int linenum)
38 vec_push(code_statements, *stmt);
39 vec_push(code_linenums, linenum);
43 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
44 prog_section_statement empty_statement = {0,{0},{0},{0}};
45 prog_section_def empty_def = {0, 0, 0};
51 * The way progs.dat is suppose to work is odd, there needs to be
52 * some null (empty) statements, functions, and 28 globals
55 vec_push(code_globals, 0);
57 vec_push(code_chars, '\0');
58 vec_push(code_functions, empty_function);
59 code_push_statement(&empty_statement, 0);
60 vec_push(code_defs, empty_def);
61 vec_push(code_fields, empty_def);
64 uint32_t code_genstring(const char *str)
66 uint32_t off = vec_size(code_chars);
68 vec_push(code_chars, *str);
71 vec_push(code_chars, 0);
75 uint32_t code_cachedstring(const char *str)
78 /* We could implement knuth-morris-pratt or something
79 * and also take substrings, but I'm uncomfortable with
80 * pointing to subparts of strings for the sake of clarity...
82 while (s < vec_size(code_chars)) {
83 if (!strcmp(str, code_chars + s))
85 while (code_chars[s]) ++s;
88 return code_genstring(str);
91 qcint code_alloc_field (size_t qcsize)
93 qcint pos = (qcint)code_entfields;
94 code_entfields += qcsize;
98 bool code_write(const char *filename, const char *lnofile) {
99 prog_header code_header;
103 code_header.statements.offset = sizeof(prog_header);
104 code_header.statements.length = vec_size(code_statements);
105 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
106 code_header.defs.length = vec_size(code_defs);
107 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
108 code_header.fields.length = vec_size(code_fields);
109 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
110 code_header.functions.length = vec_size(code_functions);
111 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
112 code_header.globals.length = vec_size(code_globals);
113 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
114 code_header.strings.length = vec_size(code_chars);
115 code_header.version = 6;
117 code_header.crc16 = opts.forced_crc;
119 code_header.crc16 = code_crc;
120 code_header.entfield = code_entfields;
122 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
123 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
126 vec_push(code_chars, '\0'); /* > */
127 vec_push(code_chars, '\0'); /* = */
128 vec_push(code_chars, '\0'); /* P */
132 uint32_t lnotype = *(unsigned int*)"LNOF";
133 uint32_t version = 1;
135 fp = util_fopen(lnofile, "wb");
139 if (fwrite(&lnotype, sizeof(lnotype), 1, fp) != 1 ||
140 fwrite(&version, sizeof(version), 1, fp) != 1 ||
141 fwrite(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
142 fwrite(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
143 fwrite(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
144 fwrite(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
145 fwrite(code_linenums, sizeof(code_linenums[0]), vec_size(code_linenums), fp) != vec_size(code_linenums))
147 con_err("failed to write lno file\n");
154 /* ensure all data is in LE format */
155 util_endianswap(&code_header, 1, sizeof(prog_header));
156 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
157 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
158 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
159 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
160 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
162 fp = util_fopen(filename, "wb");
166 if (1 != fwrite(&code_header, sizeof(prog_header) , 1 , fp) ||
167 vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
168 vec_size(code_defs) != fwrite(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
169 vec_size(code_fields) != fwrite(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
170 vec_size(code_functions) != fwrite(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
171 vec_size(code_globals) != fwrite(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
172 vec_size(code_chars) != fwrite(code_chars, 1 , vec_size(code_chars) , fp))
178 util_debug("GEN","HEADER:\n");
179 util_debug("GEN"," version: = %d\n", code_header.version );
180 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
181 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
182 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
183 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
184 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
185 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
186 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
187 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
190 util_debug("GEN", "FUNCTIONS:\n");
191 for (; it < vec_size(code_functions); it++) {
192 size_t j = code_functions[it].entry;
193 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",
194 code_functions[it].entry,
195 code_functions[it].firstlocal,
196 code_functions[it].locals,
197 code_functions[it].profile,
198 code_functions[it].name,
199 code_functions[it].file,
200 code_functions[it].nargs,
201 code_functions[it].argsize[0],
202 code_functions[it].argsize[1],
203 code_functions[it].argsize[2],
204 code_functions[it].argsize[3],
205 code_functions[it].argsize[4],
206 code_functions[it].argsize[5],
207 code_functions[it].argsize[6],
208 code_functions[it].argsize[7]
211 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
212 /* Internal functions have no code */
213 if (code_functions[it].entry >= 0) {
214 util_debug("GEN", " CODE:\n");
216 if (code_statements[j].opcode != INSTR_DONE)
217 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
218 asm_instr[code_statements[j].opcode].m,
219 code_statements[j].o1.s1,
220 code_statements[j].o2.s1,
221 code_statements[j].o3.s1
224 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
232 vec_free(code_statements);
234 vec_free(code_fields);
235 vec_free(code_functions);
236 vec_free(code_globals);
237 vec_free(code_chars);