| 188 | } |
| 189 | |
| 190 | std::string YulUtilFunctions::copyLiteralToStorageFunction(std::string const& _literal) |
| 191 | { |
| 192 | std::string functionName = "copy_literal_to_storage_" + util::toHex(util::keccak256(_literal).asBytes()); |
| 193 | |
| 194 | return m_functionCollector.createFunction(functionName, [&](std::vector<std::string>& _args, std::vector<std::string>&) { |
| 195 | _args = {"slot"}; |
| 196 | |
| 197 | if (_literal.size() >= 32) |
| 198 | { |
| 199 | size_t words = (_literal.length() + 31) / 32; |
| 200 | std::vector<std::map<std::string, std::string>> wordParams(words); |
| 201 | for (size_t i = 0; i < words; ++i) |
| 202 | { |
| 203 | wordParams[i]["offset"] = std::to_string(i); |
| 204 | wordParams[i]["wordValue"] = formatAsStringOrNumber(_literal.substr(32 * i, 32)); |
| 205 | } |
| 206 | return Whiskers(R"( |
| 207 | let oldLen := <byteArrayLength>(sload(slot)) |
| 208 | <cleanUpArrayEnd>(slot, oldLen, <length>) |
| 209 | sstore(slot, <encodedLen>) |
| 210 | let dstPtr := <dataArea>(slot) |
| 211 | <#word> |
| 212 | sstore(add(dstPtr, <offset>), <wordValue>) |
| 213 | </word> |
| 214 | )") |
| 215 | ("byteArrayLength", extractByteArrayLengthFunction()) |
| 216 | ("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage())) |
| 217 | ("dataArea", arrayDataAreaFunction(*TypeProvider::bytesStorage())) |
| 218 | ("word", wordParams) |
| 219 | ("length", std::to_string(_literal.size())) |
| 220 | ("encodedLen", std::to_string(2 * _literal.size() + 1)) |
| 221 | .render(); |
| 222 | } |
| 223 | else |
| 224 | return Whiskers(R"( |
| 225 | let oldLen := <byteArrayLength>(sload(slot)) |
| 226 | <cleanUpArrayEnd>(slot, oldLen, <length>) |
| 227 | sstore(slot, add(<wordValue>, <encodedLen>)) |
| 228 | )") |
| 229 | ("byteArrayLength", extractByteArrayLengthFunction()) |
| 230 | ("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage())) |
| 231 | ("wordValue", formatAsStringOrNumber(_literal)) |
| 232 | ("length", std::to_string(_literal.size())) |
| 233 | ("encodedLen", std::to_string(2 * _literal.size())) |
| 234 | .render(); |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | std::string YulUtilFunctions::revertWithError( |
| 239 | std::string const& _signature, |