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;
27 prog_section_def *code_defs;
28 prog_section_field *code_fields;
29 prog_section_function *code_functions;
34 uint32_t code_entfields;
37 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
38 prog_section_statement empty_statement = {0,{0},{0},{0}};
39 prog_section_def empty_def = {0, 0, 0};
44 /* omit creation of null code */
45 if (OPTS_FLAG(OMIT_NULL_BYTES))
49 * The way progs.dat is suppose to work is odd, there needs to be
50 * some null (empty) statements, functions, and 28 globals
53 vec_push(code_globals, 0);
55 vec_push(code_chars, '\0');
56 vec_push(code_functions, empty_function);
57 vec_push(code_statements, empty_statement);
58 vec_push(code_defs, empty_def);
59 vec_push(code_fields, empty_def);
62 uint32_t code_genstring(const char *str)
64 uint32_t off = vec_size(code_chars);
66 vec_push(code_chars, *str);
69 vec_push(code_chars, 0);
73 uint32_t code_cachedstring(const char *str)
76 /* We could implement knuth-morris-pratt or something
77 * and also take substrings, but I'm uncomfortable with
78 * pointing to subparts of strings for the sake of clarity...
80 while (s < vec_size(code_chars)) {
81 if (!strcmp(str, code_chars + s))
83 while (code_chars[s]) ++s;
86 return code_genstring(str);
90 prog_section_def d1 = { TYPE_VOID, 28, 1 };
91 prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
92 prog_section_def d3 = { TYPE_STRING, 30, 14};
93 prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
94 prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
95 prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
96 prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
97 prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
98 prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
99 prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
100 prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
101 prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
103 strcpy(vec_add(code_chars, 0x7), "m_init");
104 strcpy(vec_add(code_chars, 0x6), "print");
105 strcpy(vec_add(code_chars, 0xD), "hello world\n");
106 strcpy(vec_add(code_chars, 0xA), "m_keydown");
107 strcpy(vec_add(code_chars, 0x7), "m_draw");
108 strcpy(vec_add(code_chars, 0x9), "m_toggle");
109 strcpy(vec_add(code_chars, 0xB), "m_shutdown");
111 vec_push(code_globals, 1); /* m_init */
112 vec_push(code_globals, 2); /* print */
113 vec_push(code_globals, 14); /* hello world in string table */
116 vec_push(code_defs, d1); /* m_init */
117 vec_push(code_defs, d2); /* print */
118 vec_push(code_defs, d3); /*hello_world*/
119 vec_push(code_functions, f1); /* m_init */
120 vec_push(code_functions, f2); /* print */
121 vec_push(code_functions, f3); /* m_keydown */
122 vec_push(code_functions, f4);
123 vec_push(code_functions, f5);
124 vec_push(code_functions, f6);
125 vec_push(code_statements, s1);
126 vec_push(code_statements, s2);
127 vec_push(code_statements, s3);
130 qcint code_alloc_field (size_t qcsize)
132 qcint pos = (qcint)code_entfields;
133 code_entfields += qcsize;
137 bool code_write(const char *filename) {
138 prog_header code_header;
142 /* see proposal.txt */
143 if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
144 code_header.statements.offset = sizeof(prog_header);
145 code_header.statements.length = vec_size(code_statements);
146 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
147 code_header.defs.length = vec_size(code_defs);
148 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
149 code_header.fields.length = vec_size(code_fields);
150 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
151 code_header.functions.length = vec_size(code_functions);
152 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
153 code_header.globals.length = vec_size(code_globals);
154 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
155 code_header.strings.length = vec_size(code_chars);
156 code_header.version = 6;
158 code_header.crc16 = opts_forced_crc;
160 code_header.crc16 = code_crc;
161 code_header.entfield = code_entfields;
163 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
164 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
167 vec_push(code_chars, '\0'); /* > */
168 vec_push(code_chars, '\0'); /* = */
169 vec_push(code_chars, '\0'); /* P */
172 /* ensure all data is in LE format */
173 util_endianswap(&code_header, 1, sizeof(prog_header));
174 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
175 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
176 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
177 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
178 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
180 fp = util_fopen(filename, "wb");
184 if (1 != fwrite(&code_header, sizeof(prog_header) , 1 , fp) ||
185 vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
186 vec_size(code_defs) != fwrite(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
187 vec_size(code_fields) != fwrite(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
188 vec_size(code_functions) != fwrite(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
189 vec_size(code_globals) != fwrite(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
190 vec_size(code_chars) != fwrite(code_chars, 1 , vec_size(code_chars) , 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 < vec_size(code_functions); it++) {
210 size_t j = code_functions[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[it].entry,
213 code_functions[it].firstlocal,
214 code_functions[it].locals,
215 code_functions[it].profile,
216 code_functions[it].name,
217 code_functions[it].file,
218 code_functions[it].nargs,
219 code_functions[it].argsize[0],
220 code_functions[it].argsize[1],
221 code_functions[it].argsize[2],
222 code_functions[it].argsize[3],
223 code_functions[it].argsize[4],
224 code_functions[it].argsize[5],
225 code_functions[it].argsize[6],
226 code_functions[it].argsize[7]
229 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
230 /* Internal functions have no code */
231 if (code_functions[it].entry >= 0) {
232 util_debug("GEN", " CODE:\n");
234 if (code_statements[j].opcode != AINSTR_END)
235 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
236 asm_instr[code_statements[j].opcode].m,
237 code_statements[j].o1.s1,
238 code_statements[j].o2.s1,
239 code_statements[j].o3.s1
242 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
250 vec_free(code_statements);
252 vec_free(code_fields);
253 vec_free(code_functions);
254 vec_free(code_globals);
255 vec_free(code_chars);