Add support for columns to LNOF files.

This commit is contained in:
Dale Weiler 2013-08-26 14:14:33 -04:00
parent 0e077c6e42
commit fc57fa4064
4 changed files with 88 additions and 68 deletions

35
code.c
View file

@ -44,19 +44,22 @@ typedef union {
#define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
#define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
void code_push_statement(code_t *code, prog_section_statement_t *stmt, int linenum)
void code_push_statement(code_t *code, prog_section_statement_t *stmt, lex_ctx_t ctx)
{
vec_push(code->statements, *stmt);
vec_push(code->linenums, linenum);
vec_push(code->linenums, (int)ctx.line);
vec_push(code->columnnums, (int)ctx.column);
}
void code_pop_statement(code_t *code)
{
vec_pop(code->statements);
vec_pop(code->linenums);
vec_pop(code->columnnums);
}
code_t *code_init() {
static lex_ctx_t empty_ctx = {0, 0, 0};
static prog_section_function_t empty_function = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}};
static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
static prog_section_def_t empty_def = {0, 0, 0};
@ -78,7 +81,7 @@ code_t *code_init() {
vec_push(code->chars, '\0');
vec_push(code->functions, empty_function);
code_push_statement(code, &empty_statement, 0);
code_push_statement(code, &empty_statement, empty_ctx);
vec_push(code->defs, empty_def);
vec_push(code->fields, empty_def);
@ -137,7 +140,8 @@ static size_t code_size_generic(code_t *code, prog_header_t *code_header, bool l
size += sizeof(code_header->globals.length);
size += sizeof(code_header->fields.length);
size += sizeof(code_header->statements.length);
size += sizeof(code->linenums[0]) * vec_size(code->linenums);
size += sizeof(code->linenums[0]) * vec_size(code->linenums);
size += sizeof(code->columnnums[0]) * vec_size(code->columnnums);
} else {
size += sizeof(prog_header_t);
size += sizeof(prog_section_statement_t) * vec_size(code->statements);
@ -311,6 +315,7 @@ static bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, u
vec_free(code->statements);
vec_free(code->linenums);
vec_free(code->columnnums);
vec_free(code->defs);
vec_free(code->fields);
vec_free(code->functions);
@ -339,17 +344,18 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) {
if (!fp)
return false;
util_endianswap(&version, 1, sizeof(version));
util_endianswap(code->linenums, vec_size(code->linenums), sizeof(code->linenums[0]));
util_endianswap(&version, 1, sizeof(version));
util_endianswap(code->linenums, vec_size(code->linenums), sizeof(code->linenums[0]));
util_endianswap(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0]));
if (fs_file_write("LNOF", 4, 1, fp) != 1 ||
fs_file_write(&version, sizeof(version), 1, fp) != 1 ||
fs_file_write(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
fs_file_write(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
fs_file_write(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
fs_file_write(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
fs_file_write(code->linenums, sizeof(code->linenums[0]), vec_size(code->linenums), fp) != vec_size(code->linenums))
if (fs_file_write("LNOF", 4, 1, fp) != 1 ||
fs_file_write(&version, sizeof(version), 1, fp) != 1 ||
fs_file_write(&code_header.defs.length, sizeof(code_header.defs.length), 1, fp) != 1 ||
fs_file_write(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 ||
fs_file_write(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 ||
fs_file_write(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 ||
fs_file_write(code->linenums, sizeof(code->linenums[0]), vec_size(code->linenums), fp) != vec_size(code->linenums) ||
fs_file_write(code->columnnums, sizeof(code->columnnums[0]), vec_size(code->columnnums), fp) != vec_size(code->columnnums))
{
con_err("failed to write lno file\n");
}
@ -436,6 +442,7 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) {
void code_cleanup(code_t *code) {
vec_free(code->statements);
vec_free(code->linenums);
vec_free(code->columnnums);
vec_free(code->defs);
vec_free(code->fields);
vec_free(code->functions);

22
gmqcc.h
View file

@ -740,6 +740,7 @@ typedef uint32_t qcuint_t;
typedef struct {
prog_section_statement_t *statements;
int *linenums;
int *columnnums;
prog_section_def_t *defs;
prog_section_field_t *fields;
prog_section_function_t *functions;
@ -751,6 +752,16 @@ typedef struct {
qcint_t string_cached_empty;
} code_t;
/*
* A shallow copy of a lex_file to remember where which ast node
* came from.
*/
typedef struct {
const char *file;
size_t line;
size_t column;
} lex_ctx_t;
/*
* code_write -- writes out the compiled file
* code_init -- prepares the code file
@ -765,18 +776,9 @@ code_t *code_init (void);
void code_cleanup (code_t *);
uint32_t code_genstring (code_t *, const char *string);
qcint_t code_alloc_field (code_t *, size_t qcsize);
void code_push_statement(code_t *, prog_section_statement_t *stmt, int linenum);
void code_push_statement(code_t *, prog_section_statement_t *stmt, lex_ctx_t ctx);
void code_pop_statement (code_t *);
/*
* A shallow copy of a lex_file to remember where which ast node
* came from.
*/
typedef struct {
const char *file;
size_t line;
size_t column;
} lex_ctx_t;
/*===================================================================*/
/*============================ con.c ================================*/

94
ir.c
View file

@ -2822,7 +2822,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o2.s1 = 0;
stmt.o3.s1 = 0;
if (stmt.o1.s1 != 1)
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* no further instructions can be in this block */
return true;
@ -2833,17 +2833,17 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
stmt.opcode = INSTR_BITAND;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
stmt.opcode = INSTR_SUB_F;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2854,15 +2854,15 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o2.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o2.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2873,15 +2873,15 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o2.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o2.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2893,18 +2893,18 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + j;
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]) + j;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
stmt.opcode = INSTR_BITAND;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + j;
stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]) + j;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
stmt.opcode = INSTR_SUB_V;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2915,13 +2915,13 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2932,13 +2932,13 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
++stmt.o1.s1;
++stmt.o3.s1;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2950,18 +2950,18 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]) + j;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
stmt.opcode = INSTR_BITAND;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]) + j;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
stmt.opcode = INSTR_SUB_V;
stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* instruction generated */
continue;
@ -2982,13 +2982,13 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.opcode = INSTR_IF;
stmt.o2.s1 = (ontrue->code_start) - vec_size(code->statements);
if (stmt.o2.s1 != 1)
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
if (onfalse->generated) {
stmt.opcode = INSTR_IFNOT;
stmt.o2.s1 = (onfalse->code_start) - vec_size(code->statements);
if (stmt.o2.s1 != 1)
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
if (!ontrue->generated) {
if (onfalse->generated)
@ -3008,7 +3008,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
ontrue = tmp;
}
stidx = vec_size(code->statements);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
/* on false we jump, so add ontrue-path */
if (!gen_blocks_recursive(code, func, ontrue))
return false;
@ -3040,7 +3040,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o2.s1 = 0;
stmt.o3.s1 = 0;
if (stmt.o1.s1 != 1)
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
return true;
}
else if (stidx+2 == vec_size(code->statements) && code->statements[stidx].o2.s1 == 1) {
@ -3079,7 +3079,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.opcode = type_store_instr[param->vtype];
stmt.o1.u1 = ir_value_code_addr(param);
stmt.o2.u1 = OFS_PARM0 + 3 * p;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
/* Now handle extparams */
first = vec_size(instr->params);
@ -3108,7 +3108,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.opcode = type_store_instr[param->vtype];
stmt.o1.u1 = ir_value_code_addr(param);
stmt.o2.u1 = ir_value_code_addr(targetparam);
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
@ -3117,7 +3117,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
stmt.o2.u1 = 0;
stmt.o3.u1 = 0;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
retvalue = instr->_ops[0];
if (retvalue && retvalue->store != store_return &&
@ -3131,7 +3131,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
stmt.o1.u1 = OFS_RETURN;
stmt.o2.u1 = ir_value_code_addr(retvalue);
stmt.o3.u1 = 0;
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
continue;
}
@ -3181,7 +3181,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
}
}
code_push_statement(code, &stmt, instr->context.line);
code_push_statement(code, &stmt, instr->context);
}
return true;
}
@ -3218,11 +3218,16 @@ static bool gen_function_code(code_t *code, ir_function *self)
retst->opcode = INSTR_DONE;
++opts_optimizationcount[OPTIM_VOID_RETURN];
} else {
lex_ctx_t last;
stmt.opcode = INSTR_DONE;
stmt.o1.u1 = 0;
stmt.o2.u1 = 0;
stmt.o3.u1 = 0;
code_push_statement(code, &stmt, vec_last(code->linenums));
stmt.o1.u1 = 0;
stmt.o2.u1 = 0;
stmt.o3.u1 = 0;
last.line = vec_last(code->linenums);
last.column = vec_last(code->columnnums);
code_push_statement(code, &stmt, last);
}
return true;
}
@ -3355,7 +3360,7 @@ static bool gen_function_extparam_copy(code_t *code, ir_function *self)
}
stmt.o1.u1 = ir_value_code_addr(ep);
stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
code_push_statement(code, &stmt, self->context.line);
code_push_statement(code, &stmt, self->context);
}
return true;
@ -3380,7 +3385,7 @@ static bool gen_function_varargs_copy(code_t *code, ir_function *self)
if (i < 8) {
stmt.o1.u1 = OFS_PARM0 + 3*i;
stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
code_push_statement(code, &stmt, self->context.line);
code_push_statement(code, &stmt, self->context);
continue;
}
ext = i - 8;
@ -3391,7 +3396,7 @@ static bool gen_function_varargs_copy(code_t *code, ir_function *self)
stmt.o1.u1 = ir_value_code_addr(ep);
stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
code_push_statement(code, &stmt, self->context.line);
code_push_statement(code, &stmt, self->context);
}
return true;
@ -3876,11 +3881,16 @@ bool ir_builder_generate(ir_builder *self, const char *filename)
/* DP errors if the last instruction is not an INSTR_DONE. */
if (vec_last(self->code->statements).opcode != INSTR_DONE)
{
lex_ctx_t last;
stmt.opcode = INSTR_DONE;
stmt.o1.u1 = 0;
stmt.o2.u1 = 0;
stmt.o3.u1 = 0;
code_push_statement(self->code, &stmt, vec_last(self->code->linenums));
stmt.o1.u1 = 0;
stmt.o2.u1 = 0;
stmt.o3.u1 = 0;
last.line = vec_last(self->code->linenums);
last.column = vec_last(self->code->columnnums);
code_push_statement(self->code, &stmt, last);
}
if (OPTS_OPTION_BOOL(OPTION_PP_ONLY))

5
test.c
View file

@ -184,7 +184,7 @@ static int task_pclose(FILE **handles) {
return open->handles;
}
static void task_pclose(FILE **files) {
static int task_pclose(FILE **files) {
popen_t *open = ((popen_t*)files);
fs_file_close(files[1]);
fs_file_close(files[2]);
@ -192,6 +192,8 @@ static int task_pclose(FILE **handles) {
remove(open->name_out);
mem_d(open);
return EXIT_SUCCESS;
}
# define popen _popen
# define pclose _pclose
@ -1424,6 +1426,5 @@ int main(int argc, char **argv) {
succeed = test_perform("tests", defs);
stat_info();
return (succeed) ? EXIT_SUCCESS : EXIT_FAILURE;
}