Commit 58423576 authored by Matthias Braun's avatar Matthias Braun
Browse files

Simplify compound_type code

There is no need for dispatch through tpop, they all use the same
underlying data structure anyway, so just work on that in
get_compound_xxx()
parent d6058457
...@@ -46,18 +46,12 @@ static const tp_op_ops ...@@ -46,18 +46,12 @@ static const tp_op_ops
.free_attrs = free_class_attrs, .free_attrs = free_class_attrs,
.free_entities = free_compound_entities, .free_entities = free_compound_entities,
.set_type_size = set_default_size, .set_type_size = set_default_size,
.get_n_members = get_class_n_members,
.get_member = get_class_member,
.get_member_index = get_class_member_index
}, },
/** tpop operations for struct types */ /** tpop operations for struct types */
struct_ops = { struct_ops = {
.free_attrs = free_struct_attrs, .free_attrs = free_struct_attrs,
.free_entities = free_compound_entities, .free_entities = free_compound_entities,
.set_type_size = set_default_size, .set_type_size = set_default_size,
.get_n_members = get_struct_n_members,
.get_member = get_struct_member,
.get_member_index = get_struct_member_index
}, },
/** tpop operations for method types */ /** tpop operations for method types */
method_ops = { method_ops = {
...@@ -68,9 +62,6 @@ static const tp_op_ops ...@@ -68,9 +62,6 @@ static const tp_op_ops
.free_attrs = free_union_attrs, .free_attrs = free_union_attrs,
.free_entities = free_compound_entities, .free_entities = free_compound_entities,
.set_type_size = set_default_size, .set_type_size = set_default_size,
.get_n_members = get_union_n_members,
.get_member = get_union_member,
.get_member_index = get_union_member_index
}, },
/** tpop operations for array types */ /** tpop operations for array types */
array_ops = { array_ops = {
......
...@@ -29,12 +29,6 @@ typedef struct tp_op_ops { ...@@ -29,12 +29,6 @@ typedef struct tp_op_ops {
void (*free_entities)(ir_type *type); void (*free_entities)(ir_type *type);
/** Called to set the byte size of a type. */ /** Called to set the byte size of a type. */
void (*set_type_size)(ir_type *type, unsigned size); void (*set_type_size)(ir_type *type, unsigned size);
/** Called to return the number of compound members. */
size_t (*get_n_members)(ir_type const *type);
/** Called to get the pos'th compound member. */
ir_entity *(*get_member)(ir_type const *type, size_t pos);
/** Called to get the index of a compound member. */
size_t (*get_member_index)(ir_type const *type, ir_entity const *member);
} tp_op_ops; } tp_op_ops;
/** possible flags for a type opcode */ /** possible flags for a type opcode */
......
...@@ -264,29 +264,14 @@ void set_type_state(ir_type *tp, ir_type_state state) ...@@ -264,29 +264,14 @@ void set_type_state(ir_type *tp, ir_type_state state)
#ifndef NDEBUG #ifndef NDEBUG
/* Just a correctness check: */ /* Just a correctness check: */
if (state == layout_fixed) { if (state == layout_fixed && is_compound_type(tp)
switch (get_type_tpop_code(tp)) { && !(tp->flags & tf_segment)) {
case tpo_class: for (size_t i = 0, n_mem = get_compound_n_members(tp);
if (tp != get_glob_type()) { i < n_mem; i++) {
for (size_t i = 0, n_mem = get_class_n_members(tp); ir_entity *entity = get_compound_member(tp, i);
i < n_mem; i++) { if (is_Method_type(get_entity_type(entity)))
ir_entity *entity = get_class_member(tp, i); continue;
if (is_Method_type(get_entity_type(entity))) assert(get_entity_offset(entity) > -1);
continue;
assert(get_entity_offset(entity) > -1);
}
}
break;
case tpo_struct:
for (size_t i = 0, n_members = get_struct_n_members(tp);
i < n_members; i++) {
assert(get_entity_offset(get_struct_member(tp, i)) > -1);
}
break;
case tpo_union:
case tpo_array:
default:
break;
} }
} }
#endif #endif
...@@ -337,17 +322,6 @@ static void compound_free_attrs(ir_type *type) ...@@ -337,17 +322,6 @@ static void compound_free_attrs(ir_type *type)
DEL_ARR_F(type->attr.ca.members); DEL_ARR_F(type->attr.ca.members);
} }
static size_t compound_get_n_members(const ir_type *type)
{
return ARR_LEN(type->attr.ca.members);
}
static ir_entity *compound_get_member(const ir_type *type, size_t index)
{
assert(index < ARR_LEN(type->attr.ca.members));
return type->attr.ca.members[index];
}
static void compound_add_member(ir_type *type, ir_entity *entity) static void compound_add_member(ir_type *type, ir_entity *entity)
{ {
/* try to detect double-add */ /* try to detect double-add */
...@@ -357,24 +331,14 @@ static void compound_add_member(ir_type *type, ir_entity *entity) ...@@ -357,24 +331,14 @@ static void compound_add_member(ir_type *type, ir_entity *entity)
void free_compound_entities(ir_type *type) void free_compound_entities(ir_type *type)
{ {
for (size_t i = compound_get_n_members(type); i-- > 0; ) for (size_t i = get_compound_n_members(type); i-- > 0; )
free_entity(compound_get_member(type, i)); free_entity(get_compound_member(type, i));
}
static size_t compound_get_member_index(ir_type const *const type,
ir_entity const *const entity)
{
for (size_t i = 0, n = compound_get_n_members(type); i < n; ++i) {
if (compound_get_member(type, i) == entity)
return i;
}
return INVALID_MEMBER_INDEX;
} }
static void compound_remove_member(ir_type *type, const ir_entity *member) static void compound_remove_member(ir_type *type, const ir_entity *member)
{ {
for (size_t i = 0, n = ARR_LEN(type->attr.ca.members); i < n; ++i) { for (size_t i = 0, n = ARR_LEN(type->attr.ca.members); i < n; ++i) {
if (compound_get_member(type, i) == member) { if (get_compound_member(type, i) == member) {
for (; i < n - 1; ++i) for (; i < n - 1; ++i)
type->attr.ca.members[i] = type->attr.ca.members[i+1]; type->attr.ca.members[i] = type->attr.ca.members[i+1];
ARR_SETLEN(ir_entity*, type->attr.ca.members, n-1); ARR_SETLEN(ir_entity*, type->attr.ca.members, n-1);
...@@ -420,20 +384,22 @@ static void add_class_member(ir_type *clss, ir_entity *member) ...@@ -420,20 +384,22 @@ static void add_class_member(ir_type *clss, ir_entity *member)
compound_add_member(clss, member); compound_add_member(clss, member);
} }
size_t (get_class_n_members)(const ir_type *clss) size_t get_class_n_members(const ir_type *clss)
{ {
return get_class_n_members_(clss); assert(is_Class_type(clss));
return get_compound_n_members(clss);
} }
size_t get_class_member_index(ir_type const *clss, ir_entity const *const mem) ir_entity *get_class_member(ir_type const *const clss, size_t const pos)
{ {
assert(is_Class_type(clss)); assert(is_Class_type(clss));
return compound_get_member_index(clss, mem); return get_compound_member(clss, pos);
} }
ir_entity *(get_class_member)(const ir_type *clss, size_t pos) size_t get_class_member_index(ir_type const *clss, ir_entity const *const mem)
{ {
return get_class_member_(clss, pos); assert(is_Class_type(clss));
return get_compound_member_index(clss, mem);
} }
static void remove_class_member(ir_type *clss, ir_entity *member) static void remove_class_member(ir_type *clss, ir_entity *member)
...@@ -593,7 +559,7 @@ const char *get_struct_name(const ir_type *strct) ...@@ -593,7 +559,7 @@ const char *get_struct_name(const ir_type *strct)
size_t get_struct_n_members(const ir_type *strct) size_t get_struct_n_members(const ir_type *strct)
{ {
assert(is_Struct_type(strct)); assert(is_Struct_type(strct));
return compound_get_n_members(strct); return get_compound_n_members(strct);
} }
static void add_struct_member(ir_type *strct, ir_entity *member) static void add_struct_member(ir_type *strct, ir_entity *member)
...@@ -606,13 +572,13 @@ static void add_struct_member(ir_type *strct, ir_entity *member) ...@@ -606,13 +572,13 @@ static void add_struct_member(ir_type *strct, ir_entity *member)
ir_entity *get_struct_member(const ir_type *strct, size_t pos) ir_entity *get_struct_member(const ir_type *strct, size_t pos)
{ {
assert(is_Struct_type(strct)); assert(is_Struct_type(strct));
return compound_get_member(strct, pos); return get_compound_member(strct, pos);
} }
size_t get_struct_member_index(ir_type const *strct, ir_entity const *const mem) size_t get_struct_member_index(ir_type const *strct, ir_entity const *const mem)
{ {
assert(is_Struct_type(strct)); assert(is_Struct_type(strct));
return compound_get_member_index(strct, mem); return get_compound_member_index(strct, mem);
} }
static void remove_struct_member(ir_type *strct, ir_entity *member) static void remove_struct_member(ir_type *strct, ir_entity *member)
...@@ -806,7 +772,7 @@ const char *get_union_name(const ir_type *uni) ...@@ -806,7 +772,7 @@ const char *get_union_name(const ir_type *uni)
size_t get_union_n_members(const ir_type *uni) size_t get_union_n_members(const ir_type *uni)
{ {
assert(is_Union_type(uni)); assert(is_Union_type(uni));
return compound_get_n_members(uni); return get_compound_n_members(uni);
} }
static void add_union_member(ir_type *uni, ir_entity *member) static void add_union_member(ir_type *uni, ir_entity *member)
...@@ -818,13 +784,13 @@ static void add_union_member(ir_type *uni, ir_entity *member) ...@@ -818,13 +784,13 @@ static void add_union_member(ir_type *uni, ir_entity *member)
ir_entity *get_union_member(const ir_type *uni, size_t pos) ir_entity *get_union_member(const ir_type *uni, size_t pos)
{ {
assert(is_Union_type(uni)); assert(is_Union_type(uni));
return compound_get_member(uni, pos); return get_compound_member(uni, pos);
} }
size_t get_union_member_index(ir_type const *uni, ir_entity const *const mem) size_t get_union_member_index(ir_type const *uni, ir_entity const *const mem)
{ {
assert(is_Union_type(uni)); assert(is_Union_type(uni));
return compound_get_member_index(uni, mem); return get_compound_member_index(uni, mem);
} }
static void remove_union_member(ir_type *uni, ir_entity *member) static void remove_union_member(ir_type *uni, ir_entity *member)
...@@ -996,28 +962,30 @@ int (is_Primitive_type)(const ir_type *primitive) ...@@ -996,28 +962,30 @@ int (is_Primitive_type)(const ir_type *primitive)
return is_primitive_type_(primitive); return is_primitive_type_(primitive);
} }
int (is_atomic_type)(const ir_type *tp) int (is_atomic_type)(const ir_type *tp)
{ {
return is_atomic_type_(tp); return is_atomic_type_(tp);
} }
size_t get_compound_n_members(const ir_type *tp) size_t (get_compound_n_members)(ir_type const *const type)
{ {
const tp_op *op = get_type_tpop(tp); return get_compound_n_members_(type);
return op->ops.get_n_members(tp);
} }
ir_entity *get_compound_member(const ir_type *tp, size_t pos) ir_entity *(get_compound_member)(ir_type const *const type, size_t const pos)
{ {
const tp_op *op = get_type_tpop(tp); return get_compound_member_(type, pos);
return op->ops.get_member(tp, pos);
} }
size_t get_compound_member_index(ir_type const *tp, ir_entity const *const mem) size_t get_compound_member_index(ir_type const *const type,
ir_entity const *const entity)
{ {
const tp_op *op = get_type_tpop(tp); assert(is_compound_type(type));
return op->ops.get_member_index(tp, mem); for (size_t i = 0, n = get_compound_n_members(type); i < n; ++i) {
if (get_compound_member(type, i) == entity)
return i;
}
return INVALID_MEMBER_INDEX;
} }
void set_compound_variable_size(ir_type *tp, int variable_size_flag) void set_compound_variable_size(ir_type *tp, int variable_size_flag)
...@@ -1106,8 +1074,8 @@ ir_type *clone_frame_type(ir_type *type) ...@@ -1106,8 +1074,8 @@ ir_type *clone_frame_type(ir_type *type)
assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK); assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
ir_type *res = new_type_frame(); ir_type *res = new_type_frame();
for (size_t i = 0, n = get_class_n_members(type); i < n; ++i) { for (size_t i = 0, n = get_compound_n_members(type); i < n; ++i) {
ir_entity *ent = get_class_member(type, i); ir_entity *ent = get_compound_member(type, i);
ir_entity *nent = copy_entity_own(ent, res); ir_entity *nent = copy_entity_own(ent, res);
set_entity_link(ent, nent); set_entity_link(ent, nent);
set_entity_link(nent, ent); set_entity_link(nent, ent);
...@@ -1196,8 +1164,8 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, ...@@ -1196,8 +1164,8 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
if (at_start) { if (at_start) {
unsigned delta = (size + frame_align - 1) & ~(frame_align - 1); unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
/* fix all offsets so far */ /* fix all offsets so far */
for (size_t i = 0, n = get_class_n_members(frame_type); i < n; ++i) { for (size_t i = 0, n = get_compound_n_members(frame_type); i < n; ++i) {
ir_entity *ent = get_class_member(frame_type, i); ir_entity *ent = get_compound_member(frame_type, i);
set_entity_offset(ent, get_entity_offset(ent) + delta); set_entity_offset(ent, get_entity_offset(ent) + delta);
} }
......
...@@ -35,9 +35,9 @@ ...@@ -35,9 +35,9 @@
#define type_visited(tp) type_visited_(tp) #define type_visited(tp) type_visited_(tp)
#define get_type_dbg_info(tp) get_type_dbg_info_(tp) #define get_type_dbg_info(tp) get_type_dbg_info_(tp)
#define set_type_dbg_info(tp, db) set_type_dbg_info_(tp, db) #define set_type_dbg_info(tp, db) set_type_dbg_info_(tp, db)
#define get_compound_n_members(type) get_compound_n_members_(type)
#define get_compound_member(type, pos) get_compound_member_(type, pos)
#define is_Class_type(clss) is_class_type_(clss) #define is_Class_type(clss) is_class_type_(clss)
#define get_class_n_members(clss) get_class_n_members_(clss)
#define get_class_member(clss, pos) get_class_member_(clss, pos)
#define is_Struct_type(strct) is_struct_type_(strct) #define is_Struct_type(strct) is_struct_type_(strct)
#define is_Method_type(method) is_method_type_(method) #define is_Method_type(method) is_method_type_(method)
#define is_Union_type(uni) is_union_type_(uni) #define is_Union_type(uni) is_union_type_(uni)
...@@ -297,17 +297,18 @@ static inline int is_class_type_(const ir_type *clss) ...@@ -297,17 +297,18 @@ static inline int is_class_type_(const ir_type *clss)
return clss->type_op == type_class; return clss->type_op == type_class;
} }
static inline size_t get_class_n_members_(const ir_type *clss) static inline size_t get_compound_n_members_(const ir_type *type)
{ {
assert(clss->type_op == type_class); assert(is_compound_type(type));
return ARR_LEN(clss->attr.ca.members); return ARR_LEN(type->attr.ca.members);
} }
static inline ir_entity *get_class_member_(const ir_type *clss, size_t pos) static inline ir_entity *get_compound_member_(ir_type const *const type,
size_t const pos)
{ {
assert(clss->type_op == type_class); assert(is_compound_type(type));
assert(pos < get_class_n_members_(clss)); assert(pos < get_compound_n_members(type));
return clss->attr.ca.members[pos]; return type->attr.ca.members[pos];
} }
static inline int is_struct_type_(const ir_type *strct) static inline int is_struct_type_(const ir_type *strct)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment