From: Dale Weiler Date: Sun, 20 Oct 2013 03:39:37 +0000 (-0400) Subject: Some correctly-sized type flags for the AST and IR, this culls back memory usage... X-Git-Tag: 0.3.5~1^2~6 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=c90807dec6a7094cfbae116c6c0404661c5fc6d8 Some correctly-sized type flags for the AST and IR, this culls back memory usage a little. --- diff --git a/ast.h b/ast.h index 1dbfa72..52858ac 100644 --- a/ast.h +++ b/ast.h @@ -25,6 +25,8 @@ #define GMQCC_AST_HDR #include "ir.h" +typedef uint16_t ast_flag_t; + /* Note: I will not be using a _t suffix for the * "main" ast node types for now. */ @@ -53,6 +55,28 @@ typedef struct ast_label_s ast_label; typedef struct ast_goto_s ast_goto; typedef struct ast_argpipe_s ast_argpipe; +enum { + AST_FLAG_VARIADIC = 1 << 0, + AST_FLAG_NORETURN = 1 << 1, + AST_FLAG_INLINE = 1 << 2, + AST_FLAG_INITIALIZED = 1 << 3, + AST_FLAG_DEPRECATED = 1 << 4, + AST_FLAG_INCLUDE_DEF = 1 << 5, + AST_FLAG_IS_VARARG = 1 << 6, + AST_FLAG_ALIAS = 1 << 7, + AST_FLAG_ERASEABLE = 1 << 8, + AST_FLAG_ACCUMULATE = 1 << 9, + + /* + * An array declared as [] + * so that the size is taken from the initializer + */ + AST_FLAG_ARRAY_INIT = 1 << 10, + + AST_FLAG_LAST, + AST_FLAG_TYPE_MASK = (AST_FLAG_VARIADIC | AST_FLAG_NORETURN) +}; + enum { TYPE_ast_node, /* 0 */ TYPE_ast_expression, /* 1 */ @@ -134,7 +158,7 @@ struct ast_expression_common /* arrays get a member-count */ size_t count; ast_value* *params; - uint32_t flags; + ast_flag_t flags; /* void foo(string...) gets varparam set as a restriction * for variadic parameters */ @@ -147,22 +171,6 @@ struct ast_expression_common ir_value *outl; ir_value *outr; }; -#define AST_FLAG_VARIADIC (1<<0) -#define AST_FLAG_NORETURN (1<<1) -#define AST_FLAG_INLINE (1<<2) -#define AST_FLAG_INITIALIZED (1<<3) -#define AST_FLAG_DEPRECATED (1<<4) -#define AST_FLAG_INCLUDE_DEF (1<<5) -#define AST_FLAG_IS_VARARG (1<<6) -#define AST_FLAG_ALIAS (1<<7) -#define AST_FLAG_ERASEABLE (1<<8) -#define AST_FLAG_ACCUMULATE (1<<9) -/* - * An array declared as [] - * so that the size is taken from the initializer - */ -#define AST_FLAG_ARRAY_INIT (1<<10) -#define AST_FLAG_TYPE_MASK (AST_FLAG_VARIADIC | AST_FLAG_NORETURN) /* Value * @@ -656,4 +664,11 @@ const char* ast_function_label(ast_function*, const char *prefix); bool ast_function_codegen(ast_function *self, ir_builder *builder); bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir); +/* + * If the condition creates a situation where this becomes -1 size it means there are + * more AST_FLAGs than the type ast_flag_t is capable of holding. So either eliminate + * the AST flag count or change the ast_flag_t typedef to a type large enough to accomodate + * all the flags. + */ +typedef int static_assert_is_ast_flag_safe [((AST_FLAG_LAST) <= (ast_flag_t)(-1)) ? 1 : -1]; #endif diff --git a/ir.h b/ir.h index 3c236ea..1d35038 100644 --- a/ir.h +++ b/ir.h @@ -24,69 +24,86 @@ #define GMQCC_IR_HDR #include "gmqcc.h" -typedef struct -{ +/* + * Type large enough to hold all the possible IR flags. This should be + * changed if the static assertion at the end of this file fails. + */ +typedef uint8_t ir_flag_t; + +typedef struct ir_value_s ir_value; +typedef struct ir_instr_s ir_instr; +typedef struct ir_block_s ir_block; +typedef struct ir_function_s ir_function; +typedef struct ir_builder_s ir_builder; + +typedef struct { /* both inclusive */ size_t start; size_t end; } ir_life_entry_t; -struct ir_function_s; -typedef struct ir_value_s { +enum { + IR_FLAG_HAS_ARRAYS = 1 << 0, + IR_FLAG_HAS_UNINITIALIZED = 1 << 1, + IR_FLAG_HAS_GOTO = 1 << 2, + IR_FLAG_INCLUDE_DEF = 1 << 3, + IR_FLAG_ERASEABLE = 1 << 4, + + IR_FLAG_LAST, + IR_FLAG_MASK_NO_OVERLAP = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED), + IR_FLAG_MASK_NO_LOCAL_TEMPS = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED) +}; + +struct ir_value_s { char *name; - int vtype; - int store; - lex_ctx_t context; - /* even the IR knows the subtype of a field */ - int fieldtype; - /* and the output type of a function */ - int outtype; - /* 'const' vs 'var' qualifier */ - int cvq; - uint32_t flags; - - struct ir_instr_s **reads; - struct ir_instr_s **writes; + int vtype; + int store; + lex_ctx_t context; + + + int fieldtype; /* even the IR knows the subtype of a field */ + int outtype; /* and the output type of a function */ + int cvq; /* 'const' vs 'var' qualifier */ + ir_flag_t flags; + + ir_instr **reads; + ir_instr **writes; /* constantvalues */ bool hasvalue; union { - qcfloat_t vfloat; - int vint; - vec3_t vvec; - int32_t ivec[3]; - char *vstring; - struct ir_value_s *vpointer; - struct ir_function_s *vfunc; + qcfloat_t vfloat; + int vint; + vec3_t vvec; + int32_t ivec[3]; + char *vstring; + ir_value *vpointer; + ir_function *vfunc; } constval; struct { int32_t globaladdr; int32_t name; - /* filled by the local-allocator */ - int32_t local; - /* added for members */ - int32_t addroffset; - /* to generate field-addresses early */ - int32_t fieldaddr; + int32_t local; /* filled by the local-allocator */ + int32_t addroffset; /* added for members */ + int32_t fieldaddr; /* to generate field-addresses early */ } code; /* for acessing vectors */ - struct ir_value_s *members[3]; - struct ir_value_s *memberof; + ir_value *members[3]; + ir_value *memberof; - /* arrays will never overlap with temps */ - bool unique_life; - /* temps living during a CALL must be locked */ - bool locked; - bool callparam; - /* For the temp allocator */ - ir_life_entry_t *life; -} ir_value; + bool unique_life; /* arrays will never overlap with temps */ + bool locked; /* temps living during a CALL must be locked */ + bool callparam; -/* ir_value can be a variable, or created by an operation */ -/* if a result of an operation: the function should store + ir_life_entry_t *life; /* For the temp allocator */ +}; + +/* + * ir_value can be a variable, or created by an operation + * if a result of an operation: the function should store * it to remember to delete it / garbage collect it */ void ir_value_delete(ir_value*); @@ -100,19 +117,17 @@ bool ir_value_lives(ir_value*, size_t); void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...)); /* PHI data */ -typedef struct ir_phi_entry_s -{ - ir_value *value; - struct ir_block_s *from; +typedef struct ir_phi_entry_s { + ir_value *value; + ir_block *from; } ir_phi_entry_t; /* instruction */ -typedef struct ir_instr_s -{ - int opcode; - lex_ctx_t context; +struct ir_instr_s { + int opcode; + lex_ctx_t context; ir_value* (_ops[3]); - struct ir_block_s* (bops[2]); + ir_block* (bops[2]); ir_phi_entry_t *phi; ir_value **params; @@ -123,31 +138,30 @@ typedef struct ir_instr_s /* For IFs */ bool likely; - struct ir_block_s *owner; -} ir_instr; + ir_block *owner; +}; /* block */ -typedef struct ir_block_s -{ +struct ir_block_s { char *label; - lex_ctx_t context; + lex_ctx_t context; bool final; /* once a jump is added we're done */ - ir_instr **instr; - struct ir_block_s **entries; - struct ir_block_s **exits; - ir_value **living; + ir_instr **instr; + ir_block **entries; + ir_block **exits; + ir_value **living; /* For the temp-allocation */ size_t entry_id; size_t eid; bool is_return; - struct ir_function_s *owner; + ir_function *owner; bool generated; size_t code_start; -} ir_block; +}; 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_t, const char *label, int op, ir_value *operand); @@ -170,7 +184,8 @@ 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_t, ir_value *cond, 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 * next block to go to. * A goto usually becomes an OP_GOTO in the resulting code, @@ -180,37 +195,33 @@ 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_t, ir_block *to); /* function */ -typedef struct ir_function_s -{ +struct ir_function_s { char *name; int outtype; int *params; ir_block **blocks; + ir_flag_t flags; + int builtin; - uint32_t flags; - - int builtin; - - ir_value *value; - - /* values generated from operations + /* + * values generated from operations * which might get optimized away, so anything * in there needs to be deleted in the dtor. */ ir_value **values; - - /* locally defined variables */ - ir_value **locals; + ir_value **locals; /* locally defined variables */ + ir_value *value; size_t allocated_locals; size_t globaltemps; - ir_block* first; - ir_block* last; + ir_block* first; + ir_block* last; - lex_ctx_t 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 * so we can later fill in the entry pos * @@ -221,29 +232,22 @@ typedef struct ir_function_s /* for temp allocation */ size_t run_id; - struct ir_builder_s *owner; + ir_builder *owner; /* vararg support: */ size_t max_varargs; -} ir_function; +}; -#define IR_FLAG_HAS_ARRAYS (1<<1) -#define IR_FLAG_HAS_UNINITIALIZED (1<<2) -#define IR_FLAG_HAS_GOTO (1<<3) -#define IR_FLAG_INCLUDE_DEF (1<<4) -#define IR_FLAG_ERASEABLE (1<<5) -#define IR_FLAG_MASK_NO_OVERLAP (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED) -#define IR_FLAG_MASK_NO_LOCAL_TEMPS (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED) ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param); bool GMQCC_WARN ir_function_finalize(ir_function*); ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function*, const char *label); /* builder */ -#define IR_HT_SIZE 1024 +#define IR_HT_SIZE 1024 #define IR_MAX_VINSTR_TEMPS 1 -typedef struct ir_builder_s -{ + +struct ir_builder_s { char *name; ir_function **functions; ir_value **globals; @@ -276,7 +280,7 @@ typedef struct ir_builder_s /* code generator */ code_t *code; -} ir_builder; +}; ir_builder* ir_builder_new(const char *modulename); void ir_builder_delete(ir_builder*); @@ -292,7 +296,15 @@ void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...)); * Blub: don't use extern here, it's annoying and shows up in nm * for some reason :P */ -typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4)?1:-1]; -typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_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_t) == 4) ? 1 : -1]; + +/* + * If the condition creates a situation where this becomes -1 size it means there are + * more IR_FLAGs than the type ir_flag_t is capable of holding. So either eliminate + * the IR flag count or change the ir_flag_t typedef to a type large enough to accomodate + * all the flags. + */ +typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1]; #endif