From 51f4c92b8bbb63562c833e2f989afc2e80b716c9 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Mon, 31 Jul 2006 08:59:21 +0000 Subject: [PATCH] changed implementation of enum types [r8067] --- ir/tr/type.c | 73 +++++++++++++++++++++++++++++--------------------- ir/tr/type.h | 56 ++++++++++++++++++++++---------------- ir/tr/type_t.h | 40 ++++++++++++++------------- 3 files changed, 98 insertions(+), 71 deletions(-) diff --git a/ir/tr/type.c b/ir/tr/type.c index c628b3559..4975d0f44 100644 --- a/ir/tr/type.c +++ b/ir/tr/type.c @@ -437,8 +437,11 @@ set_type_state(ir_type *tp, type_state state) { break; case tpo_enumeration: assert(get_type_mode != NULL); - for (i = 0; i < get_enumeration_n_enums(tp); i++) - assert(get_enumeration_enum(tp, i) != NULL); + for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) { + ir_enum_const *ec = get_enumeration_const(tp, i); + tarval *tv = get_enumeration_value(ec); + assert(tv != NULL && tv != tarval_bad); + } break; default: break; } /* switch (tp) */ @@ -1694,18 +1697,16 @@ void set_array_size_bits(ir_type *tp, int size) { /*-----------------------------------------------------------------*/ /* create a new type enumeration -- set the enumerators independently */ -ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) { +ir_type *new_d_type_enumeration(ident *name, dbg_info *db) { ir_type *res = new_type(type_enumeration, NULL, name, db); - res->attr.ea.n_enums = n_enums; - res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0])); - res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0])); + res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, 0); hook_new_type(res); return res; } -ir_type *new_type_enumeration(ident *name, int n_enums) { - return new_d_type_enumeration(name, n_enums, NULL); +ir_type *new_type_enumeration(ident *name) { + return new_d_type_enumeration(name, NULL); } void free_enumeration_entities(ir_type *enumeration) { @@ -1713,39 +1714,51 @@ void free_enumeration_entities(ir_type *enumeration) { } void free_enumeration_attrs(ir_type *enumeration) { assert(enumeration && (enumeration->type_op == type_enumeration)); - free(enumeration->attr.ea.enumer); - free(enumeration->attr.ea.enum_nameid); + DEL_ARR_F(enumeration->attr.ea.enumer); } /* manipulate fields of enumeration type. */ -int get_enumeration_n_enums (const ir_type *enumeration) { +int get_enumeration_n_enums(const ir_type *enumeration) { assert(enumeration && (enumeration->type_op == type_enumeration)); - return enumeration->attr.ea.n_enums; + return ARR_LEN(enumeration->attr.ea.enumer); } -void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con) { - assert(enumeration && (enumeration->type_op == type_enumeration)); - assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration)); - enumeration->attr.ea.enumer[pos] = con; + +/* create a new constant */ +int add_enumeration_const(ir_type *enumeration, ident *nameid, tarval *con) { + ir_enum_const c; + int idx = ARR_LEN(enumeration->attr.ea.enumer); + + c.nameid = nameid; + c.value = con; + c.owner = enumeration; + + ARR_APP1(ir_enum_const, enumeration->attr.ea.enumer, c); + return idx; } -tarval *get_enumeration_enum (const ir_type *enumeration, int pos) { + +ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos) { assert(enumeration && (enumeration->type_op == type_enumeration)); assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration)); - return enumeration->attr.ea.enumer[pos]; + return &enumeration->attr.ea.enumer[pos]; } -void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id) { - assert(enumeration && (enumeration->type_op == type_enumeration)); - assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration)); - enumeration->attr.ea.enum_nameid[pos] = id; + +ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst) { + return enum_cnst->owner; } -ident *get_enumeration_nameid (const ir_type *enumeration, int pos) { - assert(enumeration && (enumeration->type_op == type_enumeration)); - assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration)); - return enumeration->attr.ea.enum_nameid[pos]; +void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con) { + enum_cnst->value = con; } -const char *get_enumeration_name(const ir_type *enumeration, int pos) { - assert(enumeration && (enumeration->type_op == type_enumeration)); - assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration)); - return get_id_str(enumeration->attr.ea.enum_nameid[pos]); +tarval *get_enumeration_value(const ir_enum_const *enum_cnst) { + return enum_cnst->value; +} +void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id) { + enum_cnst->nameid = id; +} +ident *get_enumeration_nameid(const ir_enum_const *enum_cnst) { + return enum_cnst->nameid; +} +const char *get_enumeration_name(const ir_enum_const *enum_cnst) { + return get_id_str(enum_cnst->nameid); } /* typecheck */ diff --git a/ir/tr/type.h b/ir/tr/type.h index ebf815e09..b68700752 100644 --- a/ir/tr/type.h +++ b/ir/tr/type.h @@ -5,7 +5,7 @@ * Author: Goetz Lindenmaier * Modified by: Michael Beck * Created: - * Copyright: (c) 2001-2003 Universität Karlsruhe + * Copyright: (c) 2001-2006 Universität Karlsruhe * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. * CVS-ID: $Id$ */ @@ -206,13 +206,14 @@ typedef enum { layout_undefined, /**< The layout of this type is not defined. Address computation to access fields is not possible, fields must be accessed by Sel - nodes. This is the default value except for + nodes. Enumeration constants might be undefined. + This is the default value except for pointer, primitive and method types. */ layout_fixed /**< The layout is fixed, all component/member entities have an offset assigned. Size of the type is known. Arrays can be accessed by explicit address - computation. Default for pointer, primitive and method - types. */ + computation. Enumeration constants must be defined. + Default for pointer, primitive and method types. */ } type_state; /** Returns a human readable string for the enum entry. */ @@ -329,7 +330,7 @@ void inc_master_type_visited(void); * @return * true if the thing is a type, else false */ -int is_type (const void *thing); +int is_type(const void *thing); /** * Checks whether two types are structurally equal. @@ -404,7 +405,7 @@ int equal_type(ir_type *typ1, ir_type *typ2); * @return smaller than the points_to type of lt. * */ -int smaller_type (ir_type *st, ir_type *lt); +int smaller_type(ir_type *st, ir_type *lt); /** * @page class_type Representation of a class type @@ -1094,40 +1095,49 @@ int is_Array_type(const ir_type *array); * Enumeration types need not necessarily be represented explicitly * by Firm types, as the frontend can lower them to integer constants as * well. For debugging purposes or similar tasks this information is useful. + * The type state layout_fixed is set, if all enumeration constant have + * there tarvals assigned. Until then * - * - *enum: The target values representing the constants used to + * - *const: The target values representing the constants used to * represent individual enumerations. - * - *enum_nameid: Idents containing the source program name of the enumeration - * constants */ /** Create a new type enumeration -- set the enumerators independently. */ -ir_type *new_type_enumeration (ident *name, int n_enums); +ir_type *new_type_enumeration(ident *name); /** Create a new type enumeration with debug information -- set the enumerators independently. */ -ir_type *new_d_type_enumeration (ident *name, int n_enums, dbg_info *db); +ir_type *new_d_type_enumeration(ident *name, dbg_info *db); /* --- manipulate fields of enumeration type. --- */ +/** Add a new enumeration constant to a enumeration type and return its position. */ +int add_enumeration_const(ir_type *enumeration, ident *nameid, tarval *con); + /** Returns the number of enumeration values of this enumeration */ -int get_enumeration_n_enums (const ir_type *enumeration); +int get_enumeration_n_enums(const ir_type *enumeration); + +/** Returns the enumeration constant at a given position. */ +ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos); + +/** Returns the enumeration type owner of an enumeration constant. */ +ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst); -/** Sets the enumeration value at a given position. */ -void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con); +/** Sets the enumeration constant value. */ +void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con); -/** Returns the enumeration value at a given position. */ -tarval *get_enumeration_enum (const ir_type *enumeration, int pos); +/** Returns the enumeration constant value. */ +tarval *get_enumeration_value(const ir_enum_const *enum_cnst); -/** Assign an ident to an enumeration value at a given position. */ -void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id); +/** Assign an ident to an enumeration constant. */ +void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id); -/** Returns the assigned ident of an enumeration value at a given position. */ -ident *get_enumeration_nameid (const ir_type *enumeration, int pos); +/** Returns the assigned ident of an enumeration constant. */ +ident *get_enumeration_nameid(const ir_enum_const *enum_cnst); -/** Returns the assigned name of an enumeration value at a given position. */ -const char *get_enumeration_name(const ir_type *enumeration, int pos); +/** Returns the assigned name of an enumeration constant. */ +const char *get_enumeration_name(const ir_enum_const *enum_cnst); /** Returns true if a type is a enumeration type. */ -int is_Enumeration_type (const ir_type *enumeration); +int is_Enumeration_type(const ir_type *enumeration); /** * @page pointer_type Representation of a pointer type diff --git a/ir/tr/type_t.h b/ir/tr/type_t.h index a8c9cb4f0..a54850c44 100644 --- a/ir/tr/type_t.h +++ b/ir/tr/type_t.h @@ -36,7 +36,7 @@ enum class_flags { cf_absctract_class = 4, /**< Set if a class is "abstract" */ }; -/** Class attributes. */ +/** Class type attributes. */ typedef struct { entity **members; /**< Array containing the fields and methods of this class. */ ir_type **subtypes; /**< Array containing the direct subtypes. */ @@ -48,18 +48,18 @@ typedef struct { unsigned clss_flags; /**< Additional class flags. */ } cls_attr; -/** struct attributes */ +/** Struct type attributes. */ typedef struct { entity **members; /**< Fields of this struct. No method entities allowed. */ } stc_attr; -/** A (type, entity) pair */ +/** A (type, entity) pair. */ typedef struct { ir_type *tp; /**< A type. */ entity *ent; /**< An entity. */ } tp_ent_pair; -/** method attributes */ +/** Method type attributes. */ typedef struct { int n_params; /**< Number of parameters. */ tp_ent_pair *param_type; /**< Array of parameter type/value entities pairs. */ @@ -73,32 +73,36 @@ typedef struct { unsigned irg_calling_conv; /**< A set of calling convention flags. */ } mtd_attr; -/** union attributes */ +/** Union type attributes. */ typedef struct { entity **members; /**< Fields of this union. No method entities allowed. */ } uni_attr; -/** array attributes */ +/** Array type attributes. */ typedef struct { - int n_dimensions; /**< Number of array dimensions. */ + int n_dimensions; /**< Number of array dimensions. */ ir_node **lower_bound; /**< Lower bounds of dimensions. Usually all 0. */ ir_node **upper_bound; /**< Upper bounds or dimensions. */ - int *order; /**< Ordering of dimensions. */ + int *order; /**< Ordering of dimensions. */ ir_type *element_type; /**< The type of the array elements. */ - entity *element_ent; /**< Entity for the array elements, to be used for - element selection with Sel. */ + entity *element_ent; /**< Entity for the array elements, to be used for + element selection with a Sel node. */ } arr_attr; -/** enum attributes */ +/** An enumerator constant. */ +struct ir_enum_const { + tarval *value; /**< The constants that represents this enumerator identifier. */ + ident *nameid; /**< The name of the enumerator identifier. */ + ir_type *owner; /**< owner type of this enumerator constant. */ +}; + +/** Enum type attributes. */ typedef struct { - int n_enums; /**< Number of enumerators. */ - tarval **enumer; /**< Contains all constants that represent a member - of the enum -- enumerators. */ - ident **enum_nameid;/**< Contains the names of the enum fields as specified by - the source program. */ + ir_enum_const *enumer; /**< Contains all enumerator constants that represent a member + of the enum -- enumerators. */ } enm_attr; -/** pointer attributes */ +/** Pointer type attributes. */ typedef struct { ir_type *points_to; /**< The type of the entity the pointer points to. */ } ptr_attr; @@ -133,7 +137,7 @@ enum type_flags { tf_layout_fixed = 8 /**< Set if the layout of a type is fixed */ }; -/** the structure of a type */ +/** The structure of a type. */ struct ir_type { firm_kind kind; /**< the firm kind, must be k_type */ const tp_op *type_op; /**< the type operation of the type */ -- 2.20.1