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)
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;
254 function.profile = 0;
255 function.name = code_chars_elements;
258 def.type = TYPE_FUNCTION;
259 def.offset = code_globals_elements;
260 def.name = code_chars_elements;
261 memset(function.argsize, 0, sizeof(function.argsize));
262 code_functions_add(function);
264 code_chars_put (name, strlen(name));
265 code_chars_add ('\0');
267 util_debug("ASM", "added internal function %s to function table\n", name);
270 * Sanatize the numerical constant used to select the
271 * internal function. Must ensure it's all numeric, since
272 * atoi can silently drop characters from a string and still
273 * produce a valid constant that would lead to runtime problems.
275 if (util_strdigit(find))
276 util_debug("ASM", "found internal function %s, -%d\n", name, atoi(find));
278 printf("invalid internal function identifier, must be all numeric\n");
282 * The function isn't an internal one. Determine the name and
283 * amount of arguments the function accepts by searching for
284 * the `#` (pound sign).
288 char *find = strchr(name, '#');
292 * Code structures for filling after determining the correct
293 * information to add to the code write system.
295 prog_section_function function;
296 prog_section_def def;
300 /* skip whitespace */
301 if (*find == ' ' || *find == '\t')
305 * If the input is larger than eight, it's considered
306 * invalid and shouldn't be allowed. The QuakeC VM only
307 * allows a maximum of eight arguments.
310 printf("invalid number of arguments, must be a valid number from 0-8\n");
318 * if we made it this far we have a valid number for the
319 * argument count, so fall through a switch statement and
323 case '8': args++; case '7': args++;
324 case '6': args++; case '5': args++;
325 case '4': args++; case '3': args++;
326 case '2': args++; case '1': args++;
330 * We need to parse the argument size now by determining
331 * the argument identifer list used after the amount of
334 memset(function.argsize, 0, sizeof(function.argsize));
335 find ++; /* skip the number */
336 while (*find == ' ' || *find == '\t') find++;
337 while (size < args) {
339 case 'V': case 'v': function.argsize[size]=3; break;
342 case 'E': case 'e': function.argsize[size]=1; break;
344 printf("missing argument identifer, expected %d\n", args);
347 printf("error invalid function argument identifier\n");
352 while (*find == ' ' || *find == '\t') find++;
354 printf("too many function argument identifers expected %d\n", args);
358 printf("missing number of argument count in function %s\n", name);
363 * Now we need to strip the name apart into it's exact size
364 * by working in the peek buffer till we hit the name again.
368 peek --; /* number */
370 while (*peek == ' ' || *peek == '\t') peek--;
373 * We're guranteed to be exactly where we need to be in the
374 * peek buffer to null terminate and get our name from name
375 * without any garbage before or after it.
380 * We got valid function structure information now. Lets add
381 * the function to the code writer function table.
383 function.entry = code_statements_elements-1;
384 function.firstlocal = 0;
386 function.profile = 0;
387 function.name = code_chars_elements;
389 function.nargs = args;
390 def.type = TYPE_FUNCTION;
391 def.offset = code_globals_elements;
392 def.name = code_chars_elements;
393 code_functions_add(function);
394 code_globals_add(code_statements_elements);
395 code_chars_put (name, strlen(name));
396 code_chars_add ('\0');
398 /* update assembly state */
400 *state = ASM_FUNCTION;
401 util_debug("ASM", "added context function %s to function table\n", name);
411 static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state *state) {
413 * This parses a valid statement in assembly and adds it to the code
414 * table to be wrote. This needs to handle correct checking of all
415 * statements to ensure the correct amount of operands are passed to
416 * the menomic. This must also check for valid function calls (ensure
417 * the names selected exist in the program scope) and ensure the correct
418 * CALL* is used (depending on the amount of arguments the function
419 * is expected to take)
421 char *c = (char*)skip;
422 prog_section_statement s;
426 * statements are only allowed when inside a function body
427 * otherwise the assembly is invalid.
429 if (*state != ASM_FUNCTION)
433 * Skip any possible whitespace, it's not wanted we're searching
434 * for an instruction. TODO: recrusive decent parser skip on line
435 * entry instead of pre-op.
437 while (*skip == ' ' || *skip == '\t')
440 for (; i < sizeof(asm_instr)/sizeof(*asm_instr); i++) {
442 * Iterate all possible instructions and check if the selected
443 * instructure in the input stream `skip` is actually a valid
446 if (!strncmp(skip, asm_instr[i].m, asm_instr[i].l)) {
447 printf("found statement %s\n", asm_instr[i].m);
449 * Parse the operands for `i` (the instruction). The order
450 * of asm_instr is in the order of the menomic encoding so
451 * `i` == menomic encoding.
454 switch (asm_instr[i].o) {
456 * Each instruction can have from 0-3 operands; and can
457 * be used with less or more operands depending on it's
460 * DONE for example can use either 0 operands, or 1 (to
461 * emulate the effect of RETURN)
463 * TODO: parse operands correctly figure out what it is
464 * that the assembly is trying to do, i.e string table
465 * lookup, function calls etc.
467 * This needs to have a fall state, we start from the
468 * end of the string and work backwards.
473 if (!(c = strrchr(c, ','))) { \
474 printf("error, expected more operands\n"); \
479 while (*c == ' ' || *c == '\t') { \
483 X = (const char*)c; \
490 const char *data; OPFILL(data);
491 printf("OP3: %s\n", data);
495 const char *data; OPFILL(data);
496 printf("OP2: %s\n", data);
500 while (*c == ' ' || *c == '\t') c++;
502 while (*c == ' ' || *c == '\t') c++;
504 printf("OP1: %s\n", c);
509 /* add the statement now */
510 code_statements_add(s);
516 void asm_parse(FILE *fp) {
519 long line = 1; /* current line */
520 size_t size = 0; /* size of line */
521 asm_state state = ASM_NULL;
528 util_debug("ASM", x); \
529 } while (0); continue
531 while ((data = asm_getline (&size, fp)) != NULL) {
532 char *copy = util_strsws(data); /* skip whitespace */
533 skip = util_strrnl(copy); /* delete newline */
535 /* TODO: statement END check */
536 if (state == ASM_FUNCTION)
539 if (asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
540 if (asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
541 if (asm_parse_stmt(skip, line, &state)){ asm_end("asm_parse_stmt\n"); }