Use the _t consistency naming scheme. Also various cleanups.

This commit is contained in:
Dale Weiler 2013-07-30 16:00:51 +00:00
parent a9ab865add
commit d6ca5673dc
13 changed files with 453 additions and 459 deletions

48
ast.c
View file

@ -84,7 +84,7 @@ static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
} }
/* Initialize main ast node aprts */ /* Initialize main ast node aprts */
static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype) static void ast_node_init(ast_node *self, lex_ctx_t ctx, int nodetype)
{ {
self->context = ctx; self->context = ctx;
self->destroy = &_ast_node_destroy; self->destroy = &_ast_node_destroy;
@ -174,7 +174,7 @@ void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
} }
} }
static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype) static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
{ {
ast_instantiate(ast_expression, ctx, ast_expression_delete_full); ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
ast_expression_init(self, NULL); ast_expression_init(self, NULL);
@ -184,7 +184,7 @@ static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
return self; return self;
} }
ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex) ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
{ {
size_t i; size_t i;
const ast_expression *fromex; const ast_expression *fromex;
@ -342,7 +342,7 @@ void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
} }
static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out); static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
ast_value* ast_value_new(lex_ctx ctx, const char *name, int t) ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
{ {
ast_instantiate(ast_value, ctx, ast_value_delete); ast_instantiate(ast_value, ctx, ast_value_delete);
ast_expression_init((ast_expression*)self, ast_expression_init((ast_expression*)self,
@ -433,7 +433,7 @@ bool ast_value_set_name(ast_value *self, const char *name)
return !!self->name; return !!self->name;
} }
ast_binary* ast_binary_new(lex_ctx ctx, int op, ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
ast_expression* left, ast_expression* right) ast_expression* left, ast_expression* right)
{ {
ast_instantiate(ast_binary, ctx, ast_binary_delete); ast_instantiate(ast_binary, ctx, ast_binary_delete);
@ -478,7 +478,7 @@ void ast_binary_delete(ast_binary *self)
mem_d(self); mem_d(self);
} }
ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op, ast_binstore* ast_binstore_new(lex_ctx_t ctx, int storop, int op,
ast_expression* left, ast_expression* right) ast_expression* left, ast_expression* right)
{ {
ast_instantiate(ast_binstore, ctx, ast_binstore_delete); ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
@ -506,7 +506,7 @@ void ast_binstore_delete(ast_binstore *self)
mem_d(self); mem_d(self);
} }
ast_unary* ast_unary_new(lex_ctx ctx, int op, ast_unary* ast_unary_new(lex_ctx_t ctx, int op,
ast_expression *expr) ast_expression *expr)
{ {
ast_instantiate(ast_unary, ctx, ast_unary_delete); ast_instantiate(ast_unary, ctx, ast_unary_delete);
@ -532,7 +532,7 @@ void ast_unary_delete(ast_unary *self)
mem_d(self); mem_d(self);
} }
ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr) ast_return* ast_return_new(lex_ctx_t ctx, ast_expression *expr)
{ {
ast_instantiate(ast_return, ctx, ast_return_delete); ast_instantiate(ast_return, ctx, ast_return_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
@ -553,7 +553,7 @@ void ast_return_delete(ast_return *self)
mem_d(self); mem_d(self);
} }
ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field) ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
{ {
if (field->vtype != TYPE_FIELD) { if (field->vtype != TYPE_FIELD) {
compile_error(ctx, "ast_entfield_new with expression not of type field"); compile_error(ctx, "ast_entfield_new with expression not of type field");
@ -562,7 +562,7 @@ ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expressi
return ast_entfield_new_force(ctx, entity, field, field->next); return ast_entfield_new_force(ctx, entity, field, field->next);
} }
ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype) ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
{ {
ast_instantiate(ast_entfield, ctx, ast_entfield_delete); ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
@ -591,7 +591,7 @@ void ast_entfield_delete(ast_entfield *self)
mem_d(self); mem_d(self);
} }
ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name) ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name)
{ {
ast_instantiate(ast_member, ctx, ast_member_delete); ast_instantiate(ast_member, ctx, ast_member_delete);
if (field >= 3) { if (field >= 3) {
@ -653,7 +653,7 @@ bool ast_member_set_name(ast_member *self, const char *name)
return !!self->name; return !!self->name;
} }
ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index) ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
{ {
ast_expression *outtype; ast_expression *outtype;
ast_instantiate(ast_array_index, ctx, ast_array_index_delete); ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
@ -696,7 +696,7 @@ void ast_array_index_delete(ast_array_index *self)
mem_d(self); mem_d(self);
} }
ast_argpipe* ast_argpipe_new(lex_ctx ctx, ast_expression *index) ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index)
{ {
ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete); ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
@ -713,7 +713,7 @@ void ast_argpipe_delete(ast_argpipe *self)
mem_d(self); mem_d(self);
} }
ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse) ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
{ {
ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete); ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
if (!ontrue && !onfalse) { if (!ontrue && !onfalse) {
@ -746,7 +746,7 @@ void ast_ifthen_delete(ast_ifthen *self)
mem_d(self); mem_d(self);
} }
ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse) ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
{ {
ast_expression *exprtype = ontrue; ast_expression *exprtype = ontrue;
ast_instantiate(ast_ternary, ctx, ast_ternary_delete); ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
@ -783,7 +783,7 @@ void ast_ternary_delete(ast_ternary *self)
mem_d(self); mem_d(self);
} }
ast_loop* ast_loop_new(lex_ctx ctx, ast_loop* ast_loop_new(lex_ctx_t ctx,
ast_expression *initexpr, ast_expression *initexpr,
ast_expression *precond, bool pre_not, ast_expression *precond, bool pre_not,
ast_expression *postcond, bool post_not, ast_expression *postcond, bool post_not,
@ -832,7 +832,7 @@ void ast_loop_delete(ast_loop *self)
mem_d(self); mem_d(self);
} }
ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels) ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels)
{ {
ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete); ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
@ -849,7 +849,7 @@ void ast_breakcont_delete(ast_breakcont *self)
mem_d(self); mem_d(self);
} }
ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op) ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
{ {
ast_instantiate(ast_switch, ctx, ast_switch_delete); ast_instantiate(ast_switch, ctx, ast_switch_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
@ -878,7 +878,7 @@ void ast_switch_delete(ast_switch *self)
mem_d(self); mem_d(self);
} }
ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined) ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
{ {
ast_instantiate(ast_label, ctx, ast_label_delete); ast_instantiate(ast_label, ctx, ast_label_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
@ -906,7 +906,7 @@ static void ast_label_register_goto(ast_label *self, ast_goto *g)
vec_push(self->gotos, g); vec_push(self->gotos, g);
} }
ast_goto* ast_goto_new(lex_ctx ctx, const char *name) ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
{ {
ast_instantiate(ast_goto, ctx, ast_goto_delete); ast_instantiate(ast_goto, ctx, ast_goto_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen); ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
@ -930,7 +930,7 @@ void ast_goto_set_label(ast_goto *self, ast_label *label)
self->target = label; self->target = label;
} }
ast_call* ast_call_new(lex_ctx ctx, ast_call* ast_call_new(lex_ctx_t ctx,
ast_expression *funcexpr) ast_expression *funcexpr)
{ {
ast_instantiate(ast_call, ctx, ast_call_delete); ast_instantiate(ast_call, ctx, ast_call_delete);
@ -1069,7 +1069,7 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
return retval; return retval;
} }
ast_store* ast_store_new(lex_ctx ctx, int op, ast_store* ast_store_new(lex_ctx_t ctx, int op,
ast_expression *dest, ast_expression *source) ast_expression *dest, ast_expression *source)
{ {
ast_instantiate(ast_store, ctx, ast_store_delete); ast_instantiate(ast_store, ctx, ast_store_delete);
@ -1094,7 +1094,7 @@ void ast_store_delete(ast_store *self)
mem_d(self); mem_d(self);
} }
ast_block* ast_block_new(lex_ctx ctx) ast_block* ast_block_new(lex_ctx_t ctx)
{ {
ast_instantiate(ast_block, ctx, ast_block_delete); ast_instantiate(ast_block, ctx, ast_block_delete);
ast_expression_init((ast_expression*)self, ast_expression_init((ast_expression*)self,
@ -1148,7 +1148,7 @@ void ast_block_set_type(ast_block *self, ast_expression *from)
ast_type_adopt(self, from); ast_type_adopt(self, from);
} }
ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype) ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
{ {
ast_instantiate(ast_function, ctx, ast_function_delete); ast_instantiate(ast_function, ctx, ast_function_delete);

48
ast.h
View file

@ -87,7 +87,7 @@ enum {
typedef void ast_node_delete(ast_node*); typedef void ast_node_delete(ast_node*);
struct ast_node_common struct ast_node_common
{ {
lex_ctx context; lex_ctx_t context;
/* I don't feel comfortable using keywords like 'delete' as names... */ /* I don't feel comfortable using keywords like 'delete' as names... */
ast_node_delete *destroy; ast_node_delete *destroy;
int nodetype; int nodetype;
@ -170,7 +170,7 @@ struct ast_expression_common
typedef union { typedef union {
double vfloat; double vfloat;
int vint; int vint;
vector vvec; vec3_t vvec;
const char *vstring; const char *vstring;
int ventity; int ventity;
ast_function *vfunc; ast_function *vfunc;
@ -208,7 +208,7 @@ struct ast_value_s
ast_value *getter; ast_value *getter;
}; };
ast_value* ast_value_new(lex_ctx ctx, const char *name, int qctype); ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int qctype);
ast_value* ast_value_copy(const ast_value *self); ast_value* ast_value_copy(const ast_value *self);
/* This will NOT delete an underlying ast_function */ /* This will NOT delete an underlying ast_function */
void ast_value_delete(ast_value*); void ast_value_delete(ast_value*);
@ -225,7 +225,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield);
void ast_value_params_add(ast_value*, ast_value*); void ast_value_params_add(ast_value*, ast_value*);
bool ast_compare_type(ast_expression *a, ast_expression *b); bool ast_compare_type(ast_expression *a, ast_expression *b);
ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex); ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex);
#define ast_type_adopt(a, b) ast_type_adopt_impl((ast_expression*)(a), (ast_expression*)(b)) #define ast_type_adopt(a, b) ast_type_adopt_impl((ast_expression*)(a), (ast_expression*)(b))
void ast_type_adopt_impl(ast_expression *self, const ast_expression *other); void ast_type_adopt_impl(ast_expression *self, const ast_expression *other);
void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize); void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize);
@ -252,7 +252,7 @@ struct ast_binary_s
ast_binary_ref refs; ast_binary_ref refs;
}; };
ast_binary* ast_binary_new(lex_ctx ctx, ast_binary* ast_binary_new(lex_ctx_t ctx,
int op, int op,
ast_expression *left, ast_expression *left,
ast_expression *right); ast_expression *right);
@ -273,7 +273,7 @@ struct ast_binstore_s
/* for &~= which uses the destination in a binary in source we can use this */ /* for &~= which uses the destination in a binary in source we can use this */
bool keep_dest; bool keep_dest;
}; };
ast_binstore* ast_binstore_new(lex_ctx ctx, ast_binstore* ast_binstore_new(lex_ctx_t ctx,
int storeop, int storeop,
int op, int op,
ast_expression *left, ast_expression *left,
@ -290,7 +290,7 @@ struct ast_unary_s
int op; int op;
ast_expression *operand; ast_expression *operand;
}; };
ast_unary* ast_unary_new(lex_ctx ctx, ast_unary* ast_unary_new(lex_ctx_t ctx,
int op, int op,
ast_expression *expr); ast_expression *expr);
@ -305,7 +305,7 @@ struct ast_return_s
ast_expression expression; ast_expression expression;
ast_expression *operand; ast_expression *operand;
}; };
ast_return* ast_return_new(lex_ctx ctx, ast_return* ast_return_new(lex_ctx_t ctx,
ast_expression *expr); ast_expression *expr);
/* Entity-field /* Entity-field
@ -329,8 +329,8 @@ struct ast_entfield_s
/* As can the field, it just must result in a value of TYPE_FIELD */ /* As can the field, it just must result in a value of TYPE_FIELD */
ast_expression *field; ast_expression *field;
}; };
ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field); ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field);
ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype); ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype);
/* Member access: /* Member access:
* *
@ -345,7 +345,7 @@ struct ast_member_s
const char *name; const char *name;
bool rvalue; bool rvalue;
}; };
ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name); ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name);
void ast_member_delete(ast_member*); void ast_member_delete(ast_member*);
bool ast_member_set_name(ast_member*, const char *name); bool ast_member_set_name(ast_member*, const char *name);
@ -366,7 +366,7 @@ struct ast_array_index_s
ast_expression *array; ast_expression *array;
ast_expression *index; ast_expression *index;
}; };
ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index); ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index);
/* Vararg pipe node: /* Vararg pipe node:
* *
@ -377,7 +377,7 @@ struct ast_argpipe_s
ast_expression expression; ast_expression expression;
ast_expression *index; ast_expression *index;
}; };
ast_argpipe* ast_argpipe_new(lex_ctx ctx, ast_expression *index); ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index);
/* Store /* Store
* *
@ -391,7 +391,7 @@ struct ast_store_s
ast_expression *dest; ast_expression *dest;
ast_expression *source; ast_expression *source;
}; };
ast_store* ast_store_new(lex_ctx ctx, int op, ast_store* ast_store_new(lex_ctx_t ctx, int op,
ast_expression *d, ast_expression *s); ast_expression *d, ast_expression *s);
/* If /* If
@ -413,7 +413,7 @@ struct ast_ifthen_s
ast_expression *on_true; ast_expression *on_true;
ast_expression *on_false; ast_expression *on_false;
}; };
ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse); ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
/* Ternary expressions... /* Ternary expressions...
* *
@ -436,7 +436,7 @@ struct ast_ternary_s
ast_expression *on_true; ast_expression *on_true;
ast_expression *on_false; ast_expression *on_false;
}; };
ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse); ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
/* A general loop node /* A general loop node
* *
@ -478,7 +478,7 @@ struct ast_loop_s
bool pre_not; bool pre_not;
bool post_not; bool post_not;
}; };
ast_loop* ast_loop_new(lex_ctx ctx, ast_loop* ast_loop_new(lex_ctx_t ctx,
ast_expression *initexpr, ast_expression *initexpr,
ast_expression *precond, bool pre_not, ast_expression *precond, bool pre_not,
ast_expression *postcond, bool post_not, ast_expression *postcond, bool post_not,
@ -493,7 +493,7 @@ struct ast_breakcont_s
bool is_continue; bool is_continue;
unsigned int levels; unsigned int levels;
}; };
ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels); ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels);
/* Switch Statements /* Switch Statements
* *
@ -517,7 +517,7 @@ struct ast_switch_s
ast_switch_case *cases; ast_switch_case *cases;
}; };
ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op); ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op);
/* Label nodes /* Label nodes
* *
@ -533,7 +533,7 @@ struct ast_label_s
bool undefined; bool undefined;
}; };
ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined); ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined);
/* GOTO nodes /* GOTO nodes
* *
@ -547,7 +547,7 @@ struct ast_goto_s
ir_block *irblock_from; ir_block *irblock_from;
}; };
ast_goto* ast_goto_new(lex_ctx ctx, const char *name); ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name);
void ast_goto_set_label(ast_goto*, ast_label*); void ast_goto_set_label(ast_goto*, ast_label*);
/* CALL node /* CALL node
@ -567,7 +567,7 @@ struct ast_call_s
ast_expression* *params; ast_expression* *params;
ast_expression *va_count; ast_expression *va_count;
}; };
ast_call* ast_call_new(lex_ctx ctx, ast_call* ast_call_new(lex_ctx_t ctx,
ast_expression *funcexpr); ast_expression *funcexpr);
bool ast_call_check_types(ast_call*, ast_expression *this_func_va_type); bool ast_call_check_types(ast_call*, ast_expression *this_func_va_type);
@ -582,7 +582,7 @@ struct ast_block_s
ast_expression* *exprs; ast_expression* *exprs;
ast_expression* *collect; ast_expression* *collect;
}; };
ast_block* ast_block_new(lex_ctx ctx); ast_block* ast_block_new(lex_ctx_t ctx);
void ast_block_delete(ast_block*); void ast_block_delete(ast_block*);
void ast_block_set_type(ast_block*, ast_expression *from); void ast_block_set_type(ast_block*, ast_expression *from);
void ast_block_collect(ast_block*, ast_expression*); void ast_block_collect(ast_block*, ast_expression*);
@ -636,7 +636,7 @@ struct ast_function_s
ast_value *fixedparams; ast_value *fixedparams;
ast_value *return_value; ast_value *return_value;
}; };
ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype); ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype);
/* This will NOT delete the underlying ast_value */ /* This will NOT delete the underlying ast_value */
void ast_function_delete(ast_function*); void ast_function_delete(ast_function*);
/* For "optimized" builds this can just keep returning "foo"... /* For "optimized" builds this can just keep returning "foo"...

90
code.c
View file

@ -26,7 +26,7 @@
/* /*
* We could use the old method of casting to uintptr_t then to void* * We could use the old method of casting to uintptr_t then to void*
* or qcint; however, it's incredibly unsafe for two reasons. * or qcint_t; however, it's incredibly unsafe for two reasons.
* 1) The compilers aliasing optimization can legally make it unstable * 1) The compilers aliasing optimization can legally make it unstable
* (it's undefined behaviour). * (it's undefined behaviour).
* *
@ -37,14 +37,14 @@
*/ */
typedef union { typedef union {
void *enter; void *enter;
qcint leave; qcint_t leave;
} code_hash_entry_t; } code_hash_entry_t;
/* Some sanity macros */ /* Some sanity macros */
#define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter) #define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
#define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave) #define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
void code_push_statement(code_t *code, prog_section_statement *stmt, int linenum) void code_push_statement(code_t *code, prog_section_statement_t *stmt, int linenum)
{ {
vec_push(code->statements, *stmt); vec_push(code->statements, *stmt);
vec_push(code->linenums, linenum); vec_push(code->linenums, linenum);
@ -57,9 +57,9 @@ void code_pop_statement(code_t *code)
} }
code_t *code_init() { code_t *code_init() {
static prog_section_function empty_function = {0,0,0,0,0,0,0,{0,0,0,0,0,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 empty_statement = {0,{0},{0},{0}}; static prog_section_statement_t empty_statement = {0,{0},{0},{0}};
static prog_section_def empty_def = {0, 0, 0}; static prog_section_def_t empty_def = {0, 0, 0};
code_t *code = (code_t*)mem_a(sizeof(code_t)); code_t *code = (code_t*)mem_a(sizeof(code_t));
int i = 0; int i = 0;
@ -121,25 +121,25 @@ uint32_t code_genstring(code_t *code, const char *str) {
return CODE_HASH_LEAVE(existing); return CODE_HASH_LEAVE(existing);
} }
qcint code_alloc_field (code_t *code, size_t qcsize) qcint_t code_alloc_field (code_t *code, size_t qcsize)
{ {
qcint pos = (qcint)code->entfields; qcint_t pos = (qcint_t)code->entfields;
code->entfields += qcsize; code->entfields += qcsize;
return pos; return pos;
} }
static void code_create_header(code_t *code, prog_header *code_header) { static void code_create_header(code_t *code, prog_header_t *code_header) {
code_header->statements.offset = sizeof(prog_header); code_header->statements.offset = sizeof(prog_header_t);
code_header->statements.length = vec_size(code->statements); code_header->statements.length = vec_size(code->statements);
code_header->defs.offset = code_header->statements.offset + (sizeof(prog_section_statement) * vec_size(code->statements)); code_header->defs.offset = code_header->statements.offset + (sizeof(prog_section_statement_t) * vec_size(code->statements));
code_header->defs.length = vec_size(code->defs); code_header->defs.length = vec_size(code->defs);
code_header->fields.offset = code_header->defs.offset + (sizeof(prog_section_def) * vec_size(code->defs)); code_header->fields.offset = code_header->defs.offset + (sizeof(prog_section_def_t) * vec_size(code->defs));
code_header->fields.length = vec_size(code->fields); code_header->fields.length = vec_size(code->fields);
code_header->functions.offset = code_header->fields.offset + (sizeof(prog_section_field) * vec_size(code->fields)); code_header->functions.offset = code_header->fields.offset + (sizeof(prog_section_field_t) * vec_size(code->fields));
code_header->functions.length = vec_size(code->functions); code_header->functions.length = vec_size(code->functions);
code_header->globals.offset = code_header->functions.offset + (sizeof(prog_section_function) * vec_size(code->functions)); code_header->globals.offset = code_header->functions.offset + (sizeof(prog_section_function_t) * vec_size(code->functions));
code_header->globals.length = vec_size(code->globals); code_header->globals.length = vec_size(code->globals);
code_header->strings.offset = code_header->globals.offset + (sizeof(int32_t) * vec_size(code->globals)); code_header->strings.offset = code_header->globals.offset + (sizeof(int32_t) * vec_size(code->globals));
code_header->strings.length = vec_size(code->chars); code_header->strings.length = vec_size(code->chars);
code_header->version = 6; code_header->version = 6;
code_header->skip = 0; code_header->skip = 0;
@ -174,10 +174,10 @@ static void code_create_header(code_t *code, prog_header *code_header) {
* These are not part of the header but we ensure LE format here to save on duplicated * These are not part of the header but we ensure LE format here to save on duplicated
* code. * code.
*/ */
util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement)); util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement_t));
util_endianswap(code->defs, vec_size(code->defs), sizeof(prog_section_def)); util_endianswap(code->defs, vec_size(code->defs), sizeof(prog_section_def_t));
util_endianswap(code->fields, vec_size(code->fields), sizeof(prog_section_field)); util_endianswap(code->fields, vec_size(code->fields), sizeof(prog_section_field_t));
util_endianswap(code->functions, vec_size(code->functions), sizeof(prog_section_function)); util_endianswap(code->functions, vec_size(code->functions), sizeof(prog_section_function_t));
util_endianswap(code->globals, vec_size(code->globals), sizeof(int32_t)); util_endianswap(code->globals, vec_size(code->globals), sizeof(int32_t));
} }
@ -187,8 +187,8 @@ static void code_create_header(code_t *code, prog_header *code_header) {
* we're going to add. * we're going to add.
*/ */
bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) { bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) {
prog_header code_header; prog_header_t code_header;
uint32_t offset = 0; uint32_t offset = 0;
if (!datmem) if (!datmem)
return false; return false;
@ -232,23 +232,23 @@ bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t
} }
/* Write out the dat */ /* Write out the dat */
*sizedat += sizeof(prog_header); *sizedat += sizeof(prog_header_t);
*sizedat += sizeof(prog_section_statement) * vec_size(code->statements); *sizedat += sizeof(prog_section_statement_t) * vec_size(code->statements);
*sizedat += sizeof(prog_section_def) * vec_size(code->defs); *sizedat += sizeof(prog_section_def_t) * vec_size(code->defs);
*sizedat += sizeof(prog_section_field) * vec_size(code->fields); *sizedat += sizeof(prog_section_field_t) * vec_size(code->fields);
*sizedat += sizeof(prog_section_function) * vec_size(code->functions); *sizedat += sizeof(prog_section_function_t) * vec_size(code->functions);
*sizedat += sizeof(int32_t) * vec_size(code->globals); *sizedat += sizeof(int32_t) * vec_size(code->globals);
*sizedat += 1 * vec_size(code->chars); *sizedat += 1 * vec_size(code->chars);
*datmem = (uint8_t*)mem_a(*sizedat); *datmem = (uint8_t*)mem_a(*sizedat);
WRITE_CHUNK(datmem, &code_header, sizeof(prog_header)); WRITE_CHUNK(datmem, &code_header, sizeof(prog_header_t));
WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement) * vec_size(code->statements)); WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement_t) * vec_size(code->statements));
WRITE_CHUNK(datmem, code->defs, sizeof(prog_section_def) * vec_size(code->defs)); WRITE_CHUNK(datmem, code->defs, sizeof(prog_section_def_t) * vec_size(code->defs));
WRITE_CHUNK(datmem, code->fields, sizeof(prog_section_field) * vec_size(code->fields)); WRITE_CHUNK(datmem, code->fields, sizeof(prog_section_field_t) * vec_size(code->fields));
WRITE_CHUNK(datmem, code->functions, sizeof(prog_section_function) * vec_size(code->functions)); WRITE_CHUNK(datmem, code->functions, sizeof(prog_section_function_t) * vec_size(code->functions));
WRITE_CHUNK(datmem, code->globals, sizeof(int32_t) * vec_size(code->globals)); WRITE_CHUNK(datmem, code->globals, sizeof(int32_t) * vec_size(code->globals));
WRITE_CHUNK(datmem, code->chars, 1 * vec_size(code->chars)); WRITE_CHUNK(datmem, code->chars, 1 * vec_size(code->chars));
#undef WRITE_CHUNK #undef WRITE_CHUNK
@ -267,9 +267,9 @@ bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t
} }
bool code_write(code_t *code, const char *filename, const char *lnofile) { bool code_write(code_t *code, const char *filename, const char *lnofile) {
prog_header code_header; prog_header_t code_header;
FILE *fp = NULL; FILE *fp = NULL;
size_t it = 2; size_t it = 2;
code_create_header(code, &code_header); code_create_header(code, &code_header);
@ -303,13 +303,13 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) {
if (!fp) if (!fp)
return false; return false;
if (1 != fs_file_write(&code_header, sizeof(prog_header) , 1 , fp) || if (1 != fs_file_write(&code_header, sizeof(prog_header_t) , 1 , fp) ||
vec_size(code->statements) != fs_file_write(code->statements, sizeof(prog_section_statement), vec_size(code->statements), fp) || vec_size(code->statements) != fs_file_write(code->statements, sizeof(prog_section_statement_t), vec_size(code->statements), fp) ||
vec_size(code->defs) != fs_file_write(code->defs, sizeof(prog_section_def) , vec_size(code->defs) , fp) || vec_size(code->defs) != fs_file_write(code->defs, sizeof(prog_section_def_t) , vec_size(code->defs) , fp) ||
vec_size(code->fields) != fs_file_write(code->fields, sizeof(prog_section_field) , vec_size(code->fields) , fp) || vec_size(code->fields) != fs_file_write(code->fields, sizeof(prog_section_field_t) , vec_size(code->fields) , fp) ||
vec_size(code->functions) != fs_file_write(code->functions, sizeof(prog_section_function) , vec_size(code->functions) , fp) || vec_size(code->functions) != fs_file_write(code->functions, sizeof(prog_section_function_t) , vec_size(code->functions) , fp) ||
vec_size(code->globals) != fs_file_write(code->globals, sizeof(int32_t) , vec_size(code->globals) , fp) || vec_size(code->globals) != fs_file_write(code->globals, sizeof(int32_t) , vec_size(code->globals) , fp) ||
vec_size(code->chars) != fs_file_write(code->chars, 1 , vec_size(code->chars) , fp)) vec_size(code->chars) != fs_file_write(code->chars, 1 , vec_size(code->chars) , fp))
{ {
fs_file_close(fp); fs_file_close(fp);
return false; return false;

View file

@ -369,11 +369,11 @@ void con_printmsg(int level, const char *name, size_t line, size_t column, const
va_end (va); va_end (va);
} }
void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) { void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, ((lex_ctx*)ctx)->column, msgtype, msg, ap); con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
} }
void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) { void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
va_list va; va_list va;
va_start(va, msg); va_start(va, msg);
con_cvprintmsg(ctx, lvl, msgtype, msg, va); con_cvprintmsg(ctx, lvl, msgtype, msg, va);
@ -381,24 +381,23 @@ void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ..
} }
/* General error interface */ /* General error interface */
size_t compile_errors = 0; size_t compile_errors = 0;
size_t compile_warnings = 0; size_t compile_warnings = 0;
size_t compile_Werrors = 0;
size_t compile_Werrors = 0; static lex_ctx_t first_werror;
static lex_ctx first_werror;
void compile_show_werrors() void compile_show_werrors()
{ {
con_cprintmsg((void*)&first_werror, LVL_ERROR, "first warning", "was here"); con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
} }
void vcompile_error(lex_ctx ctx, const char *msg, va_list ap) void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
{ {
++compile_errors; ++compile_errors;
con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap); con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
} }
void compile_error(lex_ctx ctx, const char *msg, ...) void compile_error(lex_ctx_t ctx, const char *msg, ...)
{ {
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
@ -406,7 +405,7 @@ void compile_error(lex_ctx ctx, const char *msg, ...)
va_end(ap); va_end(ap);
} }
bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap) bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
{ {
const char *msgtype = "warning"; const char *msgtype = "warning";
int lvl = LVL_WARNING; int lvl = LVL_WARNING;
@ -437,7 +436,7 @@ bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_
return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR); return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
} }
bool GMQCC_WARN compile_warning(lex_ctx ctx, int warntype, const char *fmt, ...) bool GMQCC_WARN compile_warning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
{ {
bool r; bool r;
va_list ap; va_list ap;

218
exec.c
View file

@ -40,7 +40,7 @@ static void loaderror(const char *fmt, ...)
printf(": %s\n", util_strerror(err)); printf(": %s\n", util_strerror(err));
} }
static void qcvmerror(qc_program *prog, const char *fmt, ...) static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -52,10 +52,10 @@ static void qcvmerror(qc_program *prog, const char *fmt, ...)
putchar('\n'); putchar('\n');
} }
qc_program* prog_load(const char *filename, bool skipversion) qc_program_t* prog_load(const char *filename, bool skipversion)
{ {
qc_program *prog; qc_program_t *prog;
prog_header header; prog_header_t header;
FILE *file = fs_file_open(filename, "rb"); FILE *file = fs_file_open(filename, "rb");
if (!file) if (!file)
@ -73,7 +73,7 @@ qc_program* prog_load(const char *filename, bool skipversion)
return NULL; return NULL;
} }
prog = (qc_program*)mem_a(sizeof(qc_program)); prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
if (!prog) { if (!prog) {
fs_file_close(file); fs_file_close(file);
fprintf(stderr, "failed to allocate program data\n"); fprintf(stderr, "failed to allocate program data\n");
@ -149,7 +149,7 @@ error:
return NULL; return NULL;
} }
void prog_delete(qc_program *prog) void prog_delete(qc_program_t *prog)
{ {
if (prog->filename) mem_d(prog->filename); if (prog->filename) mem_d(prog->filename);
vec_free(prog->code); vec_free(prog->code);
@ -170,15 +170,15 @@ void prog_delete(qc_program *prog)
* VM code * VM code
*/ */
const char* prog_getstring(qc_program *prog, qcint str) { const char* prog_getstring(qc_program_t *prog, qcint_t str) {
/* cast for return required for C++ */ /* cast for return required for C++ */
if (str < 0 || str >= (qcint)vec_size(prog->strings)) if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
return "<<<invalid string>>>"; return "<<<invalid string>>>";
return prog->strings + str; return prog->strings + str;
} }
prog_section_def* prog_entfield(qc_program *prog, qcint off) { prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
size_t i; size_t i;
for (i = 0; i < vec_size(prog->fields); ++i) { for (i = 0; i < vec_size(prog->fields); ++i) {
if (prog->fields[i].offset == off) if (prog->fields[i].offset == off)
@ -187,7 +187,7 @@ prog_section_def* prog_entfield(qc_program *prog, qcint off) {
return NULL; return NULL;
} }
prog_section_def* prog_getdef(qc_program *prog, qcint off) prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
{ {
size_t i; size_t i;
for (i = 0; i < vec_size(prog->defs); ++i) { for (i = 0; i < vec_size(prog->defs); ++i) {
@ -197,39 +197,39 @@ prog_section_def* prog_getdef(qc_program *prog, qcint off)
return NULL; return NULL;
} }
qcany* prog_getedict(qc_program *prog, qcint e) { qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
if (e >= (qcint)vec_size(prog->entitypool)) { if (e >= (qcint_t)vec_size(prog->entitypool)) {
prog->vmerror++; prog->vmerror++;
fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e); fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
e = 0; e = 0;
} }
return (qcany*)(prog->entitydata + (prog->entityfields * e)); return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
} }
qcint prog_spawn_entity(qc_program *prog) { qcint_t prog_spawn_entity(qc_program_t *prog) {
char *data; char *data;
qcint e; qcint_t e;
for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) { for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
if (!prog->entitypool[e]) { if (!prog->entitypool[e]) {
data = (char*)(prog->entitydata + (prog->entityfields * e)); data = (char*)(prog->entitydata + (prog->entityfields * e));
memset(data, 0, prog->entityfields * sizeof(qcint)); memset(data, 0, prog->entityfields * sizeof(qcint_t));
return e; return e;
} }
} }
vec_push(prog->entitypool, true); vec_push(prog->entitypool, true);
prog->entities++; prog->entities++;
data = (char*)vec_add(prog->entitydata, prog->entityfields); data = (char*)vec_add(prog->entitydata, prog->entityfields);
memset(data, 0, prog->entityfields * sizeof(qcint)); memset(data, 0, prog->entityfields * sizeof(qcint_t));
return e; return e;
} }
void prog_free_entity(qc_program *prog, qcint e) { void prog_free_entity(qc_program_t *prog, qcint_t e) {
if (!e) { if (!e) {
prog->vmerror++; prog->vmerror++;
fprintf(stderr, "Trying to free world entity\n"); fprintf(stderr, "Trying to free world entity\n");
return; return;
} }
if (e >= (qcint)vec_size(prog->entitypool)) { if (e >= (qcint_t)vec_size(prog->entitypool)) {
prog->vmerror++; prog->vmerror++;
fprintf(stderr, "Trying to free out of bounds entity\n"); fprintf(stderr, "Trying to free out of bounds entity\n");
return; return;
@ -242,7 +242,7 @@ void prog_free_entity(qc_program *prog, qcint e) {
prog->entitypool[e] = false; prog->entitypool[e] = false;
} }
qcint prog_tempstring(qc_program *prog, const char *str) { qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
size_t len = strlen(str); size_t len = strlen(str);
size_t at = prog->tempstring_at; size_t at = prog->tempstring_at;
@ -297,10 +297,10 @@ static size_t print_escaped_string(const char *str, size_t maxlen) {
return len; return len;
} }
static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) { static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
static char spaces[28+1] = " "; static char spaces[28+1] = " ";
prog_section_def *def; prog_section_def_t *def;
qcany *value; qcany_t *value;
int len; int len;
if (!glob) { if (!glob) {
@ -311,7 +311,7 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) {
} }
def = prog_getdef(prog, glob); def = prog_getdef(prog, glob);
value = (qcany*)(&prog->globals[glob]); value = (qcany_t*)(&prog->globals[glob]);
len = printf("[@%u] ", glob); len = printf("[@%u] ", glob);
if (def) { if (def) {
@ -357,7 +357,7 @@ done:
} }
} }
static void prog_print_statement(qc_program *prog, prog_section_statement *st) { static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
if (st->opcode >= VINSTR_END) { if (st->opcode >= VINSTR_END) {
printf("<illegal instruction %d>\n", st->opcode); printf("<illegal instruction %d>\n", st->opcode);
return; return;
@ -454,8 +454,8 @@ static void prog_print_statement(qc_program *prog, prog_section_statement *st) {
} }
} }
static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) { static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
qc_exec_stack st; qc_exec_stack_t st;
size_t parampos; size_t parampos;
int32_t p; int32_t p;
@ -472,17 +472,17 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
#ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
if (vec_size(prog->stack)) if (vec_size(prog->stack))
{ {
prog_section_function *cur; prog_section_function_t *cur;
cur = prog->stack[vec_size(prog->stack)-1].function; cur = prog->stack[vec_size(prog->stack)-1].function;
if (cur) if (cur)
{ {
qcint *globals = prog->globals + cur->firstlocal; qcint_t *globals = prog->globals + cur->firstlocal;
vec_append(prog->localstack, cur->locals, globals); vec_append(prog->localstack, cur->locals, globals);
} }
} }
#else #else
{ {
qcint *globals = prog->globals + func->firstlocal; qcint_t *globals = prog->globals + func->firstlocal;
vec_append(prog->localstack, func->locals, globals); vec_append(prog->localstack, func->locals, globals);
} }
#endif #endif
@ -503,11 +503,11 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
return func->entry; return func->entry;
} }
static qcint prog_leavefunction(qc_program *prog) { static qcint_t prog_leavefunction(qc_program_t *prog) {
prog_section_function *prev = NULL; prog_section_function_t *prev = NULL;
size_t oldsp; size_t oldsp;
qc_exec_stack st = vec_last(prog->stack); qc_exec_stack_t st = vec_last(prog->stack);
if (prog->xflags & VMXF_TRACE) { if (prog->xflags & VMXF_TRACE) {
if (vec_size(prog->function_stack)) if (vec_size(prog->function_stack))
@ -524,7 +524,7 @@ static qcint prog_leavefunction(qc_program *prog) {
oldsp = prog->stack[vec_size(prog->stack)-1].localsp; oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
#endif #endif
if (prev) { if (prev) {
qcint *globals = prog->globals + prev->firstlocal; qcint_t *globals = prog->globals + prev->firstlocal;
memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0])); memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
/* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */ /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
vec_shrinkto(prog->localstack, oldsp); vec_shrinkto(prog->localstack, oldsp);
@ -535,10 +535,10 @@ static qcint prog_leavefunction(qc_program *prog) {
return st.stmt - 1; /* offset the ++st */ return st.stmt - 1; /* offset the ++st */
} }
bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) { bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
long jumpcount = 0; long jumpcount = 0;
size_t oldxflags = prog->xflags; size_t oldxflags = prog->xflags;
prog_section_statement *st; prog_section_statement_t *st;
prog->vmerror = 0; prog->vmerror = 0;
prog->xflags = flags; prog->xflags = flags;
@ -630,15 +630,15 @@ static qcvm_parameter *main_params = NULL;
} \ } \
} while (0) } while (0)
#define GetGlobal(idx) ((qcany*)(prog->globals + (idx))) #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
#define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num)) #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
#define Return(any) *(GetGlobal(OFS_RETURN)) = (any) #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
static int qc_print(qc_program *prog) { static int qc_print(qc_program_t *prog) {
size_t i; size_t i;
const char *laststr = NULL; const char *laststr = NULL;
for (i = 0; i < (size_t)prog->argc; ++i) { for (i = 0; i < (size_t)prog->argc; ++i) {
qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i); qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
laststr = prog_getstring(prog, str->string); laststr = prog_getstring(prog, str->string);
printf("%s", laststr); printf("%s", laststr);
} }
@ -650,17 +650,17 @@ static int qc_print(qc_program *prog) {
return 0; return 0;
} }
static int qc_error(qc_program *prog) { static int qc_error(qc_program_t *prog) {
fprintf(stderr, "*** VM raised an error:\n"); fprintf(stderr, "*** VM raised an error:\n");
qc_print(prog); qc_print(prog);
prog->vmerror++; prog->vmerror++;
return -1; return -1;
} }
static int qc_ftos(qc_program *prog) { static int qc_ftos(qc_program_t *prog) {
char buffer[512]; char buffer[512];
qcany *num; qcany_t *num;
qcany str; qcany_t str;
CheckArgs(1); CheckArgs(1);
num = GetArg(0); num = GetArg(0);
util_snprintf(buffer, sizeof(buffer), "%g", num->_float); util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
@ -669,9 +669,9 @@ static int qc_ftos(qc_program *prog) {
return 0; return 0;
} }
static int qc_stof(qc_program *prog) { static int qc_stof(qc_program_t *prog) {
qcany *str; qcany_t *str;
qcany num; qcany_t num;
CheckArgs(1); CheckArgs(1);
str = GetArg(0); str = GetArg(0);
num._float = (float)strtod(prog_getstring(prog, str->string), NULL); num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
@ -679,10 +679,10 @@ static int qc_stof(qc_program *prog) {
return 0; return 0;
} }
static int qc_vtos(qc_program *prog) { static int qc_vtos(qc_program_t *prog) {
char buffer[512]; char buffer[512];
qcany *num; qcany_t *num;
qcany str; qcany_t str;
CheckArgs(1); CheckArgs(1);
num = GetArg(0); num = GetArg(0);
util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]); util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
@ -691,10 +691,10 @@ static int qc_vtos(qc_program *prog) {
return 0; return 0;
} }
static int qc_etos(qc_program *prog) { static int qc_etos(qc_program_t *prog) {
char buffer[512]; char buffer[512];
qcany *num; qcany_t *num;
qcany str; qcany_t str;
CheckArgs(1); CheckArgs(1);
num = GetArg(0); num = GetArg(0);
util_snprintf(buffer, sizeof(buffer), "%i", num->_int); util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
@ -703,24 +703,24 @@ static int qc_etos(qc_program *prog) {
return 0; return 0;
} }
static int qc_spawn(qc_program *prog) { static int qc_spawn(qc_program_t *prog) {
qcany ent; qcany_t ent;
CheckArgs(0); CheckArgs(0);
ent.edict = prog_spawn_entity(prog); ent.edict = prog_spawn_entity(prog);
Return(ent); Return(ent);
return (ent.edict ? 0 : -1); return (ent.edict ? 0 : -1);
} }
static int qc_kill(qc_program *prog) { static int qc_kill(qc_program_t *prog) {
qcany *ent; qcany_t *ent;
CheckArgs(1); CheckArgs(1);
ent = GetArg(0); ent = GetArg(0);
prog_free_entity(prog, ent->edict); prog_free_entity(prog, ent->edict);
return 0; return 0;
} }
static int qc_sqrt(qc_program *prog) { static int qc_sqrt(qc_program_t *prog) {
qcany *num, out; qcany_t *num, out;
CheckArgs(1); CheckArgs(1);
num = GetArg(0); num = GetArg(0);
out._float = sqrt(num->_float); out._float = sqrt(num->_float);
@ -728,8 +728,8 @@ static int qc_sqrt(qc_program *prog) {
return 0; return 0;
} }
static int qc_vlen(qc_program *prog) { static int qc_vlen(qc_program_t *prog) {
qcany *vec, len; qcany_t *vec, len;
CheckArgs(1); CheckArgs(1);
vec = GetArg(0); vec = GetArg(0);
len._float = sqrt(vec->vector[0] * vec->vector[0] + len._float = sqrt(vec->vector[0] * vec->vector[0] +
@ -739,10 +739,10 @@ static int qc_vlen(qc_program *prog) {
return 0; return 0;
} }
static int qc_normalize(qc_program *prog) { static int qc_normalize(qc_program_t *prog) {
double len; double len;
qcany *vec; qcany_t *vec;
qcany out; qcany_t out;
CheckArgs(1); CheckArgs(1);
vec = GetArg(0); vec = GetArg(0);
len = sqrt(vec->vector[0] * vec->vector[0] + len = sqrt(vec->vector[0] * vec->vector[0] +
@ -759,11 +759,11 @@ static int qc_normalize(qc_program *prog) {
return 0; return 0;
} }
static int qc_strcat(qc_program *prog) { static int qc_strcat(qc_program_t *prog) {
char *buffer; char *buffer;
size_t len1, len2; size_t len1, len2;
qcany *str1, *str2; qcany_t *str1, *str2;
qcany out; qcany_t out;
const char *cstr1; const char *cstr1;
const char *cstr2; const char *cstr2;
@ -784,9 +784,9 @@ static int qc_strcat(qc_program *prog) {
return 0; return 0;
} }
static int qc_strcmp(qc_program *prog) { static int qc_strcmp(qc_program_t *prog) {
qcany *str1, *str2; qcany_t *str1, *str2;
qcany out; qcany_t out;
const char *cstr1; const char *cstr1;
const char *cstr2; const char *cstr2;
@ -809,8 +809,8 @@ static int qc_strcmp(qc_program *prog) {
return 0; return 0;
} }
static int qc_floor(qc_program *prog) { static int qc_floor(qc_program_t *prog) {
qcany *num, out; qcany_t *num, out;
CheckArgs(1); CheckArgs(1);
num = GetArg(0); num = GetArg(0);
out._float = floor(num->_float); out._float = floor(num->_float);
@ -818,7 +818,7 @@ static int qc_floor(qc_program *prog) {
return 0; return 0;
} }
static prog_builtin qc_builtins[] = { static prog_builtin_t qc_builtins[] = {
NULL, NULL,
&qc_print, /* 1 */ &qc_print, /* 1 */
&qc_ftos, /* 2 */ &qc_ftos, /* 2 */
@ -869,9 +869,9 @@ static void usage(void) {
" -string <s> pass a string parameter to main() \n"); " -string <s> pass a string parameter to main() \n");
} }
static void prog_main_setparams(qc_program *prog) { static void prog_main_setparams(qc_program_t *prog) {
size_t i; size_t i;
qcany *arg; qcany_t *arg;
for (i = 0; i < vec_size(main_params); ++i) { for (i = 0; i < vec_size(main_params); ++i) {
arg = GetGlobal(OFS_PARM0 + 3*i); arg = GetGlobal(OFS_PARM0 + 3*i);
@ -931,12 +931,12 @@ void escapestring(char* dest, const char* src) {
*dest = '\0'; *dest = '\0';
} }
void prog_disasm_function(qc_program *prog, size_t id); void prog_disasm_function(qc_program_t *prog, size_t id);
int main(int argc, char **argv) { int main(int argc, char **argv) {
size_t i; size_t i;
qcint fnmain = -1; qcint_t fnmain = -1;
qc_program *prog; qc_program_t *prog;
size_t xflags = VMXF_DEFAULT; size_t xflags = VMXF_DEFAULT;
bool opts_printfields = false; bool opts_printfields = false;
bool opts_printdefs = false; bool opts_printdefs = false;
@ -1159,19 +1159,19 @@ int main(int argc, char **argv) {
if (opts_v) { if (opts_v) {
switch (prog->defs[i].type & DEF_TYPEMASK) { switch (prog->defs[i].type & DEF_TYPEMASK) {
case TYPE_FLOAT: case TYPE_FLOAT:
printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float); printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
break; break;
case TYPE_INTEGER: case TYPE_INTEGER:
printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int )); printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
break; break;
case TYPE_ENTITY: case TYPE_ENTITY:
case TYPE_FUNCTION: case TYPE_FUNCTION:
case TYPE_FIELD: case TYPE_FIELD:
case TYPE_POINTER: case TYPE_POINTER:
printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int )); printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
break; break;
case TYPE_STRING: case TYPE_STRING:
getstring = prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string); getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */ escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */
escapestring(escape, getstring); escapestring(escape, getstring);
printf(" [init: `%s`]", escape); printf(" [init: `%s`]", escape);
@ -1209,7 +1209,7 @@ int main(int argc, char **argv) {
printf(") builtin %i\n", (int)-start); printf(") builtin %i\n", (int)-start);
else { else {
size_t funsize = 0; size_t funsize = 0;
prog_section_statement *st = prog->code + start; prog_section_statement_t *st = prog->code + start;
for (;st->opcode != INSTR_DONE; ++st) for (;st->opcode != INSTR_DONE; ++st)
++funsize; ++funsize;
printf(") - %lu instructions", (unsigned long)funsize); printf(") - %lu instructions", (unsigned long)funsize);
@ -1235,7 +1235,7 @@ int main(int argc, char **argv) {
for (i = 1; i < vec_size(prog->functions); ++i) { for (i = 1; i < vec_size(prog->functions); ++i) {
const char *name = prog_getstring(prog, prog->functions[i].name); const char *name = prog_getstring(prog, prog->functions[i].name);
if (!strcmp(name, "main")) if (!strcmp(name, "main"))
fnmain = (qcint)i; fnmain = (qcint_t)i;
} }
if (fnmain > 0) if (fnmain > 0)
{ {
@ -1250,9 +1250,9 @@ int main(int argc, char **argv) {
return 0; return 0;
} }
void prog_disasm_function(qc_program *prog, size_t id) { void prog_disasm_function(qc_program_t *prog, size_t id) {
prog_section_function *fdef = prog->functions + id; prog_section_function_t *fdef = prog->functions + id;
prog_section_statement *st; prog_section_statement_t *st;
if (fdef->entry < 0) { if (fdef->entry < 0) {
printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry); printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
@ -1277,11 +1277,11 @@ void prog_disasm_function(qc_program *prog, size_t id) {
* sort of isn't, which makes it nicer looking. * sort of isn't, which makes it nicer looking.
*/ */
#define OPA ( (qcany*) (prog->globals + st->o1.u1) ) #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
#define OPB ( (qcany*) (prog->globals + st->o2.u1) ) #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
#define OPC ( (qcany*) (prog->globals + st->o3.u1) ) #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
#define GLOBAL(x) ( (qcany*) (prog->globals + (x)) ) #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
/* to be consistent with current darkplaces behaviour */ /* to be consistent with current darkplaces behaviour */
#if !defined(FLOAT_IS_TRUE_FOR_INT) #if !defined(FLOAT_IS_TRUE_FOR_INT)
@ -1289,9 +1289,9 @@ void prog_disasm_function(qc_program *prog, size_t id) {
#endif #endif
while (1) { while (1) {
prog_section_function *newf; prog_section_function_t *newf;
qcany *ed; qcany_t *ed;
qcany *ptr; qcany_t *ptr;
++st; ++st;
@ -1332,7 +1332,7 @@ while (1) {
break; break;
case INSTR_MUL_FV: case INSTR_MUL_FV:
{ {
qcfloat f = OPA->_float; qcfloat_t f = OPA->_float;
OPC->vector[0] = f * OPB->vector[0]; OPC->vector[0] = f * OPB->vector[0];
OPC->vector[1] = f * OPB->vector[1]; OPC->vector[1] = f * OPB->vector[1];
OPC->vector[2] = f * OPB->vector[2]; OPC->vector[2] = f * OPB->vector[2];
@ -1340,7 +1340,7 @@ while (1) {
} }
case INSTR_MUL_VF: case INSTR_MUL_VF:
{ {
qcfloat f = OPB->_float; qcfloat_t f = OPB->_float;
OPC->vector[0] = f * OPA->vector[0]; OPC->vector[0] = f * OPA->vector[0];
OPC->vector[1] = f * OPA->vector[1]; OPC->vector[1] = f * OPA->vector[1];
OPC->vector[2] = f * OPA->vector[2]; OPC->vector[2] = f * OPA->vector[2];
@ -1436,14 +1436,14 @@ while (1) {
goto cleanup; goto cleanup;
} }
ed = prog_getedict(prog, OPA->edict); ed = prog_getedict(prog, OPA->edict);
OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int; OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
break; break;
case INSTR_LOAD_V: case INSTR_LOAD_V:
if (OPA->edict < 0 || OPA->edict >= prog->entities) { if (OPA->edict < 0 || OPA->edict >= prog->entities) {
qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename); qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
goto cleanup; goto cleanup;
} }
if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields) if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
{ {
qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)", qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
prog->filename, prog->filename,
@ -1451,7 +1451,7 @@ while (1) {
goto cleanup; goto cleanup;
} }
ed = prog_getedict(prog, OPA->edict); ed = prog_getedict(prog, OPA->edict);
ptr = (qcany*)( ((qcint*)ed) + OPB->_int ); ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
OPC->ivector[0] = ptr->ivector[0]; OPC->ivector[0] = ptr->ivector[0];
OPC->ivector[1] = ptr->ivector[1]; OPC->ivector[1] = ptr->ivector[1];
OPC->ivector[2] = ptr->ivector[2]; OPC->ivector[2] = ptr->ivector[2];
@ -1471,7 +1471,7 @@ while (1) {
} }
ed = prog_getedict(prog, OPA->edict); ed = prog_getedict(prog, OPA->edict);
OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int; OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
break; break;
case INSTR_STORE_F: case INSTR_STORE_F:
@ -1492,29 +1492,29 @@ while (1) {
case INSTR_STOREP_ENT: case INSTR_STOREP_ENT:
case INSTR_STOREP_FLD: case INSTR_STOREP_FLD:
case INSTR_STOREP_FNC: case INSTR_STOREP_FNC:
if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) { if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int); qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
goto cleanup; goto cleanup;
} }
if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites) if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n", qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
prog->filename, prog->filename,
prog_getstring(prog, prog_entfield(prog, OPB->_int)->name), prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
OPB->_int); OPB->_int);
ptr = (qcany*)(prog->entitydata + OPB->_int); ptr = (qcany_t*)(prog->entitydata + OPB->_int);
ptr->_int = OPA->_int; ptr->_int = OPA->_int;
break; break;
case INSTR_STOREP_V: case INSTR_STOREP_V:
if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) { if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int); qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
goto cleanup; goto cleanup;
} }
if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites) if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n", qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
prog->filename, prog->filename,
prog_getstring(prog, prog_entfield(prog, OPB->_int)->name), prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
OPB->_int); OPB->_int);
ptr = (qcany*)(prog->entitydata + OPB->_int); ptr = (qcany_t*)(prog->entitydata + OPB->_int);
ptr->ivector[0] = OPA->ivector[0]; ptr->ivector[0] = OPA->ivector[0];
ptr->ivector[1] = OPA->ivector[1]; ptr->ivector[1] = OPA->ivector[1];
ptr->ivector[2] = OPA->ivector[2]; ptr->ivector[2] = OPA->ivector[2];
@ -1570,7 +1570,7 @@ while (1) {
if (!OPA->function) if (!OPA->function)
qcvmerror(prog, "NULL function in `%s`", prog->filename); qcvmerror(prog, "NULL function in `%s`", prog->filename);
if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions)) if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
{ {
qcvmerror(prog, "CALL outside the program in `%s`", prog->filename); qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
goto cleanup; goto cleanup;
@ -1584,8 +1584,8 @@ while (1) {
if (newf->entry < 0) if (newf->entry < 0)
{ {
/* negative statements are built in functions */ /* negative statements are built in functions */
qcint builtinnumber = -newf->entry; qcint_t builtinnumber = -newf->entry;
if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber]) if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
prog->builtins[builtinnumber](prog); prog->builtins[builtinnumber](prog);
else else
qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources", qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",

22
ftepp.c
View file

@ -41,7 +41,7 @@ typedef struct {
char *value; char *value;
/* a copy from the lexer */ /* a copy from the lexer */
union { union {
vector v; vec3_t v;
int i; int i;
double f; double f;
int t; /* type */ int t; /* type */
@ -49,7 +49,7 @@ typedef struct {
} pptoken; } pptoken;
typedef struct { typedef struct {
lex_ctx ctx; lex_ctx_t ctx;
char *name; char *name;
char **params; char **params;
@ -239,14 +239,14 @@ static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
#define ftepp_tokval(f) ((f)->lex->tok.value) #define ftepp_tokval(f) ((f)->lex->tok.value)
#define ftepp_ctx(f) ((f)->lex->tok.ctx) #define ftepp_ctx(f) ((f)->lex->tok.ctx)
static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...) static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
{ {
va_list ap; va_list ap;
ftepp->errors++; ftepp->errors++;
va_start(ap, fmt); va_start(ap, fmt);
con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap); con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
va_end(ap); va_end(ap);
} }
@ -257,7 +257,7 @@ static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
ftepp->errors++; ftepp->errors++;
va_start(ap, fmt); va_start(ap, fmt);
con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap); con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
va_end(ap); va_end(ap);
} }
@ -293,7 +293,7 @@ static GMQCC_INLINE void pptoken_delete(pptoken *self)
mem_d(self); mem_d(self);
} }
static ppmacro *ppmacro_new(lex_ctx ctx, const char *name) static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
{ {
ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro)); ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
@ -1406,7 +1406,7 @@ static void ftepp_directive_message(ftepp_t *ftepp) {
} }
vec_push(message, '\0'); vec_push(message, '\0');
if (ftepp->output_on) if (ftepp->output_on)
con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message); con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
vec_free(message); vec_free(message);
return; return;
} }
@ -1415,7 +1415,7 @@ static void ftepp_directive_message(ftepp_t *ftepp) {
return; return;
unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp)); unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp)); con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
} }
/** /**
@ -1427,7 +1427,7 @@ static bool ftepp_include(ftepp_t *ftepp)
{ {
lex_file *old_lexer = ftepp->lex; lex_file *old_lexer = ftepp->lex;
lex_file *inlex; lex_file *inlex;
lex_ctx ctx; lex_ctx_t ctx;
char lineno[128]; char lineno[128];
char *filename; char *filename;
char *old_includename; char *old_includename;
@ -1517,7 +1517,7 @@ static bool ftepp_hash(ftepp_t *ftepp)
ppcondition cond; ppcondition cond;
ppcondition *pc; ppcondition *pc;
lex_ctx ctx = ftepp_ctx(ftepp); lex_ctx_t ctx = ftepp_ctx(ftepp);
if (!ftepp_skipspace(ftepp)) if (!ftepp_skipspace(ftepp))
return false; return false;
@ -1878,7 +1878,7 @@ ftepp_t *ftepp_create()
void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name) void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
{ {
ppmacro *macro; ppmacro *macro;
lex_ctx ctx = { "__builtin__", 0, 0 }; lex_ctx_t ctx = { "__builtin__", 0, 0 };
ctx.file = source; ctx.file = source;
macro = ppmacro_new(ctx, name); macro = ppmacro_new(ctx, name);
/*vec_push(ftepp->macros, macro);*/ /*vec_push(ftepp->macros, macro);*/

197
gmqcc.h
View file

@ -382,14 +382,14 @@ void _util_vec_grow(void **a, size_t i, size_t s);
#define vec_upload(X,Y,S) ((void)(memcpy(vec_add((X), (S) * sizeof(*(Y))), (Y), (S) * sizeof(*(Y))))) #define vec_upload(X,Y,S) ((void)(memcpy(vec_add((X), (S) * sizeof(*(Y))), (Y), (S) * sizeof(*(Y)))))
#define vec_remove(A,I,N) ((void)(memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N))) #define vec_remove(A,I,N) ((void)(memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N)))
typedef struct trie_s { typedef struct correct_trie_s {
void *value; void *value;
struct trie_s *entries; struct correct_trie_s *entries;
} correct_trie_t; } correct_trie_t;
correct_trie_t* correct_trie_new(void); correct_trie_t* correct_trie_new(void);
typedef struct hash_table_t { typedef struct hash_table_s {
size_t size; size_t size;
struct hash_node_t **table; struct hash_node_t **table;
} hash_table_t, *ht; } hash_table_t, *ht;
@ -525,21 +525,21 @@ extern const uint16_t type_not_instr [TYPE_COUNT];
typedef struct { typedef struct {
uint32_t offset; /* Offset in file of where data begins */ uint32_t offset; /* Offset in file of where data begins */
uint32_t length; /* Length of section (how many of) */ uint32_t length; /* Length of section (how many of) */
} prog_section; } prog_section_t;
typedef struct { typedef struct {
uint32_t version; /* Program version (6) */ uint32_t version; /* Program version (6) */
uint16_t crc16; uint16_t crc16;
uint16_t skip; uint16_t skip;
prog_section statements; /* prog_section_statement */ prog_section_t statements; /* prog_section_statement */
prog_section defs; /* prog_section_def */ prog_section_t defs; /* prog_section_def */
prog_section fields; /* prog_section_field */ prog_section_t fields; /* prog_section_field */
prog_section functions; /* prog_section_function */ prog_section_t functions; /* prog_section_function */
prog_section strings; prog_section_t strings;
prog_section globals; prog_section_t globals;
uint32_t entfield; /* Number of entity fields */ uint32_t entfield; /* Number of entity fields */
} prog_header; } prog_header_t;
/* /*
* Each paramater incerements by 3 since vector types hold * Each paramater incerements by 3 since vector types hold
@ -584,7 +584,7 @@ typedef struct {
* But this one is more sane to work with, and the * But this one is more sane to work with, and the
* type sizes are guranteed. * type sizes are guranteed.
*/ */
} prog_section_statement; } prog_section_statement_t;
typedef struct { typedef struct {
/* /*
@ -602,10 +602,10 @@ typedef struct {
uint16_t type; uint16_t type;
uint16_t offset; uint16_t offset;
uint32_t name; uint32_t name;
} prog_section_both; } prog_section_both_t;
typedef prog_section_both prog_section_def; typedef prog_section_both_t prog_section_def_t;
typedef prog_section_both prog_section_field; typedef prog_section_both_t prog_section_field_t;
/* this is ORed to the type */ /* this is ORed to the type */
#define DEF_SAVEGLOBAL (1<<15) #define DEF_SAVEGLOBAL (1<<15)
@ -620,7 +620,7 @@ typedef struct {
uint32_t file; /* file of the source file */ uint32_t file; /* file of the source file */
int32_t nargs; /* number of arguments */ int32_t nargs; /* number of arguments */
uint8_t argsize[8]; /* size of arguments (keep 8 always?) */ uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
} prog_section_function; } prog_section_function_t;
/* /*
* Instructions * Instructions
@ -713,22 +713,22 @@ enum {
/* TODO: elide */ /* TODO: elide */
extern const char *util_instr_str[VINSTR_END]; extern const char *util_instr_str[VINSTR_END];
/* uhh? */ /* TOO: _t */
typedef float qcfloat; typedef float qcfloat_t;
typedef int32_t qcint; typedef int32_t qcint_t;
typedef struct { typedef struct {
prog_section_statement *statements; prog_section_statement_t *statements;
int *linenums; int *linenums;
prog_section_def *defs; prog_section_def_t *defs;
prog_section_field *fields; prog_section_field_t *fields;
prog_section_function *functions; prog_section_function_t *functions;
int *globals; int *globals;
char *chars; char *chars;
uint16_t crc; uint16_t crc;
uint32_t entfields; uint32_t entfields;
ht string_cache; ht string_cache;
qcint string_cached_empty; qcint_t string_cached_empty;
} code_t; } code_t;
/* /*
@ -744,8 +744,8 @@ GMQCC_WARN
code_t *code_init (void); code_t *code_init (void);
void code_cleanup (code_t *); void code_cleanup (code_t *);
uint32_t code_genstring (code_t *, const char *string); uint32_t code_genstring (code_t *, const char *string);
qcint code_alloc_field (code_t *, size_t qcsize); qcint_t code_alloc_field (code_t *, size_t qcsize);
void code_push_statement(code_t *, prog_section_statement *stmt, int linenum); void code_push_statement(code_t *, prog_section_statement_t *stmt, int linenum);
void code_pop_statement (code_t *); void code_pop_statement (code_t *);
/* /*
@ -756,7 +756,7 @@ typedef struct {
const char *file; const char *file;
size_t line; size_t line;
size_t column; size_t column;
} lex_ctx; } lex_ctx_t;
/*===================================================================*/ /*===================================================================*/
/*============================ con.c ================================*/ /*============================ con.c ================================*/
@ -784,8 +784,8 @@ FILE *con_default_err(void);
void con_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap); void con_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap);
void con_printmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...); void con_printmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...);
void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap); void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap);
void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...); void con_cprintmsg (lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...);
void con_close (void); void con_close (void);
void con_init (void); void con_init (void);
@ -802,10 +802,10 @@ extern size_t compile_errors;
extern size_t compile_Werrors; extern size_t compile_Werrors;
extern size_t compile_warnings; extern size_t compile_warnings;
void /********/ compile_error (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, ...); void /********/ compile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, ...);
void /********/ vcompile_error (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, va_list ap); void /********/ vcompile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, va_list ap);
bool GMQCC_WARN compile_warning (lex_ctx ctx, int warntype, const char *fmt, ...); bool GMQCC_WARN compile_warning (lex_ctx_t ctx, int warntype, const char *fmt, ...);
bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap); bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap);
void compile_show_werrors(void); void compile_show_werrors(void);
/*===================================================================*/ /*===================================================================*/
@ -821,13 +821,13 @@ enum store_types {
}; };
typedef struct { typedef struct {
qcfloat x, y, z; qcfloat_t x, y, z;
} vector; } vec3_t;
vector vec3_add (vector, vector); vec3_t vec3_add (vec3_t, vec3_t);
vector vec3_sub (vector, vector); vec3_t vec3_sub (vec3_t, vec3_t);
qcfloat vec3_mulvv(vector, vector); qcfloat_t vec3_mulvv(vec3_t, vec3_t);
vector vec3_mulvf(vector, float); vec3_t vec3_mulvf(vec3_t, float);
/*===================================================================*/ /*===================================================================*/
/*============================= exec.c ==============================*/ /*============================= exec.c ==============================*/
@ -841,22 +841,21 @@ vector vec3_mulvf(vector, float);
* float and int here. * float and int here.
*/ */
typedef union { typedef union {
qcint _int; qcint_t _int;
qcint string; qcint_t string;
qcint function; qcint_t function;
qcint edict; qcint_t edict;
qcfloat _float; qcfloat_t _float;
qcfloat vector[3]; qcfloat_t vector[3];
qcint ivector[3]; qcint_t ivector[3];
} qcany; } qcany_t;
typedef char qcfloat_size_is_correct [sizeof(qcfloat) == 4 ?1:-1]; typedef char qcfloat_t_size_is_correct [sizeof(qcfloat_t) == 4 ?1:-1];
typedef char qcint_size_is_correct [sizeof(qcint) == 4 ?1:-1]; typedef char qcint_t_size_is_correct [sizeof(qcint_t) == 4 ?1:-1];
enum { enum {
VMERR_OK, VMERR_OK,
VMERR_TEMPSTRING_ALLOC, VMERR_TEMPSTRING_ALLOC,
VMERR_END VMERR_END
}; };
@ -868,65 +867,61 @@ enum {
#define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */ #define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */
struct qc_program_s; struct qc_program_s;
typedef int (*prog_builtin_t)(struct qc_program_s *prog);
typedef int (*prog_builtin)(struct qc_program_s *prog);
typedef struct { typedef struct {
qcint stmt; qcint_t stmt;
size_t localsp; size_t localsp;
prog_section_function *function; prog_section_function_t *function;
} qc_exec_stack; } qc_exec_stack_t;
typedef struct qc_program_s { typedef struct qc_program_s {
char *filename; char *filename;
prog_section_statement_t *code;
prog_section_def_t *defs;
prog_section_def_t *fields;
prog_section_function_t *functions;
char *strings;
qcint_t *globals;
qcint_t *entitydata;
bool *entitypool;
prog_section_statement *code; const char* *function_stack;
prog_section_def *defs;
prog_section_def *fields;
prog_section_function *functions;
char *strings;
qcint *globals;
qcint *entitydata;
bool *entitypool;
const char* *function_stack;
uint16_t crc16; uint16_t crc16;
size_t tempstring_start; size_t tempstring_start;
size_t tempstring_at; size_t tempstring_at;
qcint vmerror; qcint_t vmerror;
size_t *profile; size_t *profile;
prog_builtin *builtins; prog_builtin_t *builtins;
size_t builtins_count; size_t builtins_count;
/* size_t ip; */ /* size_t ip; */
qcint entities; qcint_t entities;
size_t entityfields; size_t entityfields;
bool allowworldwrites; bool allowworldwrites;
qcint *localstack; qcint_t *localstack;
qc_exec_stack *stack; qc_exec_stack_t *stack;
size_t statement; size_t statement;
size_t xflags; size_t xflags;
int argc; /* current arg count for debugging */ int argc; /* current arg count for debugging */
} qc_program; } qc_program_t;
qc_program* prog_load(const char *filename, bool ignoreversion); qc_program_t* prog_load (const char *filename, bool ignoreversion);
void prog_delete(qc_program *prog); void prog_delete (qc_program_t *prog);
bool prog_exec (qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps);
bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps); const char* prog_getstring (qc_program_t *prog, qcint_t str);
prog_section_def_t* prog_entfield (qc_program_t *prog, qcint_t off);
const char* prog_getstring (qc_program *prog, qcint str); prog_section_def_t* prog_getdef (qc_program_t *prog, qcint_t off);
prog_section_def* prog_entfield (qc_program *prog, qcint off); qcany_t* prog_getedict (qc_program_t *prog, qcint_t e);
prog_section_def* prog_getdef (qc_program *prog, qcint off); qcint_t prog_tempstring(qc_program_t *prog, const char *_str);
qcany* prog_getedict (qc_program *prog, qcint e);
qcint prog_tempstring(qc_program *prog, const char *_str);
/*===================================================================*/ /*===================================================================*/
@ -990,7 +985,7 @@ int u8_fromchar(uchar_t w, char *to, size_t maxlen);
typedef struct { typedef struct {
const char *name; const char *name;
longbit bit; longbit bit;
} opts_flag_def; } opts_flag_def_t;
bool opts_setflag (const char *, bool); bool opts_setflag (const char *, bool);
bool opts_setwarn (const char *, bool); bool opts_setwarn (const char *, bool);
@ -1037,11 +1032,11 @@ enum {
OPTION_COUNT OPTION_COUNT
}; };
extern const opts_flag_def opts_flag_list[COUNT_FLAGS+1]; extern const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1];
extern const opts_flag_def opts_warn_list[COUNT_WARNINGS+1]; extern const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1];
extern const opts_flag_def opts_opt_list[COUNT_OPTIMIZATIONS+1]; extern const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1];
extern const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1]; extern const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1];
extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS]; extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
/* other options: */ /* other options: */
typedef enum { typedef enum {

94
ir.c
View file

@ -227,11 +227,11 @@ static bool ir_function_set_name(ir_function*, const char *name);
static void ir_function_delete(ir_function*); static void ir_function_delete(ir_function*);
static void ir_function_dump(ir_function*, char *ind, int (*oprintf)(const char*,...)); static void ir_function_dump(ir_function*, char *ind, int (*oprintf)(const char*,...));
static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx, const char *label, static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx_t, const char *label,
int op, ir_value *a, ir_value *b, int outype); int op, ir_value *a, ir_value *b, int outype);
static void ir_block_delete(ir_block*); static void ir_block_delete(ir_block*);
static ir_block* ir_block_new(struct ir_function_s *owner, const char *label); static ir_block* ir_block_new(struct ir_function_s *owner, const char *label);
static bool GMQCC_WARN ir_block_create_store(ir_block*, lex_ctx, ir_value *target, ir_value *what); static bool GMQCC_WARN ir_block_create_store(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
static bool ir_block_set_label(ir_block*, const char *label); static bool ir_block_set_label(ir_block*, const char *label);
static void ir_block_dump(ir_block*, char *ind, int (*oprintf)(const char*,...)); static void ir_block_dump(ir_block*, char *ind, int (*oprintf)(const char*,...));
@ -240,15 +240,15 @@ static void ir_instr_delete(ir_instr*);
static void ir_instr_dump(ir_instr* in, char *ind, int (*oprintf)(const char*,...)); static void ir_instr_dump(ir_instr* in, char *ind, int (*oprintf)(const char*,...));
/* error functions */ /* error functions */
static void irerror(lex_ctx ctx, const char *msg, ...) static void irerror(lex_ctx_t ctx, const char *msg, ...)
{ {
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap); con_cvprintmsg(ctx, LVL_ERROR, "internal error", msg, ap);
va_end(ap); va_end(ap);
} }
static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...) static bool irwarning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
{ {
bool r; bool r;
va_list ap; va_list ap;
@ -584,7 +584,7 @@ static void ir_function_collect_value(ir_function *self, ir_value *v)
vec_push(self->values, v); vec_push(self->values, v);
} }
ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label) ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function *self, const char *label)
{ {
ir_block* bn = ir_block_new(self, label); ir_block* bn = ir_block_new(self, label);
bn->context = ctx; bn->context = ctx;
@ -954,7 +954,7 @@ bool ir_block_set_label(ir_block *self, const char *name)
*IR Instructions *IR Instructions
*/ */
static ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op) static ir_instr* ir_instr_new(lex_ctx_t ctx, ir_block* owner, int op)
{ {
ir_instr *self; ir_instr *self;
self = (ir_instr*)mem_a(sizeof(*self)); self = (ir_instr*)mem_a(sizeof(*self));
@ -1216,7 +1216,7 @@ bool ir_value_set_func(ir_value *self, int f)
return true; return true;
} }
bool ir_value_set_vector(ir_value *self, vector v) bool ir_value_set_vector(ir_value *self, vec3_t v)
{ {
if (self->vtype != TYPE_VECTOR) if (self->vtype != TYPE_VECTOR)
return false; return false;
@ -1482,7 +1482,7 @@ static bool ir_check_unreachable(ir_block *self)
return false; return false;
} }
bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what) bool ir_block_create_store_op(ir_block *self, lex_ctx_t ctx, int op, ir_value *target, ir_value *what)
{ {
ir_instr *in; ir_instr *in;
if (!ir_check_unreachable(self)) if (!ir_check_unreachable(self))
@ -1511,7 +1511,7 @@ bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *tar
return true; return true;
} }
static bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what) static bool ir_block_create_store(ir_block *self, lex_ctx_t ctx, ir_value *target, ir_value *what)
{ {
int op = 0; int op = 0;
int vtype; int vtype;
@ -1536,7 +1536,7 @@ static bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target,
return ir_block_create_store_op(self, ctx, op, target, what); return ir_block_create_store_op(self, ctx, op, target, what);
} }
bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what) bool ir_block_create_storep(ir_block *self, lex_ctx_t ctx, ir_value *target, ir_value *what)
{ {
int op = 0; int op = 0;
int vtype; int vtype;
@ -1558,7 +1558,7 @@ bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_va
return ir_block_create_store_op(self, ctx, op, target, what); return ir_block_create_store_op(self, ctx, op, target, what);
} }
bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v) bool ir_block_create_return(ir_block *self, lex_ctx_t ctx, ir_value *v)
{ {
ir_instr *in; ir_instr *in;
if (!ir_check_unreachable(self)) if (!ir_check_unreachable(self))
@ -1578,7 +1578,7 @@ bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
return true; return true;
} }
bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v, bool ir_block_create_if(ir_block *self, lex_ctx_t ctx, ir_value *v,
ir_block *ontrue, ir_block *onfalse) ir_block *ontrue, ir_block *onfalse)
{ {
ir_instr *in; ir_instr *in;
@ -1607,7 +1607,7 @@ bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
return true; return true;
} }
bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to) bool ir_block_create_jump(ir_block *self, lex_ctx_t ctx, ir_block *to)
{ {
ir_instr *in; ir_instr *in;
if (!ir_check_unreachable(self)) if (!ir_check_unreachable(self))
@ -1625,13 +1625,13 @@ bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
return true; return true;
} }
bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to) bool ir_block_create_goto(ir_block *self, lex_ctx_t ctx, ir_block *to)
{ {
self->owner->flags |= IR_FLAG_HAS_GOTO; self->owner->flags |= IR_FLAG_HAS_GOTO;
return ir_block_create_jump(self, ctx, to); return ir_block_create_jump(self, ctx, to);
} }
ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot) ir_instr* ir_block_create_phi(ir_block *self, lex_ctx_t ctx, const char *label, int ot)
{ {
ir_value *out; ir_value *out;
ir_instr *in; ir_instr *in;
@ -1678,7 +1678,7 @@ void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
} }
/* call related code */ /* call related code */
ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn) ir_instr* ir_block_create_call(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *func, bool noreturn)
{ {
ir_value *out; ir_value *out;
ir_instr *in; ir_instr *in;
@ -1729,7 +1729,7 @@ void ir_call_param(ir_instr* self, ir_value *v)
/* binary op related code */ /* binary op related code */
ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx, ir_value* ir_block_create_binop(ir_block *self, lex_ctx_t ctx,
const char *label, int opcode, const char *label, int opcode,
ir_value *left, ir_value *right) ir_value *left, ir_value *right)
{ {
@ -1840,7 +1840,7 @@ ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot); return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
} }
ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx, ir_value* ir_block_create_unary(ir_block *self, lex_ctx_t ctx,
const char *label, int opcode, const char *label, int opcode,
ir_value *operand) ir_value *operand)
{ {
@ -1873,7 +1873,7 @@ ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot); return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
} }
static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label, static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx_t ctx, const char *label,
int op, ir_value *a, ir_value *b, int outype) int op, ir_value *a, ir_value *b, int outype)
{ {
ir_instr *instr; ir_instr *instr;
@ -1905,7 +1905,7 @@ on_error:
return NULL; return NULL;
} }
ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field) ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *ent, ir_value *field)
{ {
ir_value *v; ir_value *v;
@ -1921,7 +1921,7 @@ ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *
return v; return v;
} }
ir_value* ir_block_create_load_from_ent(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field, int outype) ir_value* ir_block_create_load_from_ent(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *ent, ir_value *field, int outype)
{ {
int op; int op;
if (ent->vtype != TYPE_ENTITY) if (ent->vtype != TYPE_ENTITY)
@ -2741,7 +2741,7 @@ static bool gen_global_pointer(code_t *code, ir_value *global)
static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *block) static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *block)
{ {
prog_section_statement stmt; prog_section_statement_t stmt;
ir_instr *instr; ir_instr *instr;
ir_block *target; ir_block *target;
ir_block *ontrue; ir_block *ontrue;
@ -3002,7 +3002,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc
static bool gen_function_code(code_t *code, ir_function *self) static bool gen_function_code(code_t *code, ir_function *self)
{ {
ir_block *block; ir_block *block;
prog_section_statement stmt, *retst; prog_section_statement_t stmt, *retst;
/* Starting from entry point, we generate blocks "as they come" /* Starting from entry point, we generate blocks "as they come"
* for now. Dead blocks will not be translated obviously. * for now. Dead blocks will not be translated obviously.
@ -3040,13 +3040,13 @@ static bool gen_function_code(code_t *code, ir_function *self)
return true; return true;
} }
static qcint ir_builder_filestring(ir_builder *ir, const char *filename) static qcint_t ir_builder_filestring(ir_builder *ir, const char *filename)
{ {
/* NOTE: filename pointers are copied, we never strdup them, /* NOTE: filename pointers are copied, we never strdup them,
* thus we can use pointer-comparison to find the string. * thus we can use pointer-comparison to find the string.
*/ */
size_t i; size_t i;
qcint str; qcint_t str;
for (i = 0; i < vec_size(ir->filenames); ++i) { for (i = 0; i < vec_size(ir->filenames); ++i) {
if (ir->filenames[i] == filename) if (ir->filenames[i] == filename)
@ -3061,8 +3061,8 @@ static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
static bool gen_global_function(ir_builder *ir, ir_value *global) static bool gen_global_function(ir_builder *ir, ir_value *global)
{ {
prog_section_function fun; prog_section_function_t fun;
ir_function *irfun; ir_function *irfun;
size_t i; size_t i;
@ -3116,8 +3116,8 @@ static ir_value* ir_gen_extparam_proto(ir_builder *ir)
static void ir_gen_extparam(ir_builder *ir) static void ir_gen_extparam(ir_builder *ir)
{ {
prog_section_def def; prog_section_def_t def;
ir_value *global; ir_value *global;
if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1) if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
global = ir_gen_extparam_proto(ir); global = ir_gen_extparam_proto(ir);
@ -3145,7 +3145,7 @@ static bool gen_function_extparam_copy(code_t *code, ir_function *self)
ir_builder *ir = self->owner; ir_builder *ir = self->owner;
ir_value *ep; ir_value *ep;
prog_section_statement stmt; prog_section_statement_t stmt;
numparams = vec_size(self->params); numparams = vec_size(self->params);
if (!numparams) if (!numparams)
@ -3180,7 +3180,7 @@ static bool gen_function_varargs_copy(code_t *code, ir_function *self)
ir_builder *ir = self->owner; ir_builder *ir = self->owner;
ir_value *ep; ir_value *ep;
prog_section_statement stmt; prog_section_statement_t stmt;
numparams = vec_size(self->params); numparams = vec_size(self->params);
if (!numparams) if (!numparams)
@ -3212,10 +3212,10 @@ static bool gen_function_varargs_copy(code_t *code, ir_function *self)
static bool gen_function_locals(ir_builder *ir, ir_value *global) static bool gen_function_locals(ir_builder *ir, ir_value *global)
{ {
prog_section_function *def; prog_section_function_t *def;
ir_function *irfun; ir_function *irfun;
size_t i; size_t i;
uint32_t firstlocal, firstglobal; uint32_t firstlocal, firstglobal;
irfun = global->constval.vfunc; irfun = global->constval.vfunc;
def = ir->code->functions + irfun->code_function_def; def = ir->code->functions + irfun->code_function_def;
@ -3261,8 +3261,8 @@ static bool gen_function_locals(ir_builder *ir, ir_value *global)
static bool gen_global_function_code(ir_builder *ir, ir_value *global) static bool gen_global_function_code(ir_builder *ir, ir_value *global)
{ {
prog_section_function *fundef; prog_section_function_t *fundef;
ir_function *irfun; ir_function *irfun;
(void)ir; (void)ir;
@ -3305,7 +3305,7 @@ static bool gen_global_function_code(ir_builder *ir, ir_value *global)
return true; return true;
} }
static void gen_vector_defs(code_t *code, prog_section_def def, const char *name) static void gen_vector_defs(code_t *code, prog_section_def_t def, const char *name)
{ {
char *component; char *component;
size_t len, i; size_t len, i;
@ -3335,7 +3335,7 @@ static void gen_vector_defs(code_t *code, prog_section_def def, const char *name
mem_d(component); mem_d(component);
} }
static void gen_vector_fields(code_t *code, prog_section_field fld, const char *name) static void gen_vector_fields(code_t *code, prog_section_field_t fld, const char *name)
{ {
char *component; char *component;
size_t len, i; size_t len, i;
@ -3367,10 +3367,10 @@ static void gen_vector_fields(code_t *code, prog_section_field fld, const char *
static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal) static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
{ {
size_t i; size_t i;
int32_t *iptr; int32_t *iptr;
prog_section_def def; prog_section_def_t def;
bool pushdef = opts.optimizeoff; bool pushdef = opts.optimizeoff;
def.type = global->vtype; def.type = global->vtype;
def.offset = vec_size(self->code->globals); def.offset = vec_size(self->code->globals);
@ -3543,8 +3543,8 @@ static GMQCC_INLINE void ir_builder_prepare_field(code_t *code, ir_value *field)
static bool ir_builder_gen_field(ir_builder *self, ir_value *field) static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
{ {
prog_section_def def; prog_section_def_t def;
prog_section_field fld; prog_section_field_t fld;
(void)self; (void)self;
@ -3613,7 +3613,7 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
bool ir_builder_generate(ir_builder *self, const char *filename) bool ir_builder_generate(ir_builder *self, const char *filename)
{ {
prog_section_statement stmt; prog_section_statement_t stmt;
size_t i; size_t i;
char *lnofile = NULL; char *lnofile = NULL;

46
ir.h
View file

@ -36,7 +36,7 @@ typedef struct ir_value_s {
char *name; char *name;
int vtype; int vtype;
int store; int store;
lex_ctx context; lex_ctx_t context;
/* even the IR knows the subtype of a field */ /* even the IR knows the subtype of a field */
int fieldtype; int fieldtype;
/* and the output type of a function */ /* and the output type of a function */
@ -53,7 +53,7 @@ typedef struct ir_value_s {
union { union {
float vfloat; float vfloat;
int vint; int vint;
vector vvec; vec3_t vvec;
int32_t ivec[3]; int32_t ivec[3];
char *vstring; char *vstring;
struct ir_value_s *vpointer; struct ir_value_s *vpointer;
@ -94,7 +94,7 @@ ir_value* ir_value_vector_member(ir_value*, unsigned int member);
bool GMQCC_WARN ir_value_set_float(ir_value*, float f); bool GMQCC_WARN ir_value_set_float(ir_value*, float f);
bool GMQCC_WARN ir_value_set_func(ir_value*, int f); bool GMQCC_WARN ir_value_set_func(ir_value*, int f);
bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s); bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s);
bool GMQCC_WARN ir_value_set_vector(ir_value*, vector v); bool GMQCC_WARN ir_value_set_vector(ir_value*, vec3_t v);
bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld); bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld);
bool ir_value_lives(ir_value*, size_t); bool ir_value_lives(ir_value*, size_t);
void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...)); void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...));
@ -110,7 +110,7 @@ typedef struct ir_phi_entry_s
typedef struct ir_instr_s typedef struct ir_instr_s
{ {
int opcode; int opcode;
lex_ctx context; lex_ctx_t context;
ir_value* (_ops[3]); ir_value* (_ops[3]);
struct ir_block_s* (bops[2]); struct ir_block_s* (bops[2]);
@ -130,7 +130,7 @@ typedef struct ir_instr_s
typedef struct ir_block_s typedef struct ir_block_s
{ {
char *label; char *label;
lex_ctx context; lex_ctx_t context;
bool final; /* once a jump is added we're done */ bool final; /* once a jump is added we're done */
ir_instr **instr; ir_instr **instr;
@ -149,26 +149,26 @@ typedef struct ir_block_s
size_t code_start; size_t code_start;
} ir_block; } ir_block;
ir_value* ir_block_create_binop(ir_block*, lex_ctx, const char *label, int op, ir_value *left, ir_value *right); ir_value* ir_block_create_binop(ir_block*, lex_ctx_t, const char *label, int op, ir_value *left, ir_value *right);
ir_value* ir_block_create_unary(ir_block*, lex_ctx, const char *label, int op, ir_value *operand); ir_value* ir_block_create_unary(ir_block*, lex_ctx_t, const char *label, int op, ir_value *operand);
bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx, int op, ir_value *target, ir_value *what); bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx_t, int op, ir_value *target, ir_value *what);
bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx, ir_value *target, ir_value *what); bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
ir_value* ir_block_create_load_from_ent(ir_block*, lex_ctx, const char *label, ir_value *ent, ir_value *field, int outype); ir_value* ir_block_create_load_from_ent(ir_block*, lex_ctx_t, const char *label, ir_value *ent, ir_value *field, int outype);
ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx, const char *label, ir_value *entity, ir_value *field); ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx_t, const char *label, ir_value *entity, ir_value *field);
/* This is to create an instruction of the form /* This is to create an instruction of the form
* <outtype>%label := opcode a, b * <outtype>%label := opcode a, b
*/ */
ir_instr* ir_block_create_phi(ir_block*, lex_ctx, const char *label, int vtype); ir_instr* ir_block_create_phi(ir_block*, lex_ctx_t, const char *label, int vtype);
ir_value* ir_phi_value(ir_instr*); ir_value* ir_phi_value(ir_instr*);
void ir_phi_add(ir_instr*, ir_block *b, ir_value *v); void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
ir_instr* ir_block_create_call(ir_block*, lex_ctx, const char *label, ir_value *func, bool noreturn); ir_instr* ir_block_create_call(ir_block*, lex_ctx_t, const char *label, ir_value *func, bool noreturn);
ir_value* ir_call_value(ir_instr*); ir_value* ir_call_value(ir_instr*);
void ir_call_param(ir_instr*, ir_value*); void ir_call_param(ir_instr*, ir_value*);
bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx, ir_value *opt_value); bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx, ir_value *cond, bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
ir_block *ontrue, ir_block *onfalse); ir_block *ontrue, ir_block *onfalse);
/* A 'goto' is an actual 'goto' coded in QC, whereas /* A 'goto' is an actual 'goto' coded in QC, whereas
* a 'jump' is a virtual construct which simply names the * a 'jump' is a virtual construct which simply names the
@ -176,8 +176,8 @@ bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx, ir_value *cond,
* A goto usually becomes an OP_GOTO in the resulting code, * A goto usually becomes an OP_GOTO in the resulting code,
* whereas a 'jump' usually doesn't add any actual instruction. * whereas a 'jump' usually doesn't add any actual instruction.
*/ */
bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx, ir_block *to); bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx_t, ir_block *to);
bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx, ir_block *to); bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx_t, ir_block *to);
/* function */ /* function */
typedef struct ir_function_s typedef struct ir_function_s
@ -208,7 +208,7 @@ typedef struct ir_function_s
ir_block* first; ir_block* first;
ir_block* last; ir_block* last;
lex_ctx context; lex_ctx_t context;
/* for prototypes - first we generate all the /* for prototypes - first we generate all the
* globals, and we remember teh function-defs * globals, and we remember teh function-defs
@ -216,7 +216,7 @@ typedef struct ir_function_s
* *
* remember the ID: * remember the ID:
*/ */
qcint code_function_def; qcint_t code_function_def;
/* for temp allocation */ /* for temp allocation */
size_t run_id; size_t run_id;
@ -236,7 +236,7 @@ typedef struct ir_function_s
ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param); ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param);
bool GMQCC_WARN ir_function_finalize(ir_function*); bool GMQCC_WARN ir_function_finalize(ir_function*);
ir_block* ir_function_create_block(lex_ctx ctx, ir_function*, const char *label); ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function*, const char *label);
/* builder */ /* builder */
#define IR_HT_SIZE 1024 #define IR_HT_SIZE 1024
@ -261,9 +261,9 @@ typedef struct ir_builder_s
uint32_t first_common_globaltemp; uint32_t first_common_globaltemp;
const char **filenames; const char **filenames;
qcint *filestrings; qcint_t *filestrings;
/* we cache the #IMMEDIATE string here */ /* we cache the #IMMEDIATE string here */
qcint str_immediate; qcint_t str_immediate;
/* there should just be this one nil */ /* there should just be this one nil */
ir_value *nil; ir_value *nil;
ir_value *reserved_va_count; ir_value *reserved_va_count;
@ -287,6 +287,6 @@ void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
* for some reason :P * for some reason :P
*/ */
typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4)?1:-1]; typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4)?1:-1];
typedef int static_assert_is_32bit_integer[(sizeof(qcfloat) == 4)?1:-1]; typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_t) == 4)?1:-1];
#endif #endif

