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 * Following parse states:
26 * ASM_FUNCTION -- in a function accepting input statements
35 char *name; /* name of constant */
36 char type; /* type, float, vector, string */
37 char elem; /* 0=x, 1=y, or 2=Z? */
38 int offset; /* location in globals */
40 VECTOR_MAKE(globals, assembly_constants);
43 * Assembly text processing: this handles the internal collection
44 * of text to allow parsing and assemblation.
46 static char *const asm_getline(size_t *byte, FILE *fp) {
48 size_t read = util_getline(&line, byte, fp);
58 * Entire external interface for main.c - to perform actual assemblation
61 void asm_init(const char *file, FILE **fp) {
62 *fp = fopen(file, "r");
65 void asm_close(FILE *fp) {
71 for (; i < assembly_constants_elements; i++)
72 mem_d(assembly_constants_data[i].name);
73 mem_d(assembly_constants_data);
77 * Dumps all values of all constants and assembly related
78 * information obtained during the assembly procedure.
82 for (; i < assembly_constants_elements; i++) {
83 globals *g = &assembly_constants_data[i];
86 util_debug("ASM", "vector %s %c[%f]\n", g->name,
87 (g->elem == 0) ? 'X' :(
88 (g->elem == 1) ? 'Y' :
89 (g->elem == 2) ? 'Z' :' '),
90 INT2FLT(code_globals_data[g->offset])
99 * Parses a type, could be global or not depending on the
100 * assembly state: global scope with assignments are constants.
101 * globals with no assignments are globals. Function body types
104 static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
105 if (!(strstr(skip, "FLOAT:") == &skip[0]) &&
106 (strstr(skip, "VECTOR:") == &skip[0]) &&
107 (strstr(skip, "ENTITY:") == &skip[0]) &&
108 (strstr(skip, "FIELD:") == &skip[0]) &&
109 (strstr(skip, "STRING:") == &skip[0])) return false;
111 /* TODO: determine if constant, global, or local */
113 /* VECTOR */ case 'V': {
119 char *find = (char*)skip + 7;
120 char *name = (char*)skip + 7;
121 while (*find == ' ' || *find == '\t') find++;
124 if (strchr(find, ',')) {
126 *strchr((name = util_strdup(find)), ',')='\0';
128 find += strlen(name) + 1;
129 while (*find == ' ' || *find == '\t') find++;
131 if (util_strupper(name) || isdigit(*name)) {
132 printf("invalid name for vector variable\n");
136 * Parse all three elements of the vector. This will only
137 * pass the first try if we hit a constant, otherwise it's
140 #define PARSE_ELEMENT(X,Y,Z) \
141 if (isdigit(*X) || *X == '-'||*X == '+') { \
142 bool negated = (*X == '-'); \
143 if (negated || *X == '+') { X++; } \
144 Y = (negated)?-atof(X):atof(X); \
145 X = strchr(X, ','); \
149 PARSE_ELEMENT(find, val1, { find ++; while (*find == ' ') { find ++; } });
150 PARSE_ELEMENT(find, val2, { find ++; while (*find == ' ') { find ++; } });
151 PARSE_ELEMENT(find, val3, { find ++; /* no need to do anything here */ });
153 #define BUILD_ELEMENT(X,Y) \
154 global.type = TYPE_VECTOR; \
155 global.name = util_strdup(name); \
157 global.offset = code_globals_elements; \
158 assembly_constants_add(global); \
159 code_globals_add(FLT2INT(Y))
160 BUILD_ELEMENT(0, val1);
161 BUILD_ELEMENT(1, val2);
162 BUILD_ELEMENT(2, val3);
166 /* TODO global not constant */
170 /* ENTITY */ case 'E': {
171 const char *find = skip + 7;
172 while (*find == ' ' || *find == '\t') find++;
173 printf("found ENTITY %s\n", find);
176 /* STRING */ case 'S': {
177 const char *find = skip + 7;
178 while (*find == ' ' || *find == '\t') find++;
179 printf("found STRING %s\n", find);
188 * Parses a function: trivial case, handles occurances of duplicated
189 * names among other things. Ensures valid name as well, and even
190 * internal engine function selection.
192 static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
193 if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
196 if (strstr(skip, "FUNCTION:") == &skip[0]) {
197 char *copy = util_strsws(skip+10);
198 char *name = util_strchp(copy, strchr(copy, '\0'));
200 /* TODO: failure system, missing name */
202 printf("expected name on function\n");
207 /* TODO: failure system, invalid name */
208 if (!isalpha(*name) || util_strupper(name)) {
209 printf("invalid identifer for function name\n");
216 * Function could be internal function, look for $
219 if (strchr(name, ',')) {
220 prog_section_function function;
221 prog_section_def def;
223 char *find = strchr(name, ',') + 1;
225 /* skip whitespace */
226 while (*find == ' ' || *find == '\t')
230 printf("expected $ for internal function selection, got %s instead\n", find);
236 if (!isdigit(*find)) {
237 printf("invalid internal identifier, expected valid number\n");
242 *strchr(name, ',')='\0';
245 * Now add the following items to the code system:
247 * definition (optional)
251 function.entry = -atoi(find);
252 function.firstlocal = 0;
253 function.profile = 0;
254 function.name = code_chars_elements;
257 def.type = TYPE_FUNCTION;
258 def.offset = code_globals_elements;
259 def.name = code_chars_elements;
260 code_functions_add(function);
262 code_globals_add (code_chars_elements);
263 code_chars_put (name, strlen(name));
264 code_chars_add ('\0');
266 util_debug("ASM", "added internal function %s to function table\n", name);
269 * Sanatize the numerical constant used to select the
270 * internal function. Must ensure it's all numeric, since
271 * atoi can silently drop characters from a string and still
272 * produce a valid constant that would lead to runtime problems.
274 if (util_strdigit(find))
275 printf("found internal function %s, -%d\n", name, atoi(find));
277 printf("invalid internal function identifier, must be all numeric\n");
281 * The function isn't an internal one. Determine the name and
282 * amount of arguments the function accepts by searching for
283 * the `#` (pound sign).
286 char *find = strchr(name, '#');
290 * Code structures for filling after determining the correct
291 * information to add to the code write system.
293 prog_section_function function;
294 prog_section_def def;
298 /* skip whitespace */
299 if (*find == ' ' || *find == '\t')
303 * If the input is larger than eight, it's considered
304 * invalid and shouldn't be allowed. The QuakeC VM only
305 * allows a maximum of eight arguments.
307 if (strlen(find) > 1 || *find == '9') {
308 printf("invalid number of arguments, must be a valid number from 0-8\n");
316 * if we made it this far we have a valid number for the
317 * argument count, so fall through a switch statement and
321 case '8': args++; case '7': args++;
322 case '6': args++; case '5': args++;
323 case '4': args++; case '3': args++;
324 case '2': args++; case '1': args++;
328 printf("missing number of argument count in function %s\n", name);
330 /* terminate name inspot */
334 * We got valid function structure information now. Lets add
335 * the function to the code writer function table.
337 function.entry = code_statements_elements;
338 function.firstlocal = 0;
339 function.profile = 0;
340 function.name = code_chars_elements;
342 function.nargs = args;
343 def.type = TYPE_FUNCTION;
344 def.offset = code_globals_elements;
345 def.name = code_chars_elements;
346 code_functions_add(function);
348 code_globals_add (code_chars_elements);
349 code_chars_put (name, strlen(name));
350 code_chars_add ('\0');
352 /* update assembly state */
353 *state = ASM_FUNCTION;
354 util_debug("ASM", "added context function %s to function table\n", name);
364 static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state *state) {
366 * This parses a valid statement in assembly and adds it to the code
367 * table to be wrote. This needs to handle correct checking of all
368 * statements to ensure the correct amount of operands are passed to
369 * the menomic. This must also check for valid function calls (ensure
370 * the names selected exist in the program scope) and ensure the correct
371 * CALL* is used (depending on the amount of arguments the function
372 * is expected to take)
375 prog_section_statement s;
378 for (; i < sizeof(asm_instr)/sizeof(*asm_instr); i++) {
380 * Iterate all possible instructions and check if the selected
381 * instructure in the input stream `skip` is actually a valid
384 if (strstr(skip, asm_instr[i].m) == &skip[0]) {
386 * Parse the operands for `i` (the instruction). The order
387 * of asm_instr is in the order of the menomic encoding so
388 * `i` == menomic encoding.
391 switch (asm_instr[i].o) {
393 * Each instruction can have from 0-3 operands; and can
394 * be used with less or more operands depending on it's
397 * DONE for example can use either 0 operands, or 1 (to
398 * emulate the effect of RETURN)
401 skip += asm_instr[i].l+1;
402 /* skip whitespace */
403 while (*skip == ' ' || *skip == '\t')
406 * TODO: parse operands correctly figure out what it is
407 * that the assembly is trying to do, i.e string table
408 * lookup, function calls etc.
410 * This needs to have a fall state, we start from the
411 * end of the string and work backwards.
420 /* add the statement now */
421 code_statements_add(s);
427 void asm_parse(FILE *fp) {
430 long line = 1; /* current line */
431 size_t size = 0; /* size of line */
432 asm_state state = ASM_NULL;
439 util_debug("ASM", x); \
440 } while (0); continue
442 while ((data = asm_getline (&size, fp)) != NULL) {
443 char *copy = util_strsws(data); /* skip whitespace */
444 skip = util_strrnl(copy); /* delete newline */
447 if (asm_parse_type(skip, line, &state)) { asm_end("asm_parse_type\n"); }
449 if (asm_parse_func(skip, line, &state)) { asm_end("asm_parse_func\n"); }
451 if (asm_parse_stmt(skip, line, &state)) { asm_end("asm_parse_stmt\n"); }
453 /* statement closure */
454 if (state == ASM_FUNCTION && (
455 (strstr(skip, "DONE") == &skip[0])||
456 (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
458 /* TODO: everything */
460 asm_end("asm_parse_end\n");