2012-04-17 08:29:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012
|
2012-04-17 20:24:22 +00:00
|
|
|
* Dale Weiler
|
2012-04-17 08:29:58 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
* so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "gmqcc.h"
|
|
|
|
/*
|
2012-04-28 07:26:40 +00:00
|
|
|
* Following parse states:
|
|
|
|
* ASM_FUNCTION -- in a function accepting input statements
|
|
|
|
* ....
|
2012-04-17 08:29:58 +00:00
|
|
|
*/
|
2012-04-28 07:26:40 +00:00
|
|
|
typedef enum {
|
|
|
|
ASM_NULL,
|
|
|
|
ASM_FUNCTION
|
|
|
|
} asm_state;
|
2012-04-17 08:29:58 +00:00
|
|
|
|
2012-04-28 07:26:40 +00:00
|
|
|
typedef struct {
|
|
|
|
char *name; /* name of constant */
|
|
|
|
int offset; /* location in globals */
|
|
|
|
} globals;
|
|
|
|
VECTOR_MAKE(globals, assembly_constants);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assembly text processing: this handles the internal collection
|
|
|
|
* of text to allow parsing and assemblation.
|
|
|
|
*/
|
2012-04-17 08:29:58 +00:00
|
|
|
static char *const asm_getline(size_t *byte, FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *line = NULL;
|
2012-04-28 09:54:24 +00:00
|
|
|
size_t read = util_getline(&line, byte, fp);
|
2012-04-17 20:24:22 +00:00
|
|
|
*byte = read;
|
|
|
|
if (read == -1) {
|
|
|
|
mem_d (line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return line;
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 07:26:40 +00:00
|
|
|
/*
|
|
|
|
* Entire external interface for main.c - to perform actual assemblation
|
|
|
|
* of assembly files.
|
|
|
|
*/
|
2012-04-17 08:29:58 +00:00
|
|
|
void asm_init(const char *file, FILE **fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
*fp = fopen(file, "r");
|
|
|
|
code_init();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
|
|
|
void asm_close(FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
fclose(fp);
|
|
|
|
code_write();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
2012-04-17 21:16:11 +00:00
|
|
|
void asm_clear() {
|
2012-04-19 22:03:30 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (; i < assembly_constants_elements; i++)
|
|
|
|
mem_d(assembly_constants_data[i].name);
|
|
|
|
mem_d(assembly_constants_data);
|
2012-04-17 21:16:11 +00:00
|
|
|
}
|
2012-04-19 20:41:03 +00:00
|
|
|
|
2012-04-27 20:45:34 +00:00
|
|
|
/*
|
|
|
|
* Parses a type, could be global or not depending on the
|
|
|
|
* assembly state: global scope with assignments are constants.
|
|
|
|
* globals with no assignments are globals. Function body types
|
|
|
|
* are locals.
|
|
|
|
*/
|
|
|
|
static inline bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
|
2012-04-28 07:53:23 +00:00
|
|
|
if (!(strstr(skip, "FLOAT:") == &skip[0]) &&
|
|
|
|
(strstr(skip, "VECTOR:") == &skip[0]) &&
|
|
|
|
(strstr(skip, "ENTITY:") == &skip[0]) &&
|
|
|
|
(strstr(skip, "FIELD:") == &skip[0]) &&
|
|
|
|
(strstr(skip, "STRING:") == &skip[0])) return false;
|
|
|
|
|
|
|
|
/* TODO: determine if constant, global, or local */
|
|
|
|
switch (*skip) {
|
|
|
|
/* VECTOR */ case 'V': {
|
2012-04-28 11:27:03 +00:00
|
|
|
float val1;
|
|
|
|
float val2;
|
|
|
|
float val3;
|
|
|
|
|
2012-04-28 07:53:23 +00:00
|
|
|
const char *find = skip + 7;
|
|
|
|
while (*find == ' ' || *find == '\t') find++;
|
2012-04-28 11:27:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse all three elements of the vector. This will only
|
|
|
|
* pass the first try if we hit a constant, otherwise it's
|
|
|
|
* a global.
|
|
|
|
*/
|
|
|
|
#define PARSE_ELEMENT(X,Y,Z) \
|
|
|
|
if (isdigit(*X) || *X == '-'||*X == '+') { \
|
|
|
|
bool negated = (*X == '-'); \
|
|
|
|
if (negated || *X == '+') { X++; } \
|
|
|
|
Y = (negated)?-atof(X):atof(X); \
|
|
|
|
X = strchr(X, ','); \
|
|
|
|
Z \
|
|
|
|
}
|
|
|
|
|
2012-04-28 19:13:41 +00:00
|
|
|
PARSE_ELEMENT(find, val1, { if(find) { find +=3; }});
|
|
|
|
PARSE_ELEMENT(find, val2, { if(find) { find +=2; }});
|
|
|
|
PARSE_ELEMENT(find, val3, { if(find) { find +=1; }});
|
2012-04-28 11:27:03 +00:00
|
|
|
#undef PARSE_ELEMENT
|
|
|
|
|
|
|
|
printf("X:[0] = %f\n", val1);
|
|
|
|
printf("Y:[1] = %f\n", val2);
|
|
|
|
printf("Z:[2] = %f\n", val3);
|
|
|
|
|
2012-04-28 07:53:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* ENTITY */ case 'E': {
|
|
|
|
const char *find = skip + 7;
|
|
|
|
while (*find == ' ' || *find == '\t') find++;
|
|
|
|
printf("found ENTITY %s\n", find);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* STRING */ case 'S': {
|
|
|
|
const char *find = skip + 7;
|
|
|
|
while (*find == ' ' || *find == '\t') find++;
|
|
|
|
printf("found STRING %s\n", find);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 20:45:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a function: trivial case, handles occurances of duplicated
|
|
|
|
* names among other things. Ensures valid name as well, and even
|
|
|
|
* internal engine function selection.
|
|
|
|
*/
|
|
|
|
static inline bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
|
|
|
|
if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strstr(skip, "FUNCTION:") == &skip[0]) {
|
2012-04-28 06:34:39 +00:00
|
|
|
char *copy = util_strsws(skip+10);
|
|
|
|
char *name = util_strchp(copy, strchr(copy, '\0'));
|
|
|
|
|
|
|
|
/* TODO: failure system, missing name */
|
|
|
|
if (!name) {
|
|
|
|
printf("expected name on function\n");
|
|
|
|
mem_d(copy);
|
|
|
|
mem_d(name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* TODO: failure system, invalid name */
|
2012-04-28 07:26:40 +00:00
|
|
|
if (!isalpha(*name) || util_strupper(name)) {
|
2012-04-28 06:34:39 +00:00
|
|
|
printf("invalid identifer for function name\n");
|
|
|
|
mem_d(copy);
|
|
|
|
mem_d(name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-28 07:26:40 +00:00
|
|
|
/*
|
|
|
|
* Function could be internal function, look for $
|
|
|
|
* to determine this.
|
|
|
|
*/
|
|
|
|
if (strchr(name, ',')) {
|
|
|
|
char *find = strchr(name, ',') + 1;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
while (*find == ' ' || *find == '\t')
|
|
|
|
find++;
|
|
|
|
|
|
|
|
if (*find != '$') {
|
|
|
|
printf("expected $ for internal function selection, got %s instead\n", find);
|
|
|
|
mem_d(copy);
|
|
|
|
mem_d(name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
find ++;
|
|
|
|
if (!isdigit(*find)) {
|
|
|
|
printf("invalid internal identifier, expected valid number\n");
|
|
|
|
mem_d(copy);
|
|
|
|
mem_d(name);
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-28 10:35:42 +00:00
|
|
|
*strchr(name, ',')='\0';
|
|
|
|
|
2012-04-28 07:37:14 +00:00
|
|
|
/*
|
|
|
|
* Now add the following items to the code system:
|
|
|
|
* function
|
|
|
|
* definition (optional)
|
|
|
|
* global (optional)
|
|
|
|
* name
|
|
|
|
*/
|
2012-04-28 07:26:40 +00:00
|
|
|
code_functions_add((prog_section_function){
|
|
|
|
-atoi(find), /* needs to be negated */
|
|
|
|
0, 0, 0,
|
|
|
|
.name = code_chars_elements,
|
|
|
|
0, 0,{0}
|
|
|
|
});
|
2012-04-28 07:37:14 +00:00
|
|
|
code_defs_add((prog_section_def){
|
|
|
|
.type = TYPE_FUNCTION,
|
|
|
|
.offset = code_globals_elements,
|
|
|
|
.name = code_chars_elements
|
|
|
|
});
|
|
|
|
code_globals_add(code_chars_elements);
|
|
|
|
|
2012-04-28 07:26:40 +00:00
|
|
|
code_chars_put(name, strlen(name));
|
|
|
|
code_chars_add('\0');
|
2012-04-28 07:37:14 +00:00
|
|
|
|
2012-04-28 07:53:23 +00:00
|
|
|
/*
|
|
|
|
* Sanatize the numerical constant used to select the
|
|
|
|
* internal function. Must ensure it's all numeric, since
|
|
|
|
* atoi can silently drop characters from a string and still
|
|
|
|
* produce a valid constant that would lead to runtime problems.
|
|
|
|
*/
|
|
|
|
if (util_strdigit(find))
|
|
|
|
printf("found internal function %s, -%d\n", name, atoi(find));
|
|
|
|
else
|
|
|
|
printf("invalid internal function identifier, must be all numeric\n");
|
|
|
|
|
2012-04-28 07:37:14 +00:00
|
|
|
} else {
|
2012-04-28 07:53:23 +00:00
|
|
|
/* TODO: function bodies */
|
2012-04-28 07:26:40 +00:00
|
|
|
}
|
2012-04-28 06:34:39 +00:00
|
|
|
|
|
|
|
mem_d(copy);
|
|
|
|
mem_d(name);
|
2012-04-27 20:45:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:29:58 +00:00
|
|
|
void asm_parse(FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *data = NULL;
|
2012-04-27 20:45:34 +00:00
|
|
|
char *skip = NULL;
|
2012-04-17 20:24:22 +00:00
|
|
|
long line = 1; /* current line */
|
|
|
|
size_t size = 0; /* size of line */
|
|
|
|
asm_state state = ASM_NULL;
|
2012-04-27 20:45:34 +00:00
|
|
|
|
2012-04-28 06:34:39 +00:00
|
|
|
#define asm_end(x) \
|
|
|
|
do { \
|
|
|
|
mem_d(data); \
|
|
|
|
mem_d(copy); \
|
|
|
|
line++; \
|
|
|
|
util_debug("ASM", x); \
|
|
|
|
} while (0); continue
|
2012-04-17 20:24:22 +00:00
|
|
|
|
2012-04-27 20:45:34 +00:00
|
|
|
while ((data = asm_getline (&size, fp)) != NULL) {
|
2012-04-28 06:34:39 +00:00
|
|
|
char *copy = util_strsws(data); /* skip whitespace */
|
|
|
|
skip = util_strrnl(copy); /* delete newline */
|
2012-04-20 04:44:44 +00:00
|
|
|
|
2012-04-27 20:45:34 +00:00
|
|
|
/* parse type */
|
2012-04-28 06:34:39 +00:00
|
|
|
if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
|
2012-04-27 20:45:34 +00:00
|
|
|
/* parse func */
|
2012-04-28 06:34:39 +00:00
|
|
|
if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
|
|
|
|
|
|
|
|
/* statement closure */
|
|
|
|
if (state == ASM_FUNCTION && (
|
|
|
|
(strstr(skip, "DONE") == &skip[0])||
|
|
|
|
(strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
|
2012-04-27 20:45:34 +00:00
|
|
|
|
2012-04-27 20:31:38 +00:00
|
|
|
/* TODO: everything */
|
|
|
|
(void)state;
|
2012-04-28 06:34:39 +00:00
|
|
|
asm_end("asm_parse_end\n");
|
2012-04-17 20:24:22 +00:00
|
|
|
}
|
2012-04-28 06:34:39 +00:00
|
|
|
#undef asm_end
|
2012-04-19 20:41:03 +00:00
|
|
|
asm_clear();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|