View file

@ -71,9 +71,9 @@ static void lexerror(lex_file *lex, const char *fmt, ...)
static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...) static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
{ {
bool r; bool r;
lex_ctx ctx; lex_ctx_t ctx;
va_list ap; va_list ap;
ctx.file = lex->name; ctx.file = lex->name;
ctx.line = lex->sline; ctx.line = lex->sline;

View file

@ -30,7 +30,7 @@ struct token_s {
char *value; char *value;
union { union {
vector v; vec3_t v;
int i; int i;
double f; double f;
int t; /* type */ int t; /* type */
@ -41,7 +41,7 @@ struct token_s {
struct token_s *prev; struct token_s *prev;
#endif #endif
lex_ctx ctx; lex_ctx_t ctx;
}; };
#if 0 #if 0

8
opts.c
View file

@ -33,21 +33,21 @@ const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1] = {
0 0
}; };
const opts_flag_def opts_opt_list[COUNT_OPTIMIZATIONS+1] = { const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1] = {
# define GMQCC_TYPE_OPTIMIZATIONS # define GMQCC_TYPE_OPTIMIZATIONS
# define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) }, # define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
# include "opts.def" # include "opts.def"
{ NULL, LONGBIT(0) } { NULL, LONGBIT(0) }
}; };
const opts_flag_def opts_warn_list[COUNT_WARNINGS+1] = { const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1] = {
# define GMQCC_TYPE_WARNS # define GMQCC_TYPE_WARNS
# define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) }, # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
# include "opts.def" # include "opts.def"
{ NULL, LONGBIT(0) } { NULL, LONGBIT(0) }
}; };
const opts_flag_def opts_flag_list[COUNT_FLAGS+1] = { const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1] = {
# define GMQCC_TYPE_FLAGS # define GMQCC_TYPE_FLAGS
# define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) }, # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
# include "opts.def" # include "opts.def"
@ -132,7 +132,7 @@ void opts_init(const char *output, int standard, size_t arraysize) {
OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = 16; OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = 16;
} }
static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) { static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
size_t i; size_t i;
for (i = 0; i < listsize; ++i) { for (i = 0; i < listsize; ++i) {

106
parser.c
View file

@ -92,7 +92,7 @@ typedef struct parser_s {
size_t *_blocklocals; size_t *_blocklocals;
ast_value **_typedefs; ast_value **_typedefs;
size_t *_blocktypedefs; size_t *_blocktypedefs;
lex_ctx *_block_ctx; lex_ctx_t *_block_ctx;
/* we store the '=' operator info */ /* we store the '=' operator info */
const oper_info *assign_op; const oper_info *assign_op;
@ -148,32 +148,32 @@ static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *
* some maths used for constant folding * some maths used for constant folding
*/ */
vector vec3_add(vector a, vector b) vec3_t vec3_add(vec3_t a, vec3_t b)
{ {
vector out; vec3_t out;
out.x = a.x + b.x; out.x = a.x + b.x;
out.y = a.y + b.y; out.y = a.y + b.y;
out.z = a.z + b.z; out.z = a.z + b.z;
return out; return out;
} }
vector vec3_sub(vector a, vector b) vec3_t vec3_sub(vec3_t a, vec3_t b)
{ {
vector out; vec3_t out;
out.x = a.x - b.x; out.x = a.x - b.x;
out.y = a.y - b.y; out.y = a.y - b.y;
out.z = a.z - b.z; out.z = a.z - b.z;
return out; return out;
} }
qcfloat vec3_mulvv(vector a, vector b) qcfloat_t vec3_mulvv(vec3_t a, vec3_t b)
{ {
return (a.x * b.x + a.y * b.y + a.z * b.z); return (a.x * b.x + a.y * b.y + a.z * b.z);
} }
vector vec3_mulvf(vector a, float b) vec3_t vec3_mulvf(vec3_t a, float b)
{ {
vector out; vec3_t out;
out.x = a.x * b; out.x = a.x * b;
out.y = a.y * b; out.y = a.y * b;
out.z = a.z * b; out.z = a.z * b;
@ -205,7 +205,7 @@ static ast_value* parser_const_float(parser_t *parser, double d)
{ {
size_t i; size_t i;
ast_value *out; ast_value *out;
lex_ctx ctx; lex_ctx_t ctx;
for (i = 0; i < vec_size(parser->imm_float); ++i) { for (i = 0; i < vec_size(parser->imm_float); ++i) {
const double compare = parser->imm_float[i]->constval.vfloat; const double compare = parser->imm_float[i]->constval.vfloat;
if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0) if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
@ -291,7 +291,7 @@ static ast_value* parser_const_string(parser_t *parser, const char *str, bool do
return out; return out;
} }
static ast_value* parser_const_vector(parser_t *parser, vector v) static ast_value* parser_const_vector(parser_t *parser, vec3_t v)
{ {
size_t i; size_t i;
ast_value *out; ast_value *out;
@ -310,7 +310,7 @@ static ast_value* parser_const_vector(parser_t *parser, vector v)
static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z) static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
{ {
vector v; vec3_t v;
v.x = x; v.x = x;
v.y = y; v.y = y;
v.z = z; v.z = z;
@ -410,7 +410,7 @@ typedef struct
size_t off; size_t off;
ast_expression *out; ast_expression *out;
ast_block *block; /* for commas and function calls */ ast_block *block; /* for commas and function calls */
lex_ctx ctx; lex_ctx_t ctx;
} sy_elem; } sy_elem;
enum { enum {
@ -428,7 +428,7 @@ typedef struct
unsigned int *paren; unsigned int *paren;
} shunt; } shunt;
static sy_elem syexp(lex_ctx ctx, ast_expression *v) { static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
sy_elem e; sy_elem e;
e.etype = 0; e.etype = 0;
e.off = 0; e.off = 0;
@ -439,7 +439,7 @@ static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
return e; return e;
} }
static sy_elem syblock(lex_ctx ctx, ast_block *v) { static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
sy_elem e; sy_elem e;
e.etype = 0; e.etype = 0;
e.off = 0; e.off = 0;
@ -450,7 +450,7 @@ static sy_elem syblock(lex_ctx ctx, ast_block *v) {
return e; return e;
} }
static sy_elem syop(lex_ctx ctx, const oper_info *op) { static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
sy_elem e; sy_elem e;
e.etype = 1 + (op - operators); e.etype = 1 + (op - operators);
e.off = 0; e.off = 0;
@ -461,7 +461,7 @@ static sy_elem syop(lex_ctx ctx, const oper_info *op) {
return e; return e;
} }
static sy_elem syparen(lex_ctx ctx, size_t off) { static sy_elem syparen(lex_ctx_t ctx, size_t off) {
sy_elem e; sy_elem e;
e.etype = 0; e.etype = 0;
e.off = off; e.off = off;
@ -484,7 +484,7 @@ static bool rotate_entfield_array_index_nodes(ast_expression **out)
ast_expression *sub; ast_expression *sub;
ast_expression *entity; ast_expression *entity;
lex_ctx ctx = ast_ctx(*out); lex_ctx_t ctx = ast_ctx(*out);
if (!ast_istype(*out, ast_array_index)) if (!ast_istype(*out, ast_array_index))
return false; return false;
@ -514,7 +514,7 @@ static bool rotate_entfield_array_index_nodes(ast_expression **out)
return true; return true;
} }
static bool immediate_is_true(lex_ctx ctx, ast_value *v) static bool immediate_is_true(lex_ctx_t ctx, ast_value *v)
{ {
switch (v->expression.vtype) { switch (v->expression.vtype) {
case TYPE_FLOAT: case TYPE_FLOAT:
@ -543,14 +543,14 @@ static bool immediate_is_true(lex_ctx ctx, ast_value *v)
static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
{ {
const oper_info *op; const oper_info *op;
lex_ctx ctx; lex_ctx_t ctx;
ast_expression *out = NULL; ast_expression *out = NULL;
ast_expression *exprs[3]; ast_expression *exprs[3];
ast_block *blocks[3]; ast_block *blocks[3];
ast_value *asvalue[3]; ast_value *asvalue[3];
ast_binstore *asbinstore; ast_binstore *asbinstore;
size_t i, assignop, addop, subop; size_t i, assignop, addop, subop;
qcint generated_op = 0; qcint_t generated_op = 0;
char ty1[1024]; char ty1[1024];
char ty2[1024]; char ty2[1024];
@ -878,7 +878,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
if (CanConstFold(exprs[0], exprs[1])) if (CanConstFold(exprs[0], exprs[1]))
out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1))); out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[0])) { else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[0])) {
vector vec = ConstV(0); vec3_t vec = ConstV(0);
if (!vec.y && !vec.z) { /* 'n 0 0' * v */ if (!vec.y && !vec.z) { /* 'n 0 0' * v */
++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS]; ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
out = (ast_expression*)ast_member_new(ctx, exprs[1], 0, NULL); out = (ast_expression*)ast_member_new(ctx, exprs[1], 0, NULL);
@ -907,7 +907,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]); out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
} }
else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[1])) { else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[1])) {
vector vec = ConstV(1); vec3_t vec = ConstV(1);
if (!vec.y && !vec.z) { /* v * 'n 0 0' */ if (!vec.y && !vec.z) { /* v * 'n 0 0' */
++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS]; ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL); out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
@ -995,7 +995,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
} }
if (CanConstFold(exprs[0], exprs[1])) { if (CanConstFold(exprs[0], exprs[1])) {
out = (ast_expression*)parser_const_float(parser, out = (ast_expression*)parser_const_float(parser,
(float)(((qcint)ConstF(0)) % ((qcint)ConstF(1)))); (float)(((qcint_t)ConstF(0)) % ((qcint_t)ConstF(1))));
} else { } else {
/* generate a call to __builtin_mod */ /* generate a call to __builtin_mod */
ast_expression *mod = intrin_func(parser, "mod"); ast_expression *mod = intrin_func(parser, "mod");
@ -1024,8 +1024,8 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
} }
if (CanConstFold(exprs[0], exprs[1])) if (CanConstFold(exprs[0], exprs[1]))
out = (ast_expression*)parser_const_float(parser, out = (ast_expression*)parser_const_float(parser,
(op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) : (op->id == opid1('|') ? (float)( ((qcint_t)ConstF(0)) | ((qcint_t)ConstF(1)) ) :
(float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) )); (float)( ((qcint_t)ConstF(0)) & ((qcint_t)ConstF(1)) ) ));
else else
out = (ast_expression*)ast_binary_new(ctx, out = (ast_expression*)ast_binary_new(ctx,
(op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND), (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
@ -1085,7 +1085,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
*/ */
if (exprs[0]->vtype == TYPE_FLOAT) { if (exprs[0]->vtype == TYPE_FLOAT) {
if(CanConstFold(exprs[0], exprs[1])) { if(CanConstFold(exprs[0], exprs[1])) {
out = (ast_expression*)parser_const_float(parser, (float)((qcint)(ConstF(0)) ^ ((qcint)(ConstF(1))))); out = (ast_expression*)parser_const_float(parser, (float)((qcint_t)(ConstF(0)) ^ ((qcint_t)(ConstF(1)))));
} else { } else {
ast_binary *expr = ast_binary_new( ast_binary *expr = ast_binary_new(
ctx, ctx,
@ -1126,9 +1126,9 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
if (CanConstFold(exprs[0], exprs[1])) { if (CanConstFold(exprs[0], exprs[1])) {
out = (ast_expression*)parser_const_vector_f( out = (ast_expression*)parser_const_vector_f(
parser, parser,
(float)(((qcint)(ConstV(0).x)) ^ ((qcint)(ConstV(1).x))), (float)(((qcint_t)(ConstV(0).x)) ^ ((qcint_t)(ConstV(1).x))),
(float)(((qcint)(ConstV(0).y)) ^ ((qcint)(ConstV(1).y))), (float)(((qcint_t)(ConstV(0).y)) ^ ((qcint_t)(ConstV(1).y))),
(float)(((qcint)(ConstV(0).z)) ^ ((qcint)(ConstV(1).z))) (float)(((qcint_t)(ConstV(0).z)) ^ ((qcint_t)(ConstV(1).z)))
); );
} else { } else {
compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against vector"); compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against vector");
@ -1142,9 +1142,9 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
if (CanConstFold(exprs[0], exprs[1])) { if (CanConstFold(exprs[0], exprs[1])) {
out = (ast_expression*)parser_const_vector_f( out = (ast_expression*)parser_const_vector_f(
parser, parser,
(float)(((qcint)(ConstV(0).x)) ^ ((qcint)(ConstF(1)))), (float)(((qcint_t)(ConstV(0).x)) ^ ((qcint_t)(ConstF(1)))),
(float)(((qcint)(ConstV(0).y)) ^ ((qcint)(ConstF(1)))), (float)(((qcint_t)(ConstV(0).y)) ^ ((qcint_t)(ConstF(1)))),
(float)(((qcint)(ConstV(0).z)) ^ ((qcint)(ConstF(1)))) (float)(((qcint_t)(ConstV(0).z)) ^ ((qcint_t)(ConstF(1))))
); );
} else { } else {
compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against float"); compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against float");
@ -1603,7 +1603,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
} }
if(CanConstFold1(exprs[0])) if(CanConstFold1(exprs[0]))
out = (ast_expression*)parser_const_float(parser, ~(qcint)ConstF(0)); out = (ast_expression*)parser_const_float(parser, ~(qcint_t)ConstF(0));
else else
out = (ast_expression*) out = (ast_expression*)
ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]); ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]);
@ -1823,7 +1823,7 @@ static ast_expression* parse_vararg_do(parser_t *parser)
ast_expression *idx, *out; ast_expression *idx, *out;
ast_value *typevar; ast_value *typevar;
ast_value *funtype = parser->function->vtype; ast_value *funtype = parser->function->vtype;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
if (!parser->function->varargs) { if (!parser->function->varargs) {
parseerror(parser, "function has no variable argument list"); parseerror(parser, "function has no variable argument list");
@ -2538,7 +2538,7 @@ static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
ast_expression *cond, *ontrue = NULL, *onfalse = NULL; ast_expression *cond, *ontrue = NULL, *onfalse = NULL;
bool ifnot = false; bool ifnot = false;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
(void)block; /* not touching */ (void)block; /* not touching */
@ -2677,7 +2677,7 @@ static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **
bool ifnot = false; bool ifnot = false;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
(void)block; /* not touching */ (void)block; /* not touching */
@ -2778,7 +2778,7 @@ static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression
bool ifnot = false; bool ifnot = false;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
(void)block; /* not touching */ (void)block; /* not touching */
@ -2903,7 +2903,7 @@ static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **ou
bool ifnot = false; bool ifnot = false;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
parser_enterblock(parser); parser_enterblock(parser);
@ -2969,7 +2969,7 @@ static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **ou
/* parse the incrementor */ /* parse the incrementor */
if (parser->tok != ')') { if (parser->tok != ')') {
lex_ctx condctx = parser_ctx(parser); lex_ctx_t condctx = parser_ctx(parser);
increment = parse_expression_leave(parser, false, false, false); increment = parse_expression_leave(parser, false, false, false);
if (!increment) if (!increment)
goto onerr; goto onerr;
@ -3021,7 +3021,7 @@ static bool parse_return(parser_t *parser, ast_block *block, ast_expression **ou
ast_value *retval = parser->function->return_value; ast_value *retval = parser->function->return_value;
ast_value *expected = parser->function->vtype; ast_value *expected = parser->function->vtype;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
(void)block; /* not touching */ (void)block; /* not touching */
@ -3120,7 +3120,7 @@ static bool parse_break_continue(parser_t *parser, ast_block *block, ast_express
{ {
size_t i; size_t i;
unsigned int levels = 0; unsigned int levels = 0;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
const char **loops = (is_continue ? parser->continues : parser->breaks); const char **loops = (is_continue ? parser->continues : parser->breaks);
(void)block; /* not touching */ (void)block; /* not touching */
@ -3407,7 +3407,7 @@ static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression *
bool noref, is_static; bool noref, is_static;
uint32_t qflags = 0; uint32_t qflags = 0;
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
(void)block; /* not touching */ (void)block; /* not touching */
(void)opval; (void)opval;
@ -3919,7 +3919,7 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression *
} }
else else
{ {
lex_ctx ctx = parser_ctx(parser); lex_ctx_t ctx = parser_ctx(parser);
ast_expression *exp = parse_expression(parser, false, false); ast_expression *exp = parse_expression(parser, false, false);
if (!exp) if (!exp)
return false; return false;
@ -3936,7 +3936,7 @@ static bool parse_enum(parser_t *parser)
{ {
bool flag = false; bool flag = false;
bool reverse = false; bool reverse = false;
qcfloat num = 0; qcfloat_t num = 0;
ast_value **values = NULL; ast_value **values = NULL;
ast_value *var = NULL; ast_value *var = NULL;
ast_value *asvalue; ast_value *asvalue;
@ -4318,7 +4318,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
} }
if (has_frame_think) { if (has_frame_think) {
lex_ctx ctx; lex_ctx_t ctx;
ast_expression *self_frame; ast_expression *self_frame;
ast_expression *self_nextthink; ast_expression *self_nextthink;
ast_expression *self_think; ast_expression *self_think;
@ -4498,7 +4498,7 @@ static ast_expression *array_accessor_split(
ast_ifthen *ifthen; ast_ifthen *ifthen;
ast_binary *cmp; ast_binary *cmp;
lex_ctx ctx = ast_ctx(array); lex_ctx_t ctx = ast_ctx(array);
if (!left || !right) { if (!left || !right) {
if (left) ast_delete(left); if (left) ast_delete(left);
@ -4528,7 +4528,7 @@ static ast_expression *array_accessor_split(
static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend) static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
{ {
lex_ctx ctx = ast_ctx(array); lex_ctx_t ctx = ast_ctx(array);
if (from+1 == afterend) { if (from+1 == afterend) {
/* set this value */ /* set this value */
@ -4593,7 +4593,7 @@ static ast_expression *array_field_setter_node(
size_t from, size_t from,
size_t afterend) size_t afterend)
{ {
lex_ctx ctx = ast_ctx(array); lex_ctx_t ctx = ast_ctx(array);
if (from+1 == afterend) { if (from+1 == afterend) {
/* set this value */ /* set this value */
@ -4664,7 +4664,7 @@ static ast_expression *array_field_setter_node(
static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend) static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
{ {
lex_ctx ctx = ast_ctx(array); lex_ctx_t ctx = ast_ctx(array);
if (from+1 == afterend) { if (from+1 == afterend) {
ast_return *ret; ast_return *ret;
@ -4900,7 +4900,7 @@ static bool parser_create_array_getter(parser_t *parser, ast_value *array, const
static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef); static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
static ast_value *parse_parameter_list(parser_t *parser, ast_value *var) static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
{ {
lex_ctx ctx; lex_ctx_t ctx;
size_t i; size_t i;
ast_value **params; ast_value **params;
ast_value *param; ast_value *param;
@ -5027,7 +5027,7 @@ static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
{ {
ast_expression *cexp; ast_expression *cexp;
ast_value *cval, *tmp; ast_value *cval, *tmp;
lex_ctx ctx; lex_ctx_t ctx;
ctx = parser_ctx(parser); ctx = parser_ctx(parser);
@ -5106,7 +5106,7 @@ static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef) static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
{ {
ast_value *var, *tmp; ast_value *var, *tmp;
lex_ctx ctx; lex_ctx_t ctx;
const char *name = NULL; const char *name = NULL;
bool isfield = false; bool isfield = false;
@ -6277,7 +6277,7 @@ static void generate_checksum(parser_t *parser, ir_builder *ir)
parser_t *parser_create() parser_t *parser_create()
{ {
parser_t *parser; parser_t *parser;
lex_ctx empty_ctx; lex_ctx_t empty_ctx;
size_t i; size_t i;
parser = (parser_t*)mem_a(sizeof(parser_t)); parser = (parser_t*)mem_a(sizeof(parser_t));