More function parsing for assembler

This commit is contained in:
Dale Weiler 2012-05-02 11:48:46 -04:00
parent ca7ab3fd5f
commit bc318bb68e
3 changed files with 74 additions and 61 deletions

34
asm.c
View file

@ -250,6 +250,7 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
*/ */
function.entry = -atoi(find); function.entry = -atoi(find);
function.firstlocal = 0; function.firstlocal = 0;
function.locals = 0;
function.profile = 0; function.profile = 0;
function.name = code_chars_elements; function.name = code_chars_elements;
function.file = 0; function.file = 0;
@ -257,9 +258,9 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
def.type = TYPE_FUNCTION; def.type = TYPE_FUNCTION;
def.offset = code_globals_elements; def.offset = code_globals_elements;
def.name = code_chars_elements; def.name = code_chars_elements;
memset(function.argsize, 0, sizeof(function.argsize));
code_functions_add(function); code_functions_add(function);
code_defs_add (def); code_defs_add (def);
code_globals_add (code_chars_elements);
code_chars_put (name, strlen(name)); code_chars_put (name, strlen(name));
code_chars_add ('\0'); code_chars_add ('\0');
@ -327,15 +328,31 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
} else { } else {
printf("missing number of argument count in function %s\n", name); printf("missing number of argument count in function %s\n", name);
} }
/* terminate name inspot */
*--peek='\0'; /*
* Now we need to strip the name apart into it's exact size
* by working in the peek buffer till we hit the name again.
*/
if (*peek == '#') {
peek --; /* '#' */
peek --; /* number */
}
while (*peek == ' ' || *peek == '\t') peek--;
/*
* We're guranteed to be exactly where we need to be in the
* peek buffer to null terminate and get our name from name
* without any garbage before or after it.
*/
*++peek='\0';
/* /*
* We got valid function structure information now. Lets add * We got valid function structure information now. Lets add
* the function to the code writer function table. * the function to the code writer function table.
*/ */
function.entry = code_statements_elements; function.entry = code_statements_elements-1;
function.firstlocal = 0; function.firstlocal = 0;
function.locals = 0;
function.profile = 0; function.profile = 0;
function.name = code_chars_elements; function.name = code_chars_elements;
function.file = 0; function.file = 0;
@ -343,13 +360,14 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
def.type = TYPE_FUNCTION; def.type = TYPE_FUNCTION;
def.offset = code_globals_elements; def.offset = code_globals_elements;
def.name = code_chars_elements; def.name = code_chars_elements;
memset(function.argsize, 0, sizeof(function.argsize));
code_functions_add(function); code_functions_add(function);
code_defs_add (def); code_globals_add(code_statements_elements);
code_globals_add (code_chars_elements);
code_chars_put (name, strlen(name)); code_chars_put (name, strlen(name));
code_chars_add ('\0'); code_chars_add ('\0');
/* update assembly state */ /* update assembly state */
*state = ASM_FUNCTION; *state = ASM_FUNCTION;
util_debug("ASM", "added context function %s to function table\n", name); util_debug("ASM", "added context function %s to function table\n", name);
} }
@ -485,6 +503,10 @@ void asm_parse(FILE *fp) {
char *copy = util_strsws(data); /* skip whitespace */ char *copy = util_strsws(data); /* skip whitespace */
skip = util_strrnl(copy); /* delete newline */ skip = util_strrnl(copy); /* delete newline */
/* TODO: statement END check */
if (state == ASM_FUNCTION)
state = ASM_NULL;
if (asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); } if (asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
if (asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); } if (asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
if (asm_parse_stmt(skip, line, &state)){ asm_end("asm_parse_stmt\n"); } if (asm_parse_stmt(skip, line, &state)){ asm_end("asm_parse_stmt\n"); }

70
code.c
View file

@ -28,8 +28,7 @@ typedef struct {
} prog_section; } prog_section;
typedef struct { typedef struct {
uint16_t version; /* Program version (6) */ uint32_t version; /* Program version (6) */
uint16_t flags; /* see propsal.txt */
uint16_t crc16; /* What is this? */ uint16_t crc16; /* What is this? */
uint16_t skip; /* see propsal.txt */ uint16_t skip; /* see propsal.txt */
@ -146,43 +145,26 @@ void code_test() {
} }
void code_write() { void code_write() {
prog_header code_header = {0}; prog_header code_header;
prog_section statements = {0};
prog_section defs = {0};
prog_section fields = {0};
prog_section functions = {0};
prog_section globals = {0};
prog_section strings = {0};
FILE *fp = NULL; FILE *fp = NULL;
size_t it = 1; size_t it = 1;
/* see proposal.txt */ /* see proposal.txt */
if (opts_omit_nullcode) { if (opts_omit_nullcode) {}
code_header.skip = 28; code_header.statements.offset = sizeof(prog_header);
code_header.flags = 1; code_header.statements.length = code_statements_elements;
} code_header.defs.offset = code_header.statements.offset + (sizeof(prog_section_statement) * code_statements_elements);
code_header.defs.length = code_defs_elements;
statements.offset = sizeof(prog_header); code_header.fields.offset = code_header.defs.offset + (sizeof(prog_section_def) * code_defs_elements);
statements.length = code_statements_elements; code_header.fields.length = code_fields_elements;
defs.offset = code_header.statements.offset + sizeof(prog_section_statement) * code_statements_elements; code_header.functions.offset = code_header.fields.offset + (sizeof(prog_section_field) * code_fields_elements);
defs.length = code_defs_elements; code_header.functions.length = code_functions_elements;
fields.offset = code_header.defs.offset + sizeof(prog_section_def) * code_defs_elements; code_header.globals.offset = code_header.functions.offset + (sizeof(prog_section_function) * code_functions_elements);
fields.length = code_fields_elements; code_header.globals.length = code_globals_elements;
functions.offset = code_header.fields.offset + sizeof(prog_section_field) * code_fields_elements; code_header.strings.offset = code_header.globals.offset + (sizeof(int32_t) * code_globals_elements);
functions.length = code_functions_elements; code_header.strings.length = code_chars_elements;
globals.offset = code_header.functions.offset + sizeof(prog_section_function) * code_functions_elements;
globals.length = code_globals_elements;
strings.offset = code_header.globals.offset + sizeof(int) * code_globals_elements;
strings.length = code_chars_elements;
code_header.version = 6; code_header.version = 6;
code_header.crc16 = 0; /* TODO: */ code_header.crc16 = 0; /* TODO: */
code_header.statements = statements;
code_header.defs = defs;
code_header.fields = fields;
code_header.functions = functions;
code_header.globals = globals;
code_header.strings = strings;
code_header.entfield = 0; /* TODO: */ code_header.entfield = 0; /* TODO: */
if (opts_darkplaces_stringtablebug) { if (opts_darkplaces_stringtablebug) {
@ -196,20 +178,20 @@ void code_write() {
/* ensure all data is in LE format */ /* ensure all data is in LE format */
util_endianswap(&code_header, 1, sizeof(prog_header)); util_endianswap(&code_header, 1, sizeof(prog_header));
util_endianswap (code_statements_data, code_statements_elements, sizeof(prog_section_statement)); util_endianswap(code_statements_data, code_statements_elements, sizeof(prog_section_statement));
util_endianswap (code_defs_data, code_defs_elements, sizeof(prog_section_def)); util_endianswap(code_defs_data, code_defs_elements, sizeof(prog_section_def));
util_endianswap (code_fields_data, code_fields_elements, sizeof(prog_section_field)); util_endianswap(code_fields_data, code_fields_elements, sizeof(prog_section_field));
util_endianswap (code_functions_data, code_functions_elements, sizeof(prog_section_function)); util_endianswap(code_functions_data, code_functions_elements, sizeof(prog_section_function));
util_endianswap (code_globals_data, code_globals_elements, sizeof(int)); util_endianswap(code_globals_data, code_globals_elements, sizeof(int32_t));
fp = fopen("program.dat", "wb"); fp = fopen("program.dat", "wb");
fwrite(&code_header, 1, sizeof(prog_header), fp); fwrite(&code_header, 1, sizeof(prog_header), fp);
fwrite (code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp); fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
fwrite (code_defs_data, 1, sizeof(prog_section_def) *code_defs_elements, fp); fwrite(code_defs_data, 1, sizeof(prog_section_def) *code_defs_elements, fp);
fwrite (code_fields_data, 1, sizeof(prog_section_field) *code_fields_elements, fp); fwrite(code_fields_data, 1, sizeof(prog_section_field) *code_fields_elements, fp);
fwrite (code_functions_data, 1, sizeof(prog_section_function) *code_functions_elements, fp); fwrite(code_functions_data, 1, sizeof(prog_section_function) *code_functions_elements, fp);
fwrite (code_globals_data, 1, sizeof(int) *code_globals_elements, fp); fwrite(code_globals_data, 1, sizeof(int32_t) *code_globals_elements, fp);
fwrite (code_chars_data, 1, 1 *code_chars_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"," version: = %d\n", code_header.version );

View file

@ -82,12 +82,21 @@ FUNCTION: pow, $97
FUNCTION: findfloat, $98 FUNCTION: findfloat, $98
FUNCTION: checkextension, $99 FUNCTION: checkextension, $99
; constants test
VECTOR: dude1, -1, +2, 38865.444
FLOAT: dude2, 1
STRING: "hello world"
FUNCTION: foo #8 ;code_chars_put("m_init", 0x6);
MUL_F arg1, arg2, arg3 ;code_chars_put("m_keydown", 0x9);
;code_chars_put("m_draw", 0x6);
;code_chars_put("m_toggle", 0x8);
;code_chars_put("m_shutdown", 0xA);
FUNCTION: m_init #1
DONE
FUNCTION: m_keydown #1
DONE
FUNCTION: m_draw #1
DONE
FUNCTION: m_toggle #1
DONE
FUNCTION: m_shutdown #1
DONE DONE