verilog_ast_common.h 6.5 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/*!
@file verilog_ast_common.h
@brief Contains Declarations of value-independent data structures like
       linked lists which are used in the ast.
*/

#include "stdarg.h"
#include "stdlib.h"
#include "string.h"

#include "verilog_ast_mem.h"

#ifndef VERILOG_AST_COMMON_H
#define VERILOG_AST_COMMON_H


// --------------- Linked List ------------------------

/*!
@defgroup ast-linked-lists Linked List
@{
@ingroup ast-utility
*/


//! Typedef for the ast_list_element_t
typedef struct ast_list_element_t ast_list_element;

/*!
@brief Storage container for a single element in the linked list.
*/
struct ast_list_element_t{
    ast_list_element * next;
    void             * data;
};


/*!
@brief Container struct for the linked list data structure.
*/
typedef struct ast_list_t {
    ast_list_element *  head;         //!< The "front" of the list.
    ast_list_element *  tail;         //!< The "end" of the list.
    ast_list_element *  walker;       //!< Used to "walk" along the list.
    unsigned int        items;        //!< Number of items in the list.
    unsigned int        current_item; //! Current position of walker in list.
} ast_list;


/*!
@brief Creates and returns a pointer to a new linked list.
*/
ast_list * ast_list_new ();

/*!
@brief Frees the memory of the supplied linked list.
@note Does not free the memory of the data elements in the list, only
the list construct itself.
*/
void       ast_list_free(ast_list * list);

/*!
@brief Adds a new item to the end of a linked list.
*/
void       ast_list_append(ast_list * list, void * data);


/*!
@brief Adds a new item to the front of a linked list.
*/
void       ast_list_preappend(ast_list * list, void * data);

/*!
@brief Finds and returns the i'th item in the linked list.
@details Returns a void* pointer. The programmer must be sure to cast this
as the correct type.
*/
void *    ast_list_get(ast_list * list, unsigned int item);

/*!
@brief Removes the i'th item from a linked list.
*/
void      ast_list_remove_at(ast_list * list, unsigned int i);

/*!
@brief concatenates the two supplied lists into one.
@param head - This will form the "front" of the new list.
@param tail - This will form the "end" of the new list.
@details This function takes all the elements in tail and appends them
to those in head. The tail argument is then released from memory, and the
original head pointer is returned, with all data items still in tact.
*/
ast_list *    ast_list_concat(ast_list * head, ast_list * tail);


/*!
@brief Searches the list, returning true or false if the data item supplied is
contained within it.
@details Performs a *pointer* comparison. That is, if the internal list
pointer has the same address as the supplied data pointer, the item is
considered to be found.
*/
int ast_list_contains(
    ast_list * list,
    void * data
);


/*! @} */

// ----------------------- Stack --------------------------------------

/*!
@defgroup ast-stack Stack
@{
@ingroup ast-utility
*/

//! Typedef for the ast_stack_element_t
typedef struct ast_stack_element_t ast_stack_element;

/*!
@brief Storage container for a single element in the stack
*/
struct ast_stack_element_t{
    ast_stack_element * next;
    void             * data;
};

//! A very simple stack.
typedef struct ast_stack_t{
    unsigned int          depth; //!< How many items are on the stack?
    ast_stack_element   * items; //!< The stack of items.
} ast_stack;

/*!
@brief Creates and returns a new stack object.
*/
ast_stack * ast_stack_new();

/*!
@brief Free the stack, but not it's contents
*/
void ast_stack_free(ast_stack * stack);

/*!
@brief Push a new item to the top of the stack.
@param [inout] stack - The stack to push to.
@param [in]    item  - The thing to push onto the stack.
*/
void ast_stack_push(
    ast_stack * stack,
    void      * item
);

/*!
@brief Pop the top item from the top of the stack.
@param [inout] stack - The stack to pop from.
*/
void * ast_stack_pop(
    ast_stack * stack
);

/*!
@brief Peek at the top item on the top of the stack.
@param [inout] stack - The stack to peek at
*/
void * ast_stack_peek(
    ast_stack * stack
);

/*!
@brief Peek at the item *below* the top item on the top of the stack.
@param [inout] stack - The stack to peek into
*/
void * ast_stack_peek2(
    ast_stack * stack
);

/*! @} */


// ----------------------- Hash Table ---------------------------------


/*!
@defgroup ast-hashtable Hash Table
@{
@ingroup ast-utility
@brief A *very* simple hash table implemented (for now) over a linked list.
@details This can be used for simple key-value pair storage. Current
access time is O(n) for a table with N elements in it.
@warning This is a *terrible* way to implement a hash table. It doesn't
even do any hashing!
@todo Re-implement over a proper hash table structure.
*/

/*! @} */


//! A single element in the hash table.
typedef struct ast_hashtable_element_t{
    char * key; //!< The key for the element.
    void * data;    //!< The data associated with they key.
} ast_hashtable_element;


//! A hash table object.
typedef struct ast_hashtable_t{
    ast_list * elements; //!< The items.
    unsigned int size;   //!< The number of elements in the table.
} ast_hashtable;

typedef enum ast_hashtable_result_e{
    HASH_SUCCESS        = 0,
    HASH_FAIL           = 1,
    HASH_KEY_COLLISION  = 2,
    HASH_KEY_NOT_FOUND  = 3
} ast_hashtable_result;

//! Creates and returns a new hashtable.
ast_hashtable * ast_hashtable_new();

//! Frees an existing hashtable, but not it's contents, only the structure.
void  ast_hashtable_free(
    ast_hashtable * table  //!< The table to free.
);

//! Inserts a new item into the hashtable.
ast_hashtable_result ast_hashtable_insert(
    ast_hashtable * table, //!< The table to insert into.
    char          * key,   //!< The key to insert with.
    void          * value  //!< The data being added.
);

//! Returns an item from the hashtable.
ast_hashtable_result ast_hashtable_get(
    ast_hashtable * table, //!< The table to fetch from.
    char          * key,   //!< The key of the data to fetch.
    void         ** value  //!< [out] The data being returned.
);

//! Removes a key value pair from the hashtable.
ast_hashtable_result ast_hashtable_delete(
    ast_hashtable * table, //!< The table to delete from.
    char          * key    //!< The key to delete.
);

//! Updates an existing item in the hashtable.
ast_hashtable_result ast_hashtable_update(
    ast_hashtable * table, //!< The table to update.
    char          * key,   //!< The key to update with.
    void          * value  //!< The new data item to update.
);

#endif