3 * Dale Weiler, Wolfgang Bumiller
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
25 prog_section_statement *code_statements;
26 prog_section_def *code_defs;
27 prog_section_field *code_fields;
28 prog_section_function *code_functions;
33 uint32_t code_entfields;
36 prog_section_function empty_function = {0,0,0,0,0,0,0,{0}};
37 prog_section_statement empty_statement = {0,{0},{0},{0}};
38 prog_section_def empty_def = {0, 0, 0};
43 /* omit creation of null code */
44 if (OPTS_FLAG(OMIT_NULL_BYTES))
48 * The way progs.dat is suppose to work is odd, there needs to be
49 * some null (empty) statements, functions, and 28 globals
52 vec_push(code_globals, 0);
54 vec_push(code_chars, '\0');
55 vec_push(code_functions, empty_function);
56 vec_push(code_statements, empty_statement);
57 vec_push(code_defs, empty_def);
58 vec_push(code_fields, empty_def);
61 uint32_t code_genstring(const char *str)
63 uint32_t off = vec_size(code_chars);
65 vec_push(code_chars, *str);
68 vec_push(code_chars, 0);
72 uint32_t code_cachedstring(const char *str)
75 /* We could implement knuth-morris-pratt or something
76 * and also take substrings, but I'm uncomfortable with
77 * pointing to subparts of strings for the sake of clarity...
79 while (s < vec_size(code_chars)) {
80 if (!strcmp(str, code_chars + s))
82 while (code_chars[s]) ++s;
85 return code_genstring(str);
89 prog_section_def d1 = { TYPE_VOID, 28, 1 };
90 prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
91 prog_section_def d3 = { TYPE_STRING, 30, 14};
92 prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
93 prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
94 prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
95 prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
96 prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
97 prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
98 prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
99 prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
100 prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
102 strcpy(vec_add(code_chars, 0x7), "m_init");
103 strcpy(vec_add(code_chars, 0x6), "print");
104 strcpy(vec_add(code_chars, 0xD), "hello world\n");
105 strcpy(vec_add(code_chars, 0xA), "m_keydown");
106 strcpy(vec_add(code_chars, 0x7), "m_draw");
107 strcpy(vec_add(code_chars, 0x9), "m_toggle");
108 strcpy(vec_add(code_chars, 0xB), "m_shutdown");
110 vec_push(code_globals, 1); /* m_init */
111 vec_push(code_globals, 2); /* print */
112 vec_push(code_globals, 14); /* hello world in string table */
115 vec_push(code_defs, d1); /* m_init */
116 vec_push(code_defs, d2); /* print */
117 vec_push(code_defs, d3); /*hello_world*/
118 vec_push(code_functions, f1); /* m_init */
119 vec_push(code_functions, f2); /* print */
120 vec_push(code_functions, f3); /* m_keydown */
121 vec_push(code_functions, f4);
122 vec_push(code_functions, f5);
123 vec_push(code_functions, f6);
124 vec_push(code_statements, s1);
125 vec_push(code_statements, s2);
126 vec_push(code_statements, s3);
129 qcint code_alloc_field (size_t qcsize)
131 qcint pos = (qcint)code_entfields;
132 code_entfields += qcsize;
136 bool code_write(const char *filename) {
137 prog_header code_header;
141 /* see proposal.txt */
142 if (OPTS_FLAG(OMIT_NULL_BYTES)) {}
143 code_header.statements.offset = sizeof(prog_header);
144 code_header.statements.length = vec_size(code_statements);
145 code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * vec_size(code_statements));
146 code_header.defs.length = vec_size(code_defs);
147 code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * vec_size(code_defs));
148 code_header.fields.length = vec_size(code_fields);
149 code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * vec_size(code_fields));
150 code_header.functions.length = vec_size(code_functions);
151 code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * vec_size(code_functions));
152 code_header.globals.length = vec_size(code_globals);
153 code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * vec_size(code_globals));
154 code_header.strings.length = vec_size(code_chars);
155 code_header.version = 6;
157 code_header.crc16 = opts_forced_crc;
159 code_header.crc16 = code_crc;
160 code_header.entfield = code_entfields;
162 if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) {
163 util_debug("GEN", "Patching stringtable for -fdarkplaces-stringtablebug\n");
166 vec_push(code_chars, '\0'); /* > */
167 vec_push(code_chars, '\0'); /* = */
168 vec_push(code_chars, '\0'); /* P */
171 /* ensure all data is in LE format */
172 util_endianswap(&code_header, 1, sizeof(prog_header));
173 util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
174 util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
175 util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
176 util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
177 util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
179 fp = util_fopen(filename, "wb");
183 if (1 != fwrite(&code_header, sizeof(prog_header), 1, fp) ||
184 vec_size(code_statements) != fwrite(code_statements, sizeof(prog_section_statement), vec_size(code_statements), fp) ||
185 vec_size(code_defs) != fwrite(code_defs, sizeof(prog_section_def) , vec_size(code_defs) , fp) ||
186 vec_size(code_fields) != fwrite(code_fields, sizeof(prog_section_field) , vec_size(code_fields) , fp) ||
187 vec_size(code_functions) != fwrite(code_functions, sizeof(prog_section_function) , vec_size(code_functions) , fp) ||
188 vec_size(code_globals) != fwrite(code_globals, sizeof(int32_t) , vec_size(code_globals) , fp) ||
189 vec_size(code_chars) != fwrite(code_chars, 1 , vec_size(code_chars) , fp))
195 util_debug("GEN","HEADER:\n");
196 util_debug("GEN"," version: = %d\n", code_header.version );
197 util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
198 util_debug("GEN"," entfield: = %d\n", code_header.entfield);
199 util_debug("GEN"," statements = {.offset = % 8d, .length = % 8d}\n", code_header.statements.offset, code_header.statements.length);
200 util_debug("GEN"," defs = {.offset = % 8d, .length = % 8d}\n", code_header.defs .offset, code_header.defs .length);
201 util_debug("GEN"," fields = {.offset = % 8d, .length = % 8d}\n", code_header.fields .offset, code_header.fields .length);
202 util_debug("GEN"," functions = {.offset = % 8d, .length = % 8d}\n", code_header.functions .offset, code_header.functions .length);
203 util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
204 util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
207 util_debug("GEN", "FUNCTIONS:\n");
208 for (; it < vec_size(code_functions); it++) {
209 size_t j = code_functions[it].entry;
210 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",
211 code_functions[it].entry,
212 code_functions[it].firstlocal,
213 code_functions[it].locals,
214 code_functions[it].profile,
215 code_functions[it].name,
216 code_functions[it].file,
217 code_functions[it].nargs,
218 code_functions[it].argsize[0],
219 code_functions[it].argsize[1],
220 code_functions[it].argsize[2],
221 code_functions[it].argsize[3],
222 code_functions[it].argsize[4],
223 code_functions[it].argsize[5],
224 code_functions[it].argsize[6],
225 code_functions[it].argsize[7]
228 util_debug("GEN", " NAME: %s\n", &code_chars[code_functions[it].name]);
229 /* Internal functions have no code */
230 if (code_functions[it].entry >= 0) {
231 util_debug("GEN", " CODE:\n");
233 if (code_statements[j].opcode != AINSTR_END)
234 util_debug("GEN", " %-12s {% 5i,% 5i,% 5i}\n",
235 asm_instr[code_statements[j].opcode].m,
236 code_statements[j].o1.s1,
237 code_statements[j].o2.s1,
238 code_statements[j].o3.s1
241 util_debug("GEN", " DONE {0x00000,0x00000,0x00000}\n");
249 vec_free(code_statements);
251 vec_free(code_fields);
252 vec_free(code_functions);
253 vec_free(code_globals);
254 vec_free(code_chars);