From 9f7f62eda3d591eb66db6ddc2c9a646a1a9fd6d1 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 7 Nov 2016 09:59:51 -0800 Subject: 3645 Extract a helper to compute the element type of an array. As a side effect, the hack for disambiguating array:address:number and array:number:3 is now in just one place. --- 032array.cc | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) (limited to '032array.cc') diff --git a/032array.cc b/032array.cc index d2b6c698..7344ffe1 100644 --- a/032array.cc +++ b/032array.cc @@ -104,10 +104,7 @@ if (!r.type->atom && r.type->left->atom && r.type->left->value == get(Type_ordin raise << maybe(current_recipe_name()) << "'" << r.original_string << "' is an array of what?\n" << end(); return 1; } - type_tree* element_type = copy_array_element(r.type); - int result = 1 + array_length(r)*size_of(element_type); - delete element_type; - return result; + return /*space for length*/1 + array_length(r)*size_of(array_element(r.type)); } //: disable the size mismatch check for arrays since the destination array @@ -190,13 +187,8 @@ def foo [ //: make sure we compute container sizes inside arrays :(before "End compute_container_sizes Non-atom Special-cases") -else if (type->left->name == "array") { - const type_tree* element_type = type->right; - // hack: support both array:num:3 and array:address:num - if (!element_type->atom && element_type->right && element_type->right->atom && is_integer(element_type->right->name)) - element_type = element_type->left; - compute_container_sizes(element_type, pending_metadata, location_for_error_messages); -} +else if (type->left->name == "array") + compute_container_sizes(array_element(type), pending_metadata, location_for_error_messages); :(before "End Unit Tests") void test_container_sizes_from_array() { @@ -318,7 +310,7 @@ case INDEX: { if (inst.products.empty()) break; reagent/*copy*/ product = inst.products.at(0); // Update INDEX product in Check - reagent element; + reagent/*local*/ element; element.type = copy_array_element(base.type); if (!types_coercible(product, element)) { raise << maybe(get(Recipe, r).name) << "'index' on '" << base.original_string << "' can't be saved in '" << product.original_string << "'; type should be '" << names_to_string_without_quotes(element.type) << "'\n" << end(); @@ -343,13 +335,11 @@ case INDEX: { raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } - type_tree* element_type = copy_array_element(base.type); - int src = base_address + 1 + index_val.at(0)*size_of(element_type); - trace(9998, "run") << "address to copy is " << src << end(); - trace(9998, "run") << "its type is " << to_string(element_type) << end(); - reagent element; - element.set_value(src); - element.type = element_type; + reagent/*local*/ element; + element.type = copy_array_element(base.type); + element.set_value(base_address + /*skip length*/1 + index_val.at(0)*size_of(element.type)); + trace(9998, "run") << "address to copy is " << element.value << end(); + trace(9998, "run") << "its type is " << to_string(element.type) << end(); // Read element products.push_back(read_memory(element)); break; @@ -357,11 +347,15 @@ case INDEX: { :(code) type_tree* copy_array_element(const type_tree* type) { + return new type_tree(*array_element(type)); +} + +type_tree* array_element(const type_tree* type) { assert(type->right); // hack: don't require parens for either array:num:3 array:address:num if (!type->right->atom && type->right->right && type->right->right->atom && is_integer(type->right->right->name)) - return new type_tree(*type->right->left); - return new type_tree(*type->right); + return type->right->left; + return type->right; } int array_length(const reagent& x) { @@ -486,7 +480,7 @@ case PUT_INDEX: { } reagent/*copy*/ value = inst.ingredients.at(2); // Update PUT_INDEX value in Check - reagent element; + reagent/*local*/ element; element.type = copy_array_element(base.type); if (!types_coercible(element, value)) { raise << maybe(get(Recipe, r).name) << "'put-index " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should store " << names_to_string_without_quotes(element.type) << " but '" << value.name << "' has type " << names_to_string_without_quotes(value.type) << '\n' << end(); @@ -516,10 +510,7 @@ case PUT_INDEX: { raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } - reagent element; - element.type = copy_array_element(base.type); - int address = base_address + 1 + index_val.at(0)*size_of(element.type); - element.set_value(address); + int address = base_address + /*skip length*/1 + index_val.at(0)*size_of(array_element(base.type)); trace(9998, "run") << "address to copy to is " << address << end(); // optimization: directly write the element rather than updating 'product' // and writing the entire array -- cgit 1.4.1-2-gfad0 n64'>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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425