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
36 char type; /* type, float, vector, string, function*/
37 char elem; /* 0=x, 1=y, or 2=Z? */
38 int offset; /* location in globals */
41 VECTOR_MAKE(asm_sym, asm_symbols);
44 * Assembly text processing: this handles the internal collection
45 * of text to allow parsing and assemblation.
47 static char* asm_getline(size_t *byte, FILE *fp) {
49 size_t read = util_getline(&line, byte, fp);
59 * Entire external interface for main.c - to perform actual assemblation
62 void asm_init(const char *file, FILE **fp) {
63 *fp = fopen(file, "r");
66 void asm_close(FILE *fp) {
68 code_write("program.dat");
72 for (; i < asm_symbols_elements; i++)
73 mem_d(asm_symbols_data[i].name);
74 mem_d(asm_symbols_data);
78 * Dumps all values of all constants and assembly related
79 * information obtained during the assembly procedure.
83 for (; i < asm_symbols_elements; i++) {
84 asm_sym *g = &asm_symbols_data[i];
85 if (!g->isconst) continue;
88 util_debug("ASM", "vector %s %c[%f]\n", g->name,
89 (g->elem == 0) ? 'X' :(
90 (g->elem == 1) ? 'Y' :
91 (g->elem == 2) ? 'Z' :' '),
92 INT2FLT(code_globals_data[g->offset])
97 util_debug("ASM", "function %s\n", g->name);
104 * Parses a type, could be global or not depending on the
105 * assembly state: global scope with assignments are constants.
106 * globals with no assignments are globals. Function body types
109 static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
110 if ((strstr(skip, "FLOAT:") != &skip[0]) &&
111 (strstr(skip, "VECTOR:") != &skip[0]) &&
112 (strstr(skip, "ENTITY:") != &skip[0]) &&
113 (strstr(skip, "FIELD:") != &skip[0]) &&
114 (strstr(skip, "STRING:") != &skip[0])) return false;
116 /* TODO: determine if constant, global, or local */
118 /* VECTOR */ case 'V': {
124 char *find = (char*)skip + 7;
125 char *name = (char*)skip + 7;
126 while (*find == ' ' || *find == '\t') find++;
129 if (strchr(find, ',')) {
131 *strchr((name = util_strdup(find)), ',')='\0';
133 find += strlen(name) + 1;
134 while (*find == ' ' || *find == '\t') find++;
136 if (util_strupper(name) || isdigit(*name)) {
137 printf("invalid name for vector variable\n");
141 * Parse all three elements of the vector. This will only
142 * pass the first try if we hit a constant, otherwise it's
145 #define PARSE_ELEMENT(X,Y,Z) \
146 if (isdigit(*X) || *X == '-'||*X == '+') { \
147 bool negated = (*X == '-'); \
148 if (negated || *X == '+') { X++; } \
149 Y = (negated)?-atof(X):atof(X); \
150 X = strchr(X, ','); \
154 PARSE_ELEMENT(find, val1, { find ++; while (*find == ' ') { find ++; } });
155 PARSE_ELEMENT(find, val2, { find ++; while (*find == ' ') { find ++; } });
156 PARSE_ELEMENT(find, val3, { find ++; /* no need to do anything here */ });
158 #define BUILD_ELEMENT(X,Y) \
159 sym.type = TYPE_VECTOR; \
160 sym.name = util_strdup(name); \
162 sym.offset = code_globals_elements; \
163 asm_symbols_add(sym); \
164 code_globals_add(FLT2INT(Y))
165 BUILD_ELEMENT(0, val1);
166 BUILD_ELEMENT(1, val2);
167 BUILD_ELEMENT(2, val3);
171 /* TODO global not constant */
175 /* ENTITY */ case 'E': {
176 const char *find = skip + 7;
177 while (*find == ' ' || *find == '\t') find++;
178 printf("found ENTITY %s\n", find);
181 /* STRING */ case 'S': {
182 const char *find = skip + 7;
183 while (*find == ' ' || *find == '\t') find++;
184 printf("found STRING %s\n", find);
193 * Parses a function: trivial case, handles occurances of duplicated
194 * names among other things. Ensures valid name as well, and even
195 * internal engine function selection.
197 static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
198 if (*state == ASM_FUNCTION)
201 if (strstr(skip, "FUNCTION:") == &skip[0]) {
203 char *look = util_strdup(skip+10);
206 while (*copy == ' ' || *copy == '\t') copy++;
208 memset(&sym, 0, sizeof(asm_sym));
211 * Chop the function name out of the string, this allocates
214 name = util_strchp(copy, strchr(copy, '\0'));
216 /* TODO: failure system, missing name */
218 printf("expected name on function\n");
223 /* TODO: failure system, invalid name */
224 if (!isalpha(*name) || util_strupper(name)) {
225 printf("invalid identifer for function name\n");
232 * Function could be internal function, look for $
235 if (strchr(name, ',')) {
236 char *find = strchr(name, ',') + 1;
237 prog_section_function function;
238 prog_section_def def;
239 memset(&function, 0, sizeof(prog_section_function));
240 memset(&def, 0, sizeof(prog_section_def));
242 /* skip whitespace */
243 while (*find == ' ' || *find == '\t')
247 printf("expected $ for internal function selection, got %s instead\n", find);
253 if (!isdigit(*find)) {
254 printf("invalid internal identifier, expected valid number\n");
259 *strchr(name, ',')='\0';
262 * Now add the following items to the code system:
264 * definition (optional)
268 function.entry = -atoi(find);
269 function.firstlocal = 0;
271 function.profile = 0;
272 function.name = code_chars_elements;
275 def.type = TYPE_FUNCTION;
276 def.offset = code_globals_elements;
277 def.name = code_chars_elements;
278 code_functions_add(function);
280 code_chars_put (name, strlen(name));
281 code_chars_add ('\0');
282 sym.type = TYPE_FUNCTION;
283 sym.name = util_strdup(name);
284 sym.offset = function.entry;
285 asm_symbols_add(sym);
287 util_debug("ASM", "added internal function %s to function table\n", name);
290 * Sanatize the numerical constant used to select the
291 * internal function. Must ensure it's all numeric, since
292 * atoi can silently drop characters from a string and still
293 * produce a valid constant that would lead to runtime problems.
295 if (util_strdigit(find))
296 util_debug("ASM", "found internal function %s, -%d\n", name, atoi(find));
298 printf("invalid internal function identifier, must be all numeric\n");
302 * The function isn't an internal one. Determine the name and
303 * amount of arguments the function accepts by searching for
304 * the `#` (pound sign).
308 char *find = strchr(name, '#');
312 * Code structures for filling after determining the correct
313 * information to add to the code write system.
315 prog_section_function function;
316 prog_section_def def;
317 memset(&function, 0, sizeof(prog_section_function));
318 memset(&def, 0, sizeof(prog_section_def));
322 /* skip whitespace */
323 if (*find == ' ' || *find == '\t')
327 * If the input is larger than eight, it's considered
328 * invalid and shouldn't be allowed. The QuakeC VM only
329 * allows a maximum of eight arguments.
332 printf("invalid number of arguments, must be a valid number from 0-8\n");
340 * if we made it this far we have a valid number for the
341 * argument count, so fall through a switch statement and
345 case '8': args++; case '7': args++;
346 case '6': args++; case '5': args++;
347 case '4': args++; case '3': args++;
348 case '2': args++; case '1': args++;
352 * We need to parse the argument size now by determining
353 * the argument identifer list used after the amount of
356 memset(function.argsize, 0, sizeof(function.argsize));
357 find ++; /* skip the number */
358 while (*find == ' ' || *find == '\t') find++;
359 while (size < args) {
361 case 'V': case 'v': function.argsize[size]=3; break;
364 case 'E': case 'e': function.argsize[size]=1; break;
366 printf("missing argument identifer, expected %d\n", args);
369 printf("error invalid function argument identifier\n");
374 while (*find == ' ' || *find == '\t') find++;
376 printf("too many function argument identifers expected %d\n", args);
380 printf("missing number of argument count in function %s\n", name);
385 * Now we need to strip the name apart into it's exact size
386 * by working in the peek buffer till we hit the name again.
390 peek --; /* number */
392 while (*peek == ' ' || *peek == '\t') peek--;
395 * We're guranteed to be exactly where we need to be in the
396 * peek buffer to null terminate and get our name from name
397 * without any garbage before or after it.
402 * We got valid function structure information now. Lets add
403 * the function to the code writer function table.
405 function.entry = code_statements_elements;
406 function.firstlocal = 0;
408 function.profile = 0;
409 function.name = code_chars_elements;
411 function.nargs = args;
412 def.type = TYPE_FUNCTION;
413 def.offset = code_globals_elements;
414 def.name = code_chars_elements;
415 code_functions_add(function);
416 code_globals_add (code_statements_elements);
417 code_chars_put (name, strlen(name));
418 code_chars_add ('\0');
419 sym.type = TYPE_FUNCTION;
420 sym.name = util_strdup(name);
421 sym.offset = function.entry;
422 asm_symbols_add(sym);
424 /* update assembly state */
426 *state = ASM_FUNCTION;
427 util_debug("ASM", "added context function %s to function table\n", name);
437 static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state *state) {
439 * This parses a valid statement in assembly and adds it to the code
440 * table to be wrote. This needs to handle correct checking of all
441 * statements to ensure the correct amount of operands are passed to
442 * the menomic. This must also check for valid function calls (ensure
443 * the names selected exist in the program scope) and ensure the correct
444 * CALL* is used (depending on the amount of arguments the function
445 * is expected to take)
453 char *c = (char*)skip;
456 prog_section_statement s;
457 memset(&s, 0, sizeof(prog_section_statement));
460 * statements are only allowed when inside a function body
461 * otherwise the assembly is invalid.
463 if (*state != ASM_FUNCTION)
467 * Skip any possible whitespace, it's not wanted we're searching
468 * for an instruction. TODO: recrusive decent parser skip on line
469 * entry instead of pre-op.
471 while (*skip == ' ' || *skip == '\t')
474 for (; i < sizeof(asm_instr)/sizeof(*asm_instr); i++) {
476 * Iterate all possible instructions and check if the selected
477 * instructure in the input stream `skip` is actually a valid
480 if (!strncmp(skip, asm_instr[i].m, asm_instr[i].l)) {
483 * We hit the end of a function scope, retarget the state
484 * and add a DONE statement to the statment table.
486 if (i == AINSTR_END) {
488 code_statements_add(s);
494 * Check the instruction type to see what sort of data
495 * it's expected to have.
497 if (i >= INSTR_CALL0 && i <= INSTR_CALL8)
498 expect = EXPECT_FUNCTION;
500 expect = EXPECT_VARIABLE;
504 "found statement %s expecting: `%s` (%ld operand(s))\n",
506 (expect == EXPECT_FUNCTION)?"function name":(
507 (expect == EXPECT_VARIABLE)?"variable name":(
508 (expect == EXPECT_VALUE ?"value" : "unknown"))),
512 * Parse the operands for `i` (the instruction). The order
513 * of asm_instr is in the order of the menomic encoding so
514 * `i` == menomic encoding.
517 switch (asm_instr[i].o) {
519 * Each instruction can have from 0-3 operands; and can
520 * be used with less or more operands depending on it's
523 * DONE for example can use either 0 operands, or 1 (to
524 * emulate the effect of RETURN)
526 * TODO: parse operands correctly figure out what it is
527 * that the assembly is trying to do, i.e string table
528 * lookup, function calls etc.
530 * This needs to have a fall state, we start from the
531 * end of the string and work backwards.
533 #define OPEATS(X,Y) X##Y
534 #define OPCCAT(X,Y) OPEATS(X,Y)
535 #define OPLOAD(X,Y) \
537 util_debug("ASM", "loading operand data ...\n"); \
538 if (expect == EXPECT_VARIABLE) { \
540 for (; f<asm_symbols_elements; f++) { \
541 if (!strncmp(asm_symbols_data[f].name, (Y), strlen(Y)) && \
542 asm_symbols_data[f].type != TYPE_FUNCTION) { \
543 (X)=asm_symbols_data[f].offset; \
544 goto OPCCAT(foundv, __LINE__); \
547 printf("no variable named %s\n", (Y)); \
549 OPCCAT(foundv,__LINE__) : \
550 printf("operand loaded for %s\n", (Y)); \
551 } else if (expect == EXPECT_FUNCTION) { \
553 * It's a function call not a variable association with an instruction \
554 * these are harder to handle. \
557 if (strchr(Y, ' ')) { \
558 *strchr(Y, ' ')='\0'; \
560 for (; f<asm_symbols_elements; f++) { \
561 if (!strncmp(asm_symbols_data[f].name, (Y), strlen(Y)) && \
562 asm_symbols_data[f].type == TYPE_FUNCTION) { \
563 (X)=asm_symbols_data[f].offset; \
564 goto OPCCAT(foundf, __LINE__); \
567 printf("no function named [%s]\n", (Y)); \
569 OPCCAT(foundf,__LINE__) : \
570 printf("operand loaded for [%s]\n", (Y)); \
573 case 3: { OPLOAD(s.o3.s1,c); break; }
574 case 2: { OPLOAD(s.o2.s1,c); break; }
576 while (*c == ' ' || *c == '\t') c++;
578 while (*c == ' ' || *c == '\t') c++;
585 /* add the statement now */
586 code_statements_add(s);
592 void asm_parse(FILE *fp) {
594 long line = 1; /* current line */
595 size_t size = 0; /* size of line */
596 asm_state state = ASM_NULL;
602 util_debug("ASM", x); \
603 } while (0); continue
605 while ((data = asm_getline (&size, fp)) != NULL) {
608 while (*copy == ' ' || *copy == '\t') copy++;
609 while (*skip != '\n') skip++;
612 if (asm_parse_type(copy, line, &state)){ asm_end("asm_parse_type\n"); }
613 if (asm_parse_func(copy, line, &state)){ asm_end("asm_parse_func\n"); }
614 if (asm_parse_stmt(copy, line, &state)){ asm_end("asm_parse_stmt\n"); }
615 asm_end("asm_parse_white\n");