From 188cd4cc8ba69ffabfbf3b78db6a36697cff79f6 Mon Sep 17 00:00:00 2001 From: Christoph Mallon Date: Wed, 16 May 2012 06:53:40 +0200 Subject: [PATCH] Use the same struct variable_t for variable and parameter entities. They behave mostly the same, so this gets rid of some code duplication. --- ast2firm.c | 77 ++++++++++++++---------------------------------------- entity.c | 2 +- entity.h | 1 - entity_t.h | 13 --------- parser.c | 21 +++------------ 5 files changed, 25 insertions(+), 89 deletions(-) diff --git a/ast2firm.c b/ast2firm.c index 559bb94..93360ef 100644 --- a/ast2firm.c +++ b/ast2firm.c @@ -1546,16 +1546,13 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref) case DECLARATION_KIND_UNKNOWN: break; - case DECLARATION_KIND_LOCAL_VARIABLE: { - ir_mode *const mode = get_ir_mode_storage(type); - ir_node *const value = get_value(entity->variable.v.value_number, mode); - return create_conv(NULL, value, get_ir_mode_arithmetic(type)); - } + case DECLARATION_KIND_LOCAL_VARIABLE: case DECLARATION_KIND_PARAMETER: { ir_mode *const mode = get_ir_mode_storage(type); - ir_node *const value = get_value(entity->parameter.v.value_number,mode); + ir_node *const value = get_value(entity->variable.v.value_number, mode); return create_conv(NULL, value, get_ir_mode_arithmetic(type)); } + case DECLARATION_KIND_FUNCTION: { return create_symconst(dbgi, entity->function.irentity); } @@ -1575,14 +1572,9 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref) return deref_address(dbgi, variable->base.type, addr); } - case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: { - ir_entity *irentity = entity->variable.v.entity; - ir_node *frame = get_local_frame(irentity); - ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity); - return deref_address(dbgi, entity->declaration.type, sel); - } + case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: case DECLARATION_KIND_PARAMETER_ENTITY: { - ir_entity *irentity = entity->parameter.v.entity; + ir_entity *irentity = entity->variable.v.entity; ir_node *frame = get_local_frame(irentity); ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity); return deref_address(dbgi, entity->declaration.type, sel); @@ -1616,18 +1608,12 @@ static ir_node *reference_addr(const reference_expression_t *ref) ir_node *const addr = create_symconst(dbgi, entity->variable.v.entity); return addr; } - case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: { - ir_entity *irentity = entity->variable.v.entity; - ir_node *frame = get_local_frame(irentity); - ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity); - return sel; - } + case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: case DECLARATION_KIND_PARAMETER_ENTITY: { - ir_entity *irentity = entity->parameter.v.entity; + ir_entity *irentity = entity->variable.v.entity; ir_node *frame = get_local_frame(irentity); ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity); - return sel; } @@ -2086,12 +2072,10 @@ static ir_node *set_value_for_expression_addr(const expression_t *expression, entity_t *entity = ref->entity; assert(is_declaration(entity)); assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN); - if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) { + if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE || + entity->declaration.kind == DECLARATION_KIND_PARAMETER) { set_value(entity->variable.v.value_number, value); return value; - } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) { - set_value(entity->parameter.v.value_number, value); - return value; } } @@ -2137,20 +2121,14 @@ static ir_node *get_value_from_lvalue(const expression_t *expression, || entity->kind == ENTITY_PARAMETER); assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN); int value_number; - if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) { + if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE || + entity->declaration.kind == DECLARATION_KIND_PARAMETER) { value_number = entity->variable.v.value_number; assert(addr == NULL); type_t *type = skip_typeref(expression->base.type); ir_mode *mode = get_ir_mode_storage(type); ir_node *res = get_value(value_number, mode); return create_conv(NULL, res, get_ir_mode_arithmetic(type)); - } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) { - value_number = entity->parameter.v.value_number; - assert(addr == NULL); - type_t *type = skip_typeref(expression->base.type); - ir_mode *mode = get_ir_mode_storage(type); - ir_node *res = get_value(value_number, mode); - return create_conv(NULL, res, get_ir_mode_arithmetic(type)); } } @@ -5309,20 +5287,9 @@ static int count_local_variables(const entity_t *entity, int count = 0; entity_t const *const end = last != NULL ? last->base.next : NULL; for (; entity != end; entity = entity->base.next) { - type_t *type; - bool address_taken; - - if (entity->kind == ENTITY_VARIABLE) { - type = skip_typeref(entity->declaration.type); - address_taken = entity->variable.address_taken; - } else if (entity->kind == ENTITY_PARAMETER) { - type = skip_typeref(entity->declaration.type); - address_taken = entity->parameter.address_taken; - } else { - continue; - } - - if (!address_taken && is_type_scalar(type)) + if ((entity->kind == ENTITY_VARIABLE || entity->kind == ENTITY_PARAMETER) && + !entity->variable.address_taken && + is_type_scalar(skip_typeref(entity->declaration.type))) ++count; } return count; @@ -5398,20 +5365,16 @@ static void initialize_function_parameters(entity_t *entity) assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN); type_t *type = skip_typeref(parameter->declaration.type); - bool needs_entity = parameter->parameter.address_taken; assert(!is_type_array(type)); - if (is_type_compound(type)) { - needs_entity = true; - } + bool const needs_entity = parameter->variable.address_taken || is_type_compound(type); ir_type *param_irtype = get_method_param_type(function_irtype, n); if (needs_entity) { ir_type *frame_type = get_irg_frame_type(irg); ir_entity *param = new_parameter_entity(frame_type, n, param_irtype); - parameter->declaration.kind - = DECLARATION_KIND_PARAMETER_ENTITY; - parameter->parameter.v.entity = param; + parameter->declaration.kind = DECLARATION_KIND_PARAMETER_ENTITY; + parameter->variable.v.entity = param; continue; } @@ -5423,13 +5386,13 @@ static void initialize_function_parameters(entity_t *entity) value = create_conv(NULL, value, mode); value = do_strict_conv(NULL, value); - parameter->declaration.kind = DECLARATION_KIND_PARAMETER; - parameter->parameter.v.value_number = next_value_number_function; + parameter->declaration.kind = DECLARATION_KIND_PARAMETER; + parameter->variable.v.value_number = next_value_number_function; set_irg_loc_description(current_ir_graph, next_value_number_function, parameter); ++next_value_number_function; - set_value(parameter->parameter.v.value_number, value); + set_value(parameter->variable.v.value_number, value); } } diff --git a/entity.c b/entity.c index 5e255c1..3a2db26 100644 --- a/entity.c +++ b/entity.c @@ -57,7 +57,7 @@ static size_t get_entity_struct_size(entity_kind_t kind) { static const size_t sizes[] = { [ENTITY_VARIABLE] = sizeof(variable_t), - [ENTITY_PARAMETER] = sizeof(parameter_t), + [ENTITY_PARAMETER] = sizeof(variable_t), [ENTITY_COMPOUND_MEMBER] = sizeof(compound_member_t), [ENTITY_FUNCTION] = sizeof(function_t), [ENTITY_TYPEDEF] = sizeof(typedef_t), diff --git a/entity.h b/entity.h index 5a0c8c4..337b45d 100644 --- a/entity.h +++ b/entity.h @@ -31,7 +31,6 @@ typedef struct namespace_t namespace_t; typedef struct declaration_t declaration_t; typedef struct typedef_t typedef_t; typedef struct variable_t variable_t; -typedef struct parameter_t parameter_t; typedef struct function_t function_t; typedef struct compound_member_t compound_member_t; typedef union entity_t entity_t; diff --git a/entity_t.h b/entity_t.h index 0e5c0d2..19db103 100644 --- a/entity_t.h +++ b/entity_t.h @@ -240,18 +240,6 @@ struct variable_t { } v; }; -struct parameter_t { - declaration_t base; - bool address_taken : 1; - bool read : 1; - - /* ast2firm info */ - union { - unsigned int value_number; - ir_entity *entity; - } v; -}; - struct function_t { declaration_t base; bool is_inline : 1; @@ -286,7 +274,6 @@ union entity_t { typedef_t typedefe; declaration_t declaration; variable_t variable; - parameter_t parameter; function_t function; compound_member_t compound_member; }; diff --git a/parser.c b/parser.c index d517bd8..3793347 100644 --- a/parser.c +++ b/parser.c @@ -1325,11 +1325,7 @@ static void mark_vars_read(expression_t *const expr, entity_t *lhs_ent) return; if (lhs_ent != entity && lhs_ent != ENT_ANY) { - if (entity->kind == ENTITY_VARIABLE) { - entity->variable.read = true; - } else { - entity->parameter.read = true; - } + entity->variable.read = true; } return; } @@ -6109,12 +6105,8 @@ static expression_t *parse_reference(void) && (current_function != NULL && entity->base.parent_scope->depth < current_function->parameters.depth) && (entity->kind == ENTITY_VARIABLE || entity->kind == ENTITY_PARAMETER)) { - if (entity->kind == ENTITY_VARIABLE) { - /* access of a variable from an outer function */ - entity->variable.address_taken = true; - } else if (entity->kind == ENTITY_PARAMETER) { - entity->parameter.address_taken = true; - } + /* access of a variable from an outer function */ + entity->variable.address_taken = true; current_function->need_closure = true; } @@ -7658,12 +7650,7 @@ static void set_address_taken(expression_t *expression, bool may_be_register) errorf(pos, "address of register '%N' requested", entity); } - if (entity->kind == ENTITY_VARIABLE) { - entity->variable.address_taken = true; - } else { - assert(entity->kind == ENTITY_PARAMETER); - entity->parameter.address_taken = true; - } + entity->variable.address_taken = true; } /** -- 2.20.1