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"
|
|
|
|
/*
|
|
|
|
* Some assembler keywords not part of the opcodes above: these are
|
|
|
|
* for creating functions, or constants.
|
|
|
|
*/
|
|
|
|
const char *const asm_keys[] = {
|
2012-04-17 20:24:22 +00:00
|
|
|
"FLOAT" , /* define float */
|
|
|
|
"VECTOR" , /* define vector */
|
|
|
|
"ENTITY" , /* define ent */
|
|
|
|
"FIELD" , /* define field */
|
|
|
|
"STRING" , /* define string */
|
|
|
|
"FUNCTION"
|
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;
|
|
|
|
ssize_t read = util_getline(&line, byte, fp);
|
|
|
|
*byte = read;
|
|
|
|
if (read == -1) {
|
|
|
|
mem_d (line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return line;
|
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 20:14:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Following parse states:
|
2012-04-17 20:24:22 +00:00
|
|
|
* ASM_FUNCTION -- in a function accepting input statements
|
|
|
|
* ....
|
2012-04-17 20:14:00 +00:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2012-04-17 20:24:22 +00:00
|
|
|
ASM_NULL,
|
|
|
|
ASM_FUNCTION
|
2012-04-17 20:14:00 +00:00
|
|
|
} asm_state;
|
|
|
|
|
|
|
|
typedef struct {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *name; /* name of constant */
|
|
|
|
int offset; /* location in globals */
|
2012-04-17 20:14:00 +00:00
|
|
|
} globals;
|
|
|
|
VECTOR_MAKE(globals, assembly_constants);
|
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-20 04:44:44 +00:00
|
|
|
int asm_parsetype(const char *key, char **skip, long line) {
|
|
|
|
size_t keylen = strlen(key);
|
|
|
|
if (!strncmp(key, *skip, keylen)) {
|
|
|
|
if ((*skip)[keylen] != ':'){
|
|
|
|
printf("%li: Missing `:` after decltype\n", line);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*skip += keylen+1;
|
2012-04-25 20:43:25 +00:00
|
|
|
/* skip whitespace */
|
2012-04-20 04:44:44 +00:00
|
|
|
while (**skip == ' ' || **skip == '\t')
|
|
|
|
(*skip)++;
|
|
|
|
|
|
|
|
if (!isalpha(**skip)) {
|
|
|
|
printf("%li: Invalid identififer: %s\n", line, *skip);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
assembly_constants_add((globals) {
|
|
|
|
.name = util_strdup("empty"),
|
|
|
|
.offset = code_globals_elements
|
|
|
|
});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:29:58 +00:00
|
|
|
void asm_parse(FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *data = NULL;
|
|
|
|
char *skip = NULL;
|
|
|
|
long line = 1; /* current line */
|
|
|
|
size_t size = 0; /* size of line */
|
|
|
|
asm_state state = ASM_NULL;
|
|
|
|
|
2012-04-20 04:44:44 +00:00
|
|
|
while ((data = skip = asm_getline(&size, fp)) != NULL) {
|
|
|
|
/* remove any whitespace at start */
|
|
|
|
while (*skip == ' ' || *skip == '\t')
|
|
|
|
skip++;
|
|
|
|
/* remove newline at end of string */
|
|
|
|
*(skip+*(&size)-1) = '\0';
|
2012-04-17 20:24:22 +00:00
|
|
|
|
2012-04-20 04:44:44 +00:00
|
|
|
if (asm_parsetype(asm_keys[5], &skip, line)) {
|
2012-04-17 20:24:22 +00:00
|
|
|
if (state != ASM_NULL) {
|
|
|
|
printf("%li: Error unfinished function block, expected DONE or RETURN\n", line);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
state = ASM_FUNCTION;
|
|
|
|
code_defs_add((prog_section_def){
|
|
|
|
.type = TYPE_VOID,
|
2012-04-20 04:44:44 +00:00
|
|
|
.offset = code_globals_elements,
|
|
|
|
.name = code_chars_elements
|
2012-04-17 20:24:22 +00:00
|
|
|
});
|
2012-04-20 04:44:44 +00:00
|
|
|
code_globals_add(code_functions_elements);
|
|
|
|
code_functions_add((prog_section_function) {
|
|
|
|
.entry = code_statements_elements,
|
2012-04-17 20:24:22 +00:00
|
|
|
.firstlocal = 0,
|
|
|
|
.locals = 0,
|
|
|
|
.profile = 0,
|
2012-04-20 04:44:44 +00:00
|
|
|
.name = code_chars_elements,
|
2012-04-17 20:24:22 +00:00
|
|
|
.file = 0,
|
|
|
|
.nargs = 0,
|
|
|
|
.argsize = {0}
|
|
|
|
});
|
2012-04-25 20:49:04 +00:00
|
|
|
code_chars_put(skip, strlen(skip));
|
2012-04-20 04:44:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if 0
|
2012-04-17 20:24:22 +00:00
|
|
|
/* if we make it this far then we have statements */
|
|
|
|
{
|
|
|
|
size_t i = 0; /* counter */
|
|
|
|
size_t o = 0; /* operands */
|
2012-04-20 04:44:44 +00:00
|
|
|
size_t c = 0; /* copy */
|
2012-04-17 20:24:22 +00:00
|
|
|
char *t = NULL; /* token */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Most ops a single statement can have is three.
|
|
|
|
* lets allocate some space for all of those here.
|
|
|
|
*/
|
|
|
|
char op[3][32768] = {{0},{0},{0}};
|
|
|
|
for (; i < sizeof(asm_instr)/sizeof(*asm_instr); i++) {
|
|
|
|
if (!strncmp(skip, asm_instr[i].m, asm_instr[i].l)) {
|
|
|
|
if (state != ASM_FUNCTION) {
|
|
|
|
printf("%li: Statement not inside function block\n", line);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update parser state */
|
|
|
|
if (i == INSTR_DONE || i == INSTR_RETURN) {
|
|
|
|
goto end;
|
|
|
|
state = ASM_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the statement */
|
2012-04-20 04:44:44 +00:00
|
|
|
c = i;
|
2012-04-17 20:24:22 +00:00
|
|
|
o = asm_instr[i].o; /* operands */
|
|
|
|
skip += asm_instr[i].l; /* skip instruction */
|
|
|
|
t = strtok(skip, " ,");
|
|
|
|
i = 0;
|
|
|
|
while (t != NULL && i < 3) {
|
|
|
|
strcpy(op[i], t);
|
|
|
|
t = strtok(NULL, " ,");
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check */
|
|
|
|
if (i != o) {
|
|
|
|
printf("not enough operands, expected: %li, got %li\n", o, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: hashtable value LOAD .... etc */
|
|
|
|
code_statements_add((prog_section_statement){
|
2012-04-20 04:44:44 +00:00
|
|
|
c,
|
|
|
|
{ atof(op[0]) },
|
|
|
|
{ atof(op[1]) },
|
|
|
|
{ atof(op[2]) }
|
2012-04-17 20:24:22 +00:00
|
|
|
});
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 04:44:44 +00:00
|
|
|
#endif
|
2012-04-17 20:24:22 +00:00
|
|
|
|
|
|
|
/* if we made it this far something is wrong */
|
|
|
|
if (*skip != '\0')
|
2012-04-21 20:02:33 +00:00
|
|
|
printf("%li: Invalid statement %s, expression, or decleration\n", line, skip);
|
2012-04-17 20:24:22 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
mem_d(data);
|
|
|
|
line ++;
|
|
|
|
}
|
2012-04-19 20:41:03 +00:00
|
|
|
asm_clear();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|