| 2420 | } |
| 2421 | |
| 2422 | std::string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type) |
| 2423 | { |
| 2424 | std::string functionName = "storage_array_index_access_" + _type.identifier(); |
| 2425 | return m_functionCollector.createFunction(functionName, [&]() { |
| 2426 | return Whiskers(R"( |
| 2427 | function <functionName>(array, index) -> slot, offset { |
| 2428 | let arrayLength := <arrayLen>(array) |
| 2429 | if iszero(lt(index, arrayLength)) { <panic>() } |
| 2430 | |
| 2431 | <?multipleItemsPerSlot> |
| 2432 | <?isBytesArray> |
| 2433 | switch lt(arrayLength, 0x20) |
| 2434 | case 0 { |
| 2435 | slot, offset := <indexAccessNoChecks>(array, index) |
| 2436 | } |
| 2437 | default { |
| 2438 | offset := sub(31, mod(index, 0x20)) |
| 2439 | slot := array |
| 2440 | } |
| 2441 | <!isBytesArray> |
| 2442 | let dataArea := <dataAreaFunc>(array) |
| 2443 | slot := add(dataArea, div(index, <itemsPerSlot>)) |
| 2444 | offset := mul(mod(index, <itemsPerSlot>), <storageBytes>) |
| 2445 | </isBytesArray> |
| 2446 | <!multipleItemsPerSlot> |
| 2447 | let dataArea := <dataAreaFunc>(array) |
| 2448 | slot := add(dataArea, mul(index, <storageSize>)) |
| 2449 | offset := 0 |
| 2450 | </multipleItemsPerSlot> |
| 2451 | } |
| 2452 | )") |
| 2453 | ("functionName", functionName) |
| 2454 | ("panic", panicFunction(PanicCode::ArrayOutOfBounds)) |
| 2455 | ("arrayLen", arrayLengthFunction(_type)) |
| 2456 | ("dataAreaFunc", arrayDataAreaFunction(_type)) |
| 2457 | ("indexAccessNoChecks", longByteArrayStorageIndexAccessNoCheckFunction()) |
| 2458 | ("multipleItemsPerSlot", _type.baseType()->storageBytes() <= 16) |
| 2459 | ("isBytesArray", _type.isByteArrayOrString()) |
| 2460 | ("storageSize", _type.baseType()->storageSize().str()) |
| 2461 | ("storageBytes", toString(_type.baseType()->storageBytes())) |
| 2462 | ("itemsPerSlot", std::to_string(32 / _type.baseType()->storageBytes())) |
| 2463 | .render(); |
| 2464 | }); |
| 2465 | } |
| 2466 | |
| 2467 | std::string YulUtilFunctions::memoryArrayIndexAccessFunction(ArrayType const& _type) |
| 2468 | { |
no test coverage detected