| 1 | /** |
| 2 | * @file pm_constant_pool.h |
| 3 | * |
| 4 | * A data structure that stores a set of strings. |
| 5 | * |
| 6 | * Each string is assigned a unique id, which can be used to compare strings for |
| 7 | * equality. This comparison ends up being much faster than strcmp, since it |
| 8 | * only requires a single integer comparison. |
| 9 | */ |
| 10 | #ifndef PRISM_CONSTANT_POOL_H |
| 11 | #define PRISM_CONSTANT_POOL_Hcxx . . $ `include/prism/util/pm_constant_pool.h:11:9`! |
| 12 | |
| 13 | #include "prism/defines.h"cxx . . $ `<file>/include/prism/defines.h`/ |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | /** |
| 22 | * When we allocate constants into the pool, we reserve 0 to mean that the slot |
| 23 | * is not yet filled. This constant is reused in other places to indicate the |
| 24 | * lack of a constant id. |
| 25 | */ |
| 26 | #define PM_CONSTANT_ID_UNSETcxx . . $ `include/prism/util/pm_constant_pool.h:26:9`! 0 |
| 27 | |
| 28 | /** |
| 29 | * A constant id is a unique identifier for a constant in the constant pool. |
| 30 | */ |
| 31 | typedef uint32_tcxx . . $ uint32_t# pm_constant_id_tcxx . . $ pm_constant_id_t#; |
| 32 | |
| 33 | /** |
| 34 | * A list of constant IDs. Usually used to represent a set of locals. |
| 35 | */ |
| 36 | typedef structcxx . . $ $anonymous_type_13cf0b34c7f15a1b_0# { |
| 37 | /** The number of constant ids in the list. */ |
| 38 | size_t sizecxx . . $ $anonymous_type_13cf0b34c7f15a1b_0#size.; |
| 39 | |
| 40 | /** The number of constant ids that have been allocated in the list. */ |
| 41 | size_t capacitycxx . . $ $anonymous_type_13cf0b34c7f15a1b_0#capacity.; |
| 42 | |
| 43 | /** The constant ids in the list. */ |
| 44 | pm_constant_id_tcxx . . $ pm_constant_id_t# *idscxx . . $ $anonymous_type_13cf0b34c7f15a1b_0#ids.; |
| 45 | } pm_constant_id_list_tcxx . . $ pm_constant_id_list_t#; |
| 46 | |
| 47 | /** |
| 48 | * Initialize a list of constant ids. |
| 49 | * |
| 50 | * @param list The list to initialize. |
| 51 | */ |
| 52 | void pm_constant_id_list_initcxx . . $ pm_constant_id_list_init(29dd568c180bfcb).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list); |
| 53 | |
| 54 | /** |
| 55 | * Initialize a list of constant ids with a given capacity. |
| 56 | * |
| 57 | * @param list The list to initialize. |
| 58 | * @param capacity The initial capacity of the list. |
| 59 | */ |
| 60 | void pm_constant_id_list_init_capacitycxx . . $ pm_constant_id_list_init_capacity(dd333c89e3e5ae76).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list, size_t capacity); |
| 61 | |
| 62 | /** |
| 63 | * Append a constant id to a list of constant ids. Returns false if any |
| 64 | * potential reallocations fail. |
| 65 | * |
| 66 | * @param list The list to append to. |
| 67 | * @param id The id to append. |
| 68 | * @return Whether the append succeeded. |
| 69 | */ |
| 70 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! pm_constant_id_list_appendcxx . . $ pm_constant_id_list_append(44ebcd91f774aab0).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list, pm_constant_id_tcxx . . $ pm_constant_id_t# id); |
| 71 | |
| 72 | /** |
| 73 | * Insert a constant id into a list of constant ids at the specified index. |
| 74 | * |
| 75 | * @param list The list to insert into. |
| 76 | * @param index The index at which to insert. |
| 77 | * @param id The id to insert. |
| 78 | */ |
| 79 | void pm_constant_id_list_insertcxx . . $ pm_constant_id_list_insert(2d113c4da006c8e3).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list, size_t index, pm_constant_id_tcxx . . $ pm_constant_id_t# id); |
| 80 | |
| 81 | /** |
| 82 | * Checks if the current constant id list includes the given constant id. |
| 83 | * |
| 84 | * @param list The list to check. |
| 85 | * @param id The id to check for. |
| 86 | * @return Whether the list includes the given id. |
| 87 | */ |
| 88 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! pm_constant_id_list_includescxx . . $ pm_constant_id_list_includes(44ebcd91f774aab0).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list, pm_constant_id_tcxx . . $ pm_constant_id_t# id); |
| 89 | |
| 90 | /** |
| 91 | * Free the memory associated with a list of constant ids. |
| 92 | * |
| 93 | * @param list The list to free. |
| 94 | */ |
| 95 | void pm_constant_id_list_freecxx . . $ pm_constant_id_list_free(29dd568c180bfcb).(pm_constant_id_list_tcxx . . $ pm_constant_id_list_t# *list); |
| 96 | |
| 97 | /** |
| 98 | * The type of bucket in the constant pool hash map. This determines how the |
| 99 | * bucket should be freed. |
| 100 | */ |
| 101 | typedef unsigned int pm_constant_pool_bucket_type_tcxx . . $ pm_constant_pool_bucket_type_t#; |
| 102 | |
| 103 | /** By default, each constant is a slice of the source. */ |
| 104 | static const pm_constant_pool_bucket_type_tcxx . . $ pm_constant_pool_bucket_type_t# PM_CONSTANT_POOL_BUCKET_DEFAULTcxx . . $ PM_CONSTANT_POOL_BUCKET_DEFAULT. = 0; |
| 105 | |
| 106 | /** An owned constant is one for which memory has been allocated. */ |
| 107 | static const pm_constant_pool_bucket_type_tcxx . . $ pm_constant_pool_bucket_type_t# PM_CONSTANT_POOL_BUCKET_OWNEDcxx . . $ PM_CONSTANT_POOL_BUCKET_OWNED. = 1; |
| 108 | |
| 109 | /** A constant constant is known at compile time. */ |
| 110 | static const pm_constant_pool_bucket_type_tcxx . . $ pm_constant_pool_bucket_type_t# PM_CONSTANT_POOL_BUCKET_CONSTANTcxx . . $ PM_CONSTANT_POOL_BUCKET_CONSTANT. = 2; |
| 111 | |
| 112 | /** A bucket in the hash map. */ |
| 113 | typedef structcxx . . $ $anonymous_type_13cf0b34c7f15a1b_1# { |
| 114 | /** The incremental ID used for indexing back into the pool. */ |
| 115 | unsigned int idcxx . . $ $anonymous_type_13cf0b34c7f15a1b_1#id.: 30; |
| 116 | |
| 117 | /** The type of the bucket, which determines how to free it. */ |
| 118 | pm_constant_pool_bucket_type_tcxx . . $ pm_constant_pool_bucket_type_t# typecxx . . $ $anonymous_type_13cf0b34c7f15a1b_1#type.: 2; |
| 119 | |
| 120 | /** The hash of the bucket. */ |
| 121 | uint32_tcxx . . $ uint32_t# hashcxx . . $ $anonymous_type_13cf0b34c7f15a1b_1#hash.; |
| 122 | } pm_constant_pool_bucket_tcxx . . $ pm_constant_pool_bucket_t#; |
| 123 | |
| 124 | /** A constant in the pool which effectively stores a string. */ |
| 125 | typedef structcxx . . $ $anonymous_type_13cf0b34c7f15a1b_2# { |
| 126 | /** A pointer to the start of the string. */ |
| 127 | const uint8_tcxx . . $ uint8_t# *startcxx . . $ $anonymous_type_13cf0b34c7f15a1b_2#start.; |
| 128 | |
| 129 | /** The length of the string. */ |
| 130 | size_t lengthcxx . . $ $anonymous_type_13cf0b34c7f15a1b_2#length.; |
| 131 | } pm_constant_tcxx . . $ pm_constant_t#; |
| 132 | |
| 133 | /** The overall constant pool, which stores constants found while parsing. */ |
| 134 | typedef structcxx . . $ $anonymous_type_13cf0b34c7f15a1b_3# { |
| 135 | /** The buckets in the hash map. */ |
| 136 | pm_constant_pool_bucket_tcxx . . $ pm_constant_pool_bucket_t# *bucketscxx . . $ $anonymous_type_13cf0b34c7f15a1b_3#buckets.; |
| 137 | |
| 138 | /** The constants that are stored in the buckets. */ |
| 139 | pm_constant_tcxx . . $ pm_constant_t# *constantscxx . . $ $anonymous_type_13cf0b34c7f15a1b_3#constants.; |
| 140 | |
| 141 | /** The number of buckets in the hash map. */ |
| 142 | uint32_tcxx . . $ uint32_t# sizecxx . . $ $anonymous_type_13cf0b34c7f15a1b_3#size.; |
| 143 | |
| 144 | /** The number of buckets that have been allocated in the hash map. */ |
| 145 | uint32_tcxx . . $ uint32_t# capacitycxx . . $ $anonymous_type_13cf0b34c7f15a1b_3#capacity.; |
| 146 | } pm_constant_pool_tcxx . . $ pm_constant_pool_t#; |
| 147 | |
| 148 | /** |
| 149 | * Initialize a new constant pool with a given capacity. |
| 150 | * |
| 151 | * @param pool The pool to initialize. |
| 152 | * @param capacity The initial capacity of the pool. |
| 153 | * @return Whether the initialization succeeded. |
| 154 | */ |
| 155 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! pm_constant_pool_initcxx . . $ pm_constant_pool_init(623db788e4f57937).(pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, uint32_tcxx . . $ uint32_t# capacity); |
| 156 | |
| 157 | /** |
| 158 | * Return a pointer to the constant indicated by the given constant id. |
| 159 | * |
| 160 | * @param pool The pool to get the constant from. |
| 161 | * @param constant_id The id of the constant to get. |
| 162 | * @return A pointer to the constant. |
| 163 | */ |
| 164 | pm_constant_tcxx . . $ pm_constant_t# * pm_constant_pool_id_to_constantcxx . . $ pm_constant_pool_id_to_constant(152889a4d6977c9f).(const pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, pm_constant_id_tcxx . . $ pm_constant_id_t# constant_id); |
| 165 | |
| 166 | /** |
| 167 | * Find a constant in a constant pool. Returns the id of the constant, or 0 if |
| 168 | * the constant is not found. |
| 169 | * |
| 170 | * @param pool The pool to find the constant in. |
| 171 | * @param start A pointer to the start of the constant. |
| 172 | * @param length The length of the constant. |
| 173 | * @return The id of the constant. |
| 174 | */ |
| 175 | pm_constant_id_tcxx . . $ pm_constant_id_t# pm_constant_pool_findcxx . . $ pm_constant_pool_find(c5d2d6ccac7c7781).(const pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, const uint8_tcxx . . $ uint8_t# *start, size_t length); |
| 176 | |
| 177 | /** |
| 178 | * Insert a constant into a constant pool that is a slice of a source string. |
| 179 | * Returns the id of the constant, or 0 if any potential calls to resize fail. |
| 180 | * |
| 181 | * @param pool The pool to insert the constant into. |
| 182 | * @param start A pointer to the start of the constant. |
| 183 | * @param length The length of the constant. |
| 184 | * @return The id of the constant. |
| 185 | */ |
| 186 | pm_constant_id_tcxx . . $ pm_constant_id_t# pm_constant_pool_insert_sharedcxx . . $ pm_constant_pool_insert_shared(9c21faf08e05bda3).(pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, const uint8_tcxx . . $ uint8_t# *start, size_t length); |
| 187 | |
| 188 | /** |
| 189 | * Insert a constant into a constant pool from memory that is now owned by the |
| 190 | * constant pool. Returns the id of the constant, or 0 if any potential calls to |
| 191 | * resize fail. |
| 192 | * |
| 193 | * @param pool The pool to insert the constant into. |
| 194 | * @param start A pointer to the start of the constant. |
| 195 | * @param length The length of the constant. |
| 196 | * @return The id of the constant. |
| 197 | */ |
| 198 | pm_constant_id_tcxx . . $ pm_constant_id_t# pm_constant_pool_insert_ownedcxx . . $ pm_constant_pool_insert_owned(488f2ad32ee21d44).(pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, uint8_tcxx . . $ uint8_t# *start, size_t length); |
| 199 | |
| 200 | /** |
| 201 | * Insert a constant into a constant pool from memory that is constant. Returns |
| 202 | * the id of the constant, or 0 if any potential calls to resize fail. |
| 203 | * |
| 204 | * @param pool The pool to insert the constant into. |
| 205 | * @param start A pointer to the start of the constant. |
| 206 | * @param length The length of the constant. |
| 207 | * @return The id of the constant. |
| 208 | */ |
| 209 | pm_constant_id_tcxx . . $ pm_constant_id_t# pm_constant_pool_insert_constantcxx . . $ pm_constant_pool_insert_constant(9c21faf08e05bda3).(pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool, const uint8_tcxx . . $ uint8_t# *start, size_t length); |
| 210 | |
| 211 | /** |
| 212 | * Free the memory associated with a constant pool. |
| 213 | * |
| 214 | * @param pool The pool to free. |
| 215 | */ |
| 216 | void pm_constant_pool_freecxx . . $ pm_constant_pool_free(7fc013cdbc0a409a).(pm_constant_pool_tcxx . . $ pm_constant_pool_t# *pool); |
| 217 | |
| 218 | #endif |