mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-03-25 03:51:26 +00:00
Starting assembly
This commit is contained in:
parent
36ff87c130
commit
857cb72a94
3 changed files with 141 additions and 178 deletions
222
assembler.c
222
assembler.c
|
@ -21,84 +21,6 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
#include "gmqcc.h"
|
||||
/*
|
||||
* This is the assembler, gmqas, this is being implemented because I'm
|
||||
* not exactly sure how codegen would work for the C compiler as of yet
|
||||
* and also I plan to allow inline assembly for the compiler.
|
||||
*/
|
||||
static const struct {
|
||||
const char *m; /* menomic */
|
||||
const size_t o; /* operands */
|
||||
const size_t l; /* menomic len */
|
||||
} const asm_instr[] = {
|
||||
[INSTR_DONE] = { "DONE" , 1, 4 },
|
||||
[INSTR_MUL_F] = { "MUL_F" , 3, 5 },
|
||||
[INSTR_MUL_V] = { "MUL_V" , 3, 5 },
|
||||
[INSTR_MUL_FV] = { "MUL_FV" , 3, 6 },
|
||||
[INSTR_MUL_VF] = { "MUL_VF" , 3, 6 },
|
||||
[INSTR_DIV_F] = { "DIV" , 0, 3 },
|
||||
[INSTR_ADD_F] = { "ADD_F" , 3, 5 },
|
||||
[INSTR_ADD_V] = { "ADD_V" , 3, 5 },
|
||||
[INSTR_SUB_F] = { "SUB_F" , 3, 5 },
|
||||
[INSTR_SUB_V] = { "DUB_V" , 3, 5 },
|
||||
[INSTR_EQ_F] = { "EQ_F" , 0, 4 },
|
||||
[INSTR_EQ_V] = { "EQ_V" , 0, 4 },
|
||||
[INSTR_EQ_S] = { "EQ_S" , 0, 4 },
|
||||
[INSTR_EQ_E] = { "EQ_E" , 0, 4 },
|
||||
[INSTR_EQ_FNC] = { "ES_FNC" , 0, 6 },
|
||||
[INSTR_NE_F] = { "NE_F" , 0, 4 },
|
||||
[INSTR_NE_V] = { "NE_V" , 0, 4 },
|
||||
[INSTR_NE_S] = { "NE_S" , 0, 4 },
|
||||
[INSTR_NE_E] = { "NE_E" , 0, 4 },
|
||||
[INSTR_NE_FNC] = { "NE_FNC" , 0, 6 },
|
||||
[INSTR_LE] = { "LE" , 0, 2 },
|
||||
[INSTR_GE] = { "GE" , 0, 2 },
|
||||
[INSTR_LT] = { "LT" , 0, 2 },
|
||||
[INSTR_GT] = { "GT" , 0, 2 },
|
||||
[INSTR_LOAD_F] = { "FIELD_F" , 0, 7 },
|
||||
[INSTR_LOAD_V] = { "FIELD_V" , 0, 7 },
|
||||
[INSTR_LOAD_S] = { "FIELD_S" , 0, 7 },
|
||||
[INSTR_LOAD_ENT] = { "FIELD_ENT" , 0, 9 },
|
||||
[INSTR_LOAD_FLD] = { "FIELD_FLD" , 0, 9 },
|
||||
[INSTR_LOAD_FNC] = { "FIELD_FNC" , 0, 9 },
|
||||
[INSTR_ADDRESS] = { "ADDRESS" , 0, 7 },
|
||||
[INSTR_STORE_F] = { "STORE_F" , 0, 7 },
|
||||
[INSTR_STORE_V] = { "STORE_V" , 0, 7 },
|
||||
[INSTR_STORE_S] = { "STORE_S" , 0, 7 },
|
||||
[INSTR_STORE_ENT] = { "STORE_ENT" , 0, 9 },
|
||||
[INSTR_STORE_FLD] = { "STORE_FLD" , 0, 9 },
|
||||
[INSTR_STORE_FNC] = { "STORE_FNC" , 0, 9 },
|
||||
[INSTR_STOREP_F] = { "STOREP_F" , 0, 8 },
|
||||
[INSTR_STOREP_V] = { "STOREP_V" , 0, 8 },
|
||||
[INSTR_STOREP_S] = { "STOREP_S" , 0, 8 },
|
||||
[INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
|
||||
[INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
|
||||
[INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
|
||||
[INSTR_RETURN] = { "RETURN" , 0, 6 },
|
||||
[INSTR_NOT_F] = { "NOT_F" , 0, 5 },
|
||||
[INSTR_NOT_V] = { "NOT_V" , 0, 5 },
|
||||
[INSTR_NOT_S] = { "NOT_S" , 0, 5 },
|
||||
[INSTR_NOT_ENT] = { "NOT_ENT" , 0, 7 },
|
||||
[INSTR_NOT_FNC] = { "NOT_FNC" , 0, 7 },
|
||||
[INSTR_IF] = { "IF" , 0, 2 },
|
||||
[INSTR_IFNOT] = { "IFNOT" , 0, 5 },
|
||||
[INSTR_CALL0] = { "CALL0" , 0, 5 },
|
||||
[INSTR_CALL1] = { "CALL1" , 0, 5 },
|
||||
[INSTR_CALL2] = { "CALL2" , 0, 5 },
|
||||
[INSTR_CALL3] = { "CALL3" , 0, 5 },
|
||||
[INSTR_CALL4] = { "CALL4" , 0, 5 },
|
||||
[INSTR_CALL5] = { "CALL5" , 0, 5 },
|
||||
[INSTR_CALL6] = { "CALL6" , 0, 5 },
|
||||
[INSTR_CALL7] = { "CALL7" , 0, 5 },
|
||||
[INSTR_CALL8] = { "CALL8" , 0, 5 },
|
||||
[INSTR_STATE] = { "STATE" , 0, 5 },
|
||||
[INSTR_GOTO] = { "GOTO" , 0, 4 },
|
||||
[INSTR_AND] = { "AND" , 0, 3 },
|
||||
[INSTR_OR] = { "OR" , 0, 2 },
|
||||
[INSTR_BITAND] = { "BITAND" , 0, 6 },
|
||||
[INSTR_BITOR] = { "BITOR" , 0, 5 }
|
||||
};
|
||||
|
||||
/*
|
||||
* Some assembler keywords not part of the opcodes above: these are
|
||||
* for creating functions, or constants.
|
||||
|
@ -162,6 +84,31 @@ void asm_clear() {
|
|||
mem_d(assembly_constants_data);
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
void asm_parse(FILE *fp) {
|
||||
char *data = NULL;
|
||||
char *skip = NULL;
|
||||
|
@ -169,116 +116,44 @@ void asm_parse(FILE *fp) {
|
|||
size_t size = 0; /* size of line */
|
||||
asm_state state = ASM_NULL;
|
||||
|
||||
while ((data = asm_getline(&size, fp)) != NULL) {
|
||||
skip = data;
|
||||
asm_skipwhite(skip);
|
||||
asm_rmnewline(skip, &size);
|
||||
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';
|
||||
|
||||
#define DECLTYPE(X, CODE) \
|
||||
if (!strncmp(X, skip, strlen(X))) { \
|
||||
if (skip[strlen(X)] != ':') { \
|
||||
printf("%li: Missing `:` after decltype\n",line); \
|
||||
exit (1); \
|
||||
} \
|
||||
skip += strlen(X)+1; \
|
||||
asm_skipwhite(skip); \
|
||||
if(!isalpha(*skip)) { \
|
||||
printf("%li: Invalid identififer: %s\n", line, skip); \
|
||||
exit (1); \
|
||||
} else { \
|
||||
size_t offset_code = code_statements_elements+1; \
|
||||
size_t offset_chars = code_chars_elements +1; \
|
||||
size_t offset_globals = code_globals_elements +1; \
|
||||
size_t offset_functions = code_functions_elements +1; \
|
||||
size_t offset_fields = code_fields_elements +1; \
|
||||
size_t offset_defs = code_defs_elements +1; \
|
||||
CODE \
|
||||
/* silent unused warnings */ \
|
||||
(void)offset_code; \
|
||||
(void)offset_chars; \
|
||||
(void)offset_globals; \
|
||||
(void)offset_functions; \
|
||||
(void)offset_fields; \
|
||||
(void)offset_defs; \
|
||||
assembly_constants_add((globals){ \
|
||||
.name = util_strdup("empty"), \
|
||||
.offset = offset_globals \
|
||||
}); \
|
||||
} \
|
||||
goto end; \
|
||||
}
|
||||
|
||||
/* FLOAT */
|
||||
DECLTYPE(asm_keys[0], {
|
||||
//util_debug("ASM", "Constant FLOAT\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_FLOAT,
|
||||
.offset = offset_globals, /* global table */
|
||||
.name = offset_chars /* string table TODO */
|
||||
});
|
||||
float f = 0; /*TODO*/
|
||||
code_globals_add(*(int*)&f);
|
||||
|
||||
});
|
||||
/* VECTOR */
|
||||
DECLTYPE(asm_keys[1], {
|
||||
//util_debug("ASM", "Constant VECTOR\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_FLOAT,
|
||||
.offset = offset_globals, /* global table */
|
||||
.name = offset_chars /* string table TODO */
|
||||
});
|
||||
float f1 = 0;
|
||||
float f2 = 0;
|
||||
float f3 = 0;
|
||||
code_globals_add(*(int*)&f1);
|
||||
code_globals_add(*(int*)&f2);
|
||||
code_globals_add(*(int*)&f3);
|
||||
});
|
||||
/* ENTITY */ DECLTYPE(asm_keys[2], {util_debug("ASM", "Constant ENTITY\n");});
|
||||
/* FIELD */ DECLTYPE(asm_keys[3], {util_debug("ASM", "Constant FIELD\n");});
|
||||
/* STRING */
|
||||
DECLTYPE(asm_keys[4], {
|
||||
//util_debug("ASM", "Constant STRING\n");
|
||||
code_defs_add((prog_section_def){
|
||||
.type = TYPE_STRING,
|
||||
.offset = offset_globals, /* offset to offset in string table (for data)*/
|
||||
.name = offset_chars /* location of name in string table (for name)*/
|
||||
});
|
||||
code_strings_add("hello world");
|
||||
});
|
||||
/* FUNCTION */
|
||||
DECLTYPE(asm_keys[5], {
|
||||
//util_debug("ASM", "Constant FUNCTION\n");
|
||||
/* TODO: parse */
|
||||
if (asm_parsetype(asm_keys[5], &skip, line)) {
|
||||
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,
|
||||
.offset = offset_globals,
|
||||
.name = offset_chars
|
||||
.offset = code_globals_elements,
|
||||
.name = code_chars_elements
|
||||
});
|
||||
code_globals_add(offset_functions);
|
||||
code_functions_add((prog_section_function){
|
||||
.entry = offset_code,
|
||||
code_globals_add(code_functions_elements);
|
||||
code_functions_add((prog_section_function) {
|
||||
.entry = code_statements_elements,
|
||||
.firstlocal = 0,
|
||||
.locals = 0,
|
||||
.profile = 0,
|
||||
.name = offset_chars,
|
||||
.name = code_chars_elements,
|
||||
.file = 0,
|
||||
.nargs = 0,
|
||||
.argsize = {0}
|
||||
});
|
||||
});
|
||||
|
||||
code_strings_add(skip);
|
||||
};
|
||||
|
||||
#if 0
|
||||
/* if we make it this far then we have statements */
|
||||
{
|
||||
size_t i = 0; /* counter */
|
||||
size_t o = 0; /* operands */
|
||||
size_t c = 0; /* copy */
|
||||
char *t = NULL; /* token */
|
||||
|
||||
/*
|
||||
|
@ -300,6 +175,7 @@ void asm_parse(FILE *fp) {
|
|||
}
|
||||
|
||||
/* parse the statement */
|
||||
c = i;
|
||||
o = asm_instr[i].o; /* operands */
|
||||
skip += asm_instr[i].l; /* skip instruction */
|
||||
t = strtok(skip, " ,");
|
||||
|
@ -310,10 +186,6 @@ void asm_parse(FILE *fp) {
|
|||
i ++;
|
||||
}
|
||||
|
||||
// util_debug("ASM", "Operand 1: %s\n", util_strrnl(op[0]));
|
||||
// util_debug("ASM", "Operand 2: %s\n", util_strrnl(op[1]));
|
||||
// util_debug("ASM", "Operand 3: %s\n", util_strrnl(op[2]));
|
||||
|
||||
/* check */
|
||||
if (i != o) {
|
||||
printf("not enough operands, expected: %li, got %li\n", o, i);
|
||||
|
@ -321,12 +193,16 @@ void asm_parse(FILE *fp) {
|
|||
|
||||
/* TODO: hashtable value LOAD .... etc */
|
||||
code_statements_add((prog_section_statement){
|
||||
i, {0}, {0}, {0}
|
||||
c,
|
||||
{ atof(op[0]) },
|
||||
{ atof(op[1]) },
|
||||
{ atof(op[2]) }
|
||||
});
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* if we made it this far something is wrong */
|
||||
if (*skip != '\0')
|
||||
|
|
24
code.c
24
code.c
|
@ -164,7 +164,7 @@ void code_write() {
|
|||
fwrite(code_globals_data, 1, sizeof(int) *code_globals_elements, fp);
|
||||
fwrite(code_chars_data, 1, 1 *code_chars_elements, fp);
|
||||
|
||||
util_debug("GEN","header:\n");
|
||||
util_debug("GEN","HEADER:\n");
|
||||
util_debug("GEN"," version: = %d\n", code_header.version );
|
||||
util_debug("GEN"," crc16: = %d\n", code_header.crc16 );
|
||||
util_debug("GEN"," entfield: = %d\n", code_header.entfield);
|
||||
|
@ -175,10 +175,11 @@ void code_write() {
|
|||
util_debug("GEN"," globals = {.offset = % 8d, .length = % 8d}\n", code_header.globals .offset, code_header.globals .length);
|
||||
util_debug("GEN"," strings = {.offset = % 8d, .length = % 8d}\n", code_header.strings .offset, code_header.strings .length);
|
||||
|
||||
/* WRITE out all functions */
|
||||
util_debug("GEN", "functions:\n");
|
||||
size_t i = 0;
|
||||
/* FUNCTIONS */
|
||||
util_debug("GEN", "FUNCTIONS:\n");
|
||||
size_t i = 1;
|
||||
for (; i < code_functions_elements; i++) {
|
||||
size_t j = code_functions_data[i].entry;
|
||||
util_debug("GEN", " {.entry =% 5d, .firstlocal =% 5d, .locals =% 5d, .profile =% 5d, .name =% 5d, .file =% 5d, .nargs =% 5d, .argsize =%0X }\n",
|
||||
code_functions_data[i].entry,
|
||||
code_functions_data[i].firstlocal,
|
||||
|
@ -189,6 +190,20 @@ void code_write() {
|
|||
code_functions_data[i].nargs,
|
||||
*((int32_t*)&code_functions_data[i].argsize)
|
||||
);
|
||||
util_debug("GEN", " NAME: %s\n", &code_chars_data[code_functions_data[i].name]);
|
||||
util_debug("GEN", " CODE:\n");
|
||||
for (;;) {
|
||||
if (code_statements_data[j].opcode != INSTR_DONE &&
|
||||
code_statements_data[j].opcode != INSTR_RETURN)
|
||||
util_debug("GEN", " %s {0x%05d,0x%05d,0x%05d}\n",
|
||||
asm_instr[code_statements_data[j].opcode].m,
|
||||
code_statements_data[j].s1,
|
||||
code_statements_data[j].s2,
|
||||
code_statements_data[j].s3
|
||||
);
|
||||
else break;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
mem_d(code_statements_data);
|
||||
|
@ -197,6 +212,5 @@ void code_write() {
|
|||
mem_d(code_functions_data);
|
||||
mem_d(code_globals_data);
|
||||
mem_d(code_chars_data);
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
|
73
gmqcc.h
73
gmqcc.h
|
@ -395,6 +395,79 @@ void code_init ();
|
|||
//===================================================================
|
||||
//========================= assembler.c =============================
|
||||
//===================================================================
|
||||
static const struct {
|
||||
const char *m; /* menomic */
|
||||
const size_t o; /* operands */
|
||||
const size_t l; /* menomic len */
|
||||
} const asm_instr[] = {
|
||||
[INSTR_DONE] = { "DONE" , 1, 4 },
|
||||
[INSTR_MUL_F] = { "MUL_F" , 3, 5 },
|
||||
[INSTR_MUL_V] = { "MUL_V" , 3, 5 },
|
||||
[INSTR_MUL_FV] = { "MUL_FV" , 3, 6 },
|
||||
[INSTR_MUL_VF] = { "MUL_VF" , 3, 6 },
|
||||
[INSTR_DIV_F] = { "DIV" , 0, 3 },
|
||||
[INSTR_ADD_F] = { "ADD_F" , 3, 5 },
|
||||
[INSTR_ADD_V] = { "ADD_V" , 3, 5 },
|
||||
[INSTR_SUB_F] = { "SUB_F" , 3, 5 },
|
||||
[INSTR_SUB_V] = { "DUB_V" , 3, 5 },
|
||||
[INSTR_EQ_F] = { "EQ_F" , 0, 4 },
|
||||
[INSTR_EQ_V] = { "EQ_V" , 0, 4 },
|
||||
[INSTR_EQ_S] = { "EQ_S" , 0, 4 },
|
||||
[INSTR_EQ_E] = { "EQ_E" , 0, 4 },
|
||||
[INSTR_EQ_FNC] = { "ES_FNC" , 0, 6 },
|
||||
[INSTR_NE_F] = { "NE_F" , 0, 4 },
|
||||
[INSTR_NE_V] = { "NE_V" , 0, 4 },
|
||||
[INSTR_NE_S] = { "NE_S" , 0, 4 },
|
||||
[INSTR_NE_E] = { "NE_E" , 0, 4 },
|
||||
[INSTR_NE_FNC] = { "NE_FNC" , 0, 6 },
|
||||
[INSTR_LE] = { "LE" , 0, 2 },
|
||||
[INSTR_GE] = { "GE" , 0, 2 },
|
||||
[INSTR_LT] = { "LT" , 0, 2 },
|
||||
[INSTR_GT] = { "GT" , 0, 2 },
|
||||
[INSTR_LOAD_F] = { "FIELD_F" , 0, 7 },
|
||||
[INSTR_LOAD_V] = { "FIELD_V" , 0, 7 },
|
||||
[INSTR_LOAD_S] = { "FIELD_S" , 0, 7 },
|
||||
[INSTR_LOAD_ENT] = { "FIELD_ENT" , 0, 9 },
|
||||
[INSTR_LOAD_FLD] = { "FIELD_FLD" , 0, 9 },
|
||||
[INSTR_LOAD_FNC] = { "FIELD_FNC" , 0, 9 },
|
||||
[INSTR_ADDRESS] = { "ADDRESS" , 0, 7 },
|
||||
[INSTR_STORE_F] = { "STORE_F" , 0, 7 },
|
||||
[INSTR_STORE_V] = { "STORE_V" , 0, 7 },
|
||||
[INSTR_STORE_S] = { "STORE_S" , 0, 7 },
|
||||
[INSTR_STORE_ENT] = { "STORE_ENT" , 0, 9 },
|
||||
[INSTR_STORE_FLD] = { "STORE_FLD" , 0, 9 },
|
||||
[INSTR_STORE_FNC] = { "STORE_FNC" , 0, 9 },
|
||||
[INSTR_STOREP_F] = { "STOREP_F" , 0, 8 },
|
||||
[INSTR_STOREP_V] = { "STOREP_V" , 0, 8 },
|
||||
[INSTR_STOREP_S] = { "STOREP_S" , 0, 8 },
|
||||
[INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
|
||||
[INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
|
||||
[INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
|
||||
[INSTR_RETURN] = { "RETURN" , 0, 6 },
|
||||
[INSTR_NOT_F] = { "NOT_F" , 0, 5 },
|
||||
[INSTR_NOT_V] = { "NOT_V" , 0, 5 },
|
||||
[INSTR_NOT_S] = { "NOT_S" , 0, 5 },
|
||||
[INSTR_NOT_ENT] = { "NOT_ENT" , 0, 7 },
|
||||
[INSTR_NOT_FNC] = { "NOT_FNC" , 0, 7 },
|
||||
[INSTR_IF] = { "IF" , 0, 2 },
|
||||
[INSTR_IFNOT] = { "IFNOT" , 0, 5 },
|
||||
[INSTR_CALL0] = { "CALL0" , 0, 5 },
|
||||
[INSTR_CALL1] = { "CALL1" , 0, 5 },
|
||||
[INSTR_CALL2] = { "CALL2" , 0, 5 },
|
||||
[INSTR_CALL3] = { "CALL3" , 0, 5 },
|
||||
[INSTR_CALL4] = { "CALL4" , 0, 5 },
|
||||
[INSTR_CALL5] = { "CALL5" , 0, 5 },
|
||||
[INSTR_CALL6] = { "CALL6" , 0, 5 },
|
||||
[INSTR_CALL7] = { "CALL7" , 0, 5 },
|
||||
[INSTR_CALL8] = { "CALL8" , 0, 5 },
|
||||
[INSTR_STATE] = { "STATE" , 0, 5 },
|
||||
[INSTR_GOTO] = { "GOTO" , 0, 4 },
|
||||
[INSTR_AND] = { "AND" , 0, 3 },
|
||||
[INSTR_OR] = { "OR" , 0, 2 },
|
||||
[INSTR_BITAND] = { "BITAND" , 0, 6 },
|
||||
[INSTR_BITOR] = { "BITOR" , 0, 5 }
|
||||
};
|
||||
|
||||
void asm_init (const char *, FILE **);
|
||||
void asm_close(FILE *);
|
||||
void asm_parse(FILE *);
|
||||
|
|
Loading…
Reference in a new issue